From c1a8afb8367d0819bfd80e127f51f30c58fdf777 Mon Sep 17 00:00:00 2001 From: Jacob Hinchliffe Date: Sat, 22 Feb 2025 03:59:28 +0000 Subject: [PATCH 1/3] Formatted random JSON file whoops --- Cargo.toml | 8 ++++---- kernel/x86_64-kernel.json | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c740bdd..972ad1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,11 @@ [workspace] -members = [ +members = [ "lib/lib_framebuffer", "lib/lib_serial", "lib/lib_ascii", - "kernel" -, "lib/lib_application"] + "kernel", + "lib/lib_application", +] resolver = "2" [workspace.package] @@ -28,4 +29,3 @@ overflow-checks = false lto = true incremental = false codegen-units = 1 - diff --git a/kernel/x86_64-kernel.json b/kernel/x86_64-kernel.json index 8f7231a..7b3dc4e 100644 --- a/kernel/x86_64-kernel.json +++ b/kernel/x86_64-kernel.json @@ -24,7 +24,8 @@ "--no-pie", "--gc-sections", "--build-id=none", - "-z", "max-page-size=0x1000" + "-z", + "max-page-size=0x1000" ] } } From 2b2219f5be07d733aa60b6e26fb851c8b65d30da Mon Sep 17 00:00:00 2001 From: Jacob Hinchliffe Date: Sat, 22 Feb 2025 04:00:37 +0000 Subject: [PATCH 2/3] Update submodules --- lib/lib_ascii | 2 +- lib/lib_serial | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lib_ascii b/lib/lib_ascii index ba43834..1ae956e 160000 --- a/lib/lib_ascii +++ b/lib/lib_ascii @@ -1 +1 @@ -Subproject commit ba43834e3ead8a242761b3d769bd959aeb126ff5 +Subproject commit 1ae956efa44d7ebb009ce23ee917a76cb4df96ca diff --git a/lib/lib_serial b/lib/lib_serial index ed2fa6b..813fc0b 160000 --- a/lib/lib_serial +++ b/lib/lib_serial @@ -1 +1 @@ -Subproject commit ed2fa6b50102607d33973c1851a0ec8c895ed4fa +Subproject commit 813fc0bfa3a14069c5a7ee18c1d70a025d12f48d From 40ad5dbbf4f706f6c1aa9a144fe71cee29f6ecd3 Mon Sep 17 00:00:00 2001 From: Jacob Hinchliffe Date: Sat, 22 Feb 2025 05:00:18 +0000 Subject: [PATCH 3/3] Add page fault handler that does nothing, I am tired --- kernel/src/arch/x86_64/interrupts.rs | 16 +++++++- kernel/src/arch/x86_64/memmap.rs | 56 ++++++++++++++++++++++++++++ kernel/src/lib.rs | 3 -- 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 kernel/src/arch/x86_64/memmap.rs diff --git a/kernel/src/arch/x86_64/interrupts.rs b/kernel/src/arch/x86_64/interrupts.rs index 160cf20..cffb882 100644 --- a/kernel/src/arch/x86_64/interrupts.rs +++ b/kernel/src/arch/x86_64/interrupts.rs @@ -1,7 +1,8 @@ use lib_ascii::println_log; use lib_serial::serial_println; use x86_64::instructions::port::Port; -use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +use x86_64::registers::control::Cr2; +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode}; use pic8259::ChainedPics; use spin::{Lazy, Mutex}; @@ -12,6 +13,7 @@ static IDT: Lazy = Lazy::new(|| { idt.double_fault.set_handler_fn(double_fault_handler); idt.general_protection_fault .set_handler_fn(general_protection_fault_handler); + idt.page_fault.set_handler_fn(page_fault_handler); idt[InterruptIndex::Timer.as_u8()].set_handler_fn(timer_interrupt_handler); idt[InterruptIndex::Keyboard.as_u8()].set_handler_fn(keyboard_interrupt_handler); @@ -85,3 +87,15 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFr .notify_end_of_interrupt(InterruptIndex::Timer.as_u8()); } } + +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); + + crate::hcf(); +} diff --git a/kernel/src/arch/x86_64/memmap.rs b/kernel/src/arch/x86_64/memmap.rs new file mode 100644 index 0000000..1b0cbbb --- /dev/null +++ b/kernel/src/arch/x86_64/memmap.rs @@ -0,0 +1,56 @@ +//! Sets up a memory map using Limine. + +use limine::{ + request::{HhdmRequest, KernelAddressRequest, MemoryMapRequest}, + response::MemoryMapResponse, +}; +use spin::Lazy; + +#[used] +#[link_section = ".requests"] +static MEMORY_MAP_REQUEST: MemoryMapRequest = MemoryMapRequest::new(); + +#[used] +#[link_section = ".requests"] +static HIGHER_HALF_DIRECT_MAP_REQUEST: HhdmRequest = HhdmRequest::new(); + +/// ```rs +/// let virt_addr = phys_addr + offset; +/// let phys_addr = virt_addr - offset; // (given VA is in the HHDM). Do not use for executable code. +/// ``` +pub static PHYSICAL_MEMORY_OFFSET: Lazy = Lazy::new(|| { + HIGHER_HALF_DIRECT_MAP_REQUEST + .get_response() + .unwrap() + .offset() +}); + +#[used] +#[link_section = ".requests"] +static KERNEL_ADDRESS_REQUEST: KernelAddressRequest = KernelAddressRequest::new(); + +/// Converts virtual addresses in the kernel to a physical address like this: +/// ```rs +/// let phys_addr = virt_addr - virtual_base + physical_base; +/// ``` +/// +/// Returns (virtual_base, physical_base) +pub static KERNEL_PHYSICAL_MEMORY_OFFSET: Lazy<(u64, u64)> = Lazy::new(|| { + let resp = KERNEL_ADDRESS_REQUEST.get_response().unwrap(); + + // These are base addresses, using Limine's built in page table. + (resp.virtual_base(), resp.physical_base()) +}); + +/// Fetches the memory map from Limine. +/// +/// # Panics +/// +/// Panics if the memory map was not found in MEMORY_MAP_REQUEST. +pub fn get_memory_map() -> &'static MemoryMapResponse { + if let Some(memory_map) = MEMORY_MAP_REQUEST.get_response() { + return memory_map; + } else { + unreachable!("Could not fetch memory map from Limine.") + } +} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index c36854e..e667a18 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,7 +1,6 @@ #![no_std] #![feature(abi_x86_interrupt)] -use arch::x86_64::interrupts; use core::arch::asm; use limine::request::{RequestsEndMarker, RequestsStartMarker}; use limine::BaseRevision; @@ -11,8 +10,6 @@ pub use lib_serial::{serial_print, serial_println, serial_read}; mod arch; -use lib_framebuffer; - /// Sets the base revision to the latest revision supported by the crate. /// See specification for further info. /// Be sure to mark all limine requests with #[used], otherwise they may be removed by the compiler.