xv6-riscv环境搭建

Docker环境配置

xv6-riscv 是MIT 6.S081课程使用的一个操作系统,其实验课是通过修改xv6内核来实现相应的功能,对于实验的第一步就是把环境搭建好,运行xv6,本身使用的是Mac,在最开始的时候打算使用Mac原生的环境,但是因为目前在brew的包管理器中,qemu的版本太高,在官网上也提示了会因为qemu的问题导致xv6运行失败,会一直卡在系统的启动那边,遂放弃,转向Docker进行环境配置。

使用的是Ubuntu镜像,使用Docker命令,获取docker命令

1
docker pull ubuntu

使用docker images 命令可以看到你的系统中现存的docker所有镜像

使用-v命令使得镜像可以共享主机的文件夹,其中{container name}为你要命名该运行容器的名字,{host dir}为主机的文件夹名称,{docker dir} 为Docker容器的文件夹名称

1
docker run -itd --name={container name} -v {host dir}:{docker dir} ubuntu /bin/bash

运行该命令后便进入shell控制台上,在后续的docker使用中,可以直接使用 docker start {container name}或者使用如下命令docker ps -a查看所有Docker镜像,然后将上述的{container name} 换成 {container id} 再通过 docker exec -it {container id} /bin/bash 进入Docker容器中,步骤如下图所示:

运行如下命令,安装编译链

1
apt install gcc-riscv64-unknown-elf git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu

检查环境是否安装成功,运行riscv64-unknown-elf-gcc --versionqemu-system-riscv64 --version,切记在Fall 2020的课程中,qemu的版本不能太高,否则会卡死,这个bug目前还没解决,至此所有的依赖环境都安装完毕

使用git clone git://g.csail.mit.edu/xv6-labs-2020 将实验代码拷贝下来,进入xv6代码目录中,切换分支git checkout util分支,键入 make qemu,出现下图所示的的内容,则运行成功,若要退出系统,按住键盘 ctrl+a 组合建,然后放开,再按x。

简单调试方法

简单查看xv6的目录结构,主要的文件夹是 >>> kernel >>> mkfs >>> user

其中kernel文件夹就是存放内核代码,你要为xv6系统添加新功能就在这个文件夹下修改,user就是存放用户态的代码,会进行系统调用之类的操作。

在写代码中,推荐使用VS Code编辑器,调试的话,使用gdb,这个gdb也是接触这个课后才使用,记录下常用命令。

对于调试输入make qemu-gdb 在同一文件夹下另开一个窗口输入 gdb-multiarch -q kernel/kernel,在VS Code中有终端拆分的功能,比较方便。

对于在内核的代码,可以直接在main函数上打断点,可以看见是在kernel/main.c文件中,这样看很别扭,无法直观看到现在的代码执行到哪,别担心,gdb提供了简单的可视化功能,输入layout split

上半部分显示的为源代码,下半部分显示的是每一条指令,在调试中next是逐行运行,nexti是逐指令运行,step是进入改行代码,stepi类似,finish便是结束在该函数中运行,b是进行打断点,可以是行号或函数名,c是运行到下一个断点,大致的调试过程表示这样。

要调试user中的代码,则需要进行新的步骤,要定位到运行的文件,查看user文件夹中源文件,可以看见所有的文件都有一个main函数,需要进行file user/_ls,这里以ls函数为例,xv6中将所有的shell命令都编译为前带下划线的二进制文件,甚于步骤与上述的一致,详细见下图序号。

其他可能出现的问题

  • python 环境问题,因为是Python3 需要添加软链接ln -s /usr/bin/python3 /usr/bin/python

  • trap 课程中stepi不能用,需要对.gdbinit.tmpl-riscv 增加 set riscv use-compressed-breakpoints yes,然后p/x $stvec会显示一个地址,在该地址处打断点 接着进行stepi即可

  • 使用的bash shell配置

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'

alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
#fi

# git branch
function git_branch {
branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
if [ "${branch}" != "" ];then
if [ "${branch}" = "(no branch)" ];then
branch="(`git rev-parse --short HEAD`...)"
fi
echo " ($branch)"
fi
}

# 终端显示路径
export PS1='\u@\[\033[01;36m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\]\$ '

相关资料