Add virtual memory translation
This commit is contained in:
parent
21224d62e2
commit
46ce73b1b0
@ -24,6 +24,7 @@ use x86_64::instructions::bochs_breakpoint;
|
|||||||
|
|
||||||
pub mod gdt;
|
pub mod gdt;
|
||||||
pub mod interrupts;
|
pub mod interrupts;
|
||||||
|
pub mod memory;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
|
40
src/main.rs
40
src/main.rs
@ -41,7 +41,7 @@ fn panic(info: &PanicInfo) -> ! {
|
|||||||
entry_point!(kernel_main);
|
entry_point!(kernel_main);
|
||||||
|
|
||||||
// Kernel entry point
|
// Kernel entry point
|
||||||
fn kernel_main(_boot_info: &'static BootInfo) -> ! {
|
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||||
// TODO: Take care of cpuid stuff and set-up all floating point exetnsions
|
// TODO: Take care of cpuid stuff and set-up all floating point exetnsions
|
||||||
// TODO: We may also need to enable debug registers ?
|
// TODO: We may also need to enable debug registers ?
|
||||||
|
|
||||||
@ -55,19 +55,33 @@ fn kernel_main(_boot_info: &'static BootInfo) -> ! {
|
|||||||
|
|
||||||
x86_64::instructions::interrupts::int3();
|
x86_64::instructions::interrupts::int3();
|
||||||
|
|
||||||
use x86_64::registers::control::Cr3;
|
use dendrobates_tinctoreus_azureus::memory;
|
||||||
|
use x86_64::VirtAddr;
|
||||||
|
use x86_64::structures::paging::{PageTable,MapperAllSizes};
|
||||||
|
|
||||||
let (level_4_page_table, flags) = Cr3::read();
|
|
||||||
println!(
|
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||||
"Level 4 page table at: {:?}, flags {:?}",
|
// new: initialize a mapper
|
||||||
level_4_page_table.start_address(),
|
let mapper = unsafe { memory::init(phys_mem_offset) };
|
||||||
flags
|
|
||||||
);
|
let addresses = [
|
||||||
serial_println!(
|
// the identity-mapped vga buffer page
|
||||||
"Level 4 page table at: {:?}, flags {:?}",
|
0xb8000,
|
||||||
level_4_page_table.start_address(),
|
// some code page
|
||||||
flags
|
0x201008,
|
||||||
);
|
// some stack page
|
||||||
|
0x0100_0020_1a10,
|
||||||
|
// virtual address mapped to physical address 0
|
||||||
|
boot_info.physical_memory_offset,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
for &address in &addresses {
|
||||||
|
let virt = VirtAddr::new(address);
|
||||||
|
// new: use the `mapper.translate_addr` method
|
||||||
|
let phys = mapper.translate_addr(virt);
|
||||||
|
serial_println!("{:?} -> {:?}", virt, phys);
|
||||||
|
}
|
||||||
|
|
||||||
serial_println!("Preparing nasty fault...");
|
serial_println!("Preparing nasty fault...");
|
||||||
unsafe {
|
unsafe {
|
||||||
|
33
src/memory.rs
Normal file
33
src/memory.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
use x86_64::{structures::paging::{PageTable,OffsetPageTable}, VirtAddr, PhysAddr};
|
||||||
|
|
||||||
|
|
||||||
|
/// Initialize a new OffsetPageTable.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the
|
||||||
|
/// complete physical memory is mapped to virtual memory at the passed
|
||||||
|
/// `physical_memory_offset`. Also, this function must be only called once
|
||||||
|
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||||
|
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
|
||||||
|
let level_4_table = active_level_4_table(physical_memory_offset);
|
||||||
|
OffsetPageTable::new(level_4_table, physical_memory_offset)
|
||||||
|
}
|
||||||
|
/// Returns a mutable reference to the active level 4 table.
|
||||||
|
///
|
||||||
|
/// This function is unsafe because the caller must guarantee that the
|
||||||
|
/// complete physical memory is mapped to virtual memory at the passed
|
||||||
|
/// `physical_memory_offset`. Also, this function must be only called once
|
||||||
|
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||||
|
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr)
|
||||||
|
-> &'static mut PageTable
|
||||||
|
{
|
||||||
|
use x86_64::registers::control::Cr3;
|
||||||
|
|
||||||
|
let (level_4_table_frame, _) = Cr3::read();
|
||||||
|
|
||||||
|
let phys = level_4_table_frame.start_address();
|
||||||
|
let virt = physical_memory_offset + phys.as_u64();
|
||||||
|
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
|
||||||
|
|
||||||
|
&mut *page_table_ptr // unsafe
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user