From b1997cf701e878556cbcece8f038222f9cf03892 Mon Sep 17 00:00:00 2001 From: Viktor Popp Date: Mon, 20 Jan 2025 09:33:20 +0100 Subject: [PATCH 1/3] Added simple `walk_stack` --- .cargo/config.toml | 5 +++-- src/kernel/debug/mod.rs | 1 + src/kernel/debug/stack_trace.rs | 20 ++++++++++++++++++++ src/kernel/mod.rs | 1 + src/main.rs | 3 +++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/kernel/debug/mod.rs create mode 100644 src/kernel/debug/stack_trace.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 5a5e02c..792a36f 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,10 +1,11 @@ [unstable] build-std = ["core", "compiler_builtins", "alloc"] build-std-features = ["compiler-builtins-mem"] -panic-abort-tests = true # Or remove this from Cargo.toml: `panic = "abort"` +panic-abort-tests = true # Or remove this from Cargo.toml: `panic = "abort"` [build] target = "x86_64-infinity_os.json" +rustflags = ["-C", "force-frame-pointers=yes"] [target.'cfg(target_os = "none")'] -runner = "bootimage runner" \ No newline at end of file +runner = "bootimage runner" diff --git a/src/kernel/debug/mod.rs b/src/kernel/debug/mod.rs new file mode 100644 index 0000000..25e307c --- /dev/null +++ b/src/kernel/debug/mod.rs @@ -0,0 +1 @@ +pub mod stack_trace; \ No newline at end of file diff --git a/src/kernel/debug/stack_trace.rs b/src/kernel/debug/stack_trace.rs new file mode 100644 index 0000000..dcfd747 --- /dev/null +++ b/src/kernel/debug/stack_trace.rs @@ -0,0 +1,20 @@ +use crate::print; +use core::arch::asm; + +#[repr(C)] +struct StackFrame { + rbp: *const StackFrame, + rip: usize, +} + +pub fn walk_stack() { + let mut frame: *const StackFrame; + unsafe { + asm!("mov {}, rbp", out(reg) frame); + } + + while let Some(stack_frame) = unsafe { frame.as_ref() } { + print!("Function address: {:#x}\n", stack_frame.rip); + frame = stack_frame.rbp; + } +} diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index 06447cf..e6e35b1 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -1,5 +1,6 @@ pub mod allocator; pub mod clock; +pub mod debug; pub mod gdt; pub mod interrupts; pub mod memory; diff --git a/src/main.rs b/src/main.rs index 63170fd..ba76117 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,8 @@ pub fn kernel_main(boot_info: &'static BootInfo) -> ! { kernel::allocator::init_heap(&mut mapper, &mut frame_allocator) .expect("heap initialization failed"); + panic!(""); + let mut executor = Executor::new(); executor.spawn(Task::new(keyboard::keyboard_task())); executor.run(); @@ -35,5 +37,6 @@ pub fn kernel_main(boot_info: &'static BootInfo) -> ! { #[panic_handler] fn panic(info: &PanicInfo) -> ! { print!("{}\n", info); + kernel::debug::stack_trace::walk_stack(); infinity_os::hlt_loop(); } From df3374956c29bb857c862cda5cf3676ec04fb983 Mon Sep 17 00:00:00 2001 From: Viktor Popp Date: Tue, 21 Jan 2025 13:01:09 +0100 Subject: [PATCH 2/3] Removed `panic("")` in main.rs --- src/main.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index ba76117..6927963 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,6 @@ pub fn kernel_main(boot_info: &'static BootInfo) -> ! { kernel::allocator::init_heap(&mut mapper, &mut frame_allocator) .expect("heap initialization failed"); - panic!(""); - let mut executor = Executor::new(); executor.spawn(Task::new(keyboard::keyboard_task())); executor.run(); From 0fd5f547975043946c2e5b18d77092b5c677e58d Mon Sep 17 00:00:00 2001 From: Viktor Popp Date: Wed, 22 Jan 2025 18:26:52 +0100 Subject: [PATCH 3/3] Made keyboard print exclude some keys --- src/kernel/task/keyboard.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/kernel/task/keyboard.rs b/src/kernel/task/keyboard.rs index 3047e81..2941535 100644 --- a/src/kernel/task/keyboard.rs +++ b/src/kernel/task/keyboard.rs @@ -5,9 +5,9 @@ use core::{ task::{Context, Poll}, }; use crossbeam_queue::ArrayQueue; -use futures_util::{stream::Stream, StreamExt}; use futures_util::task::AtomicWaker; -use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1}; +use futures_util::{stream::Stream, StreamExt}; +use pc_keyboard::{layouts, HandleControl, KeyCode, Keyboard, ScancodeSet1}; static SCANCODE_QUEUE: OnceCell> = OnceCell::uninit(); static WAKER: AtomicWaker = AtomicWaker::new(); @@ -20,7 +20,7 @@ pub(crate) fn add_scancode(scancode: u8) { if let Err(_) = queue.push(scancode) { print!("WARNING: scancode queue full; dropping keyboard input\n"); } else { - WAKER.wake(); // new + WAKER.wake(); } } else { print!("WARNING: scancode queue uninitialized\n"); @@ -75,8 +75,21 @@ pub async fn keyboard_task() { if let Ok(Some(key_event)) = keyboard.add_byte(scancode) { if let Some(key) = keyboard.process_keyevent(key_event) { match key { - DecodedKey::Unicode(character) => print!("{}", character), - DecodedKey::RawKey(key) => print!("{:?}", key), + pc_keyboard::DecodedKey::RawKey( + KeyCode::LAlt + | KeyCode::RAlt2 + | KeyCode::RAltGr + | KeyCode::LControl + | KeyCode::RControl + | KeyCode::RShift + | KeyCode::LShift + | KeyCode::PageDown + | KeyCode::PageUp + | KeyCode::Home + | KeyCode::CapsLock, + ) => {} + pc_keyboard::DecodedKey::Unicode(character) => print!("{}", character), + pc_keyboard::DecodedKey::RawKey(key) => print!("{:?}", key), } } }