]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/RTOS2/src/processIsolation.md
Doc: reworked CMSIS-RTOS2 for better abstraction from RTX and cleaned out errors.
[cmsis] / CMSIS / DoxyGen / RTOS2 / src / processIsolation.md
1 # Process Isolation {#CMSIS_RTOS_ProcessIsolation}
2
3 CMSIS-RTOS2 API supports a concept of **process isolation** that allows developers to protect execution of critical software tasks against potential flaws in other parts of a program.
4
5 Process Isolation in CMSIS-RTOS2 is enabled by following features:
6
7 - \subpage rtos_process_isolation_mpu for memory access protection in the system using Memory Protection Unit (MPU).<br/>
8     RTOS threads are executed with permission to access only memory regions and peripherals required for their operation. Hence thread code cannot accidentally modify critical RTOS kernel data or memory dedicated to other tasks.
9 .
10 - \subpage rtos_process_isolation_safety_class for access protection to RTOS objects via RTOS APIs.<br/>
11     The RTOS objects with a higher safety class assigned to them cannot be modified via RTOS API functions from threads that have lower safety class assigned.
12 .
13 - \subpage rtos_process_isolation_thread_wdt to verify execution times of threads.<br/>
14     Each thread can maintain own thread watchdog and in case of timing violations, corresponding thread watchdog alarm will be triggered.
15 .
16 - \subpage rtos_process_isolation_faults in case of a detected failure (for example thread watchdog alarm or MPU Fault).<br/>
17     The RTOS provides functions to block execution of malfunctioning components and with that dedicate system resources for operation of the safety critical threads.
18
19 \if FuSaRTS
20 Section \ref fusa_process_isolation lists safety requirements for Process Isolation functions.
21 \endif
22
23 <!-- =============================================================== !-->
24 \page rtos_process_isolation_mpu MPU Protected Zones
25
26 Memory Protection Unit (MPU) is available on many Cortex-M devices and allows to execute code with restricted access to memory regions and peripherals. Detailed information about the MPU can be found in [Cortex-M Reference Manuals](../../Core/html/index.html#ref_man_sec).
27
28 CMSIS-RTOS2 provides a concept of **MPU Protected Zones** as a simple and flexible mechanism for using MPUs with RTOS threads. MPU Protected Zones are defined by a user as a set of memory regions and peripherals with specified access rights, and each RTOS threads gets assigned to a specific MPU Protected Zone that it is allowed to use.
29
30 The figure below illustrates the concept for MPU Protected Zones for isolating threads.
31
32 ![System partitioning with MPU Protected Zones](./images/rtos_mpu.png)
33
34 Sections below explain in details how to define and use MPU Protected Zones:
35 - \ref rtos_process_isolation_mpu_def
36 - \ref rtos_process_isolation_mpu_load
37 - \ref rtos_process_isolation_mpu_objects
38 - \ref rtos_process_isolation_mpu_fault
39
40 **Function references**
41
42 Following functions implement and use MPU Protected Zone functionality:
43
44 - \ref osThreadNew : \copybrief osThreadNew
45 - \ref osThreadZone : \copybrief osThreadZone
46 - \ref osThreadGetZone : \copybrief osThreadGetZone
47 - \ref osThreadTerminateZone : \copybrief osThreadTerminateZone
48 - \ref osZoneSetup_Callback : \copybrief osZoneSetup_Callback
49
50 ## Define MPU Protected Zones {#rtos_process_isolation_mpu_def}
51
52 In the architectural design phase an application is logically split into functionalities with the same integrity level (same safety requirements). They can safely operate within the same MPU Protected Zone and hence access same memory areas and peripherals. 
53
54 MPU protected zones are defined in an MPU table where each row describes an individual MPU zone and each cell in the row specifies an MPU region within that zone. For details see section [MPU Functions](../../Core/html/group__mpu__functions.html) in CMSIS-Core(M) documentation.
55
56 \note
57 Interrupt handlers bypass the MPU protection. For this reason, it is required that potential impact of all interrupt handlers is strictly analyzed to exclude unintended memory accesses.
58
59 **Zone Identifier** (Zone ID) is used to refer to a specific MPU protected zone. Zone ID value equals to the row index (starting from 0) in the MPU table that describes corresponding MPU Protected Zone.
60
61 An MPU Protected Zone is assigned to one or more RTOS threads. This is done by providing the Zone ID value in thread attributes \ref osThreadAttr_t when creating the thread with the \ref osThreadNew function.
62
63 **Example:**
64
65 ```c
66 /* ThreadA thread attributes */
67 const osThreadAttr_t thread_A_attr = {
68   .name       = "ThreadA",       // human readable thread name
69   .attr_bits  = osThreadZone(3U) // assign thread to MPU protected zone with Zone Id 3
70 };
71 osThreadNew(ThreadA, NULL, &thread_A_attr);
72 ```
73
74 [CMSIS-Zone](https://arm-software.github.io/CMSIS_5/Zone/html/index.html) provides a utility that allows graphic configuration of MPU protected zones and generates MPU table in the CMSIS format.
75
76 ## Load MPU Protected Zone {#rtos_process_isolation_mpu_load}
77
78 When switching threads the RTOS kernel compares Zone IDs of the currently running thread and the next thread to be executed. If the Zone Ids are different then a callback function \ref osZoneSetup_Callback is called. This callback function shall be implemented in the user application code to actually switch to the new MPU Protected Zone. In the function the user should load the MPU Protected Zone according to the Zone Id provided in the argument.
79
80 **Example:**
81 ```c
82 /* Update MPU settings for newly activating Zone */
83 void osZoneSetup_Callback (uint32_t zone) {
84
85   if (zone >= ZONES_NUM) {
86     // Here issue an error for incorrect zone value
87   }
88
89   ARM_MPU_Load(mpu_table[zone], MPU_REGIONS);
90 }
91 ```
92
93 ## RTOS Objects and MPU Protection {#rtos_process_isolation_mpu_objects}
94 To access RTOS objects from the application RTOS APIs rely on a numeric `xxx_id` parameter associated with the object as explained in \ref rtos_objects. For example as `evt_flags` in this code:
95
96 ```c
97 osEventFlagsId_t evt_flags;
98 evt_flags = osEventFlagsNew(NULL);
99 osEventFlagsSet(evt_flags, 1);
100 ```
101
102 The allocation of an RTOS object to the memory in a specific MPU Protected Zone does not provide access restriction. The access restriction can be bypassed if another thread calls the CMSIS-RTOS2 API with the object ID of the RTOS object as argument. The CMSIS-RTOS2 function is executed in handler mode and therefore can access and modify the RTOS object without raising a Memory Fault.
103
104 To enable access control for RTOS objects the \ref rtos_process_isolation_safety_class concept is introduced in CMSIS-RTOS2.
105
106 ## Handle Memory Access Faults {#rtos_process_isolation_mpu_fault}
107
108 A memory access fault is triggered when a thread tries to access memory or peripherals outside of the MPU Protected Zone loaded while the thread is running. In such case Memory Management Interrupt [MemoryManagement_IRQn](../../Core/html/group__NVIC__gr.html) is triggered by the processor and its handling function is executed according to the exception vector table specified in the device startup file (by default \token{MemManage_Handler(void)} ).
109
110 The \e MemManage_Handler() interrupt handler is application specific and needs to be implemented by the user. In the handler it is possible to identify the thread that caused the memory access fault, the corresponding zone id and the safety class. This information can be used to define actions for entering a safe state. \ref rtos_process_isolation_faults provides more details on the available system recovery possibilities.
111
112 <!-- =============================================================== !-->
113 \page rtos_process_isolation_safety_class Safety Classes
114
115 \ref rtos_process_isolation_mpu_objects explains that MPU Protected Zones do not provide full access protection to RTOS objects accessed via CMSIS-RTOS2 API. The concept of a safety class fills this gap.
116
117 Every RTOS object, including thread is assigned with a numeric safety class value. A thread cannot modify an RTOS object if its safety class value is higher than the safety class value of the thread.
118 For example, it is not possible to change the priority or suspend a thread that has a higher safety class value than the thread that is currently executed.
119
120 **Function references**
121
122 - Following functions and macros are used explicitly for managing safety classes:
123   - \ref osSafetyClass : \copybrief osSafetyClass
124   - \ref osThreadGetClass : \copybrief osThreadGetClass
125   - \ref osSafetyWithSameClass : \copybrief osSafetyWithSameClass
126   - \ref osSafetyWithLowerClass : \copybrief osSafetyWithLowerClass
127   - \ref osKernelProtect : \copybrief osKernelProtect
128   - \ref osThreadSuspendClass : \copybrief osThreadSuspendClass
129   - \ref osThreadResumeClass : \copybrief osThreadResumeClass
130   - \ref osKernelDestroyClass  : \copybrief osKernelDestroyClass
131 - CMSIS-RTOS2 API functions that support safety class assignment when creating RTOS objects are listed in \ref rtos_process_isolation_safety_class_assign.
132 - CMSIS-RTOS2 API functions that verify safety class assignment before execution are listed in \ref rtos_process_isolation_safety_class_error lists.
133
134 ## Assign Safety Class to an RTOS Object {#rtos_process_isolation_safety_class_assign}
135
136 It is possible to create any objects regardless of the safety class after the kernel initialize with \ref osKernelInitialize, but before the kernel is started with \ref osKernelStart. This allows to setup a system before actually starting the RTOS kernel.
137
138 Threads of a higher safety class can create RTOS objects that belong to a lower or same safety class. For the object types listed below, the \e attr_bits can have an optional safety class value that is assigned when the RTOS object is created with the \e <i>os<Object>New</i> function. The macro \ref osSafetyClass encodes the value for the \e attr_bits field in the attr struct. For example:
139
140 ```c
141 const osEventFlagsAttr_t evt_flags_attr = {
142   .attr_bits = osSafetyClass(SAFETY_CLASS_SAFE_MODE_OPERATION)
143 };
144 osEventFlagsId_t evt_flags;
145 evt_flags = osEventFlagsNew(&evt_flags_attr);
146 ```
147
148 The following object types support safety class assignment when creating an object with corresponding \e os<Object>New function:
149
150 - \ref osThreadAttr_t \copybrief osThreadAttr_t Used in the \ref osThreadNew function.
151 - \ref osEventFlagsAttr_t \copybrief  osEventFlagsAttr_t Used in the \ref osThreadNew function.
152 - \ref osTimerAttr_t \copybrief osTimerAttr_t Used in the \ref osTimerNew function.
153 - \ref osMutexAttr_t \copybrief osMutexAttr_t Used in the \ref osMutexNew function.
154 - \ref osSemaphoreAttr_t \copybrief osSemaphoreAttr_t Used in the \ref osSemaphoreNew function.
155 - \ref osMemoryPoolAttr_t \copybrief osMemoryPoolAttr_t Used in the \ref osMemoryPoolNew function.
156 - \ref osMessageQueueAttr_t \copybrief osMessageQueueAttr_t Used in the \ref osMessageQueueNew function.
157
158 If safety class is not provided when creating the RTOS object then it inherits the safety class of the current running thread that creates the object. If the object is created before kernel is started and no safety class is provided, then it receives default safety class 0. This simplifies integration of third-party code that can be classified as non-safety critical.
159
160 ## Handle Object Access Violation {#rtos_process_isolation_safety_class_error}
161
162 RTOS API call returns error code \ref osErrorSafetyClass if the requested object manipulation cannot be performed because the target object has higher safety class than the safety class of the running thread. For example:
163
164 ```c
165 status = osEventFlagsSet(evt_flags, 1);
166 if (status == osErrorSafetyClass)
167 {
168   //handle the safety class error
169 }
170 ```
171
172 Following functions compare the safety class of the running thread with the safety class of the target object.
173
174 In \ref CMSIS_RTOS_KernelCtrl functions:
175
176 Comparison is done with safety class configured with \ref osKernelProtect
177 - \ref osKernelLock
178 - \ref osKernelRestoreLock
179 - \ref osKernelSuspend
180 - \ref osKernelProtect
181 - \ref osKernelDestroyClass
182
183 In \ref CMSIS_RTOS_ThreadMgmt functions:
184 - \ref osThreadNew
185 - \ref osThreadSetPriority
186 - \ref osThreadSuspend
187 - \ref osThreadResume
188 - \ref osThreadDetach
189 - \ref osThreadJoin
190 - \ref osThreadTerminate
191 - \ref osThreadSuspendClass
192 - \ref osThreadResumeClass
193
194 In \ref CMSIS_RTOS_ThreadFlagsMgmt functions:
195 - \ref osThreadFlagsSet
196
197 In \ref CMSIS_RTOS_EventFlags functions:
198 - \ref osEventFlagsNew
199 - \ref osEventFlagsSet
200 - \ref osEventFlagsClear
201 - \ref osEventFlagsWait
202 - \ref osEventFlagsDelete
203
204 In \ref CMSIS_RTOS_TimerMgmt functions:
205 - \ref osTimerNew
206 - \ref osTimerStart
207 - \ref osTimerStop
208 - \ref osTimerDelete
209
210 In \ref CMSIS_RTOS_MutexMgmt functions:
211 - \ref osMutexNew
212 - \ref osMutexAcquire
213 - \ref osMutexDelete
214
215 In \ref CMSIS_RTOS_SemaphoreMgmt functions:
216 - \ref osSemaphoreNew
217 - \ref osSemaphoreAcquire
218 - \ref osSemaphoreRelease
219 - \ref osSemaphoreDelete
220
221 In \ref CMSIS_RTOS_PoolMgmt functions:
222 - \ref osMemoryPoolNew
223 - \ref osMemoryPoolAlloc
224 - \ref osMemoryPoolFree
225 - \ref osMemoryPoolDelete
226
227 In \ref CMSIS_RTOS_Message functions:
228 - \ref osMessageQueueNew
229 - \ref osMessageQueuePut
230 - \ref osMessageQueueGet
231 - \ref osMessageQueueReset
232 - \ref osMessageQueueDelete
233
234 <!-- =============================================================== !-->
235 \page rtos_process_isolation_thread_wdt Thread Watchdogs
236
237 CMSIS-RTOS defines **Thread Watchdogs** that allow to control timing constraints for thread execution [temporal isolation](https://en.wikipedia.org/wiki/Temporal_isolation).
238
239 Each thread has an independent watchdog timer that is started with the function \ref osThreadFeedWatchdog(uint32_t ticks). The \token{ticks} value specifies the timeout before it expires.  Within this time interval the function \ref osThreadFeedWatchdog must be called again within the thread to restart the watchdog timer.
240
241 If the thread watchdog is not restarted during the specified amount of ticks the Watchdog Alarm  callback \ref osWatchdogAlarm_Handler(osThreadId_t thread_id) is triggered and can be used to recover the system or proceed to the system shutdown.
242
243 Figure below explains the concept with an example:
244
245 !["Example use of Thread Watchdogs](./images/thread_watchdogs.png)
246
247 \ref rtos_process_isolation_faults provides more details on the available possibilities for system recovery.
248 \note If the application suspends a thread from scheduling by calling \ref osThreadSuspend or \ref osThreadSuspendClass, the thread watchdog still continues to run, and it is expected to expire and trigger \ref osWatchdogAlarm_Handler because the thread will not be serviced as expected.
249 \note Hence it may be necessary to differentiate handling of thread watchdogs that expired unexpectedly from the thread watchdog alarms of intentionally suspended threads.
250
251 **Function references**
252
253 Summary of functions that implement thread watchdog functionality:
254
255 - \ref osThreadFeedWatchdog : \copybrief osThreadFeedWatchdog
256 - \ref osWatchdogAlarm_Handler : \copybrief osWatchdogAlarm_Handler
257
258 <!-- =============================================================== !-->
259 \page rtos_process_isolation_faults Fault Handling
260
261 When a failure, or an error is detected in a system (for example \ref rtos_process_isolation_mpu_fault "memory access fault", \ref rtos_process_isolation_thread_wdt "thread watchdog alarm", or others) CMSIS-RTOS2 API allows to stop further execution of selected RTOS threads. This can be used to block malfunctioning components or free computing resources and so enable execution of the safety critical threads. 
262
263 Following approaches are available:
264  - function \ref osThreadTerminateZone can be called in case of a fault exception. It will terminate all threads from the specified MPU Protected Zone (for example, can be the zone that has caused the fault). The function cannot be called in thread context or interrupts other than faults. Note that \ref osFaultResume can be called at the end of the handling code to return program execution into a known context and let kernel schedule the next thread ready for execution.
265  - function \ref osThreadSuspendClass can be called in case of a thread watchdog alarm or other errors handled in thread context. It allows to suspend operation of threads based on the safety class assignment. Function \ref osThreadResumeClass can be used to resume operation of threads based on their safety class. \ref rtos_process_isolation_thread_wdt contains an example that demonstrates fault handling concept for thread watchdogs.
266
267 Function \ref osKernelDestroyClass fully removes RTOS objects of specific safety classes from the system. This can be useful to do before restarting operation of terminated or suspended threads.
268
269 **Function references**
270
271 Following CMSIS-RTOS2 functions and macros support fault handling:
272
273 - \ref osThreadGetZone : \copybrief osThreadGetZone
274 - \ref osThreadTerminateZone : \copybrief osThreadTerminateZone
275 - \ref osThreadGetClass : \copybrief osThreadGetClass
276 - \ref osSafetyWithSameClass : \copybrief osSafetyWithSameClass
277 - \ref osSafetyWithLowerClass : \copybrief osSafetyWithLowerClass
278 - \ref osThreadSuspendClass : \copybrief osThreadSuspendClass
279 - \ref osThreadResumeClass : \copybrief osThreadResumeClass
280 - \ref osKernelDestroyClass : \copybrief osKernelDestroyClass
281 - \ref osFaultResume : \copybrief osFaultResume
282 - \ref osWatchdogAlarm_Handler : \copybrief osFaultResume