学习xv6:riscv64操作系统xv6实现

学习xv6:riscv64操作系统xv6实现

xv6是Dennis Ritchie和Ken Thompson的Unix的重新实现
版本6(v6)。xv6遵循v6的结构和风格,使用ANSI C为现代RISC-V多处理器实现的操作系统。

官方课程地址:6.S081 / Fall 2021

源代码:https://github.com/mit-pdos/xv6-riscv

这份源代码需要Ubuntu24.04版本或QEMU大于7.2版本。

下载源代码:

git clone https://github.com/mit-pdos/xv6-riscv.git

下载xv6配套书:

git clone https://github.com/mit-pdos/xv6-riscv-book.git

另一份公开的源代码:GitHub - mit-pdos/xv6-public: xv6 OS

这份代码可以在Ubuntu22.04下运行。 

下载源代码 

git clone https://github.com/mit-pdos/xv6-public

中文书:GitHub - deyuhua/xv6-book-chinese: MIT操作系统工程的教学操作系统Xv6的源码剖析中文翻译项目,使用ANSI标准C重新在riscv架构上实现Unix v6;

安装使用流程

安装qemu

sudo apt install qemu qemu-system

下载源代码

​git clone https://github.com/mit-pdos/xv6-public

编译并启动到xv6:

make qemu-nox

启动成功,显示: 

SeaBIOS (version 1.15.0-1)


iPXE (https://ipxe.org) 00:03.0 CA00 PCI2.10 PnP PMM+1FF8B4A0+1FECB4A0 CA00



Booting from Hard Disk..xv6...
cpu0: starting 0
sb: size 1000 nblocks 941 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58
init: starting sh

要退出qemu的xv6环境,可以CTRL+a 然后键入x,退出

实践

在Windows10里安装Ubuntu,在Microsoft Store商店里找到相应的Ubuntu版本安装即可。

在Windows 10里使用Ubuntu22.04 实践xv6-public

首先下载源代码

git clone https://github.com/mit-pdos/xv6-public

安装make 

sudo apt install make 

安装QEMU

sudo apt install qemu qemu-system

安装gcc

sudo apt install gcc

编译并进入xv6系统

 make qemu-nox

显示输出: 

SeaBIOS (version 1.15.0-1)


iPXE (https://ipxe.org) 00:03.0 CA00 PCI2.10 PnP PMM+1FF8B4A0+1FECB4A0 CA00



Booting from Hard Disk..xv6...
cpu0: starting 0
sb: size 1000 nblocks 941 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58
init: starting sh
$ ls
.              1 1 512
..             1 1 512
README         2 2 2286
cat            2 3 15468
echo           2 4 14348
forktest       2 5 8792

在Windows 10里使用Ubuntu22.04 实践xv6-riscv(失败)

安装源码

https://github.com/mit-pdos/xv6-riscv.git

安装gcc-riscv64-linux-gnu

sudo apt install gcc-riscv64-linux-gnu

编译 

make qemu

在Windows 10里使用Ubuntu24.04 实践xv6-riscv

下载源代码

git clone https://github.com/mit-pdos/xv6-riscv.git

 

 安装qemu

sudo apt install qemu qemu-system

安装make

sudo apt install make

 安装gcc-riscv64-linux-gnu

sudo apt install gcc-riscv64-linux-gnu

安装gcc

sudo apt install gcc

编译进入qemu的xv6系统

make qemu

终于进入了vx6系统:

xv6 kernel is booting

hart 1 starting
hart 2 starting
init: starting sh

上难度:添加一个自己的程序

进入xv6-riscv源代码的user目录,写一个demo.c文件:

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int main(){
        printf("hello world by printf");
}

 修改xv6-riscv根目录下的Makefile文件,在UPROGS部分加入 $U/_demo\ 这句:

UPROGS=\
        $U/_cat\
        $U/_echo\
        $U/_forktest\
        $U/_grep\
        $U/_init\
        $U/_kill\
        $U/_ln\
        $U/_ls\
        $U/_mkdir\
        $U/_rm\
        $U/_sh\
        $U/_stressfs\
        $U/_usertests\
        $U/_grind\
        $U/_wc\
        $U/_demo\
        $U/_zombie\

重新编译进入qemu的xv6系统

make qemu

进入vx6系统后,执行demo:

xv6 kernel is booting

hart 1 starting
hart 2 starting
init: starting sh
$ demo
hello world by printf$

 哈哈,咱们写的hello world 执行啦!

调试

在Ubuntu22.04 进行xv6-riscv 编译make qemu 卡住

 make qemu
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -global virtio-mmio.force-legacy=false -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

看看版本:

qemu-system-riscv64 -version
QEMU emulator version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6.24)
Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers

有人说建议升级Ubuntu到24:xv6 kernel doesn't boot: stuck at "make qemu". · Issue #262 · mit-pdos/xv6-riscv · GitHub

有人建议qemu-system-riscv64升级到大于7.2,

please run this command qemu-system-riscv64 -version first and make sure your version >= 7.2

而当前的是QEMU 6.2.0 monitor

换Ubuntu24,在Windows10里安装Ubuntu24.04

编译报错make: gcc: No such file or directory

proc.o kernel/swtch.o kernel/trampoline.o kernel/trap.o kernel/syscall.o kernel/sysproc.o kernel/bio.o kernel/fs.o kernel/log.o kernel/sleeplock.o kernel/file.o kernel/pipe.o kernel/exec.o kernel/sysfile.o kernel/kernelvec.o kernel/plic.o kernel/virtio_disk.o
riscv64-linux-gnu-ld: warning: kernel/kernel has a LOAD segment with RWX permissions
riscv64-linux-gnu-objdump -S kernel/kernel > kernel/kernel.asm
riscv64-linux-gnu-objdump -t kernel/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > kernel/kernel.sym
gcc -Werror -Wall -I. -o mkfs/mkfs mkfs/mkfs.c
make: gcc: No such file or directory
make: *** [Makefile:117: mkfs/mkfs] Error 127

sudo apt upgrade 试试

不行

sudo apt install gcc 试试

ok了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值