blob: 8cd664e260915e343bcd2d644bdf9dff334c7bcf [file] [log] [blame]
Mate Toth-Pal4341de02018-10-02 12:55:47 +02001###################
2Secure IRQ handling
3###################
4
5The Armv8-M Architecture makes it possible to configure interrupts to target
6secure state.
7
8TF-M makes it possible for secure partitions to get notified of secure
9interrupts.
10
11By default TF-M sets up interrupts to target NS state. To configure an interrupt
12to target secure state and assign a handler to it, the manifest of the partition
13must be edited.
14
15See 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 Patelb3799e12019-10-08 17:23:12 +053029 "source": "5",
Mate Toth-Pal4341de02018-10-02 12:55:47 +020030 "signal": "DUAL_TIMER"
31 },
32 {
Jaykumar Pitambarbhai Patelb3799e12019-10-08 17:23:12 +053033 "source": "TFM_IRQ_LINE_TIMER_1",
Mate Toth-Pal4341de02018-10-02 12:55:47 +020034 "signal": "TIMER_1"
35 "tfm_irq_priority": 64,
36 }
37 ],
38
39 ...
40
41 }
42
43To set up a handler in a partition, the ``irqs`` node must be added. A single
44secure partition can have handlers registered for multiple IRQs, in this case
45the list ``irqs`` has multiple elements in it.
46
47An IRQ handler is defined by the following nodes:
48
Boris Deleticaf02a712020-09-09 15:17:22 +010049- ``source``: The IRQ number or the name of the IRQ line. With the name of the
50 IRQ line, there must be defined a macro in ``tfm_peripherals_def.h`` which is
51 substituted to the IRQ line num. The IRQ line nums and sources are defined by
52 each platform: for example, they are defined in ``platform_irq.h`` for the
53 Musca-S1 platform. When defining new macros in ``tfm_peripherals_def.h``, it
54 is important the macro name matches the platform's handler function for that
55 IRQ source.
56- ``signal``: The name of the signal for this IRQ.
Mate Toth-Pal4341de02018-10-02 12:55:47 +020057- ``tfm_irq_priority``: The priority of the IRQ. This number must be in the
58 range [0-255] inclusive. Please note that some of the less significant bits of
59 this value might be dropped based on the number of priority bits implemented
60 in the platform.
61
62.. important::
63
64 The name of the privileged interrupt handler is derived from the node
65 specifying the IRQ line number.
66
Jaykumar Pitambarbhai Patelb3799e12019-10-08 17:23:12 +053067 - In case ``source`` is IRQ number, the name of the handler becomes
68 ``void irq_<number>_Handler(void)``.
69 - In case ``source`` is defined IRQ macro, the name of the handler becomes
70 ``void <macro>_Handler(void)``.
Mate Toth-Pal4341de02018-10-02 12:55:47 +020071
Boris Deleticaf02a712020-09-09 15:17:22 +010072 This is important, because the derived name has to be present in the vector
73 table as the handler of the IRQ. The platform startup functions are specified
74 in the vector table defined in the platform secure startup file. The user
75 should verify the names of the generated handlers match for a given platform
76 IRQ.
Mate Toth-Pal4341de02018-10-02 12:55:47 +020077
78.. Note::
79
Jaykumar Pitambarbhai Patelb3799e12019-10-08 17:23:12 +053080 ``signal`` and ``source`` are mandatory.
Mate Toth-Pal4341de02018-10-02 12:55:47 +020081
82 ``tfm_irq_priority`` is optional. If ``tfm_irq_priority`` is not set for an
83 IRQ, the default is value is ``TFM_DEFAULT_SECURE_IRQ_PRIOTITY``.
84
85If an IRQ handler is registered, TF-M will:
86
Jaykumar Pitambarbhai Patelb3799e12019-10-08 17:23:12 +053087- Set the IRQ with number or macro to target secure state
88- Set the priority of IRQ with number or macro to ``tfm_irq_priority`` or to
89 the default.
Mate Toth-Pal4341de02018-10-02 12:55:47 +020090
91TF-M configures the interrupt lines to be disabled by default. Interrupts for a
92service can be enabled by the secure service by calling
93``void tfm_enable_irq(psa_signal_t irq_signal)``. The function can be called in
94the service init function.
95
Mate Toth-Pal4341de02018-10-02 12:55:47 +020096Library model
Galanakis, Minoscc8cde72019-11-11 15:41:23 +000097=============
Mate Toth-Pal4341de02018-10-02 12:55:47 +020098
99In Library model a function with the name derived from the value of the
Jaykumar Pitambarbhai Patelb3799e12019-10-08 17:23:12 +0530100``source`` property is generated. This function will be put in the vector table
101by the linker (as the handlers in the startup assembly are defined as weak
102symbols). The code generated for this function will forward the call to the
103function with the name of the value of the ``signal`` property post-fixed with
104``_isr``.
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200105
106.. hint::
107
108 for a signal ``"signal": "DUAL_TIMER"`` the name of the handler function is
109 ``DUAL_TIMER_isr``
110
111The signature of the IRQ handler in the partition must be the following:
112
113.. code-block:: c
114
115 void partition_irq_handler(void);
116
117The detailed description on how secure interrupt handling works in the Library
118model see
119`Secure Partition Interrupt Handling design document <https://developer.trustedfirmware.org/w/tf_m/design/secure_partition_interrupt_handling/>`_.
120
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200121IPC model
Galanakis, Minoscc8cde72019-11-11 15:41:23 +0000122=========
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200123
124The detailed description on how secure interrupt handling works in the IPC
125model, see the
126`PSA Firmware Framework and RoT Services specification <https://pages.arm.com/psa-resources-ff.html>`_.
127
Galanakis, Minoscc8cde72019-11-11 15:41:23 +0000128**********************
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200129Implementation details
Galanakis, Minoscc8cde72019-11-11 15:41:23 +0000130**********************
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200131
Galanakis, Minosf56baf62019-11-11 13:57:42 +0000132Library model implementation
Galanakis, Minoscc8cde72019-11-11 15:41:23 +0000133============================
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200134
135As a result of the function call like behaviour of secure services in library
136model, some information that is critical for the SPM to keep track of partition
137states, is stored on the stack of the active partitions. When an interrupt
138happens, and a handler partition is set to running state, it has access to its
139whole stack, and could corrupt the data stacked by the SPM. To prevent this, a
140separate Context stack is introduced for each secure partition, that is used by
141the SPM to save this information before starting to execute secure partition
142code.
143
144A stack frame to this context stack is pushed when the execution in the
145partition is interrupted, and when a handler in the partition interrupts another
146service. So the maximal stack usage can happen in the following situation:
147
148Consider secure partition 'A'. 'A' is running, and then it is interrupted by
149an other partition. Then the lowest priority interrupt of 'A' is triggered.
150Then before the handler returns, the partition is interrupted by another
151partition's handler. Then before the running handler returns, the second
152lowest interrupt of 'A' is triggered. This can go until the highest priority
153interrupt of 'A' is triggered, and then this last handler is interrupted. At
154this point the context stack looks like this:
155
156.. code-block::
157
158 +------------+
159 | [intr_ctx] |
160 | [hndl_ctx] |
161 | . |
162 | . |
163 | . |
164 | [intr_ctx] |
165 | [hndl_ctx] |
166 | [intr_ctx] |
167 +------------+
168
169 Legend:
170 [intr_ctx]: Frame pushed when the partition is interrupted
171 [hndl_ctx]: Frame pushed when the partition is handling an interrupt
172
173So the max stack size can be calculated as a function of the IRQ count of 'A':
174
175.. code-block::
176
177
178 max_stack_size = intr_ctx_size + (IRQ_CNT * (intr_ctx_size + hndl_ctx_size))
179
180--------------
181
Boris Deleticaf02a712020-09-09 15:17:22 +0100182*Copyright (c) 2018-2020, Arm Limited. All rights reserved.*