Skip to content

Commit

Permalink
Create devicetree for x86_64
Browse files Browse the repository at this point in the history
Pass memory region infomation from devicetree
  • Loading branch information
duanyu-yu committed Feb 15, 2024
1 parent d22fcda commit 0a1d0ac
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
58 changes: 58 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 @@ -11,6 +11,7 @@ align-address = "0.1"
hermit-entry = { version = "0.9", features = ["loader"] }
log = "0.4"
sptr = "0.3"
vm-fdt = { version = "0.3", default-features = false, features = ["alloc"] }

[features]
default = []
Expand Down
49 changes: 49 additions & 0 deletions src/arch/x86_64/fdt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use alloc::format;

use multiboot::information::{MemoryType, Multiboot};
use vm_fdt::{Error as FdtError, FdtWriter};

use super::{mb_info, MEM};

pub struct DeviceTree;

impl DeviceTree {
#[cfg(all(target_os = "none", not(feature = "fc")))]
pub fn create() -> Result<&'static [u8], FdtError> {
let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut MEM).unwrap() };

let all_regions = multiboot
.memory_regions()
.expect("Could not find a memory map in the Multiboot information");
let ram_regions = all_regions.filter(|m| m.memory_type() == MemoryType::Available);

let mut fdt = FdtWriter::new()?;

let root_node = fdt.begin_node("")?;
fdt.property_string("compatible", "linux,dummy-virt")?;
fdt.property_u32("#address-cells", 0x2)?;
fdt.property_u32("#size-cells", 0x2)?;

if let Some(cmdline) = multiboot.command_line() {
let chosen_node = fdt.begin_node("chosen")?;
fdt.property_string("bootargs", cmdline)?;
fdt.end_node(chosen_node)?;
}

for m in ram_regions {
let start_address = m.base_address();
let length = m.length();

let memory_node = fdt.begin_node(format!("memory@{:x}", start_address).as_str())?;
fdt.property_string("device_type", "memory")?;
fdt.property_array_u64("reg", &[start_address, length])?;
fdt.end_node(memory_node)?;
}

fdt.end_node(root_node)?;

let fdt = fdt.finish()?;

Ok(fdt.leak())
}
}
10 changes: 9 additions & 1 deletion src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(all(target_os = "none", not(feature = "fc")))]
mod fdt;
mod paging;
mod physicalmem;

Expand All @@ -10,6 +12,8 @@ use core::ptr::write_bytes;
use core::slice;

use align_address::Align;
#[cfg(all(target_os = "none", not(feature = "fc")))]
use hermit_entry::boot_info::DeviceTreeAddress;
use hermit_entry::boot_info::{BootInfo, HardwareInfo, PlatformInfo, RawBootInfo, SerialPortBase};
use hermit_entry::elf::LoadedKernel;
#[cfg(all(target_os = "none", feature = "fc"))]
Expand All @@ -27,6 +31,8 @@ use multiboot::information::{Multiboot, PAddr};
use uart_16550::SerialPort;
use x86_64::structures::paging::{PageSize, PageTableFlags, Size2MiB, Size4KiB};

#[cfg(all(target_os = "none", not(feature = "fc")))]
use self::fdt::DeviceTree;
use self::physicalmem::PhysAlloc;

#[cfg(target_os = "none")]
Expand Down Expand Up @@ -472,14 +478,16 @@ 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: None,
device_tree: DeviceTreeAddress::new(device_tree.as_ptr() as u64),
},
load_info,
platform_info: PlatformInfo::Multiboot {
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mod none;
#[cfg(target_os = "uefi")]
mod uefi;

#[cfg(all(target_arch = "x86_64", target_os = "none", not(feature = "fc")))]
extern crate alloc;

#[cfg(target_os = "none")]
#[doc(hidden)]
fn _print(args: core::fmt::Arguments<'_>) {
Expand Down

0 comments on commit 0a1d0ac

Please sign in to comment.