-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
userinit: Refactor init to launch device model
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
1 parent
928e8ff
commit 9ae2531
Showing
4 changed files
with
93 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |