ARMプロセッサベースCortex-M 4

4187 ワード

プロセッサ動作モード
プロセッサモードはスレッドモードと処理モードに分けられる.ソフトウェアは特権モードと非特権モード(ユーザーモード)を実行し、スタックはMSPメインスタックとPSPプログラムスタックに分けられる.
処理モードでは、常に特権であり、常にプライマリスタックが使用されます.
スレッドモードでは、特権かユーザか(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を使用するには2つの変換方法があります.
共通アセンブリ命令
PUSH POP
PUSH{cond} reglistPOP{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 lowestnumbered 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. (プログラミングマニュアルp 77)
Push{r 0,r 1}は、ARMがFull Descendingスタック、すなわちスタックポインタが最後のデータを指すため、スタックアドレスが減少する.したがって、この文のスタック順序は、上述した説明に合致するように、先r 1、後r 0である.
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)