查看: 1322|回复: 0
收起左侧

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

[复制链接]

  离线 

  • TA的每日心情
    奋斗
    2021-1-15 13:53
  • 签到天数: 26 天

    [LV.4]

    发表于 2020-8-17 15:23:00 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 新ちゃん 于 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.


    国内芯片技术交流-[RISC-V]1, How to Run Linux on RISC-V with QEMU Emulatorrisc-v单片机中文社区(1)


    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:

    1. mkdir riscv-linux
    2. cd riscv-linux/
    3. 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:
    1. 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
    2. cd riscv-gnu-toolchain
    3. ./configure --prefix=/opt/riscv --enable-multilib
    4. make newlib -j8
    5. make linux -j8
    6. export PATH=$PATH:/opt/riscv/bin
    7. export RISCV=/opt/risc
    复制代码

    We’ll also need to build qemu for RISC-V
    1. cd ..
    2. git clone https://github.com/riscv/riscv-qemu.git
    3. cd riscv-qemu
    4. ./configure --target-list=riscv64-softmmu,riscv32-softmmu
    5. make -j8
    6. 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
    1. cd ..
    2. git clone https://github.com/michaeljclark/busybear-linux.git
    3. cd busybear-linux
    4. make -j8
    复制代码

    • Build RISC-V Linux
    1. cd..
    2. git clone https://github.com/riscv/riscv-linux.git
    3. cd riscv-linux
    4. git checkout riscv-linux-4.14
    5. cp ../busybear-linux/conf/linux.config .config
    6. make ARCH=riscv olddefconfig
    7. make ARCH=riscv vmlinux -j8
    复制代码

    • Build BBL (Berkeley Boot Loader):

    1. cd..
    2. git clone https://github.com/riscv/riscv-pk.git
    3. cd riscv-pk
    4. mkdir build
    5. cd build
    6. ../configure --enable-logo --host=riscv64-unknown-elf --with-payload=../../riscv-linux/vmlinux
    7. make -j8
    复制代码


    If we’re going to use the release files instead:
    1. mkdir release
    2. cd release
    3. wget https://github.com/michaeljclark/busybear-linux/releases/download/v1.0/bbl.bz2
    4. wget https://github.com/michaeljclark/busybear-linux/releases/download/v1.0/busybear.bin.bz2
    5. 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:
    1. iface br0 inet static
    2.   bridge_ports eth0
    3.   address 192.168.100.1
    4.   netmask 255.255.255.0
    5.   network 192.168.100.0
    6.   broadcast 192.168.100.255
    复制代码

    and create two scripts in the current directory

    • ifup
    1. #!/bin/sh

    2. brctl addif br0 $1
    3. ifconfig $1 up
    复制代码

    • ifdown
    1. #!/bin/sh

    2. ifconfig $1 down
    3. 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.

    [Update: See comments’ section for SSH access using the Fedora RISC-V image instead]


    Nevertheless I could still run Linux on RISC-V with QEMU using the following command:

    1. sudo qemu-system-riscv64 -nographic -machine virt   -kernel bbl -append "root=/dev/vda ro console=ttyS0"   \
    2. -drive file=busybear.bin,format=raw,id=hd0   -device virtio-blk-device,drive=hd0   \
    3. -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:

    1.               vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    2.                   vvvvvvvvvvvvvvvvvvvvvvvvvvvv
    3. rrrrrrrrrrrrr       vvvvvvvvvvvvvvvvvvvvvvvvvv
    4. rrrrrrrrrrrrrrrr      vvvvvvvvvvvvvvvvvvvvvvvv
    5. rrrrrrrrrrrrrrrrrr    vvvvvvvvvvvvvvvvvvvvvvvv
    6. rrrrrrrrrrrrrrrrrr    vvvvvvvvvvvvvvvvvvvvvvvv
    7. rrrrrrrrrrrrrrrrrr    vvvvvvvvvvvvvvvvvvvvvvvv
    8. rrrrrrrrrrrrrrrr      vvvvvvvvvvvvvvvvvvvvvv  
    9. rrrrrrrrrrrrr       vvvvvvvvvvvvvvvvvvvvvv   
    10. rr                vvvvvvvvvvvvvvvvvvvvvv      
    11. rr            vvvvvvvvvvvvvvvvvvvvvvvv      rr
    12. rrrr      vvvvvvvvvvvvvvvvvvvvvvvvvv      rrrr
    13. rrrrrr      vvvvvvvvvvvvvvvvvvvvvv      rrrrrr
    14. rrrrrrrr      vvvvvvvvvvvvvvvvvv      rrrrrrrr
    15. rrrrrrrrrr      vvvvvvvvvvvvvv      rrrrrrrrrr
    16. rrrrrrrrrrrr      vvvvvvvvvv      rrrrrrrrrrrr
    17. rrrrrrrrrrrrrr      vvvvvv      rrrrrrrrrrrrrr
    18. rrrrrrrrrrrrrrrr      vv      rrrrrrrrrrrrrrrr
    19. rrrrrrrrrrrrrrrrrr          rrrrrrrrrrrrrrrrrr
    20. rrrrrrrrrrrrrrrrrrrr      rrrrrrrrrrrrrrrrrrrr
    21. rrrrrrrrrrrrrrrrrrrrrr  rrrrrrrrrrrrrrrrrrrrrr

    22.        INSTRUCTION SETS WANT TO BE FREE
    23. [    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
    24. [    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
    25. [    0.000000] Zone ranges:
    26. [    0.000000]   DMA      [mem 0x0000000080200000-0x0000000087ffffff]
    27. [    0.000000]   Normal   empty
    28. [    0.000000] Movable zone start for each node
    29. [    0.000000] Early memory node ranges
    30. [    0.000000]   node   0: [mem 0x0000000080200000-0x0000000087ffffff]
    31. [    0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x0000000087ffffff]
    32. [    0.000000] elf_hwcap is 0x112d
    33. [    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 31815
    34. [    0.000000] Kernel command line: root=/dev/vda ro console=ttyS0
    35. [    0.000000] PID hash table entries: 512 (order: 0, 4096 bytes)
    36. [    0.000000] Dentry cache hash table entries: 16384 (order: 5, 131072 bytes)
    37. [    0.000000] Inode-cache hash table entries: 8192 (order: 4, 65536 bytes)
    38. [    0.000000] Sorting __ex_table...
    39. [    0.000000] Memory: 122496K/129024K available (2658K kernel code, 216K rwdata, 652K rodata, 96K init, 782K bss, 6528K reserved, 0K cma-reserved)
    40. [    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
    41. [    0.000000] NR_IRQS: 0, nr_irqs: 0, preallocated irqs: 0
    42. [    0.000000] riscv,cpu_intc,0: 64 local interrupts mapped
    43. [    0.000000] riscv,plic0,c000000: mapped 10 interrupts to 1/2 handlers
    44. [    0.000000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns
    45. [    0.000000] Console: colour dummy device 80x25
    46. [    0.000000] Calibrating delay loop (skipped), value calculated using timer frequency.. 20.00 BogoMIPS (lpj=100000)
    47. [    0.000000] pid_max: default: 32768 minimum: 301
    48. [    0.000000] Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
    49. [    0.000000] Mountpoint-cache hash table entries: 512 (order: 0, 4096 bytes)
    50. [    0.090000] devtmpfs: initialized
    51. [    0.110000] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
    52. [    0.110000] futex hash table entries: 256 (order: 0, 6144 bytes)
    53. [    0.110000] random: get_random_u32 called from bucket_table_alloc+0x80/0x1f6 with crng_init=0
    54. [    0.110000] NET: Registered protocol family 16
    55. [    0.140000] vgaarb: loaded
    56. [    0.150000] clocksource: Switched to clocksource riscv_clocksource
    57. [    0.170000] NET: Registered protocol family 2
    58. [    0.170000] TCP established hash table entries: 1024 (order: 1, 8192 bytes)
    59. [    0.170000] TCP bind hash table entries: 1024 (order: 1, 8192 bytes)
    60. [    0.170000] TCP: Hash tables configured (established 1024 bind 1024)
    61. [    0.180000] UDP hash table entries: 256 (order: 1, 8192 bytes)
    62. [    0.180000] UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
    63. [    0.180000] NET: Registered protocol family 1
    64. [    0.190000] workingset: timestamp_bits=62 max_order=15 bucket_order=0
    65. [    0.220000] random: fast init done
    66. [    0.270000] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 254)
    67. [    0.270000] io scheduler noop registered
    68. [    0.270000] io scheduler deadline registered
    69. [    0.270000] io scheduler cfq registered (default)
    70. [    0.270000] io scheduler mq-deadline registered
    71. [    0.270000] io scheduler kyber registered
    72. [    0.380000] Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled
    73. [    0.390000] console [ttyS0] disabled
    74. [    0.400000] 10000000.uart: ttyS0 at MMIO 0x10000000 (irq = 1, base_baud = 230400) is a 16550A
    75. [    0.430000] console [ttyS0] enabled
    76. [    0.480000] NET: Registered protocol family 10
    77. [    0.490000] Segment Routing with IPv6
    78. [    0.490000] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
    79. [    0.520000] EXT4-fs (vda): couldn't mount as ext3 due to feature incompatibilities
    80. [    0.520000] EXT4-fs (vda): INFO: recovery required on readonly filesystem
    81. [    0.530000] EXT4-fs (vda): write access will be enabled during recovery
    82. [    0.610000] EXT4-fs (vda): recovery complete
    83. [    0.620000] EXT4-fs (vda): mounted filesystem with ordered data mode. Opts: (null)
    84. [    0.630000] VFS: Mounted root (ext4 filesystem) readonly on device 254:0.
    85. [    0.630000] devtmpfs: mounted
    86. [    0.640000] Freeing unused kernel memory: 96K
    87. [    0.640000] This architecture does not have kernel memory protection.
    88. [    0.970000] EXT4-fs (vda): re-mounted. Opts: data=ordered

    89. ucbvax login:
    复制代码

    We can login with root using password “busybear” without quote.
    1. ucbvax login: root
    2. Password:
    3.     ____                   ____                     __    _                 
    4.    / __ )__  _________  __/ __ )___  ____ ______   / /   (_)___  __  ___  __
    5.   / __  / / / / ___/ / / / __  / _ \/ __ `/ ___/  / /   / / __ \/ / / / |/_/
    6. / /_/ / /_/ (__  ) /_/ / /_/ /  __/ /_/ / /     / /___/ / / / / /_/ />  <  
    7. /_____/\__,_/____/\__, /_____/\___/\__,_/_/     /_____/_/_/ /_/\__,_/_/|_|  
    8.                  /____/                                                     
    9. root@ucbvax:~# uname -a
    10. Linux ucbvax 4.14.0-00032-gd10799b22913-dirty #79 Sun Dec 17 10:41:31 NZDT 2017 riscv64 GNU/Linux
    11. root@ucbvax:~# df -h
    12. Filesystem                Size      Used Available Use% Mounted on
    13. /dev/root                58.0M     44.9M      8.6M  84% /
    14. devtmpfs                 59.8M         0     59.8M   0% /dev
    15. none                     59.9M         0     59.9M   0% /tmp
    16. none                     59.9M         0     59.9M   0% /var/tmp
    17. root@ucbvax:~# cat /proc/cpuinfo
    18. hart        : 0
    19. isa        : rv64imafdcsu
    20. mmu        : sv48

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

    26. Usage: busybox [function [arguments]...]
    27.    or: busybox --list[-full]
    28.    or: busybox --install [-s] [DIR]
    29.    or: function [arguments]...

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

    34. Currently defined functions:
    35.         [, [[, add-shell, addgroup, adduser, ar, arp, arping, ash, awk, base64,
    36.         basename, blkid, blockdev, brctl, bunzip2, bzcat, bzip2, cal, cat,
    37.         chattr, chgrp, chmod, chown, chpasswd, chroot, chrt, cksum, clear,
    38.         comm, cp, cpio, crond, crontab, cut, date, dd, delgroup, deluser, df,
    39.         diff, dirname, dmesg, dos2unix, dpkg, dpkg-deb, du, echo, ed, egrep,
    40.         env, expand, expr, factor, fallocate, false, fdisk, fgrep, find,
    41.         findfs, flock, fold, fsck, fsfreeze, fsync, fuser, getty, grep, groups,
    42.         gunzip, gzip, halt, head, hostid, hostname, id, ifconfig, ifdown, ifup,
    43.         inetd, init, install, iostat, ip, ipaddr, iplink, ipneigh, iproute,
    44.         iprule, iptunnel, kill, killall, klogd, last, less, link, ln, logger,
    45.         login, logname, logread, losetup, ls, lsattr, lsof, lspci, lsscsi,
    46.         lsusb, lzcat, lzma, makedevs, man, md5sum, microcom, mkdir, mkdosfs,
    47.         mke2fs, mkfifo, mkfs.ext2, mkfs.vfat, mknod, mkpasswd, mkswap, mktemp,
    48.         more, mount, mv, nameif, nc, netstat, nice, nl, nohup, nproc, nslookup,
    49.         ntpd, od, partprobe, passwd, paste, patch, pgrep, pidof, ping, ping6,
    50.         pipe_progress, pivot_root, pkill, pmap, poweroff, printenv, printf, ps,
    51.         pstree, pwd, pwdx, rdev, readlink, realpath, reboot, remove-shell,
    52.         renice, reset, rev, rm, rmdir, route, rpm, rpm2cpio, run-parts,
    53.         runlevel, script, sed, seq, setpriv, setserial, setsid, sh, sha1sum,
    54.         sha256sum, sha3sum, sha512sum, shred, shuf, sleep, sort, split,
    55.         ssl_client, start-stop-daemon, stat, strings, stty, su, sum, swapoff,
    56.         swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset,
    57.         tee, telnet, test, time, timeout, top, touch, tr, traceroute,
    58.         traceroute6, true, truncate, tty, tunctl, tune2fs, udhcpc, uevent,
    59.         umount, uname, uncompress, unexpand, uniq, unix2dos, unlink, unlzma,
    60.         unshare, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vi, w,
    61.         wall, watch, wc, wget, which, who, whoami, whois, xargs, xxd, xz,
    62.         xzcat, yes, zcat
    复制代码

    Type halt to turn off Linux and QEMU:
    1. root@ucbvax:~# halt
    2. root@ucbvax:~# umount: devtmpfs busy - remounted read-only
    3. umount: can't remount /dev/root read-only
    4. The system is going down NOW!
    5. Sent SIGTERM to all processes
    6. Sent SIGKILL to all processes
    7. Requesting system halt
    8. [  410.790000] reboot: System halted
    9. Power off
    复制代码
    本篇完,感谢关注:RISC-V单片机中文网




    上一篇:如何看待龙芯对外公开的 LoongArch 指令集?
    下一篇:在PYNQ-Z2上移植RISC-V
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-11-6 08:02 , Processed in 1.018159 second(s), 49 queries .

    快速回复 返回顶部 返回列表