Skip to content
Snippets Groups Projects
Commit cd4fb562 authored by Eemeli Lehtonen's avatar Eemeli Lehtonen
Browse files

page fault handler

parent 53649cea
No related branches found
No related tags found
No related merge requests found
use crate::{debug, info}; use crate::{debug, error, info};
use spin::Lazy; use spin::Lazy;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; use x86_64::{
registers::control::Cr2,
structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode},
};
// //
...@@ -67,16 +70,28 @@ extern "x86-interrupt" fn double_fault(stack: InterruptStackFrame, ec: u64) -> ! ...@@ -67,16 +70,28 @@ extern "x86-interrupt" fn double_fault(stack: InterruptStackFrame, ec: u64) -> !
panic!(); panic!();
} }
extern "x86-interrupt" fn page_fault(stack: InterruptStackFrame, ec: PageFaultErrorCode) {
let addr = Cr2::read();
error!("INT: Page fault\nAddress: {addr:?}\nErrorCode: {ec:?}\n{stack:#?}");
panic!();
}
// //
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| { static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
let mut idt = InterruptDescriptorTable::new(); let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint); idt.breakpoint.set_handler_fn(breakpoint);
let opt = idt.double_fault.set_handler_fn(double_fault);
unsafe { unsafe {
idt.double_fault opt.set_stack_index(DOUBLE_FAULT_IST);
.set_handler_fn(double_fault)
.set_stack_index(DOUBLE_FAULT_IST);
} }
idt.page_fault.set_handler_fn(page_fault);
idt idt
}); });
......
use limine::{LimineKernelAddressRequest, LimineKernelAddressResponse};
use spin::Lazy;
use x86_64::{PhysAddr, VirtAddr};
//
pub fn phys_addr() -> PhysAddr {
PhysAddr::new(KERNEL_ADDR.physical_base)
}
pub fn virt_addr() -> VirtAddr {
VirtAddr::new(KERNEL_ADDR.virtual_base)
}
//
static KERNEL_ADDR: Lazy<&'static LimineKernelAddressResponse> = Lazy::new(|| {
static REQ: LimineKernelAddressRequest = LimineKernelAddressRequest::new(0);
REQ.get_response().get().unwrap()
});
use crate::{debug, mem::map::Memmap}; use crate::mem::map::Memmap;
use limine::{LimineMemmapEntry, LimineMemmapRequest, LimineMemoryMapEntryType, NonNullPtr}; use limine::{LimineMemmapEntry, LimineMemmapRequest, LimineMemoryMapEntryType, NonNullPtr};
use spin::Lazy; use spin::Lazy;
......
...@@ -3,12 +3,15 @@ use crate::{arch, kernel_main}; ...@@ -3,12 +3,15 @@ use crate::{arch, kernel_main};
// //
pub use addr::phys_addr;
pub use addr::virt_addr;
pub use mem::memmap; pub use mem::memmap;
pub use mem::memtotal; pub use mem::memtotal;
pub use term::_print; pub use term::_print;
// //
mod addr;
mod cmdline; mod cmdline;
mod framebuffer; mod framebuffer;
mod mem; mod mem;
......
...@@ -14,6 +14,12 @@ ...@@ -14,6 +14,12 @@
// //
use x86_64::{
registers::control::Cr3,
structures::paging::{PageTable, PhysFrame},
VirtAddr,
};
extern crate alloc; extern crate alloc;
// //
...@@ -50,8 +56,46 @@ fn kernel_main() -> ! { ...@@ -50,8 +56,46 @@ fn kernel_main() -> ! {
debug!("Entering kernel_main"); debug!("Entering kernel_main");
debug!("Cmdline: {:?}", env::Arguments::get()); debug!("Cmdline: {:?}", env::Arguments::get());
debug!(
"Kernel addr: {:?} {:?}",
boot::virt_addr(),
boot::phys_addr()
);
mem::init(); mem::init();
/* let (l4, _) = Cr3::read();
let read_pt = |frame: PhysFrame| -> &mut PageTable {
let addr = VirtAddr::new(frame.start_address().as_u64());
let table: *mut PageTable = addr.as_mut_ptr();
unsafe { &mut *table }
};
for (i, e) in read_pt(l4).iter().enumerate() {
if !e.is_unused() {
println!("L4 entry {i}: {e:?}");
for (i, e) in read_pt(e.frame().unwrap()).iter().enumerate() {
if !e.is_unused() {
println!(" L3 entry {i}: {e:?}");
for (i, e) in read_pt(e.frame().unwrap()).iter().enumerate() {
if !e.is_unused() {
println!(" L2 entry {i}: {e:?}");
for (i, e) in read_pt(e.frame().unwrap()).iter().enumerate() {
if !e.is_unused() {
println!(" L1 entry {i}: {e:?}");
}
}
}
}
}
}
}
} */
// ofc. every kernel has to have this cringy ascii name splash // ofc. every kernel has to have this cringy ascii name splash
info!("\n{}\n", include_str!("./splash")); info!("\n{}\n", include_str!("./splash"));
......
use super::map::Memmap; use super::map::Memmap;
use crate::{ use crate::{
boot, debug, boot, debug,
log::{test_log_level, LogLevel},
mem::bump, mem::bump,
util::{bitmap::Bitmap, fmt::NumberPostfix}, util::{bitmap::Bitmap, fmt::NumberPostfix},
}; };
...@@ -79,7 +78,7 @@ pub fn init() { ...@@ -79,7 +78,7 @@ pub fn init() {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
bitmap.set(page as _, false).unwrap(); bitmap.set(page as _, false).unwrap();
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
let _ = bitmap.set(page as _, false); let res = bitmap.set(page as _, false);
} }
} }
...@@ -109,5 +108,5 @@ pub struct PageFrameAllocator { ...@@ -109,5 +108,5 @@ pub struct PageFrameAllocator {
// //
impl PageFrameAllocator { impl PageFrameAllocator {
pub fn free_page(&mut self, addr: u64) {} pub fn free_page(&mut self, _addr: u64) {}
} }
...@@ -6,7 +6,7 @@ use core::panic::PanicInfo; ...@@ -6,7 +6,7 @@ use core::panic::PanicInfo;
#[cfg(not(feature = "tests"))] #[cfg(not(feature = "tests"))]
#[panic_handler] #[panic_handler]
fn panic_handler(info: &PanicInfo) -> ! { fn panic_handler(info: &PanicInfo) -> ! {
crate::println!("{info}"); crate::println!("Kernel {info}");
done(); done();
} }
......
...@@ -63,6 +63,8 @@ pub fn test_panic_handler(info: &PanicInfo) { ...@@ -63,6 +63,8 @@ pub fn test_panic_handler(info: &PanicInfo) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
/* use crate::{debug, println}; */
#[allow(clippy::eq_op)] #[allow(clippy::eq_op)]
#[test_case] #[test_case]
fn trivial() { fn trivial() {
...@@ -73,10 +75,19 @@ mod tests { ...@@ -73,10 +75,19 @@ mod tests {
#[test_case] #[test_case]
fn random_tests() { fn random_tests() {
// error handling test // error handling test
// stack_overflow(79999999);
// unsafe { /* stack_overflow(79999999); */
// *(0xFFFFFFFFDEADC0DE as *mut u8) = 42;
// } /* unsafe {
*(0xFFFFFFFFDEADC0DE as *mut u8) = 42;
} */
/* unsafe {
let x = *(0xffffffffc18a8137 as *mut u8);
println!("Read worked: {x}");
*(0xffffffffc18a8137 as *mut u8) = 42;
println!("Write worked");
} */
#[allow(unused)] #[allow(unused)]
fn stack_overflow(n: usize) { fn stack_overflow(n: usize) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment