1
0
mirror of https://github.com/sgmarz/osblog.git synced 2024-11-24 02:16:19 +04:00

Changed SATP fence functions

This commit is contained in:
Stephen Marz 2019-10-25 18:39:59 -04:00
parent 7e3d97b908
commit b95aacadb4
3 changed files with 19 additions and 6 deletions

View File

@ -37,12 +37,13 @@ m_trap_vector:
csrrw t6, mscratch, t6 csrrw t6, mscratch, t6
# csrrw will atomically swap t6 into mscratch and the old # csrrw will atomically swap t6 into mscratch and the old
# value of mscratch into t6. This is nice because we just # value of mscratch into t6. This is nice because we just
# switched values and didn't destroy any value. # switched values and didn't destroy anything -- all atomically!
# in cpu.rs we have a structure of: # in cpu.rs we have a structure of:
# 32 gp regs 0 # 32 gp regs 0
# 32 fp regs 256 # 32 fp regs 256
# SATP register 512 # SATP register 512
# Trap stack 520 # Trap stack 520
# CPU HARTID 528
# We use t6 as the temporary register because it is the very # We use t6 as the temporary register because it is the very
# bottom register (x31) # bottom register (x31)
.set i, 1 .set i, 1

View File

@ -143,9 +143,14 @@ pub fn satp_read() -> usize {
} }
} }
pub fn satp_fence() { pub fn satp_fence(vaddr: usize, asid: usize) {
unsafe { unsafe {
asm!("sfence.vma"); asm!("sfence.vma $0, $1" :: "r"(vaddr), "r"(asid));
} }
} }
pub fn satp_fence_asid(asid: usize) {
unsafe {
asm!("sfence.vma zero, $0" :: "r"(asid));
}
}

View File

@ -159,6 +159,7 @@ extern "C" fn kinit() {
kheap_head + total_pages * page::PAGE_SIZE, kheap_head + total_pages * page::PAGE_SIZE,
page::EntryBits::ReadWrite.val(), page::EntryBits::ReadWrite.val(),
); );
// Using statics is inherently unsafe.
unsafe { unsafe {
// Map heap descriptors // Map heap descriptors
let num_pages = HEAP_SIZE / page::PAGE_SIZE; let num_pages = HEAP_SIZE / page::PAGE_SIZE;
@ -237,7 +238,6 @@ extern "C" fn kinit() {
0x0c20_8000, 0x0c20_8000,
page::EntryBits::ReadWrite.val(), page::EntryBits::ReadWrite.val(),
); );
page::print_page_allocations();
// When we return from here, we'll go back to boot.S and switch into // When we return from here, we'll go back to boot.S and switch into
// supervisor mode We will return the SATP register to be written when // supervisor mode We will return the SATP register to be written when
// we return. root_u is the root page table's address. When stored into // we return. root_u is the root page table's address. When stored into
@ -247,6 +247,7 @@ extern "C" fn kinit() {
// 0 = Bare (no translation) // 0 = Bare (no translation)
// 8 = Sv39 // 8 = Sv39
// 9 = Sv48 // 9 = Sv48
// build_satp has these parameters: mode, asid, page table address.
let satp_value = cpu::build_satp(8, 0, root_u); let satp_value = cpu::build_satp(8, 0, root_u);
unsafe { unsafe {
// We have to store the kernel's table. The tables will be moved // We have to store the kernel's table. The tables will be moved
@ -259,7 +260,10 @@ extern "C" fn kinit() {
); );
cpu::sscratch_write(cpu::mscratch_read()); cpu::sscratch_write(cpu::mscratch_read());
cpu::KERNEL_TRAP_FRAME[0].satp = satp_value; cpu::KERNEL_TRAP_FRAME[0].satp = satp_value;
// Move the stack pointer to the very bottom. // Move the stack pointer to the very bottom. The stack is actually in a
// non-mapped page. The stack is decrement-before push and increment after
// pop. Therefore, the stack will be allocated (decremented)
// before it is stored.
cpu::KERNEL_TRAP_FRAME[0].trap_stack = page::zalloc(1).add(page::PAGE_SIZE); cpu::KERNEL_TRAP_FRAME[0].trap_stack = page::zalloc(1).add(page::PAGE_SIZE);
id_map_range( id_map_range(
&mut root, &mut root,
@ -267,12 +271,14 @@ extern "C" fn kinit() {
cpu::KERNEL_TRAP_FRAME[0].trap_stack as usize, cpu::KERNEL_TRAP_FRAME[0].trap_stack as usize,
page::EntryBits::ReadWrite.val(), page::EntryBits::ReadWrite.val(),
); );
// The trap frame itself is stored in the mscratch register.
id_map_range( id_map_range(
&mut root, &mut root,
cpu::mscratch_read(), cpu::mscratch_read(),
cpu::mscratch_read() + core::mem::size_of::<cpu::KernelTrapFrame>(), cpu::mscratch_read() + core::mem::size_of::<cpu::KernelTrapFrame>(),
page::EntryBits::ReadWrite.val(), page::EntryBits::ReadWrite.val(),
); );
page::print_page_allocations();
let p = cpu::KERNEL_TRAP_FRAME[0].trap_stack as usize - 1; let p = cpu::KERNEL_TRAP_FRAME[0].trap_stack as usize - 1;
let m = page::virt_to_phys(&root, p).unwrap_or(0); let m = page::virt_to_phys(&root, p).unwrap_or(0);
println!("Walk 0x{:x} = 0x{:x}", p, m); println!("Walk 0x{:x} = 0x{:x}", p, m);
@ -285,7 +291,7 @@ extern "C" fn kinit() {
println!("Setting 0x{:x}", satp_value); println!("Setting 0x{:x}", satp_value);
println!("Scratch reg = 0x{:x}", cpu::mscratch_read()); println!("Scratch reg = 0x{:x}", cpu::mscratch_read());
cpu::satp_write(satp_value); cpu::satp_write(satp_value);
cpu::satp_fence(); cpu::satp_fence_asid(0);
} }
#[no_mangle] #[no_mangle]
@ -304,6 +310,7 @@ extern "C" fn kinit_hart(hartid: usize) {
// register. // register.
cpu::sscratch_write(cpu::mscratch_read()); cpu::sscratch_write(cpu::mscratch_read());
cpu::KERNEL_TRAP_FRAME[hartid].hartid = hartid; cpu::KERNEL_TRAP_FRAME[hartid].hartid = hartid;
// We can't do the following until zalloc() is locked, but we don't have locks, yet :(
// cpu::KERNEL_TRAP_FRAME[hartid].satp = cpu::KERNEL_TRAP_FRAME[0].satp; // cpu::KERNEL_TRAP_FRAME[hartid].satp = cpu::KERNEL_TRAP_FRAME[0].satp;
// cpu::KERNEL_TRAP_FRAME[hartid].trap_stack = page::zalloc(1); // cpu::KERNEL_TRAP_FRAME[hartid].trap_stack = page::zalloc(1);
} }