Skip to content

Commit

Permalink
Add option to disable unprivileged critical sections
Browse files Browse the repository at this point in the history
This commit introduces a new config
configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS which enables developers to
prevent critical sections from unprivileged tasks. It defaults to 1 for
backward compatibility. Application should set it to 0 to disable
critical sections from unprivileged tasks.

Signed-off-by: Gaurav Aggarwal <[email protected]>
  • Loading branch information
aggarg committed Nov 15, 2021
1 parent 7a38487 commit 44fc137
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
25 changes: 23 additions & 2 deletions portable/GCC/ARM_CM3_MPU/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
#define portNVIC_SYSTICK_CLK ( 0 )
#endif

#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
#endif

/* Constants required to access and manipulate the NVIC. */
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
Expand Down Expand Up @@ -162,12 +167,20 @@ void vResetPrivilege( void ) __attribute__( ( naked ) );
/**
* @brief Enter critical section.
*/
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
#endif

/**
* @brief Exit from critical section.
*/
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
#endif
/*-----------------------------------------------------------*/

/* Each task maintains its own interrupt status in the critical nesting
Expand Down Expand Up @@ -480,20 +493,26 @@ void vPortEndScheduler( void )

void vPortEnterCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

portDISABLE_INTERRUPTS();
uxCriticalNesting++;

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

void vPortExitCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

configASSERT( uxCriticalNesting );
uxCriticalNesting--;
Expand All @@ -503,7 +522,9 @@ void vPortExitCritical( void )
portENABLE_INTERRUPTS();
}

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

Expand Down
25 changes: 23 additions & 2 deletions portable/GCC/ARM_CM4_MPU/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
#define portNVIC_SYSTICK_CLK ( 0 )
#endif

#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
#endif

/* Constants required to access and manipulate the NVIC. */
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
Expand Down Expand Up @@ -175,12 +180,20 @@ void vResetPrivilege( void ) __attribute__( ( naked ) );
/**
* @brief Enter critical section.
*/
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
#endif

/**
* @brief Exit from critical section.
*/
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
#endif
/*-----------------------------------------------------------*/

/* Each task maintains its own interrupt status in the critical nesting
Expand Down Expand Up @@ -516,20 +529,26 @@ void vPortEndScheduler( void )

void vPortEnterCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

portDISABLE_INTERRUPTS();
uxCriticalNesting++;

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

void vPortExitCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

configASSERT( uxCriticalNesting );
uxCriticalNesting--;
Expand All @@ -539,7 +558,9 @@ void vPortExitCritical( void )
portENABLE_INTERRUPTS();
}

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

Expand Down
25 changes: 23 additions & 2 deletions portable/IAR/ARM_CM4F_MPU/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
#define portNVIC_SYSTICK_CLK_BIT ( 0 )
#endif

#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
#endif

/* Constants required to manipulate the core. Registers first... */
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
Expand Down Expand Up @@ -188,12 +193,20 @@ extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;
/**
* @brief Enter critical section.
*/
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
#endif

/**
* @brief Exit from critical section.
*/
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
#endif
/*-----------------------------------------------------------*/

/* Each task maintains its own interrupt status in the critical nesting
Expand Down Expand Up @@ -444,8 +457,10 @@ void vPortEndScheduler( void )

void vPortEnterCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

portDISABLE_INTERRUPTS();
uxCriticalNesting++;
Expand All @@ -460,14 +475,18 @@ void vPortEnterCritical( void )
configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
}

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

void vPortExitCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

configASSERT( uxCriticalNesting );

Expand All @@ -478,7 +497,9 @@ void vPortExitCritical( void )
portENABLE_INTERRUPTS();
}

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

Expand Down
25 changes: 23 additions & 2 deletions portable/RVDS/ARM_CM4_MPU/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@

#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE

#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
#endif

/* Constants required to access and manipulate the NVIC. */
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
Expand Down Expand Up @@ -189,12 +194,20 @@ void vResetPrivilege( void );
/**
* @brief Enter critical section.
*/
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
#endif

/**
* @brief Exit from critical section.
*/
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
#else
void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
#endif
/*-----------------------------------------------------------*/

/*
Expand Down Expand Up @@ -519,20 +532,26 @@ void vPortEndScheduler( void )

void vPortEnterCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

portDISABLE_INTERRUPTS();
uxCriticalNesting++;

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

void vPortExitCritical( void )
{
#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
BaseType_t xRunningPrivileged;
xPortRaisePrivilege( xRunningPrivileged );
#endif

configASSERT( uxCriticalNesting );
uxCriticalNesting--;
Expand All @@ -542,7 +561,9 @@ void vPortExitCritical( void )
portENABLE_INTERRUPTS();
}

#if( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
vPortResetPrivilege( xRunningPrivileged );
#endif
}
/*-----------------------------------------------------------*/

Expand Down

0 comments on commit 44fc137

Please sign in to comment.