啟動過程
https://www.cnblogs.com/strongwong/p/8657639.html
處理器工作模式
處理器模式分為線程模式和處理模式;軟件執行分特權模式和非特權模式(用戶模式);堆棧分為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, MSP, PSP, 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)