]> begriffs open source - freertos/blob - portable/GCC/ARM_CM33/non_secure/portasm.c
Style: uncrustify kernel files
[freertos] / portable / GCC / ARM_CM33 / non_secure / portasm.c
1 /*\r
2  * FreeRTOS Kernel V10.3.1\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  */\r
26 \r
27 /* Standard includes. */\r
28 #include <stdint.h>\r
29 \r
30 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE ensures that PRIVILEGED_FUNCTION\r
31  * is defined correctly and privileged functions are placed in correct sections. */\r
32 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
33 \r
34 /* Portasm includes. */\r
35 #include "portasm.h"\r
36 \r
37 /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE is needed to be defined only for the\r
38  * header files. */\r
39 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
40 \r
41 void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */\r
42 {\r
43         __asm volatile\r
44         (\r
45         "       .syntax unified                                                                 \n"\r
46         "                                                                                                       \n"\r
47         "       ldr  r2, pxCurrentTCBConst2                                             \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
48         "       ldr  r3, [r2]                                                                   \n" /* Read pxCurrentTCB. */\r
49         "       ldr  r0, [r3]                                                                   \n" /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */\r
50         "                                                                                                       \n"\r
51         #if( configENABLE_MPU == 1 )\r
52         "       dmb                                                                                             \n" /* Complete outstanding transfers before disabling MPU. */\r
53         "       ldr r2, xMPUCTRLConst2                                                  \n" /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
54         "       ldr r4, [r2]                                                                    \n" /* Read the value of MPU_CTRL. */\r
55         "       bic r4, #1                                                                              \n" /* r4 = r4 & ~1 i.e. Clear the bit 0 in r4. */\r
56         "       str r4, [r2]                                                                    \n" /* Disable MPU. */\r
57         "                                                                                                       \n"\r
58         "       adds r3, #4                                                                             \n" /* r3 = r3 + 4. r3 now points to MAIR0 in TCB. */\r
59         "       ldr  r4, [r3]                                                                   \n" /* r4 = *r3 i.e. r4 = MAIR0. */\r
60         "       ldr  r2, xMAIR0Const2                                                   \n" /* r2 = 0xe000edc0 [Location of MAIR0]. */\r
61         "       str  r4, [r2]                                                                   \n" /* Program MAIR0. */\r
62         "       ldr  r2, xRNRConst2                                                             \n" /* r2 = 0xe000ed98 [Location of RNR]. */\r
63         "       movs r4, #4                                                                             \n" /* r4 = 4. */\r
64         "       str  r4, [r2]                                                                   \n" /* Program RNR = 4. */\r
65         "       adds r3, #4                                                                             \n" /* r3 = r3 + 4. r3 now points to first RBAR in TCB. */\r
66         "       ldr  r2, xRBARConst2                                                    \n" /* r2 = 0xe000ed9c [Location of RBAR]. */\r
67         "       ldmia r3!, {r4-r11}                                                             \n" /* Read 4 set of RBAR/RLAR registers from TCB. */\r
68         "       stmia r2!, {r4-r11}                                                             \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */\r
69         "                                                                                                       \n"\r
70         "       ldr r2, xMPUCTRLConst2                                                  \n" /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
71         "       ldr r4, [r2]                                                                    \n" /* Read the value of MPU_CTRL. */\r
72         "       orr r4, #1                                                                              \n" /* r4 = r4 | 1 i.e. Set the bit 0 in r4. */\r
73         "       str r4, [r2]                                                                    \n" /* Enable MPU. */\r
74         "       dsb                                                                                             \n" /* Force memory writes before continuing. */\r
75         #endif /* configENABLE_MPU */\r
76         "                                                                                                       \n"\r
77         #if( configENABLE_MPU == 1 )\r
78         "       ldm  r0!, {r1-r4}                                                               \n" /* Read from stack - r1 = xSecureContext, r2 = PSPLIM, r3 = CONTROL and r4 = EXC_RETURN. */\r
79         "       ldr  r5, xSecureContextConst2                                   \n"\r
80         "       str  r1, [r5]                                                                   \n" /* Set xSecureContext to this task's value for the same. */\r
81         "       msr  psplim, r2                                                                 \n" /* Set this task's PSPLIM value. */\r
82         "       msr  control, r3                                                                \n" /* Set this task's CONTROL value. */\r
83         "       adds r0, #32                                                                    \n" /* Discard everything up to r0. */\r
84         "       msr  psp, r0                                                                    \n" /* This is now the new top of stack to use in the task. */\r
85         "       isb                                                                                             \n"\r
86         "       bx   r4                                                                                 \n" /* Finally, branch to EXC_RETURN. */\r
87         #else /* configENABLE_MPU */\r
88         "       ldm  r0!, {r1-r3}                                                               \n" /* Read from stack - r1 = xSecureContext, r2 = PSPLIM and r3 = EXC_RETURN. */\r
89         "       ldr  r4, xSecureContextConst2                                   \n"\r
90         "       str  r1, [r4]                                                                   \n" /* Set xSecureContext to this task's value for the same. */\r
91         "       msr  psplim, r2                                                                 \n" /* Set this task's PSPLIM value. */\r
92         "       movs r1, #2                                                                             \n" /* r1 = 2. */\r
93         "       msr  CONTROL, r1                                                                \n" /* Switch to use PSP in the thread mode. */\r
94         "       adds r0, #32                                                                    \n" /* Discard everything up to r0. */\r
95         "       msr  psp, r0                                                                    \n" /* This is now the new top of stack to use in the task. */\r
96         "       isb                                                                                             \n"\r
97         "       bx   r3                                                                                 \n" /* Finally, branch to EXC_RETURN. */\r
98         #endif /* configENABLE_MPU */\r
99         "                                                                                                       \n"\r
100         "       .align 4                                                                                \n"\r
101         "pxCurrentTCBConst2: .word pxCurrentTCB                         \n"\r
102         "xSecureContextConst2: .word xSecureContext                     \n"\r
103         #if( configENABLE_MPU == 1 )\r
104         "xMPUCTRLConst2: .word 0xe000ed94                                       \n"\r
105         "xMAIR0Const2: .word 0xe000edc0                                         \n"\r
106         "xRNRConst2: .word 0xe000ed98                                           \n"\r
107         "xRBARConst2: .word 0xe000ed9c                                          \n"\r
108         #endif /* configENABLE_MPU */\r
109         );\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */\r
114 {\r
115         __asm volatile\r
116         (\r
117         "       mrs r0, control                                                                 \n" /* r0 = CONTROL. */\r
118         "       tst r0, #1                                                                              \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */\r
119         "       ite ne                                                                                  \n"\r
120         "       movne r0, #0                                                                    \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */\r
121         "       moveq r0, #1                                                                    \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */\r
122         "       bx lr                                                                                   \n" /* Return. */\r
123         "                                                                                                       \n"\r
124         "       .align 4                                                                                \n"\r
125         ::: "r0", "memory"\r
126         );\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 void vRaisePrivilege( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */\r
131 {\r
132          __asm volatile\r
133         (\r
134         "       mrs r0, control                                                                 \n" /* Read the CONTROL register. */\r
135         "       bic r0, #1                                                                              \n" /* Clear the bit 0. */\r
136         "       msr control, r0                                                                 \n" /* Write back the new CONTROL value. */\r
137         "       bx lr                                                                                   \n" /* Return to the caller. */\r
138         ::: "r0", "memory"\r
139         );\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vResetPrivilege( void ) /* __attribute__ (( naked )) */\r
144 {\r
145         __asm volatile\r
146         (\r
147         "       mrs r0, control                                                                 \n" /* r0 = CONTROL. */\r
148         "       orr r0, #1                                                                              \n" /* r0 = r0 | 1. */\r
149         "       msr control, r0                                                                 \n" /* CONTROL = r0. */\r
150         "       bx lr                                                                                   \n" /* Return to the caller. */\r
151         :::"r0", "memory"\r
152         );\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 void vStartFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */\r
157 {\r
158         __asm volatile\r
159         (\r
160         "       ldr r0, xVTORConst                                                              \n" /* Use the NVIC offset register to locate the stack. */\r
161         "       ldr r0, [r0]                                                                    \n" /* Read the VTOR register which gives the address of vector table. */\r
162         "       ldr r0, [r0]                                                                    \n" /* The first entry in vector table is stack pointer. */\r
163         "       msr msp, r0                                                                             \n" /* Set the MSP back to the start of the stack. */\r
164         "       cpsie i                                                                                 \n" /* Globally enable interrupts. */\r
165         "       cpsie f                                                                                 \n"\r
166         "       dsb                                                                                             \n"\r
167         "       isb                                                                                             \n"\r
168         "       svc %0                                                                                  \n" /* System call to start the first task. */\r
169         "       nop                                                                                             \n"\r
170         "                                                                                                       \n"\r
171         "   .align 4                                                                            \n"\r
172         "xVTORConst: .word 0xe000ed08                                           \n"\r
173         :: "i" ( portSVC_START_SCHEDULER ) : "memory"\r
174         );\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 uint32_t ulSetInterruptMask( void ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */\r
179 {\r
180         __asm volatile\r
181         (\r
182         "       mrs r0, basepri                                                                 \n" /* r0 = basepri. Return original basepri value. */\r
183         "       mov r1, %0                                                                              \n" /* r1 = configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
184         "       msr basepri, r1                                                                 \n" /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
185         "       dsb                                                                                             \n"\r
186         "       isb                                                                                             \n"\r
187         "       bx lr                                                                                   \n" /* Return. */\r
188         :: "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"\r
189         );\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 void vClearInterruptMask( __attribute__( ( unused ) ) uint32_t ulMask ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */\r
194 {\r
195         __asm volatile\r
196         (\r
197         "       msr basepri, r0                                                                 \n" /* basepri = ulMask. */\r
198         "       dsb                                                                                             \n"\r
199         "       isb                                                                                             \n"\r
200         "       bx lr                                                                                   \n" /* Return. */\r
201         ::: "memory"\r
202         );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void PendSV_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */\r
207 {\r
208         __asm volatile\r
209         (\r
210         "       .syntax unified                                                                 \n"\r
211         "       .extern SecureContext_SaveContext                               \n"\r
212         "       .extern SecureContext_LoadContext                               \n"\r
213         "                                                                                                       \n"\r
214         "       mrs r1, psp                                                                             \n" /* Read PSP in r1. */\r
215         "       ldr r2, xSecureContextConst                                             \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */\r
216         "       ldr r0, [r2]                                                                    \n" /* Read xSecureContext - Value of xSecureContext must be in r0 as it is used as a parameter later. */\r
217         "                                                                                                       \n"\r
218         "       cbz r0, save_ns_context                                                 \n" /* No secure context to save. */\r
219         "       push {r0-r2, r14}                                                               \n"\r
220         "       bl SecureContext_SaveContext                                    \n"\r
221         "       pop {r0-r3}                                                                             \n" /* LR is now in r3. */\r
222         "       mov lr, r3                                                                              \n" /* LR = r3. */\r
223         "       lsls r2, r3, #25                                                                \n" /* r2 = 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. */\r
224         "       bpl save_ns_context                                                             \n" /* bpl - branch if positive or zero. If r2 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */\r
225         "       ldr r3, pxCurrentTCBConst                                               \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
226         "       ldr r2, [r3]                                                                    \n" /* Read pxCurrentTCB. */\r
227         #if( configENABLE_MPU == 1 )\r
228         "       subs r1, r1, #16                                                                \n" /* Make space for xSecureContext, PSPLIM, CONTROL and LR on the stack. */\r
229         "       str r1, [r2]                                                                    \n" /* Save the new top of stack in TCB. */\r
230         "       mrs r2, psplim                                                                  \n" /* r2 = PSPLIM. */\r
231         "       mrs r3, control                                                                 \n" /* r3 = CONTROL. */\r
232         "       mov r4, lr                                                                              \n" /* r4 = LR/EXC_RETURN. */\r
233         "       stmia r1!, {r0, r2-r4}                                                  \n" /* Store xSecureContext, PSPLIM, CONTROL and LR on the stack. */\r
234         #else /* configENABLE_MPU */\r
235         "       subs r1, r1, #12                                                                \n" /* Make space for xSecureContext, PSPLIM and LR on the stack. */\r
236         "       str r1, [r2]                                                                    \n" /* Save the new top of stack in TCB. */\r
237         "       mrs r2, psplim                                                                  \n" /* r2 = PSPLIM. */\r
238         "       mov r3, lr                                                                              \n" /* r3 = LR/EXC_RETURN. */\r
239         "       stmia r1!, {r0, r2-r3}                                                  \n" /* Store xSecureContext, PSPLIM and LR on the stack. */\r
240         #endif /* configENABLE_MPU */\r
241         "       b select_next_task                                                              \n"\r
242         "                                                                                                       \n"\r
243         " save_ns_context:                                                                      \n"\r
244         "       ldr r3, pxCurrentTCBConst                                               \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
245         "       ldr r2, [r3]                                                                    \n" /* Read pxCurrentTCB. */\r
246         #if( configENABLE_FPU == 1 )\r
247         "       tst lr, #0x10                                                                   \n" /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */\r
248         "       it eq                                                                                   \n"\r
249         "       vstmdbeq r1!, {s16-s31}                                                 \n" /* Store the FPU registers which are not saved automatically. */\r
250         #endif /* configENABLE_FPU */\r
251         #if( configENABLE_MPU == 1 )\r
252         "       subs r1, r1, #48                                                                \n" /* Make space for xSecureContext, PSPLIM, CONTROL, LR and the remaining registers on the stack. */\r
253         "       str r1, [r2]                                                                    \n" /* Save the new top of stack in TCB. */\r
254         "       adds r1, r1, #16                                                                \n" /* r1 = r1 + 16. */\r
255         "       stm r1, {r4-r11}                                                                \n" /* Store the registers that are not saved automatically. */\r
256         "       mrs r2, psplim                                                                  \n" /* r2 = PSPLIM. */\r
257         "       mrs r3, control                                                                 \n" /* r3 = CONTROL. */\r
258         "       mov r4, lr                                                                              \n" /* r4 = LR/EXC_RETURN. */\r
259         "       subs r1, r1, #16                                                                \n" /* r1 = r1 - 16. */\r
260         "       stm r1, {r0, r2-r4}                                                             \n" /* Store xSecureContext, PSPLIM, CONTROL and LR on the stack. */\r
261         #else /* configENABLE_MPU */\r
262         "       subs r1, r1, #44                                                                \n" /* Make space for xSecureContext, PSPLIM, LR and the remaining registers on the stack. */\r
263         "       str r1, [r2]                                                                    \n" /* Save the new top of stack in TCB. */\r
264         "       adds r1, r1, #12                                                                \n" /* r1 = r1 + 12. */\r
265         "       stm r1, {r4-r11}                                                                \n" /* Store the registers that are not saved automatically. */\r
266         "       mrs r2, psplim                                                                  \n" /* r2 = PSPLIM. */\r
267         "       mov r3, lr                                                                              \n" /* r3 = LR/EXC_RETURN. */\r
268         "       subs r1, r1, #12                                                                \n" /* r1 = r1 - 12. */\r
269         "       stmia r1!, {r0, r2-r3}                                                  \n" /* Store xSecureContext, PSPLIM and LR on the stack. */\r
270         #endif /* configENABLE_MPU */\r
271         "                                                                                                       \n"\r
272         " select_next_task:                                                                     \n"\r
273         "       mov r0, %0                                                                              \n" /* r0 = configMAX_SYSCALL_INTERRUPT_PRIORITY */\r
274         "       msr basepri, r0                                                                 \n" /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
275         "       dsb                                                                                             \n"\r
276         "       isb                                                                                             \n"\r
277         "       bl vTaskSwitchContext                                                   \n"\r
278         "       mov r0, #0                                                                              \n" /* r0 = 0. */\r
279         "       msr basepri, r0                                                                 \n" /* Enable interrupts. */\r
280         "                                                                                                       \n"\r
281         "       ldr r2, pxCurrentTCBConst                                               \n" /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
282         "       ldr r3, [r2]                                                                    \n" /* Read pxCurrentTCB. */\r
283         "       ldr r1, [r3]                                                                    \n" /* The first item in pxCurrentTCB is the task top of stack. r1 now points to the top of stack. */\r
284         "                                                                                                       \n"\r
285         #if( configENABLE_MPU == 1 )\r
286         "       dmb                                                                                             \n" /* Complete outstanding transfers before disabling MPU. */\r
287         "       ldr r2, xMPUCTRLConst                                                   \n" /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
288         "       ldr r4, [r2]                                                                    \n" /* Read the value of MPU_CTRL. */\r
289         "       bic r4, #1                                                                              \n" /* r4 = r4 & ~1 i.e. Clear the bit 0 in r4. */\r
290         "       str r4, [r2]                                                                    \n" /* Disable MPU. */\r
291         "                                                                                                       \n"\r
292         "       adds r3, #4                                                                             \n" /* r3 = r3 + 4. r3 now points to MAIR0 in TCB. */\r
293         "       ldr r4, [r3]                                                                    \n" /* r4 = *r3 i.e. r4 = MAIR0. */\r
294         "       ldr r2, xMAIR0Const                                                             \n" /* r2 = 0xe000edc0 [Location of MAIR0]. */\r
295         "       str r4, [r2]                                                                    \n" /* Program MAIR0. */\r
296         "       ldr r2, xRNRConst                                                               \n" /* r2 = 0xe000ed98 [Location of RNR]. */\r
297         "       movs r4, #4                                                                             \n" /* r4 = 4. */\r
298         "       str r4, [r2]                                                                    \n" /* Program RNR = 4. */\r
299         "       adds r3, #4                                                                             \n" /* r3 = r3 + 4. r3 now points to first RBAR in TCB. */\r
300         "       ldr  r2, xRBARConst                                                             \n" /* r2 = 0xe000ed9c [Location of RBAR]. */\r
301         "       ldmia r3!, {r4-r11}                                                             \n" /* Read 4 sets of RBAR/RLAR registers from TCB. */\r
302         "       stmia r2!, {r4-r11}                                                             \n" /* Write 4 set of RBAR/RLAR registers using alias registers. */\r
303         "                                                                                                       \n"\r
304         "       ldr r2, xMPUCTRLConst                                                   \n" /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
305         "       ldr r4, [r2]                                                                    \n" /* Read the value of MPU_CTRL. */\r
306         "       orr r4, #1                                                                              \n" /* r4 = r4 | 1 i.e. Set the bit 0 in r4. */\r
307         "       str r4, [r2]                                                                    \n" /* Enable MPU. */\r
308         "       dsb                                                                                             \n" /* Force memory writes before continuing. */\r
309         #endif /* configENABLE_MPU */\r
310         "                                                                                                       \n"\r
311         #if( configENABLE_MPU == 1 )\r
312         "       ldmia r1!, {r0, r2-r4}                                                  \n" /* Read from stack - r0 = xSecureContext, r2 = PSPLIM, r3 = CONTROL and r4 = LR. */\r
313         "       msr psplim, r2                                                                  \n" /* Restore the PSPLIM register value for the task. */\r
314         "       msr control, r3                                                                 \n" /* Restore the CONTROL register value for the task. */\r
315         "       mov lr, r4                                                                              \n" /* LR = r4. */\r
316         "       ldr r2, xSecureContextConst                                             \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */\r
317         "       str r0, [r2]                                                                    \n" /* Restore the task's xSecureContext. */\r
318         "       cbz r0, restore_ns_context                                              \n" /* If there is no secure context for the task, restore the non-secure context. */\r
319         "       push {r1,r4}                                                                    \n"\r
320         "       bl SecureContext_LoadContext                                    \n" /* Restore the secure context. */\r
321         "       pop {r1,r4}                                                                             \n"\r
322         "       mov lr, r4                                                                              \n" /* LR = r4. */\r
323         "       lsls r2, r4, #25                                                                \n" /* r2 = 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. */\r
324         "       bpl restore_ns_context                                                  \n" /* bpl - branch if positive or zero. If r2 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */\r
325         "       msr psp, r1                                                                             \n" /* Remember the new top of stack for the task. */\r
326         "       bx lr                                                                                   \n"\r
327         #else /* configENABLE_MPU */\r
328         "       ldmia r1!, {r0, r2-r3}                                                  \n" /* Read from stack - r0 = xSecureContext, r2 = PSPLIM and r3 = LR. */\r
329         "       msr psplim, r2                                                                  \n" /* Restore the PSPLIM register value for the task. */\r
330         "       mov lr, r3                                                                              \n" /* LR = r3. */\r
331         "       ldr r2, xSecureContextConst                                             \n" /* Read the location of xSecureContext i.e. &( xSecureContext ). */\r
332         "       str r0, [r2]                                                                    \n" /* Restore the task's xSecureContext. */\r
333         "       cbz r0, restore_ns_context                                              \n" /* If there is no secure context for the task, restore the non-secure context. */\r
334         "       push {r1,r3}                                                                    \n"\r
335         "       bl SecureContext_LoadContext                                    \n" /* Restore the secure context. */\r
336         "       pop {r1,r3}                                                                             \n"\r
337         "       mov lr, r3                                                                              \n" /* LR = r3. */\r
338         "       lsls r2, r3, #25                                                                \n" /* r2 = 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. */\r
339         "       bpl restore_ns_context                                                  \n" /* bpl - branch if positive or zero. If r2 >= 0 ==> Bit[6] in EXC_RETURN is 0 i.e. non-secure stack was used. */\r
340         "       msr psp, r1                                                                             \n" /* Remember the new top of stack for the task. */\r
341         "       bx lr                                                                                   \n"\r
342         #endif /* configENABLE_MPU */\r
343         "                                                                                                       \n"\r
344         " restore_ns_context:                                                           \n"\r
345         "       ldmia r1!, {r4-r11}                                                             \n" /* Restore the registers that are not automatically restored. */\r
346         #if( configENABLE_FPU == 1 )\r
347         "       tst lr, #0x10                                                                   \n" /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */\r
348         "       it eq                                                                                   \n"\r
349         "       vldmiaeq r1!, {s16-s31}                                                 \n" /* Restore the FPU registers which are not restored automatically. */\r
350         #endif /* configENABLE_FPU */\r
351         "       msr psp, r1                                                                             \n" /* Remember the new top of stack for the task. */\r
352         "       bx lr                                                                                   \n"\r
353         "                                                                                                       \n"\r
354         "       .align 4                                                                                \n"\r
355         "pxCurrentTCBConst: .word pxCurrentTCB                          \n"\r
356         "xSecureContextConst: .word xSecureContext                      \n"\r
357         #if( configENABLE_MPU == 1 )\r
358         "xMPUCTRLConst: .word 0xe000ed94                                        \n"\r
359         "xMAIR0Const: .word 0xe000edc0                                          \n"\r
360         "xRNRConst: .word 0xe000ed98                                            \n"\r
361         "xRBARConst: .word 0xe000ed9c                                           \n"\r
362         #endif /* configENABLE_MPU */\r
363         :: "i"( configMAX_SYSCALL_INTERRUPT_PRIORITY )\r
364         );\r
365 }\r
366 /*-----------------------------------------------------------*/\r
367 \r
368 void SVC_Handler( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */\r
369 {\r
370         __asm volatile\r
371         (\r
372         "       tst lr, #4                                                                              \n"\r
373         "       ite eq                                                                                  \n"\r
374         "       mrseq r0, msp                                                                   \n"\r
375         "       mrsne r0, psp                                                                   \n"\r
376         "       ldr r1, svchandler_address_const                                \n"\r
377         "       bx r1                                                                                   \n"\r
378         "                                                                                                       \n"\r
379         "       .align 4                                                                                \n"\r
380         "svchandler_address_const: .word vPortSVCHandler_C      \n"\r
381         );\r
382 }\r
383 /*-----------------------------------------------------------*/\r
384 \r
385 void vPortAllocateSecureContext( uint32_t ulSecureStackSize ) /* __attribute__ (( naked )) */\r
386 {\r
387         __asm volatile\r
388         (\r
389         "       svc %0                                                                                  \n" /* Secure context is allocated in the supervisor call. */\r
390         "       bx lr                                                                                   \n" /* Return. */\r
391         :: "i" ( portSVC_ALLOCATE_SECURE_CONTEXT ) : "memory"\r
392         );\r
393 }\r
394 /*-----------------------------------------------------------*/\r
395 \r
396 void vPortFreeSecureContext( uint32_t *pulTCB ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */\r
397 {\r
398         __asm volatile\r
399         (\r
400         "       ldr r1, [r0]                                                                    \n" /* The first item in the TCB is the top of the stack. */\r
401         "       ldr r0, [r1]                                                                    \n" /* The first item on the stack is the task's xSecureContext. */\r
402         "       cmp r0, #0                                                                              \n" /* Raise svc if task's xSecureContext is not NULL. */\r
403         "       it ne                                                                                   \n"\r
404         "       svcne %0                                                                                \n" /* Secure context is freed in the supervisor call. */\r
405         "       bx lr                                                                                   \n" /* Return. */\r
406         :: "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"\r
407         );\r
408 }\r
409 /*-----------------------------------------------------------*/\r