Wrote stack unwinder. NEEDTO: fix NoUnwindInfo

Probably incorrect PC was set.
This commit is contained in:
2025-03-05 22:21:38 +00:00
parent 8ee4af1a48
commit b26dc6de01
15 changed files with 435 additions and 175 deletions
+35 -36
View File
@@ -12,23 +12,33 @@
)]
extern crate alloc;
use crate::{arch::x86_64::memory::init_page_table, prelude::*};
use arch::x86_64::memory::allocation::heap_alloc::init_heap;
use crate::{
// TODO: Fix nesting under `arch`. A lot of code does not NEED to run on
// x86_64. Note that the panic handler does.
arch::x86_64::{
cpu::apic::enable_apic,
drivers::{
ascii::screensize_chars, framebuffer::display::screensize_px,
},
memory::{
FRAME_ALLOCATOR,
allocation::{
heap_alloc::init_heap, page_alloc::FoundryOSFrameAllocator,
},
init_page_table,
units::MemoryUnits,
},
},
prelude::*,
};
use arch::x86_64::memory::mapping;
use core::arch::asm;
use limine::BaseRevision;
use std::unwind;
use std::unwind::eh_info::ELF;
use x86_64::VirtAddr;
use arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator;
use crate::arch::x86_64::cpu::apic::enable_apic;
use crate::arch::x86_64::drivers::ascii::screensize_chars;
use crate::arch::x86_64::drivers::framebuffer::display::screensize_px;
use crate::arch::x86_64::memory::FRAME_ALLOCATOR;
use crate::arch::x86_64::memory::units::MemoryUnits;
pub mod arch;
mod panic;
pub mod resources;
#[allow(unused)] // We aren't using much of this right now.
pub mod std;
@@ -36,11 +46,10 @@ pub mod util;
pub mod prelude {
pub use crate::{
eprint, eprintln, print, print_log, println, println_log, serial_print,
serial_println,
debug, debugln,
std::io::{_print, _print_log, _serial_write, _print_err},
debug, debugln, eprint, eprintln, print, print_log, println,
println_log, serial_print, serial_println,
std::debug::_debug,
std::io::{_print, _print_err, _print_log, _serial_write},
};
}
@@ -54,13 +63,6 @@ pub mod prelude {
#[unsafe(link_section = ".requests")]
static BASE_REVISION: BaseRevision = BaseRevision::new();
#[panic_handler]
fn rust_panic(_info: &core::panic::PanicInfo) -> ! {
println!("Kernel panic: {}", _info);
serial_println!("Kernel panic: {}", _info);
hcf();
}
pub fn hcf() -> ! {
loop {
unsafe {
@@ -70,6 +72,9 @@ pub fn hcf() -> ! {
}
}
/// Panicking before this is initialised is unwise. We should probably extract
/// very early init into it's own function because Stack Traces may require
/// allocations etc.
pub fn boot() -> Result<(), &'static str> {
if !BASE_REVISION.is_supported() {
return Err("Base revision not supported");
@@ -104,11 +109,16 @@ pub fn boot() -> Result<(), &'static str> {
debugln!("[Success]");
debugln!(" Initialising Memory Subsystem... ");
let physical_memory_offset = VirtAddr::new(*mapping::PHYSICAL_MEMORY_OFFSET);
let physical_memory_offset =
VirtAddr::new(*mapping::PHYSICAL_MEMORY_OFFSET);
init_page_table(physical_memory_offset);
FoundryOSFrameAllocator::init(memory_map);
let available_bytes = FRAME_ALLOCATOR.get().unwrap().lock().available_memory();
debugln!(" => Available Memory: {}", MemoryUnits::from_bytes(available_bytes as usize));
let available_bytes =
FRAME_ALLOCATOR.get().unwrap().lock().available_memory();
debugln!(
" => Available Memory: {}",
MemoryUnits::from_bytes(available_bytes as usize)
);
debugln!("[Success]");
@@ -134,16 +144,5 @@ pub fn boot() -> Result<(), &'static str> {
x86_64::instructions::interrupts::enable();
debugln!("[Success]");
debug!(" Setting up stack unwinder, panic handler... ");
// Setup stack traces and proper panic handler. TODO: Handle panics
// differently if not initialised.
let eh_frame_ptr = ELF
.get_section_addr(".eh_frame_hdr")
.expect("Could not get `.eh_frame_hdr` address.");
let _eh_info =
unsafe { unwind::eh_info::EhInfo::from_hdr_ptr(eh_frame_ptr) };
debugln!("[Success]");
Ok(())
}