FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

Ubuntu 22.04 LTSでのRISC-Vツールチェイン試行

実際にはM1 Mac上で構築したLima上のUbuntu 22.04 LTS上でのビルドの試行だけれども。QEMUでのLinuxブート環境が構築したくて、RISC-V Getting Started Guideをそのまま試してみることにした。

risc-v-getting-started-guide.readthedocs.io

RISC-Vツールチェインの準備

ベアメタルクロスコンパイラ

git clone https://github.com/riscv-collab/riscv-gnu-toolchain.git -b 2022.05.15
cd riscv-gnu-toolchain
mkdir build
./configure --prefix=${RISCV}
make -j$(nproc)
make install
$ ~/riscv64/bin/riscv64-unknown-elf-gcc --version
riscv64-unknown-elf-gcc () 11.1.0
Copyright (C) 2021 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.

Linux用のコンパイラ

M1 Macネイティブでは全然うまく行かなかったのが、あっという間にうまく行った。素晴らしい。

git clone https://github.com/riscv-collab/riscv-gnu-toolchain.git -b 2022.05.15
cd riscv-gnu-toolchain
mkdir build
./configure --prefix=${RISCV}
make linux -j$(nproc)
make install
$ ~/riscv64-linux/bin/riscv64-unknown-linux-gnu-gcc --version
riscv64-unknown-linux-gnu-gcc (g5964b5cd727) 11.1.0
Copyright (C) 2021 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.

LinuxカーネルQEMUのインストール

次に、Linuxカーネルのビルド試行がしたいので、QEMUのインストールと、Linuxカーネルをビルドする。

risc-v-getting-started-guide.readthedocs.io

QEMUのインストール

QEMUのインストールは、Gitリポジトリからはできなくなっている?v7.0.0を試行したのだが、結局公式ページからダウンロードしてビルドした。

wget https://download.qemu.org/qemu-7.0.0.tar.xz
tar xvJf qemu-7.0.0.tar.xz
cd qemu-7.0.0
./configure --target-list=riscv64-softmmu
make -j$(nproc)
sudo make install
$ qemu-system-riscv64 --version
QEMU emulator version 7.0.0
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

Linuxカーネルのビルド

git clone https://github.com/torvalds/linux
cd linux
git checkout v5.4.0
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j $(nproc)

BusyBoxのビルド

git clone https://git.busybox.net/busybox
cd busybox
CROSS_COMPILE=riscv64-unknown-linux-gnu- make defconfig
CROSS_COMPILE=riscv64-unknown-linux-gnu- make -j $(nproc)

QEMUの立ち上げ

マニュアルに従って以下のように指定した。

sudo qemu-system-riscv64 -nographic -machine virt      -kernel ../linux/arch/riscv/boot/Image -append "root=/dev/vda ro console=ttyS0"      -drive file=busybox,format=raw,id=hd0      -device virtio-blk-device,drive=hd0
OpenSBI v1.0                                                                                                      
   ____                    _____ ____ _____                                                                       
  / __ \                  / ____|  _ \_   _|                                                                      
 | |  | |_ __   ___ _ __ | (___ | |_) || |                                                                        
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |                                                                        
 | |__| | |_) |  __/ | | |____) | |_) || |_                                                                       
  \____/| .__/ \___|_| |_|_____/|____/_____|                                                                      
        | |                                                                                                       
        |_|                                                                                                       
                                                                                                                  
Platform Name             : riscv-virtio,qemu                                                                     
Platform Features         : medeleg                                                                               
Platform HART Count       : 1                                                                                     
Platform IPI Device       : aclint-mswi                                                                           
Platform Timer Device     : aclint-mtimer @ 10000000Hz                                                            
Platform Console Device   : uart8250                                                                              
Platform HSM Device       : ---                                                                                   
Platform Reboot Device    : sifive_test                                                                           
Platform Shutdown Device  : sifive_test                                                                           
Firmware Base             : 0x80000000                                                                            
Firmware Size             : 252 KB                                                                                
Runtime SBI Version       : 0.3                                                                                   
                                                                                                                  
Domain0 Name              : root                                                                                  
Domain0 Boot HART         : 0                                                                                     
Domain0 HARTs             : 0*                                                                                    
Domain0 Region00          : 0x0000000002000000-0x000000000200ffff (I)                            
Domain0 Region01          : 0x0000000080000000-0x000000008003ffff ()                      
Domain0 Region02          : 0x0000000000000000-0xffffffffffffffff (R,W,X)                        
Domain0 Next Address      : 0x0000000080200000                                                                    
Domain0 Next Arg1         : 0x0000000087000000                                                                    
Domain0 Next Mode         : S-mode                                                                                
Domain0 SysReset          : yes                                                                                   
                                                                                                                  
Boot HART ID              : 0                                                                                     
Boot HART Domain          : root                                                                                  
Boot HART ISA             : rv64imafdcsuh                                                                         
Boot HART Features        : scounteren,mcounteren,time                                                            
Boot HART PMP Count       : 16
...
[    0.363287] [<ffffffff80004a28>] dump_backtrace+0x1c/0x24
[    0.363425] [<ffffffff80640dda>] show_stack+0x2c/0x38
[    0.363498] [<ffffffff80645df8>] dump_stack_lvl+0x40/0x58
[    0.363618] [<ffffffff80645e24>] dump_stack+0x14/0x1c
[    0.363688] [<ffffffff806410e6>] panic+0x106/0x2b0
[    0.363838] [<ffffffff80801462>] mount_block_root+0x202/0x21e
[    0.363991] [<ffffffff80801666>] mount_root+0x1e8/0x218
[    0.364071] [<ffffffff808017d8>] prepare_namespace+0x142/0x184
[    0.364284] [<ffffffff80800ffc>] kernel_init_freeable+0x1ee/0x20a
[    0.364366] [<ffffffff8064a20c>] kernel_init+0x1e/0x10a
[    0.364431] [<ffffffff800030b6>] ret_from_exception+0x0/0xc
[    0.364899] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(254,0) ]---

なんかがおかしいなあ。BusyBoxの作成に失敗したのか?