2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 * Copyright 2024 Arm Limited and/or its affiliates
5 * <open-source-office@arm.com>
7 * SPDX-License-Identifier: MIT
9 * Permission is hereby granted, free of charge, to any person obtaining a copy of
10 * this software and associated documentation files (the "Software"), to deal in
11 * the Software without restriction, including without limitation the rights to
12 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13 * the Software, and to permit persons to whom the Software is furnished to do so,
14 * subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * https://www.FreeRTOS.org
27 * https://github.com/FreeRTOS
30 /* Including FreeRTOSConfig.h here will cause build errors if the header file
31 contains code not understood by the assembler - for example the 'extern' keyword.
32 To avoid errors place any such code inside a #ifdef __ICCARM__/#endif block so
33 the code is included in C files but excluded by the preprocessor in assembly
34 files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler. */
35 #include "FreeRTOSConfig.h"
37 /* System call numbers includes. */
38 #include "mpu_syscall_numbers.h"
40 #ifndef configUSE_MPU_WRAPPERS_V1
41 #define configUSE_MPU_WRAPPERS_V1 0
45 EXTERN vTaskSwitchContext
46 EXTERN vPortSVCHandler_C
47 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
48 EXTERN vSystemCallEnter
49 EXTERN vSystemCallExit
53 PUBLIC vResetPrivilege
54 PUBLIC vRestoreContextOfFirstTask
55 PUBLIC vRaisePrivilege
56 PUBLIC vStartFirstTask
57 PUBLIC ulSetInterruptMask
58 PUBLIC vClearInterruptMask
61 /*-----------------------------------------------------------*/
63 /*---------------- Unprivileged Functions -------------------*/
65 /*-----------------------------------------------------------*/
67 SECTION .text:CODE:NOROOT(2)
69 /*-----------------------------------------------------------*/
72 mrs r0, control /* r0 = CONTROL. */
73 tst r0, #1 /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
75 movne r0, #0 /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
76 moveq r0, #1 /* CONTROL[0]==0. Return true to indicate that the processor is not privileged. */
78 /*-----------------------------------------------------------*/
81 mrs r0, control /* r0 = CONTROL. */
82 orr r0, r0, #1 /* r0 = r0 | 1. */
83 msr control, r0 /* CONTROL = r0. */
84 bx lr /* Return to the caller. */
85 /*-----------------------------------------------------------*/
87 /*----------------- Privileged Functions --------------------*/
89 /*-----------------------------------------------------------*/
91 SECTION privileged_functions:CODE:NOROOT(2)
93 /*-----------------------------------------------------------*/
95 #if ( configENABLE_MPU == 1 )
97 vRestoreContextOfFirstTask:
98 program_mpu_first_task:
99 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
100 ldr r0, [r2] /* r0 = pxCurrentTCB. */
102 dmb /* Complete outstanding transfers before disabling MPU. */
103 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
104 ldr r2, [r1] /* Read the value of MPU_CTRL. */
105 bic r2, #1 /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
106 str r2, [r1] /* Disable MPU. */
108 adds r0, #4 /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
109 ldr r1, [r0] /* r1 = *r0 i.e. r1 = MAIR0. */
110 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
111 str r1, [r2] /* Program MAIR0. */
113 adds r0, #4 /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
114 ldr r1, =0xe000ed98 /* r1 = 0xe000ed98 [Location of RNR]. */
115 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
117 movs r3, #4 /* r3 = 4. */
118 str r3, [r1] /* Program RNR = 4. */
119 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
120 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
122 #if ( configTOTAL_MPU_REGIONS == 16 )
123 movs r3, #8 /* r3 = 8. */
124 str r3, [r1] /* Program RNR = 8. */
125 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
126 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
127 movs r3, #12 /* r3 = 12. */
128 str r3, [r1] /* Program RNR = 12. */
129 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
130 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
131 #endif /* configTOTAL_MPU_REGIONS == 16 */
133 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
134 ldr r2, [r1] /* Read the value of MPU_CTRL. */
135 orr r2, #1 /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
136 str r2, [r1] /* Enable MPU. */
137 dsb /* Force memory writes before continuing. */
139 restore_context_first_task:
140 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
141 ldr r0, [r2] /* r0 = pxCurrentTCB.*/
142 ldr r1, [r0] /* r1 = Location of saved context in TCB. */
144 restore_special_regs_first_task:
145 #if ( configENABLE_PAC == 1 )
146 ldmdb r1!, {r2-r5} /* Read task's dedicated PAC key from the task's context. */
147 msr PAC_KEY_P_0, r2 /* Write the task's dedicated PAC key to the PAC key registers. */
151 clrm {r2-r5} /* Clear r2-r5. */
152 #endif /* configENABLE_PAC */
153 ldmdb r1!, {r2-r4, lr} /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, LR restored. */
158 restore_general_regs_first_task:
159 ldmdb r1!, {r4-r11} /* r4-r11 contain hardware saved context. */
160 stmia r2!, {r4-r11} /* Copy the hardware saved context on the task stack. */
161 ldmdb r1!, {r4-r11} /* r4-r11 restored. */
163 restore_context_done_first_task:
164 str r1, [r0] /* Save the location where the context should be saved next as the first member of TCB. */
166 msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
169 #else /* configENABLE_MPU */
171 vRestoreContextOfFirstTask:
172 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
173 ldr r1, [r2] /* Read pxCurrentTCB. */
174 ldr r0, [r1] /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
176 #if ( configENABLE_PAC == 1 )
177 ldmia r0!, {r1-r4} /* Read task's dedicated PAC key from stack. */
178 msr PAC_KEY_P_3, r1 /* Write the task's dedicated PAC key to the PAC key registers. */
182 clrm {r1-r4} /* Clear r1-r4. */
183 #endif /* configENABLE_PAC */
185 ldm r0!, {r1-r2} /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
186 msr psplim, r1 /* Set this task's PSPLIM value. */
187 mrs r1, control /* Obtain current control register value. */
188 orrs r1, r1, #2 /* r1 = r1 | 0x2 - Set the second bit to use the program stack pointe (PSP). */
189 msr control, r1 /* Write back the new control register value. */
190 adds r0, #32 /* Discard everything up to r0. */
191 msr psp, r0 /* This is now the new top of stack to use in the task. */
194 msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
195 bx r2 /* Finally, branch to EXC_RETURN. */
197 #endif /* configENABLE_MPU */
198 /*-----------------------------------------------------------*/
201 mrs r0, control /* Read the CONTROL register. */
202 bic r0, r0, #1 /* Clear the bit 0. */
203 msr control, r0 /* Write back the new CONTROL value. */
204 bx lr /* Return to the caller. */
205 /*-----------------------------------------------------------*/
208 ldr r0, =0xe000ed08 /* Use the NVIC offset register to locate the stack. */
209 ldr r0, [r0] /* Read the VTOR register which gives the address of vector table. */
210 ldr r0, [r0] /* The first entry in vector table is stack pointer. */
211 msr msp, r0 /* Set the MSP back to the start of the stack. */
212 cpsie i /* Globally enable interrupts. */
216 svc 102 /* System call to start the first task. portSVC_START_SCHEDULER = 102. */
217 /*-----------------------------------------------------------*/
220 mrs r0, basepri /* r0 = basepri. Return original basepri value. */
221 mov r1, #configMAX_SYSCALL_INTERRUPT_PRIORITY
222 msr basepri, r1 /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
226 /*-----------------------------------------------------------*/
229 msr basepri, r0 /* basepri = ulMask. */
233 /*-----------------------------------------------------------*/
235 #if ( configENABLE_MPU == 1 )
238 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
239 ldr r0, [r2] /* r0 = pxCurrentTCB. */
240 ldr r1, [r0] /* r1 = Location in TCB where the context should be saved. */
241 mrs r2, psp /* r2 = PSP. */
244 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
245 add r2, r2, #0x20 /* Move r2 to location where s0 is saved. */
248 vstmiaeq r1!, {s16-s31} /* Store s16-s31. */
249 vldmiaeq r2, {s0-s16} /* Copy hardware saved FP context into s0-s16. */
250 vstmiaeq r1!, {s0-s16} /* Store hardware saved FP context. */
251 sub r2, r2, #0x20 /* Set r2 back to the location of hardware saved context. */
252 #endif /* configENABLE_FPU || configENABLE_MVE */
253 stmia r1!, {r4-r11} /* Store r4-r11. */
254 ldmia r2, {r4-r11} /* Copy the hardware saved context into r4-r11. */
255 stmia r1!, {r4-r11} /* Store the hardware saved context. */
258 mrs r3, psplim /* r3 = PSPLIM. */
259 mrs r4, control /* r4 = CONTROL. */
260 stmia r1!, {r2-r4, lr} /* Store original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
261 #if ( configENABLE_PAC == 1 )
262 mrs r2, PAC_KEY_P_0 /* Read task's dedicated PAC key from the PAC key registers. */
266 stmia r1!, {r2-r5} /* Store the task's dedicated PAC key on the task's context. */
267 clrm {r2-r5} /* Clear r2-r5. */
268 #endif /* configENABLE_PAC */
270 str r1, [r0] /* Save the location from where the context should be restored as the first member of TCB. */
273 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
274 msr basepri, r0 /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
277 bl vTaskSwitchContext
278 mov r0, #0 /* r0 = 0. */
279 msr basepri, r0 /* Enable interrupts. */
282 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
283 ldr r0, [r2] /* r0 = pxCurrentTCB. */
285 dmb /* Complete outstanding transfers before disabling MPU. */
286 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
287 ldr r2, [r1] /* Read the value of MPU_CTRL. */
288 bic r2, #1 /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
289 str r2, [r1] /* Disable MPU. */
291 adds r0, #4 /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
292 ldr r1, [r0] /* r1 = *r0 i.e. r1 = MAIR0. */
293 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
294 str r1, [r2] /* Program MAIR0. */
296 adds r0, #4 /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
297 ldr r1, =0xe000ed98 /* r1 = 0xe000ed98 [Location of RNR]. */
298 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
300 movs r3, #4 /* r3 = 4. */
301 str r3, [r1] /* Program RNR = 4. */
302 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
303 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
305 #if ( configTOTAL_MPU_REGIONS == 16 )
306 movs r3, #8 /* r3 = 8. */
307 str r3, [r1] /* Program RNR = 8. */
308 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
309 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
310 movs r3, #12 /* r3 = 12. */
311 str r3, [r1] /* Program RNR = 12. */
312 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
313 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
314 #endif /* configTOTAL_MPU_REGIONS == 16 */
316 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
317 ldr r2, [r1] /* Read the value of MPU_CTRL. */
318 orr r2, #1 /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
319 str r2, [r1] /* Enable MPU. */
320 dsb /* Force memory writes before continuing. */
323 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
324 ldr r0, [r2] /* r0 = pxCurrentTCB.*/
325 ldr r1, [r0] /* r1 = Location of saved context in TCB. */
327 restore_special_regs:
328 #if ( configENABLE_PAC == 1 )
329 ldmdb r1!, {r2-r5} /* Read task's dedicated PAC key from the task's context. */
330 msr PAC_KEY_P_0, r2 /* Write the task's dedicated PAC key to the PAC key registers. */
334 clrm {r2-r5} /* Clear r2-r5. */
335 #endif /* configENABLE_PAC */
336 ldmdb r1!, {r2-r4, lr} /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, LR restored. */
341 restore_general_regs:
342 ldmdb r1!, {r4-r11} /* r4-r11 contain hardware saved context. */
343 stmia r2!, {r4-r11} /* Copy the hardware saved context on the task stack. */
344 ldmdb r1!, {r4-r11} /* r4-r11 restored. */
345 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
348 vldmdbeq r1!, {s0-s16} /* s0-s16 contain hardware saved FP context. */
349 vstmiaeq r2!, {s0-s16} /* Copy hardware saved FP context on the task stack. */
350 vldmdbeq r1!, {s16-s31} /* Restore s16-s31. */
351 #endif /* configENABLE_FPU || configENABLE_MVE */
353 restore_context_done:
354 str r1, [r0] /* Save the location where the context should be saved next as the first member of TCB. */
357 #else /* configENABLE_MPU */
360 mrs r0, psp /* Read PSP in r0. */
361 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
362 tst lr, #0x10 /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
364 vstmdbeq r0!, {s16-s31} /* Store the additional FP context registers which are not saved automatically. */
365 #endif /* configENABLE_FPU || configENABLE_MVE */
367 mrs r2, psplim /* r2 = PSPLIM. */
368 mov r3, lr /* r3 = LR/EXC_RETURN. */
369 stmdb r0!, {r2-r11} /* Store on the stack - PSPLIM, LR and registers that are not automatically. */
371 #if ( configENABLE_PAC == 1 )
372 mrs r1, PAC_KEY_P_3 /* Read task's dedicated PAC key from the PAC key registers. */
376 stmdb r0!, {r1-r4} /* Store the task's dedicated PAC key on the stack. */
377 clrm {r1-r4} /* Clear r1-r4. */
378 #endif /* configENABLE_PAC */
380 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
381 ldr r1, [r2] /* Read pxCurrentTCB. */
382 str r0, [r1] /* Save the new top of stack in TCB. */
384 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
385 msr basepri, r0 /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
388 bl vTaskSwitchContext
389 mov r0, #0 /* r0 = 0. */
390 msr basepri, r0 /* Enable interrupts. */
392 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
393 ldr r1, [r2] /* Read pxCurrentTCB. */
394 ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
396 #if ( configENABLE_PAC == 1 )
397 ldmia r0!, {r2-r5} /* Read task's dedicated PAC key from stack. */
398 msr PAC_KEY_P_3, r2 /* Write the task's dedicated PAC key to the PAC key registers. */
402 clrm {r2-r5} /* Clear r2-r5. */
403 #endif /* configENABLE_PAC */
405 ldmia r0!, {r2-r11} /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r11 restored. */
407 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
408 tst r3, #0x10 /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
410 vldmiaeq r0!, {s16-s31} /* Restore the additional FP context registers which are not restored automatically. */
411 #endif /* configENABLE_FPU || configENABLE_MVE */
413 msr psplim, r2 /* Restore the PSPLIM register value for the task. */
414 msr psp, r0 /* Remember the new top of stack for the task. */
417 #endif /* configENABLE_MPU */
418 /*-----------------------------------------------------------*/
420 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
430 cmp r2, #NUM_SYSTEM_CALLS
432 cmp r2, #104 /* portSVC_SYSTEM_CALL_EXIT. */
444 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
453 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
454 /*-----------------------------------------------------------*/