stevessr / 在Windows上运行MIT xv6-riscv

Created Thu, 28 Aug 2025 18:52:56 +0800 Modified Wed, 13 May 2026 11:28:35 +0000

虽然mit官方的教程让你在linux下运行,但是我不喜欢WSL(还是太穷了,内存不够吃)

qemu

首先确保你安装了mysy2环境,
接着每日一滚pacman -Syyu

随后使用pacman -S mingw-w64-x86_64-qemu安装qemu
image
随后打开你的环境变量,在Path最后上添加一行,是qemu存放的位置
这里为
C:\msys64\mingw64\bin

riscV gcc windows

先安装一个make pacman -S make
然后追加一个普通gcc winget install BrechtSanders.WinLibs.POSIX.MSVCRT
下载预构建的gcc工具链(其他的
https://gnutoolchains.com/risc-v/
image
选择你心仪的版本,随后下载

打开
image
自行安装(默认会勾选自动配置环境变量)

启动xv6

克隆仓库
https://github.com/mit-pdos/xv6-riscv

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

此时不要急,可能会报错,我们将mkfs.c进行修改

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>

修改为

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif

fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);修改为fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);

bzero(*, sizeof(de));修改为memset(*, 0, sizeof(de));对,这个*代表任意长度字符串,让ai自己该也行

assert(index(shortname, '/') == 0);修改为assert(strchr(shortname, '/') == 0);

open(argv[i], 0))修改为open(argv[i], O_RDONLY|O_BINARY))

void
wsect(uint sec, void *buf)
{
  if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE)
    die("lseek");
  if(write(fsfd, buf, BSIZE) != BSIZE)
    die("write");
}

修改为

void
wsect(uint sec, void *buf)
{
  off_t off = (off_t)sec * BSIZE;
  if(lseek(fsfd, off, 0) != off) {
    fprintf(stderr, "wsect: lseek failed sec=%u off=%lld errno=%d %s\n", sec, (long long)off, errno, strerror(errno));
    die("lseek");
  }
  ssize_t wr = write(fsfd, buf, BSIZE);
  if(wr != BSIZE) {
    fprintf(stderr, "wsect: write failed sec=%u off=%lld wrote=%zd errno=%d %s\n", sec, (long long)off, wr, errno, strerror(errno));
    die("write");
  }
}

void
rsect(uint sec, void *buf)
{
  if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE)
    die("lseek");
  if(read(fsfd, buf, BSIZE) != BSIZE)
    die("read");
}

修改为

void
rsect(uint sec, void *buf)
{
  off_t off = (off_t)sec * BSIZE;
  if(lseek(fsfd, off, 0) != off) {
    fprintf(stderr, "rsect: lseek failed sec=%u off=%lld errno=%d %s\n", sec, (long long)off, errno, strerror(errno));
    die("lseek");
  }
  ssize_t rc = read(fsfd, buf, BSIZE);
  if(rc != BSIZE) {
    fprintf(stderr, "rsect: read failed sec=%u off=%lld read=%zd errno=%d %s\n", sec, (long long)off, rc, errno, strerror(errno));
    die("read");
  }
}

bcopy(p, buf + off - (fbn * BSIZE), n1);更改为memmove(buf + off - (fbn * BSIZE), p, n1);

因为有些是BSD,在Windows上不兼容

然后make qemu
开始写作业吧烧酒
image