xv6:Operating system organization
xv6:Operating system organization
本文逐段翻译 xv6 book rev5 的 Chapter 2,保留原文顺序、代码文件名、函数名、图表和练习;重点说明 operating system 如何同时实现 multiplexing、isolation 和 interaction。
TL;DR
- 三项要求:操作系统必须同时满足 multiplexing、isolation 和 interaction。
- 组织边界:user mode 与 supervisor mode 之间的硬件边界,为 system-call validation 和 process isolation 提供基础。
- 内核结构:monolithic kernel 便于子系统紧密协作;microkernel 将更多服务放到 user-level servers,以缩小 supervisor-mode 代码。
- xv6 process:一个 address space 加一个 thread;前者提供“自己的 memory”幻觉,后者提供“自己的 CPU”幻觉。
- 安全模型:假设 user code 可能恶意或有 bug,但 kernel 自身代码和 RISC-V hardware 按文档正确工作。
名词解释
| 名词 | 含义 | 本文对应物 |
|---|---|---|
| multiplexing | 在多个执行对象之间复用有限硬件资源 | xv6 在 processes 之间 time-share CPUs |
| isolation | 防止一个 process 破坏或窥探另一个 process 或 kernel | user/supervisor mode、address space、time-slicing |
| interaction | 允许 processes 以受控方式协作 | system calls、file descriptors、pipes |
| monolithic kernel | 大部分操作系统作为一个 supervisor-mode program 运行 | xv6、Linux 的主结构 |
| microkernel | kernel 只保留最小机制,其余服务作为 user-level servers 运行 | Minix、L4、QNX 等 |
| address space | process 可见的 virtual memory 范围 | xv6 的 per-process page table |
目录
| 章节 | 说明 |
|---|---|
| Chapter 2:Operating system organization | 本章问题与 RISC-V 平台 |
| 2.1 Abstracting physical resources | 为什么要用 operating system abstraction |
| 2.2 User mode、supervisor mode 和 system calls | privilege levels 与 ecall |
| 2.3 Kernel organization | monolithic kernel 与 microkernel |
| 2.4 Code:xv6 organization | xv6 源码文件分工 |
| 2.5 Process overview | address space、thread、stack |
| 2.6 Code:starting xv6、the first process and system call | xv6 启动和第一个 process |
| 2.7 Security Model | threat assumptions 和 panic |
| 2.8 Real world | 现代 kernel 的多线程 process |
| 2.9 Exercises | 原文练习 |
Chapter 2:Operating system organization
操作系统的一个关键要求,是同时支持多项活动。例如,可以使用 Chapter 1 中的 fork 和 exec system calls,同时启动一个 compiler 和一个 text editor 作为 processes。操作系统必须在这些 processes 之间 time-share CPUs、memory 等 resources。操作系统还必须安排 processes 之间的 isolation:如果一个 process 有 bug 并发生 malfunction,它不应该影响无关的 processes。不过,完全 isolation 又太强了,因为 processes 应当能够有意地交互;pipelines 就是一个例子。因此,操作系统必须满足三项要求:multiplexing、isolation 和 interaction。
本章概述操作系统如何组织自身,以实现这三项要求。实现方式有很多种,但本文聚焦于以 monolithic kernel 为中心的主流设计;许多 Unix operating systems 都采用这种设计。本章还概述 xv6 process——xv6 中 isolation 的基本单位。
xv6 运行在 multi-core RISC-V microprocessor 上,它的大量 low-level functionality(例如 process implementation)都特定于 RISC-V。RISC-V 是 64-bit CPU,而 xv6 使用“LP64” C 编写:在 C programming language 中,long(L)和 pointers(P)是 64 bits,但 int 是 32 bits。本书假设读者已经在某种 architecture 上做过少量 machine-level programming,并会在需要时介绍 RISC-V-specific ideas。完整的 specification 可参阅 user-level ISA [2] 和 privileged architecture [3] 文档,也可以参考 The RISC-V Reader: An Open Architecture Atlas [15]。
完整计算机中的 CPU 周围有 support hardware,其中相当一部分以 I/O interfaces 的形式存在。xv6 面向 qemu 的 -machine virt option 所模拟的 support hardware 编写。这些 hardware 包括 RAM、包含 boot code 的 ROM、连接 user keyboard/screen 的 serial connection,以及用于存储的 disk。
2.1 Abstracting physical resources
遇到操作系统时,人们首先可能会问:为什么需要它?也就是说,可以把图 1.2 中的 system calls 实现成一个 library,让 applications 与该 library link。按照这种方案,每个 application 甚至可以拥有一个为自身需求定制的 library。applications 可以直接与 hardware resources 交互,并以最适合自身的方式使用这些 resources(例如实现高性能或可预测性能)。一些 embedded devices 或 real-time systems 的 operating systems 就是这样组织的。
这种 library 方案的缺点是:如果同时运行多个 applications,那么 applications 必须行为良好。例如,每个 application 都必须定期放弃 CPU,让其他 applications 有机会运行。如果所有 applications 都互相信任且没有 bugs,这种 cooperative time-sharing 方案可能没问题。但 applications 通常并不互信,也可能有 bugs,因此人们往往希望获得比 cooperative scheme 更强的 isolation。
为了实现 strong isolation,禁止 applications 直接访问敏感的 hardware resources,而是把 resources 抽象成 services,会很有帮助。例如,Unix applications 只通过 file system 的 open、read、write 和 close system calls 与 storage 交互,而不是直接读取和写入 disk。这样既向 application 提供了 pathnames 的便利,也允许提供该 interface 的 operating system 管理 disk。即使 isolation 不是关注点,有意交互的 programs(或者只是希望不要妨碍彼此的 programs)也很可能发现,file system 比直接使用 disk 更方便。
类似地,Unix 会在 processes 之间透明地切换 hardware CPUs,并在必要时保存和恢复 register state,因此 applications 不必意识到 time-sharing 的存在。这种 transparency 允许 operating system 共享 CPUs,即使某些 applications 陷入 infinite loops。
再例如,Unix processes 使用 exec 构造自己的 memory image,而不是直接与 physical memory 交互。这样 operating system 可以决定把 process 放在 memory 的什么位置;如果 memory 紧张,operating system 甚至可能把 process 的部分 data 存到 disk 上。exec 还为 users 提供了 file system,用来存储 executable program images。
Unix processes 之间的许多 interaction 都通过 file descriptors 发生。file descriptors 不仅隐藏了许多细节(例如 pipe 或 file 中的 data 存放在哪里),它们的定义还简化了 interaction。例如,如果 pipeline 中的一个 application exit 或 fail,kernel 会自动为 pipeline 中的下一个 process 生成 end-of-file signal。
图 1.2 中的 system-call interface 经过精心设计,既为 programmers 提供便利,又保留实现 strong isolation 的可能性。Unix interface 并不是抽象 resources 的唯一方式,但事实证明它是一个很好的方式。
2.2 User mode、supervisor mode、和 system calls
Strong isolation 需要在 applications 与 operating system 之间建立硬边界。即使 application 有 bug 或是恶意的,也不应允许它干扰 operating system 或其他 programs 的运行。为了实现 strong isolation,operating system 必须安排 applications 不能修改(甚至不能读取)operating system 的 data structures 和 instructions,也不能访问其他 processes 的 memory。
CPUs 提供了 strong isolation 所需的 hardware support。例如,RISC-V 有三个 privilege levels,它们限制 code 能够做什么:machine mode、supervisor mode 和 user mode。machine mode 中执行的 instructions 拥有 full privilege;CPU 从 machine mode 开始运行。machine mode 主要用于在 boot 期间设置 computer。xv6 在 machine mode 中短暂执行,然后切换到 supervisor mode。
在 supervisor mode 中,CPU 可以执行 privileged instructions,例如 enable 和 disable interrupts,读取和写入保存 page table 地址的 register 等。如果 user mode 中的 application 尝试执行 privileged instruction,CPU 不会执行该 instruction,而是 trap 到 supervisor mode 中的特殊 code;该 code 可以终止 application。Chapter 1 的 Figure 1.1 展示了这种组织方式。application 只能执行 user-mode instructions(例如加法),因此称为在 user space 中运行;supervisor mode 中的软件还可以执行 privileged instructions,因此称为在 kernel space 中运行。在 kernel space 中运行的软件(也就是 supervisor mode 中的软件)称为 kernel。
applications 通过 read 等 system calls 与 kernel 交互。applications 不允许直接调用 kernel functions,也不允许访问 kernel memory。RISC-V 为 system calls 提供 ecall instruction;它把 CPU 从 user mode 切换到 supervisor mode,并跳转到 kernel 指定的 entry point。CPU 切换到 supervisor mode 后,kernel 可以验证 system call 的 arguments(例如检查传入 system call 的 address 是否属于 application memory),判断 application 是否被允许执行请求的 operation(例如检查 application 是否被允许写入指定 file),然后拒绝请求或执行它。kernel 必须控制进入 supervisor mode 的 entry point,这一点很重要;如果 application 能够决定 kernel entry point,恶意 application 就可能在跳过 arguments validation 的位置进入 kernel。
2.3 Kernel organization
一个关键设计问题是:操作系统的哪些部分应该在 supervisor mode 中运行?一种可能是让整个 operating system 都位于 kernel 中,让所有 system calls 的 implementations 都在 supervisor mode 中运行。这种组织方式称为 monolithic kernel。
在 monolithic organization 中,整个 operating system 是一个运行在 supervisor mode 中的 single program。这种组织方式方便的一个原因是,OS designer 不必把 code 划分为需要和不需要 supervisor privileges 的部分。此外,operating system 的不同部分也容易协作,因为它们都属于同一个 program。例如,monolithic kernel 可以让 file system 和 virtual memory system 共享 disk block cache。
缺点是,monolithic kernels 往往会变得庞大而复杂,以至于没有任何一个 developer 能理解 code 不同部分之间的全部交互;这很容易产生 bugs。kernel 中的 bug 尤其麻烦,因为它可能导致整个 computer crash,使许多 applications malfunction,或者让整个 computer 暴露在 security attacks 之下。
microkernel 的目标是减少 kernel 中 bugs 的发生。其思想是把绝对最少的 functionality 放入 kernel 本身,让尽可能少的 code 在 supervisor mode 中执行,使 kernel 易于理解并分析其 correctness。operating system 的主体作为 user-level server processes 运行。例如,file system code 会作为 server process 在 user mode 中执行,而不是在 supervisor mode 中执行。
图 2.1 展示了这种 microkernel design。图中,file system 作为 user-level server process 运行。为了允许 applications 与 file server 交互,kernel 提供 inter-process communication mechanism,使一个 user-mode process 能向另一个 user-mode process 发送 messages。例如,如果 shell 这样的 application 想读写 file,它会向 file server 发送 message 并等待 response。
在 microkernel 中,kernel interface 由少量 low-level functions 组成,用于启动 applications、发送 messages、访问 device hardware 等。由于操作系统的大部分内容位于 user-level servers 中,这种组织方式可以让 kernel 相对简单。
现实世界中,monolithic kernels 和 microkernels 都很流行。许多 Unix kernels 是 monolithic 的。例如,Linux 有 monolithic kernel,尽管某些 OS functions 作为 user-level servers 运行(例如 window system)。Linux 为 OS-intensive applications 提供高性能,部分原因是 kernel 的 subsystems 可以紧密集成。
Minix、L4 和 QNX 等 operating systems 采用带 servers 的 microkernel 组织方式,并在 embedded settings 中得到广泛部署。L4 的一个 variant——seL4——足够小,已经针对 memory safety 和其他 security properties 完成 verification [8]。
操作系统 developers 对哪一种组织方式更好存在许多争论,但没有哪一方拥有决定性证据。此外,这很大程度上取决于“更好”意味着什么:更快的 performance、更小的 code size、更可靠的 kernel,还是更可靠的完整 operating system(包括 user-level services)等。
还有一些 practical considerations 可能比组织方式本身更重要。有些 operating systems 拥有 microkernel,但为了 performance 的原因,把部分 user-level services 运行在 kernel space 中。有些 operating systems 拥有 monolithic kernels,是因为它们就是这样起步的;而迁移到 pure microkernel organization 缺乏足够 incentive,因为添加新 features 可能比重写现有 operating system 以适应 microkernel design 更重要。
从本书的角度看,microkernel 和 monolithic operating systems 共享许多关键 ideas。二者都实现 system calls、使用 page tables、处理 interrupts、支持 processes、使用 locks 进行 concurrency control、实现 file system 等。本书关注这些 core ideas。
xv6 像大多数 Unix operating systems 一样,实现为 monolithic kernel。因此,xv6 kernel interface 就对应 operating system interface,而 kernel 实现了完整的 operating system。由于 xv6 不提供很多 services,它的 kernel 比某些 microkernels 更小;但从概念上说,xv6 是 monolithic 的。
2.4 Code:xv6 organization
xv6 kernel source 位于 kernel/ sub-directory 中。图 2.2 列出了这些 files,并按 kernel 的主要 responsibilities 分组:starting the system(booting)、creating and controlling processes、handling traps(interrupts 和 system calls)、allocating memory 与 configuring virtual addresses、controlling devices,以及 managing the file-system。
| File | Description |
|---|---|
entry.S | Very first boot instructions. |
main.c | Control initialization of other modules. |
start.c | Early machine-mode boot code. |
exec.c | exec() system call. |
proc.c | Processes and scheduling. |
swtch.S | Thread switching. |
sysproc.c | Process-related system calls. |
kernelvec.S | Handle traps from kernel code. |
trampoline.S | Handle traps from user code. |
trap.c | C code to handle and return from traps and interrupts. |
syscall.c | Dispatch system calls to handling function. |
vm.c | Manage page tables and address spaces. |
kalloc.c | Physical page allocator. |
console.c | Connect to the user keyboard and screen. |
plic.c | RISC-V interrupt controller. |
printf.c | Formatted output to the console. |
uart.c | Serial-port console device driver. |
virtio_disk.c | Disk device driver. |
bio.c | Disk block cache for the file system. |
file.c | File descriptor support. |
fs.c | File system. |
log.c | File system logging and crash recovery. |
sysfile.c | File-related system calls. |
pipe.c | Pipes. |
sleeplock.c | Locks that yield the CPU. |
spinlock.c | Locks that don’t yield the CPU. |
string.c | C string and byte-array library. |
图 2.2: Xv6 kernel source files。
2.5 Process overview
在 xv6(以及其他 Unix operating systems)中,process 是 isolation 的基本单位。process abstraction 防止一个 process 破坏或窥探另一个 process 的 memory、CPU、file descriptors 等;它也防止 process 破坏 kernel 本身,因此 process 无法颠覆 kernel 的 isolation mechanisms。kernel 必须谨慎地实现 process abstraction,因为有 bug 或恶意的 application 可能诱骗 kernel 或 hardware 做出坏事(例如绕过 isolation)。kernel 用于实现 processes 的 mechanisms 包括 user/supervisor mode flag、address spaces 和 thread 的 time-slicing。
为了帮助实现 isolation,process abstraction 给 program 造成一种幻觉:它拥有自己的 private machine。process 为 program 提供看起来属于自己的 private memory system,即 address space;其他 processes 无法读取或写入该 address space。process 还为 program 提供看起来属于自己的 CPU,用来执行 program 的 instructions。
xv6 使用由 hardware 实现的 page tables,为每个 process 提供独立的 address space。RISC-V page table 把 virtual address(RISC-V instruction 操作的 address)翻译(或“map”)成 physical address(CPU 发送给 main memory 的 address)。
xv6 为每个 process 维护一个独立的 page table,用来定义该 process 的 address space。如图 2.3 所示,address space 包含从 virtual address zero 开始的 process user memory。首先是 instructions,之后是 global variables,然后是 stack,最后是“heap”区域(供 malloc 使用);process 可以按需扩展 heap。
限制 process address space 最大 size 的因素有很多:RISC-V 上的 pointers 宽度为 64 bits;hardware 在 page tables 中查找 virtual address 时只使用低 39 bits;而 xv6 只使用这 39 bits 中的 38 bits。因此,最大 address 是 2^38 − 1 = 0x3fffffffff,即 MAXVA (0899)。
在 address space 的顶部,xv6 放置一个 trampoline page(4096 bytes)和一个 trapframe page。xv6 使用这两个 pages 在进入 kernel 和返回时完成 transition;trampoline page 包含进出 kernel 的 code,trapframe 是 kernel 保存 process user registers 的位置,Chapter 4 会解释这一点。
xv6 kernel 为每个 process 维护许多 pieces of state,并将它们收集到一个 struct (2034) 中。process 最重要的 kernel state 包括它的 page table、kernel stack 和 run state。我们使用 p->xxx 这样的 notation 表示 proc structure 的 elements;例如,p->pagetable 是指向 process page table 的 pointer。
现在请阅读定义 struct proc 的 kernel/proc.h。对你来说,理解 xv6 code 比理解本书更重要;你应当优先阅读 code,需要澄清 code 时再查阅本书。有些 code 的目的起初可能并不明显,但进一步阅读和搜索 code 会有帮助。请自由探索和修改 code。
每个 process 都有一个 control thread(简称 thread),保存执行该 process 所需的 state。在任意时刻,一个 thread 可能正在某个 CPU 上执行,也可能 suspended(没有执行,但未来能够恢复执行)。为了在 processes 之间切换 CPU,kernel 会暂停当前运行在该 CPU 上的 thread 并保存其 state,然后恢复另一个 process 之前 suspended 的 thread 的 state。
一个 thread 的大量 state(local variables、function-call return addresses)都存储在线程的 stacks 上。每个 process 有两个 stacks:user stack 和 kernel stack(p->kstack)。当 process 执行 user instructions 时,只有 user stack 在使用,kernel stack 是空闲的。当 process 进入 kernel(执行 system call 或响应 interrupt)时,kernel code 在 process 的 kernel stack 上执行;process 位于 kernel 中时,它的 user stack 仍包含已保存的 data,但不会被主动使用。process 的 thread 会在主动使用 user stack 和 kernel stack 之间交替。kernel stack 是独立的,并且受到 user code 保护,因此即使 process 已经破坏自己的 user stack,kernel 仍然可以执行。
process 的 user code 可以通过执行 RISC-V ecall instruction 发起 system call。该 instruction 切换到 supervisor mode,并把 program counter 改为 kernel 定义的 entry point。entry point 处的 code 切换到 process 的 kernel stack,并执行实现该 system call 的 kernel instructions。system call 完成后,kernel 通过执行 sret instruction 返回 user space;sret 切换到 user mode,并从 system-call instruction 之后的位置继续执行 user instructions。process 的 thread 还可以在 kernel 中 block,等待 I/O;I/O 完成后,从中断的位置恢复执行。
p->state 表示 process 当前是已分配、ready to run、正在某个 CPU 上运行、等待 I/O,还是正在退出。
p->pagetable 保存 process 的 page table,格式符合 RISC-V hardware 的要求。xv6 让 paging hardware 在 process 执行 user code 时使用该 process 的 p->pagetable。process 的 page table 也记录了用于保存 process memory 的 physical pages 的地址。
总之,一个 process 结合了两个 design ideas:address space 让 process 产生拥有自己 memory 的幻觉,thread 让 process 产生拥有自己 CPU 的幻觉。在 xv6 中,一个 process 由一个 address space 和一个 thread 组成。在真实 operating systems 中,一个 process 可能拥有多个 threads,以利用多个 CPUs。
2.6 Code:starting xv6、the first process and system call
为了让 xv6 更具体,本节概述 kernel 如何启动并运行第一个 process。后续章节会更详细地描述本概览中出现的 mechanisms。
请阅读 kernel/entry.S、kernel/start.c、kernel/main.c 和 user/init.c。
RISC-V computer 上电时,会初始化自身并运行一个存储在 read-only memory 中的 boot loader。boot loader 把 xv6 kernel 复制到 physical address 0x80000000 的 memory 中。它把 kernel 放在 0x80000000 而不是 0x0 的原因,是 address range 0x0:0x80000000 包含 I/O devices。
随后 boot loader 跳转到 _entry (1006),从这里启动 xv6。RISC-V 启动时 paging hardware 是 disabled 的:virtual addresses 直接 map 到 physical addresses。_entry 处的 instructions 设置一个 stack,使 xv6 能运行 C code。xv6 在 start.c (1060) 中为这个 stack 声明空间 stack0。_entry 处的 code 把 stack-pointer register sp 加载为 stack0+4096,也就是 stack 顶部,因为 RISC-V 的 stack 向下增长。现在 kernel 有了 stack,_entry 调用 start (1064) 处的 C code。
start function 执行一些只有 machine mode 中的 CPU 才允许执行的 setup;其中最重要的是编程 clock chip,使它产生 timer interrupts。然后,start 使用 RISC-V mret instruction 切换到 supervisor mode,并跳转到 main (1160)。mret 需要一些 setup:start 在 mstatus register 中把 previous privilege mode 设置为 supervisor;把 main 的 address 写入 mepc register,以设置 destination address;向 page-table register satp 写入 0,以在 supervisor mode 中禁用 virtual address translation;并把所有 interrupts 和 exceptions delegate 给 supervisor mode。
main (1160) 初始化多个 devices 和 subsystems 后,通过调用 userinit (2327) 创建第一个 process。所有新创建的 processes 都从 kernel 中的 forkret (2653) 开始执行。对于第一个 process 这个特殊情况,forkret 调用 kexec,加载 user program /init。
调用 kexec 后,forkret 在 /init process 中返回 user space。init (7764) 在需要时创建新的 console device file,并将其打开为 file descriptors 0、1 和 2。然后,它在 console 上启动 shell。系统启动完成。
2.7 Security Model
你可能会疑惑操作系统如何处理有 bug 或恶意的 code。由于应对恶意行为严格比处理意外 bugs 更难,所以把主要注意力放在防御恶意行为上是合理的。下面是 operating system design 中典型 security assumptions 和 goals 的 high-level view。
操作系统必须假设 process 的 user-level code 会尽最大努力破坏 kernel 或其他 processes。user code 可能尝试 dereference allowed address space 之外的 pointers;可能尝试执行不允许 user code 执行的 instructions;可能尝试读取和写入 RISC-V control registers;可能尝试访问 device hardware;也可能向 system calls 传递精心构造的 values,试图诱骗 kernel crash 或做出愚蠢的事情。
kernel 的目标是约束每个 user process,使它只能访问自己的 user memory,使用 32 个 general-purpose RISC-V registers,并以 system calls 预期允许的方式影响 kernel 和其他 processes。kernel 必须阻止其他一切 actions。这些通常是 kernel design 中的绝对要求。
对 kernel 自身 code 的期待则不同。我们假设 kernel code 由善意且谨慎的 programmers 编写,没有 bugs,也不包含恶意内容。这一假设会影响我们分析 kernel code 的方式。例如,有许多内部 kernel functions(例如 spin locks),如果 kernel code 使用不正确,就会造成严重问题。不过,我们假设 kernel 正确使用自己的 functions。在 hardware 层面,我们假设 RISC-V CPU、RAM、disk 等按照 documentation 中的说明工作,不存在 hardware bugs。
现实生活并没有这么简单。要阻止 abusive user programs 通过 system calls 消耗 kernel-protected resources,使 system 无法使用,是很困难的;这些 resources 包括 disk space、CPU time、process-table slots 等。写出 100% 没有 bugs 的 kernel code,或者设计出没有 bugs 的 hardware,通常是不可能的;如果恶意 user code 的作者知道 kernel 或 hardware 的 bugs,他们就会利用这些 bugs。即使在 Linux 这样的成熟且广泛使用的 kernel 中,人们也经常发现此前未知的 vulnerabilities [1]。最后,user code 和 kernel code 的界线有时会变得模糊:某些 privileged user-level processes 可能提供 essential services,实际上成为 operating system 的一部分;在某些 operating systems 中,privileged user code 甚至可以向 kernel 插入新 code(例如 Linux 的 loadable kernel modules 和 eBPF)。
作为应对 kernel bugs 的部分防御,xv6 code 会检查 inconsistencies 和 unrecoverable errors,并通过调用 panic() 做出“panic”响应。该 function 打印 error message 并 halt system。panic 并不可取,但比继续执行更好。通常,panic 是由 kernel bug 引起的:该 bug 使 kernel data 不正确,或者使 kernel 执行非法 action,例如引用不存在的 memory。在这种情况下,与其尝试在不一致状态下继续运行,不如使用 panic() 停止执行,这样更安全。kernel developer 会通过定位并修复底层 code bug 来响应 panic。
2.8 Real world
大多数 operating systems 都采用 process concept,并且大多数 processes 看起来与 xv6 的 processes 相似。不过,modern operating systems 支持一个 process 中包含多个 threads,使单个 process 能利用多个 CPUs。在一个 process 中支持多个 threads,需要相当多 xv6 没有的 machinery,通常还包括 interface changes(例如 Linux 的 clone,一种 fork 的 variant),用来控制 process 的哪些 aspects 在 threads 之间共享。
2.9 Exercises
- 为 xv6 添加一个 system call,返回当前可用的 free memory 数量。
图与格式复核
- Figure 2.1:microkernel 与 file-system server 示意图,已重建为 SVG,并放在原文讨论 microkernel design 的位置,目标显示宽度 480px。
- Figure 2.2:xv6 kernel source files,保留为 Markdown table;文件名、扩展名和职责逐项对应原文。
- Figure 2.3:process virtual address space layout,已重建为 SVG,并放在
MAXVA说明之后,目标显示宽度 520px。 - 代码/源码引用:保留
kernel/、user/、函数名、register 名称、地址和 booklet 行号;不翻译 API 或源码标识符。
术语对照
| English | 译法/处理 |
|---|---|
| multiplexing | multiplexing;必要处译作复用 |
| isolation | isolation;必要处译作隔离 |
| interaction | interaction;必要处译作交互 |
| privilege level | privilege level;必要处译作特权级别 |
| machine mode / supervisor mode / user mode | 保留英文模式名 |
| monolithic kernel / microkernel | 保留英文结构名 |
| address space | address space;必要处译作地址空间 |
| thread / stack | 保留英文,必要处译作线程/栈 |
| trap / interrupt / exception | 保留英文,按上下文译作 trap/中断/异常 |
参考资料
- MIT 6.1810 Fall 2025:课程主页
- Russ Cox、Frans Kaashoek、Robert Morris:xv6 book rev5
- MIT PDOS:xv6-riscv source
评论 (0)