2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
28 /* Standard includes. */
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE ensures that PRIVILEGED_FUNCTION
32 * is defined correctly and privileged functions are placed in correct sections. */
33 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
35 /* Portasm includes. */
38 /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE is needed to be defined only for the
40 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
42 void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
48 " ldr r2, pxCurrentTCBConst2 \n"/* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
49 " ldr r1, [r2] \n"/* Read pxCurrentTCB. */
50 " ldr r0, [r1] \n"/* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
52 #if ( configENABLE_MPU == 1 )
53 " dmb \n"/* Complete outstanding transfers before disabling MPU. */
54 " ldr r2, xMPUCTRLConst2 \n"/* r2 = 0xe000ed94 [Location of MPU_CTRL]. */
55 " ldr r4, [r2] \n"/* Read the value of MPU_CTRL. */
56 " bic r4, #1 \n"/* r4 = r4 & ~1 i.e. Clear the bit 0 in r4. */
57 " str r4, [r2] \n"/* Disable MPU. */
59 " adds r1, #4 \n"/* r1 = r1 + 4. r1 now points to MAIR0 in TCB. */
60 " ldr r3, [r1] \n"/* r3 = *r1 i.e. r3 = MAIR0. */
61 " ldr r2, xMAIR0Const2 \n"/* r2 = 0xe000edc0 [Location of MAIR0]. */
62 " str r3, [r2] \n"/* Program MAIR0. */
63 " ldr r2, xRNRConst2 \n"/* r2 = 0xe000ed98 [Location of RNR]. */
64 " movs r3, #4 \n"/* r3 = 4. */
65 " str r3, [r2] \n"/* Program RNR = 4. */
66 " adds r1, #4 \n"/* r1 = r1 + 4. r1 now points to first RBAR in TCB. */
67 " ldr r2, xRBARConst2 \n"/* r2 = 0xe000ed9c [Location of RBAR]. */
68 " ldmia r1!, {r4-r11} \n"/* Read 4 set of RBAR/RLAR registers from TCB. */
69 " stmia r2!, {r4-r11} \n"/* Write 4 set of RBAR/RLAR registers using alias registers. */
71 " ldr r2, xMPUCTRLConst2 \n"/* r2 = 0xe000ed94 [Location of MPU_CTRL]. */
72 " ldr r4, [r2] \n"/* Read the value of MPU_CTRL. */
73 " orr r4, #1 \n"/* r4 = r4 | 1 i.e. Set the bit 0 in r4. */
74 " str r4, [r2] \n"/* Enable MPU. */
75 " dsb \n"/* Force memory writes before continuing. */
76 #endif /* configENABLE_MPU */
78 #if ( configENABLE_MPU == 1 )
79 " ldm r0!, {r1-r3} \n"/* Read from stack - r1 = PSPLIM, r2 = CONTROL and r3 = EXC_RETURN. */
80 " msr psplim, r1 \n"/* Set this task's PSPLIM value. */
81 " msr control, r2 \n"/* Set this task's CONTROL value. */
82 " adds r0, #32 \n"/* Discard everything up to r0. */
83 " msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
86 " msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
87 " bx r3 \n"/* Finally, branch to EXC_RETURN. */
88 #else /* configENABLE_MPU */
89 " ldm r0!, {r1-r2} \n"/* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
90 " msr psplim, r1 \n"/* Set this task's PSPLIM value. */
91 " movs r1, #2 \n"/* r1 = 2. */
92 " msr CONTROL, r1 \n"/* Switch to use PSP in the thread mode. */
93 " adds r0, #32 \n"/* Discard everything up to r0. */
94 " msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
97 " msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
98 " bx r2 \n"/* Finally, branch to EXC_RETURN. */
99 #endif /* configENABLE_MPU */
102 "pxCurrentTCBConst2: .word pxCurrentTCB \n"
103 #if ( configENABLE_MPU == 1 )
104 "xMPUCTRLConst2: .word 0xe000ed94 \n"
105 "xMAIR0Const2: .word 0xe000edc0 \n"
106 "xRNRConst2: .word 0xe000ed98 \n"
107 "xRBARConst2: .word 0xe000ed9c \n"
108 #endif /* configENABLE_MPU */
111 /*-----------------------------------------------------------*/
113 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
117 " mrs r0, control \n"/* r0 = CONTROL. */
118 " tst r0, #1 \n"/* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
120 " movne r0, #0 \n"/* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
121 " moveq r0, #1 \n"/* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
122 " bx lr \n"/* Return. */
128 /*-----------------------------------------------------------*/
130 void vRaisePrivilege( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
134 " mrs r0, control \n"/* Read the CONTROL register. */
135 " bic r0, #1 \n"/* Clear the bit 0. */
136 " msr control, r0 \n"/* Write back the new CONTROL value. */
137 " bx lr \n"/* Return to the caller. */
141 /*-----------------------------------------------------------*/
143 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
147 " mrs r0, control \n"/* r0 = CONTROL. */
148 " orr r0, #1 \n"/* r0 = r0 | 1. */
149 " msr control, r0 \n"/* CONTROL = r0. */
150 " bx lr \n"/* Return to the caller. */
154 /*-----------------------------------------------------------*/
156 void vStartFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
160 " ldr r0, xVTORConst \n"/* Use the NVIC offset register to locate the stack. */
161 " ldr r0, [r0] \n"/* Read the VTOR register which gives the address of vector table. */
162 " ldr r0, [r0] \n"/* The first entry in vector table is stack pointer. */
163 " msr msp, r0 \n"/* Set the MSP back to the start of the stack. */
164 " cpsie i \n"/* Globally enable interrupts. */
168 " svc %0 \n"/* System call to start the first task. */
172 "xVTORConst: .word 0xe000ed08 \n"
173 ::"i" ( portSVC_START_SCHEDULER ) : "memory"
176 /*-----------------------------------------------------------*/
178 uint32_t ulSetInterruptMask( void ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */
182 " mrs r0, basepri \n"/* r0 = basepri. Return original basepri value. */
183 " mov r1, %0 \n"/* r1 = configMAX_SYSCALL_INTERRUPT_PRIORITY. */
184 " msr basepri, r1 \n"/* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
187 " bx lr \n"/* Return. */
188 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
191 /*-----------------------------------------------------------*/
193 void vClearInterruptMask( __attribute__( ( unused ) ) uint32_t ulMask ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */
197 " msr basepri, r0 \n"/* basepri = ulMask. */
200 " bx lr \n"/* Return. */
204 /*-----------------------------------------------------------*/
206 void PendSV_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
210 " .syntax unified \n"
212 " mrs r0, psp \n"/* Read PSP in r0. */
213 #if ( configENABLE_FPU == 1 )
214 " tst lr, #0x10 \n"/* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */
216 " vstmdbeq r0!, {s16-s31} \n"/* Store the FPU registers which are not saved automatically. */
217 #endif /* configENABLE_FPU */
218 #if ( configENABLE_MPU == 1 )
219 " mrs r1, psplim \n"/* r1 = PSPLIM. */
220 " mrs r2, control \n"/* r2 = CONTROL. */
221 " mov r3, lr \n"/* r3 = LR/EXC_RETURN. */
222 " stmdb r0!, {r1-r11} \n"/* Store on the stack - PSPLIM, CONTROL, LR and registers that are not automatically saved. */
223 #else /* configENABLE_MPU */
224 " mrs r2, psplim \n"/* r2 = PSPLIM. */
225 " mov r3, lr \n"/* r3 = LR/EXC_RETURN. */
226 " stmdb r0!, {r2-r11} \n"/* Store on the stack - PSPLIM, LR and registers that are not automatically saved. */
227 #endif /* configENABLE_MPU */
229 " ldr r2, pxCurrentTCBConst \n"/* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
230 " ldr r1, [r2] \n"/* Read pxCurrentTCB. */
231 " str r0, [r1] \n"/* Save the new top of stack in TCB. */
233 " mov r0, %0 \n"/* r0 = configMAX_SYSCALL_INTERRUPT_PRIORITY */
234 " msr basepri, r0 \n"/* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
237 " bl vTaskSwitchContext \n"
238 " mov r0, #0 \n"/* r0 = 0. */
239 " msr basepri, r0 \n"/* Enable interrupts. */
241 " ldr r2, pxCurrentTCBConst \n"/* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
242 " ldr r1, [r2] \n"/* Read pxCurrentTCB. */
243 " ldr r0, [r1] \n"/* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
245 #if ( configENABLE_MPU == 1 )
246 " dmb \n"/* Complete outstanding transfers before disabling MPU. */
247 " ldr r2, xMPUCTRLConst \n"/* r2 = 0xe000ed94 [Location of MPU_CTRL]. */
248 " ldr r4, [r2] \n"/* Read the value of MPU_CTRL. */
249 " bic r4, #1 \n"/* r4 = r4 & ~1 i.e. Clear the bit 0 in r4. */
250 " str r4, [r2] \n"/* Disable MPU. */
252 " adds r1, #4 \n"/* r1 = r1 + 4. r1 now points to MAIR0 in TCB. */
253 " ldr r3, [r1] \n"/* r3 = *r1 i.e. r3 = MAIR0. */
254 " ldr r2, xMAIR0Const \n"/* r2 = 0xe000edc0 [Location of MAIR0]. */
255 " str r3, [r2] \n"/* Program MAIR0. */
256 " ldr r2, xRNRConst \n"/* r2 = 0xe000ed98 [Location of RNR]. */
257 " movs r3, #4 \n"/* r3 = 4. */
258 " str r3, [r2] \n"/* Program RNR = 4. */
259 " adds r1, #4 \n"/* r1 = r1 + 4. r1 now points to first RBAR in TCB. */
260 " ldr r2, xRBARConst \n"/* r2 = 0xe000ed9c [Location of RBAR]. */
261 " ldmia r1!, {r4-r11} \n"/* Read 4 sets of RBAR/RLAR registers from TCB. */
262 " stmia r2!, {r4-r11} \n"/* Write 4 set of RBAR/RLAR registers using alias registers. */
264 " ldr r2, xMPUCTRLConst \n"/* r2 = 0xe000ed94 [Location of MPU_CTRL]. */
265 " ldr r4, [r2] \n"/* Read the value of MPU_CTRL. */
266 " orr r4, #1 \n"/* r4 = r4 | 1 i.e. Set the bit 0 in r4. */
267 " str r4, [r2] \n"/* Enable MPU. */
268 " dsb \n"/* Force memory writes before continuing. */
269 #endif /* configENABLE_MPU */
271 #if ( configENABLE_MPU == 1 )
272 " ldmia r0!, {r1-r11} \n"/* Read from stack - r1 = PSPLIM, r2 = CONTROL, r3 = LR and r4-r11 restored. */
273 #else /* configENABLE_MPU */
274 " ldmia r0!, {r2-r11} \n"/* Read from stack - r2 = PSPLIM, r3 = LR and r4-r11 restored. */
275 #endif /* configENABLE_MPU */
277 #if ( configENABLE_FPU == 1 )
278 " tst r3, #0x10 \n"/* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */
280 " vldmiaeq r0!, {s16-s31} \n"/* Restore the FPU registers which are not restored automatically. */
281 #endif /* configENABLE_FPU */
283 #if ( configENABLE_MPU == 1 )
284 " msr psplim, r1 \n"/* Restore the PSPLIM register value for the task. */
285 " msr control, r2 \n"/* Restore the CONTROL register value for the task. */
286 #else /* configENABLE_MPU */
287 " msr psplim, r2 \n"/* Restore the PSPLIM register value for the task. */
288 #endif /* configENABLE_MPU */
289 " msr psp, r0 \n"/* Remember the new top of stack for the task. */
293 "pxCurrentTCBConst: .word pxCurrentTCB \n"
294 #if ( configENABLE_MPU == 1 )
295 "xMPUCTRLConst: .word 0xe000ed94 \n"
296 "xMAIR0Const: .word 0xe000edc0 \n"
297 "xRNRConst: .word 0xe000ed98 \n"
298 "xRBARConst: .word 0xe000ed9c \n"
299 #endif /* configENABLE_MPU */
300 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
303 /*-----------------------------------------------------------*/
305 void SVC_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
313 " ldr r1, svchandler_address_const \n"
317 "svchandler_address_const: .word vPortSVCHandler_C \n"
320 /*-----------------------------------------------------------*/