blob: f611d7002db4d9bf84e33d2af5686f6863fed0a3 [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
12context based on :ref:`SMC` exceptions and interrupt notifications Interrupt
13notifications 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=================================
19This section lists all the cases where optee_os is involved in world context
20switches. Optee_os executes in the secure world. World switch is done by the
21cores secure monitor level/mode, referred below as the Monitor.
22
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
25service targets the trusted OS, the Monitor will switch to optee_os world
26execution. When the secure world returns to the normal world, optee_os executes
27a SMC that is caught by the Monitor which switches back to the normal world.
28
29When a secure interrupt is signaled by the Arm GIC, it shall reach the optee_os
30interrupt exception vector. If the secure world is executing, optee_os will
31handle straight the interrupt from its exception vector. If the normal world is
32executing when the secure interrupt raises, the Monitor vector must handle the
33exception and invoke the optee_os to serve the interrupt.
34
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
38world is executing when the non-secure interrupt raises, optee_os will
39temporarily 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
52The Monitor can be external to optee_os (case ``CFG_WITH_ARM_TRUSTED_FW=y``).
53If not, provides a local secure monitor ``core/arch/arm/sm``. Armv7-A platforms
54should use the optee_os secure monitor. Armv8-A platforms are likely to rely on
55an `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
72 - secure interrupts to the Monitor that will forward to optee_os
73 - non-secure interrupts to the executing world exception vector.
74
75When the secure world is executing, the system is configured to route:
76
77 - secure and non-secure interrupts to the executing optee_os exception
78 vector. optee_os shall forward the non-secure interrupts to the normal
79 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=============================
88Two types of interrupt are defined in optee_os:
89
90 - **Native interrupt** - The interrupt handled by optee_os (for example:
91 secure interrupt)
92 - **Foreign interrupt** - The interrupt not handled by optee_os (for
93 example: non-secure interrupt which is handled by normal world)
94
95For Arm **GICv2** mode, native interrupt is sent as FIQ and foreign interrupt
96is sent as IRQ. For Arm **GICv3** mode, foreign interrupt is sent as FIQ which
97could be handled by either secure world (aarch32 Monitor mode or aarch64 EL3)
98or normal world. Arm GICv3 mode can be enabled by setting ``CFG_ARM_GICV3=y``.
99For clarity, this document mainly chooses the GICv2 convention and refers the
100IRQ as optee_os foreign interrupts, and FIQ as optee_os native interrupts.
101Native interrupts must be securely routed to optee_os. Foreign interrupts, when
102trapped during secure world execution might need to be efficiently routed to
103the normal world.
104
105Normal World invokes optee_os using SMC
106=======================================
107
108**Entering the Secure Monitor**
109
110The monitor manages all entries and exits of secure world. To enter secure
111world from normal world the monitor saves the state of normal world (general
112purpose registers and system registers which are not banked) and restores the
113previous state of secure world. Then a return from exception is performed and
114the restored secure state is resumed. Exit from secure world to normal world is
115the reverse.
116
117Some general purpose registers are not saved and restored on entry and exit,
118those are used to pass parameters between secure and normal world (see
119ARM_DEN0028A_SMC_Calling_Convention_ for details).
120
121**Entry and exit of Trusted OS**
122
123On entry and exit of Trusted OS each CPU is uses a separate entry stack and runs
124with IRQ and FIQ blocked. SMCs are categorised in two flavors: **fast** and
125**standard**.
126
127 - For **fast** SMCs, optee_os will execute on the entry stack with IRQ/FIQ
128 blocked until the execution returns to normal world.
129
130 - For **standard** SMCs, optee_os will at some point execute the requested
131 service with interrupts unblocked. In order to handle interrupts, mainly
132 forwarding of foreign interrupts, optee_os assigns a trusted thread
133 (`core/arch/arm/kernel/thread.c`_) to the SMC request. The trusted thread
134 stores the execution context of the requested service. This context can be
135 suspended and resumed as the requested service executes and is
136 interrupted. The trusted thread is released only once the service
137 execution returns with a completion status.
138
139 For **standard** SMCs, optee_os allocates or resumes a trusted thread then
140 unblock the IRQ/FIQ lines. When the optee_os needs to invoke the normal
141 world from a foreign interrupt or a remote service call, optee_os blocks
142 IRQ/FIQ and suspends the trusted thread. When suspending, optee_os gets
143 back to the entry stack.
144
145 - **Both** fast and standard SMC end on the entry stack with IRQ/FIQ blocked
146 and optee_os invokes the Monitor through a SMC to return to the normal
147 world.
148
149.. figure:: ../images/core/interrupt_handling/tee_invoke.png
150 :figclass: align-center
151
152 SMC entry to secure world
153
154Deliver non-secure interrupts to Normal World
155=============================================
156This section uses the Arm GICv1/v2 conventions: IRQ signals non-secure
157interrupts while FIQ signals secure interrupts. On a GICv3 configuration, one
158should exchange IRQ and FIQ in this section.
159
160**Forward a Foreign Interrupt from Secure World to Normal World**
161
162When an IRQ is received in secure world as an IRQ exception then secure world:
163
164 1. Saves trusted thread context (entire state of all processor modes for
165 Armv7-A)
166
167 2. Blocks (masks) all interrupts (IRQ and FIQ)
168
169 3. Switches to entry stack
170
171 4. Issues an SMC with a value to indicates to normal world that an IRQ has
172 been delivered and last SMC call should be continued
173
174The monitor restores normal world context with a return code indicating that an
175IRQ is about to be delivered. Normal world issues a new SMC indicating that it
176should continue last SMC.
177
178The monitor restores secure world context which locates the previously saved
179context and checks that it is a return from IRQ that is requested before
180restoring the context and lets the secure world IRQ handler return from
181exception where the execution would be resumed.
182
183Note that the monitor itself does not know/care that it has just forwarded an
184IRQ to normal world. The bookkeeping is done in the trusted thread handling in
185Trusted OS. Normal world is responsible to decide when the secure world thread
186should resume execution (for details, see :ref:`thread_handling`).
187
188.. figure:: ../images/core/interrupt_handling/irq.png
189 :figclass: align-center
190
191 IRQ received in secure world and forwarded to normal world
192
193**Deliver a non-secure interrupt to normal world when ``SCR_NS`` is set**
194
195Since ``SCR_IRQ`` is cleared, an IRQ will be delivered using the state vector
196(``VBAR``) in the normal world. The IRQ is received as any other exception by
197normal world, the monitor and the Trusted OS are not involved at all.
198
199Deliver secure interrupts to Secure World
200=========================================
201This section uses the Arm GICv1/v2 conventions: FIQ signals secure interrupts
202while IRQ signals non-secure interrupts. On a GICv3 configuration, one should
203exchange IRQ and FIQ in this section. A FIQ can be received during two different
204states, either in normal world (``SCR_NS`` is set) or in secure world
205(``SCR_NS`` is cleared). When the secure monitor is active (Armv8-A EL3 or
206Armv7-A Monitor mode) FIQ is masked. FIQ reception in the two different states
207is described below.
208
209**Deliver FIQ to secure world when SCR_NS is set**
210
211When the monitor gets an FIQ exception it:
212
213 1. Saves normal world context and restores secure world context from last
214 secure world exit (which will have IRQ and FIQ blocked)
215 2. Clears ``SCR_FIQ`` when clearing ``SCR_NS``
216 3. Sets FIQ as parameter to secure world entry
217 4. Does a return from exception into secure context
218 5. Secure world unmasks FIQs because of the FIQ parameter
219 6. FIQ is received as in exception using the state vector
220 7. The state vector handle returns from exception in secure world
221 8. Secure world issues an SMC to return to normal world
222 9. Monitor saves secure world context and restores normal world context
223 10. Does a return from exception into restored context
224
225.. figure:: ../images/core/interrupt_handling/fiq.png
226 :figclass: align-center
227
228 FIQ received when SCR_NS is set
229
230.. figure:: ../images/core/interrupt_handling/irq_fiq.png
231 :figclass: align-center
232
233 FIQ received while processing an IRQ forwarded from secure world
234
235**Deliver FIQ to secure world when SCR_NS is cleared**
236
237Since ``SCR_FIQ`` is cleared when ``SCR_NS`` is cleared a FIQ will be delivered
238using the state vector (``VBAR``) in secure world. The FIQ is received as any
239other exception by Trusted OS, the monitor is not involved at all.
240
241Trusted thread scheduling
242=========================
243**Trusted thread for standard services**
244
245OP-TEE standard services are carried through standard SMC. Execution of these
246services can be interrupted by foreign interrupts. To suspend and restore the
247service execution, optee_os assigns a trusted thread at standard SMCs entry.
248
249The trusted thread terminates when optee_os returns to the normal world with a
250service completion status.
251
252A trusted thread execution can be interrupted by a native interrupt. In this
253case the native interrupt is handled by the interrupt exception handlers and
254once served, optee_os returns to the execution trusted thread.
255
256A trusted thread execution can be interrupted by a foreign interrupt. In this
257case, optee_os suspends the trusted thread and invokes the normal world through
258the Monitor (optee_os so-called RPC services). The trusted threads will resume
259only once normal world invokes the optee_os with the RPC service status.
260
261A trusted thread execution can lead optee_os to invoke a service in normal
262world: access a file, get the REE current time, etc. The trusted thread is
263suspended/resumed during remote service execution.
264
265**Scheduling considerations**
266
267When a trusted thread is interrupted by a foreign interrupt and when optee_os
268invokes a normal world service, the normal world gets the opportunity to
269reschedule the running applications. The trusted thread will resume only once
270the client application is scheduled back. Thus, a trusted thread execution
271follows the scheduling of the normal world caller context.
272
273Optee_os does not implement any thread scheduling. Each trusted thread is
274expected to track a service that is invoked from the normal world and should
275return to it with an execution status.
276
277The OP-TEE Linux driver (as implemented in `drivers/tee/optee`_ since Linux
278kernel 4.12) is designed so that the Linux thread invoking OP-TEE gets assigned
279a trusted thread on TEE side. The execution of the trusted thread is tied to the
280execution of the caller Linux thread which is under the Linux kernel scheduling
281decision. This means trusted threads are scheduled by the Linux kernel.
282
283**Trusted thread constraints**
284
285TEE core handles a static number of trusted threads, see ``CFG_NUM_THREADS``.
286
287Trusted threads are only expensive on memory constrained system, mainly
288regarding the execution stack size.
289
290On SMP systems, optee_os can execute several trusted threads in parallel if the
291normal world supports scheduling of processes. Even on UP systems, supporting
292several trusted threads in optee_os helps normal world scheduler to be
293efficient.
294
295----
296
297.. _memory_objects:
298
299Memory objects
300**************
301A memory object, **MOBJ**, describes a piece of memory. The interface provided
302is mostly abstract when it comes to using the MOBJ to populate translation
303tables etc. There are different kinds of MOBJs describing:
304
305 - Physically contiguous memory
306 - created with ``mobj_phys_alloc(...)``.
307
308 - Virtual memory
309 - one instance with the name ``mobj_virt`` available.
310 - spans the entire virtual address space.
311
312 - Physically contiguous memory allocated from a ``tee_mm_pool_t *``
313 - created with ``mobj_mm_alloc(...)``.
314
315 - Paged memory
316 - created with ``mobj_paged_alloc(...)``.
317 - only contains the supplied size and makes ``mobj_is_paged(...)``
318 return true if supplied as argument.
319
320 - Secure copy paged shared memory
321 - created with ``mobj_seccpy_shm_alloc(...)``.
322 - makes ``mobj_is_paged(...)`` and ``mobj_is_secure(...)`` return true
323 if supplied as argument.
324
325----
326
327.. _mmu:
328
329MMU
330***
331Translation tables
332==================
333OP-TEE uses several L1 translation tables, one large spanning 4 GiB and two or
334more small tables spanning 32 MiB. The large translation table handles kernel
335mode mapping and matches all addresses not covered by the small translation
336tables. The small translation tables are assigned per thread and covers the
337mapping of the virtual memory space for one TA context.
338
339Memory space between small and large translation table is configured by TTBRC.
340TTBR1 always points to the large translation table. TTBR0 points to the a small
341translation table when user mapping is active and to the large translation table
342when no user mapping is currently active. For details about registers etc,
343please refer to a Technical Reference Manual for your architecture, for example
344`Cortex-A53 TRM`_.
345
346The translation tables has certain alignment constraints, the alignment (of the
347physical address) has to be the same as the size of the translation table. The
348translation tables are statically allocated to avoid fragmentation of memory due
349to the alignment constraints.
350
351Each thread has one small L1 translation table of its own. Each TA context has a
352compact representation of its L1 translation table. The compact representation
353is used to initialize the thread specific L1 translation table when the TA
354context is activated.
355
356.. graphviz::
357
358 digraph xlat_table {
359 graph [
360 rankdir = "LR"
361 ];
362 node [
363 fontsize = "16"
364 shape = "ellipse"
365 ];
366 edge [
367 ];
368 "node_ttb" [
369 label = "<f0> TTBR0 | <f1> TTBR1"
370 shape = "record"
371 ];
372 "node_large_l1" [
373 label = "<f0> Large L1\nSpans 4 GiB"
374 shape = "record"
375 ];
376 "node_small_l1" [
377 label = "Small L1\nSpans 32 MiB\nper entry | <f0> 0 | <f1> 1 | ... | <fn> n"
378 shape = "record"
379 ];
380
381 "node_ttb":f0 -> "node_small_l1":f0 [ label = "Thread 0 ctx active" ];
382 "node_ttb":f0 -> "node_small_l1":f1 [ label = "Thread 1 ctx active" ];
383 "node_ttb":f0 -> "node_small_l1":fn [ label = "Thread n ctx active" ];
384 "node_ttb":f0 -> "node_large_l1" [ label="No active ctx" ];
385 "node_ttb":f1 -> "node_large_l1";
386 }
387
388
389Switching to user mode
390======================
391This section only applies with following configuration flags:
392
393 - ``CFG_WITH_LPAE=n``
394 - ``CFG_CORE_UNMAP_CORE_AT_EL0=y``
395
396When switching to user mode only a minimal kernel mode mapping is kept. This is
397achieved by selecting a zeroed out big L1 translation in TTBR1 when
398transitioning to user mode. When returning back to kernel mode the original L1
399translation table is restored in TTBR1.
400
401Switching to normal world
402=========================
403When switching to normal world either via a foreign interrupt (see
404:ref:`native_foreign_irqs` or RPC there is a chance that secure world will
405resume execution on a different CPU. This means that the new CPU need to be
406configured with the context of the currently active TA. This is solved by always
Jens Wiklanderddde3a82019-02-25 12:46:18 +0100407setting the TA context in the CPU when resuming execution.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200408
409----
410
411.. _pager:
412
413Pager
414*****
415OP-TEE currently requires >256 KiB RAM for OP-TEE kernel memory. This is not a
416problem if OP-TEE uses TrustZone protected DDR, but for security reasons OP-TEE
417may need to use TrustZone protected SRAM instead. The amount of available SRAM
418varies between platforms, from just a few KiB up to over 512 KiB. Platforms with
419just a few KiB of SRAM cannot be expected to be able to run a complete TEE
420solution in SRAM. But those with 128 to 256 KiB of SRAM can be expected to have
421a capable TEE solution in SRAM. The pager provides a solution to this by demand
422paging parts of OP-TEE using virtual memory.
423
424Secure memory
425=============
426TrustZone protected SRAM is generally considered more secure than TrustZone
427protected DRAM as there is usually more attack vectors on DRAM. The attack
428vectors are hardware dependent and can be different for different platforms.
429
430Backing store
431=============
432TrustZone protected DRAM or in some cases non-secure DRAM is used as backing
433store. The data in the backing store is integrity protected with one hash
434(SHA-256) per page (4KiB). Readonly pages are not encrypted since the OP-TEE
435binary itself is not encrypted.
436
437Partitioning of memory
438======================
439The code that handles demand paging must always be available as it would
440otherwise lead to deadlock. The virtual memory is partitioned as:
441
442 +--------------+-------------------+
443 | Type | Sections |
444 +==============+===================+
445 | unpaged | | text |
446 | | | rodata |
447 | | | data |
448 | | | bss |
449 | | | heap1 |
450 | | | nozi |
451 | | | heap2 |
452 +--------------+-------------------+
453 | init / paged | | text_init |
454 | | | rodata_init |
455 +--------------+-------------------+
456 | paged | | text_pageable |
457 | | | rodata_pageable |
458 +--------------+-------------------+
459 | demand alloc | |
460 +--------------+-------------------+
461
462Where ``nozi`` stands for "not zero initialized", this section contains entry
463stacks (thread stack when TEE pager is not enabled) and translation tables (TEE
464pager cached translation table when the pager is enabled and LPAE MMU is used).
465
466The ``init`` area is available when OP-TEE is initializing and contains
467everything that is needed to initialize the pager. After the pager has been
468initialized this area will be used for demand paged instead.
469
470The ``demand alloc`` area is a special area where the pages are allocated and
471removed from the pager on demand. Those pages are returned when OP-TEE does not
472need them any longer. The thread stacks currently belongs this area. This means
473that when a stack is not used the physical pages can be used by the pager for
474better performance.
475
476The technique to gather code in the different area is based on compiling all
477functions and data into separate sections. The unpaged text and rodata is then
478gathered by linking all object files with ``--gc-sections`` to eliminate
479sections that are outside the dependency graph of the entry functions for
480unpaged functions. A script analyzes this ELF file and generates the bits of the
481final link script. The process is repeated for init text and rodata. What is
482not "unpaged" or "init" becomes "paged".
483
484Partitioning of the binary
485==========================
486.. note::
487 The struct definitions provided in this section are explicitly covered by
488 the following dual license:
489
490 .. code-block:: none
491
492 SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
493
494The binary is partitioned into four parts as:
495
496
497 +----------+
498 | Binary |
499 +==========+
500 | Header |
501 +----------+
502 | Init |
503 +----------+
504 | Hashes |
505 +----------+
506 | Pageable |
507 +----------+
508
509The header is defined as:
510
511.. code-block:: c
512
513 #define OPTEE_MAGIC 0x4554504f
514 #define OPTEE_VERSION 1
515 #define OPTEE_ARCH_ARM32 0
516 #define OPTEE_ARCH_ARM64 1
517
518 struct optee_header {
519 uint32_t magic;
520 uint8_t version;
521 uint8_t arch;
522 uint16_t flags;
523 uint32_t init_size;
524 uint32_t init_load_addr_hi;
525 uint32_t init_load_addr_lo;
526 uint32_t init_mem_usage;
527 uint32_t paged_size;
528 };
529
530The header is only used by the loader of OP-TEE, not OP-TEE itself. To
531initialize OP-TEE the loader loads the complete binary into memory and copies
532what follows the header and the following ``init_size`` bytes to
533``(init_load_addr_hi << 32 | init_load_addr_lo)``. ``init_mem_usage`` is used by
534the loader to be able to check that there is enough physical memory available
535for OP-TEE to be able to initialize at all. The loader supplies in ``r0/x0`` the
536address of the first byte following what was not copied and jumps to the load
537address to start OP-TEE.
538
539In addition to overall binary with partitions inside described as above, three
540extra binaries are generated simultaneously during build process for loaders who
541support loading separate binaries:
542
543 +-----------+
544 | v2 binary |
545 +===========+
546 | Header |
547 +-----------+
548
549 +-----------+
550 | v2 binary |
551 +===========+
552 | Init |
553 +-----------+
554 | Hashes |
555 +-----------+
556
557 +-----------+
558 | v2 binary |
559 +===========+
560 | Pageable |
561 +-----------+
562
563In this case, loaders load header binary first to get image list and information
564of each image; and then load each of them into specific load address assigned in
565structure. These binaries are named with `v2` suffix to distinguish from the
566existing binaries. Header format is updated to help loaders loading binaries
567efficiently:
568
569.. code-block:: c
570
571 #define OPTEE_IMAGE_ID_PAGER 0
572 #define OPTEE_IMAGE_ID_PAGED 1
573
574 struct optee_image {
575 uint32_t load_addr_hi;
576 uint32_t load_addr_lo;
577 uint32_t image_id;
578 uint32_t size;
579 };
580
581 struct optee_header_v2 {
582 uint32_t magic;
583 uint8_t version;
584 uint8_t arch;
585 uint16_t flags;
586 uint32_t nb_images;
587 struct optee_image optee_image[];
588 };
589
590Magic number and architecture are identical as original. Version is increased to
591two. ``load_addr_hi`` and ``load_addr_lo`` may be ``0xFFFFFFFF`` for pageable
592binary since pageable part may get loaded by loader into dynamic available
593position. ``image_id`` indicates how loader handles current binary. Loaders who
594don't support separate loading just ignore all v2 binaries.
595
596Initializing the pager
597======================
598The pager is initialized as early as possible during boot in order to minimize
599the "init" area. The global variable ``tee_mm_vcore`` describes the virtual
600memory range that is covered by the level 2 translation table supplied to
601``tee_pager_init(...)``.
602
603Assign pageable areas
604---------------------
605A virtual memory range to be handled by the pager is registered with a call to
606``tee_pager_add_core_area()``.
607
608.. code-block:: c
609
610 bool tee_pager_add_area(tee_mm_entry_t *mm,
611 uint32_t flags,
612 const void *store,
613 const void *hashes);
614
615which takes a pointer to ``tee_mm_entry_t`` to tell the range, flags to tell how
616memory should be mapped (readonly, execute etc), and pointers to backing store
617and hashes of the pages.
618
619Assign physical pages
620---------------------
621Physical SRAM pages are supplied by calling ``tee_pager_add_pages(...)``
622
623.. code-block:: c
624
625 void tee_pager_add_pages(tee_vaddr_t vaddr,
626 size_t npages,
627 bool unmap);
628
629``tee_pager_add_pages(...)`` takes the physical address stored in the entry
630mapping the virtual address ``vaddr`` and ``npages`` entries after that and uses
631it to map new pages when needed. The unmap parameter tells whether the pages
632should be unmapped immediately since they does not contain initialized data or
633be kept mapped until they need to be recycled. The pages in the "init" area are
634supplied with ``unmap == false`` since those page have valid content and are in
635use.
636
637Invocation
638==========
639The pager is invoked as part of the abort handler. A pool of physical pages are
640used to map different virtual addresses. When a new virtual address needs to be
641mapped a free physical page is mapped at the new address, if a free physical
642page cannot be found the oldest physical page is selected instead. When the page
643is mapped new data is copied from backing store and the hash of the page is
644verified. If it is OK the pager returns from the exception to resume the
645execution.
646
647Paging of user TA
648=================
649Paging of user TAs can optionally be enabled with ``CFG_PAGED_USER_TA=y``.
650Paging of user TAs is analogous to paging of OP-TEE kernel parts but with a few
651differences:
652
653 - Read/write pages are paged in addition to read-only pages
654 - Page tables are managed dynamically
655
656``tee_pager_add_uta_area(...)`` is used to setup initial read/write mapping
657needed when populating the TA. When the TA is fully populated and relocated
658``tee_pager_set_uta_area_attr(...)`` changes the mapping of the area to strict
659permissions used when the TA is running.
660
661----
662
663.. _stacks:
664
665Stacks
666******
667Different stacks are used during different stages. The stacks are:
668
669 - **Secure monitor stack** (128 bytes), bound to the CPU. Only available if
670 OP-TEE is compiled with a secure monitor always the case if the target is
671 Armv7-A but never for Armv8-A.
672
673 - **Temp stack** (small ~1KB), bound to the CPU. Used when transitioning
674 from one state to another. Interrupts are always disabled when using this
675 stack, aborts are fatal when using the temp stack.
676
677 - **Abort stack** (medium ~2KB), bound to the CPU. Used when trapping a data
678 or pre-fetch abort. Aborts from user space are never fatal the TA is only
679 killed. Aborts from kernel mode are used by the pager to do the demand
680 paging, if pager is disabled all kernel mode aborts are fatal.
681
682 - **Thread stack** (large ~8KB), not bound to the CPU instead used by the
683 current thread/task. Interrupts are usually enabled when using this stack.
684
685Notes for Armv7-A/AArch32
686 .. list-table::
687 :header-rows: 1
688 :widths: 1 5
689
690 * - Stack
691 - Comment
692
693 * - Temp
694 - Assigned to ``SP_SVC`` during entry/exit, always assigned to
695 ``SP_IRQ`` and ``SP_FIQ``
696
697 * - Abort
698 - Always assigned to ``SP_ABT``
699
700 * - Thread
701 - Assigned to ``SP_SVC`` while a thread is active
702
703Notes for AArch64
704 There are only two stack pointers, ``SP_EL1`` and ``SP_EL0``, available for
705 OP-TEE in AArch64. When an exception is received stack pointer is always
706 ``SP_EL1`` which is used temporarily while assigning an appropriate stack
707 pointer for ``SP_EL0``. ``SP_EL1`` is always assigned the value of
708 ``thread_core_local[cpu_id]``. This structure has some spare space for
709 temporary storage of registers and also keeps the relevant stack pointers.
710 In general when we talk about assigning a stack pointer to the CPU below we
711 mean ``SP_EL0``.
712
713Boot
714====
715During early boot the CPU is configured with the temp stack which is used until
716OP-TEE exits to normal world the first time.
717
718Notes for AArch64
719 ``SPSEL`` is always ``0`` on entry/exit to have ``SP_EL0`` acting as stack
720 pointer.
721
722Normal entry
723============
724Each time OP-TEE is entered from normal world the temp stack is used as the
725initial stack. For fast calls, this is the only stack used. For normal calls an
726empty thread slot is selected and the CPU switches to that stack.
727
728Normal exit
729===========
730Normal exit occurs when a thread has finished its task and the thread is freed.
731When the main thread function, ``tee_entry_std(...)``, returns interrupts are
732disabled and the CPU switches to the temp stack instead. The thread is freed and
733OP-TEE exits to normal world.
734
735RPC exit
736========
737RPC exit occurs when OP-TEE need some service from normal world. RPC can
738currently only be performed with a thread is in running state. RPC is initiated
739with a call to ``thread_rpc(...)`` which saves the state in a way that when the
740thread is restored it will continue at the next instruction as if this function
741did a normal return. CPU switches to use the temp stack before returning to
742normal world.
743
744Foreign interrupt exit
745======================
746Foreign interrupt exit occurs when OP-TEE receives a foreign interrupt. For Arm
747GICv2 mode, foreign interrupt is sent as IRQ which is always handled in normal
748world. Foreign interrupt exit is similar to RPC exit but it is
749``thread_irq_handler(...)`` and ``elx_irq(...)`` (respectively for
750Armv7-A/Aarch32 and for Aarch64) that saves the thread state instead. The thread
751is resumed in the same way though. For Arm GICv3 mode, foreign interrupt is sent
752as FIQ which could be handled by either secure world (EL3 in AArch64) or normal
753world. This mode is not supported yet.
754
755Notes for Armv7-A/AArch32
756 SP_IRQ is initialized to temp stack instead of a separate stack. Prior to
757 exiting to normal world CPU state is changed to SVC and temp stack is
758 selected.
759
760Notes for AArch64
761 ``SP_EL0`` is assigned temp stack and is selected during IRQ processing. The
762 original ``SP_EL0`` is saved in the thread context to be restored when
763 resuming.
764
765Resume entry
766============
767OP-TEE is entered using the temp stack in the same way as for normal entry. The
768thread to resume is looked up and the state is restored to resume execution. The
769procedure to resume from an RPC exit or an foreign interrupt exit is exactly the
770same.
771
772Syscall
773=======
774Syscall's are executed using the thread stack.
775
776Notes for Armv7-A/AArch32
777 Nothing special ``SP_SVC`` is already set with thread stack.
778
779Notes for syscall AArch64
780 Early in the exception processing the original ``SP_EL0`` is saved in
781 ``struct thread_svc_regs`` in case the TA is executed in AArch64. Current
782 thread stack is assigned to ``SP_EL0`` which is then selected. When
783 returning ``SP_EL0`` is assigned what is in ``struct thread_svc_regs``. This
784 allows ``tee_svc_sys_return_helper(...)`` having the syscall exception
785 handler return directly to ``thread_unwind_user_mode(...)``.
786
787----
788
789.. _shared_memory:
790
791Shared Memory
792*************
793Shared Memory is a block of memory that is shared between the non-secure and the
794secure world. It is used to transfer data between both worlds.
795
796Shared Memory Allocation
797========================
798The shared memory is allocated by the Linux driver from a pool ``struct
799shm_pool``, the pool contains:
800
801 - The physical address of the start of the pool
802 - The size of the pool
803 - Whether or not the memory is cached
804 - List of chunk of memory allocated.
805
806.. note::
807 - The shared memory pool is physically contiguous.
808 - The shared memory area is **not secure** as it is used by both non-secure
809 and secure world.
810
811Shared Memory Configuration
812===========================
813It is the Linux kernel driver for OP-TEE that is responsible for initializing
814the shared memory pool, given information provided by the OP-TEE core. The
815Linux driver issues a SMC call ``OPTEE_SMC_GET_SHM_CONFIG`` to retrieve the
816information
817
818 - Physical address of the start of the pool
819 - Size of the pool
820 - Whether or not the memory is cached
821
822The shared memory pool configuration is platform specific. The memory mapping,
823including the area ``MEM_AREA_NSEC_SHM`` (shared memory with non-secure world),
824is retrieved by calling the platform-specific function ``bootcfg_get_memory()``.
825Please refer to this function and the area type ``MEM_AREA_NSEC_SHM`` to see the
826configuration for the platform of interest. The Linux driver will then
827initialize the shared memory pool accordingly.
828
829.. todo::
830
831 Joakim: bootcfg_get_memory(...) is no longer in our code. Text needs update.
832
833Shared Memory Chunk Allocation
834==============================
835It is the Linux kernel driver for OP-TEE that is responsible for allocating
836chunks of shared memory. OP-TEE linux kernel driver relies on linux kernel
837generic allocation support (``CONFIG_GENERIC_ALLOCATION``) to allocation/release
838of shared memory physical chunks. OP-TEE linux kernel driver relies on linux
839kernel dma-buf support (``CONFIG_DMA_SHARED_BUFFER``) to track shared memory
840buffers references.
841
842Using shared memory
843===================
844From the Client Application
845 The client application can ask for shared memory allocation using the
846 GlobalPlatform Client API function ``TEEC_AllocateSharedMemory(...)``. The
847 client application can also provide shared memory through the GlobalPlatform
848 Client API function ``TEEC_RegisterSharedMemory(...)``. In such a case, the
849 provided memory must be physically contiguous, since OP-TEE core, who does
850 not handle scatter-gather memory, is able to use the provided range of
851 memory addresses. Note that the reference count of a shared memory chunk is
852 incremented when shared memory is registered, and initialized to 1 on
853 allocation.
854
855From the Linux Driver
856 Occasionally the Linux kernel driver needs to allocate shared memory for the
857 communication with secure world, for example when using buffers of type
858 ``TEEC_TempMemoryReference``.
859
860From OP-TEE core
861 In case OP-TEE core needs information from TEE supplicant (dynamic TA
862 loading, REE time request,...), shared memory must be allocated. Allocation
863 depends on the use case. OP-TEE core asks for the following shared memory
864 allocation:
865
866 - ``optee_msg_arg`` structure, used to pass the arguments to the
867 non-secure world, where the allocation will be done by sending a
868 ``OPTEE_SMC_RPC_FUNC_ALLOC`` message.
869
870 - In some cases, a payload might be needed for storing the result from
871 TEE supplicant, for example when loading a Trusted Application. This
872 type of allocation will be done by sending the message
873 ``OPTEE_MSG_RPC_CMD_SHM_ALLOC(OPTEE_MSG_RPC_SHM_TYPE_APPL,...)``,
874 which then will return:
875
876 - the physical address of the shared memory
877 - a handle to the memory, that later on will be used later on when
878 freeing this memory.
879
880From TEE Supplicant
881 TEE supplicant is also working with shared memory, used to exchange data
882 between normal and secure worlds. TEE supplicant receives a memory address
883 from the OP-TEE core, used to store the data. This is for example the case
884 when a Trusted Application is loaded. In this case, TEE supplicant must
885 register the provided shared memory in the same way a client application
886 would do, involving the Linux driver.
887
888----
889
890.. _smc:
891
892SMC
893***
894SMC Interface
895=============
896OP-TEE's SMC interface is defined in two levels using optee_smc.h_ and
897optee_msg.h_. The former file defines SMC identifiers and what is passed in the
898registers for each SMC. The latter file defines the OP-TEE Message protocol
899which is not restricted to only SMC even if that currently is the only option
900available.
901
902SMC communication
903=================
904The main structure used for the SMC communication is defined in ``struct
905optee_msg_arg`` (in optee_msg.h_). If we are looking into the source code, we
906could see that communication mainly is achieved using ``optee_msg_arg`` and
907``thread_smc_args`` (in thread.h_), where ``optee_msg_arg`` could be seen as the
908main structure. What will happen is that the :ref:`linux_kernel` driver will get
909the parameters either from :ref:`optee_client` or directly from an internal
910service in Linux kernel. The TEE driver will populate the struct
911``optee_msg_arg`` with the parameters plus some additional bookkeeping
912information. Parameters for the SMC are passed in registers 1 to 7, register 0
913holds the SMC id which among other things tells whether it is a standard or a
914fast call.
915
916----
917
918.. _thread_handling:
919
920Thread handling
921***************
922OP-TEE core uses a couple of threads to be able to support running jobs in
923parallel (not fully enabled!). There are handlers for different purposes. In
924thread.c_ you will find a function called ``thread_init_primary(...)`` which
925assigns ``init_handlers`` (functions) that should be called when OP-TEE core
926receives standard or fast calls, FIQ and PSCI calls. There are default handlers
927for these services, but the platform can decide if they want to implement their
928own platform specific handlers instead.
929
930Synchronization primitives
931==========================
932OP-TEE has three primitives for synchronization of threads and CPUs:
933*spin-lock*, *mutex*, and *condvar*.
934
935Spin-lock
936 A spin-lock is represented as an ``unsigned int``. This is the most
937 primitive lock. Interrupts should be disabled before attempting to take a
938 spin-lock and should remain disabled until the lock is released. A spin-lock
939 is initialized with ``SPINLOCK_UNLOCK``.
940
941 .. list-table:: Spin lock functions
942 :header-rows: 1
943 :widths: 1 5
944
945 * - Function
946 - Purpose
947
948 * - ``cpu_spin_lock(...)``
949 - Locks a spin-lock
950
951 * - ``cpu_spin_trylock(...)``
952 - Locks a spin-lock if unlocked and returns ``0`` else the spin-lock
953 is unchanged and the function returns ``!0``
954
955 * - ``cpu_spin_unlock(...)``
956 - Unlocks a spin-lock
957
958Mutex
959 A mutex is represented by ``struct mutex``. A mutex can be locked and
960 unlocked with interrupts enabled or disabled, but only from a normal thread.
961 A mutex cannot be used in an interrupt handler, abort handler or before a
962 thread has been selected for the CPU. A mutex is initialized with either
963 ``MUTEX_INITIALIZER`` or ``mutex_init(...)``.
964
965 .. list-table:: Mutex functions
966 :header-rows: 1
967 :widths: 1 5
968
969 * - Function
970 - Purpose
971
972 * - ``mutex_lock(...)``
973 - Locks a mutex. If the mutex is unlocked this is a fast operation,
974 else the function issues an RPC to wait in normal world.
975
976 * - ``mutex_unlock(...)``
977 - Unlocks a mutex. If there is no waiters this is a fast operation,
978 else the function issues an RPC to wake up a waiter in normal world.
979
980 * - ``mutex_trylock(...)``
981 - Locks a mutex if unlocked and returns ``true`` else the mutex is
982 unchanged and the function returns ``false``.
983
984 * - ``mutex_destroy(...)``
985 - Asserts that the mutex is unlocked and there is no waiters, after
986 this the memory used by the mutex can be freed.
987
988 When a mutex is locked it is owned by the thread calling ``mutex_lock(...)``
989 or ``mutex_trylock(...)``, the mutex may only be unlocked by the thread
990 owning the mutex. A thread should not exit to TA user space when holding a
991 mutex.
992
993Condvar
994 A condvar is represented by ``struct condvar``. A condvar is similar to a
995 ``pthread_condvar_t`` in the pthreads standard, only less advanced.
996 Condition variables are used to wait for some condition to be fulfilled and
997 are always used together a mutex. Once a condition variable has been used
998 together with a certain mutex, it must only be used with that mutex until
999 destroyed. A condvar is initialized with ``CONDVAR_INITIALIZER`` or
1000 ``condvar_init(...)``.
1001
1002 .. list-table:: Condvar functions
1003 :header-rows: 1
1004 :widths: 1 5
1005
1006 * - Function
1007 - Purpose
1008
1009 * - ``condvar_wait(...)``
1010 - Atomically unlocks the supplied mutex and waits in normal world via
1011 an RPC for the condition variable to be signaled, when the function
1012 returns the mutex is locked again.
1013
1014 * - ``condvar_signal(...)``
1015 - Wakes up one waiter of the condition variable (waiting in
1016 ``condvar_wait(...)``).
1017
1018 * - ``condvar_broadcast(...)``
1019 - Wake up all waiters of the condition variable.
1020
1021 The caller of ``condvar_signal(...)`` or ``condvar_broadcast(...)`` should
1022 hold the mutex associated with the condition variable to guarantee that a
1023 waiter does not miss the signal.
1024
1025.. _core/arch/arm/kernel/thread.c: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/kernel/thread.c
1026.. _optee_msg.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/optee_msg.h
1027.. _optee_smc.h: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/sm/optee_smc.h
1028.. _thread.c: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/kernel/thread.c
1029.. _thread.h: https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/kernel/thread.h
1030
1031.. _ARM_DEN0028A_SMC_Calling_Convention: http://infocenter.arm.com/help/topic/com.arm.doc.den0028b/ARM_DEN0028B_SMC_Calling_Convention.pdf
1032.. _Cortex-A53 TRM: http://infocenter.arm.com/help/topic/com.arm.doc.ddi0500j/DDI0500J_cortex_a53_trm.pdf
1033.. _drivers/tee/optee: https://github.com/torvalds/linux/tree/master/drivers/tee/optee
1034.. _Trusted Firmware A: https://github.com/ARM-software/arm-trusted-firmware