Introduction to Paging

This commit is contained in:
Sainnhe Park 2022-01-12 16:54:57 +08:00
parent 6f67926a75
commit c3e1897c23
2 changed files with 22 additions and 1 deletions

View File

@ -1,9 +1,10 @@
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
use crate::{println, print}; use crate::{println, print};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use crate::gdt; use crate::gdt;
use pic8259::ChainedPics; use pic8259::ChainedPics;
use spin; use spin;
use crate::hlt_loop;
lazy_static! { lazy_static! {
static ref IDT: InterruptDescriptorTable = { static ref IDT: InterruptDescriptorTable = {
@ -17,6 +18,7 @@ lazy_static! {
.set_handler_fn(timer_interrupt_handler); .set_handler_fn(timer_interrupt_handler);
idt[InterruptIndex::Keyboard.as_usize()] idt[InterruptIndex::Keyboard.as_usize()]
.set_handler_fn(keyboard_interrupt_handler); .set_handler_fn(keyboard_interrupt_handler);
idt.page_fault.set_handler_fn(page_fault_handler);
idt idt
}; };
} }
@ -89,3 +91,12 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
.notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8()); .notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
} }
} }
extern "x86-interrupt" fn page_fault_handler(stack_frame: InterruptStackFrame, error_code: PageFaultErrorCode) {
use x86_64::registers::control::Cr2;
println!("EXCEPTION: PAGE FAULT");
println!("Accessed Address: {:?}", Cr2::read());
println!("Error Code: {:?}", error_code);
println!("{:#?}", stack_frame);
hlt_loop();
}

View File

@ -16,6 +16,16 @@ pub extern "C" fn _start() -> ! {
anos::init(); anos::init();
let ptr = 0x2031b2 as *mut u32;
unsafe { let x = *ptr; }
println!("read worked");
unsafe { *ptr = 42 };
println!("write worked");
use x86_64::registers::control::Cr3;
let (level_4_page_table, _) = Cr3::read();
println!("Level 4 page table at: {:?}", level_4_page_table.start_address());
// 触发一个中断 // 触发一个中断
x86_64::instructions::interrupts::int3(); x86_64::instructions::interrupts::int3();
println!("It didn't crash!"); println!("It didn't crash!");