[RISCV]1, 用QEMU模拟器跑一个RISC-V上的Linux

Origin site:https://www.cnx-software.com/2018/03/16/how-to-run-linux-on-risc-v-with-qemu-emulator/

[RISCV]1, 用QEMU模拟器跑一个RISC-V上的Linux
[RISCV]2, qemu引导riscv linux–摆脱busybear
[RISCV]3, 用u-boot来引导riscv-linux kernel


由于不断增长的RISC-V硬件和软件生态系统,并且随着最近发布的HiFive Unleashed,我们甚至有一块能够运行Linux的主板,RISC-V开源体系结构变得越来越有趣。 唯一的问题:它的价格太贵了 —— 999美元。

但幸运的是,仅使用您当前的PC,就可以在RISC-V上使用Linux进行实验而无需额外的硬件。 Imperas提供了可在Windows和Linux上运行的商业解决方案,该解决方案依赖于busybear-linux RISC-V Linux根文件系统,该文件系统由busybox和dropbear SSH服务器组成。 rootfs也可以与QEMU一起使用,因此我在Ubuntu 16.04中进行了尝试。
在这里插入图片描述
按照Github上的步骤,编译过程变得很容易,我的计算机由AMD FX8350处理器和16GB RAM供电,整个过程花费了大约2个小时,因此最好使用最快的计算机。 它还需要约26 GB的存储在您的构建计算机上。

首先,我们创建一个工作目录,并检索RISC-V工具链:

mkdir riscv-linux
cd riscv-linux/
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain

现在,在安装一些依赖项之后,我们现在可以构建RISC-V newlib和Linux工具链:
(建议有条件的可以在服务器上编,然后拿到本机来用,或者直接下载二进制文件用)

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 -j $(nproc)
make linux -j $(nproc)
export PATH=$PATH:/opt/riscv/bin
export RISCV=/opt/risc

我们还需要为RISC-V构建qemu:

cd ..
git clone https://github.com/riscv/riscv-qemu.git
cd riscv-qemu
./configure --target-list=riscv64-softmmu,riscv32-softmmu
make -j $(nproc)
sudo make install

接下来的三个步骤将构建rootfsLinux 4.14内核引导加载程序,但是它们是可选的,因为您可以下载二进制版本

  • Build busybear-linux
cd ..
git clone https://github.com/michaeljclark/busybear-linux.git
cd busybear-linux
make -j $(nproc)
  • 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 -j $(nproc)
  • 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 -j $(nproc)

如果我们要改用二进制文件:

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

您通常想设置Linux桥接网络,我尝试了一下,但是失败了,时间用完了。 我在计算机的/ etc / network / interfaces中添加了以下几行:

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

并在当前目录中创建两个脚本

  • ifup
#!/bin/sh

brctl addif br0 $1
ifconfig $1 up
  • ifdown
#!/bin/sh

ifconfig $1 down
brctl delif br0 $1

但是我没有使其在合理的时间内工作。 在Arch Linux上详细研究QEMU网络页面应该会有所帮助。
[Update: See comments’ section for SSH access using the Fedora RISC-V image instead]

尽管如此,我仍然可以使用以下命令在带有QEMU的RISC-V上运行Linux:

 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
rrrrrrrrrrrrrrrrrrrrrr  rrrrrrrrrrrrrrrrrrrrrr

       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      [mem 0x0000000080200000-0x0000000087ffffff]
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000080200000-0x0000000087ffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x0000000087ffffff]
[    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 [ttyS0] disabled
[    0.400000] 10000000.uart: ttyS0 at MMIO 0x10000000 (irq = 1, base_baud = 230400) is a 16550A
[    0.430000] console [ttyS0] 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: 

我们可以使用密码“ busybear”以root用户身份登录, 不带引号。

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.6M  84% /
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 [function [arguments]...]
   or: busybox --list[-full]
   or: busybox --install [-s] [DIR]
   or: function [arguments]...

	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

键入halt以关闭Linux和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
[  410.790000] reboot: System halted
Power off
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山猫Show

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值