Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 1 | ################### |
| 2 | Secure IRQ handling |
| 3 | ################### |
| 4 | |
| 5 | The Armv8-M Architecture makes it possible to configure interrupts to target |
| 6 | secure state. |
| 7 | |
| 8 | TF-M makes it possible for secure partitions to get notified of secure |
| 9 | interrupts. |
| 10 | |
| 11 | By default TF-M sets up interrupts to target NS state. To configure an interrupt |
| 12 | to target secure state and assign a handler to it, the manifest of the partition |
| 13 | must be edited. |
| 14 | |
| 15 | See the following example: |
| 16 | |
| 17 | |
| 18 | .. code-block:: yaml |
| 19 | |
| 20 | { |
| 21 | "name": "...", |
| 22 | "type": "...", |
| 23 | "priority": "...", |
| 24 | |
| 25 | ... |
| 26 | |
| 27 | "irqs": [ |
| 28 | { |
Jaykumar Pitambarbhai Patel | b3799e1 | 2019-10-08 17:23:12 +0530 | [diff] [blame] | 29 | "source": "5", |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 30 | "signal": "DUAL_TIMER" |
| 31 | }, |
| 32 | { |
Jaykumar Pitambarbhai Patel | b3799e1 | 2019-10-08 17:23:12 +0530 | [diff] [blame] | 33 | "source": "TFM_IRQ_LINE_TIMER_1", |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 34 | "signal": "TIMER_1" |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 35 | } |
| 36 | ], |
| 37 | |
| 38 | ... |
| 39 | |
| 40 | } |
| 41 | |
| 42 | To set up a handler in a partition, the ``irqs`` node must be added. A single |
| 43 | secure partition can have handlers registered for multiple IRQs, in this case |
| 44 | the list ``irqs`` has multiple elements in it. |
| 45 | |
| 46 | An IRQ handler is defined by the following nodes: |
| 47 | |
Boris Deletic | af02a71 | 2020-09-09 15:17:22 +0100 | [diff] [blame] | 48 | - ``source``: The IRQ number or the name of the IRQ line. With the name of the |
| 49 | IRQ line, there must be defined a macro in ``tfm_peripherals_def.h`` which is |
| 50 | substituted to the IRQ line num. The IRQ line nums and sources are defined by |
| 51 | each platform: for example, they are defined in ``platform_irq.h`` for the |
| 52 | Musca-S1 platform. When defining new macros in ``tfm_peripherals_def.h``, it |
| 53 | is important the macro name matches the platform's handler function for that |
| 54 | IRQ source. |
| 55 | - ``signal``: The name of the signal for this IRQ. |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 56 | |
| 57 | .. important:: |
| 58 | |
| 59 | The name of the privileged interrupt handler is derived from the node |
| 60 | specifying the IRQ line number. |
| 61 | |
Jaykumar Pitambarbhai Patel | b3799e1 | 2019-10-08 17:23:12 +0530 | [diff] [blame] | 62 | - In case ``source`` is IRQ number, the name of the handler becomes |
| 63 | ``void irq_<number>_Handler(void)``. |
| 64 | - In case ``source`` is defined IRQ macro, the name of the handler becomes |
| 65 | ``void <macro>_Handler(void)``. |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 66 | |
Boris Deletic | af02a71 | 2020-09-09 15:17:22 +0100 | [diff] [blame] | 67 | This is important, because the derived name has to be present in the vector |
| 68 | table as the handler of the IRQ. The platform startup functions are specified |
| 69 | in the vector table defined in the platform secure startup file. The user |
| 70 | should verify the names of the generated handlers match for a given platform |
| 71 | IRQ. |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 72 | |
| 73 | .. Note:: |
| 74 | |
Jaykumar Pitambarbhai Patel | b3799e1 | 2019-10-08 17:23:12 +0530 | [diff] [blame] | 75 | ``signal`` and ``source`` are mandatory. |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 76 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 77 | If an IRQ handler is registered, TF-M will: |
| 78 | |
Jaykumar Pitambarbhai Patel | b3799e1 | 2019-10-08 17:23:12 +0530 | [diff] [blame] | 79 | - Set the IRQ with number or macro to target secure state |
Kevin Peng | 0979b0e | 2021-06-15 10:54:53 +0800 | [diff] [blame] | 80 | - Set the priority of IRQ, the number is platform dependent. Platforms can |
| 81 | decide the priorities of each IRQ. |
| 82 | |
| 83 | .. Note:: |
| 84 | |
| 85 | The priority value for IRQ must be smaller than the value of PendSV, which is |
| 86 | 0x80. |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 87 | |
| 88 | TF-M configures the interrupt lines to be disabled by default. Interrupts for a |
| 89 | service can be enabled by the secure service by calling |
| 90 | ``void tfm_enable_irq(psa_signal_t irq_signal)``. The function can be called in |
| 91 | the service init function. |
| 92 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 93 | Library model |
Galanakis, Minos | cc8cde7 | 2019-11-11 15:41:23 +0000 | [diff] [blame] | 94 | ============= |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 95 | |
| 96 | In Library model a function with the name derived from the value of the |
Jaykumar Pitambarbhai Patel | b3799e1 | 2019-10-08 17:23:12 +0530 | [diff] [blame] | 97 | ``source`` property is generated. This function will be put in the vector table |
| 98 | by the linker (as the handlers in the startup assembly are defined as weak |
| 99 | symbols). The code generated for this function will forward the call to the |
| 100 | function with the name of the value of the ``signal`` property post-fixed with |
| 101 | ``_isr``. |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 102 | |
| 103 | .. hint:: |
| 104 | |
| 105 | for a signal ``"signal": "DUAL_TIMER"`` the name of the handler function is |
| 106 | ``DUAL_TIMER_isr`` |
| 107 | |
| 108 | The signature of the IRQ handler in the partition must be the following: |
| 109 | |
| 110 | .. code-block:: c |
| 111 | |
| 112 | void partition_irq_handler(void); |
| 113 | |
| 114 | The detailed description on how secure interrupt handling works in the Library |
| 115 | model see |
| 116 | `Secure Partition Interrupt Handling design document <https://developer.trustedfirmware.org/w/tf_m/design/secure_partition_interrupt_handling/>`_. |
| 117 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 118 | IPC model |
Galanakis, Minos | cc8cde7 | 2019-11-11 15:41:23 +0000 | [diff] [blame] | 119 | ========= |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 120 | |
| 121 | The detailed description on how secure interrupt handling works in the IPC |
| 122 | model, see the |
| 123 | `PSA Firmware Framework and RoT Services specification <https://pages.arm.com/psa-resources-ff.html>`_. |
| 124 | |
Galanakis, Minos | cc8cde7 | 2019-11-11 15:41:23 +0000 | [diff] [blame] | 125 | ********************** |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 126 | Implementation details |
Galanakis, Minos | cc8cde7 | 2019-11-11 15:41:23 +0000 | [diff] [blame] | 127 | ********************** |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 128 | |
Galanakis, Minos | f56baf6 | 2019-11-11 13:57:42 +0000 | [diff] [blame] | 129 | Library model implementation |
Galanakis, Minos | cc8cde7 | 2019-11-11 15:41:23 +0000 | [diff] [blame] | 130 | ============================ |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 131 | |
| 132 | As a result of the function call like behaviour of secure services in library |
| 133 | model, some information that is critical for the SPM to keep track of partition |
| 134 | states, is stored on the stack of the active partitions. When an interrupt |
| 135 | happens, and a handler partition is set to running state, it has access to its |
| 136 | whole stack, and could corrupt the data stacked by the SPM. To prevent this, a |
| 137 | separate Context stack is introduced for each secure partition, that is used by |
| 138 | the SPM to save this information before starting to execute secure partition |
| 139 | code. |
| 140 | |
| 141 | A stack frame to this context stack is pushed when the execution in the |
| 142 | partition is interrupted, and when a handler in the partition interrupts another |
| 143 | service. So the maximal stack usage can happen in the following situation: |
| 144 | |
| 145 | Consider secure partition 'A'. 'A' is running, and then it is interrupted by |
| 146 | an other partition. Then the lowest priority interrupt of 'A' is triggered. |
| 147 | Then before the handler returns, the partition is interrupted by another |
| 148 | partition's handler. Then before the running handler returns, the second |
| 149 | lowest interrupt of 'A' is triggered. This can go until the highest priority |
| 150 | interrupt of 'A' is triggered, and then this last handler is interrupted. At |
| 151 | this point the context stack looks like this: |
| 152 | |
David Hu | 050756f | 2021-04-13 17:53:01 +0800 | [diff] [blame] | 153 | .. code-block:: rst |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 154 | |
| 155 | +------------+ |
| 156 | | [intr_ctx] | |
| 157 | | [hndl_ctx] | |
| 158 | | . | |
| 159 | | . | |
| 160 | | . | |
| 161 | | [intr_ctx] | |
| 162 | | [hndl_ctx] | |
| 163 | | [intr_ctx] | |
| 164 | +------------+ |
| 165 | |
| 166 | Legend: |
| 167 | [intr_ctx]: Frame pushed when the partition is interrupted |
| 168 | [hndl_ctx]: Frame pushed when the partition is handling an interrupt |
| 169 | |
| 170 | So the max stack size can be calculated as a function of the IRQ count of 'A': |
| 171 | |
David Hu | 050756f | 2021-04-13 17:53:01 +0800 | [diff] [blame] | 172 | .. code-block:: c |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 173 | |
| 174 | max_stack_size = intr_ctx_size + (IRQ_CNT * (intr_ctx_size + hndl_ctx_size)) |
| 175 | |
| 176 | -------------- |
| 177 | |
David Hu | 050756f | 2021-04-13 17:53:01 +0800 | [diff] [blame] | 178 | *Copyright (c) 2018-2021, Arm Limited. All rights reserved.* |