Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 2: Computer-System Structures 计算机系统结构

Similar presentations


Presentation on theme: "Module 2: Computer-System Structures 计算机系统结构"— Presentation transcript:

1 Module 2: Computer-System Structures 计算机系统结构
I/O Structure I/O结构 Storage Structure 存储构造 Storage Hierarchy 存储层次 Hardware Protection 硬件保护 General System Architecture 一般系统体系结构 Applied Operating System Concepts

2 Computer-System Architecture 计算机系统体系结构
Applied Operating System Concepts

3 Applied Operating System Concepts
CPU 专门设计了一系列基本机制: - 具有特权级别的处理器状态,能在不同特权级运行的各种特权指令。 - 硬件机制使得OS可以和普通程序隔离 实现保护和控制 Applied Operating System Concepts

4 Applied Operating System Concepts
在多道程序系统中,为保证安全,把指令系统分为两类。 1)、特权指令 只允许操作系统使用,不允许一般用户使用的指令。特权指令一般引起处理器状态的切换 处理器通过特殊的机制将处理器状态切换到操作系统运行的特权状态(管态) 然后将处理权移交给操作系统中的一段特殊代码,这一个过程称为陷入() 若一般用户可以使用,就会影响系统安全,影响其它用户使用。如:修改程序状态字;设置中断屏蔽;启动I/O设备;清内存;设置时钟;停机等 如果一般用户程序要做与特权指令有关的工作,则需要请求操作系统代劳,即由操作系统统一管理。 2)、非特权指令 除特权指令以外的指令,即一般用户可使用的指令。 Applied Operating System Concepts

5 Applied Operating System Concepts
CPU的构成 CPU由运算器、控制器、一系列的寄存器以及高速缓存构成 运算器实现指令中的算术和逻辑运算,是计算机计算的核心 控制器负责控制程序运行的流程,包括取指令、维护CPU状态、CPU与内存的交互等等 Applied Operating System Concepts

6 Applied Operating System Concepts
CPU的构成 寄存器是指令在CPU内部作处理的过程中暂存数据、地址以及指令信息的存储设备 在计算机的存储系统中它具有最快的访问速度 高速缓存 一般由控制器中的内存管理单元(MMU:Memory Management Unit)管理,访问速度快于内存,低于寄存器。利用程序局部性原理,使得高速指令处理和低速内存访问得以匹配,从而提高CPU的效率 Applied Operating System Concepts

7 Applied Operating System Concepts
控制和状态寄存器 用于控制处理器的操作 大部分对于用户是不可见的 一部分可以在某种特权模式(由OS使用)下访问 常见的控制和状态寄存器: 程序计数器(PC:Program Counter),记录将要取出的指令的地址 指令寄存器(IR:Instruction Register),包含最近取出的指令 程序状态字(PSW:Program Status Word),记录处理器的运行模式信息等等 Applied Operating System Concepts

8 Computer-System Operation 计算机系统运行
I/O devices and the CPU can execute concurrently. I/O 设备与CPU可并行运行 Each device controller is in charge of a particular device type. 每一设备控制器负责一个设备类型 Each device controller has a local buffer. 每一设备控制器有一局部缓存 CPU moves data from/to main memory to/from local buffers CPU 通过局部缓存与主存交换数据 I/O is from the device to local buffer of controller. I/O从设备到设备控制器的局部缓存 Device controller informs CPU that it has finished its operation by causing an interrupt. 设备控制器通过引起中断通知CPU操作已完成 Applied Operating System Concepts

9 Applied Operating System Concepts
interrupt 中断 中断是实现多道程序操作系统的前提,没有中断,操作系统无法改变CPU 的状态(即目态-管态之间的互换) 中断对于操作系统的重要性就像机器中的驱动齿轮一样,所以有人把操作系统称为是由“中断驱动”或者 “(中断)事件驱动” Applied Operating System Concepts

10 Applied Operating System Concepts
interrupt 中断 中断是现代计算机系统中的基本设施之一,它起着通讯联络作用,协调系统对各种外部事件的响应和处理。中断是实现多道程序的必要条件。 Applied Operating System Concepts

11 Applied Operating System Concepts
中断的功能 中断机制使得: OS可以捕获普通程序发出的系统功能调用 及时处理设备的中断请求 防止用户程序中破坏性的活动等等 Applied Operating System Concepts

12 Applied Operating System Concepts
What is interrupt? CPU对系统发生的某个事件作出的一种反应 CPU暂停正在执行的程序,保留现场后自动转去执行相应事件的处理程序,处理完成后返回断点,继续执行被打断的程序 “中断”名称源于: 当异步事件(随机事件)发生后,打断了对当前程序的执行,而转去处理该异步事件 直到处理完了后,再转回原程序中断点继续执行 Applied Operating System Concepts

13 Applied Operating System Concepts
从用户角度看中断 Applied Operating System Concepts

14 Goals of introducing interrupt 引入中断的目的
解决主机与外设的并行工作问题 提高可靠性 实现实时控制 特点: 中断是随机的 中断是自动处理的 Applied Operating System Concepts

15 Applied Operating System Concepts
中断系统的有关概念 中断由软件、硬件协同完成,硬件机构称中断装置。 中断装置:指发现中断,响应中断的硬件。 中断处理程序是由软件来完成的。 以上合称中断系统 中断源:引起中断发生的事件 中断寄存器:硬件为每个中断源设置寄存器,中断发生时信息被记录在寄存器中,以便分析处理(记录中断) 中断字:中断寄存器中的内容 系统堆栈:在内存开辟的一块区域用来临时保存进程运行现场 Applied Operating System Concepts

16 Applied Operating System Concepts
中断装置的基本功能 提供识别中断源的方法 提供查询中断状态的方法,通常使用一个寄存器存储有关中断的状态信息,称为中断字 提供中断现场保护的能力 提供中断处理程序寻址能力,找到恰当的中断处理程序 Applied Operating System Concepts

17 Applied Operating System Concepts
中断寄存器 为了区分和不丢失中断信号 对应每个中断源分别用一固定触发器寄存中断信号 规定值为1时,表示有中断信号,为0时表示无 这些触发器的全体称为中断寄存器 每个触发器称为一个中断位 所以中断寄存器是由若干个中断位组成 Applied Operating System Concepts

18 Applied Operating System Concepts
CPU如何发现中断信号? 处理器的控制部件中设一个能检测中断的机构,称为中断扫描机构 在每条指令执行周期的最后时刻扫描中断寄存器,询问是否有中断信号 若无中断信号,继续执行下一条指令 若有中断,中断硬件将该中断触发器内容按规定编码送入PSW的相应位,称为中断码 Applied Operating System Concepts

19 Applied Operating System Concepts
中断类型 强迫性中断 正在运行的程序所不期望的,由于某种硬件故障或外部请求引起的 自愿性中断 用户在程序中有意识安排的中断,是由于用户在编制程序时因为要求操作系统提供服务,有意使用“访管”指令或系统调用,使中断发生 Applied Operating System Concepts

20 Applied Operating System Concepts
强迫性中断 输入/输出(I/O)中断:主要来自外部设备通道 程序性中断:运行程序中本身的中断 (如溢出,缺页中断,缺段中断,地址越界) 时钟中断 硬件故障 Applied Operating System Concepts

21 Applied Operating System Concepts
自愿性中断 执行I/O,创建进程,分配内存 信号量操作,发送/接收消息 操作系统提供若干功能子程序(系统调用),这些系统调用在管态下执行。 现代计算机系统硬件提供一条“访管指令”,该指令可以在目态下执行。 用户编制程序需要请求操作系统服务时,使用系统调用。编译程序将其转换成目标程序中的“访管指令”及一些参数。 目标程序执行时,当CPU执行到“访管指令”,产生自愿性中断,操作系统接过控制权(管态)。 操作系统分析相关参数,让对应的“系统调用”子程序为用户服务。 完成系统调用后,操作系统将CPU状态改变为目态,返回到用户程序继续执行。 各计算机系统都提供“访管指令”作为调用操作系统功能的手段,但其名称和格式可能不同。当CPU执行到访管指令时,产生中断。 UNIX:trap,DOS:INT Applied Operating System Concepts

22 Applied Operating System Concepts
多级中断 多数微型处理器有着多级中断系统,可以有多根中断请求线(级)从不同设备连接到中断逻辑 具有相同特性和优先级的设备可连到同一中断级上 中断优先级:中断优先级是由硬件规定的,系统根据引起中断事件的重要性与紧迫程度,将中断源划分为若干个级别 在多级中断系统中,可能同时有多个中断请求,CPU接受中断优先级最高的那个中断,忽略优先级较低的那些中断 Applied Operating System Concepts

23 Applied Operating System Concepts
中断屏蔽 在CPU上运行的程序,有时由于种种原因,不希望其在执行过程中被别的事件所中断,称为中断屏蔽 在PSW中设置中断屏蔽码以屏蔽某些指定的中断类型 如果其PSW的中断禁止位建立后,则屏蔽中断 (不包括不可屏蔽的那些中断) 如果PSW中的中断禁止位未建立,则可以接受其中断优先级高于运行程序中断优先级的那些中断 各设备接口中也有中断禁止位,以禁止该设备的中断 Applied Operating System Concepts

24 Applied Operating System Concepts
中断屏蔽(续) 可屏蔽的中断请求:主要是输入输出设备的I/O中断, 通过建立在PSW中的中断屏蔽位加以屏蔽,即使再有I/O中断,处理器也不响应 不可屏蔽的中断请求:属于机器故障中断,包括内存奇偶校验错以及掉电等中断源、程序中的问题所引起的中断(如溢出、除法错都可以引起中断)和软件中断等 Applied Operating System Concepts

25 Applied Operating System Concepts
中断响应 发现中断、接收中断的过程(由中断装置完成) 1)发现中断源(识别中断原因) 接受中断请求,当有多个中断源存在时,选择优先级高的中断源 2)保存现场,将现场信息推入系统堆栈,使中断处理结束后,程序能继续运行 3)引出中断处理程序 Applied Operating System Concepts

26 Interrupt Handling 中断处理
典型的处理过程: (1)设备给处理器发一个中断信号 (2)处理器处理完当前指令后检测到中断,判断出中断来源并向发送中断的设备发送确认中断信号,确认信号使得该设备将中断信号恢复到一般状态 (3)处理器开始为软件处理中断做准备: 保存中断点的程序执行上下文环境,这通常包括PSW,程序计数器PC中的下一条指令位置,一些寄存器的值,它们通常保存在系统控制栈中 处理器状态被切换到管态 Applied Operating System Concepts

27 Interrupt Handling 中断处理
(4)处理器根据中断源查询中断向量表,获得该中断处理程序入口地址,并将PC置成该地址,处理器开始一个新的指令周期,控制转移到中断处理程序 (5)中断处理程序开始工作,包括检查I/O相关的状态信息,操纵I/O设备或者在设备和主存之间传送数据等等 (6)中断处理结束时,处理器检测到中断返回指令,被中断程序的上下文环境从系统堆栈中被恢复 处理器状态恢复成原来的状态。 (7)PSW和PC被恢复成中断前的值,处理器开始一个新的指令周期,中断处理结束 Applied Operating System Concepts

28 Applied Operating System Concepts
中断嵌套 在处理一个中断时,系统又响应了新的中断 中断按照优先度分级 允许优先级高的中断打断优先级低的中断处理过程 Applied Operating System Concepts

29 Applied Operating System Concepts
中断嵌套 Applied Operating System Concepts

30 Applied Operating System Concepts
I/O Structure I/O结构 两种方法I/O : 1 After I/O starts, control returns to user program only upon I/O completion. 在I/O开始之后,仅当I/O完成之后控制才返回用户程序,实现方法: wait instruction idles the CPU until the next interrupt 等待指令空转CPU直至I/O完成 At most one I/O request is outstanding at a time, no simultaneous I/O processing.每一时刻最多一个I/O请求,无并行I/O处理 2 After I/O starts, control returns to user program, without waiting for I/O completion. 在I/O开始之后,不等I/O完成控制就返回用户程序,实现方法: System call – request to the operating system to allow user to wait for I/O completion.系统调用-请求操作系统允许用户等待I/O完成 Device-status table contains entry for each I/O device indicating its type, address, and state.设备状态表包括每个I/O设备的类型、地址和状态 Operating system indexes into I/O device table to determine device status and to modify table entry to include interrupt.操作系统引用设备状态表以确定设备状态并修改表入口加入中断 Applied Operating System Concepts

31 Two I/O methods 两种I/O方法
Synchronous 同步 Asynchronous 异步 Applied Operating System Concepts

32 Direct Memory Access (DMA) Structure 直接内存访问(DMA)结构
Used for high-speed I/O devices able to transmit information at close to memory speeds. 用于高速I/O设备能够以接近存储器的速率传送信息 Device controller transfers blocks of data from buffer storage directly to main memory without CPU intervention. 无须CPU的介入,设备控制器直接把数据块从缓冲区存储传送到主存 Only one interrupt is generated per block, rather than the one interrupt per byte. 每数据块只要一个中断,而不是每个字节一个中断 Applied Operating System Concepts

33 Applied Operating System Concepts
Storage system存储系统 是支持OS运行硬件环境的一个重要方面: 作业必须把它的程序和数据存放在内存中才能运行 多道程序系统中,同时有若干个程序和相关的数据要放入主存储器 操作系统本身也要存放在主存储器中并运行 操作系统要管理、保护程序和数据,使它们不至于受到破坏 Applied Operating System Concepts

34 Applied Operating System Concepts
存储器的类型 读写型的存储器 常被称为随机访问存储器(RAM:Random Access Memory) RAM主要用作存放随机存取的程序和数据 只读型的存储器 只能从其中读取数据,但不能随意用普通方法写入数据(写入数据只能用特殊方法) 称为只读存储器(ROM:Read-Only Memory) 在微机中,一些常驻内存的模块以微程序形式固化在ROM中 Applied Operating System Concepts

35 Applied Operating System Concepts
存储器的层次结构 存储系统设计三个问题: 容量:需求无止境 速度:能匹配处理器的速度 成本:成本和其它部件相比应在合适范围之内 Applied Operating System Concepts

36 Applied Operating System Concepts
存储器的层次结构(续) 容量、速度和成本三个目标不可能同时达到最优,要作权衡 存取速度快,每比特价格高 容量大,每比特价格越低,同时存取速度也越慢 解决方案:采用层次化的存储体系结构 当沿着层次下降时,每比特的价格将下降,容量将增大,速度将变慢,处理器的访问频率也将下降 Applied Operating System Concepts

37 Storage Hierarchy 存储层次
Storage systems organized in hierarchy.存储系统分层组织 Speed 速度 cost 成本 volatility 易失性 Caching – copying information into faster storage system; main memory can be viewed as a last cache for secondary storage. 缓存- 把信息复制进较快的存储系统;主存可看作为二级存储器的最后一个缓存 Applied Operating System Concepts

38 Storage Structure 存储结构
Main memory – only large storage media that the CPU can access directly.主存- CPU可以直接访问的唯一大型存储介质 Secondary storage – extension of main memory that provides large nonvolatile storage capacity.二级存储器-主存的扩展提供了非易失的存储容量 Applied Operating System Concepts

39 Storage-Device Hierarchy 存储设备层次
寄存器 缓存 主存储器 电子盘 磁盘 光盘 磁带 Applied Operating System Concepts

40 Applied Operating System Concepts
存储访问局部性原理 提高存储系统效能关键点:程序存储访问局部性原理 程序执行时,有很多的循环和子程序调用,一旦进入这样的程序段,就会重复存取相同的指令集合 对数据存取也有局部性,在较短的时间内,稳定地保持在一个存储器的局部区域 处理器主要和存储器的局部打交道 在经过一段时间以后,使用的代码和数据集合会改变 Applied Operating System Concepts

41 Applied Operating System Concepts
存储保护 多用户,多任务操作系统:OS给每个运行进程分配一个存储区域 问题:多个程序同时在同一台机器上运行,怎样才能互不侵犯? 解决:硬件支持 界地址寄存器(界限寄存器) 存储键 地址转换 Applied Operating System Concepts

42 Applied Operating System Concepts
界地址寄存器 被广泛使用的一种存储保护技术 机制简单,易于实现 实现方法: 在CPU中设置一对下限寄存器和上限寄存器,存放用户作业在主存中的下限和上限地址 也可将一个寄存器作为基址寄存器,另一寄存器作为限长寄存器(指示存储区长度) 每当CPU要访问主存,硬件自动将被访问的主存地址与界限寄存器的内容进行比较,以判断是否越界 如果未越界,则按此地址访问主存,否则将产生程序中断——越界中断(存储保护中断) Applied Operating System Concepts

43 Applied Operating System Concepts
存储键 每个存储块有一个由二进位组成的存储保护键 一用户作业被允许进入主存,OS分给它一个唯一的存储键号 并将分配给该作业各存储块存储键也置成同样键号 当OS挑选该作业运行时,OS将它的存储键号放入程序状态字PSW存储键(“钥匙”)域中 每当CPU访问主存时,都将该主存块的存储键与PSW中的“钥匙”进行比较 如果相匹配,则允许访问,否则,拒绝并报警 Applied Operating System Concepts

44 Hardware Protection 硬件保护
Dual-Mode Operation 两状态运行 I/O Protection I/O保护 Memory Protection 内存保护 CPU Protection CPU 保护 Applied Operating System Concepts

45 Dual-Mode Operation两状态运行
既要保证多个用户 程序对系统资源的共享,又要保证OS对系统资源的控制 Provide hardware support to differentiate between at least two modes of operations. 至少在两个运行状态之间提供硬件支持 1. User mode – execution done on behalf of a user. 用户态-代表用户程序的执行 2. Monitor mode (also supervisor mode or system mode) – execution done on behalf of operating system. 管态(特权模式或系统模式)-代表操作系统程序执行 Applied Operating System Concepts

46 Dual-Mode Operation两状态运行(Cont.)
Mode bit added to computer hardware to indicate the current mode: monitor (0) or user (1). 状态位添入计算机硬件,指示正确的状态:管态(0) 或用户态(1) When an interrupt or fault occurs hardware switches to monitor mode. 当中断或错误出现,硬件切换至管态 monitor user Interrupt/fault set user mode Privileged instructions can be issued only in monitor mode. 特权指令只能在管态下使用 Applied Operating System Concepts

47 Applied Operating System Concepts
CPU状态 CPU如何知道当前运行的是操作系统还是一般应用软件?有赖于处理器状态的标识 在程序状态字(PSW)中专门设置一位,它是根据运行程序使用指令的不同权限而设置。 管态(特态):能执行指令全集(包括特权、非特权指令),具有改变CPU状态的能力。操作系统在管态下运行。(特权态,核心态,系统态) 目态(普态):只能执行非特权指令,用户程序在目态下运行。(如果在目态下用户执行了特权指令,CPU拒绝执行该指令(非法操作),产生中断,由操作系统取得控制权并处理,通知用户“程序执行非法指令”。) (用户态) Applied Operating System Concepts

48 Applied Operating System Concepts
程序状态字PSW PSW (Program Status Word ): CPU的工作状态码——指明管态还是目态,用来说明当前在CPU上执行的是操作系统还是一般用户,从而决定其是否可以使用特权指令或拥有其它的特殊权力 条件码——反映指令执行后的结果特征 中断屏蔽码——指出是否允许中断 Applied Operating System Concepts

49 Applied Operating System Concepts
CPU状态的转换 在系统运行过程中,CPU状态是动态改变的,时而运行于管态,时而目态。这两种状态可以相互转换。 目态到管态:其转换的唯一途径是通过中断。 管态到目态:可通过设置PSW指令(修改程序状态字),实现从操作系统向用户程序的转换。 Applied Operating System Concepts

50 Applied Operating System Concepts
I/O Protection I/O保护 All I/O instructions are privileged instructions. 所有I/O指令是特权指令 Must ensure that a user program could never gain control of the computer in monitor mode 必须确保用户程序决不能以管态控制计算机 Applied Operating System Concepts

51 Memory Protection 存储保护
Must provide memory protection at least for the interrupt vector and the interrupt service routines.至少要为中断向量和中断服务程序提供存储保护 In order to have memory protection, add two registers that determine the range of legal addresses a program may access: 为了进行存储保护,增加2个寄存器用于确定一个程序的合法地址范围 base register – holds the smallest legal physical memory address. 基址寄存器- 存有最小的合法物理存储器地址 Limit register – contains the size of the range 限长基址寄存器-存有允许范围的大小 Memory outside the defined range is protected. 在定义范围之外的存储器受到保护 Applied Operating System Concepts

52 Applied Operating System Concepts
A Base And A limit Register Define A Logical Address Space 基址与限长寄存器定义逻辑地址空间 Applied Operating System Concepts

53 Protection Hardware 保护硬件
When executing in monitor mode, the operating system has unrestricted access to both monitor and user’s memory. 在管态下运行时,操作系统对系统和用户存储空间有不受限制的访问权 The load instructions for the base and limit registers are privileged instructions. 用于基址和限长寄存器的装载指令是特权指令 Applied Operating System Concepts

54 Applied Operating System Concepts
CPU Protection CPU保护 Timer – interrupts computer after specified period to ensure operating system maintains control.(to prevent a user program from running too long) 定时器-在指定的时间段之后中断计算机,以确保操作系统保持控制 Timer is decremented every clock tick.每一时钟滴答定时器减少 When timer reaches the value 0, an interrupt occurs.定时器为零时中断发生 Timer commonly used to implement time sharing. 定时器通常用于实现分时 Timer also used to compute the current time.定时器也用于计算当前时间 Load-timer is a privileged instruction.装载定时器是特权指令 Applied Operating System Concepts


Download ppt "Module 2: Computer-System Structures 计算机系统结构"

Similar presentations


Ads by Google