2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
28 /* Including FreeRTOSConfig.h here will cause build errors if the header file
29 contains code not understood by the assembler - for example the 'extern' keyword.
30 To avoid errors place any such code inside a #ifdef __ICCARM__/#endif block so
31 the code is included in C files but excluded by the preprocessor in assembly
32 files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler. */
33 #include "FreeRTOSConfig.h"
35 /* System call numbers includes. */
36 #include "mpu_syscall_numbers.h"
38 #ifndef configUSE_MPU_WRAPPERS_V1
39 #define configUSE_MPU_WRAPPERS_V1 0
43 EXTERN vTaskSwitchContext
44 EXTERN vPortSVCHandler_C
45 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
46 EXTERN vSystemCallEnter
47 EXTERN vSystemCallExit
51 PUBLIC vResetPrivilege
52 PUBLIC vRestoreContextOfFirstTask
53 PUBLIC vRaisePrivilege
54 PUBLIC vStartFirstTask
55 PUBLIC ulSetInterruptMask
56 PUBLIC vClearInterruptMask
59 /*-----------------------------------------------------------*/
61 /*---------------- Unprivileged Functions -------------------*/
63 /*-----------------------------------------------------------*/
65 SECTION .text:CODE:NOROOT(2)
67 /*-----------------------------------------------------------*/
70 mrs r0, control /* r0 = CONTROL. */
71 tst r0, #1 /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
73 movne r0, #0 /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
74 moveq r0, #1 /* CONTROL[0]==0. Return true to indicate that the processor is not privileged. */
76 /*-----------------------------------------------------------*/
79 mrs r0, control /* r0 = CONTROL. */
80 orr r0, r0, #1 /* r0 = r0 | 1. */
81 msr control, r0 /* CONTROL = r0. */
82 bx lr /* Return to the caller. */
83 /*-----------------------------------------------------------*/
85 /*----------------- Privileged Functions --------------------*/
87 /*-----------------------------------------------------------*/
89 SECTION privileged_functions:CODE:NOROOT(2)
91 /*-----------------------------------------------------------*/
93 #if ( configENABLE_MPU == 1 )
95 vRestoreContextOfFirstTask:
96 program_mpu_first_task:
97 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
98 ldr r0, [r2] /* r0 = pxCurrentTCB. */
100 dmb /* Complete outstanding transfers before disabling MPU. */
101 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
102 ldr r2, [r1] /* Read the value of MPU_CTRL. */
103 bic r2, #1 /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
104 str r2, [r1] /* Disable MPU. */
106 adds r0, #4 /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
107 ldr r1, [r0] /* r1 = *r0 i.e. r1 = MAIR0. */
108 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
109 str r1, [r2] /* Program MAIR0. */
111 adds r0, #4 /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
112 ldr r1, =0xe000ed98 /* r1 = 0xe000ed98 [Location of RNR]. */
113 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
115 movs r3, #4 /* r3 = 4. */
116 str r3, [r1] /* Program RNR = 4. */
117 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
118 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
120 #if ( configTOTAL_MPU_REGIONS == 16 )
121 movs r3, #8 /* r3 = 8. */
122 str r3, [r1] /* Program RNR = 8. */
123 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
124 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
125 movs r3, #12 /* r3 = 12. */
126 str r3, [r1] /* Program RNR = 12. */
127 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
128 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
129 #endif /* configTOTAL_MPU_REGIONS == 16 */
131 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
132 ldr r2, [r1] /* Read the value of MPU_CTRL. */
133 orr r2, #1 /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
134 str r2, [r1] /* Enable MPU. */
135 dsb /* Force memory writes before continuing. */
137 restore_context_first_task:
138 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
139 ldr r0, [r2] /* r0 = pxCurrentTCB.*/
140 ldr r1, [r0] /* r1 = Location of saved context in TCB. */
142 restore_special_regs_first_task:
143 ldmdb r1!, {r2-r4, lr} /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, LR restored. */
148 restore_general_regs_first_task:
149 ldmdb r1!, {r4-r11} /* r4-r11 contain hardware saved context. */
150 stmia r2!, {r4-r11} /* Copy the hardware saved context on the task stack. */
151 ldmdb r1!, {r4-r11} /* r4-r11 restored. */
153 restore_context_done_first_task:
154 str r1, [r0] /* Save the location where the context should be saved next as the first member of TCB. */
156 msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
159 #else /* configENABLE_MPU */
161 vRestoreContextOfFirstTask:
162 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
163 ldr r1, [r2] /* Read pxCurrentTCB. */
164 ldr r0, [r1] /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
166 ldm r0!, {r1-r2} /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
167 msr psplim, r1 /* Set this task's PSPLIM value. */
168 movs r1, #2 /* r1 = 2. */
169 msr CONTROL, r1 /* Switch to use PSP in the thread mode. */
170 adds r0, #32 /* Discard everything up to r0. */
171 msr psp, r0 /* This is now the new top of stack to use in the task. */
174 msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
175 bx r2 /* Finally, branch to EXC_RETURN. */
177 #endif /* configENABLE_MPU */
178 /*-----------------------------------------------------------*/
181 mrs r0, control /* Read the CONTROL register. */
182 bic r0, r0, #1 /* Clear the bit 0. */
183 msr control, r0 /* Write back the new CONTROL value. */
184 bx lr /* Return to the caller. */
185 /*-----------------------------------------------------------*/
188 ldr r0, =0xe000ed08 /* Use the NVIC offset register to locate the stack. */
189 ldr r0, [r0] /* Read the VTOR register which gives the address of vector table. */
190 ldr r0, [r0] /* The first entry in vector table is stack pointer. */
191 msr msp, r0 /* Set the MSP back to the start of the stack. */
192 cpsie i /* Globally enable interrupts. */
196 svc 102 /* System call to start the first task. portSVC_START_SCHEDULER = 102. */
197 /*-----------------------------------------------------------*/
200 mrs r0, basepri /* r0 = basepri. Return original basepri value. */
201 mov r1, #configMAX_SYSCALL_INTERRUPT_PRIORITY
202 msr basepri, r1 /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
206 /*-----------------------------------------------------------*/
209 msr basepri, r0 /* basepri = ulMask. */
213 /*-----------------------------------------------------------*/
215 #if ( configENABLE_MPU == 1 )
218 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
219 ldr r0, [r2] /* r0 = pxCurrentTCB. */
220 ldr r1, [r0] /* r1 = Location in TCB where the context should be saved. */
221 mrs r2, psp /* r2 = PSP. */
224 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
225 add r2, r2, #0x20 /* Move r2 to location where s0 is saved. */
228 vstmiaeq r1!, {s16-s31} /* Store s16-s31. */
229 vldmiaeq r2, {s0-s16} /* Copy hardware saved FP context into s0-s16. */
230 vstmiaeq r1!, {s0-s16} /* Store hardware saved FP context. */
231 sub r2, r2, #0x20 /* Set r2 back to the location of hardware saved context. */
232 #endif /* configENABLE_FPU || configENABLE_MVE */
234 stmia r1!, {r4-r11} /* Store r4-r11. */
235 ldmia r2, {r4-r11} /* Copy the hardware saved context into r4-r11. */
236 stmia r1!, {r4-r11} /* Store the hardware saved context. */
239 mrs r3, psplim /* r3 = PSPLIM. */
240 mrs r4, control /* r4 = CONTROL. */
241 stmia r1!, {r2-r4, lr} /* Store original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
242 str r1, [r0] /* Save the location from where the context should be restored as the first member of TCB. */
245 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
246 msr basepri, r0 /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
249 bl vTaskSwitchContext
250 mov r0, #0 /* r0 = 0. */
251 msr basepri, r0 /* Enable interrupts. */
254 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
255 ldr r0, [r2] /* r0 = pxCurrentTCB. */
257 dmb /* Complete outstanding transfers before disabling MPU. */
258 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
259 ldr r2, [r1] /* Read the value of MPU_CTRL. */
260 bic r2, #1 /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
261 str r2, [r1] /* Disable MPU. */
263 adds r0, #4 /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
264 ldr r1, [r0] /* r1 = *r0 i.e. r1 = MAIR0. */
265 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
266 str r1, [r2] /* Program MAIR0. */
268 adds r0, #4 /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
269 ldr r1, =0xe000ed98 /* r1 = 0xe000ed98 [Location of RNR]. */
270 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
272 movs r3, #4 /* r3 = 4. */
273 str r3, [r1] /* Program RNR = 4. */
274 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
275 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
277 #if ( configTOTAL_MPU_REGIONS == 16 )
278 movs r3, #8 /* r3 = 8. */
279 str r3, [r1] /* Program RNR = 8. */
280 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
281 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
282 movs r3, #12 /* r3 = 12. */
283 str r3, [r1] /* Program RNR = 12. */
284 ldmia r0!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
285 stmia r2, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
286 #endif /* configTOTAL_MPU_REGIONS == 16 */
288 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
289 ldr r2, [r1] /* Read the value of MPU_CTRL. */
290 orr r2, #1 /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
291 str r2, [r1] /* Enable MPU. */
292 dsb /* Force memory writes before continuing. */
295 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
296 ldr r0, [r2] /* r0 = pxCurrentTCB.*/
297 ldr r1, [r0] /* r1 = Location of saved context in TCB. */
299 restore_special_regs:
300 ldmdb r1!, {r2-r4, lr} /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, LR restored. */
305 restore_general_regs:
306 ldmdb r1!, {r4-r11} /* r4-r11 contain hardware saved context. */
307 stmia r2!, {r4-r11} /* Copy the hardware saved context on the task stack. */
308 ldmdb r1!, {r4-r11} /* r4-r11 restored. */
309 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
312 vldmdbeq r1!, {s0-s16} /* s0-s16 contain hardware saved FP context. */
313 vstmiaeq r2!, {s0-s16} /* Copy hardware saved FP context on the task stack. */
314 vldmdbeq r1!, {s16-s31} /* Restore s16-s31. */
315 #endif /* configENABLE_FPU || configENABLE_MVE */
317 restore_context_done:
318 str r1, [r0] /* Save the location where the context should be saved next as the first member of TCB. */
321 #else /* configENABLE_MPU */
324 mrs r0, psp /* Read PSP in r0. */
325 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
326 tst lr, #0x10 /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
328 vstmdbeq r0!, {s16-s31} /* Store the additional FP context registers which are not saved automatically. */
329 #endif /* configENABLE_FPU || configENABLE_MVE */
331 mrs r2, psplim /* r2 = PSPLIM. */
332 mov r3, lr /* r3 = LR/EXC_RETURN. */
333 stmdb r0!, {r2-r11} /* Store on the stack - PSPLIM, LR and registers that are not automatically. */
335 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
336 ldr r1, [r2] /* Read pxCurrentTCB. */
337 str r0, [r1] /* Save the new top of stack in TCB. */
339 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
340 msr basepri, r0 /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
343 bl vTaskSwitchContext
344 mov r0, #0 /* r0 = 0. */
345 msr basepri, r0 /* Enable interrupts. */
347 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
348 ldr r1, [r2] /* Read pxCurrentTCB. */
349 ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
351 ldmia r0!, {r2-r11} /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r11 restored. */
353 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
354 tst r3, #0x10 /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
356 vldmiaeq r0!, {s16-s31} /* Restore the additional FP context registers which are not restored automatically. */
357 #endif /* configENABLE_FPU || configENABLE_MVE */
359 msr psplim, r2 /* Restore the PSPLIM register value for the task. */
360 msr psp, r0 /* Remember the new top of stack for the task. */
363 #endif /* configENABLE_MPU */
364 /*-----------------------------------------------------------*/
366 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
376 cmp r2, #NUM_SYSTEM_CALLS
378 cmp r2, #104 /* portSVC_SYSTEM_CALL_EXIT. */
390 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
399 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
400 /*-----------------------------------------------------------*/