blob: 58130cde0e8feb7d758d32383c496ded9a677a0c [file] [log] [blame]
Dan Handley610e7e12018-03-01 18:44:00 +00001Trusted Firmware-A interrupt management design guide
2====================================================
Douglas Raillardd7c21b72017-06-28 15:23:03 +01003
4
5.. section-numbering::
6 :suffix: .
7
8.. contents::
9
10This framework is responsible for managing interrupts routed to EL3. It also
11allows EL3 software to configure the interrupt routing behavior. Its main
12objective is to implement the following two requirements.
13
14#. It should be possible to route interrupts meant to be handled by secure
15 software (Secure interrupts) to EL3, when execution is in non-secure state
16 (normal world). The framework should then take care of handing control of
17 the interrupt to either software in EL3 or Secure-EL1 depending upon the
18 software configuration and the GIC implementation. This requirement ensures
19 that secure interrupts are under the control of the secure software with
20 respect to their delivery and handling without the possibility of
21 intervention from non-secure software.
22
23#. It should be possible to route interrupts meant to be handled by
24 non-secure software (Non-secure interrupts) to the last executed exception
25 level in the normal world when the execution is in secure world at
26 exception levels lower than EL3. This could be done with or without the
27 knowledge of software executing in Secure-EL1/Secure-EL0. The choice of
28 approach should be governed by the secure software. This requirement
29 ensures that non-secure software is able to execute in tandem with the
30 secure software without overriding it.
31
32Concepts
33--------
34
35Interrupt types
36~~~~~~~~~~~~~~~
37
38The framework categorises an interrupt to be one of the following depending upon
39the exception level(s) it is handled in.
40
41#. Secure EL1 interrupt. This type of interrupt can be routed to EL3 or
42 Secure-EL1 depending upon the security state of the current execution
43 context. It is always handled in Secure-EL1.
44
45#. Non-secure interrupt. This type of interrupt can be routed to EL3,
46 Secure-EL1, Non-secure EL1 or EL2 depending upon the security state of the
47 current execution context. It is always handled in either Non-secure EL1
48 or EL2.
49
50#. EL3 interrupt. This type of interrupt can be routed to EL3 or Secure-EL1
51 depending upon the security state of the current execution context. It is
52 always handled in EL3.
53
54The following constants define the various interrupt types in the framework
55implementation.
56
57::
58
59 #define INTR_TYPE_S_EL1 0
60 #define INTR_TYPE_EL3 1
61 #define INTR_TYPE_NS 2
62
63Routing model
64~~~~~~~~~~~~~
65
66A type of interrupt can be either generated as an FIQ or an IRQ. The target
67exception level of an interrupt type is configured through the FIQ and IRQ bits
68in the Secure Configuration Register at EL3 (``SCR_EL3.FIQ`` and ``SCR_EL3.IRQ``
69bits). When ``SCR_EL3.FIQ``\ =1, FIQs are routed to EL3. Otherwise they are routed
70to the First Exception Level (FEL) capable of handling interrupts. When
71``SCR_EL3.IRQ``\ =1, IRQs are routed to EL3. Otherwise they are routed to the
72FEL. This register is configured independently by EL3 software for each security
73state prior to entry into a lower exception level in that security state.
74
75A routing model for a type of interrupt (generated as FIQ or IRQ) is defined as
76its target exception level for each security state. It is represented by a
77single bit for each security state. A value of ``0`` means that the interrupt
78should be routed to the FEL. A value of ``1`` means that the interrupt should be
79routed to EL3. A routing model is applicable only when execution is not in EL3.
80
81The default routing model for an interrupt type is to route it to the FEL in
82either security state.
83
84Valid routing models
85~~~~~~~~~~~~~~~~~~~~
86
87The framework considers certain routing models for each type of interrupt to be
88incorrect as they conflict with the requirements mentioned in Section 1. The
89following sub-sections describe all the possible routing models and specify
90which ones are valid or invalid. EL3 interrupts are currently supported only
Dan Handley610e7e12018-03-01 18:44:00 +000091for GIC version 3.0 (Arm GICv3) and only the Secure-EL1 and Non-secure interrupt
Sandrine Bailleux073c1b32019-02-25 14:02:26 +010092types are supported for GIC version 2.0 (Arm GICv2) (see `Assumptions in
93Interrupt Management Framework`_). The terminology used in the following
94sub-sections is explained below.
Douglas Raillardd7c21b72017-06-28 15:23:03 +010095
96#. **CSS**. Current Security State. ``0`` when secure and ``1`` when non-secure
97
98#. **TEL3**. Target Exception Level 3. ``0`` when targeted to the FEL. ``1`` when
99 targeted to EL3.
100
101Secure-EL1 interrupts
102^^^^^^^^^^^^^^^^^^^^^
103
104#. **CSS=0, TEL3=0**. Interrupt is routed to the FEL when execution is in
105 secure state. This is a valid routing model as secure software is in
106 control of handling secure interrupts.
107
108#. **CSS=0, TEL3=1**. Interrupt is routed to EL3 when execution is in secure
109 state. This is a valid routing model as secure software in EL3 can
110 handover the interrupt to Secure-EL1 for handling.
111
112#. **CSS=1, TEL3=0**. Interrupt is routed to the FEL when execution is in
113 non-secure state. This is an invalid routing model as a secure interrupt
114 is not visible to the secure software which violates the motivation behind
Dan Handley610e7e12018-03-01 18:44:00 +0000115 the Arm Security Extensions.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100116
117#. **CSS=1, TEL3=1**. Interrupt is routed to EL3 when execution is in
118 non-secure state. This is a valid routing model as secure software in EL3
119 can handover the interrupt to Secure-EL1 for handling.
120
121Non-secure interrupts
122^^^^^^^^^^^^^^^^^^^^^
123
124#. **CSS=0, TEL3=0**. Interrupt is routed to the FEL when execution is in
125 secure state. This allows the secure software to trap non-secure
126 interrupts, perform its book-keeping and hand the interrupt to the
127 non-secure software through EL3. This is a valid routing model as secure
128 software is in control of how its execution is preempted by non-secure
129 interrupts.
130
131#. **CSS=0, TEL3=1**. Interrupt is routed to EL3 when execution is in secure
132 state. This is a valid routing model as secure software in EL3 can save
133 the state of software in Secure-EL1/Secure-EL0 before handing the
134 interrupt to non-secure software. This model requires additional
135 coordination between Secure-EL1 and EL3 software to ensure that the
136 former's state is correctly saved by the latter.
137
138#. **CSS=1, TEL3=0**. Interrupt is routed to FEL when execution is in
Jeenu Viswambharan61c5bc72018-01-10 14:56:03 +0000139 non-secure state. This is a valid routing model as a non-secure interrupt
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100140 is handled by non-secure software.
141
142#. **CSS=1, TEL3=1**. Interrupt is routed to EL3 when execution is in
143 non-secure state. This is an invalid routing model as there is no valid
144 reason to route the interrupt to EL3 software and then hand it back to
145 non-secure software for handling.
146
147EL3 interrupts
148^^^^^^^^^^^^^^
149
150#. **CSS=0, TEL3=0**. Interrupt is routed to the FEL when execution is in
151 Secure-EL1/Secure-EL0. This is a valid routing model as secure software
152 in Secure-EL1/Secure-EL0 is in control of how its execution is preempted
153 by EL3 interrupt and can handover the interrupt to EL3 for handling.
154
Jeenu Viswambharanf4194ee2018-01-10 15:00:20 +0000155 However, when ``EL3_EXCEPTION_HANDLING`` is ``1``, this routing model is
156 invalid as EL3 interrupts are unconditionally routed to EL3, and EL3
Jeenu Viswambharancbb40d52017-10-18 14:30:53 +0100157 interrupts will always preempt Secure EL1/EL0 execution. See `exception
158 handling`__ documentation.
159
160 .. __: exception-handling.rst#interrupt-handling
Jeenu Viswambharanf4194ee2018-01-10 15:00:20 +0000161
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100162#. **CSS=0, TEL3=1**. Interrupt is routed to EL3 when execution is in
163 Secure-EL1/Secure-EL0. This is a valid routing model as secure software
164 in EL3 can handle the interrupt.
165
166#. **CSS=1, TEL3=0**. Interrupt is routed to the FEL when execution is in
167 non-secure state. This is an invalid routing model as a secure interrupt
168 is not visible to the secure software which violates the motivation behind
Dan Handley610e7e12018-03-01 18:44:00 +0000169 the Arm Security Extensions.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100170
171#. **CSS=1, TEL3=1**. Interrupt is routed to EL3 when execution is in
172 non-secure state. This is a valid routing model as secure software in EL3
173 can handle the interrupt.
174
175Mapping of interrupt type to signal
176~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177
178The framework is meant to work with any interrupt controller implemented by a
179platform. A interrupt controller could generate a type of interrupt as either an
180FIQ or IRQ signal to the CPU depending upon the current security state. The
181mapping between the type and signal is known only to the platform. The framework
182uses this information to determine whether the IRQ or the FIQ bit should be
183programmed in ``SCR_EL3`` while applying the routing model for a type of
184interrupt. The platform provides this information through the
185``plat_interrupt_type_to_line()`` API (described in the
Dan Handley610e7e12018-03-01 18:44:00 +0000186`Porting Guide`_). For example, on the FVP port when the platform uses an Arm GICv2
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100187interrupt controller, Secure-EL1 interrupts are signaled through the FIQ signal
188while Non-secure interrupts are signaled through the IRQ signal. This applies
189when execution is in either security state.
190
191Effect of mapping of several interrupt types to one signal
192^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
193
194It should be noted that if more than one interrupt type maps to a single
195interrupt signal, and if any one of the interrupt type sets **TEL3=1** for a
196particular security state, then interrupt signal will be routed to EL3 when in
197that security state. This means that all the other interrupt types using the
198same interrupt signal will be forced to the same routing model. This should be
199borne in mind when choosing the routing model for an interrupt type.
200
Dan Handley610e7e12018-03-01 18:44:00 +0000201For example, in Arm GICv3, when the execution context is Secure-EL1/
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100202Secure-EL0, both the EL3 and the non secure interrupt types map to the FIQ
203signal. So if either one of the interrupt type sets the routing model so
204that **TEL3=1** when **CSS=0**, the FIQ bit in ``SCR_EL3`` will be programmed to
205route the FIQ signal to EL3 when executing in Secure-EL1/Secure-EL0, thereby
206effectively routing the other interrupt type also to EL3.
207
208Assumptions in Interrupt Management Framework
209---------------------------------------------
210
211The framework makes the following assumptions to simplify its implementation.
212
213#. Although the framework has support for 2 types of secure interrupts (EL3
214 and Secure-EL1 interrupt), only interrupt controller architectures
Dan Handley610e7e12018-03-01 18:44:00 +0000215 like Arm GICv3 has architectural support for EL3 interrupts in the form of
216 Group 0 interrupts. In Arm GICv2, all secure interrupts are assumed to be
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100217 handled in Secure-EL1. They can be delivered to Secure-EL1 via EL3 but they
218 cannot be handled in EL3.
219
220#. Interrupt exceptions (``PSTATE.I`` and ``F`` bits) are masked during execution
221 in EL3.
222
Jeenu Viswambharan61c5bc72018-01-10 14:56:03 +0000223#. Interrupt management: the following sections describe how interrupts are
224 managed by the interrupt handling framework. This entails:
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100225
Jeenu Viswambharan61c5bc72018-01-10 14:56:03 +0000226 #. Providing an interface to allow registration of a handler and
227 specification of the routing model for a type of interrupt.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100228
Jeenu Viswambharan61c5bc72018-01-10 14:56:03 +0000229 #. Implementing support to hand control of an interrupt type to its
230 registered handler when the interrupt is generated.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100231
232Both aspects of interrupt management involve various components in the secure
233software stack spanning from EL3 to Secure-EL1. These components are described
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100234in the section `Software components`_. The framework stores information
235associated with each type of interrupt in the following data structure.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100236
237.. code:: c
238
239 typedef struct intr_type_desc {
240 interrupt_type_handler_t handler;
241 uint32_t flags;
242 uint32_t scr_el3[2];
243 } intr_type_desc_t;
244
245The ``flags`` field stores the routing model for the interrupt type in
246bits[1:0]. Bit[0] stores the routing model when execution is in the secure
247state. Bit[1] stores the routing model when execution is in the non-secure
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100248state. As mentioned in Section `Routing model`_, a value of ``0`` implies that
249the interrupt should be targeted to the FEL. A value of ``1`` implies that it
250should be targeted to EL3. The remaining bits are reserved and SBZ. The helper
251macro ``set_interrupt_rm_flag()`` should be used to set the bits in the
252``flags`` parameter.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100253
254The ``scr_el3[2]`` field also stores the routing model but as a mapping of the
255model in the ``flags`` field to the corresponding bit in the ``SCR_EL3`` for each
256security state.
257
258The framework also depends upon the platform port to configure the interrupt
259controller to distinguish between secure and non-secure interrupts. The platform
260is expected to be aware of the secure devices present in the system and their
261associated interrupt numbers. It should configure the interrupt controller to
262enable the secure interrupts, ensure that their priority is always higher than
263the non-secure interrupts and target them to the primary CPU. It should also
264export the interface described in the `Porting Guide`_ to enable
265handling of interrupts.
266
Dan Handley610e7e12018-03-01 18:44:00 +0000267In the remainder of this document, for the sake of simplicity a Arm GICv2 system
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100268is considered and it is assumed that the FIQ signal is used to generate Secure-EL1
269interrupts and the IRQ signal is used to generate non-secure interrupts in either
270security state. EL3 interrupts are not considered.
271
272Software components
273-------------------
274
275Roles and responsibilities for interrupt management are sub-divided between the
276following components of software running in EL3 and Secure-EL1. Each component is
277briefly described below.
278
Dan Handley610e7e12018-03-01 18:44:00 +0000279#. EL3 Runtime Firmware. This component is common to all ports of TF-A.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100280
281#. Secure Payload Dispatcher (SPD) service. This service interfaces with the
282 Secure Payload (SP) software which runs in Secure-EL1/Secure-EL0 and is
283 responsible for switching execution between secure and non-secure states.
284 A switch is triggered by a Secure Monitor Call and it uses the APIs
285 exported by the Context management library to implement this functionality.
286 Switching execution between the two security states is a requirement for
287 interrupt management as well. This results in a significant dependency on
Dan Handley610e7e12018-03-01 18:44:00 +0000288 the SPD service. TF-A implements an example Test Secure Payload Dispatcher
289 (TSPD) service.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100290
291 An SPD service plugs into the EL3 runtime firmware and could be common to
Dan Handley610e7e12018-03-01 18:44:00 +0000292 some ports of TF-A.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100293
294#. Secure Payload (SP). On a production system, the Secure Payload corresponds
295 to a Secure OS which runs in Secure-EL1/Secure-EL0. It interfaces with the
Dan Handley610e7e12018-03-01 18:44:00 +0000296 SPD service to manage communication with non-secure software. TF-A
297 implements an example secure payload called Test Secure Payload (TSP)
298 which runs only in Secure-EL1.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100299
Dan Handley610e7e12018-03-01 18:44:00 +0000300 A Secure payload implementation could be common to some ports of TF-A,
301 just like the SPD service.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100302
303Interrupt registration
304----------------------
305
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100306This section describes in detail the role of each software component (see
307`Software components`_) during the registration of a handler for an interrupt
308type.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100309
310EL3 runtime firmware
311~~~~~~~~~~~~~~~~~~~~
312
313This component declares the following prototype for a handler of an interrupt type.
314
315.. code:: c
316
317 typedef uint64_t (*interrupt_type_handler_t)(uint32_t id,
318 uint32_t flags,
319 void *handle,
320 void *cookie);
321
322The ``id`` is parameter is reserved and could be used in the future for passing
323the interrupt id of the highest pending interrupt only if there is a foolproof
324way of determining the id. Currently it contains ``INTR_ID_UNAVAILABLE``.
325
326The ``flags`` parameter contains miscellaneous information as follows.
327
328#. Security state, bit[0]. This bit indicates the security state of the lower
329 exception level when the interrupt was generated. A value of ``1`` means
330 that it was in the non-secure state. A value of ``0`` indicates that it was
331 in the secure state. This bit can be used by the handler to ensure that
332 interrupt was generated and routed as per the routing model specified
333 during registration.
334
335#. Reserved, bits[31:1]. The remaining bits are reserved for future use.
336
337The ``handle`` parameter points to the ``cpu_context`` structure of the current CPU
338for the security state specified in the ``flags`` parameter.
339
340Once the handler routine completes, execution will return to either the secure
341or non-secure state. The handler routine must return a pointer to
342``cpu_context`` structure of the current CPU for the target security state. On
343AArch64, this return value is currently ignored by the caller as the
344appropriate ``cpu_context`` to be used is expected to be set by the handler
345via the context management library APIs.
346A portable interrupt handler implementation must set the target context both in
347the structure pointed to by the returned pointer and via the context management
348library APIs. The handler should treat all error conditions as critical errors
349and take appropriate action within its implementation e.g. use assertion
350failures.
351
352The runtime firmware provides the following API for registering a handler for a
353particular type of interrupt. A Secure Payload Dispatcher service should use
354this API to register a handler for Secure-EL1 and optionally for non-secure
355interrupts. This API also requires the caller to specify the routing model for
356the type of interrupt.
357
358.. code:: c
359
360 int32_t register_interrupt_type_handler(uint32_t type,
361 interrupt_type_handler handler,
362 uint64_t flags);
363
364The ``type`` parameter can be one of the three interrupt types listed above i.e.
365``INTR_TYPE_S_EL1``, ``INTR_TYPE_NS`` & ``INTR_TYPE_EL3``. The ``flags`` parameter
366is as described in Section 2.
367
368The function will return ``0`` upon a successful registration. It will return
369``-EALREADY`` in case a handler for the interrupt type has already been
370registered. If the ``type`` is unrecognised or the ``flags`` or the ``handler`` are
371invalid it will return ``-EINVAL``.
372
373Interrupt routing is governed by the configuration of the ``SCR_EL3.FIQ/IRQ`` bits
374prior to entry into a lower exception level in either security state. The
375context management library maintains a copy of the ``SCR_EL3`` system register for
376each security state in the ``cpu_context`` structure of each CPU. It exports the
377following APIs to let EL3 Runtime Firmware program and retrieve the routing
378model for each security state for the current CPU. The value of ``SCR_EL3`` stored
379in the ``cpu_context`` is used by the ``el3_exit()`` function to program the
380``SCR_EL3`` register prior to returning from the EL3 exception level.
381
382.. code:: c
383
384 uint32_t cm_get_scr_el3(uint32_t security_state);
385 void cm_write_scr_el3_bit(uint32_t security_state,
386 uint32_t bit_pos,
387 uint32_t value);
388
389``cm_get_scr_el3()`` returns the value of the ``SCR_EL3`` register for the specified
390security state of the current CPU. ``cm_write_scr_el3()`` writes a ``0`` or ``1`` to
391the bit specified by ``bit_pos``. ``register_interrupt_type_handler()`` invokes
392``set_routing_model()`` API which programs the ``SCR_EL3`` according to the routing
393model using the ``cm_get_scr_el3()`` and ``cm_write_scr_el3_bit()`` APIs.
394
395It is worth noting that in the current implementation of the framework, the EL3
396runtime firmware is responsible for programming the routing model. The SPD is
397responsible for ensuring that the routing model has been adhered to upon
398receiving an interrupt.
399
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100400.. _spd-int-registration:
401
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100402Secure payload dispatcher
403~~~~~~~~~~~~~~~~~~~~~~~~~
404
405A SPD service is responsible for determining and maintaining the interrupt
406routing model supported by itself and the Secure Payload. It is also responsible
407for ferrying interrupts between secure and non-secure software depending upon
408the routing model. It could determine the routing model at build time or at
409runtime. It must use this information to register a handler for each interrupt
410type using the ``register_interrupt_type_handler()`` API in EL3 runtime firmware.
411
412If the routing model is not known to the SPD service at build time, then it must
413be provided by the SP as the result of its initialisation. The SPD should
414program the routing model only after SP initialisation has completed e.g. in the
415SPD initialisation function pointed to by the ``bl32_init`` variable.
416
417The SPD should determine the mechanism to pass control to the Secure Payload
418after receiving an interrupt from the EL3 runtime firmware. This information
419could either be provided to the SPD service at build time or by the SP at
420runtime.
421
422Test secure payload dispatcher behavior
423~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
424
Jeenu Viswambharan2f40f322018-01-11 14:30:22 +0000425**Note:** where this document discusses ``TSP_NS_INTR_ASYNC_PREEMPT`` as being
426``1``, the same results also apply when ``EL3_EXCEPTION_HANDLING`` is ``1``.
427
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100428The TSPD only handles Secure-EL1 interrupts and is provided with the following
429routing model at build time.
430
431- Secure-EL1 interrupts are routed to EL3 when execution is in non-secure
432 state and are routed to the FEL when execution is in the secure state
433 i.e **CSS=0, TEL3=0** & **CSS=1, TEL3=1** for Secure-EL1 interrupts
434
435- When the build flag ``TSP_NS_INTR_ASYNC_PREEMPT`` is zero, the default routing
436 model is used for non-secure interrupts. They are routed to the FEL in
437 either security state i.e **CSS=0, TEL3=0** & **CSS=1, TEL3=0** for
438 Non-secure interrupts.
439
440- When the build flag ``TSP_NS_INTR_ASYNC_PREEMPT`` is defined to 1, then the
441 non secure interrupts are routed to EL3 when execution is in secure state
442 i.e **CSS=0, TEL3=1** for non-secure interrupts. This effectively preempts
443 Secure-EL1. The default routing model is used for non secure interrupts in
444 non-secure state. i.e **CSS=1, TEL3=0**.
445
446It performs the following actions in the ``tspd_init()`` function to fulfill the
447requirements mentioned earlier.
448
449#. It passes control to the Test Secure Payload to perform its
450 initialisation. The TSP provides the address of the vector table
451 ``tsp_vectors`` in the SP which also includes the handler for Secure-EL1
452 interrupts in the ``sel1_intr_entry`` field. The TSPD passes control to the TSP at
453 this address when it receives a Secure-EL1 interrupt.
454
455 The handover agreement between the TSP and the TSPD requires that the TSPD
456 masks all interrupts (``PSTATE.DAIF`` bits) when it calls
457 ``tsp_sel1_intr_entry()``. The TSP has to preserve the callee saved general
Sandrine Bailleux15530dd2019-02-08 15:26:36 +0100458 purpose, SP_EL1/Secure-EL0, LR, VFP and system registers. It can use
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100459 ``x0-x18`` to enable its C runtime.
460
461#. The TSPD implements a handler function for Secure-EL1 interrupts. This
462 function is registered with the EL3 runtime firmware using the
463 ``register_interrupt_type_handler()`` API as follows
464
465 .. code:: c
466
467 /* Forward declaration */
468 interrupt_type_handler tspd_secure_el1_interrupt_handler;
469 int32_t rc, flags = 0;
470 set_interrupt_rm_flag(flags, NON_SECURE);
471 rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
472 tspd_secure_el1_interrupt_handler,
473 flags);
474 if (rc)
475 panic();
476
477#. When the build flag ``TSP_NS_INTR_ASYNC_PREEMPT`` is defined to 1, the TSPD
478 implements a handler function for non-secure interrupts. This function is
479 registered with the EL3 runtime firmware using the
480 ``register_interrupt_type_handler()`` API as follows
481
482 .. code:: c
483
484 /* Forward declaration */
485 interrupt_type_handler tspd_ns_interrupt_handler;
486 int32_t rc, flags = 0;
487 set_interrupt_rm_flag(flags, SECURE);
488 rc = register_interrupt_type_handler(INTR_TYPE_NS,
489 tspd_ns_interrupt_handler,
490 flags);
491 if (rc)
492 panic();
493
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100494.. _sp-int-registration:
495
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100496Secure payload
497~~~~~~~~~~~~~~
498
499A Secure Payload must implement an interrupt handling framework at Secure-EL1
500(Secure-EL1 IHF) to support its chosen interrupt routing model. Secure payload
501execution will alternate between the below cases.
502
503#. In the code where IRQ, FIQ or both interrupts are enabled, if an interrupt
504 type is targeted to the FEL, then it will be routed to the Secure-EL1
505 exception vector table. This is defined as the **asynchronous mode** of
506 handling interrupts. This mode applies to both Secure-EL1 and non-secure
507 interrupts.
508
509#. In the code where both interrupts are disabled, if an interrupt type is
510 targeted to the FEL, then execution will eventually migrate to the
511 non-secure state. Any non-secure interrupts will be handled as described
512 in the routing model where **CSS=1 and TEL3=0**. Secure-EL1 interrupts
513 will be routed to EL3 (as per the routing model where **CSS=1 and
514 TEL3=1**) where the SPD service will hand them to the SP. This is defined
515 as the **synchronous mode** of handling interrupts.
516
517The interrupt handling framework implemented by the SP should support one or
518both these interrupt handling models depending upon the chosen routing model.
519
520The following list briefly describes how the choice of a valid routing model
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100521(see `Valid routing models`_) effects the implementation of the Secure-EL1
522IHF. If the choice of the interrupt routing model is not known to the SPD
523service at compile time, then the SP should pass this information to the SPD
524service at runtime during its initialisation phase.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100525
Dan Handley610e7e12018-03-01 18:44:00 +0000526As mentioned earlier, an Arm GICv2 system is considered and it is assumed that
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100527the FIQ signal is used to generate Secure-EL1 interrupts and the IRQ signal
528is used to generate non-secure interrupts in either security state.
529
530Secure payload IHF design w.r.t secure-EL1 interrupts
531^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
532
533#. **CSS=0, TEL3=0**. If ``PSTATE.F=0``, Secure-EL1 interrupts will be
534 triggered at one of the Secure-EL1 FIQ exception vectors. The Secure-EL1
535 IHF should implement support for handling FIQ interrupts asynchronously.
536
537 If ``PSTATE.F=1`` then Secure-EL1 interrupts will be handled as per the
538 synchronous interrupt handling model. The SP could implement this scenario
539 by exporting a separate entrypoint for Secure-EL1 interrupts to the SPD
540 service during the registration phase. The SPD service would also need to
541 know the state of the system, general purpose and the ``PSTATE`` registers
542 in which it should arrange to return execution to the SP. The SP should
543 provide this information in an implementation defined way during the
544 registration phase if it is not known to the SPD service at build time.
545
546#. **CSS=1, TEL3=1**. Interrupts are routed to EL3 when execution is in
547 non-secure state. They should be handled through the synchronous interrupt
548 handling model as described in 1. above.
549
550#. **CSS=0, TEL3=1**. Secure-EL1 interrupts are routed to EL3 when execution
551 is in secure state. They will not be visible to the SP. The ``PSTATE.F`` bit
552 in Secure-EL1/Secure-EL0 will not mask FIQs. The EL3 runtime firmware will
553 call the handler registered by the SPD service for Secure-EL1 interrupts.
554 Secure-EL1 IHF should then handle all Secure-EL1 interrupt through the
555 synchronous interrupt handling model described in 1. above.
556
557Secure payload IHF design w.r.t non-secure interrupts
558^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
559
560#. **CSS=0, TEL3=0**. If ``PSTATE.I=0``, non-secure interrupts will be
561 triggered at one of the Secure-EL1 IRQ exception vectors . The Secure-EL1
562 IHF should co-ordinate with the SPD service to transfer execution to the
563 non-secure state where the interrupt should be handled e.g the SP could
564 allocate a function identifier to issue a SMC64 or SMC32 to the SPD
565 service which indicates that the SP execution has been preempted by a
566 non-secure interrupt. If this function identifier is not known to the SPD
567 service at compile time then the SP could provide it during the
568 registration phase.
569
570 If ``PSTATE.I=1`` then the non-secure interrupt will pend until execution
571 resumes in the non-secure state.
572
573#. **CSS=0, TEL3=1**. Non-secure interrupts are routed to EL3. They will not
574 be visible to the SP. The ``PSTATE.I`` bit in Secure-EL1/Secure-EL0 will
575 have not effect. The SPD service should register a non-secure interrupt
576 handler which should save the SP state correctly and resume execution in
577 the non-secure state where the interrupt will be handled. The Secure-EL1
578 IHF does not need to take any action.
579
580#. **CSS=1, TEL3=0**. Non-secure interrupts are handled in the FEL in
581 non-secure state (EL1/EL2) and are not visible to the SP. This routing
582 model does not affect the SP behavior.
583
584A Secure Payload must also ensure that all Secure-EL1 interrupts are correctly
585configured at the interrupt controller by the platform port of the EL3 runtime
586firmware. It should configure any additional Secure-EL1 interrupts which the EL3
587runtime firmware is not aware of through its platform port.
588
589Test secure payload behavior
590~~~~~~~~~~~~~~~~~~~~~~~~~~~~
591
592The routing model for Secure-EL1 and non-secure interrupts chosen by the TSP is
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100593described in Section `Secure Payload Dispatcher`__. It is known to the TSPD
594service at build time.
595
596.. __: #spd-int-registration
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100597
598The TSP implements an entrypoint (``tsp_sel1_intr_entry()``) for handling Secure-EL1
599interrupts taken in non-secure state and routed through the TSPD service
600(synchronous handling model). It passes the reference to this entrypoint via
601``tsp_vectors`` to the TSPD service.
602
603The TSP also replaces the default exception vector table referenced through the
604``early_exceptions`` variable, with a vector table capable of handling FIQ and IRQ
605exceptions taken at the same (Secure-EL1) exception level. This table is
606referenced through the ``tsp_exceptions`` variable and programmed into the
Sandrine Bailleux15530dd2019-02-08 15:26:36 +0100607VBAR_EL1. It caters for the asynchronous handling model.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100608
Dan Handley610e7e12018-03-01 18:44:00 +0000609The TSP also programs the Secure Physical Timer in the Arm Generic Timer block
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100610to raise a periodic interrupt (every half a second) for the purpose of testing
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100611interrupt management across all the software components listed in `Software
612components`_.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100613
614Interrupt handling
615------------------
616
617This section describes in detail the role of each software component (see
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100618Section `Software components`_) in handling an interrupt of a particular type.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100619
620EL3 runtime firmware
621~~~~~~~~~~~~~~~~~~~~
622
623The EL3 runtime firmware populates the IRQ and FIQ exception vectors referenced
624by the ``runtime_exceptions`` variable as follows.
625
626#. IRQ and FIQ exceptions taken from the current exception level with
627 ``SP_EL0`` or ``SP_EL3`` are reported as irrecoverable error conditions. As
628 mentioned earlier, EL3 runtime firmware always executes with the
629 ``PSTATE.I`` and ``PSTATE.F`` bits set.
630
631#. The following text describes how the IRQ and FIQ exceptions taken from a
632 lower exception level using AArch64 or AArch32 are handled.
633
634When an interrupt is generated, the vector for each interrupt type is
635responsible for:
636
637#. Saving the entire general purpose register context (x0-x30) immediately
638 upon exception entry. The registers are saved in the per-cpu ``cpu_context``
639 data structure referenced by the ``SP_EL3``\ register.
640
641#. Saving the ``ELR_EL3``, ``SP_EL0`` and ``SPSR_EL3`` system registers in the
642 per-cpu ``cpu_context`` data structure referenced by the ``SP_EL3`` register.
643
644#. Switching to the C runtime stack by restoring the ``CTX_RUNTIME_SP`` value
645 from the per-cpu ``cpu_context`` data structure in ``SP_EL0`` and
646 executing the ``msr spsel, #0`` instruction.
647
648#. Determining the type of interrupt. Secure-EL1 interrupts will be signaled
649 at the FIQ vector. Non-secure interrupts will be signaled at the IRQ
650 vector. The platform should implement the following API to determine the
651 type of the pending interrupt.
652
653 .. code:: c
654
655 uint32_t plat_ic_get_interrupt_type(void);
656
657 It should return either ``INTR_TYPE_S_EL1`` or ``INTR_TYPE_NS``.
658
659#. Determining the handler for the type of interrupt that has been generated.
660 The following API has been added for this purpose.
661
662 .. code:: c
663
664 interrupt_type_handler get_interrupt_type_handler(uint32_t interrupt_type);
665
666 It returns the reference to the registered handler for this interrupt
667 type. The ``handler`` is retrieved from the ``intr_type_desc_t`` structure as
668 described in Section 2. ``NULL`` is returned if no handler has been
669 registered for this type of interrupt. This scenario is reported as an
670 irrecoverable error condition.
671
672#. Calling the registered handler function for the interrupt type generated.
673 The ``id`` parameter is set to ``INTR_ID_UNAVAILABLE`` currently. The id along
674 with the current security state and a reference to the ``cpu_context_t``
675 structure for the current security state are passed to the handler function
676 as its arguments.
677
678 The handler function returns a reference to the per-cpu ``cpu_context_t``
679 structure for the target security state.
680
681#. Calling ``el3_exit()`` to return from EL3 into a lower exception level in
682 the security state determined by the handler routine. The ``el3_exit()``
683 function is responsible for restoring the register context from the
684 ``cpu_context_t`` data structure for the target security state.
685
686Secure payload dispatcher
687~~~~~~~~~~~~~~~~~~~~~~~~~
688
689Interrupt entry
690^^^^^^^^^^^^^^^
691
692The SPD service begins handling an interrupt when the EL3 runtime firmware calls
693the handler function for that type of interrupt. The SPD service is responsible
694for the following:
695
696#. Validating the interrupt. This involves ensuring that the interrupt was
Jeenu Viswambharan61c5bc72018-01-10 14:56:03 +0000697 generated according to the interrupt routing model specified by the SPD
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100698 service during registration. It should use the security state of the
699 exception level (passed in the ``flags`` parameter of the handler) where
700 the interrupt was taken from to determine this. If the interrupt is not
701 recognised then the handler should treat it as an irrecoverable error
702 condition.
703
Jeenu Viswambharan61c5bc72018-01-10 14:56:03 +0000704 An SPD service can register a handler for Secure-EL1 and/or Non-secure
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100705 interrupts. A non-secure interrupt should never be routed to EL3 from
706 from non-secure state. Also if a routing model is chosen where Secure-EL1
707 interrupts are routed to S-EL1 when execution is in Secure state, then a
708 S-EL1 interrupt should never be routed to EL3 from secure state. The handler
709 could use the security state flag to check this.
710
711#. Determining whether a context switch is required. This depends upon the
712 routing model and interrupt type. For non secure and S-EL1 interrupt,
713 if the security state of the execution context where the interrupt was
714 generated is not the same as the security state required for handling
715 the interrupt, a context switch is required. The following 2 cases
716 require a context switch from secure to non-secure or vice-versa:
717
718 #. A Secure-EL1 interrupt taken from the non-secure state should be
719 routed to the Secure Payload.
720
721 #. A non-secure interrupt taken from the secure state should be routed
722 to the last known non-secure exception level.
723
724 The SPD service must save the system register context of the current
725 security state. It must then restore the system register context of the
726 target security state. It should use the ``cm_set_next_eret_context()`` API
727 to ensure that the next ``cpu_context`` to be restored is of the target
728 security state.
729
730 If the target state is secure then execution should be handed to the SP as
731 per the synchronous interrupt handling model it implements. A Secure-EL1
732 interrupt can be routed to EL3 while execution is in the SP. This implies
733 that SP execution can be preempted while handling an interrupt by a
734 another higher priority Secure-EL1 interrupt or a EL3 interrupt. The SPD
735 service should be able to handle this preemption or manage secure interrupt
736 priorities before handing control to the SP.
737
738#. Setting the return value of the handler to the per-cpu ``cpu_context`` if
739 the interrupt has been successfully validated and ready to be handled at a
740 lower exception level.
741
742The routing model allows non-secure interrupts to interrupt Secure-EL1 when in
743secure state if it has been configured to do so. The SPD service and the SP
744should implement a mechanism for routing these interrupts to the last known
745exception level in the non-secure state. The former should save the SP context,
746restore the non-secure context and arrange for entry into the non-secure state
747so that the interrupt can be handled.
748
749Interrupt exit
750^^^^^^^^^^^^^^
751
752When the Secure Payload has finished handling a Secure-EL1 interrupt, it could
753return control back to the SPD service through a SMC32 or SMC64. The SPD service
754should handle this secure monitor call so that execution resumes in the
755exception level and the security state from where the Secure-EL1 interrupt was
756originally taken.
757
758Test secure payload dispatcher Secure-EL1 interrupt handling
759^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
760
761The example TSPD service registers a handler for Secure-EL1 interrupts taken
762from the non-secure state. During execution in S-EL1, the TSPD expects that the
763Secure-EL1 interrupts are handled in S-EL1 by TSP. Its handler
764``tspd_secure_el1_interrupt_handler()`` expects only to be invoked for Secure-EL1
765originating from the non-secure state. It takes the following actions upon being
766invoked.
767
768#. It uses the security state provided in the ``flags`` parameter to ensure
769 that the secure interrupt originated from the non-secure state. It asserts
770 if this is not the case.
771
772#. It saves the system register context for the non-secure state by calling
773 ``cm_el1_sysregs_context_save(NON_SECURE);``.
774
775#. It sets the ``ELR_EL3`` system register to ``tsp_sel1_intr_entry`` and sets the
776 ``SPSR_EL3.DAIF`` bits in the secure CPU context. It sets ``x0`` to
777 ``TSP_HANDLE_SEL1_INTR_AND_RETURN``. If the TSP was preempted earlier by a non
778 secure interrupt during ``yielding`` SMC processing, save the registers that
779 will be trashed, which is the ``ELR_EL3`` and ``SPSR_EL3``, in order to be able
780 to re-enter TSP for Secure-EL1 interrupt processing. It does not need to
781 save any other secure context since the TSP is expected to preserve it
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100782 (see section `Test secure payload dispatcher behavior`_).
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100783
784#. It restores the system register context for the secure state by calling
785 ``cm_el1_sysregs_context_restore(SECURE);``.
786
787#. It ensures that the secure CPU context is used to program the next
788 exception return from EL3 by calling ``cm_set_next_eret_context(SECURE);``.
789
790#. It returns the per-cpu ``cpu_context`` to indicate that the interrupt can
791 now be handled by the SP. ``x1`` is written with the value of ``elr_el3``
792 register for the non-secure state. This information is used by the SP for
793 debugging purposes.
794
795The figure below describes how the interrupt handling is implemented by the TSPD
796when a Secure-EL1 interrupt is generated when execution is in the non-secure
797state.
798
799|Image 1|
800
801The TSP issues an SMC with ``TSP_HANDLED_S_EL1_INTR`` as the function identifier to
802signal completion of interrupt handling.
803
804The TSPD service takes the following actions in ``tspd_smc_handler()`` function
805upon receiving an SMC with ``TSP_HANDLED_S_EL1_INTR`` as the function identifier:
806
807#. It ensures that the call originated from the secure state otherwise
808 execution returns to the non-secure state with ``SMC_UNK`` in ``x0``.
809
810#. It restores the saved ``ELR_EL3`` and ``SPSR_EL3`` system registers back to
811 the secure CPU context (see step 3 above) in case the TSP had been preempted
812 by a non secure interrupt earlier.
813
814#. It restores the system register context for the non-secure state by
815 calling ``cm_el1_sysregs_context_restore(NON_SECURE)``.
816
817#. It ensures that the non-secure CPU context is used to program the next
818 exception return from EL3 by calling ``cm_set_next_eret_context(NON_SECURE)``.
819
820#. ``tspd_smc_handler()`` returns a reference to the non-secure ``cpu_context``
821 as the return value.
822
823Test secure payload dispatcher non-secure interrupt handling
824^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
825
826The TSP in Secure-EL1 can be preempted by a non-secure interrupt during
827``yielding`` SMC processing or by a higher priority EL3 interrupt during
Jeenu Viswambharan2f40f322018-01-11 14:30:22 +0000828Secure-EL1 interrupt processing. When ``EL3_EXCEPTION_HANDLING`` is ``0``, only
829non-secure interrupts can cause preemption of TSP since there are no EL3
830interrupts in the system. With ``EL3_EXCEPTION_HANDLING=1`` however, any EL3
831interrupt may preempt Secure execution.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100832
833It should be noted that while TSP is preempted, the TSPD only allows entry into
834the TSP either for Secure-EL1 interrupt handling or for resuming the preempted
835``yielding`` SMC in response to the ``TSP_FID_RESUME`` SMC from the normal world.
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100836(See Section `Implication of preempted SMC on Non-Secure Software`_).
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100837
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100838The non-secure interrupt triggered in Secure-EL1 during ``yielding`` SMC
839processing can be routed to either EL3 or Secure-EL1 and is controlled by build
840option ``TSP_NS_INTR_ASYNC_PREEMPT`` (see Section `Test secure payload
841dispatcher behavior`_). If the build option is set, the TSPD will set the
842routing model for the non-secure interrupt to be routed to EL3 from secure state
843i.e. **TEL3=1, CSS=0** and registers ``tspd_ns_interrupt_handler()`` as the
844non-secure interrupt handler. The ``tspd_ns_interrupt_handler()`` on being
845invoked ensures that the interrupt originated from the secure state and disables
846routing of non-secure interrupts from secure state to EL3. This is to prevent
847further preemption (by a non-secure interrupt) when TSP is reentered for
848handling Secure-EL1 interrupts that triggered while execution was in the normal
849world. The ``tspd_ns_interrupt_handler()`` then invokes
850``tspd_handle_sp_preemption()`` for further handling.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100851
852If the ``TSP_NS_INTR_ASYNC_PREEMPT`` build option is zero (default), the default
853routing model for non-secure interrupt in secure state is in effect
854i.e. **TEL3=0, CSS=0**. During ``yielding`` SMC processing, the IRQ
855exceptions are unmasked i.e. ``PSTATE.I=0``, and a non-secure interrupt will
856trigger at Secure-EL1 IRQ exception vector. The TSP saves the general purpose
857register context and issues an SMC with ``TSP_PREEMPTED`` as the function
858identifier to signal preemption of TSP. The TSPD SMC handler,
859``tspd_smc_handler()``, ensures that the SMC call originated from the
860secure state otherwise execution returns to the non-secure state with
861``SMC_UNK`` in ``x0``. It then invokes ``tspd_handle_sp_preemption()`` for
862further handling.
863
864The ``tspd_handle_sp_preemption()`` takes the following actions upon being
865invoked:
866
867#. It saves the system register context for the secure state by calling
868 ``cm_el1_sysregs_context_save(SECURE)``.
869
870#. It restores the system register context for the non-secure state by
871 calling ``cm_el1_sysregs_context_restore(NON_SECURE)``.
872
873#. It ensures that the non-secure CPU context is used to program the next
874 exception return from EL3 by calling ``cm_set_next_eret_context(NON_SECURE)``.
875
876#. ``SMC_PREEMPTED`` is set in x0 and return to non secure state after
877 restoring non secure context.
878
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100879The Normal World is expected to resume the TSP after the ``yielding`` SMC
880preemption by issuing an SMC with ``TSP_FID_RESUME`` as the function identifier
881(see section `Implication of preempted SMC on Non-Secure Software`_). The TSPD
882service takes the following actions in ``tspd_smc_handler()`` function upon
883receiving this SMC:
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100884
885#. It ensures that the call originated from the non secure state. An
886 assertion is raised otherwise.
887
888#. Checks whether the TSP needs a resume i.e check if it was preempted. It
889 then saves the system register context for the non-secure state by calling
890 ``cm_el1_sysregs_context_save(NON_SECURE)``.
891
892#. Restores the secure context by calling
893 ``cm_el1_sysregs_context_restore(SECURE)``
894
895#. It ensures that the secure CPU context is used to program the next
896 exception return from EL3 by calling ``cm_set_next_eret_context(SECURE)``.
897
898#. ``tspd_smc_handler()`` returns a reference to the secure ``cpu_context`` as the
899 return value.
900
901The figure below describes how the TSP/TSPD handle a non-secure interrupt when
902it is generated during execution in the TSP with ``PSTATE.I`` = 0 when the
903``TSP_NS_INTR_ASYNC_PREEMPT`` build flag is 0.
904
905|Image 2|
906
907Secure payload
908~~~~~~~~~~~~~~
909
910The SP should implement one or both of the synchronous and asynchronous
911interrupt handling models depending upon the interrupt routing model it has
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100912chosen (as described in section `Secure Payload`__).
913
914.. __: #sp-int-registration
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100915
916In the synchronous model, it should begin handling a Secure-EL1 interrupt after
917receiving control from the SPD service at an entrypoint agreed upon during build
918time or during the registration phase. Before handling the interrupt, the SP
919should save any Secure-EL1 system register context which is needed for resuming
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100920normal execution in the SP later e.g. ``SPSR_EL1``, ``ELR_EL1``. After handling
921the interrupt, the SP could return control back to the exception level and
922security state where the interrupt was originally taken from. The SP should use
923an SMC32 or SMC64 to ask the SPD service to do this.
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100924
925In the asynchronous model, the Secure Payload is responsible for handling
926non-secure and Secure-EL1 interrupts at the IRQ and FIQ vectors in its exception
927vector table when ``PSTATE.I`` and ``PSTATE.F`` bits are 0. As described earlier,
928when a non-secure interrupt is generated, the SP should coordinate with the SPD
929service to pass control back to the non-secure state in the last known exception
930level. This will allow the non-secure interrupt to be handled in the non-secure
931state.
932
933Test secure payload behavior
934^^^^^^^^^^^^^^^^^^^^^^^^^^^^
935
936The TSPD hands control of a Secure-EL1 interrupt to the TSP at the
937``tsp_sel1_intr_entry()``. The TSP handles the interrupt while ensuring that the
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100938handover agreement described in Section `Test secure payload dispatcher
939behavior`_ is maintained. It updates some statistics by calling
940``tsp_update_sync_sel1_intr_stats()``. It then calls
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100941``tsp_common_int_handler()`` which.
942
943#. Checks whether the interrupt is the secure physical timer interrupt. It
944 uses the platform API ``plat_ic_get_pending_interrupt_id()`` to get the
945 interrupt number. If it is not the secure physical timer interrupt, then
946 that means that a higher priority interrupt has preempted it. Invoke
947 ``tsp_handle_preemption()`` to handover control back to EL3 by issuing
948 an SMC with ``TSP_PREEMPTED`` as the function identifier.
949
950#. Handles the secure timer interrupt interrupt by acknowledging it using the
951 ``plat_ic_acknowledge_interrupt()`` platform API, calling
952 ``tsp_generic_timer_handler()`` to reprogram the secure physical generic
953 timer and calling the ``plat_ic_end_of_interrupt()`` platform API to signal
954 end of interrupt processing.
955
956The TSP passes control back to the TSPD by issuing an SMC64 with
957``TSP_HANDLED_S_EL1_INTR`` as the function identifier.
958
959The TSP handles interrupts under the asynchronous model as follows.
960
961#. Secure-EL1 interrupts are handled by calling the ``tsp_common_int_handler()``
962 function. The function has been described above.
963
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100964#. Non-secure interrupts are handled by calling the ``tsp_common_int_handler()``
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100965 function which ends up invoking ``tsp_handle_preemption()`` and issuing an
966 SMC64 with ``TSP_PREEMPTED`` as the function identifier. Execution resumes at
Sandrine Bailleux073c1b32019-02-25 14:02:26 +0100967 the instruction that follows this SMC instruction when the TSPD hands control
968 to the TSP in response to an SMC with ``TSP_FID_RESUME`` as the function
969 identifier from the non-secure state (see section `Test secure payload
970 dispatcher non-secure interrupt handling`_).
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100971
Sandrine Bailleux4da4b392019-02-25 10:33:51 +0100972Other considerations
973--------------------
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100974
975Implication of preempted SMC on Non-Secure Software
Sandrine Bailleux4da4b392019-02-25 10:33:51 +0100976~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Douglas Raillardd7c21b72017-06-28 15:23:03 +0100977
978A ``yielding`` SMC call to Secure payload can be preempted by a non-secure
979interrupt and the execution can return to the non-secure world for handling
980the interrupt (For details on ``yielding`` SMC refer `SMC calling convention`_).
981In this case, the SMC call has not completed its execution and the execution
982must return back to the secure payload to resume the preempted SMC call.
983This can be achieved by issuing an SMC call which instructs to resume the
984preempted SMC.
985
986A ``fast`` SMC cannot be preempted and hence this case will not happen for
987a fast SMC call.
988
989In the Test Secure Payload implementation, ``TSP_FID_RESUME`` is designated
990as the resume SMC FID. It is important to note that ``TSP_FID_RESUME`` is a
991``yielding`` SMC which means it too can be be preempted. The typical non
992secure software sequence for issuing a ``yielding`` SMC would look like this,
993assuming ``P.STATE.I=0`` in the non secure state :
994
995.. code:: c
996
997 int rc;
998 rc = smc(TSP_YIELD_SMC_FID, ...); /* Issue a Yielding SMC call */
999 /* The pending non-secure interrupt is handled by the interrupt handler
1000 and returns back here. */
1001 while (rc == SMC_PREEMPTED) { /* Check if the SMC call is preempted */
1002 rc = smc(TSP_FID_RESUME); /* Issue resume SMC call */
1003 }
1004
1005The ``TSP_YIELD_SMC_FID`` is any ``yielding`` SMC function identifier and the smc()
1006function invokes a SMC call with the required arguments. The pending non-secure
1007interrupt causes an IRQ exception and the IRQ handler registered at the
1008exception vector handles the non-secure interrupt and returns. The return value
1009from the SMC call is tested for ``SMC_PREEMPTED`` to check whether it is
1010preempted. If it is, then the resume SMC call ``TSP_FID_RESUME`` is issued. The
1011return value of the SMC call is tested again to check if it is preempted.
1012This is done in a loop till the SMC call succeeds or fails. If a ``yielding``
1013SMC is preempted, until it is resumed using ``TSP_FID_RESUME`` SMC and
1014completed, the current TSPD prevents any other SMC call from re-entering
1015TSP by returning ``SMC_UNK`` error.
1016
1017--------------
1018
Sandrine Bailleux4da4b392019-02-25 10:33:51 +01001019*Copyright (c) 2014-2019, Arm Limited and Contributors. All rights reserved.*
Douglas Raillardd7c21b72017-06-28 15:23:03 +01001020
1021.. _Porting Guide: ./porting-guide.rst
1022.. _SMC calling convention: http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
1023
1024.. |Image 1| image:: diagrams/sec-int-handling.png?raw=true
1025.. |Image 2| image:: diagrams/non-sec-int-handling.png?raw=true