2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
30 /* Variables and functions. */
31 .extern ullMaxAPIPriorityMask
33 .extern vTaskSwitchContext
34 .extern vApplicationIRQHandler
35 .extern ullPortInterruptNesting
36 .extern ullPortTaskHasFPUContext
37 .extern ullCriticalNesting
38 .extern ullPortYieldRequired
41 .extern _freertos_vector_table
43 .global FreeRTOS_IRQ_Handler
44 .global FreeRTOS_SWI_Handler
45 .global vPortRestoreTaskContext
48 .macro portSAVE_CONTEXT
50 /* Switch to use the EL0 stack pointer. */
53 /* Save the entire context. */
54 STP X0, X1, [SP, #-0x10]!
55 STP X2, X3, [SP, #-0x10]!
56 STP X4, X5, [SP, #-0x10]!
57 STP X6, X7, [SP, #-0x10]!
58 STP X8, X9, [SP, #-0x10]!
59 STP X10, X11, [SP, #-0x10]!
60 STP X12, X13, [SP, #-0x10]!
61 STP X14, X15, [SP, #-0x10]!
62 STP X16, X17, [SP, #-0x10]!
63 STP X18, X19, [SP, #-0x10]!
64 STP X20, X21, [SP, #-0x10]!
65 STP X22, X23, [SP, #-0x10]!
66 STP X24, X25, [SP, #-0x10]!
67 STP X26, X27, [SP, #-0x10]!
68 STP X28, X29, [SP, #-0x10]!
69 STP X30, XZR, [SP, #-0x10]!
81 STP X2, X3, [SP, #-0x10]!
83 /* Save the critical section nesting depth. */
84 LDR X0, ullCriticalNestingConst
87 /* Save the FPU context indicator. */
88 LDR X0, ullPortTaskHasFPUContextConst
91 /* Save the FPU context, if any (32 128-bit registers). */
94 STP Q0, Q1, [SP,#-0x20]!
95 STP Q2, Q3, [SP,#-0x20]!
96 STP Q4, Q5, [SP,#-0x20]!
97 STP Q6, Q7, [SP,#-0x20]!
98 STP Q8, Q9, [SP,#-0x20]!
99 STP Q10, Q11, [SP,#-0x20]!
100 STP Q12, Q13, [SP,#-0x20]!
101 STP Q14, Q15, [SP,#-0x20]!
102 STP Q16, Q17, [SP,#-0x20]!
103 STP Q18, Q19, [SP,#-0x20]!
104 STP Q20, Q21, [SP,#-0x20]!
105 STP Q22, Q23, [SP,#-0x20]!
106 STP Q24, Q25, [SP,#-0x20]!
107 STP Q26, Q27, [SP,#-0x20]!
108 STP Q28, Q29, [SP,#-0x20]!
109 STP Q30, Q31, [SP,#-0x20]!
112 /* Store the critical nesting count and FPU context indicator. */
113 STP X2, X3, [SP, #-0x10]!
115 LDR X0, pxCurrentTCBConst
117 MOV X0, SP /* Move SP into X0 for saving. */
120 /* Switch to use the ELx stack pointer. */
125 ; /**********************************************************************/
127 .macro portRESTORE_CONTEXT
129 /* Switch to use the EL0 stack pointer. */
132 /* Set the SP to point to the stack of the task being restored. */
133 LDR X0, pxCurrentTCBConst
138 LDP X2, X3, [SP], #0x10 /* Critical nesting and FPU context. */
140 /* Set the PMR register to be correct for the current critical nesting
142 LDR X0, ullCriticalNestingConst /* X0 holds the address of ullCriticalNesting. */
143 MOV X1, #255 /* X1 holds the unmask value. */
144 LDR X4, ullICCPMRConst /* X4 holds the address of the ICCPMR constant. */
146 LDR X5, [X4] /* X5 holds the address of the ICCPMR register. */
148 LDR X6, ullMaxAPIPriorityMaskConst
149 LDR X1, [X6] /* X1 holds the mask value. */
151 STR W1, [X5] /* Write the mask value to ICCPMR. */
152 DSB SY /* _RB_Barriers probably not required here. */
154 STR X3, [X0] /* Restore the task's critical nesting count. */
156 /* Restore the FPU context indicator. */
157 LDR X0, ullPortTaskHasFPUContextConst
160 /* Restore the FPU context, if any. */
163 LDP Q30, Q31, [SP], #0x20
164 LDP Q28, Q29, [SP], #0x20
165 LDP Q26, Q27, [SP], #0x20
166 LDP Q24, Q25, [SP], #0x20
167 LDP Q22, Q23, [SP], #0x20
168 LDP Q20, Q21, [SP], #0x20
169 LDP Q18, Q19, [SP], #0x20
170 LDP Q16, Q17, [SP], #0x20
171 LDP Q14, Q15, [SP], #0x20
172 LDP Q12, Q13, [SP], #0x20
173 LDP Q10, Q11, [SP], #0x20
174 LDP Q8, Q9, [SP], #0x20
175 LDP Q6, Q7, [SP], #0x20
176 LDP Q4, Q5, [SP], #0x20
177 LDP Q2, Q3, [SP], #0x20
178 LDP Q0, Q1, [SP], #0x20
180 LDP X2, X3, [SP], #0x10 /* SPSR and ELR. */
183 /* Restore the SPSR. */
185 /* Restore the ELR. */
188 /* Restore the SPSR. */
189 MSR SPSR_EL3, X3 /*_RB_ Assumes started in EL3. */
190 /* Restore the ELR. */
194 LDP X30, XZR, [SP], #0x10
195 LDP X28, X29, [SP], #0x10
196 LDP X26, X27, [SP], #0x10
197 LDP X24, X25, [SP], #0x10
198 LDP X22, X23, [SP], #0x10
199 LDP X20, X21, [SP], #0x10
200 LDP X18, X19, [SP], #0x10
201 LDP X16, X17, [SP], #0x10
202 LDP X14, X15, [SP], #0x10
203 LDP X12, X13, [SP], #0x10
204 LDP X10, X11, [SP], #0x10
205 LDP X8, X9, [SP], #0x10
206 LDP X6, X7, [SP], #0x10
207 LDP X4, X5, [SP], #0x10
208 LDP X2, X3, [SP], #0x10
209 LDP X0, X1, [SP], #0x10
211 /* Switch to use the ELx stack pointer. _RB_ Might not be required. */
219 /******************************************************************************
220 * FreeRTOS_SWI_Handler handler is used to perform a context switch.
221 *****************************************************************************/
223 .type FreeRTOS_SWI_Handler, %function
224 FreeRTOS_SWI_Handler:
225 /* Save the context of the current task and select a new task to run. */
236 CMP X1, #0x15 /* 0x15 = SVC instruction. */
238 CMP X1, #0x17 /* 0x17 = SMC instruction. */
241 BL vTaskSwitchContext
246 /* Full ESR is in X0, exception class code is in X1. */
249 /******************************************************************************
250 * vPortRestoreTaskContext is used to start the scheduler.
251 *****************************************************************************/
253 .type vPortRestoreTaskContext, %function
254 vPortRestoreTaskContext:
255 .set freertos_vector_base, _freertos_vector_table
257 /* Install the FreeRTOS interrupt handlers. */
258 LDR X1, =freertos_vector_base
267 /* Start the first task. */
271 /******************************************************************************
272 * FreeRTOS_IRQ_Handler handles IRQ entry and exit.
273 *****************************************************************************/
275 .type FreeRTOS_IRQ_Handler, %function
276 FreeRTOS_IRQ_Handler:
277 /* Save volatile registers. */
278 STP X0, X1, [SP, #-0x10]!
279 STP X2, X3, [SP, #-0x10]!
280 STP X4, X5, [SP, #-0x10]!
281 STP X6, X7, [SP, #-0x10]!
282 STP X8, X9, [SP, #-0x10]!
283 STP X10, X11, [SP, #-0x10]!
284 STP X12, X13, [SP, #-0x10]!
285 STP X14, X15, [SP, #-0x10]!
286 STP X16, X17, [SP, #-0x10]!
287 STP X18, X19, [SP, #-0x10]!
288 STP X29, X30, [SP, #-0x10]!
290 /* Save the SPSR and ELR. */
298 STP X2, X3, [SP, #-0x10]!
300 /* Increment the interrupt nesting counter. */
301 LDR X5, ullPortInterruptNestingConst
302 LDR X1, [X5] /* Old nesting count in X1. */
304 STR X6, [X5] /* Address of nesting count variable in X5. */
306 /* Maintain the interrupt nesting information across the function call. */
307 STP X1, X5, [SP, #-0x10]!
309 /* Read value from the interrupt acknowledge register, which is stored in W0
310 for future parameter and interrupt clearing use. */
311 LDR X2, ullICCIARConst
313 LDR W0, [X3] /* ICCIAR in W0 as parameter. */
315 /* Maintain the ICCIAR value across the function call. */
316 STP X0, X1, [SP, #-0x10]!
318 /* Call the C handler. */
319 BL vApplicationIRQHandler
321 /* Disable interrupts. */
326 /* Restore the ICCIAR value. */
327 LDP X0, X1, [SP], #0x10
329 /* End IRQ processing by writing ICCIAR to the EOI register. */
330 LDR X4, ullICCEOIRConst
334 /* Restore the critical nesting count. */
335 LDP X1, X5, [SP], #0x10
338 /* Has interrupt nesting unwound? */
340 B.NE Exit_IRQ_No_Context_Switch
342 /* Is a context switch required? */
343 LDR X0, ullPortYieldRequiredConst
346 B.EQ Exit_IRQ_No_Context_Switch
348 /* Reset ullPortYieldRequired to 0. */
352 /* Restore volatile registers. */
353 LDP X4, X5, [SP], #0x10 /* SPSR and ELR. */
358 MSR SPSR_EL3, X5 /*_RB_ Assumes started in EL3. */
364 LDP X29, X30, [SP], #0x10
365 LDP X18, X19, [SP], #0x10
366 LDP X16, X17, [SP], #0x10
367 LDP X14, X15, [SP], #0x10
368 LDP X12, X13, [SP], #0x10
369 LDP X10, X11, [SP], #0x10
370 LDP X8, X9, [SP], #0x10
371 LDP X6, X7, [SP], #0x10
372 LDP X4, X5, [SP], #0x10
373 LDP X2, X3, [SP], #0x10
374 LDP X0, X1, [SP], #0x10
376 /* Save the context of the current task and select a new task to run. */
378 BL vTaskSwitchContext
381 Exit_IRQ_No_Context_Switch:
382 /* Restore volatile registers. */
383 LDP X4, X5, [SP], #0x10 /* SPSR and ELR. */
388 MSR SPSR_EL3, X5 /*_RB_ Assumes started in EL3. */
394 LDP X29, X30, [SP], #0x10
395 LDP X18, X19, [SP], #0x10
396 LDP X16, X17, [SP], #0x10
397 LDP X14, X15, [SP], #0x10
398 LDP X12, X13, [SP], #0x10
399 LDP X10, X11, [SP], #0x10
400 LDP X8, X9, [SP], #0x10
401 LDP X6, X7, [SP], #0x10
402 LDP X4, X5, [SP], #0x10
403 LDP X2, X3, [SP], #0x10
404 LDP X0, X1, [SP], #0x10
412 pxCurrentTCBConst: .dword pxCurrentTCB
413 ullCriticalNestingConst: .dword ullCriticalNesting
414 ullPortTaskHasFPUContextConst: .dword ullPortTaskHasFPUContext
416 ullICCPMRConst: .dword ullICCPMR
417 ullMaxAPIPriorityMaskConst: .dword ullMaxAPIPriorityMask
418 vApplicationIRQHandlerConst: .word vApplicationIRQHandler
419 ullPortInterruptNestingConst: .dword ullPortInterruptNesting
420 ullPortYieldRequiredConst: .dword ullPortYieldRequired
421 ullICCIARConst: .dword ullICCIAR
422 ullICCEOIRConst: .dword ullICCEOIR