Skip to content

Commit 94a549d

Browse files
committed
don't map boot info and memory map into bootloader's memory
Modifying the bootloaders page tables by mapping addresses that we don't know for sure are available can lead to all sorts of problems. Simply stop doing that.
1 parent 2548c25 commit 94a549d

File tree

1 file changed

+45
-47
lines changed

1 file changed

+45
-47
lines changed

common/src/lib.rs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,7 @@ pub struct Mappings {
463463

464464
/// Allocates and initializes the boot info struct and the memory map.
465465
///
466-
/// The boot info and memory map are mapped to both the kernel and bootloader
467-
/// address space at the same address. This makes it possible to return a Rust
468-
/// reference that is valid in both address spaces. The necessary physical frames
469-
/// are taken from the given `frame_allocator`.
466+
/// The necessary physical frames are taken from the given `frame_allocator`.
470467
pub fn create_boot_info<I, D>(
471468
config: &BootloaderConfig,
472469
boot_config: &BootConfig,
@@ -513,19 +510,15 @@ where
513510
Ok(tlb) => tlb.flush(),
514511
Err(err) => panic!("failed to map page {:?}: {:?}", page, err),
515512
}
516-
// we need to be able to access it too
517-
match unsafe {
518-
page_tables
519-
.bootloader
520-
.map_to(page, frame, flags, &mut frame_allocator)
521-
} {
522-
Ok(tlb) => tlb.flush(),
523-
Err(err) => panic!("failed to map page {:?}: {:?}", page, err),
524-
}
525513
}
526514

527-
let boot_info: &'static mut MaybeUninit<BootInfo> =
528-
unsafe { &mut *boot_info_addr.as_mut_ptr() };
515+
let boot_info: &'static mut MaybeUninit<BootInfo> = unsafe {
516+
// SAFETY: This is technically UB because the current page tables don't
517+
// map `memory_map_regions_addr`, so we have to be careful to not
518+
// access any elements.
519+
// We have to do this because `BootInfo` needs a `MemoryRegions`.
520+
&mut *boot_info_addr.as_mut_ptr()
521+
};
529522

530523
log::info!("Create Memory Map");
531524

@@ -546,38 +539,43 @@ where
546539
log::info!("Create bootinfo");
547540

548541
// create boot info
549-
let boot_info = boot_info.write({
550-
let mut info = BootInfo::new(memory_regions.into());
551-
info.framebuffer = mappings
552-
.framebuffer
553-
.map(|addr| unsafe {
554-
FrameBuffer::new(
555-
addr.as_u64(),
556-
system_info
557-
.framebuffer
558-
.expect(
559-
"there shouldn't be a mapping for the framebuffer if there is \
560-
no framebuffer",
561-
)
562-
.info,
563-
)
564-
})
565-
.into();
566-
info.physical_memory_offset = mappings.physical_memory_offset.map(VirtAddr::as_u64).into();
567-
info.recursive_index = mappings.recursive_index.map(Into::into).into();
568-
info.rsdp_addr = system_info.rsdp_addr.map(|addr| addr.as_u64()).into();
569-
info.tls_template = mappings.tls_template.into();
570-
info.ramdisk_addr = mappings
571-
.ramdisk_slice_start
572-
.map(|addr| addr.as_u64())
573-
.into();
574-
info.ramdisk_len = mappings.ramdisk_slice_len;
575-
info.kernel_addr = mappings.kernel_slice_start.as_u64();
576-
info.kernel_len = mappings.kernel_slice_len as _;
577-
info.kernel_image_offset = mappings.kernel_image_offset.as_u64();
578-
info._test_sentinel = boot_config._test_sentinel;
579-
info
580-
});
542+
let mut info = BootInfo::new(memory_regions.into());
543+
info.framebuffer = mappings
544+
.framebuffer
545+
.map(|addr| unsafe {
546+
FrameBuffer::new(
547+
addr.as_u64(),
548+
system_info
549+
.framebuffer
550+
.expect(
551+
"there shouldn't be a mapping for the framebuffer if there is \
552+
no framebuffer",
553+
)
554+
.info,
555+
)
556+
})
557+
.into();
558+
info.physical_memory_offset = mappings.physical_memory_offset.map(VirtAddr::as_u64).into();
559+
info.recursive_index = mappings.recursive_index.map(Into::into).into();
560+
info.rsdp_addr = system_info.rsdp_addr.map(|addr| addr.as_u64()).into();
561+
info.tls_template = mappings.tls_template.into();
562+
info.ramdisk_addr = mappings
563+
.ramdisk_slice_start
564+
.map(|addr| addr.as_u64())
565+
.into();
566+
info.ramdisk_len = mappings.ramdisk_slice_len;
567+
info.kernel_addr = mappings.kernel_slice_start.as_u64();
568+
info.kernel_len = mappings.kernel_slice_len as _;
569+
info.kernel_image_offset = mappings.kernel_image_offset.as_u64();
570+
info._test_sentinel = boot_config._test_sentinel;
571+
572+
// Write to boot info directly to the identity-mapped frame.
573+
let boot_info_frame = page_tables.kernel.translate_page(start_page).unwrap();
574+
assert!(size_of::<BootInfo>() <= Size4KiB::SIZE as usize);
575+
let ptr = boot_info_frame.start_address().as_u64() as *mut BootInfo;
576+
unsafe {
577+
ptr.write(info);
578+
}
581579

582580
boot_info_addr
583581
}

0 commit comments

Comments
 (0)