Skip to content

Commit 9fa8de8

Browse files
committed
feat: implement decompression of Hermit images
1 parent 8577575 commit 9fa8de8

File tree

13 files changed

+538
-149
lines changed

13 files changed

+538
-149
lines changed

Cargo.lock

Lines changed: 344 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ align-address = "0.3"
1111
allocator-api2 = { version = "0.3", default-features = false }
1212
anstyle = { version = "1", default-features = false }
1313
cfg-if = "1"
14-
hermit-entry = { version = "0.10", features = ["loader"] }
14+
hermit-entry = { version = "0.10", git = "https://github.com/fogti/hermit-entry.git", branch = "image-reader", features = ["allocator-api2", "loader"] }
1515
log = "0.4"
1616
one-shot-mutex = "0.2"
1717
sptr = "0.3"

src/arch/aarch64/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use sptr::Strict;
2020

2121
use crate::BootInfoExt;
2222
use crate::arch::paging::*;
23-
use crate::os::CONSOLE;
23+
use crate::os::{CONSOLE, ExtraBootInfo};
2424

2525
unsafe extern "C" {
2626
static mut loader_end: u8;
@@ -110,12 +110,14 @@ pub fn find_kernel() -> &'static [u8] {
110110
}
111111
}
112112

113-
pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
113+
pub unsafe fn boot_kernel(kernel_info: LoadedKernel, extra_info: ExtraBootInfo) -> ! {
114114
let LoadedKernel {
115115
load_info,
116116
entry_point,
117117
} = kernel_info;
118118

119+
assert!(extra_info.image.is_none());
120+
119121
let dtb = unsafe {
120122
Fdt::from_ptr(sptr::from_exposed_addr(get_dtb_addr() as usize))
121123
.expect(".dtb file has invalid header")

src/arch/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cfg_if::cfg_if! {
55
} else if #[cfg(target_arch = "riscv64")] {
66
mod riscv64;
77
pub use self::riscv64::*;
8-
} else if #[cfg(all(target_arch = "x86_64"))] {
8+
} else if #[cfg(target_arch = "x86_64")] {
99
mod x86_64;
1010
pub use self::x86_64::*;
1111
}

src/arch/riscv64/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use log::info;
1717
use sptr::Strict;
1818

1919
use crate::BootInfoExt;
20+
use crate::os::ExtraBootInfo;
2021

2122
fn find_kernel_linux(chosen: &FdtNode<'_, '_>) -> Option<&'static [u8]> {
2223
let initrd_start = chosen.property("linux,initrd-start")?.as_usize()?;
@@ -90,7 +91,7 @@ pub unsafe fn get_memory(memory_size: u64) -> u64 {
9091
u64::try_from(start_address).unwrap()
9192
}
9293

93-
pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
94+
pub unsafe fn boot_kernel(kernel_info: LoadedKernel, extra_info: ExtraBootInfo) -> ! {
9495
let LoadedKernel {
9596
load_info,
9697
entry_point,
@@ -118,6 +119,8 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
118119
DeviceTreeAddress::new(fdt_addr.try_into().unwrap())
119120
};
120121

122+
assert!(extra_info.image.is_none());
123+
121124
let boot_info = BootInfo {
122125
hardware_info: HardwareInfo {
123126
phys_addr_range,

src/arch/x86_64/firecracker.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use super::physicalmem::PhysAlloc;
2020
use super::{KERNEL_STACK_SIZE, SERIAL_IO_PORT, paging};
2121
use crate::BootInfoExt;
2222
use crate::fdt::Fdt;
23+
use crate::os::ExtraBootInfo;
2324

2425
unsafe extern "C" {
2526
static mut loader_end: u8;
@@ -118,7 +119,7 @@ pub fn find_kernel() -> &'static [u8] {
118119
unsafe { slice::from_raw_parts(sptr::from_exposed_addr(elf_start), elf_len) }
119120
}
120121

121-
pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
122+
pub unsafe fn boot_kernel(kernel_info: LoadedKernel, extra_info: ExtraBootInfo) -> ! {
122123
let LoadedKernel {
123124
load_info,
124125
entry_point,
@@ -174,6 +175,10 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
174175

175176
let mut fdt = Fdt::new("firecracker").unwrap();
176177

178+
if let Some(image) = extra_info.image {
179+
fdt = fdt.image_range(image);
180+
}
181+
177182
// Load the boot_param memory-map information
178183
let linux_e820_entries =
179184
unsafe { *(sptr::from_exposed_addr::<u8>(boot_params + E820_ENTRIES_OFFSET)) };

src/arch/x86_64/multiboot.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use super::physicalmem::PhysAlloc;
1818
use crate::BootInfoExt;
1919
use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT};
2020
use crate::fdt::Fdt;
21+
use crate::os::ExtraBootInfo;
2122

2223
unsafe extern "C" {
2324
static mut loader_end: u8;
@@ -55,7 +56,7 @@ impl MemoryManagement for Mem {
5556
pub struct DeviceTree;
5657

5758
impl DeviceTree {
58-
pub fn create() -> FdtWriterResult<&'static [u8]> {
59+
pub fn create(extra_info: &ExtraBootInfo) -> FdtWriterResult<&'static [u8]> {
5960
let mut mem = Mem;
6061
let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut mem).unwrap() };
6162

@@ -65,6 +66,10 @@ impl DeviceTree {
6566

6667
let mut fdt = Fdt::new("multiboot")?.memory_regions(memory_regions)?;
6768

69+
if let Some(image) = extra_info.image {
70+
fdt = fdt.image_range(image);
71+
}
72+
6873
if let Some(cmdline) = multiboot.command_line() {
6974
fdt = fdt.bootargs(cmdline.to_owned())?;
7075
}
@@ -142,7 +147,7 @@ pub fn find_kernel() -> &'static [u8] {
142147
unsafe { slice::from_raw_parts(sptr::from_exposed_addr(elf_start), elf_len) }
143148
}
144149

145-
pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
150+
pub unsafe fn boot_kernel(kernel_info: LoadedKernel, extra_info: ExtraBootInfo) -> ! {
146151
let LoadedKernel {
147152
load_info,
148153
entry_point,
@@ -185,7 +190,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
185190
write_bytes(stack, 0, KERNEL_STACK_SIZE.try_into().unwrap());
186191
}
187192

188-
let device_tree = DeviceTree::create().expect("Unable to create devicetree!");
193+
let device_tree = DeviceTree::create(extra_info).expect("Unable to create devicetree!");
189194
let device_tree =
190195
DeviceTreeAddress::new(u64::try_from(device_tree.as_ptr().expose_addr()).unwrap());
191196

src/fdt.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct Fdt {
99
writer: FdtWriter,
1010
root_node: FdtWriterNode,
1111
bootargs: Option<String>,
12+
image_range: Option<(u64, u64)>,
1213
}
1314

1415
impl Fdt {
@@ -31,9 +32,15 @@ impl Fdt {
3132

3233
pub fn finish(mut self) -> FdtWriterResult<Vec<u8>> {
3334
let chosen_node = self.writer.begin_node("chosen")?;
35+
3436
if let Some(bootargs) = &self.bootargs {
3537
self.writer.property_string("bootargs", bootargs)?;
3638
}
39+
40+
if let Some((image_start, image_len)) = self.image_range {
41+
self.writer_property_array_u64("image_reg", &[image_start, image_len])?;
42+
}
43+
3744
self.writer.end_node(chosen_node)?;
3845

3946
self.writer.end_node(self.root_node)?;
@@ -48,6 +55,13 @@ impl Fdt {
4855
Ok(self)
4956
}
5057

58+
pub fn image_range(mut self, image: &[u8]) -> FdtWriterResult<Self> {
59+
assert!(self.image_range.is_none() && !image.is_empty());
60+
let image_start = (&image[0]) as *const u8 as u64;
61+
self.image_range = Some((image_start, image.len() as u64));
62+
self.memory(image_start..(image_start + (image.len() as u64)))
63+
}
64+
5165
#[cfg_attr(all(target_arch = "x86_64", not(target_os = "uefi")), expect(unused))]
5266
pub fn rsdp(mut self, rsdp: u64) -> FdtWriterResult<Self> {
5367
let rsdp_node = self.writer.begin_node(&format!("hermit,rsdp@{rsdp:x}"))?;

src/main.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use hermit_entry::boot_info::{BootInfo, RawBootInfo};
1212
mod macros;
1313

1414
mod arch;
15+
1516
mod bump_allocator;
17+
mod oneshot_allocator;
18+
1619
#[cfg(any(target_os = "uefi", target_arch = "x86_64"))]
1720
mod fdt;
1821
mod log;
@@ -45,3 +48,38 @@ fn _print(args: core::fmt::Arguments<'_>) {
4548

4649
self::os::CONSOLE.lock().write_fmt(args).unwrap();
4750
}
51+
52+
/// Detects the input format are resolves the kernel
53+
fn resolve_kernel<'a, A: allocator_api2::alloc::Allocator>(
54+
input_blob: &[u8],
55+
alloc: A,
56+
buf: &'a mut Option<allocator_api2::boxes::Box<hermit_entry::tar_parser::Bytes, A>>,
57+
) -> (&'a [u8], Option<hermit_entry::config::Config>) {
58+
use hermit_entry::{Format, ThinTree, decompress_image_with_allocator, detect_format};
59+
match detect_format(input_blob) {
60+
Some(Format::Elf) => (input_blob, None),
61+
62+
Some(Format::Gzip) => {
63+
*buf = Some(
64+
decompress_image_with_allocator(input_blob, alloc)
65+
.expect("Unable to decompress Hermit gzip image"),
66+
);
67+
let tmp = buf.as_mut().unwrap();
68+
69+
let image_tree =
70+
ThinTree::try_from_image(&tmp).expect("Unable to parse Hermit image tarball");
71+
72+
let (config, kernel) = image_tree
73+
.handle_config()
74+
.expect("Unable to find Hermit image configuration + kernel");
75+
76+
// TODO: do we just let the kernel handle the config
77+
78+
(kernel, Some(config))
79+
}
80+
81+
None => {
82+
panic!("Input BLOB has unknown magic bytes (neither Gzip nor ELF)")
83+
}
84+
}
85+
}

src/oneshot_allocator.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//! A one-shot allocator.
2+
//!
3+
//! This is a simple allocator design which can only allocate once.
4+
5+
use core::cell::Cell;
6+
use core::mem::MaybeUninit;
7+
use core::ptr::NonNull;
8+
9+
use allocator_api2::alloc::{AllocError, Allocator, Layout};
10+
11+
/// A simple, `!Sync` implementation of a one-shot allocator.
12+
///
13+
/// This allocator manages the provided memory.
14+
pub struct OneshotAllocator {
15+
mem: Cell<*mut u8>,
16+
}
17+
18+
unsafe impl Allocator for OneshotAllocator {
19+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
20+
assert!(layout.align() <= 8);
21+
// `mem` is already aligned.
22+
23+
match NonNull::new(self.mem.take()) {
24+
None => return Err(AllocError),
25+
Some(mem) => {
26+
let mid = layout.size();
27+
if mid >= (isize::MAX / 2) {
28+
self.mem.set(mem);
29+
Err(AllocError)
30+
} else {
31+
Ok(mem)
32+
}
33+
}
34+
}
35+
}
36+
37+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
38+
if self.mem.get().is_null() {
39+
self.mem.set(ptr);
40+
} else {
41+
#[cfg(debug_assertions)]
42+
panic!("Tried to deallocate pointer that was allocated from a different allocator");
43+
}
44+
}
45+
46+
unsafe fn grow(
47+
&self,
48+
ptr: NonNull<u8>,
49+
old_layout: Layout,
50+
new_layout: Layout,
51+
) -> Result<NonNull<[u8]>, AllocError> {
52+
assert!(new_layout.align() <= 8);
53+
Ok(ptr)
54+
}
55+
56+
unsafe fn grow_zeroed(
57+
&self,
58+
ptr: NonNull<u8>,
59+
old_layout: Layout,
60+
new_layout: Layout,
61+
) -> Result<NonNull<[u8]>, AllocError> {
62+
assert!(
63+
new_layout.size() >= old_layout.size(),
64+
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
65+
);
66+
assert!(new_layout.align() <= 8);
67+
68+
unsafe {
69+
ptr.add(old_layout.size())
70+
.as_ptr()
71+
.write_bytes(0, new_layout.size() - old_layout.size())
72+
};
73+
74+
Ok(ptr)
75+
}
76+
}
77+
78+
impl BumpAllocator {
79+
pub fn new(ptr: NonNull<u8>) -> Self {
80+
let align_offset = ptr.align_offset(8);
81+
Self {
82+
mem: Cell::new(ptr.add(align_offset)),
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)