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;
291 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
292 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
293 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
294 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
295 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
296 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
297 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
298 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
299 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
301 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
302 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
303 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
304 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
305 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
306 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
307 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
308 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
309 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
311 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
313 /* Ensure that the system call stack is double word aligned. */
314 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
315 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
316 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
318 /* This is not NULL only for the duration of a system call. */
319 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
321 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
323 return &( xMPUSettings->ulContext[ 19 ] );
325 /*-----------------------------------------------------------*/
327 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
329 void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
334 ".extern vSVCHandler_C \n"
335 ".extern vSystemCallEnter \n"
336 ".extern vSystemCallEnter_1 \n"
337 ".extern vSystemCallExit \n"
344 "ldr r1, [r0, #24] \n"
345 "ldrb r2, [r1, #-2] \n"
347 "beq vSystemCallEnter \n"
349 "beq vSystemCallEnter_1 \n"
351 "beq vSystemCallExit \n"
355 : "i" ( portSVC_SYSTEM_CALL_ENTER ), "i" ( portSVC_SYSTEM_CALL_ENTER_1 ), "i" ( portSVC_SYSTEM_CALL_EXIT )
356 : "r0", "r1", "r2", "memory"
360 #else /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
362 void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
364 /* Assumes psp was in use. */
367 #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
376 ::"i" ( vSVCHandler_C ) : "r0", "memory"
380 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
381 /*-----------------------------------------------------------*/
383 void vSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
388 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
389 #if defined( __ARMCC_VERSION )
391 /* Declaration when these variable are defined in code instead of being
392 * exported from linker scripts. */
393 extern uint32_t * __syscalls_flash_start__;
394 extern uint32_t * __syscalls_flash_end__;
396 /* Declaration when these variable are exported from linker scripts. */
397 extern uint32_t __syscalls_flash_start__[];
398 extern uint32_t __syscalls_flash_end__[];
399 #endif /* #if defined( __ARMCC_VERSION ) */
400 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
402 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
403 * argument (r0) is pulParam[ 0 ]. */
404 ulPC = pulParam[ portOFFSET_TO_PC ];
405 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
407 switch( ucSVCNumber )
409 case portSVC_START_SCHEDULER:
410 portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
411 prvRestoreContextOfFirstTask();
415 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
417 /* Barriers are normally not required
418 * but do ensure the code is completely
419 * within the specified behaviour for the
421 __asm volatile ( "dsb" ::: "memory" );
422 __asm volatile ( "isb" );
426 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
427 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
428 * svc was raised from any of the
431 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
432 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
436 " mrs r1, control \n" /* Obtain current control value. */
437 " bic r1, #1 \n" /* Set privilege bit. */
438 " msr control, r1 \n" /* Write back new control value. */
444 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
445 case portSVC_RAISE_PRIVILEGE:
448 " mrs r1, control \n" /* Obtain current control value. */
449 " bic r1, #1 \n" /* Set privilege bit. */
450 " msr control, r1 \n" /* Write back new control value. */
454 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
456 default: /* Unknown SVC call. */
460 /*-----------------------------------------------------------*/
462 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
464 void vSystemCallEnter( uint32_t * pulTaskStack ) /* PRIVILEGED_FUNCTION */
466 extern TaskHandle_t pxCurrentTCB;
467 xMPU_SETTINGS * pxMpuSettings;
468 uint32_t * pulSystemCallStack;
469 uint32_t ulSystemCallLocation, i;
470 const uint32_t ulStackFrameSize = 8;
472 #if defined( __ARMCC_VERSION )
474 /* Declaration when these variable are defined in code instead of being
475 * exported from linker scripts. */
476 extern uint32_t * __syscalls_flash_start__;
477 extern uint32_t * __syscalls_flash_end__;
479 /* Declaration when these variable are exported from linker scripts. */
480 extern uint32_t __syscalls_flash_start__[];
481 extern uint32_t __syscalls_flash_end__[];
482 #endif /* #if defined( __ARMCC_VERSION ) */
484 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
486 /* If the request did not come from the system call section, do nothing. */
487 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
488 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
490 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
491 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
493 /* This is not NULL only for the duration of the system call. */
494 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
496 /* Make space on the system call stack for the stack frame. */
497 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
499 /* Copy the stack frame. */
500 for( i = 0; i < ulStackFrameSize; i++ )
502 pulSystemCallStack[ i ] = pulTaskStack[ i ];
505 /* Use the pulSystemCallStack in thread mode. */
506 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
508 /* Raise the privilege for the duration of the system call. */
510 " mrs r1, control \n" /* Obtain current control value. */
511 " bic r1, #1 \n" /* Clear nPRIV bit. */
512 " msr control, r1 \n" /* Write back new control value. */
516 /* Remember the location where we should copy the stack frame when we exit from
517 * the system call. */
518 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
520 /* Store the value of the Link Register before the SVC was raised. We need to
521 * restore it when we exit from the system call. */
522 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
524 /* Record if the hardware used padding to force the stack pointer
525 * to be double word aligned. */
526 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
528 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
532 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
535 /* We ensure in pxPortInitialiseStack that the system call stack is
536 * double word aligned and therefore, there is no need of padding.
537 * Clear the bit[9] of stacked xPSR. */
538 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
542 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
543 /*-----------------------------------------------------------*/
545 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
547 void vSystemCallEnter_1( uint32_t * pulTaskStack ) /* PRIVILEGED_FUNCTION */
549 extern TaskHandle_t pxCurrentTCB;
550 xMPU_SETTINGS * pxMpuSettings;
551 uint32_t * pulSystemCallStack;
552 uint32_t ulSystemCallLocation, i;
553 const uint32_t ulStackFrameSize = 8;
555 #if defined( __ARMCC_VERSION )
557 /* Declaration when these variable are defined in code instead of being
558 * exported from linker scripts. */
559 extern uint32_t * __syscalls_flash_start__;
560 extern uint32_t * __syscalls_flash_end__;
562 /* Declaration when these variable are exported from linker scripts. */
563 extern uint32_t __syscalls_flash_start__[];
564 extern uint32_t __syscalls_flash_end__[];
565 #endif /* #if defined( __ARMCC_VERSION ) */
567 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
569 /* If the request did not come from the system call section, do nothing. */
570 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
571 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
573 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
574 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
576 /* This is not NULL only for the duration of the system call. */
577 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
579 /* Make space on the system call stack for the stack frame and
580 * the parameter passed on the stack. We only need to copy one
581 * parameter but we still reserve 2 spaces to keep the stack
582 * double word aligned. */
583 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize - 2UL;
585 /* Copy the stack frame. */
586 for( i = 0; i < ulStackFrameSize; i++ )
588 pulSystemCallStack[ i ] = pulTaskStack[ i ];
591 /* Copy the parameter which is passed the stack. */
592 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
594 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize + 1 ];
596 /* Record if the hardware used padding to force the stack pointer
597 * to be double word aligned. */
598 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
602 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize ];
604 /* Record if the hardware used padding to force the stack pointer
605 * to be double word aligned. */
606 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
609 /* Use the pulSystemCallStack in thread mode. */
610 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
612 /* Raise the privilege for the duration of the system call. */
614 " mrs r1, control \n" /* Obtain current control value. */
615 " bic r1, #1 \n" /* Clear nPRIV bit. */
616 " msr control, r1 \n" /* Write back new control value. */
620 /* Remember the location where we should copy the stack frame when we exit from
621 * the system call. */
622 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
624 /* Store the value of the Link Register before the SVC was raised. We need to
625 * restore it when we exit from the system call. */
626 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
628 /* We ensure in pxPortInitialiseStack that the system call stack is
629 * double word aligned and therefore, there is no need of padding.
630 * Clear the bit[9] of stacked xPSR. */
631 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
635 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
636 /*-----------------------------------------------------------*/
638 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
640 void vSystemCallExit( uint32_t * pulSystemCallStack ) /* PRIVILEGED_FUNCTION */
642 extern TaskHandle_t pxCurrentTCB;
643 xMPU_SETTINGS * pxMpuSettings;
644 uint32_t * pulTaskStack;
645 uint32_t ulSystemCallLocation, i;
646 const uint32_t ulStackFrameSize = 8;
648 #if defined( __ARMCC_VERSION )
650 /* Declaration when these variable are defined in code instead of being
651 * exported from linker scripts. */
652 extern uint32_t * __syscalls_flash_start__;
653 extern uint32_t * __syscalls_flash_end__;
655 /* Declaration when these variable are exported from linker scripts. */
656 extern uint32_t __syscalls_flash_start__[];
657 extern uint32_t __syscalls_flash_end__[];
658 #endif /* #if defined( __ARMCC_VERSION ) */
660 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
662 /* If the request did not come from the system call section, do nothing. */
663 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
664 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
666 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
667 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
669 /* Make space on the task stack for the stack frame. */
670 pulTaskStack = pulTaskStack - ulStackFrameSize;
672 /* Copy the stack frame. */
673 for( i = 0; i < ulStackFrameSize; i++ )
675 pulTaskStack[ i ] = pulSystemCallStack[ i ];
678 /* Use the pulTaskStack in thread mode. */
679 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
681 /* Drop the privilege before returning to the thread mode. */
683 " mrs r1, control \n" /* Obtain current control value. */
684 " orr r1, #1 \n" /* Set nPRIV bit. */
685 " msr control, r1 \n" /* Write back new control value. */
689 /* Restore the stacked link register to what it was at the time of
690 * system call entry. */
691 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
693 /* If the hardware used padding to force the stack pointer
694 * to be double word aligned, set the stacked xPSR bit[9],
695 * otherwise clear it. */
696 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
698 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
702 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
705 /* This is not NULL only for the duration of the system call. */
706 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
710 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
711 /*-----------------------------------------------------------*/
713 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
715 BaseType_t xTaskIsPrivileged = pdFALSE;
716 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
718 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
720 xTaskIsPrivileged = pdTRUE;
723 return xTaskIsPrivileged;
725 /*-----------------------------------------------------------*/
727 static void prvRestoreContextOfFirstTask( void )
731 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
734 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
736 /*------------ Program MPU. ------------ */
737 " ldr r3, pxCurrentTCBConst2 \n" /* r3 = pxCurrentTCBConst2. */
738 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
739 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
741 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
742 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
743 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
744 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
745 " str r3, [r0] \n" /* Disable MPU. */
747 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
748 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
749 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
751 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
752 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
753 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
754 " str r3, [r0] \n" /* Enable MPU. */
755 " dsb \n" /* Force memory writes before continuing. */
757 /*---------- Restore Context. ---------- */
758 " ldr r3, pxCurrentTCBConst2 \n" /* r3 = pxCurrentTCBConst2. */
759 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
760 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
762 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
764 " stmia r0, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
765 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
766 " msr control, r3 \n"
767 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
770 " msr basepri, r0 \n"
773 " .ltorg \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
775 "pxCurrentTCBConst2: .word pxCurrentTCB \n"
778 /*-----------------------------------------------------------*/
781 * See header file for description.
783 BaseType_t xPortStartScheduler( void )
785 #if ( configASSERT_DEFINED == 1 )
787 volatile uint8_t ucOriginalPriority;
788 volatile uint32_t ulImplementedPrioBits = 0;
789 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
790 volatile uint8_t ucMaxPriorityValue;
792 /* Determine the maximum priority from which ISR safe FreeRTOS API
793 * functions can be called. ISR safe functions are those that end in
794 * "FromISR". FreeRTOS maintains separate thread and ISR API functions
795 * to ensure interrupt entry is as fast and simple as possible.
797 * Save the interrupt priority value that is about to be clobbered. */
798 ucOriginalPriority = *pucFirstUserPriorityRegister;
800 /* Determine the number of priority bits available. First write to all
802 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
804 /* Read the value back to see how many bits stuck. */
805 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
807 /* Use the same mask on the maximum system call priority. */
808 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
810 /* Check that the maximum system call priority is nonzero after
811 * accounting for the number of priority bits supported by the
812 * hardware. A priority of 0 is invalid because setting the BASEPRI
813 * register to 0 unmasks all interrupts, and interrupts with priority 0
814 * cannot be masked using BASEPRI.
815 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
816 configASSERT( ucMaxSysCallPriority );
818 /* Check that the bits not implemented in hardware are zero in
819 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
820 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
822 /* Calculate the maximum acceptable priority group value for the number
823 * of bits read back. */
825 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
827 ulImplementedPrioBits++;
828 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
831 if( ulImplementedPrioBits == 8 )
833 /* When the hardware implements 8 priority bits, there is no way for
834 * the software to configure PRIGROUP to not have sub-priorities. As
835 * a result, the least significant bit is always used for sub-priority
836 * and there are 128 preemption priorities and 2 sub-priorities.
838 * This may cause some confusion in some cases - for example, if
839 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
840 * priority interrupts will be masked in Critical Sections as those
841 * are at the same preemption priority. This may appear confusing as
842 * 4 is higher (numerically lower) priority than
843 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
844 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
845 * to 4, this confusion does not happen and the behaviour remains the same.
847 * The following assert ensures that the sub-priority bit in the
848 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
850 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
851 ulMaxPRIGROUPValue = 0;
855 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
858 /* Shift the priority group value back to its position within the AIRCR
860 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
861 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
863 /* Restore the clobbered interrupt priority register to its original
865 *pucFirstUserPriorityRegister = ucOriginalPriority;
867 #endif /* configASSERT_DEFINED */
869 /* Make PendSV and SysTick the same priority as the kernel, and the SVC
870 * handler higher priority so it can be used to exit a critical section (where
871 * lower priorities are masked). */
872 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
873 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
875 /* Configure the regions in the MPU that are common to all tasks. */
878 /* Start the timer that generates the tick ISR. Interrupts are disabled
880 vPortSetupTimerInterrupt();
882 /* Initialise the critical nesting count ready for the first task. */
883 uxCriticalNesting = 0;
885 /* Start the first task. */
887 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
890 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
891 " cpsie i \n" /* Globally enable interrupts. */
895 " svc %0 \n" /* System call to start first task. */
898 ::"i" ( portSVC_START_SCHEDULER ) : "memory" );
900 /* Should not get here! */
903 /*-----------------------------------------------------------*/
905 void vPortEndScheduler( void )
907 /* Not implemented in ports where there is nothing to return to.
908 * Artificially force an assert. */
909 configASSERT( uxCriticalNesting == 1000UL );
911 /*-----------------------------------------------------------*/
913 void vPortEnterCritical( void )
915 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
916 if( portIS_PRIVILEGED() == pdFALSE )
918 portRAISE_PRIVILEGE();
919 portMEMORY_BARRIER();
921 portDISABLE_INTERRUPTS();
923 portMEMORY_BARRIER();
925 portRESET_PRIVILEGE();
926 portMEMORY_BARRIER();
930 portDISABLE_INTERRUPTS();
933 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
934 portDISABLE_INTERRUPTS();
936 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
938 /*-----------------------------------------------------------*/
940 void vPortExitCritical( void )
942 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
943 if( portIS_PRIVILEGED() == pdFALSE )
945 portRAISE_PRIVILEGE();
946 portMEMORY_BARRIER();
948 configASSERT( uxCriticalNesting );
951 if( uxCriticalNesting == 0 )
953 portENABLE_INTERRUPTS();
956 portMEMORY_BARRIER();
958 portRESET_PRIVILEGE();
959 portMEMORY_BARRIER();
963 configASSERT( uxCriticalNesting );
966 if( uxCriticalNesting == 0 )
968 portENABLE_INTERRUPTS();
971 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
972 configASSERT( uxCriticalNesting );
975 if( uxCriticalNesting == 0 )
977 portENABLE_INTERRUPTS();
979 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
981 /*-----------------------------------------------------------*/
983 void xPortPendSVHandler( void )
985 /* This is a naked function. */
989 " ldr r3, pxCurrentTCBConst \n" /* r3 = pxCurrentTCBConst. */
990 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
991 " ldr r1, [r2] \n" /* r1 = Location where the context should be saved. */
993 /*------------ Save Context. ----------- */
994 " mrs r3, control \n"
998 " stmia r1!, {r3-r11, lr} \n" /* Store CONTROL register, r4-r11 and LR. */
999 " ldmia r0, {r4-r11} \n" /* Copy hardware saved context into r4-r11. */
1000 " stmia r1!, {r0, r4-r11} \n" /* Store original PSP (after hardware has saved context) and the hardware saved context. */
1001 " str r1, [r2] \n" /* Save the location from where the context should be restored as the first member of TCB. */
1003 /*---------- Select next task. --------- */
1005 " msr basepri, r0 \n"
1008 " bl vTaskSwitchContext \n"
1010 " msr basepri, r0 \n"
1012 /*------------ Program MPU. ------------ */
1013 " ldr r3, pxCurrentTCBConst \n" /* r3 = pxCurrentTCBConst. */
1014 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1015 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
1017 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
1018 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1019 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1020 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
1021 " str r3, [r0] \n" /* Disable MPU. */
1023 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
1024 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
1025 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
1027 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1028 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1029 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
1030 " str r3, [r0] \n" /* Enable MPU. */
1031 " dsb \n" /* Force memory writes before continuing. */
1033 /*---------- Restore Context. ---------- */
1034 " ldr r3, pxCurrentTCBConst \n" /* r3 = pxCurrentTCBConst. */
1035 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1036 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
1038 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
1040 " stmia r0, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
1041 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
1042 " msr control, r3 \n"
1044 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
1047 " .ltorg \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
1049 "pxCurrentTCBConst: .word pxCurrentTCB \n"
1050 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
1053 /*-----------------------------------------------------------*/
1055 void xPortSysTickHandler( void )
1059 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
1061 /* Increment the RTOS tick. */
1062 if( xTaskIncrementTick() != pdFALSE )
1064 /* Pend a context switch. */
1065 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
1068 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
1070 /*-----------------------------------------------------------*/
1073 * Setup the systick timer to generate the tick interrupts at the required
1076 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
1078 /* Stop and clear the SysTick. */
1079 portNVIC_SYSTICK_CTRL_REG = 0UL;
1080 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1082 /* Configure SysTick to interrupt at the requested rate. */
1083 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1084 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE );
1086 /*-----------------------------------------------------------*/
1088 static void prvSetupMPU( void )
1090 extern uint32_t __privileged_functions_start__[];
1091 extern uint32_t __privileged_functions_end__[];
1092 extern uint32_t __FLASH_segment_start__[];
1093 extern uint32_t __FLASH_segment_end__[];
1094 extern uint32_t __privileged_data_start__[];
1095 extern uint32_t __privileged_data_end__[];
1097 /* Check the expected MPU is present. */
1098 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1100 /* First setup the unprivileged flash for unprivileged read only access. */
1101 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1102 ( portMPU_REGION_VALID ) |
1103 ( portUNPRIVILEGED_FLASH_REGION );
1105 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1106 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1107 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1108 ( portMPU_REGION_ENABLE );
1110 /* Setup the privileged flash for privileged only access. This is where
1111 * the kernel code is * placed. */
1112 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1113 ( portMPU_REGION_VALID ) |
1114 ( portPRIVILEGED_FLASH_REGION );
1116 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1117 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1118 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1119 ( portMPU_REGION_ENABLE );
1121 /* Setup the privileged data RAM region. This is where the kernel data
1123 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1124 ( portMPU_REGION_VALID ) |
1125 ( portPRIVILEGED_RAM_REGION );
1127 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1128 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1129 ( portMPU_REGION_EXECUTE_NEVER ) |
1130 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1131 ( portMPU_REGION_ENABLE );
1133 /* By default allow everything to access the general peripherals. The
1134 * system peripherals and registers are protected. */
1135 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1136 ( portMPU_REGION_VALID ) |
1137 ( portGENERAL_PERIPHERALS_REGION );
1139 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1140 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1141 ( portMPU_REGION_ENABLE );
1143 /* Enable the memory fault exception. */
1144 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1146 /* Enable the MPU with the background region configured. */
1147 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1150 /*-----------------------------------------------------------*/
1152 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1154 uint32_t ulRegionSize, ulReturnValue = 4;
1156 /* 32 is the smallest region size, 31 is the largest valid value for
1158 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1160 if( ulActualSizeInBytes <= ulRegionSize )
1170 /* Shift the code by one before returning so it can be written directly
1171 * into the the correct bit position of the attribute register. */
1172 return( ulReturnValue << 1UL );
1174 /*-----------------------------------------------------------*/
1176 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
1180 " mrs r0, control \n" /* r0 = CONTROL. */
1181 " tst r0, #1 \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
1183 " movne r0, #0 \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
1184 " moveq r0, #1 \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
1185 " bx lr \n" /* Return. */
1191 /*-----------------------------------------------------------*/
1193 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
1197 " mrs r0, control \n" /* r0 = CONTROL. */
1198 " orr r0, #1 \n" /* r0 = r0 | 1. */
1199 " msr control, r0 \n" /* CONTROL = r0. */
1200 " bx lr \n" /* Return to the caller. */
1204 /*-----------------------------------------------------------*/
1206 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1207 const struct xMEMORY_REGION * const xRegions,
1208 StackType_t * pxBottomOfStack,
1209 uint32_t ulStackDepth )
1211 extern uint32_t __SRAM_segment_start__[];
1212 extern uint32_t __SRAM_segment_end__[];
1213 extern uint32_t __privileged_data_start__[];
1214 extern uint32_t __privileged_data_end__[];
1218 if( xRegions == NULL )
1220 /* No MPU regions are specified so allow access to all RAM. */
1221 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1222 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1223 ( portMPU_REGION_VALID ) |
1224 ( portSTACK_REGION ); /* Region number. */
1226 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1227 ( portMPU_REGION_READ_WRITE ) |
1228 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1229 ( portMPU_REGION_EXECUTE_NEVER ) |
1230 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1231 ( portMPU_REGION_ENABLE );
1233 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1234 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1235 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1236 tskMPU_WRITE_PERMISSION );
1238 /* Invalidate user configurable regions. */
1239 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1241 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1242 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1243 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1244 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1245 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1250 /* This function is called automatically when the task is created - in
1251 * which case the stack region parameters will be valid. At all other
1252 * times the stack parameters will not be valid and it is assumed that the
1253 * stack region has already been configured. */
1254 if( ulStackDepth > 0 )
1256 /* Define the region that allows access to the stack. */
1257 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1258 ( ( uint32_t ) pxBottomOfStack ) |
1259 ( portMPU_REGION_VALID ) |
1260 ( portSTACK_REGION ); /* Region number. */
1262 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1263 ( portMPU_REGION_READ_WRITE ) |
1264 ( portMPU_REGION_EXECUTE_NEVER ) |
1265 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1266 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1267 ( portMPU_REGION_ENABLE );
1268 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1269 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1270 ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1UL );
1271 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1272 tskMPU_WRITE_PERMISSION );
1277 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1279 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1281 /* Translate the generic region definition contained in
1282 * xRegions into the CM3 specific MPU settings that are then
1283 * stored in xMPUSettings. */
1284 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1285 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1286 ( portMPU_REGION_VALID ) |
1287 ( ul - 1UL ); /* Region number. */
1289 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1290 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1291 ( xRegions[ lIndex ].ulParameters ) |
1292 ( portMPU_REGION_ENABLE );
1294 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1295 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1296 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1298 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1299 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1301 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1304 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1306 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1311 /* Invalidate the region. */
1312 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1313 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1314 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1315 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1316 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1323 /*-----------------------------------------------------------*/
1325 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1326 uint32_t ulBufferLength,
1327 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1330 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1331 BaseType_t xAccessGranted = pdFALSE;
1332 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1334 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1336 xAccessGranted = pdTRUE;
1340 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1342 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1343 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1345 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1347 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1348 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1349 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1350 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1351 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1352 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1353 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1355 xAccessGranted = pdTRUE;
1362 return xAccessGranted;
1364 /*-----------------------------------------------------------*/
1366 #if ( configASSERT_DEFINED == 1 )
1368 void vPortValidateInterruptPriority( void )
1370 uint32_t ulCurrentInterrupt;
1371 uint8_t ucCurrentPriority;
1373 /* Obtain the number of the currently executing interrupt. */
1374 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1376 /* Is the interrupt number a user defined interrupt? */
1377 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1379 /* Look up the interrupt's priority. */
1380 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1382 /* The following assertion will fail if a service routine (ISR) for
1383 * an interrupt that has been assigned a priority above
1384 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1385 * function. ISR safe FreeRTOS API functions must *only* be called
1386 * from interrupts that have been assigned a priority at or below
1387 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1389 * Numerically low interrupt priority numbers represent logically high
1390 * interrupt priorities, therefore the priority of the interrupt must
1391 * be set to a value equal to or numerically *higher* than
1392 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1394 * Interrupts that use the FreeRTOS API must not be left at their
1395 * default priority of zero as that is the highest possible priority,
1396 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1397 * and therefore also guaranteed to be invalid.
1399 * FreeRTOS maintains separate thread and ISR API functions to ensure
1400 * interrupt entry is as fast and simple as possible.
1402 * The following links provide detailed information:
1403 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1404 * https://www.FreeRTOS.org/FAQHelp.html */
1405 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1408 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1409 * that define each interrupt's priority to be split between bits that
1410 * define the interrupt's pre-emption priority bits and bits that define
1411 * the interrupt's sub-priority. For simplicity all bits must be defined
1412 * to be pre-emption priority bits. The following assertion will fail if
1413 * this is not the case (if some bits represent a sub-priority).
1415 * If the application only uses CMSIS libraries for interrupt
1416 * configuration then the correct setting can be achieved on all Cortex-M
1417 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1418 * scheduler. Note however that some vendor specific peripheral libraries
1419 * assume a non-zero priority group setting, in which cases using a value
1420 * of zero will result in unpredictable behaviour. */
1421 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1424 #endif /* configASSERT_DEFINED */
1425 /*-----------------------------------------------------------*/