/**
\cond (ARMv8M)
*/

/** 
\page using_TrustZone_pg  Using TrustZone for Armv8-M


\details 
The optional Armv8-M Security Extension is similar to Arm TrustZone technology used in Cortex-A processors, but is 
optimized for ultra-low power embedded applications. TrustZone for Armv8-M enables of multiple software security 
domains that restrict access to secure memory and I/O only for trusted software.

TrustZone for Armv8-M:
  - preserves low interrupt latencies for both Secure and Non-secure domains.
  - does not impose code overhead, cycle overhead or the complexity of a virtualization based solution.
  - introduces the Secure Gateway (SG) processor instruction for calls to the secure domain.

\b Notations

This manual uses the following notations to identify functions and hardware resources that are related to TrustZone for Armv8-M:

 - prefix \b TZ or \b __TZ indicates a function that is available only in Armv8-M TrustZone enabled devices.
 - postfix \b _NS indicates a hardware resource that belongs to the Non-secure state.
 - postfix \b _S indicates a hardware resource that belongs to the Secure state.
 
  
\section useCase_TrustZone Simplified Use Case with TrustZone

An Armv8-M TrustZone enabled device has restricted access for data, code, and I/O access to trusted 
software that runs in the Secure state. Typical applications are secure IoT nodes, firmware IP protection, 
or multi-party embedded software deployments.

The figure <b>Simplified Use Case</b> shows and embedded application that is split into a <b>User Project</b> 
(executed in Non-secure state) and a <b>Firmware Project</b> (executed in Secure state). 

 - <b>System Start:</b> after power on or reset, an Armv8-M system starts code execution in the <b>Secure state</b>. The access rights for the <b>Non-secure state</b> is configured.

 - <b>User Application:</b> control can be transferred to <b>Non-secure state</b> to execute user code. This code can only call functions in the <b>secure state</b> that are marked for execution with the <b>SG</b> (secure gate) instruction and memory attributes. Any attempt to access memory or peripherals that are assigned to the <b>Secure state</b> triggers a security exception.

 - <b>Firmware callbacks:</b> code running in the <b>Secure state</b> can execute code in the <b>Non-secure state</b> using call-back function pointers. For example, a communication stack (protected firmware) could use an I/O driver that is configured in user space.
 
\anchor SimpleUseCase
\image html "SimpleUseCase.png" "Simplified Use Case"

Program execution in the <b>Secure state</b> is further protected by TrustZone hardware from software failures.
For example, an Armv8-M system may implement two independent SYSTICK timers which allows to stop code execution 
in <b>Non-secure state</b> in case of timing violations. Also function pointer callbacks from <b>Secure state</b> 
to <b>Non-secure state</b> protected by a special CPU instruction and the address bit 0 which prevents anciently 
executing code in <b>Non-secure state</b>.

\subsection Example_TrustZone Program Examples

This CMSIS software pack contains the following program examples that show the usage of TrustZone for Armv8-M on Cortex-M33 devices:

Example                                     | Description
:-------------------------------------------|:----------------
TrustZone for Armv8-M No RTOS               | bare-metal secure/non-secure programming without RTOS (shows the Simplified Use Case).
TrustZone for Armv8-M RTOS                  | secure/non-secure RTOS example with thread context management
TrustZone for Armv8-M RTOS Security Tests   | secure/non-secure RTOS example with security test cases and system recovery

Other sample application that reflects this <a href="#SimpleUseCase"><b>Simplified Use Case</b></a> is the <b>Armv8MBL Secure/Non-Secure example</b> that is available in 
the Software Pack <b>Keil - Arm V2M-MPS2 Board Support PACK for Cortex-M System Design Kit Devices</b> 
(Keil:V2M-MPS2_CMx_BSP.1.2.0.pack or higher).

\section Model_TrustZone Programmers Model with TrustZone

The figure <a href="#MemoryMap_S"><b>Secure Memory Map</b></a> shows the memory view for the <b>Secure state</b>.  In the Secure state all
memory and peripherals can be accessed. The <b>System Control and Debug</b> area provides access to secure peripherals
and non-secure peripherals that are mirrored at a memory alias.  

The secure peripherals are only accessible during program execution in <b>Secure state</b>. The Secure Attribute Unit (SAU)
configures the non-secure memory, peripheral, and interrupt access. Also available are a secure MPU (memory protection 
unit), secure SCB (system control block), and secure SysTick timer.

The system supports two separate interrupt vector tables for secure and non-secure code execution. 
This interrupt assignment is controlled during <b>Secure state</b> code execution via the NVIC 
(nested vector interrupt controller).

\anchor MemoryMap_S
\image html "MemoryMap_S.png" "Secure Memory Map"

The figure <a href="#MemoryMap_NS"><b>Non-Secure Memory Map</b></a> shows the memory view for the Non-secure state. This memory view is identical
to the traditional Cortex-M memory map. Access to any secure memory or peripheral space triggers the secure exception
that executes a handler in <b>Secure state</b>.

The \ref partition_h_pg defines the initial setup of the <a href="#MemoryMap_NS"><b>Non-Secure Memory Map</b></a> during system start in the Secure state
(refer to functions \ref SystemInit and \ref TZ_SAU_Setup).

<!-- <img id="MemoryMap_NS" src="MemoryMap_NS.png"><CENTER><b>Non-Secure Memory Map</b></CENTER> -->

\anchor MemoryMap_NS
\image html "MemoryMap_NS.png" "Non-Secure Memory Map" 

The figure <b>Registers</b> shows the register view of the Armv8-M system with TrustZone. As the general purpose registers
are can be accessed from any state (secure or non-secure), function calls between the states use these registers for parameter
and return values.

The register R13 is the stack pointer alias, and the actual stack pointer (PSP_NS, MSP_NS, PSP_S, MSP_S)  
accessed depends on state (Secure or Non-secure) and mode (handler=exception/interrupt execution or
thread=normal code execution). 

In Armv8-M Mainline, each stack pointer has a limit register (PSPLIM_NS, MSPLIM_NS, PSPLIM_S, MSPLIM_S)
that traps stack overflows with the \b UsageFault exception (register UFSR bit STKOF=1).

An Armv8-M system with TrustZone has an independent \b CONTROL register for each state (Secure or Non-secure).
The interrupt/exception control registers (PRIMASK, FAULTMASK, BASEPRI) are banked between the states (Secure or Non-secure),
however the interrupt priority for the Non-Secure state can be lowered (SCB_AIRCR register bit PRIS) so that 
secure interrupts have always higher priority.

The core registers of the current state (Secure or Non-secure) are accessed using the standard \ref Core_Register_gr
functions. In Secure state all non-secure registers are accessible using the \ref coreregister_trustzone_functions 
related to TrustZone for Armv8-M.

\image html "Registers.png" "Registers"

\subsection RTOS_TrustZone_stacksealing Stack Sealing

CMSIS-Core \ref stacksealing_support_trustzone_functions provide standard interface for implementing the <a href="https://developer.arm.com/support/arm-security-updates/armv8-m-stack-sealing" target="_blank"><b>Secure Stack Sealing technique</b></a> recommended for mitigating some security vulnerabilities on Armv8-M systems with TrustZone.

\ref startup_c_sec_v8 demonstrates how this functionality can be used in a device startup file. 

Stack Sealing also requires an application project to have a linker script that explicitly reserves 8 bytes for the stack seal on top of the secure main stack. Linker files provided with \ref device_examples for Armv8-M cores demonstrate how this can be implemented. For example see .\\Device\\ARM\\ARMCM33\\Source\\ARM\\ARMCM33_ac6.sct.

To learn more about the stack sealing implementation in CMSIS projects for Armv8-M devices, refer to
<a href="https://www.keil.com/appnotes/docs/apnt_335.asp" target="_blank"><b>Application Note 335</b></a>.

\section CMSIS_Files_TrustZone CMSIS Files for TrustZone

The CMSIS-Core files are extended by the header files \ref partition_h_pg and \ref partition_gen_h_pg :

 - The file \ref partition_h_pg "partition_<device>.h" defines the initial system configuration and during SystemInit in Secure state.
 - The file \ref partition_gen_h_pg "partition_gen.h" is optional and contains SAU region and interrupt target assignments. This file may be generated using CMSIS-Zone.

\note
Refer to \ref using_pg for a general description of the CMSIS-Core (Cortex-M) files.

\image html "CMSIS_TZ_files.png" "CMSIS with extensions for TrustZone"

&nbsp;

\subsection RTOS_TrustZone RTOS Thread Context Management

To provide a consistent RTOS thread context management for Armv8-M TrustZone across the various real-time operating systems (RTOS), the CMSIS-Core (Cortex-M) includes header file <b>TZ_context.h</b> with API definitions.
An <i>non-secure application</i> which uses an RTOS and calls <i>secure</i> library modules requires the management of the <i>secure</i> stack space.   Since <i>secure state</i> registers cannot be accessed 
by the RTOS that runs in <i>non-secure state</i> secure functions implement the thread context switch.

As the <i>non-secure state</i> and <i>secure state</i> parts of an application are separated, the API for managing the <i>secure</i> stack space should be standardized. Otherwise the <i>secure</i> library modules
would force the <i>non-secure state</i> application to use a matching RTOS implementation.

\image html "TZ_context.png" "RTOS Thread Context Management for Armv8-M TrustZone"

To allocate the context memory for threads, an RTOS kernel that runs in <i>non-secure state</i> calls the interface functions defined by the header file <b>TZ_context.h</b>. The <b>TZ_context</b> functions itself are
part of the <i>secure state</i> application. An minimum implementation is provided as part of RTOS2 and should handle the secure stack for the thread execution. However it is also possible to implement the context memory 
management system with additional features such as access control to <i>secure state</i> memory regions using an MPU.

The API functions of <b>TZ_context</b> are described in the chapter <a href="modules.html">\b Reference </a> under \ref trustzone_functions - \ref context_trustzone_functions.

Refer to \ref Example_TrustZone for RTOS examples that provide a template implementation for <b>TZ_context.c</b>.
  
*/

/**
\endcond
*/