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 5, 2024
1 parent 6482515 commit 3086be2
Show file tree
Hide file tree
Showing 5 changed files with 150 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
65 changes: 65 additions & 0 deletions src/arch/x86_64/fdt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use alloc::vec::Vec;

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

use super::{mb_info, MEM};

pub struct DeviceTree(Vec<u8>);

impl DeviceTree {
pub const fn new() -> Self {
Self(Vec::new())
}

pub fn set(&mut self, vec: Vec<u8>) {
self.0 = vec;
}

pub unsafe fn multiboot_info(&mut self) -> Result<Vec<u8>, FdtError> {
let multiboot = 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 vec = fdt.finish()?;

self.set(vec.clone());

Ok(vec)

}

pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
}
18 changes: 16 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,22 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
KERNEL_STACK_SIZE.try_into().unwrap(),
);

static mut DEVICE_TREE: DeviceTree = DeviceTree::new();

let _ = DEVICE_TREE.multiboot_info();

let fdt_addr = DEVICE_TREE.as_ptr() as u64;

info!("DeviceTree located at {:#x}", fdt_addr);

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(fdt_addr),
},
load_info,
platform_info: PlatformInfo::Multiboot {
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![cfg_attr(target_arch = "riscv64", allow(unstable_name_collisions))]
#![allow(clippy::missing_safety_doc)]
#![allow(unstable_name_collisions)]
#![allow(unused_imports)]

#[macro_use]
mod macros;
Expand All @@ -20,6 +21,9 @@ mod uefi;

use core::fmt::{self, Write};

#[macro_use]
extern crate alloc;

#[doc(hidden)]
fn _print(args: fmt::Arguments<'_>) {
unsafe {
Expand Down

0 comments on commit 3086be2

Please sign in to comment.