Stack overflow recognition

PXROS-HR implements a task stack guard — a memory space that is placed at the bottom of the stack and cannot be used by the task (see Task stack memory organization below). This memory space is set to the highest priority MPU. The MPU is set to no access in both privileged and unprivileged mode, resulting in an exception (trap) on every access to this area, even if any lower priority MPU is set with allowed access for this area (task’s stack).

The task stack guard must be large enough to protect the stack space for one complete exception frame pushed by hardware on interrupt/trap entry (26/8 words for extended/normal exception frame) or the maximum number of registers that are saved to the task (process) stack inside the kernel at once via a store-multiple instruction (16/10 words).

If the FPU is used, the task stack guard takes the last 128 bytes from the task’s stack. In case of no FPU, it only takes 64 bytes. The values come from the minimal words that need to be covered and then the size is aligned to 32 bytes to fulfill MPU alignment requirements.

128 bytes = 26 words (104 bytes) aligned to 32 bytes
Code 1. Task stack guard size definintion in "pxdef.h"
/* Task stack guard size in bytes.
 * The task stack guard must be large enough to protect the stack space for one complete
 * exception frame pushed by hardware on interrupt/trap entry (26/8 words for
 * extended/normal exception frame) or the maximum number of registers that are saved to
 * the task (process) stack inside the kernel at once via a store-multiple instruction
 * (16/10 words). */
#if defined(__ARM_PCS_VFP)
    #define PX_TASK_STACK_GUARD_SIZE  PXMEM_ADJUST(26 * sizeof(PxUInt32_t))
#else
    #define PX_TASK_STACK_GUARD_SIZE  PXMEM_ADJUST(10 * sizeof(PxUInt32_t))

The stack grows downwards, meaning it goes from the end address to the start address because, in PXROS-HR, the start address is lower than the end address.

stack description
Fig. 1. Task stack memory organization