From b165f0b62229e5c060961e3423c6d0a6460dff10 Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Mon, 21 Feb 2022 22:25:27 +0100 Subject: [PATCH 1/3] commands: add a command to print stack also add get_reg asm macro and print_memory function to print n bytes from an adress --- src/elsos.rs | 2 +- src/tty/basic_commands.rs | 9 +++++++++ src/utilities.rs | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/elsos.rs b/src/elsos.rs index 93cf69c..05573f7 100644 --- a/src/elsos.rs +++ b/src/elsos.rs @@ -35,7 +35,6 @@ pub static mut SETTINGS: Settings = Settings #[no_mangle] pub extern "C" fn kernel_main(magic: u32, address: u32) { - gdt::init_gdt(); init_vga(); vga::cursor::Cursor::init(0, 15); @@ -99,3 +98,4 @@ macro_rules! logln () => ($crate::log!("\n")); ($($arg:tt)*) => ($crate::log!("{}\n", format_args!($($arg)*))); } + diff --git a/src/tty/basic_commands.rs b/src/tty/basic_commands.rs index 7349e74..f54ce5c 100644 --- a/src/tty/basic_commands.rs +++ b/src/tty/basic_commands.rs @@ -12,6 +12,7 @@ pub fn execute(command: &str) "reboot" => reboot(), "scheen" => scheen(), "pm" |"printmem" => printmem(), + "ps" |"printstack" => print_stack(), "panic" => panic(), "" => {}, _ => crate::println!("{}: unknown command. Use help for more", command) @@ -56,6 +57,14 @@ fn printmem() crate::serial_println!("=============="); } +fn print_stack() +{ + unsafe + { + utilities::print_memory(crate::get_reg!("esp") as *const u8, 10 * 16); + } +} + fn panic() { panic!("panic()"); diff --git a/src/utilities.rs b/src/utilities.rs index 5eb204b..49e1152 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -64,3 +64,44 @@ pub unsafe fn from_c_str(ptr: *const u8) -> &'static [u8] slice::from_raw_parts(ptr, strlen(ptr) + 1) } +pub unsafe fn print_memory(ptr: *const u8, n: usize) +{ + let mut i: usize = 0; + + while i < n + { + if i % 16 == 0 + { + crate::log!("{:0x}: ", ptr.add(i) as u32); + } + crate::log!("{:02x?} ", *ptr.add(i)); + i += 1; + if i % 16 == 0 + { + crate::log!(" |"); + for i in i - 16..i + { + let chr = *ptr.add(i); + crate::log!("{}", if chr > 0x1f && chr < 0x7f {chr as char } else { '.' }); + } + crate::log!("|"); + crate::logln!(); + } + else if i % 8 == 0 + { + crate::log!(" "); + } + } + crate::logln!(); +} + +#[macro_export] +macro_rules! get_reg +{ + ($reg:expr) => + {{ + let val: u32; + core::arch::asm!(concat!("mov {}, ", $reg), out(reg) val); + val + }} +} From 756f00d5c6c06b00296fdc9966931bb374668a82 Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Wed, 23 Feb 2022 17:52:49 +0100 Subject: [PATCH 2/3] panic: add register and stack dump on panic --- src/elsos.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/elsos.rs b/src/elsos.rs index 05573f7..16172dd 100644 --- a/src/elsos.rs +++ b/src/elsos.rs @@ -78,7 +78,48 @@ pub fn ok_fail(value: bool) -> &'static str fn panic(info: &PanicInfo) -> ! { vga::panic(); - logln!("\n\x1B[31;49m{}\x1B[39;49m", info); + logln!("\n\x1B[31;49m{}\x1B[39;49m\n", info); + + let eax: u32; + let ebx: u32; + let ecx: u32; + let edx: u32; + + let esi: u32; + let edi: u32; + let esp: u32; + let ebp: u32; + + unsafe + { + eax = crate::get_reg!("eax"); + ebx = crate::get_reg!("ebx"); + ecx = crate::get_reg!("ecx"); + edx = crate::get_reg!("edx"); + + esi = crate::get_reg!("esi"); + edi = crate::get_reg!("edi"); + esp = crate::get_reg!("esp"); + ebp = crate::get_reg!("ebp"); + } + + logln!("eax: {:08x} ebx: {:08x} ecx: {:08x} edx: {:08x}", eax, ebx, ecx, edx); + logln!("esi: {:08x} edi: {:08x} esp: {:08x} ebp: {:08x}", esi, edi, esp, ebp); + + log!("\nstack: "); + for i in 0..24 + { + unsafe + { + log!("{:08x} ", *(esp as *const u32).add(i * 4)); + if (i + 1) % 8 == 0 + { + log!("\n "); + } + } + } + + logln!(""); loop {} } From fe242128deb8344caf812c37204a56f421310cef Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Mon, 7 Mar 2022 12:24:02 +0100 Subject: [PATCH 3/3] version: bump to 0.0.2 - kfs-2 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 61cf316..146364f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ VERSION=0 PATCHLEVEL=0 -SUBLEVEL=1 +SUBLEVEL=2 EXTRAVERSION= export VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION