--- title: ARMv8 categories: arm, armv8, arm64, embedded ... ARMv8架構介紹 ===================================== .. image:: http://www.arm.com/zh/images/roadmap/V5_to_V8_Architecture.jpg ARMv8架構有一個重要的特點是他與其之前的架構相容。 Execution state ---------------------------- - AArch64 - 64-bit Execution state. - 提供 31組 64-bit 的通用暫存器 - 提供 64-bit Program Counter(PC), Stack-Poiner(SP)與Exception-Link-Register (ELR) - 定義最多四種(EL0 - EL3)特權模式 - 支援 64-bit 虛擬地址 - 定義一組PSTATE來保存PE state - AArch32 - 32-bit Execution state - 提供 16組 32-bit 的通用暫存器 - 提供 1組 ELR,作為從Hyp-Mode的Exception返回之用 - 提供 A32(相容ARMv7 ARM)與 T32(相容ARMv7 Thumb)兩種指令集 - 使用32-bit 虛擬地址 - 使用單一的CPSR來保存PE state 在A32與T32中做切換只要透過BX即可,但要在AArch32與AArch64間做切換只能透過Exception .. image:: http://loda.hala01.com/wp-content/uploads/2014/12/image005.png (圖片來自loda大大的blog) AArch64 指令集 (instruction set) 介紹 ===================================== ARMv8-A_Architecture_Reference_Manual P.111 系統層級架構 (System Level Architecture) ===================================== ARMv8-A_Architecture_Reference_Manual P.1405 例外層級 (Exception levels) ---------------------------- ARMv8-A_Architecture_Reference_Manual P.1408 - ARMv8-A架構定義了四個例外層級,分別為EL0到EL3,其中數字越大代表特權(privilege)越大。 - EL0: 無特權模式(unprivileged) - EL1: 作業系統核心模式(OS kernel mode) - EL2: 虛擬機器監視器模式(Hypervisor mode) - EL3: TrustZone® monitor mode - 要提升到較高層級需要透過exceptions(如: 中斷、page faults等)。 - EL0 => EL1: SVC (system call) - EL1 => EL2: HVC (hypervisor call) - EL2 => EL3: SMC (secure monitor call) - 在轉換時會將返回位址(return address)紀錄在例外連結暫存器ELR(Exception-Link-Register)。 - 每個EL會有個別的SP(stack pointer) .. image:: /embedded/armv8 arch.png 安全性狀態 (Security state) --------------------------------------------------------------------------- ARMv8-A架構提供兩種安全性狀態,他們有個別的實體記憶體定址空間(Secure physical address space)。 - 安全狀態(Secure state): PE可以存取安全及不安全的實體定址空間 - 不安全狀態(Non-Secure state): 只能存取不安全的實體定址空間 虛擬化 (Virtualization) ------------------------------------------------------------------------------------ 這邊的虛擬化只有支援有實現EL2的架構。以下為其基礎模型: - 一個跑在EL2的Hypervisor負責切換跑在EL1、EL0的virtual machines - 一些跑在virtual machines上(在EL1中)的Guest OS - 每個Guest OS上跑在EL0的應用程式 每個VM會被Hypervisor指定一個VMID。 EL2只會實現在 Non-secure state,並負責: - 提供虛擬值給少數特定的暫存器(1)。Guset OS 或其上的應用程式讀取這些暫存器時會得到虛擬的值。 - Trapping: 當在做記憶體管理及存取其他大多數的暫存器((1)之外的)時會產生exception並由EL2處理。 - Routing interrupt: 將中斷分配給 - 現在的Guest OS - 現在沒在執行的Guest OS - hypervisor (以上會在個別的章節特別探討) 實現EL2包含以下實作: - Hypervisor Call (HVC) exception - Traps to EL2 - 虛擬中斷: - 包括: - Virtual SError - Virtual IRQ - Virtual FIQ - 所有虛擬中斷會由EL1處理 - 每個虛擬中斷可由EL2個別啟用 - 每個虛擬中斷都會有其對應的實體中斷 - 當一個虛擬中斷被啟用時,其對應的實體中斷會由EL2處理(除非EL3指定他要處理) 指令與例外處理暫存器 (Registers for instruction processing and exception handling) ------------------------------------------------------------------------------------ 31 個通用暫存器R0-R30。可以作為64-bit暫存器X0-X30或是 31個 32-bit暫存器W0-W30來被存取。 SP暫存器 (stack pointer registers) ...................................... - 每個例外層級都有專用的SP: SP_EL0 ~ SP_EL3 (SP_EL2、SP_EL3只有在有實現該層的架構時才會存在) - 例外(exception)發生時,預設會使用例外目標層級的SP_ELx (例如例外目標為EL1就會選用SP_EL1) - 也可透過更新PSTATE.SP.選用SP_EL0 - 可以透過t(SP_EL0)、h(SP_ELx)的後綴表明SP的例外層級 .. image:: /embedded/sp_th.png SIMD 與浮點暫存器 ...................................... - 共用32個128位暫存器V0-V31 - 可作為以下來存取 - 32 doubleword (64-bit) registers, D0-D31. - 32 word (32-bit) registers, S0-S31. - 32 halfword (16-bit) registers, H0-H31. - 32 byte (8-bit) registers, B0-B31. 程式狀態儲存暫存器SPSRs (Saved Program Status Registers) ............................................................................ - 在例外發生時用來儲存PE的狀態 - 在AArch64中,每個例外層級都有一個SPSR: SPSR_EL1 ~ SPSR_EL3 - 在例外發生時,會把PE的狀態存在目標層級的SPSR中 - 當從例外返回時,PE的狀態會從SPSR中載入 .. image:: /embedded/SPSR.png (請對照 P.1418) 例外連結暫存器ELRs (Exception Link Registers) ............................................. - 保存例外返回的位置 - 例如: 當例外目標為EL1時,返回的位置會存在ELR_EL1中 - 從例外返回時,會載入ELR的位只到PC中 - 當例外是從 AArch32 到AArch64 時 ELR bits[63:32]被設為0。 虛擬記憶體系統架構 (Virtual Memory System Architecture) ------------------------------------------------------------------------------------ ARMv8-A_Architecture_Reference_Manual P.1707 概述 ...................................... Virtual Memory System Architecture簡稱VMSA。 VMSA有記憶體管理單元(MMU)來負責控制地址轉換(address translation)。 地址轉換在不同例外層級與安全狀態之間是獨立的。 .. image:: /embedded/mm.jpg - 虛擬地址Virtual Address (VA) - instruction 使用的地址(PC, LR, ELR, SP 保存的地址) - 最大為48 bits - EL1&0 的VA範圍分為兩個子範圍。 0x0000_0000_0000_0000 ~ 0x0000_FFFF_FFFF_FFFF 及 0xFFFF_0000_0000_0000 ~ 0xFFFF_FFFF_FFFF_FFFF。 - 中間實體地址Intermediate Physical Address (IPA) - 當提供二階地址轉換時,作為第一階的輸出,第二階的輸入。 - 當只提供一階地址轉換時,IPA即為PA - 最大為48 bits - 實體地址Physical Address (PA) - 在實體記憶體MAP中的位址 - EL3 和 Secure EL1 例外層及提供獨立的Secure、Non-secure實體定址空間。 - Secure state存取VA可以被轉換成Secure或Non-secure的實體定址空間 - Non-secure state時虛擬地址只能對應到Non-secure state的實體定址空間 - 地址轉換: - Translation table base register (TTBR) 指向translation table的起始位置。 - EL1&0 stage 1 translation 需要兩個translation table,一個是對低位址定址空間,一個是對高位址。 .. image:: /embedded/TTBR01.jpg - TLB 可以cache Translation table entries 地址轉換階級控制 (Controlling address translation stages) ............................................................................ - 不同的implementation,translation stages的實作也會不同 .. image:: /embedded/AArch64_translation_stages.jpg - 對每個地址轉換階級: - A system control register(CR) bit 啟用該層地址轉換 - A CR bit 決定轉換表查找的 endianness - Translation Control Register (TCR)控制該層地址轉換 - 若該層VA支援分開的VA 範圍(EL0、EL1),則提供 Translation Table Base Register (TTBR)給每個子範圍。即: - 一個TCR - 一個TTBR給每個VA子範圍 - TTBR保存查找所需的base address - MMU相應的系統控制暫存器 .. image:: /embedded/CR.jpg - stage 2 translation 是ARM 對記憶體虛擬化的擴展。 虛擬化必須讓Guest OS不能存取hypervisor的記憶體空間。在沒有虛擬化擴展的情況下,是透過"shadow page tables"來完成這個要求。 - Shadow page tables: hypervisor 建立屬於自己的page table,當他發現有page fault時,會讀取OS所建立的page table及OS map的地址,再實際map到MMU使用的page table,因此只有hypervisor實際能把VA轉換成PA。缺點是複雜及耗能。 - With ARM Virtualization extensions: 將上面的動作交給硬體來做,hypervisor讓硬體認為guest OS產生的實體地址為虛擬地址,然後再以此地址(IPA)做第二階translation。 .. image:: http://www.futurechips.org/wp-content/uploads/2013/03/stage-1-translation-3.png (圖片來自http://www.futurechips.org/understanding-chips/arm-virtualization-part-2-memory-interrupts.html) 記憶體轉換顆粒大小(Memory translation granule size) (暫譯) ............................................................................ - 定義了: - 單一轉換表的最大大小 - page size - VMSAv8-64支援4KB, 16KB, and 64KB的translation granule sizes .. image:: /embedded/granule size.jpg - granule size決定了page及一次查找可以轉換的bit數 .. image:: /embedded/4KB.jpg .. image:: /embedded/16KB.jpg .. image:: /embedded/64KB.jpg Translation table walks ................................................................. - 從讀取轉換表開始,TTBR會保存該階級的base address - 每次查找會返回一個descriptor標明以下其中一個情況: - 該entry是walk的最後一個entry。此entry包含該次存取的OA、權限及屬性。 - 需要額外層級的查找。 此entry包含該層級查找的translation table base address。 - descriptor提供final translation的 hierarchical attributes - invalid: Translation fault .. image:: /embedded/stage of address translation.jpg 通用計時器 (The Generic Timer) ------------------------------------------------------------------------------------ ARMv8-A_Architecture_Reference_Manual P.1855 reference ===================================== * `ARMv8-A_Architecture_Reference_Manual_(Issue_A.a) (需登入)`_ * `loda armv8-與-linux的新手筆記`_ * http://www.slideshare.net/badaindonesia/linux-on-arm-64bit-architecture?related=1 * `Principles of ARM® Memory Maps `_ * http://www.futurechips.org/understanding-chips/arm-virtualization-part-2-memory-interrupts.html