收藏 分销(赏)

MIPS体系结构.doc

上传人:pc****0 文档编号:7825903 上传时间:2025-01-19 格式:DOC 页数:44 大小:292KB
下载 相关 举报
MIPS体系结构.doc_第1页
第1页 / 共44页
MIPS体系结构.doc_第2页
第2页 / 共44页
MIPS体系结构.doc_第3页
第3页 / 共44页
MIPS体系结构.doc_第4页
第4页 / 共44页
MIPS体系结构.doc_第5页
第5页 / 共44页
点击查看更多>>
资源描述

1、MIPS CPU 体系结构概述 http:/www.xtrj.org/mips/陈怀临1。序言 本文介绍MIPS体系结构,着重于其寄存器约定,MMU及存储管理,异常和中断处理等等。 通过本文,希望能提供一个基本的轮廓概念给对MIPS CPU及之上OS有兴趣的读者。 并能开始阅读更详细的归约(SPECIFICATION)资料。 MIPS是最早的,最成功的RISC(Reduced Instruction Set Computer)处理 器之一,起源于Stanford Univ的电机系. 其创始人 John L. Hennessy在1984年在硅谷创立 了MIPS INC. 公司()。John L.

2、 Hennessy目前是Stanford Univ. 的校长。在此之前,他是 Stanford电子工程学院的Dean。CS专业的学生都知道两本著名的书: “Computer Organization and Design : The Hardware/Software Interface” 和 ”Computer Architecture : A Quantitative Approach“。其Co-author就是Hennessy. MIPS的名字为“Microcomputer without interlocked pipeline stages的缩写。另外一个通常的非正式的说法是”Mil

3、lions of instructions per second. MIPS 芯片在工业界目前用的比较多的是:MIPS INC。的R10000;QED(。1996年从MIPS INC。分(SPIN OFF)出来的)的R5000, R7000等。指令集 详细的资料请参阅MIPS归约。 一般而言,MIPS指令系统有:MIPS I;MIPS II;MIPS III 和MIPS IV。可想而知,指令系统是向后兼容的。例如,基于MIPS II的代码可以在MIP III和MIPS IV的处理器上跑一跑:-) 下面是当我们用gcc时,如何指定指令和CPU的选项。 -mcpu=cpu type Assume

4、the defaults for the machine type cpu type when scheduling instructions. The choices for cpu type are r2000, r3000, r4000, r4400, r4600, and r6000. While picking a specific cpu type will schedule things appropriately for that particular chip, the compiler will not generate any code that does not mee

5、t level 1 of the MIPS ISA (instruction set architecture) without the -mips2 or -mips3 switches being used. -mips1 Issue instructions from level 1 of the MIPS ISA. This is the default. r3000 is the default cpu type at this ISA level. -mips2 Issue instructions from level 2 of the MIPS ISA (branch like

6、ly, square root instructions). r6000 is the default cpu type at this ISA level. -mips3 Issue instructions from level 3 of the MIPS ISA (64 bit instructions). r4000 is the default cpu type at this ISA level. This option does not change the sizes of any of the C data types. 读者可能发现,对于大多数而言,我们应该是用MIPS I

7、II或-mips3。要提醒的是R5000和R10000也都是R4000的延伸产品。 下面是几点补充: *MIPS指令是32位长,即使在64位的CPU上。这对于局部跳转指令的理解很有帮助。 比如:J (TARGET);JAL (TARGET)。J和JAL的OPERCODE是6位,剩下的26为存放跳转偏移量。由于任何一个指令都是32位(或4字节)对齐(ALIGN)的,所以J 和JAL最大的伸缩空间是228=256M。如果你的程序要作超过256M的跳转,你就必须用JALR或JR,通过一个GPR寄存器来存放你的跳转地址。由于一个寄存器是32或64位的,你就没有任何限制了。 *MIPS CPU的SR(S

8、TATUS REGISTER)中有几位是很重要的设置,当我们选择指令系统或要用64位的MIPS的CPU CORE在32模式下(绝大多数情况,弟兄们 别告诉我你在写64位的程序:-) )。 SRXX: 1:MIPS IV INSTRUCTION SET USABLE 0:MIPS IV INSTRUCTION SET UNUSABLE SRKX SRSX SRUX: 0:CPU工作在32位模式下 1:CPU工作在64位模式下 一般而言,如果你要从头写一个MIPS核心为32位程序,最好把上述值设为0。为什么最好呢?因为我在工作中没有去冒风险,设她们为1,who knows what would h

9、appen?:-) And then why bother:-)? *在以后我们会单独的一章讲将流水线和指令系统,特别是跳转指令的关系。在这里,我们只简单提一下。对任何一个跳传指令后面,FOR SIMPLITY,要加上一个空转指令(NOP)。从而使得CPU的PIPELINE不会错误的执行一个预取(PRE_FETCH)得指令。当然这个NOP可以替换为别的。以后再讲。放一个NOP是最简单和安全的。有兴趣的读者可以用mips64-elf-objdump -d 来反汇编一个 OBJECT文件。你就会一目了然了。 *一定要记住:MIPS I,II, III和IV指令系统不包含PRIVILEDGED IN

10、STRUCTIONS。 换句话说,都是那些在USER MODE下可以用的指令(当然KERNEL下也能用)。对于CPO的操作不属于指令系统。 *有一点在MIPS CPU下,要千万注意:ALIGN。MIPS对ALIGN的要求是严厉的。这一点与POWERPC是天壤之别。指令必须是32位对齐。数据类型必须在她们的的 大小边界对齐。简单的比如:When CPU running under 32bit mode, int must 32bit aligned; long 32bit aligned; pointer must be 32bit aligned; char must 8 bit aligne

11、d. long long must 64 bit aligned;关于这一点 ,我是吃过苦头的。当然我知道大家还会犯错在这里:-),即使知道了。有些事情学是没用的:-)。一定要注意。 *我建议读者阅读SPECIFICATION时要花时间看一看指令系统的定义。其实不难。每一种指令不外乎几个域(FIELDS)。寄存器约定 对于在一个CPU上进行开发,掌握其工作的CPU的寄存器约定是非常重要的。 MIPS体系结构提供了32个GPR(GENERAL PURPOSE REGISTER)。这32个寄存器的用法大致如下: REGISTER NAME USAGE $0 $zero 常量0(constant v

12、alue 0) $2-$3 $v0-$v1 函数调用返回值(values for results and expression evaluation) $4-$7 $a0-$a3 函数调用参数(arguments) $8-$15 $t0-$t7 暂时的(或随便用的) $16-$23 $s0-$s7 保存的(或如果用,需要SAVE/RESTORE的)(saved) $24-$25 $t8-$t9 暂时的(或随便用的) $28 $gp 全局指针(Global Pointer) $29 $sp 堆栈指针(Stack Pointer) $30 $fp 帧指针(Frame Pointer) (BNN:f

13、p is stale acutally, and can be simply used as $t8) $31 $ra 返回地址(return address) 对一个CPU的寄存器约定的正确用法是非常重要的。当然对C语言开发者不需要关心,因为COMPILER会TAKE CARE。但对于KERNEL的开发或DRIVER开发的人就*必须*清楚。 一般来讲,你通过objdump -d可以清醒的看到寄存器的用法。 下面通过我刚才写的一个简单例子来讲解: / vi Hello.c Hello.c New file /* Example to illustrate mips register conve

14、ntion * -Author: BNN * 11/29/2001 */ int addFunc(int,int); int subFunc(int); void main() int x,y,z; x= 1; y=2; z = addFunc(x,y); int addFunc(int x,int y) int value1 = 5; int value2; value2 = subFunc(value1); return (x+y+value2); int subFunc(int value) return value-; 上面是一个C程序,main()函数调用一个加法的子函数。让我们来看

15、看编译器是如何产生代码的。 /bnn:74 /bin/mips-elf-gcc -c Hello.o Hello.c -mips3 -mcpu=r4000 -mgp32 -mfp32 -O1 /bnn:75 /bin/mips64-elf-objdump -d Hello.o Hello.o: file format elf32-bigmips Disassembly of section .text: /* main Function */ 0000000000000000 : /*create a stack frame by moving the stack pointer 8 *byt

16、es down and meantime update the sp value */ 0: 27bdfff8 addiu $sp,$sp,-8 /* Save the return address to the current sp position.*/ 4: afbf0000 sw $ra,0($sp) 8: 0c000000 jal 0 /* nop is for the delay slot */ c: 00000000 nop /* Fill the argument a0 with the value 1 */ 10: 24040001 li $a0,1 /* Jump the

17、addFunc */ 14: 0c00000a jal 28 /* NOTE HERE: Why we fill the second argument *behind the addFunc function call? * This is all about the -O1 compilation optimizaiton. * With mips architecture, the instruciton after jump * will also be fetched into the pipline and get * exectuted. Therefore, we can pr

18、omise that the * second argument will be filled with the value of * integer 2. */ 18: 24050002 li $a1,2 /*Load the return address from the stack pointer * Note here that the result v0 contains the result of * addFunc function call */ 1c: 8fbf0000 lw $ra,0($sp) /* Return */ 20: 03e00008 jr $ra /* Res

19、tore the stack frame */ 24: 27bd0008 addiu $sp,$sp,8 /* addFunc Function */ 0000000000000028 : /* Create a stack frame by allocating 16 bytes or 4 * words size */ 28: 27bdfff0 addiu $sp,$sp,-16 /* Save the return address into the stack with 8 bytes * offset. Please note that compiler does not save t

20、he * ra to 0($sp). *Think of why, in contrast of the previous PowerPC * EABI convention */ 2c: afbf0008 sw $ra,8($sp) /* We save the s1 reg. value into the stack * because we will use s1 in this function * Note that the 4,5,6,7($sp) positions will then * be occupied by this 32 bits size register */

21、30: afb10004 sw $s1,4($sp) /* Withe same reason, save s0 reg. */ 34: afb00000 sw $s0,0($sp) /* Retrieve the argument 0 into s0 reg. */ 38: 0080802d move $s0,$a0 /* Retrieve the argument 1 into s1 reg. */ 3c: 00a0882d move $s1,$a1 /* Call the subFunc with a0 with 5 */ 40: 0c000019 jal 64 /* In the de

22、lay slot, we load the 5 into argument a0 reg *for subFunc call. */ 44: 24040005 li $a0,5 /* s0 = s0+s1; note that s0 and s1 holds the values of * x,y, respectively */ 48: 02118021 addu $s0,$s0,$s1 /* v0 = s0+v0; v0 holds the return results of subFunc *call; And we let v0 hold the final results */ 4c

23、: 02021021 addu $v0,$s0,$v0 /*Retrieve the ra value from stack */ 50: 8fbf0008 lw $ra,8($sp) /*!restore the s1 reg. value */ 54: 8fb10004 lw $s1,4($sp) /*! restore the s0 reg. value */ 58: 8fb00000 lw $s0,0($sp) /* Return back to main func */ 5c: 03e00008 jr $ra /* Update/restore the stack pointer/f

24、rame */ 60: 27bd0010 addiu $sp,$sp,16 /* subFunc Function */ 0000000000000064 : /* return back to addFunc function */ 64: 03e00008 jr $ra /* Taking advantage of the mips delay slot, filling the * result reg v0 by simply assigning the v0 as the value *of a0. This is a bug from my c source * codes-val

25、ue-. I should write my codes * like -value, instead. 68: 0080102d move $v0,$a0 希望大家静下心来把上面的代码看懂。一定要注意编译器为什么在使用s0和s1之前要先把她们SAVE起来,然后再RESTORE,虽然在这个例子中虽然main 函数没用s0和s1。 另外的一点是:由于我们加了“-O1”优化,编译器利用了“delay slot来执行那些必须执行的指令,而不是简单的塞一个”nop指令在那里。非常的漂亮。 最后,考大家一个问题,为了使得大家更加理解寄存器的用法: *在写一个核心调度context switch()例程时

26、,我们需要SAVE/RESTORE$t0-$t7吗?如果不,为什么? *在写一个时钟中断处理例程时,我们需要SAVE/RESTORE$t0-$t7吗?如果是,为什么?MMU和 Memory Management 对于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 th

27、e 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 l

28、inux 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

29、 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 MI

30、PS 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 是不可以存取的,在加电时(POWER ON)!必须等到MMU TLB初始化之后才

31、可以。 *同理对地址from 0xC000 0000 to 0xFFFF 0000 *MIPS的CPU运行有3个态-User Mode; Supervisor Mode and Kernel Mode. For simplicity, lets 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 supervi

32、sor 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. 换句话说,虚拟 地址本身是不能唯

33、一 确定一个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 ASIDs. 对于一个多任务操 作系统来讲,每个任务都有 自己的4G虚拟空间,但是有自己的ASID。MMU 控制寄存器 对于一个Kernel Engineer来说,对MMU的处理主要是通过MMU的一些控制寄存器来完成的。 MIPS体系结构中集成了一个叫做System

34、 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 Registe

35、r 这个寄存器是用来指定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. Otherwis

36、e, 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在Em

37、bedded 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。

38、 在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,或大多数

39、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

40、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.所以,任何地

41、址的访问,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 M

42、apped; Full Associative, and Set Associative. 从而理解为什么Cache中可以存在多个具有相同虚拟地址的entry. For example,the above Addr1 for proccess A and Addr1 for process B.MIPS 异常和中断处理(Exception and Interrupt handling) 任何一个CPU都要提供一个详细的异常和中断处理机制。一个软件系统,如操作系统,就是一个时序逻辑系统,通过时钟,外部事件来驱动整个预先定义好的逻辑行为。这也是为什么当写一个操作系统时如何定义时间的计算是非常重要的

43、原因。 大家都非常清楚UNIX提供了一整套系统调用(System Call)。系统调用其实就是一段EXCEPTION处理程序。 我们可能要问:为什么CPU要提供Excpetion 和 Interrupt Handling呢? *处理illegal behavior, 例如,TLB Fault, or, we say, the Page fault; Cache Error; * Provide an approach for accessing priviledged resources, for example, CP0 registers. As we know, for user lev

44、el tasks/processes, they are running with the User Mode priviledge and are prohibilited to directly control CPO. CPU need provide a mechanism for them to trap to kernel mode and then safely manipulate resources that are only available when CPU runs in kernel mode. * Provide handling for external/int

45、ernal interrupts. For instance, the timer interrupts and watch dog exceptions. Those two interrupt/exceptions are very important for an embedded system applicances. Now lets get back to how MIPS supports its exception and interrupt handling. For simplicty, all information below will be based on R7K

46、CPU, which is derived from the R4k family. * The first thing for understanding MIPS exception handling is: MIPS adopts *Precise Exceptions* mechanisms. What that means? Here is the explaination from the book of See MIPS Run: In a precise-exception CPU, on any exception we get pointed at one instruction(the exception victim). All instructions preceding the exception victim in exe

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服