ARM处理器基础Cortex-M4

处理器工作模式

处理器模式分为线程模式和处理模式;软件执行分特权模式和非特权模式(用户模式);堆栈分为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.

有操作系统的环境,推荐线程模式使用程序堆栈PSP,内核和异常处理使用主堆栈MSP。

https://blog.csdn.net/qq_31073871/article/details/80399334

https://blog.csdn.net/u013477200/article/details/50715621

在复位后,处理器处于线程模式+特权级;

特权到用户:在特权级下的代码可以通过置位CONTROL[0]来进入用户级。

用户到特权:用户级的程序不能简简单单地试图改写 CONTROL寄存器就回到特权级,它必须先“申诉”:执行一条系统调用指令(SVC)。这会触发SVC异常,然后由异常服务例程(通常是操作系统的一部分)接管,如果批准了进入,则异常服务例程修改 CONTROL寄存器,才能在用户级的线程模式下重新进入特权级。 事实上,从用户级到特权级的唯一途径就是异常。

By default, Thread mode uses the MSP.
To switch the stack pointer used in Thread mode to the PSP
(1) use the MSR instruction to set the Active stack pointer bit to 1, CONTROL[1] = 1
(2) perform an exception return to Thread mode with the appropriate EXC_RETURN value

When changing the stack pointer, software must use an ISB instruction immediately after the MSR instruction. This ensures that instructions after the ISB instruction execute using the new stack pointer. 

线程默认使用主堆栈MSP,若想使用程序堆栈PSP,有两种转换方式。

常用汇编指令

PUSH POP

PUSH{cond} reglist
POP{cond} reglist

• PUSH stores registers on the stack in order of decreasing register numbers, with the highest numbered register using the highest memory address and the lowest
numbered register using the lowest memory address.
• POP loads registers from the stack in order of increasing register numbers, with the lowest numbered register using the lowest memory address and the highest numbered register using the highest memory address. (编程手册p77)

push {r0, r1},由于ARM使用Full Descending堆栈,即堆栈指针指向最后一个数据,堆栈地址递减。因此,该句入栈顺序为先r1,后r0,以符合上面的描述。

MSR

Move the contents of a general-purpose register into the specified special register. 

拷贝通用Reg到特殊S reg

• ‘Rn’ is the source register.
• ‘spec_reg’ can be any of: APSR, IPSR, EPSR, IEPSR, IAPSR, EAPSR, PSR, MSPPSP, PRIMASK, BASEPRI, BASEPRI_MAX, FAULTMASK, or CONTROL.

The register access operation in MSR depends on the privilege level. Unprivileged software can only access the APSR

除APSR外,操作其它特殊寄存器,必须在特权模式下。类似指令MRS。

CPS

Change processor state.

CPSID i ; Disable interrupts and configurable fault handlers (set PRIMASK)
CPSID f ; Disable interrupts and all fault handlers (set FAULTMASK)
CPSIE i ; Enable interrupts and configurable fault handlers(clear PRIMASK)
CPSIE f ; Enable interrupts and fault handlers (clear FAULTMASK)

原文地址:https://www.cnblogs.com/yanhc/p/12643953.html