mips的内存管理-kseg0,kseg1虚拟和物理地址映射理解

来源:互联网 发布:淘宝职业买手 编辑:程序博客网 时间:2024/05/21 06:49

《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.

 
原创粉丝点击