]> begriffs open source - freertos/blob - portable/IAR/ARM_CM55/non_secure/portasm.s
Fix spelling typos (#1168)
[freertos] / portable / IAR / ARM_CM55 / non_secure / portasm.s
1 /*
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>
6  *
7  * SPDX-License-Identifier: MIT
8  *
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:
15  *
16  * The above copyright notice and this permission notice shall be included in all
17  * copies or substantial portions of the Software.
18  *
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.
25  *
26  * https://www.FreeRTOS.org
27  * https://github.com/FreeRTOS
28  *
29  */
30 /* Including FreeRTOSConfig.h here will cause build errors if the header file
31 contains code not understood by the assembler - for example the 'extern' keyword.
32 To avoid errors place any such code inside a #ifdef __ICCARM__/#endif block so
33 the code is included in C files but excluded by the preprocessor in assembly
34 files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler. */
35 #include "FreeRTOSConfig.h"
36
37 /* System call numbers includes. */
38 #include "mpu_syscall_numbers.h"
39
40 #ifndef configUSE_MPU_WRAPPERS_V1
41     #define configUSE_MPU_WRAPPERS_V1 0
42 #endif
43
44     EXTERN pxCurrentTCB
45     EXTERN xSecureContext
46     EXTERN vTaskSwitchContext
47     EXTERN vPortSVCHandler_C
48     EXTERN SecureContext_SaveContext
49     EXTERN SecureContext_LoadContext
50 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
51     EXTERN vSystemCallEnter
52     EXTERN vSystemCallExit
53 #endif
54
55     PUBLIC xIsPrivileged
56     PUBLIC vResetPrivilege
57     PUBLIC vPortAllocateSecureContext
58     PUBLIC vRestoreContextOfFirstTask
59     PUBLIC vRaisePrivilege
60     PUBLIC vStartFirstTask
61     PUBLIC ulSetInterruptMask
62     PUBLIC vClearInterruptMask
63     PUBLIC PendSV_Handler
64     PUBLIC SVC_Handler
65     PUBLIC vPortFreeSecureContext
66 /*-----------------------------------------------------------*/
67
68 /*---------------- Unprivileged Functions -------------------*/
69
70 /*-----------------------------------------------------------*/
71
72     SECTION .text:CODE:NOROOT(2)
73     THUMB
74 /*-----------------------------------------------------------*/
75
76 xIsPrivileged:
77     mrs r0, control                         /* r0 = CONTROL. */
78     tst r0, #1                              /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
79     ite ne
80     movne r0, #0                            /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
81     moveq r0, #1                            /* CONTROL[0]==0. Return true to indicate that the processor is not privileged. */
82     bx lr                                   /* Return. */
83 /*-----------------------------------------------------------*/
84
85 vResetPrivilege:
86     mrs r0, control                         /* r0 = CONTROL. */
87     orr r0, r0, #1                          /* r0 = r0 | 1. */
88     msr control, r0                         /* CONTROL = r0. */
89     bx lr                                   /* Return to the caller. */
90 /*-----------------------------------------------------------*/
91
92 vPortAllocateSecureContext:
93     svc 100                                 /* Secure context is allocated in the supervisor call. portSVC_ALLOCATE_SECURE_CONTEXT = 100. */
94     bx lr                                   /* Return. */
95 /*-----------------------------------------------------------*/
96
97 /*----------------- Privileged Functions --------------------*/
98
99 /*-----------------------------------------------------------*/
100
101     SECTION privileged_functions:CODE:NOROOT(2)
102     THUMB
103 /*-----------------------------------------------------------*/
104
105 #if ( configENABLE_MPU == 1 )
106
107 vRestoreContextOfFirstTask:
108     program_mpu_first_task:
109         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
110         ldr r0, [r3]                        /* r0 = pxCurrentTCB. */
111
112         dmb                                 /* Complete outstanding transfers before disabling MPU. */
113         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
114         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
115         bic r2, #1                          /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
116         str r2, [r1]                        /* Disable MPU. */
117
118         adds r0, #4                         /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
119         ldr r1, [r0]                        /* r1 = *r0 i.e. r1 = MAIR0. */
120         ldr r2, =0xe000edc0                 /* r2 = 0xe000edc0 [Location of MAIR0]. */
121         str r1, [r2]                        /* Program MAIR0. */
122
123         adds r0, #4                         /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
124         ldr r1, =0xe000ed98                 /* r1 = 0xe000ed98 [Location of RNR]. */
125         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
126
127         movs r3, #4                         /* r3 = 4. */
128         str r3, [r1]                        /* Program RNR = 4. */
129         ldmia r0!, {r4-r11}                 /* Read 4 set of RBAR/RLAR registers from TCB. */
130         stmia r2, {r4-r11}                  /* Write 4 set of RBAR/RLAR registers using alias registers. */
131
132     #if ( configTOTAL_MPU_REGIONS == 16 )
133         movs r3, #8                         /* r3 = 8. */
134         str r3, [r1]                        /* Program RNR = 8. */
135         ldmia r0!, {r4-r11}                 /* Read 4 set of RBAR/RLAR registers from TCB. */
136         stmia r2, {r4-r11}                  /* Write 4 set of RBAR/RLAR registers using alias registers. */
137         movs r3, #12                        /* r3 = 12. */
138         str r3, [r1]                        /* Program RNR = 12. */
139         ldmia r0!, {r4-r11}                 /* Read 4 set of RBAR/RLAR registers from TCB. */
140         stmia r2, {r4-r11}                  /* Write 4 set of RBAR/RLAR registers using alias registers. */
141     #endif /* configTOTAL_MPU_REGIONS == 16 */
142
143         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
144         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
145         orr r2, #1                          /* r2 = r1 | 1 i.e. Set the bit 0 in r2. */
146         str r2, [r1]                        /* Enable MPU. */
147         dsb                                 /* Force memory writes before continuing. */
148
149     restore_context_first_task:
150         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
151         ldr r1, [r3]                        /* r1 = pxCurrentTCB.*/
152         ldr r2, [r1]                        /* r2 = Location of saved context in TCB. */
153
154     restore_special_regs_first_task:
155         ldmdb r2!, {r0, r3-r5, lr}          /* r0 = xSecureContext, r3 = original PSP, r4 = PSPLIM, r5 = CONTROL, LR restored. */
156         msr psp, r3
157         msr psplim, r4
158         msr control, r5
159         ldr r4, =xSecureContext             /* Read the location of xSecureContext i.e. &( xSecureContext ). */
160         str r0, [r4]                        /* Restore xSecureContext. */
161
162     restore_general_regs_first_task:
163         ldmdb r2!, {r4-r11}                 /* r4-r11 contain hardware saved context. */
164         stmia r3!, {r4-r11}                 /* Copy the hardware saved context on the task stack. */
165         ldmdb r2!, {r4-r11}                 /* r4-r11 restored. */
166
167     restore_context_done_first_task:
168         str r2, [r1]                        /* Save the location where the context should be saved next as the first member of TCB. */
169         mov r0, #0
170         msr basepri, r0                     /* Ensure that interrupts are enabled when the first task starts. */
171         bx lr
172
173 #else /* configENABLE_MPU */
174
175 vRestoreContextOfFirstTask:
176     ldr  r2, =pxCurrentTCB                  /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
177     ldr  r3, [r2]                           /* Read pxCurrentTCB. */
178     ldr  r0, [r3]                           /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
179
180     ldm  r0!, {r1-r3}                       /* Read from stack - r1 = xSecureContext, r2 = PSPLIM and r3 = EXC_RETURN. */
181     ldr  r4, =xSecureContext
182     str  r1, [r4]                           /* Set xSecureContext to this task's value for the same. */
183     msr  psplim, r2                         /* Set this task's PSPLIM value. */
184     mrs  r1, control                        /* Obtain current control register value. */
185     orrs r1, r1, #2                         /* r1 = r1 | 0x2 - Set the second bit to use the program stack pointe (PSP). */
186     msr control, r1                         /* Write back the new control register value. */
187     adds r0, #32                            /* Discard everything up to r0. */
188     msr  psp, r0                            /* This is now the new top of stack to use in the task. */
189     isb
190     mov  r0, #0
191     msr  basepri, r0                        /* Ensure that interrupts are enabled when the first task starts. */
192     bx   r3                                 /* Finally, branch to EXC_RETURN. */
193
194 #endif /* configENABLE_MPU */
195 /*-----------------------------------------------------------*/
196
197 vRaisePrivilege:
198     mrs  r0, control                        /* Read the CONTROL register. */
199     bic r0, r0, #1                          /* Clear the bit 0. */
200     msr  control, r0                        /* Write back the new CONTROL value. */
201     bx lr                                   /* Return to the caller. */
202 /*-----------------------------------------------------------*/
203
204 vStartFirstTask:
205     ldr r0, =0xe000ed08                     /* Use the NVIC offset register to locate the stack. */
206     ldr r0, [r0]                            /* Read the VTOR register which gives the address of vector table. */
207     ldr r0, [r0]                            /* The first entry in vector table is stack pointer. */
208     msr msp, r0                             /* Set the MSP back to the start of the stack. */
209     cpsie i                                 /* Globally enable interrupts. */
210     cpsie f
211     dsb
212     isb
213     svc 102                                 /* System call to start the first task. portSVC_START_SCHEDULER = 102. */
214 /*-----------------------------------------------------------*/
215
216 ulSetInterruptMask:
217     mrs r0, basepri                         /* r0 = basepri. Return original basepri value. */
218     mov r1, #configMAX_SYSCALL_INTERRUPT_PRIORITY
219     msr basepri, r1                         /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
220     dsb
221     isb
222     bx lr                                   /* Return. */
223 /*-----------------------------------------------------------*/
224
225 vClearInterruptMask:
226     msr basepri, r0                         /* basepri = ulMask. */
227     dsb
228     isb
229     bx lr                                   /* Return. */
230 /*-----------------------------------------------------------*/
231
232 #if ( configENABLE_MPU == 1 )
233
234 PendSV_Handler:
235     ldr r3, =xSecureContext                 /* Read the location of xSecureContext i.e. &( xSecureContext ). */
236     ldr r0, [r3]                            /* Read xSecureContext - Value of xSecureContext must be in r0 as it is used as a parameter later. */
237     ldr r3, =pxCurrentTCB                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
238     ldr r1, [r3]                            /* Read pxCurrentTCB - Value of pxCurrentTCB must be in r1 as it is used as a parameter later. */
239     ldr r2, [r1]                            /* r2 = Location in TCB where the context should be saved. */
240
241     cbz r0, save_ns_context                 /* No secure context to save. */
242     save_s_context:
243         push {r0-r2, lr}
244         bl SecureContext_SaveContext        /* Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
245         pop {r0-r2, lr}
246
247     save_ns_context:
248         mov r3, lr                          /* r3 = LR (EXC_RETURN). */
249         lsls r3, r3, #25                    /* 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. */
250         bmi save_special_regs               /* r3 < 0 ==> Bit[6] in EXC_RETURN is 1 ==> secure stack was used to store the stack frame. */
251
252     save_general_regs:
253         mrs r3, psp
254
255     #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
256         add r3, r3, #0x20                   /* Move r3 to location where s0 is saved. */
257         tst lr, #0x10
258         ittt eq
259         vstmiaeq r2!, {s16-s31}             /* Store s16-s31. */
260         vldmiaeq r3, {s0-s16}               /* Copy hardware saved FP context into s0-s16. */
261         vstmiaeq r2!, {s0-s16}              /* Store hardware saved FP context. */
262         sub r3, r3, #0x20                   /* Set r3 back to the location of hardware saved context. */
263     #endif /* configENABLE_FPU || configENABLE_MVE */
264
265         stmia r2!, {r4-r11}                 /* Store r4-r11. */
266         ldmia r3, {r4-r11}                  /* Copy the hardware saved context into r4-r11. */
267         stmia r2!, {r4-r11}                 /* Store the hardware saved context. */
268
269     save_special_regs:
270         mrs r3, psp                         /* r3 = PSP. */
271         mrs r4, psplim                      /* r4 = PSPLIM. */
272         mrs r5, control                     /* r5 = CONTROL. */
273         stmia r2!, {r0, r3-r5, lr}          /* Store xSecureContext, original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
274         str r2, [r1]                        /* Save the location from where the context should be restored as the first member of TCB. */
275
276     select_next_task:
277         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
278         msr basepri, r0                     /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
279         dsb
280         isb
281         bl vTaskSwitchContext
282         mov r0, #0                          /* r0 = 0. */
283         msr basepri, r0                     /* Enable interrupts. */
284
285     program_mpu:
286         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
287         ldr r0, [r3]                        /* r0 = pxCurrentTCB.*/
288
289         dmb                                 /* Complete outstanding transfers before disabling MPU. */
290         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
291         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
292         bic r2, #1                          /* r2 = r2 & ~1 i.e. Clear the bit 0 in r2. */
293         str r2, [r1]                        /* Disable MPU. */
294
295         adds r0, #4                         /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
296         ldr r1, [r0]                        /* r1 = *r0 i.e. r1 = MAIR0. */
297         ldr r2, =0xe000edc0                 /* r2 = 0xe000edc0 [Location of MAIR0]. */
298         str r1, [r2]                        /* Program MAIR0. */
299
300         adds r0, #4                         /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
301         ldr r1, =0xe000ed98                 /* r1 = 0xe000ed98 [Location of RNR]. */
302         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
303
304         movs r3, #4                         /* r3 = 4. */
305         str r3, [r1]                        /* Program RNR = 4. */
306         ldmia r0!, {r4-r11}                 /* Read 4 sets of RBAR/RLAR registers from TCB. */
307         stmia r2, {r4-r11}                  /* Write 4 set of RBAR/RLAR registers using alias registers. */
308
309     #if ( configTOTAL_MPU_REGIONS == 16 )
310         movs r3, #8                         /* r3 = 8. */
311         str r3, [r1]                        /* Program RNR = 8. */
312         ldmia r0!, {r4-r11}                 /* Read 4 sets of RBAR/RLAR registers from TCB. */
313         stmia r2, {r4-r11}                  /* Write 4 set of RBAR/RLAR registers using alias registers. */
314         movs r3, #12                        /* r3 = 12. */
315         str r3, [r1]                        /* Program RNR = 12. */
316         ldmia r0!, {r4-r11}                 /* Read 4 sets of RBAR/RLAR registers from TCB. */
317         stmia r2, {r4-r11}                  /* Write 4 set of RBAR/RLAR registers using alias registers. */
318     #endif /* configTOTAL_MPU_REGIONS == 16 */
319
320        ldr r1, =0xe000ed94                  /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
321        ldr r2, [r1]                         /* Read the value of MPU_CTRL. */
322        orr r2, #1                           /* r2 = r2 | 1 i.e. Set the bit 0 in r2. */
323        str r2, [r1]                         /* Enable MPU. */
324        dsb                                  /* Force memory writes before continuing. */
325
326     restore_context:
327         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
328         ldr r1, [r3]                        /* r1 = pxCurrentTCB.*/
329         ldr r2, [r1]                        /* r2 = Location of saved context in TCB. */
330
331     restore_special_regs:
332         ldmdb r2!, {r0, r3-r5, lr}          /* r0 = xSecureContext, r3 = original PSP, r4 = PSPLIM, r5 = CONTROL, LR restored. */
333         msr psp, r3
334         msr psplim, r4
335         msr control, r5
336         ldr r4, =xSecureContext             /* Read the location of xSecureContext i.e. &( xSecureContext ). */
337         str r0, [r4]                        /* Restore xSecureContext. */
338         cbz r0, restore_ns_context          /* No secure context to restore. */
339
340     restore_s_context:
341         push {r1-r3, lr}
342         bl SecureContext_LoadContext        /* Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
343         pop {r1-r3, lr}
344
345     restore_ns_context:
346         mov r0, lr                          /* r0 = LR (EXC_RETURN). */
347         lsls r0, r0, #25                    /* 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. */
348         bmi restore_context_done            /* r0 < 0 ==> Bit[6] in EXC_RETURN is 1 ==> secure stack was used to store the stack frame. */
349
350     restore_general_regs:
351         ldmdb r2!, {r4-r11}                 /* r4-r11 contain hardware saved context. */
352         stmia r3!, {r4-r11}                 /* Copy the hardware saved context on the task stack. */
353         ldmdb r2!, {r4-r11}                 /* r4-r11 restored. */
354
355     #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
356         tst lr, #0x10
357         ittt eq
358         vldmdbeq r2!, {s0-s16}              /* s0-s16 contain hardware saved FP context. */
359         vstmiaeq r3!, {s0-s16}              /* Copy hardware saved FP context on the task stack. */
360         vldmdbeq r2!, {s16-s31}             /* Restore s16-s31. */
361     #endif /* configENABLE_FPU || configENABLE_MVE */
362
363     restore_context_done:
364         str r2, [r1]                        /* Save the location where the context should be saved next as the first member of TCB. */
365         bx lr
366
367 #else /* configENABLE_MPU */
368
369 PendSV_Handler:
370     ldr r3, =xSecureContext                 /* Read the location of xSecureContext i.e. &( xSecureContext ). */
371     ldr r0, [r3]                            /* Read xSecureContext - Value of xSecureContext must be in r0 as it is used as a parameter later. */
372     ldr r3, =pxCurrentTCB                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
373     ldr r1, [r3]                            /* Read pxCurrentTCB - Value of pxCurrentTCB must be in r1 as it is used as a parameter later. */
374     mrs r2, psp                             /* Read PSP in r2. */
375
376     cbz r0, save_ns_context                 /* No secure context to save. */
377     push {r0-r2, r14}
378     bl SecureContext_SaveContext            /* Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
379     pop {r0-r3}                             /* LR is now in r3. */
380     mov lr, r3                              /* LR = r3. */
381     lsls r1, r3, #25                        /* 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. */
382     bpl save_ns_context                     /* bpl - branch if positive or zero. If r1 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */
383
384     ldr r3, =pxCurrentTCB                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
385     ldr r1, [r3]                            /* Read pxCurrentTCB. */
386     subs r2, r2, #12                        /* Make space for xSecureContext, PSPLIM and LR on the stack. */
387     str r2, [r1]                            /* Save the new top of stack in TCB. */
388     mrs r1, psplim                          /* r1 = PSPLIM. */
389     mov r3, lr                              /* r3 = LR/EXC_RETURN. */
390     stmia r2!, {r0, r1, r3}                 /* Store xSecureContext, PSPLIM and LR on the stack. */
391     b select_next_task
392
393     save_ns_context:
394         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
395         ldr r1, [r3]                        /* Read pxCurrentTCB. */
396     #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
397         tst lr, #0x10                       /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
398         it eq
399         vstmdbeq r2!, {s16-s31}             /* Store the additional FP context registers which are not saved automatically. */
400     #endif /* configENABLE_FPU || configENABLE_MVE */
401         subs r2, r2, #44                    /* Make space for xSecureContext, PSPLIM, LR and the remaining registers on the stack. */
402         str r2, [r1]                        /* Save the new top of stack in TCB. */
403         adds r2, r2, #12                    /* r2 = r2 + 12. */
404         stm r2, {r4-r11}                    /* Store the registers that are not saved automatically. */
405         mrs r1, psplim                      /* r1 = PSPLIM. */
406         mov r3, lr                          /* r3 = LR/EXC_RETURN. */
407         subs r2, r2, #12                    /* r2 = r2 - 12. */
408         stmia r2!, {r0, r1, r3}             /* Store xSecureContext, PSPLIM and LR on the stack. */
409
410     select_next_task:
411         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
412         msr basepri, r0                     /* Disable interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY. */
413         dsb
414         isb
415         bl vTaskSwitchContext
416         mov r0, #0                          /* r0 = 0. */
417         msr basepri, r0                     /* Enable interrupts. */
418
419         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
420         ldr r1, [r3]                        /* Read pxCurrentTCB. */
421         ldr r2, [r1]                        /* The first item in pxCurrentTCB is the task top of stack. r2 now points to the top of stack. */
422
423         ldmia r2!, {r0, r1, r4}             /* Read from stack - r0 = xSecureContext, r1 = PSPLIM and r4 = LR. */
424         msr psplim, r1                      /* Restore the PSPLIM register value for the task. */
425         mov lr, r4                          /* LR = r4. */
426         ldr r3, =xSecureContext             /* Read the location of xSecureContext i.e. &( xSecureContext ). */
427         str r0, [r3]                        /* Restore the task's xSecureContext. */
428         cbz r0, restore_ns_context          /* If there is no secure context for the task, restore the non-secure context. */
429         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
430         ldr r1, [r3]                        /* Read pxCurrentTCB. */
431         push {r2, r4}
432         bl SecureContext_LoadContext        /* Restore the secure context. Params are in r0 and r1. r0 = xSecureContext and r1 = pxCurrentTCB. */
433         pop {r2, r4}
434         mov lr, r4                          /* LR = r4. */
435         lsls r1, r4, #25                    /* 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. */
436         bpl restore_ns_context              /* bpl - branch if positive or zero. If r1 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */
437         msr psp, r2                         /* Remember the new top of stack for the task. */
438         bx lr
439
440     restore_ns_context:
441         ldmia r2!, {r4-r11}                 /* Restore the registers that are not automatically restored. */
442     #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
443         tst lr, #0x10                       /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the Extended Stack Frame is in use. */
444         it eq
445         vldmiaeq r2!, {s16-s31}             /* Restore the additional FP context registers which are not restored automatically. */
446     #endif /* configENABLE_FPU || configENABLE_MVE */
447         msr psp, r2                         /* Remember the new top of stack for the task. */
448         bx lr
449
450 #endif /* configENABLE_MPU */
451 /*-----------------------------------------------------------*/
452
453 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
454
455 SVC_Handler:
456     tst lr, #4
457     ite eq
458     mrseq r0, msp
459     mrsne r0, psp
460
461     ldr r1, [r0, #24]
462     ldrb r2, [r1, #-2]
463     cmp r2, #NUM_SYSTEM_CALLS
464     blt syscall_enter
465     cmp r2, #104            /* portSVC_SYSTEM_CALL_EXIT. */
466     beq syscall_exit
467     b vPortSVCHandler_C
468
469     syscall_enter:
470         mov r1, lr
471         b vSystemCallEnter
472
473     syscall_exit:
474         mov r1, lr
475         b vSystemCallExit
476
477 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
478
479 SVC_Handler:
480     tst lr, #4
481     ite eq
482     mrseq r0, msp
483     mrsne r0, psp
484     b vPortSVCHandler_C
485
486 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
487 /*-----------------------------------------------------------*/
488
489 vPortFreeSecureContext:
490     /* r0 = uint32_t *pulTCB. */
491     ldr r2, [r0]                            /* The first item in the TCB is the top of the stack. */
492     ldr r1, [r2]                            /* The first item on the stack is the task's xSecureContext. */
493     cmp r1, #0                              /* Raise svc if task's xSecureContext is not NULL. */
494     it ne
495     svcne 101                               /* Secure context is freed in the supervisor call. portSVC_FREE_SECURE_CONTEXT = 101. */
496     bx lr                                   /* Return. */
497 /*-----------------------------------------------------------*/
498
499     END