理解xv6——3.1 系统调用、异常和中断

理解xv6——3.1 系统调用、异常和中断

首页枪战射击计划异常汉化版更新时间:2024-06-23

There are three cases when control must be transferred from a user program to the kernel. First, a system call: when a user program asks for an operating system service, as we saw at the end of the last chapter. Second, an exception: when a program performs an illegal action. Examples of illegal actions include divide by zero, attempt to access memory for a page-table entry that is not present, and so on. Third, an interrupt: when a device generates a signal to indicate that it needs attention from the operating system. For example, a clock chip may generate an interrupt every 100 msec to allow the kernel to implement time sharing. As another example, when the disk has read a block from disk, it generates an interrupt to alert the operating system that the block is ready to be retrieved.

在三种情况下,必须将控制权从用户程序转移到内核。首先,系统调用:当一个用户程序请求操作系统服务时,就像我们在上一章结尾看到的那样。第二个例外:当程序执行非法操作时。非法操作的示例包括除以零、尝试访问不存在的页表条目的内存等。第三,中断:当设备生成信号以指示其需要操作系统的关注时。例如,时钟芯片可以每100毫秒生成一个中断,以允许内核实现时间共享。再如,当盘已经从盘读取块时,它生成中断以警告操作系统该块准备好被检索。


The kernel handles all interrupts, rather than processes handling them, because in most cases only the kernel has the required privilege and state. For example, in order to time-slice among processes in response the clock interrupts, the kernel must be involved, if only to force uncooperative processes to yield the processor.

内核处理所有中断,而不是进程处理它们,因为在大多数情况下,只有内核具有所需的特权和状态。例如,为了在响应时钟中断的进程之间进行时间切片,内核必须参与,只是是为了迫使不合作的进程放弃处理器。

In all three cases, the operating system design must arrange for the following to happen. The system must save the processor’s registers for future transparent resume. The system must be set up for execution in the kernel. The system must chose a place for the kernel to start executing. The kernel must be able to retrieve information about the event, e.g., system call arguments. It must all be done securely; the system must maintain isolation of user processes and the kernel.

在所有这三种情况下,操作系统设计必须安排以下情况。系统必须保存处理器的寄存器,以便将来透明恢复。系统必须设置为在内核中执行。系统必须为内核选择一个开始执行的位置。内核必须能够检索有关事件的信息,例如,系统调用参数。这一切都必须稳妥地进行;系统必须保持用户进程和内核的隔离。


To achieve this goal the operating system must be aware of the details of how the hardware handles system calls, exceptions, and interrupts. In most processors these three events are handled by a single hardware mechanism. For example, on the x86, a program invokes a system call by generating an interrupt using the int instruction. Similarly, exceptions generate an interrupt too. Thus, if the operating system has a plan for interrupt handling, then the operating system can handle system calls and exceptions too.

为了实现这个目标,操作系统必须知道硬件如何处理系统调用、异常和中断的细节。在大多数处理器中,这三个事件由单个硬件机制处理。例如,在x86上,程序通过使用int指令生成中断来调用系统调用。同样,异常也会生成中断。因此,如果操作系统具有用于中断处理的计划,则操作系统也可以处理系统调用和异常。


The basic plan is as follows. An interrupts stops the normal processor loop and starts executing a new sequence called an interrupt handler. Before starting the interrupt handler, the processor saves its registers, so that the operating system can restore them when it returns from the interrupt. A challenge in the transition to and from the interrupt handler is that the processor should switch from user mode to kernel mode, and back.

基本计划如下。中断停止正常的处理器的序列循环,并开始执行一个称为中断处理程序的新序列。在启动中断处理程序之前,处理器保存其寄存器,以便操作系统在从中断返回时可以恢复它们。转换到中断处理程序和从中断处理程序转换的一个挑战是,处理器应该从用户模式切换到内核模式,然后再切换回来。


A word on terminology: Although the official x86 term is exception, xv6 uses the term trap, largely because it was the term used by the PDP11/40 and therefore is the conventional Unix term.

Furthermore, this chapter uses the terms trap and interrupt interchangeably, but it is important to remember that traps are caused by the current process running on a processor (e.g., the process makes a system call and as a result generates a trap), and interrupts are caused by devices and may not be related to the currently running process.

For example, a disk may generate an interrupt when it is done retrieving a block for one process, but at the time of the interrupt some other process may be running. This property of interrupts makes thinking about interrupts more difficult than thinking about traps, because interrupts happen concurrently with other activities. Both rely, however, on the same hardware mechanism to transfer control between user and kernel mode securely, which we will discuss next.

关于术语:虽然官方的x86术语是异常,但xv6使用术语trap,主要是因为它是PDP 11/40使用的术语,因此是传统的Unix术语。

此外,本章交替使用术语陷阱和中断,但重要的是要记住陷阱是由处理器上运行的当前进程引起的(例如,进程进行系统调用,结果生成陷阱),而中断由设备引起,可能与当前运行的进程无关。

例如,磁盘可能在完成检索一个进程的块时生成中断,但在中断时,一些其他进程可能正在运行。中断的这个特性使得考虑中断比考虑陷阱更困难,因为中断与其他活动同时发生。但是,这两种方式都依赖于相同的硬件机制来安全地在用户和内核模式之间转移控制,我们将在下面讨论。

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved