第505回の
ツールチェインとDebianのRISC-V対応状況
RISC-V対応のLinuxシステムを作るためには、
2020年1月時点で、
- binutils:2.
28以上 - GCC:7.
0以上 - glibc:2.
27以上 - Linux:4.
19以上 - QEMU:2.
12以上 - dpkg:1.
19. 0.5以上 - U-Boot:2018.
11以上
そしてこれらはすべて、
もちろん登場したばかりのアーキテクチャーであるため、
Debianでは公式でサポートしているCPUアーキテクチャー
RISC-V向けのアーキテクチャーとしては
残念ながらUbuntuはRISC-Vをサポートしていません。もし今後サポートすることになるとしても、
ただしUbuntu上でRISC-Vのバイナリをビルドしたり、
構築環境の準備
Ubuntu上でRISC-V環境を構築するためにはUbuntu 19.
今回はより新しい環境を求めて、
20.
まずはmultipassをインストールします。
$ sudo snap install multipass --classic
ちなみに第590回で紹介したあと、
次に20.
$ multipass launch -n riscv -d 40G -m 4G daily:20.04
ルートファイルシステムを作る都合上、
しばらく時間が必要ですが、
$ multipass shell riscv
ここから先の手順はすべて、
RISC-Vのルートファイルシステムの作成
まずはルートファイルシステム構築に必要なパッケージをインストールしておきましょう。
$ sudo apt install debootstrap \ debian-ports-archive-keyring \ qemu-user-static binfmt-support
debootstrapはDebianのベースシステムを構築するためのツールで
qemu-user-staticとbinfmt-supportは、
次にルートファイルシステムを作成します。
$ mkdir riscv && cd $_ $ sudo debootstrap --arch=riscv64 \ --keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg \ --include=debian-ports-archive-keyring \ unstable rootfs-riscv64 \ http://deb.debian.org/debian-ports
「--keyring
」--include
」
ちなみに今回はコード名にunstableを指定しています。Ports向けリポジトリで他に指定できるのはexperimentalとPorts固有のテスト環境であるunreleasedのみです。そのためタイミングが悪いと、
もしうまくルートファイルシステムを作れないようならsnapshot.
特定の日付のリポジトリを使うなら、
$ sudo debootstrap --arch=riscv64 \ --keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg \ --include=debian-ports-archive-keyring \ unstable rootfs-riscv64 \ https://snapshot.debian.org/archive/debian-ports/20191219T030209Z
ただしこの場合、/etc/
」
この時点でルートファイルシステムのサイズは300MiB弱程度のようです。
$ sudo du -hs rootfs-riscv64/ 295M rootfs-riscv64/
ルートファイルシステムを構築したら、
$ sudo chroot rootfs-riscv64 root@riscv:/#
まずはリポジトリデータのアップデートです。特にsnapshot.
root@riscv:/# echo "deb http://deb.debian.org/debian-ports unstable main" \ >> /etc/apt/sources.list root@riscv:/# apt update Get:1 http://deb.debian.org/debian-ports unstable InRelease [56.0 kB] Hit:2 https://snapshot.debian.org/archive/debian-ports/20191219T030209Z unstable InRelease Get:3 http://deb.debian.org/debian-ports unstable/main all Packages [8379 kB] Get:4 http://deb.debian.org/debian-ports unstable/main riscv64 Packages [20.4 MB] Fetched 28.8 MB in 17s (1658 kB/s) Reading package lists... Done Building dependency tree... Done 24 packages can be upgraded. Run 'apt list --upgradable' to see them. root@riscv:/# apt full-upgrade
ネットワーク設定が空なので、
root@riscv:/# cat /etc/network/interfaces # interfaces(5) file used by ifup(8) and ifdown(8) # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d ローカルループバック用の設定 root@riscv:/# cat >>/etc/network/interfaces.d/lo <<EOF auto lo iface lo inet loopback EOF eth0用の設定 root@riscv:/# cat >>/etc/network/interfaces.d/eth0 <<EOF auto eth0 iface eth0 inet dhcp EOF
次にアカウントを作成しておきましょう。
root@riscv:/# adduser shibata Adding user `shibata' ... Adding new group `shibata' (1000) ... Adding new user `shibata' (1000) with group `shibata' ... Creating home directory `/home/shibata' ... Copying files from `/etc/skel' ... New password: Retype new password: passwd: password updated successfully Changing the user information for shibata Enter the new value, or press ENTER for the default Full Name []: Mitsuya Shibata Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n]
rootアカウントのパスワードはロックされています。管理者権限に昇格できるよう、
root@riscv:/# apt install sudo root@riscv:/# usermod -aG sudo shibata root@riscv:/# id shibata uid=1000(shibata) gid=1000(shibata) groups=1000(shibata),27(sudo)
QEMU上だとhvc0はttyS0と同じデバイスになるため、
root@riscv:/# ln -sf /dev/null /etc/systemd/system/[email protected]
カーネルとブートローダーをインストールして、
root@riscv:/# apt install linux-image-riscv64 u-boot-menu
カーネルのインストール時に次のようなエラーが大量に表示されるかもしれません。
depmod: ERROR: ../libkmod/libkmod.c:515 lookup_builtin_file() could not open builtin file '/lib/modules/5.4.0-2-riscv64/modules.builtin.bin' depmod: ERROR: ../libkmod/libkmod.c:515 lookup_builtin_file() could not open builtin file '/var/tmp/mkinitramfs_SdQixj/lib/modules/5.4.0-2-ris cv64/modules.builtin.bin'
どうやらこれはinitramfs-toolsの不具合のようです。とりあえずパッケージに組み込まれているinitramfsをそのまま使うのであれば実害はないはずなので、
ブートローダーはU-Bootを使います。そこで次のように設定し、
root@riscv:/# cat >>/etc/default/u-boot <<EOF U_BOOT_PARAMETERS="rw noquiet root=/dev/vda1" U_BOOT_FDT_DIR="noexist" EOF root@riscv:/# u-boot-update P: Checking for EXTLINUX directory... found. /usr/bin/cat: /proc/cmdline: No such file or directory P: Writing config for vmlinux-5.4.0-2-riscv64... P: Updating /boot/extlinux/extlinux.conf...
「U_
」
ルートファイルシステム内部の設定が終わったので、
root@riscv:/# exit
最後に作成したルートファイルシステムをQEMUイメージに変換します。方法はいくつか存在しますが、
$ sudo apt install --no-install-recommends libguestfs-tools $ sudo virt-make-fs --partition --type=ext4 --size=10G rootfs-riscv64/ rootfs-riscv64.img $ sudo chown ${USER} rootfs-riscv64.img
これでイメージの作成は完了です。
QEMUからRISC-Vイメージを立ち上げる
作成したイメージをQEMUから立ち上げましょう。必要なのはRISC-V用のエミュレーターとファームウェア、
$ sudo apt install --no-install-recommends \ qemu-system-misc opensbi
amd64などのアーキテクチャーと異なり、
RISC-V向けのU-Boot本体は残念ながらUbuntuのリポジトリに存在しません。U-Boot 2019.
$ wget http://ftp.jp.debian.org/debian/pool/main/u/u-boot/u-boot-qemu_2020.01+dfsg-1_all.deb $ sudo apt install ./u-boot-qemu_2020.01+dfsg-1_all.deb
本パッケージはQEMU上で動かすバイナリの集合体のようなものであり、
さて、
$ qemu-system-riscv64 -nographic -machine virt -m 2G \ -kernel /usr/lib/riscv64-linux-gnu/opensbi/qemu/virt/fw_jump.elf \ -device loader,file=/usr/lib/u-boot/qemu-riscv64_smode/u-boot.bin,addr=0x80200000 \ -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-device,rng=rng0 \ -append "console=ttyS0 rw root=/dev/vda1" \ -device virtio-blk-device,drive=hd0 -drive file=rootfs-riscv64.img,format=raw,id=hd0 \ -device virtio-net-device,netdev=usernet -netdev user,id=usernet,hostfwd=tcp::22222-:22
まずはU-Bootが立ち上がります。
OpenSBI v0.5 (Oct 12 2019 06:02:51) ____ _____ ____ _____ / __ \ / ____| _ \_ _| | | | |_ __ ___ _ __ | (___ | |_) || | | | | | '_ \ / _ \ '_ \ \___ \| _ < | | | |__| | |_) | __/ | | |____) | |_) || |_ \____/| .__/ \___|_| |_|_____/|____/_____| | | |_| Platform Name : QEMU Virt Machine Platform HART Features : RV64ACDFIMSU Platform Max HARTs : 8 Current Hart : 0 Firmware Base : 0x80000000 Firmware Size : 116 KB Runtime SBI Version : 0.2 PMP0: 0x0000000080000000-0x000000008001ffff (A) PMP1: 0x0000000000000000-0xffffffffffffffff (A,R,W,X) U-Boot 2020.01+dfsg-1 (Jan 08 2020 - 08:19:44 +0000) CPU: rv64imafdcsu Model: riscv-virtio,qemu DRAM: 2 GiB In: uart@10000000 Out: uart@10000000 Err: uart@10000000 Net: Warning: virtio-net#2 using MAC address from ROM eth0: virtio-net#2 Hit any key to stop autoboot: 0 Device 0: QEMU VirtIO Block Device Type: Hard Disk Capacity: 10240.0 MB = 10.0 GB (20971520 x 512) ... is now current device Scanning virtio 0:1... Found /boot/extlinux/extlinux.conf Retrieving file: /boot/extlinux/extlinux.conf 611 bytes read in 1 ms (596.7 KiB/s) U-Boot menu 1: Debian GNU/Linux kernel 5.4.0-2-riscv64 2: Debian GNU/Linux kernel 5.4.0-2-riscv64 (rescue target) Enter choice: 1
「U-Boot menu」
1: Debian GNU/Linux kernel 5.4.0-2-riscv64 Retrieving file: /boot/initrd.img-5.4.0-2-riscv64 49472068 bytes read in 47 ms (1003.8 MiB/s) Retrieving file: /boot/vmlinux-5.4.0-2-riscv64 9381656 bytes read in 13 ms (688.2 MiB/s) append: rw noquiet root=/dev/vda1 ## Flattened Device Tree blob at ff7410c0 Booting using the fdt blob at 0xff7410c0 Using Device Tree in place at 00000000ff7410c0, end 00000000ff744d99 Starting kernel ... [ 0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000 [ 0.000000] Linux version 5.4.0-2-riscv64 ([email protected]) (gcc version 9.2.1 20200104 (Debian 9.2.1-22)) #1 SMP Debian 5.4 .8-1 (2020-01-05) [ 0.000000] Initial ramdisk at: 0x(____ptrval____) (49472068 bytes) [ 0.000000] Zone ranges: [ 0.000000] DMA32 [mem 0x0000000080200000-0x00000000ffffffff] [ 0.000000] Normal empty [ 0.000000] Movable zone start for each node [ 0.000000] Early memory node ranges [ 0.000000] node 0: [mem 0x0000000080200000-0x00000000ffffffff] [ 0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x00000000ffffffff] [ 0.000000] software IO TLB: mapped [mem 0xf9741000-0xfd741000] (64MB) [ 0.000000] elf_hwcap is 0x112d [ 0.000000] percpu: Embedded 26 pages/cpu s68440 r8192 d29864 u106496 [ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 515592 [ 0.000000] Kernel command line: rw noquiet root=/dev/vda1
カーネルとinitramfsがロードされて起動を開始します。
Debian GNU/Linux bullseye/sid riscv ttyS0 riscv login: shibata Password: Linux riscv 5.4.0-2-riscv64 #1 SMP Debian 5.4.8-1 (2020-01-05) riscv64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. shibata@riscv:~$
systemdも起動したらログインバナーが表示されるので、
/proc/
を確認するとRISC-VのISAモードが表示されていることがわかります。
shibata@riscv:~$ cat /proc/cpuinfo processor : 0 hart : 0 isa : rv64imafdcu mmu : sv48
もちろん、
shibata@riscv:~$ file /usr/bin/busybox /usr/bin/busybox: ELF 64-bit LSB shared object, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, for GNU/Linux 4.15.0, BuildID[sha1]=5cbc897e13ba8b5015132999916418f8423d5728, stripped
あとは
ちなみに動いているのはQEMUの上なので、
QEMU 4.0.0 monitor - type 'help' for more information (qemu) info registers pc ffffffe000085596 mhartid 0000000000000000 mstatus 0000000000000000 mip 0000000000000000 mie 00000000000002aa mideleg 0000000000000222 medeleg 000000000000b109 mtvec 0000000080000488 mepc ffffffe00046d810 mcause 0000000000000009 zero 0000000000000000 ra ffffffe0000bdf94 sp ffffffe0007d3f30 gp ffffffe00087f080 tp ffffffe0007da300 t0 0000000000000000 t1 0000000000006000 t2 0000000000003933 s0 ffffffe0007d3f40 s1 ffffffe00087f7a0 a0 ffffffe0000bdf94 a1 0000000000000001 a2 0000000000000000 a3 ffffffe0008b6b58 a4 000000000000083e a5 0000000000000000 a6 000000005bd58b6b a7 0000000000000000 s2 0000000000000000 s3 ffffffe00087f96c s4 0000000000000001 s5 ffffffe00087f618 s6 ffffffe000036008 s7 0000000000000000 s8 00000000fff63cc6 s9 0000000000000003 s10 0000000000000000 s11 00000000ff770b10 t3 0000000000000000 t4 0000000000000001 t5 ffffffe000705838 t6 0000000000000000 ft0 0000000000000000 ft1 0000000000000000 ft2 0000000000000000 ft3 0000000000000000 ft4 0000000000000000 ft5 0000000000000000 ft6 0000000000000000 ft7 0000000000000000 fs0 0000000000000000 fs1 0000000000000000 fa0 0000000000000000 fa1 0000000000000000 fa2 0000000000000000 fa3 0000000000000000 fa4 0000000000000000 fa5 0000000000000000 fa6 0000000000000000 fa7 0000000000000000 fs2 0000000000000000 fs3 0000000000000000 fs4 0000000000000000 fs5 0000000000000000 fs6 0000000000000000 fs7 0000000000000000 fs8 0000000000000000 fs9 0000000000000000 fs10 0000000000000000 fs11 0000000000000000 ft8 0000000000000000 ft9 0000000000000000 ft10 0000000000000000 ft11 0000000000000000