📄 chapter6.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0036)http://www.xtrj.org/smr/chapter6.htm -->
<HTML><HEAD><TITLE>?????TLB</TITLE><!-- http://www.xtrj.org/smr/chapter6.htm -->
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2180" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId></HEAD>
<BODY>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><B><SPAN lang=ZH-CN
style="FONT-SIZE: 18pt; FONT-FAMILY: SimSun; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内存管理与</SPAN><SPAN
style="FONT-SIZE: 18pt; mso-bidi-font-size: 12.0pt">TLB<O:P>
</O:P></SPAN></B></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们倾向于直接从最底层引入本书中的大部分主题进行探讨,对于一本关注计算机底层体系结构的书而言,这似乎是自然而然的。然而,为了说清楚内存管理硬件,我们得从</SPAN>MIPS
R2000<SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所寻求实现的</SPAN>unix<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">风格的虚拟存储系统开始讲起。本章的后面我们还会讨论一下相同的硬件如何在其他环境下工作。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">早期的</SPAN>MIPS
CPU<SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定位于支持运行在</SPAN>UNIX<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工作站与服务器上的应用程序,因此内存管理硬件被构想为一个最小化的能帮助</SPAN>BSD
UNIX<SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">——一个经过完善设计并拥有充分多虚拟存储需求的操作系统的典型——提供内存管理功能的硬件。很明显的是,这些设计者们十分熟悉</SPAN>DEC
VAX<SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">小型机,并且在从这种体系结构中获取了众多思路的同时,也摒弃了许多复杂设计。尤其是许多</SPAN>VAX<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用微代码来解决的问题,在</SPAN>MIPS<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中被交由软件处理。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">本章中我们将从</SPAN>MIPS<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的设计起点开始,面对着一个</SPAN>unix<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类型的操作系统以及它的虚存系统的众多需求。我们将会展示一下</SPAN>MIPS<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的硬件是如何满足这些需求的。结尾时,我们会讨论一下在不能像通常一样使用内存管理硬件的嵌入式系统中,您可以采取的几种使用方式。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内存地址转译硬件(下面我们将称其</SPAN>MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,全称为</SPAN>memory
management unit<SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)有几类不同用途:</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo1; tab-stops: list 42.0pt"><SPAN
style="FONT-FAMILY: Wingdings">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">重定位(</SPAN>Relocation<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">):程序的函数方法和预先声明的数据地址均在编译期间决定,</SPAN>MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">允许程序在任何物理地址运行。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo1; tab-stops: list 42.0pt"><SPAN
style="FONT-FAMILY: Wingdings">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为程序分配内存:</SPAN>MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以从物理内存里许多零散的页中创建连续的程序空间,使我们能从一个充满固定大小页面的池里分配内存。如果我们不停分配释放大小不一的内存块,就会碰上内存碎片问题:我们不得不止步于一个布满“小孤岛”的内存空间,无法满足对较大块内存的申请要求,哪怕此时所有的空闲空间之和是足够的。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo1; tab-stops: list 42.0pt"><SPAN
style="FONT-FAMILY: Wingdings">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">隐藏和保护:用户级程序只能访问</SPAN>kuseg<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内存区域(较低的程序地址)内的数据。这类程序只能在操作系统所许可的内存区域中取得数据。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42pt"><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">此外,每一页可以独立的指定为可写权限或者写保护权限;操作系统甚至可以停止一个意外的写覆盖代码空间的应用程序。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo1; tab-stops: list 42.0pt"><SPAN
style="FONT-FAMILY: Wingdings">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">扩展地址空间:</SPAN><SPAN
lang=ZH-CN> </SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有些</SPAN>CPU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不能直接访问它们拥有的全部物理空间。尽管</SPAN>MIPS
I <SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系列</SPAN>CPU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是真正的</SPAN>32<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位体系结构,它们却布局了地址映射,使得未被映射的地址空间窗口</SPAN>kseg0<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN>kseg1<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">(它们不依赖</SPAN>MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行地址转换)落在了物理内存的开头的</SPAN>512M<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内。如果你要访问更高地址,则必须通过</SPAN>MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo1; tab-stops: list 42.0pt"><SPAN
style="FONT-FAMILY: Wingdings">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内存映射对程序的适应化:在</SPAN>MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的帮助下,你的程序能够去使用适合它的地址。同一段程序的许多份拷贝可能会同时运行在一个庞大的操作系统里,令它们去使用相同的程序地址变得更容易。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo1; tab-stops: list 42.0pt"><SPAN
style="FONT-FAMILY: Wingdings">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN><SPAN lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调页能力:程序可以好像已经得到它们所申请分配的所有资源一样正常的运行,而操作系统实际上只分配给它们当前所需的资源。访问未分配空间的程序会导致一个交由操作系统处理的异常(</SPAN>exception<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">),操作系统此时才在这块内存中装入适当数据并令应用程序继续运行。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 21pt"> <O:P> </O:P></P>
<P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt">UNIX<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内存管理工作的本质是为了能运行众多不同的任务(即</SPAN>multitasking<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">——多进程),并且每个任务各自拥有自己的内存空间。如果这个工作圆满完成,那么各任务的命运将彼此独立开来(操作系统自身也因此得以保护):一个任务自身崩溃或者错误的做某些事不会影响整个系统。显然,对一个使用分布终端来运行学生们程序的大学而言,这是一个很有用的特性;然而不仅如此,甚至是要求最严格的商业系统环境也需要能够在运行的同时支持实验软件或原型软件一并进行调试和测试。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt">MMU<SPAN
lang=ZH-CN
style="FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">并不仅仅为了建立巨大而完备的虚拟存储系统,小的嵌入式程序也能在重定位和更有效的内存分配里受益。如果能把应用程序观念上的地址映射到任何可获得的物理地址,系统在不同时刻运行不同程序就会更加容易。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><SPAN lang=ZH-CN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -