1. 程式人生 > >arm體系結構學習筆記 part1--處理器模式與暫存器

arm體系結構學習筆記 part1--處理器模式與暫存器

先來看看arm920t的處理器的七種模式:

ARM920T supports seven modes of operation:

· User (usr): The normal ARM program execution state

· FIQ (fiq): Designed to support a data transfer or channel process

· IRQ (irq): Used for general-purpose interrupt handling

· Supervisor (svc): Protected mode for the operating system

· Abort mode (abt): Entered after a data or instruction prefetch abort

· System (sys): A privileged user mode for the operating system

· Undefined (und): Entered when an undefined instruction is executed

從上而下分別為:

*使用者模式:一般ARM程式執行的模式

*快速中斷模式:支援高速資料傳輸和通道處理

*外部中斷模式:通常的中斷處理

*特權模式:提供給作業系統使用的保護模式

*資料訪問終止模式:資料或者指令預取失敗後進入的模式

*系統模式:作業系統的特權使用者模式

*未定義模式:當執行未定義指令的時候進入的模式

Mode changes can be made using the control of software, or may be brought about by external interrupts or

exception processing. Most application programs will execute in User mode. The non-user modes' known as

privileged modes-are entered in order to service interrupts or exceptions, or to access protected resources.

模式的切換可以由軟體控制 也可以由外部中斷或者異常處理引起。   大部分的程式都執行在使用者模式。當響應中斷服務,異常發生,或者訪問受保護的資源時進入非使用者模式即特權模式。

***既然arm920t有七種模式,那麼處理器當前執行的模式如何獲取?   
答案是   可以通過設定cpsr的程式狀態暫存器的低五位決定了處理器的當前模式

既然談到了cpsr  那麼我們就來看看cpsr暫存器:

THE PROGRAM STATUS REGISTERS

The ARM920T contains a Current Program Status Register (CPSR), plus five Saved Program Status Registers

(SPSRs) for use by exception handlers. These register's functions are:

· Hold information about the most recently performed ALU operation

· Control the enabling and disabling of interrupts

· Set the processor operating mode

cpsr程式狀態暫存器的功能:

*儲存邏輯運算單元ALU執行的資訊

*使能或者禁止中斷

*設定處理器的操作模式

再來看看cpsr各位的資訊:

The N, Z, C and V bits are the condition code flags. These may be changed as a result of arithmetic and logical
operations, and may be tested to determine whether an instruction should be executed.

31-28:用於控制邏輯運算,邏輯運算完成可能會改變其中的某一位或者某幾位,也可能都不變。這些位也可以作為指令執行與否的判定條件。具體就不深入寫了,檢視datasheet或者看看arm指令可以明白有些指令是可以帶有條件控制的可選引數的。eg.      beq  lmain
27-8:為預留位,以後系列的處理器可能用到
I位:為IRQ中斷控制位   I=1時禁止IRQ中斷
F位:為FIQ中斷控制位    F=1時禁止FIQ中斷
T位:用來控制指令執行狀態  T=0為arm指令   T=1為thumb指令
M4, M3, M2, M1 and M0 bits (M[4:0]) 模式控制位。
具體參見下圖:

下面來看看如何讀取cpsr的內容的程式碼,並作簡要的分析:

程式碼如下  makefile就不貼了

 

這裡涉及到gcc內嵌彙編的知識    可以參照這篇博文先作一定的瞭解

執行結果:

 

看看0x600000d3是高四位為0110 低八位為1101 0011
看看處理器模式位為低五位   10011  為svc特權模式。
T位為0表示cpu運行於arm態
F位為和I位為1表示禁止IRQ和FIQ中斷

下面瞭解一下arm920t的暫存器內容:

REGISTERS

ARM920T has a total of 37 registers - 31 general-purpose 32-bit registers and six status registers - but these cannot

all be seen at once. The processor state and operating mode decides which registers are available to the

programmer.

arm920t總共有37個暫存器--31個通用的32位暫存器和6個狀態暫存器。但是這些暫存器同一時刻並不是全部可見。處理器的狀態和工作模式決定了哪些暫存器對程式設計師是可見的。

The ARM State Register Set

In ARM state, 16 general registers and one or two status registers are visible at any one time. 

在arm態,任何時候都有16個通用暫存器和一到兩個狀態暫存器是可見的。

見下圖:

簡單說一下  以使用者模式和系統模式下的r14和FIQ下的R14_fiq為例    使用者模式和系統模式下的r14是同一個物理暫存器   但是使用者模式或者系統模式下的r14和FIQ模式下的r14是兩個完全不同的物理暫存器      所以   (*^__^*) 嘻嘻……   帶左下三角的暫存器都是不同的物理暫存器   37個  有空好好數一下   嘿嘿

同理 thumb態就直接上圖了  呵呵  偷懶一下