Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ziicfiss emulation #51

Merged
merged 64 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
3962ccf
[update] ignore rootfs image file
Alignof Oct 1, 2024
9a04bd7
[update] update crate version of raki
Alignof Oct 2, 2024
0bddaf2
[!][fix] set `scounteren` CSR
Alignof Oct 2, 2024
d3a448e
[wip][add] add handling Zicfiss instruciton
Alignof Oct 2, 2024
f0e2c84
[add] add `emulate_extension` module
Alignof Oct 2, 2024
201c9a7
[wip][update] move an instruction emulation to `emulate_extension` mo…
Alignof Oct 2, 2024
020d4cf
[add] add structs `Zicfiss` and `ShadowStack`
Alignof Oct 2, 2024
6dd5921
[wip][add] add global variable `ZICFISS_DATA`
Alignof Oct 2, 2024
daeb5d0
[add] add `ShadowStack::get_ssp`
Alignof Oct 2, 2024
bf9e59c
[add] implement `Zicfiss::new`
Alignof Oct 2, 2024
8ec5913
[fix] fix `ShadowStack::pop`
Alignof Oct 2, 2024
b1d493f
[wip][add] implement instruction emulation
Alignof Oct 2, 2024
7a174e6
[add] add `FwftFeature `
Alignof Oct 3, 2024
048c1ab
[add] implement sbi fwft ecall emulation
Alignof Oct 3, 2024
e13e1f5
[fix] modify `hs_forward_exception` to forward stval value
Alignof Oct 5, 2024
ad42bc4
[update] make `hstrap_exit` public
Alignof Oct 5, 2024
2cb2742
[wip][add] add `pseudo_vs_exception`
Alignof Oct 5, 2024
a664b4a
[add] add `Context::sstatus`
Alignof Oct 6, 2024
f1d78e5
[fix] fix updating priv mode in `pseudo_vs_exception`
Alignof Oct 6, 2024
48643c8
[!][add] add `zicfiss` to `riscv,isa-extensions` in device tree
Alignof Oct 6, 2024
1dd26ad
[fix] fix handling global data in `zicfiss::instruction`
Alignof Oct 6, 2024
471610d
[add] add `CsrData`
Alignof Oct 6, 2024
99f88f3
[add] add Zicfiss CSRs emulation
Alignof Oct 6, 2024
7e71116
[!][update] remove `ShadowStack` struct (shadow stack is allocated by…
Alignof Oct 6, 2024
a1eac01
[wip][update] devide `Zicfiss.sse` into each priv level
Alignof Oct 8, 2024
9d8e7c7
[fix] fix FWFT SBI call
Alignof Oct 8, 2024
d5dbf83
[add] add `Zicfiss::is_ss_enable`
Alignof Oct 8, 2024
f5c9897
[fix] fix Zicfiss::is_ss_enable to prevent deadlock
Alignof Oct 8, 2024
0dd159f
[add] add `EmulateExtension` trait
Alignof Oct 8, 2024
66f353f
[add] add `hstateen0` module
Alignof Oct 9, 2024
112f64b
[wip][add] disable `ENVCFG` state via `xstateen0`
Alignof Oct 9, 2024
878a765
[add] add `csr_field` to `EmulateExtension`
Alignof Oct 9, 2024
80dfafc
[add] implement CSR field emulation (only supported envcfg for now)
Alignof Oct 9, 2024
0909e74
[fix] update mmu-type to sv39 in dts
Alignof Oct 9, 2024
36dcd62
[add] add `GuestVirtualAddress`
Alignof Oct 9, 2024
34e9b6f
[add] add `sv39::trans_addr`
Alignof Oct 9, 2024
7efb5fa
[add] add `Vsatp` CSR to h_extension module
Alignof Oct 10, 2024
37a3a7d
[add] add `PteFieldSv39`, `PteFieldSv39x4` trait
Alignof Oct 10, 2024
a4864a6
[add] add `AddressFieldSv39` and `AddressFieldSv39x4` trait
Alignof Oct 10, 2024
747554f
[add] add new level to `PageTableLevel`
Alignof Oct 10, 2024
ad4e2d6
[update] remove `FIRST_LV_PAGE_TABLE_LEN` from sv39.rs
Alignof Oct 10, 2024
59c3a00
[add] add sv57.rs
Alignof Oct 10, 2024
c0c6988
[add] add `vs_stage_trans_addr` and `g_stage_trans_addr`
Alignof Oct 10, 2024
7a0f8c2
[update] rename `hgatp::HgatpMode` to `hgatp::Mode`
Alignof Oct 10, 2024
76569a7
[update] add `ssp_hp_ptr` to convert ssp value to hypervisor physical…
Alignof Oct 10, 2024
86f58f0
[add] add `PageTableAddress::to_host_physical_ptr`
Alignof Oct 10, 2024
ad03547
[update] remove `entire_ppn` fro each PteField trait
Alignof Oct 10, 2024
bf2ea41
[update] change return type of `trans_addr` to `Result` to handle pag…
Alignof Oct 10, 2024
7c877d5
[fix] fix `pseudo_vs_exception` to disable interrupt in VS-mode handler
Alignof Oct 10, 2024
59ae015
[update] modify `ssp_hp_ptr` to handle VS-stage address translation f…
Alignof Oct 12, 2024
47b3630
[wip][update] allow write only leaf pte for Zicfiss
Alignof Oct 13, 2024
2f3f895
[update] change argument type of `EmulateExtension` to reference
Alignof Oct 13, 2024
f04665a
[wip][add] add `Context::update_sepc_by_inst`
Alignof Oct 13, 2024
4ec54c2
[add] add `update_sepc_by_htinst_value`
Alignof Oct 14, 2024
9256392
[add] add `exception/instruction_handler.rs`
Alignof Oct 14, 2024
ca4bcdc
[add] add exception/page_fault_handler.rs
Alignof Oct 14, 2024
5706bbb
[update] update version of `raki` crate
Alignof Oct 14, 2024
881a6b0
[add] add build instruction for Zicfiss guest image
Alignof Oct 14, 2024
1791f89
[update] update cargo runner command
Alignof Oct 14, 2024
ac8d24f
[refactor] apply `cargo clippy --fix`
Alignof Oct 14, 2024
a55968e
[refactor][update] remove result type from `sv39::trans_addr`
Alignof Oct 14, 2024
51b6184
[refactor][update] remove `StateEnField` and add `hstateen0::clear_en…
Alignof Oct 14, 2024
4f42e8d
[refactor] fix cargo clippy warnings
Alignof Oct 14, 2024
e883ec8
[doc] add doc comment to `FwftFeature`
Alignof Oct 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
[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
-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 +26,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.*
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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" }
raki = "1.2.0"
riscv = "0.11.1"
riscv-rt = "0.11.0"
rustsbi = { version = "0.4.0-alpha.1", features = ["machine"] }
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)
```
6 changes: 3 additions & 3 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\0zmmul\0za64rs\0zaamo\0zalrsc\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\0zicfiss\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_zmmul_za64rs_zaamo_zalrsc_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_ssccptr_sscounterenw_sstc_sstvala_sstvecd_svadu";
mmu-type = "riscv,sv57";
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,sv39";

interrupt-controller {
#interrupt-cells = <0x01>;
Expand Down
89 changes: 89 additions & 0 deletions src/emulate_extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Extension emulation

pub mod zicfiss;

use crate::h_extension::csrs::vstvec;
use crate::trap::hypervisor_supervisor::hstrap_exit;
use crate::HYPERVISOR_DATA;

use core::arch::asm;
use raki::Instruction;
use riscv::register::sstatus;

/// Trait for extention emulation.
pub trait EmulateExtension {
/// Emulate instruction
fn instruction(&mut self, inst: &Instruction);
/// Emulate CSR
fn csr(&mut self, inst: &Instruction);
/// Emulate CSR field that already exists.
fn csr_field(&mut self, inst: &Instruction, write_to_csr_value: u64, read_csr_value: &mut u64);
}

/// CSR data for CSRs emulation.
pub struct CsrData(u64);

impl CsrData {
/// Return raw data.
pub fn bits(&self) -> u64 {
self.0
}

/// Write data to CSR.
/// For CSRRW or CSRRWI
pub fn write(&mut self, data: u64) {
self.0 = data;
}

/// Set bit in CSR.
/// For CSRRS or CSRRSI
pub fn set(&mut self, mask: u64) {
self.0 |= mask;
}

/// Clear bit in CSR.
/// For CSRRC or CSRRCI
pub fn clear(&mut self, mask: u64) {
self.0 &= !mask;
}
}

/// Throw an VS-level exception.
/// * `exception_num`: Exception number. (store to vscause)
/// * `trap_value`: Trap value. (store to vstval)
pub fn pseudo_vs_exception(exception_num: usize, trap_value: usize) -> ! {
unsafe {
let hypervisor_data = HYPERVISOR_DATA.lock();
let mut context = hypervisor_data.get().unwrap().guest().context;
asm!(
"csrw vsepc, {sepc}",
"csrw vscause, {cause}",
"csrw vstval, {tval}",
sepc = in(reg) context.sepc(),
cause = in(reg) exception_num,
tval = in(reg) trap_value,
);

let spp = sstatus::read().spp();
let vsstatus: usize;
asm!("csrr {status}, vsstatus", status = out(reg) vsstatus);
let sie = vsstatus >> 1 & 0x1;
asm!(
"csrw vsstatus, {status}",
status = in(reg) (vsstatus & !(1 << 8)) | (spp as usize) << 8
);
// disable interrupt
asm!(
"csrs vsstatus, {status}",
"csrci vsstatus, 0b10",
status = in(reg) sie << 5,
);
context.set_sstatus(context.sstatus() | 1 << 8);

context.set_sepc(vstvec::read().bits());

drop(hypervisor_data);

hstrap_exit();
}
}
Loading
Loading