From 3f385ba79dc6b80eb78a251002a057637e5b75c6 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:34:31 +0200 Subject: [PATCH] Unify KernelArgs type between architectures --- src/arch/aarch64/start.rs | 90 +++++++++-------------------------- src/arch/riscv64/start.rs | 92 ++++++++++-------------------------- src/arch/x86_shared/start.rs | 71 ++++------------------------ src/startup/mod.rs | 66 ++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 197 deletions(-) diff --git a/src/arch/aarch64/start.rs b/src/arch/aarch64/start.rs index 19edc2ac67..f0689c5d40 100644 --- a/src/arch/aarch64/start.rs +++ b/src/arch/aarch64/start.rs @@ -17,7 +17,10 @@ use crate::{ dtb, dtb::register_dev_memory_ranges, paging, - startup::memory::{register_bootloader_areas, register_memory_region, BootloaderMemoryKind}, + startup::{ + memory::{register_bootloader_areas, register_memory_region, BootloaderMemoryKind}, + KernelArgs, + }, }; /// Test of zero values in BSS. @@ -28,26 +31,6 @@ static mut DATA_TEST_NONZERO: usize = 0xFFFF_FFFF_FFFF_FFFF; pub static AP_READY: AtomicBool = AtomicBool::new(false); static BSP_READY: AtomicBool = AtomicBool::new(false); -#[derive(Debug)] -#[repr(C, packed(8))] -pub struct KernelArgs { - kernel_base: usize, - kernel_size: usize, - stack_base: usize, - stack_size: usize, - env_base: usize, - env_size: usize, - hwdesc_base: usize, - hwdesc_size: usize, - areas_base: usize, - areas_size: usize, - - /// The physical base 64-bit pointer to the contiguous bootstrap/initfs. - bootstrap_base: usize, - /// Size of contiguous bootstrap/initfs physical region, not necessarily page aligned. - bootstrap_size: usize, -} - /// The entry to Rust, all things must be initialized #[unsafe(no_mangle)] pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { @@ -63,8 +46,8 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { // Convert env to slice let env = slice::from_raw_parts( - (crate::PHYS_OFFSET + args.env_base) as *const u8, - args.env_size, + (crate::PHYS_OFFSET + args.env_base as usize) as *const u8, + args.env_size as usize, ); // Set up graphical debug @@ -75,8 +58,8 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { let hwdesc_data = if args.hwdesc_base != 0 { Some(unsafe { slice::from_raw_parts( - (crate::PHYS_OFFSET + args.hwdesc_base) as *const u8, - args.hwdesc_size, + (crate::PHYS_OFFSET + args.hwdesc_base as usize) as *const u8, + args.hwdesc_size as usize, ) }) } else { @@ -101,68 +84,39 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { } info!("Redox OS starting..."); - info!( - "Kernel: {:X}:{:X}", - { args.kernel_base }, - args.kernel_base + args.kernel_size - ); - info!( - "Stack: {:X}:{:X}", - { args.stack_base }, - args.stack_base + args.stack_size - ); - info!( - "Env: {:X}:{:X}", - { args.env_base }, - args.env_base + args.env_size - ); - info!( - "HWDESC: {:X}:{:X}", - { args.hwdesc_base }, - args.hwdesc_base + args.hwdesc_size - ); - info!( - "Areas: {:X}:{:X}", - { args.areas_base }, - args.areas_base + args.areas_size - ); - info!( - "Bootstrap: {:X}:{:X}", - { args.bootstrap_base }, - args.bootstrap_base + args.bootstrap_size - ); + args.print(); interrupt::init(); // Initialize RMM - register_bootloader_areas(args.areas_base, args.areas_size); + register_bootloader_areas(args.areas_base as usize, args.areas_size as usize); if let Ok(dtb) = &dtb_res { register_dev_memory_ranges(dtb); } register_memory_region( - args.kernel_base, - args.kernel_size, + args.kernel_base as usize, + args.kernel_size as usize, BootloaderMemoryKind::Kernel, ); register_memory_region( - args.stack_base, - args.stack_size, + args.stack_base as usize, + args.stack_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.env_base, - args.env_size, + args.env_base as usize, + args.env_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.hwdesc_base, - args.hwdesc_size, + args.hwdesc_base as usize, + args.hwdesc_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.bootstrap_base, - args.bootstrap_size, + args.bootstrap_base as usize, + args.bootstrap_size as usize, BootloaderMemoryKind::IdentityMap, ); crate::startup::memory::init(None, None); @@ -203,9 +157,9 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { crate::Bootstrap { base: crate::memory::Frame::containing(crate::paging::PhysicalAddress::new( - args.bootstrap_base, + args.bootstrap_base as usize, )), - page_count: args.bootstrap_size / crate::memory::PAGE_SIZE, + page_count: (args.bootstrap_size as usize) / crate::memory::PAGE_SIZE, env, } }; diff --git a/src/arch/riscv64/start.rs b/src/arch/riscv64/start.rs index f662abfaa8..313ecc28b9 100644 --- a/src/arch/riscv64/start.rs +++ b/src/arch/riscv64/start.rs @@ -16,7 +16,10 @@ use crate::{ device, devices::graphical_debug, dtb::register_dev_memory_ranges, - startup::memory::{register_bootloader_areas, register_memory_region, BootloaderMemoryKind}, + startup::{ + memory::{register_bootloader_areas, register_memory_region, BootloaderMemoryKind}, + KernelArgs, + }, }; /// Test of zero values in BSS. @@ -26,25 +29,6 @@ static mut DATA_TEST_NONZERO: usize = 0xFFFF_FFFF_FFFF_FFFF; pub static BOOT_HART_ID: AtomicUsize = AtomicUsize::new(0); -#[repr(packed)] -pub struct KernelArgs { - kernel_base: usize, - kernel_size: usize, - stack_base: usize, - stack_size: usize, - env_base: usize, - env_size: usize, - acpi_base: usize, - acpi_size: usize, - areas_base: usize, - areas_size: usize, - - /// The physical base 64-bit pointer to the contiguous bootstrap/initfs. - bootstrap_base: usize, - /// Size of contiguous bootstrap/initfs physical region, not necessarily page aligned. - bootstrap_size: usize, -} - fn get_boot_hart_id(env: &[u8]) -> Option { for line in core::str::from_utf8(env).unwrap_or("").lines() { let mut parts = line.splitn(2, '='); @@ -79,12 +63,15 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { } let env = slice::from_raw_parts( - (crate::PHYS_OFFSET + args.env_base) as *const u8, - args.env_size, + (crate::PHYS_OFFSET + args.env_base as usize) as *const u8, + args.env_size as usize, ); - let dtb_data = if args.acpi_base != 0 { - Some((crate::PHYS_OFFSET + args.acpi_base, args.acpi_size)) + let dtb_data = if args.hwdesc_base != 0 { + Some(( + crate::PHYS_OFFSET + args.hwdesc_base as usize, + args.hwdesc_size as usize, + )) } else { None }; @@ -99,36 +86,7 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { } info!("Redox OS starting..."); - info!( - "Kernel: {:X}:{:X}", - { args.kernel_base }, - args.kernel_base + args.kernel_size - ); - info!( - "Stack: {:X}:{:X}", - { args.stack_base }, - args.stack_base + args.stack_size - ); - info!( - "Env: {:X}:{:X}", - { args.env_base }, - args.env_base + args.env_size - ); - info!( - "RSDPs: {:X}:{:X}", - { args.acpi_size }, - args.acpi_size + args.acpi_size - ); - info!( - "Areas: {:X}:{:X}", - { args.areas_base }, - args.areas_base + args.areas_size - ); - info!( - "Bootstrap: {:X}:{:X}", - { args.bootstrap_base }, - args.bootstrap_base + args.bootstrap_size - ); + args.print(); if let Some(dtb) = &dtb { device::dump_fdt(&dtb); @@ -137,40 +95,40 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { interrupt::init(); let bootstrap = crate::Bootstrap { - base: Frame::containing(PhysicalAddress::new(args.bootstrap_base)), - page_count: args.bootstrap_size / PAGE_SIZE, + base: Frame::containing(PhysicalAddress::new(args.bootstrap_base as usize)), + page_count: args.bootstrap_size as usize / PAGE_SIZE, env, }; // Initialize RMM - register_bootloader_areas(args.areas_base, args.areas_size); + register_bootloader_areas(args.areas_base as usize, args.areas_size as usize); if let Some(dt) = &dtb { register_dev_memory_ranges(dt); } register_memory_region( - args.kernel_base, - args.kernel_size, + args.kernel_base as usize, + args.kernel_size as usize, BootloaderMemoryKind::Kernel, ); register_memory_region( - args.stack_base, - args.stack_size, + args.stack_base as usize, + args.stack_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.env_base, - args.env_size, + args.env_base as usize, + args.env_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.acpi_base, - args.acpi_size, + args.hwdesc_base as usize, + args.hwdesc_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.bootstrap_base, - args.bootstrap_size, + args.bootstrap_base as usize, + args.bootstrap_size as usize, BootloaderMemoryKind::IdentityMap, ); diff --git a/src/arch/x86_shared/start.rs b/src/arch/x86_shared/start.rs index c643c02c88..728accd256 100644 --- a/src/arch/x86_shared/start.rs +++ b/src/arch/x86_shared/start.rs @@ -18,7 +18,10 @@ use crate::{ devices::graphical_debug, gdt, idt, interrupt, paging::{self, PhysicalAddress, RmmA, RmmArch, TableKind}, - startup::memory::{register_bootloader_areas, register_memory_region, BootloaderMemoryKind}, + startup::{ + memory::{register_bootloader_areas, register_memory_region, BootloaderMemoryKind}, + KernelArgs, + }, }; /// Test of zero values in BSS. @@ -29,33 +32,6 @@ static DATA_TEST_NONZERO: SyncUnsafeCell = SyncUnsafeCell::new(usize::max pub static AP_READY: AtomicBool = AtomicBool::new(false); static BSP_READY: AtomicBool = AtomicBool::new(false); -#[repr(C, packed(8))] -pub struct KernelArgs { - kernel_base: u64, - kernel_size: u64, - stack_base: u64, - stack_size: u64, - env_base: u64, - env_size: u64, - - /// The base pointer to the saved RSDP. - /// - /// This field can be NULL, and if so, the system has not booted with UEFI or in some other way - /// retrieved the RSDPs. The kernel or a userspace driver will thus try searching the BIOS - /// memory instead. On UEFI systems, searching is not guaranteed to actually work though. - acpi_rsdp_base: u64, - /// The size of the RSDP region. - acpi_rsdp_size: u64, - - areas_base: u64, - areas_size: u64, - - /// The physical base 64-bit pointer to the contiguous bootstrap/initfs. - bootstrap_base: u64, - /// Size of contiguous bootstrap/initfs physical region, not necessarily page aligned. - bootstrap_size: u64, -} - /// The entry to Rust, all things must be initialized #[unsafe(no_mangle)] pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { @@ -82,36 +58,7 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { graphical_debug::init(env); info!("Redox OS starting..."); - info!( - "Kernel: {:X}:{:X}", - { args.kernel_base }, - { args.kernel_base } + { args.kernel_size } - ); - info!( - "Stack: {:X}:{:X}", - { args.stack_base }, - { args.stack_base } + { args.stack_size } - ); - info!( - "Env: {:X}:{:X}", - { args.env_base }, - { args.env_base } + { args.env_size } - ); - info!( - "RSDP: {:X}:{:X}", - { args.acpi_rsdp_base }, - { args.acpi_rsdp_base } + { args.acpi_rsdp_size } - ); - info!( - "Areas: {:X}:{:X}", - { args.areas_base }, - { args.areas_base } + { args.areas_size } - ); - info!( - "Bootstrap: {:X}:{:X}", - { args.bootstrap_base }, - { args.bootstrap_base } + { args.bootstrap_size } - ); + args.print(); // Set up GDT gdt::init_bsp(args.stack_base as usize + args.stack_size as usize); @@ -137,8 +84,8 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { BootloaderMemoryKind::IdentityMap, ); register_memory_region( - args.acpi_rsdp_base as usize, - args.acpi_rsdp_size as usize, + args.hwdesc_base as usize, + args.hwdesc_size as usize, BootloaderMemoryKind::IdentityMap, ); register_memory_region( @@ -183,8 +130,8 @@ pub unsafe extern "C" fn kstart(args_ptr: *const KernelArgs) -> ! { // Read ACPI tables, starts APs #[cfg(feature = "acpi")] { - acpi::init(if args.acpi_rsdp_base != 0 { - Some((args.acpi_rsdp_base as usize + crate::PHYS_OFFSET) as *const u8) + acpi::init(if args.hwdesc_base != 0 { + Some((args.hwdesc_base as usize + crate::PHYS_OFFSET) as *const u8) } else { None }); diff --git a/src/startup/mod.rs b/src/startup/mod.rs index eb291915fd..3b24328537 100644 --- a/src/startup/mod.rs +++ b/src/startup/mod.rs @@ -1 +1,67 @@ pub mod memory; + +#[repr(C, packed(8))] +pub(crate) struct KernelArgs { + pub(crate) kernel_base: u64, + pub(crate) kernel_size: u64, + + pub(crate) stack_base: u64, + pub(crate) stack_size: u64, + + pub(crate) env_base: u64, + pub(crate) env_size: u64, + + /// The base pointer to the saved RSDP or device tree blob. + /// + /// On x86 this field can be NULL, and if so, the system has not booted + /// with UEFI or in some other way retrieved the RSDPs. The kernel or a + /// userspace driver will thus try searching the BIOS memory instead. On + /// UEFI systems, searching is not guaranteed to actually work though. + /// On other architectures this field must always contain a pointer to + /// either an RSDP or device tree blob. + pub(crate) hwdesc_base: u64, + pub(crate) hwdesc_size: u64, + + pub(crate) areas_base: u64, + pub(crate) areas_size: u64, + + /// The physical base 64-bit pointer to the contiguous bootstrap/initfs. + pub(crate) bootstrap_base: u64, + /// Size of contiguous bootstrap/initfs physical region, not necessarily page aligned. + pub(crate) bootstrap_size: u64, +} + +impl KernelArgs { + pub(crate) fn print(&self) { + info!( + "Kernel: {:X}:{:X}", + { self.kernel_base }, + self.kernel_base + self.kernel_size + ); + info!( + "Stack: {:X}:{:X}", + { self.stack_base }, + self.stack_base + self.stack_size + ); + info!( + "Env: {:X}:{:X}", + { self.env_base }, + self.env_base + self.env_size + ); + info!( + "HWDESC: {:X}:{:X}", + { self.hwdesc_base }, + self.hwdesc_base + self.hwdesc_size + ); + info!( + "Areas: {:X}:{:X}", + { self.areas_base }, + self.areas_base + self.areas_size + ); + info!( + "Bootstrap: {:X}:{:X}", + { self.bootstrap_base }, + self.bootstrap_base + self.bootstrap_size + ); + } +}