]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ARMv8M/non_secure/portable/IAR/ARM_CM33_NTZ/portasm.s
Add "is inside interrupt" function to MPU ports.
[freertos] / FreeRTOS / Source / portable / ARMv8M / non_secure / portable / IAR / ARM_CM33_NTZ / portasm.s
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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  * 1 tab == 4 spaces!\r
26  */\r
27 /* Including FreeRTOSConfig.h here will cause build errors if the header file\r
28 contains code not understood by the assembler - for example the 'extern' keyword.\r
29 To avoid errors place any such code inside a #ifdef __ICCARM__/#endif block so\r
30 the code is included in C files but excluded by the preprocessor in assembly\r
31 files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler. */\r
32 #include "FreeRTOSConfig.h"\r
33 \r
34         EXTERN pxCurrentTCB\r
35         EXTERN vTaskSwitchContext\r
36         EXTERN vPortSVCHandler_C\r
37 \r
38         PUBLIC xIsPrivileged\r
39         PUBLIC vResetPrivilege\r
40         PUBLIC vRestoreContextOfFirstTask\r
41         PUBLIC vRaisePrivilege\r
42         PUBLIC vStartFirstTask\r
43         PUBLIC ulSetInterruptMask\r
44         PUBLIC vClearInterruptMask\r
45         PUBLIC PendSV_Handler\r
46         PUBLIC SVC_Handler\r
47 /*-----------------------------------------------------------*/\r
48 \r
49 /*---------------- Unprivileged Functions -------------------*/\r
50 \r
51 /*-----------------------------------------------------------*/\r
52 \r
53         SECTION .text:CODE:NOROOT(2)\r
54         THUMB\r
55 /*-----------------------------------------------------------*/\r
56 \r
57 xIsPrivileged:\r
58         mrs r0, control                                                 /* r0 = CONTROL. */\r
59         tst r0, #1                                                              /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */\r
60         ite ne\r
61         movne r0, #0                                                    /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */\r
62         moveq r0, #1                                                    /* CONTROL[0]==0. Return true to indicate that the processor is not privileged. */\r
63         bx lr                                                                   /* Return. */\r
64 /*-----------------------------------------------------------*/\r
65 \r
66 vResetPrivilege:\r
67         mrs r0, control                                                 /* r0 = CONTROL. */\r
68         orr r0, r0, #1                                                  /* r0 = r0 | 1. */\r
69         msr control, r0                                                 /* CONTROL = r0. */\r
70         bx lr                                                                   /* Return to the caller. */\r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /*----------------- Privileged Functions --------------------*/\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77         SECTION privileged_functions:CODE:NOROOT(2)\r
78         THUMB\r
79 /*-----------------------------------------------------------*/\r
80 \r
81 vRestoreContextOfFirstTask:\r
82         ldr  r2, =pxCurrentTCB                                  /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
83         ldr  r1, [r2]                                                   /* Read pxCurrentTCB. */\r
84         ldr  r0, [r1]                                                   /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */\r
85 \r
86 #if ( configENABLE_MPU == 1 )\r
87         dmb                                                                             /* Complete outstanding transfers before disabling MPU. */\r
88         ldr r2, =0xe000ed94                                             /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
89         ldr r4, [r2]                                                    /* Read the value of MPU_CTRL. */\r
90         bic r4, r4, #1                                                  /* r4 = r4 & ~1 i.e. Clear the bit 0 in r4. */\r
91         str r4, [r2]                                                    /* Disable MPU. */\r
92 \r
93         adds r1, #4                                                             /* r1 = r1 + 4. r1 now points to MAIR0 in TCB. */\r
94         ldr r3, [r1]                                                    /* r3 = *r1 i.e. r3 = MAIR0. */\r
95         ldr r2, =0xe000edc0                                             /* r2 = 0xe000edc0 [Location of MAIR0]. */\r
96         str r3, [r2]                                                    /* Program MAIR0. */\r
97         ldr r2, =0xe000ed98                                             /* r2 = 0xe000ed98 [Location of RNR]. */\r
98         movs r3, #4                                                             /* r3 = 4. */\r
99         str r3, [r2]                                                    /* Program RNR = 4. */\r
100         adds r1, #4                                                             /* r1 = r1 + 4. r1 now points to first RBAR in TCB. */\r
101         ldr r2, =0xe000ed9c                                             /* r2 = 0xe000ed9c [Location of RBAR]. */\r
102         ldmia r1!, {r4-r11}                                             /* Read 4 sets of RBAR/RLAR registers from TCB. */\r
103         stmia r2!, {r4-r11}                                             /* Write 4 set of RBAR/RLAR registers using alias registers. */\r
104 \r
105         ldr r2, =0xe000ed94                                             /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
106         ldr r4, [r2]                                                    /* Read the value of MPU_CTRL. */\r
107         orr r4, r4, #1                                                  /* r4 = r4 | 1 i.e. Set the bit 0 in r4. */\r
108         str r4, [r2]                                                    /* Enable MPU. */\r
109         dsb                                                                             /* Force memory writes before continuing. */\r
110 #endif /* configENABLE_MPU */\r
111 \r
112 #if ( configENABLE_MPU == 1 )\r
113         ldm  r0!, {r1-r3}                                               /* Read from stack - r1 = PSPLIM, r2 = CONTROL and r3 = EXC_RETURN. */\r
114         msr  psplim, r1                                                 /* Set this task's PSPLIM value. */\r
115         msr  control, r2                                                /* Set this task's CONTROL value. */\r
116         adds r0, #32                                                    /* Discard everything up to r0. */\r
117         msr  psp, r0                                                    /* This is now the new top of stack to use in the task. */\r
118         isb\r
119         bx   r3                                                                 /* Finally, branch to EXC_RETURN. */\r
120 #else /* configENABLE_MPU */\r
121         ldm  r0!, {r1-r2}                                               /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */\r
122         msr  psplim, r1                                                 /* Set this task's PSPLIM value. */\r
123         movs r1, #2                                                             /* r1 = 2. */\r
124         msr  CONTROL, r1                                                /* Switch to use PSP in the thread mode. */\r
125         adds r0, #32                                                    /* Discard everything up to r0. */\r
126         msr  psp, r0                                                    /* This is now the new top of stack to use in the task. */\r
127         isb\r
128         bx   r2                                                                 /* Finally, branch to EXC_RETURN. */\r
129 #endif /* configENABLE_MPU */\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 vRaisePrivilege:\r
133         mrs  r0, control                                                /* Read the CONTROL register. */\r
134         bic r0, r0, #1                                                  /* Clear the bit 0. */\r
135         msr  control, r0                                                /* Write back the new CONTROL value. */\r
136         bx lr                                                                   /* Return to the caller. */\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 vStartFirstTask:\r
140         ldr r0, =0xe000ed08                                             /* Use the NVIC offset register to locate the stack. */\r
141         ldr r0, [r0]                                                    /* Read the VTOR register which gives the address of vector table. */\r
142         ldr r0, [r0]                                                    /* The first entry in vector table is stack pointer. */\r
143         msr msp, r0                                                             /* Set the MSP back to the start of the stack. */\r
144         cpsie i                                                                 /* Globally enable interrupts. */\r
145         cpsie f\r
146         dsb\r
147         isb\r
148         svc 2                                                                   /* System call to start the first task. portSVC_START_SCHEDULER = 2. */\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 ulSetInterruptMask:\r
152         mrs r0, basepri                                                 /* r0 = basepri. Return original basepri value. */\r
153         mov r1, #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
154         msr basepri, r1                                                 /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
155         dsb\r
156         isb\r
157         bx lr                                                                   /* Return. */\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 vClearInterruptMask:\r
161         msr basepri, r0                                                 /* basepri = ulMask. */\r
162         dsb\r
163         isb\r
164         bx lr                                                                   /* Return. */\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 PendSV_Handler:\r
168         mrs r0, psp                                                             /* Read PSP in r0. */\r
169 #if ( configENABLE_FPU == 1 )\r
170         tst lr, #0x10                                                   /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */\r
171         it eq\r
172         vstmdbeq r0!, {s16-s31}                                 /* Store the FPU registers which are not saved automatically. */\r
173 #endif /* configENABLE_FPU */\r
174 #if ( configENABLE_MPU == 1 )\r
175         mrs r1, psplim                                                  /* r1 = PSPLIM. */\r
176         mrs r2, control                                                 /* r2 = CONTROL. */\r
177         mov r3, lr                                                              /* r3 = LR/EXC_RETURN. */\r
178         stmdb r0!, {r1-r11}                                             /* Store on the stack - PSPLIM, CONTROL, LR and registers that are not automatically saved. */\r
179 #else /* configENABLE_MPU */\r
180         mrs r2, psplim                                                  /* r2 = PSPLIM. */\r
181         mov r3, lr                                                              /* r3 = LR/EXC_RETURN. */\r
182         stmdb r0!, {r2-r11}                                             /* Store on the stack - PSPLIM, LR and registers that are not automatically. */\r
183 #endif /* configENABLE_MPU */\r
184 \r
185         ldr r2, =pxCurrentTCB                                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
186         ldr r1, [r2]                                                    /* Read pxCurrentTCB. */\r
187         str r0, [r1]                                                    /* Save the new top of stack in TCB. */\r
188 \r
189         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
190         msr basepri, r0                                                 /* Disable interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
191         dsb\r
192         isb\r
193         bl vTaskSwitchContext\r
194         mov r0, #0                                                              /* r0 = 0. */\r
195         msr basepri, r0                                                 /* Enable interrupts. */\r
196 \r
197         ldr r2, =pxCurrentTCB                                   /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */\r
198         ldr r1, [r2]                                                    /* Read pxCurrentTCB. */\r
199         ldr r0, [r1]                                                    /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */\r
200 \r
201 #if ( configENABLE_MPU == 1 )\r
202         dmb                                                                             /* Complete outstanding transfers before disabling MPU. */\r
203         ldr r2, =0xe000ed94                                             /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
204         ldr r4, [r2]                                                    /* Read the value of MPU_CTRL. */\r
205         bic r4, r4, #1                                                  /* r4 = r4 & ~1 i.e. Clear the bit 0 in r4. */\r
206         str r4, [r2]                                                    /* Disable MPU. */\r
207 \r
208         adds r1, #4                                                             /* r1 = r1 + 4. r1 now points to MAIR0 in TCB. */\r
209         ldr r3, [r1]                                                    /* r3 = *r1 i.e. r3 = MAIR0. */\r
210         ldr r2, =0xe000edc0                                             /* r2 = 0xe000edc0 [Location of MAIR0]. */\r
211         str r3, [r2]                                                    /* Program MAIR0. */\r
212         ldr r2, =0xe000ed98                                             /* r2 = 0xe000ed98 [Location of RNR]. */\r
213         movs r3, #4                                                             /* r3 = 4. */\r
214         str r3, [r2]                                                    /* Program RNR = 4. */\r
215         adds r1, #4                                                             /* r1 = r1 + 4. r1 now points to first RBAR in TCB. */\r
216         ldr r2, =0xe000ed9c                                             /* r2 = 0xe000ed9c [Location of RBAR]. */\r
217         ldmia r1!, {r4-r11}                                             /* Read 4 sets of RBAR/RLAR registers from TCB. */\r
218         stmia r2!, {r4-r11}                                             /* Write 4 set of RBAR/RLAR registers using alias registers. */\r
219 \r
220         ldr r2, =0xe000ed94                                             /* r2 = 0xe000ed94 [Location of MPU_CTRL]. */\r
221         ldr r4, [r2]                                                    /* Read the value of MPU_CTRL. */\r
222         orr r4, r4, #1                                                  /* r4 = r4 | 1 i.e. Set the bit 0 in r4. */\r
223         str r4, [r2]                                                    /* Enable MPU. */\r
224         dsb                                                                             /* Force memory writes before continuing. */\r
225 #endif /* configENABLE_MPU */\r
226 \r
227 #if ( configENABLE_MPU == 1 )\r
228         ldmia r0!, {r1-r11}                                             /* Read from stack - r1 = PSPLIM, r2 = CONTROL, r3 = LR and r4-r11 restored. */\r
229 #else /* configENABLE_MPU */\r
230         ldmia r0!, {r2-r11}                                             /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r11 restored. */\r
231 #endif /* configENABLE_MPU */\r
232 \r
233 #if ( configENABLE_FPU == 1 )\r
234         tst r3, #0x10                                                   /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */\r
235         it eq\r
236         vldmiaeq r0!, {s16-s31}                                 /* Restore the FPU registers which are not restored automatically. */\r
237 #endif /* configENABLE_FPU */\r
238 \r
239  #if ( configENABLE_MPU == 1 )\r
240         msr psplim, r1                                                  /* Restore the PSPLIM register value for the task. */\r
241         msr control, r2                                                 /* Restore the CONTROL register value for the task. */\r
242 #else /* configENABLE_MPU */\r
243         msr psplim, r2                                                  /* Restore the PSPLIM register value for the task. */\r
244 #endif /* configENABLE_MPU */\r
245         msr psp, r0                                                             /* Remember the new top of stack for the task. */\r
246         bx r3\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 SVC_Handler:\r
250         tst lr, #4\r
251         ite eq\r
252         mrseq r0, msp\r
253         mrsne r0, psp\r
254         b vPortSVCHandler_C\r
255 /*-----------------------------------------------------------*/\r
256 \r
257         END\r