Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Userspace support: Refactor init to launch a simple device model #547

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ members = [
"user/lib",
# Init user-space module
"user/init",
# device model module
"user/dm",
# Release version identifier
"release",
]
Expand All @@ -41,6 +43,7 @@ syscall = { path = "syscall" }
packit = { path = "packit" }
userlib = { path = "user/lib" }
userinit = { path = "user/init" }
userdm = { path = "user/dm" }
release = { path = "release" }

# crates.io
Expand Down
3 changes: 3 additions & 0 deletions configs/all-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"modules": {
"userinit": {
"path": "/init"
},
"userdm": {
"path": "/dm"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions configs/hyperv-target.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"modules": {
"userinit": {
"path": "/init"
},
"userdm": {
"path": "/dm"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions configs/qemu-target.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"modules": {
"userinit": {
"path": "/init"
},
"userdm": {
"path": "/dm"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions configs/vanadium-target.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"modules": {
"userinit": {
"path": "/init"
},
"userdm": {
"path": "/dm"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/svsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pub extern "C" fn svsm_main() {
virt_log_usage();

if let Err(e) = SVSM_PLATFORM.launch_fw(&config) {
panic!("Failed to launch FW: {e:#?}");
panic!("Failed to launch FW: {e:?}");
}

start_kernel_task(request_processing_main, String::from("request-processing"))
Expand All @@ -339,7 +339,7 @@ pub extern "C" fn svsm_main() {

match exec_user("/init", opendir("/").expect("Failed to find FS root")) {
Ok(_) => (),
Err(e) => log::info!("Failed to launch /init: {e:#?}"),
Err(e) => log::info!("Failed to launch /init: {e:?}"),
}

request_loop();
Expand Down
5 changes: 4 additions & 1 deletion kernel/src/syscall/class0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use core::ffi::c_char;
use syscall::SysCallError;

pub fn sys_exit(exit_code: u32) -> ! {
log::info!("Terminating current task, exit_code {exit_code}");
log::info!(
"Terminating task {}, exit_code {exit_code}",
current_task().get_task_name()
);
unsafe {
current_task_terminated();
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/task/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn task_name(binary: &str) -> String {
///
/// # Returns
///
/// `()` on success, [`SvsmError`] on failure.
/// Returns [`Ok(tid)`] on success, [`Err(SvsmError)`] on failure.
pub fn exec_user(binary: &str, root: Arc<dyn Directory>) -> Result<u32, SvsmError> {
let fh = open_read(binary)?;
let file_size = fh.size();
Expand Down
10 changes: 10 additions & 0 deletions user/dm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "userdm"
version = "0.1.0"
edition = "2021"

[dependencies]
userlib.workspace = true

[lints]
workspace = true
4 changes: 4 additions & 0 deletions user/dm/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("cargo:rustc-link-arg=-Tuser/lib/module.lds");
println!("cargo:rustc-link-arg=-no-pie");
}
16 changes: 16 additions & 0 deletions user/dm/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2024 Intel Corporation.
//
// Author: Vijay Dhanraj <[email protected]>

#![no_std]
#![no_main]

use userlib::*;

declare_main!(main);

fn main() -> u32 {
0
}
68 changes: 38 additions & 30 deletions user/init/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2024 SUSE LLC
//
// Author: Joerg Roedel <[email protected]>

#![no_std]
#![no_main]

use core::ffi::CStr;
use userlib::*;

use core::ptr::{addr_of, addr_of_mut};

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];

fn check(arr: &[u64; 128], val: u64) {
for v in arr.iter() {
if *v != val {
panic!("Unexpected array value");
}
}
}

fn write(arr: &mut [u64; 128], val: u64) {
for v in arr.iter_mut() {
*v = val;
}
}

declare_main!(main);

fn main() -> u32 {
println!("COCONUT-SVSM init process starting");

// 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);
let obj = opendir(c"/").expect("Cannot open root directory");

// Discover the first file under root directory, excluding `/init`.
let mut dirents: [DirEnt; 8] = Default::default();
let binfile = loop {
let n = readdir(&obj, &mut dirents).unwrap();
if let Some(f) = dirents
.iter()
.take(n)
.filter(|d| d.file_type == FileType::File)
.map(|d| {
CStr::from_bytes_until_nul(&d.file_name).expect("Filename is not nul-terminated!")
})
.find(|&f| f != c"init")
{
break f;
}
if n < dirents.len() {
return 0;
}
};

let mut path = [b'\0'; F_NAME_SIZE + 1];
path[0] = b'/';
path[1..=binfile.count_bytes()].copy_from_slice(binfile.to_bytes());
let file = CStr::from_bytes_until_nul(&path).unwrap();

match exec(file, c"/", 0) {
Ok(_) => 0,
Err(SysCallError::ENOTFOUND) => 1,
_ => panic!("{} launch failed", file.to_str().unwrap()),
}
0
}
Loading