tlkd_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. /*******************************************************************************
  8. * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
  9. * plug-in component to the Secure Monitor, registered as a runtime service. The
  10. * SPD is expected to be a functional extension of the Secure Payload (SP) that
  11. * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
  12. * the Trusted OS/Applications range to the dispatcher. The SPD will either
  13. * handle the request locally or delegate it to the Secure Payload. It is also
  14. * responsible for initialising and maintaining communication with the SP.
  15. ******************************************************************************/
  16. #include <assert.h>
  17. #include <bl31/interrupt_mgmt.h>
  18. #include <errno.h>
  19. #include <stddef.h>
  20. #include <arch_helpers.h>
  21. #include <bl31/bl31.h>
  22. #include <bl32/payloads/tlk.h>
  23. #include <common/bl_common.h>
  24. #include <common/debug.h>
  25. #include <common/runtime_svc.h>
  26. #include <lib/el3_runtime/context_mgmt.h>
  27. #include <plat/common/platform.h>
  28. #include <tools_share/uuid.h>
  29. #include "tlkd_private.h"
  30. extern const spd_pm_ops_t tlkd_pm_ops;
  31. /*******************************************************************************
  32. * Per-cpu Secure Payload state
  33. ******************************************************************************/
  34. tlk_context_t tlk_ctx;
  35. /*******************************************************************************
  36. * CPU number on which TLK booted up
  37. ******************************************************************************/
  38. static uint32_t boot_cpu;
  39. /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
  40. DEFINE_SVC_UUID2(tlk_uuid,
  41. 0xc9e911bd, 0xba2b, 0xee52, 0xb1, 0x72,
  42. 0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
  43. static int32_t tlkd_init(void);
  44. /*******************************************************************************
  45. * Secure Payload Dispatcher's timer interrupt handler
  46. ******************************************************************************/
  47. static uint64_t tlkd_interrupt_handler(uint32_t id,
  48. uint32_t flags,
  49. void *handle,
  50. void *cookie)
  51. {
  52. cpu_context_t *s_cpu_context;
  53. int irq = plat_ic_get_pending_interrupt_id();
  54. /* acknowledge the interrupt and mark it complete */
  55. (void)plat_ic_acknowledge_interrupt();
  56. plat_ic_end_of_interrupt(irq);
  57. /*
  58. * Disable the routing of NS interrupts from secure world to
  59. * EL3 while interrupted on this core.
  60. */
  61. disable_intr_rm_local(INTR_TYPE_S_EL1, SECURE);
  62. /* Check the security state when the exception was generated */
  63. assert(get_interrupt_src_ss(flags) == NON_SECURE);
  64. assert(handle == cm_get_context(NON_SECURE));
  65. /* Save non-secure state */
  66. cm_el1_sysregs_context_save(NON_SECURE);
  67. /* Get a reference to the secure context */
  68. s_cpu_context = cm_get_context(SECURE);
  69. assert(s_cpu_context);
  70. /*
  71. * Restore non-secure state. There is no need to save the
  72. * secure system register context since the SP was supposed
  73. * to preserve it during S-EL1 interrupt handling.
  74. */
  75. cm_el1_sysregs_context_restore(SECURE);
  76. cm_set_next_eret_context(SECURE);
  77. /* Provide the IRQ number to the SPD */
  78. SMC_RET4(s_cpu_context, (uint32_t)TLK_IRQ_FIRED, 0, (uint32_t)irq, 0);
  79. }
  80. /*******************************************************************************
  81. * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
  82. * (aarch32/aarch64) if not already known and initialises the context for entry
  83. * into the SP for its initialisation.
  84. ******************************************************************************/
  85. static int32_t tlkd_setup(void)
  86. {
  87. entry_point_info_t *tlk_ep_info;
  88. uint32_t flags;
  89. int32_t ret;
  90. /*
  91. * Get information about the Secure Payload (BL32) image. Its
  92. * absence is a critical failure.
  93. */
  94. tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
  95. if (!tlk_ep_info) {
  96. WARN("No SP provided. Booting device without SP"
  97. " initialization. SMC`s destined for SP"
  98. " will return SMC_UNK\n");
  99. return 1;
  100. }
  101. /*
  102. * If there's no valid entry point for SP, we return a non-zero value
  103. * signalling failure initializing the service. We bail out without
  104. * registering any handlers
  105. */
  106. if (!tlk_ep_info->pc)
  107. return 1;
  108. /*
  109. * Inspect the SP image's SPSR and determine it's execution state
  110. * i.e whether AArch32 or AArch64.
  111. */
  112. tlkd_init_tlk_ep_state(tlk_ep_info,
  113. (tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
  114. tlk_ep_info->pc,
  115. &tlk_ctx);
  116. /* get a list of all S-EL1 IRQs from the platform */
  117. /* register interrupt handler */
  118. flags = 0;
  119. set_interrupt_rm_flag(flags, NON_SECURE);
  120. ret = register_interrupt_type_handler(INTR_TYPE_S_EL1,
  121. tlkd_interrupt_handler,
  122. flags);
  123. if (ret != 0) {
  124. ERROR("failed to register tlkd interrupt handler (%d)\n", ret);
  125. }
  126. /*
  127. * All TLK SPD initialization done. Now register our init function
  128. * with BL31 for deferred invocation
  129. */
  130. bl31_register_bl32_init(&tlkd_init);
  131. return 0;
  132. }
  133. /*******************************************************************************
  134. * This function passes control to the Secure Payload image (BL32) for the first
  135. * time on the primary cpu after a cold boot. It assumes that a valid secure
  136. * context has already been created by tlkd_setup() which can be directly
  137. * used. This function performs a synchronous entry into the Secure payload.
  138. * The SP passes control back to this routine through a SMC.
  139. ******************************************************************************/
  140. static int32_t tlkd_init(void)
  141. {
  142. entry_point_info_t *tlk_entry_point;
  143. /*
  144. * Get information about the Secure Payload (BL32) image. Its
  145. * absence is a critical failure.
  146. */
  147. tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
  148. assert(tlk_entry_point);
  149. cm_init_my_context(tlk_entry_point);
  150. /*
  151. * TLK runs only on a single CPU. Store the value of the boot
  152. * CPU for sanity checking later.
  153. */
  154. boot_cpu = plat_my_core_pos();
  155. /*
  156. * Arrange for an entry into the test secure payload.
  157. */
  158. return tlkd_synchronous_sp_entry(&tlk_ctx);
  159. }
  160. /*******************************************************************************
  161. * This function is responsible for handling all SMCs in the Trusted OS/App
  162. * range from the non-secure state as defined in the SMC Calling Convention
  163. * Document. It is also responsible for communicating with the Secure payload
  164. * to delegate work and return results back to the non-secure state. Lastly it
  165. * will also return any information that the secure payload needs to do the
  166. * work assigned to it.
  167. ******************************************************************************/
  168. static uintptr_t tlkd_smc_handler(uint32_t smc_fid,
  169. u_register_t x1,
  170. u_register_t x2,
  171. u_register_t x3,
  172. u_register_t x4,
  173. void *cookie,
  174. void *handle,
  175. u_register_t flags)
  176. {
  177. cpu_context_t *ns_cpu_context;
  178. gp_regs_t *gp_regs;
  179. uint32_t ns;
  180. uint64_t par;
  181. /* Passing a NULL context is a critical programming error */
  182. assert(handle);
  183. /* These SMCs are only supported by a single CPU */
  184. if (boot_cpu != plat_my_core_pos())
  185. SMC_RET1(handle, SMC_UNK);
  186. /* Determine which security state this SMC originated from */
  187. ns = is_caller_non_secure(flags);
  188. switch (smc_fid) {
  189. /*
  190. * This function ID is used by SP to indicate that it was
  191. * preempted by a non-secure world IRQ.
  192. */
  193. case TLK_PREEMPTED:
  194. if (ns)
  195. SMC_RET1(handle, SMC_UNK);
  196. assert(handle == cm_get_context(SECURE));
  197. cm_el1_sysregs_context_save(SECURE);
  198. /* Get a reference to the non-secure context */
  199. ns_cpu_context = cm_get_context(NON_SECURE);
  200. assert(ns_cpu_context);
  201. /*
  202. * Restore non-secure state. There is no need to save the
  203. * secure system register context since the SP was supposed
  204. * to preserve it during S-EL1 interrupt handling.
  205. */
  206. cm_el1_sysregs_context_restore(NON_SECURE);
  207. cm_set_next_eret_context(NON_SECURE);
  208. SMC_RET1(ns_cpu_context, x1);
  209. /*
  210. * This is a request from the non-secure context to:
  211. *
  212. * a. register shared memory with the SP for storing it's
  213. * activity logs.
  214. * b. register shared memory with the SP for passing args
  215. * required for maintaining sessions with the Trusted
  216. * Applications.
  217. * c. register shared persistent buffers for secure storage
  218. * d. register NS DRAM ranges passed by Cboot
  219. * e. register Root of Trust parameters from Cboot for Verified Boot
  220. * f. open/close sessions
  221. * g. issue commands to the Trusted Apps
  222. * h. resume the preempted yielding SMC call.
  223. */
  224. case TLK_REGISTER_LOGBUF:
  225. case TLK_REGISTER_REQBUF:
  226. case TLK_SS_REGISTER_HANDLER:
  227. case TLK_REGISTER_NS_DRAM_RANGES:
  228. case TLK_SET_ROOT_OF_TRUST:
  229. case TLK_OPEN_TA_SESSION:
  230. case TLK_CLOSE_TA_SESSION:
  231. case TLK_TA_LAUNCH_OP:
  232. case TLK_TA_SEND_EVENT:
  233. case TLK_RESUME_FID:
  234. case TLK_SET_BL_VERSION:
  235. case TLK_LOCK_BL_INTERFACE:
  236. case TLK_BL_RPMB_SERVICE:
  237. if (!ns)
  238. SMC_RET1(handle, SMC_UNK);
  239. /*
  240. * This is a fresh request from the non-secure client.
  241. * The parameters are in x1 and x2. Figure out which
  242. * registers need to be preserved, save the non-secure
  243. * state and send the request to the secure payload.
  244. */
  245. assert(handle == cm_get_context(NON_SECURE));
  246. /*
  247. * Check if we are already processing a yielding SMC
  248. * call. Of all the supported fids, only the "resume"
  249. * fid expects the flag to be set.
  250. */
  251. if (smc_fid == TLK_RESUME_FID) {
  252. if (!get_yield_smc_active_flag(tlk_ctx.state))
  253. SMC_RET1(handle, SMC_UNK);
  254. } else {
  255. if (get_yield_smc_active_flag(tlk_ctx.state))
  256. SMC_RET1(handle, SMC_UNK);
  257. }
  258. cm_el1_sysregs_context_save(NON_SECURE);
  259. /*
  260. * Verify if there is a valid context to use.
  261. */
  262. assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
  263. /*
  264. * Mark the SP state as active.
  265. */
  266. set_yield_smc_active_flag(tlk_ctx.state);
  267. /*
  268. * We are done stashing the non-secure context. Ask the
  269. * secure payload to do the work now.
  270. */
  271. cm_el1_sysregs_context_restore(SECURE);
  272. cm_set_next_eret_context(SECURE);
  273. /*
  274. * TLK is a 32-bit Trusted OS and so expects the SMC
  275. * arguments via r0-r7. TLK expects the monitor frame
  276. * registers to be 64-bits long. Hence, we pass x0 in
  277. * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
  278. *
  279. * As smc_fid is a uint32 value, r1 contains 0.
  280. */
  281. gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
  282. write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
  283. write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
  284. write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
  285. write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
  286. SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
  287. (uint32_t)(x1 >> 32));
  288. /*
  289. * Translate NS/EL1-S virtual addresses.
  290. *
  291. * x1 = virtual address
  292. * x3 = type (NS/S)
  293. *
  294. * Returns PA:lo in r0, PA:hi in r1.
  295. */
  296. case TLK_VA_TRANSLATE:
  297. /* Should be invoked only by secure world */
  298. if (ns)
  299. SMC_RET1(handle, SMC_UNK);
  300. /* NS virtual addresses are 64-bit long */
  301. if (x3 & TLK_TRANSLATE_NS_VADDR)
  302. x1 = (uint32_t)x1 | (x2 << 32);
  303. if (!x1)
  304. SMC_RET1(handle, SMC_UNK);
  305. /*
  306. * TODO: Sanity check x1. This would require platform
  307. * support.
  308. */
  309. /* virtual address and type: ns/s */
  310. par = tlkd_va_translate(x1, x3);
  311. /* return physical address in r0-r1 */
  312. SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
  313. /*
  314. * This is a request from the SP to mark completion of
  315. * a yielding function ID.
  316. */
  317. case TLK_REQUEST_DONE:
  318. if (ns)
  319. SMC_RET1(handle, SMC_UNK);
  320. /*
  321. * Mark the SP state as inactive.
  322. */
  323. clr_yield_smc_active_flag(tlk_ctx.state);
  324. /* Get a reference to the non-secure context */
  325. ns_cpu_context = cm_get_context(NON_SECURE);
  326. assert(ns_cpu_context);
  327. /*
  328. * This is a request completion SMC and we must switch to
  329. * the non-secure world to pass the result.
  330. */
  331. cm_el1_sysregs_context_save(SECURE);
  332. /*
  333. * We are done stashing the secure context. Switch to the
  334. * non-secure context and return the result.
  335. */
  336. cm_el1_sysregs_context_restore(NON_SECURE);
  337. cm_set_next_eret_context(NON_SECURE);
  338. SMC_RET1(ns_cpu_context, x1);
  339. /*
  340. * This function ID is used only by the SP to indicate it has
  341. * finished initialising itself after a cold boot
  342. */
  343. case TLK_ENTRY_DONE:
  344. if (ns)
  345. SMC_RET1(handle, SMC_UNK);
  346. /*
  347. * SP has been successfully initialized. Register power
  348. * management hooks with PSCI
  349. */
  350. psci_register_spd_pm_hook(&tlkd_pm_ops);
  351. /*
  352. * TLK reports completion. The SPD must have initiated
  353. * the original request through a synchronous entry
  354. * into the SP. Jump back to the original C runtime
  355. * context.
  356. */
  357. tlkd_synchronous_sp_exit(&tlk_ctx, x1);
  358. break;
  359. /*
  360. * These function IDs are used only by TLK to indicate it has
  361. * finished:
  362. * 1. suspending itself after an earlier psci cpu_suspend
  363. * request.
  364. * 2. resuming itself after an earlier psci cpu_suspend
  365. * request.
  366. * 3. powering down after an earlier psci system_off/system_reset
  367. * request.
  368. */
  369. case TLK_SUSPEND_DONE:
  370. case TLK_RESUME_DONE:
  371. if (ns)
  372. SMC_RET1(handle, SMC_UNK);
  373. /*
  374. * TLK reports completion. TLKD must have initiated the
  375. * original request through a synchronous entry into the SP.
  376. * Jump back to the original C runtime context, and pass x1 as
  377. * return value to the caller
  378. */
  379. tlkd_synchronous_sp_exit(&tlk_ctx, x1);
  380. break;
  381. /*
  382. * This function ID is used by SP to indicate that it has completed
  383. * handling the secure interrupt.
  384. */
  385. case TLK_IRQ_DONE:
  386. if (ns)
  387. SMC_RET1(handle, SMC_UNK);
  388. assert(handle == cm_get_context(SECURE));
  389. /* save secure world context */
  390. cm_el1_sysregs_context_save(SECURE);
  391. /* Get a reference to the non-secure context */
  392. ns_cpu_context = cm_get_context(NON_SECURE);
  393. assert(ns_cpu_context);
  394. /*
  395. * Restore non-secure state. There is no need to save the
  396. * secure system register context since the SP was supposed
  397. * to preserve it during S-EL1 interrupt handling.
  398. */
  399. cm_el1_sysregs_context_restore(NON_SECURE);
  400. cm_set_next_eret_context(NON_SECURE);
  401. SMC_RET0(ns_cpu_context);
  402. /*
  403. * Return the number of service function IDs implemented to
  404. * provide service to non-secure
  405. */
  406. case TOS_CALL_COUNT:
  407. SMC_RET1(handle, TLK_NUM_FID);
  408. /*
  409. * Return TLK's UID to the caller
  410. */
  411. case TOS_UID:
  412. SMC_UUID_RET(handle, tlk_uuid);
  413. /*
  414. * Return the version of current implementation
  415. */
  416. case TOS_CALL_VERSION:
  417. SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
  418. default:
  419. WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid);
  420. break;
  421. }
  422. SMC_RET1(handle, SMC_UNK);
  423. }
  424. /* Define a SPD runtime service descriptor for fast SMC calls */
  425. DECLARE_RT_SVC(
  426. tlkd_tos_fast,
  427. OEN_TOS_START,
  428. OEN_TOS_END,
  429. SMC_TYPE_FAST,
  430. tlkd_setup,
  431. tlkd_smc_handler
  432. );
  433. /* Define a SPD runtime service descriptor for yielding SMC calls */
  434. DECLARE_RT_SVC(
  435. tlkd_tos_std,
  436. OEN_TOS_START,
  437. OEN_TOS_END,
  438. SMC_TYPE_YIELD,
  439. NULL,
  440. tlkd_smc_handler
  441. );
  442. /* Define a SPD runtime service descriptor for fast SMC calls */
  443. DECLARE_RT_SVC(
  444. tlkd_tap_fast,
  445. OEN_TAP_START,
  446. OEN_TAP_END,
  447. SMC_TYPE_FAST,
  448. NULL,
  449. tlkd_smc_handler
  450. );
  451. /* Define a SPD runtime service descriptor for yielding SMC calls */
  452. DECLARE_RT_SVC(
  453. tlkd_tap_std,
  454. OEN_TAP_START,
  455. OEN_TAP_END,
  456. SMC_TYPE_YIELD,
  457. NULL,
  458. tlkd_smc_handler
  459. );