From 4c690b700b019186723f8f57d70bcf276bba1e1b Mon Sep 17 00:00:00 2001 From: Soren Ptak Date: Tue, 19 Dec 2023 08:14:24 -0500 Subject: [PATCH 1/2] Include portmacro.h and the FreeRTOSConfig.h file in mpu_wrappers.h, as these are the files that would define portUSING_MPU_WRAPPERS. Always export the PRIVILEGED attributes, so MPU users can easily assign their functions to also be placed with the FreeRTOS Kernel calls --- include/mpu_wrappers.h | 94 ++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/include/mpu_wrappers.h b/include/mpu_wrappers.h index 51e7bcf4054..0d60939cce8 100644 --- a/include/mpu_wrappers.h +++ b/include/mpu_wrappers.h @@ -29,24 +29,42 @@ #ifndef MPU_WRAPPERS_H #define MPU_WRAPPERS_H + /* Ensure API functions go in the privileged execution section. */ + #ifndef PRIVILEGED_FUNCTION + #define PRIVILEGED_FUNCTION __attribute__( ( section( "privileged_functions" ) ) ) + #endif /* PRIVILEGED_FUNCTION */ + + #ifndef PRIVILEGED_DATA + #define PRIVILEGED_DATA __attribute__( ( section( "privileged_data" ) ) ) + #endif /* PRIVILEGED_DATA */ + + #ifndef FREERTOS_SYSTEM_CALL + #define FREERTOS_SYSTEM_CALL __attribute__( ( section( "freertos_system_calls" ) ) ) + #endif /* FREERTOS_SYSTEM_CALL */ + + /* This file redefines API functions to be called through a wrapper macro, but * only for ports that are using the MPU. */ +/* Include FreeRTOSConfig.h and portmacro.h to determine if the port is using the MPU */ +#include "FreeRTOSConfig.h" +#include "portmacro.h" + #if ( portUSING_MPU_WRAPPERS == 1 ) -/* MPU_WRAPPERS_INCLUDED_FROM_API_FILE will be defined when this file is - * included from queue.c or task.c to prevent it from having an effect within - * those files. */ + /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE will be defined when this file is + * included from queue.c or task.c to prevent it from having an effect within + * those files. */ #ifndef MPU_WRAPPERS_INCLUDED_FROM_API_FILE -/* - * Map standard (non MPU) API functions to equivalents that start - * "MPU_". This will cause the application code to call the MPU_ - * version, which wraps the non-MPU version with privilege promoting - * then demoting code, so the kernel code always runs will full - * privileges. - */ + /* + * Map standard (non MPU) API functions to equivalents that start + * "MPU_". This will cause the application code to call the MPU_ + * version, which wraps the non-MPU version with privilege promoting + * then demoting code, so the kernel code always runs will full + * privileges. + */ -/* Map standard task.h API functions to the MPU equivalents. */ + /* Map standard task.h API functions to the MPU equivalents. */ #define vTaskDelay MPU_vTaskDelay #define xTaskDelayUntil MPU_xTaskDelayUntil #define xTaskAbortDelay MPU_xTaskAbortDelay @@ -82,9 +100,9 @@ #define ulTaskGetRunTimePercent MPU_ulTaskGetRunTimePercent #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ -/* Privileged only wrappers for Task APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ + /* Privileged only wrappers for Task APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define xTaskCreate MPU_xTaskCreate #define xTaskCreateStatic MPU_xTaskCreateStatic #define vTaskDelete MPU_vTaskDelete @@ -107,7 +125,7 @@ #define vTaskGenericNotifyGiveFromISR MPU_vTaskGenericNotifyGiveFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ -/* Map standard queue.h API functions to the MPU equivalents. */ + /* Map standard queue.h API functions to the MPU equivalents. */ #define xQueueGenericSend MPU_xQueueGenericSend #define xQueueReceive MPU_xQueueReceive #define xQueuePeek MPU_xQueuePeek @@ -126,9 +144,9 @@ #define pcQueueGetName MPU_pcQueueGetName #endif /* #if ( configQUEUE_REGISTRY_SIZE > 0 ) */ -/* Privileged only wrappers for Queue APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ + /* Privileged only wrappers for Queue APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define vQueueDelete MPU_vQueueDelete #define xQueueCreateMutex MPU_xQueueCreateMutex #define xQueueCreateMutexStatic MPU_xQueueCreateMutexStatic @@ -153,7 +171,7 @@ #define xQueueSelectFromSetFromISR MPU_xQueueSelectFromSetFromISR #endif /* if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ -/* Map standard timer.h API functions to the MPU equivalents. */ + /* Map standard timer.h API functions to the MPU equivalents. */ #define pvTimerGetTimerID MPU_pvTimerGetTimerID #define vTimerSetTimerID MPU_vTimerSetTimerID #define xTimerIsTimerActive MPU_xTimerIsTimerActive @@ -165,9 +183,9 @@ #define xTimerGetPeriod MPU_xTimerGetPeriod #define xTimerGetExpiryTime MPU_xTimerGetExpiryTime -/* Privileged only wrappers for Timer APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ + /* Privileged only wrappers for Timer APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) #define xTimerGetReloadMode MPU_xTimerGetReloadMode #define xTimerCreate MPU_xTimerCreate @@ -176,7 +194,7 @@ #define xTimerGenericCommandFromISR MPU_xTimerGenericCommandFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ -/* Map standard event_group.h API functions to the MPU equivalents. */ + /* Map standard event_group.h API functions to the MPU equivalents. */ #define xEventGroupWaitBits MPU_xEventGroupWaitBits #define xEventGroupClearBits MPU_xEventGroupClearBits #define xEventGroupSetBits MPU_xEventGroupSetBits @@ -187,9 +205,9 @@ #define vEventGroupSetNumber MPU_vEventGroupSetNumber #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) */ -/* Privileged only wrappers for Event Group APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ + /* Privileged only wrappers for Event Group APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define xEventGroupCreate MPU_xEventGroupCreate #define xEventGroupCreateStatic MPU_xEventGroupCreateStatic #define vEventGroupDelete MPU_vEventGroupDelete @@ -201,8 +219,8 @@ #define xEventGroupGetBitsFromISR MPU_xEventGroupGetBitsFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ -/* Map standard message/stream_buffer.h API functions to the MPU - * equivalents. */ + /* Map standard message/stream_buffer.h API functions to the MPU + * equivalents. */ #define xStreamBufferSend MPU_xStreamBufferSend #define xStreamBufferReceive MPU_xStreamBufferReceive #define xStreamBufferIsFull MPU_xStreamBufferIsFull @@ -212,9 +230,9 @@ #define xStreamBufferSetTriggerLevel MPU_xStreamBufferSetTriggerLevel #define xStreamBufferNextMessageLengthBytes MPU_xStreamBufferNextMessageLengthBytes -/* Privileged only wrappers for Stream Buffer APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ + /* Privileged only wrappers for Stream Buffer APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define xStreamBufferGenericCreate MPU_xStreamBufferGenericCreate #define xStreamBufferGenericCreateStatic MPU_xStreamBufferGenericCreateStatic @@ -229,12 +247,6 @@ #define xStreamBufferReceiveCompletedFromISR MPU_xStreamBufferReceiveCompletedFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ -/* Remove the privileged function macro, but keep the PRIVILEGED_DATA - * macro so applications can place data in privileged access sections - * (useful when using statically allocated objects). */ - #define PRIVILEGED_FUNCTION - #define PRIVILEGED_DATA __attribute__( ( section( "privileged_data" ) ) ) - #define FREERTOS_SYSTEM_CALL #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) @@ -264,14 +276,6 @@ #define vRevokeAccessToTimer( xTask, xTimerToRevokeAccess ) vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xTimerToRevokeAccess ) ) #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */ - - #else /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE */ - -/* Ensure API functions go in the privileged execution section. */ - #define PRIVILEGED_FUNCTION __attribute__( ( section( "privileged_functions" ) ) ) - #define PRIVILEGED_DATA __attribute__( ( section( "privileged_data" ) ) ) - #define FREERTOS_SYSTEM_CALL __attribute__( ( section( "freertos_system_calls" ) ) ) - #endif /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE */ #else /* portUSING_MPU_WRAPPERS */ From bbcf62798cf2fb395d16df4c3c2c73fc0e0f6242 Mon Sep 17 00:00:00 2001 From: Soren Ptak Date: Wed, 20 Dec 2023 10:28:17 -0500 Subject: [PATCH 2/2] Always define the privileged data, privileged functions, and system call attributes --- include/mpu_wrappers.h | 87 +++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 51 deletions(-) diff --git a/include/mpu_wrappers.h b/include/mpu_wrappers.h index 0d60939cce8..6117bbbcc98 100644 --- a/include/mpu_wrappers.h +++ b/include/mpu_wrappers.h @@ -29,42 +29,24 @@ #ifndef MPU_WRAPPERS_H #define MPU_WRAPPERS_H - /* Ensure API functions go in the privileged execution section. */ - #ifndef PRIVILEGED_FUNCTION - #define PRIVILEGED_FUNCTION __attribute__( ( section( "privileged_functions" ) ) ) - #endif /* PRIVILEGED_FUNCTION */ - - #ifndef PRIVILEGED_DATA - #define PRIVILEGED_DATA __attribute__( ( section( "privileged_data" ) ) ) - #endif /* PRIVILEGED_DATA */ - - #ifndef FREERTOS_SYSTEM_CALL - #define FREERTOS_SYSTEM_CALL __attribute__( ( section( "freertos_system_calls" ) ) ) - #endif /* FREERTOS_SYSTEM_CALL */ - - /* This file redefines API functions to be called through a wrapper macro, but * only for ports that are using the MPU. */ -/* Include FreeRTOSConfig.h and portmacro.h to determine if the port is using the MPU */ -#include "FreeRTOSConfig.h" -#include "portmacro.h" - #if ( portUSING_MPU_WRAPPERS == 1 ) - /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE will be defined when this file is - * included from queue.c or task.c to prevent it from having an effect within - * those files. */ +/* MPU_WRAPPERS_INCLUDED_FROM_API_FILE will be defined when this file is + * included from queue.c or task.c to prevent it from having an effect within + * those files. */ #ifndef MPU_WRAPPERS_INCLUDED_FROM_API_FILE - /* - * Map standard (non MPU) API functions to equivalents that start - * "MPU_". This will cause the application code to call the MPU_ - * version, which wraps the non-MPU version with privilege promoting - * then demoting code, so the kernel code always runs will full - * privileges. - */ +/* + * Map standard (non MPU) API functions to equivalents that start + * "MPU_". This will cause the application code to call the MPU_ + * version, which wraps the non-MPU version with privilege promoting + * then demoting code, so the kernel code always runs will full + * privileges. + */ - /* Map standard task.h API functions to the MPU equivalents. */ +/* Map standard task.h API functions to the MPU equivalents. */ #define vTaskDelay MPU_vTaskDelay #define xTaskDelayUntil MPU_xTaskDelayUntil #define xTaskAbortDelay MPU_xTaskAbortDelay @@ -100,9 +82,9 @@ #define ulTaskGetRunTimePercent MPU_ulTaskGetRunTimePercent #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ - /* Privileged only wrappers for Task APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ +/* Privileged only wrappers for Task APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define xTaskCreate MPU_xTaskCreate #define xTaskCreateStatic MPU_xTaskCreateStatic #define vTaskDelete MPU_vTaskDelete @@ -125,7 +107,7 @@ #define vTaskGenericNotifyGiveFromISR MPU_vTaskGenericNotifyGiveFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ - /* Map standard queue.h API functions to the MPU equivalents. */ +/* Map standard queue.h API functions to the MPU equivalents. */ #define xQueueGenericSend MPU_xQueueGenericSend #define xQueueReceive MPU_xQueueReceive #define xQueuePeek MPU_xQueuePeek @@ -144,9 +126,9 @@ #define pcQueueGetName MPU_pcQueueGetName #endif /* #if ( configQUEUE_REGISTRY_SIZE > 0 ) */ - /* Privileged only wrappers for Queue APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ +/* Privileged only wrappers for Queue APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define vQueueDelete MPU_vQueueDelete #define xQueueCreateMutex MPU_xQueueCreateMutex #define xQueueCreateMutexStatic MPU_xQueueCreateMutexStatic @@ -171,7 +153,7 @@ #define xQueueSelectFromSetFromISR MPU_xQueueSelectFromSetFromISR #endif /* if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ - /* Map standard timer.h API functions to the MPU equivalents. */ +/* Map standard timer.h API functions to the MPU equivalents. */ #define pvTimerGetTimerID MPU_pvTimerGetTimerID #define vTimerSetTimerID MPU_vTimerSetTimerID #define xTimerIsTimerActive MPU_xTimerIsTimerActive @@ -183,9 +165,9 @@ #define xTimerGetPeriod MPU_xTimerGetPeriod #define xTimerGetExpiryTime MPU_xTimerGetExpiryTime - /* Privileged only wrappers for Timer APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ +/* Privileged only wrappers for Timer APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) #define xTimerGetReloadMode MPU_xTimerGetReloadMode #define xTimerCreate MPU_xTimerCreate @@ -194,7 +176,7 @@ #define xTimerGenericCommandFromISR MPU_xTimerGenericCommandFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ - /* Map standard event_group.h API functions to the MPU equivalents. */ +/* Map standard event_group.h API functions to the MPU equivalents. */ #define xEventGroupWaitBits MPU_xEventGroupWaitBits #define xEventGroupClearBits MPU_xEventGroupClearBits #define xEventGroupSetBits MPU_xEventGroupSetBits @@ -205,9 +187,9 @@ #define vEventGroupSetNumber MPU_vEventGroupSetNumber #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) */ - /* Privileged only wrappers for Event Group APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ +/* Privileged only wrappers for Event Group APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define xEventGroupCreate MPU_xEventGroupCreate #define xEventGroupCreateStatic MPU_xEventGroupCreateStatic #define vEventGroupDelete MPU_vEventGroupDelete @@ -219,8 +201,8 @@ #define xEventGroupGetBitsFromISR MPU_xEventGroupGetBitsFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ - /* Map standard message/stream_buffer.h API functions to the MPU - * equivalents. */ +/* Map standard message/stream_buffer.h API functions to the MPU + * equivalents. */ #define xStreamBufferSend MPU_xStreamBufferSend #define xStreamBufferReceive MPU_xStreamBufferReceive #define xStreamBufferIsFull MPU_xStreamBufferIsFull @@ -230,9 +212,9 @@ #define xStreamBufferSetTriggerLevel MPU_xStreamBufferSetTriggerLevel #define xStreamBufferNextMessageLengthBytes MPU_xStreamBufferNextMessageLengthBytes - /* Privileged only wrappers for Stream Buffer APIs. These are needed so that - * the application can use opaque handles maintained in mpu_wrappers.c - * with all the APIs. */ +/* Privileged only wrappers for Stream Buffer APIs. These are needed so that + * the application can use opaque handles maintained in mpu_wrappers.c + * with all the APIs. */ #define xStreamBufferGenericCreate MPU_xStreamBufferGenericCreate #define xStreamBufferGenericCreateStatic MPU_xStreamBufferGenericCreateStatic @@ -247,8 +229,6 @@ #define xStreamBufferReceiveCompletedFromISR MPU_xStreamBufferReceiveCompletedFromISR #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ - - #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) #define vGrantAccessToTask( xTask, xTaskToGrantAccess ) vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xTaskToGrantAccess ) ) @@ -276,8 +256,13 @@ #define vRevokeAccessToTimer( xTask, xTimerToRevokeAccess ) vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xTimerToRevokeAccess ) ) #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */ + #endif /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE */ + #define PRIVILEGED_FUNCTION __attribute__( ( section( "privileged_functions" ) ) ) + #define PRIVILEGED_DATA __attribute__( ( section( "privileged_data" ) ) ) + #define FREERTOS_SYSTEM_CALL __attribute__( ( section( "freertos_system_calls" ) ) ) + #else /* portUSING_MPU_WRAPPERS */ #define PRIVILEGED_FUNCTION