ehf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (c) 2017-2024, 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 GIC600_ERRATA_WA_2384374
  177. if (cur_pri_idx == EHF_INVALID_IDX) {
  178. old_mask = plat_ic_deactivate_priority(pe_data->init_pri_mask);
  179. } else {
  180. old_mask = plat_ic_deactivate_priority(priority);
  181. }
  182. #else
  183. if (cur_pri_idx == EHF_INVALID_IDX) {
  184. old_mask = plat_ic_set_priority_mask(pe_data->init_pri_mask);
  185. } else {
  186. old_mask = plat_ic_set_priority_mask(priority);
  187. }
  188. #endif
  189. if (old_mask > priority) {
  190. ERROR("Deactivation priority (0x%x) lower than Priority Mask (0x%x)\n",
  191. priority, old_mask);
  192. panic();
  193. }
  194. EHF_LOG("deactivate prio=%d\n", get_pe_highest_active_idx(pe_data));
  195. }
  196. /*
  197. * After leaving Non-secure world, stash current Non-secure Priority Mask, and
  198. * set Priority Mask to the highest Non-secure priority so that Non-secure
  199. * interrupts cannot preempt Secure execution.
  200. *
  201. * If the current running priority is in the secure range, or if there are
  202. * outstanding priority activations, this function does nothing.
  203. *
  204. * This function subscribes to the 'cm_exited_normal_world' event published by
  205. * the Context Management Library.
  206. */
  207. static void *ehf_exited_normal_world(const void *arg)
  208. {
  209. unsigned int run_pri;
  210. pe_exc_data_t *pe_data = this_cpu_data();
  211. /* If the running priority is in the secure range, do nothing */
  212. run_pri = plat_ic_get_running_priority();
  213. if (IS_PRI_SECURE(run_pri))
  214. return NULL;
  215. /* Do nothing if there are explicit activations */
  216. if (has_valid_pri_activations(pe_data))
  217. return NULL;
  218. assert(pe_data->ns_pri_mask == 0u);
  219. pe_data->ns_pri_mask =
  220. (uint8_t) plat_ic_set_priority_mask(GIC_HIGHEST_NS_PRIORITY);
  221. /* The previous Priority Mask is not expected to be in secure range */
  222. if (IS_PRI_SECURE(pe_data->ns_pri_mask)) {
  223. ERROR("Priority Mask (0x%x) already in secure range\n",
  224. pe_data->ns_pri_mask);
  225. panic();
  226. }
  227. EHF_LOG("Priority Mask: 0x%x => 0x%x\n", pe_data->ns_pri_mask,
  228. GIC_HIGHEST_NS_PRIORITY);
  229. return NULL;
  230. }
  231. /*
  232. * Conclude Secure execution and prepare for return to Non-secure world. Restore
  233. * the Non-secure Priority Mask previously stashed upon leaving Non-secure
  234. * world.
  235. *
  236. * If there the current running priority is in the secure range, or if there are
  237. * outstanding priority activations, this function does nothing.
  238. *
  239. * This function subscribes to the 'cm_entering_normal_world' event published by
  240. * the Context Management Library.
  241. */
  242. static void *ehf_entering_normal_world(const void *arg)
  243. {
  244. unsigned int old_pmr, run_pri;
  245. pe_exc_data_t *pe_data = this_cpu_data();
  246. /* If the running priority is in the secure range, do nothing */
  247. run_pri = plat_ic_get_running_priority();
  248. if (IS_PRI_SECURE(run_pri))
  249. return NULL;
  250. /*
  251. * If there are explicit activations, do nothing. The Priority Mask will
  252. * be restored upon the last deactivation.
  253. */
  254. if (has_valid_pri_activations(pe_data))
  255. return NULL;
  256. /* Do nothing if we don't have a valid Priority Mask to restore */
  257. if (pe_data->ns_pri_mask == 0U)
  258. return NULL;
  259. old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
  260. /*
  261. * When exiting secure world, the current Priority Mask must be
  262. * GIC_HIGHEST_NS_PRIORITY (as set during entry), or the Non-secure
  263. * priority mask set upon calling ehf_allow_ns_preemption()
  264. */
  265. if ((old_pmr != GIC_HIGHEST_NS_PRIORITY) &&
  266. (old_pmr != pe_data->ns_pri_mask)) {
  267. ERROR("Invalid Priority Mask (0x%x) restored\n", old_pmr);
  268. panic();
  269. }
  270. EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
  271. pe_data->ns_pri_mask = 0;
  272. return NULL;
  273. }
  274. /*
  275. * Program Priority Mask to the original Non-secure priority such that
  276. * Non-secure interrupts may preempt Secure execution (for example, during
  277. * Yielding SMC calls). The 'preempt_ret_code' parameter indicates the Yielding
  278. * SMC's return value in case the call was preempted.
  279. *
  280. * This API is expected to be invoked before delegating a yielding SMC to Secure
  281. * EL1. I.e. within the window of secure execution after Non-secure context is
  282. * saved (after entry into EL3) and Secure context is restored (before entering
  283. * Secure EL1).
  284. */
  285. void ehf_allow_ns_preemption(uint64_t preempt_ret_code)
  286. {
  287. cpu_context_t *ns_ctx;
  288. unsigned int old_pmr __unused;
  289. pe_exc_data_t *pe_data = this_cpu_data();
  290. /*
  291. * We should have been notified earlier of entering secure world, and
  292. * therefore have stashed the Non-secure priority mask.
  293. */
  294. assert(pe_data->ns_pri_mask != 0U);
  295. /* Make sure no priority levels are active when requesting this */
  296. if (has_valid_pri_activations(pe_data)) {
  297. ERROR("PE %lx has priority activations: 0x%x\n",
  298. read_mpidr_el1(), pe_data->active_pri_bits);
  299. panic();
  300. }
  301. /*
  302. * Program preempted return code to x0 right away so that, if the
  303. * Yielding SMC was indeed preempted before a dispatcher gets a chance
  304. * to populate it, the caller would find the correct return value.
  305. */
  306. ns_ctx = cm_get_context(NON_SECURE);
  307. assert(ns_ctx != NULL);
  308. write_ctx_reg(get_gpregs_ctx(ns_ctx), CTX_GPREG_X0, preempt_ret_code);
  309. old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
  310. EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
  311. pe_data->ns_pri_mask = 0;
  312. }
  313. /*
  314. * Return whether Secure execution has explicitly allowed Non-secure interrupts
  315. * to preempt itself (for example, during Yielding SMC calls).
  316. */
  317. unsigned int ehf_is_ns_preemption_allowed(void)
  318. {
  319. unsigned int run_pri;
  320. pe_exc_data_t *pe_data = this_cpu_data();
  321. /* If running priority is in secure range, return false */
  322. run_pri = plat_ic_get_running_priority();
  323. if (IS_PRI_SECURE(run_pri))
  324. return 0;
  325. /*
  326. * If Non-secure preemption was permitted by calling
  327. * ehf_allow_ns_preemption() earlier:
  328. *
  329. * - There wouldn't have been priority activations;
  330. * - We would have cleared the stashed the Non-secure Priority Mask.
  331. */
  332. if (has_valid_pri_activations(pe_data))
  333. return 0;
  334. if (pe_data->ns_pri_mask != 0U)
  335. return 0;
  336. return 1;
  337. }
  338. /*
  339. * Top-level EL3 interrupt handler.
  340. */
  341. static uint64_t ehf_el3_interrupt_handler(uint32_t id, uint32_t flags,
  342. void *handle, void *cookie)
  343. {
  344. int ret = 0;
  345. uint32_t intr_raw;
  346. unsigned int intr, pri, idx;
  347. ehf_handler_t handler;
  348. /*
  349. * Top-level interrupt type handler from Interrupt Management Framework
  350. * doesn't acknowledge the interrupt; so the interrupt ID must be
  351. * invalid.
  352. */
  353. assert(id == INTR_ID_UNAVAILABLE);
  354. /*
  355. * Acknowledge interrupt. Proceed with handling only for valid interrupt
  356. * IDs. This situation may arise because of Interrupt Management
  357. * Framework identifying an EL3 interrupt, but before it's been
  358. * acknowledged here, the interrupt was either deasserted, or there was
  359. * a higher-priority interrupt of another type.
  360. */
  361. intr_raw = plat_ic_acknowledge_interrupt();
  362. intr = plat_ic_get_interrupt_id(intr_raw);
  363. if (intr == INTR_ID_UNAVAILABLE)
  364. return 0;
  365. /* Having acknowledged the interrupt, get the running priority */
  366. pri = plat_ic_get_running_priority();
  367. /* Check EL3 interrupt priority is in secure range */
  368. assert(IS_PRI_SECURE(pri));
  369. /*
  370. * Translate the priority to a descriptor index. We do this by masking
  371. * and shifting the running priority value (platform-supplied).
  372. */
  373. idx = pri_to_idx(pri);
  374. /* Validate priority */
  375. assert(pri == IDX_TO_PRI(idx));
  376. handler = (ehf_handler_t) RAW_HANDLER(
  377. exception_data.ehf_priorities[idx].ehf_handler);
  378. if (handler == NULL) {
  379. ERROR("No EL3 exception handler for priority 0x%x\n",
  380. IDX_TO_PRI(idx));
  381. panic();
  382. }
  383. /*
  384. * Call registered handler. Pass the raw interrupt value to registered
  385. * handlers.
  386. */
  387. ret = handler(intr_raw, flags, handle, cookie);
  388. return (uint64_t) ret;
  389. }
  390. /*
  391. * Initialize the EL3 exception handling.
  392. */
  393. void __init ehf_init(void)
  394. {
  395. unsigned int flags = 0;
  396. int ret __unused;
  397. /* Ensure EL3 interrupts are supported */
  398. assert(plat_ic_has_interrupt_type(INTR_TYPE_EL3));
  399. /*
  400. * Make sure that priority water mark has enough bits to represent the
  401. * whole priority array.
  402. */
  403. assert(exception_data.num_priorities <= (sizeof(ehf_pri_bits_t) * 8U));
  404. assert(exception_data.ehf_priorities != NULL);
  405. /*
  406. * Bit 7 of GIC priority must be 0 for secure interrupts. This means
  407. * platforms must use at least 1 of the remaining 7 bits.
  408. */
  409. assert((exception_data.pri_bits >= 1U) ||
  410. (exception_data.pri_bits < 8U));
  411. /* Route EL3 interrupts when in Non-secure. */
  412. set_interrupt_rm_flag(flags, NON_SECURE);
  413. /* Route EL3 interrupts only when SPM_MM present in secure. */
  414. #if SPM_MM
  415. set_interrupt_rm_flag(flags, SECURE);
  416. #endif
  417. /* Register handler for EL3 interrupts */
  418. ret = register_interrupt_type_handler(INTR_TYPE_EL3,
  419. ehf_el3_interrupt_handler, flags);
  420. assert(ret == 0);
  421. }
  422. /*
  423. * Register a handler at the supplied priority. Registration is allowed only if
  424. * a handler hasn't been registered before, or one wasn't provided at build
  425. * time. The priority for which the handler is being registered must also accord
  426. * with the platform-supplied data.
  427. */
  428. void ehf_register_priority_handler(unsigned int pri, ehf_handler_t handler)
  429. {
  430. unsigned int idx;
  431. /* Sanity check for handler */
  432. assert(handler != NULL);
  433. /* Handler ought to be 4-byte aligned */
  434. assert((((uintptr_t) handler) & 3U) == 0U);
  435. /* Ensure we register for valid priority */
  436. idx = pri_to_idx(pri);
  437. assert(idx < exception_data.num_priorities);
  438. assert(IDX_TO_PRI(idx) == pri);
  439. /* Return failure if a handler was already registered */
  440. if (exception_data.ehf_priorities[idx].ehf_handler != EHF_NO_HANDLER_) {
  441. ERROR("Handler already registered for priority 0x%x\n", pri);
  442. panic();
  443. }
  444. /*
  445. * Install handler, and retain the valid bit. We assume that the handler
  446. * is 4-byte aligned, which is usually the case.
  447. */
  448. exception_data.ehf_priorities[idx].ehf_handler =
  449. (((uintptr_t) handler) | EHF_PRI_VALID_);
  450. EHF_LOG("register pri=0x%x handler=%p\n", pri, handler);
  451. }
  452. SUBSCRIBE_TO_EVENT(cm_entering_normal_world, ehf_entering_normal_world);
  453. SUBSCRIBE_TO_EVENT(cm_exited_normal_world, ehf_exited_normal_world);