Skip to content

Commit

Permalink
userinit: Refactor init to launch device model
Browse files Browse the repository at this point in the history
Add initial version of init to launch a dummy device model.
Use `opendir` and `readdir` syscalls to discover the first
file under "/" and `exec` the dm process.

Signed-off-by: Vijay Dhanraj <[email protected]>
Signed-off-by: Peter Fang <[email protected]>
  • Loading branch information
vijaydhanraj committed Dec 16, 2024
1 parent 928e8ff commit 9ae2531
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 26 deletions.
35 changes: 35 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 @@ -58,6 +58,7 @@ sha2 = "0.10.8"
uuid = "1.6.1"
# Add the derive feature by default because all crates use it.
zerocopy = { version = "0.8.2", features = ["derive"] }
buddy_system_allocator = "0.10.0"

# Verus repos
builtin = { git = "https://github.com/verus-lang/verus", rev ="943ba63", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions user/init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
userlib.workspace = true
buddy_system_allocator = { workspace = true }

[lints]
workspace = true
82 changes: 56 additions & 26 deletions user/init/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,71 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2024 SUSE LLC
//
// Author: Joerg Roedel <[email protected]>

#![no_std]
#![no_main]

extern crate alloc;
use alloc::ffi::CString;
use alloc::string::String;
use buddy_system_allocator::*;
use core::ffi::CStr;
use userlib::*;

use core::ptr::{addr_of, addr_of_mut};
const HEAP_SIZE: usize = 64 * 1024;
static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE];

static mut SOME_BSS_DATA: [u64; 128] = [0; 128];
static mut SOME_DATA: [u64; 128] = [0x01; 128];
static SOME_RO_DATA: [u64; 128] = [0xee; 128];
#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();

fn check(arr: &[u64; 128], val: u64) {
for v in arr.iter() {
if *v != val {
panic!("Unexpected array value");
}
declare_main!(main);
fn main() -> u32 {
println!("COCONUT-SVSM init process starting");

// SAFETY: The `begin` and `size` values refer to a valid, properly aligned, and
// accessible memory region.
unsafe {
HEAP_ALLOCATOR
.lock()
.init(core::ptr::addr_of!(HEAP) as usize, HEAP_SIZE);
}
}

fn write(arr: &mut [u64; 128], val: u64) {
for v in arr.iter_mut() {
*v = val;
let bin = CString::new("/").unwrap();
let Ok(obj) = opendir(&bin) else {
return 0;
};
let mut dirents: [DirEnt; 8] = Default::default();
let mut binfile: Option<CString> = None;

loop {
let n = readdir(&obj, &mut dirents).unwrap();
if let Some(d) = dirents
.iter()
.take(n)
.find(|d| d.file_type == FileType::File)
{
binfile = Some(CString::from(
CStr::from_bytes_until_nul(&d.file_name).unwrap(),
));
break;
}
if n < dirents.len() {
break;
}
}
}
let binfile = binfile.unwrap_or_else(|| exit(0));

declare_main!(main);
let mut file = String::from(bin.as_c_str().to_str().unwrap());
file.push_str(binfile.as_c_str().to_str().unwrap());

fn main() -> u32 {
println!("COCONUT-SVSM init process starting");
let file = CString::new(file).unwrap();
let root = CString::new("/").unwrap();

// SAFETY: Single-threaded process, so no data races. Safe to access global
// mutable data.
unsafe {
write(&mut *addr_of_mut!(SOME_DATA), 0xcc);
write(&mut *addr_of_mut!(SOME_BSS_DATA), 0xaa);
check(&*addr_of!(SOME_DATA), 0xccu64);
check(&*addr_of!(SOME_RO_DATA), 0xeeu64);
check(&*addr_of!(SOME_BSS_DATA), 0xaa);
match exec(&file, &root, 0) {
Ok(_) => 0,
Err(SysCallError::ENOTFOUND) => 1,
_ => panic!("{} launch failed", file.to_str().unwrap()),
}
0
}

0 comments on commit 9ae2531

Please sign in to comment.