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
31 /* Standard includes. */
34 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE ensures that PRIVILEGED_FUNCTION
35 * is defined correctly and privileged functions are placed in correct sections. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* Portasm includes. */
41 /* System call numbers includes. */
42 #include "mpu_syscall_numbers.h"
44 /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE is needed to be defined only for the
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
48 #if ( configENABLE_MPU == 1 )
50 void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
56 " program_mpu_first_task: \n"
57 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
58 " ldr r0, [r2] \n" /* r0 = pxCurrentTCB. */
60 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
61 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
62 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
63 " bic r2, #1 \n" /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
64 " str r2, [r1] \n" /* Disable MPU. */
66 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
67 " ldr r1, [r0] \n" /* r1 = *r0 i.e. r1 = MAIR0. */
68 " ldr r2, =0xe000edc0 \n" /* r2 = 0xe000edc0 [Location of MAIR0]. */
69 " str r1, [r2] \n" /* Program MAIR0. */
71 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
72 " ldr r1, =0xe000ed98 \n" /* r1 = 0xe000ed98 [Location of RNR]. */
73 " ldr r2, =0xe000ed9c \n" /* r2 = 0xe000ed9c [Location of RBAR]. */
75 " movs r3, #4 \n" /* r3 = 4. */
76 " str r3, [r1] \n" /* Program RNR = 4. */
77 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
78 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
80 #if ( configTOTAL_MPU_REGIONS == 16 )
81 " movs r3, #8 \n" /* r3 = 8. */
82 " str r3, [r1] \n" /* Program RNR = 8. */
83 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
84 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
85 " movs r3, #12 \n" /* r3 = 12. */
86 " str r3, [r1] \n" /* Program RNR = 12. */
87 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
88 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
89 #endif /* configTOTAL_MPU_REGIONS == 16 */
91 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
92 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
93 " orr r2, #1 \n" /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
94 " str r2, [r1] \n" /* Enable MPU. */
95 " dsb \n" /* Force memory writes before continuing. */
97 " restore_context_first_task: \n"
98 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
99 " ldr r0, [r2] \n" /* r0 = pxCurrentTCB.*/
100 " ldr r1, [r0] \n" /* r1 = Location of saved context in TCB. */
102 " restore_special_regs_first_task: \n"
103 " ldmdb r1!, {r2-r4, lr} \n" /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, LR restored. */
106 " msr control, r4 \n"
108 " restore_general_regs_first_task: \n"
109 " ldmdb r1!, {r4-r11} \n" /* r4-r11 contain hardware saved context. */
110 " stmia r2!, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
111 " ldmdb r1!, {r4-r11} \n" /* r4-r11 restored. */
113 " restore_context_done_first_task: \n"
114 " str r1, [r0] \n" /* Save the location where the context should be saved next as the first member of TCB. */
116 " msr basepri, r0 \n" /* Ensure that interrupts are enabled when the first task starts. */
121 #else /* configENABLE_MPU */
123 void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
127 " .syntax unified \n"
129 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
130 " ldr r1, [r2] \n" /* Read pxCurrentTCB. */
131 " ldr r0, [r1] \n" /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
133 " ldm r0!, {r1-r2} \n" /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
134 " msr psplim, r1 \n" /* Set this task's PSPLIM value. */
135 " mrs r1, control \n" /* Obtain current control register value. */
136 " orrs r1, r1, #2 \n" /* r1 = r1 | 0x2 - Set the second bit to use the program stack pointer (PSP). */
137 " msr control, r1 \n" /* Write back the new control register value. */
138 " adds r0, #32 \n" /* Discard everything up to r0. */
139 " msr psp, r0 \n" /* This is now the new top of stack to use in the task. */
142 " msr basepri, r0 \n" /* Ensure that interrupts are enabled when the first task starts. */
143 " bx r2 \n" /* Finally, branch to EXC_RETURN. */
147 #endif /* configENABLE_MPU */
148 /*-----------------------------------------------------------*/
150 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
154 " .syntax unified \n"
156 " mrs r0, control \n" /* r0 = CONTROL. */
157 " tst r0, #1 \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
159 " movne r0, #0 \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
160 " moveq r0, #1 \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
161 " bx lr \n" /* Return. */
165 /*-----------------------------------------------------------*/
167 void vRaisePrivilege( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
171 " .syntax unified \n"
173 " mrs r0, control \n" /* Read the CONTROL register. */
174 " bic r0, #1 \n" /* Clear the bit 0. */
175 " msr control, r0 \n" /* Write back the new CONTROL value. */
176 " bx lr \n" /* Return to the caller. */
180 /*-----------------------------------------------------------*/
182 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
186 " .syntax unified \n"
188 " mrs r0, control \n" /* r0 = CONTROL. */
189 " orr r0, #1 \n" /* r0 = r0 | 1. */
190 " msr control, r0 \n" /* CONTROL = r0. */
191 " bx lr \n" /* Return to the caller. */
195 /*-----------------------------------------------------------*/
197 void vStartFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
201 " .syntax unified \n"
203 " ldr r0, =0xe000ed08 \n" /* Use the NVIC offset register to locate the stack. */
204 " ldr r0, [r0] \n" /* Read the VTOR register which gives the address of vector table. */
205 " ldr r0, [r0] \n" /* The first entry in vector table is stack pointer. */
206 " msr msp, r0 \n" /* Set the MSP back to the start of the stack. */
207 " cpsie i \n" /* Globally enable interrupts. */
211 " svc %0 \n" /* System call to start the first task. */
213 ::"i" ( portSVC_START_SCHEDULER ) : "memory"
216 /*-----------------------------------------------------------*/
218 uint32_t ulSetInterruptMask( void ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */
222 " .syntax unified \n"
224 " mrs r0, basepri \n" /* r0 = basepri. Return original basepri value. */
225 " mov r1, %0 \n" /* r1 = configMAX_SYSCALL_INTERRUPT_PRIORITY. */
226 " msr basepri, r1 \n" /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
229 " bx lr \n" /* Return. */
230 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
233 /*-----------------------------------------------------------*/
235 void vClearInterruptMask( __attribute__( ( unused ) ) uint32_t ulMask ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */
239 " .syntax unified \n"
241 " msr basepri, r0 \n" /* basepri = ulMask. */
244 " bx lr \n" /* Return. */
248 /*-----------------------------------------------------------*/
250 #if ( configENABLE_MPU == 1 )
252 void PendSV_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
256 " .syntax unified \n"
258 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
259 " ldr r0, [r2] \n" /* r0 = pxCurrentTCB. */
260 " ldr r1, [r0] \n" /* r1 = Location in TCB where the context should be saved. */
261 " mrs r2, psp \n" /* r2 = PSP. */
263 " save_general_regs: \n"
264 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
265 " add r2, r2, #0x20 \n" /* Move r2 to location where s0 is saved. */
268 " vstmiaeq r1!, {s16-s31} \n" /* Store s16-s31. */
269 " vldmiaeq r2, {s0-s16} \n" /* Copy hardware saved FP context into s0-s16. */
270 " vstmiaeq r1!, {s0-s16} \n" /* Store hardware saved FP context. */
271 " sub r2, r2, #0x20 \n" /* Set r2 back to the location of hardware saved context. */
272 #endif /* configENABLE_FPU || configENABLE_MVE */
274 " stmia r1!, {r4-r11} \n" /* Store r4-r11. */
275 " ldmia r2, {r4-r11} \n" /* Copy the hardware saved context into r4-r11. */
276 " stmia r1!, {r4-r11} \n" /* Store the hardware saved context. */
278 " save_special_regs: \n"
279 " mrs r3, psplim \n" /* r3 = PSPLIM. */
280 " mrs r4, control \n" /* r4 = CONTROL. */
281 " stmia r1!, {r2-r4, lr} \n" /* Store original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
282 " str r1, [r0] \n" /* Save the location from where the context should be restored as the first member of TCB. */
284 " select_next_task: \n"
285 " mov r0, %0 \n" /* r0 = configMAX_SYSCALL_INTERRUPT_PRIORITY */
286 " msr basepri, r0 \n" /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
289 " bl vTaskSwitchContext \n"
290 " mov r0, #0 \n" /* r0 = 0. */
291 " msr basepri, r0 \n" /* Enable interrupts. */
294 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
295 " ldr r0, [r2] \n" /* r0 = pxCurrentTCB. */
297 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
298 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
299 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
300 " bic r2, #1 \n" /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
301 " str r2, [r1] \n" /* Disable MPU. */
303 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
304 " ldr r1, [r0] \n" /* r1 = *r0 i.e. r1 = MAIR0. */
305 " ldr r2, =0xe000edc0 \n" /* r2 = 0xe000edc0 [Location of MAIR0]. */
306 " str r1, [r2] \n" /* Program MAIR0. */
308 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
309 " ldr r1, =0xe000ed98 \n" /* r1 = 0xe000ed98 [Location of RNR]. */
310 " ldr r2, =0xe000ed9c \n" /* r2 = 0xe000ed9c [Location of RBAR]. */
312 " movs r3, #4 \n" /* r3 = 4. */
313 " str r3, [r1] \n" /* Program RNR = 4. */
314 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
315 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
317 #if ( configTOTAL_MPU_REGIONS == 16 )
318 " movs r3, #8 \n" /* r3 = 8. */
319 " str r3, [r1] \n" /* Program RNR = 8. */
320 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
321 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
322 " movs r3, #12 \n" /* r3 = 12. */
323 " str r3, [r1] \n" /* Program RNR = 12. */
324 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
325 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
326 #endif /* configTOTAL_MPU_REGIONS == 16 */
328 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
329 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
330 " orr r2, #1 \n" /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
331 " str r2, [r1] \n" /* Enable MPU. */
332 " dsb \n" /* Force memory writes before continuing. */
334 " restore_context: \n"
335 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
336 " ldr r0, [r2] \n" /* r0 = pxCurrentTCB.*/
337 " ldr r1, [r0] \n" /* r1 = Location of saved context in TCB. */
339 " restore_special_regs: \n"
340 " ldmdb r1!, {r2-r4, lr} \n" /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, LR restored. */
343 " msr control, r4 \n"
345 " restore_general_regs: \n"
346 " ldmdb r1!, {r4-r11} \n" /* r4-r11 contain hardware saved context. */
347 " stmia r2!, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
348 " ldmdb r1!, {r4-r11} \n" /* r4-r11 restored. */
349 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
352 " vldmdbeq r1!, {s0-s16} \n" /* s0-s16 contain hardware saved FP context. */
353 " vstmiaeq r2!, {s0-s16} \n" /* Copy hardware saved FP context on the task stack. */
354 " vldmdbeq r1!, {s16-s31} \n" /* Restore s16-s31. */
355 #endif /* configENABLE_FPU || configENABLE_MVE */
357 " restore_context_done: \n"
358 " str r1, [r0] \n" /* Save the location where the context should be saved next as the first member of TCB. */
360 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
364 #else /* configENABLE_MPU */
366 void PendSV_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
370 " .syntax unified \n"
372 " mrs r0, psp \n" /* Read PSP in r0. */
374 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
375 " tst lr, #0x10 \n" /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
377 " vstmdbeq r0!, {s16-s31} \n" /* Store the additional FP context registers which are not saved automatically. */
378 #endif /* configENABLE_FPU || configENABLE_MVE */
380 " mrs r2, psplim \n" /* r2 = PSPLIM. */
381 " mov r3, lr \n" /* r3 = LR/EXC_RETURN. */
382 " stmdb r0!, {r2-r11} \n" /* Store on the stack - PSPLIM, LR and registers that are not automatically saved. */
384 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
385 " ldr r1, [r2] \n" /* Read pxCurrentTCB. */
386 " str r0, [r1] \n" /* Save the new top of stack in TCB. */
388 " mov r0, %0 \n" /* r0 = configMAX_SYSCALL_INTERRUPT_PRIORITY */
389 " msr basepri, r0 \n" /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
392 " bl vTaskSwitchContext \n"
393 " mov r0, #0 \n" /* r0 = 0. */
394 " msr basepri, r0 \n" /* Enable interrupts. */
396 " ldr r2, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
397 " ldr r1, [r2] \n" /* Read pxCurrentTCB. */
398 " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
400 " ldmia r0!, {r2-r11} \n" /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r11 restored. */
402 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
403 " tst r3, #0x10 \n" /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
405 " vldmiaeq r0!, {s16-s31} \n" /* Restore the additional FP context registers which are not restored automatically. */
406 #endif /* configENABLE_FPU || configENABLE_MVE */
408 " msr psplim, r2 \n" /* Restore the PSPLIM register value for the task. */
409 " msr psp, r0 \n" /* Remember the new top of stack for the task. */
411 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
415 #endif /* configENABLE_MPU */
416 /*-----------------------------------------------------------*/
418 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
420 void SVC_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
425 ".extern vPortSVCHandler_C \n"
426 ".extern vSystemCallEnter \n"
427 ".extern vSystemCallExit \n"
434 "ldr r1, [r0, #24] \n"
435 "ldrb r2, [r1, #-2] \n"
437 "blt syscall_enter \n"
439 "beq syscall_exit \n"
440 "b vPortSVCHandler_C \n"
444 " b vSystemCallEnter \n"
448 " b vSystemCallExit \n"
451 : "i" ( NUM_SYSTEM_CALLS ), "i" ( portSVC_SYSTEM_CALL_EXIT )
452 : "r0", "r1", "r2", "memory"
456 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
458 void SVC_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
462 " .syntax unified \n"
468 " ldr r1, =vPortSVCHandler_C \n"
473 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
474 /*-----------------------------------------------------------*/