ehf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2017-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*
  7. * Exception handlers at EL3, their priority levels, and management.
  8. */
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. #include <bl31/ehf.h>
  12. #include <bl31/interrupt_mgmt.h>
  13. #include <context.h>
  14. #include <common/debug.h>
  15. #include <drivers/arm/gic_common.h>
  16. #include <lib/el3_runtime/context_mgmt.h>
  17. #include <lib/el3_runtime/cpu_data.h>
  18. #include <lib/el3_runtime/pubsub_events.h>
  19. #include <plat/common/platform.h>
  20. /* Output EHF logs as verbose */
  21. #define EHF_LOG(...) VERBOSE("EHF: " __VA_ARGS__)
  22. #define EHF_INVALID_IDX (-1)
  23. /* For a valid handler, return the actual function pointer; otherwise, 0. */
  24. #define RAW_HANDLER(h) \
  25. ((ehf_handler_t) ((((h) & EHF_PRI_VALID_) != 0U) ? \
  26. ((h) & ~EHF_PRI_VALID_) : 0U))
  27. #define PRI_BIT(idx) (((ehf_pri_bits_t) 1u) << (idx))
  28. /*
  29. * Convert index into secure priority using the platform-defined priority bits
  30. * field.
  31. */
  32. #define IDX_TO_PRI(idx) \
  33. ((((unsigned) idx) << (7u - exception_data.pri_bits)) & 0x7fU)
  34. /* Check whether a given index is valid */
  35. #define IS_IDX_VALID(idx) \
  36. ((exception_data.ehf_priorities[idx].ehf_handler & EHF_PRI_VALID_) != 0U)
  37. /* Returns whether given priority is in secure priority range */
  38. #define IS_PRI_SECURE(pri) (((pri) & 0x80U) == 0U)
  39. /* To be defined by the platform */
  40. extern const ehf_priorities_t exception_data;
  41. /* Translate priority to the index in the priority array */
  42. static unsigned int pri_to_idx(unsigned int priority)
  43. {
  44. unsigned int idx;
  45. idx = EHF_PRI_TO_IDX(priority, exception_data.pri_bits);
  46. assert(idx < exception_data.num_priorities);
  47. assert(IS_IDX_VALID(idx));
  48. return idx;
  49. }
  50. /* Return whether there are outstanding priority activation */
  51. static bool has_valid_pri_activations(pe_exc_data_t *pe_data)
  52. {
  53. return pe_data->active_pri_bits != 0U;
  54. }
  55. static pe_exc_data_t *this_cpu_data(void)
  56. {
  57. return &get_cpu_data(ehf_data);
  58. }
  59. /*
  60. * Return the current priority index of this CPU. If no priority is active,
  61. * return EHF_INVALID_IDX.
  62. */
  63. static int get_pe_highest_active_idx(pe_exc_data_t *pe_data)
  64. {
  65. if (!has_valid_pri_activations(pe_data))
  66. return EHF_INVALID_IDX;
  67. /* Current priority is the right-most bit */
  68. return (int) __builtin_ctz(pe_data->active_pri_bits);
  69. }
  70. /*
  71. * Mark priority active by setting the corresponding bit in active_pri_bits and
  72. * programming the priority mask.
  73. *
  74. * This API is to be used as part of delegating to lower ELs other than for
  75. * interrupts; e.g. while handling synchronous exceptions.
  76. *
  77. * This API is expected to be invoked before restoring context (Secure or
  78. * Non-secure) in preparation for the respective dispatch.
  79. */
  80. void ehf_activate_priority(unsigned int priority)
  81. {
  82. int cur_pri_idx;
  83. unsigned int old_mask, run_pri, idx;
  84. pe_exc_data_t *pe_data = this_cpu_data();
  85. /*
  86. * Query interrupt controller for the running priority, or idle priority
  87. * if no interrupts are being handled. The requested priority must be
  88. * less (higher priority) than the active running priority.
  89. */
  90. run_pri = plat_ic_get_running_priority();
  91. if (priority >= run_pri) {
  92. ERROR("Running priority higher (0x%x) than requested (0x%x)\n",
  93. run_pri, priority);
  94. panic();
  95. }
  96. /*
  97. * If there were priority activations already, the requested priority
  98. * must be less (higher priority) than the current highest priority
  99. * activation so far.
  100. */
  101. cur_pri_idx = get_pe_highest_active_idx(pe_data);
  102. idx = pri_to_idx(priority);
  103. if ((cur_pri_idx != EHF_INVALID_IDX) &&
  104. (idx >= ((unsigned int) cur_pri_idx))) {
  105. ERROR("Activation priority mismatch: req=0x%x current=0x%x\n",
  106. priority, IDX_TO_PRI(cur_pri_idx));
  107. panic();
  108. }
  109. /* Set the bit corresponding to the requested priority */
  110. pe_data->active_pri_bits |= PRI_BIT(idx);
  111. /*
  112. * Program priority mask for the activated level. Check that the new
  113. * priority mask is setting a higher priority level than the existing
  114. * mask.
  115. */
  116. old_mask = plat_ic_set_priority_mask(priority);
  117. if (priority >= old_mask) {
  118. ERROR("Requested priority (0x%x) lower than Priority Mask (0x%x)\n",
  119. priority, old_mask);
  120. panic();
  121. }
  122. /*
  123. * If this is the first activation, save the priority mask. This will be
  124. * restored after the last deactivation.
  125. */
  126. if (cur_pri_idx == EHF_INVALID_IDX)
  127. pe_data->init_pri_mask = (uint8_t) old_mask;
  128. EHF_LOG("activate prio=%d\n", get_pe_highest_active_idx(pe_data));
  129. }
  130. /*
  131. * Mark priority inactive by clearing the corresponding bit in active_pri_bits,
  132. * and programming the priority mask.
  133. *
  134. * This API is expected to be used as part of delegating to to lower ELs other
  135. * than for interrupts; e.g. while handling synchronous exceptions.
  136. *
  137. * This API is expected to be invoked after saving context (Secure or
  138. * Non-secure), having concluded the respective dispatch.
  139. */
  140. void ehf_deactivate_priority(unsigned int priority)
  141. {
  142. int cur_pri_idx;
  143. pe_exc_data_t *pe_data = this_cpu_data();
  144. unsigned int old_mask, run_pri, idx;
  145. /*
  146. * Query interrupt controller for the running priority, or idle priority
  147. * if no interrupts are being handled. The requested priority must be
  148. * less (higher priority) than the active running priority.
  149. */
  150. run_pri = plat_ic_get_running_priority();
  151. if (priority >= run_pri) {
  152. ERROR("Running priority higher (0x%x) than requested (0x%x)\n",
  153. run_pri, priority);
  154. panic();
  155. }
  156. /*
  157. * Deactivation is allowed only when there are priority activations, and
  158. * the deactivation priority level must match the current activated
  159. * priority.
  160. */
  161. cur_pri_idx = get_pe_highest_active_idx(pe_data);
  162. idx = pri_to_idx(priority);
  163. if ((cur_pri_idx == EHF_INVALID_IDX) ||
  164. (idx != ((unsigned int) cur_pri_idx))) {
  165. ERROR("Deactivation priority mismatch: req=0x%x current=0x%x\n",
  166. priority, IDX_TO_PRI(cur_pri_idx));
  167. panic();
  168. }
  169. /* Clear bit corresponding to highest priority */
  170. pe_data->active_pri_bits &= (pe_data->active_pri_bits - 1u);
  171. /*
  172. * Restore priority mask corresponding to the next priority, or the
  173. * one stashed earlier if there are no more to deactivate.
  174. */
  175. cur_pri_idx = get_pe_highest_active_idx(pe_data);
  176. if (cur_pri_idx == EHF_INVALID_IDX)
  177. old_mask = plat_ic_set_priority_mask(pe_data->init_pri_mask);
  178. else
  179. old_mask = plat_ic_set_priority_mask(priority);
  180. if (old_mask > priority) {
  181. ERROR("Deactivation priority (0x%x) lower than Priority Mask (0x%x)\n",
  182. priority, old_mask);
  183. panic();
  184. }
  185. EHF_LOG("deactivate prio=%d\n", get_pe_highest_active_idx(pe_data));
  186. }
  187. /*
  188. * After leaving Non-secure world, stash current Non-secure Priority Mask, and
  189. * set Priority Mask to the highest Non-secure priority so that Non-secure
  190. * interrupts cannot preempt Secure execution.
  191. *
  192. * If the current running priority is in the secure range, or if there are
  193. * outstanding priority activations, this function does nothing.
  194. *
  195. * This function subscribes to the 'cm_exited_normal_world' event published by
  196. * the Context Management Library.
  197. */
  198. static void *ehf_exited_normal_world(const void *arg)
  199. {
  200. unsigned int run_pri;
  201. pe_exc_data_t *pe_data = this_cpu_data();
  202. /* If the running priority is in the secure range, do nothing */
  203. run_pri = plat_ic_get_running_priority();
  204. if (IS_PRI_SECURE(run_pri))
  205. return NULL;
  206. /* Do nothing if there are explicit activations */
  207. if (has_valid_pri_activations(pe_data))
  208. return NULL;
  209. assert(pe_data->ns_pri_mask == 0u);
  210. pe_data->ns_pri_mask =
  211. (uint8_t) plat_ic_set_priority_mask(GIC_HIGHEST_NS_PRIORITY);
  212. /* The previous Priority Mask is not expected to be in secure range */
  213. if (IS_PRI_SECURE(pe_data->ns_pri_mask)) {
  214. ERROR("Priority Mask (0x%x) already in secure range\n",
  215. pe_data->ns_pri_mask);
  216. panic();
  217. }
  218. EHF_LOG("Priority Mask: 0x%x => 0x%x\n", pe_data->ns_pri_mask,
  219. GIC_HIGHEST_NS_PRIORITY);
  220. return NULL;
  221. }
  222. /*
  223. * Conclude Secure execution and prepare for return to Non-secure world. Restore
  224. * the Non-secure Priority Mask previously stashed upon leaving Non-secure
  225. * world.
  226. *
  227. * If there the current running priority is in the secure range, or if there are
  228. * outstanding priority activations, this function does nothing.
  229. *
  230. * This function subscribes to the 'cm_entering_normal_world' event published by
  231. * the Context Management Library.
  232. */
  233. static void *ehf_entering_normal_world(const void *arg)
  234. {
  235. unsigned int old_pmr, run_pri;
  236. pe_exc_data_t *pe_data = this_cpu_data();
  237. /* If the running priority is in the secure range, do nothing */
  238. run_pri = plat_ic_get_running_priority();
  239. if (IS_PRI_SECURE(run_pri))
  240. return NULL;
  241. /*
  242. * If there are explicit activations, do nothing. The Priority Mask will
  243. * be restored upon the last deactivation.
  244. */
  245. if (has_valid_pri_activations(pe_data))
  246. return NULL;
  247. /* Do nothing if we don't have a valid Priority Mask to restore */
  248. if (pe_data->ns_pri_mask == 0U)
  249. return NULL;
  250. old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
  251. /*
  252. * When exiting secure world, the current Priority Mask must be
  253. * GIC_HIGHEST_NS_PRIORITY (as set during entry), or the Non-secure
  254. * priority mask set upon calling ehf_allow_ns_preemption()
  255. */
  256. if ((old_pmr != GIC_HIGHEST_NS_PRIORITY) &&
  257. (old_pmr != pe_data->ns_pri_mask)) {
  258. ERROR("Invalid Priority Mask (0x%x) restored\n", old_pmr);
  259. panic();
  260. }
  261. EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
  262. pe_data->ns_pri_mask = 0;
  263. return NULL;
  264. }
  265. /*
  266. * Program Priority Mask to the original Non-secure priority such that
  267. * Non-secure interrupts may preempt Secure execution (for example, during
  268. * Yielding SMC calls). The 'preempt_ret_code' parameter indicates the Yielding
  269. * SMC's return value in case the call was preempted.
  270. *
  271. * This API is expected to be invoked before delegating a yielding SMC to Secure
  272. * EL1. I.e. within the window of secure execution after Non-secure context is
  273. * saved (after entry into EL3) and Secure context is restored (before entering
  274. * Secure EL1).
  275. */
  276. void ehf_allow_ns_preemption(uint64_t preempt_ret_code)
  277. {
  278. cpu_context_t *ns_ctx;
  279. unsigned int old_pmr __unused;
  280. pe_exc_data_t *pe_data = this_cpu_data();
  281. /*
  282. * We should have been notified earlier of entering secure world, and
  283. * therefore have stashed the Non-secure priority mask.
  284. */
  285. assert(pe_data->ns_pri_mask != 0U);
  286. /* Make sure no priority levels are active when requesting this */
  287. if (has_valid_pri_activations(pe_data)) {
  288. ERROR("PE %lx has priority activations: 0x%x\n",
  289. read_mpidr_el1(), pe_data->active_pri_bits);
  290. panic();
  291. }
  292. /*
  293. * Program preempted return code to x0 right away so that, if the
  294. * Yielding SMC was indeed preempted before a dispatcher gets a chance
  295. * to populate it, the caller would find the correct return value.
  296. */
  297. ns_ctx = cm_get_context(NON_SECURE);
  298. assert(ns_ctx != NULL);
  299. write_ctx_reg(get_gpregs_ctx(ns_ctx), CTX_GPREG_X0, preempt_ret_code);
  300. old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
  301. EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
  302. pe_data->ns_pri_mask = 0;
  303. }
  304. /*
  305. * Return whether Secure execution has explicitly allowed Non-secure interrupts
  306. * to preempt itself (for example, during Yielding SMC calls).
  307. */
  308. unsigned int ehf_is_ns_preemption_allowed(void)
  309. {
  310. unsigned int run_pri;
  311. pe_exc_data_t *pe_data = this_cpu_data();
  312. /* If running priority is in secure range, return false */
  313. run_pri = plat_ic_get_running_priority();
  314. if (IS_PRI_SECURE(run_pri))
  315. return 0;
  316. /*
  317. * If Non-secure preemption was permitted by calling
  318. * ehf_allow_ns_preemption() earlier:
  319. *
  320. * - There wouldn't have been priority activations;
  321. * - We would have cleared the stashed the Non-secure Priority Mask.
  322. */
  323. if (has_valid_pri_activations(pe_data))
  324. return 0;
  325. if (pe_data->ns_pri_mask != 0U)
  326. return 0;
  327. return 1;
  328. }
  329. /*
  330. * Top-level EL3 interrupt handler.
  331. */
  332. static uint64_t ehf_el3_interrupt_handler(uint32_t id, uint32_t flags,
  333. void *handle, void *cookie)
  334. {
  335. int ret = 0;
  336. uint32_t intr_raw;
  337. unsigned int intr, pri, idx;
  338. ehf_handler_t handler;
  339. /*
  340. * Top-level interrupt type handler from Interrupt Management Framework
  341. * doesn't acknowledge the interrupt; so the interrupt ID must be
  342. * invalid.
  343. */
  344. assert(id == INTR_ID_UNAVAILABLE);
  345. /*
  346. * Acknowledge interrupt. Proceed with handling only for valid interrupt
  347. * IDs. This situation may arise because of Interrupt Management
  348. * Framework identifying an EL3 interrupt, but before it's been
  349. * acknowledged here, the interrupt was either deasserted, or there was
  350. * a higher-priority interrupt of another type.
  351. */
  352. intr_raw = plat_ic_acknowledge_interrupt();
  353. intr = plat_ic_get_interrupt_id(intr_raw);
  354. if (intr == INTR_ID_UNAVAILABLE)
  355. return 0;
  356. /* Having acknowledged the interrupt, get the running priority */
  357. pri = plat_ic_get_running_priority();
  358. /* Check EL3 interrupt priority is in secure range */
  359. assert(IS_PRI_SECURE(pri));
  360. /*
  361. * Translate the priority to a descriptor index. We do this by masking
  362. * and shifting the running priority value (platform-supplied).
  363. */
  364. idx = pri_to_idx(pri);
  365. /* Validate priority */
  366. assert(pri == IDX_TO_PRI(idx));
  367. handler = (ehf_handler_t) RAW_HANDLER(
  368. exception_data.ehf_priorities[idx].ehf_handler);
  369. if (handler == NULL) {
  370. ERROR("No EL3 exception handler for priority 0x%x\n",
  371. IDX_TO_PRI(idx));
  372. panic();
  373. }
  374. /*
  375. * Call registered handler. Pass the raw interrupt value to registered
  376. * handlers.
  377. */
  378. ret = handler(intr_raw, flags, handle, cookie);
  379. return (uint64_t) ret;
  380. }
  381. /*
  382. * Initialize the EL3 exception handling.
  383. */
  384. void __init ehf_init(void)
  385. {
  386. unsigned int flags = 0;
  387. int ret __unused;
  388. /* Ensure EL3 interrupts are supported */
  389. assert(plat_ic_has_interrupt_type(INTR_TYPE_EL3) != 0);
  390. /*
  391. * Make sure that priority water mark has enough bits to represent the
  392. * whole priority array.
  393. */
  394. assert(exception_data.num_priorities <= (sizeof(ehf_pri_bits_t) * 8U));
  395. assert(exception_data.ehf_priorities != NULL);
  396. /*
  397. * Bit 7 of GIC priority must be 0 for secure interrupts. This means
  398. * platforms must use at least 1 of the remaining 7 bits.
  399. */
  400. assert((exception_data.pri_bits >= 1U) ||
  401. (exception_data.pri_bits < 8U));
  402. /* Route EL3 interrupts when in Non-secure. */
  403. set_interrupt_rm_flag(flags, NON_SECURE);
  404. /*
  405. * Route EL3 interrupts when in secure, only when SPMC is not present
  406. * in S-EL2.
  407. */
  408. #if !(defined(SPD_spmd) && (SPMD_SPM_AT_SEL2 == 1))
  409. set_interrupt_rm_flag(flags, SECURE);
  410. #endif /* !(defined(SPD_spmd) && (SPMD_SPM_AT_SEL2 == 1)) */
  411. /* Register handler for EL3 interrupts */
  412. ret = register_interrupt_type_handler(INTR_TYPE_EL3,
  413. ehf_el3_interrupt_handler, flags);
  414. assert(ret == 0);
  415. }
  416. /*
  417. * Register a handler at the supplied priority. Registration is allowed only if
  418. * a handler hasn't been registered before, or one wasn't provided at build
  419. * time. The priority for which the handler is being registered must also accord
  420. * with the platform-supplied data.
  421. */
  422. void ehf_register_priority_handler(unsigned int pri, ehf_handler_t handler)
  423. {
  424. unsigned int idx;
  425. /* Sanity check for handler */
  426. assert(handler != NULL);
  427. /* Handler ought to be 4-byte aligned */
  428. assert((((uintptr_t) handler) & 3U) == 0U);
  429. /* Ensure we register for valid priority */
  430. idx = pri_to_idx(pri);
  431. assert(idx < exception_data.num_priorities);
  432. assert(IDX_TO_PRI(idx) == pri);
  433. /* Return failure if a handler was already registered */
  434. if (exception_data.ehf_priorities[idx].ehf_handler != EHF_NO_HANDLER_) {
  435. ERROR("Handler already registered for priority 0x%x\n", pri);
  436. panic();
  437. }
  438. /*
  439. * Install handler, and retain the valid bit. We assume that the handler
  440. * is 4-byte aligned, which is usually the case.
  441. */
  442. exception_data.ehf_priorities[idx].ehf_handler =
  443. (((uintptr_t) handler) | EHF_PRI_VALID_);
  444. EHF_LOG("register pri=0x%x handler=%p\n", pri, handler);
  445. }
  446. SUBSCRIBE_TO_EVENT(cm_entering_normal_world, ehf_entering_normal_world);
  447. SUBSCRIBE_TO_EVENT(cm_exited_normal_world, ehf_exited_normal_world);