]> begriffs open source - freertos/blob - portable/IAR/ARM_CM23/non_secure/portmacrocommon.h
Fix the context array size for MPU ports (#1230)
[freertos] / portable / IAR / ARM_CM23 / non_secure / portmacrocommon.h
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  * Copyright 2024 Arm Limited and/or its affiliates
5  * <open-source-office@arm.com>
6  *
7  * SPDX-License-Identifier: MIT
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of
10  * this software and associated documentation files (the "Software"), to deal in
11  * the Software without restriction, including without limitation the rights to
12  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13  * the Software, and to permit persons to whom the Software is furnished to do so,
14  * subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in all
17  * copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * https://www.FreeRTOS.org
27  * https://github.com/FreeRTOS
28  *
29  */
30
31 #ifndef PORTMACROCOMMON_H
32 #define PORTMACROCOMMON_H
33
34 /* *INDENT-OFF* */
35 #ifdef __cplusplus
36     extern "C" {
37 #endif
38 /* *INDENT-ON* */
39
40 /*------------------------------------------------------------------------------
41  * Port specific definitions.
42  *
43  * The settings in this file configure FreeRTOS correctly for the given hardware
44  * and compiler.
45  *
46  * These settings should not be altered.
47  *------------------------------------------------------------------------------
48  */
49
50 #ifndef configENABLE_FPU
51     #error configENABLE_FPU must be defined in FreeRTOSConfig.h.  Set configENABLE_FPU to 1 to enable the FPU or 0 to disable the FPU.
52 #endif /* configENABLE_FPU */
53
54 #ifndef configENABLE_MPU
55     #error configENABLE_MPU must be defined in FreeRTOSConfig.h.  Set configENABLE_MPU to 1 to enable the MPU or 0 to disable the MPU.
56 #endif /* configENABLE_MPU */
57
58 #ifndef configENABLE_TRUSTZONE
59     #error configENABLE_TRUSTZONE must be defined in FreeRTOSConfig.h.  Set configENABLE_TRUSTZONE to 1 to enable TrustZone or 0 to disable TrustZone.
60 #endif /* configENABLE_TRUSTZONE */
61
62 /*-----------------------------------------------------------*/
63
64 /**
65  * @brief Type definitions.
66  */
67 #define portCHAR          char
68 #define portFLOAT         float
69 #define portDOUBLE        double
70 #define portLONG          long
71 #define portSHORT         short
72 #define portSTACK_TYPE    uint32_t
73 #define portBASE_TYPE     long
74
75 typedef portSTACK_TYPE   StackType_t;
76 typedef long             BaseType_t;
77 typedef unsigned long    UBaseType_t;
78
79 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
80     typedef uint16_t     TickType_t;
81     #define portMAX_DELAY              ( TickType_t ) 0xffff
82 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
83     typedef uint32_t     TickType_t;
84     #define portMAX_DELAY              ( TickType_t ) 0xffffffffUL
85
86 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
87  * not need to be guarded with a critical section. */
88     #define portTICK_TYPE_IS_ATOMIC    1
89 #else
90     #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
91 #endif
92 /*-----------------------------------------------------------*/
93
94 /**
95  * Architecture specifics.
96  */
97 #define portSTACK_GROWTH                   ( -1 )
98 #define portTICK_PERIOD_MS                 ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
99 #define portBYTE_ALIGNMENT                 8
100 #define portNOP()
101 #define portINLINE                         __inline
102 #ifndef portFORCE_INLINE
103     #define portFORCE_INLINE               inline __attribute__( ( always_inline ) )
104 #endif
105 #define portHAS_STACK_OVERFLOW_CHECKING    1
106 /*-----------------------------------------------------------*/
107
108 /**
109  * @brief Extern declarations.
110  */
111 extern BaseType_t xPortIsInsideInterrupt( void );
112
113 extern void vPortYield( void ) /* PRIVILEGED_FUNCTION */;
114
115 extern void vPortEnterCritical( void ) /* PRIVILEGED_FUNCTION */;
116 extern void vPortExitCritical( void ) /* PRIVILEGED_FUNCTION */;
117
118 extern uint32_t ulSetInterruptMask( void ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */;
119 extern void vClearInterruptMask( uint32_t ulMask ) /* __attribute__(( naked )) PRIVILEGED_FUNCTION */;
120
121 #if ( configENABLE_TRUSTZONE == 1 )
122     extern void vPortAllocateSecureContext( uint32_t ulSecureStackSize ); /* __attribute__ (( naked )) */
123     extern void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */;
124 #endif /* configENABLE_TRUSTZONE */
125
126 #if ( configENABLE_MPU == 1 )
127     extern BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */;
128     extern void vResetPrivilege( void ) /* __attribute__ (( naked )) */;
129 #endif /* configENABLE_MPU */
130
131 #if ( configENABLE_PAC == 1 )
132
133     /**
134      * @brief Generates 128-bit task's random PAC key.
135      *
136      * @param[out] pulTaskPacKey Pointer to a 4-word (128-bits) array to be
137      *             filled with a 128-bit random number.
138      */
139     void vApplicationGenerateTaskRandomPacKey( uint32_t * pulTaskPacKey );
140
141 #endif /* configENABLE_PAC */
142 /*-----------------------------------------------------------*/
143
144 /**
145  * @brief MPU specific constants.
146  */
147 #if ( configENABLE_MPU == 1 )
148     #define portUSING_MPU_WRAPPERS    1
149     #define portPRIVILEGE_BIT         ( 0x80000000UL )
150 #else
151     #define portPRIVILEGE_BIT         ( 0x0UL )
152 #endif /* configENABLE_MPU */
153
154 /* MPU settings that can be overridden in FreeRTOSConfig.h. */
155 #ifndef configTOTAL_MPU_REGIONS
156     /* Define to 8 for backward compatibility. */
157     #define configTOTAL_MPU_REGIONS    ( 8UL )
158 #endif
159
160 /* MPU regions. */
161 #define portPRIVILEGED_FLASH_REGION                   ( 0UL )
162 #define portUNPRIVILEGED_FLASH_REGION                 ( 1UL )
163 #define portUNPRIVILEGED_SYSCALLS_REGION              ( 2UL )
164 #define portPRIVILEGED_RAM_REGION                     ( 3UL )
165 #define portSTACK_REGION                              ( 4UL )
166 #define portFIRST_CONFIGURABLE_REGION                 ( 5UL )
167 #define portLAST_CONFIGURABLE_REGION                  ( configTOTAL_MPU_REGIONS - 1UL )
168 #define portNUM_CONFIGURABLE_REGIONS                  ( ( portLAST_CONFIGURABLE_REGION - portFIRST_CONFIGURABLE_REGION ) + 1 )
169 #define portTOTAL_NUM_REGIONS                         ( portNUM_CONFIGURABLE_REGIONS + 1 )       /* Plus one to make space for the stack region. */
170
171 /* Device memory attributes used in MPU_MAIR registers.
172  *
173  * 8-bit values encoded as follows:
174  *  Bit[7:4] - 0000 - Device Memory
175  *  Bit[3:2] - 00 --> Device-nGnRnE
176  *              01 --> Device-nGnRE
177  *              10 --> Device-nGRE
178  *              11 --> Device-GRE
179  *  Bit[1:0] - 00, Reserved.
180  */
181 #define portMPU_DEVICE_MEMORY_nGnRnE                  ( 0x00 )       /* 0000 0000 */
182 #define portMPU_DEVICE_MEMORY_nGnRE                   ( 0x04 )       /* 0000 0100 */
183 #define portMPU_DEVICE_MEMORY_nGRE                    ( 0x08 )       /* 0000 1000 */
184 #define portMPU_DEVICE_MEMORY_GRE                     ( 0x0C )       /* 0000 1100 */
185
186 /* Normal memory attributes used in MPU_MAIR registers. */
187 #define portMPU_NORMAL_MEMORY_NON_CACHEABLE           ( 0x44 )       /* Non-cacheable. */
188 #define portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE    ( 0xFF )       /* Non-Transient, Write-back, Read-Allocate and Write-Allocate. */
189
190 /* Attributes used in MPU_RBAR registers. */
191 #define portMPU_REGION_NON_SHAREABLE                  ( 0UL << 3UL )
192 #define portMPU_REGION_INNER_SHAREABLE                ( 1UL << 3UL )
193 #define portMPU_REGION_OUTER_SHAREABLE                ( 2UL << 3UL )
194
195 #define portMPU_REGION_PRIVILEGED_READ_WRITE          ( 0UL << 1UL )
196 #define portMPU_REGION_READ_WRITE                     ( 1UL << 1UL )
197 #define portMPU_REGION_PRIVILEGED_READ_ONLY           ( 2UL << 1UL )
198 #define portMPU_REGION_READ_ONLY                      ( 3UL << 1UL )
199
200 #define portMPU_REGION_EXECUTE_NEVER                  ( 1UL )
201 /*-----------------------------------------------------------*/
202
203 #if ( configENABLE_MPU == 1 )
204
205     /**
206      * @brief Settings to define an MPU region.
207      */
208     typedef struct MPURegionSettings
209     {
210         uint32_t ulRBAR; /**< RBAR for the region. */
211         uint32_t ulRLAR; /**< RLAR for the region. */
212     } MPURegionSettings_t;
213
214     #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
215
216         #ifndef configSYSTEM_CALL_STACK_SIZE
217             #error configSYSTEM_CALL_STACK_SIZE must be defined to the desired size of the system call stack in words for using MPU wrappers v2.
218         #endif
219
220         /* When MPU wrapper v2 is used, the task's context is stored in TCB and
221          * pxTopOfStack member of TCB points to the context location in TCB. We,
222          * therefore, need to read PSP to find the task's current top of stack. */
223         #define portGET_CURRENT_TOP_OF_STACK( pxCurrentTopOfStack ) { __asm volatile ( "mrs %0, psp"  : "=r" ( pxCurrentTopOfStack ) ); }
224
225         /**
226          * @brief System call stack.
227          */
228         typedef struct SYSTEM_CALL_STACK_INFO
229         {
230             uint32_t ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE ];
231             uint32_t * pulSystemCallStack;
232             uint32_t * pulSystemCallStackLimit;
233             uint32_t * pulTaskStack;
234             uint32_t ulLinkRegisterAtSystemCallEntry;
235             uint32_t ulStackLimitRegisterAtSystemCallEntry;
236         } xSYSTEM_CALL_STACK_INFO;
237
238     #endif /* configUSE_MPU_WRAPPERS_V1 == 0 */
239
240     /**
241      * @brief MPU settings as stored in the TCB.
242      */
243     #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
244
245         #if ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 1 ) )
246
247             /*
248              * +-----------+---------------+----------+-----------------+------------------------------+------------+-----+
249              * |  s16-s31  | s0-s15, FPSCR |  r4-r11  | r0-r3, r12, LR, | xSecureContext, PSP, PSPLIM, | TaskPacKey |     |
250              * |           |               |          | PC, xPSR        | CONTROL, EXC_RETURN          |            |     |
251              * +-----------+---------------+----------+-----------------+------------------------------+------------+-----+
252              *
253              * <-----------><--------------><---------><----------------><-----------------------------><-----------><---->
254              *      16             17            8               8                     5                     16         1
255              */
256             #define MAX_CONTEXT_SIZE    71
257
258         #elif ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 0 ) )
259
260             /*
261              * +-----------+---------------+----------+-----------------+------------------------------+-----+
262              * |  s16-s31  | s0-s15, FPSCR |  r4-r11  | r0-r3, r12, LR, | xSecureContext, PSP, PSPLIM, |     |
263              * |           |               |          | PC, xPSR        | CONTROL, EXC_RETURN          |     |
264              * +-----------+---------------+----------+-----------------+------------------------------+-----+
265              *
266              * <-----------><--------------><---------><----------------><-----------------------------><---->
267              *      16             17            8               8                     5                   1
268              */
269             #define MAX_CONTEXT_SIZE    55
270
271         #elif ( ( configENABLE_TRUSTZONE == 0 ) && ( configENABLE_PAC == 1 ) )
272
273             /*
274              * +-----------+---------------+----------+-----------------+----------------------+------------+-----+
275              * |  s16-s31  | s0-s15, FPSCR |  r4-r11  | r0-r3, r12, LR, | PSP, PSPLIM, CONTROL | TaskPacKey |     |
276              * |           |               |          | PC, xPSR        | EXC_RETURN           |            |     |
277              * +-----------+---------------+----------+-----------------+----------------------+------------+-----+
278              *
279              * <-----------><--------------><---------><----------------><---------------------><-----------><---->
280              *      16             17            8               8                  4                16         1
281              */
282             #define MAX_CONTEXT_SIZE    70
283
284         #else /* if ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 1 ) ) */
285
286             /*
287              * +-----------+---------------+----------+-----------------+----------------------+-----+
288              * |  s16-s31  | s0-s15, FPSCR |  r4-r11  | r0-r3, r12, LR, | PSP, PSPLIM, CONTROL |     |
289              * |           |               |          | PC, xPSR        | EXC_RETURN           |     |
290              * +-----------+---------------+----------+-----------------+----------------------+-----+
291              *
292              * <-----------><--------------><---------><----------------><---------------------><---->
293              *      16             17            8               8                  4              1
294              */
295             #define MAX_CONTEXT_SIZE    54
296
297         #endif /* #if ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 1 ) ) */
298
299     #else /* #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) ) */
300
301         #if ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 1 ) )
302
303             /*
304              * +----------+-----------------+------------------------------+------------+-----+
305              * |  r4-r11  | r0-r3, r12, LR, | xSecureContext, PSP, PSPLIM, | TaskPacKey |     |
306              * |          | PC, xPSR        | CONTROL, EXC_RETURN          |            |     |
307              * +----------+-----------------+------------------------------+------------+-----+
308              *
309              * <---------><----------------><------------------------------><-----------><---->
310              *     8               8                      5                      16         1
311              */
312             #define MAX_CONTEXT_SIZE    38
313
314         #elif ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 0 ) )
315
316             /*
317              * +----------+-----------------+------------------------------+-----+
318              * |  r4-r11  | r0-r3, r12, LR, | xSecureContext, PSP, PSPLIM, |     |
319              * |          | PC, xPSR        | CONTROL, EXC_RETURN          |     |
320              * +----------+-----------------+------------------------------+-----+
321              *
322              * <---------><----------------><------------------------------><---->
323              *     8               8                      5                   1
324              */
325             #define MAX_CONTEXT_SIZE    22
326
327         #elif ( ( configENABLE_TRUSTZONE == 0 ) && ( configENABLE_PAC == 1 ) )
328
329             /*
330              * +----------+-----------------+----------------------+------------+-----+
331              * |  r4-r11  | r0-r3, r12, LR, | PSP, PSPLIM, CONTROL | TaskPacKey |     |
332              * |          | PC, xPSR        | EXC_RETURN           |            |     |
333              * +----------+-----------------+----------------------+------------+-----+
334              *
335              * <---------><----------------><----------------------><-----------><---->
336              *     8               8                  4                  16         1
337              */
338             #define MAX_CONTEXT_SIZE    37
339
340         #else /* #if( configENABLE_TRUSTZONE == 1 ) */
341
342             /*
343              * +----------+-----------------+----------------------+-----+
344              * |  r4-r11  | r0-r3, r12, LR, | PSP, PSPLIM, CONTROL |     |
345              * |          | PC, xPSR        | EXC_RETURN           |     |
346              * +----------+-----------------+----------------------+-----+
347              *
348              * <---------><----------------><----------------------><---->
349              *     8               8                  4              1
350              */
351             #define MAX_CONTEXT_SIZE    21
352
353         #endif /* #if ( ( configENABLE_TRUSTZONE == 1 ) && ( configENABLE_PAC == 1 ) ) */
354
355     #endif /* #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) ) */
356
357     /* Flags used for xMPU_SETTINGS.ulTaskFlags member. */
358     #define portSTACK_FRAME_HAS_PADDING_FLAG    ( 1UL << 0UL )
359     #define portTASK_IS_PRIVILEGED_FLAG         ( 1UL << 1UL )
360
361     /* Size of an Access Control List (ACL) entry in bits. */
362     #define portACL_ENTRY_SIZE_BITS             ( 32U )
363
364     typedef struct MPU_SETTINGS
365     {
366         uint32_t ulMAIR0;                                              /**< MAIR0 for the task containing attributes for all the 4 per task regions. */
367         MPURegionSettings_t xRegionsSettings[ portTOTAL_NUM_REGIONS ]; /**< Settings for 4 per task regions. */
368         uint32_t ulContext[ MAX_CONTEXT_SIZE ];
369         uint32_t ulTaskFlags;
370
371         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
372             xSYSTEM_CALL_STACK_INFO xSystemCallStackInfo;
373             #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
374                 uint32_t ulAccessControlList[ ( configPROTECTED_KERNEL_OBJECT_POOL_SIZE / portACL_ENTRY_SIZE_BITS ) + 1 ];
375             #endif
376         #endif
377     } xMPU_SETTINGS;
378
379 #endif /* configENABLE_MPU == 1 */
380 /*-----------------------------------------------------------*/
381
382 /**
383  * @brief Validate priority of ISRs that are allowed to call FreeRTOS
384  * system calls.
385  */
386 #if ( configASSERT_DEFINED == 1 )
387     #if ( portHAS_ARMV8M_MAIN_EXTENSION == 1 )
388         void vPortValidateInterruptPriority( void );
389         #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID()    vPortValidateInterruptPriority()
390     #endif
391 #endif
392
393 /**
394  * @brief SVC numbers.
395  */
396 #define portSVC_ALLOCATE_SECURE_CONTEXT    100
397 #define portSVC_FREE_SECURE_CONTEXT        101
398 #define portSVC_START_SCHEDULER            102
399 #define portSVC_RAISE_PRIVILEGE            103
400 #define portSVC_SYSTEM_CALL_EXIT           104
401 #define portSVC_YIELD                      105
402 /*-----------------------------------------------------------*/
403
404 /**
405  * @brief Scheduler utilities.
406  */
407 #if ( configENABLE_MPU == 1 )
408     #define portYIELD()               __asm volatile ( "svc %0" ::"i" ( portSVC_YIELD ) : "memory" )
409     #define portYIELD_WITHIN_API()    vPortYield()
410 #else
411     #define portYIELD()               vPortYield()
412     #define portYIELD_WITHIN_API()    vPortYield()
413 #endif
414
415 #define portNVIC_INT_CTRL_REG     ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
416 #define portNVIC_PENDSVSET_BIT    ( 1UL << 28UL )
417 #define portEND_SWITCHING_ISR( xSwitchRequired )            \
418     do                                                      \
419     {                                                       \
420         if( xSwitchRequired )                               \
421         {                                                   \
422             traceISR_EXIT_TO_SCHEDULER();                   \
423             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
424         }                                                   \
425         else                                                \
426         {                                                   \
427             traceISR_EXIT();                                \
428         }                                                   \
429     } while( 0 )
430 #define portYIELD_FROM_ISR( x )    portEND_SWITCHING_ISR( x )
431 /*-----------------------------------------------------------*/
432
433 /**
434  * @brief Critical section management.
435  */
436 #define portSET_INTERRUPT_MASK_FROM_ISR()         ulSetInterruptMask()
437 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x )    vClearInterruptMask( x )
438 #define portENTER_CRITICAL()                      vPortEnterCritical()
439 #define portEXIT_CRITICAL()                       vPortExitCritical()
440 /*-----------------------------------------------------------*/
441
442 /**
443  * @brief Tickless idle/low power functionality.
444  */
445 #ifndef portSUPPRESS_TICKS_AND_SLEEP
446     extern void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
447     #define portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime )    vPortSuppressTicksAndSleep( xExpectedIdleTime )
448 #endif
449 /*-----------------------------------------------------------*/
450
451 /**
452  * @brief Task function macros as described on the FreeRTOS.org WEB site.
453  */
454 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )
455 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
456 /*-----------------------------------------------------------*/
457
458 #if ( configENABLE_TRUSTZONE == 1 )
459
460 /**
461  * @brief Allocate a secure context for the task.
462  *
463  * Tasks are not created with a secure context. Any task that is going to call
464  * secure functions must call portALLOCATE_SECURE_CONTEXT() to allocate itself a
465  * secure context before it calls any secure function.
466  *
467  * @param[in] ulSecureStackSize The size of the secure stack to be allocated.
468  */
469     #define portALLOCATE_SECURE_CONTEXT( ulSecureStackSize )    vPortAllocateSecureContext( ulSecureStackSize )
470
471 /**
472  * @brief Called when a task is deleted to delete the task's secure context,
473  * if it has one.
474  *
475  * @param[in] pxTCB The TCB of the task being deleted.
476  */
477     #define portCLEAN_UP_TCB( pxTCB )                           vPortFreeSecureContext( ( uint32_t * ) pxTCB )
478 #endif /* configENABLE_TRUSTZONE */
479 /*-----------------------------------------------------------*/
480
481 #if ( configENABLE_MPU == 1 )
482
483 /**
484  * @brief Checks whether or not the processor is privileged.
485  *
486  * @return 1 if the processor is already privileged, 0 otherwise.
487  */
488     #define portIS_PRIVILEGED()      xIsPrivileged()
489
490 /**
491  * @brief Raise an SVC request to raise privilege.
492  *
493  * The SVC handler checks that the SVC was raised from a system call and only
494  * then it raises the privilege. If this is called from any other place,
495  * the privilege is not raised.
496  */
497     #define portRAISE_PRIVILEGE()    __asm volatile ( "svc %0 \n" ::"i" ( portSVC_RAISE_PRIVILEGE ) : "memory" );
498
499 /**
500  * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
501  * register.
502  */
503     #define portRESET_PRIVILEGE()    vResetPrivilege()
504 #else
505     #define portIS_PRIVILEGED()
506     #define portRAISE_PRIVILEGE()
507     #define portRESET_PRIVILEGE()
508 #endif /* configENABLE_MPU */
509 /*-----------------------------------------------------------*/
510
511 #if ( configENABLE_MPU == 1 )
512
513     extern BaseType_t xPortIsTaskPrivileged( void );
514
515 /**
516  * @brief Checks whether or not the calling task is privileged.
517  *
518  * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
519  */
520     #define portIS_TASK_PRIVILEGED()    xPortIsTaskPrivileged()
521
522 #endif /* configENABLE_MPU == 1 */
523 /*-----------------------------------------------------------*/
524
525 /**
526  * @brief Barriers.
527  */
528 #define portMEMORY_BARRIER()    __asm volatile ( "" ::: "memory" )
529 /*-----------------------------------------------------------*/
530
531 /* Select correct value of configUSE_PORT_OPTIMISED_TASK_SELECTION
532  * based on whether or not Mainline extension is implemented. */
533 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
534     #if ( portHAS_ARMV8M_MAIN_EXTENSION == 1 )
535         #define configUSE_PORT_OPTIMISED_TASK_SELECTION    1
536     #else
537         #define configUSE_PORT_OPTIMISED_TASK_SELECTION    0
538     #endif
539 #endif /* #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION */
540
541 /**
542  * @brief Port-optimised task selection.
543  */
544 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 1 )
545
546 /**
547  * @brief Count the number of leading zeros in a 32-bit value.
548  */
549     static portFORCE_INLINE uint32_t ulPortCountLeadingZeros( uint32_t ulBitmap )
550     {
551         uint32_t ulReturn;
552
553         __asm volatile ( "clz %0, %1" : "=r" ( ulReturn ) : "r" ( ulBitmap ) : "memory" );
554
555         return ulReturn;
556     }
557
558 /* Check the configuration. */
559     #if ( configMAX_PRIORITIES > 32 )
560         #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32.  It is very rare that a system requires more than 10 to 15 different priorities as tasks that share a priority will time slice.
561     #endif
562
563     #if ( portHAS_ARMV8M_MAIN_EXTENSION == 0 )
564         #error ARMv8-M baseline implementations (such as Cortex-M23) do not support port-optimised task selection.  Please set configUSE_PORT_OPTIMISED_TASK_SELECTION to 0 or leave it undefined.
565     #endif
566
567 /**
568  * @brief Store/clear the ready priorities in a bit map.
569  */
570     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities )      ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
571     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities )       ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
572
573 /**
574  * @brief Get the priority of the highest-priority task that is ready to execute.
575  */
576     #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    uxTopPriority = ( 31UL - ulPortCountLeadingZeros( ( uxReadyPriorities ) ) )
577
578 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
579 /*-----------------------------------------------------------*/
580
581 /* *INDENT-OFF* */
582 #ifdef __cplusplus
583     }
584 #endif
585 /* *INDENT-ON* */
586
587 #endif /* PORTMACROCOMMON_H */