|
| 1 | +use crate::boot_info::{self, BootInfo}; |
| 2 | +use crate::global_alloc; |
| 3 | +use crate::memory::HostPhysAddr; |
| 4 | +use alloc::vec::Vec; |
| 5 | + |
| 6 | +extern "C" { |
| 7 | + pub static MULTIBOOT_HEADER_START: u32; |
| 8 | + pub static MULTIBOOT_HEADER_END: u32; |
| 9 | + |
| 10 | + // The _value_ of the last byte of the mythril binary. The |
| 11 | + // address of this symbol is the actual end. |
| 12 | + pub static END_OF_BINARY: u8; |
| 13 | +} |
| 14 | + |
| 15 | +// NOTE: see multiboot2::header_location for more information |
| 16 | +pub fn header_location() -> (u32, u32) { |
| 17 | + unsafe { (MULTIBOOT_HEADER_START, MULTIBOOT_HEADER_END) } |
| 18 | +} |
| 19 | + |
| 20 | +fn setup_global_alloc_region<'a, F>( |
| 21 | + info: &'a multiboot::Multiboot<'a, F>, |
| 22 | +) -> (u64, u64) |
| 23 | +where |
| 24 | + F: Fn(u64, usize) -> Option<&'a [u8]>, |
| 25 | +{ |
| 26 | + let regions = info |
| 27 | + .memory_regions() |
| 28 | + .expect("Missing multiboot memory regions"); |
| 29 | + |
| 30 | + let available = regions.filter_map(|region| match region.memory_type() { |
| 31 | + multiboot::MemoryType::Available => Some(( |
| 32 | + region.base_address(), |
| 33 | + region.base_address() + region.length(), |
| 34 | + )), |
| 35 | + _ => None, |
| 36 | + }); |
| 37 | + |
| 38 | + debug!("Modules:"); |
| 39 | + let modules = |
| 40 | + info.modules() |
| 41 | + .expect("No multiboot modules found") |
| 42 | + .map(|module| { |
| 43 | + debug!(" 0x{:x}-0x{:x}", module.start, module.end); |
| 44 | + (module.start, module.end) |
| 45 | + }); |
| 46 | + |
| 47 | + // Avoid allocating over the actual mythril binary (just use 0 as the start |
| 48 | + // for now). |
| 49 | + let mythril_bounds = |
| 50 | + [(0 as u64, unsafe { &END_OF_BINARY as *const u8 as u64 })]; |
| 51 | + debug!( |
| 52 | + "Mythril binary bounds: 0x{:x}-0x{:x}", |
| 53 | + mythril_bounds[0].0, mythril_bounds[0].1 |
| 54 | + ); |
| 55 | + |
| 56 | + let excluded = modules.chain(mythril_bounds.iter().copied()); |
| 57 | + |
| 58 | + // TODO(alschwalm): For now, we just use the portion of the largest available |
| 59 | + // region that is above the highest excluded region. |
| 60 | + let max_excluded = excluded |
| 61 | + .max_by(|left, right| left.1.cmp(&right.1)) |
| 62 | + .expect("No max excluded region"); |
| 63 | + |
| 64 | + let largest_region = available |
| 65 | + .max_by(|left, right| (left.1 - left.0).cmp(&(right.1 - right.0))) |
| 66 | + .expect("No largest region"); |
| 67 | + |
| 68 | + if largest_region.0 > max_excluded.1 { |
| 69 | + largest_region |
| 70 | + } else if max_excluded.1 > largest_region.0 |
| 71 | + && max_excluded.1 < largest_region.1 |
| 72 | + { |
| 73 | + (max_excluded.1, largest_region.1) |
| 74 | + } else { |
| 75 | + panic!("Unable to find suitable global alloc region") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +pub fn early_init_multiboot(addr: HostPhysAddr) -> BootInfo { |
| 80 | + fn multiboot_addr_translate<'a>( |
| 81 | + paddr: u64, |
| 82 | + size: usize, |
| 83 | + ) -> Option<&'a [u8]> { |
| 84 | + unsafe { Some(core::slice::from_raw_parts(paddr as *const u8, size)) } |
| 85 | + } |
| 86 | + let multiboot_info = unsafe { |
| 87 | + multiboot::Multiboot::new(addr.as_u64(), multiboot_addr_translate) |
| 88 | + .expect("Failed to create Multiboot structure") |
| 89 | + }; |
| 90 | + |
| 91 | + let alloc_region = setup_global_alloc_region(&multiboot_info); |
| 92 | + |
| 93 | + info!( |
| 94 | + "Allocating from 0x{:x}-{:x}", |
| 95 | + alloc_region.0, alloc_region.1 |
| 96 | + ); |
| 97 | + |
| 98 | + unsafe { |
| 99 | + global_alloc::Allocator::allocate_from(alloc_region.0, alloc_region.1); |
| 100 | + } |
| 101 | + |
| 102 | + let modules = multiboot_info |
| 103 | + .modules() |
| 104 | + .expect("No multiboot modules found") |
| 105 | + .map(|module| boot_info::BootModule { |
| 106 | + address: HostPhysAddr::new(module.start), |
| 107 | + size: (module.end - module.start) as usize, |
| 108 | + identifier: module.string.map(alloc::string::String::from), |
| 109 | + }) |
| 110 | + .collect::<Vec<_>>(); |
| 111 | + |
| 112 | + BootInfo { |
| 113 | + modules: modules, |
| 114 | + rsdp: None, |
| 115 | + } |
| 116 | +} |
0 commit comments