From c3e1897c23cbea927cde047c296d913d7878da54 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 12 Jan 2022 16:54:57 +0800 Subject: [PATCH] Introduction to Paging --- src/interrupts.rs | 13 ++++++++++++- src/main.rs | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/interrupts.rs b/src/interrupts.rs index 870d8d3..652888c 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,9 +1,10 @@ -use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode}; use crate::{println, print}; use lazy_static::lazy_static; use crate::gdt; use pic8259::ChainedPics; use spin; +use crate::hlt_loop; lazy_static! { static ref IDT: InterruptDescriptorTable = { @@ -17,6 +18,7 @@ lazy_static! { .set_handler_fn(timer_interrupt_handler); idt[InterruptIndex::Keyboard.as_usize()] .set_handler_fn(keyboard_interrupt_handler); + idt.page_fault.set_handler_fn(page_fault_handler); idt }; } @@ -89,3 +91,12 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac .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(); +} diff --git a/src/main.rs b/src/main.rs index 693740f..def8cbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,16 @@ pub extern "C" fn _start() -> ! { 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(); println!("It didn't crash!");