changed page allocation to happen on page fault for performance reasons

This commit is contained in:
2025-02-27 23:57:23 +00:00
parent 2915d0c879
commit 192100be7a
9 changed files with 144 additions and 61 deletions
+39 -11
View File
@@ -1,7 +1,10 @@
use libk::drivers::memory::{FRAME_ALLOCATOR, OFFSET_PAGE_TABLE};
use libk::prelude::*;
use pic8259::ChainedPics;
use x86_64::registers::control::Cr2;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
use x86_64::structures::paging::mapper::MapperFlushAll;
use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB};
use spin::{Lazy, Mutex};
@@ -52,12 +55,19 @@ impl InterruptIndex {
pub fn init_idt() {
IDT.load();
// enable_apic();
// TODO: fix apic
// unsafe {
// PICS.lock().initialize();
// PICS.lock().write_masks(0xfc, 0xff);
// }
}
pub fn enable_pic() {
unsafe {
PICS.lock().initialize();
PICS.lock().write_masks(0xfc, 0xff);
}
}
pub fn disable_pic() {
unsafe {
PICS.lock().disable();
}
}
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
@@ -118,10 +128,28 @@ extern "x86-interrupt" fn page_fault_handler(
stack_frame: InterruptStackFrame,
error_code: PageFaultErrorCode,
) {
serial_println!("Exception: Page Fault");
serial_println!("Accessed Address: {:?}", Cr2::read());
serial_println!("Error Code: {:?}", error_code);
serial_println!("{:#?}", stack_frame);
// serial_println!("Exception: Page Fault");
// serial_println!("Accessed Address: {:?}", Cr2::read());
// serial_println!("Error Code: {:?}", error_code);
// serial_println!("{:#?}", stack_frame);
panic!("page fault");
if let Some(frame_allocator) = FRAME_ALLOCATOR.get() {
let mut f = frame_allocator.lock();
let frame = f.allocate_frame().unwrap();
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
let page: Page<Size4KiB> = Page::containing_address(Cr2::read().unwrap());
unsafe {
let mut mapper = OFFSET_PAGE_TABLE.get().unwrap().lock();
match mapper.map_to(page, frame, flags, &mut *f) {
Ok(_) => {}
Err(why) => panic!("failed to map page: {:?}", why),
}
}
MapperFlushAll::new().flush_all();
} else {
panic!("failed to get frame allocator");
}
}