Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173676
  • 博文数量: 24
  • 博客积分: 1575
  • 博客等级: 上尉
  • 技术积分: 235
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-28 22:51
文章分类

全部博文(24)

文章存档

2016年(1)

2013年(1)

2012年(5)

2011年(9)

2010年(2)

2009年(5)

2008年(1)

我的朋友

分类: 嵌入式

2011-01-02 10:55:31

第三章 MMU Memory Management

对于MIPSMMUMemory Management, the first and yet important one we need
always
keep in mind is: No real-mode

没有实模式。这一点是MIPS CPU 的一个很重要的特点(或缺点)

我们会问了:BNNGive 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 存储体系结构

我们在这里不谈64CPU,只谈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 phyiscal
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有点象

其他CPUreal-mode方式。

*(虚拟)地址from 0x0000 0000 to 0x8000 0000 是不可以存取的,在加电时(POWER
ON)!必须等到MMU TLB初始化之后才可以。

*同理对地址from 0xC000 0000 to 0xFFFF 0000


*MIPSCPU运行有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,96TLB 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提供了48TLB pair,所以index寄存器的值是从047。换句话说,每次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可以是4K16K64K256K1M4M16M。这种可变PAGE SIZE提供了很好的灵活性,特别是对Embedded System Software. 对于Embedded System Softare,一个很大的区别就是:不允许大量的Page Fault.
这一点是传统OSGeneral OSEmbedded OS上的致命缺陷。也是为什么POSIX 1B的目的所在。传统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

下面简单谈谈MIPSJTLB

MIPS中,如R5000 JTLB is provided. JTLB stands for Joint TLB. 什么意思呢?就是
TLB buffer中包含的mixed Instruction and Data TLB 映射。有的CPUInstruction TLB Data TLB buffer 是分开的。

当然MIPS(R5000)确实还有两个小的,分开的Instruction TLBData TLB。但其大小很小。主要是为了Performance,而且是对系统软件透明的。


在这里再谈谈MMU TLBCPU Level 1 Cache的关系。

我们知道,MIPS,或大多数CPU,的Level 1 Cache都是采用Virtually Indexed and Physicall tagged. 通过这个机制,OS就不需要在每次进程切换的时候去flush CACHE。为什么呢?

举一个例子吧:

进程A的一个虚拟地址Addr1,其对应的物理地址是addre1
进程B的一个虚拟地址Addr1,其对应的物理地址是addre2;

在某个时刻,进程A在运行中,并且Addr1Level 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会错误的把进程ALevel 1中的Addr1addr1返回给CPU吗?

我们的回答应该是:不会的。
原因是:
当进程切换时,OS会将进程BASIDPID填入ASID寄存器中。请记住:对TLB的访问,(ASID + VPN)才是Primary Key.

由于MIPSCACHE属性是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 控制器。然后,我们必须等待MMUPhysical 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.
阅读(1580) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-08 17:42:22

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com