Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building steps on WSL2/Win11 (Ubuntu 22.04) #47

Open
inkydragon opened this issue Jan 11, 2025 · 0 comments
Open

Building steps on WSL2/Win11 (Ubuntu 22.04) #47

inkydragon opened this issue Jan 11, 2025 · 0 comments

Comments

@inkydragon
Copy link

inkydragon commented Jan 11, 2025

经过一些尝试可以在 qemu 上运行 hello。
记录一下对应的修改。
待整理并优化、去除不必要的步骤后,可以添加到现有的文档中。

安装 riscv 工具链

参考文档:编译工具链 toolchains - XiangShan 官方文档

  • 按照文档中的说明安装工具链,这里选择 riscv32-elf-ubuntu-22.04-gcc-nightly-2024.12.16-nightly.tar.xz
    • 这里选择最新的版本 2024.12.16
    • 需要 riscv64 或其他 ubuntu 版本注意自行选择

下载并安装

## 新建工具链所在文件夹
mkdir ~/riscv/
cd ~/riscv/

## 下载工具链
wget https://mirror.iscas.ac.cn/riscv-toolchains/release/riscv-collab/riscv-gnu-toolchain/Nightly%3A%20December%2016%2C%202024/riscv32-elf-ubuntu-22.04-gcc-nightly-2024.12.16-nightly.tar.xz
# wget https://mirror.iscas.ac.cn/riscv-toolchains/release/riscv-collab/riscv-gnu-toolchain/Nightly%3A%20December%2016%2C%202024/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.12.16-nightly.tar.xz

## 解压
tar xvf riscv32-elf-ubuntu-22.04-gcc-nightly-2024.12.16-nightly.tar.xz -C riscv/
# tar xvf riscv64-elf-ubuntu-22.04-gcc-nightly-2024.12.16-nightly.tar.xz -C riscv/

## 重命名工具链名称。可以实现 rv32/64 共存
mv riscv riscv32
# mv riscv riscv64

验证工具链安装

~$ cd ~/riscv/riscv32/
~/riscv/riscv32$ ls
bin  include  lib  libexec  riscv32-unknown-elf  share
~/riscv/riscv32$ pwd
/home/cyhan/riscv/riscv32
# 记下 pwd 的输出,稍后使用

添加到环境变量中

修改 ~/.bashrc
如使用 vim: vim ~/.bashrc
在末尾或合适的地方添加以下行:

# riscv
export PATH=:/home/cyhan/riscv/riscv32/bin:${PATH}
# export PATH=:/home/cyhan/riscv/riscv64/bin:${PATH}

:wq 保存并退出;
source ~/.bashrc 加载修改

验证工具链安装

~$ riscv32-unknown-elf-  # 按 TAB 键补全可选项
riscv32-unknown-elf-addr2line      riscv32-unknown-elf-g++            riscv32-unknown-elf-gcov-dump      riscv32-unknown-elf-lto-dump       riscv32-unknown-elf-size
riscv32-unknown-elf-ar             riscv32-unknown-elf-gcc            riscv32-unknown-elf-gcov-tool      riscv32-unknown-elf-nm             riscv32-unknown-elf-strings
riscv32-unknown-elf-as             riscv32-unknown-elf-gcc-14.2.0     riscv32-unknown-elf-gdb            riscv32-unknown-elf-objcopy        riscv32-unknown-elf-strip
riscv32-unknown-elf-c++            riscv32-unknown-elf-gcc-ar         riscv32-unknown-elf-gdb-add-index  riscv32-unknown-elf-objdump
riscv32-unknown-elf-c++filt        riscv32-unknown-elf-gcc-nm         riscv32-unknown-elf-gprof          riscv32-unknown-elf-ranlib
riscv32-unknown-elf-cpp            riscv32-unknown-elf-gcc-ranlib     riscv32-unknown-elf-ld             riscv32-unknown-elf-readelf
riscv32-unknown-elf-elfedit        riscv32-unknown-elf-gcov           riscv32-unknown-elf-ld.bfd         riscv32-unknown-elf-run
~$ riscv32-unknown-elf-gcc --version
riscv32-unknown-elf-gcc () 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

安装 qemu

使用系统自带的 qemu (版本较老,但够用)

sudo apt update
sudo apt install qemu-system-riscv32

验证安装:

~$ qemu-system-riscv32 --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

编译项目

patchs

toolchains/riscv32-unknown-elf.lua

diff --git a/toolchains/riscv32-unknown-elf.lua b/toolchains/riscv32-unknown-elf.lua
index b5ef32a..3a00b1a 100644
--- a/toolchains/riscv32-unknown-elf.lua
+++ b/toolchains/riscv32-unknown-elf.lua
@@ -1,5 +1,5 @@
-PATH = "D:\\My\\plugin\\riscv32-gnu-toolchain-win"
-BINDIR =PATH.."\\bin"
+PATH = "/home/cyhan/riscv/riscv32"
+BINDIR =PATH.."/bin"
 PREFIX = "riscv32-unknown-elf-"
 
 TOOLCHAIN_NAME = "riscv32-unknown-elf"

xscript/qemu-board-config.lua

diff --git a/xscript/qemu-board-config.lua b/xscript/qemu-board-config.lua
index 326f3ea..4965f0e 100644
--- a/xscript/qemu-board-config.lua
+++ b/xscript/qemu-board-config.lua
@@ -74,7 +74,7 @@ local testcase_config = {
         crt = "riscv", 
         envs = {
             DEFINE = " ",
-            DEVICE = " -mcmodel=medany -mstrict-align -march=rv32imac -mabi=ilp32",
+            DEVICE = " -mcmodel=medany -mstrict-align -march=rv32imac -mabi=ilp32 -mfloat-abi=softfp",
             DEBUG  = " -gdwarf-2 "
         },
         flags = {

xscript/mlibc-config.lua

  • 这里如果指定 -mfloat-abi 会报错
diff --git a/xscript/mlibc-config.lua b/xscript/mlibc-config.lua
index 878d292..2a9616a 100644
--- a/xscript/mlibc-config.lua
+++ b/xscript/mlibc-config.lua
@@ -30,7 +30,7 @@ local mlibc_config = {
     ["riscv32"] = {
         target = "riscv32",
         toolchain = "riscv32-unknown-elf",
-        arch_flags = " -mcmodel=medany -march=rv32imac ",
+        arch_flags = " -mcmodel=medany -march=rv32imac -mabi=ilp32",
         define_flags = " "
     }
 }

编译

# 配置工具链
# 注意这里不能省略后面的 config 选项,不然默认使用 arm 工具链,然后提示找不到工具链
xmake f -p cross --toolchain=riscv32-unknown-elf --crt-arch=riscv32 --mlibc-arch=riscv32 --qemu-board=qemu-virt-riscv32
# 编译
xmake

xmake build mlibc
xmake build crt0

运行 hello

# 进入文件夹
cd helloworld/qemu/qemu-virt-riscv32

新建 qemu.sh (实际上是复制了 qemu.bat)

qemu-system-riscv32 -machine virt -nographic -m 256M \
    -kernel qemu-virt-riscv32.elf -device virtio-serial-device

直接运行提示缺少固件(有点怀疑是 qemu 的问题)

$ ./qemu.sh
qemu-system-riscv32: Unable to load the RISC-V firmware "opensbi-riscv32-generic-fw_dynamic.bin"

下载固件

参考:Getting Started | OS in 1,000 Lines

$ curl -LO https://github.com/qemu/qemu/raw/v6.2.0/pc-bios/opensbi-riscv32-generic-fw_dynamic.bin

再次运行成功。这里最后是个死循环,会一直卡着(按 Ctrl + a;然后按 x 键,退出 qemu)。

$ ./qemu.sh

OpenSBI v0.9
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : timer,mfdeleg
Platform HART Count       : 1
Firmware Base             : 0x80000000
Firmware Size             : 100 KB
Runtime SBI Version       : 0.2

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*
Domain0 Region00          : 0x80000000-0x8001ffff ()
Domain0 Region01          : 0x00000000-0xffffffff (R,W,X)
Domain0 Next Address      : 0x80200000
Domain0 Next Arg1         : 0x8f000000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART ISA             : rv32imafdcsu
Boot HART Features        : scounteren,mcounteren,time
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 32
Boot HART MHPM Count      : 0
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x00000222
Boot HART MEDELEG         : 0x0000b109
Uart Test
Hello mlibc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant