Skip to content

Commit

Permalink
remove sbi legacy && use sbi-rt && update README
Browse files Browse the repository at this point in the history
  • Loading branch information
KuangjuX committed Feb 6, 2023
1 parent d5dde60 commit 84cca2c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ make qemu
- [x] Shadow page tables
- [x] Synchronize guest page table & shadow page table
- [x] Foward Expections & Interrupts
- [x] Update PTE accessed and dirty bits
- [x] Timers
- [ ] Serial IO emulate
- [ ] Expose and/or emulate peripherals
- [ ] passthrough virtio block and networkd devices
- [ ] multicore supported
Expand Down
81 changes: 0 additions & 81 deletions src/boards/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,4 @@ pub const MMIO: &[(usize, usize)] = &[
(0x0010_0000, 0x00_2000), // VIRT_TEST/RTC in virt machine
];

pub const QEMU_VIRT_START: usize = 0x0010_0000;
pub const QEMU_VIRT_SIZE: usize = 0x00_2000;

//ref:: https://github.com/andre-richter/qemu-exit
use core::arch::asm;

const EXIT_SUCCESS: u32 = 0x5555; // Equals `exit(0)`. qemu successful exit

const EXIT_FAILURE_FLAG: u32 = 0x3333;
const EXIT_FAILURE: u32 = exit_code_encode(1); // Equals `exit(1)`. qemu failed exit
const EXIT_RESET: u32 = 0x7777; // qemu reset

pub trait QEMUExit {
/// Exit with specified return code.
///
/// Note: For `X86`, code is binary-OR'ed with `0x1` inside QEMU.
fn exit(&self, code: u32) -> !;

/// Exit QEMU using `EXIT_SUCCESS`, aka `0`, if possible.
///
/// Note: Not possible for `X86`.
fn exit_success(&self) -> !;

/// Exit QEMU using `EXIT_FAILURE`, aka `1`.
fn exit_failure(&self) -> !;
}

/// RISCV64 configuration
pub struct RISCV64 {
/// Address of the sifive_test mapped device.
addr: u64,
}

/// Encode the exit code using EXIT_FAILURE_FLAG.
const fn exit_code_encode(code: u32) -> u32 {
(code << 16) | EXIT_FAILURE_FLAG
}

impl RISCV64 {
/// Create an instance.
pub const fn new(addr: u64) -> Self {
RISCV64 { addr }
}
}

impl QEMUExit for RISCV64 {
/// Exit qemu with specified exit code.
fn exit(&self, code: u32) -> ! {
// If code is not a special value, we need to encode it with EXIT_FAILURE_FLAG.
let code_new = match code {
EXIT_SUCCESS | EXIT_FAILURE | EXIT_RESET => code,
_ => exit_code_encode(code),
};

unsafe {
asm!(
"sw {0}, 0({1})",
in(reg)code_new, in(reg)self.addr
);

// For the case that the QEMU exit attempt did not work, transition into an infinite
// loop. Calling `panic!()` here is unfeasible, since there is a good chance
// this function here is the last expression in the `panic!()` handler
// itself. This prevents a possible infinite loop.
loop {
asm!("wfi", options(nomem, nostack));
}
}
}

fn exit_success(&self) -> ! {
self.exit(EXIT_SUCCESS);
}

fn exit_failure(&self) -> ! {
self.exit(EXIT_FAILURE);
}
}

const VIRT_TEST: u64 = 0x100000;

pub const QEMU_EXIT_HANDLE: RISCV64 = RISCV64::new(VIRT_TEST);
3 changes: 0 additions & 3 deletions src/guest/pmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ pub fn page_table_mode<P: PageTable>(page_table: P, hart_id: usize) -> PageTable
}


fn translate_addr<R: Fn(usize) -> Option<usize>>() -> Option<usize> {
unimplemented!()
}

fn update_pte_readonly<P: PageTable>(vpn: VirtPageNum, spt: &mut P) -> bool {
if let Some(pte) = spt.find_pte(vpn) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod mm;

use constants::layout::PAGE_SIZE;

use crate::{guest::{GuestKernel, GUEST_KERNEL_MANAGER, run_guest_kernel}};
use crate::guest::{GuestKernel, GUEST_KERNEL_MANAGER, run_guest_kernel};
use crate::mm::MemorySet;

#[link_section = ".initrd"]
Expand Down
6 changes: 4 additions & 2 deletions src/sbi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const SBI_REMOTE_SFENCE_VMA: usize = 6;
const SBI_REMOTE_SFENCE_VMA_ASID: usize = 7;
const SBI_SHUTDOWN: usize = 8;


#[inline(always)]
/// general sbi call
fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
Expand Down Expand Up @@ -44,8 +45,9 @@ pub fn set_timer(stime: usize) {
sbi_rt::set_timer(stime as u64);
}

use crate::board::QEMUExit;
/// use sbi call to shutdown the kernel
pub fn shutdown() -> ! {
crate::board::QEMU_EXIT_HANDLE.exit_failure();
// crate::board::QEMU_EXIT_HANDLE.exit_failure();
sbi_rt::system_reset(sbi_rt::Shutdown, sbi_rt::SystemFailure);
unreachable!()
}

0 comments on commit 84cca2c

Please sign in to comment.