1. 程式人生 > 其它 >在 QEMU 上執行 RISC-V 32 位版本的 Linux

在 QEMU 上執行 RISC-V 32 位版本的 Linux

技術標籤:RISC-VLinux

前言

最近中科院軟體所PLCT實驗室要開始基於 OpenJDK11 進行 RISC-V 32G 的移植工作,需要搭建好 RISC-V 32 位的 Linux 環境,本文就是介紹 RISC-V 32 位 Yocto版本的 Linux 環境的搭建過程。

1. 環境準備

1.1 宿主機

本次部署基於 Ubuntu 18.04.5 LTS 進行操作。

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.5 LTS
Release:	18.04
Codename:	bionic
1.2 交叉工具鏈

構建 32-bit RV32GC 的 riscv-gnu-toolchain,以下兩種方法均可以完成構建,本文采用的是第二種。

  • 基於官方的指導構建

    https://github.com/riscv/riscv-gnu-toolchain

  • 直接下載構建好的工具鏈

    http://plct.zdlgv5.com/riscv32_extlib.tar.gzplct.zdlgv5.com

    $ wget http://plct.zdlgv5.com/riscv32_extlib.tar.gz
    $ tar -xzvf riscv32_extlib.tar.gz
    

    設定環境變數

    $ export PATH=$PATH:./riscv32/bin
    

注:

  1. 上面的環境變數只在當前終端有效,也可以 ~/.bashrc 檔案中永久生效。

  2. 測試構建是否成功可以執行:

$ riscv32-unknown-linux-gnu-gcc -v

​ 出現類似輸出即表示安裝成功。

$ riscv32-unknown-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=riscv32-unknown-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/home/jiayou/riscv32/bin/../libexec/gcc/riscv32-unknown-linux-gnu/9.2.0/lto-wrapper
Target: riscv32-unknown-linux-gnu
Configured with: /home/zhangdingli/toolchain/riscv-gnu-toolchain/riscv-gcc/configure --target=riscv32-unknown-linux-gnu --prefix=/home/zhangdingli/toolchain/riscv32 --with-sysroot=/home/zhangdingli/toolchain/riscv32/sysroot --with-system-zlib --enable-shared --enable-tls --enable-languages=c,c++,fortran --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libsanitizer --disable-nls --disable-bootstrap --src=.././riscv-gcc --disable-multilib --with-abi=ilp32d --with-arch=rv32gc --with-tune=rocket 'CFLAGS_FOR_TARGET=-O2   -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-O2   -mcmodel=medlow'
Thread model: posix
gcc version 9.2.0 (GCC) 

2. 部署 Yocto

2.1 Create workspace
$ mkdir riscv-yocto && cd riscv-yocto
$ repo init -u git://github.com/riscv/meta-riscv  -b master -m tools/manifests/riscv-yocto.xml
$ repo sync
$ repo start work --all

注:

這裡可能會遇到下面的錯誤:

Traceback (most recent call last):
  File "/home/jiayou/riscv-sifive/.repo/repo/main.py", line 49, in <module>
    import event_log
  File "/home/jiayou/riscv-sifive/.repo/repo/event_log.py", line 167, in <module>
    _EVENT_ID = multiprocessing.Value('i', 1)
  File "/snap/git-repo/18/usr/lib/python2.7/multiprocessing/__init__.py", line 253, in Value
    return Value(typecode_or_type, *args, **kwds)
  File "/snap/git-repo/18/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 108, in Value
    lock = RLock()
  File "/snap/git-repo/18/usr/lib/python2.7/multiprocessing/__init__.py", line 183, in RLock
    return RLock()
  File "/snap/git-repo/18/usr/lib/python2.7/multiprocessing/synchronize.py", line 172, in __init__
    SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1)
  File "/snap/git-repo/18/usr/lib/python2.7/multiprocessing/synchronize.py", line 75, in __init__
    sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
OSError: [Errno 13] Permission denied

解決方法如下:

$ mkdir -p ~/.bin
$ PATH="${HOME}/.bin:${PATH}"
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
$ chmod a+rx ~/.bin/repo
2.2 Update existing workspace
$ cd riscv-yocto
$ repo sync
$ repo rebase
2.3 Setup Build Environment
. ./meta-riscv/setup.sh
2.4 Build Images

To build a console-only image for the 64-bit QEMU machine

$ MACHINE=qemuriscv32 bitbake core-image-full-cmdline

注:第一次執行這步需要較長的執行過程。

2.5 Running in QEMU
MACHINE=qemuriscv32 runqemu nographic

成功啟動後終端測試如下:

...
OpenEmbedded nodistro.0 qemuriscv32 ttyS0

qemuriscv32 login: root
[email protected]:~# uname -a
Linux qemuriscv32 5.8.18-yocto-standard #1 SMP PREEMPT Tue Jan 5 10:15:33 UTC 2021 riscv32 riscv32 riscv32 GNU/Linux
[email protected]:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       123M   88M   27M  78% /
devtmpfs        115M     0  115M   0% /dev
tmpfs           115M     0  115M   0% /dev/shm
tmpfs            46M  8.4M   38M  19% /run
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
tmpfs           115M     0  115M   0% /tmp
tmpfs           115M   72K  115M   1% /var/volatile
tmpfs            23M     0   23M   0% /run/user/0
[email protected]:~# cat /proc/cpuinfo 
processor	: 0
hart		: 0
isa		: rv32imafdcsu
mmu		: sv32

[email protected]:~# free -mh
              total        used        free      shared  buff/cache   available
Mem:          228Mi        28Mi       157Mi       8.0Mi        42Mi       184Mi
Swap:            0B          0B          0B
[email protected]:~#

3. 修改虛機配置

上面啟動的虛機本身儲存空間較小,且不能使用 SSH 和 SCP 功能,以下操作是對應的解決辦法。

3.1 開啟 SSH 和 SCP
  1. 修改虛機配置檔案 riscv-yocto/openembedded-core/meta/conf/machine/include/riscv/qemuriscv.inc

    QB_TAP_OPT = "-netdev tap,id=net0,[email protected]@,script=no,downscript=no"
    

    ​ 為

    QB_TAP_OPT = "-netdev tap,id=net0,[email protected]@,script=no,downscript=no,hostfwd=tcp::2222-:22"
    
  2. 重新編譯映象

    $ MACHINE=qemuriscv32 bitbake core-image-full-cmdline
    
  3. 啟動虛擬機器

    $ MACHINE=qemuriscv32 runqemu nographic slirp
    
  4. 測試
    SCP

    $ scp -P 2222 ./jdk32.tar.gz [email protected]:~/
    The authenticity of host '[localhost]:2222 ([127.0.0.1]:2222)' can't be established.
    ECDSA key fingerprint is SHA256:qnd5gnS289OoComcMnUqAn/JC24ibc+GUNDiUghshcU.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '[localhost]:2222' (ECDSA) to the list of known hosts.
    jdk32.tar.gz                                                                                                                                                                                                  100%  160MB   8.8MB/s   00:18    
    [email protected]:~$
    

    SSH

    $ ssh -p 2222 [email protected] 
    Last login: Thu Dec 10 11:45:03 2020
    [email protected]:~# 
    

注:

  1. 啟動 SSH 和 SCP 後啟動虛機命令變為 " MACHINE=qemuriscv32 runqemu nographic slirp "
  2. 參考: https://github.com/riscv/meta-riscv/issues/195
3.2 修改虛機儲存

編譯的映象儲存在 riscv-yocto/build/tmp-glibc/deploy/images/qemuriscv32 檔案下,可以進去該目錄下進行相關操作

  • 進入映象儲存位置
$ cd riscv-yocto/build/tmp-glibc/deploy/images/qemuriscv32
$ ls -lh
total 289M
lrwxrwxrwx 2 jiayou jiayou   74 Jan  5 10:19 Image -> Image--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.bin
-rw-r--r-- 2 jiayou jiayou  21M Jan  5 10:19 Image--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.bin
lrwxrwxrwx 2 jiayou jiayou   74 Jan  5 10:19 Image-qemuriscv32.bin -> Image--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.bin
-rw-rw-r-- 2 jiayou jiayou 1.5K Jan  5 11:18 core-image-full-cmdline-qemuriscv32-20210105094513.qemuboot.conf
-rw-r--r-- 2 jiayou jiayou 132M Jan  5 12:04 core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.ext4
-rw-r--r-- 2 jiayou jiayou  15K Jan  5 11:18 core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.manifest
-rw-rw-r-- 2 jiayou jiayou  29M Jan  5 11:18 core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.tar.bz2
-rw-r--r-- 2 jiayou jiayou 108M Jan  5 11:18 core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.wic.qcow2
-rw-r--r-- 2 jiayou jiayou 214K Jan  5 11:18 core-image-full-cmdline-qemuriscv32-20210105094513.testdata.json
lrwxrwxrwx 2 jiayou jiayou   62 Jan  5 11:18 core-image-full-cmdline-qemuriscv32.ext4 -> core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.ext4
lrwxrwxrwx 2 jiayou jiayou   66 Jan  5 11:18 core-image-full-cmdline-qemuriscv32.manifest -> core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.manifest
lrwxrwxrwx 2 jiayou jiayou   64 Jan  5 11:18 core-image-full-cmdline-qemuriscv32.qemuboot.conf -> core-image-full-cmdline-qemuriscv32-20210105094513.qemuboot.conf
lrwxrwxrwx 2 jiayou jiayou   65 Jan  5 11:18 core-image-full-cmdline-qemuriscv32.tar.bz2 -> core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.tar.bz2
lrwxrwxrwx 2 jiayou jiayou   64 Jan  5 11:18 core-image-full-cmdline-qemuriscv32.testdata.json -> core-image-full-cmdline-qemuriscv32-20210105094513.testdata.json
lrwxrwxrwx 2 jiayou jiayou   67 Jan  5 11:18 core-image-full-cmdline-qemuriscv32.wic.qcow2 -> core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.wic.qcow2
-rw-rw-r-- 2 jiayou jiayou 4.1K Jan  5 11:18 core-image-full-cmdline.env
-rwxr-xr-x 2 jiayou jiayou  61K Jan  5 11:02 fw_dynamic.bin
-rwxr-xr-x 2 jiayou jiayou 555K Jan  5 11:02 fw_dynamic.elf
-rwxr-xr-x 2 jiayou jiayou  61K Jan  5 11:02 fw_jump.bin
-rwxr-xr-x 2 jiayou jiayou 555K Jan  5 11:02 fw_jump.elf
-rwxr-xr-x 2 jiayou jiayou  25M Jan  5 11:02 fw_payload.bin
-rwxr-xr-x 2 jiayou jiayou  22M Jan  5 11:02 fw_payload.elf
-rw-rw-r-- 2 jiayou jiayou 2.4M Jan  5 10:19 modules--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.tgz
lrwxrwxrwx 2 jiayou jiayou   76 Jan  5 10:19 modules-qemuriscv32.tgz -> modules--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.tgz
lrwxrwxrwx 2 jiayou jiayou   75 Jan  5 10:19 uImage -> uImage--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.bin
-rw-r--r-- 2 jiayou jiayou 5.8M Jan  5 10:19 uImage--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.bin
lrwxrwxrwx 2 jiayou jiayou   75 Jan  5 10:19 uImage-qemuriscv32.bin -> uImage--5.8.18+git0+b976de4f41_3c5d210805-r0-qemuriscv32-20210105094513.bin
  • 修改映象大小
# 建立名字為new_img.img,大小為:10G 的Image
$ dd if=/dev/zero of=new_img.img bs=1G count=10
# 格式化新建的Image ext4
$ mkfs.ext4 new_img.img
# 建立新舊Image的掛在目錄
$ sudo mkdir newImage
$ sudo mkdir oldImage
# 掛載Image
$ sudo mount new_img.img ./newImage/
$ sudo mount core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.ext4 ./oldImage/
# 將舊image對應的目錄頁拷貝到新image對應的目錄
$ sudo cp -r ./oldImage/* ./newImage/
# 解除安裝目錄
sudo umount ./newImage/
sudo umount ./oldImage/
# 將新的image檔案修改為舊image檔名
$ sudo mv core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.ext4 core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.ext4.backup
$ sudo mv new_img.img core-image-full-cmdline-qemuriscv32-20210105094513.rootfs.ext4

再啟動虛機檢視儲存確實變為 10 G

qemuriscv32 login: root
[email protected]:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       9.8G  144M  9.2G   2% /
devtmpfs        115M     0  115M   0% /dev
tmpfs           115M     0  115M   0% /dev/shm
tmpfs            46M  8.4M   38M  19% /run
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
tmpfs           115M     0  115M   0% /tmp
tmpfs           115M   72K  115M   1% /var/volatile
tmpfs            23M     0   23M   0% /run/user/0
[email protected]:~# 

注:
參考:https://www.cnblogs.com/grandblogs/p/12216955.html