新ちゃん 发表于 2020-8-17 15:23:00

[RISC-V]1, How to Run Linux on RISC-V with QEMU Emulator

本帖最后由 新ちゃん 于 2020-8-21 03:51 编辑

Origin site:https://www.cnx-software.com/2018/03/16/how-to-run-linux-on-risc-v-with-qemu-emulator/
RISC-V open source architecture is starting to become more and more interesting thanks the growing RISC-V hardware & software ecosystem, and with the recent release of HiFive Unleashed, we even have a board capable of running Linux. The only problem: it costs $999.
But luckily, it’s possible to experiment with Linux on RISC-V without extra hardware, just using your current PC. Imperas offers a commercial solution working on both Windows and Linux that relies on busybear-linux RISC-V Linux root filesystem comprised of busybox and dropbear SSH server. The rootfs also works with QEMU, so I tried it in Ubuntu 16.04.


The instructions on Github are quite easy to follow. My computer is powered by an AMD FX8350 processor coupled with 16GB RAM, and the whole process took around 2 hours, so better use the fastest computer possible. It also requires around 26 GB of storage on your build machine.
First, let’s create a working directory, and retrieve the RISC-V toolchain:mkdir riscv-linux
cd riscv-linux/
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
Now we can now build the RISC-V newlib & Linux toolchains) after installing some dependencies:
sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv --enable-multilib
make newlib -j8
make linux -j8
export PATH=$PATH:/opt/riscv/bin
export RISCV=/opt/risc
We’ll also need to build qemu for RISC-V
cd ..
git clone https://github.com/riscv/riscv-qemu.git
cd riscv-qemu
./configure --target-list=riscv64-softmmu,riscv32-softmmu
make -j8
sudo make install
The next three steps will build the rootfs, Linux 4.14 kernel, and the bootloader, but there are optional since you could just as well as download the binary releases.

[*]Build busybear-linux
cd ..
git clone https://github.com/michaeljclark/busybear-linux.git
cd busybear-linux
make -j8

[*]Build RISC-V Linux
cd..
git clone https://github.com/riscv/riscv-linux.git
cd riscv-linux
git checkout riscv-linux-4.14
cp ../busybear-linux/conf/linux.config .config
make ARCH=riscv olddefconfig
make ARCH=riscv vmlinux -j8

[*]Build BBL (Berkeley Boot Loader):

cd..
git clone https://github.com/riscv/riscv-pk.git
cd riscv-pk
mkdir build
cd build
../configure --enable-logo --host=riscv64-unknown-elf --with-payload=../../riscv-linux/vmlinux
make -j8

If we’re going to use the release files instead:
mkdir release
cd release
wget https://github.com/michaeljclark/busybear-linux/releases/download/v1.0/bbl.bz2
wget https://github.com/michaeljclark/busybear-linux/releases/download/v1.0/busybear.bin.bz2
bzip2 -d *.bz2
You’d normally want to setup Linux bridged networking, and I tried, but failed and ran out of time. I added the following lines to /etc/network/interfaces in my computer:
iface br0 inet static
bridge_ports eth0
address 192.168.100.1
netmask 255.255.255.0
network 192.168.100.0
broadcast 192.168.100.255
and create two scripts in the current directory
[*]ifup
#!/bin/sh

brctl addif br0 $1
ifconfig $1 up

[*]ifdown
#!/bin/sh

ifconfig $1 down
brctl delif br0 $1
But I did not get it to work in a reasonable time. Studying QEMU Networking page on Arch Linux in details should help.


Nevertheless I could still run Linux on RISC-V with QEMU using the following command: sudo qemu-system-riscv64 -nographic -machine virt   -kernel bbl -append "root=/dev/vda ro console=ttyS0"   \
-drive file=busybear.bin,format=raw,id=hd0   -device virtio-blk-device,drive=hd0   \
-netdev type=tap,script=./ifup,downscript=./ifdown,id=net0   -device virtio-net-device,netdev=net0
The boot takes just over one second to our minimal rootfs. Here’s the boot log:            vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
                  vvvvvvvvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrr       vvvvvvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrrrrr      vvvvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrrrrrrr    vvvvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrrrrrrr    vvvvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrrrrrrr    vvvvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrrrrr      vvvvvvvvvvvvvvvvvvvvvv
rrrrrrrrrrrrr       vvvvvvvvvvvvvvvvvvvvvv   
rr                vvvvvvvvvvvvvvvvvvvvvv      
rr            vvvvvvvvvvvvvvvvvvvvvvvv      rr
rrrr      vvvvvvvvvvvvvvvvvvvvvvvvvv      rrrr
rrrrrr      vvvvvvvvvvvvvvvvvvvvvv      rrrrrr
rrrrrrrr      vvvvvvvvvvvvvvvvvv      rrrrrrrr
rrrrrrrrrr      vvvvvvvvvvvvvv      rrrrrrrrrr
rrrrrrrrrrrr      vvvvvvvvvv      rrrrrrrrrrrr
rrrrrrrrrrrrrr      vvvvvv      rrrrrrrrrrrrrr
rrrrrrrrrrrrrrrr      vv      rrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrr          rrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrr      rrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

       INSTRUCTION SETS WANT TO BE FREE
[    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
[    0.000000] Linux version 4.14.0-00032-gd10799b22913-dirty (mclark@minty) (gcc version 7.1.1 20170509 (GCC)) #79 Sun Dec 17 10:41:31 NZDT 2017
[    0.000000] Zone ranges:
[    0.000000]   DMA      
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0:
[    0.000000] Initmem setup node 0
[    0.000000] elf_hwcap is 0x112d
[    0.000000] Built 1 zonelists, mobility grouping on.Total pages: 31815
[    0.000000] Kernel command line: root=/dev/vda ro console=ttyS0
[    0.000000] PID hash table entries: 512 (order: 0, 4096 bytes)
[    0.000000] Dentry cache hash table entries: 16384 (order: 5, 131072 bytes)
[    0.000000] Inode-cache hash table entries: 8192 (order: 4, 65536 bytes)
[    0.000000] Sorting __ex_table...
[    0.000000] Memory: 122496K/129024K available (2658K kernel code, 216K rwdata, 652K rodata, 96K init, 782K bss, 6528K reserved, 0K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] NR_IRQS: 0, nr_irqs: 0, preallocated irqs: 0
[    0.000000] riscv,cpu_intc,0: 64 local interrupts mapped
[    0.000000] riscv,plic0,c000000: mapped 10 interrupts to 1/2 handlers
[    0.000000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns
[    0.000000] Console: colour dummy device 80x25
[    0.000000] Calibrating delay loop (skipped), value calculated using timer frequency.. 20.00 BogoMIPS (lpj=100000)
[    0.000000] pid_max: default: 32768 minimum: 301
[    0.000000] Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
[    0.000000] Mountpoint-cache hash table entries: 512 (order: 0, 4096 bytes)
[    0.090000] devtmpfs: initialized
[    0.110000] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[    0.110000] futex hash table entries: 256 (order: 0, 6144 bytes)
[    0.110000] random: get_random_u32 called from bucket_table_alloc+0x80/0x1f6 with crng_init=0
[    0.110000] NET: Registered protocol family 16
[    0.140000] vgaarb: loaded
[    0.150000] clocksource: Switched to clocksource riscv_clocksource
[    0.170000] NET: Registered protocol family 2
[    0.170000] TCP established hash table entries: 1024 (order: 1, 8192 bytes)
[    0.170000] TCP bind hash table entries: 1024 (order: 1, 8192 bytes)
[    0.170000] TCP: Hash tables configured (established 1024 bind 1024)
[    0.180000] UDP hash table entries: 256 (order: 1, 8192 bytes)
[    0.180000] UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
[    0.180000] NET: Registered protocol family 1
[    0.190000] workingset: timestamp_bits=62 max_order=15 bucket_order=0
[    0.220000] random: fast init done
[    0.270000] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 254)
[    0.270000] io scheduler noop registered
[    0.270000] io scheduler deadline registered
[    0.270000] io scheduler cfq registered (default)
[    0.270000] io scheduler mq-deadline registered
[    0.270000] io scheduler kyber registered
[    0.380000] Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled
[    0.390000] console disabled
[    0.400000] 10000000.uart: ttyS0 at MMIO 0x10000000 (irq = 1, base_baud = 230400) is a 16550A
[    0.430000] console enabled
[    0.480000] NET: Registered protocol family 10
[    0.490000] Segment Routing with IPv6
[    0.490000] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
[    0.520000] EXT4-fs (vda): couldn't mount as ext3 due to feature incompatibilities
[    0.520000] EXT4-fs (vda): INFO: recovery required on readonly filesystem
[    0.530000] EXT4-fs (vda): write access will be enabled during recovery
[    0.610000] EXT4-fs (vda): recovery complete
[    0.620000] EXT4-fs (vda): mounted filesystem with ordered data mode. Opts: (null)
[    0.630000] VFS: Mounted root (ext4 filesystem) readonly on device 254:0.
[    0.630000] devtmpfs: mounted
[    0.640000] Freeing unused kernel memory: 96K
[    0.640000] This architecture does not have kernel memory protection.
[    0.970000] EXT4-fs (vda): re-mounted. Opts: data=ordered

ucbvax login:
We can login with root using password “busybear” without quote.
ucbvax login: root
Password:
    ____                   ____                     __    _               
   / __ )_____________/ __ )_______ ______   / /   (_)__________
/ __/ / / / ___/ / / / __/ _ \/ __ `/ ___// /   / / __ \/ / / / |/_/
/ /_/ / /_/ (__) /_/ / /_/ /__/ /_/ / /   / /___/ / / / / /_/ /><
/_____/\__,_/____/\__, /_____/\___/\__,_/_/   /_____/_/_/ /_/\__,_/_/|_|
               /____/                                                   
root@ucbvax:~# uname -a
Linux ucbvax 4.14.0-00032-gd10799b22913-dirty #79 Sun Dec 17 10:41:31 NZDT 2017 riscv64 GNU/Linux
root@ucbvax:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                58.0M   44.9M      8.6M84% /
devtmpfs               59.8M         0   59.8M   0% /dev
none                     59.9M         0   59.9M   0% /tmp
none                     59.9M         0   59.9M   0% /var/tmp
root@ucbvax:~# cat /proc/cpuinfo
hart      : 0
isa      : rv64imafdcsu
mmu      : sv48

root@ucbvax:~# busybox
BusyBox v1.27.2 (2017-12-15 12:14:50 NZDT) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2015.
Licensed under GPLv2. See source distribution for detailed
copyright notices.

Usage: busybox ...]
   or: busybox --list[-full]
   or: busybox --install [-s]
   or: function ...

      BusyBox is a multi-call binary that combines many common Unix
      utilities into a single executable.Most people will create a
      link to busybox for each function they wish to use and BusyBox
      will act like whatever it was invoked as.

Currently defined functions:
      [, [[, add-shell, addgroup, adduser, ar, arp, arping, ash, awk, base64,
      basename, blkid, blockdev, brctl, bunzip2, bzcat, bzip2, cal, cat,
      chattr, chgrp, chmod, chown, chpasswd, chroot, chrt, cksum, clear,
      comm, cp, cpio, crond, crontab, cut, date, dd, delgroup, deluser, df,
      diff, dirname, dmesg, dos2unix, dpkg, dpkg-deb, du, echo, ed, egrep,
      env, expand, expr, factor, fallocate, false, fdisk, fgrep, find,
      findfs, flock, fold, fsck, fsfreeze, fsync, fuser, getty, grep, groups,
      gunzip, gzip, halt, head, hostid, hostname, id, ifconfig, ifdown, ifup,
      inetd, init, install, iostat, ip, ipaddr, iplink, ipneigh, iproute,
      iprule, iptunnel, kill, killall, klogd, last, less, link, ln, logger,
      login, logname, logread, losetup, ls, lsattr, lsof, lspci, lsscsi,
      lsusb, lzcat, lzma, makedevs, man, md5sum, microcom, mkdir, mkdosfs,
      mke2fs, mkfifo, mkfs.ext2, mkfs.vfat, mknod, mkpasswd, mkswap, mktemp,
      more, mount, mv, nameif, nc, netstat, nice, nl, nohup, nproc, nslookup,
      ntpd, od, partprobe, passwd, paste, patch, pgrep, pidof, ping, ping6,
      pipe_progress, pivot_root, pkill, pmap, poweroff, printenv, printf, ps,
      pstree, pwd, pwdx, rdev, readlink, realpath, reboot, remove-shell,
      renice, reset, rev, rm, rmdir, route, rpm, rpm2cpio, run-parts,
      runlevel, script, sed, seq, setpriv, setserial, setsid, sh, sha1sum,
      sha256sum, sha3sum, sha512sum, shred, shuf, sleep, sort, split,
      ssl_client, start-stop-daemon, stat, strings, stty, su, sum, swapoff,
      swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset,
      tee, telnet, test, time, timeout, top, touch, tr, traceroute,
      traceroute6, true, truncate, tty, tunctl, tune2fs, udhcpc, uevent,
      umount, uname, uncompress, unexpand, uniq, unix2dos, unlink, unlzma,
      unshare, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vi, w,
      wall, watch, wc, wget, which, who, whoami, whois, xargs, xxd, xz,
      xzcat, yes, zcat
Type halt to turn off Linux and QEMU:
root@ucbvax:~# halt
root@ucbvax:~# umount: devtmpfs busy - remounted read-only
umount: can't remount /dev/root read-only
The system is going down NOW!
Sent SIGTERM to all processes
Sent SIGKILL to all processes
Requesting system halt
reboot: System halted
Power off本篇完,感谢关注:RISC-V单片机中文网
页: [1]
查看完整版本: [RISC-V]1, How to Run Linux on RISC-V with QEMU Emulator