1 /*
2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ARM CM4F MPU port.
31 *----------------------------------------------------------*/
32
33 /* IAR includes. */
34 #include <intrinsics.h>
35
36 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
37 * all the API functions to use the MPU wrappers. That should only be done when
38 * task.h is included from an application file. */
39 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
40
41 /* Scheduler includes. */
42 #include "FreeRTOS.h"
43 #include "task.h"
44 #include "mpu_syscall_numbers.h"
45
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
47
48 #ifndef __ARMVFP__
49 #error This port can only be used when the project options are configured to enable hardware floating point support.
50 #endif
51
52 #if ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )
53 #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http: /*www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
54 #endif
55
56 #ifndef configSYSTICK_CLOCK_HZ
57 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
58 /* Ensure the SysTick is clocked at the same frequency as the core. */
59 #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
60 #else
61
62 /* The way the SysTick is clocked is not modified in case it is not the same
63 * as the core. */
64 #define portNVIC_SYSTICK_CLK_BIT ( 0 )
65 #endif
66
67 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
68 #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
69 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
70 #endif
71
72 /* Constants required to manipulate the core. Registers first... */
73 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
74 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
75 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
76 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
77 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
78 #define portNVIC_SYS_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
79 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
80
81 /* Constants required to access and manipulate the MPU. */
82 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
83 #define portMPU_REGION_BASE_ADDRESS_REG ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
84 #define portMPU_REGION_ATTRIBUTE_REG ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
85 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
86 #define portEXPECTED_MPU_TYPE_VALUE ( configTOTAL_MPU_REGIONS << 8UL )
87 #define portMPU_ENABLE ( 0x01UL )
88 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
89 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
90 #define portMPU_REGION_VALID ( 0x10UL )
91 #define portMPU_REGION_ENABLE ( 0x01UL )
92 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
93 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
94
95 /* ...then bits in the registers. */
96 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
97 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
98 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
99 #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
100 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
101
102 /* Constants used to detect Cortex-M7 r0p0 and r0p1 cores, and ensure
103 * that a work around is active for errata 837070. */
104 #define portCPUID ( *( ( volatile uint32_t * ) 0xE000ed00 ) )
105 #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
106 #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
107
108 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
109 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
110 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
111 #define portNVIC_SVC_PRI ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
112
113 /* Constants required to check the validity of an interrupt priority. */
114 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
115 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
116 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
117 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
118 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
119 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
120 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
121 #define portPRIGROUP_SHIFT ( 8UL )
122
123 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
124 #define portVECTACTIVE_MASK ( 0xFFUL )
125
126 /* Constants required to manipulate the VFP. */
127 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
128 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
129
130 /* Constants required to set up the initial stack. */
131 #define portINITIAL_XPSR ( 0x01000000 )
132 #define portINITIAL_EXC_RETURN ( 0xfffffffd )
133 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
134 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
135
136 /* Constants used during system call enter and exit. */
137 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
138 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
139
140 /* Offsets in the stack to the parameters when inside the SVC handler. */
141 #define portOFFSET_TO_LR ( 5 )
142 #define portOFFSET_TO_PC ( 6 )
143 #define portOFFSET_TO_PSR ( 7 )
144
145 /* The systick is a 24-bit counter. */
146 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
147
148 /* A fiddle factor to estimate the number of SysTick counts that would have
149 * occurred while the SysTick counter is stopped during tickless idle
150 * calculations. */
151 #define portMISSED_COUNTS_FACTOR ( 45UL )
152
153 /* For strict compliance with the Cortex-M spec the task start address should
154 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
155 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
156
157 /* Does addr lie within [start, end] address range? */
158 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
159 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
160
161 /* Is the access request satisfied by the available permissions? */
162 #define portIS_AUTHORIZED( accessRequest, permissions ) \
163 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
164
165 /* Max value that fits in a uint32_t type. */
166 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
167
168 /* Check if adding a and b will result in overflow. */
169 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
170 /*-----------------------------------------------------------*/
171
172 /*
173 * Configure a number of standard MPU regions that are used by all tasks.
174 */
175 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
176
177 /*
178 * Return the smallest MPU region size that a given number of bytes will fit
179 * into. The region size is returned as the value that should be programmed
180 * into the region attribute register for that region.
181 */
182 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
183
184 /*
185 * Setup the timer to generate the tick interrupts. The implementation in this
186 * file is weak to allow application writers to change the timer used to
187 * generate the tick interrupt.
188 */
189 void vPortSetupTimerInterrupt( void );
190
191 /*
192 * Exception handlers.
193 */
194 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
195
196 /*
197 * Start first task is a separate function so it can be tested in isolation.
198 */
199 extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;
200
201 /*
202 * Turn the VFP on.
203 */
204 extern void vPortEnableVFP( void );
205
206 /*
207 * The C portion of the SVC handler.
208 */
209 void vPortSVCHandler_C( uint32_t * pulParam ) PRIVILEGED_FUNCTION;
210
211 /*
212 * Called from the SVC handler used to start the scheduler.
213 */
214 extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;
215
216 /**
217 * @brief Enter critical section.
218 */
219 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
220 void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
221 #else
222 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
223 #endif
224
225 /**
226 * @brief Exit from critical section.
227 */
228 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
229 void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
230 #else
231 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
232 #endif
233
234 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
235
236 /**
237 * @brief Sets up the system call stack so that upon returning from
238 * SVC, the system call stack is used.
239 *
240 * @param pulTaskStack The current SP when the SVC was raised.
241 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
242 * @param ucSystemCallNumber The system call number of the system call.
243 */
244 void vSystemCallEnter( uint32_t * pulTaskStack,
245 uint32_t ulLR,
246 uint8_t ucSystemCallNumber ) PRIVILEGED_FUNCTION;
247
248 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
249
250 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
251
252 /**
253 * @brief Raise SVC for exiting from a system call.
254 */
255 void vRequestSystemCallExit( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
256
257 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
258
259 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
260
261 /**
262 * @brief Sets up the task stack so that upon returning from
263 * SVC, the task stack is used again.
264 *
265 * @param pulSystemCallStack The current SP when the SVC was raised.
266 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
267 */
268 void vSystemCallExit( uint32_t * pulSystemCallStack,
269 uint32_t ulLR ) PRIVILEGED_FUNCTION;
270
271 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
272
273 /**
274 * @brief Checks whether or not the calling task is privileged.
275 *
276 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
277 */
278 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
279
280 /*-----------------------------------------------------------*/
281
282 /* Each task maintains its own interrupt status in the critical nesting
283 * variable. */
284 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
285
286 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
287
288 /*
289 * This variable is set to pdTRUE when the scheduler is started.
290 */
291 PRIVILEGED_DATA static BaseType_t xSchedulerRunning = pdFALSE;
292
293 #endif
294
295 /*
296 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
297 * FreeRTOS API functions are not called from interrupts that have been assigned
298 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
299 */
300 #if ( configASSERT_DEFINED == 1 )
301 static uint8_t ucMaxSysCallPriority = 0;
302 static uint32_t ulMaxPRIGROUPValue = 0;
303 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
304 #endif /* configASSERT_DEFINED */
305
306 /*-----------------------------------------------------------*/
307
308 /*
309 * See header file for description.
310 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters,BaseType_t xRunPrivileged,xMPU_SETTINGS * xMPUSettings)311 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
312 TaskFunction_t pxCode,
313 void * pvParameters,
314 BaseType_t xRunPrivileged,
315 xMPU_SETTINGS * xMPUSettings )
316 {
317 if( xRunPrivileged == pdTRUE )
318 {
319 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
320 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
321 }
322 else
323 {
324 xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
325 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
326 }
327 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
328 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
329 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
330 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
331 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
332 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
333 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
334 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
335 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
336
337 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
338 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
339 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
340 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
341 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
342 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
343 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
344 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
345 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
346
347 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
348 {
349 /* Ensure that the system call stack is double word aligned. */
350 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
351 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
352 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
353
354 /* This is not NULL only for the duration of a system call. */
355 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
356 }
357 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
358
359 return &( xMPUSettings->ulContext[ 19 ] );
360 }
361 /*-----------------------------------------------------------*/
362
vPortSVCHandler_C(uint32_t * pulParam)363 void vPortSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
364 {
365 uint8_t ucSVCNumber;
366 uint32_t ulPC;
367
368 #if ( ( configUSE_MPU_WRAPPERS_V1 == 1 ) && ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) )
369 extern uint32_t __syscalls_flash_start__[];
370 extern uint32_t __syscalls_flash_end__[];
371 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 1 ) && ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) ) */
372
373 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
374 * argument (r0) is pulParam[ 0 ]. */
375 ulPC = pulParam[ portOFFSET_TO_PC ];
376 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
377
378 switch( ucSVCNumber )
379 {
380 case portSVC_START_SCHEDULER:
381 portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
382 vPortRestoreContextOfFirstTask();
383 break;
384
385 case portSVC_YIELD:
386 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
387
388 /* Barriers are normally not required
389 * but do ensure the code is completely
390 * within the specified behaviour for the
391 * architecture. */
392 __asm volatile ( "dsb" ::: "memory" );
393 __asm volatile ( "isb" );
394
395 break;
396
397 #if ( configUSE_MPU_WRAPPERS_V1 == 1 )
398 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
399 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
400 * svc was raised from any of the
401 * system calls. */
402
403 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
404 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
405 {
406 __asm volatile
407 (
408 " mrs r1, control \n" /* Obtain current control value. */
409 " bic r1, r1, #1 \n" /* Set privilege bit. */
410 " msr control, r1 \n" /* Write back new control value. */
411 ::: "r1", "memory"
412 );
413 }
414
415 break;
416 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
417 case portSVC_RAISE_PRIVILEGE:
418 __asm volatile
419 (
420 " mrs r1, control \n" /* Obtain current control value. */
421 " bic r1, r1, #1 \n" /* Set privilege bit. */
422 " msr control, r1 \n" /* Write back new control value. */
423 ::: "r1", "memory"
424 );
425 break;
426 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
427 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
428
429 default: /* Unknown SVC call. */
430 break;
431 }
432 }
433 /*-----------------------------------------------------------*/
434
435 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
436
vSystemCallEnter(uint32_t * pulTaskStack,uint32_t ulLR,uint8_t ucSystemCallNumber)437 void vSystemCallEnter( uint32_t * pulTaskStack,
438 uint32_t ulLR,
439 uint8_t ucSystemCallNumber ) /* PRIVILEGED_FUNCTION */
440 {
441 extern TaskHandle_t pxCurrentTCB;
442 extern UBaseType_t uxSystemCallImplementations[ NUM_SYSTEM_CALLS ];
443 xMPU_SETTINGS * pxMpuSettings;
444 uint32_t * pulSystemCallStack;
445 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
446
447 #if defined( __ARMCC_VERSION )
448 /* Declaration when these variable are defined in code instead of being
449 * exported from linker scripts. */
450 extern uint32_t * __syscalls_flash_start__;
451 extern uint32_t * __syscalls_flash_end__;
452 #else
453 /* Declaration when these variable are exported from linker scripts. */
454 extern uint32_t __syscalls_flash_start__[];
455 extern uint32_t __syscalls_flash_end__[];
456 #endif /* #if defined( __ARMCC_VERSION ) */
457
458 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
459 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
460
461 /* Checks:
462 * 1. SVC is raised from the system call section (i.e. application is
463 * not raising SVC directly).
464 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must be NULL as
465 * it is non-NULL only during the execution of a system call (i.e.
466 * between system call enter and exit).
467 * 3. System call is not for a kernel API disabled by the configuration
468 * in FreeRTOSConfig.h.
469 * 4. We do not need to check that ucSystemCallNumber is within range
470 * because the assembly SVC handler checks that before calling
471 * this function.
472 */
473 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
474 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) &&
475 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL ) &&
476 ( uxSystemCallImplementations[ ucSystemCallNumber ] != ( UBaseType_t ) 0 ) )
477 {
478 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
479
480 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
481 {
482 /* Extended frame i.e. FPU in use. */
483 ulStackFrameSize = 26;
484 __asm volatile
485 (
486 " vpush {s0} \n" /* Trigger lazy stacking. */
487 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
488 ::: "memory"
489 );
490 }
491 else
492 {
493 /* Standard frame i.e. FPU not in use. */
494 ulStackFrameSize = 8;
495 }
496
497 /* Make space on the system call stack for the stack frame. */
498 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
499
500 /* Copy the stack frame. */
501 for( i = 0; i < ulStackFrameSize; i++ )
502 {
503 pulSystemCallStack[ i ] = pulTaskStack[ i ];
504 }
505
506 /* Use the pulSystemCallStack in thread mode. */
507 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
508
509 /* Raise the privilege for the duration of the system call. */
510 __asm volatile
511 (
512 " mrs r1, control \n" /* Obtain current control value. */
513 " bic r1, #1 \n" /* Clear nPRIV bit. */
514 " msr control, r1 \n" /* Write back new control value. */
515 ::: "r1", "memory"
516 );
517
518 /* Remember the location where we should copy the stack frame when we exit from
519 * the system call. */
520 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
521
522 /* Store the value of the Link Register before the SVC was raised.
523 * It contains the address of the caller of the System Call entry
524 * point (i.e. the caller of the MPU_<API>). We need to restore it
525 * when we exit from the system call. */
526 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
527
528
529 /* Start executing the system call upon returning from this handler. */
530 pulSystemCallStack[ portOFFSET_TO_PC ] = uxSystemCallImplementations[ ucSystemCallNumber ];
531
532 /* Raise a request to exit from the system call upon finishing the
533 * system call. */
534 pulSystemCallStack[ portOFFSET_TO_LR ] = ( uint32_t ) vRequestSystemCallExit;
535
536 /* Record if the hardware used padding to force the stack pointer
537 * to be double word aligned. */
538 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
539 {
540 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
541 }
542 else
543 {
544 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
545 }
546
547 /* We ensure in pxPortInitialiseStack that the system call stack is
548 * double word aligned and therefore, there is no need of padding.
549 * Clear the bit[9] of stacked xPSR. */
550 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
551 }
552 }
553
554 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
555 /*-----------------------------------------------------------*/
556
557 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
558
vRequestSystemCallExit(void)559 void vRequestSystemCallExit( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
560 {
561 __asm volatile ( "svc %0 \n" ::"i" ( portSVC_SYSTEM_CALL_EXIT ) : "memory" );
562 }
563
564 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
565 /*-----------------------------------------------------------*/
566
567 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
568
vSystemCallExit(uint32_t * pulSystemCallStack,uint32_t ulLR)569 void vSystemCallExit( uint32_t * pulSystemCallStack,
570 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
571 {
572 extern TaskHandle_t pxCurrentTCB;
573 xMPU_SETTINGS * pxMpuSettings;
574 uint32_t * pulTaskStack;
575 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
576
577 #if defined( __ARMCC_VERSION )
578 /* Declaration when these variable are defined in code instead of being
579 * exported from linker scripts. */
580 extern uint32_t * __privileged_functions_start__;
581 extern uint32_t * __privileged_functions_end__;
582 #else
583 /* Declaration when these variable are exported from linker scripts. */
584 extern uint32_t __privileged_functions_start__[];
585 extern uint32_t __privileged_functions_end__[];
586 #endif /* #if defined( __ARMCC_VERSION ) */
587
588 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
589 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
590
591 /* Checks:
592 * 1. SVC is raised from the privileged code (i.e. application is not
593 * raising SVC directly). This SVC is only raised from
594 * vRequestSystemCallExit which is in the privileged code section.
595 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must not be NULL -
596 * this means that we previously entered a system call and the
597 * application is not attempting to exit without entering a system
598 * call.
599 */
600 if( ( ulSystemCallLocation >= ( uint32_t ) __privileged_functions_start__ ) &&
601 ( ulSystemCallLocation <= ( uint32_t ) __privileged_functions_end__ ) &&
602 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack != NULL ) )
603 {
604 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
605
606 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
607 {
608 /* Extended frame i.e. FPU in use. */
609 ulStackFrameSize = 26;
610 __asm volatile
611 (
612 " vpush {s0} \n" /* Trigger lazy stacking. */
613 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
614 ::: "memory"
615 );
616 }
617 else
618 {
619 /* Standard frame i.e. FPU not in use. */
620 ulStackFrameSize = 8;
621 }
622
623 /* Make space on the task stack for the stack frame. */
624 pulTaskStack = pulTaskStack - ulStackFrameSize;
625
626 /* Copy the stack frame. */
627 for( i = 0; i < ulStackFrameSize; i++ )
628 {
629 pulTaskStack[ i ] = pulSystemCallStack[ i ];
630 }
631
632 /* Use the pulTaskStack in thread mode. */
633 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
634
635 /* Drop the privilege before returning to the thread mode. */
636 __asm volatile
637 (
638 " mrs r1, control \n" /* Obtain current control value. */
639 " orr r1, #1 \n" /* Set nPRIV bit. */
640 " msr control, r1 \n" /* Write back new control value. */
641 ::: "r1", "memory"
642 );
643
644 /* Return to the caller of the System Call entry point (i.e. the
645 * caller of the MPU_<API>). */
646 pulTaskStack[ portOFFSET_TO_PC ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
647 /* Ensure that LR has a valid value.*/
648 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
649
650 /* If the hardware used padding to force the stack pointer
651 * to be double word aligned, set the stacked xPSR bit[9],
652 * otherwise clear it. */
653 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
654 {
655 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
656 }
657 else
658 {
659 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
660 }
661
662 /* This is not NULL only for the duration of the system call. */
663 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
664 }
665 }
666
667 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
668 /*-----------------------------------------------------------*/
669
xPortIsTaskPrivileged(void)670 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
671 {
672 BaseType_t xTaskIsPrivileged = pdFALSE;
673 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
674
675 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
676 {
677 xTaskIsPrivileged = pdTRUE;
678 }
679
680 return xTaskIsPrivileged;
681 }
682 /*-----------------------------------------------------------*/
683
684 /*
685 * See header file for description.
686 */
xPortStartScheduler(void)687 BaseType_t xPortStartScheduler( void )
688 {
689 /* Errata 837070 workaround must only be enabled on Cortex-M7 r0p0
690 * and r0p1 cores. */
691 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
692 configASSERT( ( portCPUID == portCORTEX_M7_r0p1_ID ) || ( portCPUID == portCORTEX_M7_r0p0_ID ) );
693 #else
694 /* When using this port on a Cortex-M7 r0p0 or r0p1 core, define
695 * configENABLE_ERRATA_837070_WORKAROUND to 1 in your
696 * FreeRTOSConfig.h. */
697 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
698 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
699 #endif
700
701 #if ( configASSERT_DEFINED == 1 )
702 {
703 volatile uint8_t ucOriginalPriority;
704 volatile uint32_t ulImplementedPrioBits = 0;
705 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
706 volatile uint8_t ucMaxPriorityValue;
707
708 /* Determine the maximum priority from which ISR safe FreeRTOS API
709 * functions can be called. ISR safe functions are those that end in
710 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
711 * ensure interrupt entry is as fast and simple as possible.
712 *
713 * Save the interrupt priority value that is about to be clobbered. */
714 ucOriginalPriority = *pucFirstUserPriorityRegister;
715
716 /* Determine the number of priority bits available. First write to all
717 * possible bits. */
718 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
719
720 /* Read the value back to see how many bits stuck. */
721 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
722
723 /* Use the same mask on the maximum system call priority. */
724 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
725
726 /* Check that the maximum system call priority is nonzero after
727 * accounting for the number of priority bits supported by the
728 * hardware. A priority of 0 is invalid because setting the BASEPRI
729 * register to 0 unmasks all interrupts, and interrupts with priority 0
730 * cannot be masked using BASEPRI.
731 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
732 configASSERT( ucMaxSysCallPriority );
733
734 /* Check that the bits not implemented in hardware are zero in
735 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
736 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
737
738 /* Calculate the maximum acceptable priority group value for the number
739 * of bits read back. */
740
741 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
742 {
743 ulImplementedPrioBits++;
744 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
745 }
746
747 if( ulImplementedPrioBits == 8 )
748 {
749 /* When the hardware implements 8 priority bits, there is no way for
750 * the software to configure PRIGROUP to not have sub-priorities. As
751 * a result, the least significant bit is always used for sub-priority
752 * and there are 128 preemption priorities and 2 sub-priorities.
753 *
754 * This may cause some confusion in some cases - for example, if
755 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
756 * priority interrupts will be masked in Critical Sections as those
757 * are at the same preemption priority. This may appear confusing as
758 * 4 is higher (numerically lower) priority than
759 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
760 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
761 * to 4, this confusion does not happen and the behaviour remains the same.
762 *
763 * The following assert ensures that the sub-priority bit in the
764 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
765 * confusion. */
766 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
767 ulMaxPRIGROUPValue = 0;
768 }
769 else
770 {
771 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
772 }
773
774 /* Shift the priority group value back to its position within the AIRCR
775 * register. */
776 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
777 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
778
779 /* Restore the clobbered interrupt priority register to its original
780 * value. */
781 *pucFirstUserPriorityRegister = ucOriginalPriority;
782 }
783 #endif /* configASSERT_DEFINED */
784
785 /* Make PendSV and SysTick the lowest priority interrupts. */
786 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
787 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
788
789 /* Configure the regions in the MPU that are common to all tasks. */
790 prvSetupMPU();
791
792 /* Start the timer that generates the tick ISR. Interrupts are disabled
793 * here already. */
794 vPortSetupTimerInterrupt();
795
796 /* Initialise the critical nesting count ready for the first task. */
797 uxCriticalNesting = 0;
798
799 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
800 {
801 xSchedulerRunning = pdTRUE;
802 }
803 #endif
804
805 /* Ensure the VFP is enabled - it should be anyway. */
806 vPortEnableVFP();
807
808 /* Lazy save always. */
809 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
810
811 /* Start the first task. */
812 vPortStartFirstTask();
813
814 /* Should not get here! */
815 return 0;
816 }
817 /*-----------------------------------------------------------*/
818
vPortEndScheduler(void)819 void vPortEndScheduler( void )
820 {
821 /* Not implemented in ports where there is nothing to return to.
822 * Artificially force an assert. */
823 configASSERT( uxCriticalNesting == 1000UL );
824 }
825 /*-----------------------------------------------------------*/
826
vPortEnterCritical(void)827 void vPortEnterCritical( void )
828 {
829 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
830 if( portIS_PRIVILEGED() == pdFALSE )
831 {
832 portRAISE_PRIVILEGE();
833 portMEMORY_BARRIER();
834
835 portDISABLE_INTERRUPTS();
836 uxCriticalNesting++;
837
838 /* This is not the interrupt safe version of the enter critical function so
839 * assert() if it is being called from an interrupt context. Only API
840 * functions that end in "FromISR" can be used in an interrupt. Only assert if
841 * the critical nesting count is 1 to protect against recursive calls if the
842 * assert function also uses a critical section. */
843 if( uxCriticalNesting == 1 )
844 {
845 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
846 }
847
848 portMEMORY_BARRIER();
849
850 portRESET_PRIVILEGE();
851 portMEMORY_BARRIER();
852 }
853 else
854 {
855 portDISABLE_INTERRUPTS();
856 uxCriticalNesting++;
857
858 /* This is not the interrupt safe version of the enter critical function so
859 * assert() if it is being called from an interrupt context. Only API
860 * functions that end in "FromISR" can be used in an interrupt. Only assert if
861 * the critical nesting count is 1 to protect against recursive calls if the
862 * assert function also uses a critical section. */
863 if( uxCriticalNesting == 1 )
864 {
865 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
866 }
867 }
868 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
869 portDISABLE_INTERRUPTS();
870 uxCriticalNesting++;
871
872 /* This is not the interrupt safe version of the enter critical function so
873 * assert() if it is being called from an interrupt context. Only API
874 * functions that end in "FromISR" can be used in an interrupt. Only assert if
875 * the critical nesting count is 1 to protect against recursive calls if the
876 * assert function also uses a critical section. */
877 if( uxCriticalNesting == 1 )
878 {
879 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
880 }
881 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
882 }
883 /*-----------------------------------------------------------*/
884
vPortExitCritical(void)885 void vPortExitCritical( void )
886 {
887 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
888 if( portIS_PRIVILEGED() == pdFALSE )
889 {
890 portRAISE_PRIVILEGE();
891 portMEMORY_BARRIER();
892
893 configASSERT( uxCriticalNesting );
894 uxCriticalNesting--;
895
896 if( uxCriticalNesting == 0 )
897 {
898 portENABLE_INTERRUPTS();
899 }
900
901 portMEMORY_BARRIER();
902
903 portRESET_PRIVILEGE();
904 portMEMORY_BARRIER();
905 }
906 else
907 {
908 configASSERT( uxCriticalNesting );
909 uxCriticalNesting--;
910
911 if( uxCriticalNesting == 0 )
912 {
913 portENABLE_INTERRUPTS();
914 }
915 }
916 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
917 configASSERT( uxCriticalNesting );
918 uxCriticalNesting--;
919
920 if( uxCriticalNesting == 0 )
921 {
922 portENABLE_INTERRUPTS();
923 }
924 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
925 }
926 /*-----------------------------------------------------------*/
927
xPortSysTickHandler(void)928 void xPortSysTickHandler( void )
929 {
930 /* The SysTick runs at the lowest interrupt priority, so when this interrupt
931 * executes all interrupts must be unmasked. There is therefore no need to
932 * save and then restore the interrupt mask value as its value is already
933 * known. */
934 portDISABLE_INTERRUPTS();
935 {
936 /* Increment the RTOS tick. */
937 if( xTaskIncrementTick() != pdFALSE )
938 {
939 /* A context switch is required. Context switching is performed in
940 * the PendSV interrupt. Pend the PendSV interrupt. */
941 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
942 }
943 }
944 portENABLE_INTERRUPTS();
945 }
946 /*-----------------------------------------------------------*/
947
948 /*
949 * Setup the systick timer to generate the tick interrupts at the required
950 * frequency.
951 */
vPortSetupTimerInterrupt(void)952 __weak void vPortSetupTimerInterrupt( void )
953 {
954 /* Stop and clear the SysTick. */
955 portNVIC_SYSTICK_CTRL_REG = 0UL;
956 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
957
958 /* Configure SysTick to interrupt at the requested rate. */
959 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
960 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
961 }
962 /*-----------------------------------------------------------*/
963
prvSetupMPU(void)964 static void prvSetupMPU( void )
965 {
966 extern uint32_t __privileged_functions_start__[];
967 extern uint32_t __privileged_functions_end__[];
968 extern uint32_t __FLASH_segment_start__[];
969 extern uint32_t __FLASH_segment_end__[];
970 extern uint32_t __privileged_data_start__[];
971 extern uint32_t __privileged_data_end__[];
972
973 /* The only permitted number of regions are 8 or 16. */
974 configASSERT( ( configTOTAL_MPU_REGIONS == 8 ) || ( configTOTAL_MPU_REGIONS == 16 ) );
975
976 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
977 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
978
979 /* Check the expected MPU is present. */
980 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
981 {
982 /* First setup the unprivileged flash for unprivileged read only access. */
983 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
984 ( portMPU_REGION_VALID ) |
985 ( portUNPRIVILEGED_FLASH_REGION );
986
987 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
988 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
989 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
990 ( portMPU_REGION_ENABLE );
991
992 /* Setup the privileged flash for privileged only access. This is where
993 * the kernel code is placed. */
994 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
995 ( portMPU_REGION_VALID ) |
996 ( portPRIVILEGED_FLASH_REGION );
997
998 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
999 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1000 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1001 ( portMPU_REGION_ENABLE );
1002
1003 /* Setup the privileged data RAM region. This is where the kernel data
1004 * is placed. */
1005 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1006 ( portMPU_REGION_VALID ) |
1007 ( portPRIVILEGED_RAM_REGION );
1008
1009 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1010 ( portMPU_REGION_EXECUTE_NEVER ) |
1011 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1012 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1013 ( portMPU_REGION_ENABLE );
1014
1015 /* By default allow everything to access the general peripherals. The
1016 * system peripherals and registers are protected. */
1017 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1018 ( portMPU_REGION_VALID ) |
1019 ( portGENERAL_PERIPHERALS_REGION );
1020
1021 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1022 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1023 ( portMPU_REGION_ENABLE );
1024
1025 /* Enable the memory fault exception. */
1026 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1027
1028 /* Enable the MPU with the background region configured. */
1029 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1030 }
1031 }
1032 /*-----------------------------------------------------------*/
1033
prvGetMPURegionSizeSetting(uint32_t ulActualSizeInBytes)1034 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1035 {
1036 uint32_t ulRegionSize, ulReturnValue = 4;
1037
1038 /* 32 is the smallest region size, 31 is the largest valid value for
1039 * ulReturnValue. */
1040 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1041 {
1042 if( ulActualSizeInBytes <= ulRegionSize )
1043 {
1044 break;
1045 }
1046 else
1047 {
1048 ulReturnValue++;
1049 }
1050 }
1051
1052 /* Shift the code by one before returning so it can be written directly
1053 * into the the correct bit position of the attribute register. */
1054 return( ulReturnValue << 1UL );
1055 }
1056 /*-----------------------------------------------------------*/
1057
vPortStoreTaskMPUSettings(xMPU_SETTINGS * xMPUSettings,const struct xMEMORY_REGION * const xRegions,StackType_t * pxBottomOfStack,uint32_t ulStackDepth)1058 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1059 const struct xMEMORY_REGION * const xRegions,
1060 StackType_t * pxBottomOfStack,
1061 uint32_t ulStackDepth )
1062 {
1063 extern uint32_t __SRAM_segment_start__[];
1064 extern uint32_t __SRAM_segment_end__[];
1065 extern uint32_t __privileged_data_start__[];
1066 extern uint32_t __privileged_data_end__[];
1067 int32_t lIndex;
1068 uint32_t ul;
1069
1070 if( xRegions == NULL )
1071 {
1072 /* No MPU regions are specified so allow access to all RAM. */
1073 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1074 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1075 ( portMPU_REGION_VALID ) |
1076 ( portSTACK_REGION ); /* Region number. */
1077
1078 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1079 ( portMPU_REGION_READ_WRITE ) |
1080 ( portMPU_REGION_EXECUTE_NEVER ) |
1081 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1082 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1083 ( portMPU_REGION_ENABLE );
1084
1085 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1086 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1087 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1088 tskMPU_WRITE_PERMISSION );
1089
1090 /* Invalidate user configurable regions. */
1091 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1092 {
1093 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1094 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1095 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1096 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1097 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1098 }
1099 }
1100 else
1101 {
1102 /* This function is called automatically when the task is created - in
1103 * which case the stack region parameters will be valid. At all other
1104 * times the stack parameters will not be valid and it is assumed that the
1105 * stack region has already been configured. */
1106 if( ulStackDepth > 0 )
1107 {
1108 /* Define the region that allows access to the stack. */
1109 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1110 ( ( uint32_t ) pxBottomOfStack ) |
1111 ( portMPU_REGION_VALID ) |
1112 ( portSTACK_REGION ); /* Region number. */
1113
1114 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1115 ( portMPU_REGION_READ_WRITE ) |
1116 ( portMPU_REGION_EXECUTE_NEVER ) |
1117 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1118 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1119 ( portMPU_REGION_ENABLE );
1120
1121 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1122 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1123 ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1UL );
1124 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1125 tskMPU_WRITE_PERMISSION );
1126 }
1127
1128 lIndex = 0;
1129
1130 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1131 {
1132 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1133 {
1134 /* Translate the generic region definition contained in
1135 * xRegions into the CM4 specific MPU settings that are then
1136 * stored in xMPUSettings. */
1137 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1138 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1139 ( portMPU_REGION_VALID ) |
1140 ( ul - 1UL ); /* Region number. */
1141
1142 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1143 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1144 ( xRegions[ lIndex ].ulParameters ) |
1145 ( portMPU_REGION_ENABLE );
1146
1147 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1148 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1149 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1150
1151 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1152 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1153 {
1154 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1155 }
1156
1157 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1158 {
1159 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1160 }
1161 }
1162 else
1163 {
1164 /* Invalidate the region. */
1165 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1166 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1167 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1168 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1169 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1170 }
1171
1172 lIndex++;
1173 }
1174 }
1175 }
1176 /*-----------------------------------------------------------*/
1177
xPortIsAuthorizedToAccessBuffer(const void * pvBuffer,uint32_t ulBufferLength,uint32_t ulAccessRequested)1178 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1179 uint32_t ulBufferLength,
1180 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1181
1182 {
1183 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1184 BaseType_t xAccessGranted = pdFALSE;
1185 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1186
1187 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1188 {
1189 xAccessGranted = pdTRUE;
1190 }
1191 else
1192 {
1193 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1194 {
1195 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1196 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1197
1198 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1199 {
1200 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1201 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1202 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1203 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1204 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1205 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1206 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1207 {
1208 xAccessGranted = pdTRUE;
1209 break;
1210 }
1211 }
1212 }
1213 }
1214
1215 return xAccessGranted;
1216 }
1217 /*-----------------------------------------------------------*/
1218
1219
1220 #if ( configASSERT_DEFINED == 1 )
1221
vPortValidateInterruptPriority(void)1222 void vPortValidateInterruptPriority( void )
1223 {
1224 uint32_t ulCurrentInterrupt;
1225 uint8_t ucCurrentPriority;
1226
1227 /* Obtain the number of the currently executing interrupt. */
1228 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1229
1230 /* Is the interrupt number a user defined interrupt? */
1231 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1232 {
1233 /* Look up the interrupt's priority. */
1234 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1235
1236 /* The following assertion will fail if a service routine (ISR) for
1237 * an interrupt that has been assigned a priority above
1238 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1239 * function. ISR safe FreeRTOS API functions must *only* be called
1240 * from interrupts that have been assigned a priority at or below
1241 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1242 *
1243 * Numerically low interrupt priority numbers represent logically high
1244 * interrupt priorities, therefore the priority of the interrupt must
1245 * be set to a value equal to or numerically *higher* than
1246 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1247 *
1248 * Interrupts that use the FreeRTOS API must not be left at their
1249 * default priority of zero as that is the highest possible priority,
1250 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1251 * and therefore also guaranteed to be invalid.
1252 *
1253 * FreeRTOS maintains separate thread and ISR API functions to ensure
1254 * interrupt entry is as fast and simple as possible.
1255 *
1256 * The following links provide detailed information:
1257 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1258 * https://www.FreeRTOS.org/FAQHelp.html */
1259 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1260 }
1261
1262 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1263 * that define each interrupt's priority to be split between bits that
1264 * define the interrupt's pre-emption priority bits and bits that define
1265 * the interrupt's sub-priority. For simplicity all bits must be defined
1266 * to be pre-emption priority bits. The following assertion will fail if
1267 * this is not the case (if some bits represent a sub-priority).
1268 *
1269 * If the application only uses CMSIS libraries for interrupt
1270 * configuration then the correct setting can be achieved on all Cortex-M
1271 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1272 * scheduler. Note however that some vendor specific peripheral libraries
1273 * assume a non-zero priority group setting, in which cases using a value
1274 * of zero will result in unpredictable behaviour. */
1275 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1276 }
1277
1278 #endif /* configASSERT_DEFINED */
1279 /*-----------------------------------------------------------*/
1280
1281 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1282
vPortGrantAccessToKernelObject(TaskHandle_t xInternalTaskHandle,int32_t lInternalIndexOfKernelObject)1283 void vPortGrantAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1284 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1285 {
1286 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1287 xMPU_SETTINGS * xTaskMpuSettings;
1288
1289 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1290 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1291
1292 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1293
1294 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] |= ( 1U << ulAccessControlListEntryBit );
1295 }
1296
1297 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1298 /*-----------------------------------------------------------*/
1299
1300 #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1301
vPortRevokeAccessToKernelObject(TaskHandle_t xInternalTaskHandle,int32_t lInternalIndexOfKernelObject)1302 void vPortRevokeAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1303 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1304 {
1305 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1306 xMPU_SETTINGS * xTaskMpuSettings;
1307
1308 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1309 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1310
1311 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1312
1313 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] &= ~( 1U << ulAccessControlListEntryBit );
1314 }
1315
1316 #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1317 /*-----------------------------------------------------------*/
1318
1319 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1320
1321 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
1322
xPortIsAuthorizedToAccessKernelObject(int32_t lInternalIndexOfKernelObject)1323 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1324 {
1325 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1326 BaseType_t xAccessGranted = pdFALSE;
1327 const xMPU_SETTINGS * xTaskMpuSettings;
1328
1329 if( xSchedulerRunning == pdFALSE )
1330 {
1331 /* Grant access to all the kernel objects before the scheduler
1332 * is started. It is necessary because there is no task running
1333 * yet and therefore, we cannot use the permissions of any
1334 * task. */
1335 xAccessGranted = pdTRUE;
1336 }
1337 else
1338 {
1339 xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1340
1341 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1342 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1343
1344 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1345 {
1346 xAccessGranted = pdTRUE;
1347 }
1348 else
1349 {
1350 if( ( xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] & ( 1U << ulAccessControlListEntryBit ) ) != 0 )
1351 {
1352 xAccessGranted = pdTRUE;
1353 }
1354 }
1355 }
1356
1357 return xAccessGranted;
1358 }
1359
1360 #else /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1361
xPortIsAuthorizedToAccessKernelObject(int32_t lInternalIndexOfKernelObject)1362 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1363 {
1364 ( void ) lInternalIndexOfKernelObject;
1365
1366 /* If Access Control List feature is not used, all the tasks have
1367 * access to all the kernel objects. */
1368 return pdTRUE;
1369 }
1370
1371 #endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1372
1373 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1374 /*-----------------------------------------------------------*/
1375