2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
36 /* Hardware registers. */
41 /* Variables and functions. */
42 .extern ulMaxAPIPriorityMask
43 .extern _freertos_vector_table
45 .extern vTaskSwitchContext
46 .extern vApplicationIRQHandler
47 .extern ulPortInterruptNesting
49 #if defined( __ARM_FP )
50 .extern ulPortTaskHasFPUContext
53 .global FreeRTOS_IRQ_Handler
54 .global FreeRTOS_SWI_Handler
55 .global vPortRestoreTaskContext
57 .macro portSAVE_CONTEXT
59 /* Save the LR and SPSR onto the system mode stack before switching to
60 system mode to save the remaining system mode registers. */
65 /* Push the critical nesting count. */
66 LDR R2, ulCriticalNestingConst
70 #if defined( __ARM_FP )
71 /* Does the task have a floating point context that needs saving? If
72 ulPortTaskHasFPUContext is 0 then no. */
73 LDR R2, ulPortTaskHasFPUContextConst
77 /* Save the floating point context, if any. */
82 /* Save ulPortTaskHasFPUContext itself. */
86 /* Save the stack pointer in the TCB. */
87 LDR R0, pxCurrentTCBConst
93 ; /**********************************************************************/
95 .macro portRESTORE_CONTEXT
97 /* Set the SP to point to the stack of the task being restored. */
98 LDR R0, pxCurrentTCBConst
102 #if defined( __ARM_FP )
104 * Is there a floating point context to restore? If the restored
105 * ulPortTaskHasFPUContext is zero then no.
107 LDR R0, ulPortTaskHasFPUContextConst
112 /* Restore the floating point context, if any. */
116 #endif /* __ARM_FP */
118 /* Restore the critical section nesting depth. */
119 LDR R0, ulCriticalNestingConst
123 /* Ensure the priority mask is correct for the critical nesting depth. */
124 LDR R2, ulICCPMRConst
128 LDRNE R4, ulMaxAPIPriorityMaskConst
132 /* Restore all system mode registers other than the SP (which is already
136 /* Return to the task code, loading CPSR on the way. */
142 /******************************************************************************
143 * SVC handler is used to start the scheduler.
144 *****************************************************************************/
146 .type FreeRTOS_SWI_Handler, %function
147 FreeRTOS_SWI_Handler:
148 /* Save the context of the current task and select a new task to run. */
150 LDR R0, vTaskSwitchContextConst
155 /******************************************************************************
156 * vPortRestoreTaskContext is used to start the scheduler.
157 *****************************************************************************/
158 .type vPortRestoreTaskContext, %function
159 vPortRestoreTaskContext:
160 /* Switch to system mode. */
165 .type FreeRTOS_IRQ_Handler, %function
166 FreeRTOS_IRQ_Handler:
168 /* Return to the interrupted instruction. */
171 /* Push the return address and SPSR. */
176 /* Change to supervisor mode to allow reentry. */
179 /* Push used registers. */
182 /* Increment nesting count. r3 holds the address of ulPortInterruptNesting
183 for future use. r1 holds the original ulPortInterruptNesting value for
185 LDR r3, ulPortInterruptNestingConst
190 /* Read value from the interrupt acknowledge register, which is stored in r0
191 for future parameter and interrupt clearing use. */
192 LDR r2, ulICCIARConst
196 /* Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
197 future use. _RB_ Is this ever needed provided the start of the stack is
198 alligned on an 8-byte boundary? */
203 /* Call the interrupt handler. */
205 LDR r1, vApplicationIRQHandlerConst
214 /* Write the value read from ICCIAR to ICCEOIR. */
215 LDR r4, ulICCEOIRConst
219 /* Restore the old nesting count. */
222 /* A context switch is never performed if the nesting count is not 0. */
224 BNE exit_without_switch
226 /* Did the interrupt request a context switch? r1 holds the address of
227 ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
229 LDR r1, =ulPortYieldRequired
232 BNE switch_before_exit
235 /* No context switch. Restore used registers, LR_irq and SPSR before
245 /* A context swtich is to be performed. Clear the context switch pending
250 /* Restore used registers, LR-irq and SPSR before saving the context
251 to the task stack. */
259 /* Call the function that selects the new task to execute.
260 vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
261 instructions, or 8 byte aligned stack allocated data. LR does not need
262 saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */
263 LDR R0, vTaskSwitchContextConst
266 /* Restore the context of, and branch to, the task selected to execute
270 /******************************************************************************
271 * If the application provides an implementation of vApplicationIRQHandler(),
272 * then it will get called directly without saving the FPU registers on
273 * interrupt entry, and this weak implementation of
274 * vApplicationIRQHandler() will not get called.
276 * If the application provides its own implementation of
277 * vApplicationFPUSafeIRQHandler() then this implementation of
278 * vApplicationIRQHandler() will be called, save the FPU registers, and then
279 * call vApplicationFPUSafeIRQHandler().
281 * Therefore, if the application writer wants FPU registers to be saved on
282 * interrupt entry their IRQ handler must be called
283 * vApplicationFPUSafeIRQHandler(), and if the application writer does not want
284 * FPU registers to be saved on interrupt entry their IRQ handler must be
285 * called vApplicationIRQHandler().
286 *****************************************************************************/
288 .weak vApplicationIRQHandler
289 .type vApplicationIRQHandler, %function
290 vApplicationIRQHandler:
294 #if defined( __ARM_FP )
299 LDR r1, vApplicationFPUSafeIRQHandlerConst
305 #endif /* __ARM_FP */
309 ulICCIARConst: .word ulICCIAR
310 ulICCEOIRConst: .word ulICCEOIR
311 ulICCPMRConst: .word ulICCPMR
312 pxCurrentTCBConst: .word pxCurrentTCB
313 ulCriticalNestingConst: .word ulCriticalNesting
315 #if defined( __ARM_FP )
316 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext
317 vApplicationFPUSafeIRQHandlerConst: .word vApplicationFPUSafeIRQHandler
318 #endif /* __ARM_FP */
320 ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask
321 vTaskSwitchContextConst: .word vTaskSwitchContext
322 vApplicationIRQHandlerConst: .word vApplicationIRQHandler
323 ulPortInterruptNestingConst: .word ulPortInterruptNesting