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 CM4 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. */
41 #include "mpu_syscall_numbers.h"
44 #error This port can only be used when the project options are configured to enable hardware floating point support.
47 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
49 #ifndef configSYSTICK_CLOCK_HZ
50 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
51 /* Ensure the SysTick is clocked at the same frequency as the core. */
52 #define portNVIC_SYSTICK_CLK ( 1UL << 2UL )
55 /* The way the SysTick is clocked is not modified in case it is not the same
57 #define portNVIC_SYSTICK_CLK ( 0 )
60 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
61 #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
62 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
65 /* Prototype of all Interrupt Service Routines (ISRs). */
66 typedef void ( * portISR_t )( void );
68 /* Constants required to access and manipulate the NVIC. */
69 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
70 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
71 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
72 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
73 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
74 #define portNVIC_SYS_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
75 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
77 /* Constants used to detect Cortex-M7 r0p0 and r0p1 cores, and ensure
78 * that a work around is active for errata 837070. */
79 #define portCPUID ( *( ( volatile uint32_t * ) 0xE000ed00 ) )
80 #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
81 #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
83 /* Constants required to access and manipulate the MPU. */
84 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
85 #define portMPU_REGION_BASE_ADDRESS_REG ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
86 #define portMPU_REGION_ATTRIBUTE_REG ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
87 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
88 #define portEXPECTED_MPU_TYPE_VALUE ( configTOTAL_MPU_REGIONS << 8UL )
89 #define portMPU_ENABLE ( 0x01UL )
90 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
91 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
92 #define portMPU_REGION_VALID ( 0x10UL )
93 #define portMPU_REGION_ENABLE ( 0x01UL )
94 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
95 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
97 /* Constants required to access and manipulate the SysTick. */
98 #define portNVIC_SYSTICK_INT ( 0x00000002UL )
99 #define portNVIC_SYSTICK_ENABLE ( 0x00000001UL )
100 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
101 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
102 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
104 /* Constants required to manipulate the VFP. */
105 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34UL ) /* Floating point context control register. */
106 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
108 /* Constants required to set up the initial stack. */
109 #define portINITIAL_XPSR ( 0x01000000UL )
110 #define portINITIAL_EXC_RETURN ( 0xfffffffdUL )
111 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
112 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
114 /* Constants used to check the installation of the FreeRTOS interrupt handlers. */
115 #define portSCB_VTOR_REG ( *( ( portISR_t ** ) 0xE000ED08 ) )
116 #define portVECTOR_INDEX_SVC ( 11 )
117 #define portVECTOR_INDEX_PENDSV ( 14 )
119 /* Constants required to check the validity of an interrupt priority. */
120 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
121 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
122 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
123 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
124 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
125 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
126 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
127 #define portPRIGROUP_SHIFT ( 8UL )
129 /* Constants used during system call enter and exit. */
130 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
131 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
133 /* Offsets in the stack to the parameters when inside the SVC handler. */
134 #define portOFFSET_TO_LR ( 5 )
135 #define portOFFSET_TO_PC ( 6 )
136 #define portOFFSET_TO_PSR ( 7 )
139 /* For strict compliance with the Cortex-M spec the task start address should
140 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
141 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
143 /* Does addr lie within [start, end] address range? */
144 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
145 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
147 /* Is the access request satisfied by the available permissions? */
148 #define portIS_AUTHORIZED( accessRequest, permissions ) \
149 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
151 /* Max value that fits in a uint32_t type. */
152 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
154 /* Check if adding a and b will result in overflow. */
155 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
156 /*-----------------------------------------------------------*/
159 * Configure a number of standard MPU regions that are used by all tasks.
161 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
164 * Return the smallest MPU region size that a given number of bytes will fit
165 * into. The region size is returned as the value that should be programmed
166 * into the region attribute register for that region.
168 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
171 * Setup the timer to generate the tick interrupts. The implementation in this
172 * file is weak to allow application writers to change the timer used to
173 * generate the tick interrupt.
175 void vPortSetupTimerInterrupt( void );
178 * Standard FreeRTOS exception handlers.
180 void xPortPendSVHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
181 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
182 void vPortSVCHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
185 * Starts the scheduler by restoring the context of the first task to run.
187 static void prvRestoreContextOfFirstTask( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
190 * C portion of the SVC handler. The SVC handler is split between an asm entry
191 * and a C wrapper for simplicity of coding and maintenance.
193 void vSVCHandler_C( uint32_t * pulRegisters ) __attribute__( ( noinline ) ) PRIVILEGED_FUNCTION;
196 * Function to enable the VFP.
198 static void vPortEnableVFP( void ) __attribute__( ( naked ) );
201 * @brief Checks whether or not the processor is privileged.
203 * @return 1 if the processor is already privileged, 0 otherwise.
205 BaseType_t xIsPrivileged( void ) __attribute__( ( naked ) );
208 * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
211 * Bit 0 of the CONTROL register defines the privilege level of Thread Mode.
212 * Bit[0] = 0 --> The processor is running privileged
213 * Bit[0] = 1 --> The processor is running unprivileged.
215 void vResetPrivilege( void ) __attribute__( ( naked ) );
218 * @brief Make a task unprivileged.
220 void vPortSwitchToUserMode( void );
223 * @brief Enter critical section.
225 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
226 void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
228 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
232 * @brief Exit from critical section.
234 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
235 void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
237 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
240 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
243 * @brief Sets up the system call stack so that upon returning from
244 * SVC, the system call stack is used.
246 * @param pulTaskStack The current SP when the SVC was raised.
247 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
248 * @param ucSystemCallNumber The system call number of the system call.
250 void vSystemCallEnter( uint32_t * pulTaskStack,
252 uint8_t ucSystemCallNumber ) PRIVILEGED_FUNCTION;
254 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
256 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
259 * @brief Raise SVC for exiting from a system call.
261 void vRequestSystemCallExit( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
263 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
265 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
268 * @brief Sets up the task stack so that upon returning from
269 * SVC, the task stack is used again.
271 * @param pulSystemCallStack The current SP when the SVC was raised.
272 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
274 void vSystemCallExit( uint32_t * pulSystemCallStack,
275 uint32_t ulLR ) PRIVILEGED_FUNCTION;
277 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
280 * @brief Checks whether or not the calling task is privileged.
282 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
284 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
285 /*-----------------------------------------------------------*/
287 /* Each task maintains its own interrupt status in the critical nesting
288 * variable. Note this is not saved as part of the task context as context
289 * switches can only occur when uxCriticalNesting is zero. */
290 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
292 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
295 * This variable is set to pdTRUE when the scheduler is started.
297 PRIVILEGED_DATA static BaseType_t xSchedulerRunning = pdFALSE;
299 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
302 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
303 * FreeRTOS API functions are not called from interrupts that have been assigned
304 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
306 #if ( configASSERT_DEFINED == 1 )
307 static uint8_t ucMaxSysCallPriority = 0;
308 static uint32_t ulMaxPRIGROUPValue = 0;
309 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
310 #endif /* configASSERT_DEFINED */
312 /*-----------------------------------------------------------*/
315 * See header file for description.
317 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
318 TaskFunction_t pxCode,
320 BaseType_t xRunPrivileged,
321 xMPU_SETTINGS * xMPUSettings )
323 if( xRunPrivileged == pdTRUE )
325 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
326 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
330 xMPUSettings->ulTaskFlags &= ( ~( portTASK_IS_PRIVILEGED_FLAG ) );
331 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
334 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
335 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
336 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
337 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
338 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
339 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
340 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
341 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
342 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
344 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
345 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
346 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
347 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
348 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
349 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
350 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
351 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
352 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
354 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
356 /* Ensure that the system call stack is double word aligned. */
357 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
358 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
359 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
361 /* This is not NULL only for the duration of a system call. */
362 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
364 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
366 return &( xMPUSettings->ulContext[ 19 ] );
368 /*-----------------------------------------------------------*/
370 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
372 void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
377 ".extern vSVCHandler_C \n"
378 ".extern vSystemCallEnter \n"
379 ".extern vSystemCallExit \n"
386 "ldr r1, [r0, #24] \n"
387 "ldrb r2, [r1, #-2] \n"
389 "blt syscall_enter \n"
391 "beq syscall_exit \n"
396 " b vSystemCallEnter \n"
400 " b vSystemCallExit \n"
403 : "i" ( NUM_SYSTEM_CALLS ), "i" ( portSVC_SYSTEM_CALL_EXIT )
404 : "r0", "r1", "r2", "memory"
408 #else /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
410 void vPortSVCHandler( void )
412 /* Assumes psp was in use. */
415 #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
424 ::"i" ( vSVCHandler_C ) : "r0", "memory"
428 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
429 /*-----------------------------------------------------------*/
431 void vSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
436 #if ( ( configUSE_MPU_WRAPPERS_V1 == 1 ) && ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) )
437 #if defined( __ARMCC_VERSION )
439 /* Declaration when these variable are defined in code instead of being
440 * exported from linker scripts. */
441 extern uint32_t * __syscalls_flash_start__;
442 extern uint32_t * __syscalls_flash_end__;
444 /* Declaration when these variable are exported from linker scripts. */
445 extern uint32_t __syscalls_flash_start__[];
446 extern uint32_t __syscalls_flash_end__[];
447 #endif /* #if defined( __ARMCC_VERSION ) */
448 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 1 ) && ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) ) */
450 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
451 * argument (r0) is pulParam[ 0 ]. */
452 ulPC = pulParam[ portOFFSET_TO_PC ];
453 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
455 switch( ucSVCNumber )
457 case portSVC_START_SCHEDULER:
458 prvRestoreContextOfFirstTask();
462 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
464 /* Barriers are normally not required
465 * but do ensure the code is completely
466 * within the specified behaviour for the
468 __asm volatile ( "dsb" ::: "memory" );
469 __asm volatile ( "isb" );
473 #if ( configUSE_MPU_WRAPPERS_V1 == 1 )
474 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
475 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
476 * svc was raised from any of the
479 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
480 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
484 " mrs r1, control \n" /* Obtain current control value. */
485 " bic r1, #1 \n" /* Set privilege bit. */
486 " msr control, r1 \n" /* Write back new control value. */
492 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
493 case portSVC_RAISE_PRIVILEGE:
496 " mrs r1, control \n" /* Obtain current control value. */
497 " bic r1, #1 \n" /* Set privilege bit. */
498 " msr control, r1 \n" /* Write back new control value. */
502 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
503 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
505 default: /* Unknown SVC call. */
509 /*-----------------------------------------------------------*/
511 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
513 void vSystemCallEnter( uint32_t * pulTaskStack,
515 uint8_t ucSystemCallNumber ) /* PRIVILEGED_FUNCTION */
517 extern TaskHandle_t pxCurrentTCB;
518 extern UBaseType_t uxSystemCallImplementations[ NUM_SYSTEM_CALLS ];
519 xMPU_SETTINGS * pxMpuSettings;
520 uint32_t * pulSystemCallStack;
521 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
523 #if defined( __ARMCC_VERSION )
524 /* Declaration when these variable are defined in code instead of being
525 * exported from linker scripts. */
526 extern uint32_t * __syscalls_flash_start__;
527 extern uint32_t * __syscalls_flash_end__;
529 /* Declaration when these variable are exported from linker scripts. */
530 extern uint32_t __syscalls_flash_start__[];
531 extern uint32_t __syscalls_flash_end__[];
532 #endif /* #if defined( __ARMCC_VERSION ) */
534 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
535 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
538 * 1. SVC is raised from the system call section (i.e. application is
539 * not raising SVC directly).
540 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must be NULL as
541 * it is non-NULL only during the execution of a system call (i.e.
542 * between system call enter and exit).
543 * 3. System call is not for a kernel API disabled by the configuration
544 * in FreeRTOSConfig.h.
545 * 4. We do not need to check that ucSystemCallNumber is within range
546 * because the assembly SVC handler checks that before calling
549 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
550 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) &&
551 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL ) &&
552 ( uxSystemCallImplementations[ ucSystemCallNumber ] != ( UBaseType_t ) 0 ) )
554 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
556 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
558 /* Extended frame i.e. FPU in use. */
559 ulStackFrameSize = 26;
561 " vpush {s0} \n" /* Trigger lazy stacking. */
562 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
568 /* Standard frame i.e. FPU not in use. */
569 ulStackFrameSize = 8;
572 /* Make space on the system call stack for the stack frame. */
573 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
575 /* Copy the stack frame. */
576 for( i = 0; i < ulStackFrameSize; i++ )
578 pulSystemCallStack[ i ] = pulTaskStack[ i ];
581 /* Use the pulSystemCallStack in thread mode. */
582 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
584 /* Raise the privilege for the duration of the system call. */
586 " mrs r1, control \n" /* Obtain current control value. */
587 " bic r1, #1 \n" /* Clear nPRIV bit. */
588 " msr control, r1 \n" /* Write back new control value. */
592 /* Remember the location where we should copy the stack frame when we exit from
593 * the system call. */
594 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
596 /* Store the value of the Link Register before the SVC was raised.
597 * It contains the address of the caller of the System Call entry
598 * point (i.e. the caller of the MPU_<API>). We need to restore it
599 * when we exit from the system call. */
600 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
602 /* Start executing the system call upon returning from this handler. */
603 pulSystemCallStack[ portOFFSET_TO_PC ] = uxSystemCallImplementations[ ucSystemCallNumber ];
604 /* Raise a request to exit from the system call upon finishing the
606 pulSystemCallStack[ portOFFSET_TO_LR ] = ( uint32_t ) vRequestSystemCallExit;
608 /* Record if the hardware used padding to force the stack pointer
609 * to be double word aligned. */
610 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
612 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
616 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
619 /* We ensure in pxPortInitialiseStack that the system call stack is
620 * double word aligned and therefore, there is no need of padding.
621 * Clear the bit[9] of stacked xPSR. */
622 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
626 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
627 /*-----------------------------------------------------------*/
629 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
631 void vRequestSystemCallExit( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
633 __asm volatile ( "svc %0 \n" ::"i" ( portSVC_SYSTEM_CALL_EXIT ) : "memory" );
636 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
637 /*-----------------------------------------------------------*/
639 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
641 void vSystemCallExit( uint32_t * pulSystemCallStack,
642 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
644 extern TaskHandle_t pxCurrentTCB;
645 xMPU_SETTINGS * pxMpuSettings;
646 uint32_t * pulTaskStack;
647 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
649 #if defined( __ARMCC_VERSION )
650 /* Declaration when these variable are defined in code instead of being
651 * exported from linker scripts. */
652 extern uint32_t * __privileged_functions_start__;
653 extern uint32_t * __privileged_functions_end__;
655 /* Declaration when these variable are exported from linker scripts. */
656 extern uint32_t __privileged_functions_start__[];
657 extern uint32_t __privileged_functions_end__[];
658 #endif /* #if defined( __ARMCC_VERSION ) */
660 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
661 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
664 * 1. SVC is raised from the privileged code (i.e. application is not
665 * raising SVC directly). This SVC is only raised from
666 * vRequestSystemCallExit which is in the privileged code section.
667 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must not be NULL -
668 * this means that we previously entered a system call and the
669 * application is not attempting to exit without entering a system
672 if( ( ulSystemCallLocation >= ( uint32_t ) __privileged_functions_start__ ) &&
673 ( ulSystemCallLocation <= ( uint32_t ) __privileged_functions_end__ ) &&
674 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack != NULL ) )
676 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
678 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
680 /* Extended frame i.e. FPU in use. */
681 ulStackFrameSize = 26;
683 " vpush {s0} \n" /* Trigger lazy stacking. */
684 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
690 /* Standard frame i.e. FPU not in use. */
691 ulStackFrameSize = 8;
694 /* Make space on the task stack for the stack frame. */
695 pulTaskStack = pulTaskStack - ulStackFrameSize;
697 /* Copy the stack frame. */
698 for( i = 0; i < ulStackFrameSize; i++ )
700 pulTaskStack[ i ] = pulSystemCallStack[ i ];
703 /* Use the pulTaskStack in thread mode. */
704 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
706 /* Drop the privilege before returning to the thread mode. */
708 " mrs r1, control \n" /* Obtain current control value. */
709 " orr r1, #1 \n" /* Set nPRIV bit. */
710 " msr control, r1 \n" /* Write back new control value. */
714 /* Return to the caller of the System Call entry point (i.e. the
715 * caller of the MPU_<API>). */
716 pulTaskStack[ portOFFSET_TO_PC ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
717 /* Ensure that LR has a valid value.*/
718 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
720 /* If the hardware used padding to force the stack pointer
721 * to be double word aligned, set the stacked xPSR bit[9],
722 * otherwise clear it. */
723 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
725 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
729 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
732 /* This is not NULL only for the duration of the system call. */
733 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
737 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
738 /*-----------------------------------------------------------*/
740 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
742 BaseType_t xTaskIsPrivileged = pdFALSE;
743 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
745 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
747 xTaskIsPrivileged = pdTRUE;
750 return xTaskIsPrivileged;
752 /*-----------------------------------------------------------*/
754 static void prvRestoreContextOfFirstTask( void )
758 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
761 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
763 /*------------ Program MPU. ------------ */
764 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
765 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
766 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
768 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
769 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
770 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
771 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
772 " str r3, [r0] \n" /* Disable MPU. */
774 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
775 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
776 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
778 #if ( configTOTAL_MPU_REGIONS == 16 )
779 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 4 - 8]. */
780 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 4 - 8]. */
781 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 9 - 12]. */
782 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 9 - 12]. */
783 #endif /* configTOTAL_MPU_REGIONS == 16. */
785 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
786 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
787 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
788 " str r3, [r0] \n" /* Enable MPU. */
789 " dsb \n" /* Force memory writes before continuing. */
791 /*---------- Restore Context. ---------- */
792 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
793 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
794 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
796 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
798 " stmia r0, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
799 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
800 " msr control, r3 \n"
801 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
804 " msr basepri, r0 \n"
807 " .ltorg \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
810 /*-----------------------------------------------------------*/
813 * See header file for description.
815 BaseType_t xPortStartScheduler( void )
817 /* Errata 837070 workaround must only be enabled on Cortex-M7 r0p0
819 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
820 configASSERT( ( portCPUID == portCORTEX_M7_r0p1_ID ) || ( portCPUID == portCORTEX_M7_r0p0_ID ) );
823 /* When using this port on a Cortex-M7 r0p0 or r0p1 core, define
824 * configENABLE_ERRATA_837070_WORKAROUND to 1 in your
825 * FreeRTOSConfig.h. */
826 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
827 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
830 /* An application can install FreeRTOS interrupt handlers in one of the
832 * 1. Direct Routing - Install the functions vPortSVCHandler and
833 * xPortPendSVHandler for SVCall and PendSV interrupts respectively.
834 * 2. Indirect Routing - Install separate handlers for SVCall and PendSV
835 * interrupts and route program control from those handlers to
836 * vPortSVCHandler and xPortPendSVHandler functions.
838 * Applications that use Indirect Routing must set
839 * configCHECK_HANDLER_INSTALLATION to 0 in their FreeRTOSConfig.h. Direct
840 * routing, which is validated here when configCHECK_HANDLER_INSTALLATION
841 * is 1, should be preferred when possible. */
842 #if ( configCHECK_HANDLER_INSTALLATION == 1 )
844 const portISR_t * const pxVectorTable = portSCB_VTOR_REG;
846 /* Validate that the application has correctly installed the FreeRTOS
847 * handlers for SVCall and PendSV interrupts. We do not check the
848 * installation of the SysTick handler because the application may
849 * choose to drive the RTOS tick using a timer other than the SysTick
850 * timer by overriding the weak function vPortSetupTimerInterrupt().
852 * Assertion failures here indicate incorrect installation of the
853 * FreeRTOS handlers. For help installing the FreeRTOS handlers, see
854 * https://www.freertos.org/Why-FreeRTOS/FAQs.
856 * Systems with a configurable address for the interrupt vector table
857 * can also encounter assertion failures or even system faults here if
858 * VTOR is not set correctly to point to the application's vector table. */
859 configASSERT( pxVectorTable[ portVECTOR_INDEX_SVC ] == vPortSVCHandler );
860 configASSERT( pxVectorTable[ portVECTOR_INDEX_PENDSV ] == xPortPendSVHandler );
862 #endif /* configCHECK_HANDLER_INSTALLATION */
864 #if ( configASSERT_DEFINED == 1 )
866 volatile uint8_t ucOriginalPriority;
867 volatile uint32_t ulImplementedPrioBits = 0;
868 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
869 volatile uint8_t ucMaxPriorityValue;
871 /* Determine the maximum priority from which ISR safe FreeRTOS API
872 * functions can be called. ISR safe functions are those that end in
873 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
874 * ensure interrupt entry is as fast and simple as possible.
876 * Save the interrupt priority value that is about to be clobbered. */
877 ucOriginalPriority = *pucFirstUserPriorityRegister;
879 /* Determine the number of priority bits available. First write to all
881 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
883 /* Read the value back to see how many bits stuck. */
884 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
886 /* Use the same mask on the maximum system call priority. */
887 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
889 /* Check that the maximum system call priority is nonzero after
890 * accounting for the number of priority bits supported by the
891 * hardware. A priority of 0 is invalid because setting the BASEPRI
892 * register to 0 unmasks all interrupts, and interrupts with priority 0
893 * cannot be masked using BASEPRI.
894 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
895 configASSERT( ucMaxSysCallPriority );
897 /* Check that the bits not implemented in hardware are zero in
898 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
899 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
901 /* Calculate the maximum acceptable priority group value for the number
902 * of bits read back. */
904 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
906 ulImplementedPrioBits++;
907 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
910 if( ulImplementedPrioBits == 8 )
912 /* When the hardware implements 8 priority bits, there is no way for
913 * the software to configure PRIGROUP to not have sub-priorities. As
914 * a result, the least significant bit is always used for sub-priority
915 * and there are 128 preemption priorities and 2 sub-priorities.
917 * This may cause some confusion in some cases - for example, if
918 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
919 * priority interrupts will be masked in Critical Sections as those
920 * are at the same preemption priority. This may appear confusing as
921 * 4 is higher (numerically lower) priority than
922 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
923 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
924 * to 4, this confusion does not happen and the behaviour remains the same.
926 * The following assert ensures that the sub-priority bit in the
927 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
929 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
930 ulMaxPRIGROUPValue = 0;
934 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
937 /* Shift the priority group value back to its position within the AIRCR
939 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
940 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
942 /* Restore the clobbered interrupt priority register to its original
944 *pucFirstUserPriorityRegister = ucOriginalPriority;
946 #endif /* configASSERT_DEFINED */
948 /* Make PendSV and SysTick the lowest priority interrupts, and make SVCall
949 * the highest priority. */
950 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
951 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
952 portNVIC_SHPR2_REG = 0;
954 /* Configure the regions in the MPU that are common to all tasks. */
957 /* Start the timer that generates the tick ISR. Interrupts are disabled
959 vPortSetupTimerInterrupt();
961 /* Initialise the critical nesting count ready for the first task. */
962 uxCriticalNesting = 0;
964 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
966 xSchedulerRunning = pdTRUE;
968 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
970 /* Ensure the VFP is enabled - it should be anyway. */
973 /* Lazy save always. */
974 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
976 /* Start the first task. This also clears the bit that indicates the FPU is
977 * in use in case the FPU was used before the scheduler was started - which
978 * would otherwise result in the unnecessary leaving of space in the SVC stack
979 * for lazy saving of FPU registers. */
981 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
984 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
985 " mov r0, #0 \n" /* Clear the bit that indicates the FPU is in use, see comment above. */
986 " msr control, r0 \n"
987 " cpsie i \n" /* Globally enable interrupts. */
991 " svc %0 \n" /* System call to start first task. */
994 ::"i" ( portSVC_START_SCHEDULER ) : "memory" );
996 /* Should not get here! */
999 /*-----------------------------------------------------------*/
1001 void vPortEndScheduler( void )
1003 /* Not implemented in ports where there is nothing to return to.
1004 * Artificially force an assert. */
1005 configASSERT( uxCriticalNesting == 1000UL );
1007 /*-----------------------------------------------------------*/
1009 void vPortEnterCritical( void )
1011 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
1012 if( portIS_PRIVILEGED() == pdFALSE )
1014 portRAISE_PRIVILEGE();
1015 portMEMORY_BARRIER();
1017 portDISABLE_INTERRUPTS();
1018 uxCriticalNesting++;
1019 portMEMORY_BARRIER();
1021 portRESET_PRIVILEGE();
1022 portMEMORY_BARRIER();
1026 portDISABLE_INTERRUPTS();
1027 uxCriticalNesting++;
1029 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1030 portDISABLE_INTERRUPTS();
1031 uxCriticalNesting++;
1032 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1034 /*-----------------------------------------------------------*/
1036 void vPortExitCritical( void )
1038 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
1039 if( portIS_PRIVILEGED() == pdFALSE )
1041 portRAISE_PRIVILEGE();
1042 portMEMORY_BARRIER();
1044 configASSERT( uxCriticalNesting );
1045 uxCriticalNesting--;
1047 if( uxCriticalNesting == 0 )
1049 portENABLE_INTERRUPTS();
1052 portMEMORY_BARRIER();
1054 portRESET_PRIVILEGE();
1055 portMEMORY_BARRIER();
1059 configASSERT( uxCriticalNesting );
1060 uxCriticalNesting--;
1062 if( uxCriticalNesting == 0 )
1064 portENABLE_INTERRUPTS();
1067 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1068 configASSERT( uxCriticalNesting );
1069 uxCriticalNesting--;
1071 if( uxCriticalNesting == 0 )
1073 portENABLE_INTERRUPTS();
1075 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
1077 /*-----------------------------------------------------------*/
1079 void xPortPendSVHandler( void )
1081 /* This is a naked function. */
1085 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
1086 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1087 " ldr r1, [r2] \n" /* r1 = Location where the context should be saved. */
1089 /*------------ Save Context. ----------- */
1090 " mrs r3, control \n"
1094 " add r0, r0, #0x20 \n" /* Move r0 to location where s0 is saved. */
1097 " vstmiaeq r1!, {s16-s31} \n" /* Store s16-s31. */
1098 " vldmiaeq r0, {s0-s16} \n" /* Copy hardware saved FP context into s0-s16. */
1099 " vstmiaeq r1!, {s0-s16} \n" /* Store hardware saved FP context. */
1100 " sub r0, r0, #0x20 \n" /* Set r0 back to the location of hardware saved context. */
1102 " stmia r1!, {r3-r11, lr} \n" /* Store CONTROL register, r4-r11 and LR. */
1103 " ldmia r0, {r4-r11} \n" /* Copy hardware saved context into r4-r11. */
1104 " stmia r1!, {r0, r4-r11} \n" /* Store original PSP (after hardware has saved context) and the hardware saved context. */
1105 " str r1, [r2] \n" /* Save the location from where the context should be restored as the first member of TCB. */
1107 /*---------- Select next task. --------- */
1109 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
1110 " cpsid i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
1112 " msr basepri, r0 \n"
1115 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
1116 " cpsie i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
1118 " bl vTaskSwitchContext \n"
1120 " msr basepri, r0 \n"
1122 /*------------ Program MPU. ------------ */
1123 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
1124 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1125 " add r2, r2, #4 \n" /* r2 = Second item in the TCB which is xMPUSettings. */
1127 " dmb \n" /* Complete outstanding transfers before disabling MPU. */
1128 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1129 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1130 " bic r3, #1 \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
1131 " str r3, [r0] \n" /* Disable MPU. */
1133 " ldr r0, =0xe000ed9c \n" /* Region Base Address register. */
1134 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
1135 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
1137 #if ( configTOTAL_MPU_REGIONS == 16 )
1138 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 4 - 7]. */
1139 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 4 - 7]. */
1140 " ldmia r2!, {r4-r11} \n" /* Read 4 sets of MPU registers [MPU Region # 8 - 11]. */
1141 " stmia r0, {r4-r11} \n" /* Write 4 sets of MPU registers. [MPU Region # 8 - 11]. */
1142 #endif /* configTOTAL_MPU_REGIONS == 16. */
1144 " ldr r0, =0xe000ed94 \n" /* MPU_CTRL register. */
1145 " ldr r3, [r0] \n" /* Read the value of MPU_CTRL. */
1146 " orr r3, #1 \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
1147 " str r3, [r0] \n" /* Enable MPU. */
1148 " dsb \n" /* Force memory writes before continuing. */
1150 /*---------- Restore Context. ---------- */
1151 " ldr r3, =pxCurrentTCB \n" /* r3 = =pxCurrentTCB. */
1152 " ldr r2, [r3] \n" /* r2 = pxCurrentTCB. */
1153 " ldr r1, [r2] \n" /* r1 = Location of saved context in TCB. */
1155 " ldmdb r1!, {r0, r4-r11} \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
1157 " stmia r0!, {r4-r11} \n" /* Copy the hardware saved context on the task stack. */
1158 " ldmdb r1!, {r3-r11, lr} \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
1159 " msr control, r3 \n"
1163 " vldmdbeq r1!, {s0-s16} \n" /* s0-s16 contain hardware saved FP context. */
1164 " vstmiaeq r0!, {s0-s16} \n" /* Copy hardware saved FP context on the task stack. */
1165 " vldmdbeq r1!, {s16-s31} \n" /* Restore s16-s31. */
1167 " str r1, [r2] \n" /* Save the location where the context should be saved next as the first member of TCB. */
1170 " .ltorg \n" /* Assemble the current literal pool to avoid offset-out-of-bound errors with lto. */
1171 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
1174 /*-----------------------------------------------------------*/
1176 void xPortSysTickHandler( void )
1180 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
1183 /* Increment the RTOS tick. */
1184 if( xTaskIncrementTick() != pdFALSE )
1186 traceISR_EXIT_TO_SCHEDULER();
1187 /* Pend a context switch. */
1188 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
1195 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
1197 /*-----------------------------------------------------------*/
1200 * Setup the systick timer to generate the tick interrupts at the required
1203 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
1205 /* Stop and clear the SysTick. */
1206 portNVIC_SYSTICK_CTRL_REG = 0UL;
1207 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1209 /* Configure SysTick to interrupt at the requested rate. */
1210 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1211 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE );
1213 /*-----------------------------------------------------------*/
1215 /* This is a naked function. */
1216 static void vPortEnableVFP( void )
1220 " ldr.w r0, =0xE000ED88 \n" /* The FPU enable bits are in the CPACR. */
1223 " orr r1, r1, #( 0xf << 20 ) \n" /* Enable CP10 and CP11 coprocessors, then save back. */
1229 /*-----------------------------------------------------------*/
1231 static void prvSetupMPU( void )
1233 #if defined( __ARMCC_VERSION )
1235 /* Declaration when these variable are defined in code instead of being
1236 * exported from linker scripts. */
1237 extern uint32_t * __privileged_functions_start__;
1238 extern uint32_t * __privileged_functions_end__;
1239 extern uint32_t * __FLASH_segment_start__;
1240 extern uint32_t * __FLASH_segment_end__;
1241 extern uint32_t * __privileged_data_start__;
1242 extern uint32_t * __privileged_data_end__;
1244 /* Declaration when these variable are exported from linker scripts. */
1245 extern uint32_t __privileged_functions_start__[];
1246 extern uint32_t __privileged_functions_end__[];
1247 extern uint32_t __FLASH_segment_start__[];
1248 extern uint32_t __FLASH_segment_end__[];
1249 extern uint32_t __privileged_data_start__[];
1250 extern uint32_t __privileged_data_end__[];
1251 #endif /* if defined( __ARMCC_VERSION ) */
1253 /* The only permitted number of regions are 8 or 16. */
1254 configASSERT( ( configTOTAL_MPU_REGIONS == 8 ) || ( configTOTAL_MPU_REGIONS == 16 ) );
1256 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
1257 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
1259 /* Check the expected MPU is present. */
1260 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1262 /* First setup the unprivileged flash for unprivileged read only access. */
1263 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1264 ( portMPU_REGION_VALID ) |
1265 ( portUNPRIVILEGED_FLASH_REGION );
1267 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1268 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1269 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1270 ( portMPU_REGION_ENABLE );
1272 /* Setup the privileged flash for privileged only access. This is where
1273 * the kernel code is placed. */
1274 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1275 ( portMPU_REGION_VALID ) |
1276 ( portPRIVILEGED_FLASH_REGION );
1278 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1279 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1280 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1281 ( portMPU_REGION_ENABLE );
1283 /* Setup the privileged data RAM region. This is where the kernel data
1285 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1286 ( portMPU_REGION_VALID ) |
1287 ( portPRIVILEGED_RAM_REGION );
1289 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1290 ( portMPU_REGION_EXECUTE_NEVER ) |
1291 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1292 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1293 ( portMPU_REGION_ENABLE );
1295 /* By default allow everything to access the general peripherals. The
1296 * system peripherals and registers are protected. */
1297 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1298 ( portMPU_REGION_VALID ) |
1299 ( portGENERAL_PERIPHERALS_REGION );
1301 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1302 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1303 ( portMPU_REGION_ENABLE );
1305 /* Enable the memory fault exception. */
1306 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1308 /* Enable the MPU with the background region configured. */
1309 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1312 /*-----------------------------------------------------------*/
1314 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1316 uint32_t ulRegionSize, ulReturnValue = 4;
1318 /* 32 is the smallest region size, 31 is the largest valid value for
1320 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1322 if( ulActualSizeInBytes <= ulRegionSize )
1332 /* Shift the code by one before returning so it can be written directly
1333 * into the the correct bit position of the attribute register. */
1334 return( ulReturnValue << 1UL );
1336 /*-----------------------------------------------------------*/
1338 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
1342 " mrs r0, control \n" /* r0 = CONTROL. */
1343 " tst r0, #1 \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
1345 " movne r0, #0 \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
1346 " moveq r0, #1 \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
1347 " bx lr \n" /* Return. */
1351 /*-----------------------------------------------------------*/
1353 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
1357 " mrs r0, control \n" /* r0 = CONTROL. */
1358 " orr r0, #1 \n" /* r0 = r0 | 1. */
1359 " msr control, r0 \n" /* CONTROL = r0. */
1360 " bx lr \n" /* Return to the caller. */
1364 /*-----------------------------------------------------------*/
1366 void vPortSwitchToUserMode( void )
1368 /* Load the current task's MPU settings from its TCB. */
1369 xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL );
1371 /* Mark the task as unprivileged. */
1372 xTaskMpuSettings->ulTaskFlags &= ( ~( portTASK_IS_PRIVILEGED_FLAG ) );
1374 /* Lower the processor's privilege level. */
1377 /*-----------------------------------------------------------*/
1379 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1380 const struct xMEMORY_REGION * const xRegions,
1381 StackType_t * pxBottomOfStack,
1382 configSTACK_DEPTH_TYPE uxStackDepth )
1384 #if defined( __ARMCC_VERSION )
1386 /* Declaration when these variable are defined in code instead of being
1387 * exported from linker scripts. */
1388 extern uint32_t * __SRAM_segment_start__;
1389 extern uint32_t * __SRAM_segment_end__;
1390 extern uint32_t * __privileged_data_start__;
1391 extern uint32_t * __privileged_data_end__;
1393 /* Declaration when these variable are exported from linker scripts. */
1394 extern uint32_t __SRAM_segment_start__[];
1395 extern uint32_t __SRAM_segment_end__[];
1396 extern uint32_t __privileged_data_start__[];
1397 extern uint32_t __privileged_data_end__[];
1398 #endif /* if defined( __ARMCC_VERSION ) */
1403 if( xRegions == NULL )
1405 /* No MPU regions are specified so allow access to all RAM. */
1406 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1407 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1408 ( portMPU_REGION_VALID ) |
1409 ( portSTACK_REGION ); /* Region number. */
1411 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1412 ( portMPU_REGION_READ_WRITE ) |
1413 ( portMPU_REGION_EXECUTE_NEVER ) |
1414 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1415 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1416 ( portMPU_REGION_ENABLE );
1418 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1419 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1420 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1421 tskMPU_WRITE_PERMISSION );
1423 /* Invalidate user configurable regions. */
1424 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1426 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1427 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1428 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1429 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1430 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1435 /* This function is called automatically when the task is created - in
1436 * which case the stack region parameters will be valid. At all other
1437 * times the stack parameters will not be valid and it is assumed that the
1438 * stack region has already been configured. */
1439 if( uxStackDepth > 0 )
1441 /* Define the region that allows access to the stack. */
1442 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1443 ( ( uint32_t ) pxBottomOfStack ) |
1444 ( portMPU_REGION_VALID ) |
1445 ( portSTACK_REGION ); /* Region number. */
1447 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1448 ( portMPU_REGION_READ_WRITE ) |
1449 ( portMPU_REGION_EXECUTE_NEVER ) |
1450 ( prvGetMPURegionSizeSetting( uxStackDepth * ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t ) ) ) |
1451 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1452 ( portMPU_REGION_ENABLE );
1454 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1455 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1456 ( uxStackDepth * ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t ) ) - 1UL );
1457 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1458 tskMPU_WRITE_PERMISSION );
1463 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1465 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1467 /* Translate the generic region definition contained in
1468 * xRegions into the CM4 specific MPU settings that are then
1469 * stored in xMPUSettings. */
1470 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1471 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1472 ( portMPU_REGION_VALID ) |
1473 ( ul - 1UL ); /* Region number. */
1475 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1476 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1477 ( xRegions[ lIndex ].ulParameters ) |
1478 ( portMPU_REGION_ENABLE );
1480 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1481 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1482 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1484 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1485 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1487 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1490 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1492 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1497 /* Invalidate the region. */
1498 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1499 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1500 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1501 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1502 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1509 /*-----------------------------------------------------------*/
1511 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1513 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1514 uint32_t ulBufferLength,
1515 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1518 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1519 BaseType_t xAccessGranted = pdFALSE;
1520 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1522 if( xSchedulerRunning == pdFALSE )
1524 /* Grant access to all the kernel objects before the scheduler
1525 * is started. It is necessary because there is no task running
1526 * yet and therefore, we cannot use the permissions of any
1528 xAccessGranted = pdTRUE;
1530 else if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1532 xAccessGranted = pdTRUE;
1536 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1538 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1539 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1541 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1543 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1544 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1545 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1546 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1547 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1548 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1549 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1551 xAccessGranted = pdTRUE;
1558 return xAccessGranted;
1561 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1562 /*-----------------------------------------------------------*/
1564 #if ( configASSERT_DEFINED == 1 )
1566 void vPortValidateInterruptPriority( void )
1568 uint32_t ulCurrentInterrupt;
1569 uint8_t ucCurrentPriority;
1571 /* Obtain the number of the currently executing interrupt. */
1572 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1574 /* Is the interrupt number a user defined interrupt? */
1575 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1577 /* Look up the interrupt's priority. */
1578 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1580 /* The following assertion will fail if a service routine (ISR) for
1581 * an interrupt that has been assigned a priority above
1582 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1583 * function. ISR safe FreeRTOS API functions must *only* be called
1584 * from interrupts that have been assigned a priority at or below
1585 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1587 * Numerically low interrupt priority numbers represent logically high
1588 * interrupt priorities, therefore the priority of the interrupt must
1589 * be set to a value equal to or numerically *higher* than
1590 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1592 * Interrupts that use the FreeRTOS API must not be left at their
1593 * default priority of zero as that is the highest possible priority,
1594 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1595 * and therefore also guaranteed to be invalid.
1597 * FreeRTOS maintains separate thread and ISR API functions to ensure
1598 * interrupt entry is as fast and simple as possible.
1600 * The following links provide detailed information:
1601 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1602 * https://www.freertos.org/Why-FreeRTOS/FAQs */
1603 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1606 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1607 * that define each interrupt's priority to be split between bits that
1608 * define the interrupt's pre-emption priority bits and bits that define
1609 * the interrupt's sub-priority. For simplicity all bits must be defined
1610 * to be pre-emption priority bits. The following assertion will fail if
1611 * this is not the case (if some bits represent a sub-priority).
1613 * If the application only uses CMSIS libraries for interrupt
1614 * configuration then the correct setting can be achieved on all Cortex-M
1615 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1616 * scheduler. Note however that some vendor specific peripheral libraries
1617 * assume a non-zero priority group setting, in which cases using a value
1618 * of zero will result in unpredictable behaviour. */
1619 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1622 #endif /* configASSERT_DEFINED */
1623 /*-----------------------------------------------------------*/
1625 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1627 void vPortGrantAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1628 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1630 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1631 xMPU_SETTINGS * xTaskMpuSettings;
1633 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1634 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1636 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1638 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] |= ( 1U << ulAccessControlListEntryBit );
1641 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1642 /*-----------------------------------------------------------*/
1644 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1646 void vPortRevokeAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1647 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1649 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1650 xMPU_SETTINGS * xTaskMpuSettings;
1652 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1653 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1655 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1657 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] &= ~( 1U << ulAccessControlListEntryBit );
1660 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1661 /*-----------------------------------------------------------*/
1663 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1665 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
1667 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1669 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1670 BaseType_t xAccessGranted = pdFALSE;
1671 const xMPU_SETTINGS * xTaskMpuSettings;
1673 if( xSchedulerRunning == pdFALSE )
1675 /* Grant access to all the kernel objects before the scheduler
1676 * is started. It is necessary because there is no task running
1677 * yet and therefore, we cannot use the permissions of any
1679 xAccessGranted = pdTRUE;
1683 xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1685 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1686 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1688 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1690 xAccessGranted = pdTRUE;
1694 if( ( xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] & ( 1U << ulAccessControlListEntryBit ) ) != 0 )
1696 xAccessGranted = pdTRUE;
1701 return xAccessGranted;
1704 #else /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1706 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1708 ( void ) lInternalIndexOfKernelObject;
1710 /* If Access Control List feature is not used, all the tasks have
1711 * access to all the kernel objects. */
1715 #endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1717 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1718 /*-----------------------------------------------------------*/