2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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
29 /* Standard includes. */
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE ensures that PRIVILEGED_FUNCTION
33 * is defined correctly and privileged functions are placed in correct sections. */
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36 /* Portasm includes. */
39 /* System call numbers includes. */
40 #include "mpu_syscall_numbers.h"
42 /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE is needed to be defined only for the
44 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
46 #if ( configENABLE_MPU == 1 )
48 void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
54 " program_mpu_first_task: \n"
55 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
56 " ldr r0, [r3] \n" /* r0 = pxCurrentTCB. */
58 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
59 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
60 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
61 " bic r2, #1 \n" /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
62 " str r2, [r1] \n" /* Disable MPU. */
64 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
65 " ldr r1, [r0] \n" /* r1 = *r0 i.e. r1 = MAIR0. */
66 " ldr r2, =0xe000edc0 \n" /* r2 = 0xe000edc0 [Location of MAIR0]. */
67 " str r1, [r2] \n" /* Program MAIR0. */
69 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
70 " ldr r1, =0xe000ed98 \n" /* r1 = 0xe000ed98 [Location of RNR]. */
71 " ldr r2, =0xe000ed9c \n" /* r2 = 0xe000ed9c [Location of RBAR]. */
73 " movs r3, #4 \n" /* r3 = 4. */
74 " str r3, [r1] \n" /* Program RNR = 4. */
75 " ldmia r0!, {r4-r11} \n" /* Read 4 set of RBAR/RLAR registers from TCB. */
76 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
78 #if ( configTOTAL_MPU_REGIONS == 16 )
79 " movs r3, #8 \n" /* r3 = 8. */
80 " str r3, [r1] \n" /* Program RNR = 8. */
81 " ldmia r0!, {r4-r11} \n" /* Read 4 set of RBAR/RLAR registers from TCB. */
82 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
83 " movs r3, #12 \n" /* r3 = 12. */
84 " str r3, [r1] \n" /* Program RNR = 12. */
85 " ldmia r0!, {r4-r11} \n" /* Read 4 set of RBAR/RLAR registers from TCB. */
86 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
87 #endif /* configTOTAL_MPU_REGIONS == 16 */
89 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
90 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
91 " orr r2, #1 \n" /* r2 = r1 | 1 i.e. Set the bit 0 in r2. */
92 " str r2, [r1] \n" /* Enable MPU. */
93 " dsb \n" /* Force memory writes before continuing. */
95 " restore_context_first_task: \n"
96 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
97 " ldr r1, [r3] \n" /* r1 = pxCurrentTCB.*/
98 " ldr r2, [r1] \n" /* r2 = Location of saved context in TCB. */
100 " restore_special_regs_first_task: \n"
101 " ldmdb r2!, {r0, r3-r5, lr} \n" /* r0 = xSecureContext, r3 = original PSP, r4 = PSPLIM, r5 = CONTROL, LR restored. */
104 " msr control, r5 \n"
105 " ldr r4, =xSecureContext \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */
106 " str r0, [r4] \n" /* Restore xSecureContext. */
108 " restore_general_regs_first_task: \n"
109 " ldmdb r2!, {r4-r11} \n" /* r4-r11 contain hardware saved context. */
110 " stmia r3!, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
111 " ldmdb r2!, {r4-r11} \n" /* r4-r11 restored. */
113 " restore_context_done_first_task: \n"
114 " str r2, [r1] \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 r3, [r2] \n" /* Read pxCurrentTCB. */
131 " ldr r0, [r3] \n" /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
133 " ldm r0!, {r1-r3} \n" /* Read from stack - r1 = xSecureContext, r2 = PSPLIM and r3 = EXC_RETURN. */
134 " ldr r4, =xSecureContext \n"
135 " str r1, [r4] \n" /* Set xSecureContext to this task's value for the same. */
136 " msr psplim, r2 \n" /* Set this task's PSPLIM value. */
137 " movs r1, #2 \n" /* r1 = 2. */
138 " msr CONTROL, r1 \n" /* Switch to use PSP in the thread mode. */
139 " adds r0, #32 \n" /* Discard everything up to r0. */
140 " msr psp, r0 \n" /* This is now the new top of stack to use in the task. */
143 " msr basepri, r0 \n" /* Ensure that interrupts are enabled when the first task starts. */
144 " bx r3 \n" /* Finally, branch to EXC_RETURN. */
148 #endif /* configENABLE_MPU */
149 /*-----------------------------------------------------------*/
151 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
155 " .syntax unified \n"
157 " mrs r0, control \n" /* r0 = CONTROL. */
158 " tst r0, #1 \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
160 " movne r0, #0 \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
161 " moveq r0, #1 \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
162 " bx lr \n" /* Return. */
166 /*-----------------------------------------------------------*/
168 void vRaisePrivilege( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
172 " .syntax unified \n"
174 " mrs r0, control \n" /* Read the CONTROL register. */
175 " bic r0, #1 \n" /* Clear the bit 0. */
176 " msr control, r0 \n" /* Write back the new CONTROL value. */
177 " bx lr \n" /* Return to the caller. */
181 /*-----------------------------------------------------------*/
183 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
187 " .syntax unified \n"
189 " mrs r0, control \n" /* r0 = CONTROL. */
190 " orr r0, #1 \n" /* r0 = r0 | 1. */
191 " msr control, r0 \n" /* CONTROL = r0. */
192 " bx lr \n" /* Return to the caller. */
196 /*-----------------------------------------------------------*/
198 void vStartFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
202 " .syntax unified \n"
204 " ldr r0, =0xe000ed08 \n" /* Use the NVIC offset register to locate the stack. */
205 " ldr r0, [r0] \n" /* Read the VTOR register which gives the address of vector table. */
206 " ldr r0, [r0] \n" /* The first entry in vector table is stack pointer. */
207 " msr msp, r0 \n" /* Set the MSP back to the start of the stack. */
208 " cpsie i \n" /* Globally enable interrupts. */
212 " svc %0 \n" /* System call to start the first task. */
214 ::"i" ( portSVC_START_SCHEDULER ) : "memory"
217 /*-----------------------------------------------------------*/
219 uint32_t ulSetInterruptMask( void ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */
223 " .syntax unified \n"
225 " mrs r0, basepri \n" /* r0 = basepri. Return original basepri value. */
226 " mov r1, %0 \n" /* r1 = configMAX_SYSCALL_INTERRUPT_PRIORITY. */
227 " msr basepri, r1 \n" /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
230 " bx lr \n" /* Return. */
231 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
234 /*-----------------------------------------------------------*/
236 void vClearInterruptMask( __attribute__( ( unused ) ) uint32_t ulMask ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */
240 " .syntax unified \n"
242 " msr basepri, r0 \n" /* basepri = ulMask. */
245 " bx lr \n" /* Return. */
249 /*-----------------------------------------------------------*/
251 #if ( configENABLE_MPU == 1 )
253 void PendSV_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
257 " .syntax unified \n"
258 " .extern SecureContext_SaveContext \n"
259 " .extern SecureContext_LoadContext \n"
261 " ldr r3, =xSecureContext \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */
262 " ldr r0, [r3] \n" /* Read xSecureContext - Value of xSecureContext must be in r0 as it is used as a parameter later. */
263 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
264 " ldr r1, [r3] \n" /* Read pxCurrentTCB - Value of pxCurrentTCB must be in r1 as it is used as a parameter later. */
265 " ldr r2, [r1] \n" /* r2 = Location in TCB where the context should be saved. */
267 " cbz r0, save_ns_context \n" /* No secure context to save. */
268 " save_s_context: \n"
269 " push {r0-r2, lr} \n"
270 " bl SecureContext_SaveContext \n" /* Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
271 " pop {r0-r2, lr} \n"
273 " save_ns_context: \n"
274 " mov r3, lr \n" /* r3 = LR (EXC_RETURN). */
275 " lsls r3, r3, #25 \n" /* r3 = r3 << 25. Bit[6] of EXC_RETURN is 1 if secure stack was used, 0 if non-secure stack was used to store stack frame. */
276 " bmi save_special_regs \n" /* r3 < 0 ==> Bit[6] in EXC_RETURN is 1 ==> secure stack was used to store the stack frame. */
278 " save_general_regs: \n"
281 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
282 " add r3, r3, #0x20 \n" /* Move r3 to location where s0 is saved. */
285 " vstmiaeq r2!, {s16-s31} \n" /* Store s16-s31. */
286 " vldmiaeq r3, {s0-s16} \n" /* Copy hardware saved FP context into s0-s16. */
287 " vstmiaeq r2!, {s0-s16} \n" /* Store hardware saved FP context. */
288 " sub r3, r3, #0x20 \n" /* Set r3 back to the location of hardware saved context. */
289 #endif /* configENABLE_FPU || configENABLE_MVE */
291 " stmia r2!, {r4-r11} \n" /* Store r4-r11. */
292 " ldmia r3, {r4-r11} \n" /* Copy the hardware saved context into r4-r11. */
293 " stmia r2!, {r4-r11} \n" /* Store the hardware saved context. */
295 " save_special_regs: \n"
296 " mrs r3, psp \n" /* r3 = PSP. */
297 " mrs r4, psplim \n" /* r4 = PSPLIM. */
298 " mrs r5, control \n" /* r5 = CONTROL. */
299 " stmia r2!, {r0, r3-r5, lr} \n" /* Store xSecureContext, original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
300 " str r2, [r1] \n" /* Save the location from where the context should be restored as the first member of TCB. */
302 " select_next_task: \n"
303 " mov r0, %0 \n" /* r0 = configMAX_SYSCALL_INTERRUPT_PRIORITY */
304 " msr basepri, r0 \n" /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
307 " bl vTaskSwitchContext \n"
308 " mov r0, #0 \n" /* r0 = 0. */
309 " msr basepri, r0 \n" /* Enable interrupts. */
312 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
313 " ldr r0, [r3] \n" /* r0 = pxCurrentTCB.*/
315 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
316 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
317 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
318 " bic r2, #1 \n" /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
319 " str r2, [r1] \n" /* Disable MPU. */
321 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
322 " ldr r1, [r0] \n" /* r1 = *r0 i.e. r1 = MAIR0. */
323 " ldr r2, =0xe000edc0 \n" /* r2 = 0xe000edc0 [Location of MAIR0]. */
324 " str r1, [r2] \n" /* Program MAIR0. */
326 " adds r0, #4 \n" /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
327 " ldr r1, =0xe000ed98 \n" /* r1 = 0xe000ed98 [Location of RNR]. */
328 " ldr r2, =0xe000ed9c \n" /* r2 = 0xe000ed9c [Location of RBAR]. */
330 " movs r3, #4 \n" /* r3 = 4. */
331 " str r3, [r1] \n" /* Program RNR = 4. */
332 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
333 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
335 #if ( configTOTAL_MPU_REGIONS == 16 )
336 " movs r3, #8 \n" /* r3 = 8. */
337 " str r3, [r1] \n" /* Program RNR = 8. */
338 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
339 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
340 " movs r3, #12 \n" /* r3 = 12. */
341 " str r3, [r1] \n" /* Program RNR = 12. */
342 " ldmia r0!, {r4-r11} \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */
343 " stmia r2, {r4-r11} \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */
344 #endif /* configTOTAL_MPU_REGIONS == 16 */
346 " ldr r1, =0xe000ed94 \n" /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
347 " ldr r2, [r1] \n" /* Read the value of MPU_CTRL. */
348 " orr r2, #1 \n" /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
349 " str r2, [r1] \n" /* Enable MPU. */
350 " dsb \n" /* Force memory writes before continuing. */
352 " restore_context: \n"
353 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
354 " ldr r1, [r3] \n" /* r1 = pxCurrentTCB.*/
355 " ldr r2, [r1] \n" /* r2 = Location of saved context in TCB. */
357 " restore_special_regs: \n"
358 " ldmdb r2!, {r0, r3-r5, lr} \n" /* r0 = xSecureContext, r3 = original PSP, r4 = PSPLIM, r5 = CONTROL, LR restored. */
361 " msr control, r5 \n"
362 " ldr r4, =xSecureContext \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */
363 " str r0, [r4] \n" /* Restore xSecureContext. */
364 " cbz r0, restore_ns_context \n" /* No secure context to restore. */
366 " restore_s_context: \n"
367 " push {r1-r3, lr} \n"
368 " bl SecureContext_LoadContext \n" /* Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
369 " pop {r1-r3, lr} \n"
371 " restore_ns_context: \n"
372 " mov r0, lr \n" /* r0 = LR (EXC_RETURN). */
373 " lsls r0, r0, #25 \n" /* r0 = r0 << 25. Bit[6] of EXC_RETURN is 1 if secure stack was used, 0 if non-secure stack was used to store stack frame. */
374 " bmi restore_context_done \n" /* r0 < 0 ==> Bit[6] in EXC_RETURN is 1 ==> secure stack was used to store the stack frame. */
376 " restore_general_regs: \n"
377 " ldmdb r2!, {r4-r11} \n" /* r4-r11 contain hardware saved context. */
378 " stmia r3!, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
379 " ldmdb r2!, {r4-r11} \n" /* r4-r11 restored. */
380 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
383 " vldmdbeq r2!, {s0-s16} \n" /* s0-s16 contain hardware saved FP context. */
384 " vstmiaeq r3!, {s0-s16} \n" /* Copy hardware saved FP context on the task stack. */
385 " vldmdbeq r2!, {s16-s31} \n" /* Restore s16-s31. */
386 #endif /* configENABLE_FPU || configENABLE_MVE */
388 " restore_context_done: \n"
389 " str r2, [r1] \n" /* Save the location where the context should be saved next as the first member of TCB. */
391 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
395 #else /* configENABLE_MPU */
397 void PendSV_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
401 " .syntax unified \n"
402 " .extern SecureContext_SaveContext \n"
403 " .extern SecureContext_LoadContext \n"
405 " ldr r3, =xSecureContext \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */
406 " ldr r0, [r3] \n" /* Read xSecureContext - Value of xSecureContext must be in r0 as it is used as a parameter later. */
407 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
408 " ldr r1, [r3] \n" /* Read pxCurrentTCB - Value of pxCurrentTCB must be in r1 as it is used as a parameter later. */
409 " mrs r2, psp \n" /* Read PSP in r2. */
411 " cbz r0, save_ns_context \n" /* No secure context to save. */
412 " push {r0-r2, r14} \n"
413 " bl SecureContext_SaveContext \n" /* Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
414 " pop {r0-r3} \n" /* LR is now in r3. */
415 " mov lr, r3 \n" /* LR = r3. */
416 " lsls r1, r3, #25 \n" /* r1 = r3 << 25. Bit[6] of EXC_RETURN is 1 if secure stack was used, 0 if non-secure stack was used to store stack frame. */
417 " bpl save_ns_context \n" /* bpl - branch if positive or zero. If r1 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */
419 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
420 " ldr r1, [r3] \n" /* Read pxCurrentTCB.*/
421 " subs r2, r2, #12 \n" /* Make space for xSecureContext, PSPLIM and LR on the stack. */
422 " str r2, [r1] \n" /* Save the new top of stack in TCB. */
423 " mrs r1, psplim \n" /* r1 = PSPLIM. */
424 " mov r3, lr \n" /* r3 = LR/EXC_RETURN. */
425 " stmia r2!, {r0, r1, r3} \n" /* Store xSecureContext, PSPLIM and LR on the stack. */
426 " b select_next_task \n"
428 " save_ns_context: \n"
429 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
430 " ldr r1, [r3] \n" /* Read pxCurrentTCB. */
431 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
432 " tst lr, #0x10 \n" /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
434 " vstmdbeq r2!, {s16-s31} \n" /* Store the additional FP context registers which are not saved automatically. */
435 #endif /* configENABLE_FPU || configENABLE_MVE */
436 " subs r2, r2, #44 \n" /* Make space for xSecureContext, PSPLIM, LR and the remaining registers on the stack. */
437 " str r2, [r1] \n" /* Save the new top of stack in TCB. */
438 " adds r2, r2, #12 \n" /* r2 = r2 + 12. */
439 " stm r2, {r4-r11} \n" /* Store the registers that are not saved automatically. */
440 " mrs r1, psplim \n" /* r1 = PSPLIM. */
441 " mov r3, lr \n" /* r3 = LR/EXC_RETURN. */
442 " subs r2, r2, #12 \n" /* r2 = r2 - 12. */
443 " stmia r2!, {r0, r1, r3} \n" /* Store xSecureContext, PSPLIM and LR on the stack. */
445 " select_next_task: \n"
446 " mov r0, %0 \n" /* r0 = configMAX_SYSCALL_INTERRUPT_PRIORITY */
447 " msr basepri, r0 \n" /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */
450 " bl vTaskSwitchContext \n"
451 " mov r0, #0 \n" /* r0 = 0. */
452 " msr basepri, r0 \n" /* Enable interrupts. */
454 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
455 " ldr r1, [r3] \n" /* Read pxCurrentTCB. */
456 " ldr r2, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. r2 now points to the top of stack. */
458 " ldmia r2!, {r0, r1, r4} \n" /* Read from stack - r0 = xSecureContext, r1 = PSPLIM and r4 = LR. */
459 " msr psplim, r1 \n" /* Restore the PSPLIM register value for the task. */
460 " mov lr, r4 \n" /* LR = r4. */
461 " ldr r3, =xSecureContext \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */
462 " str r0, [r3] \n" /* Restore the task's xSecureContext. */
463 " cbz r0, restore_ns_context \n" /* If there is no secure context for the task, restore the non-secure context. */
464 " ldr r3, =pxCurrentTCB \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
465 " ldr r1, [r3] \n" /* Read pxCurrentTCB. */
467 " bl SecureContext_LoadContext \n" /* Restore the secure context. Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
469 " mov lr, r4 \n" /* LR = r4. */
470 " lsls r1, r4, #25 \n" /* r1 = r4 << 25. Bit[6] of EXC_RETURN is 1 if secure stack was used, 0 if non-secure stack was used to store stack frame. */
471 " bpl restore_ns_context \n" /* bpl - branch if positive or zero. If r1 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */
472 " msr psp, r2 \n" /* Remember the new top of stack for the task. */
475 " restore_ns_context: \n"
476 " ldmia r2!, {r4-r11} \n" /* Restore the registers that are not automatically restored. */
477 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
478 " tst lr, #0x10 \n" /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
480 " vldmiaeq r2!, {s16-s31} \n" /* Restore the additional FP context registers which are not restored automatically. */
481 #endif /* configENABLE_FPU || configENABLE_MVE */
482 " msr psp, r2 \n" /* Remember the new top of stack for the task. */
484 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
488 #endif /* configENABLE_MPU */
489 /*-----------------------------------------------------------*/
491 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
493 void SVC_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
498 ".extern vPortSVCHandler_C \n"
499 ".extern vSystemCallEnter \n"
500 ".extern vSystemCallExit \n"
507 "ldr r1, [r0, #24] \n"
508 "ldrb r2, [r1, #-2] \n"
510 "blt syscall_enter \n"
512 "beq syscall_exit \n"
513 "b vPortSVCHandler_C \n"
517 " b vSystemCallEnter \n"
521 " b vSystemCallExit \n"
524 : "i" ( NUM_SYSTEM_CALLS ), "i" ( portSVC_SYSTEM_CALL_EXIT )
525 : "r0", "r1", "r2", "memory"
529 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
531 void SVC_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
535 " .syntax unified \n"
541 " ldr r1, =vPortSVCHandler_C \n"
546 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
547 /*-----------------------------------------------------------*/
549 void vPortAllocateSecureContext( uint32_t ulSecureStackSize ) /* __attribute__ (( naked )) */
553 " .syntax unified \n"
555 " svc %0 \n" /* Secure context is allocated in the supervisor call. */
556 " bx lr \n" /* Return. */
557 ::"i" ( portSVC_ALLOCATE_SECURE_CONTEXT ) : "memory"
560 /*-----------------------------------------------------------*/
562 void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
566 " .syntax unified \n"
568 " ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
569 " ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
570 " cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
572 " svcne %0 \n" /* Secure context is freed in the supervisor call. */
573 " bx lr \n" /* Return. */
574 ::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
577 /*-----------------------------------------------------------*/