Skip to content

Commit f3d6337

Browse files
committed
refactor(x86_64): use pointer for boot address
1 parent 12022cc commit f3d6337

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/arch/x86_64/platform/linux/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::borrow::ToOwned;
22
use core::ffi::CStr;
33
use core::ptr::write_bytes;
4-
use core::sync::atomic::{AtomicUsize, Ordering};
4+
use core::sync::atomic::{AtomicPtr, Ordering};
55
use core::{ptr, slice};
66

77
use align_address::Align;
@@ -35,9 +35,9 @@ mod entry {
3535
);
3636
}
3737

38-
static BOOT_PARAMS: AtomicUsize = AtomicUsize::new(0);
38+
static BOOT_PARAMS: AtomicPtr<BootParams> = AtomicPtr::new(ptr::null_mut());
3939

40-
unsafe extern "C" fn rust_start(boot_params: usize) -> ! {
40+
unsafe extern "C" fn rust_start(boot_params: *mut BootParams) -> ! {
4141
BOOT_PARAMS.store(boot_params, Ordering::Relaxed);
4242
unsafe {
4343
crate::os::loader_main();
@@ -134,9 +134,12 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
134134
load_info,
135135
platform_info: PlatformInfo::LinuxBootParams {
136136
command_line: Some(command_line),
137-
boot_params_addr: (BOOT_PARAMS.load(Ordering::Relaxed) as u64)
138-
.try_into()
139-
.unwrap(),
137+
boot_params_addr: u64::try_from(
138+
BOOT_PARAMS.load(Ordering::Relaxed).expose_provenance(),
139+
)
140+
.unwrap()
141+
.try_into()
142+
.unwrap(),
140143
},
141144
};
142145

@@ -157,9 +160,10 @@ trait BootParamsExt {
157160

158161
impl BootParamsExt for BootParams {
159162
unsafe fn map() {
160-
let addr = BOOT_PARAMS.load(Ordering::Relaxed);
163+
let ptr = BOOT_PARAMS.load(Ordering::Relaxed);
161164

162-
info!("Linux boot parameters: {addr:#x}");
165+
info!("Linux boot parameters: {ptr:p}");
166+
let addr = ptr.expose_provenance();
163167
assert!(addr.is_aligned_to(Size4KiB::SIZE as usize));
164168
assert_ne!(addr, 0);
165169

@@ -168,8 +172,7 @@ impl BootParamsExt for BootParams {
168172
}
169173

170174
unsafe fn get() -> &'static Self {
171-
let addr = BOOT_PARAMS.load(Ordering::Relaxed);
172-
let ptr = ptr::with_exposed_provenance(addr);
175+
let ptr = BOOT_PARAMS.load(Ordering::Relaxed);
173176
unsafe { &*ptr }
174177
}
175178

src/arch/x86_64/platform/multiboot/mod.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::borrow::ToOwned;
22
use core::ptr::write_bytes;
3-
use core::sync::atomic::{AtomicUsize, Ordering};
3+
use core::sync::atomic::{AtomicPtr, Ordering};
44
use core::{mem, ptr, slice};
55

66
use align_address::Align;
@@ -9,7 +9,7 @@ use hermit_entry::boot_info::{
99
};
1010
use hermit_entry::elf::LoadedKernel;
1111
use log::info;
12-
use multiboot::information::{MemoryManagement, Multiboot, PAddr};
12+
use multiboot::information::{MemoryManagement, Multiboot, MultibootInfo, PAddr};
1313
use vm_fdt::FdtWriterResult;
1414
use x86_64::structures::paging::{PageSize, PageTableFlags, Size2MiB, Size4KiB};
1515

@@ -36,9 +36,9 @@ mod entry {
3636
);
3737
}
3838

39-
static MB_INFO: AtomicUsize = AtomicUsize::new(0);
39+
static MB_INFO: AtomicPtr<MultibootInfo> = AtomicPtr::new(ptr::null_mut());
4040

41-
unsafe extern "C" fn rust_start(mb_info: usize) -> ! {
41+
unsafe extern "C" fn rust_start(mb_info: *mut MultibootInfo) -> ! {
4242
MB_INFO.store(mb_info, Ordering::Relaxed);
4343
unsafe {
4444
crate::os::loader_main();
@@ -95,13 +95,18 @@ pub fn find_kernel() -> &'static [u8] {
9595
paging::clean_up();
9696
// Identity-map the Multiboot information.
9797
let mb_info = MB_INFO.load(Ordering::Relaxed);
98-
assert!(mb_info > 0, "Could not find Multiboot information");
99-
info!("Found Multiboot information at {mb_info:#x}");
100-
paging::map::<Size4KiB>(mb_info, mb_info, 1, PageTableFlags::empty());
98+
assert!(!mb_info.is_null(), "Could not find Multiboot information");
99+
info!("Found Multiboot information at {mb_info:p}");
100+
paging::map::<Size4KiB>(
101+
mb_info.expose_provenance(),
102+
mb_info.expose_provenance(),
103+
1,
104+
PageTableFlags::empty(),
105+
);
101106

102107
let mut mem = Mem;
103108
// Load the Multiboot information and identity-map the modules information.
104-
let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut mem).unwrap() };
109+
let multiboot = unsafe { Multiboot::from_ref(&mut *mb_info, &mut mem) };
105110

106111
// Iterate through all modules.
107112
// Collect the start address of the first module and the highest end address of all modules.
@@ -170,9 +175,9 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
170175
.addr()
171176
.align_up(Size4KiB::SIZE as usize);
172177

173-
if new_stack + KERNEL_STACK_SIZE as usize > mb_info {
174-
new_stack =
175-
(mb_info + mem::size_of::<Multiboot<'_, '_>>()).align_up(Size4KiB::SIZE as usize);
178+
if new_stack + KERNEL_STACK_SIZE as usize > mb_info.addr() {
179+
new_stack = (mb_info.addr() + mem::size_of::<Multiboot<'_, '_>>())
180+
.align_up(Size4KiB::SIZE as usize);
176181
}
177182

178183
let command_line = multiboot.command_line();

0 commit comments

Comments
 (0)