2 * FreeRTOS Kernel V10.4.4
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
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>
39 EXTERN vTaskSwitchContext
40 EXTERN vPortSVCHandler_C
42 PUBLIC xPortPendSVHandler
43 PUBLIC vPortSVCHandler
44 PUBLIC vPortStartFirstTask
46 PUBLIC vPortRestoreContextOfFirstTask
48 PUBLIC vResetPrivilege
50 /*-----------------------------------------------------------*/
55 /* Get the location of the current TCB. */
59 /* Is the task using the FPU context? If so, push high vfp registers. */
62 vstmdbeq r0!, {s16-s31}
64 /* Save the core registers. */
66 stmdb r0!, {r1, r4-r11, r14}
68 /* Save the new top of stack into the first member of the TCB. */
72 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
81 /* The first item in pxCurrentTCB is the task top of stack. */
84 /* Move onto the second item in the TCB... */
87 dmb /* Complete outstanding transfers before disabling MPU. */
88 ldr r2, =0xe000ed94 /* MPU_CTRL register. */
89 ldr r3, [r2] /* Read the value of MPU_CTRL. */
90 bic r3, r3, #1 /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
91 str r3, [r2] /* Disable MPU. */
93 /* Region Base Address register. */
95 /* Read 4 sets of MPU registers [MPU Region # 4 - 7]. */
97 /* Write 4 sets of MPU registers [MPU Region # 4 - 7]. */
100 #ifdef configTOTAL_MPU_REGIONS
101 #if ( configTOTAL_MPU_REGIONS == 16 )
102 /* Read 4 sets of MPU registers [MPU Region # 8 - 11]. */
104 /* Write 4 sets of MPU registers. [MPU Region # 8 - 11]. */
106 /* Read 4 sets of MPU registers [MPU Region # 12 - 15]. */
108 /* Write 4 sets of MPU registers. [MPU Region # 12 - 15]. */
110 #endif /* configTOTAL_MPU_REGIONS == 16. */
111 #endif /* configTOTAL_MPU_REGIONS */
113 ldr r2, =0xe000ed94 /* MPU_CTRL register. */
114 ldr r3, [r2] /* Read the value of MPU_CTRL. */
115 orr r3, r3, #1 /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
116 str r3, [r2] /* Enable MPU. */
117 dsb /* Force memory writes before continuing. */
119 /* Pop the registers that are not automatically saved on exception entry. */
120 ldmia r0!, {r3-r11, r14}
123 /* Is the task using the FPU context? If so, pop the high vfp registers
127 vldmiaeq r0!, {s16-s31}
135 /*-----------------------------------------------------------*/
138 #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
148 /*-----------------------------------------------------------*/
151 /* Use the NVIC offset register to locate the stack. */
155 /* Set the msp back to the start of the stack. */
157 /* Clear the bit that indicates the FPU is in use in case the FPU was used
158 before the scheduler was started - which would otherwise result in the
159 unnecessary leaving of space in the SVC stack for lazy saving of FPU
163 /* Call SVC to start the first task. */
170 /*-----------------------------------------------------------*/
172 vPortRestoreContextOfFirstTask:
173 /* Use the NVIC offset register to locate the stack. */
177 /* Set the msp back to the start of the stack. */
179 /* Restore the context. */
180 ldr r3, =pxCurrentTCB
182 /* The first item in the TCB is the task top of stack. */
184 /* Move onto the second item in the TCB... */
187 dmb /* Complete outstanding transfers before disabling MPU. */
188 ldr r2, =0xe000ed94 /* MPU_CTRL register. */
189 ldr r3, [r2] /* Read the value of MPU_CTRL. */
190 bic r3, r3, #1 /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
191 str r3, [r2] /* Disable MPU. */
193 /* Region Base Address register. */
195 /* Read 4 sets of MPU registers [MPU Region # 4 - 7]. */
197 /* Write 4 sets of MPU registers [MPU Region # 4 - 7]. */
200 #ifdef configTOTAL_MPU_REGIONS
201 #if ( configTOTAL_MPU_REGIONS == 16 )
202 /* Read 4 sets of MPU registers [MPU Region # 8 - 11]. */
204 /* Write 4 sets of MPU registers. [MPU Region # 8 - 11]. */
206 /* Read 4 sets of MPU registers [MPU Region # 12 - 15]. */
208 /* Write 4 sets of MPU registers. [MPU Region # 12 - 15]. */
210 #endif /* configTOTAL_MPU_REGIONS == 16. */
211 #endif /* configTOTAL_MPU_REGIONS */
213 ldr r2, =0xe000ed94 /* MPU_CTRL register. */
214 ldr r3, [r2] /* Read the value of MPU_CTRL. */
215 orr r3, r3, #1 /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
216 str r3, [r2] /* Enable MPU. */
217 dsb /* Force memory writes before continuing. */
219 /* Pop the registers that are not automatically saved on exception entry. */
220 ldmia r0!, {r3-r11, r14}
222 /* Restore the task stack pointer. */
228 /*-----------------------------------------------------------*/
231 /* The FPU enable bits are in the CPACR. */
232 ldr.w r0, =0xE000ED88
235 /* Enable CP10 and CP11 coprocessors, then save back. */
236 orr r1, r1, #( 0xf << 20 )
240 /*-----------------------------------------------------------*/
243 mrs r0, control /* r0 = CONTROL. */
244 tst r0, #1 /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
246 movne r0, #0 /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
247 moveq r0, #1 /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
249 /*-----------------------------------------------------------*/
252 mrs r0, control /* r0 = CONTROL. */
253 orr r0, r0, #1 /* r0 = r0 | 1. */
254 msr control, r0 /* CONTROL = r0. */
255 bx lr /* Return to the caller. */
256 /*-----------------------------------------------------------*/