《mips-24kf-manual.gliethttp.pdf》p89頁 所以mips複位和中斷髮生,都會自動進入kernel模式 . The core enters Kernel mode both at reset and when an exception is recognized. . In User mode, virtual addresses 0x8000_0000 to 0xFFFF_FFFF are invalid and cause an exception if accessed. . An unmapped segment does not use the TLB or the FM to translate from virtual-to-physical addresses. Especially after reset, it is important to have unmapped memory segments, because the TLB is not yet programmed to perform the translation. . Unmapped segments have a fixed simple translation from virtual to physical address. This is much like the transla- tions the FM provides for the core, but we will still make the distinction. . Except for kseg0, unmapped segments are always uncached. The cacheability of kseg0 is set in the K0 field of the CP0 registe . A mapped segment does use the TLB or the FM to translate from virtual-to-physical addresses. . For the core with the FM MMU, the mapped segments have a fixed translation from virtual to physical address. . kseg0, the physical address selected is defined by subtracting 0x8000_0000 from the virtual address. . Optionally, the MMU can be based on a simple algorithm to translate virtual addresses into physical addresses via a Fixed Mapping (FM) mechanism. These translations are different for various regions of the virtual address space (useg/kuseg, kseg0, kseg1, kseg2/3). ======================================== 對 於MIPS的MMU和Memory Management, the first and yet important one we need always keep in mind is: No real-mode 沒有實模式。這一點是MIPS CPU 的一個很重要的特點(或缺點)。 我們會問了:BNN,Give me a break. Without CPU running in the real-mode, how could you boot up a kernel? Well, here is the thing: Bydefault, MIPS architecture , when power on, has enabled/mapped two memory areas. In other words, those two memory areas are the places where your boot codes HAVE TO resident and run on top of. If you read the makefiles of MIPS linux source tree, you would easily find the infor. For example, 0x8000xxxx or some things like that. MIPS 儲存體繫結構 我 們在這裡不談64位CPU,只談32位的。 MIPS將儲存空間劃分為4大塊--kuseg, kseg0,kseg1 and kseg2. ------------------------------------------------------------------ 0xFFFF FFFF mapped kseg2 0xC000 0000 unmapped uncached kseg1 0xA000 0000 unmapped cached kseg0 0x8000 0000 2G kuseg 0x0000 0000 ------------------------------------------------------------------ 對 於上述圖表,弟兄們要記住以下幾點: * 當開電(Power On)的時候,只有kseg0 and kseg1 是可以存取的。 *kseg0 512M(From 0x8000 0000 to 0xA000 0000) are DIRECTLY mapped to physical memory ranging from 0x0000 0000 to 0x2000 0000, with cache-able(either write back or write through, which is decided by SR(Status Register of MIPS CPU) *kseg1 512M(From 0xA000 0000 to 0xC000 0000) are (also) DIRECTLy mapped to physical memory ranging from 0x0000 0000 t0 0x2000 0000, with non-cachable. 以上兩點對於理解MIPS OS的啟動是至關重要的。細心的讀者會發現:kseg1有點象其他CPU的real-mode方式。 *(虛擬)地址from 0x0000 0000 to 0x8000 0000 是不可以存取的,在加電時(POWERON)!必須等到MMU TLB初始化之後才可以。 *同理對地址 from 0xC000 0000 to 0xFFFF 0000 *MIPS的CPU運行有3個態--User Mode; Supervisor Mode and Kernel Mode.For simplicity, let's just talk about User Mode and Kernel Mode. Please always keep this in mind: CPU can ONLY access kuseg memory area when running in User Mode CPU MUST be in kernel mode or supervisor mode when visiting kseg0, kseg1 and kseg2 memory area. * MMU TLB MIPS CPU通過TLB 來translates all virtual addresses generated by the CPU.對於這一點,這裡不多廢話。 下面談談ASID(Address Space Identifier). Basically, ASID, plus the VA(Virtual Address) are composed of the primary key of an TLB entry. 換句話說,虛擬位址本身是不能唯一,確定一個TLB entry的。一般而言,ASID的值就是相應的process ID. Note that ASID can minimized TLB re-loads, since several TLB entries can have the same virtual page number, but different ASID's. 對於一個多任務作業系統來講,每個任務都有自己的4G虛擬空間,但是有自己的ASID。 MMU 控制寄存器 對於一個Kernel Engineer來說,對MMU的處理主要是通過MMU的一些控制寄存器來完成的。MIPS體繫結構中整合了一個叫做System Control Coprocessor (CP0)的組件。CP0就是我們常說的MMU控制器。在CP0中,除了TLB entry(例如,對RM5200,有48pair,96個TLB entry),一些控制寄存器提供給OS KERNEL來控制MMU的行為。 每 個CP0控制寄存器都對應一個唯一的寄存器號。MIPS提供特殊的指令來對CP0進行操作。 mfc0 reg. CP0_REG mtc0 reg. CP0_REG 我們通過上述的兩條指令來把一個GPR寄存器的值assign給一個CP0寄存器,從而達到控制MMU的目的。 下 面簡單介紹幾個與TLB相關的CP0控制寄存器。 Index Register 這個寄存器是用來指定TLB entry的,當你進行TLB讀寫的時候。我們已經知道,例如,MIPS R5提供了48個TLB pair,所以index寄存器的值是從0到47。換句話說,每次TLB寫的行為是對一個pair發生的。這一點是與其他的CPU MMU TLB 讀寫不同的。 EntryLo0, EntryLo1 這兩個寄存器是用來specify 一個TLB pair的偶(even)和奇(odd)物理(Physical)頁面地址。 一定要注意的是:EntryLo0 is used for even pages; EntryLo1 is used for odd pages.Otherwise, the MMU will get exception fault. Entry Hi Entry Hi寄存器存放VPN2,或一個TLB的虛擬位址部分。注意的是:ASID value也是在這裡被體現。 Page Mask MIPS TLB提供可變大小的TLB地址映射。一個PAGE可以是4K,16K,64K,256K,1M,4M或16M。這種可變PAGE SIZE提供了很好的靈活性,特別是對Embedded System Software. 對於Embedded System Softare,一個很大的區別就是:不允許大量的Page Fault.這一點是傳統OS或General OS在Embedded OS上的致命缺陷。也是為什麼POSIX 1。B的目的所在。傳統OS儲存管理的一個原則就是:Page On Demand.這對大多Embedded System是不允許的。 For embedded system,往往是需要在系統初始化的時刻就對所有的儲存進行configuration, 以確保在系統運行時不會有Page Fault. 上述幾個寄存器除了MAP一個虛擬頁面之外,還包括設定一個頁面的屬性。其中包括: writable or not; invalide or not; cache write back or write through 下面簡單談談 MIPS的JTLB。 在MIPS中, 如R5000, JTLB is provided. JTLB stands for Joint TLB. 什麼意思呢?就是 TLB buffer中包含的mixed Instruction and Data TLB 映射。有的CPU的Instruction TLB 和Data TLB buffer 是分開的。 當然MIPS(R5000)確實還有兩個小 的,分開的Instruction TLB和Data TLB。但其大小很小。主要是為了Performance,而且是對系統軟體透明的。 在這 裡再談談MMU TLB和CPU Level 1 Cache的關係。 我們知道,MIPS,或大多數CPU,的Level 1 Cache都是採用Virtually Indexed and Physicall tagged. 通過這個機制,OS就不需要在每次進程切換的時候去flush CACHE。為什麼呢? 舉一個例子吧: 進程A的一個虛擬位址Addr1, 其對應的物理地址是addre1; 進程B的一個虛擬位址Addr1,其對應的物理地址是addre2; 在某個時刻,進程A在運行中,並且 Addr1在Level 1 CACHE中。 這時候,OS does a context swith and bring process B up, having process A sleep. Now, let's assume that the first instruction/data fetch process B does is to access its own virtual address Addr1. 這時候CPU會錯誤的把進程A在Level 1中的Addr1的addr1返回給CPU嗎? 我們的回答應該 是:不會的。 原因是: 當進程切換時,OS會將進程B的ASID或PID填入ASID寄存器中。請記住:對TLB的訪問,(ASID + VPN)才是Primary Key. 由於MIPS的CACHE屬性是Virtually Indexed, Physically tagged.所以,任何地址的訪問,CPU都會issue the request to MMU for TLB translation to get the correct physical address, which then will be used for level cache matching. 與此同時,CPU會把虛擬位址訊號傳給Level 1 Cache 控制器。然後,我們必須等待MMU的Physical Address資料。只有physical tag也 匹配上了,我們才能說一個:Cache Hit. 所以,我們不需要擔心不同的進程有相同的虛擬位址的事情。 弟兄們可以重溫一下我們講過的Direct Mapped; Full Associative, and Set Associative. 從而理解為什麼Cache中可以存在多個具有相同虛擬位址的entry. For example,the above Addr1 for proccess A and Addr1 for process B. |