|
1 | 1 | /*
|
2 | 2 | * FreeRTOS Kernel <DEVELOPMENT BRANCH>
|
3 | 3 | * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
| 4 | + * Copyright 2024 Arm Limited and/or its affiliates |
| 5 | + |
4 | 6 | *
|
5 | 7 | * SPDX-License-Identifier: MIT
|
6 | 8 | *
|
@@ -110,6 +112,7 @@ typedef void ( * portISR_t )( void );
|
110 | 112 | #define portSCB_VTOR_REG ( *( ( portISR_t ** ) 0xe000ed08 ) )
|
111 | 113 | #define portSCB_SYS_HANDLER_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
|
112 | 114 | #define portSCB_MEM_FAULT_ENABLE_BIT ( 1UL << 16UL )
|
| 115 | +#define portSCB_USG_FAULT_ENABLE_BIT ( 1UL << 18UL ) |
113 | 116 | /*-----------------------------------------------------------*/
|
114 | 117 |
|
115 | 118 | /**
|
@@ -373,6 +376,20 @@ typedef void ( * portISR_t )( void );
|
373 | 376 | * any secure calls.
|
374 | 377 | */
|
375 | 378 | #define portNO_SECURE_CONTEXT 0
|
| 379 | + |
| 380 | +/** |
| 381 | + * @brief Constants required to check and configure PACBTI security feature implementation. |
| 382 | + */ |
| 383 | +#if ( portHAS_PACBTI_FEATURE == 1 ) |
| 384 | + |
| 385 | + #define portID_ISAR5_REG ( *( ( volatile uint32_t * ) 0xe000ed74 ) ) |
| 386 | + |
| 387 | + #define portCONTROL_UPAC_EN ( 1UL << 7UL ) |
| 388 | + #define portCONTROL_PAC_EN ( 1UL << 6UL ) |
| 389 | + #define portCONTROL_UBTI_EN ( 1UL << 5UL ) |
| 390 | + #define portCONTROL_BTI_EN ( 1UL << 4UL ) |
| 391 | + |
| 392 | +#endif /* portHAS_PACBTI_FEATURE */ |
376 | 393 | /*-----------------------------------------------------------*/
|
377 | 394 |
|
378 | 395 | /**
|
@@ -410,6 +427,26 @@ static void prvTaskExitError( void );
|
410 | 427 | static void prvSetupFPU( void ) PRIVILEGED_FUNCTION;
|
411 | 428 | #endif /* configENABLE_FPU */
|
412 | 429 |
|
| 430 | +#if ( portHAS_PACBTI_FEATURE == 1 ) |
| 431 | + |
| 432 | +/** |
| 433 | + * @brief Configures PACBTI features. |
| 434 | + * |
| 435 | + * This function configures the Pointer Authentication, and Branch Target |
| 436 | + * Identification security features as per the user configuration. It returns |
| 437 | + * the value of the special purpose CONTROL register accordingly, and optionally |
| 438 | + * updates the CONTROL register value. Currently, only Cortex-M85 (ARMv8.1-M |
| 439 | + * architecture based) target supports PACBTI security feature. |
| 440 | + * |
| 441 | + * @param xWriteControlRegister Used to control whether the special purpose |
| 442 | + * CONTROL register should be updated or not. |
| 443 | + * |
| 444 | + * @return CONTROL register value according to the configured PACBTI option. |
| 445 | + */ |
| 446 | + static uint32_t prvConfigurePACBTI( BaseType_t xWriteControlRegister ); |
| 447 | + |
| 448 | +#endif /* portHAS_PACBTI_FEATURE */ |
| 449 | + |
413 | 450 | /**
|
414 | 451 | * @brief Setup the timer to generate the tick interrupts.
|
415 | 452 | *
|
@@ -1457,6 +1494,7 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
|
1457 | 1494 | xMPU_SETTINGS * xMPUSettings ) /* PRIVILEGED_FUNCTION */
|
1458 | 1495 | {
|
1459 | 1496 | uint32_t ulIndex = 0;
|
| 1497 | + uint32_t ulControl = 0x0; |
1460 | 1498 |
|
1461 | 1499 | xMPUSettings->ulContext[ ulIndex ] = 0x04040404; /* r4. */
|
1462 | 1500 | ulIndex++;
|
@@ -1503,16 +1541,24 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
|
1503 | 1541 | xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) pxEndOfStack; /* PSPLIM. */
|
1504 | 1542 | ulIndex++;
|
1505 | 1543 |
|
| 1544 | + #if ( portHAS_PACBTI_FEATURE == 1 ) |
| 1545 | + { |
| 1546 | + /* Check PACBTI security feature configuration before pushing the |
| 1547 | + * CONTROL register's value on task's TCB. */ |
| 1548 | + ulControl = prvConfigurePACBTI( pdFALSE ); |
| 1549 | + } |
| 1550 | + #endif /* portHAS_PACBTI_FEATURE */ |
| 1551 | + |
1506 | 1552 | if( xRunPrivileged == pdTRUE )
|
1507 | 1553 | {
|
1508 | 1554 | xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
|
1509 |
| - xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) portINITIAL_CONTROL_PRIVILEGED; /* CONTROL. */ |
| 1555 | + xMPUSettings->ulContext[ ulIndex ] = ( ulControl | ( uint32_t ) portINITIAL_CONTROL_PRIVILEGED ); /* CONTROL. */ |
1510 | 1556 | ulIndex++;
|
1511 | 1557 | }
|
1512 | 1558 | else
|
1513 | 1559 | {
|
1514 | 1560 | xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
|
1515 |
| - xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) portINITIAL_CONTROL_UNPRIVILEGED; /* CONTROL. */ |
| 1561 | + xMPUSettings->ulContext[ ulIndex ] = ( ulControl | ( uint32_t ) portINITIAL_CONTROL_UNPRIVILEGED ); /* CONTROL. */ |
1516 | 1562 | ulIndex++;
|
1517 | 1563 | }
|
1518 | 1564 |
|
@@ -1740,6 +1786,14 @@ BaseType_t xPortStartScheduler( void ) /* PRIVILEGED_FUNCTION */
|
1740 | 1786 | portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
|
1741 | 1787 | portNVIC_SHPR2_REG = 0;
|
1742 | 1788 |
|
| 1789 | + #if ( portHAS_PACBTI_FEATURE == 1 ) |
| 1790 | + { |
| 1791 | + /* Set the CONTROL register value based on PACBTI security feature |
| 1792 | + * configuration before starting the first task. */ |
| 1793 | + ( void) prvConfigurePACBTI( pdTRUE ); |
| 1794 | + } |
| 1795 | + #endif /* portHAS_PACBTI_FEATURE */ |
| 1796 | + |
1743 | 1797 | #if ( configENABLE_MPU == 1 )
|
1744 | 1798 | {
|
1745 | 1799 | /* Setup the Memory Protection Unit (MPU). */
|
@@ -2158,3 +2212,42 @@ BaseType_t xPortIsInsideInterrupt( void )
|
2158 | 2212 |
|
2159 | 2213 | #endif /* #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) */
|
2160 | 2214 | /*-----------------------------------------------------------*/
|
| 2215 | + |
| 2216 | +#if ( portHAS_PACBTI_FEATURE == 1 ) |
| 2217 | + |
| 2218 | + static uint32_t prvConfigurePACBTI( BaseType_t xWriteControlRegister ) |
| 2219 | + { |
| 2220 | + uint32_t ulControl = 0x0; |
| 2221 | + |
| 2222 | + /* Ensure that PACBTI is implemented. */ |
| 2223 | + configASSERT( portID_ISAR5_REG != 0x0 ); |
| 2224 | + |
| 2225 | + /* Enable UsageFault exception if PAC or BTI is enabled. */ |
| 2226 | + #if( ( configENABLE_PAC == 1 ) || ( configENABLE_BTI == 1 ) ) |
| 2227 | + { |
| 2228 | + portSCB_SYS_HANDLER_CTRL_STATE_REG |= portSCB_USG_FAULT_ENABLE_BIT; |
| 2229 | + } |
| 2230 | + #endif |
| 2231 | + |
| 2232 | + #if( configENABLE_PAC == 1 ) |
| 2233 | + { |
| 2234 | + ulControl |= ( portCONTROL_UPAC_EN | portCONTROL_PAC_EN ); |
| 2235 | + } |
| 2236 | + #endif |
| 2237 | + |
| 2238 | + #if( configENABLE_BTI == 1 ) |
| 2239 | + { |
| 2240 | + ulControl |= ( portCONTROL_UBTI_EN | portCONTROL_BTI_EN ); |
| 2241 | + } |
| 2242 | + #endif |
| 2243 | + |
| 2244 | + if( xWriteControlRegister == pdTRUE ) |
| 2245 | + { |
| 2246 | + __asm volatile ( "msr control, %0" : : "r" ( ulControl ) ); |
| 2247 | + } |
| 2248 | + |
| 2249 | + return ulControl; |
| 2250 | + } |
| 2251 | + |
| 2252 | +#endif /* #if ( portHAS_PACBTI_FEATURE == 1 ) */ |
| 2253 | +/*-----------------------------------------------------------*/ |
0 commit comments