]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ARMv8M/non_secure/port.c
First Official Release of ARMV8M Support. This release removes Pre-Release from all...
[freertos] / FreeRTOS / Source / portable / ARMv8M / non_secure / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 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.\r
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
29  * all the API functions to use the MPU wrappers. That should only be done when\r
30  * task.h is included from an application file. */\r
31 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* MPU wrappers includes. */\r
38 #include "mpu_wrappers.h"\r
39 \r
40 /* Portasm includes. */\r
41 #include "portasm.h"\r
42 \r
43 #if( configENABLE_TRUSTZONE == 1 )\r
44     /* Secure components includes. */\r
45     #include "secure_context.h"\r
46     #include "secure_init.h"\r
47 #endif /* configENABLE_TRUSTZONE */\r
48 \r
49 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
50 /*-----------------------------------------------------------*/\r
51 \r
52 /**\r
53  * @brief Constants required to manipulate the NVIC.\r
54  */\r
55 #define portNVIC_SYSTICK_CTRL               ( ( volatile uint32_t * ) 0xe000e010 )\r
56 #define portNVIC_SYSTICK_LOAD               ( ( volatile uint32_t * ) 0xe000e014 )\r
57 #define portNVIC_SYSTICK_CURRENT_VALUE      ( ( volatile uint32_t * ) 0xe000e018 )\r
58 #define portNVIC_INT_CTRL                   ( ( volatile uint32_t * ) 0xe000ed04 )\r
59 #define portNVIC_SYSPRI2                    ( ( volatile uint32_t * ) 0xe000ed20 )\r
60 #define portNVIC_SYSTICK_CLK                ( 0x00000004 )\r
61 #define portNVIC_SYSTICK_INT                ( 0x00000002 )\r
62 #define portNVIC_SYSTICK_ENABLE             ( 0x00000001 )\r
63 #define portNVIC_PENDSVSET                  ( 0x10000000 )\r
64 #define portMIN_INTERRUPT_PRIORITY          ( 255UL )\r
65 #define portNVIC_PENDSV_PRI                 ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
66 #define portNVIC_SYSTICK_PRI                ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /**\r
70  * @brief Constants required to manipulate the SCB.\r
71  */\r
72 #define portSCB_SYS_HANDLER_CTRL_STATE_REG  ( * ( volatile uint32_t * ) 0xe000ed24 )\r
73 #define portSCB_MEM_FAULT_ENABLE            ( 1UL << 16UL )\r
74 /*-----------------------------------------------------------*/\r
75 \r
76 /**\r
77  * @brief Constants required to manipulate the FPU.\r
78  */\r
79 #define portCPACR                           ( ( volatile uint32_t * ) 0xe000ed88 )  /* Coprocessor Access Control Register. */\r
80 #define portCPACR_CP10_VALUE                ( 3UL )\r
81 #define portCPACR_CP11_VALUE                portCPACR_CP10_VALUE\r
82 #define portCPACR_CP10_POS                  ( 20UL )\r
83 #define portCPACR_CP11_POS                  ( 22UL )\r
84 \r
85 #define portFPCCR                           ( ( volatile uint32_t * ) 0xe000ef34 )  /* Floating Point Context Control Register. */\r
86 #define portFPCCR_ASPEN_POS                 ( 31UL )\r
87 #define portFPCCR_ASPEN_MASK                ( 1UL << portFPCCR_ASPEN_POS )\r
88 #define portFPCCR_LSPEN_POS                 ( 30UL )\r
89 #define portFPCCR_LSPEN_MASK                ( 1UL << portFPCCR_LSPEN_POS )\r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /**\r
93  * @brief Constants required to manipulate the MPU.\r
94  */\r
95 #define portMPU_TYPE_REG                    ( * ( ( volatile uint32_t * ) 0xe000ed90 ) )\r
96 #define portMPU_CTRL_REG                    ( * ( ( volatile uint32_t * ) 0xe000ed94 ) )\r
97 #define portMPU_RNR_REG                     ( * ( ( volatile uint32_t * ) 0xe000ed98 ) )\r
98 \r
99 #define portMPU_RBAR_REG                    ( * ( ( volatile uint32_t * ) 0xe000ed9c ) )\r
100 #define portMPU_RLAR_REG                    ( * ( ( volatile uint32_t * ) 0xe000eda0 ) )\r
101 \r
102 #define portMPU_RBAR_A1_REG                 ( * ( ( volatile uint32_t * ) 0xe000eda4 ) )\r
103 #define portMPU_RLAR_A1_REG                 ( * ( ( volatile uint32_t * ) 0xe000eda8 ) )\r
104 \r
105 #define portMPU_RBAR_A2_REG                 ( * ( ( volatile uint32_t * ) 0xe000edac ) )\r
106 #define portMPU_RLAR_A2_REG                 ( * ( ( volatile uint32_t * ) 0xe000edb0 ) )\r
107 \r
108 #define portMPU_RBAR_A3_REG                 ( * ( ( volatile uint32_t * ) 0xe000edb4 ) )\r
109 #define portMPU_RLAR_A3_REG                 ( * ( ( volatile uint32_t * ) 0xe000edb8 ) )\r
110 \r
111 #define portMPU_MAIR0_REG                   ( * ( ( volatile uint32_t * ) 0xe000edc0 ) )\r
112 #define portMPU_MAIR1_REG                   ( * ( ( volatile uint32_t * ) 0xe000edc4 ) )\r
113 \r
114 #define portMPU_RBAR_ADDRESS_MASK           ( 0xffffffe0 ) /* Must be 32-byte aligned. */\r
115 #define portMPU_RLAR_ADDRESS_MASK           ( 0xffffffe0 ) /* Must be 32-byte aligned. */\r
116 \r
117 #define portMPU_MAIR_ATTR0_POS              ( 0UL )\r
118 #define portMPU_MAIR_ATTR0_MASK             ( 0x000000ff )\r
119 \r
120 #define portMPU_MAIR_ATTR1_POS              ( 8UL )\r
121 #define portMPU_MAIR_ATTR1_MASK             ( 0x0000ff00 )\r
122 \r
123 #define portMPU_MAIR_ATTR2_POS              ( 16UL )\r
124 #define portMPU_MAIR_ATTR2_MASK             ( 0x00ff0000 )\r
125 \r
126 #define portMPU_MAIR_ATTR3_POS              ( 24UL )\r
127 #define portMPU_MAIR_ATTR3_MASK             ( 0xff000000 )\r
128 \r
129 #define portMPU_MAIR_ATTR4_POS              ( 0UL )\r
130 #define portMPU_MAIR_ATTR4_MASK             ( 0x000000ff )\r
131 \r
132 #define portMPU_MAIR_ATTR5_POS              ( 8UL )\r
133 #define portMPU_MAIR_ATTR5_MASK             ( 0x0000ff00 )\r
134 \r
135 #define portMPU_MAIR_ATTR6_POS              ( 16UL )\r
136 #define portMPU_MAIR_ATTR6_MASK             ( 0x00ff0000 )\r
137 \r
138 #define portMPU_MAIR_ATTR7_POS              ( 24UL )\r
139 #define portMPU_MAIR_ATTR7_MASK             ( 0xff000000 )\r
140 \r
141 #define portMPU_RLAR_ATTR_INDEX0            (  0UL << 1UL )\r
142 #define portMPU_RLAR_ATTR_INDEX1            (  1UL << 1UL )\r
143 #define portMPU_RLAR_ATTR_INDEX2            (  2UL << 1UL )\r
144 #define portMPU_RLAR_ATTR_INDEX3            (  3UL << 1UL )\r
145 #define portMPU_RLAR_ATTR_INDEX4            (  4UL << 1UL )\r
146 #define portMPU_RLAR_ATTR_INDEX5            (  5UL << 1UL )\r
147 #define portMPU_RLAR_ATTR_INDEX6            (  6UL << 1UL )\r
148 #define portMPU_RLAR_ATTR_INDEX7            (  7UL << 1UL )\r
149 \r
150 #define portMPU_RLAR_REGION_ENABLE          ( 1UL )\r
151 \r
152 /* Enable privileged access to unmapped region. */\r
153 #define portMPU_PRIV_BACKGROUND_ENABLE      ( 1UL << 2UL )\r
154 \r
155 /* Enable MPU. */\r
156 #define portMPU_ENABLE                      ( 1UL << 0UL )\r
157 \r
158 /* Expected value of the portMPU_TYPE register. */\r
159 #define portEXPECTED_MPU_TYPE_VALUE         ( 8UL << 8UL ) /* 8 regions, unified. */\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /**\r
163  * @brief Constants required to set up the initial stack.\r
164  */\r
165 #define portINITIAL_XPSR                    ( 0x01000000 )\r
166 \r
167 /**\r
168  * @brief Initial EXC_RETURN value.\r
169  *\r
170  *     FF         FF         FF         BC\r
171  * 1111 1111  1111 1111  1111 1111  1011 1100\r
172  *\r
173  * Bit[6] - 0 --> The exception was taken from the Non-Secure state.\r
174  * Bit[5] - 1 --> Do not skip stacking of additional state context.\r
175  * Bit[4] - 1 --> The PE did not allocate space on the stack for FP context.\r
176  * Bit[3] - 1 --> Return to the Thread mode.\r
177  * Bit[2] - 1 --> Restore registers from the process stack.\r
178  * Bit[1] - 0 --> Reserved, 0.\r
179  * Bit[0] - 0 --> The exception was taken to the Non-Secure state.\r
180  */\r
181 #define portINITIAL_EXC_RETURN              ( 0xffffffbc )\r
182 \r
183 /**\r
184  * @brief CONTROL register privileged bit mask.\r
185  *\r
186  * Bit[0] in CONTROL register tells the privilege:\r
187  *  Bit[0] = 0 ==> The task is privileged.\r
188  *  Bit[0] = 1 ==> The task is not privileged.\r
189  */\r
190 #define portCONTROL_PRIVILEGED_MASK         ( 1UL << 0UL )\r
191 \r
192 /**\r
193  * @brief Initial CONTROL register values.\r
194  */\r
195 #define portINITIAL_CONTROL_UNPRIVILEGED    ( 0x3 )\r
196 #define portINITIAL_CONTROL_PRIVILEGED      ( 0x2 )\r
197 \r
198 /**\r
199  * @brief Let the user override the pre-loading of the initial LR with the\r
200  * address of prvTaskExitError() in case it messes up unwinding of the stack\r
201  * in the debugger.\r
202  */\r
203 #ifdef configTASK_RETURN_ADDRESS\r
204     #define portTASK_RETURN_ADDRESS         configTASK_RETURN_ADDRESS\r
205 #else\r
206     #define portTASK_RETURN_ADDRESS         prvTaskExitError\r
207 #endif\r
208 \r
209 /**\r
210  * @brief If portPRELOAD_REGISTERS then registers will be given an initial value\r
211  * when a task is created. This helps in debugging at the cost of code size.\r
212  */\r
213 #define portPRELOAD_REGISTERS               1\r
214 \r
215 /**\r
216  * @brief A task is created without a secure context, and must call\r
217  * portALLOCATE_SECURE_CONTEXT() to give itself a secure context before it makes\r
218  * any secure calls.\r
219  */\r
220 #define portNO_SECURE_CONTEXT               0\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 /**\r
224  * @brief Setup the timer to generate the tick interrupts.\r
225  */\r
226 static void prvSetupTimerInterrupt( void ) PRIVILEGED_FUNCTION;\r
227 \r
228 /**\r
229  * @brief Used to catch tasks that attempt to return from their implementing\r
230  * function.\r
231  */\r
232 static void prvTaskExitError( void );\r
233 \r
234 #if( configENABLE_MPU == 1 )\r
235     /**\r
236      * @brief Setup the Memory Protection Unit (MPU).\r
237      */\r
238     static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;\r
239 #endif /* configENABLE_MPU */\r
240 \r
241 #if( configENABLE_FPU == 1 )\r
242     /**\r
243      * @brief Setup the Floating Point Unit (FPU).\r
244      */\r
245     static void prvSetupFPU( void ) PRIVILEGED_FUNCTION;\r
246 #endif /* configENABLE_FPU */\r
247 \r
248 /**\r
249  * @brief Yield the processor.\r
250  */\r
251 void vPortYield( void ) PRIVILEGED_FUNCTION;\r
252 \r
253 /**\r
254  * @brief Enter critical section.\r
255  */\r
256 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;\r
257 \r
258 /**\r
259  * @brief Exit from critical section.\r
260  */\r
261 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;\r
262 \r
263 /**\r
264  * @brief SysTick handler.\r
265  */\r
266 void SysTick_Handler( void ) PRIVILEGED_FUNCTION;\r
267 \r
268 /**\r
269  * @brief C part of SVC handler.\r
270  */\r
271 void vPortSVCHandler_C( uint32_t *pulCallerStackAddress ) PRIVILEGED_FUNCTION;\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 /**\r
275  * @brief Each task maintains its own interrupt status in the critical nesting\r
276  * variable.\r
277  */\r
278 static volatile uint32_t ulCriticalNesting = 0xaaaaaaaaUL;\r
279 \r
280 #if( configENABLE_TRUSTZONE == 1 )\r
281     /**\r
282      * @brief Saved as part of the task context to indicate which context the\r
283      * task is using on the secure side.\r
284      */\r
285     volatile SecureContextHandle_t xSecureContext = portNO_SECURE_CONTEXT;\r
286 #endif /* configENABLE_TRUSTZONE */\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 static void prvSetupTimerInterrupt( void ) /* PRIVILEGED_FUNCTION */\r
290 {\r
291     /* Stop and reset the SysTick. */\r
292     *( portNVIC_SYSTICK_CTRL ) = 0UL;\r
293     *( portNVIC_SYSTICK_CURRENT_VALUE ) = 0UL;\r
294 \r
295     /* Configure SysTick to interrupt at the requested rate. */\r
296     *( portNVIC_SYSTICK_LOAD ) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
297     *( portNVIC_SYSTICK_CTRL ) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 static void prvTaskExitError( void )\r
302 {\r
303 volatile uint32_t ulDummy = 0UL;\r
304 \r
305     /* A function that implements a task must not exit or attempt to return to\r
306      * its caller as there is nothing to return to. If a task wants to exit it\r
307      * should instead call vTaskDelete( NULL ). Artificially force an assert()\r
308      * to be triggered if configASSERT() is defined, then stop here so\r
309      * application writers can catch the error. */\r
310     configASSERT( ulCriticalNesting == ~0UL );\r
311     portDISABLE_INTERRUPTS();\r
312 \r
313     while( ulDummy == 0 )\r
314     {\r
315         /* This file calls prvTaskExitError() after the scheduler has been\r
316          * started to remove a compiler warning about the function being\r
317          * defined but never called.  ulDummy is used purely to quieten other\r
318          * warnings about code appearing after this function is called - making\r
319          * ulDummy volatile makes the compiler think the function could return\r
320          * and therefore not output an 'unreachable code' warning for code that\r
321          * appears after it. */\r
322     }\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 #if( configENABLE_MPU == 1 )\r
327     static void prvSetupMPU( void ) /* PRIVILEGED_FUNCTION */\r
328     {\r
329     #if defined( __ARMCC_VERSION )\r
330         /* Declaration when these variable are defined in code instead of being\r
331          * exported from linker scripts. */\r
332         extern uint32_t * __privileged_functions_start__;\r
333         extern uint32_t * __privileged_functions_end__;\r
334         extern uint32_t * __syscalls_flash_start__;\r
335         extern uint32_t * __unprivileged_flash_end__;\r
336         extern uint32_t * __privileged_sram_start__;\r
337         extern uint32_t * __privileged_sram_end__;\r
338     #else\r
339         /* Declaration when these variable are exported from linker scripts. */\r
340         extern uint32_t __privileged_functions_start__[];\r
341         extern uint32_t __privileged_functions_end__[];\r
342         extern uint32_t __syscalls_flash_start__[];\r
343         extern uint32_t __unprivileged_flash_end__[];\r
344         extern uint32_t __privileged_sram_start__[];\r
345         extern uint32_t __privileged_sram_end__[];\r
346     #endif /* defined( __ARMCC_VERSION ) */\r
347 \r
348         /* Check that the MPU is present. */\r
349         if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )\r
350         {\r
351             /* MAIR0 - Index 0. */\r
352             portMPU_MAIR0_REG |= ( ( portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE << portMPU_MAIR_ATTR0_POS ) & portMPU_MAIR_ATTR0_MASK );\r
353             /* MAIR0 - Index 1. */\r
354             portMPU_MAIR0_REG |= ( ( portMPU_DEVICE_MEMORY_nGnRE << portMPU_MAIR_ATTR1_POS ) & portMPU_MAIR_ATTR1_MASK );\r
355 \r
356             /* Setup privileged flash as Read Only so that privileged tasks can\r
357              * read it but not modify. */\r
358             portMPU_RNR_REG = portPRIVILEGED_FLASH_REGION;\r
359             portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_functions_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
360                             ( portMPU_REGION_NON_SHAREABLE ) |\r
361                             ( portMPU_REGION_PRIVILEGED_READ_ONLY );\r
362             portMPU_RLAR_REG = ( ( ( uint32_t ) __privileged_functions_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
363                             ( portMPU_RLAR_ATTR_INDEX0 ) |\r
364                             ( portMPU_RLAR_REGION_ENABLE );\r
365 \r
366             /* Setup unprivileged flash and system calls flash as Read Only by\r
367              * both privileged and unprivileged tasks. All tasks can read it but\r
368              * no-one can modify. */\r
369             portMPU_RNR_REG = portUNPRIVILEGED_FLASH_REGION;\r
370             portMPU_RBAR_REG = ( ( ( uint32_t ) __syscalls_flash_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
371                             ( portMPU_REGION_NON_SHAREABLE ) |\r
372                             ( portMPU_REGION_READ_ONLY );\r
373             portMPU_RLAR_REG = ( ( ( uint32_t ) __unprivileged_flash_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
374                             ( portMPU_RLAR_ATTR_INDEX0 ) |\r
375                             ( portMPU_RLAR_REGION_ENABLE );\r
376 \r
377             /* Setup RAM containing kernel data for privileged access only. */\r
378             portMPU_RNR_REG = portPRIVILEGED_RAM_REGION;\r
379             portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_sram_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
380                             ( portMPU_REGION_NON_SHAREABLE ) |\r
381                             ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
382                             ( portMPU_REGION_EXECUTE_NEVER );\r
383             portMPU_RLAR_REG = ( ( ( uint32_t ) __privileged_sram_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
384                             ( portMPU_RLAR_ATTR_INDEX0 ) |\r
385                             ( portMPU_RLAR_REGION_ENABLE );\r
386 \r
387             /* By default allow everything to access the general peripherals.\r
388              * The system peripherals and registers are protected. */\r
389             portMPU_RNR_REG = portUNPRIVILEGED_DEVICE_REGION;\r
390             portMPU_RBAR_REG = ( ( ( uint32_t ) portDEVICE_REGION_START_ADDRESS ) & portMPU_RBAR_ADDRESS_MASK ) |\r
391                             ( portMPU_REGION_NON_SHAREABLE ) |\r
392                             ( portMPU_REGION_READ_WRITE ) |\r
393                             ( portMPU_REGION_EXECUTE_NEVER );\r
394             portMPU_RLAR_REG = ( ( ( uint32_t ) portDEVICE_REGION_END_ADDRESS ) & portMPU_RLAR_ADDRESS_MASK ) |\r
395                             ( portMPU_RLAR_ATTR_INDEX1 ) |\r
396                             ( portMPU_RLAR_REGION_ENABLE );\r
397 \r
398             /* Enable mem fault. */\r
399             portSCB_SYS_HANDLER_CTRL_STATE_REG |= portSCB_MEM_FAULT_ENABLE;\r
400 \r
401             /* Enable MPU with privileged background access i.e. unmapped\r
402              * regions have privileged access. */\r
403             portMPU_CTRL_REG |= ( portMPU_PRIV_BACKGROUND_ENABLE | portMPU_ENABLE );\r
404         }\r
405     }\r
406 #endif /* configENABLE_MPU */\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 #if( configENABLE_FPU == 1 )\r
410     static void prvSetupFPU( void ) /* PRIVILEGED_FUNCTION */\r
411     {\r
412         #if( configENABLE_TRUSTZONE == 1 )\r
413         {\r
414             /* Enable non-secure access to the FPU. */\r
415             SecureInit_EnableNSFPUAccess();\r
416         }\r
417         #endif /* configENABLE_TRUSTZONE */\r
418 \r
419         /* CP10 = 11 ==> Full access to FPU i.e. both privileged and\r
420          * unprivileged code should be able to access FPU. CP11 should be\r
421          * programmed to the same value as CP10. */\r
422         *( portCPACR ) |= ( ( portCPACR_CP10_VALUE << portCPACR_CP10_POS ) |\r
423                             ( portCPACR_CP11_VALUE << portCPACR_CP11_POS )\r
424                         );\r
425 \r
426         /* ASPEN = 1 ==> Hardware should automatically preserve floating point\r
427          * context on exception entry and restore on exception return.\r
428          * LSPEN = 1 ==> Enable lazy context save of FP state. */\r
429         *( portFPCCR ) |= ( portFPCCR_ASPEN_MASK | portFPCCR_LSPEN_MASK );\r
430     }\r
431 #endif /* configENABLE_FPU */\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 void vPortYield( void ) /* PRIVILEGED_FUNCTION */\r
435 {\r
436     /* Set a PendSV to request a context switch. */\r
437     *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
438 \r
439     /* Barriers are normally not required but do ensure the code is\r
440      * completely within the specified behaviour for the architecture. */\r
441     __asm volatile( "dsb" ::: "memory" );\r
442     __asm volatile( "isb" );\r
443 }\r
444 /*-----------------------------------------------------------*/\r
445 \r
446 void vPortEnterCritical( void ) /* PRIVILEGED_FUNCTION */\r
447 {\r
448     portDISABLE_INTERRUPTS();\r
449     ulCriticalNesting++;\r
450 \r
451     /* Barriers are normally not required but do ensure the code is\r
452      * completely within the specified behaviour for the architecture. */\r
453     __asm volatile( "dsb" ::: "memory" );\r
454     __asm volatile( "isb" );\r
455 }\r
456 /*-----------------------------------------------------------*/\r
457 \r
458 void vPortExitCritical( void ) /* PRIVILEGED_FUNCTION */\r
459 {\r
460     configASSERT( ulCriticalNesting );\r
461     ulCriticalNesting--;\r
462 \r
463     if( ulCriticalNesting == 0 )\r
464     {\r
465         portENABLE_INTERRUPTS();\r
466     }\r
467 }\r
468 /*-----------------------------------------------------------*/\r
469 \r
470 void SysTick_Handler( void ) /* PRIVILEGED_FUNCTION */\r
471 {\r
472 uint32_t ulPreviousMask;\r
473 \r
474     ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
475     {\r
476         /* Increment the RTOS tick. */\r
477         if( xTaskIncrementTick() != pdFALSE )\r
478         {\r
479             /* Pend a context switch. */\r
480             *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;\r
481         }\r
482     }\r
483     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
484 }\r
485 /*-----------------------------------------------------------*/\r
486 \r
487 void vPortSVCHandler_C( uint32_t *pulCallerStackAddress ) /* PRIVILEGED_FUNCTION */\r
488 {\r
489 #if( configENABLE_MPU == 1 )\r
490     #if defined( __ARMCC_VERSION )\r
491         /* Declaration when these variable are defined in code instead of being\r
492          * exported from linker scripts. */\r
493         extern uint32_t * __syscalls_flash_start__;\r
494         extern uint32_t * __syscalls_flash_end__;\r
495     #else\r
496         /* Declaration when these variable are exported from linker scripts. */\r
497         extern uint32_t __syscalls_flash_start__[];\r
498         extern uint32_t __syscalls_flash_end__[];\r
499     #endif /* defined( __ARMCC_VERSION ) */\r
500 #endif /* configENABLE_MPU */\r
501 \r
502 uint32_t ulPC;\r
503 \r
504 #if( configENABLE_TRUSTZONE == 1 )\r
505     uint32_t ulR0;\r
506     #if( configENABLE_MPU == 1 )\r
507         uint32_t ulControl, ulIsTaskPrivileged;\r
508     #endif /* configENABLE_MPU */\r
509 #endif /* configENABLE_TRUSTZONE */\r
510 uint8_t ucSVCNumber;\r
511 \r
512     /* Register are stored on the stack in the following order - R0, R1, R2, R3,\r
513      * R12, LR, PC, xPSR. */\r
514     ulPC = pulCallerStackAddress[ 6 ];\r
515     ucSVCNumber = ( ( uint8_t *) ulPC )[ -2 ];\r
516 \r
517     switch( ucSVCNumber )\r
518     {\r
519         #if( configENABLE_TRUSTZONE == 1 )\r
520             case portSVC_ALLOCATE_SECURE_CONTEXT:\r
521             {\r
522                 /* R0 contains the stack size passed as parameter to the\r
523                  * vPortAllocateSecureContext function. */\r
524                 ulR0 = pulCallerStackAddress[ 0 ];\r
525 \r
526                 #if( configENABLE_MPU == 1 )\r
527                 {\r
528                     /* Read the CONTROL register value. */\r
529                     __asm volatile ( "mrs %0, control"  : "=r" ( ulControl ) );\r
530 \r
531                     /* The task that raised the SVC is privileged if Bit[0]\r
532                      * in the CONTROL register is 0. */\r
533                     ulIsTaskPrivileged = ( ( ulControl & portCONTROL_PRIVILEGED_MASK ) == 0 );\r
534 \r
535                     /* Allocate and load a context for the secure task. */\r
536                     xSecureContext = SecureContext_AllocateContext( ulR0, ulIsTaskPrivileged );\r
537                 }\r
538                 #else\r
539                 {\r
540                     /* Allocate and load a context for the secure task. */\r
541                     xSecureContext = SecureContext_AllocateContext( ulR0 );\r
542                 }\r
543                 #endif /* configENABLE_MPU */\r
544 \r
545                 configASSERT( xSecureContext != NULL );\r
546                 SecureContext_LoadContext( xSecureContext );\r
547             }\r
548             break;\r
549 \r
550             case portSVC_FREE_SECURE_CONTEXT:\r
551             {\r
552                 /* R0 contains the secure context handle to be freed. */\r
553                 ulR0 = pulCallerStackAddress[ 0 ];\r
554 \r
555                 /* Free the secure context. */\r
556                 SecureContext_FreeContext( ( SecureContextHandle_t ) ulR0 );\r
557             }\r
558             break;\r
559         #endif /* configENABLE_TRUSTZONE */\r
560 \r
561         case portSVC_START_SCHEDULER:\r
562         {\r
563             #if( configENABLE_TRUSTZONE == 1 )\r
564             {\r
565                 /* De-prioritize the non-secure exceptions so that the\r
566                  * non-secure pendSV runs at the lowest priority. */\r
567                 SecureInit_DePrioritizeNSExceptions();\r
568 \r
569                 /* Initialize the secure context management system. */\r
570                 SecureContext_Init();\r
571             }\r
572             #endif /* configENABLE_TRUSTZONE */\r
573 \r
574             #if( configENABLE_FPU == 1 )\r
575             {\r
576                 /* Setup the Floating Point Unit (FPU). */\r
577                 prvSetupFPU();\r
578             }\r
579             #endif /* configENABLE_FPU */\r
580 \r
581             /* Setup the context of the first task so that the first task starts\r
582              * executing. */\r
583             vRestoreContextOfFirstTask();\r
584         }\r
585         break;\r
586 \r
587         #if( configENABLE_MPU == 1 )\r
588             case portSVC_RAISE_PRIVILEGE:\r
589             {\r
590                 /* Only raise the privilege, if the svc was raised from any of\r
591                  * the system calls. */\r
592                 if( ulPC >= ( uint32_t ) __syscalls_flash_start__ &&\r
593                     ulPC <= ( uint32_t ) __syscalls_flash_end__ )\r
594                 {\r
595                    vRaisePrivilege();\r
596                 }\r
597             }\r
598             break;\r
599         #endif /* configENABLE_MPU */\r
600 \r
601         default:\r
602         {\r
603             /* Incorrect SVC call. */\r
604             configASSERT( pdFALSE );\r
605         }\r
606     }\r
607 }\r
608 /*-----------------------------------------------------------*/\r
609 \r
610 #if( configENABLE_MPU == 1 )\r
611     StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, StackType_t *pxEndOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged ) /* PRIVILEGED_FUNCTION */\r
612 #else\r
613     StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, StackType_t *pxEndOfStack, TaskFunction_t pxCode, void *pvParameters ) /* PRIVILEGED_FUNCTION */\r
614 #endif /* configENABLE_MPU */\r
615 {\r
616     /* Simulate the stack frame as it would be created by a context switch\r
617      * interrupt. */\r
618     #if( portPRELOAD_REGISTERS == 0 )\r
619     {\r
620         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
621         *pxTopOfStack = portINITIAL_XPSR;                           /* xPSR */\r
622         pxTopOfStack--;\r
623         *pxTopOfStack = ( StackType_t ) pxCode;                     /* PC */\r
624         pxTopOfStack--;\r
625         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;    /* LR */\r
626         pxTopOfStack -= 5;                                          /* R12, R3, R2 and R1. */\r
627         *pxTopOfStack = ( StackType_t ) pvParameters;               /* R0 */\r
628         pxTopOfStack -= 9;                                          /* R11..R4, EXC_RETURN. */\r
629         *pxTopOfStack = portINITIAL_EXC_RETURN;\r
630 \r
631         #if( configENABLE_MPU == 1 )\r
632         {\r
633             pxTopOfStack--;\r
634             if( xRunPrivileged == pdTRUE )\r
635             {\r
636                 *pxTopOfStack = portINITIAL_CONTROL_PRIVILEGED;     /* Slot used to hold this task's CONTROL value. */\r
637             }\r
638             else\r
639             {\r
640                 *pxTopOfStack = portINITIAL_CONTROL_UNPRIVILEGED;   /* Slot used to hold this task's CONTROL value. */\r
641             }\r
642         }\r
643         #endif /* configENABLE_MPU */\r
644 \r
645         pxTopOfStack--;\r
646         *pxTopOfStack = ( StackType_t ) pxEndOfStack;   /* Slot used to hold this task's PSPLIM value. */\r
647 \r
648         #if( configENABLE_TRUSTZONE == 1 )\r
649         {\r
650             pxTopOfStack--;\r
651             *pxTopOfStack = portNO_SECURE_CONTEXT;      /* Slot used to hold this task's xSecureContext value. */\r
652         }\r
653         #endif /* configENABLE_TRUSTZONE */\r
654     }\r
655     #else /* portPRELOAD_REGISTERS */\r
656     {\r
657         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
658         *pxTopOfStack = portINITIAL_XPSR;                           /* xPSR */\r
659         pxTopOfStack--;\r
660         *pxTopOfStack = ( StackType_t ) pxCode;                     /* PC */\r
661         pxTopOfStack--;\r
662         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;    /* LR */\r
663         pxTopOfStack--;\r
664         *pxTopOfStack = ( StackType_t ) 0x12121212UL;               /* R12 */\r
665         pxTopOfStack--;\r
666         *pxTopOfStack = ( StackType_t ) 0x03030303UL;               /* R3 */\r
667         pxTopOfStack--;\r
668         *pxTopOfStack = ( StackType_t ) 0x02020202UL;               /* R2 */\r
669         pxTopOfStack--;\r
670         *pxTopOfStack = ( StackType_t ) 0x01010101UL;               /* R1 */\r
671         pxTopOfStack--;\r
672         *pxTopOfStack = ( StackType_t ) pvParameters;               /* R0 */\r
673         pxTopOfStack--;\r
674         *pxTopOfStack = ( StackType_t ) 0x11111111UL;               /* R11 */\r
675         pxTopOfStack--;\r
676         *pxTopOfStack = ( StackType_t ) 0x10101010UL;               /* R10 */\r
677         pxTopOfStack--;\r
678         *pxTopOfStack = ( StackType_t ) 0x09090909UL;               /* R09 */\r
679         pxTopOfStack--;\r
680         *pxTopOfStack = ( StackType_t ) 0x08080808UL;               /* R08 */\r
681         pxTopOfStack--;\r
682         *pxTopOfStack = ( StackType_t ) 0x07070707UL;               /* R07 */\r
683         pxTopOfStack--;\r
684         *pxTopOfStack = ( StackType_t ) 0x06060606UL;               /* R06 */\r
685         pxTopOfStack--;\r
686         *pxTopOfStack = ( StackType_t ) 0x05050505UL;               /* R05 */\r
687         pxTopOfStack--;\r
688         *pxTopOfStack = ( StackType_t ) 0x04040404UL;               /* R04 */\r
689         pxTopOfStack--;\r
690         *pxTopOfStack = portINITIAL_EXC_RETURN;                     /* EXC_RETURN */\r
691 \r
692         #if( configENABLE_MPU == 1 )\r
693         {\r
694             pxTopOfStack--;\r
695             if( xRunPrivileged == pdTRUE )\r
696             {\r
697                 *pxTopOfStack = portINITIAL_CONTROL_PRIVILEGED;     /* Slot used to hold this task's CONTROL value. */\r
698             }\r
699             else\r
700             {\r
701                 *pxTopOfStack = portINITIAL_CONTROL_UNPRIVILEGED;   /* Slot used to hold this task's CONTROL value. */\r
702             }\r
703         }\r
704         #endif /* configENABLE_MPU */\r
705 \r
706         pxTopOfStack--;\r
707         *pxTopOfStack = ( StackType_t ) pxEndOfStack;   /* Slot used to hold this task's PSPLIM value. */\r
708 \r
709         #if( configENABLE_TRUSTZONE == 1 )\r
710         {\r
711             pxTopOfStack--;\r
712             *pxTopOfStack = portNO_SECURE_CONTEXT;      /* Slot used to hold this task's xSecureContext value. */\r
713         }\r
714         #endif /* configENABLE_TRUSTZONE */\r
715     }\r
716     #endif /* portPRELOAD_REGISTERS */\r
717 \r
718     return pxTopOfStack;\r
719 }\r
720 /*-----------------------------------------------------------*/\r
721 \r
722 BaseType_t xPortStartScheduler( void ) /* PRIVILEGED_FUNCTION */\r
723 {\r
724     /* Make PendSV, CallSV and SysTick the same priority as the kernel. */\r
725     *( portNVIC_SYSPRI2 ) |= portNVIC_PENDSV_PRI;\r
726     *( portNVIC_SYSPRI2 ) |= portNVIC_SYSTICK_PRI;\r
727 \r
728     #if( configENABLE_MPU == 1 )\r
729     {\r
730         /* Setup the Memory Protection Unit (MPU). */\r
731         prvSetupMPU();\r
732     }\r
733     #endif /* configENABLE_MPU */\r
734 \r
735     /* Start the timer that generates the tick ISR. Interrupts are disabled\r
736      * here already. */\r
737     prvSetupTimerInterrupt();\r
738 \r
739     /* Initialize the critical nesting count ready for the first task. */\r
740     ulCriticalNesting = 0;\r
741 \r
742     /* Start the first task. */\r
743     vStartFirstTask();\r
744 \r
745     /* Should never get here as the tasks will now be executing. Call the task\r
746      * exit error function to prevent compiler warnings about a static function\r
747      * not being called in the case that the application writer overrides this\r
748      * functionality by defining configTASK_RETURN_ADDRESS. Call\r
749      * vTaskSwitchContext() so link time optimization does not remove the\r
750      * symbol. */\r
751     vTaskSwitchContext();\r
752     prvTaskExitError();\r
753 \r
754     /* Should not get here. */\r
755     return 0;\r
756 }\r
757 /*-----------------------------------------------------------*/\r
758 \r
759 void vPortEndScheduler( void ) /* PRIVILEGED_FUNCTION */\r
760 {\r
761     /* Not implemented in ports where there is nothing to return to.\r
762      * Artificially force an assert. */\r
763     configASSERT( ulCriticalNesting == 1000UL );\r
764 }\r
765 /*-----------------------------------------------------------*/\r
766 \r
767 #if( configENABLE_MPU == 1 )\r
768     void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth )\r
769     {\r
770     uint32_t ulRegionStartAddress, ulRegionEndAddress, ulRegionNumber;\r
771     int32_t lIndex = 0;\r
772 \r
773         /* Setup MAIR0. */\r
774         xMPUSettings->ulMAIR0 = ( ( portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE << portMPU_MAIR_ATTR0_POS ) & portMPU_MAIR_ATTR0_MASK );\r
775         xMPUSettings->ulMAIR0 |= ( ( portMPU_DEVICE_MEMORY_nGnRE << portMPU_MAIR_ATTR1_POS ) & portMPU_MAIR_ATTR1_MASK );\r
776 \r
777         /* This function is called automatically when the task is created - in\r
778          * which case the stack region parameters will be valid.  At all other\r
779          * times the stack parameters will not be valid and it is assumed that\r
780          * the stack region has already been configured. */\r
781         if( ulStackDepth > 0 )\r
782         {\r
783             /* Define the region that allows access to the stack. */\r
784             ulRegionStartAddress = ( ( uint32_t ) pxBottomOfStack ) & portMPU_RBAR_ADDRESS_MASK;\r
785             ulRegionEndAddress = ( uint32_t ) pxBottomOfStack + ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1;\r
786             ulRegionEndAddress  &= portMPU_RLAR_ADDRESS_MASK;\r
787 \r
788             xMPUSettings->xRegionsSettings[ 0 ].ulRBAR = ( ulRegionStartAddress ) |\r
789                                                         ( portMPU_REGION_NON_SHAREABLE ) |\r
790                                                         ( portMPU_REGION_READ_WRITE ) |\r
791                                                         ( portMPU_REGION_EXECUTE_NEVER );\r
792 \r
793             xMPUSettings->xRegionsSettings[ 0 ].ulRLAR = ( ulRegionEndAddress ) |\r
794                                                         ( portMPU_RLAR_ATTR_INDEX0 ) |\r
795                                                         ( portMPU_RLAR_REGION_ENABLE );\r
796         }\r
797 \r
798         /* User supplied configurable regions. */\r
799         for( ulRegionNumber = 1; ulRegionNumber <= portNUM_CONFIGURABLE_REGIONS; ulRegionNumber++ )\r
800         {\r
801             /* If xRegions is NULL i.e. the task has not specified any MPU\r
802              * region, the else part ensures that all the configurable MPU\r
803              * regions are invalidated. */\r
804             if( ( xRegions != NULL ) && ( xRegions[ lIndex ].ulLengthInBytes > 0UL ) )\r
805             {\r
806                 /* Translate the generic region definition contained in xRegions\r
807                  * into the ARMv8 specific MPU settings that are then stored in\r
808                  * xMPUSettings. */\r
809                 ulRegionStartAddress = ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) & portMPU_RBAR_ADDRESS_MASK;\r
810                 ulRegionEndAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1;\r
811                 ulRegionEndAddress  &= portMPU_RLAR_ADDRESS_MASK;\r
812 \r
813                 /* Start address. */\r
814                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR = ( ulRegionStartAddress ) |\r
815                                                                         ( portMPU_REGION_NON_SHAREABLE );\r
816 \r
817                 /* RO/RW. */\r
818                 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_READ_ONLY ) != 0 )\r
819                 {\r
820                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_READ_ONLY );\r
821                 }\r
822                 else\r
823                 {\r
824                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_READ_WRITE );\r
825                 }\r
826 \r
827                 /* XN. */\r
828                 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_EXECUTE_NEVER ) != 0 )\r
829                 {\r
830                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_EXECUTE_NEVER );\r
831                 }\r
832 \r
833                 /* End Address. */\r
834                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR = ( ulRegionEndAddress ) |\r
835                                                                         ( portMPU_RLAR_REGION_ENABLE );\r
836 \r
837                 /* Normal memory/ Device memory. */\r
838                 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_DEVICE_MEMORY ) != 0 )\r
839                 {\r
840                     /* Attr1 in MAIR0 is configured as device memory. */\r
841                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR |= portMPU_RLAR_ATTR_INDEX1;\r
842                 }\r
843                 else\r
844                 {\r
845                     /* Attr1 in MAIR0 is configured as normal memory. */\r
846                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR |= portMPU_RLAR_ATTR_INDEX0;\r
847                 }\r
848             }\r
849             else\r
850             {\r
851                 /* Invalidate the region. */\r
852                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR = 0UL;\r
853                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR = 0UL;\r
854             }\r
855 \r
856             lIndex++;\r
857         }\r
858     }\r
859 #endif /* configENABLE_MPU */\r
860 /*-----------------------------------------------------------*/\r