第二章:模块的编译与运行-3 User Space and Kernel Space

In continuation of the previous text:第二章:模块的编译与运行-2, let's GO ahead .

User Space and Kernel Space

A module runs in kernel space, whereas applications run in user space. This concept is at the base of operating systems theory.

模块运行在内核空间(kernel space),而应用程序运行在用户空间(user space)。这一概念是操作系统理论的基础。
The role of the operating system, in practice, is to provide programs with a consistent view of the computer’s hardware. In addition, the operating system must account for independent operation of programs and protection against unauthorized access to resources. This nontrivial task is possible only if the CPU enforces protection of system software from the applications.

从实际功能来看,操作系统的作用是为程序提供对计算机硬件的统一视图。此外,操作系统还必须实现程序的独立运行,并防止对资源的未授权访问。要完成这一复杂任务,前提是 CPU 能确保系统软件免受应用程序的干扰 —— 只有这样,上述目标才能实现。

Every modern processor is able to enforce this behavior. The chosen approach is to implement different operating modalities (or levels) in the CPU itself. The levels have different roles, and some operations are disallowed at the lower levels; program code can switch from one level to another only through a limited number of gates. Unix systems are designed to take advantage of this hardware feature, using two such levels. All current processors have at least two protection levels, and some, like the x86 family, have more levels; when several levels exist, the highest and lowest levels are used. Under Unix, the kernel executes in the highest level (also called supervisor mode), where everything is allowed, whereas applications execute in the lowest level
(the so-called user mode), where the processor regulates direct access to hardware and unauthorized access to memory.

所有现代处理器都能够强制实现这种行为。其采用的方式是在 CPU 内部实现不同的操作模式(或等级)。不同等级具有不同的作用,较低等级会禁止某些操作;程序代码只能通过有限的 “门径”(指特定指令或机制)从一个等级切换到另一个等级。

Unix 系统的设计充分利用了这种硬件特性,它仅使用其中两个等级。当前所有处理器都至少具备两个保护等级,部分处理器(如 x86 系列)则拥有更多等级;当存在多个等级时,会使用最高等级和最低等级。在 Unix 系统中:

  • 内核运行在最高等级(也称为 “管态” 或 “超级用户模式”,supervisor mode),该模式下允许执行所有操作;

  • 应用程序运行在最低等级(即 “用户模式”,user mode),该模式下处理器会限制对硬件的直接访问,以及对内存的未授权访问。

We usually refer to the execution modes as kernel space and user space. These terms encompass not only the different privilege levels inherent in the two modes, but also the fact that each mode can have its own memory mapping—its own address space—as well.

我们通常将这两种执行模式称为内核空间(kernel space)和用户空间(user space)。这些术语不仅包含两种模式固有的不同特权等级,还体现了一个关键特性:两种模式可拥有各自的内存映射 —— 即各自独立的地址空间。
Unix transfers execution from user space to kernel space whenever an application issues a system call or is suspended by a hardware interrupt. Kernel code executing a system call is working in the context of a process—it operates on behalf of the call-ing process and is able to access data in the process’s address space. Code that han-dles interrupts, on the other hand, is asynchronous with respect to processes and is not related to any particular process.

在 Unix 系统中,当应用程序发起系统调用(system call),或被硬件中断(interrupt)暂停时,执行权会从用户空间转移到内核空间:

  • 执行系统调用的内核代码运行在进程上下文(process context)中 —— 它代表发起调用的进程工作,能够访问该进程地址空间中的数据;

  • 而处理中断的内核代码则与进程异步(asynchronous),它不隶属于任何特定进程。

The role of a module is to extend kernel functionality; modularized code runs in kernel space. Usually a driver performs both the tasks outlined previously: some functions in the module are executed as part of system calls, and some are in charge of interrupt handling.

模块的作用是扩展内核功能,因此模块化代码运行在内核空间。通常,一个驱动程序会同时承担上述两种任务:模块中的部分函数作为系统调用的一部分执行,另一部分则负责处理中断。

补充说明:

  1. 内核空间与用户空间的本质区别二者是 CPU 通过 “特权级” 划分的内存区域和执行环境:

    • 内核空间:拥有最高特权级(如 x86 架构的 Ring 0),可直接访问硬件资源(如内存、CPU 寄存器、外设),执行特权指令(如修改内存映射、中断控制);
    • 用户空间:特权级较低(如 x86 架构的 Ring 3),无法直接操作硬件,需通过内核提供的 “系统调用”(如openread)间接访问资源,若越权操作会被 CPU 拦截并触发错误。
  2. “统一硬件视图” 的含义不同硬件(如不同品牌的显卡、硬盘)的底层接口存在差异,操作系统通过内核驱动将这些差异封装,为上层应用提供标准化接口(如文件系统接口、网络接口)。例如,应用程序调用write写入文件时,无需关心数据最终是存在机械硬盘还是 SSD—— 这一硬件差异由内核中的存储驱动处理。

  3. CPU 的 “保护机制”这是实现空间隔离的核心硬件支持。例如,当用户空间的应用程序试图直接访问内核空间的内存地址时,CPU 会检测到 “特权级不匹配”,立即触发 “页面错误”(page fault)或 “保护错误”(protection fault),并将控制权交还给内核,由内核终止违规程序,避免其破坏系统稳定性。这种硬件级保护是操作系统实现 “程序独立运行” 和 “资源安全” 的基础。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeeplyMind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值