Skip to content

Commit

Permalink
fix: put raw boot info into take-static
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Apr 3, 2024
1 parent 46bbfd3 commit d5fb4b9
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 98 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hermit-entry = { version = "0.9", features = ["loader"] }
log = "0.4"
one-shot-mutex = "0.1"
sptr = "0.3"
take-static = "0.1"
vm-fdt = { version = "0.3", default-features = false, features = ["alloc"] }

[features]
Expand Down
34 changes: 18 additions & 16 deletions src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
}

let current_stack_address = load_info.kernel_image_addr_range.start - KERNEL_STACK_SIZE as u64;
pub static mut BOOT_INFO: Option<RawBootInfo> = None;

take_static::take_static! {
static RAW_BOOT_INFO: Option<RawBootInfo> = None;
}

let raw_boot_info = RAW_BOOT_INFO.take().unwrap();

let dtb = unsafe {
Dtb::from_raw(sptr::from_exposed_addr(DEVICE_TREE as usize))
Expand All @@ -246,22 +251,19 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
let ram_start = u64::from_be_bytes(start_slice.try_into().unwrap());
let ram_size = u64::from_be_bytes(size_slice.try_into().unwrap());

let boot_info = {
let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: ram_start..ram_start + ram_size,
serial_port_base: SerialPortBase::new(0x1000),
device_tree: core::num::NonZeroU64::new(DEVICE_TREE),
},
load_info,
platform_info: PlatformInfo::LinuxBoot,
};
RawBootInfo::from(boot_info)
let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: ram_start..ram_start + ram_size,
serial_port_base: SerialPortBase::new(0x1000),
device_tree: core::num::NonZeroU64::new(DEVICE_TREE),
},
load_info,
platform_info: PlatformInfo::LinuxBoot,
};

unsafe {
BOOT_INFO = Some(boot_info);
}
info!("boot_info = {boot_info:#?}");
*raw_boot_info = Some(RawBootInfo::from(boot_info));
info!("boot_info at {raw_boot_info:p}");

// Jump to the kernel entry point and provide the Multiboot information to it.
info!(
Expand Down Expand Up @@ -291,7 +293,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
"br {entry}",
stack_address = in(reg) current_stack_address,
entry = in(reg) entry_point,
in("x0") BOOT_INFO.as_ref().unwrap(),
in("x0") raw_boot_info,
in("x1") 0,
options(noreturn)
)
Expand Down
78 changes: 38 additions & 40 deletions src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,47 +100,45 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {

info!("hart_id = {}", start::get_hart_id());

static mut BOOT_INFO: Option<RawBootInfo> = None;

let boot_info = {
let phys_addr_range = {
let memory = fdt.memory();
let mut regions = memory.regions();

let mem_region = regions.next().unwrap();
assert!(
regions.next().is_none(),
"hermit-loader can only handle one memory region yet"
);

let mem_base = u64::try_from(mem_region.starting_address.addr()).unwrap();
let mem_size = u64::try_from(mem_region.size.unwrap()).unwrap();
mem_base..mem_base + mem_size
};

let device_tree = {
let fdt_addr = start::get_fdt_ptr().expose_addr();
DeviceTreeAddress::new(fdt_addr.try_into().unwrap())
};

let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range,
serial_port_base: None,
device_tree,
},
load_info,
platform_info: PlatformInfo::LinuxBoot,
};

info!("boot_info = {boot_info:#?}");

RawBootInfo::from(boot_info)
take_static::take_static! {
static RAW_BOOT_INFO: Option<RawBootInfo> = None;
}

let raw_boot_info = RAW_BOOT_INFO.take().unwrap();

let phys_addr_range = {
let memory = fdt.memory();
let mut regions = memory.regions();

let mem_region = regions.next().unwrap();
assert!(
regions.next().is_none(),
"hermit-loader can only handle one memory region yet"
);

let mem_base = u64::try_from(mem_region.starting_address.addr()).unwrap();
let mem_size = u64::try_from(mem_region.size.unwrap()).unwrap();
mem_base..mem_base + mem_size
};

unsafe {
BOOT_INFO = Some(boot_info);
}
let device_tree = {
let fdt_addr = start::get_fdt_ptr().expose_addr();
DeviceTreeAddress::new(fdt_addr.try_into().unwrap())
};

let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range,
serial_port_base: None,
device_tree,
},
load_info,
platform_info: PlatformInfo::LinuxBoot,
};

info!("boot_info = {boot_info:#?}");
*raw_boot_info = Some(RawBootInfo::from(boot_info));
info!("boot_info at {raw_boot_info:p}");

// Check expected signature of entry function
let entry: Entry = {
Expand All @@ -158,7 +156,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
entry = in(reg) entry,
stack = in(reg) start::get_stack_ptr(),
in("a0") start::get_hart_id(),
in("a1") BOOT_INFO.as_ref().unwrap(),
in("a1") raw_boot_info,
options(noreturn)
)
}
Expand Down
86 changes: 44 additions & 42 deletions src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,29 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
start_address, end_address
);

static mut BOOT_INFO: Option<RawBootInfo> = None;

let boot_info = {
let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: start_address as u64..end_address as u64,
serial_port_base: SerialPortBase::new(SERIAL_IO_PORT),
device_tree: None,
},
load_info,
platform_info: PlatformInfo::LinuxBootParams {
command_line,
boot_params_addr: (unsafe { boot_params } as u64).try_into().unwrap(),
},
};
RawBootInfo::from(boot_info)
};
unsafe {
BOOT_INFO = Some(boot_info);
info!("BootInfo located at {:p}", &BOOT_INFO);
take_static::take_static! {
static RAW_BOOT_INFO: Option<RawBootInfo> = None;
}

let raw_boot_info = RAW_BOOT_INFO.take().unwrap();

let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: start_address as u64..end_address as u64,
serial_port_base: SerialPortBase::new(SERIAL_IO_PORT),
device_tree: None,
},
load_info,
platform_info: PlatformInfo::LinuxBootParams {
command_line,
boot_params_addr: (unsafe { boot_params } as u64).try_into().unwrap(),
},
};

info!("boot_info = {boot_info:#?}");
*raw_boot_info = Some(RawBootInfo::from(boot_info));
info!("boot_info at {raw_boot_info:p}");

// Jump to the kernel entry point and provide the Multiboot information to it.
info!(
"Jumping to HermitCore Application Entry Point at {:#x}",
Expand All @@ -425,7 +426,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
"jmp {entry}",
stack_address = in(reg) current_stack_address,
entry = in(reg) entry_point,
in("rdi") BOOT_INFO.as_ref().unwrap(),
in("rdi") raw_boot_info,
in("rsi") 0,
options(noreturn)
)
Expand Down Expand Up @@ -483,28 +484,29 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {

let device_tree = DeviceTree::create().expect("Unable to create devicetree!");

static mut BOOT_INFO: Option<RawBootInfo> = None;

let boot_info = {
let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: 0..0,
serial_port_base: SerialPortBase::new(SERIAL_IO_PORT),
device_tree: DeviceTreeAddress::new(device_tree.as_ptr() as u64),
},
load_info,
platform_info: PlatformInfo::Multiboot {
command_line,
multiboot_info_addr: (unsafe { mb_info } as u64).try_into().unwrap(),
},
};
RawBootInfo::from(boot_info)
};
unsafe {
BOOT_INFO = Some(boot_info);
info!("BootInfo located at {:p}", &BOOT_INFO);
take_static::take_static! {
static RAW_BOOT_INFO: Option<RawBootInfo> = None;
}

let raw_boot_info = RAW_BOOT_INFO.take().unwrap();

let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: 0..0,
serial_port_base: SerialPortBase::new(SERIAL_IO_PORT),
device_tree: DeviceTreeAddress::new(device_tree.as_ptr() as u64),
},
load_info,
platform_info: PlatformInfo::Multiboot {
command_line,
multiboot_info_addr: (unsafe { mb_info } as u64).try_into().unwrap(),
},
};

info!("boot_info = {boot_info:#?}");
*raw_boot_info = Some(RawBootInfo::from(boot_info));
info!("boot_info at {raw_boot_info:p}");

// Jump to the kernel entry point and provide the Multiboot information to it.
info!(
"Jumping to HermitCore Application Entry Point at {:#x}",
Expand All @@ -528,7 +530,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
"jmp {entry}",
stack_address = in(reg) current_stack_address,
entry = in(reg) entry_point,
in("rdi") BOOT_INFO.as_ref().unwrap(),
in("rdi") raw_boot_info,
in("rsi") 0,
options(noreturn)
)
Expand Down

0 comments on commit d5fb4b9

Please sign in to comment.