简体   繁体   English

x86处理器如何“知道”正在运行的多个进程?

[英]How are x86 processors 'aware' of multiple processes being run?

I'm sure the answer to this question is somewhere on the internet (or even stackoverflow), however, I'm having a difficult time formulating the question to get good results. 我敢肯定,这个问题的答案是在互联网上某个地方(甚至是stackoverflow),但是,我在制定该问题以取得良好结果方面遇到了困难。

I've been studying x86 for the last week and have been confused by the concept of x86 processors being 'aware' of different processes running. 我上周一直在研究x86,但对x86处理器的概念“意识到”正在运行的不同进程感到困惑。 For example, when operating in protected mode, the x86 processor is able to limit the memory in which a process can access. 例如,以保护模式运行时,x86处理器能够限制进程可以访问的内存。 This makes me believe the processor is somehow aware of multiple processes existing. 这使我相信处理器以某种方式知道存在的多个进程。

This confuses me. 这使我感到困惑。 From the processor perspective, I would assume that it is just getting a list of instructions to execute and it is the responsibility of the operating system to order those instructions (via time slices, memory management, etc.) such that multiple processes can run. 从处理器的角度来看,我假设它只是获取要执行的指令列表,并且操作系统有责任对这些指令进行排序(通过时间片,内存管理等),以便可以运行多个进程。 From the processors perspective, I assume all it would see is a sequential list of instructions. 从处理器的角度来看,我假设所看到的只是顺序的指令列表。

However, due to the fact that an x86 processor can limit memory access by process, the CPU is somehow aware that there are multiple processes. 但是,由于x86处理器可以按进程限制对内存的访问,因此CPU以某种方式意识到存在多个进程。

The CPU is not aware of process scheduling, the latter can be implemented in many different ways while the former must be a general purpose chip. CPU并不了解进程调度,后者可以以许多不同的方式实现,而前者必须是通用芯片。

What happens is this 这是怎么回事

  1. The OS is in control (read: the CPU is executing an OS routine) and setups a sandbox A to be used to run a quantum of Process A. 操作系统处于控制状态(读取:CPU正在执行操作系统例程),并设置了一个沙箱A以用于运行进程A的数量。
  2. Process A is executed inside this sandbox. 进程A在此沙箱中执行。
  3. A periodic interrupt occurs, the routine that serves this interrupt belongs to the OS and it is outside the sandbox. 发生周期性中断,处理此中断的例程属于OS,并且位于沙箱外部。
  4. The OS is in control and setups a sandbox B to be used to run a quantum of Process B. 操作系统处于控制之下,并设置了一个沙箱B以用于运行一定数量的进程B。
  5. Process B is executed inside this sandbox. 进程B在此沙箱中执行。

The process repeats. 重复该过程。

The protected mode let the OS create sandboxes, particularly it defines privileges (in Real mode every program has the same privileges) so that Process A and Process B cannot escape the sandbox (for example by disabling the interrupts and running forever). 受保护的模式使OS可以创建沙箱,特别是它定义了特权(在实模式下,每个程序都具有相同的特权),因此进程A和进程B无法逃脱沙箱(例如,通过禁用中断并永远运行)。

The CPU is not aware of what it's happening, it just see a sequence of instructions executed under different privileges and "constraints". CPU并不知道发生了什么,它只是看到在不同的特权和“约束”下执行的指令序列。
It is the OS that configures the CPU differently before executing a different program. 是OS在执行其他程序之前对CPU进行了不同的配置。
This also helps to explain the difference between threads and processes: threads scheduling doesn't change the sandbox. 这也有助于解释线程和进程之间的区别:线程调度不会改变沙箱。

As Jester remembered me in the comments, x86 has support for task management. 正如Jester在评论中记得我时,x86支持任务管理。
However, an HW task doesn't map one-to-one to an OS process (IIRC this feature is scarcely used by the major OSes). 但是,硬件任务不会一对一地映射到OS进程(主要OS几乎不使用IIRC此功能)。

Reading Intel Manual 3 will help you understand what the CPU is capable of and where the responsibility for a secure OS shifts from the CPU to the kernel. 阅读英特尔手册3将帮助您了解CPU的功能以及安全OS的职责从CPU转移到内核的位置。

The CPU sees ONE process at a time. CPU一次看到一个进程。 The CPU has no knowledge of a process unless it is executing. 除非正在执行,否则CPU不知道进程。

For example, when operating in protected mode, the x86 processor is able to limit the memory in which a process can access. 例如,以保护模式运行时,x86处理器能够限制进程可以访问的内存。

What you are describing is logical address translation. 您所描述的是逻辑地址转换。 The Operating system defines a set of page tables for a process that provide the mapping from a logical page to physical page frames. 操作系统为进程定义了一组页表,这些表提供了从逻辑页到物理页框架的映射。 The page tables also define the access allowed to those pages. 页面表还定义了对这些页面的访问权限。 Each process has its own set of page tables (in some cases the parts of the set may be shared). 每个进程都有其自己的页面表集(在某些情况下,该部分集可以共享)。

The structure of the page tables is defined by the processor. 页表的结构由处理器定义。 The contents of those tables are established by the operating system. 这些表的内容由操作系统建立。 Thus, the "limit" on the memory is comes from the operating system. 因此,内存上的“限制”来自操作系统。

Part of the procedure for changing the current process (triggered by the operating system) is to swap the page tables for the new process in place of the ones for the old. 更改当前进程(由操作系统触发)的部分过程是将新进程的页表替换为旧进程的页表。

The processors sees those "limits" on memory access from the perspective of the currently executing process and at any given time has no knowledge whatsoever of the equivalent "limits" (page tables) of any other process. 处理器从当前正在执行的进程的角度看到内存访问上的那些“限制”,并且在任何给定时间都不知道任何其他进程的等效“限制”(页表)。

However, due to the fact that an x86 processor can limit memory access by process, the CPU is somehow aware that there are multiple processes. 但是,由于x86处理器可以按进程限制对内存的访问,因此CPU以某种方式意识到存在多个进程。

The CPU is only aware of the page tables of the current process. CPU仅知道当前进程的页表。 It only knows the "limits" on the process that is executing. 它只知道正在执行的进程的“限制”。 The current page table set defines what physical memory the process can access and how it may access it (read/write/execute + restrictions by mode). 当前页表集定义了进程可以访问的物理内存以及访问方式(读/写/执行+模式限制)。

From the processors perspective, I assume all it would see is a sequential list of instructions. 从处理器的角度来看,我假设所看到的只是顺序的指令列表。

Plus interrupts that are a key part of process scheduling. 加上中断是流程调度的关键部分。

From the hardware's perspective, you are right that all a CPU does is "execute instructions", one at a time. 从硬件的角度来看,CPU所做的全部是一次“执行指令”,这是正确的。 In some (perhaps mildly simplified) sense, that's all that's going on. 从某种意义上(也许是略微简化),这就是全部。

If you had some specific computational task to perform, you could indeed write a suitable stream of instructions so that you can power on your hardware, it executes your instructions, and then halts, or shuts down, or whatever. 如果您要执行某些特定的计算任务,则确实可以编写适当的指令流,以便可以打开硬件电源,执行指令,然后停止或关闭或执行任何操作。 That's how very early computers were in fact operated. 实际上,这就是早期计算机运行的方式。

However, this mode of operating a computer is extremely unwieldy and doesn't scale at all, since it requires a single operator to take responsibility of everything , and in the process reinventing all sorts of wheels. 但是,这种操作计算机的方式极其笨拙,根本无法扩展,因为它需要一个操作员来负责所有事情 ,并在此过程中重新发明了各种轮子。 That's where the concept of an operating system comes in: The OS is a specific kind of instruction stream that's loaded at start-up which can in turn load up and execute other bits of instructions, dynamically . 这就是一个操作系统的概念进来:该操作系统是一个特定类型的该真实在启动时加载指令流的这可以加载和执行指令的其它位, 动态 This compartmentalization allows for reuse of core functionality (think device drivers), and to adapt the functionality of the machine dynamically (ie while it's running, as opposed to reprogramming and resetting it). 这种划分允许重复使用核心功能(考虑设备驱动程序),并动态地调整机器的功能(即,在运行时,而不是重新编程和重置机器)。 Morever, it allows those parts of the instructions that are loaded dynamically to be authored by different people, so that we have a single platform that can execute "user-defined instructions", ie what we conventionally understand as a "program". 此外,它允许动态加载的指令的那些部分由不同的人员创作,因此我们拥有一个可以执行“用户定义的指令”(即我们通常理解为“程序”)的平台。

So now we have all the pieces: The code that the CPU executes when it is powered up is the operating system, and the operating system dynamically manages the execution of further code. 因此,现在我们拥有了所有内容:CPU上电时执行的代码是操作系统,并且操作系统动态管理其他代码的执行。 Most of these units of execution are called processes . 这些执行单元中的大多数称为流程 (But not all code is like this. For example, loadable kernel modules in Linux are dynamically loaded, but don't constitute a process.) That is, a process is an abstract concept in the operating system that delineates between the OS's own code and the "hosted" code that it runs on request. (但并非所有代码都是这样。例如,Linux中的可加载内核模块是动态加载的,但不构成进程。)也就是说,进程是操作系统中的抽象概念,它在OS自己的代码之间进行了描述。以及根据要求运行的“托管”代码。

Depending on the type of OS, execution of processes may have fancy features such as virtual memory (each process sees its own, separate memory) and protection (no process can interfere with the operation of the OS or of other processes). 根据操作系统的类型,进程的执行可能具有一些奇特的功能,例如虚拟内存(每个进程都有自己的独立内存)和保护(任何进程都不会干扰OS或其他进程的操作)。 OSes implement such features by using CPU features: a memory manager unit that provides address translation, and protection rings that limit the instructions available to a piece of execution. 操作系统通过使用CPU功能来实现这些功能:提供地址转换的内存管理器单元,以及限制执行部分可用指令的保护环。 Not all OSes do this, though; 但是,并非所有的操作系​​统都这样做。 in DOS, for instance, every process has full access to the physical memory and thus to the OS's state. 例如,在DOS中,每个进程都具有对物理内存的完全访问权限,从而对操作系统的状态具有完全访问权限。 Regardless, an OS typically provides an API for processes (eg "system calls"), again using hardware features (interrupts or special system call instructions), and user code generally interacts with the environment through this API rather than talking to peripherals directly. 无论如何,OS通常会再次使用硬件功能(中断或特殊的系统调用指令)为进程(例如“系统调用”)提供API,并且用户代码通常通过此API与环境交互,而不是直接与外围设备交互。 For example, this means that hardware drivers are only implemented by the OS, and user code can make opaque "print output" calls without having to know the details of the available output devices. 例如,这意味着硬件驱动程序仅由OS实现,并且用户代码可以进行不透明的“打印输出”调用,而不必知道可用输出设备的详细信息。

Example: Maybe it's useful to illustrate what processes are on the popular Linux operating system, running on x86 hardware: A new process is started when an existing process (eg a shell, or init ) calls the clone system call, by raising interrupt 128. The interrupt makes the CPU transfer control to the interrupt handler routine, which was set up by the OS during boot-up. 示例:也许说明在x86硬件上运行的流行Linux操作系统上有哪些进程是有用的:当现有进程(例如shell或init )通过引发中断128调用clone系统调用时,将启动一个新进程。中断使CPU将控制权转移给中断处理程序例程,该例程由操作系统在启动期间设置。 When the interrupt handler is entered, the CPU switches to ring 0, privileged mode. 输入中断处理程序后,CPU切换到环0特权模式。 The interrupt handler makes the kernel create the new process, and then transfers control back to the calling process (which implies switching to protection ring 3, unprivileged; processes only ever execute in ring 3). 中断处理程序使内核创建新进程,然后将控制权转移回调用进程(这意味着切换到保护3,无特权;仅在保护3中执行进程)。 For the creation of the new process, the kernel creates the relevant internal book keeping structures, sets up new page tables in the MMU, and then transfers control to the entry point for the clone call, similar to the way the original call returns. 为了创建新流程,内核会创建相关的内部簿记结构,在MMU中设置新的页表,然后将控制权转移到clone调用的入口点,类似于原始调用返回的方式。 (I'm glossing over issues of scheduling here; only one transfer of control happens at a time, and the others are "scheduled" to happen later.) The fact that a new process exists now is merely reflected in the kernel's internal bookkeeping data. (我在这里讨论调度问题;一次仅发生一次控制转移,而其他“调度”则在以后发生。)现在存在一个新过程的事实仅反映在内核的内部簿记数据中。 The CPU doesn't know anything about it; CPU对此一无所知。 all it sees is that interrupts get fired and page tables get changed regularly. 它所看到的只是触发中断和定期更改页表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM