FreeRTOS Privilege Status Detection

jerryhross wrote on Saturday, November 14, 2009:

FreeRTOSS6.0.0:

Definition in:   FreeRTOS.h

#ifndef portPRIVILEGE_BIT
    #define portPRIVILEGE_BIT ( ( unsigned portBASE_TYPE ) 0x00 )
#endif

Usage in:  tasks.c

        /* Should the task be created in privileged mode? */
LINE: 403
        if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )
        {
            xRunPrivileged = pdTRUE;
        }
        else
        {
            xRunPrivileged = pdFALSE;
        }
        uxPriority &= ~portPRIVILEGE_BIT;

The code at line 403 will always fail the test ( uxPriority & portPRIVILEGE_BIT ) != 0x00 because portPRIVILEGE_BIT is the value 0 which represents the privilege bit position in uxPriority.  It is being used as a value, zero (0),  instead of a bit position of zero (0) so the expression (uxPriority & portPRIVILEGE_BIT) will always produce a value of zero (0) which will never be !=0x00.

Jerry Ross

rtel wrote on Saturday, November 14, 2009:

#ifndef portPRIVILEGE_BIT
#define portPRIVILEGE_BIT ( ( unsigned portBASE_TYPE ) 0x00 )
#endif

This definition is the default setting when portPRIVILEGE_BIT is not defined anywhere else.  In the Cortex M3 MPU ports portPRIVILEGE_BIT is defined as 0x80000000, which means bit 31 indicates the privilege status.

if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )
{
xRunPrivileged = pdTRUE;
}
else
{
xRunPrivileged = pdFALSE;
}

If portPRIVILEGE_BIT is 0 then xRunPrivileged will always be set to pdFALSE as anything bitwise ANDed with 0 will always be zero.  However, if portPRIVILEGE_BIT is set to 0 then the MPU is not being used and pdFALSE is the correct value for xRunPrivileged.  A value of 0 does not mean bit 0, it means no bits are set.

Regards.

jerryhross wrote on Saturday, November 14, 2009:

Thank you for the quick reply.  I understand your logic now.  I should have scanned the entire code base for an answer.  I will be more thorough next time.

My compiler elides the expression because it has no effect.