debugfs_smc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2019, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <lib/debugfs.h>
  10. #include <lib/smccc.h>
  11. #include <lib/spinlock.h>
  12. #include <lib/xlat_tables/xlat_tables_v2.h>
  13. #include <smccc_helpers.h>
  14. #define MAX_PATH_LEN 256
  15. #define MOUNT 0
  16. #define CREATE 1
  17. #define OPEN 2
  18. #define CLOSE 3
  19. #define READ 4
  20. #define WRITE 5
  21. #define SEEK 6
  22. #define BIND 7
  23. #define STAT 8
  24. #define INIT 10
  25. #define VERSION 11
  26. /* This is the virtual address to which we map the NS shared buffer */
  27. #define DEBUGFS_SHARED_BUF_VIRT ((void *)0x81000000U)
  28. static union debugfs_parms {
  29. struct {
  30. char fname[MAX_PATH_LEN];
  31. } open;
  32. struct {
  33. char srv[MAX_PATH_LEN];
  34. char where[MAX_PATH_LEN];
  35. char spec[MAX_PATH_LEN];
  36. } mount;
  37. struct {
  38. char path[MAX_PATH_LEN];
  39. dir_t dir;
  40. } stat;
  41. struct {
  42. char oldpath[MAX_PATH_LEN];
  43. char newpath[MAX_PATH_LEN];
  44. } bind;
  45. } parms;
  46. /* debugfs_access_lock protects shared buffer and internal */
  47. /* FS functions from concurrent accesses. */
  48. static spinlock_t debugfs_access_lock;
  49. static bool debugfs_initialized;
  50. uintptr_t debugfs_smc_handler(unsigned int smc_fid,
  51. u_register_t cmd,
  52. u_register_t arg2,
  53. u_register_t arg3,
  54. u_register_t arg4,
  55. void *cookie,
  56. void *handle,
  57. u_register_t flags)
  58. {
  59. int64_t smc_ret = DEBUGFS_E_INVALID_PARAMS, smc_resp = 0;
  60. int ret;
  61. /* Allow calls from non-secure only */
  62. if (is_caller_secure(flags)) {
  63. SMC_RET1(handle, DEBUGFS_E_DENIED);
  64. }
  65. /* Expect a SiP service fast call */
  66. if ((GET_SMC_TYPE(smc_fid) != SMC_TYPE_FAST) ||
  67. (GET_SMC_OEN(smc_fid) != OEN_SIP_START)) {
  68. SMC_RET1(handle, SMC_UNK);
  69. }
  70. /* Truncate parameters if 32b SMC convention call */
  71. if (GET_SMC_CC(smc_fid) == SMC_32) {
  72. arg2 &= 0xffffffff;
  73. arg3 &= 0xffffffff;
  74. arg4 &= 0xffffffff;
  75. }
  76. spin_lock(&debugfs_access_lock);
  77. if (debugfs_initialized == true) {
  78. /* Copy NS shared buffer to internal secure location */
  79. memcpy(&parms, (void *)DEBUGFS_SHARED_BUF_VIRT,
  80. sizeof(union debugfs_parms));
  81. }
  82. switch (cmd) {
  83. case INIT:
  84. if (debugfs_initialized == false) {
  85. /* TODO: check PA validity e.g. whether */
  86. /* it is an NS region. */
  87. ret = mmap_add_dynamic_region(arg2,
  88. (uintptr_t)DEBUGFS_SHARED_BUF_VIRT,
  89. PAGE_SIZE_4KB,
  90. MT_MEMORY | MT_RW | MT_NS);
  91. if (ret == 0) {
  92. debugfs_initialized = true;
  93. smc_ret = SMC_OK;
  94. smc_resp = 0;
  95. }
  96. }
  97. break;
  98. case VERSION:
  99. smc_ret = SMC_OK;
  100. smc_resp = DEBUGFS_VERSION;
  101. break;
  102. case MOUNT:
  103. ret = mount(parms.mount.srv,
  104. parms.mount.where,
  105. parms.mount.spec);
  106. if (ret == 0) {
  107. smc_ret = SMC_OK;
  108. smc_resp = 0;
  109. }
  110. break;
  111. case OPEN:
  112. ret = open(parms.open.fname, arg2);
  113. if (ret >= 0) {
  114. smc_ret = SMC_OK;
  115. smc_resp = ret;
  116. }
  117. break;
  118. case CLOSE:
  119. ret = close(arg2);
  120. if (ret == 0) {
  121. smc_ret = SMC_OK;
  122. smc_resp = 0;
  123. }
  124. break;
  125. case READ:
  126. ret = read(arg2, DEBUGFS_SHARED_BUF_VIRT, arg3);
  127. if (ret >= 0) {
  128. smc_ret = SMC_OK;
  129. smc_resp = ret;
  130. }
  131. break;
  132. case SEEK:
  133. ret = seek(arg2, arg3, arg4);
  134. if (ret == 0) {
  135. smc_ret = SMC_OK;
  136. smc_resp = 0;
  137. }
  138. break;
  139. case BIND:
  140. ret = bind(parms.bind.oldpath, parms.bind.newpath);
  141. if (ret == 0) {
  142. smc_ret = SMC_OK;
  143. smc_resp = 0;
  144. }
  145. break;
  146. case STAT:
  147. ret = stat(parms.stat.path, &parms.stat.dir);
  148. if (ret == 0) {
  149. memcpy((void *)DEBUGFS_SHARED_BUF_VIRT, &parms,
  150. sizeof(union debugfs_parms));
  151. smc_ret = SMC_OK;
  152. smc_resp = 0;
  153. }
  154. break;
  155. /* Not implemented */
  156. case CREATE:
  157. /* Intentional fall-through */
  158. /* Not implemented */
  159. case WRITE:
  160. /* Intentional fall-through */
  161. default:
  162. smc_ret = SMC_UNK;
  163. smc_resp = 0;
  164. }
  165. spin_unlock(&debugfs_access_lock);
  166. SMC_RET2(handle, smc_ret, smc_resp);
  167. /* Not reached */
  168. return smc_ret;
  169. }
  170. int debugfs_smc_setup(void)
  171. {
  172. debugfs_initialized = false;
  173. debugfs_access_lock.lock = 0;
  174. return 0;
  175. }