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 8, 2024
1 parent 6482515 commit c8e6560
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
64 changes: 64 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
50 changes: 50 additions & 0 deletions src/arch/x86_64/fdt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
extern crate alloc;
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 new() -> 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())
}
}
12 changes: 10 additions & 2 deletions 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,7 +12,9 @@ use core::ptr::write_bytes;
use core::slice;

use align_address::Align;
use hermit_entry::boot_info::{BootInfo, HardwareInfo, PlatformInfo, RawBootInfo, SerialPortBase};
use hermit_entry::boot_info::{
BootInfo, DeviceTreeAddress, HardwareInfo, PlatformInfo, RawBootInfo, SerialPortBase,
};
use hermit_entry::elf::LoadedKernel;
#[cfg(all(target_os = "none", feature = "fc"))]
use hermit_entry::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 @@ -438,14 +444,16 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
KERNEL_STACK_SIZE.try_into().unwrap(),
);

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

static mut BOOT_INFO: Option<RawBootInfo> = None;

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
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![warn(rust_2018_idioms)]
#![cfg_attr(target_arch = "riscv64", allow(unstable_name_collisions))]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::new_ret_no_self)]
#![allow(unstable_name_collisions)]

#[macro_use]
Expand Down

0 comments on commit c8e6560

Please sign in to comment.