# Initialising variables in priviledged data when using MPU

**URL:** <https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962>\
**Category:** Kernel\
**Created:** [November 27, 2021, 1:18pm UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962 "2021-11-27T13:18:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malcolm](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Malcolm](https://forums.freertos.org/u/Malcolm)\
**Post date:** [November 27, 2021, 1:18pm UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/1 "2021-11-27T13:18:26Z")

</div>

I am porting the MPU example for ST32L475 to ST32F401, making changes as necessary and using the linker script. However I have a problem that none of the variables declared as priviledged data are being initialised, eg  
PRIVILEGED\_DATA static volatile UBaseType\_t uxCurrentNumberOfTasks = ( UBaseType\_t ) 0U;  
PRIVILEGED\_DATA static volatile BaseType\_t xSchedulerRunning = pdFALSE;  
Rather they are left at their cleared value of 0xffffffff.  
This causes problems when these values are then tested, giving incorrect results and thus the software freezes with an error.  
Where is this corrected?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 27, 2021, 1:44pm UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/2 "2021-11-27T13:44:46Z")

</div>

That should be handled in the startup code (which will be in assembly). For a ‘simple’ processor without the privileged mode, there will be one loop to copy initialization data from flash to the data segment, with a privileged processor (or any setup with two different types of data segment) you will need two loops to fill these.

You probably should look at the vendor provided code for the new MPU to see how such code was done (assuming they support this mode).

---

<div class="post-metadata">

**Author:** ![Malcolm](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Malcolm](https://forums.freertos.org/u/Malcolm)\
**Post date:** [November 27, 2021, 4:15pm UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/3 "2021-11-27T16:15:03Z")

</div>

I can see the loader copies from sidata to \_sdata, but the variables are set to 0xffffffff, such as

PRIVILEGED\_DATA static volatile UBaseType\_t uxSchedulerSuspended = ( UBaseType\_t ) pdFALSE;

which stops the code

/\* FreeRTOS kernel data. _/  
.privileged\_data :  
{  
. = ALIGN(4);  
\_sdata = .; /_ Create a global symbol at data start. \*/  
_(privileged\_data)  
. = ALIGN(4);  
FILL(0xDEAD);  
/_ Ensure that non-privileged data is placed after the region reserved for  
\* privileged kernel data. _/  
/_ Note that dot (.) actually refers to the byte offset from the start of  
\* the current section (.privileged\_data in this case). As a result, setting  
\* dot (.) to a value sets the size of the section. \*/  
. = **privileged\_data\_region\_size** ;  
} \>RAM AT\> FLASH

/\* Initialized data sections into “RAM” Ram type memory _/  
.data :  
{  
. = ALIGN(4);  
 (.data) / .data sections /  
 (.data) / .data_ sections \*/

```
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */

```

} \>RAM AT\> FLASH

---

<div class="post-metadata">

**Author:** ![Malcolm](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Malcolm](https://forums.freertos.org/u/Malcolm)\
**Post date:** [November 27, 2021, 5:48pm UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/4 "2021-11-27T17:48:34Z")

</div>

Here is the problem and solution. The LOADADDR() needs changing to  
\_sidata = LOADADDR(.privileged\_data);

As below in the link script.

/\* Used by the startup to initialize data \*/  
\_sidata = LOADADDR(.privileged\_data);

/\* FreeRTOS kernel data. _/  
.privileged\_data :  
{  
. = ALIGN(4);  
\_sdata = .; /_ Create a global symbol at data start. \*/  
_(privileged\_data)  
. = ALIGN(4);  
FILL(0xDEAD);  
/_ Ensure that non-privileged data is placed after the region reserved for  
\* privileged kernel data. _/  
/_ Note that dot (.) actually refers to the byte offset from the start of  
\* the current section (.privileged\_data in this case). As a result, setting  
\* dot (.) to a value sets the size of the section. \*/  
. = **privileged\_data\_region\_size** ;  
} \>RAM AT\> FLASH

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 27, 2021, 6:35pm UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/5 "2021-11-27T18:35:58Z")

</div>

You can place the privileged data within the normal .data section so the startup code will initialise it. Here is an example: [https://github.com/FreeRTOS/FreeRTOS/blob/V10.4.1/FreeRTOS/Demo/CORTEX\_MPU\_M33F\_NXP\_LPC55S69\_MCUXpresso/Projects/MCUXpresso/NonSecure/FreeRTOSDemo\_ns.ld#L128](https://github.com/FreeRTOS/FreeRTOS/blob/V10.4.1/FreeRTOS/Demo/CORTEX_MPU_M33F_NXP_LPC55S69_MCUXpresso/Projects/MCUXpresso/NonSecure/FreeRTOSDemo_ns.ld#L128)

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 28, 2021, 12:06am UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/6 "2021-11-28T00:06:19Z")

</div>

> [@Malcolm](#):
>
> Here is the problem and solution. The LOADADDR() needs changing to  
> \_sidata = LOADADDR(.privileged\_data);

This mostly depends on the variables needed by your startup code to correctly initialize the data section. We do have the same in our samples wherever needed - [https://github.com/FreeRTOS/FreeRTOS/blob/b1b3a0a3e98b226e7fe59a6db84bd3f3e2179967/FreeRTOS/Demo/CORTEX\_MPU\_STM32L4\_Discovery\_GCC\_IAR\_Keil/Projects/GCC/STM32L475VGTX\_FLASH.ld#L183](https://github.com/FreeRTOS/FreeRTOS/blob/b1b3a0a3e98b226e7fe59a6db84bd3f3e2179967/FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/Projects/GCC/STM32L475VGTX_FLASH.ld#L183)

Another method, as mentioned by Richard, is to use the data section in the Linker script. An example for Cortex-M4 is here - [https://github.com/FreeRTOS/FreeRTOS/blob/b1b3a0a3e98b226e7fe59a6db84bd3f3e2179967/FreeRTOS/Demo/CORTEX\_MPU\_LPC54018\_MCUXpresso/Projects/MCUXpresso/FreeRTOSDemo.ld#L205](https://github.com/FreeRTOS/FreeRTOS/blob/b1b3a0a3e98b226e7fe59a6db84bd3f3e2179967/FreeRTOS/Demo/CORTEX_MPU_LPC54018_MCUXpresso/Projects/MCUXpresso/FreeRTOSDemo.ld#L205)

Please let us know if problem exists in any of our samples.

Thanks.

---

<div class="post-metadata">

**Author:** ![Malcolm](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Malcolm](https://forums.freertos.org/u/Malcolm)\
**Post date:** [November 28, 2021, 9:45am UTC](https://forums.freertos.org/t/initialising-variables-in-priviledged-data-when-using-mpu/13962/7 "2021-11-28T09:45:15Z")

</div>

Thank you for such rapid and helpful advice. This was my fault for not copying all the required sections. Correcting \_sidata = LOADADDR(.privileged\_data); resolved my issues, and checking, this is correct in all the FreeRTOS examples. However the suggestions put me on the correct track to determine the issue and along the way I learnt more about the initialisation code and the linker scripts. Nice to hear from Richard, with whom I first had contact in 2005.
