Xv6-RISC-V学习笔记

简介

xv6-public GitHub

xv6-riscv Gitee

麻省理工6.828课程

英文文档

中文文档

xv6 是 MIT 开发的一个教学用的完整的类 Unix 操作系统,并且在 MIT 的操作系统课程 6.828 中使用。通过阅读并理解 xv6 的代码,可以清楚地了解操作系统中众多核心的概念,对操作系统感兴趣的同学十分推荐一读!这份文档是中文翻译的 MIT xv6 文档,是阅读代码过程中非常好的参考资料。

qemu调试xv6环境

环境搭建

下载xv6源码(riscv版本)

安装编译环境

1
sudo apt-get install -y qemu-system-misc binutils-riscv64-linux-gnu gcc-riscv64-linux-gnu gdb-multiarch

直接make编译,make qemu启动环境。

若使用VSCode进行调试,参考附录进行配置。

QEMU virt简介

QEMU riscv 启动代码

QEMU virt有8个hart,通过汇编指令csrr a1, mhartid可以读取hart对应的值。所有的hart都会执行执行内核代码。

Bootloader在启动后,会将内核放置在0x80000000

附录

VSCode一键调试

xv6-riscv目录中放置xv6源码

  • 已知问题,启动过程中QEMU直接退出,需要修改GDB配置文件.gdbinit
1
2
3
4
5
6
set confirm off
set architecture riscv:rv64
@REM target remote 127.0.0.1:26000
symbol-file kernel/kernel
set disassemble-next-line auto
set riscv use-compressed-breakpoints yes
  • tasks.json内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/xv6-riscv"
},
"tasks": [
{
"label": "xv6-qemu-task",
"type": "shell",
// "command": "echo all done",
"isBackground": true,
"command": "make && make qemu-gdb",
"problemMatcher": [
{
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"beginsPattern": ".*Now run 'gdb' in another window.",
"endsPattern": "."
}
}
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
  • launch.json内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "xv6-qemu-gdb",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/xv6-riscv/kernel/kernel",
"stopAtEntry": true,
"cwd": "${workspaceRoot}/xv6-riscv",
"miDebuggerServerAddress": "127.0.0.1:26000",
"miDebuggerPath": "/usr/bin/gdb-multiarch",
"MIMode": "gdb",
"preLaunchTask": "xv6-qemu-task",
}
]
}