This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Cortex M0+ Problems using MPU + SVC Call

Hi, i am using an NXP S32K118 Microcontroller ( ARM Cortex M0+ ) And im having issues combining the usage of the MPU ( Memory protection unit ) + the SVC Calls

I need to transition between privileged/unprivileged functions, and implement memory protection, so im doing the following:

I have a main "scheduler" function that runs in thread mode with privileges and uses the main stack pointer. This function is used to call the rest of the tasks, which use the process stack pointer and run in thread mode that can be privileged or unprivileged, depending on some specific settings.

The main stack is protected. unprivileged access is read only, and privileged access can read/write/execute

In order to switch from this main scheduler function, to the rest of the functions, I am using an SVC call, where i initialize the process stack for the next task, and set the corresponding privileges before jumping onto the next function by setting the corresponding EXEC_RETURN value into LR register ( In order to return from the SVC call using the process stack pointer ).

So at this point, everything works fine, I have jumped into the corresponding task and i´m using the process stack pointer with the configured privileges ( that can be privileged or unprivileged for the current task )

In order to re-enable privileges, and get back to the main scheduler function, I call another SVC that re-enables the privileges and returns using the corresponding EXEC_RETURN to return from the SVC call using the main stack pointer, so I´m back on the main scheduler function.

The problem I have is that if the current task has no privileges, when I use the SVC Call to regain privileges and return to the main scheduler function, i get a hardfault error because the MPU denies the access to the main stack, as this region is protected for unprivileged access (unprivileged read only, privileged full access). This only happens when the current task has no privileges, if the current task has privileges everything works fine. 

So, what is the problem here? I though the SVC Calls always execute in handler mode, and this mode is always privileged, so i didnt expect this error, as the memory is only protected for unprivileged accesses