Thanks for the response you guys have shown in earlier tutorials on Cortex M3 series of microcontrollers. As I always request you, please check the data sheet or user manual for thorough and deep knowledge of NXP ARM controllers. NXP provides a solid documentation and example code support to all their microcontrollers.
In this series of tutorial we are going to discuss Operation modes and access modes of Cortex M3 microcontrollers.
Cortex-M3 has two operation modes and two privilege levels.
The operations modes are as follow:
- Thread mode :
The processor enters Thread mode on reset, or as an exception return. Privileged and unprivileged code can run in Thread mode.
- Handler mode.
The processor enters Handler mode as a result of an exception. All exceptions are handled in Handler mode. All code is privileged in Handler mode.
The Cortex-M3 supports two operation modes the thread mode for process execution and the handler mode for exception handler’s code, each mode has its own stack pointer, the process stack pointer (PSP) and the main stack pointer (MSP).
Usually the thread and handler modes use the same stack pointer thus sharing the stack memory; however, by configuring the processor to use different stack pointers, the stack memory for those two modes can be separated, consequently protecting the system stack memory form a faulty user process.
- Privileged level
- User level
The Cortex-M3 supports two access levels, user and privileged access levels, in user level access to certain registers and instructions is restricted and if an MPU is available access to memory regions, containing OS data or another process data, can also be restricted for a user process. This is mainly intended for use by a multitasking OS.
So, what are these operation modes and privilege levels?
Suppose, we are running a normal program, that time an interrupt occurred in between. According to the definition of interrupt word, the normal program has to stop where ever it is and the exception handler has to work. In this, these two modes of operation come into the picture.
When you are running a normal program the operation mode tells that this is Thread mode and when you are running exception handler, then it tells you that, this is Handler mode.
Coming to privilege levels, what exactly they do?
The privilege levels (privileged level and user level) provide a mechanism for safeguarding memory accesses to critical regions as well as providing a basic security model.
When the processor is running a main program (that means processor is in thread mode), it can be either in a privileged state or a user state, but exception handlers can only be in a privileged state. When the processor exits reset, It is in thread mode, with privileged access rights. In the privileged state, a program has access to all memory ranges (except when prohibited by MPU settings) and can use all supported instructions.J
Software in the privileged access level can switch the program into the user access level using the control register. When an exception takes place, the processor will always switch back to the privileged state and return to the previous state when exiting the exception handler. But a user program cannot change back to the privileged state by writing to the control register. It has to go through an exception handler that programs the control register to switch the processor back into the privileged access level when returning to thread mode.
Main use of Privilege levels:
The separation of privilege and user levels improves system reliability by preventing system configuration registers from being accessed or changed by some non trusted programs. If an MPU is available, it can be used in conjunction with privilege levels to protect critical memory locations, such as programs and data for OSs.
For example, with privileged accesses, usually used by the OS kernel, all memory locations can be accessed (unless prohibited by MPU setup). When the OS launches a user application, it is likely to be executed in the user access level to protect the system from failing due to a crash of non trusted user programs.
See the next figure you may get a little more clarity about this concept.
Please download Cortex-M3 Technical Reference Manual here. Guys keep commenting below to make this tutorial flawless. I need you guys support to run this website…please comment and correct me to help others. If you like this article then please like our facebook page here.