Skip to content

Commit

Permalink
Bootloader: Scrub kernel's exe memory before loading
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 18, 2025
1 parent 415549a commit 2db28c7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ overflow-checks = true
[profile.kernel]
inherits = "release"
panic = "abort"
opt-level=1
strip = true
opt-level=0
strip = false
debug = 1
overflow-checks = true

[profile.dummy]
inherits = "release"
panic = "abort"
opt-level=0
strip = true
strip = false
debug = 1
overflow-checks = true
2 changes: 1 addition & 1 deletion bootloader/stage-16bit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions bootloader/stage-32bit/src/multiboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});

Expand All @@ -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
Expand All @@ -523,15 +525,20 @@ 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!");
};

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),
Expand Down
1 change: 1 addition & 0 deletions bootloader/stage-64bit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions crates/mem/src/phys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit 2db28c7

Please sign in to comment.