2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 EXTERN vTaskSwitchContext
\r
30 EXTERN vPortSVCHandler_C
\r
32 PUBLIC xIsPrivileged
\r
33 PUBLIC vResetPrivilege
\r
34 PUBLIC vRestoreContextOfFirstTask
\r
35 PUBLIC vRaisePrivilege
\r
36 PUBLIC vStartFirstTask
\r
37 PUBLIC ulSetInterruptMaskFromISR
\r
38 PUBLIC vClearInterruptMaskFromISR
\r
39 PUBLIC PendSV_Handler
\r
41 /*-----------------------------------------------------------*/
\r
43 /*---------------- Unprivileged Functions -------------------*/
\r
45 /*-----------------------------------------------------------*/
\r
47 SECTION .text:CODE:NOROOT(2)
\r
49 /*-----------------------------------------------------------*/
\r
52 mrs r0, control /* r0 = CONTROL. */
\r
53 tst r0, #1 /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
\r
55 movne r0, #0 /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
\r
56 moveq r0, #1 /* CONTROL[0]==0. Return true to indicate that the processor is not privileged. */
\r
58 /*-----------------------------------------------------------*/
\r
61 mrs r0, control /* r0 = CONTROL. */
\r
62 orr r0, r0, #1 /* r0 = r0 | 1. */
\r
63 msr control, r0 /* CONTROL = r0. */
\r
64 bx lr /* Return to the caller. */
\r
65 /*-----------------------------------------------------------*/
\r
67 /*----------------- Privileged Functions --------------------*/
\r
69 /*-----------------------------------------------------------*/
\r
71 SECTION privileged_functions:CODE:NOROOT(2)
\r
73 /*-----------------------------------------------------------*/
\r
75 vRestoreContextOfFirstTask:
\r
76 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
\r
77 ldr r1, [r2] /* Read pxCurrentTCB. */
\r
78 ldr r0, [r1] /* Read top of stack from TCB - The first item in pxCurrentTCB is the task top of stack. */
\r
80 #if ( configENABLE_MPU == 1 )
\r
81 adds r1, #4 /* r1 = r1 + 4. r1 now points to MAIR0 in TCB. */
\r
82 ldr r3, [r1] /* r3 = *r1 i.e. r3 = MAIR0. */
\r
83 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
\r
84 str r3, [r2] /* Program MAIR0. */
\r
85 ldr r2, =0xe000ed98 /* r2 = 0xe000ed98 [Location of RNR]. */
\r
86 movs r3, #4 /* r3 = 4. */
\r
87 str r3, [r2] /* Program RNR = 4. */
\r
88 adds r1, #4 /* r1 = r1 + 4. r1 now points to first RBAR in TCB. */
\r
89 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
\r
90 ldmia r1!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
\r
91 stmia r2!, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
\r
92 #endif /* configENABLE_MPU */
\r
94 #if ( configENABLE_MPU == 1 )
\r
95 ldm r0!, {r1-r3} /* Read from stack - r1 = PSPLIM, r2 = CONTROL and r3 = EXC_RETURN. */
\r
96 msr psplim, r1 /* Set this task's PSPLIM value. */
\r
97 msr control, r2 /* Set this task's CONTROL value. */
\r
98 adds r0, #32 /* Discard everything up to r0. */
\r
99 msr psp, r0 /* This is now the new top of stack to use in the task. */
\r
101 bx r3 /* Finally, branch to EXC_RETURN. */
\r
102 #else /* configENABLE_MPU */
\r
103 ldm r0!, {r1-r2} /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
\r
104 msr psplim, r1 /* Set this task's PSPLIM value. */
\r
105 movs r1, #2 /* r1 = 2. */
\r
106 msr CONTROL, r1 /* Switch to use PSP in the thread mode. */
\r
107 adds r0, #32 /* Discard everything up to r0. */
\r
108 msr psp, r0 /* This is now the new top of stack to use in the task. */
\r
110 bx r2 /* Finally, branch to EXC_RETURN. */
\r
111 #endif /* configENABLE_MPU */
\r
112 /*-----------------------------------------------------------*/
\r
115 mrs r0, control /* Read the CONTROL register. */
\r
116 bic r0, r0, #1 /* Clear the bit 0. */
\r
117 msr control, r0 /* Write back the new CONTROL value. */
\r
118 bx lr /* Return to the caller. */
\r
119 /*-----------------------------------------------------------*/
\r
122 ldr r0, =0xe000ed08 /* Use the NVIC offset register to locate the stack. */
\r
123 ldr r0, [r0] /* Read the VTOR register which gives the address of vector table. */
\r
124 ldr r0, [r0] /* The first entry in vector table is stack pointer. */
\r
125 msr msp, r0 /* Set the MSP back to the start of the stack. */
\r
126 cpsie i /* Globally enable interrupts. */
\r
130 svc 2 /* System call to start the first task. portSVC_START_SCHEDULER = 2. */
\r
131 /*-----------------------------------------------------------*/
\r
133 ulSetInterruptMaskFromISR:
\r
137 /*-----------------------------------------------------------*/
\r
139 vClearInterruptMaskFromISR:
\r
142 /*-----------------------------------------------------------*/
\r
145 mrs r0, psp /* Read PSP in r0. */
\r
146 #if ( configENABLE_FPU == 1 )
\r
147 tst lr, #0x10 /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */
\r
149 vstmdbeq r0!, {s16-s31} /* Store the FPU registers which are not saved automatically. */
\r
150 #endif /* configENABLE_FPU */
\r
151 #if ( configENABLE_MPU == 1 )
\r
152 mrs r1, psplim /* r1 = PSPLIM. */
\r
153 mrs r2, control /* r2 = CONTROL. */
\r
154 mov r3, lr /* r3 = LR/EXC_RETURN. */
\r
155 stmdb r0!, {r1-r11} /* Store on the stack - PSPLIM, CONTROL, LR and registers that are not automatically saved. */
\r
156 #else /* configENABLE_MPU */
\r
157 mrs r2, psplim /* r2 = PSPLIM. */
\r
158 mov r3, lr /* r3 = LR/EXC_RETURN. */
\r
159 stmdb r0!, {r2-r11} /* Store on the stack - PSPLIM, LR and registers that are not automatically. */
\r
160 #endif /* configENABLE_MPU */
\r
162 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
\r
163 ldr r1, [r2] /* Read pxCurrentTCB. */
\r
164 str r0, [r1] /* Save the new top of stack in TCB. */
\r
167 bl vTaskSwitchContext
\r
170 ldr r2, =pxCurrentTCB /* Read the location of pxCurrentTCB i.e. &( pxCurrentTCB ). */
\r
171 ldr r1, [r2] /* Read pxCurrentTCB. */
\r
172 ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. r0 now points to the top of stack. */
\r
174 #if ( configENABLE_MPU == 1 )
\r
175 adds r1, #4 /* r1 = r1 + 4. r1 now points to MAIR0 in TCB. */
\r
176 ldr r3, [r1] /* r3 = *r1 i.e. r3 = MAIR0. */
\r
177 ldr r2, =0xe000edc0 /* r2 = 0xe000edc0 [Location of MAIR0]. */
\r
178 str r3, [r2] /* Program MAIR0. */
\r
179 ldr r2, =0xe000ed98 /* r2 = 0xe000ed98 [Location of RNR]. */
\r
180 movs r3, #4 /* r3 = 4. */
\r
181 str r3, [r2] /* Program RNR = 4. */
\r
182 adds r1, #4 /* r1 = r1 + 4. r1 now points to first RBAR in TCB. */
\r
183 ldr r2, =0xe000ed9c /* r2 = 0xe000ed9c [Location of RBAR]. */
\r
184 ldmia r1!, {r4-r11} /* Read 4 sets of RBAR/RLAR registers from TCB. */
\r
185 stmia r2!, {r4-r11} /* Write 4 set of RBAR/RLAR registers using alias registers. */
\r
186 #endif /* configENABLE_MPU */
\r
188 #if ( configENABLE_MPU == 1 )
\r
189 ldmia r0!, {r1-r11} /* Read from stack - r1 = PSPLIM, r2 = CONTROL, r3 = LR and r4-r11 restored. */
\r
190 #else /* configENABLE_MPU */
\r
191 ldmia r0!, {r2-r11} /* Read from stack - r2 = PSPLIM, r3 = LR and r4-r11 restored. */
\r
192 #endif /* configENABLE_MPU */
\r
194 #if ( configENABLE_FPU == 1 )
\r
195 tst r3, #0x10 /* Test Bit[4] in LR. Bit[4] of EXC_RETURN is 0 if the FPU is in use. */
\r
197 vldmiaeq r0!, {s16-s31} /* Restore the FPU registers which are not restored automatically. */
\r
198 #endif /* configENABLE_FPU */
\r
200 #if ( configENABLE_MPU == 1 )
\r
201 msr psplim, r1 /* Restore the PSPLIM register value for the task. */
\r
202 msr control, r2 /* Restore the CONTROL register value for the task. */
\r
203 #else /* configENABLE_MPU */
\r
204 msr psplim, r2 /* Restore the PSPLIM register value for the task. */
\r
205 #endif /* configENABLE_MPU */
\r
206 msr psp, r0 /* Remember the new top of stack for the task. */
\r
208 /*-----------------------------------------------------------*/
\r
215 b vPortSVCHandler_C
\r
216 /*-----------------------------------------------------------*/
\r