]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CA9/portASM.S
0a9c6da38764b153a85584a799ee511eb5d1d7f2
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CA9 / portASM.S
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29         .text\r
30         .arm\r
31 \r
32         .set SYS_MODE,  0x1f\r
33         .set SVC_MODE,  0x13\r
34         .set IRQ_MODE,  0x12\r
35 \r
36         /* Hardware registers. */\r
37         .extern ulICCIAR\r
38         .extern ulICCEOIR\r
39         .extern ulICCPMR\r
40 \r
41         /* Variables and functions. */\r
42         .extern ulMaxAPIPriorityMask\r
43         .extern _freertos_vector_table\r
44         .extern pxCurrentTCB\r
45         .extern vTaskSwitchContext\r
46         .extern vApplicationIRQHandler\r
47         .extern ulPortInterruptNesting\r
48         .extern ulPortTaskHasFPUContext\r
49 \r
50         .global FreeRTOS_IRQ_Handler\r
51         .global FreeRTOS_SWI_Handler\r
52         .global vPortRestoreTaskContext\r
53 \r
54 \r
55 \r
56 \r
57 .macro portSAVE_CONTEXT\r
58 \r
59         /* Save the LR and SPSR onto the system mode stack before switching to\r
60         system mode to save the remaining system mode registers. */\r
61         SRSDB   sp!, #SYS_MODE\r
62         CPS             #SYS_MODE\r
63         PUSH    {R0-R12, R14}\r
64 \r
65         /* Push the critical nesting count. */\r
66         LDR             R2, ulCriticalNestingConst\r
67         LDR             R1, [R2]\r
68         PUSH    {R1}\r
69 \r
70         /* Does the task have a floating point context that needs saving?  If\r
71         ulPortTaskHasFPUContext is 0 then no. */\r
72         LDR             R2, ulPortTaskHasFPUContextConst\r
73         LDR             R3, [R2]\r
74         CMP             R3, #0\r
75 \r
76         /* Save the floating point context, if any. */\r
77         FMRXNE  R1,  FPSCR\r
78         VPUSHNE {D0-D15}\r
79         VPUSHNE {D16-D31}\r
80         PUSHNE  {R1}\r
81 \r
82         /* Save ulPortTaskHasFPUContext itself. */\r
83         PUSH    {R3}\r
84 \r
85         /* Save the stack pointer in the TCB. */\r
86         LDR             R0, pxCurrentTCBConst\r
87         LDR             R1, [R0]\r
88         STR             SP, [R1]\r
89 \r
90         .endm\r
91 \r
92 ; /**********************************************************************/\r
93 \r
94 .macro portRESTORE_CONTEXT\r
95 \r
96         /* Set the SP to point to the stack of the task being restored. */\r
97         LDR             R0, pxCurrentTCBConst\r
98         LDR             R1, [R0]\r
99         LDR             SP, [R1]\r
100 \r
101         /* Is there a floating point context to restore?  If the restored\r
102         ulPortTaskHasFPUContext is zero then no. */\r
103         LDR             R0, ulPortTaskHasFPUContextConst\r
104         POP             {R1}\r
105         STR             R1, [R0]\r
106         CMP             R1, #0\r
107 \r
108         /* Restore the floating point context, if any. */\r
109         POPNE   {R0}\r
110         VPOPNE  {D16-D31}\r
111         VPOPNE  {D0-D15}\r
112         VMSRNE  FPSCR, R0\r
113 \r
114         /* Restore the critical section nesting depth. */\r
115         LDR             R0, ulCriticalNestingConst\r
116         POP             {R1}\r
117         STR             R1, [R0]\r
118 \r
119         /* Ensure the priority mask is correct for the critical nesting depth. */\r
120         LDR             R2, ulICCPMRConst\r
121         LDR             R2, [R2]\r
122         CMP             R1, #0\r
123         MOVEQ   R4, #255\r
124         LDRNE   R4, ulMaxAPIPriorityMaskConst\r
125         LDRNE   R4, [R4]\r
126         STR             R4, [R2]\r
127 \r
128         /* Restore all system mode registers other than the SP (which is already\r
129         being used). */\r
130         POP             {R0-R12, R14}\r
131 \r
132         /* Return to the task code, loading CPSR on the way. */\r
133         RFEIA   sp!\r
134 \r
135         .endm\r
136 \r
137 \r
138 \r
139 \r
140 /******************************************************************************\r
141  * SVC handler is used to start the scheduler.\r
142  *****************************************************************************/\r
143 .align 4\r
144 .type FreeRTOS_SWI_Handler, %function\r
145 FreeRTOS_SWI_Handler:\r
146         /* Save the context of the current task and select a new task to run. */\r
147         portSAVE_CONTEXT\r
148         LDR R0, vTaskSwitchContextConst\r
149         BLX     R0\r
150         portRESTORE_CONTEXT\r
151 \r
152 \r
153 /******************************************************************************\r
154  * vPortRestoreTaskContext is used to start the scheduler.\r
155  *****************************************************************************/\r
156 .type vPortRestoreTaskContext, %function\r
157 vPortRestoreTaskContext:\r
158         /* Switch to system mode. */\r
159         CPS             #SYS_MODE\r
160         portRESTORE_CONTEXT\r
161 \r
162 .align 4\r
163 .type FreeRTOS_IRQ_Handler, %function\r
164 FreeRTOS_IRQ_Handler:\r
165         /* Return to the interrupted instruction. */\r
166         SUB             lr, lr, #4\r
167 \r
168         /* Push the return address and SPSR. */\r
169         PUSH    {lr}\r
170         MRS             lr, SPSR\r
171         PUSH    {lr}\r
172 \r
173         /* Change to supervisor mode to allow reentry. */\r
174         CPS             #SVC_MODE\r
175 \r
176         /* Push used registers. */\r
177         PUSH    {r0-r4, r12}\r
178 \r
179         /* Increment nesting count.  r3 holds the address of ulPortInterruptNesting\r
180         for future use.  r1 holds the original ulPortInterruptNesting value for\r
181         future use. */\r
182         LDR             r3, ulPortInterruptNestingConst\r
183         LDR             r1, [r3]\r
184         ADD             r4, r1, #1\r
185         STR             r4, [r3]\r
186 \r
187         /* Read value from the interrupt acknowledge register, which is stored in r0\r
188         for future parameter and interrupt clearing use. */\r
189         LDR     r2, ulICCIARConst\r
190         LDR             r2, [r2]\r
191         LDR             r0, [r2]\r
192 \r
193         /* Ensure bit 2 of the stack pointer is clear.  r2 holds the bit 2 value for\r
194         future use.  _RB_ Does this ever actually need to be done provided the start\r
195         of the stack is 8-byte aligned? */\r
196         MOV             r2, sp\r
197         AND             r2, r2, #4\r
198         SUB             sp, sp, r2\r
199 \r
200         /* Call the interrupt handler.  r4 pushed to maintain alignment. */\r
201         PUSH    {r0-r4, lr}\r
202         LDR             r1, vApplicationIRQHandlerConst\r
203         BLX             r1\r
204         POP             {r0-r4, lr}\r
205         ADD             sp, sp, r2\r
206 \r
207         CPSID   i\r
208         DSB\r
209         ISB\r
210 \r
211         /* Write the value read from ICCIAR to ICCEOIR. */\r
212         LDR     r4, ulICCEOIRConst\r
213         LDR             r4, [r4]\r
214         STR             r0, [r4]\r
215 \r
216         /* Restore the old nesting count. */\r
217         STR             r1, [r3]\r
218 \r
219         /* A context switch is never performed if the nesting count is not 0. */\r
220         CMP             r1, #0\r
221         BNE             exit_without_switch\r
222 \r
223         /* Did the interrupt request a context switch?  r1 holds the address of\r
224         ulPortYieldRequired and r0 the value of ulPortYieldRequired for future\r
225         use. */\r
226         LDR             r1, =ulPortYieldRequired\r
227         LDR             r0, [r1]\r
228         CMP             r0, #0\r
229         BNE             switch_before_exit\r
230 \r
231 exit_without_switch:\r
232         /* No context switch.  Restore used registers, LR_irq and SPSR before\r
233         returning. */\r
234         POP             {r0-r4, r12}\r
235         CPS             #IRQ_MODE\r
236         POP             {LR}\r
237         MSR             SPSR_cxsf, LR\r
238         POP             {LR}\r
239         MOVS    PC, LR\r
240 \r
241 switch_before_exit:\r
242         /* A context swtich is to be performed.  Clear the context switch pending\r
243         flag. */\r
244         MOV             r0, #0\r
245         STR             r0, [r1]\r
246 \r
247         /* Restore used registers, LR-irq and SPSR before saving the context\r
248         to the task stack. */\r
249         POP             {r0-r4, r12}\r
250         CPS             #IRQ_MODE\r
251         POP             {LR}\r
252         MSR             SPSR_cxsf, LR\r
253         POP             {LR}\r
254         portSAVE_CONTEXT\r
255 \r
256         /* Call the function that selects the new task to execute.\r
257         vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD\r
258         instructions, or 8 byte aligned stack allocated data.  LR does not need\r
259         saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */\r
260         LDR             R0, vTaskSwitchContextConst\r
261         BLX             R0\r
262 \r
263         /* Restore the context of, and branch to, the task selected to execute\r
264         next. */\r
265         portRESTORE_CONTEXT\r
266 \r
267 \r
268 /******************************************************************************\r
269  * If the application provides an implementation of vApplicationIRQHandler(),\r
270  * then it will get called directly without saving the FPU registers on\r
271  * interrupt entry, and this weak implementation of\r
272  * vApplicationIRQHandler() will not get called.\r
273  *\r
274  * If the application provides its own implementation of\r
275  * vApplicationFPUSafeIRQHandler() then this implementation of\r
276  * vApplicationIRQHandler() will be called, save the FPU registers, and then\r
277  * call vApplicationFPUSafeIRQHandler().\r
278  *\r
279  * Therefore, if the application writer wants FPU registers to be saved on\r
280  * interrupt entry their IRQ handler must be called\r
281  * vApplicationFPUSafeIRQHandler(), and if the application writer does not want\r
282  * FPU registers to be saved on interrupt entry their IRQ handler must be\r
283  * called vApplicationIRQHandler().\r
284  *****************************************************************************/\r
285 \r
286 .align 4\r
287 .weak vApplicationIRQHandler\r
288 .type vApplicationIRQHandler, %function\r
289 vApplicationIRQHandler:\r
290         PUSH    {LR}\r
291         FMRX    R1,  FPSCR\r
292         VPUSH   {D0-D15}\r
293         VPUSH   {D16-D31}\r
294         PUSH    {R1}\r
295 \r
296         LDR             r1, vApplicationFPUSafeIRQHandlerConst\r
297         BLX             r1\r
298 \r
299         POP             {R0}\r
300         VPOP    {D16-D31}\r
301         VPOP    {D0-D15}\r
302         VMSR    FPSCR, R0\r
303 \r
304         POP {PC}\r
305 \r
306 \r
307 ulICCIARConst:  .word ulICCIAR\r
308 ulICCEOIRConst: .word ulICCEOIR\r
309 ulICCPMRConst: .word ulICCPMR\r
310 pxCurrentTCBConst: .word pxCurrentTCB\r
311 ulCriticalNestingConst: .word ulCriticalNesting\r
312 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext\r
313 ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask\r
314 vTaskSwitchContextConst: .word vTaskSwitchContext\r
315 vApplicationIRQHandlerConst: .word vApplicationIRQHandler\r
316 ulPortInterruptNestingConst: .word ulPortInterruptNesting\r
317 vApplicationFPUSafeIRQHandlerConst: .word vApplicationFPUSafeIRQHandler\r
318 \r
319 .end\r
320 \r
321 \r
322 \r
323 \r
324 \r