处理器模式分为线程模式和处理模式;软件执行分特权模式和非特权模式(用户模式);堆栈分为MSP Main主堆栈和PSP Program程序堆栈。
处理模式下,总是为特权,总是使用主堆栈。
线程模式下,可设置是特权还是用户(CONTROL Reg[0]),可设置使用主堆栈还是程序堆栈(CONTROL Reg[1])。
PM0214,Programming manual,STM32F3 and STM32F4 Series Cortex ® -M4 programming manual,p16
The processor modes are:
Thread mode: Used to execute application software.
The processor enters Thread mode when it comes out of reset.
The CONTROL register controls whether software execution is
privileged or unprivileged, see CONTROL register on page 24.
Handler mode: Used to handle exceptions.
The processor returns to Thread mode when it has finished exception
processing.
Software execution is always privileged.
The privilege levels for software execution are:
Unprivileged: Unprivileged software executes at the unprivileged level and:
• Has limited access to the MSR and MRS instructions, and cannot
use the CPS instruction
• Cannot access the system timer, NVIC, or system control block
• Might have restricted access to memory or peripherals
• Must use the SVC instruction to make a supervisor call to transfer control to privileged software
Privileged: Privileged software executes at the privileged level and can use all the
instructions and has access to all resources.
Can write to the CONTROL register to change the privilege level for
software execution.
In an OS environment, it is recommended that threads running in Thread mode use the process stack and the kernel and exception handlers use the main stack.
https://blog.csdn.net/qq_31073871/article/details/80399334
https://blog.csdn.net/u013477200/article/details/50715621
在复位后,处理器处于线程模式+特权级;
特权到用户:在特权级下的代码可以通过置位CONTROL[0]来进入用户级。
用户到特权:用户级的程序不能简简单单地试图改写 CONTROL寄存器就回到特权级,它必须先“申诉”:执行一条系统调用指令(SVC)。这会触发SVC异常,然后由异常服务例程(通常是操作系统的一部分)接管,如果批准了进入,则异常服务例程修改 CONTROL寄存器,才能在用户级的线程模式下重新进入特权级。 事实上,从用户级到特权级的唯一途径就是异常。
原文地址:https://www.cnblogs.com/yanhc/p/12643953.html