Skip to content

Commit

Permalink
Merge pull request #57 from Alignof/develop
Browse files Browse the repository at this point in the history
Ver 1.1.0
  • Loading branch information
Alignof authored Oct 17, 2024
2 parents 27a1567 + e2c0b6b commit 211a56e
Show file tree
Hide file tree
Showing 43 changed files with 1,625 additions and 292 deletions.
9 changes: 6 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
[target.riscv64imac-unknown-none-elf]
runner = """
qemu-system-riscv64
-cpu rv64,smstateen=true
-machine virt
-bios none
-nographic
-m 2G
-initrd vmlinux
-drive file=rootfs.img,format=raw,id=hd0,if=none
-initrd vmlinux_debug
-drive file=rootfs.ext2,format=raw,id=hd0,if=none
-device virtio-blk-pci,drive=hd0,iommu_platform=true,disable-legacy=on
-netdev user,id=n1
-device virtio-net-pci,netdev=n1,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
Expand All @@ -24,4 +28,3 @@ rustflags = [

[build]
target = "riscv64imac-unknown-none-elf"

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Cargo.lock
.gdb_history
vmlinux
vmlinux_debug
rootfs.*
18 changes: 15 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
[package]
name = "hikami"
version = "1.0.0"
version = "1.1.0"
edition = "2021"

[lints.clippy]
pedantic = "warn"
missing_docs_in_private_items = "warn"

[lints.rust]
missing_docs = "warn"

[lints.rustdoc]
missing_crate_level_docs = "warn"


[features]
# for real device
embedded_host_dtb = []

[dependencies]
elf = { version = "0.7.2", default-features = false }
fdt = "0.1.5"
linked_list_allocator = "0.10.5"
raki = { version = "1.0.0" }
raki = "1.2.0"
riscv = "0.11.1"
riscv-rt = "0.11.0"
riscv-rt = { git = "https://github.com/Alignof/riscv", branch = "fix/link_error_on_latest_rust" }
rustsbi = { version = "0.4.0-alpha.1", features = ["machine"] }
sbi-rt = "0.0.3"
sbi-spec = { version = "0.0.7", features = [ "legacy" ] }
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Poster in RISC-V Days Tokyo 2024 Summer: [PDF](https://riscv.or.jp/wp-content/up
## Run Linux
### Build QEMU
We need to build manually the QEMU to support IOMMU.
```
```sh
$ git clone https://github.com/qemu/qemu.git -b staging
$ cd qemu/
# https://patchwork.ozlabs.org/project/qemu-devel/list/?series=417654
Expand All @@ -21,7 +21,7 @@ $ make -j $(nproc)
Ver. 9.2 or later should officially support IOMMU, so it should no longer be necessary to apply patches.

### Build Linux
```
```sh
$ git clone https://github.com/torvalds/linux -b v6.9

$ cd /path/to/this/repository
Expand All @@ -35,7 +35,7 @@ $ mv vmlinux /path/to/this/repository
See also for custom guest image: `guest_image/README.md`.

### Create rootfs
```
```sh
$ git clone https://gitee.com/mirrors/busyboxsource.git
$ cd busyboxsource

Expand Down Expand Up @@ -70,13 +70,13 @@ $ mv rootfs.img /path/to/this/repository
```

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

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

Expand Down
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! build.rs - For build script for cargo project.
use std::env;
use std::fs;
use std::path::PathBuf;

/// Build script for cargo project
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

Expand Down
Binary file modified guest.dtb
Binary file not shown.
24 changes: 22 additions & 2 deletions guest_image/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Build guest image

## device tree
```
```sh
$ ./build_dtb.sh create
$ vim guest.dts # edit dts
$ ./build_dtb.sh build
# guest.dtb is created to repository root.
```

## Linux (with debug info)
```
```sh
$ git clone https://github.com/torvalds/linux -b v6.9

$ cd /path/to/this/repository
Expand All @@ -22,3 +22,23 @@ $ 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
```

## Linux (For Zicfiss)
See [https://lwn.net/Articles/992578/](https://lwn.net/Articles/992578/).
```sh
# Toolchain
$ git clone [email protected]:sifive/riscv-gnu-toolchain.git -b cfi-dev
$ riscv-gnu-toolchain/configure --prefix=<path-to-where-to-build> --with-arch=rv64gc_zicfilp_zicfiss --enable-linux --disable-gdb --with-extra-multilib-test="rv64gc_zicfilp_zicfiss-lp64d:-static"
$ make -j$(nproc)

# Opensbi
$ git clone [email protected]:deepak0414/opensbi.git -b v6_cfi_spec_split_opensbi
$ make CROSS_COMPILE=<your riscv toolchain> -j$(nproc) PLATFORM=generic

# Linux
$ git clone https://github.com/torvalds/linux -b v6.12-rc1
$ wget https://patchwork.kernel.org/series/896898/mbox/ --output-document riscv-control-flow-integrity-for-usermode.patch
$ git am riscv-control-flow-integrity-for-usermode.patch
$ make ARCH=riscv CROSS_COMPILE=<path-to-cfi-riscv-gnu-toolchain>/build/bin/riscv64-unknown-linux-gnu- -j$(nproc) defconfig
$ make ARCH=riscv CROSS_COMPILE=<path-to-cfi-riscv-gnu-toolchain>/build/bin/riscv64-unknown-linux-gnu- -j$(nproc)
```
92 changes: 76 additions & 16 deletions guest_image/build_dtb.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,93 @@
#!/bin/bash

qemu_path="../../qemu_iommu/build/qemu-system-riscv64"

function help() {
echo "create: create dts from qemu dtb"
echo "build: build dtb from guest.dts"
echo "create [host/guest/all]: create host or guest dts from qemu dtb"
echo "build [host/guest/all]: build host or guest dtb from guest.dts"
}

function create_host() {
$qemu_path -S -gdb tcp::10000 \
-machine virt \
-bios none \
-m 256M \
-initrd ../vmlinux_debug \
-drive file=../rootfs.ext2,format=raw,id=hd0,if=none \
-device virtio-blk-device,drive=hd0 \
-netdev user,id=n1 \
-device virtio-net-pci,netdev=n1 \
-device riscv-iommu-pci \
-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 host.dts qemu.dtb
rm -f qemu.dtb
}

function create_guest() {
$qemu_path -S -gdb tcp::10000 \
-machine virt \
-bios none \
-m 256M \
-initrd ../vmlinux_debug \
-drive file=../rootfs.ext2,format=raw,id=hd0,if=none \
-device virtio-blk-device,drive=hd0 \
-netdev user,id=n1 \
-device virtio-net-pci,netdev=n1 \
-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
}

if [ "$#" -eq 0 ]; then
help
fi

if [ "$#" -eq 1 ]; then
echo "specify target: host or guest or all"
help
fi

if [ "$#" -eq 2 ]; then
case "$1" in
"create")
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
case "$2" in
"host")
create_host
;;
"guest")
create_guest
;;
"all")
create_host
create_guest
;;
*)
echo "specify target: host or guest or all"
help
;;
esac
;;
"build")
dtc -I dts -O dtb -o ../guest.dtb guest.dts
case "$2" in
"host")
dtc -I dts -O dtb -o ../host.dtb host.dts
;;
"guest")
dtc -I dts -O dtb -o ../guest.dtb guest.dts
;;
"all")
dtc -I dts -O dtb -o ../host.dtb host.dts
dtc -I dts -O dtb -o ../guest.dtb guest.dts
;;
*)
echo "specify target: host or guest or all"
help
;;
esac
;;
*)
echo "command not found"
Expand Down
32 changes: 10 additions & 22 deletions guest_image/guest.dts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ranges = <0x00 0x00 0x4000000 0x2000000>;
#address-cells = <0x01>;
#size-cells = <0x01>;
compatible = "qemu,platform\0simple-bus";
compatible = "qemu,platform", "simple-bus";
};

memory@90000000 {
Expand All @@ -47,9 +47,9 @@
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\0zmmul\0za64rs\0zaamo\0zalrsc\0zawrs\0zfa\0zca\0zcd\0zba\0zbb\0zbc\0zbs\0ssccptr\0sscounterenw\0sstc\0sstvala\0sstvecd\0svadu";
riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "h", "zic64b", "zicbom", "zicbop", "zicboz", "ziccamoa", "ziccif", "zicclsm", "ziccrse", "zicfiss", "zicntr", "zicsr", "zifencei", "zihintntl", "zihintpause", "zihpm", "zmmul", "za64rs", "zaamo", "zalrsc", "zawrs", "zfa", "zca", "zcd", "zba", "zbb", "zbc", "zbs", "ssccptr", "sscounterenw", "sstc", "sstvala", "sstvecd", "svadu";
riscv,isa-base = "rv64i";
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";
riscv,isa = "rv64imafdch_zic64b_zicbom_zicbop_zicboz_ziccamoa_ziccif_zicclsm_ziccrse_zicfiss_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 {
Expand Down Expand Up @@ -90,10 +90,10 @@

chosen {
bootargs = "root=/dev/vda rw console=ttyS0";
linux,initrd-end = <0x00 0x97f67ce0>;
linux,initrd-end = <0x00 0x9812b230>;
linux,initrd-start = <0x00 0x88000000>;
stdout-path = "/soc/serial@10000000";
rng-seed = <0xcaf53f7f 0x52fe6542 0xf3156947 0xa50b572d 0xc6f64192 0x47d6e429 0x1985224b 0xe8a94cc2>;
rng-seed = <0x2b05a034 0xe739bfb7 0x512d2025 0xf0ec97e8 0x69399e86 0x1f2de5a9 0xcacea4aa 0xafb54655>;
};

soc {
Expand All @@ -112,15 +112,15 @@
serial@10000000 {
interrupts = <0x0a>;
interrupt-parent = <0x03>;
clock-frequency = "\08@";
clock-frequency = "", "8@";
reg = <0x00 0x10000000 0x00 0x100>;
compatible = "ns16550a";
};

test@100000 {
phandle = <0x04>;
reg = <0x00 0x100000 0x00 0x1000>;
compatible = "sifive,test1\0sifive,test0\0syscon";
compatible = "sifive,test1", "sifive,test0", "syscon";
};

virtio_mmio@10008000 {
Expand Down Expand Up @@ -185,25 +185,21 @@
reg = <0x00 0xc000000 0x00 0x600000>;
interrupts-extended = <0x02 0x0b 0x02 0x09>;
interrupt-controller;
compatible = "sifive,plic-1.0.0\0riscv,plic0";
compatible = "sifive,plic-1.0.0", "riscv,plic0";
#address-cells = <0x00>;
#interrupt-cells = <0x01>;
};

clint@2000000 {
interrupts-extended = <0x02 0x03 0x02 0x07>;
reg = <0x00 0x2000000 0x00 0x10000>;
compatible = "sifive,clint0\0riscv,clint0";
compatible = "sifive,clint0", "riscv,clint0";
};

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>;
// 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>;
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 @@ -213,14 +209,6 @@
#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 211a56e

Please sign in to comment.