From 2db28c7b6b4d37308f819d01e4699d810197a83c Mon Sep 17 00:00:00 2001 From: corigan01 Date: Sat, 18 Jan 2025 15:58:26 -0600 Subject: [PATCH] Bootloader: Scrub kernel's exe memory before loading --- Cargo.toml | 6 +++--- bootloader/stage-16bit/src/main.rs | 2 +- bootloader/stage-32bit/src/multiboot.rs | 11 +++++++++-- bootloader/stage-64bit/src/main.rs | 1 + crates/mem/src/phys.rs | 11 +++++++++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fa08a53b..2e6cb991 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,8 +97,8 @@ overflow-checks = true [profile.kernel] inherits = "release" panic = "abort" -opt-level=1 -strip = true +opt-level=0 +strip = false debug = 1 overflow-checks = true @@ -106,6 +106,6 @@ overflow-checks = true inherits = "release" panic = "abort" opt-level=0 -strip = true +strip = false debug = 1 overflow-checks = true diff --git a/bootloader/stage-16bit/src/main.rs b/bootloader/stage-16bit/src/main.rs index e614f762..bdcbbe00 100644 --- a/bootloader/stage-16bit/src/main.rs +++ b/bootloader/stage-16bit/src/main.rs @@ -156,7 +156,7 @@ fn main(disk_id: u16) -> ! { stage_to_stage.video_mode = Some((closest_video_id, closest_video_info)); logln!( - "Optimal Video Mode {:00x}): {}x{} {}bbp", + "Optimal Video Mode id={:#04x}: {}x{} {}bbp", closest_video_id.get_id(), closest_video_info.width, closest_video_info.height, diff --git a/bootloader/stage-32bit/src/multiboot.rs b/bootloader/stage-32bit/src/multiboot.rs index 320ce549..f1492423 100644 --- a/bootloader/stage-32bit/src/multiboot.rs +++ b/bootloader/stage-32bit/src/multiboot.rs @@ -501,7 +501,7 @@ pub fn get_stage_to_stage_from_multiboot_header(header: *const Multiboot1Info) - assert_eq!(unsafe { core::ptr::read_volatile(stack_ptr) }, 0); let header_ref = unsafe { &*header }; - logln!("Multiboot mode! Launched from boot loader {:?}!", unsafe { + logln!("Multiboot mode! Launched from bootloader {:?}!", unsafe { core::ffi::CStr::from_ptr(header_ref.boot_loader_name as *const i8) }); @@ -512,6 +512,8 @@ pub fn get_stage_to_stage_from_multiboot_header(header: *const Multiboot1Info) - ) }; + // FIXME: We should make a more generic memory entry to pass around instead of + // using e820 mappings. let mut e820_map: [bios::memory::MemoryEntry; MAX_MEMORY_MAP_ENTRIES] = [unsafe { core::mem::zeroed() }; MAX_MEMORY_MAP_ENTRIES]; e820_map @@ -523,7 +525,13 @@ pub fn get_stage_to_stage_from_multiboot_header(header: *const Multiboot1Info) - e820.region_type = entry.kind; }); + // Qemu writes all of the PTRs and LENs of each of our bootloader compoenents into memory addr +1Mib + // + // You can find more details of this in the meta/main.rs file. let &[stage32_ptr, stage32_len, stage64_ptr, stage64_len, kernel_ptr, kernel_len, initfs_ptr, initfs_len] = + // FIXME: I am not sure if this is the best way of passing these arguments in, but + // its also only for emulator booting so I think its fine for now. Maybe + // replace in the future? (unsafe { core::slice::from_raw_parts(0x100000 as *const u64, 8) }) else { unreachable!("Cannot match compile time length amount of elements!"); @@ -531,7 +539,6 @@ pub fn get_stage_to_stage_from_multiboot_header(header: *const Multiboot1Info) - Stage16toStage32 { bootloader_stack_ptr: (stack_ptr as u64, INIT_STACK.len() as u64), - // FIXME: We should try and link with these in the linkerscript! stage32_ptr: (stage32_ptr, stage32_len), stage64_ptr: (stage64_ptr, stage64_len), kernel_ptr: (kernel_ptr, kernel_len), diff --git a/bootloader/stage-64bit/src/main.rs b/bootloader/stage-64bit/src/main.rs index 40b9d975..95aaf2bb 100644 --- a/bootloader/stage-64bit/src/main.rs +++ b/bootloader/stage-64bit/src/main.rs @@ -226,6 +226,7 @@ fn build_memory_map(s2s: &Stage32toStage64, kernel_exe_len: usize) -> paging::Pa }) .expect("Unable to find region for kernel pages"); mm.add_region(kernels_pages).unwrap(); + kernels_pages.scrub(0); let kernels_stack_pages = mm .find_continuous_of(PhysMemoryKind::Free, PAGE_2M, PAGE_2M, 1 * MIB as u64) diff --git a/crates/mem/src/phys.rs b/crates/mem/src/phys.rs index f439a00c..1c1077c2 100644 --- a/crates/mem/src/phys.rs +++ b/crates/mem/src/phys.rs @@ -97,6 +97,17 @@ impl PhysMemoryEntry { pub const fn len(&self) -> u64 { self.end - self.start } + + /// Write a pattern of bytes to this area + /// + /// # Note + /// The pages that repr this memory entry must already be writeable and page mapped! + pub unsafe fn scrub(&self, byte_pattern: u8) { + let phys_slice = unsafe { + core::slice::from_raw_parts_mut(self.start as *mut u8, (self.end - self.start) as usize) + }; + phys_slice.fill(byte_pattern); + } } pub struct PhysMemoryIter<'a, const N: usize> {