OSTEP:Laboratory:xv6 Projects
OSTEP:Laboratory:xv6 Projects
本篇对应 OSTEP Version 1.10 的 Appendix H “Laboratory: xv6 Projects”。这些 project 直接修改 MIT teaching kernel xv6,把 OSTEP 的概念落实到 kernel implementation。
类型与定位
- 笔记类型:实验项目翻译/知识整理
- 翻译基准:官方 Laboratory: xv6 Projects
- 保真约定:xv6、system call、kernel、page table、inode、journal 等保留原样;项目任务按原文顺序整理。
- 图件说明:原文没有独立图片。
- 源文档核对:官方材料共三页,正文按 H.1–H.11 连续展开;PDF 页脚和版本信息不作为项目内容录入。
目录
| 章节 | 内容 |
|---|---|
| 类型与定位 | 材料范围、翻译基准与保真约定 |
| 项目安排 | xv6、合作方式与官方项目仓库 |
| 项目清单 | 全部 xv6 project 与对应主线 |
| H.1 Intro Project | system call 入门项目 |
| H.2 Processes and Scheduling | scheduler、exit/wait 与 process state |
| H.3 Intro to Virtual Memory | VA/PA、invalid address 与 page table |
| H.4 Copy-on-write Mappings | vfork 与 COW |
| H.5 Memory mappings | lazy page-in 与 mmap |
| H.6 Kernel Threads | clone、shared address space 与 locks |
| H.7 Advanced Kernel Threads | spin/sleep locks 与 condition variables |
| H.8 Extent-based File System | extent inode |
| H.9 Fast File System | block groups 与 allocation policy |
| H.10 Journaling File System | on-disk log 与 crash recovery |
| H.11 File System Checker | xv6 file-system consistency |
| 项目验收 | 项目完成标准与验证要求 |
项目安排
xv6 是 MIT 提供的 teaching kernel,也很适合动手实践;完成这些 projects 会让课堂上学习的 material 与项目更加直接相关。除了最开始的一两个 project 之外,这些 projects 通常两人合作完成,让面对 kernel 的困难任务稍微容易一些。
官方项目描述与 testing framework 会逐步加入 ostep-projects;如果要实际完成 projects,应以该仓库的最新内容为准。
项目清单
| 项目 | 主要内容 | 对应主线 |
|---|---|---|
| Intro Project | 添加简单 system call | system call、trap |
| Processes and Scheduling | 修改 scheduler、exit/wait error | processes、CPU virtualization |
| Intro to Virtual Memory | VA 到 PA translation、invalid address | address translation |
| Copy-on-write Mappings | 实现 vfork 与 COW | page table、fork |
| Memory mappings | lazy page-in、mmap | virtual memory、file mapping |
| Kernel Threads | clone、shared address space、simple locks | concurrency |
| Advanced Kernel Threads | spin/sleep locks、condition variables | synchronization |
| Extent-based File System | inode 使用 extent | file system |
| Fast File System | block groups、allocation policy、large-file exception | FFS |
| Journaling File System | on-disk log 与 crash recovery | crash consistency |
| File System Checker | 检查 xv6 file-system consistency | FSCK |
H.1 Intro Project
为 xv6 添加简单 system call,例如:
- 统计每个 system call 被调用的次数;
- 返回 kernel 中可查询的其他信息;
- 理解 system call 从 user code 进入 kernel、完成 dispatch 并返回的全过程。
一种典型变体是为每个 system call 设置一个 counter,统计它被调用了多少次;也可以设计其他 information-gathering calls。通过这个 project,学生会真正理解一次 system call 是如何发生的。
H.2 Processes and Scheduling
修改默认 Round Robin scheduler,尝试:
- Lottery scheduler;
- Multi-level feedback queue;
- 其他 priority policy;
- 让 exit 返回 error code;
- 通过 wait 获取 child 的 error code;
- 观察 context switch 和 process state。
对应 OSTEP:进程抽象、OSTEP:CPU 调度导论、OSTEP:多级反馈队列。
一个小型 addendum 是要求 process 在 exit 时返回 proper error code,并能够通过 wait() system call 取得该 error code。
H.3 Intro to Virtual Memory
添加 system call:给定 virtual address,返回 translated physical address,或报告地址无效。这个 project 让学生直接观察:
- page table 如何建立;
- VA 到 PA 如何转换;
- invalid access 如何处理;
- null-pointer dereference 如何触发 fault。
它让学生看到 virtual memory system 如何建立 page tables,同时不需要完成太多额外工作。另一个 variant 是修改 xv6,使 null-pointer dereference 真正生成 fault。
H.4 Copy-on-write Mappings
这个 project 为 xv6 添加一种轻量级的 fork(),称为 vfork():
- 不直接复制 mappings,而是将 parent/child 设置为指向 shared pages 的 copy-on-write mappings;
- 当 reference 触及这类 page 时,kernel 创建真实 copy;
- 相应更新 page tables。
对应 OSTEP:Complete Virtual Memory Systems 中的 COW。
H.5 Memory mappings
增加 memory-mapped file 支持。简单版本可以对 executable code pages 做 lazy page-in;完整版本实现 mmap system call,并在 page dereference 时从 disk fault in page。
这是一条与前面 virtual memory project 不同的实现路径:完整方案需要构建 mmap() system call 以及在 dereference 时从 disk fault in pages 所需的全部 infrastructure。
H.6 Kernel Threads
增加 clone system call,使新 kernel thread 与 caller 共享同一 address space;再在其上建立简单 thread library 和 locks。
clone() 的行为类似 fork(),但使用同一个 address space。学生需要据此理解如何实现 system call,以及如何创建真正的 kernel thread。
H.7 Advanced Kernel Threads
在 Kernel Threads project 的基础上继续实现:
- spin locks;
- processor 不可用时 sleep 的 locks;
- condition variables;
- 所需的 kernel support。
这个 project 在 kernel threads 之上构建更完整的 thread library,并加入多种 locks,包括 spin locks、processor 不可用时会 sleep 的 locks,以及 condition variables;同时补充所需的 kernel support。
H.8 Extent-based File System
对 file type EXTENT 修改 inode,使其存储 extent,也就是 pointer/length pair,而不只是普通 block pointer。这是理解 file-system layout 的较轻量入口。
这是一个相对轻量的 file-system 入门项目;它要求理解 extent(pointer、length pair)如何替代普通 block pointers。
H.9 Fast File System
把基本 xv6 file system 改造成 Berkeley FFS:
- 新建 mkfs tool;
- 引入 block groups;
- 设计新的 block-allocation policy;
- 增加 large-file exception;
- 深入理解 locality、metadata 和 allocation。
H.10 Journaling File System
为 xv6 添加 rudimentary journaling layer:
- 收集一次 file write 造成的 dirty blocks;
- 将 pending update 写入 on-disk log;
- 之后才原地修改 data blocks;
- 人为加入 crash points;
- 验证 recovery 后 file system 始终保持 consistent state。
对应 OSTEP:Crash Consistency:FSCK 与 Journaling。
H.11 File System Checker
为 xv6 file system 实现 checker,学习:
- consistency invariant;
- inode、bitmap、directory 和 link 的关系;
- 如何遍历 image;
- 如何报告或修复错误。
项目验收
- [ ] 固定 xv6 source、toolchain、architecture 和 emulator;
- [ ] 每项修改建立独立 commit;
- [ ] 记录 kernel build、boot、test 命令;
- [ ] 为 page fault、concurrent access、crash recovery 等边界写测试;
- [ ] 说明每项 project 验证的 OS abstraction;
- [ ] 保存关键 console output 和失败复盘。
当前撰写与执行记录
| 项目组 | 文档状态 | 实现/运行状态 | 首轮验收重点 |
|---|---|---|---|
| Intro Project | ✅ 已整理 | ⬜ 未执行 | system call dispatch 与 user/kernel boundary |
| Processes and Scheduling | ✅ 已整理 | ⬜ 未执行 | process state、scheduler trace、wait/exit |
| Virtual Memory | ✅ 已整理 | ⬜ 未执行 | VA/PA、page fault、invalid address |
| Copy-on-write / mmap | ✅ 已整理 | ⬜ 未执行 | PTE flags、fault path、shared mappings |
| Kernel Threads | ✅ 已整理 | ⬜ 未执行 | shared address space、lock、sleep/wakeup |
| File System Projects | ✅ 已整理 | ⬜ 未执行 | inode、allocation、journal、checker |
运行前必须固定 xv6 source commit、compiler/toolchain、emulator、architecture 和 test command;运行后保存 kernel build、boot、测试输出,以及每个失败点对应的 invariant。
材料基线核对
当前 ostep-projects HEAD 为 76cff3f89f4bf337af6e02e53a831b7eeb1396df。已通过 GitHub codeload 获取 README 指定的 mit-pdos/xv6-public source(master HEAD eeb7b415dbcb12cc362d0783e41c3d1f44066b17)到临时工作目录。source 完整存在,但当前 macOS 环境缺少 i386-elf-gcc,实际执行 make TOOLPREFIX=i386-elf- qemu-nox 在编译 ulib.o 时以 exit 2 失败;同时缺少 qemu-system-x86_64,因此尚未 boot 或运行 test harness。
find initial-xv6 -maxdepth 2 -type f
./test-getreadcount.sh
# 当前 checkout 的 project harness 仍需把 source 放入 initial-xv6/src/
# source baseline build: exit=2, i386-elf-gcc: No such file or directory
source 获取已完成;当前阻塞是 cross-toolchain 与 QEMU 未安装,而不是 project code 的测试失败。补齐工具后再把 source 放入 project harness 并记录具体 build、boot 和 test output。
参考资料
评论 (0)