use ax_memory_addr::PhysAddr;
use super::paging::{PWCH_VALUE, PWCL_VALUE};
pub fn init_trap() {
unsafe {
unsafe extern "C" {
fn exception_entry_base();
}
core::arch::asm!(include_asm_macros!(), "csrwr $r0, KSAVE_KSP");
crate::asm::write_exception_entry_base(exception_entry_base as *const () as usize);
}
}
pub fn boot_vector() -> usize {
super::entry::boot::vector()
}
pub unsafe fn install_boot_vectors(vector: usize, refill: PhysAddr) {
unsafe {
core::arch::asm!(
"csrrd {ecfg}, 0x4", "bstrins.d {ecfg}, {spacing}, 18, 16",
"csrwr {ecfg}, 0x4", "csrwr {vector}, 0xc", "csrwr {refill}, 0x88",
ecfg = out(reg) _, spacing = in(reg) 7usize,
vector = in(reg) vector, refill = in(reg) refill.as_usize(), options(nostack),
);
}
}
#[inline(always)]
pub unsafe fn install_boot_page_table(root: PhysAddr) {
unsafe {
core::arch::asm!(
"csrrd {stlbps}, 0x1e", "bstrins.d {stlbps}, {ps}, 5, 0",
"csrwr {root}, 0x1a", "csrwr {root}, 0x19", "dbar 0",
"csrwr {stlbps}, 0x1e", "csrwr {pwcl}, 0x1c", "csrwr {pwch}, 0x1d",
"invtlb 0x0, $r0, $r0",
stlbps = out(reg) _, ps = in(reg) 12usize, root = in(reg) root.as_usize(),
pwcl = in(reg) PWCL_VALUE as usize, pwch = in(reg) PWCH_VALUE as usize,
options(nostack),
);
}
}
#[inline(always)]
pub unsafe fn enable_paged_translation() {
let mut crmd: usize;
unsafe {
core::arch::asm!("csrrd {}, 0", out(reg) crmd, options(nostack));
crmd = (crmd & !((1 << 3) | (3 << 5) | (3 << 7))) | (1 << 4) | (1 << 5) | (1 << 7);
core::arch::asm!("csrwr {}, 0", in(reg) crmd, options(nostack));
}
}
#[unsafe(naked)]
pub unsafe extern "C" fn jump_to(
_argument: usize,
_stack: crate::VirtAddr,
_entry: crate::VirtAddr,
) -> ! {
core::arch::naked_asm!("ibar 0", "dbar 0", "move $sp, $a1", "jr $a2");
}