前言

又开新坑了。选这门课是因为吃了CS自学指南的安利以及被某人催着自学。课是好课,不过做实验看起来有些难度,所以做个留档。

个人环境

放一个实验环境(neofetch)在这里做参考。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                    'c.          palemoons@MacBookPro
,xNMM. --------------------
.OMMMMo OS: macOS 12.1 21C52 arm64
OMMM0, Host: MacBookPro17,1
.;loddo:' loolloddol;. Kernel: 21.2.0
cKMMMMMMMMMMNWMMMMMMMMMM0: Uptime: 1 day, 7 hours, 36 mins
.KMMMMMMMMMMMMMMMMMMMMMMMWd. Packages: 193 (brew)
XMMMMMMMMMMMMMMMMMMMMMMMX. Shell: zsh 5.8
;MMMMMMMMMMMMMMMMMMMMMMMM: Resolution: 1440x900
:MMMMMMMMMMMMMMMMMMMMMMMM: DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX. WM: Quartz Compositor
kMMMMMMMMMMMMMMMMMMMMMMMMWd. WM Theme: Blue (Dark)
.XMMMMMMMMMMMMMMMMMMMMMMMMMMk Terminal: iTerm2
.XMMMMMMMMMMMMMMMMMMMMMMMMK. Terminal Font: Monaco 14
kMMMMMMMMMMMMMMMMMMMMMMd CPU: Apple M1
;KMMMMMMMWXXWMMMMMMMk. GPU: Apple M1
.cooc,. .,coo:. Memory: 2139MiB / 16384MiB

环境配置

进行所有实验之前,我们先需要启动课程提供的操作系统xv6。

除了包管理器homebrew,还需要安装编译工具链riscv-tools以及虚拟机qemu。前者安装比较顺利,直接按照实验操作手册里面的步骤来就行。比较麻烦的是高版本的qemu无法正常启动xv6,这导致不能用homebrew直接安装qemu(它只提供最新版本),为此只能编译特定版本的源码。

根据测试,下载v5.1.0版本的源码后,再打两个补丁即可正常安装qemu并启动xv6。

  1. 前往qemu官网下载页面选择5.1.0版本下载并解压。

  2. 打两个补丁。补丁1补丁2

  3. 为了加快编译速度以及避免编译全平台架构时挂掉,只选择编译riscv64-softmmu。配置如下:

    1
    ./configure --disable-kvm --disable-werror --prefix=/usr/local --target-list="riscv64-softmmu"

    设置target-list很重要,否则很容易在某些奇怪的地方报错导致全部木大。

  4. make && make install

到这里qemu安装完成,按照实验手册来即可正常启动xv6。

1
2
3
4
5
6
7
8
9
palemoons@MacBookPro ~/Documents/xv6-labs-2020 (util) $ make qemu
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

xv6 kernel is booting

hart 1 starting
hart 2 starting
init: starting sh
$

Gdb配置

Updated on 2022.3.10

Lecture3中提到了使用gdb调试程序,此处添加对于gdb的启动记录。

之前下载的riscv工具链里已经自带了gdb,且Mac M1也能正常使用,因此可以直接尝试启动。

1
2
make CPUS=1 qumu-gdb #窗口1中”调试模式“启动
riscv64-unknown-elf-gdb #窗口2中启动gdb

这里没有直接成功,出现了以下警告:

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
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=arm-apple-darwin21.2.0 --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
warning: File "/Users/palemoons/Documents/xv6-labs-2020/.gdbinit" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
add-auto-load-safe-path /Users/palemoons/Documents/xv6-labs-2020/.gdbinit
line to your configuration file "/Users/palemoons/.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
line to your configuration file "/Users/palemoons/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual. E.g., run from the shell:
info "(gdb)Auto-loading safe path"
(gdb)

大意是初始化文件.gdbinit的自动加载被禁用,这里程序建议添加全局配置文件把该工作区的初始化文件添加进自动加载列表。有兴趣可以试试~~,反正我没设置成功~~。也可以直接手动加载.gdbinit文件,在gdb环境下source .gdbinit

1
2
3
4
5
6
(gdb) source .gdbinit
The target architecture is set to "riscv:rv64".
warning: No executable has been specified and target does not support
determining executable automatically. Try using the "file" command.
0x0000000000001000 in ?? ()
(gdb)

实验自带的.gdbinit文件中,有对于监听端口的设置,需要把端口修改到实际对应的端口上。

References

参考了两个issue:

  1. make qemu
  2. make qemu时出现问题

gdb配置参考了一篇博客:

  1. MIT 6.S081/Fall 2020 搭建risc-v与xv6开发调试环境