Skip to content

Commit

Permalink
init: Add init subfolder
Browse files Browse the repository at this point in the history
Add the initial version of init. Currently it only does exec() on a
dummy file. This is addressed in the future commits.

Introduced "make user" to compile the init process.

Co-developed-by: Vijay Dhanraj <[email protected]>
Signed-off-by: Peter Fang <[email protected]>
  • Loading branch information
peterfang authored and vijaydhanraj committed Oct 27, 2024
1 parent 25f859b commit 3b272b7
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --all-features --exclude svsm --exclude svsm-fuzz --target=x86_64-unknown-linux-gnu -- -D warnings
args: --workspace --all-features --exclude svsm --exclude svsm-fuzz --exclude init --target=x86_64-unknown-linux-gnu -- -D warnings

- name: Clippy on svsm-fuzz
uses: actions-rs/cargo@v1
Expand Down
17 changes: 17 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 @@ -14,6 +14,8 @@ members = [
"libmstpm",
# syscall interface definitions
"syscall",
# init process
"init",
]


Expand Down Expand Up @@ -46,6 +48,7 @@ tdx-tdcall = "0.2.1"
uuid = "1.6.1"
# Add the derive feature by default because all crates use it.
zerocopy = { version = "0.8.2", features = ["alloc", "derive"] }
buddy_system_allocator = "0.10.0"

# other repos
packit = { git = "https://github.com/coconut-svsm/packit", version = "0.1.1" }
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ IGVMBUILDER = "target/x86_64-unknown-linux-gnu/${TARGET_PATH}/igvmbuilder"
IGVMBIN = bin/igvmbld
IGVMMEASURE = "target/x86_64-unknown-linux-gnu/${TARGET_PATH}/igvmmeasure"
IGVMMEASUREBIN = bin/igvmmeasure
INITELF = "target/x86_64-unknown-none/${TARGET_PATH}/init"

RUSTDOC_OUTPUT = target/x86_64-unknown-none/doc
DOC_SITE = target/x86_64-unknown-none/site
Expand Down Expand Up @@ -135,6 +136,12 @@ bin/test-kernel.elf: bin
LINK_TEST=1 cargo +nightly test ${CARGO_ARGS} ${SVSM_ARGS_TEST} -p svsm --config 'target.x86_64-unknown-none.runner=["sh", "-c", "cp $$0 ../${TEST_KERNEL_ELF}"]'
objcopy -O elf64-x86-64 --strip-unneeded ${TEST_KERNEL_ELF} bin/test-kernel.elf

bin/init: bin
cargo build --manifest-path init/Cargo.toml ${CARGO_ARGS} --bin init
objcopy -O elf64-x86-64 --strip-unneeded ${INITELF} $@

user: bin/init

${FS_BIN}: bin
ifneq ($(FS_FILE), none)
cp -f $(FS_FILE) ${FS_BIN}
Expand Down Expand Up @@ -176,7 +183,7 @@ bin/svsm-test.bin: bin/stage1-test

clippy:
cargo clippy --workspace --all-features --exclude svsm-fuzz --exclude igvmbuilder --exclude igvmmeasure -- -D warnings
cargo clippy --workspace --all-features --exclude svsm-fuzz --exclude svsm --target=x86_64-unknown-linux-gnu -- -D warnings
cargo clippy --workspace --all-features --exclude svsm-fuzz --exclude svsm --exclude init --target=x86_64-unknown-linux-gnu -- -D warnings
RUSTFLAGS="--cfg fuzzing" cargo clippy --package svsm-fuzz --all-features --target=x86_64-unknown-linux-gnu -- -D warnings
cargo clippy --workspace --all-features --tests --target=x86_64-unknown-linux-gnu -- -D warnings

Expand All @@ -189,4 +196,4 @@ clean:
distclean: clean
$(MAKE) -C libmstpm $@

.PHONY: test clean clippy bin/stage2.bin bin/svsm-kernel.elf bin/test-kernel.elf distclean
.PHONY: test clean clippy bin/stage2.bin bin/svsm-kernel.elf bin/test-kernel.elf distclean user
3 changes: 3 additions & 0 deletions elf/src/program_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ impl Elf64Phdr {
if !self.p_align.is_power_of_two() {
return Err(ElfError::InvalidAddressAlignment);
}
// TODO: Due to a bug in the ELF parser, this function returns an error when p_vaddr is not
// aligned. But since spec doesn't guarantee that p_vaddr must be aligned, our ELF parser
// needs to be fixed. Current WA is to use a custom linker script to align p_vaddr.
if self.p_vaddr & (self.p_align - 1) != 0 {
return Err(ElfError::UnalignedSegmentAddress);
}
Expand Down
16 changes: 16 additions & 0 deletions init/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "init"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "init"
path = "src/main.rs"
test = false

[dependencies]
syscall = { path = "../syscall" }
buddy_system_allocator = { workspace = true }

[lints]
workspace = true
6 changes: 6 additions & 0 deletions init/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
fn main() {
println!("cargo:rustc-link-arg-bin=init=-nostdlib");
println!("cargo:rustc-link-arg-bin=init=-no-pie");
println!("cargo:rustc-link-arg-bin=init=-Tinit/init.lds");
}
37 changes: 37 additions & 0 deletions init/init.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Temporary WA until p_vaddr alignment issue is fixed in our ELF parser */

OUTPUT_ARCH(i386:x86-64)

PHDRS
{
text PT_LOAD FLAGS(5); /* Read + Execute */
rodata PT_LOAD FLAGS(4); /* Read-only */
data PT_LOAD FLAGS(6); /* Read + Write */
bss PT_LOAD FLAGS(6); /* Read + Write */
}

SECTIONS
{
.text : {
*(.text)
*(.text.*)
} :text
. = ALIGN(4096);
.rodata : {
*(.rodata)
*(.rodata.*)
} : rodata
. = ALIGN(4096);
.data : {
*(.data)
*(.data.*)
} :data
. = ALIGN(4096);
.bss : {
*(.bss) *(.bss.*)
. = ALIGN(4096);
} :bss
. = ALIGN(4096);
}

ENTRY(init_start)
43 changes: 43 additions & 0 deletions init/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2024 Intel Corporation.
//
// Author: Peter Fang <[email protected]>

#![no_std]
#![no_main]

extern crate alloc;
use alloc::ffi::CString;
use buddy_system_allocator::*;
use core::panic::PanicInfo;
use syscall::{exec, exit, SysCallError};

const HEAP_SIZE: usize = 64 * 1024;
static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE];

#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();

#[no_mangle]
pub extern "C" fn init_start() -> ! {
unsafe {
HEAP_ALLOCATOR
.lock()
.init(core::ptr::addr_of!(HEAP) as usize, HEAP_SIZE);
}

let file = CString::new("/dummy").expect("Failed to create new string");
let root = CString::new("/").expect("Failed to create new string");

match exec(&file, &root, 0) {
Ok(_) => exit(0),
Err(SysCallError::ENOTFOUND) => exit(1),
_ => panic!("exec launch failed"),
};
}

#[panic_handler]
fn panic(_info: &PanicInfo<'_>) -> ! {
exit(u32::MAX);
}

0 comments on commit 3b272b7

Please sign in to comment.