Skip to content

Commit

Permalink
Merge pull request #48 from Alignof/develop
Browse files Browse the repository at this point in the history
Ver 1.0.0
  • Loading branch information
Alignof authored Sep 29, 2024
2 parents 9f3b3f0 + e9c5bfb commit 27a1567
Show file tree
Hide file tree
Showing 36 changed files with 1,347 additions and 277 deletions.
19 changes: 16 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
[target.riscv64imac-unknown-none-elf]
runner = "qemu-system-riscv64 -S -gdb tcp::10000 -machine virt -bios none -nographic -m 2G -initrd vmlinux_debug -kernel"
#runner = "qemu-system-riscv64 -S -gdb tcp::10000 -d int,in_asm,cpu_reset,mmu,page,guest_errors -machine virt -bios none -nographic -m 2G -initrd vmlinux -kernel"
#runner = "qemu-system-riscv64 -machine virt -bios none -nographic -m 2G -initrd vmlinux -kernel"
runner = """
qemu-system-riscv64
-machine virt
-bios none
-nographic
-m 2G
-initrd vmlinux
-drive file=rootfs.img,format=raw,id=hd0,if=none
-device virtio-blk-pci,drive=hd0,iommu_platform=true,disable-legacy=on
-append root=/dev/vda,rw,console=ttyS0
-device riscv-iommu-pci
-kernel
"""
# for debug
# runner = "../../qemu/build/qemu-system-riscv64 -S -gdb tcp::10000 -d int,in_asm,cpu_reset,mmu,page,guest_errors -machine virt -bios none -nographic -m 2G -initrd vmlinux_debug -drive file=rootfs.img,format=raw,id=hd0,if=none -device virtio-blk-pci,drive=hd0,iommu_platform=true,disable-legacy=on -append root=/dev/vda,rw,console=ttyS0 -device riscv-iommu-pci -kernel"
# memo: maintenance packet Qqemu.PhyMemMode:1

rustflags = [
"-C", "link-arg=-Tmemory.x",
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Tag

on:
push:
branches:
- master
paths:
- Cargo.toml

jobs:
tag:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: salsify/action-detect-and-tag-new-version@v2
id: detect_tag
with:
create-tag: true
version-command: cargo read-manifest | jq -r .version
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ Cargo.lock

# Added by me
.gdb_history
fedora-vmlinux
stage4-disk.img
vmlinux
vmlinux_debug
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hikami"
version = "0.4.0"
version = "1.0.0"
edition = "2021"

[lints.clippy]
Expand All @@ -10,11 +10,10 @@ pedantic = "warn"
elf = { version = "0.7.2", default-features = false }
fdt = "0.1.5"
linked_list_allocator = "0.10.5"
raki = { version = "1.0.0-beta.3" }
raki = { version = "1.0.0" }
riscv = "0.11.1"
riscv-rt = "0.11.0"
rustsbi = { version = "0.4.0-alpha.1", features = ["machine"] }
sbi-rt = "0.0.3"
sbi-spec = { version = "0.0.7", features = [ "legacy" ] }
spin = "0.9.8"
wild_screen_alloc = { version = "0.1.1", path = "wild-screen-alloc" }
81 changes: 78 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,84 @@
# hikami
[![Rust](https://github.com/Alignof/hikami/actions/workflows/rust.yml/badge.svg)](https://github.com/Alignof/hikami/actions/workflows/rust.yml)
Light weight hypervisor for RISC-V H-extension.
Light weight type-1 hypervisor for RISC-V H-extension.

## Tutorial
TODO
This project aims not only to realize a lightweight hypervisor that can be used on RISC-V H extensions, but also to easily reproduce and manage the "extension" on the hypervisor. (currently in progress)
Poster in RISC-V Days Tokyo 2024 Summer: [PDF](https://riscv.or.jp/wp-content/uploads/RV-Days_Tokyo_2024_Summer_paper_9.pdf)

## Run Linux
### Build QEMU
We need to build manually the QEMU to support IOMMU.
```
$ git clone https://github.com/qemu/qemu.git -b staging
$ cd qemu/
# https://patchwork.ozlabs.org/project/qemu-devel/list/?series=417654
$ wget https://patchwork.ozlabs.org/series/417654/mbox/ --output-document riscv-QEMU-RISC-V-IOMMU-Support.patch
$ git apply riscv-QEMU-RISC-V-IOMMU-Support.patch
$ ./configure --target-list=riscv64-softmmu
$ make -j $(nproc)
# $ sudo make install
```
Ver. 9.2 or later should officially support IOMMU, so it should no longer be necessary to apply patches.

### Build Linux
```
$ git clone https://github.com/torvalds/linux -b v6.9
$ cd /path/to/this/repository
$ cp ./guest_image/.config /path/to/linux
$ cd /path/to/linux
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j$(nproc)
$ mv vmlinux /path/to/this/repository
```
See also for custom guest image: `guest_image/README.md`.

### Create rootfs
```
$ git clone https://gitee.com/mirrors/busyboxsource.git
$ cd busyboxsource
# Select: Settings -> Build Options -> Build static binary
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make menuconfig
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make -j8
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make install
$ cd ../
$ qemu-img create rootfs.img 1g
$ mkfs.ext4 rootfs.img
$ mkdir rootfs
$ mount -o loop rootfs.img rootfs
$ cd rootfs
$ cp -r ../busyboxsource/_install/* .
$ mkdir proc dev tec etc/init.d
$ cd etc/init.d/
$ cat << EOS > rcS
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
EOS
$ chmod +x rcS
$ umount rootfs
$ mv rootfs.img /path/to/this/repository
```

### Run
```
# The actual command to be executed is written in .cargo/config.toml.
$ cargo r
```

## Documents
```
$ cargo doc --open
```

## References
- [The RISC-V Instruction Set Manual: Volume I Version 20240411](https://github.com/riscv/riscv-isa-manual/releases/download/20240411/unpriv-isa-asciidoc.pdf)
Expand Down
Binary file modified guest.dtb
Binary file not shown.
29 changes: 18 additions & 11 deletions guest_image/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
# Build guest image

## Linux
## device tree
```
git clone https://github.com/torvalds/linux -b v6.9
$ ./build_dtb.sh create
$ vim guest.dts # edit dts
$ ./build_dtb.sh build
# guest.dtb is created to repository root.
```

## Linux (with debug info)
```
$ git clone https://github.com/torvalds/linux -b v6.9
cd /path/to/this/repository
cp ./.config /path/to/linux
$ cd /path/to/this/repository
$ cp ./guest_image/.config /path/to/linux
cd /path/to/linux
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig
# For debug
# make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- menuconfig
# DEBUG_KERNEL [=y], DEBUG_INFO [=y], EFI [=n], RELOCATABLE [=n]
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j$(nproc)
mv vmlinux /path/to/linux/vmlinx_debug
$ cd /path/to/linux
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- menuconfig
$ DEBUG_KERNEL [=y], DEBUG_INFO [=y], EFI [=n], RELOCATABLE [=n]
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j$(nproc)
$ mv vmlinux /path/to/linux/vmlinx_debug
```
12 changes: 11 additions & 1 deletion guest_image/build_dtb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ fi
if [ "$#" -eq 1 ]; then
case "$1" in
"create")
qemu-system-riscv64 -S -gdb tcp::10000 -machine virt -bios none -m 256M -machine dumpdtb=qemu.dtb
qemu-system-riscv64 -S -gdb tcp::10000 \
-machine virt \
-bios none \
-m 256M \
-initrd ../vmlinux_debug \
-device riscv-iommu-pci \
-drive file=../rootfs.img,format=raw,id=hd0,if=none \
-device virtio-blk-device,drive=hd0 \
-append "root=/dev/vda rw console=ttyS0" \
-kernel ../target/riscv64imac-unknown-none-elf/debug/hikami \
-machine dumpdtb=qemu.dtb
dtc -I dtb -O dts -o guest.dts qemu.dtb
rm -f qemu.dtb
;;
Expand Down
25 changes: 20 additions & 5 deletions guest_image/guest.dts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
riscv,cbop-block-size = <0x40>;
riscv,cboz-block-size = <0x40>;
riscv,cbom-block-size = <0x40>;
riscv,isa-extensions = "i\0m\0a\0f\0d\0c\0h\0zic64b\0zicbom\0zicbop\0zicboz\0ziccamoa\0ziccif\0zicclsm\0ziccrse\0zicntr\0zicsr\0zifencei\0zihintntl\0zihintpause\0zihpm\0za64rs\0zawrs\0zfa\0zca\0zcd\0zba\0zbb\0zbc\0zbs\0ssccptr\0sscounterenw\0sstc\0sstvala\0sstvecd\0svadu";
riscv,isa-extensions = "i\0m\0a\0f\0d\0c\0h\0zic64b\0zicbom\0zicbop\0zicboz\0ziccamoa\0ziccif\0zicclsm\0ziccrse\0zicntr\0zicsr\0zifencei\0zihintntl\0zihintpause\0zihpm\0zmmul\0za64rs\0zaamo\0zalrsc\0zawrs\0zfa\0zca\0zcd\0zba\0zbb\0zbc\0zbs\0ssccptr\0sscounterenw\0sstc\0sstvala\0sstvecd\0svadu";
riscv,isa-base = "rv64i";
riscv,isa = "rv64imafdch_zic64b_zicbom_zicbop_zicboz_ziccamoa_ziccif_zicclsm_ziccrse_zicntr_zicsr_zifencei_zihintntl_zihintpause_zihpm_za64rs_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_ssccptr_sscounterenw_sstc_sstvala_sstvecd_svadu";
mmu-type = "riscv,sv39";
riscv,isa = "rv64imafdch_zic64b_zicbom_zicbop_zicboz_ziccamoa_ziccif_zicclsm_ziccrse_zicntr_zicsr_zifencei_zihintntl_zihintpause_zihpm_zmmul_za64rs_zaamo_zalrsc_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_ssccptr_sscounterenw_sstc_sstvala_sstvecd_svadu";
mmu-type = "riscv,sv57";

interrupt-controller {
#interrupt-cells = <0x01>;
Expand Down Expand Up @@ -89,8 +89,11 @@
};

chosen {
bootargs = "root=/dev/vda rw console=ttyS0";
linux,initrd-end = <0x00 0x97f67ce0>;
linux,initrd-start = <0x00 0x88000000>;
stdout-path = "/soc/serial@10000000";
rng-seed = <0x84243e45 0x3047b358 0xbacf5c32 0x578c88fe 0x89c61300 0x205ccc21 0x650e0486 0xf27a7c6f>;
rng-seed = <0xcaf53f7f 0x52fe6542 0xf3156947 0xa50b572d 0xc6f64192 0x47d6e429 0x1985224b 0xe8a94cc2>;
};

soc {
Expand Down Expand Up @@ -196,7 +199,11 @@
pci@30000000 {
interrupt-map-mask = <0x1800 0x00 0x00 0x07>;
interrupt-map = <0x00 0x00 0x00 0x01 0x03 0x20 0x00 0x00 0x00 0x02 0x03 0x21 0x00 0x00 0x00 0x03 0x03 0x22 0x00 0x00 0x00 0x04 0x03 0x23 0x800 0x00 0x00 0x01 0x03 0x21 0x800 0x00 0x00 0x02 0x03 0x22 0x800 0x00 0x00 0x03 0x03 0x23 0x800 0x00 0x00 0x04 0x03 0x20 0x1000 0x00 0x00 0x01 0x03 0x22 0x1000 0x00 0x00 0x02 0x03 0x23 0x1000 0x00 0x00 0x03 0x03 0x20 0x1000 0x00 0x00 0x04 0x03 0x21 0x1800 0x00 0x00 0x01 0x03 0x23 0x1800 0x00 0x00 0x02 0x03 0x20 0x1800 0x00 0x00 0x03 0x03 0x21 0x1800 0x00 0x00 0x04 0x03 0x22>;
ranges = <0x1000000 0x00 0x00 0x00 0x3000000 0x00 0x10000 0x2000000 0x00 0x40000000 0x00 0x40000000 0x00 0x40000000 0x3000000 0x04 0x00 0x04 0x00 0x04 0x00>;
// BUS_ADDRESS(3) CPU_PHYSICAL(2) SIZE(2)
ranges = <
0x1000000 0x00 0x00 0x00 0x3000000 0x00 0x10000
0x2000000 0x00 0x40000000 0x00 0x40000000 0x00 0x40000000
0x3000000 0x04 0x00 0x04 0x00 0x04 0x00>;
reg = <0x00 0x30000000 0x00 0x10000000>;
dma-coherent;
bus-range = <0x00 0xff>;
Expand All @@ -206,6 +213,14 @@
#size-cells = <0x02>;
#interrupt-cells = <0x01>;
#address-cells = <0x03>;
iommu-map = <0x00 0x8000 0x00 0x08 0x09 0x8000 0x09 0xfff7>;

iommu@8 {
reg = <0x800 0x00 0x00 0x00 0x00>;
phandle = <0x8000>;
#iommu-cells = <0x01>;
compatible = "riscv,pci-iommu";
};
};
};
};
Loading

0 comments on commit 27a1567

Please sign in to comment.