]> begriffs open source - freertos/blob - portable/IAR/ARM_CM23_NTZ/non_secure/portasm.s
Update system call entry mechanism (#896)
[freertos] / portable / IAR / ARM_CM23_NTZ / non_secure / portasm.s
1 /*
2  * FreeRTOS Kernel V10.6.1
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 /* Including FreeRTOSConfig.h here will cause build errors if the header file
29 contains code not understood by the assembler - for example the 'extern' keyword.
30 To avoid errors place any such code inside a #ifdef __ICCARM__/#endif block so
31 the code is included in C files but excluded by the preprocessor in assembly
32 files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler. */
33 #include "FreeRTOSConfig.h"
34
35 /* System call numbers includes. */
36 #include "mpu_syscall_numbers.h"
37
38 #ifndef configUSE_MPU_WRAPPERS_V1
39     #define configUSE_MPU_WRAPPERS_V1 0
40 #endif
41
42     EXTERN pxCurrentTCB
43     EXTERN vTaskSwitchContext
44     EXTERN vPortSVCHandler_C
45 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
46     EXTERN vSystemCallEnter
47     EXTERN vSystemCallExit
48 #endif
49
50     PUBLIC xIsPrivileged
51     PUBLIC vResetPrivilege
52     PUBLIC vRestoreContextOfFirstTask
53     PUBLIC vRaisePrivilege
54     PUBLIC vStartFirstTask
55     PUBLIC ulSetInterruptMask
56     PUBLIC vClearInterruptMask
57     PUBLIC PendSV_Handler
58     PUBLIC SVC_Handler
59
60 #if ( configENABLE_FPU == 1 )
61     #error Cortex-M23 does not have a Floating Point Unit (FPU) and therefore configENABLE_FPU must be set to 0.
62 #endif
63 /*-----------------------------------------------------------*/
64
65 /*---------------- Unprivileged Functions -------------------*/
66
67 /*-----------------------------------------------------------*/
68
69     SECTION .text:CODE:NOROOT(2)
70     THUMB
71 /*-----------------------------------------------------------*/
72
73 xIsPrivileged:
74     mrs r0, control                         /* r0 = CONTROL. */
75     movs r1, #1                             /* r1 = 1. */
76     tst r0, r1                              /* Perform r0 & r1 (bitwise AND) and update the conditions flag. */
77     beq running_privileged                  /* If the result of previous AND operation was 0, branch. */
78     movs r0, #0                             /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
79     bx lr                                   /* Return. */
80     running_privileged:
81         movs r0, #1                         /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
82         bx lr                               /* Return. */
83
84 /*-----------------------------------------------------------*/
85
86 vResetPrivilege:
87     mrs r0, control                         /* r0 = CONTROL. */
88     movs r1, #1                             /* r1 = 1. */
89     orrs r0, r1                             /* r0 = r0 | r1. */
90     msr control, r0                         /* CONTROL = r0. */
91     bx lr                                   /* Return to the caller. */
92 /*-----------------------------------------------------------*/
93
94 /*----------------- Privileged Functions --------------------*/
95
96 /*-----------------------------------------------------------*/
97
98     SECTION privileged_functions:CODE:NOROOT(2)
99     THUMB
100 /*-----------------------------------------------------------*/
101
102 #if ( configENABLE_MPU == 1 )
103
104 vRestoreContextOfFirstTask:
105     program_mpu_first_task:
106         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
107         ldr r0, [r3]                        /* r0 = pxCurrentTCB.*/
108
109         dmb                                 /* Complete outstanding transfers before disabling MPU. */
110         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
111         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
112         movs r3, #1                         /* r3 = 1. */
113         bics r2, r3                         /* r2 = r2 & ~r3 i.e. Clear the bit 0 in r2. */
114         str r2, [r1]                        /* Disable MPU. */
115
116         adds r0, #4                         /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
117         ldr r1, [r0]                        /* r1 = *r0 i.e. r1 = MAIR0. */
118         ldr r2, =0xe000edc0                 /* r2 = 0xe000edc0 [Location of MAIR0]. */
119         str r1, [r2]                        /* Program MAIR0. */
120
121         adds r0, #4                         /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
122         ldr r1, =0xe000ed98                 /* r1 = 0xe000ed98 [Location of RNR]. */
123
124         movs r3, #4                         /* r3 = 4. */
125         str r3, [r1]                        /* Program RNR = 4. */
126         ldmia r0!, {r4-r5}                  /* Read first set of RBAR/RLAR registers from TCB. */
127         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
128         stmia r2!, {r4-r5}                  /* Write first set of RBAR/RLAR registers. */
129         movs r3, #5                         /* r3 = 5. */
130         str r3, [r1]                        /* Program RNR = 5. */
131         ldmia r0!, {r4-r5}                  /* Read second set of RBAR/RLAR registers from TCB. */
132         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
133         stmia r2!, {r4-r5}                  /* Write second set of RBAR/RLAR registers. */
134         movs r3, #6                         /* r3 = 6. */
135         str r3, [r1]                        /* Program RNR = 6. */
136         ldmia r0!, {r4-r5}                  /* Read third set of RBAR/RLAR registers from TCB. */
137         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
138         stmia r2!, {r4-r5}                  /* Write third set of RBAR/RLAR registers. */
139         movs r3, #7                         /* r3 = 6. */
140         str r3, [r1]                        /* Program RNR = 7. */
141         ldmia r0!, {r4-r5}                  /* Read fourth set of RBAR/RLAR registers from TCB. */
142         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
143         stmia r2!, {r4-r5}                  /* Write fourth set of RBAR/RLAR registers. */
144
145         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
146         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
147         movs r3, #1                         /* r3 = 1. */
148         orrs r2, r3                         /* r2 = r2 | r3 i.e. Set the bit 0 in r2. */
149         str r2, [r1]                        /* Enable MPU. */
150         dsb                                 /* Force memory writes before continuing. */
151
152     restore_context_first_task:
153         ldr r2, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
154         ldr r0, [r2]                        /* r0 = pxCurrentTCB.*/
155         ldr r1, [r0]                        /* r1 = Location of saved context in TCB. */
156
157     restore_special_regs_first_task:
158         subs r1, #16
159         ldmia r1!, {r2-r5}                  /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, r5 = LR. */
160         subs r1, #16
161         msr psp, r2
162         msr psplim, r3
163         msr control, r4
164         mov lr, r5
165
166     restore_general_regs_first_task:
167         subs r1, #32
168         ldmia r1!, {r4-r7}                  /* r4-r7 contain half of the hardware saved context. */
169         stmia r2!, {r4-r7}                  /* Copy half of the the hardware saved context on the task stack. */
170         ldmia r1!, {r4-r7}                  /* r4-r7 contain rest half of the hardware saved context. */
171         stmia r2!, {r4-r7}                  /* Copy rest half of the the hardware saved context on the task stack. */
172         subs r1, #48
173         ldmia r1!, {r4-r7}                  /* Restore r8-r11. */
174         mov r8, r4                          /* r8 = r4. */
175         mov r9, r5                          /* r9 = r5. */
176         mov r10, r6                         /* r10 = r6. */
177         mov r11, r7                         /* r11 = r7. */
178         subs r1, #32
179         ldmia r1!, {r4-r7}                  /* Restore r4-r7. */
180         subs r1, #16
181
182     restore_context_done_first_task:
183         str r1, [r0]                        /* Save the location where the context should be saved next as the first member of TCB. */
184         bx lr
185
186 #else /* configENABLE_MPU */
187
188 vRestoreContextOfFirstTask:
189     ldr  r2, =pxCurrentTCB                  /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
190     ldr  r1, [r2]                           /* Read pxCurrentTCB. */
191     ldr  r0, [r1]                           /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
192
193     ldm  r0!, {r1-r2}                       /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
194     msr  psplim, r1                         /* Set this task's PSPLIM value. */
195     movs r1, #2                             /* r1 = 2. */
196     msr  CONTROL, r1                        /* Switch to use PSP in the thread mode. */
197     adds r0, #32                            /* Discard everything up to r0. */
198     msr  psp, r0                            /* This is now the new top of stack to use in the task. */
199     isb
200     bx   r2                                 /* Finally, branch to EXC_RETURN. */
201
202 #endif /* configENABLE_MPU */
203 /*-----------------------------------------------------------*/
204
205 vRaisePrivilege:
206     mrs  r0, control                        /* Read the CONTROL register. */
207     movs r1, #1                             /* r1 = 1. */
208     bics r0, r1                             /* Clear the bit 0. */
209     msr  control, r0                        /* Write back the new CONTROL value. */
210     bx lr                                   /* Return to the caller. */
211 /*-----------------------------------------------------------*/
212
213 vStartFirstTask:
214     ldr r0, =0xe000ed08                     /* Use the NVIC offset register to locate the stack. */
215     ldr r0, [r0]                            /* Read the VTOR register which gives the address of vector table. */
216     ldr r0, [r0]                            /* The first entry in vector table is stack pointer. */
217     msr msp, r0                             /* Set the MSP back to the start of the stack. */
218     cpsie i                                 /* Globally enable interrupts. */
219     dsb
220     isb
221     svc 102                                 /* System call to start the first task. portSVC_START_SCHEDULER = 102. */
222     nop
223 /*-----------------------------------------------------------*/
224
225 ulSetInterruptMask:
226     mrs r0, PRIMASK
227     cpsid i
228     bx lr
229 /*-----------------------------------------------------------*/
230
231 vClearInterruptMask:
232     msr PRIMASK, r0
233     bx lr
234 /*-----------------------------------------------------------*/
235
236 #if ( configENABLE_MPU == 1 )
237
238 PendSV_Handler:
239     ldr r2, =pxCurrentTCB                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
240     ldr r0, [r2]                            /* r0 = pxCurrentTCB. */
241     ldr r1, [r0]                            /* r1 = Location in TCB where the context should be saved. */
242     mrs r2, psp                             /* r2 = PSP. */
243
244     save_general_regs:
245         stmia r1!, {r4-r7}                  /* Store r4-r7. */
246         mov r4, r8                          /* r4 = r8. */
247         mov r5, r9                          /* r5 = r9. */
248         mov r6, r10                         /* r6 = r10. */
249         mov r7, r11                         /* r7 = r11. */
250         stmia r1!, {r4-r7}                  /* Store r8-r11. */
251         ldmia r2!, {r4-r7}                  /* Copy half of the  hardware saved context into r4-r7. */
252         stmia r1!, {r4-r7}                  /* Store the hardware saved context. */
253         ldmia r2!, {r4-r7}                  /* Copy rest half of the  hardware saved context into r4-r7. */
254         stmia r1!, {r4-r7}                  /* Store the hardware saved context. */
255
256     save_special_regs:
257         mrs r2, psp                         /* r2 = PSP. */
258         mrs r3, psplim                      /* r3 = PSPLIM. */
259         mrs r4, control                     /* r4 = CONTROL. */
260         mov r5, lr                          /* r5 = LR. */
261         stmia r1!, {r2-r5}                  /* Store original PSP (after hardware has saved context), PSPLIM, CONTROL and LR. */
262         str r1, [r0]                        /* Save the location from where the context should be restored as the first member of TCB. */
263
264     select_next_task:
265         cpsid i
266         bl vTaskSwitchContext
267         cpsie i
268
269     program_mpu:
270         ldr r3, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
271         ldr r0, [r3]                        /* r0 = pxCurrentTCB.*/
272
273         dmb                                 /* Complete outstanding transfers before disabling MPU. */
274         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
275         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
276         movs r3, #1                         /* r3 = 1. */
277         bics r2, r3                         /* r2 = r2 & ~r3 i.e. Clear the bit 0 in r2. */
278         str r2, [r1]                        /* Disable MPU. */
279
280         adds r0, #4                         /* r0 = r0 + 4. r0 now points to MAIR0 in TCB. */
281         ldr r1, [r0]                        /* r1 = *r0 i.e. r1 = MAIR0. */
282         ldr r2, =0xe000edc0                 /* r2 = 0xe000edc0 [Location of MAIR0]. */
283         str r1, [r2]                        /* Program MAIR0. */
284
285         adds r0, #4                         /* r0 = r0 + 4. r0 now points to first RBAR in TCB. */
286         ldr r1, =0xe000ed98                 /* r1 = 0xe000ed98 [Location of RNR]. */
287
288         movs r3, #4                         /* r3 = 4. */
289         str r3, [r1]                        /* Program RNR = 4. */
290         ldmia r0!, {r4-r5}                  /* Read first set of RBAR/RLAR registers from TCB. */
291         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
292         stmia r2!, {r4-r5}                  /* Write first set of RBAR/RLAR registers. */
293         movs r3, #5                         /* r3 = 5. */
294         str r3, [r1]                        /* Program RNR = 5. */
295         ldmia r0!, {r4-r5}                  /* Read second set of RBAR/RLAR registers from TCB. */
296         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
297         stmia r2!, {r4-r5}                  /* Write second set of RBAR/RLAR registers. */
298         movs r3, #6                         /* r3 = 6. */
299         str r3, [r1]                        /* Program RNR = 6. */
300         ldmia r0!, {r4-r5}                  /* Read third set of RBAR/RLAR registers from TCB. */
301         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
302         stmia r2!, {r4-r5}                  /* Write third set of RBAR/RLAR registers. */
303         movs r3, #7                         /* r3 = 6. */
304         str r3, [r1]                        /* Program RNR = 7. */
305         ldmia r0!, {r4-r5}                  /* Read fourth set of RBAR/RLAR registers from TCB. */
306         ldr r2, =0xe000ed9c                 /* r2 = 0xe000ed9c [Location of RBAR]. */
307         stmia r2!, {r4-r5}                  /* Write fourth set of RBAR/RLAR registers. */
308
309         ldr r1, =0xe000ed94                 /* r1 = 0xe000ed94 [Location of MPU_CTRL]. */
310         ldr r2, [r1]                        /* Read the value of MPU_CTRL. */
311         movs r3, #1                         /* r3 = 1. */
312         orrs r2, r3                         /* r2 = r2 | r3 i.e. Set the bit 0 in r2. */
313         str r2, [r1]                        /* Enable MPU. */
314         dsb                                 /* Force memory writes before continuing. */
315
316     restore_context:
317         ldr r2, =pxCurrentTCB               /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
318         ldr r0, [r2]                        /* r0 = pxCurrentTCB.*/
319         ldr r1, [r0]                        /* r1 = Location of saved context in TCB. */
320
321     restore_special_regs:
322         subs r1, #16
323         ldmia r1!, {r2-r5}                  /* r2 = original PSP, r3 = PSPLIM, r4 = CONTROL, r5 = LR. */
324         subs r1, #16
325         msr psp, r2
326         msr psplim, r3
327         msr control, r4
328         mov lr, r5
329
330     restore_general_regs:
331         subs r1, #32
332         ldmia r1!, {r4-r7}                  /* r4-r7 contain half of the hardware saved context. */
333         stmia r2!, {r4-r7}                  /* Copy half of the the hardware saved context on the task stack. */
334         ldmia r1!, {r4-r7}                  /* r4-r7 contain rest half of the hardware saved context. */
335         stmia r2!, {r4-r7}                  /* Copy rest half of the the hardware saved context on the task stack. */
336         subs r1, #48
337         ldmia r1!, {r4-r7}                  /* Restore r8-r11. */
338         mov r8, r4                          /* r8 = r4. */
339         mov r9, r5                          /* r9 = r5. */
340         mov r10, r6                         /* r10 = r6. */
341         mov r11, r7                         /* r11 = r7. */
342         subs r1, #32
343         ldmia r1!, {r4-r7}                  /* Restore r4-r7. */
344         subs r1, #16
345
346     restore_context_done:
347         str r1, [r0]                        /* Save the location where the context should be saved next as the first member of TCB. */
348         bx lr
349
350 #else /* configENABLE_MPU */
351
352 PendSV_Handler:
353     mrs r0, psp                             /* Read PSP in r0. */
354     ldr r2, =pxCurrentTCB                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
355     ldr r1, [r2]                            /* Read pxCurrentTCB. */
356
357     subs r0, r0, #40                        /* Make space for PSPLIM, LR and the remaining registers on the stack. */
358     str r0, [r1]                            /* Save the new top of stack in TCB. */
359     mrs r2, psplim                          /* r2 = PSPLIM. */
360     mov r3, lr                              /* r3 = LR/EXC_RETURN. */
361     stmia r0!, {r2-r7}                      /* Store on the stack - PSPLIM, LR and low registers that are not automatically saved. */
362     mov r4, r8                              /* r4 = r8. */
363     mov r5, r9                              /* r5 = r9. */
364     mov r6, r10                             /* r6 = r10. */
365     mov r7, r11                             /* r7 = r11. */
366     stmia r0!, {r4-r7}                      /* Store the high registers that are not saved automatically. */
367
368     cpsid i
369     bl vTaskSwitchContext
370     cpsie i
371
372     ldr r2, =pxCurrentTCB                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
373     ldr r1, [r2]                            /* Read pxCurrentTCB. */
374     ldr r0, [r1]                            /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
375
376     adds r0, r0, #24                        /* Move to the high registers. */
377     ldmia r0!, {r4-r7}                      /* Restore the high registers that are not automatically restored. */
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     msr psp, r0                             /* Remember the new top of stack for the task. */
383     subs r0, r0, #40                        /* Move to the starting of the saved context. */
384     ldmia r0!, {r2-r7}                      /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r7 restored. */
385     msr psplim, r2                          /* Restore the PSPLIM register value for the task. */
386     bx r3
387
388 #endif /* configENABLE_MPU */
389 /*-----------------------------------------------------------*/
390
391 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
392
393 SVC_Handler:
394     movs r0, #4
395     mov r1, lr
396     tst r0, r1
397     beq stack_on_msp
398     stack_on_psp:
399         mrs r0, psp
400         b route_svc
401     stack_on_msp:
402         mrs r0, msp
403         b route_svc
404
405     route_svc:
406         ldr r3, [r0, #24]
407         subs r3, #2
408         ldrb r2, [r3, #0]
409         cmp r2, #NUM_SYSTEM_CALLS
410         blt system_call_enter
411         cmp r2, #104        /* portSVC_SYSTEM_CALL_EXIT. */
412         beq system_call_exit
413         b vPortSVCHandler_C
414
415     system_call_enter:
416         b vSystemCallEnter
417     system_call_exit:
418         b vSystemCallExit
419
420 #else /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
421
422 SVC_Handler:
423     movs r0, #4
424     mov r1, lr
425     tst r0, r1
426     beq stacking_used_msp
427     mrs r0, psp
428     b vPortSVCHandler_C
429     stacking_used_msp:
430         mrs r0, msp
431         b vPortSVCHandler_C
432
433 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
434 /*-----------------------------------------------------------*/
435
436     END