started proper memory page handling with mapped page style
This commit is contained in:
+1
-1
@@ -21,7 +21,7 @@ name = "stack_overflow"
|
||||
harness = false
|
||||
|
||||
[dependencies]
|
||||
bootloader = "0.9"
|
||||
bootloader = {version = "0.9", features = ["map_physical_memory"]}
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
x86_64 = "0.14.2"
|
||||
|
||||
+8
-2
@@ -7,11 +7,18 @@
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[cfg(test)]
|
||||
use bootloader::{BootInfo, entry_point};
|
||||
|
||||
pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
pub mod serial;
|
||||
pub mod vga_buffer;
|
||||
|
||||
#[cfg(test)]
|
||||
entry_point!(test_kernel_main);
|
||||
|
||||
pub fn init() {
|
||||
interrupts::init_idt();
|
||||
gdt::init();
|
||||
@@ -64,8 +71,7 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! {
|
||||
// this this is here cuz lib.rs built separately, so need test for lib functions as well
|
||||
/// Entry point for `cargo test`
|
||||
#[cfg(test)]
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
||||
init();
|
||||
test_main();
|
||||
hlt_loop();
|
||||
|
||||
+27
-9
@@ -5,21 +5,39 @@
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use rusty_os::println;
|
||||
use rusty_os::{memory, println};
|
||||
|
||||
#[unsafe(no_mangle)] // dont mangle name of this function, keep as-is
|
||||
pub extern "C" fn _start() -> ! {
|
||||
use bootloader::{BootInfo, entry_point};
|
||||
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
println!("Hello there {}!", "Sameed");
|
||||
|
||||
rusty_os::init();
|
||||
|
||||
use x86_64::registers::control::Cr3;
|
||||
use x86_64::{VirtAddr, structures::paging::Translate};
|
||||
|
||||
let (level_4_page_table, _) = Cr3::read();
|
||||
println!(
|
||||
"Level 4 page table at: {:?}",
|
||||
level_4_page_table.start_address()
|
||||
);
|
||||
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||
// initialize a memory mapper
|
||||
let mapper = unsafe { memory::init(phys_mem_offset) };
|
||||
|
||||
let addresses = [
|
||||
// the identity-mapped vga buffer page
|
||||
0xb8000,
|
||||
// some code page
|
||||
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);
|
||||
let phys = mapper.translate_addr(virt);
|
||||
println!("{:?} -> {:?}", virt, phys);
|
||||
}
|
||||
|
||||
// if im runnign tests, jump to the test_main entrypoint
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
use x86_64::structures::paging::OffsetPageTable;
|
||||
use x86_64::{VirtAddr, structures::paging::PageTable};
|
||||
|
||||
/// 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> {
|
||||
unsafe {
|
||||
let level_4_table = active_level_4_table(physical_memory_offset);
|
||||
OffsetPageTable::new(level_4_table, physical_memory_offset)
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
unsafe { &mut *page_table_ptr }
|
||||
}
|
||||
Reference in New Issue
Block a user