pncd_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*******************************************************************************
  7. * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
  8. * plug-in component to the Secure Monitor, registered as a runtime service. The
  9. * SPD is expected to be a functional extension of the Secure Payload (SP) that
  10. * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
  11. * the Trusted OS/Applications range to the dispatcher. The SPD will either
  12. * handle the request locally or delegate it to the Secure Payload. It is also
  13. * responsible for initialising and maintaining communication with the SP.
  14. ******************************************************************************/
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <arch_helpers.h>
  20. #include <bl31/bl31.h>
  21. #include <bl31/interrupt_mgmt.h>
  22. #include <bl_common.h>
  23. #include <common/debug.h>
  24. #include <common/ep_info.h>
  25. #include <drivers/arm/gic_common.h>
  26. #include <lib/el3_runtime/context_mgmt.h>
  27. #include <lib/spinlock.h>
  28. #include <plat/common/platform.h>
  29. #include <pnc.h>
  30. #include "pncd_private.h"
  31. #include <runtime_svc.h>
  32. #include <tools_share/uuid.h>
  33. /*******************************************************************************
  34. * Structure to keep track of ProvenCore state
  35. ******************************************************************************/
  36. static pnc_context_t pncd_sp_context;
  37. static bool ree_info;
  38. static uint64_t ree_base_addr;
  39. static uint64_t ree_length;
  40. static uint64_t ree_tag;
  41. static bool pnc_initialized;
  42. static spinlock_t smc_handler_lock;
  43. static int pncd_init(void);
  44. static void context_save(unsigned long security_state)
  45. {
  46. assert(sec_state_is_valid(security_state));
  47. cm_el1_sysregs_context_save((uint32_t) security_state);
  48. #if CTX_INCLUDE_FPREGS
  49. simd_ctx_save((uint32_t)security_state, false);
  50. #endif
  51. }
  52. static void *context_restore(unsigned long security_state)
  53. {
  54. void *handle;
  55. assert(sec_state_is_valid(security_state));
  56. /* Get a reference to the next context */
  57. handle = cm_get_context((uint32_t) security_state);
  58. assert(handle);
  59. /* Restore state */
  60. cm_el1_sysregs_context_restore((uint32_t) security_state);
  61. #if CTX_INCLUDE_FPREGS
  62. simd_ctx_restore((uint32_t)security_state);
  63. #endif
  64. cm_set_next_eret_context((uint32_t) security_state);
  65. return handle;
  66. }
  67. static uint64_t pncd_sel1_interrupt_handler(uint32_t id,
  68. uint32_t flags, void *handle, void *cookie);
  69. /*******************************************************************************
  70. * Switch context to the specified security state and return the targeted
  71. * handle. Note that the context may remain unchanged if the switch is not
  72. * allowed.
  73. ******************************************************************************/
  74. void *pncd_context_switch_to(unsigned long security_state)
  75. {
  76. unsigned long sec_state_from =
  77. security_state == SECURE ? NON_SECURE : SECURE;
  78. assert(sec_state_is_valid(security_state));
  79. /* Check if this is the first world switch */
  80. if (!pnc_initialized) {
  81. int rc;
  82. uint32_t flags;
  83. assert(sec_state_from == SECURE);
  84. INFO("PnC initialization done\n");
  85. /*
  86. * Register an interrupt handler for S-EL1 interrupts
  87. * when generated during code executing in the
  88. * non-secure state.
  89. */
  90. flags = 0U;
  91. set_interrupt_rm_flag(flags, NON_SECURE);
  92. rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
  93. pncd_sel1_interrupt_handler,
  94. flags);
  95. if (rc != 0) {
  96. ERROR("Failed to register S-EL1 interrupt handler (%d)\n",
  97. rc);
  98. panic();
  99. }
  100. context_save(SECURE);
  101. pnc_initialized = true;
  102. /*
  103. * Release the lock before restoring the EL3 context to
  104. * bl31_main.
  105. */
  106. spin_unlock(&smc_handler_lock);
  107. /*
  108. * SP reports completion. The SPD must have initiated
  109. * the original request through a synchronous entry
  110. * into the SP. Jump back to the original C runtime
  111. * context.
  112. */
  113. pncd_synchronous_sp_exit(&pncd_sp_context, (uint64_t) 0x0);
  114. /* Unreachable */
  115. ERROR("Returned from pncd_synchronous_sp_exit... Should not happen\n");
  116. panic();
  117. }
  118. /* Check that the world switch is allowed */
  119. if (read_mpidr() != pncd_sp_context.mpidr) {
  120. if (sec_state_from == SECURE) {
  121. /*
  122. * Secure -> Non-Secure world switch initiated on a CPU where there
  123. * should be no Trusted OS running
  124. */
  125. WARN("Secure to Non-Secure switch requested on CPU where ProvenCore is not supposed to be running...\n");
  126. }
  127. /*
  128. * Secure or Non-Secure world wants to switch world but there is no Secure
  129. * software on this core
  130. */
  131. return cm_get_context((uint32_t) sec_state_from);
  132. }
  133. context_save(sec_state_from);
  134. return context_restore(security_state);
  135. }
  136. /*******************************************************************************
  137. * This function is the handler registered for S-EL1 interrupts by the PNCD. It
  138. * validates the interrupt and upon success arranges entry into the PNC at
  139. * 'pnc_sel1_intr_entry()' for handling the interrupt.
  140. ******************************************************************************/
  141. static uint64_t pncd_sel1_interrupt_handler(uint32_t id,
  142. uint32_t flags,
  143. void *handle,
  144. void *cookie)
  145. {
  146. /* Check the security state when the exception was generated */
  147. assert(get_interrupt_src_ss(flags) == NON_SECURE);
  148. /* Sanity check the pointer to this cpu's context */
  149. assert(handle == cm_get_context(NON_SECURE));
  150. /* switch to PnC */
  151. handle = pncd_context_switch_to(SECURE);
  152. assert(handle != NULL);
  153. SMC_RET0(handle);
  154. }
  155. #pragma weak plat_pncd_setup
  156. int plat_pncd_setup(void)
  157. {
  158. return 0;
  159. }
  160. /*******************************************************************************
  161. * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
  162. * (aarch32/aarch64) if not already known and initialises the context for entry
  163. * into the SP for its initialisation.
  164. ******************************************************************************/
  165. static int pncd_setup(void)
  166. {
  167. entry_point_info_t *pnc_ep_info;
  168. /*
  169. * Get information about the Secure Payload (BL32) image. Its
  170. * absence is a critical failure.
  171. *
  172. * TODO: Add support to conditionally include the SPD service
  173. */
  174. pnc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
  175. if (!pnc_ep_info) {
  176. WARN("No PNC provided by BL2 boot loader, Booting device without PNC initialization. SMC`s destined for PNC will return SMC_UNK\n");
  177. return 1;
  178. }
  179. /*
  180. * If there's no valid entry point for SP, we return a non-zero value
  181. * signalling failure initializing the service. We bail out without
  182. * registering any handlers
  183. */
  184. if (!pnc_ep_info->pc) {
  185. return 1;
  186. }
  187. pncd_init_pnc_ep_state(pnc_ep_info,
  188. pnc_ep_info->pc,
  189. &pncd_sp_context);
  190. /*
  191. * All PNCD initialization done. Now register our init function with
  192. * BL31 for deferred invocation
  193. */
  194. bl31_register_bl32_init(&pncd_init);
  195. bl31_set_next_image_type(NON_SECURE);
  196. return plat_pncd_setup();
  197. }
  198. /*******************************************************************************
  199. * This function passes control to the Secure Payload image (BL32) for the first
  200. * time on the primary cpu after a cold boot. It assumes that a valid secure
  201. * context has already been created by pncd_setup() which can be directly used.
  202. * It also assumes that a valid non-secure context has been initialised by PSCI
  203. * so it does not need to save and restore any non-secure state. This function
  204. * performs a synchronous entry into the Secure payload. The SP passes control
  205. * back to this routine through a SMC.
  206. ******************************************************************************/
  207. static int32_t pncd_init(void)
  208. {
  209. entry_point_info_t *pnc_entry_point;
  210. uint64_t rc = 0;
  211. /*
  212. * Get information about the Secure Payload (BL32) image. Its
  213. * absence is a critical failure.
  214. */
  215. pnc_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
  216. assert(pnc_entry_point);
  217. cm_init_my_context(pnc_entry_point);
  218. /*
  219. * Arrange for an entry into the test secure payload. It will be
  220. * returned via PNC_ENTRY_DONE case
  221. */
  222. rc = pncd_synchronous_sp_entry(&pncd_sp_context);
  223. /*
  224. * If everything went well at this point, the return value should be 0.
  225. */
  226. return rc == 0;
  227. }
  228. #pragma weak plat_pncd_smc_handler
  229. /*******************************************************************************
  230. * This function is responsible for handling the platform-specific SMCs in the
  231. * Trusted OS/App range as defined in the SMC Calling Convention Document.
  232. ******************************************************************************/
  233. uintptr_t plat_pncd_smc_handler(uint32_t smc_fid,
  234. u_register_t x1,
  235. u_register_t x2,
  236. u_register_t x3,
  237. u_register_t x4,
  238. void *cookie,
  239. void *handle,
  240. u_register_t flags)
  241. {
  242. (void) smc_fid;
  243. (void) x1;
  244. (void) x2;
  245. (void) x3;
  246. (void) x4;
  247. (void) cookie;
  248. (void) flags;
  249. SMC_RET1(handle, SMC_UNK);
  250. }
  251. /*******************************************************************************
  252. * This function is responsible for handling all SMCs in the Trusted OS/App
  253. * range as defined in the SMC Calling Convention Document. It is also
  254. * responsible for communicating with the Secure payload to delegate work and
  255. * return results back to the non-secure state. Lastly it will also return any
  256. * information that the secure payload needs to do the work assigned to it.
  257. *
  258. * It should only be called with the smc_handler_lock held.
  259. ******************************************************************************/
  260. static uintptr_t pncd_smc_handler_unsafe(uint32_t smc_fid,
  261. u_register_t x1,
  262. u_register_t x2,
  263. u_register_t x3,
  264. u_register_t x4,
  265. void *cookie,
  266. void *handle,
  267. u_register_t flags)
  268. {
  269. uint32_t ns;
  270. /* Determine which security state this SMC originated from */
  271. ns = is_caller_non_secure(flags);
  272. assert(ns != 0 || read_mpidr() == pncd_sp_context.mpidr);
  273. switch (smc_fid) {
  274. case SMC_CONFIG_SHAREDMEM:
  275. if (ree_info) {
  276. /* Do not Yield */
  277. SMC_RET0(handle);
  278. }
  279. /*
  280. * Fetch the physical base address (x1) and size (x2) of the
  281. * shared memory allocated by the Non-Secure world. This memory
  282. * will be used by PNC to communicate with the Non-Secure world.
  283. * Verifying the validity of these values is up to the Trusted
  284. * OS.
  285. */
  286. ree_base_addr = x1 | (x2 << 32);
  287. ree_length = x3;
  288. ree_tag = x4;
  289. INFO("IN SMC_CONFIG_SHAREDMEM: addr=%lx, length=%lx, tag=%lx\n",
  290. (unsigned long) ree_base_addr,
  291. (unsigned long) ree_length,
  292. (unsigned long) ree_tag);
  293. if ((ree_base_addr % 0x200000) != 0) {
  294. SMC_RET1(handle, SMC_UNK);
  295. }
  296. if ((ree_length % 0x200000) != 0) {
  297. SMC_RET1(handle, SMC_UNK);
  298. }
  299. ree_info = true;
  300. /* Do not Yield */
  301. SMC_RET4(handle, 0, 0, 0, 0);
  302. break;
  303. case SMC_GET_SHAREDMEM:
  304. if (ree_info) {
  305. x1 = (1U << 16) | ree_tag;
  306. x2 = ree_base_addr & 0xFFFFFFFF;
  307. x3 = (ree_base_addr >> 32) & 0xFFFFFFFF;
  308. x4 = ree_length & 0xFFFFFFFF;
  309. SMC_RET4(handle, x1, x2, x3, x4);
  310. } else {
  311. SMC_RET4(handle, 0, 0, 0, 0);
  312. }
  313. break;
  314. case SMC_ACTION_FROM_NS:
  315. if (ns == 0) {
  316. SMC_RET1(handle, SMC_UNK);
  317. }
  318. if (SPD_PNCD_S_IRQ < MIN_PPI_ID) {
  319. plat_ic_raise_s_el1_sgi(SPD_PNCD_S_IRQ,
  320. pncd_sp_context.mpidr);
  321. } else {
  322. plat_ic_set_interrupt_pending(SPD_PNCD_S_IRQ);
  323. }
  324. SMC_RET0(handle);
  325. break;
  326. case SMC_ACTION_FROM_S:
  327. if (ns != 0) {
  328. SMC_RET1(handle, SMC_UNK);
  329. }
  330. if (SPD_PNCD_NS_IRQ < MIN_PPI_ID) {
  331. /*
  332. * NS SGI is sent to the same core as the one running
  333. * PNC
  334. */
  335. plat_ic_raise_ns_sgi(SPD_PNCD_NS_IRQ, read_mpidr());
  336. } else {
  337. plat_ic_set_interrupt_pending(SPD_PNCD_NS_IRQ);
  338. }
  339. SMC_RET0(handle);
  340. break;
  341. case SMC_YIELD:
  342. assert(handle == cm_get_context(ns != 0 ? NON_SECURE : SECURE));
  343. handle = pncd_context_switch_to(ns != 0 ? SECURE : NON_SECURE);
  344. assert(handle != NULL);
  345. SMC_RET0(handle);
  346. break;
  347. default:
  348. INFO("Unknown smc: %x\n", smc_fid);
  349. break;
  350. }
  351. return plat_pncd_smc_handler(smc_fid, x1, x2, x3, x4,
  352. cookie, handle, flags);
  353. }
  354. static uintptr_t pncd_smc_handler(uint32_t smc_fid,
  355. u_register_t x1,
  356. u_register_t x2,
  357. u_register_t x3,
  358. u_register_t x4,
  359. void *cookie,
  360. void *handle,
  361. u_register_t flags)
  362. {
  363. uintptr_t ret;
  364. /* SMC handling is serialized */
  365. spin_lock(&smc_handler_lock);
  366. ret = pncd_smc_handler_unsafe(smc_fid, x1, x2, x3, x4, cookie, handle,
  367. flags);
  368. spin_unlock(&smc_handler_lock);
  369. return ret;
  370. }
  371. /* Define a SPD runtime service descriptor for fast SMC calls */
  372. DECLARE_RT_SVC(
  373. pncd_fast,
  374. OEN_TOS_START,
  375. OEN_TOS_END,
  376. SMC_TYPE_FAST,
  377. pncd_setup,
  378. pncd_smc_handler
  379. );
  380. /* Define a SPD runtime service descriptor for standard SMC calls */
  381. DECLARE_RT_SVC(
  382. pncd_std,
  383. OEN_TOS_START,
  384. OEN_TOS_END,
  385. SMC_TYPE_YIELD,
  386. NULL,
  387. pncd_smc_handler
  388. );