2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ARM CM4F MPU port.
31 *----------------------------------------------------------*/
34 #include <intrinsics.h>
36 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
37 * all the API functions to use the MPU wrappers. That should only be done when
38 * task.h is included from an application file. */
39 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
41 /* Scheduler includes. */
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
48 #error This port can only be used when the project options are configured to enable hardware floating point support.
51 #if ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )
52 #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http: /*www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
55 #ifndef configSYSTICK_CLOCK_HZ
56 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
57 /* Ensure the SysTick is clocked at the same frequency as the core. */
58 #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
61 /* The way the SysTick is clocked is not modified in case it is not the same
63 #define portNVIC_SYSTICK_CLK_BIT ( 0 )
66 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
67 #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
68 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
71 /* Constants required to manipulate the core. Registers first... */
72 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
73 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
74 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
75 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
76 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
77 #define portNVIC_SYS_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
78 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
80 /* Constants required to access and manipulate the MPU. */
81 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
82 #define portMPU_REGION_BASE_ADDRESS_REG ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
83 #define portMPU_REGION_ATTRIBUTE_REG ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
84 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
85 #define portEXPECTED_MPU_TYPE_VALUE ( configTOTAL_MPU_REGIONS << 8UL )
86 #define portMPU_ENABLE ( 0x01UL )
87 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
88 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
89 #define portMPU_REGION_VALID ( 0x10UL )
90 #define portMPU_REGION_ENABLE ( 0x01UL )
91 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
92 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
94 /* ...then bits in the registers. */
95 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
96 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
97 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
98 #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
99 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
101 /* Constants used to detect Cortex-M7 r0p0 and r0p1 cores, and ensure
102 * that a work around is active for errata 837070. */
103 #define portCPUID ( *( ( volatile uint32_t * ) 0xE000ed00 ) )
104 #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
105 #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
107 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
108 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
109 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
110 #define portNVIC_SVC_PRI ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
112 /* Constants required to check the validity of an interrupt priority. */
113 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
114 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
115 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
116 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
117 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
118 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
119 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
120 #define portPRIGROUP_SHIFT ( 8UL )
122 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
123 #define portVECTACTIVE_MASK ( 0xFFUL )
125 /* Constants required to manipulate the VFP. */
126 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
127 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
129 /* Constants required to set up the initial stack. */
130 #define portINITIAL_XPSR ( 0x01000000 )
131 #define portINITIAL_EXC_RETURN ( 0xfffffffd )
132 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
133 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
135 /* Constants used during system call enter and exit. */
136 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
137 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
139 /* Offsets in the stack to the parameters when inside the SVC handler. */
140 #define portOFFSET_TO_LR ( 5 )
141 #define portOFFSET_TO_PC ( 6 )
142 #define portOFFSET_TO_PSR ( 7 )
144 /* The systick is a 24-bit counter. */
145 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
147 /* A fiddle factor to estimate the number of SysTick counts that would have
148 * occurred while the SysTick counter is stopped during tickless idle
150 #define portMISSED_COUNTS_FACTOR ( 45UL )
152 /* For strict compliance with the Cortex-M spec the task start address should
153 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
154 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
156 /* Does addr lie within [start, end] address range? */
157 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
158 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
160 /* Is the access request satisfied by the available permissions? */
161 #define portIS_AUTHORIZED( accessRequest, permissions ) \
162 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
164 /* Max value that fits in a uint32_t type. */
165 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
167 /* Check if adding a and b will result in overflow. */
168 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
169 /*-----------------------------------------------------------*/
172 * Configure a number of standard MPU regions that are used by all tasks.
174 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
177 * Return the smallest MPU region size that a given number of bytes will fit
178 * into. The region size is returned as the value that should be programmed
179 * into the region attribute register for that region.
181 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
184 * Setup the timer to generate the tick interrupts. The implementation in this
185 * file is weak to allow application writers to change the timer used to
186 * generate the tick interrupt.
188 void vPortSetupTimerInterrupt( void );
191 * Exception handlers.
193 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
196 * Start first task is a separate function so it can be tested in isolation.
198 extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;
203 extern void vPortEnableVFP( void );
206 * The C portion of the SVC handler.
208 void vPortSVCHandler_C( uint32_t * pulParam ) PRIVILEGED_FUNCTION;
211 * Called from the SVC handler used to start the scheduler.
213 extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;
216 * @brief Enter critical section.
218 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
219 void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
221 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
225 * @brief Exit from critical section.
227 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
228 void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
230 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
233 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
236 * @brief Sets up the system call stack so that upon returning from
237 * SVC, the system call stack is used.
239 * It is used for the system calls with up to 4 parameters.
241 * @param pulTaskStack The current SP when the SVC was raised.
242 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
244 void vSystemCallEnter( uint32_t * pulTaskStack,
245 uint32_t ulLR ) PRIVILEGED_FUNCTION;
247 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
249 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
252 * @brief Sets up the system call stack so that upon returning from
253 * SVC, the system call stack is used.
255 * It is used for the system calls with 5 parameters.
257 * @param pulTaskStack The current SP when the SVC was raised.
258 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
260 void vSystemCallEnter_1( uint32_t * pulTaskStack,
261 uint32_t ulLR ) PRIVILEGED_FUNCTION;
263 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
265 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
268 * @brief Sets up the task stack so that upon returning from
269 * SVC, the task stack is used again.
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.
274 void vSystemCallExit( uint32_t * pulSystemCallStack,
275 uint32_t ulLR ) PRIVILEGED_FUNCTION;
277 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
280 * @brief Checks whether or not the calling task is privileged.
282 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
284 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
287 * @brief Make a task unprivileged.
289 void vPortSwitchToUserMode( void );
291 /*-----------------------------------------------------------*/
293 /* Each task maintains its own interrupt status in the critical nesting
295 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
298 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
299 * FreeRTOS API functions are not called from interrupts that have been assigned
300 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
302 #if ( configASSERT_DEFINED == 1 )
303 static uint8_t ucMaxSysCallPriority = 0;
304 static uint32_t ulMaxPRIGROUPValue = 0;
305 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
306 #endif /* configASSERT_DEFINED */
308 /*-----------------------------------------------------------*/
311 * See header file for description.
313 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
314 TaskFunction_t pxCode,
316 BaseType_t xRunPrivileged,
317 xMPU_SETTINGS * xMPUSettings )
319 if( xRunPrivileged == pdTRUE )
321 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
322 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
326 xMPUSettings->ulTaskFlags &= ( ~( portTASK_IS_PRIVILEGED_FLAG ) );
327 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
330 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
331 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
332 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
333 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
334 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
335 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
336 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
337 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
338 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
340 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
341 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
342 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
343 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
344 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
345 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
346 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
347 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
348 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
350 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
352 /* Ensure that the system call stack is double word aligned. */
353 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
354 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
355 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
357 /* This is not NULL only for the duration of a system call. */
358 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
360 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
362 return &( xMPUSettings->ulContext[ 19 ] );
364 /*-----------------------------------------------------------*/
366 void vPortSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
371 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
372 extern uint32_t __syscalls_flash_start__[];
373 extern uint32_t __syscalls_flash_end__[];
374 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
376 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
377 * argument (r0) is pulParam[ 0 ]. */
378 ulPC = pulParam[ portOFFSET_TO_PC ];
379 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
381 switch( ucSVCNumber )
383 case portSVC_START_SCHEDULER:
384 portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
385 vPortRestoreContextOfFirstTask();
389 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
391 /* Barriers are normally not required
392 * but do ensure the code is completely
393 * within the specified behaviour for the
395 __asm volatile ( "dsb" ::: "memory" );
396 __asm volatile ( "isb" );
400 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
401 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
402 * svc was raised from any of the
405 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
406 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
410 " mrs r1, control \n" /* Obtain current control value. */
411 " bic r1, r1, #1 \n" /* Set privilege bit. */
412 " msr control, r1 \n" /* Write back new control value. */
418 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
419 case portSVC_RAISE_PRIVILEGE:
422 " mrs r1, control \n" /* Obtain current control value. */
423 " bic r1, r1, #1 \n" /* Set privilege bit. */
424 " msr control, r1 \n" /* Write back new control value. */
428 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
430 default: /* Unknown SVC call. */
434 /*-----------------------------------------------------------*/
436 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
438 void vSystemCallEnter( uint32_t * pulTaskStack,
439 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
441 extern TaskHandle_t pxCurrentTCB;
442 xMPU_SETTINGS * pxMpuSettings;
443 uint32_t * pulSystemCallStack;
444 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
446 #if defined( __ARMCC_VERSION )
448 /* Declaration when these variable are defined in code instead of being
449 * exported from linker scripts. */
450 extern uint32_t * __syscalls_flash_start__;
451 extern uint32_t * __syscalls_flash_end__;
453 /* Declaration when these variable are exported from linker scripts. */
454 extern uint32_t __syscalls_flash_start__[];
455 extern uint32_t __syscalls_flash_end__[];
456 #endif /* #if defined( __ARMCC_VERSION ) */
458 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
460 /* If the request did not come from the system call section, do nothing. */
461 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
462 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
464 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
465 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
467 /* This is not NULL only for the duration of the system call. */
468 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
470 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
472 /* Extended frame i.e. FPU in use. */
473 ulStackFrameSize = 26;
475 " vpush {s0} \n" /* Trigger lazy stacking. */
476 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
482 /* Standard frame i.e. FPU not in use. */
483 ulStackFrameSize = 8;
486 /* Make space on the system call stack for the stack frame. */
487 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
489 /* Copy the stack frame. */
490 for( i = 0; i < ulStackFrameSize; i++ )
492 pulSystemCallStack[ i ] = pulTaskStack[ i ];
495 /* Use the pulSystemCallStack in thread mode. */
496 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
498 /* Raise the privilege for the duration of the system call. */
500 " mrs r1, control \n" /* Obtain current control value. */
501 " bic r1, #1 \n" /* Clear nPRIV bit. */
502 " msr control, r1 \n" /* Write back new control value. */
506 /* Remember the location where we should copy the stack frame when we exit from
507 * the system call. */
508 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
510 /* Store the value of the Link Register before the SVC was raised. We need to
511 * restore it when we exit from the system call. */
512 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
514 /* Record if the hardware used padding to force the stack pointer
515 * to be double word aligned. */
516 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
518 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
522 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
525 /* We ensure in pxPortInitialiseStack that the system call stack is
526 * double word aligned and therefore, there is no need of padding.
527 * Clear the bit[9] of stacked xPSR. */
528 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
532 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
533 /*-----------------------------------------------------------*/
535 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
537 void vSystemCallEnter_1( uint32_t * pulTaskStack,
538 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
540 extern TaskHandle_t pxCurrentTCB;
541 xMPU_SETTINGS * pxMpuSettings;
542 uint32_t * pulSystemCallStack;
543 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
545 #if defined( __ARMCC_VERSION )
547 /* Declaration when these variable are defined in code instead of being
548 * exported from linker scripts. */
549 extern uint32_t * __syscalls_flash_start__;
550 extern uint32_t * __syscalls_flash_end__;
552 /* Declaration when these variable are exported from linker scripts. */
553 extern uint32_t __syscalls_flash_start__[];
554 extern uint32_t __syscalls_flash_end__[];
555 #endif /* #if defined( __ARMCC_VERSION ) */
557 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
559 /* If the request did not come from the system call section, do nothing. */
560 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
561 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
563 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
564 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
566 /* This is not NULL only for the duration of the system call. */
567 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
569 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
571 /* Extended frame i.e. FPU in use. */
572 ulStackFrameSize = 26;
574 " vpush {s0} \n" /* Trigger lazy stacking. */
575 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
581 /* Standard frame i.e. FPU not in use. */
582 ulStackFrameSize = 8;
585 /* Make space on the system call stack for the stack frame and
586 * the parameter passed on the stack. We only need to copy one
587 * parameter but we still reserve 2 spaces to keep the stack
588 * double word aligned. */
589 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize - 2UL;
591 /* Copy the stack frame. */
592 for( i = 0; i < ulStackFrameSize; i++ )
594 pulSystemCallStack[ i ] = pulTaskStack[ i ];
597 /* Copy the parameter which is passed the stack. */
598 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
600 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize + 1 ];
602 /* Record if the hardware used padding to force the stack pointer
603 * to be double word aligned. */
604 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
608 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize ];
610 /* Record if the hardware used padding to force the stack pointer
611 * to be double word aligned. */
612 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
615 /* Use the pulSystemCallStack in thread mode. */
616 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
618 /* Raise the privilege for the duration of the system call. */
620 " mrs r1, control \n" /* Obtain current control value. */
621 " bic r1, #1 \n" /* Clear nPRIV bit. */
622 " msr control, r1 \n" /* Write back new control value. */
626 /* Remember the location where we should copy the stack frame when we exit from
627 * the system call. */
628 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
630 /* Store the value of the Link Register before the SVC was raised. We need to
631 * restore it when we exit from the system call. */
632 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
634 /* We ensure in pxPortInitialiseStack that the system call stack is
635 * double word aligned and therefore, there is no need of padding.
636 * Clear the bit[9] of stacked xPSR. */
637 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
641 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
642 /*-----------------------------------------------------------*/
644 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
646 void vSystemCallExit( uint32_t * pulSystemCallStack,
647 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
649 extern TaskHandle_t pxCurrentTCB;
650 xMPU_SETTINGS * pxMpuSettings;
651 uint32_t * pulTaskStack;
652 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
654 #if defined( __ARMCC_VERSION )
656 /* Declaration when these variable are defined in code instead of being
657 * exported from linker scripts. */
658 extern uint32_t * __syscalls_flash_start__;
659 extern uint32_t * __syscalls_flash_end__;
661 /* Declaration when these variable are exported from linker scripts. */
662 extern uint32_t __syscalls_flash_start__[];
663 extern uint32_t __syscalls_flash_end__[];
664 #endif /* #if defined( __ARMCC_VERSION ) */
666 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
668 /* If the request did not come from the system call section, do nothing. */
669 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
670 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
672 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
673 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
675 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
677 /* Extended frame i.e. FPU in use. */
678 ulStackFrameSize = 26;
680 " vpush {s0} \n" /* Trigger lazy stacking. */
681 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
687 /* Standard frame i.e. FPU not in use. */
688 ulStackFrameSize = 8;
691 /* Make space on the task stack for the stack frame. */
692 pulTaskStack = pulTaskStack - ulStackFrameSize;
694 /* Copy the stack frame. */
695 for( i = 0; i < ulStackFrameSize; i++ )
697 pulTaskStack[ i ] = pulSystemCallStack[ i ];
700 /* Use the pulTaskStack in thread mode. */
701 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
703 /* Drop the privilege before returning to the thread mode. */
705 " mrs r1, control \n" /* Obtain current control value. */
706 " orr r1, #1 \n" /* Set nPRIV bit. */
707 " msr control, r1 \n" /* Write back new control value. */
711 /* Restore the stacked link register to what it was at the time of
712 * system call entry. */
713 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
715 /* If the hardware used padding to force the stack pointer
716 * to be double word aligned, set the stacked xPSR bit[9],
717 * otherwise clear it. */
718 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
720 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
724 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
727 /* This is not NULL only for the duration of the system call. */
728 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
732 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
733 /*-----------------------------------------------------------*/
735 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
737 BaseType_t xTaskIsPrivileged = pdFALSE;
738 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
740 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
742 xTaskIsPrivileged = pdTRUE;
745 return xTaskIsPrivileged;
747 /*-----------------------------------------------------------*/
749 void vPortSwitchToUserMode( void )
751 /* Load the current task's MPU settings from its TCB. */
752 xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL );
754 /* Mark the task as unprivileged. */
755 xTaskMpuSettings->ulTaskFlags &= ( ~( portTASK_IS_PRIVILEGED_FLAG ) );
757 /* Lower the processor's privilege level. */
760 /*-----------------------------------------------------------*/
763 * See header file for description.
765 BaseType_t xPortStartScheduler( void )
767 /* Errata 837070 workaround must only be enabled on Cortex-M7 r0p0
769 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
770 configASSERT( ( portCPUID == portCORTEX_M7_r0p1_ID ) || ( portCPUID == portCORTEX_M7_r0p0_ID ) );
773 /* When using this port on a Cortex-M7 r0p0 or r0p1 core, define
774 * configENABLE_ERRATA_837070_WORKAROUND to 1 in your
775 * FreeRTOSConfig.h. */
776 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
777 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
780 #if ( configASSERT_DEFINED == 1 )
782 volatile uint8_t ucOriginalPriority;
783 volatile uint32_t ulImplementedPrioBits = 0;
784 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
785 volatile uint8_t ucMaxPriorityValue;
787 /* Determine the maximum priority from which ISR safe FreeRTOS API
788 * functions can be called. ISR safe functions are those that end in
789 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
790 * ensure interrupt entry is as fast and simple as possible.
792 * Save the interrupt priority value that is about to be clobbered. */
793 ucOriginalPriority = *pucFirstUserPriorityRegister;
795 /* Determine the number of priority bits available. First write to all
797 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
799 /* Read the value back to see how many bits stuck. */
800 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
802 /* Use the same mask on the maximum system call priority. */
803 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
805 /* Check that the maximum system call priority is nonzero after
806 * accounting for the number of priority bits supported by the
807 * hardware. A priority of 0 is invalid because setting the BASEPRI
808 * register to 0 unmasks all interrupts, and interrupts with priority 0
809 * cannot be masked using BASEPRI.
810 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
811 configASSERT( ucMaxSysCallPriority );
813 /* Check that the bits not implemented in hardware are zero in
814 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
815 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( uint8_t ) ( ~( uint32_t ) ucMaxPriorityValue ) ) == 0U );
817 /* Calculate the maximum acceptable priority group value for the number
818 * of bits read back. */
820 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
822 ulImplementedPrioBits++;
823 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
826 if( ulImplementedPrioBits == 8 )
828 /* When the hardware implements 8 priority bits, there is no way for
829 * the software to configure PRIGROUP to not have sub-priorities. As
830 * a result, the least significant bit is always used for sub-priority
831 * and there are 128 preemption priorities and 2 sub-priorities.
833 * This may cause some confusion in some cases - for example, if
834 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
835 * priority interrupts will be masked in Critical Sections as those
836 * are at the same preemption priority. This may appear confusing as
837 * 4 is higher (numerically lower) priority than
838 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
839 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
840 * to 4, this confusion does not happen and the behaviour remains the same.
842 * The following assert ensures that the sub-priority bit in the
843 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
845 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
846 ulMaxPRIGROUPValue = 0;
850 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
853 /* Shift the priority group value back to its position within the AIRCR
855 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
856 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
858 /* Restore the clobbered interrupt priority register to its original
860 *pucFirstUserPriorityRegister = ucOriginalPriority;
862 #endif /* configASSERT_DEFINED */
864 /* Make PendSV and SysTick the lowest priority interrupts. */
865 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
866 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
868 /* Configure the regions in the MPU that are common to all tasks. */
871 /* Start the timer that generates the tick ISR. Interrupts are disabled
873 vPortSetupTimerInterrupt();
875 /* Initialise the critical nesting count ready for the first task. */
876 uxCriticalNesting = 0;
878 /* Ensure the VFP is enabled - it should be anyway. */
881 /* Lazy save always. */
882 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
884 /* Start the first task. */
885 vPortStartFirstTask();
887 /* Should not get here! */
890 /*-----------------------------------------------------------*/
892 void vPortEndScheduler( void )
894 /* Not implemented in ports where there is nothing to return to.
895 * Artificially force an assert. */
896 configASSERT( uxCriticalNesting == 1000UL );
898 /*-----------------------------------------------------------*/
900 void vPortEnterCritical( void )
902 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
903 if( portIS_PRIVILEGED() == pdFALSE )
905 portRAISE_PRIVILEGE();
906 portMEMORY_BARRIER();
908 portDISABLE_INTERRUPTS();
911 /* This is not the interrupt safe version of the enter critical function so
912 * assert() if it is being called from an interrupt context. Only API
913 * functions that end in "FromISR" can be used in an interrupt. Only assert if
914 * the critical nesting count is 1 to protect against recursive calls if the
915 * assert function also uses a critical section. */
916 if( uxCriticalNesting == 1 )
918 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
921 portMEMORY_BARRIER();
923 portRESET_PRIVILEGE();
924 portMEMORY_BARRIER();
928 portDISABLE_INTERRUPTS();
931 /* This is not the interrupt safe version of the enter critical function so
932 * assert() if it is being called from an interrupt context. Only API
933 * functions that end in "FromISR" can be used in an interrupt. Only assert if
934 * the critical nesting count is 1 to protect against recursive calls if the
935 * assert function also uses a critical section. */
936 if( uxCriticalNesting == 1 )
938 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
941 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
942 portDISABLE_INTERRUPTS();
945 /* This is not the interrupt safe version of the enter critical function so
946 * assert() if it is being called from an interrupt context. Only API
947 * functions that end in "FromISR" can be used in an interrupt. Only assert if
948 * the critical nesting count is 1 to protect against recursive calls if the
949 * assert function also uses a critical section. */
950 if( uxCriticalNesting == 1 )
952 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
954 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
956 /*-----------------------------------------------------------*/
958 void vPortExitCritical( void )
960 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
961 if( portIS_PRIVILEGED() == pdFALSE )
963 portRAISE_PRIVILEGE();
964 portMEMORY_BARRIER();
966 configASSERT( uxCriticalNesting );
969 if( uxCriticalNesting == 0 )
971 portENABLE_INTERRUPTS();
974 portMEMORY_BARRIER();
976 portRESET_PRIVILEGE();
977 portMEMORY_BARRIER();
981 configASSERT( uxCriticalNesting );
984 if( uxCriticalNesting == 0 )
986 portENABLE_INTERRUPTS();
989 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
990 configASSERT( uxCriticalNesting );
993 if( uxCriticalNesting == 0 )
995 portENABLE_INTERRUPTS();
997 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
999 /*-----------------------------------------------------------*/
1001 void xPortSysTickHandler( void )
1003 /* The SysTick runs at the lowest interrupt priority, so when this interrupt
1004 * executes all interrupts must be unmasked. There is therefore no need to
1005 * save and then restore the interrupt mask value as its value is already
1007 portDISABLE_INTERRUPTS();
1010 /* Increment the RTOS tick. */
1011 if( xTaskIncrementTick() != pdFALSE )
1013 traceISR_EXIT_TO_SCHEDULER();
1015 /* A context switch is required. Context switching is performed in
1016 * the PendSV interrupt. Pend the PendSV interrupt. */
1017 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
1024 portENABLE_INTERRUPTS();
1026 /*-----------------------------------------------------------*/
1029 * Setup the systick timer to generate the tick interrupts at the required
1032 __weak void vPortSetupTimerInterrupt( void )
1034 /* Stop and clear the SysTick. */
1035 portNVIC_SYSTICK_CTRL_REG = 0UL;
1036 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1038 /* Configure SysTick to interrupt at the requested rate. */
1039 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1040 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
1042 /*-----------------------------------------------------------*/
1044 static void prvSetupMPU( void )
1046 extern uint32_t __privileged_functions_start__[];
1047 extern uint32_t __privileged_functions_end__[];
1048 extern uint32_t __FLASH_segment_start__[];
1049 extern uint32_t __FLASH_segment_end__[];
1050 extern uint32_t __privileged_data_start__[];
1051 extern uint32_t __privileged_data_end__[];
1053 /* The only permitted number of regions are 8 or 16. */
1054 configASSERT( ( configTOTAL_MPU_REGIONS == 8 ) || ( configTOTAL_MPU_REGIONS == 16 ) );
1056 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
1057 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
1059 /* Check the expected MPU is present. */
1060 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1062 /* First setup the unprivileged flash for unprivileged read only access. */
1063 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1064 ( portMPU_REGION_VALID ) |
1065 ( portUNPRIVILEGED_FLASH_REGION );
1067 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1068 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1069 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1070 ( portMPU_REGION_ENABLE );
1072 /* Setup the privileged flash for privileged only access. This is where
1073 * the kernel code is placed. */
1074 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1075 ( portMPU_REGION_VALID ) |
1076 ( portPRIVILEGED_FLASH_REGION );
1078 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1079 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1080 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1081 ( portMPU_REGION_ENABLE );
1083 /* Setup the privileged data RAM region. This is where the kernel data
1085 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1086 ( portMPU_REGION_VALID ) |
1087 ( portPRIVILEGED_RAM_REGION );
1089 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1090 ( portMPU_REGION_EXECUTE_NEVER ) |
1091 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1092 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1093 ( portMPU_REGION_ENABLE );
1095 /* By default allow everything to access the general peripherals. The
1096 * system peripherals and registers are protected. */
1097 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1098 ( portMPU_REGION_VALID ) |
1099 ( portGENERAL_PERIPHERALS_REGION );
1101 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1102 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1103 ( portMPU_REGION_ENABLE );
1105 /* Enable the memory fault exception. */
1106 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1108 /* Enable the MPU with the background region configured. */
1109 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1112 /*-----------------------------------------------------------*/
1114 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1116 uint32_t ulRegionSize, ulReturnValue = 4;
1118 /* 32 is the smallest region size, 31 is the largest valid value for
1120 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1122 if( ulActualSizeInBytes <= ulRegionSize )
1132 /* Shift the code by one before returning so it can be written directly
1133 * into the the correct bit position of the attribute register. */
1134 return( ulReturnValue << 1UL );
1136 /*-----------------------------------------------------------*/
1138 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1139 const struct xMEMORY_REGION * const xRegions,
1140 StackType_t * pxBottomOfStack,
1141 uint32_t ulStackDepth )
1143 extern uint32_t __SRAM_segment_start__[];
1144 extern uint32_t __SRAM_segment_end__[];
1145 extern uint32_t __privileged_data_start__[];
1146 extern uint32_t __privileged_data_end__[];
1150 if( xRegions == NULL )
1152 /* No MPU regions are specified so allow access to all RAM. */
1153 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1154 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1155 ( portMPU_REGION_VALID ) |
1156 ( portSTACK_REGION ); /* Region number. */
1158 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1159 ( portMPU_REGION_READ_WRITE ) |
1160 ( portMPU_REGION_EXECUTE_NEVER ) |
1161 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1162 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1163 ( portMPU_REGION_ENABLE );
1165 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1166 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1167 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1168 tskMPU_WRITE_PERMISSION );
1170 /* Invalidate user configurable regions. */
1171 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1173 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1174 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1175 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1176 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1177 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1182 /* This function is called automatically when the task is created - in
1183 * which case the stack region parameters will be valid. At all other
1184 * times the stack parameters will not be valid and it is assumed that the
1185 * stack region has already been configured. */
1186 if( ulStackDepth > 0 )
1188 /* Define the region that allows access to the stack. */
1189 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1190 ( ( uint32_t ) pxBottomOfStack ) |
1191 ( portMPU_REGION_VALID ) |
1192 ( portSTACK_REGION ); /* Region number. */
1194 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1195 ( portMPU_REGION_READ_WRITE ) |
1196 ( portMPU_REGION_EXECUTE_NEVER ) |
1197 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1198 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1199 ( portMPU_REGION_ENABLE );
1201 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1202 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1203 ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1UL );
1204 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1205 tskMPU_WRITE_PERMISSION );
1210 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1212 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1214 /* Translate the generic region definition contained in
1215 * xRegions into the CM4 specific MPU settings that are then
1216 * stored in xMPUSettings. */
1217 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1218 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1219 ( portMPU_REGION_VALID ) |
1220 ( ul - 1UL ); /* Region number. */
1222 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1223 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1224 ( xRegions[ lIndex ].ulParameters ) |
1225 ( portMPU_REGION_ENABLE );
1227 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1228 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1229 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1231 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1232 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1234 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1237 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1239 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1244 /* Invalidate the region. */
1245 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1246 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1247 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1248 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1249 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1256 /*-----------------------------------------------------------*/
1258 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1259 uint32_t ulBufferLength,
1260 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1263 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1264 BaseType_t xAccessGranted = pdFALSE;
1265 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1267 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1269 xAccessGranted = pdTRUE;
1273 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1275 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1276 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1278 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1280 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1281 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1282 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1283 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1284 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1285 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1286 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1288 xAccessGranted = pdTRUE;
1295 return xAccessGranted;
1297 /*-----------------------------------------------------------*/
1300 #if ( configASSERT_DEFINED == 1 )
1302 void vPortValidateInterruptPriority( void )
1304 uint32_t ulCurrentInterrupt;
1305 uint8_t ucCurrentPriority;
1307 /* Obtain the number of the currently executing interrupt. */
1308 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1310 /* Is the interrupt number a user defined interrupt? */
1311 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1313 /* Look up the interrupt's priority. */
1314 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1316 /* The following assertion will fail if a service routine (ISR) for
1317 * an interrupt that has been assigned a priority above
1318 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1319 * function. ISR safe FreeRTOS API functions must *only* be called
1320 * from interrupts that have been assigned a priority at or below
1321 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1323 * Numerically low interrupt priority numbers represent logically high
1324 * interrupt priorities, therefore the priority of the interrupt must
1325 * be set to a value equal to or numerically *higher* than
1326 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1328 * Interrupts that use the FreeRTOS API must not be left at their
1329 * default priority of zero as that is the highest possible priority,
1330 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1331 * and therefore also guaranteed to be invalid.
1333 * FreeRTOS maintains separate thread and ISR API functions to ensure
1334 * interrupt entry is as fast and simple as possible.
1336 * The following links provide detailed information:
1337 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1338 * https://www.FreeRTOS.org/FAQHelp.html */
1339 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1342 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1343 * that define each interrupt's priority to be split between bits that
1344 * define the interrupt's pre-emption priority bits and bits that define
1345 * the interrupt's sub-priority. For simplicity all bits must be defined
1346 * to be pre-emption priority bits. The following assertion will fail if
1347 * this is not the case (if some bits represent a sub-priority).
1349 * If the application only uses CMSIS libraries for interrupt
1350 * configuration then the correct setting can be achieved on all Cortex-M
1351 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1352 * scheduler. Note however that some vendor specific peripheral libraries
1353 * assume a non-zero priority group setting, in which cases using a value
1354 * of zero will result in unpredictable behaviour. */
1355 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1358 #endif /* configASSERT_DEFINED */
1359 /*-----------------------------------------------------------*/
1361 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1363 void vPortGrantAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1364 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1366 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1367 xMPU_SETTINGS * xTaskMpuSettings;
1369 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1370 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1372 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1374 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] |= ( 1U << ulAccessControlListEntryBit );
1377 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1378 /*-----------------------------------------------------------*/
1380 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1382 void vPortRevokeAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1383 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1385 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1386 xMPU_SETTINGS * xTaskMpuSettings;
1388 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1389 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1391 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1393 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] &= ~( 1U << ulAccessControlListEntryBit );
1396 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1397 /*-----------------------------------------------------------*/
1399 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1401 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
1403 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1405 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1406 BaseType_t xAccessGranted = pdFALSE;
1407 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1409 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1410 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1412 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1414 xAccessGranted = pdTRUE;
1418 if( ( xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] & ( 1U << ulAccessControlListEntryBit ) ) != 0 )
1420 xAccessGranted = pdTRUE;
1424 return xAccessGranted;
1427 #else /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1429 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1431 ( void ) lInternalIndexOfKernelObject;
1433 /* If Access Control List feature is not used, all the tasks have
1434 * access to all the kernel objects. */
1438 #endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1440 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1441 /*-----------------------------------------------------------*/