1 /*
2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
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.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ARM CM4 MPU port.
31 *----------------------------------------------------------*/
32
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
37
38 /* Scheduler includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41 #include "mpu_syscall_numbers.h"
42
43 #ifndef __ARM_FP
44 #error This port can only be used when the project options are configured to enable hardware floating point support.
45 #endif
46
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
48
49 #ifndef configSYSTICK_CLOCK_HZ
50 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
51 /* Ensure the SysTick is clocked at the same frequency as the core. */
52 #define portNVIC_SYSTICK_CLK ( 1UL << 2UL )
53 #else
54
55 /* The way the SysTick is clocked is not modified in case it is not the same
56 * as the core. */
57 #define portNVIC_SYSTICK_CLK ( 0 )
58 #endif
59
60 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
61 #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
62 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
63 #endif
64
65 /* Prototype of all Interrupt Service Routines (ISRs). */
66 typedef void ( * portISR_t )( void );
67
68 /* Constants required to access and manipulate the NVIC. */
69 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
70 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
71 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
72 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
73 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
74 #define portNVIC_SYS_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
75 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
76
77 /* Constants used to detect Cortex-M7 r0p0 and r0p1 cores, and ensure
78 * that a work around is active for errata 837070. */
79 #define portCPUID ( *( ( volatile uint32_t * ) 0xE000ed00 ) )
80 #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
81 #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
82
83 /* Constants required to access and manipulate the MPU. */
84 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
85 #define portMPU_REGION_BASE_ADDRESS_REG ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
86 #define portMPU_REGION_ATTRIBUTE_REG ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
87 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
88 #define portEXPECTED_MPU_TYPE_VALUE ( configTOTAL_MPU_REGIONS << 8UL )
89 #define portMPU_ENABLE ( 0x01UL )
90 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
91 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
92 #define portMPU_REGION_VALID ( 0x10UL )
93 #define portMPU_REGION_ENABLE ( 0x01UL )
94 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
95 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
96
97 /* Constants required to access and manipulate the SysTick. */
98 #define portNVIC_SYSTICK_INT ( 0x00000002UL )
99 #define portNVIC_SYSTICK_ENABLE ( 0x00000001UL )
100 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
101 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
102 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
103
104 /* Constants required to manipulate the VFP. */
105 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34UL ) /* Floating point context control register. */
106 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
107
108 /* Constants required to set up the initial stack. */
109 #define portINITIAL_XPSR ( 0x01000000UL )
110 #define portINITIAL_EXC_RETURN ( 0xfffffffdUL )
111 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
112 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
113
114 /* Constants used to check the installation of the FreeRTOS interrupt handlers. */
115 #define portSCB_VTOR_REG ( *( ( portISR_t ** ) 0xE000ED08 ) )
116 #define portVECTOR_INDEX_SVC ( 11 )
117 #define portVECTOR_INDEX_PENDSV ( 14 )
118
119 /* Constants required to check the validity of an interrupt priority. */
120 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
121 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
122 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
123 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
124 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
125 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
126 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
127 #define portPRIGROUP_SHIFT ( 8UL )
128
129 /* Constants used during system call enter and exit. */
130 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
131 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
132
133 /* Offsets in the stack to the parameters when inside the SVC handler. */
134 #define portOFFSET_TO_LR ( 5 )
135 #define portOFFSET_TO_PC ( 6 )
136 #define portOFFSET_TO_PSR ( 7 )
137
138
139 /* For strict compliance with the Cortex-M spec the task start address should
140 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
141 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
142
143 /* Does addr lie within [start, end] address range? */
144 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
145 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
146
147 /* Is the access request satisfied by the available permissions? */
148 #define portIS_AUTHORIZED( accessRequest, permissions ) \
149 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
150
151 /* Max value that fits in a uint32_t type. */
152 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
153
154 /* Check if adding a and b will result in overflow. */
155 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
156 /*-----------------------------------------------------------*/
157
158 /*
159 * Configure a number of standard MPU regions that are used by all tasks.
160 */
161 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
162
163 /*
164 * Return the smallest MPU region size that a given number of bytes will fit
165 * into. The region size is returned as the value that should be programmed
166 * into the region attribute register for that region.
167 */
168 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
169
170 /*
171 * Setup the timer to generate the tick interrupts. The implementation in this
172 * file is weak to allow application writers to change the timer used to
173 * generate the tick interrupt.
174 */
175 void vPortSetupTimerInterrupt( void );
176
177 /*
178 * Standard FreeRTOS exception handlers.
179 */
180 void xPortPendSVHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
181 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
182 void vPortSVCHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
183
184 /*
185 * Starts the scheduler by restoring the context of the first task to run.
186 */
187 static void prvRestoreContextOfFirstTask( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
188
189 /*
190 * C portion of the SVC handler. The SVC handler is split between an asm entry
191 * and a C wrapper for simplicity of coding and maintenance.
192 */
193 void vSVCHandler_C( uint32_t * pulRegisters ) __attribute__( ( noinline ) ) PRIVILEGED_FUNCTION;
194
195 /*
196 * Function to enable the VFP.
197 */
198 static void vPortEnableVFP( void ) __attribute__( ( naked ) );
199
200 /**
201 * @brief Checks whether or not the processor is privileged.
202 *
203 * @return 1 if the processor is already privileged, 0 otherwise.
204 */
205 BaseType_t xIsPrivileged( void ) __attribute__( ( naked ) );
206
207 /**
208 * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
209 * register.
210 *
211 * Bit 0 of the CONTROL register defines the privilege level of Thread Mode.
212 * Bit[0] = 0 --> The processor is running privileged
213 * Bit[0] = 1 --> The processor is running unprivileged.
214 */
215 void vResetPrivilege( void ) __attribute__( ( naked ) );
216
217 /**
218 * @brief Make a task unprivileged.
219 */
220 void vPortSwitchToUserMode( void );
221
222 /**
223 * @brief Enter critical section.
224 */
225 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
226 void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
227 #else
228 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
229 #endif
230
231 /**
232 * @brief Exit from critical section.
233 */
234 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
235 void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
236 #else
237 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
238 #endif
239
240 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
241
242 /**
243 * @brief Sets up the system call stack so that upon returning from
244 * SVC, the system call stack is used.
245 *
246 * @param pulTaskStack The current SP when the SVC was raised.
247 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
248 * @param ucSystemCallNumber The system call number of the system call.
249 */
250 void vSystemCallEnter( uint32_t * pulTaskStack,
251 uint32_t ulLR,
252 uint8_t ucSystemCallNumber ) PRIVILEGED_FUNCTION;
253
254 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
255
256 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
257
258 /**
259 * @brief Raise SVC for exiting from a system call.
260 */
261 void vRequestSystemCallExit( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
262
263 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
264
265 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
266
267 /**
268 * @brief Sets up the task stack so that upon returning from
269 * SVC, the task stack is used again.
270 *
271 * @param pulSystemCallStack The current SP when the SVC was raised.
272 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
273 */
274 void vSystemCallExit( uint32_t * pulSystemCallStack,
275 uint32_t ulLR ) PRIVILEGED_FUNCTION;
276
277 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
278
279 /**
280 * @brief Checks whether or not the calling task is privileged.
281 *
282 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
283 */
284 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
285 /*-----------------------------------------------------------*/
286
287 /* Each task maintains its own interrupt status in the critical nesting
288 * variable. Note this is not saved as part of the task context as context
289 * switches can only occur when uxCriticalNesting is zero. */
290 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
291
292 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
293
294 /*
295 * This variable is set to pdTRUE when the scheduler is started.
296 */
297 PRIVILEGED_DATA static BaseType_t xSchedulerRunning = pdFALSE;
298
299 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
300
301 /*
302 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
303 * FreeRTOS API functions are not called from interrupts that have been assigned
304 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
305 */
306 #if ( configASSERT_DEFINED == 1 )
307 static uint8_t ucMaxSysCallPriority = 0;
308 static uint32_t ulMaxPRIGROUPValue = 0;
309 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
310 #endif /* configASSERT_DEFINED */
311
312 /*-----------------------------------------------------------*/
313
314 /*
315 * See header file for description.
316 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters,BaseType_t xRunPrivileged,xMPU_SETTINGS * xMPUSettings)317 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
318 TaskFunction_t pxCode,
319 void * pvParameters,
320 BaseType_t xRunPrivileged,
321 xMPU_SETTINGS * xMPUSettings )
322 {
323 if( xRunPrivileged == pdTRUE )
324 {
325 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
326 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
327 }
328 else
329 {
330 xMPUSettings->ulTaskFlags &= ( ~( portTASK_IS_PRIVILEGED_FLAG ) );
331 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
332 }
333
334 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
335 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
336 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
337 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
338 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
339 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
340 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
341 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
342 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
343
344 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
345 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
346 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
347 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
348 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
349 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
350 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
351 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
352 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
353
354 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
355 {
356 /* Ensure that the system call stack is double word aligned. */
357 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
358 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
359 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
360
361 /* This is not NULL only for the duration of a system call. */
362 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
363 }
364 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
365
366 return &( xMPUSettings->ulContext[ 19 ] );
367 }
368 /*-----------------------------------------------------------*/
369
370 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
371
vPortSVCHandler(void)372 void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
373 {
374 __asm volatile
375 (
376 ".syntax unified \n"
377 ".extern vSVCHandler_C \n"
378 ".extern vSystemCallEnter \n"
379 ".extern vSystemCallExit \n"
380 " \n"
381 "tst lr, #4 \n"
382 "ite eq \n"
383 "mrseq r0, msp \n"
384 "mrsne r0, psp \n"
385 " \n"
386 "ldr r1, [r0, #24] \n"
387 "ldrb r2, [r1, #-2] \n"
388 "cmp r2, %0 \n"
389 "blt syscall_enter \n"
390 "cmp r2, %1 \n"
391 "beq syscall_exit \n"
392 "b vSVCHandler_C \n"
393 " \n"
394 "syscall_enter: \n"
395 " mov r1, lr \n"
396 " b vSystemCallEnter \n"
397 " \n"
398 "syscall_exit: \n"
399 " mov r1, lr \n"
400 " b vSystemCallExit \n"
401 " \n"
402 : /* No outputs. */
403 : "i" ( NUM_SYSTEM_CALLS ), "i" ( portSVC_SYSTEM_CALL_EXIT )
404 : "r0", "r1", "r2", "memory"
405 );
406 }
407
408 #else /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
409
vPortSVCHandler(void)410 void vPortSVCHandler( void )
411 {
412 /* Assumes psp was in use. */
413 __asm volatile
414 (
415 #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
416 " tst lr, #4 \n"
417 " ite eq \n"
418 " mrseq r0, msp \n"
419 " mrsne r0, psp \n"
420 #else
421 " mrs r0, psp \n"
422 #endif
423 " b %0 \n"
424 ::"i" ( vSVCHandler_C ) : "r0", "memory"
425 );
426 }
427
428 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
429 /*-----------------------------------------------------------*/
430
vSVCHandler_C(uint32_t * pulParam)431 void vSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
432 {
433 uint8_t ucSVCNumber;
434 uint32_t ulPC;
435
436 #if ( ( configUSE_MPU_WRAPPERS_V1 == 1 ) && ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) )
437 #if defined( __ARMCC_VERSION )
438
439 /* Declaration when these variable are defined in code instead of being
440 * exported from linker scripts. */
441 extern uint32_t * __syscalls_flash_start__;
442 extern uint32_t * __syscalls_flash_end__;
443 #else
444 /* Declaration when these variable are exported from linker scripts. */
445 extern uint32_t __syscalls_flash_start__[];
446 extern uint32_t __syscalls_flash_end__[];
447 #endif /* #if defined( __ARMCC_VERSION ) */
448 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 1 ) && ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) ) */
449
450 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
451 * argument (r0) is pulParam[ 0 ]. */
452 ulPC = pulParam[ portOFFSET_TO_PC ];
453 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
454
455 switch( ucSVCNumber )
456 {
457 case portSVC_START_SCHEDULER:
458 prvRestoreContextOfFirstTask();
459 break;
460
461 case portSVC_YIELD:
462 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
463
464 /* Barriers are normally not required
465 * but do ensure the code is completely
466 * within the specified behaviour for the
467 * architecture. */
468 __asm volatile ( "dsb" ::: "memory" );
469 __asm volatile ( "isb" );
470
471 break;
472
473 #if ( configUSE_MPU_WRAPPERS_V1 == 1 )
474 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
475 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
476 * svc was raised from any of the
477 * system calls. */
478
479 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
480 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
481 {
482 __asm volatile
483 (
484 " mrs r1, control \n" /* Obtain current control value. */
485 " bic r1, #1 \n" /* Set privilege bit. */
486 " msr control, r1 \n" /* Write back new control value. */
487 ::: "r1", "memory"
488 );
489 }
490
491 break;
492 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
493 case portSVC_RAISE_PRIVILEGE:
494 __asm volatile
495 (
496 " mrs r1, control \n" /* Obtain current control value. */
497 " bic r1, #1 \n" /* Set privilege bit. */
498 " msr control, r1 \n" /* Write back new control value. */
499 ::: "r1", "memory"
500 );
501 break;
502 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
503 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
504
505 default: /* Unknown SVC call. */
506 break;
507 }
508 }
509 /*-----------------------------------------------------------*/
510
511 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
512
vSystemCallEnter(uint32_t * pulTaskStack,uint32_t ulLR,uint8_t ucSystemCallNumber)513 void vSystemCallEnter( uint32_t * pulTaskStack,
514 uint32_t ulLR,
515 uint8_t ucSystemCallNumber ) /* PRIVILEGED_FUNCTION */
516 {
517 extern TaskHandle_t pxCurrentTCB;
518 extern UBaseType_t uxSystemCallImplementations[ NUM_SYSTEM_CALLS ];
519 xMPU_SETTINGS * pxMpuSettings;
520 uint32_t * pulSystemCallStack;
521 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
522
523 #if defined( __ARMCC_VERSION )
524 /* Declaration when these variable are defined in code instead of being
525 * exported from linker scripts. */
526 extern uint32_t * __syscalls_flash_start__;
527 extern uint32_t * __syscalls_flash_end__;
528 #else
529 /* Declaration when these variable are exported from linker scripts. */
530 extern uint32_t __syscalls_flash_start__[];
531 extern uint32_t __syscalls_flash_end__[];
532 #endif /* #if defined( __ARMCC_VERSION ) */
533
534 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
535 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
536
537 /* Checks:
538 * 1. SVC is raised from the system call section (i.e. application is
539 * not raising SVC directly).
540 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must be NULL as
541 * it is non-NULL only during the execution of a system call (i.e.
542 * between system call enter and exit).
543 * 3. System call is not for a kernel API disabled by the configuration
544 * in FreeRTOSConfig.h.
545 * 4. We do not need to check that ucSystemCallNumber is within range
546 * because the assembly SVC handler checks that before calling
547 * this function.
548 */
549 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
550 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) &&
551 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL ) &&
552 ( uxSystemCallImplementations[ ucSystemCallNumber ] != ( UBaseType_t ) 0 ) )
553 {
554 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
555
556 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
557 {
558 /* Extended frame i.e. FPU in use. */
559 ulStackFrameSize = 26;
560 __asm volatile (
561 " vpush {s0} \n" /* Trigger lazy stacking. */
562 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
563 ::: "memory"
564 );
565 }
566 else
567 {
568 /* Standard frame i.e. FPU not in use. */
569 ulStackFrameSize = 8;
570 }
571
572 /* Make space on the system call stack for the stack frame. */
573 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
574
575 /* Copy the stack frame. */
576 for( i = 0; i < ulStackFrameSize; i++ )
577 {
578 pulSystemCallStack[ i ] = pulTaskStack[ i ];
579 }
580
581 /* Use the pulSystemCallStack in thread mode. */
582 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
583
584 /* Raise the privilege for the duration of the system call. */
585 __asm volatile (
586 " mrs r1, control \n" /* Obtain current control value. */
587 " bic r1, #1 \n" /* Clear nPRIV bit. */
588 " msr control, r1 \n" /* Write back new control value. */
589 ::: "r1", "memory"
590 );
591
592 /* Remember the location where we should copy the stack frame when we exit from
593 * the system call. */
594 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
595
596 /* Store the value of the Link Register before the SVC was raised.
597 * It contains the address of the caller of the System Call entry
598 * point (i.e. the caller of the MPU_<API>). We need to restore it
599 * when we exit from the system call. */
600 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
601
602 /* Start executing the system call upon returning from this handler. */
603 pulSystemCallStack[ portOFFSET_TO_PC ] = uxSystemCallImplementations[ ucSystemCallNumber ];
604 /* Raise a request to exit from the system call upon finishing the
605 * system call. */
606 pulSystemCallStack[ portOFFSET_TO_LR ] = ( uint32_t ) vRequestSystemCallExit;
607
608 /* Record if the hardware used padding to force the stack pointer
609 * to be double word aligned. */
610 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
611 {
612 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
613 }
614 else
615 {
616 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
617 }
618
619 /* We ensure in pxPortInitialiseStack that the system call stack is
620 * double word aligned and therefore, there is no need of padding.
621 * Clear the bit[9] of stacked xPSR. */
622 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
623 }
624 }
625
626 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
627 /*-----------------------------------------------------------*/
628
629 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
630
vRequestSystemCallExit(void)631 void vRequestSystemCallExit( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
632 {
633 __asm volatile ( "svc %0 \n" ::"i" ( portSVC_SYSTEM_CALL_EXIT ) : "memory" );
634 }
635
636 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
637 /*-----------------------------------------------------------*/
638
639 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
640
vSystemCallExit(uint32_t * pulSystemCallStack,uint32_t ulLR)641 void vSystemCallExit( uint32_t * pulSystemCallStack,
642 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
643 {
644 extern TaskHandle_t pxCurrentTCB;
645 xMPU_SETTINGS * pxMpuSettings;
646 uint32_t * pulTaskStack;
647 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
648
649 #if defined( __ARMCC_VERSION )
650 /* Declaration when these variable are defined in code instead of being
651 * exported from linker scripts. */
652 extern uint32_t * __privileged_functions_start__;
653 extern uint32_t * __privileged_functions_end__;
654 #else
655 /* Declaration when these variable are exported from linker scripts. */
656 extern uint32_t __privileged_functions_start__[];
657 extern uint32_t __privileged_functions_end__[];
658 #endif /* #if defined( __ARMCC_VERSION ) */
659
660 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
661 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
662
663 /* Checks:
664 * 1. SVC is raised from the privileged code (i.e. application is not
665 * raising SVC directly). This SVC is only raised from
666 * vRequestSystemCallExit which is in the privileged code section.
667 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must not be NULL -
668 * this means that we previously entered a system call and the
669 * application is not attempting to exit without entering a system
670 * call.
671 */
672 if( ( ulSystemCallLocation >= ( uint32_t ) __privileged_functions_start__ ) &&
673 ( ulSystemCallLocation <= ( uint32_t ) __privileged_functions_end__ ) &&
674 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack != NULL ) )
675 {
676 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
677
678 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
679 {
680 /* Extended frame i.e. FPU in use. */
681 ulStackFrameSize = 26;
682 __asm volatile (
683 " vpush {s0} \n" /* Trigger lazy stacking. */
684 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
685 ::: "memory"
686 );
687 }
688 else
689 {
690 /* Standard frame i.e. FPU not in use. */
691 ulStackFrameSize = 8;
692 }
693
694 /* Make space on the task stack for the stack frame. */
695 pulTaskStack = pulTaskStack - ulStackFrameSize;
696
697 /* Copy the stack frame. */
698 for( i = 0; i < ulStackFrameSize; i++ )
699 {
700 pulTaskStack[ i ] = pulSystemCallStack[ i ];
701 }
702
703 /* Use the pulTaskStack in thread mode. */
704 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
705
706 /* Drop the privilege before returning to the thread mode. */
707 __asm volatile (
708 " mrs r1, control \n" /* Obtain current control value. */
709 " orr r1, #1 \n" /* Set nPRIV bit. */
710 " msr control, r1 \n" /* Write back new control value. */
711 ::: "r1", "memory"
712 );
713
714 /* Return to the caller of the System Call entry point (i.e. the
715 * caller of the MPU_<API>). */
716 pulTaskStack[ portOFFSET_TO_PC ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
717 /* Ensure that LR has a valid value.*/
718 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
719
720 /* If the hardware used padding to force the stack pointer
721 * to be double word aligned, set the stacked xPSR bit[9],
722 * otherwise clear it. */
723 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
724 {
725 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
726 }
727 else
728 {
729 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
730 }
731
732 /* This is not NULL only for the duration of the system call. */
733 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
734 }
735 }
736
737 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
738 /*-----------------------------------------------------------*/
739
xPortIsTaskPrivileged(void)740 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
741 {
742 BaseType_t xTaskIsPrivileged = pdFALSE;
743 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
744
745 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
746 {
747 xTaskIsPrivileged = pdTRUE;
748 }
749
750 return xTaskIsPrivileged;
751 }
752 /*-----------------------------------------------------------*/
753
prvRestoreContextOfFirstTask(void)754 static void prvRestoreContextOfFirstTask( void )
755 {
756 __asm volatile
757 (
758 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
759 " ldr r0, [r0] \n"
760 " ldr r0, [r0] \n"
761 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
762 " \n"
763 /*------------ Program MPU. ------------ */
764 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
765 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
766 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
767 " \n"
768 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
769 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
770 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
771 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
772 " str r3, [r0] \n" /* Disable MPU. */
773 " \n"
774 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
775 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
776 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
777 " \n"
778 #if ( configTOTAL_MPU_REGIONS == 16 )
779 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 4 - 8]. */
780 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 4 - 8]. */
781 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 9 - 12]. */
782 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 9 - 12]. */
783 #endif /* configTOTAL_MPU_REGIONS == 16. */
784 " \n"
785 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
786 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
787 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
788 " str r3, [r0] \n" /* Enable MPU. */
789 " dsb \n" /* Force memory writes before continuing. */
790 " \n"
791 /*---------- Restore Context. ---------- */
792 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
793 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
794 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
795 " \n"
796 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
797 " msr psp, r0 \n"
798 " stmia r0, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
799 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
800 " msr control, r3 \n"
801 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
802 " \n"
803 " mov r0, #0 \n"
804 " msr basepri, r0 \n"
805 " bx lr \n"
806 " \n"
807 " .ltorg \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
808 );
809 }
810 /*-----------------------------------------------------------*/
811
812 /*
813 * See header file for description.
814 */
xPortStartScheduler(void)815 BaseType_t xPortStartScheduler( void )
816 {
817 /* Errata 837070 workaround must only be enabled on Cortex-M7 r0p0
818 * and r0p1 cores. */
819 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
820 configASSERT( ( portCPUID == portCORTEX_M7_r0p1_ID ) || ( portCPUID == portCORTEX_M7_r0p0_ID ) );
821 #else
822
823 /* When using this port on a Cortex-M7 r0p0 or r0p1 core, define
824 * configENABLE_ERRATA_837070_WORKAROUND to 1 in your
825 * FreeRTOSConfig.h. */
826 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
827 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
828 #endif
829
830 /* An application can install FreeRTOS interrupt handlers in one of the
831 * following ways:
832 * 1. Direct Routing - Install the functions vPortSVCHandler and
833 * xPortPendSVHandler for SVCall and PendSV interrupts respectively.
834 * 2. Indirect Routing - Install separate handlers for SVCall and PendSV
835 * interrupts and route program control from those handlers to
836 * vPortSVCHandler and xPortPendSVHandler functions.
837 *
838 * Applications that use Indirect Routing must set
839 * configCHECK_HANDLER_INSTALLATION to 0 in their FreeRTOSConfig.h. Direct
840 * routing, which is validated here when configCHECK_HANDLER_INSTALLATION
841 * is 1, should be preferred when possible. */
842 #if ( configCHECK_HANDLER_INSTALLATION == 1 )
843 {
844 const portISR_t * const pxVectorTable = portSCB_VTOR_REG;
845
846 /* Validate that the application has correctly installed the FreeRTOS
847 * handlers for SVCall and PendSV interrupts. We do not check the
848 * installation of the SysTick handler because the application may
849 * choose to drive the RTOS tick using a timer other than the SysTick
850 * timer by overriding the weak function vPortSetupTimerInterrupt().
851 *
852 * Assertion failures here indicate incorrect installation of the
853 * FreeRTOS handlers. For help installing the FreeRTOS handlers, see
854 * https://www.freertos.org/Why-FreeRTOS/FAQs.
855 *
856 * Systems with a configurable address for the interrupt vector table
857 * can also encounter assertion failures or even system faults here if
858 * VTOR is not set correctly to point to the application's vector table. */
859 configASSERT( pxVectorTable[ portVECTOR_INDEX_SVC ] == vPortSVCHandler );
860 configASSERT( pxVectorTable[ portVECTOR_INDEX_PENDSV ] == xPortPendSVHandler );
861 }
862 #endif /* configCHECK_HANDLER_INSTALLATION */
863
864 #if ( configASSERT_DEFINED == 1 )
865 {
866 volatile uint8_t ucOriginalPriority;
867 volatile uint32_t ulImplementedPrioBits = 0;
868 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
869 volatile uint8_t ucMaxPriorityValue;
870
871 /* Determine the maximum priority from which ISR safe FreeRTOS API
872 * functions can be called. ISR safe functions are those that end in
873 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
874 * ensure interrupt entry is as fast and simple as possible.
875 *
876 * Save the interrupt priority value that is about to be clobbered. */
877 ucOriginalPriority = *pucFirstUserPriorityRegister;
878
879 /* Determine the number of priority bits available. First write to all
880 * possible bits. */
881 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
882
883 /* Read the value back to see how many bits stuck. */
884 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
885
886 /* Use the same mask on the maximum system call priority. */
887 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
888
889 /* Check that the maximum system call priority is nonzero after
890 * accounting for the number of priority bits supported by the
891 * hardware. A priority of 0 is invalid because setting the BASEPRI
892 * register to 0 unmasks all interrupts, and interrupts with priority 0
893 * cannot be masked using BASEPRI.
894 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
895 configASSERT( ucMaxSysCallPriority );
896
897 /* Check that the bits not implemented in hardware are zero in
898 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
899 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
900
901 /* Calculate the maximum acceptable priority group value for the number
902 * of bits read back. */
903
904 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
905 {
906 ulImplementedPrioBits++;
907 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
908 }
909
910 if( ulImplementedPrioBits == 8 )
911 {
912 /* When the hardware implements 8 priority bits, there is no way for
913 * the software to configure PRIGROUP to not have sub-priorities. As
914 * a result, the least significant bit is always used for sub-priority
915 * and there are 128 preemption priorities and 2 sub-priorities.
916 *
917 * This may cause some confusion in some cases - for example, if
918 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
919 * priority interrupts will be masked in Critical Sections as those
920 * are at the same preemption priority. This may appear confusing as
921 * 4 is higher (numerically lower) priority than
922 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
923 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
924 * to 4, this confusion does not happen and the behaviour remains the same.
925 *
926 * The following assert ensures that the sub-priority bit in the
927 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
928 * confusion. */
929 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
930 ulMaxPRIGROUPValue = 0;
931 }
932 else
933 {
934 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
935 }
936
937 /* Shift the priority group value back to its position within the AIRCR
938 * register. */
939 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
940 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
941
942 /* Restore the clobbered interrupt priority register to its original
943 * value. */
944 *pucFirstUserPriorityRegister = ucOriginalPriority;
945 }
946 #endif /* configASSERT_DEFINED */
947
948 /* Make PendSV and SysTick the lowest priority interrupts, and make SVCall
949 * the highest priority. */
950 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
951 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
952 portNVIC_SHPR2_REG = 0;
953
954 /* Configure the regions in the MPU that are common to all tasks. */
955 prvSetupMPU();
956
957 /* Start the timer that generates the tick ISR. Interrupts are disabled
958 * here already. */
959 vPortSetupTimerInterrupt();
960
961 /* Initialise the critical nesting count ready for the first task. */
962 uxCriticalNesting = 0;
963
964 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
965 {
966 xSchedulerRunning = pdTRUE;
967 }
968 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
969
970 /* Ensure the VFP is enabled - it should be anyway. */
971 vPortEnableVFP();
972
973 /* Lazy save always. */
974 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
975
976 /* Start the first task. This also clears the bit that indicates the FPU is
977 * in use in case the FPU was used before the scheduler was started - which
978 * would otherwise result in the unnecessary leaving of space in the SVC stack
979 * for lazy saving of FPU registers. */
980 __asm volatile (
981 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
982 " ldr r0, [r0] \n"
983 " ldr r0, [r0] \n"
984 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
985 " mov r0, #0 \n" /* Clear the bit that indicates the FPU is in use, see comment above. */
986 " msr control, r0 \n"
987 " cpsie i \n" /* Globally enable interrupts. */
988 " cpsie f \n"
989 " dsb \n"
990 " isb \n"
991 " svc %0 \n" /* System call to start first task. */
992 " nop \n"
993 " .ltorg \n"
994 ::"i" ( portSVC_START_SCHEDULER ) : "memory" );
995
996 /* Should not get here! */
997 return 0;
998 }
999 /*-----------------------------------------------------------*/
1000
vPortEndScheduler(void)1001 void vPortEndScheduler( void )
1002 {
1003 /* Not implemented in ports where there is nothing to return to.
1004 * Artificially force an assert. */
1005 configASSERT( uxCriticalNesting == 1000UL );
1006 }
1007 /*-----------------------------------------------------------*/
1008
vPortEnterCritical(void)1009 void vPortEnterCritical( void )
1010 {
1011 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
1012 if( portIS_PRIVILEGED() == pdFALSE )
1013 {
1014 portRAISE_PRIVILEGE();
1015 portMEMORY_BARRIER();
1016
1017 portDISABLE_INTERRUPTS();
1018 uxCriticalNesting++;
1019 portMEMORY_BARRIER();
1020
1021 portRESET_PRIVILEGE();
1022 portMEMORY_BARRIER();
1023 }
1024 else
1025 {
1026 portDISABLE_INTERRUPTS();
1027 uxCriticalNesting++;
1028 }
1029 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1030 portDISABLE_INTERRUPTS();
1031 uxCriticalNesting++;
1032 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1033 }
1034 /*-----------------------------------------------------------*/
1035
vPortExitCritical(void)1036 void vPortExitCritical( void )
1037 {
1038 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
1039 if( portIS_PRIVILEGED() == pdFALSE )
1040 {
1041 portRAISE_PRIVILEGE();
1042 portMEMORY_BARRIER();
1043
1044 configASSERT( uxCriticalNesting );
1045 uxCriticalNesting--;
1046
1047 if( uxCriticalNesting == 0 )
1048 {
1049 portENABLE_INTERRUPTS();
1050 }
1051
1052 portMEMORY_BARRIER();
1053
1054 portRESET_PRIVILEGE();
1055 portMEMORY_BARRIER();
1056 }
1057 else
1058 {
1059 configASSERT( uxCriticalNesting );
1060 uxCriticalNesting--;
1061
1062 if( uxCriticalNesting == 0 )
1063 {
1064 portENABLE_INTERRUPTS();
1065 }
1066 }
1067 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1068 configASSERT( uxCriticalNesting );
1069 uxCriticalNesting--;
1070
1071 if( uxCriticalNesting == 0 )
1072 {
1073 portENABLE_INTERRUPTS();
1074 }
1075 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1076 }
1077 /*-----------------------------------------------------------*/
1078
xPortPendSVHandler(void)1079 void xPortPendSVHandler( void )
1080 {
1081 /* This is a naked function. */
1082
1083 __asm volatile
1084 (
1085 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
1086 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1087 " ldr r1, [r2] \n" /* r1 = Location where the context should be saved. */
1088 " \n"
1089 /*------------ Save Context. ----------- */
1090 " mrs r3, control \n"
1091 " mrs r0, psp \n"
1092 " isb \n"
1093 " \n"
1094 " add r0, r0, #0x20 \n" /* Move r0 to location where s0 is saved. */
1095 " tst lr, #0x10 \n"
1096 " ittt eq \n"
1097 " vstmiaeq r1!, {s16-s31} \n" /* Store s16-s31. */
1098 " vldmiaeq r0, {s0-s16} \n" /* Copy hardware saved FP context into s0-s16. */
1099 " vstmiaeq r1!, {s0-s16} \n" /* Store hardware saved FP context. */
1100 " sub r0, r0, #0x20 \n" /* Set r0 back to the location of hardware saved context. */
1101 " \n"
1102 " stmia r1!, {r3-r11, lr} \n" /* Store CONTROL register, r4-r11 and LR. */
1103 " ldmia r0, {r4-r11} \n" /* Copy hardware saved context into r4-r11. */
1104 " stmia r1!, {r0, r4-r11} \n" /* Store original PSP (after hardware has saved context) and the hardware saved context. */
1105 " str r1, [r2] \n" /* Save the location from where the context should be restored as the first member of TCB. */
1106 " \n"
1107 /*---------- Select next task. --------- */
1108 " mov r0, %0 \n"
1109 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
1110 " cpsid i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
1111 #endif
1112 " msr basepri, r0 \n"
1113 " dsb \n"
1114 " isb \n"
1115 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
1116 " cpsie i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
1117 #endif
1118 " bl vTaskSwitchContext \n"
1119 " mov r0, #0 \n"
1120 " msr basepri, r0 \n"
1121 " \n"
1122 /*------------ Program MPU. ------------ */
1123 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
1124 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1125 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
1126 " \n"
1127 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
1128 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1129 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1130 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
1131 " str r3, [r0] \n" /* Disable MPU. */
1132 " \n"
1133 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
1134 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
1135 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
1136 " \n"
1137 #if ( configTOTAL_MPU_REGIONS == 16 )
1138 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 4 - 7]. */
1139 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 4 - 7]. */
1140 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 8 - 11]. */
1141 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 8 - 11]. */
1142 #endif /* configTOTAL_MPU_REGIONS == 16. */
1143 " \n"
1144 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1145 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1146 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
1147 " str r3, [r0] \n" /* Enable MPU. */
1148 " dsb \n" /* Force memory writes before continuing. */
1149 " \n"
1150 /*---------- Restore Context. ---------- */
1151 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
1152 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1153 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
1154 " \n"
1155 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
1156 " msr psp, r0 \n"
1157 " stmia r0!, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
1158 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
1159 " msr control, r3 \n"
1160
1161 " tst lr, #0x10 \n"
1162 " ittt eq \n"
1163 " vldmdbeq r1!, {s0-s16} \n" /* s0-s16 contain hardware saved FP context. */
1164 " vstmiaeq r0!, {s0-s16} \n" /* Copy hardware saved FP context on the task stack. */
1165 " vldmdbeq r1!, {s16-s31} \n" /* Restore s16-s31. */
1166
1167 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
1168 " bx lr \n"
1169 " \n"
1170 " .ltorg \n" /* Assemble the current literal pool to avoid offset-out-of-bound errors with lto. */
1171 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
1172 );
1173 }
1174 /*-----------------------------------------------------------*/
1175
xPortSysTickHandler(void)1176 void xPortSysTickHandler( void )
1177 {
1178 uint32_t ulDummy;
1179
1180 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
1181 traceISR_ENTER();
1182 {
1183 /* Increment the RTOS tick. */
1184 if( xTaskIncrementTick() != pdFALSE )
1185 {
1186 traceISR_EXIT_TO_SCHEDULER();
1187 /* Pend a context switch. */
1188 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
1189 }
1190 else
1191 {
1192 traceISR_EXIT();
1193 }
1194 }
1195 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
1196 }
1197 /*-----------------------------------------------------------*/
1198
1199 /*
1200 * Setup the systick timer to generate the tick interrupts at the required
1201 * frequency.
1202 */
vPortSetupTimerInterrupt(void)1203 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
1204 {
1205 /* Stop and clear the SysTick. */
1206 portNVIC_SYSTICK_CTRL_REG = 0UL;
1207 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1208
1209 /* Configure SysTick to interrupt at the requested rate. */
1210 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1211 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE );
1212 }
1213 /*-----------------------------------------------------------*/
1214
1215 /* This is a naked function. */
vPortEnableVFP(void)1216 static void vPortEnableVFP( void )
1217 {
1218 __asm volatile
1219 (
1220 " ldr.w r0, =0xE000ED88 \n" /* The FPU enable bits are in the CPACR. */
1221 " ldr r1, [r0] \n"
1222 " \n"
1223 " orr r1, r1, #( 0xf << 20 ) \n" /* Enable CP10 and CP11 coprocessors, then save back. */
1224 " str r1, [r0] \n"
1225 " bx r14 \n"
1226 " .ltorg \n"
1227 );
1228 }
1229 /*-----------------------------------------------------------*/
1230
prvSetupMPU(void)1231 static void prvSetupMPU( void )
1232 {
1233 #if defined( __ARMCC_VERSION )
1234
1235 /* Declaration when these variable are defined in code instead of being
1236 * exported from linker scripts. */
1237 extern uint32_t * __privileged_functions_start__;
1238 extern uint32_t * __privileged_functions_end__;
1239 extern uint32_t * __FLASH_segment_start__;
1240 extern uint32_t * __FLASH_segment_end__;
1241 extern uint32_t * __privileged_data_start__;
1242 extern uint32_t * __privileged_data_end__;
1243 #else
1244 /* Declaration when these variable are exported from linker scripts. */
1245 extern uint32_t __privileged_functions_start__[];
1246 extern uint32_t __privileged_functions_end__[];
1247 extern uint32_t __FLASH_segment_start__[];
1248 extern uint32_t __FLASH_segment_end__[];
1249 extern uint32_t __privileged_data_start__[];
1250 extern uint32_t __privileged_data_end__[];
1251 #endif /* if defined( __ARMCC_VERSION ) */
1252
1253 /* The only permitted number of regions are 8 or 16. */
1254 configASSERT( ( configTOTAL_MPU_REGIONS == 8 ) || ( configTOTAL_MPU_REGIONS == 16 ) );
1255
1256 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
1257 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
1258
1259 /* Check the expected MPU is present. */
1260 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1261 {
1262 /* First setup the unprivileged flash for unprivileged read only access. */
1263 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1264 ( portMPU_REGION_VALID ) |
1265 ( portUNPRIVILEGED_FLASH_REGION );
1266
1267 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1268 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1269 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1270 ( portMPU_REGION_ENABLE );
1271
1272 /* Setup the privileged flash for privileged only access. This is where
1273 * the kernel code is placed. */
1274 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1275 ( portMPU_REGION_VALID ) |
1276 ( portPRIVILEGED_FLASH_REGION );
1277
1278 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1279 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1280 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1281 ( portMPU_REGION_ENABLE );
1282
1283 /* Setup the privileged data RAM region. This is where the kernel data
1284 * is placed. */
1285 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1286 ( portMPU_REGION_VALID ) |
1287 ( portPRIVILEGED_RAM_REGION );
1288
1289 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1290 ( portMPU_REGION_EXECUTE_NEVER ) |
1291 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1292 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1293 ( portMPU_REGION_ENABLE );
1294
1295 /* By default allow everything to access the general peripherals. The
1296 * system peripherals and registers are protected. */
1297 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1298 ( portMPU_REGION_VALID ) |
1299 ( portGENERAL_PERIPHERALS_REGION );
1300
1301 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1302 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1303 ( portMPU_REGION_ENABLE );
1304
1305 /* Enable the memory fault exception. */
1306 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1307
1308 /* Enable the MPU with the background region configured. */
1309 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1310 }
1311 }
1312 /*-----------------------------------------------------------*/
1313
prvGetMPURegionSizeSetting(uint32_t ulActualSizeInBytes)1314 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1315 {
1316 uint32_t ulRegionSize, ulReturnValue = 4;
1317
1318 /* 32 is the smallest region size, 31 is the largest valid value for
1319 * ulReturnValue. */
1320 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1321 {
1322 if( ulActualSizeInBytes <= ulRegionSize )
1323 {
1324 break;
1325 }
1326 else
1327 {
1328 ulReturnValue++;
1329 }
1330 }
1331
1332 /* Shift the code by one before returning so it can be written directly
1333 * into the the correct bit position of the attribute register. */
1334 return( ulReturnValue << 1UL );
1335 }
1336 /*-----------------------------------------------------------*/
1337
xIsPrivileged(void)1338 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
1339 {
1340 __asm volatile
1341 (
1342 " mrs r0, control \n" /* r0 = CONTROL. */
1343 " tst r0, #1 \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
1344 " ite ne \n"
1345 " movne r0, #0 \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
1346 " moveq r0, #1 \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
1347 " bx lr \n" /* Return. */
1348 ::: "r0", "memory"
1349 );
1350 }
1351 /*-----------------------------------------------------------*/
1352
vResetPrivilege(void)1353 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
1354 {
1355 __asm volatile
1356 (
1357 " mrs r0, control \n" /* r0 = CONTROL. */
1358 " orr r0, #1 \n" /* r0 = r0 | 1. */
1359 " msr control, r0 \n" /* CONTROL = r0. */
1360 " bx lr \n" /* Return to the caller. */
1361 ::: "r0", "memory"
1362 );
1363 }
1364 /*-----------------------------------------------------------*/
1365
vPortSwitchToUserMode(void)1366 void vPortSwitchToUserMode( void )
1367 {
1368 /* Load the current task's MPU settings from its TCB. */
1369 xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL );
1370
1371 /* Mark the task as unprivileged. */
1372 xTaskMpuSettings->ulTaskFlags &= ( ~( portTASK_IS_PRIVILEGED_FLAG ) );
1373
1374 /* Lower the processor's privilege level. */
1375 vResetPrivilege();
1376 }
1377 /*-----------------------------------------------------------*/
1378
vPortStoreTaskMPUSettings(xMPU_SETTINGS * xMPUSettings,const struct xMEMORY_REGION * const xRegions,StackType_t * pxBottomOfStack,configSTACK_DEPTH_TYPE uxStackDepth)1379 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1380 const struct xMEMORY_REGION * const xRegions,
1381 StackType_t * pxBottomOfStack,
1382 configSTACK_DEPTH_TYPE uxStackDepth )
1383 {
1384 #if defined( __ARMCC_VERSION )
1385
1386 /* Declaration when these variable are defined in code instead of being
1387 * exported from linker scripts. */
1388 extern uint32_t * __SRAM_segment_start__;
1389 extern uint32_t * __SRAM_segment_end__;
1390 extern uint32_t * __privileged_data_start__;
1391 extern uint32_t * __privileged_data_end__;
1392 #else
1393 /* Declaration when these variable are exported from linker scripts. */
1394 extern uint32_t __SRAM_segment_start__[];
1395 extern uint32_t __SRAM_segment_end__[];
1396 extern uint32_t __privileged_data_start__[];
1397 extern uint32_t __privileged_data_end__[];
1398 #endif /* if defined( __ARMCC_VERSION ) */
1399
1400 int32_t lIndex;
1401 uint32_t ul;
1402
1403 if( xRegions == NULL )
1404 {
1405 /* No MPU regions are specified so allow access to all RAM. */
1406 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1407 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1408 ( portMPU_REGION_VALID ) |
1409 ( portSTACK_REGION ); /* Region number. */
1410
1411 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1412 ( portMPU_REGION_READ_WRITE ) |
1413 ( portMPU_REGION_EXECUTE_NEVER ) |
1414 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1415 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1416 ( portMPU_REGION_ENABLE );
1417
1418 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1419 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1420 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1421 tskMPU_WRITE_PERMISSION );
1422
1423 /* Invalidate user configurable regions. */
1424 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1425 {
1426 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1427 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1428 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1429 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1430 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1431 }
1432 }
1433 else
1434 {
1435 /* This function is called automatically when the task is created - in
1436 * which case the stack region parameters will be valid. At all other
1437 * times the stack parameters will not be valid and it is assumed that the
1438 * stack region has already been configured. */
1439 if( uxStackDepth > 0 )
1440 {
1441 /* Define the region that allows access to the stack. */
1442 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1443 ( ( uint32_t ) pxBottomOfStack ) |
1444 ( portMPU_REGION_VALID ) |
1445 ( portSTACK_REGION ); /* Region number. */
1446
1447 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1448 ( portMPU_REGION_READ_WRITE ) |
1449 ( portMPU_REGION_EXECUTE_NEVER ) |
1450 ( prvGetMPURegionSizeSetting( uxStackDepth * ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t ) ) ) |
1451 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1452 ( portMPU_REGION_ENABLE );
1453
1454 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1455 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1456 ( uxStackDepth * ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t ) ) - 1UL );
1457 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1458 tskMPU_WRITE_PERMISSION );
1459 }
1460
1461 lIndex = 0;
1462
1463 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1464 {
1465 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1466 {
1467 /* Translate the generic region definition contained in
1468 * xRegions into the CM4 specific MPU settings that are then
1469 * stored in xMPUSettings. */
1470 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1471 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1472 ( portMPU_REGION_VALID ) |
1473 ( ul - 1UL ); /* Region number. */
1474
1475 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1476 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1477 ( xRegions[ lIndex ].ulParameters ) |
1478 ( portMPU_REGION_ENABLE );
1479
1480 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1481 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1482 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1483
1484 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1485 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1486 {
1487 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1488 }
1489
1490 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1491 {
1492 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1493 }
1494 }
1495 else
1496 {
1497 /* Invalidate the region. */
1498 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1499 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1500 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1501 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1502 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1503 }
1504
1505 lIndex++;
1506 }
1507 }
1508 }
1509 /*-----------------------------------------------------------*/
1510
1511 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1512
xPortIsAuthorizedToAccessBuffer(const void * pvBuffer,uint32_t ulBufferLength,uint32_t ulAccessRequested)1513 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1514 uint32_t ulBufferLength,
1515 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1516
1517 {
1518 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1519 BaseType_t xAccessGranted = pdFALSE;
1520 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1521
1522 if( xSchedulerRunning == pdFALSE )
1523 {
1524 /* Grant access to all the kernel objects before the scheduler
1525 * is started. It is necessary because there is no task running
1526 * yet and therefore, we cannot use the permissions of any
1527 * task. */
1528 xAccessGranted = pdTRUE;
1529 }
1530 else if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1531 {
1532 xAccessGranted = pdTRUE;
1533 }
1534 else
1535 {
1536 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1537 {
1538 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1539 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1540
1541 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1542 {
1543 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1544 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1545 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1546 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1547 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1548 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1549 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1550 {
1551 xAccessGranted = pdTRUE;
1552 break;
1553 }
1554 }
1555 }
1556 }
1557
1558 return xAccessGranted;
1559 }
1560
1561 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1562 /*-----------------------------------------------------------*/
1563
1564 #if ( configASSERT_DEFINED == 1 )
1565
vPortValidateInterruptPriority(void)1566 void vPortValidateInterruptPriority( void )
1567 {
1568 uint32_t ulCurrentInterrupt;
1569 uint8_t ucCurrentPriority;
1570
1571 /* Obtain the number of the currently executing interrupt. */
1572 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1573
1574 /* Is the interrupt number a user defined interrupt? */
1575 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1576 {
1577 /* Look up the interrupt's priority. */
1578 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1579
1580 /* The following assertion will fail if a service routine (ISR) for
1581 * an interrupt that has been assigned a priority above
1582 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1583 * function. ISR safe FreeRTOS API functions must *only* be called
1584 * from interrupts that have been assigned a priority at or below
1585 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1586 *
1587 * Numerically low interrupt priority numbers represent logically high
1588 * interrupt priorities, therefore the priority of the interrupt must
1589 * be set to a value equal to or numerically *higher* than
1590 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1591 *
1592 * Interrupts that use the FreeRTOS API must not be left at their
1593 * default priority of zero as that is the highest possible priority,
1594 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1595 * and therefore also guaranteed to be invalid.
1596 *
1597 * FreeRTOS maintains separate thread and ISR API functions to ensure
1598 * interrupt entry is as fast and simple as possible.
1599 *
1600 * The following links provide detailed information:
1601 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1602 * https://www.freertos.org/Why-FreeRTOS/FAQs */
1603 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1604 }
1605
1606 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1607 * that define each interrupt's priority to be split between bits that
1608 * define the interrupt's pre-emption priority bits and bits that define
1609 * the interrupt's sub-priority. For simplicity all bits must be defined
1610 * to be pre-emption priority bits. The following assertion will fail if
1611 * this is not the case (if some bits represent a sub-priority).
1612 *
1613 * If the application only uses CMSIS libraries for interrupt
1614 * configuration then the correct setting can be achieved on all Cortex-M
1615 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1616 * scheduler. Note however that some vendor specific peripheral libraries
1617 * assume a non-zero priority group setting, in which cases using a value
1618 * of zero will result in unpredictable behaviour. */
1619 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1620 }
1621
1622 #endif /* configASSERT_DEFINED */
1623 /*-----------------------------------------------------------*/
1624
1625 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1626
vPortGrantAccessToKernelObject(TaskHandle_t xInternalTaskHandle,int32_t lInternalIndexOfKernelObject)1627 void vPortGrantAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1628 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1629 {
1630 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1631 xMPU_SETTINGS * xTaskMpuSettings;
1632
1633 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1634 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1635
1636 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1637
1638 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] |= ( 1U << ulAccessControlListEntryBit );
1639 }
1640
1641 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1642 /*-----------------------------------------------------------*/
1643
1644 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1645
vPortRevokeAccessToKernelObject(TaskHandle_t xInternalTaskHandle,int32_t lInternalIndexOfKernelObject)1646 void vPortRevokeAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1647 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1648 {
1649 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1650 xMPU_SETTINGS * xTaskMpuSettings;
1651
1652 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1653 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1654
1655 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1656
1657 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] &= ~( 1U << ulAccessControlListEntryBit );
1658 }
1659
1660 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1661 /*-----------------------------------------------------------*/
1662
1663 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1664
1665 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
1666
xPortIsAuthorizedToAccessKernelObject(int32_t lInternalIndexOfKernelObject)1667 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1668 {
1669 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1670 BaseType_t xAccessGranted = pdFALSE;
1671 const xMPU_SETTINGS * xTaskMpuSettings;
1672
1673 if( xSchedulerRunning == pdFALSE )
1674 {
1675 /* Grant access to all the kernel objects before the scheduler
1676 * is started. It is necessary because there is no task running
1677 * yet and therefore, we cannot use the permissions of any
1678 * task. */
1679 xAccessGranted = pdTRUE;
1680 }
1681 else
1682 {
1683 xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1684
1685 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1686 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1687
1688 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1689 {
1690 xAccessGranted = pdTRUE;
1691 }
1692 else
1693 {
1694 if( ( xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] & ( 1U << ulAccessControlListEntryBit ) ) != 0 )
1695 {
1696 xAccessGranted = pdTRUE;
1697 }
1698 }
1699 }
1700
1701 return xAccessGranted;
1702 }
1703
1704 #else /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1705
xPortIsAuthorizedToAccessKernelObject(int32_t lInternalIndexOfKernelObject)1706 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1707 {
1708 ( void ) lInternalIndexOfKernelObject;
1709
1710 /* If Access Control List feature is not used, all the tasks have
1711 * access to all the kernel objects. */
1712 return pdTRUE;
1713 }
1714
1715 #endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1716
1717 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1718 /*-----------------------------------------------------------*/
1719