搭建 LA-Linux 模拟环境
尝试使用 qemu 模拟 loongarch 环境来运行社区版本的 linux 操作系统,结合开源提供的代码和文档做这个实验。应该需要准备
wangjianfeng@dev:~/virtual/loongarch-linux$ tree -L 1
.
├── boot-qemu.sh
├── pc-bios
├── QEMU_EFI.fd
├── qemu-system-loongarch64
├── ramdisk
└── vmlinux.efi
1 directory, 5 files
1 交叉编译器
cd /opt/tools/cross-tools/
wget https://github.com/loongson/build-tools/releases/download/2022.08.11/loongarch64-clfs-5.1-cross-tools-gcc-glibc.tar.xz
tar xvf loongarch64-clfs-5.1-cross-tools-gcc-glibc.tar.xz
# echo "export PATH=/opt/tools/cross-tools/bin:$PATH" >> ~/.bashrc
# source ~/.bashrc
2 bios 固件
2.1 下载
git clone https://github.com/loongson/edk2-LoongarchVirt.git
git checkout --track origin/LoongArch
cd edk2-LoongarchVirt
git submodule update --init
2.2 编译
#! /bin/bash
CC_PREFIX=/opt/tools/cross-tools/
export PATH=${CC_PREFIX}/bin:$PATH
export WORKSPACE=`pwd`
export PACKAGES_PATH=$WORKSPACE/edk2-LoongarchVirt
export GCC5_LOONGARCH64_PREFIX=loongarch64-unknown-linux-gnu-
source edk2-LoongarchVirt/edksetup.sh
make -C edk2-LoongarchVirt/BaseTools
build --buildtarget=DEBUG --tagname=GCC5 --arch=LOONGARCH64 --platform=OvmfPkg/LoongArchQemu/Loongson.dsc
build --buildtarget=RELEASE --tagname=GCC5 --arch=LOONGARCH64 --platform=OvmfPkg/LoongArchQemu/Loongson.dsc
编译前源码中默认放置了 QEMU_EFI.fd_debug 和 QEMU_EFI.fd_release。编译后得到文件 “QEMU_EFI.fd”。
3 linux 内核
3.1 下载
git clone git@github.com:loongson/linux.git
git checkout --track origin/loongarch-next
3.2 编译
#! /bin/bash
CC_PREFIX=/opt/tools/cross-tools/
export PATH="$PATH:${CC_PREFIX}/bin:PATH"
export LD_LIBRARY_PATH="${CC_PREFIX}/lib:$LD_LIBRARY_PATH"
make ARCH=loongarch clean
make ARCH=loongarch defconfig
#make ARCH=loongarch menuconfig
make ARCH=loongarch CROSS_COMPILE=loongarch64-unknown-linux-gnu- -j8
#make modules_install ARCH=loongarch CROSS_COMPILE=loongarch64-linux-gnu- INSTALL_MOD_PATH=./modules
#cp vmlinux vmlinux-public
#tar zcf modules.tar.gz modules vmlinu* System.map
得到 efi 文件 “vmlinux.efi”
4 qemu 程序
4.1 下载
git@github.com:loongson/qemu.git
4.2 编译
./configure --disable-rdma --disable-pvrdma \
--target-list="loongarch64-softmmu" \
--disable-libiscsi --disable-libnfs --disable-libpmem \
--disable-glusterfs --enable-libusb --enable-usb-redir \
--disable-opengl --disable-xen --enable-spice \
--enable-debug --disable-capstone --disable-kvm \
--enable-profiler
make
# make install
得到 efi 文件 “qemu-system-loongarch64” 和 pc-bios 文件包
5 initrd 文件
N/A
6 运行脚本
6.1 准备
#!/bin/bash
./qemu-system-loongarch64 -L ./pc-bios \
-m 4G -smp 1 --cpu la464 --machine virt \
-bios ./QEMU_EFI.fd \
-kernel ./vmlinux.efi \
-serial stdio \
-monitor telnet:localhost:4495,server,nowait \
-initrd ./ramdisk \
-append "root=/dev/ram rdinit=/sbin/init console=ttyS0,115200" --nographic
6.2 运行
wangjianfeng@dev:~/virtual/loongarch-linux$ ./boot-qemu.sh
....
....
[ 3.409556] This architecture does not have kernel memory protection.
[ 3.409939] Run /sbin/init as init process
/ # poweroff
7 参考资料
-
https://github.com/loongson/qemu/blob/master/docs/system/loongarch/loongson3.rst#loongson3-virt-generic-platform-virt
-
https://github.com/yangxiaojuan-loongson/qemu-binary
-
https://github.com/loongson/build-tools