blob: 6295e49b424f2faac3ba9f457a83704b63e18ca5 [file] [log] [blame]
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001.. _core:
2
3####
4Core
5####
6
7.. _interrupt_handling:
8
9Interrupt handling
10******************
11This section describes how :ref:`optee_os` handles switches of world execution
Joakim Bech1e506862019-06-24 10:00:51 +020012context based on :ref:`SMC` exceptions and interrupt notifications. Interrupt
Joakim Bech8e5c5b32018-10-25 08:18:32 +020013notifications are IRQ/FIQ exceptions which may also imply switching of world
14execution context: normal world to secure world, or secure world to normal
15world.
16
17Use cases of world context switch
18=================================
Jens Wiklander01d61a92021-05-12 14:56:22 +020019This section lists all the cases where OP-TEE OS is involved in world context
Joakim Bech8e5c5b32018-10-25 08:18:32 +020020switches. Optee_os executes in the secure world. World switch is done by the
Joakim Becheb397802019-09-13 11:45:06 +020021core's secure monitor level/mode, referred below as the Monitor.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020022
23When the normal world invokes the secure world, the normal world executes a SMC
24instruction. The SMC exception is always trapped by the Monitor. If the related
Jens Wiklander01d61a92021-05-12 14:56:22 +020025service targets the trusted OS, the Monitor will switch to OP-TEE OS world
26execution. When the secure world returns to the normal world, OP-TEE OS executes
Joakim Bech8e5c5b32018-10-25 08:18:32 +020027a SMC that is caught by the Monitor which switches back to the normal world.
28
Jens Wiklander01d61a92021-05-12 14:56:22 +020029When a secure interrupt is signaled by the Arm GIC, it shall reach the OP-TEE OS
30interrupt exception vector. If the secure world is executing, OP-TEE OS will
31handle interrupt straight from its exception vector. If the normal world is
Joakim Bech8e5c5b32018-10-25 08:18:32 +020032executing when the secure interrupt raises, the Monitor vector must handle the
Jens Wiklander01d61a92021-05-12 14:56:22 +020033exception and invoke OP-TEE OS to serve the interrupt.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020034
35When a non-secure interrupt is signaled by the Arm GIC, it shall reach the
36normal world interrupt exception vector. If the normal world is executing, it
37will handle straight the exception from its exception vector. If the secure
Jens Wiklander01d61a92021-05-12 14:56:22 +020038world is executing when the non-secure interrupt raises, OP-TEE OS will
Joakim Bech8e5c5b32018-10-25 08:18:32 +020039temporarily return back to normal world via the Monitor to let normal world
40serve the interrupt.
41
42Core exception vectors
43======================
44Monitor vector is ``VBAR_EL3`` in AArch64 and ``MVBAR`` in Armv7-A/AArch32.
45Monitor can be reached while normal world or secure world is executing. The
46executing secure state is known to the Monitor through the ``SCR_NS``.
47
48Monitor can be reached from a SMC exception, an IRQ or FIQ exception (so-called
49interrupts) and from asynchronous aborts. Obviously monitor aborts (data,
50prefetch, undef) are local to the Monitor execution.
51
Jens Wiklander01d61a92021-05-12 14:56:22 +020052The Monitor can be external to OP-TEE OS (case ``CFG_WITH_ARM_TRUSTED_FW=y``).
Joakim Bech8e5c5b32018-10-25 08:18:32 +020053If not, provides a local secure monitor ``core/arch/arm/sm``. Armv7-A platforms
Jens Wiklander01d61a92021-05-12 14:56:22 +020054should use the OP-TEE OS secure monitor. Armv8-A platforms are likely to rely on
Joakim Bech8e5c5b32018-10-25 08:18:32 +020055an `Trusted Firmware A`_.
56
57When executing outside the Monitor, the system is executing either in the
58normal world (``SCR_NS=1``) or in the secure world (``SCR_NS=0``). Each world
59owns its own exception vector table (state vector):
60
61 - ``VBAR_EL2`` or ``VBAR_EL1`` non-secure or ``VBAR_EL1`` secure for
62 AArch64.
63 - ``HVBAR`` or ``VBAR`` non-secure or ``VBAR`` secure for Armv7-A and
64 AArch32.
65
66All SMC exceptions are trapped in the Monitor vector. IRQ/FIQ exceptions can be
67trapped either in the Monitor vector or in the state vector of the executing
68world.
69
70When the normal world is executing, the system is configured to route:
71
Jens Wiklander01d61a92021-05-12 14:56:22 +020072 - secure interrupts to the Monitor that will forward to OP-TEE OS
Joakim Bech8e5c5b32018-10-25 08:18:32 +020073 - non-secure interrupts to the executing world exception vector.
74
75When the secure world is executing, the system is configured to route:
76
Jens Wiklander01d61a92021-05-12 14:56:22 +020077 - secure and non-secure interrupts to the executing OP-TEE OS exception
78 vector. OP-TEE OS shall forward the non-secure interrupts to the normal
Joakim Bech8e5c5b32018-10-25 08:18:32 +020079 world.
80
81Optee_os non-secure interrupts are always trapped in the state vector of the
82executing world. This is reflected by a static value of ``SCR_(IRQ|FIQ)``.
83
84.. _native_foreign_irqs:
85
86Native and foreign interrupts
87=============================
Jens Wiklander01d61a92021-05-12 14:56:22 +020088Two types of interrupt are defined from OP-TEE OS point of view.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020089
Jens Wiklander01d61a92021-05-12 14:56:22 +020090 - **Native interrupt** - The interrupt handled by OP-TEE OS, secure
91 interrupts targetting S-EL1 or secure privileged mode
92 - **Foreign interrupt** - The interrupt not handled by OP-TEE OS, non-secure
93 interrupts targetting normal world or secure interrupts targetting EL3.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020094
Jens Wiklander01d61a92021-05-12 14:56:22 +020095For Arm **GICv2** mode, a native interrupt is signalled with a FIQ and a
96foreign interrupt is signalled with an IRQ. For Arm **GICv3** mode, a
97foreign interrupts is signalled as a FIQ which could be handled by either
98secure world (aarch32 Monitor mode or aarch64 EL3) or normal world.
99
100Arm GICv3 mode can be enabled by setting ``CFG_ARM_GICV3=y``.
101Native interrupts must be securely routed to OP-TEE OS. Foreign interrupts, when
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200102trapped during secure world execution might need to be efficiently routed to
103the normal world.
104
Jens Wiklander01d61a92021-05-12 14:56:22 +0200105IRQ and FIQ keeps their meaning in normal world so for clarity we will keep
106using those names in the normal world context.
107
108Normal World invokes OP-TEE OS using SMC
109========================================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200110
111**Entering the Secure Monitor**
112
113The monitor manages all entries and exits of secure world. To enter secure
114world from normal world the monitor saves the state of normal world (general
115purpose registers and system registers which are not banked) and restores the
116previous state of secure world. Then a return from exception is performed and
117the restored secure state is resumed. Exit from secure world to normal world is
118the reverse.
119
120Some general purpose registers are not saved and restored on entry and exit,
121those are used to pass parameters between secure and normal world (see
122ARM_DEN0028A_SMC_Calling_Convention_ for details).
123
124**Entry and exit of Trusted OS**
125
126On entry and exit of Trusted OS each CPU is uses a separate entry stack and runs
Jens Wiklander01d61a92021-05-12 14:56:22 +0200127with IRQ and FIQ masked. SMCs are categorised in two flavors: **fast** and
128**yielding**.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200129
Jens Wiklander01d61a92021-05-12 14:56:22 +0200130 - For **fast** SMCs, OP-TEE OS will execute on the entry stack with IRQ/FIQ
131 masked until the execution returns to normal world.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200132
Jens Wiklander01d61a92021-05-12 14:56:22 +0200133 - For **yielding** SMCs, OP-TEE OS will at some point execute the requested
134 service with interrupts unmasked. In order to handle interrupts, mainly
135 forwarding of foreign interrupts, OP-TEE OS assigns a trusted thread
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200136 (`core/arch/arm/kernel/thread.c`_) to the SMC request. The trusted thread
137 stores the execution context of the requested service. This context can be
138 suspended and resumed as the requested service executes and is
139 interrupted. The trusted thread is released only once the service
140 execution returns with a completion status.
141
Jens Wiklander01d61a92021-05-12 14:56:22 +0200142 For **yielding** SMCs, OP-TEE OS allocates or resumes a trusted thread
143 then unmasks the IRQ and FIQ lines. When the OP-TEE OS needs to invoke the
144 normal world from a foreign interrupt or a remote service call, OP-TEE OS
145 masks IRQ and FIQ and suspends the trusted thread. When suspending,
146 OP-TEE OS gets back to the entry stack.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200147
Jens Wiklander01d61a92021-05-12 14:56:22 +0200148 - **Both** fast and yielding SMCs end on the entry stack with IRQ and
149 FIQ masked and OP-TEE OS invokes the Monitor through a SMC to return
150 to the normal world.
Jens Wiklander2c39d742021-05-10 16:02:08 +0200151
152.. uml::
153 :align: center
154 :caption: SMC entry to secure world
155
156 participant "Normal World" as nwd
157 participant "Secure Monitor" as smon
158 participant "OP-TEE OS entry" as entry
159 participant "OP-TEE OS" as optee
160 == IRQ and FIQ unmasked ==
161 nwd -> smon : smc: TEE_FUNC_INVOKE
162 smon -> smon : Save non-secure context
163 smon -> smon : Restore secure context
164 smon --> entry : eret: TEE_FUNC_INVOKE
165 entry -> entry : assign thread
166 entry -> optee : TEE_FUNC_INVOKE
167 == IRQ and FIQ unmasked ==
168 optee -> optee : process
169 == IRQ and FIQ masked ==
170 optee --> entry : SMC_CALL_RETURN
171 entry -> smon : smc: SMC_CALL_RETURN
172 smon -> smon : Save secure context
173 smon -> smon : Restore non-secure context
174 == IRQ and FIQ unmasked ==
175 smon --> nwd : eret: return
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200176
177Deliver non-secure interrupts to Normal World
178=============================================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200179
180**Forward a Foreign Interrupt from Secure World to Normal World**
181
Jens Wiklander01d61a92021-05-12 14:56:22 +0200182When a foreign interrupt is received in secure world as an IRQ or FIQ
183exception then secure world:
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200184
185 1. Saves trusted thread context (entire state of all processor modes for
186 Armv7-A)
187
Jens Wiklander01d61a92021-05-12 14:56:22 +0200188 2. Masks all interrupts (IRQ and FIQ)
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200189
190 3. Switches to entry stack
191
192 4. Issues an SMC with a value to indicates to normal world that an IRQ has
Jens Wiklander01d61a92021-05-12 14:56:22 +0200193 been detected and last SMC call should be continued
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200194
195The monitor restores normal world context with a return code indicating that an
196IRQ is about to be delivered. Normal world issues a new SMC indicating that it
197should continue last SMC.
198
Jens Wiklander01d61a92021-05-12 14:56:22 +0200199The monitor restores secure world context which locates the previously
200saved context and checks that it is a return from a foreign interrupt that
201is requested before restoring the context and lets the secure world foreign
202interrupt handler return from exception where the execution would be
203resumed.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200204
Jens Wiklander01d61a92021-05-12 14:56:22 +0200205Note that the monitor itself does not know or care that it has just forwarded
206a foreign interrupt to normal world. The bookkeeping is done in the trusted
207thread handling in OP-TEE OS. Normal world is responsible to decide when
208the secure world thread should resume execution (for details, see
209:ref:`thread_handling`).
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200210
Jens Wiklander01d61a92021-05-12 14:56:22 +0200211.. uml::
212 :align: center
213 :caption: Foreign interrupt received in secure world and forwarded to
214 normal world
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200215
Jens Wiklander01d61a92021-05-12 14:56:22 +0200216 participant "Normal World" as nwd
217 participant "Secure Monitor" as smon
218 participant "OP-TEE OS entry" as entry
219 participant "OP-TEE OS" as optee
220 == IRQ and FIQ unmasked ==
221 optee -> optee : process
222 == IRQ and FIQ unmasked,\nForeign interrupt received ==
223 optee -> optee : suspend thread
224 optee -> entry : forward foreign interrupt
225 entry -> smon : smc: forward foreign interrupt
226 smon -> smon: Save secure context
227 smon -> smon: Restore non-secure context
228 == IRQ and FIQ unmasked ==
229 smon --> nwd : eret: IRQ forwarded
230 == FIQ unmasked, IRQ received ==
231 nwd -> nwd : process IRQ
232 == IRQ and FIQ unmasked ==
233 nwd -> smon : smc: return from IRQ
234 == IRQ and FIQ masked ==
235 smon -> smon : Save non-secure context
236 smon -> smon : Restore secure context
237 smon --> entry : eret: return from foreign interrupt
238 entry -> entry : find thread
239 entry --> optee : resume execution
240 == IRQ and FIQ unmasked ==
241 optee -> optee : process
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200242
Jens Wiklander01d61a92021-05-12 14:56:22 +0200243**Deliver a foreign interrupt to normal world when ``SCR_NS`` is set**
244
245Since ``SCR_IRQ`` is cleared, an IRQ will be delivered using the exception
246vector (``VBAR``) in the normal world. The IRQ is received as any other
247exception by normal world, the monitor and the OP-TEE OS are not involved
248at all.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200249
250Deliver secure interrupts to Secure World
251=========================================
Jens Wiklander01d61a92021-05-12 14:56:22 +0200252A secure (foreign) interrupt can be received during two different states,
253either in normal world (``SCR_NS`` is set) or in secure world (``SCR_NS``
254is cleared). When the secure monitor is active (Armv8-A EL3 or Armv7-A
255Monitor mode) FIQ and IRQ are masked. FIQ reception in the two different
256states is described below.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200257
Jens Wiklander01d61a92021-05-12 14:56:22 +0200258**Deliver secure interrupt to secure world when SCR_NS is set**
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200259
Jens Wiklander01d61a92021-05-12 14:56:22 +0200260When the monitor traps a secure interrupt it:
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200261
262 1. Saves normal world context and restores secure world context from last
263 secure world exit (which will have IRQ and FIQ blocked)
264 2. Clears ``SCR_FIQ`` when clearing ``SCR_NS``
Jens Wiklander01d61a92021-05-12 14:56:22 +0200265 3. Does a return from exception into OP-TEE OS via the secure interrupt
266 entry point
267 4. OP-TEE OS handles the native interrupt directly in the entry point
268 5. OP-TEE OS issues an SMC to return to normal world
269 6. The monitor saves the secure world context and restores the normal world context
270 7. Does a return from exception into the restored context
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200271
Jens Wiklander01d61a92021-05-12 14:56:22 +0200272.. uml::
273 :align: center
274 :caption: Secure interrupt received when SCR_NS is set
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200275
Jens Wiklander01d61a92021-05-12 14:56:22 +0200276 participant "Normal World" as nwd
277 participant "Secure Monitor" as smon
278 participant "OP-TEE OS entry" as entry
279 participant "OP-TEE OS" as optee
280 == IRQ and FIQ unmasked ==
281 == Running in non-secure world (SCR_NS set) ==
282 nwd -> nwd : process
283 == IRQ and FIQ masked,\nSecure interrupt received ==
284 smon -> smon : Save non-secure context
285 smon -> smon : Restore secure context
286 smon --> entry : eret: native interrupt entry point
287 entry -> entry: process received native interrupt
288 entry -> smon: smc: return
289 smon -> smon : Save secure context
290 smon -> smon : Restore non-secure context
291 smon --> nwd : eret: return to Normal world
292 == IRQ and FIQ unmasked ==
293 nwd -> nwd : process
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200294
295**Deliver FIQ to secure world when SCR_NS is cleared**
296
Jens Wiklander01d61a92021-05-12 14:56:22 +0200297.. uml::
298 :align: center
299 :caption: FIQ received while processing an IRQ forwarded from secure world
300
301 participant "Normal World" as nwd
302 participant "Secure Monitor" as smon
303 participant "OP-TEE OS entry" as entry
304 participant "OP-TEE OS" as optee
305 == IRQ and FIQ unmasked ==
306 optee -> optee : process
307 == IRQ and FIQ unmasked,\nForeign interrupt received ==
308 optee -> optee : suspend thread
309 optee -> entry : forward foreign interrupt
310 entry -> smon : smc: forward foreign interrupt
311 smon -> smon: Save secure context
312 smon -> smon: Restore non-secure context
313 == IRQ and FIQ unmasked ==
314 smon --> nwd : eret: IRQ forwarded
315 == FIQ unmasked, IRQ received ==
316 nwd -> nwd : process IRQ
317 == IRQ and FIQ masked,\nSecure interrupt received ==
318 smon -> smon : Save non-secure context
319 smon -> smon : Restore secure context
320 smon --> entry : eret: native interrupt entry point
321 entry -> entry : process received native interrupt
322 entry -> smon: smc: return
323 smon -> smon : Save secure context
324 smon -> smon : Restore non-secure context
325 smon --> nwd : eret: return to Normal world
326 == FIQ unmasked\nIRQ still being processed ==
327 nwd -> nwd : process IRQ
328 == IRQ and FIQ unmasked ==
329 nwd -> smon : smc: return from IRQ
330 == IRQ and FIQ masked ==
331 smon -> smon : Save non-secure context
332 smon -> smon : Restore secure context
333 smon --> entry : eret: return from foreign interrupt
334 entry -> entry : find thread
335 entry --> optee : resume execution
336 == IRQ and FIQ unmasked ==
337 optee -> optee : process
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200338
339Trusted thread scheduling
340=========================
341**Trusted thread for standard services**
342
Jens Wiklander01d61a92021-05-12 14:56:22 +0200343OP-TEE yielding services are carried through standard SMC. Execution of these
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200344services can be interrupted by foreign interrupts. To suspend and restore the
Jens Wiklander01d61a92021-05-12 14:56:22 +0200345service execution, optee_os assigns a trusted thread at yielding SMC entry.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200346
347The trusted thread terminates when optee_os returns to the normal world with a
348service completion status.
349
350A trusted thread execution can be interrupted by a native interrupt. In this
351case the native interrupt is handled by the interrupt exception handlers and
352once served, optee_os returns to the execution trusted thread.
353
354A trusted thread execution can be interrupted by a foreign interrupt. In this
355case, optee_os suspends the trusted thread and invokes the normal world through
356the Monitor (optee_os so-called RPC services). The trusted threads will resume
357only once normal world invokes the optee_os with the RPC service status.
358
359A trusted thread execution can lead optee_os to invoke a service in normal
360world: access a file, get the REE current time, etc. The trusted thread is
Jens Wiklander01d61a92021-05-12 14:56:22 +0200361first suspended then resumed during remote service execution.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200362
363**Scheduling considerations**
364
365When a trusted thread is interrupted by a foreign interrupt and when optee_os
366invokes a normal world service, the normal world gets the opportunity to
367reschedule the running applications. The trusted thread will resume only once
368the client application is scheduled back. Thus, a trusted thread execution
369follows the scheduling of the normal world caller context.
370
371Optee_os does not implement any thread scheduling. Each trusted thread is
372expected to track a service that is invoked from the normal world and should
373return to it with an execution status.
374
375The OP-TEE Linux driver (as implemented in `drivers/tee/optee`_ since Linux
376kernel 4.12) is designed so that the Linux thread invoking OP-TEE gets assigned
377a trusted thread on TEE side. The execution of the trusted thread is tied to the
378execution of the caller Linux thread which is under the Linux kernel scheduling
379decision. This means trusted threads are scheduled by the Linux kernel.
380
381**Trusted thread constraints**
382
383TEE core handles a static number of trusted threads, see ``CFG_NUM_THREADS``.
384
Jens Wiklander01d61a92021-05-12 14:56:22 +0200385Trusted threads are expensive on memory constrained system, mainly
386because of the execution stack size.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200387
388On SMP systems, optee_os can execute several trusted threads in parallel if the
389normal world supports scheduling of processes. Even on UP systems, supporting
390several trusted threads in optee_os helps normal world scheduler to be
391efficient.
392
Etienne Carriereeda1e0f2023-05-07 17:29:28 +0200393Core handlers for native interrupts
394===================================
395
396OP-TEE core provides methods for device drivers to setup and register
397handler functions for native interrupt controller drivers
398(see:ref:`native_foreign_irqs`).
399Interrupt handlers can be nested as when an interrupt controller
400exposes interrupts which signaling is multiplexed on an interrupt
401controlled by a parent interrupt controller.
402
403Interrupt controllers are represented by an instance of ``struct itr_chip``.
404An interrupt controller exposes a given number of interrupts, each identified
405by an index from 0 to N-1 where N is the total number of interrupts exposed
406by that controller. In the literature, an interrupt index identifier
407is called interrupt number.
408
409**Interrupt management API functions**
410
411Interrupt management resources are declared in header file interrupt.h_.
412Interrupt consumers main API functions are:
413
Etienne Carrieredc8cede2023-12-13 09:20:13 +0100414 - ``interrupt_enable()`` and ``interrupt_disable()`` to respectively
415 enable or disable an interrupt.
Etienne Carriereeda1e0f2023-05-07 17:29:28 +0200416
Etienne Carrieredc8cede2023-12-13 09:20:13 +0100417 - ``interrupt_mask()`` and ``interrupt_unmask()`` to respectively mask
418 or unmask an interrupt. Masking of an enabled interrupt temporarily
419 disables the interrupt while unmasking enables a previously masked
420 interrupt. ``interrupt_mask()`` and ``interrupt_unmask()`` are
421 allowed to be called from an interrupt context, but
422 ``interrupt_enable()`` and ``interrupt_disable()`` not so.
Etienne Carriereeda1e0f2023-05-07 17:29:28 +0200423
Etienne Carrieredc8cede2023-12-13 09:20:13 +0100424 - ``interrupt_configure()`` to configure an interrupt detection mode
425 and priority.
Etienne Carriereeda1e0f2023-05-07 17:29:28 +0200426
Etienne Carrieredc8cede2023-12-13 09:20:13 +0100427 - ``interrupt_add_handler()`` to configure an interrupt and register
Etienne Carriereeda1e0f2023-05-07 17:29:28 +0200428 an interrupt handler function, see below.
429
430 - ``interrupt_remove_handler()`` to unregister an interrupt handler
431 function from an interrupt.
432
433**Interrupt controller drivers**
434
435An interrupt controller instance, named chip (``struct itr_chip``) defines
436operation function handlers for management of the interrupt(s) it controls.
437An interrupt chip driver must provide operation handler functions ``.add``,
438``.mask``, ``.unmask``, ``.enable`` and ``.disable``. There are other
439operation handler functions that are optional, as for example ``.rasie_pi``,
440``.raise_sgi`` and ``.set_priority``.
441
442An interrupt chip driver registers the controller instance with API
443function ``itr_chip_init()``. The driver calls the registered interrupt
444consumer(s) handler(s) with API function ``interrupt_call_handlers()``.
445
446**CPU main interrupt controller driver**
447
448The CPU interrupt controller (e.g. a GIC instance on Arm architecture CPUs)
449is called the main interrupt controller. Its driver must register as main
450controller using API function ``interrupt_main_init()``. The function is
451in charge of calling ``itr_chip_init()`` for that chip instance.
452
453Interrupt consumer drivers can get a reference to the main interrupt
454controller with the API function ``interrupt_get_main_chip()``.
455
456**Interrupt handlers**
457
458Interrupt handler functions are callback functions registered by interrupt
459consumer drivers that core shall call when the related interrupt occurs.
460Structure ``struct itr_handler`` references a handler. It contains the
461handler function entry point, the interrupt number, the interrupt controller
462device and a few more parameters.
463
464An interrupt handler function return value is of type ``enum itr_return``.
465It shall return ``ITRR_HANDLED`` when the interrupt is served and
466``ITRR_NONE`` when the interrupt cannot be served.
467
468The interrupt handler runs in an interrupt context rather than a thread
469context. When this occurs, all other interrupts are masked, necessitating fast
470execution of the interrupt handler to avoid delaying or missing out on other
471interrupts. When an interrupt occurs that requires the completion of
472long-running operations, the interrupt handler should request the OP-TEE
473bottom half thread (see :ref:`notifications`) to execute those operations.
474
475API function ``interrupt_add_handler()``,
476``interrupt_add_handler_with_chip()`` and ``interrupt_alloc_add_handler()``
477configure and register a handler function to a given interrupt.
478
479API function ``interrupt_remove_handler()`` and
480``interrupt_remove_free_handler()`` unregister a registered handler.
481
482**Interrupt consumer driver**
483
484A typical implementation of a driver consuming an interrupt includes
485retrieving of the interrupt resource (interrupt controller and interrupt
486number in that controller), configuring the interrupt, registering a handler
487for the interrupt and enabling/disabling the interrupt.
488
489For example, the dummy driver below prints a debug trace when the related
490interrupt occurs:
491
492.. code-block:: c
493
494 static struct itr_handler *foo_int1_handler;
495
496 static struct foo_int1_data = {
497 /* field with some interrupt handler private data */
498 };
499
500 static enum itr_return foo_it_handler_fn(struct itr_handler *h)
501 {
502 foo_acknowledge_interrupt(h->it);.
503 DMSG("Interrupt FOO%u served", h->it);
504
505 return ITRR_HANDLED;
506 }
507
508 static TEE_Result foo_initialization(void)
509 {
510 TEE_Result res = TEE_ERROR_GENERIC;
511
512 res = interrupt_alloc_add_handler(itr_core_get(),
513 GIC_INT_FOO,
514 foo_it_handler_fn,
515 ITRF_TRIGGER_LEVEL,
516 &foo_int1_data,
517 &foo_int1_handler);
518 if (res)
519 return res;
520
521 interrupt_enable(itr_chip, it_num);
522
523 return TEE_SUCCESS;
524 }
525
526 static void foo_release(void)
527 {
528 if (foo_int1_handler) {
529 interrupt_disable(foo_int1_handler->chip,
530 foo_int1_handler->it);
531
532 interrupt_remove_free_handler(&foo_int1_handler);
533 }
534 }
535
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200536----
537
Jens Wiklander5507c022021-05-14 15:12:56 +0200538.. _notifications:
539
540Notifications
541*************
542
543There are two kinds of notifications that secure world can use to make
544normal world aware of some event.
545
5461. Synchronous notifications delivered with ``OPTEE_RPC_CMD_NOTIFICATION``
547 using the ``OPTEE_RPC_NOTIFICATION_SEND`` parameter.
5482. Asynchronous notifications delivered with a combination of a non-secure
549 interrupt and a fast call from the non-secure interrupt handler.
550
551Secure world can wait in normal for a notification to arrive. This allows
552the calling thread to sleep instead of spinning when waiting for something.
553This happens for instance when a thread waits for a mutex to become
554available.
555
556Synchronous notifications are limited by depending on RPC for delivery, this
557is only usable from a normal thread context. Secure interrupt handler or
558other atomic context cannot use synchronous notifications due to this.
559
560Asynchrononous notifications uses a platform specific way of triggering a
561non-secure interrupt. This is done with ``itr_raise_pi()`` in a way
562suitable for a secure interrupt handler or another atomic context. This is
563useful when using a top half and bottom half kind of design in a device
564driver. The top half is done in the secure interrupt handler which then
565triggers normal world to make a yielding call into secure world to do the
566bottom half processing.
567
568.. uml::
569 :align: center
570 :caption: Top half, bottom half example
571
572 participant "OP-TEE OS\ninterrupt handler" as sec_itr
573 participant "OP-TEE OS\nfastcall handler" as fastcall
574 participant "Interrupt\ncontroller" as itc
575 participant "Normal World\ninterrupt handler" as ns_itr
576 participant "Normal World\nthread" as ns_thr
577 participant "OP-TEE OS\nyielding do bottom half" as bottom
578
579 itc --> sec_itr : Secure interrupt
580 activate sec_itr
581 sec_itr -> sec_itr : Top half processing
582 sec_itr --> itc : Trigger NS interrupt
583 itc --> ns_itr : Non-secure interrupt
584 activate ns_itr
585 sec_itr --> itc: End of interrupt
586 deactivate sec_itr
587 ns_itr -> fastcall ++: Get notification
588 fastcall -> ns_itr --: Return notification
589 alt Do bottom half notifcation
590 ns_itr --> ns_thr : Wake thread
591 activate ns_thr
592 ns_itr --> itc: End of interrupt
593 deactivate ns_itr
594 ns_thr -> bottom ++: Do bottom half
595 bottom -> bottom : Process bottom half
596 bottom -> ns_thr --: Done
597 deactivate ns_thr
598 else Some other notification
599 end
600
601.. uml::
602 :align: center
603 :caption: Synchronous example
604
605 participant "OP-TEE OS\nthread 1" as sec_thr1
606 participant "Normal World\nthread 1" as ns_thr1
607 participant "OP-TEE OS\nthread 2" as sec_thr2
608 participant "Normal World\nthread 2" as ns_thr2
609
610 activate ns_thr1
611 ns_thr1 -> sec_thr1 ++ : Invoke
612 sec_thr1 -> sec_thr1 : Lock mutex
613 sec_thr1 -> sec_thr1 : Process
614 activate ns_thr2
615 ns_thr2 -> sec_thr2 ++: Invoke
616 sec_thr2 -> ns_thr2 -- : RPC: Wait for mutex
617 ns_thr2 -> ns_thr2 : Wait for notifcation
618 deactivate ns_thr2
619 sec_thr1 -> sec_thr1 : Unlock mutex
620 sec_thr1 -> ns_thr1 -- : RPC: Notify mutex unlocked
621 ns_thr1 --> ns_thr2 : Notify mutex unlocked
622 activate ns_thr2
623 ns_thr1 -> sec_thr1 ++ : Return from RPC
624 sec_thr1 -> sec_thr1 : Process
625 sec_thr1 -> ns_thr1 -- : Return from Invoke
626 deactivate ns_thr1
627 ns_thr2 -> sec_thr2 ++ : Return from RPC
628 sec_thr2 -> sec_thr2 : Lock mutex
629 sec_thr2 -> sec_thr2 : Process
630 sec_thr2 -> sec_thr2 : Unlock mutex
631 sec_thr2 -> sec_thr2 : Process
632 sec_thr2 -> ns_thr2 -- : Return from Invoke
633 deactivate ns_thr2
634
635Notifications are identified with a value, allocated as:
636
6370 - 63
638 Mixed asynchronous and synchronous range
63964 - Max
640 Synchronous only range
641
642If the **Max** value is smaller than 63, then there's only the mixed range.
643
644If asynchronous notifications are enabled then is the value 0 reserved for
Joakim Bech3cb68c62023-03-08 14:55:26 +0100645signalling the a driver need a bottom half call, that is the yielding call
Jens Wiklander5507c022021-05-14 15:12:56 +0200646``OPTEE_MSG_CMD_DO_BOTTOM_HALF``.
647
648The rest of the asynchronous notification values are managed with two
649functions ``notif_alloc_async_value()`` and ``notif_free_async_value()``.
650
651----
652
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200653.. _memory_objects:
654
655Memory objects
656**************
657A memory object, **MOBJ**, describes a piece of memory. The interface provided
658is mostly abstract when it comes to using the MOBJ to populate translation
659tables etc. There are different kinds of MOBJs describing:
660
661 - Physically contiguous memory
662 - created with ``mobj_phys_alloc(...)``.
663
664 - Virtual memory
665 - one instance with the name ``mobj_virt`` available.
666 - spans the entire virtual address space.
667
668 - Physically contiguous memory allocated from a ``tee_mm_pool_t *``
669 - created with ``mobj_mm_alloc(...)``.
670
671 - Paged memory
672 - created with ``mobj_paged_alloc(...)``.
673 - only contains the supplied size and makes ``mobj_is_paged(...)``
674 return true if supplied as argument.
675
676 - Secure copy paged shared memory
677 - created with ``mobj_seccpy_shm_alloc(...)``.
678 - makes ``mobj_is_paged(...)`` and ``mobj_is_secure(...)`` return true
679 if supplied as argument.
680
681----
682
683.. _mmu:
684
685MMU
686***
687Translation tables
688==================
Jens Wiklander18b953f2021-05-14 13:00:21 +0200689
690OP-TEE supports two translation table formats:
691
6921. Short-descriptor translation table format, available on ARMv7-A and
693 ARMv8-A AArch32
6942. Long-descriptor translation format, available on ARMv7-A with LPAE and
695 ARMv8-A
696
697ARMv7-A without LPAE (Large Physical Address Extension) must use the
698short-descriptor translation table format only. ARMv8-A AArch64 must use
699the long-descriptor translation format only.
700
701Translation table format is a static build time configuration option,
702``CFG_WITH_LPAE``. The design around the translation table handling has
703been centered around these factors:
704
7051. Share translation tables between CPUs when possible to save memory
706 and simplify paging
7072. Support non-global CPU specific mappings to allow executing different
708 TAs in parallel.
709
710Short-descriptor translation table format
711-----------------------------------------
712
713Several L1 translation tables are used, one large spanning 4 GiB and two or
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200714more small tables spanning 32 MiB. The large translation table handles kernel
715mode mapping and matches all addresses not covered by the small translation
716tables. The small translation tables are assigned per thread and covers the
717mapping of the virtual memory space for one TA context.
718
Ding Tao7620b4c2022-05-10 13:42:19 +0000719Memory space between small and large translation table is configured by TTBCR.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200720TTBR1 always points to the large translation table. TTBR0 points to the a small
721translation table when user mapping is active and to the large translation table
722when no user mapping is currently active. For details about registers etc,
723please refer to a Technical Reference Manual for your architecture, for example
724`Cortex-A53 TRM`_.
725
726The translation tables has certain alignment constraints, the alignment (of the
727physical address) has to be the same as the size of the translation table. The
728translation tables are statically allocated to avoid fragmentation of memory due
729to the alignment constraints.
730
731Each thread has one small L1 translation table of its own. Each TA context has a
732compact representation of its L1 translation table. The compact representation
733is used to initialize the thread specific L1 translation table when the TA
734context is activated.
735
736.. graphviz::
Jens Wiklander18b953f2021-05-14 13:00:21 +0200737 :align: center
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200738
739 digraph xlat_table {
740 graph [
741 rankdir = "LR"
742 ];
743 node [
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200744 shape = "ellipse"
745 ];
746 edge [
747 ];
748 "node_ttb" [
749 label = "<f0> TTBR0 | <f1> TTBR1"
750 shape = "record"
751 ];
752 "node_large_l1" [
753 label = "<f0> Large L1\nSpans 4 GiB"
754 shape = "record"
755 ];
756 "node_small_l1" [
757 label = "Small L1\nSpans 32 MiB\nper entry | <f0> 0 | <f1> 1 | ... | <fn> n"
758 shape = "record"
759 ];
760
761 "node_ttb":f0 -> "node_small_l1":f0 [ label = "Thread 0 ctx active" ];
762 "node_ttb":f0 -> "node_small_l1":f1 [ label = "Thread 1 ctx active" ];
763 "node_ttb":f0 -> "node_small_l1":fn [ label = "Thread n ctx active" ];
764 "node_ttb":f0 -> "node_large_l1" [ label="No active ctx" ];
765 "node_ttb":f1 -> "node_large_l1";
766 }
767
Jens Wiklander18b953f2021-05-14 13:00:21 +0200768Long-descriptor translation table format
769----------------------------------------
770
771Each CPU is assigned a L1 translation table which is programmed into
772Translation Table Base Register 0 (``TTBR0`` or ``TTBR0_EL1`` as
773appropriate).
774
775L1 and L2 translation tables are statically allocated and initialized at
776boot. Normally there is only one shared L2 table, but with ASLR enabled the
777virtual address space used for the shared mapping may need to use two
778tables. An unused entry in the L1 table is selected to point to the per
779thread L2 table. With ASLR configured this means that different per thread
780entry may be selected each time the system boots. Note that this entry will
781only point to a table when the per thread mapping is activated.
782
783The L2 translation tables in their turn point to L3 tables which use the
784small page granularity of 4 KiB. The shared mappings has the L3 tables
785initialized too at boot, but the per thread L3 tables are dynamic and are
786only assigned when the mapping is activated.
787
788.. graphviz::
789 :align: center
790 :caption: Example translation table setup with 4GiB virtual address space
791 with L3 tables excluded
792
793 digraph xlat_table {
794 graph [ rankdir = "LR" ];
795 node [ ];
796 edge [ ];
797
798 "ttbr0" [
799 label = "TTBR0"
800 shape = "record"
801 ];
802 "node_l1" [
803 label = "<h> Per CPU L1 table | <f0> 0 | <f1> 1 | <f2> 2 | <f3> 3"
804 shape = "record"
805 ];
806 "shared_l2_n" [
807 label = "<h> Shared L2 table n | 0 | ... | 512"
808 shape = "record"
809 ]
810 "shared_l2_m" [
811 label = "<h> Shared L2 table m | 0 | ... | 512"
812 shape = "record"
813 ]
814 "per_thread_l2" [
815 label = "<h> Per thread L2 table | 0 | ... | 512"
816 shape = "record"
817 ]
818 "ttbr0" -> "node_l1":h;
819 "node_l1":f2 -> "shared_l2_n":h;
820 "node_l1":f3 -> "shared_l2_m":h;
821 "node_l1":f0 -> "per_thread_l2":h;
822 }
823
824
Jens Wiklander03b05a02019-02-25 13:44:38 +0100825Page table cache
826================
827Page tables used to map TAs are managed with the page table cache. When the
828context of a TA is unmapped, all its page tables are released with a call
829to ``pgt_free()``. All page tables needed when mapping a TA are allocated
830using ``pgt_alloc()``.
831
832A fixed maximum number of translation tables are available in a pool. One
833thread may execute a TA which needs all or almost all tables. This can
834block TAs from being executed by other threads. To ensure that all TAs
835eventually will be permitted to execute ``pgt_alloc()`` temporarily frees
836eventual tables allocated before waiting for tables to become available.
837
838The page table cache behaves differently depending on configuration
839options.
840
841Without paging (``CFG_WITH_PAGER=n``)
842-------------------------------------
843This is the easiest configuration. All page tables are statically allocated
844in the ``.nozi.pgt_cache`` section. ``pgt_alloc()`` allocates tables from the
845free-list and ``pgt_free()`` returns the tables directly to the free-list.
846
847With paging enabled (``CFG_WITH_PAGER=y``)
848------------------------------------------
849
850Page tables are allocated as zero initialized locked pages during boot
851using ``tee_pager_alloc()``. Locked pages are populated with physical pages
852on demand from the pager. The physical page can be released when not needed
853any longer with ``tee_pager_release_phys()``.
854
855With ``CFG_WITH_LPAE=y`` each translation table has the same size as a
856physical page which makes it easy to release the physical page when the
857translation table isn't needed any longer. With the short-descriptor table
858format (``CFG_WITH_LPAE=n``) it becomes more complicated as four
859translation tables are stored in each page. Additional bookkeeping is used
860to tell when the page for used by four separate translation tables can be
861released.
862
863With paging of user TA enabled (``CFG_PAGED_USER_TA=y``)
864--------------------------------------------------------
865With paging of user TAs enabled a cache of recently used translation tables
866is used. This can save us from a storm of page faults when restoring the
867mappings of a recently unmapped TA. Which translation tables should be
868cached is indicated with reference counting by the pager on used tables.
869When a table needs to be forcefully freed
870``tee_pager_pgt_save_and_release_entries()`` is called to let the pager
871know that the table can't be used any longer.
872
873When a mapping in a TA is removed it also needs to be purged from cached
874tables with ``pgt_flush_ctx_range()`` to prevent old mappings from being
875accidentally reused.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200876
877Switching to user mode
878======================
879This section only applies with following configuration flags:
880
881 - ``CFG_WITH_LPAE=n``
882 - ``CFG_CORE_UNMAP_CORE_AT_EL0=y``
883
884When switching to user mode only a minimal kernel mode mapping is kept. This is
885achieved by selecting a zeroed out big L1 translation in TTBR1 when
886transitioning to user mode. When returning back to kernel mode the original L1
887translation table is restored in TTBR1.
888
889Switching to normal world
890=========================
891When switching to normal world either via a foreign interrupt (see
892:ref:`native_foreign_irqs` or RPC there is a chance that secure world will
893resume execution on a different CPU. This means that the new CPU need to be
894configured with the context of the currently active TA. This is solved by always
Jens Wiklanderddde3a82019-02-25 12:46:18 +0100895setting the TA context in the CPU when resuming execution.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200896
897----
898
899.. _pager:
900
901Pager
902*****
903OP-TEE currently requires >256 KiB RAM for OP-TEE kernel memory. This is not a
904problem if OP-TEE uses TrustZone protected DDR, but for security reasons OP-TEE
905may need to use TrustZone protected SRAM instead. The amount of available SRAM
906varies between platforms, from just a few KiB up to over 512 KiB. Platforms with
907just a few KiB of SRAM cannot be expected to be able to run a complete TEE
908solution in SRAM. But those with 128 to 256 KiB of SRAM can be expected to have
909a capable TEE solution in SRAM. The pager provides a solution to this by demand
910paging parts of OP-TEE using virtual memory.
911
912Secure memory
913=============
914TrustZone protected SRAM is generally considered more secure than TrustZone
915protected DRAM as there is usually more attack vectors on DRAM. The attack
916vectors are hardware dependent and can be different for different platforms.
917
918Backing store
919=============
920TrustZone protected DRAM or in some cases non-secure DRAM is used as backing
921store. The data in the backing store is integrity protected with one hash
922(SHA-256) per page (4KiB). Readonly pages are not encrypted since the OP-TEE
923binary itself is not encrypted.
924
925Partitioning of memory
926======================
927The code that handles demand paging must always be available as it would
928otherwise lead to deadlock. The virtual memory is partitioned as:
929
930 +--------------+-------------------+
931 | Type | Sections |
932 +==============+===================+
933 | unpaged | | text |
934 | | | rodata |
935 | | | data |
936 | | | bss |
937 | | | heap1 |
938 | | | nozi |
939 | | | heap2 |
940 +--------------+-------------------+
941 | init / paged | | text_init |
942 | | | rodata_init |
943 +--------------+-------------------+
944 | paged | | text_pageable |
945 | | | rodata_pageable |
946 +--------------+-------------------+
947 | demand alloc | |
948 +--------------+-------------------+
949
950Where ``nozi`` stands for "not zero initialized", this section contains entry
951stacks (thread stack when TEE pager is not enabled) and translation tables (TEE
952pager cached translation table when the pager is enabled and LPAE MMU is used).
953
954The ``init`` area is available when OP-TEE is initializing and contains
955everything that is needed to initialize the pager. After the pager has been
956initialized this area will be used for demand paged instead.
957
958The ``demand alloc`` area is a special area where the pages are allocated and
959removed from the pager on demand. Those pages are returned when OP-TEE does not
960need them any longer. The thread stacks currently belongs this area. This means
961that when a stack is not used the physical pages can be used by the pager for
962better performance.
963
964The technique to gather code in the different area is based on compiling all
965functions and data into separate sections. The unpaged text and rodata is then
966gathered by linking all object files with ``--gc-sections`` to eliminate
967sections that are outside the dependency graph of the entry functions for
968unpaged functions. A script analyzes this ELF file and generates the bits of the
969final link script. The process is repeated for init text and rodata. What is
970not "unpaged" or "init" becomes "paged".
971
972Partitioning of the binary
973==========================
974.. note::
975 The struct definitions provided in this section are explicitly covered by
976 the following dual license:
977
978 .. code-block:: none
979
980 SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
981
982The binary is partitioned into four parts as:
983
984
985 +----------+
986 | Binary |
987 +==========+
988 | Header |
989 +----------+
990 | Init |
991 +----------+
992 | Hashes |
993 +----------+
994 | Pageable |
995 +----------+
996
997The header is defined as:
998
999.. code-block:: c
1000
1001 #define OPTEE_MAGIC 0x4554504f
1002 #define OPTEE_VERSION 1
1003 #define OPTEE_ARCH_ARM32 0
1004 #define OPTEE_ARCH_ARM64 1
1005
1006 struct optee_header {
1007 uint32_t magic;
1008 uint8_t version;
1009 uint8_t arch;
1010 uint16_t flags;
1011 uint32_t init_size;
1012 uint32_t init_load_addr_hi;
1013 uint32_t init_load_addr_lo;
1014 uint32_t init_mem_usage;
1015 uint32_t paged_size;
1016 };
1017
1018The header is only used by the loader of OP-TEE, not OP-TEE itself. To
1019initialize OP-TEE the loader loads the complete binary into memory and copies
1020what follows the header and the following ``init_size`` bytes to
1021``(init_load_addr_hi << 32 | init_load_addr_lo)``. ``init_mem_usage`` is used by
1022the loader to be able to check that there is enough physical memory available
1023for OP-TEE to be able to initialize at all. The loader supplies in ``r0/x0`` the
1024address of the first byte following what was not copied and jumps to the load
1025address to start OP-TEE.
1026
1027In addition to overall binary with partitions inside described as above, three
1028extra binaries are generated simultaneously during build process for loaders who
1029support loading separate binaries:
1030
1031 +-----------+
1032 | v2 binary |
1033 +===========+
1034 | Header |
1035 +-----------+
1036
1037 +-----------+
1038 | v2 binary |
1039 +===========+
1040 | Init |
1041 +-----------+
1042 | Hashes |
1043 +-----------+
1044
1045 +-----------+
1046 | v2 binary |
1047 +===========+
1048 | Pageable |
1049 +-----------+
1050
1051In this case, loaders load header binary first to get image list and information
1052of each image; and then load each of them into specific load address assigned in
1053structure. These binaries are named with `v2` suffix to distinguish from the
1054existing binaries. Header format is updated to help loaders loading binaries
1055efficiently:
1056
1057.. code-block:: c
1058
1059 #define OPTEE_IMAGE_ID_PAGER 0
1060 #define OPTEE_IMAGE_ID_PAGED 1
1061
1062 struct optee_image {
1063 uint32_t load_addr_hi;
1064 uint32_t load_addr_lo;
1065 uint32_t image_id;
1066 uint32_t size;
1067 };
1068
1069 struct optee_header_v2 {
1070 uint32_t magic;
1071 uint8_t version;
1072 uint8_t arch;
1073 uint16_t flags;
1074 uint32_t nb_images;
1075 struct optee_image optee_image[];
1076 };
1077
1078Magic number and architecture are identical as original. Version is increased to
1079two. ``load_addr_hi`` and ``load_addr_lo`` may be ``0xFFFFFFFF`` for pageable
1080binary since pageable part may get loaded by loader into dynamic available
1081position. ``image_id`` indicates how loader handles current binary. Loaders who
1082don't support separate loading just ignore all v2 binaries.
1083
1084Initializing the pager
1085======================
1086The pager is initialized as early as possible during boot in order to minimize
1087the "init" area. The global variable ``tee_mm_vcore`` describes the virtual
1088memory range that is covered by the level 2 translation table supplied to
1089``tee_pager_init(...)``.
1090
1091Assign pageable areas
1092---------------------
1093A virtual memory range to be handled by the pager is registered with a call to
1094``tee_pager_add_core_area()``.
1095
1096.. code-block:: c
1097
1098 bool tee_pager_add_area(tee_mm_entry_t *mm,
1099 uint32_t flags,
1100 const void *store,
1101 const void *hashes);
1102
1103which takes a pointer to ``tee_mm_entry_t`` to tell the range, flags to tell how
1104memory should be mapped (readonly, execute etc), and pointers to backing store
1105and hashes of the pages.
1106
1107Assign physical pages
1108---------------------
1109Physical SRAM pages are supplied by calling ``tee_pager_add_pages(...)``
1110
1111.. code-block:: c
1112
1113 void tee_pager_add_pages(tee_vaddr_t vaddr,
1114 size_t npages,
1115 bool unmap);
1116
1117``tee_pager_add_pages(...)`` takes the physical address stored in the entry
1118mapping the virtual address ``vaddr`` and ``npages`` entries after that and uses
1119it to map new pages when needed. The unmap parameter tells whether the pages
1120should be unmapped immediately since they does not contain initialized data or
1121be kept mapped until they need to be recycled. The pages in the "init" area are
1122supplied with ``unmap == false`` since those page have valid content and are in
1123use.
1124
1125Invocation
1126==========
1127The pager is invoked as part of the abort handler. A pool of physical pages are
1128used to map different virtual addresses. When a new virtual address needs to be
1129mapped a free physical page is mapped at the new address, if a free physical
1130page cannot be found the oldest physical page is selected instead. When the page
1131is mapped new data is copied from backing store and the hash of the page is
1132verified. If it is OK the pager returns from the exception to resume the
1133execution.
1134
Jens Wiklanderaecf4412019-02-26 12:33:14 +01001135Data structures
1136===============
1137.. figure:: ../images/core/tee_pager_area.png
1138 :figclass: align-center
1139
1140 How the main pager data structures relates to each other
1141
1142``struct tee_pager_area``
1143-------------------------
1144This is a central data structure when handling paged
1145memory ranges. It's defined as:
1146
1147.. code-block:: c
1148
1149 struct tee_pager_area {
1150 struct fobj *fobj;
1151 size_t fobj_pgoffs;
1152 enum tee_pager_area_type type;
1153 uint32_t flags;
1154 vaddr_t base;
1155 size_t size;
1156 struct pgt *pgt;
1157 TAILQ_ENTRY(tee_pager_area) link;
1158 TAILQ_ENTRY(tee_pager_area) fobj_link;
1159 };
1160
1161Where ``base`` and ``size`` tells the memory range and ``fobj`` and
1162``fobj_pgoffs`` holds the content. A ``struct tee_pager_area`` can only use
1163``struct fobj`` and one ``struct pgt`` (translation table) so memory ranges
1164spanning multiple fobjs or pgts are split into multiple areas.
1165
1166``struct fobj``
1167---------------
1168This is a polymorph object, using different implmentations depending on how
1169it's initialized. It's defines as:
1170
1171.. code-block:: c
1172
1173 struct fobj_ops {
1174 void (*free)(struct fobj *fobj);
1175 TEE_Result (*load_page)(struct fobj *fobj, unsigned int page_idx,
1176 void *va);
1177 TEE_Result (*save_page)(struct fobj *fobj, unsigned int page_idx,
1178 const void *va);
1179 };
1180
1181 struct fobj {
1182 const struct fobj_ops *ops;
1183 unsigned int num_pages;
1184 struct refcount refc;
1185 struct tee_pager_area_head areas;
1186 };
1187
1188:``num_pages``: Tells how many pages this ``fobj`` covers.
1189:``refc``: A reference counter, everyone referring to a ``fobj`` need to
1190 increase and decrease this as needed.
1191:``areas``: A list of areas using this ``fobj``, traversed when making
1192 a virtual page unavailable.
1193
1194``struct tee_pager_pmem``
1195-------------------------
1196This structure represents a physical page. It's defined as:
1197
1198.. code-block:: c
1199
1200 struct tee_pager_pmem {
1201 unsigned int flags;
1202 unsigned int fobj_pgidx;
1203 struct fobj *fobj;
1204 void *va_alias;
1205 TAILQ_ENTRY(tee_pager_pmem) link;
1206 };
1207
1208:``PMEM_FLAG_DIRTY``: Bit is set in ``flags`` when the page is mapped
1209 read/write at at least one location.
1210:``PMEM_FLAG_HIDDEN``: Bit is set in ``flags`` when the page is hidden, that
1211 is, not accessible anywhere.
1212:``fobj_pgidx``: The page at this index in the ``fobj`` is used in this
1213 physical page.
1214:``fobj``: The ``fobj`` backing this page.
1215:``va_alias``: Virtual address where this physical page is updated
1216 when loading it from backing store or when writing it
1217 back.
1218
1219All ``struct tee_pager_pmem`` are stored either in the global list
1220``tee_pager_pmem_head`` or in ``tee_pager_lock_pmem_head``. The latter is
1221used by pages which are mapped and then locked in memory on demand. The
1222pages are returned back to ``tee_pager_pmem_head`` when the pages are
1223exlicitly released with a call to ``tee_pager_release_phys()``.
1224
1225A physical page can be used by more than one ``tee_pager_area``
1226simultaneously. This is also know as shared secure memory and will appear
1227as such for both read-only and read-write mappings.
1228
1229When a page is hidden it's unmapped from all translation tables and the
1230``PMEM_FLAG_HIDDEN`` bit is set, but kept in memory. When a physical page
1231is released it's also unmapped from all translation tables and it's content
1232is written back to storage, then the ``fobj`` field is set to ``NULL`` to
1233note the physical page as unused.
1234
1235Note that when ``struct tee_pager_pmem`` references a ``fobj`` it doesn't
1236update the reference counter since it's already guaranteed to be available
1237due the ``struct tee_pager_area`` which must reference the ``fobj`` too.
1238
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001239Paging of user TA
1240=================
1241Paging of user TAs can optionally be enabled with ``CFG_PAGED_USER_TA=y``.
1242Paging of user TAs is analogous to paging of OP-TEE kernel parts but with a few
1243differences:
1244
1245 - Read/write pages are paged in addition to read-only pages
1246 - Page tables are managed dynamically
1247
1248``tee_pager_add_uta_area(...)`` is used to setup initial read/write mapping
1249needed when populating the TA. When the TA is fully populated and relocated
1250``tee_pager_set_uta_area_attr(...)`` changes the mapping of the area to strict
1251permissions used when the TA is running.
1252
Jens Wiklanderaecf4412019-02-26 12:33:14 +01001253Paging shared secure memory
1254---------------------------
1255Shared secure memory is achieved by letting several ``tee_pager_area``
1256using the same backing ``fobj``. When a ``tee_pager_area`` is allocated and
1257assigned a ``fobj`` it's also added to a list for ``tee_pager_areas`` using
1258this ``fobj``. This helps when a physical page is released.
1259
1260When a fault occurs first a matching ``tee_pager_area`` is located. Then
1261``tee_pager_pmem_head`` is searched to see if a physical page already holds
1262the page of the ``fobj`` needed. If so the ``pgt`` is updated to map the
1263physical page at the appropriate locatation. If no physical page was holding
1264the page a new physical page is allocated, initialized and finally mapped.
1265
1266In order to make as few updates to mappings as possible changes to less
1267restricted, no access -> read-only or read-only to read-write, is done only
1268for the virtual address was used when the page fault occurred. Changes in
1269the other direction has to be done in all translation tables used to map
1270the physical page.
1271
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001272----
1273
1274.. _stacks:
1275
1276Stacks
1277******
1278Different stacks are used during different stages. The stacks are:
1279
1280 - **Secure monitor stack** (128 bytes), bound to the CPU. Only available if
1281 OP-TEE is compiled with a secure monitor always the case if the target is
1282 Armv7-A but never for Armv8-A.
1283
1284 - **Temp stack** (small ~1KB), bound to the CPU. Used when transitioning
1285 from one state to another. Interrupts are always disabled when using this
1286 stack, aborts are fatal when using the temp stack.
1287
1288 - **Abort stack** (medium ~2KB), bound to the CPU. Used when trapping a data
1289 or pre-fetch abort. Aborts from user space are never fatal the TA is only
1290 killed. Aborts from kernel mode are used by the pager to do the demand
1291 paging, if pager is disabled all kernel mode aborts are fatal.
1292
1293 - **Thread stack** (large ~8KB), not bound to the CPU instead used by the
1294 current thread/task. Interrupts are usually enabled when using this stack.
1295
1296Notes for Armv7-A/AArch32
1297 .. list-table::
1298 :header-rows: 1
1299 :widths: 1 5
1300
1301 * - Stack
1302 - Comment
1303
1304 * - Temp
1305 - Assigned to ``SP_SVC`` during entry/exit, always assigned to
1306 ``SP_IRQ`` and ``SP_FIQ``
1307
1308 * - Abort
1309 - Always assigned to ``SP_ABT``
1310
1311 * - Thread
1312 - Assigned to ``SP_SVC`` while a thread is active
1313
1314Notes for AArch64
1315 There are only two stack pointers, ``SP_EL1`` and ``SP_EL0``, available for
1316 OP-TEE in AArch64. When an exception is received stack pointer is always
1317 ``SP_EL1`` which is used temporarily while assigning an appropriate stack
1318 pointer for ``SP_EL0``. ``SP_EL1`` is always assigned the value of
1319 ``thread_core_local[cpu_id]``. This structure has some spare space for
1320 temporary storage of registers and also keeps the relevant stack pointers.
1321 In general when we talk about assigning a stack pointer to the CPU below we
1322 mean ``SP_EL0``.
1323
1324Boot
1325====
1326During early boot the CPU is configured with the temp stack which is used until
1327OP-TEE exits to normal world the first time.
1328
1329Notes for AArch64
1330 ``SPSEL`` is always ``0`` on entry/exit to have ``SP_EL0`` acting as stack
1331 pointer.
1332
1333Normal entry
1334============
1335Each time OP-TEE is entered from normal world the temp stack is used as the
1336initial stack. For fast calls, this is the only stack used. For normal calls an
1337empty thread slot is selected and the CPU switches to that stack.
1338
1339Normal exit
1340===========
1341Normal exit occurs when a thread has finished its task and the thread is freed.
1342When the main thread function, ``tee_entry_std(...)``, returns interrupts are
1343disabled and the CPU switches to the temp stack instead. The thread is freed and
1344OP-TEE exits to normal world.
1345
1346RPC exit
1347========
1348RPC exit occurs when OP-TEE need some service from normal world. RPC can
1349currently only be performed with a thread is in running state. RPC is initiated
1350with a call to ``thread_rpc(...)`` which saves the state in a way that when the
1351thread is restored it will continue at the next instruction as if this function
1352did a normal return. CPU switches to use the temp stack before returning to
1353normal world.
1354
1355Foreign interrupt exit
1356======================
1357Foreign interrupt exit occurs when OP-TEE receives a foreign interrupt. For Arm
1358GICv2 mode, foreign interrupt is sent as IRQ which is always handled in normal
1359world. Foreign interrupt exit is similar to RPC exit but it is
1360``thread_irq_handler(...)`` and ``elx_irq(...)`` (respectively for
1361Armv7-A/Aarch32 and for Aarch64) that saves the thread state instead. The thread
1362is resumed in the same way though. For Arm GICv3 mode, foreign interrupt is sent
1363as FIQ which could be handled by either secure world (EL3 in AArch64) or normal
1364world. This mode is not supported yet.
1365
1366Notes for Armv7-A/AArch32
1367 SP_IRQ is initialized to temp stack instead of a separate stack. Prior to
1368 exiting to normal world CPU state is changed to SVC and temp stack is
1369 selected.
1370
1371Notes for AArch64
1372 ``SP_EL0`` is assigned temp stack and is selected during IRQ processing. The
1373 original ``SP_EL0`` is saved in the thread context to be restored when
1374 resuming.
1375
1376Resume entry
1377============
1378OP-TEE is entered using the temp stack in the same way as for normal entry. The
1379thread to resume is looked up and the state is restored to resume execution. The
1380procedure to resume from an RPC exit or an foreign interrupt exit is exactly the
1381same.
1382
1383Syscall
1384=======
1385Syscall's are executed using the thread stack.
1386
1387Notes for Armv7-A/AArch32
1388 Nothing special ``SP_SVC`` is already set with thread stack.
1389
1390Notes for syscall AArch64
1391 Early in the exception processing the original ``SP_EL0`` is saved in
1392 ``struct thread_svc_regs`` in case the TA is executed in AArch64. Current
1393 thread stack is assigned to ``SP_EL0`` which is then selected. When
1394 returning ``SP_EL0`` is assigned what is in ``struct thread_svc_regs``. This
1395 allows ``tee_svc_sys_return_helper(...)`` having the syscall exception
1396 handler return directly to ``thread_unwind_user_mode(...)``.
1397
1398----
1399
1400.. _shared_memory:
1401
1402Shared Memory
1403*************
1404Shared Memory is a block of memory that is shared between the non-secure and the
1405secure world. It is used to transfer data between both worlds.
1406
Etienne Carriere9c600252019-03-11 11:01:48 +01001407The shared memory is allocated and managed by the non-secure world, i.e. the
1408Linux OP-TEE driver. Secure world only considers the individual shared buffers,
1409not their pool. Each shared memory is referenced with associated attributes:
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001410
Etienne Carriere9c600252019-03-11 11:01:48 +01001411 - Buffer start address and byte size,
1412 - Cache attributes of the shared memory buffer,
1413 - List of chunks if mapped from noncontiguous pages.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001414
Etienne Carriere9c600252019-03-11 11:01:48 +01001415Shared memory buffer references manipulated must fit inside one of the
1416shared memory areas known from the OP-TEE core. OP-TEE supports two kinds
Jerome Forissier7fa91cf2020-07-30 16:03:59 +02001417of shared memory areas: an area for contiguous buffers and an area for
1418noncontiguous buffers. At least one has to be enabled.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001419
Etienne Carriere9c600252019-03-11 11:01:48 +01001420Contiguous shared buffers
1421=========================
Etienne Carriere9c600252019-03-11 11:01:48 +01001422Configuration directives ``CFG_SHMEM_START`` and ``CFG_SHMEM_SIZE``
1423define a share memory area where shared memory buffers are contiguous.
1424Generic memory layout registers it as the ``MEM_AREA_NSEC_SHM`` memory area.
1425
1426The non-secure world issues ``OPTEE_SMC_GET_SHM_CONFIG`` to retrieve contiguous
1427shared memory area configuration:
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001428
1429 - Physical address of the start of the pool
1430 - Size of the pool
1431 - Whether or not the memory is cached
1432
Jens Wiklandera70d2f42019-04-25 12:40:49 +02001433Contiguous shared memory (also known as static or reserved shared memory)
1434is enabled with the configuration flag ``CFG_CORE_RESERVED_SHM=y``.
1435
Etienne Carriere9c600252019-03-11 11:01:48 +01001436Noncontiguous shared buffers
1437============================
Etienne Carriere9c600252019-03-11 11:01:48 +01001438To benefit from noncontiguous shared memory buffers, secure world register
1439dynamic shared memory areas and non-secure world must register noncontiguous
1440buffers prior to referring to them using the OP-TEE API.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001441
Etienne Carriere9c600252019-03-11 11:01:48 +01001442The OP-TEE core generic boot sequence discovers dynamic shared areas from the
1443device tree and/or areas explicitly registered by the platform.
1444
1445Non-secure side needs to register buffers as 4kByte chunks lists into OP-TEE
1446core using the ``OPTEE_MSG_CMD_REGISTER_SHM`` API prior referencing to them
1447using the OP-TEE invocation API.
1448
Jens Wiklandera70d2f42019-04-25 12:40:49 +02001449Noncontiguous shared memory (also known as dynamic shared memory) is
1450enabled with the configuration flag ``CFG_CORE_DYN_SHM=y``.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001451
Jerome Forissier7fa91cf2020-07-30 16:03:59 +02001452For performance reasons, the TEE Client Library (``libteec``) uses
1453noncontiguous shared memory when available since it avoids copies in some
1454situations.
1455
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001456Shared Memory Chunk Allocation
1457==============================
1458It is the Linux kernel driver for OP-TEE that is responsible for allocating
1459chunks of shared memory. OP-TEE linux kernel driver relies on linux kernel
1460generic allocation support (``CONFIG_GENERIC_ALLOCATION``) to allocation/release
1461of shared memory physical chunks. OP-TEE linux kernel driver relies on linux
1462kernel dma-buf support (``CONFIG_DMA_SHARED_BUFFER``) to track shared memory
1463buffers references.
1464
Jens Wiklander5b145172021-06-30 10:15:41 +02001465Registering shared memory
1466=========================
1467
1468Only dynamic or physically non-contiguous shared memory needs to be
1469registered. Static or physically contiguous shared memory is already known
1470to OP-TEE OS.
1471
1472SMC based OP-TEE MSG ABI
1473------------------------
1474
1475With the SMC based OP-TEE MSG ABI there are a few exceptions where memory
1476doesn't need to be shared before it can be accessed from OP-TEE OS. These
1477are:
1478
14791. When issuing the SMC ``OPTEE_SMC_CALL_WITH_ARG`` where the physical
1480 address of the supplied ``struct optee_msg_arg`` is passed in one of the
1481 registers.
14822. When issuing the SMC ``OPTEE_SMC_CALL_RETURN_FROM_RPC`` as a return from
1483 the request ``OPTEE_SMC_RETURN_RPC_ALLOC`` to allocate memory. This RPC
1484 return is combined with an implicit registration of shared memory. The
1485 registration is ended with a ``OPTEE_SMC_RETURN_RPC_FREE`` request.
1486
1487.. uml::
1488 :align: center
1489 :caption: Register shared memory example
1490
1491 participant "Normal World\nOS Kernel" as ns
1492 participant "Secure World\nOP-TEE OS" as sec
1493
1494 ns -> sec : OPTEE_MSG_CMD_REGISTER_SHM(Cookie, memory)
1495 sec -> sec : Register shared memory passed
1496 sec -> ns : Return
1497
1498.. uml::
1499 :align: center
1500 :caption: Unregister shared memory example
1501
1502 participant "Normal World\nOS Kernel" as ns
1503 participant "Secure World\nOP-TEE OS" as sec
1504
1505 ns -> sec : OPTEE_MSG_CMD_UNREGISTER_SHM(Cookie)
1506 sec -> sec : Unregister shared memory
1507 sec -> ns : Return
1508
1509FF-A based OP-TEE MSG ABI
1510-------------------------
1511
1512With the FF-A based OP-TEE MSG ABI memory must always be registered before
1513it can be used by OP-TEE OS. This case can potentially also involve another
1514component in secure world, SPMC at ``S-EL2`` a secure hypervisor which
1515controls which memory OP-TEE OS can see or use.
1516
1517In the case where there are no SPMC at ``S-EL2`` OP-TEE OS will take care
1518of that part of the communication with normal world. This means that for
1519normal world communication with OP-TEE OS is the same regardless of the
1520presence of a secure hypervisor.
1521
1522Registration of shared memory is a two step procedure. It's first
1523registered with a call to the SPMC which returns a cookie or global memory
1524handle. This cookie is later used when calling OP-TEE OS, if the cookie
1525isn't already known to OP-TEE OS it will ask the SPMC to make the memory
1526available. This lazy second step is a way of saving an extra round trip to
1527secure world.
1528
1529.. uml::
1530 :align: center
1531 :caption: Register shared memory example
1532
1533 participant "Normal World\nOS Kernel" as ns
1534 participant "Secure World\nSPMC" as spmc
1535 participant "Secure World\nOP-TEE OS" as sec
1536
1537 ns -> spmc : FFA_MEM_SHARE(memory)
1538 spmc -> spmc : Register shared memory passed
1539 spmc -> ns : Return cookie
1540
1541.. uml::
1542 :align: center
1543 :caption: Calling OP-TEE OS with shared memory
1544
1545 participant "Normal World\nOS Kernel" as ns
1546 participant "Secure World\nSPMC" as spmc
1547 participant "Secure World\nOP-TEE OS" as sec
1548
1549 ns -> sec: OPTEE_FFA_YIELDING_CALL_WITH_ARG(cookie)
1550 alt cookie not known
1551 sec -> spmc : FFA_MEM_RETRIEVE_REQ(cookie)
1552 spmc -> sec : Return memory description
1553 sec -> sec : Register shared memory
1554 end
1555 sec -> sec : Process the yielding call
1556 sec -> ns : Return
1557
1558Unregistration of shared memory is also done in two steps. First with a
1559call to OP-TEE and then with a call to the SPMC. If the lazy second
1560step of shared memory has not been done, then OP-TEE OS doesn't need
1561to interact with the SPMC.
1562
1563.. uml::
1564 :align: center
1565 :caption: Unregister shared memroy
1566
1567 participant "Normal World\nOS Kernel" as ns
1568 participant "Secure World\nSPMC" as spmc
1569 participant "Secure World\nOP-TEE OS" as sec
1570
1571 ns -> sec: OPTEE_FFA_UNREGISTER_SHM(cookie)
1572 alt cookie known
1573 sec -> sec : Unregister shared memory
1574 sec -> spmc : FFA_MEM_RELINQUISH(cookie)
1575 spmc -> sec : Return
1576 end
1577 sec -> ns : Return
1578
1579 ns -> spmc : FFA_MEM_RECLAIM(cookie)
1580 spmc -> spmc : Unregister shared memory
1581 spmc -> ns : Return
1582
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001583Using shared memory
1584===================
1585From the Client Application
1586 The client application can ask for shared memory allocation using the
1587 GlobalPlatform Client API function ``TEEC_AllocateSharedMemory(...)``. The
Etienne Carriere9c600252019-03-11 11:01:48 +01001588 client application can also register a memory through the GlobalPlatform
1589 Client API function ``TEEC_RegisterSharedMemory(...)``. The shared memory
1590 reference can then be used as parameter when invoking a trusted application.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001591
1592From the Linux Driver
1593 Occasionally the Linux kernel driver needs to allocate shared memory for the
1594 communication with secure world, for example when using buffers of type
1595 ``TEEC_TempMemoryReference``.
1596
1597From OP-TEE core
1598 In case OP-TEE core needs information from TEE supplicant (dynamic TA
1599 loading, REE time request,...), shared memory must be allocated. Allocation
1600 depends on the use case. OP-TEE core asks for the following shared memory
1601 allocation:
1602
1603 - ``optee_msg_arg`` structure, used to pass the arguments to the
1604 non-secure world, where the allocation will be done by sending a
1605 ``OPTEE_SMC_RPC_FUNC_ALLOC`` message.
1606
1607 - In some cases, a payload might be needed for storing the result from
1608 TEE supplicant, for example when loading a Trusted Application. This
1609 type of allocation will be done by sending the message
1610 ``OPTEE_MSG_RPC_CMD_SHM_ALLOC(OPTEE_MSG_RPC_SHM_TYPE_APPL,...)``,
1611 which then will return:
1612
1613 - the physical address of the shared memory
1614 - a handle to the memory, that later on will be used later on when
1615 freeing this memory.
1616
1617From TEE Supplicant
1618 TEE supplicant is also working with shared memory, used to exchange data
1619 between normal and secure worlds. TEE supplicant receives a memory address
1620 from the OP-TEE core, used to store the data. This is for example the case
1621 when a Trusted Application is loaded. In this case, TEE supplicant must
1622 register the provided shared memory in the same way a client application
1623 would do, involving the Linux driver.
1624
1625----
1626
1627.. _smc:
1628
1629SMC
1630***
1631SMC Interface
1632=============
1633OP-TEE's SMC interface is defined in two levels using optee_smc.h_ and
1634optee_msg.h_. The former file defines SMC identifiers and what is passed in the
1635registers for each SMC. The latter file defines the OP-TEE Message protocol
1636which is not restricted to only SMC even if that currently is the only option
1637available.
1638
1639SMC communication
1640=================
1641The main structure used for the SMC communication is defined in ``struct
1642optee_msg_arg`` (in optee_msg.h_). If we are looking into the source code, we
1643could see that communication mainly is achieved using ``optee_msg_arg`` and
1644``thread_smc_args`` (in thread.h_), where ``optee_msg_arg`` could be seen as the
1645main structure. What will happen is that the :ref:`linux_kernel` driver will get
1646the parameters either from :ref:`optee_client` or directly from an internal
1647service in Linux kernel. The TEE driver will populate the struct
1648``optee_msg_arg`` with the parameters plus some additional bookkeeping
1649information. Parameters for the SMC are passed in registers 1 to 7, register 0
1650holds the SMC id which among other things tells whether it is a standard or a
1651fast call.
1652
1653----
1654
1655.. _thread_handling:
1656
1657Thread handling
1658***************
1659OP-TEE core uses a couple of threads to be able to support running jobs in
1660parallel (not fully enabled!). There are handlers for different purposes. In
1661thread.c_ you will find a function called ``thread_init_primary(...)`` which
1662assigns ``init_handlers`` (functions) that should be called when OP-TEE core
1663receives standard or fast calls, FIQ and PSCI calls. There are default handlers
1664for these services, but the platform can decide if they want to implement their
1665own platform specific handlers instead.
1666
1667Synchronization primitives
1668==========================
1669OP-TEE has three primitives for synchronization of threads and CPUs:
1670*spin-lock*, *mutex*, and *condvar*.
1671
1672Spin-lock
1673 A spin-lock is represented as an ``unsigned int``. This is the most
1674 primitive lock. Interrupts should be disabled before attempting to take a
1675 spin-lock and should remain disabled until the lock is released. A spin-lock
1676 is initialized with ``SPINLOCK_UNLOCK``.
1677
1678 .. list-table:: Spin lock functions
1679 :header-rows: 1
1680 :widths: 1 5
1681
1682 * - Function
1683 - Purpose
1684
1685 * - ``cpu_spin_lock(...)``
1686 - Locks a spin-lock
1687
1688 * - ``cpu_spin_trylock(...)``
1689 - Locks a spin-lock if unlocked and returns ``0`` else the spin-lock
1690 is unchanged and the function returns ``!0``
1691
1692 * - ``cpu_spin_unlock(...)``
1693 - Unlocks a spin-lock
1694
1695Mutex
1696 A mutex is represented by ``struct mutex``. A mutex can be locked and
1697 unlocked with interrupts enabled or disabled, but only from a normal thread.
1698 A mutex cannot be used in an interrupt handler, abort handler or before a
1699 thread has been selected for the CPU. A mutex is initialized with either
1700 ``MUTEX_INITIALIZER`` or ``mutex_init(...)``.
1701
1702 .. list-table:: Mutex functions
1703 :header-rows: 1
1704 :widths: 1 5
1705
1706 * - Function
1707 - Purpose
1708
1709 * - ``mutex_lock(...)``
1710 - Locks a mutex. If the mutex is unlocked this is a fast operation,
1711 else the function issues an RPC to wait in normal world.
1712
1713 * - ``mutex_unlock(...)``
1714 - Unlocks a mutex. If there is no waiters this is a fast operation,
1715 else the function issues an RPC to wake up a waiter in normal world.
1716
1717 * - ``mutex_trylock(...)``
1718 - Locks a mutex if unlocked and returns ``true`` else the mutex is
1719 unchanged and the function returns ``false``.
1720
1721 * - ``mutex_destroy(...)``
1722 - Asserts that the mutex is unlocked and there is no waiters, after
1723 this the memory used by the mutex can be freed.
1724
1725 When a mutex is locked it is owned by the thread calling ``mutex_lock(...)``
1726 or ``mutex_trylock(...)``, the mutex may only be unlocked by the thread
1727 owning the mutex. A thread should not exit to TA user space when holding a
1728 mutex.
1729
1730Condvar
1731 A condvar is represented by ``struct condvar``. A condvar is similar to a
1732 ``pthread_condvar_t`` in the pthreads standard, only less advanced.
1733 Condition variables are used to wait for some condition to be fulfilled and
1734 are always used together a mutex. Once a condition variable has been used
1735 together with a certain mutex, it must only be used with that mutex until
1736 destroyed. A condvar is initialized with ``CONDVAR_INITIALIZER`` or
1737 ``condvar_init(...)``.
1738
1739 .. list-table:: Condvar functions
1740 :header-rows: 1
1741 :widths: 1 5
1742
1743 * - Function
1744 - Purpose
1745
1746 * - ``condvar_wait(...)``
1747 - Atomically unlocks the supplied mutex and waits in normal world via
1748 an RPC for the condition variable to be signaled, when the function
1749 returns the mutex is locked again.
1750
1751 * - ``condvar_signal(...)``
1752 - Wakes up one waiter of the condition variable (waiting in
1753 ``condvar_wait(...)``).
1754
1755 * - ``condvar_broadcast(...)``
1756 - Wake up all waiters of the condition variable.
1757
1758 The caller of ``condvar_signal(...)`` or ``condvar_broadcast(...)`` should
1759 hold the mutex associated with the condition variable to guarantee that a
1760 waiter does not miss the signal.
1761
1762.. _core/arch/arm/kernel/thread.c: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/kernel/thread.c
1763.. _optee_msg.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/optee_msg.h
1764.. _optee_smc.h: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/sm/optee_smc.h
1765.. _thread.c: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/kernel/thread.c
1766.. _thread.h: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/kernel/thread.h
Etienne Carriereeda1e0f2023-05-07 17:29:28 +02001767.. _interrupt.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/kernel/interrupt.h
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001768
1769.. _ARM_DEN0028A_SMC_Calling_Convention: http://infocenter.arm.com/help/topic/com.arm.doc.den0028b/ARM_DEN0028B_SMC_Calling_Convention.pdf
1770.. _Cortex-A53 TRM: http://infocenter.arm.com/help/topic/com.arm.doc.ddi0500j/DDI0500J_cortex_a53_trm.pdf
1771.. _drivers/tee/optee: https://github.com/torvalds/linux/tree/master/drivers/tee/optee
1772.. _Trusted Firmware A: https://github.com/ARM-software/arm-trusted-firmware