2 * FreeRTOS Kernel V10.6.1
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
60 #if ( configENABLE_FPU == 1 )
61 #error Cortex-M23 does not have a Floating Point Unit (FPU) and therefore configENABLE_FPU must be set to 0.
63 /*-----------------------------------------------------------*/
65 /*---------------- Unprivileged Functions -------------------*/
67 /*-----------------------------------------------------------*/
69 SECTION .text:CODE:NOROOT(2)
71 /*-----------------------------------------------------------*/
74 mrs r0, control /* r0 = CONTROL. */
75 movs r1, #1 /* r1 = 1. */
76 tst r0, r1 /* Perform r0 & r1 (bitwise AND) and update the conditions flag. */
77 beq running_privileged /* If the result of previous AND operation was 0, branch. */
78 movs r0, #0 /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
81 movs r0, #1 /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
84 /*-----------------------------------------------------------*/
87 mrs r0, control /* r0 = CONTROL. */
88 movs r1, #1 /* r1 = 1. */
89 orrs r0, r1 /* r0 = r0 | r1. */
90 msr control, r0 /* CONTROL = r0. */
91 bx lr /* Return to the caller. */
92 /*-----------------------------------------------------------*/
94 /*----------------- Privileged Functions --------------------*/
96 /*-----------------------------------------------------------*/
98 SECTION privileged_functions:CODE:NOROOT(2)
100 /*-----------------------------------------------------------*/
102 #if ( configENABLE_MPU == 1 )
104 vRestoreContextOfFirstTask:
105 program_mpu_first_task:
106 ldr r3, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
107 ldr r0, [r3] /* r0 = pxCurrentTCB.*/
109 dmb /* Complete outstanding transfers before disabling MPU. */
110 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
111 ldr r2, [r1] /* Read the value of MPU_CTRL. */
112 movs r3, #1 /* r3 = 1. */
113 bics r2, r3 /* r2 = r2 & ~r3 i.e. Clear the bit 0 in r2. */
114 str r2, [r1] /* Disable MPU. */
116 adds r0, #4 /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
117 ldr r1, [r0] /* r1 = *r0 i.e. r1 = MAIR0. */
118 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
119 str r1, [r2] /* Program MAIR0. */
121 adds r0, #4 /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
122 ldr r1, =0xe000ed98 /* r1 = 0xe000ed98 [Location of RNR]. */
124 movs r3, #4 /* r3 = 4. */
125 str r3, [r1] /* Program RNR = 4. */
126 ldmia r0!, {r4-r5} /* Read first set of RBAR/RLAR registers from TCB. */
127 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
128 stmia r2!, {r4-r5} /* Write first set of RBAR/RLAR registers. */
129 movs r3, #5 /* r3 = 5. */
130 str r3, [r1] /* Program RNR = 5. */
131 ldmia r0!, {r4-r5} /* Read second set of RBAR/RLAR registers from TCB. */
132 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
133 stmia r2!, {r4-r5} /* Write second set of RBAR/RLAR registers. */
134 movs r3, #6 /* r3 = 6. */
135 str r3, [r1] /* Program RNR = 6. */
136 ldmia r0!, {r4-r5} /* Read third set of RBAR/RLAR registers from TCB. */
137 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
138 stmia r2!, {r4-r5} /* Write third set of RBAR/RLAR registers. */
139 movs r3, #7 /* r3 = 6. */
140 str r3, [r1] /* Program RNR = 7. */
141 ldmia r0!, {r4-r5} /* Read fourth set of RBAR/RLAR registers from TCB. */
142 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
143 stmia r2!, {r4-r5} /* Write fourth set of RBAR/RLAR registers. */
145 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
146 ldr r2, [r1] /* Read the value of MPU_CTRL. */
147 movs r3, #1 /* r3 = 1. */
148 orrs r2, r3 /* r2 = r2 | r3 i.e. Set the bit 0 in r2. */
149 str r2, [r1] /* Enable MPU. */
150 dsb /* Force memory writes before continuing. */
152 restore_context_first_task:
153 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
154 ldr r0, [r2] /* r0 = pxCurrentTCB.*/
155 ldr r1, [r0] /* r1 = Location of saved context in TCB. */
157 restore_special_regs_first_task:
159 ldmia r1!, {r2-r5} /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, r5 = LR. */
166 restore_general_regs_first_task:
168 ldmia r1!, {r4-r7} /* r4-r7 contain half of the hardware saved context. */
169 stmia r2!, {r4-r7} /* Copy half of the the hardware saved context on the task stack. */
170 ldmia r1!, {r4-r7} /* r4-r7 contain rest half of the hardware saved context. */
171 stmia r2!, {r4-r7} /* Copy rest half of the the hardware saved context on the task stack. */
173 ldmia r1!, {r4-r7} /* Restore r8-r11. */
174 mov r8, r4 /* r8 = r4. */
175 mov r9, r5 /* r9 = r5. */
176 mov r10, r6 /* r10 = r6. */
177 mov r11, r7 /* r11 = r7. */
179 ldmia r1!, {r4-r7} /* Restore r4-r7. */
182 restore_context_done_first_task:
183 str r1, [r0] /* Save the location where the context should be saved next as the first member of TCB. */
186 #else /* configENABLE_MPU */
188 vRestoreContextOfFirstTask:
189 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
190 ldr r1, [r2] /* Read pxCurrentTCB. */
191 ldr r0, [r1] /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
193 ldm r0!, {r1-r2} /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
194 msr psplim, r1 /* Set this task's PSPLIM value. */
195 movs r1, #2 /* r1 = 2. */
196 msr CONTROL, r1 /* Switch to use PSP in the thread mode. */
197 adds r0, #32 /* Discard everything up to r0. */
198 msr psp, r0 /* This is now the new top of stack to use in the task. */
200 bx r2 /* Finally, branch to EXC_RETURN. */
202 #endif /* configENABLE_MPU */
203 /*-----------------------------------------------------------*/
206 mrs r0, control /* Read the CONTROL register. */
207 movs r1, #1 /* r1 = 1. */
208 bics r0, r1 /* Clear the bit 0. */
209 msr control, r0 /* Write back the new CONTROL value. */
210 bx lr /* Return to the caller. */
211 /*-----------------------------------------------------------*/
214 ldr r0, =0xe000ed08 /* Use the NVIC offset register to locate the stack. */
215 ldr r0, [r0] /* Read the VTOR register which gives the address of vector table. */
216 ldr r0, [r0] /* The first entry in vector table is stack pointer. */
217 msr msp, r0 /* Set the MSP back to the start of the stack. */
218 cpsie i /* Globally enable interrupts. */
221 svc 102 /* System call to start the first task. portSVC_START_SCHEDULER = 102. */
223 /*-----------------------------------------------------------*/
229 /*-----------------------------------------------------------*/
234 /*-----------------------------------------------------------*/
236 #if ( configENABLE_MPU == 1 )
239 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
240 ldr r0, [r2] /* r0 = pxCurrentTCB. */
241 ldr r1, [r0] /* r1 = Location in TCB where the context should be saved. */
242 mrs r2, psp /* r2 = PSP. */
245 stmia r1!, {r4-r7} /* Store r4-r7. */
246 mov r4, r8 /* r4 = r8. */
247 mov r5, r9 /* r5 = r9. */
248 mov r6, r10 /* r6 = r10. */
249 mov r7, r11 /* r7 = r11. */
250 stmia r1!, {r4-r7} /* Store r8-r11. */
251 ldmia r2!, {r4-r7} /* Copy half of the hardware saved context into r4-r7. */
252 stmia r1!, {r4-r7} /* Store the hardware saved context. */
253 ldmia r2!, {r4-r7} /* Copy rest half of the hardware saved context into r4-r7. */
254 stmia r1!, {r4-r7} /* Store the hardware saved context. */
257 mrs r2, psp /* r2 = PSP. */
258 mrs r3, psplim /* r3 = PSPLIM. */
259 mrs r4, control /* r4 = CONTROL. */
260 mov r5, lr /* r5 = LR. */
261 stmia r1!, {r2-r5} /* Store original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
262 str r1, [r0] /* Save the location from where the context should be restored as the first member of TCB. */
266 bl vTaskSwitchContext
270 ldr r3, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
271 ldr r0, [r3] /* r0 = pxCurrentTCB.*/
273 dmb /* Complete outstanding transfers before disabling MPU. */
274 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
275 ldr r2, [r1] /* Read the value of MPU_CTRL. */
276 movs r3, #1 /* r3 = 1. */
277 bics r2, r3 /* r2 = r2 & ~r3 i.e. Clear the bit 0 in r2. */
278 str r2, [r1] /* Disable MPU. */
280 adds r0, #4 /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
281 ldr r1, [r0] /* r1 = *r0 i.e. r1 = MAIR0. */
282 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
283 str r1, [r2] /* Program MAIR0. */
285 adds r0, #4 /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
286 ldr r1, =0xe000ed98 /* r1 = 0xe000ed98 [Location of RNR]. */
288 movs r3, #4 /* r3 = 4. */
289 str r3, [r1] /* Program RNR = 4. */
290 ldmia r0!, {r4-r5} /* Read first set of RBAR/RLAR registers from TCB. */
291 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
292 stmia r2!, {r4-r5} /* Write first set of RBAR/RLAR registers. */
293 movs r3, #5 /* r3 = 5. */
294 str r3, [r1] /* Program RNR = 5. */
295 ldmia r0!, {r4-r5} /* Read second set of RBAR/RLAR registers from TCB. */
296 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
297 stmia r2!, {r4-r5} /* Write second set of RBAR/RLAR registers. */
298 movs r3, #6 /* r3 = 6. */
299 str r3, [r1] /* Program RNR = 6. */
300 ldmia r0!, {r4-r5} /* Read third set of RBAR/RLAR registers from TCB. */
301 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
302 stmia r2!, {r4-r5} /* Write third set of RBAR/RLAR registers. */
303 movs r3, #7 /* r3 = 6. */
304 str r3, [r1] /* Program RNR = 7. */
305 ldmia r0!, {r4-r5} /* Read fourth set of RBAR/RLAR registers from TCB. */
306 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
307 stmia r2!, {r4-r5} /* Write fourth set of RBAR/RLAR registers. */
309 ldr r1, =0xe000ed94 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
310 ldr r2, [r1] /* Read the value of MPU_CTRL. */
311 movs r3, #1 /* r3 = 1. */
312 orrs r2, r3 /* r2 = r2 | r3 i.e. Set the bit 0 in r2. */
313 str r2, [r1] /* Enable MPU. */
314 dsb /* Force memory writes before continuing. */
317 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
318 ldr r0, [r2] /* r0 = pxCurrentTCB.*/
319 ldr r1, [r0] /* r1 = Location of saved context in TCB. */
321 restore_special_regs:
323 ldmia r1!, {r2-r5} /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, r5 = LR. */
330 restore_general_regs:
332 ldmia r1!, {r4-r7} /* r4-r7 contain half of the hardware saved context. */
333 stmia r2!, {r4-r7} /* Copy half of the the hardware saved context on the task stack. */
334 ldmia r1!, {r4-r7} /* r4-r7 contain rest half of the hardware saved context. */
335 stmia r2!, {r4-r7} /* Copy rest half of the the hardware saved context on the task stack. */
337 ldmia r1!, {r4-r7} /* Restore r8-r11. */
338 mov r8, r4 /* r8 = r4. */
339 mov r9, r5 /* r9 = r5. */
340 mov r10, r6 /* r10 = r6. */
341 mov r11, r7 /* r11 = r7. */
343 ldmia r1!, {r4-r7} /* Restore r4-r7. */
346 restore_context_done:
347 str r1, [r0] /* Save the location where the context should be saved next as the first member of TCB. */
350 #else /* configENABLE_MPU */
353 mrs r0, psp /* Read PSP in r0. */
354 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
355 ldr r1, [r2] /* Read pxCurrentTCB. */
357 subs r0, r0, #40 /* Make space for PSPLIM, LR and the remaining registers on the stack. */
358 str r0, [r1] /* Save the new top of stack in TCB. */
359 mrs r2, psplim /* r2 = PSPLIM. */
360 mov r3, lr /* r3 = LR/EXC_RETURN. */
361 stmia r0!, {r2-r7} /* Store on the stack - PSPLIM, LR and low registers that are not automatically saved. */
362 mov r4, r8 /* r4 = r8. */
363 mov r5, r9 /* r5 = r9. */
364 mov r6, r10 /* r6 = r10. */
365 mov r7, r11 /* r7 = r11. */
366 stmia r0!, {r4-r7} /* Store the high registers that are not saved automatically. */
369 bl vTaskSwitchContext
372 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
373 ldr r1, [r2] /* Read pxCurrentTCB. */
374 ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
376 adds r0, r0, #24 /* Move to the high registers. */
377 ldmia r0!, {r4-r7} /* Restore the high registers that are not automatically restored. */
378 mov r8, r4 /* r8 = r4. */
379 mov r9, r5 /* r9 = r5. */
380 mov r10, r6 /* r10 = r6. */
381 mov r11, r7 /* r11 = r7. */
382 msr psp, r0 /* Remember the new top of stack for the task. */
383 subs r0, r0, #40 /* Move to the starting of the saved context. */
384 ldmia r0!, {r2-r7} /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r7 restored. */
385 msr psplim, r2 /* Restore the PSPLIM register value for the task. */
388 #endif /* configENABLE_MPU */
389 /*-----------------------------------------------------------*/
391 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
409 cmp r2, #NUM_SYSTEM_CALLS
410 blt system_call_enter
411 cmp r2, #104 /* portSVC_SYSTEM_CALL_EXIT. */
420 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
426 beq stacking_used_msp
433 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
434 /*-----------------------------------------------------------*/