xTaskCreateRestricted - puxStackBuffer

Hello,
I would like to clarify if I understood manual. Below you will find my questions regarding to requirements:

  1. Requirement 1: “MPU regions - the size and alignment of each region must both be equal to the same power of two value”
    As I understand, each MPU region should have same value of size as alignment.
    But all regions do not have to have same size.
    E.g.
    Region 1 : size 128kB, alignment 128kB,
    Region 2 : size 512kB, alignment 512kB,
    Region 3 : size 64kB, alignment 64kB,

Am I write or maybe all MPU regions should have same size and alignment?

Requirement 2: task stack buffer should have same value of size as alignment.
Are there any restriction concerning task stack size and MPU regions size? Should they (task stack size and MPU region size) be equal or there are not such restrictions?

The requirements for the size and alignment of the MPU regions is a hardware restriction, not a software restriction, and is different for ARMv7-M (Cortex-M3 and family) and ARMv8-M (Cortex-M33 and family). To get the requirements you can refer to the hardware manuals. The requirement for the stack to comply with the same size and alignment requirements is because on ARMv7-M an MPU region is used to bound the stack to catch stack overflows.

Just looking up in the FreeRTOS book I see:

Region Start Address and Size Constraints
The MPU hardware imposes two rules that region start address and size definitions must comply with:

  1. The region size must be a binary power of two between 32 bytes and 64 gigabytes, inclusive. For example, 32 bytes, 64 bytes, 128 bytes, 256 bytes, and so on are all valid region sizes.
  2. The start address must be a multiple of the region size. For example, a region that is configured to be 65536 bytes long must start on an address that is exactly divisible by 65536.
1 Like

@rtel , thank you for explanation. I am using ARMv7-R so restrictions are same as you mentioned.

One more question: Are there any restriction concerning task stack size in reference to MPU regions size? eg. Task stack size should be equal or greater than minimal size of MPU regions? Or it does not matter?

And the last one question:
Is it possible to make Task1 stack a part of MPU region that is used in Task2 as MPU region?

One more question: Are there any restriction concerning task stack size in reference to MPU regions size? eg. Task stack size should be equal or greater than minimal size of MPU regions? Or it does not matter?

An MPU region is used to grant a task access to its stack. As a result, the memory used for task stack must satisfy all the above mentioned MPU constraints.

As an example, if the task stack size is not a power of 2, the MPU region will grant it access up to the nearest power of 2. Lets say you define a task stack size of 200 bytes, the task will have access to the additional 56 bytes (because nearest power of 2 is 256). See the following diagram:

                     A                               B
  <----------------------------------------> <-------------->
  +-----------------------------------------+---------------+
  |                                         |               |
  |      Task Stack [200 bytes]             |    56 bytes   |
  |                                         |               |
  +-----------------------------------------+---------------+
  <--------------------------------------------------------->
        MPU region used for task stack covers 256 bytes

In the above diagram, the task is not expected to have access to memory region B but it will still have access to it because the stack memory region does not satisfy MPU size constraint.

Is it possible to make Task1 stack a part of MPU region that is used in Task2 as MPU region?

It is possible to achieve that but both the tasks will have access to each other’s stack. Why would you want to do that?

Thanks.

1 Like

@aggarg Thank you.
The last question was asked just for increase knowledge about relation between task stack and other MPU regions.