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 CM3 MPU port.
31 *----------------------------------------------------------*/
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
38 /* Scheduler includes. */
42 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
44 #ifndef configSYSTICK_CLOCK_HZ
45 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
46 /* Ensure the SysTick is clocked at the same frequency as the core. */
47 #define portNVIC_SYSTICK_CLK ( 1UL << 2UL )
50 /* The way the SysTick is clocked is not modified in case it is not the same
52 #define portNVIC_SYSTICK_CLK ( 0 )
55 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
56 #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
57 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
60 /* Constants required to access and manipulate the NVIC. */
61 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
62 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
63 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
64 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
65 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
66 #define portNVIC_SYS_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
67 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
69 /* Constants required to access and manipulate the MPU. */
70 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
71 #define portMPU_REGION_BASE_ADDRESS_REG ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
72 #define portMPU_REGION_ATTRIBUTE_REG ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
73 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
74 #define portEXPECTED_MPU_TYPE_VALUE ( 8UL << 8UL ) /* 8 regions, unified. */
75 #define portMPU_ENABLE ( 0x01UL )
76 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
77 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
78 #define portMPU_REGION_VALID ( 0x10UL )
79 #define portMPU_REGION_ENABLE ( 0x01UL )
80 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
81 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
83 /* Constants required to access and manipulate the SysTick. */
84 #define portNVIC_SYSTICK_INT ( 0x00000002UL )
85 #define portNVIC_SYSTICK_ENABLE ( 0x00000001UL )
86 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
87 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
88 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
89 #define portNVIC_SVC_PRI ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
91 /* Constants required to set up the initial stack. */
92 #define portINITIAL_XPSR ( 0x01000000 )
93 #define portINITIAL_EXC_RETURN ( 0xfffffffdUL )
94 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
95 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
97 /* Constants required to check the validity of an interrupt priority. */
98 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
99 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
100 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
101 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
102 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
103 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
104 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
105 #define portPRIGROUP_SHIFT ( 8UL )
107 /* Constants used during system call enter and exit. */
108 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
110 /* Offsets in the stack to the parameters when inside the SVC handler. */
111 #define portOFFSET_TO_LR ( 5 )
112 #define portOFFSET_TO_PC ( 6 )
113 #define portOFFSET_TO_PSR ( 7 )
115 /* For strict compliance with the Cortex-M spec the task start address should
116 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
117 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
119 /* Does addr lie within [start, end] address range? */
120 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
121 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
123 /* Is the access request satisfied by the available permissions? */
124 #define portIS_AUTHORIZED( accessRequest, permissions ) \
125 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
127 /* Max value that fits in a uint32_t type. */
128 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
130 /* Check if adding a and b will result in overflow. */
131 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
132 /*-----------------------------------------------------------*/
135 * Configure a number of standard MPU regions that are used by all tasks.
137 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
140 * Return the smallest MPU region size that a given number of bytes will fit
141 * into. The region size is returned as the value that should be programmed
142 * into the region attribute register for that region.
144 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
147 * Setup the timer to generate the tick interrupts. The implementation in this
148 * file is weak to allow application writers to change the timer used to
149 * generate the tick interrupt.
151 void vPortSetupTimerInterrupt( void );
154 * Standard FreeRTOS exception handlers.
156 void xPortPendSVHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
157 void xPortSysTickHandler( void ) __attribute__( ( optimize( "3" ) ) ) PRIVILEGED_FUNCTION;
158 void vPortSVCHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
161 * Starts the scheduler by restoring the context of the first task to run.
163 static void prvRestoreContextOfFirstTask( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
166 * C portion of the SVC handler. The SVC handler is split between an asm entry
167 * and a C wrapper for simplicity of coding and maintenance.
169 void vSVCHandler_C( uint32_t * pulRegisters ) __attribute__( ( noinline ) ) PRIVILEGED_FUNCTION;
172 * @brief Checks whether or not the processor is privileged.
174 * @return 1 if the processor is already privileged, 0 otherwise.
176 BaseType_t xIsPrivileged( void ) __attribute__( ( naked ) );
179 * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
182 * Bit 0 of the CONTROL register defines the privilege level of Thread Mode.
183 * Bit[0] = 0 --> The processor is running privileged
184 * Bit[0] = 1 --> The processor is running unprivileged.
186 void vResetPrivilege( void ) __attribute__( ( naked ) );
189 * @brief Enter critical section.
191 #if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
192 void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
194 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
198 * @brief Exit from critical section.
200 #if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
201 void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
203 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
206 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
209 * @brief Sets up the system call stack so that upon returning from
210 * SVC, the system call stack is used.
212 * It is used for the system calls with up to 4 parameters.
214 * @param pulTaskStack The current SP when the SVC was raised.
216 void vSystemCallEnter( uint32_t * pulTaskStack ) PRIVILEGED_FUNCTION;
218 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
220 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
223 * @brief Sets up the system call stack so that upon returning from
224 * SVC, the system call stack is used.
226 * It is used for the system calls with 5 parameters.
228 * @param pulTaskStack The current SP when the SVC was raised.
230 void vSystemCallEnter_1( uint32_t * pulTaskStack ) PRIVILEGED_FUNCTION;
232 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
234 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
237 * @brief Sets up the task stack so that upon returning from
238 * SVC, the task stack is used again.
240 * @param pulSystemCallStack The current SP when the SVC was raised.
242 void vSystemCallExit( uint32_t * pulSystemCallStack ) PRIVILEGED_FUNCTION;
244 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
247 * @brief Checks whether or not the calling task is privileged.
249 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
251 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
252 /*-----------------------------------------------------------*/
254 /* Each task maintains its own interrupt status in the critical nesting
255 * variable. Note this is not saved as part of the task context as context
256 * switches can only occur when uxCriticalNesting is zero. */
257 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
260 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
261 * FreeRTOS API functions are not called from interrupts that have been assigned
262 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
264 #if ( configASSERT_DEFINED == 1 )
265 static uint8_t ucMaxSysCallPriority = 0;
266 static uint32_t ulMaxPRIGROUPValue = 0;
267 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
268 #endif /* configASSERT_DEFINED */
269 /*-----------------------------------------------------------*/
272 * See header file for description.
274 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
275 TaskFunction_t pxCode,
277 BaseType_t xRunPrivileged,
278 xMPU_SETTINGS * xMPUSettings )
280 if( xRunPrivileged == pdTRUE )
282 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
283 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
287 xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
288 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
290 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
291 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
292 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
293 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
294 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
295 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
296 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
297 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
298 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
300 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
301 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
302 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
303 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
304 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
305 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
306 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
307 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
308 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
310 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
312 /* Ensure that the system call stack is double word aligned. */
313 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
314 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
315 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
317 /* This is not NULL only for the duration of a system call. */
318 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
320 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
322 return &( xMPUSettings->ulContext[ 19 ] );
324 /*-----------------------------------------------------------*/
326 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
328 void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
333 ".extern vSVCHandler_C \n"
334 ".extern vSystemCallEnter \n"
335 ".extern vSystemCallEnter_1 \n"
336 ".extern vSystemCallExit \n"
343 "ldr r1, [r0, #24] \n"
344 "ldrb r2, [r1, #-2] \n"
346 "beq vSystemCallEnter \n"
348 "beq vSystemCallEnter_1 \n"
350 "beq vSystemCallExit \n"
354 :"i" ( portSVC_SYSTEM_CALL_ENTER ), "i" ( portSVC_SYSTEM_CALL_ENTER_1 ), "i" ( portSVC_SYSTEM_CALL_EXIT )
355 : "r0", "r1", "r2", "memory"
359 #else /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
361 void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
363 /* Assumes psp was in use. */
366 #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
375 ::"i" ( vSVCHandler_C ) : "r0", "memory"
379 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
380 /*-----------------------------------------------------------*/
382 void vSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
387 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
388 #if defined( __ARMCC_VERSION )
390 /* Declaration when these variable are defined in code instead of being
391 * exported from linker scripts. */
392 extern uint32_t * __syscalls_flash_start__;
393 extern uint32_t * __syscalls_flash_end__;
395 /* Declaration when these variable are exported from linker scripts. */
396 extern uint32_t __syscalls_flash_start__[];
397 extern uint32_t __syscalls_flash_end__[];
398 #endif /* #if defined( __ARMCC_VERSION ) */
399 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
401 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
402 * argument (r0) is pulParam[ 0 ]. */
403 ulPC = pulParam[ portOFFSET_TO_PC ];
404 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
406 switch( ucSVCNumber )
408 case portSVC_START_SCHEDULER:
409 portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
410 prvRestoreContextOfFirstTask();
414 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
416 /* Barriers are normally not required
417 * but do ensure the code is completely
418 * within the specified behaviour for the
420 __asm volatile ( "dsb" ::: "memory" );
421 __asm volatile ( "isb" );
425 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
426 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
427 * svc was raised from any of the
430 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
431 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
435 " mrs r1, control \n"/* Obtain current control value. */
436 " bic r1, #1 \n"/* Set privilege bit. */
437 " msr control, r1 \n"/* Write back new control value. */
443 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
444 case portSVC_RAISE_PRIVILEGE:
447 " mrs r1, control \n"/* Obtain current control value. */
448 " bic r1, #1 \n"/* Set privilege bit. */
449 " msr control, r1 \n"/* Write back new control value. */
453 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
455 default: /* Unknown SVC call. */
459 /*-----------------------------------------------------------*/
461 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
463 void vSystemCallEnter( uint32_t * pulTaskStack ) /* PRIVILEGED_FUNCTION */
465 extern TaskHandle_t pxCurrentTCB;
466 xMPU_SETTINGS * pxMpuSettings;
467 uint32_t * pulSystemCallStack;
468 uint32_t ulSystemCallLocation, i;
469 const uint32_t ulStackFrameSize = 8;
470 #if defined( __ARMCC_VERSION )
471 /* Declaration when these variable are defined in code instead of being
472 * exported from linker scripts. */
473 extern uint32_t * __syscalls_flash_start__;
474 extern uint32_t * __syscalls_flash_end__;
476 /* Declaration when these variable are exported from linker scripts. */
477 extern uint32_t __syscalls_flash_start__[];
478 extern uint32_t __syscalls_flash_end__[];
479 #endif /* #if defined( __ARMCC_VERSION ) */
481 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
483 /* If the request did not come from the system call section, do nothing. */
484 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
485 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
487 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
488 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
490 /* This is not NULL only for the duration of the system call. */
491 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
493 /* Make space on the system call stack for the stack frame. */
494 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
496 /* Copy the stack frame. */
497 for( i = 0; i < ulStackFrameSize; i++ )
499 pulSystemCallStack[ i ] = pulTaskStack[ i ];
502 /* Use the pulSystemCallStack in thread mode. */
503 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
505 /* Raise the privilege for the duration of the system call. */
507 " mrs r1, control \n" /* Obtain current control value. */
508 " bic r1, #1 \n" /* Clear nPRIV bit. */
509 " msr control, r1 \n" /* Write back new control value. */
513 /* Remember the location where we should copy the stack frame when we exit from
514 * the system call. */
515 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
517 /* Store the value of the Link Register before the SVC was raised. We need to
518 * restore it when we exit from the system call. */
519 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
521 /* Record if the hardware used padding to force the stack pointer
522 * to be double word aligned. */
523 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
525 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
529 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
532 /* We ensure in pxPortInitialiseStack that the system call stack is
533 * double word aligned and therefore, there is no need of padding.
534 * Clear the bit[9] of stacked xPSR. */
535 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
539 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
540 /*-----------------------------------------------------------*/
542 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
544 void vSystemCallEnter_1( uint32_t * pulTaskStack ) /* PRIVILEGED_FUNCTION */
546 extern TaskHandle_t pxCurrentTCB;
547 xMPU_SETTINGS * pxMpuSettings;
548 uint32_t * pulSystemCallStack;
549 uint32_t ulSystemCallLocation, i;
550 const uint32_t ulStackFrameSize = 8;
551 #if defined( __ARMCC_VERSION )
552 /* Declaration when these variable are defined in code instead of being
553 * exported from linker scripts. */
554 extern uint32_t * __syscalls_flash_start__;
555 extern uint32_t * __syscalls_flash_end__;
557 /* Declaration when these variable are exported from linker scripts. */
558 extern uint32_t __syscalls_flash_start__[];
559 extern uint32_t __syscalls_flash_end__[];
560 #endif /* #if defined( __ARMCC_VERSION ) */
562 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
564 /* If the request did not come from the system call section, do nothing. */
565 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
566 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
568 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
569 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
571 /* This is not NULL only for the duration of the system call. */
572 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
574 /* Make space on the system call stack for the stack frame and
575 * the parameter passed on the stack. We only need to copy one
576 * parameter but we still reserve 2 spaces to keep the stack
577 * double word aligned. */
578 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize - 2UL;
580 /* Copy the stack frame. */
581 for( i = 0; i < ulStackFrameSize; i++ )
583 pulSystemCallStack[ i ] = pulTaskStack[ i ];
586 /* Copy the parameter which is passed the stack. */
587 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
589 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize + 1 ];
590 /* Record if the hardware used padding to force the stack pointer
591 * to be double word aligned. */
592 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
596 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize ];
597 /* Record if the hardware used padding to force the stack pointer
598 * to be double word aligned. */
599 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
602 /* Use the pulSystemCallStack in thread mode. */
603 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
605 /* Raise the privilege for the duration of the system call. */
607 " mrs r1, control \n" /* Obtain current control value. */
608 " bic r1, #1 \n" /* Clear nPRIV bit. */
609 " msr control, r1 \n" /* Write back new control value. */
613 /* Remember the location where we should copy the stack frame when we exit from
614 * the system call. */
615 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
617 /* Store the value of the Link Register before the SVC was raised. We need to
618 * restore it when we exit from the system call. */
619 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
621 /* We ensure in pxPortInitialiseStack that the system call stack is
622 * double word aligned and therefore, there is no need of padding.
623 * Clear the bit[9] of stacked xPSR. */
624 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
628 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
629 /*-----------------------------------------------------------*/
631 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
633 void vSystemCallExit( uint32_t * pulSystemCallStack ) /* PRIVILEGED_FUNCTION */
635 extern TaskHandle_t pxCurrentTCB;
636 xMPU_SETTINGS * pxMpuSettings;
637 uint32_t * pulTaskStack;
638 uint32_t ulSystemCallLocation, i;
639 const uint32_t ulStackFrameSize = 8;
640 #if defined( __ARMCC_VERSION )
641 /* Declaration when these variable are defined in code instead of being
642 * exported from linker scripts. */
643 extern uint32_t * __syscalls_flash_start__;
644 extern uint32_t * __syscalls_flash_end__;
646 /* Declaration when these variable are exported from linker scripts. */
647 extern uint32_t __syscalls_flash_start__[];
648 extern uint32_t __syscalls_flash_end__[];
649 #endif /* #if defined( __ARMCC_VERSION ) */
651 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
653 /* If the request did not come from the system call section, do nothing. */
654 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
655 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
657 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
658 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
660 /* Make space on the task stack for the stack frame. */
661 pulTaskStack = pulTaskStack - ulStackFrameSize;
663 /* Copy the stack frame. */
664 for( i = 0; i < ulStackFrameSize; i++ )
666 pulTaskStack[ i ] = pulSystemCallStack[ i ];
669 /* Use the pulTaskStack in thread mode. */
670 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
672 /* Drop the privilege before returning to the thread mode. */
674 " mrs r1, control \n" /* Obtain current control value. */
675 " orr r1, #1 \n" /* Set nPRIV bit. */
676 " msr control, r1 \n" /* Write back new control value. */
680 /* Restore the stacked link register to what it was at the time of
681 * system call entry. */
682 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
684 /* If the hardware used padding to force the stack pointer
685 * to be double word aligned, set the stacked xPSR bit[9],
686 * otherwise clear it. */
687 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
689 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
693 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
696 /* This is not NULL only for the duration of the system call. */
697 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
701 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
702 /*-----------------------------------------------------------*/
704 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
706 BaseType_t xTaskIsPrivileged = pdFALSE;
707 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
709 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
711 xTaskIsPrivileged = pdTRUE;
714 return xTaskIsPrivileged;
716 /*-----------------------------------------------------------*/
718 static void prvRestoreContextOfFirstTask( void )
722 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
725 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
727 /*------------ Program MPU. ------------ */
728 " ldr r3, pxCurrentTCBConst2 \n" /* r3 = pxCurrentTCBConst2. */
729 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
730 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
732 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
733 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
734 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
735 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
736 " str r3, [r0] \n" /* Disable MPU. */
738 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
739 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
740 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
742 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
743 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
744 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
745 " str r3, [r0] \n" /* Enable MPU. */
746 " dsb \n" /* Force memory writes before continuing. */
748 /*---------- Restore Context. ---------- */
749 " ldr r3, pxCurrentTCBConst2 \n" /* r3 = pxCurrentTCBConst2. */
750 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
751 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
753 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
755 " stmia r0, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
756 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
757 " msr control, r3 \n"
758 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
761 " msr basepri, r0 \n"
764 " .ltorg \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
766 "pxCurrentTCBConst2: .word pxCurrentTCB \n"
769 /*-----------------------------------------------------------*/
772 * See header file for description.
774 BaseType_t xPortStartScheduler( void )
776 #if ( configASSERT_DEFINED == 1 )
778 volatile uint8_t ucOriginalPriority;
779 volatile uint32_t ulImplementedPrioBits = 0;
780 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
781 volatile uint8_t ucMaxPriorityValue;
783 /* Determine the maximum priority from which ISR safe FreeRTOS API
784 * functions can be called. ISR safe functions are those that end in
785 * "FromISR". FreeRTOS maintains separate thread and ISR API functions
786 * to ensure interrupt entry is as fast and simple as possible.
788 * Save the interrupt priority value that is about to be clobbered. */
789 ucOriginalPriority = *pucFirstUserPriorityRegister;
791 /* Determine the number of priority bits available. First write to all
793 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
795 /* Read the value back to see how many bits stuck. */
796 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
798 /* Use the same mask on the maximum system call priority. */
799 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
801 /* Check that the maximum system call priority is nonzero after
802 * accounting for the number of priority bits supported by the
803 * hardware. A priority of 0 is invalid because setting the BASEPRI
804 * register to 0 unmasks all interrupts, and interrupts with priority 0
805 * cannot be masked using BASEPRI.
806 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
807 configASSERT( ucMaxSysCallPriority );
809 /* Check that the bits not implemented in hardware are zero in
810 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
811 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
813 /* Calculate the maximum acceptable priority group value for the number
814 * of bits read back. */
816 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
818 ulImplementedPrioBits++;
819 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
822 if( ulImplementedPrioBits == 8 )
824 /* When the hardware implements 8 priority bits, there is no way for
825 * the software to configure PRIGROUP to not have sub-priorities. As
826 * a result, the least significant bit is always used for sub-priority
827 * and there are 128 preemption priorities and 2 sub-priorities.
829 * This may cause some confusion in some cases - for example, if
830 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
831 * priority interrupts will be masked in Critical Sections as those
832 * are at the same preemption priority. This may appear confusing as
833 * 4 is higher (numerically lower) priority than
834 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
835 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
836 * to 4, this confusion does not happen and the behaviour remains the same.
838 * The following assert ensures that the sub-priority bit in the
839 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
841 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
842 ulMaxPRIGROUPValue = 0;
846 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
849 /* Shift the priority group value back to its position within the AIRCR
851 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
852 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
854 /* Restore the clobbered interrupt priority register to its original
856 *pucFirstUserPriorityRegister = ucOriginalPriority;
858 #endif /* configASSERT_DEFINED */
860 /* Make PendSV and SysTick the same priority as the kernel, and the SVC
861 * handler higher priority so it can be used to exit a critical section (where
862 * lower priorities are masked). */
863 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
864 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
866 /* Configure the regions in the MPU that are common to all tasks. */
869 /* Start the timer that generates the tick ISR. Interrupts are disabled
871 vPortSetupTimerInterrupt();
873 /* Initialise the critical nesting count ready for the first task. */
874 uxCriticalNesting = 0;
876 /* Start the first task. */
878 " ldr r0, =0xE000ED08 \n"/* Use the NVIC offset register to locate the stack. */
881 " msr msp, r0 \n"/* Set the msp back to the start of the stack. */
882 " cpsie i \n"/* Globally enable interrupts. */
886 " svc %0 \n"/* System call to start first task. */
889 ::"i" ( portSVC_START_SCHEDULER ) : "memory" );
891 /* Should not get here! */
894 /*-----------------------------------------------------------*/
896 void vPortEndScheduler( void )
898 /* Not implemented in ports where there is nothing to return to.
899 * Artificially force an assert. */
900 configASSERT( uxCriticalNesting == 1000UL );
902 /*-----------------------------------------------------------*/
904 void vPortEnterCritical( void )
906 #if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
907 if( portIS_PRIVILEGED() == pdFALSE )
909 portRAISE_PRIVILEGE();
910 portMEMORY_BARRIER();
912 portDISABLE_INTERRUPTS();
914 portMEMORY_BARRIER();
916 portRESET_PRIVILEGE();
917 portMEMORY_BARRIER();
921 portDISABLE_INTERRUPTS();
925 portDISABLE_INTERRUPTS();
929 /*-----------------------------------------------------------*/
931 void vPortExitCritical( void )
933 #if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
934 if( portIS_PRIVILEGED() == pdFALSE )
936 portRAISE_PRIVILEGE();
937 portMEMORY_BARRIER();
939 configASSERT( uxCriticalNesting );
942 if( uxCriticalNesting == 0 )
944 portENABLE_INTERRUPTS();
946 portMEMORY_BARRIER();
948 portRESET_PRIVILEGE();
949 portMEMORY_BARRIER();
953 configASSERT( uxCriticalNesting );
956 if( uxCriticalNesting == 0 )
958 portENABLE_INTERRUPTS();
962 configASSERT( uxCriticalNesting );
965 if( uxCriticalNesting == 0 )
967 portENABLE_INTERRUPTS();
971 /*-----------------------------------------------------------*/
973 void xPortPendSVHandler( void )
975 /* This is a naked function. */
979 " ldr r3, pxCurrentTCBConst \n" /* r3 = pxCurrentTCBConst. */
980 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
981 " ldr r1, [r2] \n" /* r1 = Location where the context should be saved. */
983 /*------------ Save Context. ----------- */
984 " mrs r3, control \n"
988 " stmia r1!, {r3-r11, lr} \n" /* Store CONTROL register, r4-r11 and LR. */
989 " ldmia r0, {r4-r11} \n" /* Copy hardware saved context into r4-r11. */
990 " stmia r1!, {r0, r4-r11} \n" /* Store original PSP (after hardware has saved context) and the hardware saved context. */
991 " str r1, [r2] \n" /* Save the location from where the context should be restored as the first member of TCB. */
993 /*---------- Select next task. --------- */
995 " msr basepri, r0 \n"
998 " bl vTaskSwitchContext \n"
1000 " msr basepri, r0 \n"
1002 /*------------ Program MPU. ------------ */
1003 " ldr r3, pxCurrentTCBConst \n" /* r3 = pxCurrentTCBConst. */
1004 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1005 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
1007 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
1008 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1009 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1010 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
1011 " str r3, [r0] \n" /* Disable MPU. */
1013 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
1014 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
1015 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
1017 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1018 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1019 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
1020 " str r3, [r0] \n" /* Enable MPU. */
1021 " dsb \n" /* Force memory writes before continuing. */
1023 /*---------- Restore Context. ---------- */
1024 " ldr r3, pxCurrentTCBConst \n" /* r3 = pxCurrentTCBConst. */
1025 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1026 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
1028 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
1030 " stmia r0, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
1031 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
1032 " msr control, r3 \n"
1034 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
1037 " .ltorg \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
1039 "pxCurrentTCBConst: .word pxCurrentTCB \n"
1040 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
1043 /*-----------------------------------------------------------*/
1045 void xPortSysTickHandler( void )
1049 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
1051 /* Increment the RTOS tick. */
1052 if( xTaskIncrementTick() != pdFALSE )
1054 /* Pend a context switch. */
1055 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
1058 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
1060 /*-----------------------------------------------------------*/
1063 * Setup the systick timer to generate the tick interrupts at the required
1066 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
1068 /* Stop and clear the SysTick. */
1069 portNVIC_SYSTICK_CTRL_REG = 0UL;
1070 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1072 /* Configure SysTick to interrupt at the requested rate. */
1073 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1074 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE );
1076 /*-----------------------------------------------------------*/
1078 static void prvSetupMPU( void )
1080 extern uint32_t __privileged_functions_start__[];
1081 extern uint32_t __privileged_functions_end__[];
1082 extern uint32_t __FLASH_segment_start__[];
1083 extern uint32_t __FLASH_segment_end__[];
1084 extern uint32_t __privileged_data_start__[];
1085 extern uint32_t __privileged_data_end__[];
1087 /* Check the expected MPU is present. */
1088 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1090 /* First setup the unprivileged flash for unprivileged read only access. */
1091 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1092 ( portMPU_REGION_VALID ) |
1093 ( portUNPRIVILEGED_FLASH_REGION );
1095 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1096 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1097 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1098 ( portMPU_REGION_ENABLE );
1100 /* Setup the privileged flash for privileged only access. This is where
1101 * the kernel code is * placed. */
1102 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1103 ( portMPU_REGION_VALID ) |
1104 ( portPRIVILEGED_FLASH_REGION );
1106 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1107 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1108 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1109 ( portMPU_REGION_ENABLE );
1111 /* Setup the privileged data RAM region. This is where the kernel data
1113 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1114 ( portMPU_REGION_VALID ) |
1115 ( portPRIVILEGED_RAM_REGION );
1117 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1118 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1119 ( portMPU_REGION_EXECUTE_NEVER ) |
1120 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1121 ( portMPU_REGION_ENABLE );
1123 /* By default allow everything to access the general peripherals. The
1124 * system peripherals and registers are protected. */
1125 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1126 ( portMPU_REGION_VALID ) |
1127 ( portGENERAL_PERIPHERALS_REGION );
1129 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1130 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1131 ( portMPU_REGION_ENABLE );
1133 /* Enable the memory fault exception. */
1134 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1136 /* Enable the MPU with the background region configured. */
1137 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1140 /*-----------------------------------------------------------*/
1142 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1144 uint32_t ulRegionSize, ulReturnValue = 4;
1146 /* 32 is the smallest region size, 31 is the largest valid value for
1148 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1150 if( ulActualSizeInBytes <= ulRegionSize )
1160 /* Shift the code by one before returning so it can be written directly
1161 * into the the correct bit position of the attribute register. */
1162 return( ulReturnValue << 1UL );
1164 /*-----------------------------------------------------------*/
1166 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
1170 " mrs r0, control \n"/* r0 = CONTROL. */
1171 " tst r0, #1 \n"/* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
1173 " movne r0, #0 \n"/* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
1174 " moveq r0, #1 \n"/* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
1175 " bx lr \n"/* Return. */
1181 /*-----------------------------------------------------------*/
1183 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
1187 " mrs r0, control \n"/* r0 = CONTROL. */
1188 " orr r0, #1 \n"/* r0 = r0 | 1. */
1189 " msr control, r0 \n"/* CONTROL = r0. */
1190 " bx lr \n"/* Return to the caller. */
1194 /*-----------------------------------------------------------*/
1196 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1197 const struct xMEMORY_REGION * const xRegions,
1198 StackType_t * pxBottomOfStack,
1199 uint32_t ulStackDepth )
1201 extern uint32_t __SRAM_segment_start__[];
1202 extern uint32_t __SRAM_segment_end__[];
1203 extern uint32_t __privileged_data_start__[];
1204 extern uint32_t __privileged_data_end__[];
1208 if( xRegions == NULL )
1210 /* No MPU regions are specified so allow access to all RAM. */
1211 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1212 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1213 ( portMPU_REGION_VALID ) |
1214 ( portSTACK_REGION ); /* Region number. */
1216 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1217 ( portMPU_REGION_READ_WRITE ) |
1218 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1219 ( portMPU_REGION_EXECUTE_NEVER ) |
1220 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1221 ( portMPU_REGION_ENABLE );
1223 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1224 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1225 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1226 tskMPU_WRITE_PERMISSION );
1228 /* Invalidate user configurable regions. */
1229 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1231 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1232 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1233 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1234 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1235 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1240 /* This function is called automatically when the task is created - in
1241 * which case the stack region parameters will be valid. At all other
1242 * times the stack parameters will not be valid and it is assumed that the
1243 * stack region has already been configured. */
1244 if( ulStackDepth > 0 )
1246 /* Define the region that allows access to the stack. */
1247 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1248 ( ( uint32_t ) pxBottomOfStack ) |
1249 ( portMPU_REGION_VALID ) |
1250 ( portSTACK_REGION ); /* Region number. */
1252 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1253 ( portMPU_REGION_READ_WRITE ) |
1254 ( portMPU_REGION_EXECUTE_NEVER ) |
1255 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1256 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1257 ( portMPU_REGION_ENABLE );
1258 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1259 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1260 ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1UL );
1261 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1262 tskMPU_WRITE_PERMISSION );
1267 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1269 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1271 /* Translate the generic region definition contained in
1272 * xRegions into the CM3 specific MPU settings that are then
1273 * stored in xMPUSettings. */
1274 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1275 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1276 ( portMPU_REGION_VALID ) |
1277 ( ul - 1UL ); /* Region number. */
1279 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1280 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1281 ( xRegions[ lIndex ].ulParameters ) |
1282 ( portMPU_REGION_ENABLE );
1284 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1285 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1286 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1287 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1288 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1290 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1292 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1294 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1299 /* Invalidate the region. */
1300 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1301 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1302 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1303 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1304 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1311 /*-----------------------------------------------------------*/
1313 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1314 uint32_t ulBufferLength,
1315 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1318 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1319 BaseType_t xAccessGranted = pdFALSE;
1320 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1322 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1324 xAccessGranted = pdTRUE;
1328 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1330 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1331 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1333 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1335 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1336 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1337 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1338 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1339 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1340 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1341 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1343 xAccessGranted = pdTRUE;
1350 return xAccessGranted;
1352 /*-----------------------------------------------------------*/
1354 #if ( configASSERT_DEFINED == 1 )
1356 void vPortValidateInterruptPriority( void )
1358 uint32_t ulCurrentInterrupt;
1359 uint8_t ucCurrentPriority;
1361 /* Obtain the number of the currently executing interrupt. */
1362 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1364 /* Is the interrupt number a user defined interrupt? */
1365 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1367 /* Look up the interrupt's priority. */
1368 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1370 /* The following assertion will fail if a service routine (ISR) for
1371 * an interrupt that has been assigned a priority above
1372 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1373 * function. ISR safe FreeRTOS API functions must *only* be called
1374 * from interrupts that have been assigned a priority at or below
1375 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1377 * Numerically low interrupt priority numbers represent logically high
1378 * interrupt priorities, therefore the priority of the interrupt must
1379 * be set to a value equal to or numerically *higher* than
1380 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1382 * Interrupts that use the FreeRTOS API must not be left at their
1383 * default priority of zero as that is the highest possible priority,
1384 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1385 * and therefore also guaranteed to be invalid.
1387 * FreeRTOS maintains separate thread and ISR API functions to ensure
1388 * interrupt entry is as fast and simple as possible.
1390 * The following links provide detailed information:
1391 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1392 * https://www.FreeRTOS.org/FAQHelp.html */
1393 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1396 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1397 * that define each interrupt's priority to be split between bits that
1398 * define the interrupt's pre-emption priority bits and bits that define
1399 * the interrupt's sub-priority. For simplicity all bits must be defined
1400 * to be pre-emption priority bits. The following assertion will fail if
1401 * this is not the case (if some bits represent a sub-priority).
1403 * If the application only uses CMSIS libraries for interrupt
1404 * configuration then the correct setting can be achieved on all Cortex-M
1405 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1406 * scheduler. Note however that some vendor specific peripheral libraries
1407 * assume a non-zero priority group setting, in which cases using a value
1408 * of zero will result in unpredicable behaviour. */
1409 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1412 #endif /* configASSERT_DEFINED */
1413 /*-----------------------------------------------------------*/