blob: 3c6727f77cd1577bcbf1ca353cd25d8e2c845ac5 [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
393----
394
395.. _memory_objects:
396
397Memory objects
398**************
399A memory object, **MOBJ**, describes a piece of memory. The interface provided
400is mostly abstract when it comes to using the MOBJ to populate translation
401tables etc. There are different kinds of MOBJs describing:
402
403 - Physically contiguous memory
404 - created with ``mobj_phys_alloc(...)``.
405
406 - Virtual memory
407 - one instance with the name ``mobj_virt`` available.
408 - spans the entire virtual address space.
409
410 - Physically contiguous memory allocated from a ``tee_mm_pool_t *``
411 - created with ``mobj_mm_alloc(...)``.
412
413 - Paged memory
414 - created with ``mobj_paged_alloc(...)``.
415 - only contains the supplied size and makes ``mobj_is_paged(...)``
416 return true if supplied as argument.
417
418 - Secure copy paged shared memory
419 - created with ``mobj_seccpy_shm_alloc(...)``.
420 - makes ``mobj_is_paged(...)`` and ``mobj_is_secure(...)`` return true
421 if supplied as argument.
422
423----
424
425.. _mmu:
426
427MMU
428***
429Translation tables
430==================
431OP-TEE uses several L1 translation tables, one large spanning 4 GiB and two or
432more small tables spanning 32 MiB. The large translation table handles kernel
433mode mapping and matches all addresses not covered by the small translation
434tables. The small translation tables are assigned per thread and covers the
435mapping of the virtual memory space for one TA context.
436
437Memory space between small and large translation table is configured by TTBRC.
438TTBR1 always points to the large translation table. TTBR0 points to the a small
439translation table when user mapping is active and to the large translation table
440when no user mapping is currently active. For details about registers etc,
441please refer to a Technical Reference Manual for your architecture, for example
442`Cortex-A53 TRM`_.
443
444The translation tables has certain alignment constraints, the alignment (of the
445physical address) has to be the same as the size of the translation table. The
446translation tables are statically allocated to avoid fragmentation of memory due
447to the alignment constraints.
448
449Each thread has one small L1 translation table of its own. Each TA context has a
450compact representation of its L1 translation table. The compact representation
451is used to initialize the thread specific L1 translation table when the TA
452context is activated.
453
454.. graphviz::
455
456 digraph xlat_table {
457 graph [
458 rankdir = "LR"
459 ];
460 node [
461 fontsize = "16"
462 shape = "ellipse"
463 ];
464 edge [
465 ];
466 "node_ttb" [
467 label = "<f0> TTBR0 | <f1> TTBR1"
468 shape = "record"
469 ];
470 "node_large_l1" [
471 label = "<f0> Large L1\nSpans 4 GiB"
472 shape = "record"
473 ];
474 "node_small_l1" [
475 label = "Small L1\nSpans 32 MiB\nper entry | <f0> 0 | <f1> 1 | ... | <fn> n"
476 shape = "record"
477 ];
478
479 "node_ttb":f0 -> "node_small_l1":f0 [ label = "Thread 0 ctx active" ];
480 "node_ttb":f0 -> "node_small_l1":f1 [ label = "Thread 1 ctx active" ];
481 "node_ttb":f0 -> "node_small_l1":fn [ label = "Thread n ctx active" ];
482 "node_ttb":f0 -> "node_large_l1" [ label="No active ctx" ];
483 "node_ttb":f1 -> "node_large_l1";
484 }
485
Jens Wiklander03b05a02019-02-25 13:44:38 +0100486Page table cache
487================
488Page tables used to map TAs are managed with the page table cache. When the
489context of a TA is unmapped, all its page tables are released with a call
490to ``pgt_free()``. All page tables needed when mapping a TA are allocated
491using ``pgt_alloc()``.
492
493A fixed maximum number of translation tables are available in a pool. One
494thread may execute a TA which needs all or almost all tables. This can
495block TAs from being executed by other threads. To ensure that all TAs
496eventually will be permitted to execute ``pgt_alloc()`` temporarily frees
497eventual tables allocated before waiting for tables to become available.
498
499The page table cache behaves differently depending on configuration
500options.
501
502Without paging (``CFG_WITH_PAGER=n``)
503-------------------------------------
504This is the easiest configuration. All page tables are statically allocated
505in the ``.nozi.pgt_cache`` section. ``pgt_alloc()`` allocates tables from the
506free-list and ``pgt_free()`` returns the tables directly to the free-list.
507
508With paging enabled (``CFG_WITH_PAGER=y``)
509------------------------------------------
510
511Page tables are allocated as zero initialized locked pages during boot
512using ``tee_pager_alloc()``. Locked pages are populated with physical pages
513on demand from the pager. The physical page can be released when not needed
514any longer with ``tee_pager_release_phys()``.
515
516With ``CFG_WITH_LPAE=y`` each translation table has the same size as a
517physical page which makes it easy to release the physical page when the
518translation table isn't needed any longer. With the short-descriptor table
519format (``CFG_WITH_LPAE=n``) it becomes more complicated as four
520translation tables are stored in each page. Additional bookkeeping is used
521to tell when the page for used by four separate translation tables can be
522released.
523
524With paging of user TA enabled (``CFG_PAGED_USER_TA=y``)
525--------------------------------------------------------
526With paging of user TAs enabled a cache of recently used translation tables
527is used. This can save us from a storm of page faults when restoring the
528mappings of a recently unmapped TA. Which translation tables should be
529cached is indicated with reference counting by the pager on used tables.
530When a table needs to be forcefully freed
531``tee_pager_pgt_save_and_release_entries()`` is called to let the pager
532know that the table can't be used any longer.
533
534When a mapping in a TA is removed it also needs to be purged from cached
535tables with ``pgt_flush_ctx_range()`` to prevent old mappings from being
536accidentally reused.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200537
538Switching to user mode
539======================
540This section only applies with following configuration flags:
541
542 - ``CFG_WITH_LPAE=n``
543 - ``CFG_CORE_UNMAP_CORE_AT_EL0=y``
544
545When switching to user mode only a minimal kernel mode mapping is kept. This is
546achieved by selecting a zeroed out big L1 translation in TTBR1 when
547transitioning to user mode. When returning back to kernel mode the original L1
548translation table is restored in TTBR1.
549
550Switching to normal world
551=========================
552When switching to normal world either via a foreign interrupt (see
553:ref:`native_foreign_irqs` or RPC there is a chance that secure world will
554resume execution on a different CPU. This means that the new CPU need to be
555configured with the context of the currently active TA. This is solved by always
Jens Wiklanderddde3a82019-02-25 12:46:18 +0100556setting the TA context in the CPU when resuming execution.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200557
558----
559
560.. _pager:
561
562Pager
563*****
564OP-TEE currently requires >256 KiB RAM for OP-TEE kernel memory. This is not a
565problem if OP-TEE uses TrustZone protected DDR, but for security reasons OP-TEE
566may need to use TrustZone protected SRAM instead. The amount of available SRAM
567varies between platforms, from just a few KiB up to over 512 KiB. Platforms with
568just a few KiB of SRAM cannot be expected to be able to run a complete TEE
569solution in SRAM. But those with 128 to 256 KiB of SRAM can be expected to have
570a capable TEE solution in SRAM. The pager provides a solution to this by demand
571paging parts of OP-TEE using virtual memory.
572
573Secure memory
574=============
575TrustZone protected SRAM is generally considered more secure than TrustZone
576protected DRAM as there is usually more attack vectors on DRAM. The attack
577vectors are hardware dependent and can be different for different platforms.
578
579Backing store
580=============
581TrustZone protected DRAM or in some cases non-secure DRAM is used as backing
582store. The data in the backing store is integrity protected with one hash
583(SHA-256) per page (4KiB). Readonly pages are not encrypted since the OP-TEE
584binary itself is not encrypted.
585
586Partitioning of memory
587======================
588The code that handles demand paging must always be available as it would
589otherwise lead to deadlock. The virtual memory is partitioned as:
590
591 +--------------+-------------------+
592 | Type | Sections |
593 +==============+===================+
594 | unpaged | | text |
595 | | | rodata |
596 | | | data |
597 | | | bss |
598 | | | heap1 |
599 | | | nozi |
600 | | | heap2 |
601 +--------------+-------------------+
602 | init / paged | | text_init |
603 | | | rodata_init |
604 +--------------+-------------------+
605 | paged | | text_pageable |
606 | | | rodata_pageable |
607 +--------------+-------------------+
608 | demand alloc | |
609 +--------------+-------------------+
610
611Where ``nozi`` stands for "not zero initialized", this section contains entry
612stacks (thread stack when TEE pager is not enabled) and translation tables (TEE
613pager cached translation table when the pager is enabled and LPAE MMU is used).
614
615The ``init`` area is available when OP-TEE is initializing and contains
616everything that is needed to initialize the pager. After the pager has been
617initialized this area will be used for demand paged instead.
618
619The ``demand alloc`` area is a special area where the pages are allocated and
620removed from the pager on demand. Those pages are returned when OP-TEE does not
621need them any longer. The thread stacks currently belongs this area. This means
622that when a stack is not used the physical pages can be used by the pager for
623better performance.
624
625The technique to gather code in the different area is based on compiling all
626functions and data into separate sections. The unpaged text and rodata is then
627gathered by linking all object files with ``--gc-sections`` to eliminate
628sections that are outside the dependency graph of the entry functions for
629unpaged functions. A script analyzes this ELF file and generates the bits of the
630final link script. The process is repeated for init text and rodata. What is
631not "unpaged" or "init" becomes "paged".
632
633Partitioning of the binary
634==========================
635.. note::
636 The struct definitions provided in this section are explicitly covered by
637 the following dual license:
638
639 .. code-block:: none
640
641 SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
642
643The binary is partitioned into four parts as:
644
645
646 +----------+
647 | Binary |
648 +==========+
649 | Header |
650 +----------+
651 | Init |
652 +----------+
653 | Hashes |
654 +----------+
655 | Pageable |
656 +----------+
657
658The header is defined as:
659
660.. code-block:: c
661
662 #define OPTEE_MAGIC 0x4554504f
663 #define OPTEE_VERSION 1
664 #define OPTEE_ARCH_ARM32 0
665 #define OPTEE_ARCH_ARM64 1
666
667 struct optee_header {
668 uint32_t magic;
669 uint8_t version;
670 uint8_t arch;
671 uint16_t flags;
672 uint32_t init_size;
673 uint32_t init_load_addr_hi;
674 uint32_t init_load_addr_lo;
675 uint32_t init_mem_usage;
676 uint32_t paged_size;
677 };
678
679The header is only used by the loader of OP-TEE, not OP-TEE itself. To
680initialize OP-TEE the loader loads the complete binary into memory and copies
681what follows the header and the following ``init_size`` bytes to
682``(init_load_addr_hi << 32 | init_load_addr_lo)``. ``init_mem_usage`` is used by
683the loader to be able to check that there is enough physical memory available
684for OP-TEE to be able to initialize at all. The loader supplies in ``r0/x0`` the
685address of the first byte following what was not copied and jumps to the load
686address to start OP-TEE.
687
688In addition to overall binary with partitions inside described as above, three
689extra binaries are generated simultaneously during build process for loaders who
690support loading separate binaries:
691
692 +-----------+
693 | v2 binary |
694 +===========+
695 | Header |
696 +-----------+
697
698 +-----------+
699 | v2 binary |
700 +===========+
701 | Init |
702 +-----------+
703 | Hashes |
704 +-----------+
705
706 +-----------+
707 | v2 binary |
708 +===========+
709 | Pageable |
710 +-----------+
711
712In this case, loaders load header binary first to get image list and information
713of each image; and then load each of them into specific load address assigned in
714structure. These binaries are named with `v2` suffix to distinguish from the
715existing binaries. Header format is updated to help loaders loading binaries
716efficiently:
717
718.. code-block:: c
719
720 #define OPTEE_IMAGE_ID_PAGER 0
721 #define OPTEE_IMAGE_ID_PAGED 1
722
723 struct optee_image {
724 uint32_t load_addr_hi;
725 uint32_t load_addr_lo;
726 uint32_t image_id;
727 uint32_t size;
728 };
729
730 struct optee_header_v2 {
731 uint32_t magic;
732 uint8_t version;
733 uint8_t arch;
734 uint16_t flags;
735 uint32_t nb_images;
736 struct optee_image optee_image[];
737 };
738
739Magic number and architecture are identical as original. Version is increased to
740two. ``load_addr_hi`` and ``load_addr_lo`` may be ``0xFFFFFFFF`` for pageable
741binary since pageable part may get loaded by loader into dynamic available
742position. ``image_id`` indicates how loader handles current binary. Loaders who
743don't support separate loading just ignore all v2 binaries.
744
745Initializing the pager
746======================
747The pager is initialized as early as possible during boot in order to minimize
748the "init" area. The global variable ``tee_mm_vcore`` describes the virtual
749memory range that is covered by the level 2 translation table supplied to
750``tee_pager_init(...)``.
751
752Assign pageable areas
753---------------------
754A virtual memory range to be handled by the pager is registered with a call to
755``tee_pager_add_core_area()``.
756
757.. code-block:: c
758
759 bool tee_pager_add_area(tee_mm_entry_t *mm,
760 uint32_t flags,
761 const void *store,
762 const void *hashes);
763
764which takes a pointer to ``tee_mm_entry_t`` to tell the range, flags to tell how
765memory should be mapped (readonly, execute etc), and pointers to backing store
766and hashes of the pages.
767
768Assign physical pages
769---------------------
770Physical SRAM pages are supplied by calling ``tee_pager_add_pages(...)``
771
772.. code-block:: c
773
774 void tee_pager_add_pages(tee_vaddr_t vaddr,
775 size_t npages,
776 bool unmap);
777
778``tee_pager_add_pages(...)`` takes the physical address stored in the entry
779mapping the virtual address ``vaddr`` and ``npages`` entries after that and uses
780it to map new pages when needed. The unmap parameter tells whether the pages
781should be unmapped immediately since they does not contain initialized data or
782be kept mapped until they need to be recycled. The pages in the "init" area are
783supplied with ``unmap == false`` since those page have valid content and are in
784use.
785
786Invocation
787==========
788The pager is invoked as part of the abort handler. A pool of physical pages are
789used to map different virtual addresses. When a new virtual address needs to be
790mapped a free physical page is mapped at the new address, if a free physical
791page cannot be found the oldest physical page is selected instead. When the page
792is mapped new data is copied from backing store and the hash of the page is
793verified. If it is OK the pager returns from the exception to resume the
794execution.
795
Jens Wiklanderaecf4412019-02-26 12:33:14 +0100796Data structures
797===============
798.. figure:: ../images/core/tee_pager_area.png
799 :figclass: align-center
800
801 How the main pager data structures relates to each other
802
803``struct tee_pager_area``
804-------------------------
805This is a central data structure when handling paged
806memory ranges. It's defined as:
807
808.. code-block:: c
809
810 struct tee_pager_area {
811 struct fobj *fobj;
812 size_t fobj_pgoffs;
813 enum tee_pager_area_type type;
814 uint32_t flags;
815 vaddr_t base;
816 size_t size;
817 struct pgt *pgt;
818 TAILQ_ENTRY(tee_pager_area) link;
819 TAILQ_ENTRY(tee_pager_area) fobj_link;
820 };
821
822Where ``base`` and ``size`` tells the memory range and ``fobj`` and
823``fobj_pgoffs`` holds the content. A ``struct tee_pager_area`` can only use
824``struct fobj`` and one ``struct pgt`` (translation table) so memory ranges
825spanning multiple fobjs or pgts are split into multiple areas.
826
827``struct fobj``
828---------------
829This is a polymorph object, using different implmentations depending on how
830it's initialized. It's defines as:
831
832.. code-block:: c
833
834 struct fobj_ops {
835 void (*free)(struct fobj *fobj);
836 TEE_Result (*load_page)(struct fobj *fobj, unsigned int page_idx,
837 void *va);
838 TEE_Result (*save_page)(struct fobj *fobj, unsigned int page_idx,
839 const void *va);
840 };
841
842 struct fobj {
843 const struct fobj_ops *ops;
844 unsigned int num_pages;
845 struct refcount refc;
846 struct tee_pager_area_head areas;
847 };
848
849:``num_pages``: Tells how many pages this ``fobj`` covers.
850:``refc``: A reference counter, everyone referring to a ``fobj`` need to
851 increase and decrease this as needed.
852:``areas``: A list of areas using this ``fobj``, traversed when making
853 a virtual page unavailable.
854
855``struct tee_pager_pmem``
856-------------------------
857This structure represents a physical page. It's defined as:
858
859.. code-block:: c
860
861 struct tee_pager_pmem {
862 unsigned int flags;
863 unsigned int fobj_pgidx;
864 struct fobj *fobj;
865 void *va_alias;
866 TAILQ_ENTRY(tee_pager_pmem) link;
867 };
868
869:``PMEM_FLAG_DIRTY``: Bit is set in ``flags`` when the page is mapped
870 read/write at at least one location.
871:``PMEM_FLAG_HIDDEN``: Bit is set in ``flags`` when the page is hidden, that
872 is, not accessible anywhere.
873:``fobj_pgidx``: The page at this index in the ``fobj`` is used in this
874 physical page.
875:``fobj``: The ``fobj`` backing this page.
876:``va_alias``: Virtual address where this physical page is updated
877 when loading it from backing store or when writing it
878 back.
879
880All ``struct tee_pager_pmem`` are stored either in the global list
881``tee_pager_pmem_head`` or in ``tee_pager_lock_pmem_head``. The latter is
882used by pages which are mapped and then locked in memory on demand. The
883pages are returned back to ``tee_pager_pmem_head`` when the pages are
884exlicitly released with a call to ``tee_pager_release_phys()``.
885
886A physical page can be used by more than one ``tee_pager_area``
887simultaneously. This is also know as shared secure memory and will appear
888as such for both read-only and read-write mappings.
889
890When a page is hidden it's unmapped from all translation tables and the
891``PMEM_FLAG_HIDDEN`` bit is set, but kept in memory. When a physical page
892is released it's also unmapped from all translation tables and it's content
893is written back to storage, then the ``fobj`` field is set to ``NULL`` to
894note the physical page as unused.
895
896Note that when ``struct tee_pager_pmem`` references a ``fobj`` it doesn't
897update the reference counter since it's already guaranteed to be available
898due the ``struct tee_pager_area`` which must reference the ``fobj`` too.
899
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200900Paging of user TA
901=================
902Paging of user TAs can optionally be enabled with ``CFG_PAGED_USER_TA=y``.
903Paging of user TAs is analogous to paging of OP-TEE kernel parts but with a few
904differences:
905
906 - Read/write pages are paged in addition to read-only pages
907 - Page tables are managed dynamically
908
909``tee_pager_add_uta_area(...)`` is used to setup initial read/write mapping
910needed when populating the TA. When the TA is fully populated and relocated
911``tee_pager_set_uta_area_attr(...)`` changes the mapping of the area to strict
912permissions used when the TA is running.
913
Jens Wiklanderaecf4412019-02-26 12:33:14 +0100914Paging shared secure memory
915---------------------------
916Shared secure memory is achieved by letting several ``tee_pager_area``
917using the same backing ``fobj``. When a ``tee_pager_area`` is allocated and
918assigned a ``fobj`` it's also added to a list for ``tee_pager_areas`` using
919this ``fobj``. This helps when a physical page is released.
920
921When a fault occurs first a matching ``tee_pager_area`` is located. Then
922``tee_pager_pmem_head`` is searched to see if a physical page already holds
923the page of the ``fobj`` needed. If so the ``pgt`` is updated to map the
924physical page at the appropriate locatation. If no physical page was holding
925the page a new physical page is allocated, initialized and finally mapped.
926
927In order to make as few updates to mappings as possible changes to less
928restricted, no access -> read-only or read-only to read-write, is done only
929for the virtual address was used when the page fault occurred. Changes in
930the other direction has to be done in all translation tables used to map
931the physical page.
932
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200933----
934
935.. _stacks:
936
937Stacks
938******
939Different stacks are used during different stages. The stacks are:
940
941 - **Secure monitor stack** (128 bytes), bound to the CPU. Only available if
942 OP-TEE is compiled with a secure monitor always the case if the target is
943 Armv7-A but never for Armv8-A.
944
945 - **Temp stack** (small ~1KB), bound to the CPU. Used when transitioning
946 from one state to another. Interrupts are always disabled when using this
947 stack, aborts are fatal when using the temp stack.
948
949 - **Abort stack** (medium ~2KB), bound to the CPU. Used when trapping a data
950 or pre-fetch abort. Aborts from user space are never fatal the TA is only
951 killed. Aborts from kernel mode are used by the pager to do the demand
952 paging, if pager is disabled all kernel mode aborts are fatal.
953
954 - **Thread stack** (large ~8KB), not bound to the CPU instead used by the
955 current thread/task. Interrupts are usually enabled when using this stack.
956
957Notes for Armv7-A/AArch32
958 .. list-table::
959 :header-rows: 1
960 :widths: 1 5
961
962 * - Stack
963 - Comment
964
965 * - Temp
966 - Assigned to ``SP_SVC`` during entry/exit, always assigned to
967 ``SP_IRQ`` and ``SP_FIQ``
968
969 * - Abort
970 - Always assigned to ``SP_ABT``
971
972 * - Thread
973 - Assigned to ``SP_SVC`` while a thread is active
974
975Notes for AArch64
976 There are only two stack pointers, ``SP_EL1`` and ``SP_EL0``, available for
977 OP-TEE in AArch64. When an exception is received stack pointer is always
978 ``SP_EL1`` which is used temporarily while assigning an appropriate stack
979 pointer for ``SP_EL0``. ``SP_EL1`` is always assigned the value of
980 ``thread_core_local[cpu_id]``. This structure has some spare space for
981 temporary storage of registers and also keeps the relevant stack pointers.
982 In general when we talk about assigning a stack pointer to the CPU below we
983 mean ``SP_EL0``.
984
985Boot
986====
987During early boot the CPU is configured with the temp stack which is used until
988OP-TEE exits to normal world the first time.
989
990Notes for AArch64
991 ``SPSEL`` is always ``0`` on entry/exit to have ``SP_EL0`` acting as stack
992 pointer.
993
994Normal entry
995============
996Each time OP-TEE is entered from normal world the temp stack is used as the
997initial stack. For fast calls, this is the only stack used. For normal calls an
998empty thread slot is selected and the CPU switches to that stack.
999
1000Normal exit
1001===========
1002Normal exit occurs when a thread has finished its task and the thread is freed.
1003When the main thread function, ``tee_entry_std(...)``, returns interrupts are
1004disabled and the CPU switches to the temp stack instead. The thread is freed and
1005OP-TEE exits to normal world.
1006
1007RPC exit
1008========
1009RPC exit occurs when OP-TEE need some service from normal world. RPC can
1010currently only be performed with a thread is in running state. RPC is initiated
1011with a call to ``thread_rpc(...)`` which saves the state in a way that when the
1012thread is restored it will continue at the next instruction as if this function
1013did a normal return. CPU switches to use the temp stack before returning to
1014normal world.
1015
1016Foreign interrupt exit
1017======================
1018Foreign interrupt exit occurs when OP-TEE receives a foreign interrupt. For Arm
1019GICv2 mode, foreign interrupt is sent as IRQ which is always handled in normal
1020world. Foreign interrupt exit is similar to RPC exit but it is
1021``thread_irq_handler(...)`` and ``elx_irq(...)`` (respectively for
1022Armv7-A/Aarch32 and for Aarch64) that saves the thread state instead. The thread
1023is resumed in the same way though. For Arm GICv3 mode, foreign interrupt is sent
1024as FIQ which could be handled by either secure world (EL3 in AArch64) or normal
1025world. This mode is not supported yet.
1026
1027Notes for Armv7-A/AArch32
1028 SP_IRQ is initialized to temp stack instead of a separate stack. Prior to
1029 exiting to normal world CPU state is changed to SVC and temp stack is
1030 selected.
1031
1032Notes for AArch64
1033 ``SP_EL0`` is assigned temp stack and is selected during IRQ processing. The
1034 original ``SP_EL0`` is saved in the thread context to be restored when
1035 resuming.
1036
1037Resume entry
1038============
1039OP-TEE is entered using the temp stack in the same way as for normal entry. The
1040thread to resume is looked up and the state is restored to resume execution. The
1041procedure to resume from an RPC exit or an foreign interrupt exit is exactly the
1042same.
1043
1044Syscall
1045=======
1046Syscall's are executed using the thread stack.
1047
1048Notes for Armv7-A/AArch32
1049 Nothing special ``SP_SVC`` is already set with thread stack.
1050
1051Notes for syscall AArch64
1052 Early in the exception processing the original ``SP_EL0`` is saved in
1053 ``struct thread_svc_regs`` in case the TA is executed in AArch64. Current
1054 thread stack is assigned to ``SP_EL0`` which is then selected. When
1055 returning ``SP_EL0`` is assigned what is in ``struct thread_svc_regs``. This
1056 allows ``tee_svc_sys_return_helper(...)`` having the syscall exception
1057 handler return directly to ``thread_unwind_user_mode(...)``.
1058
1059----
1060
1061.. _shared_memory:
1062
1063Shared Memory
1064*************
1065Shared Memory is a block of memory that is shared between the non-secure and the
1066secure world. It is used to transfer data between both worlds.
1067
Etienne Carriere9c600252019-03-11 11:01:48 +01001068The shared memory is allocated and managed by the non-secure world, i.e. the
1069Linux OP-TEE driver. Secure world only considers the individual shared buffers,
1070not their pool. Each shared memory is referenced with associated attributes:
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001071
Etienne Carriere9c600252019-03-11 11:01:48 +01001072 - Buffer start address and byte size,
1073 - Cache attributes of the shared memory buffer,
1074 - List of chunks if mapped from noncontiguous pages.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001075
Etienne Carriere9c600252019-03-11 11:01:48 +01001076Shared memory buffer references manipulated must fit inside one of the
1077shared memory areas known from the OP-TEE core. OP-TEE supports two kinds
Jerome Forissier7fa91cf2020-07-30 16:03:59 +02001078of shared memory areas: an area for contiguous buffers and an area for
1079noncontiguous buffers. At least one has to be enabled.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001080
Etienne Carriere9c600252019-03-11 11:01:48 +01001081Contiguous shared buffers
1082=========================
Etienne Carriere9c600252019-03-11 11:01:48 +01001083Configuration directives ``CFG_SHMEM_START`` and ``CFG_SHMEM_SIZE``
1084define a share memory area where shared memory buffers are contiguous.
1085Generic memory layout registers it as the ``MEM_AREA_NSEC_SHM`` memory area.
1086
1087The non-secure world issues ``OPTEE_SMC_GET_SHM_CONFIG`` to retrieve contiguous
1088shared memory area configuration:
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001089
1090 - Physical address of the start of the pool
1091 - Size of the pool
1092 - Whether or not the memory is cached
1093
Jens Wiklandera70d2f42019-04-25 12:40:49 +02001094Contiguous shared memory (also known as static or reserved shared memory)
1095is enabled with the configuration flag ``CFG_CORE_RESERVED_SHM=y``.
1096
Etienne Carriere9c600252019-03-11 11:01:48 +01001097Noncontiguous shared buffers
1098============================
Etienne Carriere9c600252019-03-11 11:01:48 +01001099To benefit from noncontiguous shared memory buffers, secure world register
1100dynamic shared memory areas and non-secure world must register noncontiguous
1101buffers prior to referring to them using the OP-TEE API.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001102
Etienne Carriere9c600252019-03-11 11:01:48 +01001103The OP-TEE core generic boot sequence discovers dynamic shared areas from the
1104device tree and/or areas explicitly registered by the platform.
1105
1106Non-secure side needs to register buffers as 4kByte chunks lists into OP-TEE
1107core using the ``OPTEE_MSG_CMD_REGISTER_SHM`` API prior referencing to them
1108using the OP-TEE invocation API.
1109
Jens Wiklandera70d2f42019-04-25 12:40:49 +02001110Noncontiguous shared memory (also known as dynamic shared memory) is
1111enabled with the configuration flag ``CFG_CORE_DYN_SHM=y``.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001112
Jerome Forissier7fa91cf2020-07-30 16:03:59 +02001113For performance reasons, the TEE Client Library (``libteec``) uses
1114noncontiguous shared memory when available since it avoids copies in some
1115situations.
1116
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001117Shared Memory Chunk Allocation
1118==============================
1119It is the Linux kernel driver for OP-TEE that is responsible for allocating
1120chunks of shared memory. OP-TEE linux kernel driver relies on linux kernel
1121generic allocation support (``CONFIG_GENERIC_ALLOCATION``) to allocation/release
1122of shared memory physical chunks. OP-TEE linux kernel driver relies on linux
1123kernel dma-buf support (``CONFIG_DMA_SHARED_BUFFER``) to track shared memory
1124buffers references.
1125
1126Using shared memory
1127===================
1128From the Client Application
1129 The client application can ask for shared memory allocation using the
1130 GlobalPlatform Client API function ``TEEC_AllocateSharedMemory(...)``. The
Etienne Carriere9c600252019-03-11 11:01:48 +01001131 client application can also register a memory through the GlobalPlatform
1132 Client API function ``TEEC_RegisterSharedMemory(...)``. The shared memory
1133 reference can then be used as parameter when invoking a trusted application.
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001134
1135From the Linux Driver
1136 Occasionally the Linux kernel driver needs to allocate shared memory for the
1137 communication with secure world, for example when using buffers of type
1138 ``TEEC_TempMemoryReference``.
1139
1140From OP-TEE core
1141 In case OP-TEE core needs information from TEE supplicant (dynamic TA
1142 loading, REE time request,...), shared memory must be allocated. Allocation
1143 depends on the use case. OP-TEE core asks for the following shared memory
1144 allocation:
1145
1146 - ``optee_msg_arg`` structure, used to pass the arguments to the
1147 non-secure world, where the allocation will be done by sending a
1148 ``OPTEE_SMC_RPC_FUNC_ALLOC`` message.
1149
1150 - In some cases, a payload might be needed for storing the result from
1151 TEE supplicant, for example when loading a Trusted Application. This
1152 type of allocation will be done by sending the message
1153 ``OPTEE_MSG_RPC_CMD_SHM_ALLOC(OPTEE_MSG_RPC_SHM_TYPE_APPL,...)``,
1154 which then will return:
1155
1156 - the physical address of the shared memory
1157 - a handle to the memory, that later on will be used later on when
1158 freeing this memory.
1159
1160From TEE Supplicant
1161 TEE supplicant is also working with shared memory, used to exchange data
1162 between normal and secure worlds. TEE supplicant receives a memory address
1163 from the OP-TEE core, used to store the data. This is for example the case
1164 when a Trusted Application is loaded. In this case, TEE supplicant must
1165 register the provided shared memory in the same way a client application
1166 would do, involving the Linux driver.
1167
1168----
1169
1170.. _smc:
1171
1172SMC
1173***
1174SMC Interface
1175=============
1176OP-TEE's SMC interface is defined in two levels using optee_smc.h_ and
1177optee_msg.h_. The former file defines SMC identifiers and what is passed in the
1178registers for each SMC. The latter file defines the OP-TEE Message protocol
1179which is not restricted to only SMC even if that currently is the only option
1180available.
1181
1182SMC communication
1183=================
1184The main structure used for the SMC communication is defined in ``struct
1185optee_msg_arg`` (in optee_msg.h_). If we are looking into the source code, we
1186could see that communication mainly is achieved using ``optee_msg_arg`` and
1187``thread_smc_args`` (in thread.h_), where ``optee_msg_arg`` could be seen as the
1188main structure. What will happen is that the :ref:`linux_kernel` driver will get
1189the parameters either from :ref:`optee_client` or directly from an internal
1190service in Linux kernel. The TEE driver will populate the struct
1191``optee_msg_arg`` with the parameters plus some additional bookkeeping
1192information. Parameters for the SMC are passed in registers 1 to 7, register 0
1193holds the SMC id which among other things tells whether it is a standard or a
1194fast call.
1195
1196----
1197
1198.. _thread_handling:
1199
1200Thread handling
1201***************
1202OP-TEE core uses a couple of threads to be able to support running jobs in
1203parallel (not fully enabled!). There are handlers for different purposes. In
1204thread.c_ you will find a function called ``thread_init_primary(...)`` which
1205assigns ``init_handlers`` (functions) that should be called when OP-TEE core
1206receives standard or fast calls, FIQ and PSCI calls. There are default handlers
1207for these services, but the platform can decide if they want to implement their
1208own platform specific handlers instead.
1209
1210Synchronization primitives
1211==========================
1212OP-TEE has three primitives for synchronization of threads and CPUs:
1213*spin-lock*, *mutex*, and *condvar*.
1214
1215Spin-lock
1216 A spin-lock is represented as an ``unsigned int``. This is the most
1217 primitive lock. Interrupts should be disabled before attempting to take a
1218 spin-lock and should remain disabled until the lock is released. A spin-lock
1219 is initialized with ``SPINLOCK_UNLOCK``.
1220
1221 .. list-table:: Spin lock functions
1222 :header-rows: 1
1223 :widths: 1 5
1224
1225 * - Function
1226 - Purpose
1227
1228 * - ``cpu_spin_lock(...)``
1229 - Locks a spin-lock
1230
1231 * - ``cpu_spin_trylock(...)``
1232 - Locks a spin-lock if unlocked and returns ``0`` else the spin-lock
1233 is unchanged and the function returns ``!0``
1234
1235 * - ``cpu_spin_unlock(...)``
1236 - Unlocks a spin-lock
1237
1238Mutex
1239 A mutex is represented by ``struct mutex``. A mutex can be locked and
1240 unlocked with interrupts enabled or disabled, but only from a normal thread.
1241 A mutex cannot be used in an interrupt handler, abort handler or before a
1242 thread has been selected for the CPU. A mutex is initialized with either
1243 ``MUTEX_INITIALIZER`` or ``mutex_init(...)``.
1244
1245 .. list-table:: Mutex functions
1246 :header-rows: 1
1247 :widths: 1 5
1248
1249 * - Function
1250 - Purpose
1251
1252 * - ``mutex_lock(...)``
1253 - Locks a mutex. If the mutex is unlocked this is a fast operation,
1254 else the function issues an RPC to wait in normal world.
1255
1256 * - ``mutex_unlock(...)``
1257 - Unlocks a mutex. If there is no waiters this is a fast operation,
1258 else the function issues an RPC to wake up a waiter in normal world.
1259
1260 * - ``mutex_trylock(...)``
1261 - Locks a mutex if unlocked and returns ``true`` else the mutex is
1262 unchanged and the function returns ``false``.
1263
1264 * - ``mutex_destroy(...)``
1265 - Asserts that the mutex is unlocked and there is no waiters, after
1266 this the memory used by the mutex can be freed.
1267
1268 When a mutex is locked it is owned by the thread calling ``mutex_lock(...)``
1269 or ``mutex_trylock(...)``, the mutex may only be unlocked by the thread
1270 owning the mutex. A thread should not exit to TA user space when holding a
1271 mutex.
1272
1273Condvar
1274 A condvar is represented by ``struct condvar``. A condvar is similar to a
1275 ``pthread_condvar_t`` in the pthreads standard, only less advanced.
1276 Condition variables are used to wait for some condition to be fulfilled and
1277 are always used together a mutex. Once a condition variable has been used
1278 together with a certain mutex, it must only be used with that mutex until
1279 destroyed. A condvar is initialized with ``CONDVAR_INITIALIZER`` or
1280 ``condvar_init(...)``.
1281
1282 .. list-table:: Condvar functions
1283 :header-rows: 1
1284 :widths: 1 5
1285
1286 * - Function
1287 - Purpose
1288
1289 * - ``condvar_wait(...)``
1290 - Atomically unlocks the supplied mutex and waits in normal world via
1291 an RPC for the condition variable to be signaled, when the function
1292 returns the mutex is locked again.
1293
1294 * - ``condvar_signal(...)``
1295 - Wakes up one waiter of the condition variable (waiting in
1296 ``condvar_wait(...)``).
1297
1298 * - ``condvar_broadcast(...)``
1299 - Wake up all waiters of the condition variable.
1300
1301 The caller of ``condvar_signal(...)`` or ``condvar_broadcast(...)`` should
1302 hold the mutex associated with the condition variable to guarantee that a
1303 waiter does not miss the signal.
1304
1305.. _core/arch/arm/kernel/thread.c: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/kernel/thread.c
1306.. _optee_msg.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/optee_msg.h
1307.. _optee_smc.h: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/sm/optee_smc.h
1308.. _thread.c: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/kernel/thread.c
1309.. _thread.h: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/kernel/thread.h
1310
1311.. _ARM_DEN0028A_SMC_Calling_Convention: http://infocenter.arm.com/help/topic/com.arm.doc.den0028b/ARM_DEN0028B_SMC_Calling_Convention.pdf
1312.. _Cortex-A53 TRM: http://infocenter.arm.com/help/topic/com.arm.doc.ddi0500j/DDI0500J_cortex_a53_trm.pdf
1313.. _drivers/tee/optee: https://github.com/torvalds/linux/tree/master/drivers/tee/optee
1314.. _Trusted Firmware A: https://github.com/ARM-software/arm-trusted-firmware