Skip to content

Commit

Permalink
WIP: RISC-V paging
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Maslowski <[email protected]>
  • Loading branch information
orangecms committed Nov 24, 2023
1 parent 40f9b81 commit 4e327e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
22 changes: 13 additions & 9 deletions riscv64/src/l.S
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ start:
// Volume II: RISC-V Privileged Architectures V20211203 p75
// SXLEN 64, MODE 8: Sv39 aka Page-based 39-bit virtual addressing
lui t0, %hi(boot_page_table)
// page size is 4k (2^12) - drop lowest 12 bits
srli t4, t0, 12

// subtract offset to get the physical address of the root page table
li t1, 0xffffffffc0000000 - 0x80000000
// physical address of root page table
sub t3, t0, t1
srli t0, t3, 12 // page size 4k (2^12)
li t1, 8 << 60 // mode Sv39
or t0, t0, t1
sub t2, t0, t1

// last page: 0xffffffff_c0000000 -> 0x80000000 (1G)
// page size is 4k (2^12) - drop lowest 12 bits
srli t3, t2, 12
// final SATP
li t1, 8 << 60 // mode Sv39
or t0, t3, t1

slli t2, t3, 28
ori t2, t2, 0xcf // VRWXAD
sw t2, 8*255(t3) // #255
// t3 holds the root page table physical address divided by 4k; shift for flags
slli t1, t4, 10
ori t1, t4, 0xc1 // DAGU XWRV
sd t1, 8*255(t2) // #255

sfence.vma
csrw satp, t0
Expand Down
29 changes: 24 additions & 5 deletions riscv64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ impl PageTable {

pub fn get_entry(&self, at: u16) -> PageTableEntry {
let addr = self.addr + (at as u64 * Self::ENTRY_SIZE);
let val = unsafe { read_volatile(addr as *const u64) };
val.into()
let high = unsafe { read_volatile((addr + 4) as *const u64) };
let low = unsafe { read_volatile(addr as *const u64) };
((high << 32) | low).into()
}

pub fn create_entry(&self) {}
Expand Down Expand Up @@ -330,15 +331,33 @@ pub extern "C" fn main9(hartid: usize, dtb_ptr: u64) -> ! {
println!(" 0 {:?}", bpt.get_entry(0));
println!(" 1 {:?}", bpt.get_entry(1));
println!(" 2 {:?}", bpt.get_entry(2));
let x1 = read32((bpt_addr + 255 * 8 + 4).try_into().unwrap());
let x2 = read32((bpt_addr + 255 * 8).try_into().unwrap());
println!("{x1:12x}{x2:12x}");
println!();
println!();

let hi = read32((bpt_addr + 255 * 8 + 4).try_into().unwrap());
let lo = read32((bpt_addr + 255 * 8).try_into().unwrap());
println!(" {hi:08x} {lo:08x}");
println!("255 {:?}", bpt.get_entry(255));

println!();
println!();
println!("508 {:?}", bpt.get_entry(508));
println!("509 {:?}", bpt.get_entry(509));
println!("510 {:?}", bpt.get_entry(510));
println!("511 {:?}", bpt.get_entry(511));
println!();
println!();

// fixed 25 bits, used 39 bits
const VFIXED: usize = 0xff_ff_ff_80__00_00_00_00;
let ppn2 = 255 << (9 + 9 + 12);
let ppn1 = 255 << (9 + 12);
let ppn0 = 255 << 12;
let poff = 0x0;
let vaddr = VFIXED | ppn2 | ppn1 | ppn0 | poff;
println!("{vaddr:016x}");
let x1 = read32(vaddr);
println!("{x1:08x}");
#[cfg(not(test))]
sbi::shutdown();
#[cfg(test)]
Expand Down

0 comments on commit 4e327e9

Please sign in to comment.