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