Skip to content

MPU fault during application startup #14471

@nordic-krch

Description

@nordic-krch

Describe the bug
Application can hit mpu fault before starting main thread. This is caused by stack misuse. In function z_arch_switch_to_main_thread thread entry is provided as forth argument (_main). Then within the function stack pointer is changed:

__set_PSP((u32_t)start_of_main_stack);

After stack is changed, call to thread entry occurs which is using _main as the argument. If variable is stored on stack, it will be fetched from stack but at that point this is already different stack and value is corrupted:

z_thread_entry(_main, 0, 0, 0);

So _main variable will be corrupted.

To Reproduce
Modify hello_world example by using following prj.conf:

CONFIG_MPU_STACK_GUARD=y
CONFIG_NO_OPTIMIZATIONS=y
CONFIG_LOG=y

Steps to reproduce the behavior:

  1. mkdir build; cd build
  2. cmake -GNinja -DBOARD=nrf52840_pca10056 ..
  3. ninja flash
  4. See error
Fatal fault in essential thread! Spinning...
***** MPU FAULT *****
  Instruction Access Violation
***** Hardware exception *****
Current thread ID = 0x200005d8
Faulting instruction address = 0x20001b48
Fatal fault in essential thread! Spinning...

Expected behavior
Sample should start.

Impact
Suddenly system crashes during initialization.

Metadata

Metadata

Assignees

Labels

bugThe issue is a bug, or the PR is fixing a bugpriority: highHigh impact/importance bug

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions