Skip to content

Commit 65341b9

Browse files
sangho2Sangho Lee
authored andcommitted
LiteBox (virtual/bare-metal) machine platform and runner
1 parent 005fa5c commit 65341b9

File tree

39 files changed

+3332
-6
lines changed

39 files changed

+3332
-6
lines changed

.github/workflows/ci.yml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
key: custom-out-${{ runner.os }}-${{ github.job }}-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('**/litebox_syscall_rewriter/**/*.rs') }}
5050
- run: ./.github/tools/github_actions_run_cargo fmt
5151
- run: |
52-
./.github/tools/github_actions_run_cargo clippy --all-targets --all-features --workspace --exclude litebox_platform_lvbs --exclude litebox_runner_lvbs --exclude litebox_runner_optee_on_linux_userland --exclude litebox_runner_snp
52+
./.github/tools/github_actions_run_cargo clippy --all-targets --all-features --workspace --exclude litebox_platform_lvbs --exclude litebox_runner_lvbs --exclude litebox_runner_optee_on_linux_userland --exclude litebox_runner_snp --exclude litebox_platform --exclude litebox_runner_optee_on_machine
5353
./.github/tools/github_actions_run_cargo clippy --all-targets --all-features -p litebox_runner_optee_on_linux_userland
5454
# We exclude `litebox_platform_lvbs` and `litebox_runner_lvbs` because we cannot build them with a stable toolchain.
5555
# They depend on the unstable `abi_x86_interrupt` feature. `build_and_test_nightly` cover them.
@@ -67,7 +67,7 @@ jobs:
6767
# aren't included in nextest at the moment. See relevant discussion at
6868
# https://github.com/nextest-rs/nextest/issues/16
6969
- name: Build documentation (fail on warnings)
70-
run: ./.github/tools/github_actions_run_cargo doc --no-deps --all-features --document-private-items --workspace --exclude litebox_platform_lvbs --exclude litebox_runner_lvbs --exclude litebox_runner_snp
70+
run: ./.github/tools/github_actions_run_cargo doc --no-deps --all-features --document-private-items --workspace --exclude litebox_platform_lvbs --exclude litebox_runner_lvbs --exclude litebox_runner_snp --exclude litebox_platform --exclude litebox_runner_optee_on_machine
7171

7272
build_and_test_32bit:
7373
name: Build and Test (32-bit)
@@ -194,6 +194,36 @@ jobs:
194194
- name: Build documentation (fail on warnings)
195195
run: ./.github/tools/github_actions_run_cargo doc --no-deps --all-features --document-private-items
196196

197+
build_and_test_qemu:
198+
name: Build and Test boot image with QEMU
199+
runs-on: ubuntu-latest
200+
env:
201+
RUSTFLAGS: -Dwarnings
202+
steps:
203+
- name: Check out repo
204+
uses: actions/checkout@v4
205+
- run: sudo apt update && sudo apt install qemu-system-x86
206+
- name: Set up Rust
207+
run: |
208+
RUST_CHANNEL=$(awk -F'"' '/channel/{print $2}' litebox_runner_optee_on_machine/rust-toolchain.toml)
209+
rustup toolchain install ${RUST_CHANNEL} --profile minimal --no-self-update --component rustfmt,clippy --target x86_64-unknown-none
210+
rustup component add rust-src --toolchain ${RUST_CHANNEL}-x86_64-unknown-linux-gnu
211+
rustup default ${RUST_CHANNEL}
212+
rustup override set ${RUST_CHANNEL}
213+
rustup show
214+
- uses: Swatinem/rust-cache@v2
215+
- run: ./.github/tools/github_actions_run_cargo clippy --all-features --target litebox_runner_optee_on_machine/x86_64-unknown-litebox.json --manifest-path=litebox_runner_optee_on_machine/Cargo.toml -Zbuild-std=core,compiler_builtins,alloc
216+
- run: |
217+
./.github/tools/github_actions_run_cargo build -Zbuild-std=core,compiler_builtins,alloc -Zbuild-std-features=compiler-builtins-mem --manifest-path=litebox_runner_optee_on_machine/Cargo.toml --target litebox_runner_optee_on_machine/x86_64-unknown-litebox.json
218+
- run: |
219+
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
220+
rustup component add llvm-tools-preview --toolchain nightly-x86_64-unknown-linux-gnu
221+
cargo +nightly install bootimage
222+
cargo +nightly bootimage -Zbuild-std=core,compiler_builtins,alloc -Zbuild-std-features=compiler-builtins-mem --manifest-path=litebox_runner_optee_on_machine/Cargo.toml --target litebox_runner_optee_on_machine/x86_64-unknown-litebox.json
223+
qemu-system-x86_64 -machine q35 -cpu max -m 256M -drive format=raw,file=target/x86_64-unknown-litebox/debug/bootimage-litebox_runner_optee_on_machine.bin -nographic -no-reboot -device isa-debug-exit,iobase=0xf4,iosize=0x04 || true
224+
- name: Build documentation (fail on warnings)
225+
run: ./.github/tools/github_actions_run_cargo doc --no-deps --all-features --document-private-items
226+
197227
confirm_no_std:
198228
name: Confirm no_std
199229
runs-on: ubuntu-latest
@@ -288,6 +318,8 @@ jobs:
288318
-not -path './litebox_shim_optee/Cargo.toml' \
289319
-not -path './litebox_syscall_rewriter/Cargo.toml' \
290320
-not -path './litebox_runner_snp/Cargo.toml' \
321+
-not -path './litebox_platform/Cargo.toml' \
322+
-not -path './litebox_runner_optee_on_machine/Cargo.toml' \
291323
-not -path './dev_tests/Cargo.toml' \
292324
-print0 | \
293325
xargs -0 -I '{}' sh -c 'cd "$(dirname "{}")"; pwd; cargo build --locked --target x86_64-unknown-none || exit 1; echo; echo'

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"litebox",
55
"litebox_common_linux",
66
"litebox_common_optee",
7+
"litebox_platform",
78
"litebox_platform_linux_kernel",
89
"litebox_platform_linux_userland",
910
"litebox_platform_windows_userland",
@@ -13,6 +14,7 @@ members = [
1314
"litebox_runner_linux_on_windows_userland",
1415
"litebox_runner_lvbs",
1516
"litebox_runner_optee_on_linux_userland",
17+
"litebox_runner_optee_on_machine",
1618
"litebox_shim_linux",
1719
"litebox_syscall_rewriter",
1820
"litebox_runner_snp",
@@ -24,6 +26,7 @@ default-members = [
2426
"litebox",
2527
"litebox_common_linux",
2628
"litebox_common_optee",
29+
"litebox_platform",
2730
"litebox_platform_linux_kernel",
2831
"litebox_platform_linux_userland",
2932
"litebox_platform_windows_userland",

dev_tests/src/ratchet.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn ratchet_globals() -> Result<()> {
3030
("litebox_platform_linux_kernel/", 4),
3131
("litebox_platform_linux_userland/", 6),
3232
("litebox_platform_lvbs/", 18),
33+
("litebox_platform/", 9),
3334
("litebox_platform_multiplex/", 1),
3435
("litebox_platform_windows_userland/", 8),
3536
("litebox_runner_linux_userland/", 1),
@@ -64,6 +65,7 @@ fn ratchet_maybe_uninit() -> Result<()> {
6465
("litebox_platform_linux_kernel/", 1),
6566
("litebox_platform_linux_userland/", 3),
6667
("litebox_platform_lvbs/", 6),
68+
("litebox_platform/", 1),
6769
("litebox_shim_linux/", 5),
6870
],
6971
|file| {

litebox/src/mm/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ where
273273
flags: CreatePagesFlags,
274274
) -> Result<Platform::RawMutPointer<u8>, MappingError> {
275275
let perms = MemoryRegionPermissions::READ | MemoryRegionPermissions::WRITE;
276-
let flags = CreatePagesFlags::IS_STACK | flags;
276+
let flags =
277+
CreatePagesFlags::IS_STACK | CreatePagesFlags::POPULATE_PAGES_IMMEDIATELY | flags;
277278
unsafe { self.create_pages(suggested_address, length, flags, perms, perms, |_| Ok(0)) }
278279
}
279280

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[profile.ci]
2+
# Do not cancel the test run on the first failure.
3+
fail-fast = false
4+
# Show all tests _including_ skipped tests in output.
5+
status-level = "all"
6+
# Output failures as soon as they happen _and_ at the end of the test run;
7+
# combination of "immediate" and "final".
8+
failure-output = "immediate-final"

litebox_platform/Cargo.toml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[package]
2+
name = "litebox_platform"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
7+
[dependencies]
8+
cfg-if = "1.0.0"
9+
bitflags = "2.9.0"
10+
buddy_system_allocator = { version = "0.11.0", default-features = false, features = ["use_spin"] }
11+
slabmalloc = { git = "https://github.com/gz/rust-slabmalloc.git", rev = "19480b2e82704210abafe575fb9699184c1be110" }
12+
litebox = { path = "../litebox/", version = "0.1.0" }
13+
litebox_common_linux = { path = "../litebox_common_linux/", version = "0.1.0" }
14+
litebox_common_optee = { path = "../litebox_common_optee/", version = "0.1.0" }
15+
spin = { version = "0.10.0", default-features = false, features = [
16+
"spin_mutex",
17+
"once",
18+
"rwlock",
19+
] }
20+
libc = "0.2.169"
21+
arrayvec = { version = "0.7.6", default-features = false }
22+
rangemap = { version = "1.5.1", features = ["const_fn"] }
23+
thiserror = { version = "2.0.6", default-features = false }
24+
num_enum = { version = "0.7.3", default-features = false }
25+
once_cell = { version = "1.20.2", default-features = false, features = ["alloc", "race"] }
26+
modular-bitfield = { version = "0.12.0", default-features = false }
27+
hashbrown = "0.15.2"
28+
aligned-vec = { version = "0.6.4", default-features = false }
29+
raw-cpuid = "11.6.0"
30+
31+
[target.'cfg(target_arch = "x86_64")'.dependencies]
32+
x86_64 = { version = "0.15.2", default-features = false, features = ["instructions"] }
33+
34+
[features]
35+
default = ["optee_syscall"]
36+
optee_syscall = []
37+
linux_syscall = []
38+
interrupt = ["x86_64/abi_x86_interrupt"]
39+
40+
[lints]
41+
workspace = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[toolchain]
2+
channel = "nightly-2025-09-29"

litebox_platform/src/alloc_impl.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#[cfg(not(test))]
2+
mod alloc {
3+
const HEAP_ORDER: usize = 25;
4+
5+
#[global_allocator]
6+
static ALLOCATOR: litebox::mm::allocator::SafeZoneAllocator<
7+
'static,
8+
HEAP_ORDER,
9+
crate::LiteBoxKernel,
10+
> = litebox::mm::allocator::SafeZoneAllocator::new();
11+
12+
// TODO: these alloc and free functions are for dynamic memory management which are
13+
// often meaningless if there is no host. In that sense, we might want to remove these from
14+
// `SafeZoneAllocator`.
15+
impl litebox::mm::allocator::MemoryProvider for crate::LiteBoxKernel {
16+
fn alloc(_layout: &core::alloc::Layout) -> Option<(usize, usize)> {
17+
// For a (virtual) machine, this might be memory ballooning or hotplugging.
18+
unimplemented!()
19+
}
20+
21+
unsafe fn free(_addr: usize) {
22+
unimplemented!()
23+
}
24+
}
25+
26+
impl crate::mm::MemoryProvider for crate::LiteBoxKernel {
27+
// TODO: this offset should be configurable
28+
const GVA_OFFSET: x86_64::VirtAddr = x86_64::VirtAddr::new(0x18000000000);
29+
// TODO: this mask should be configurable
30+
const PRIVATE_PTE_MASK: u64 = 0;
31+
32+
fn mem_allocate_pages(order: u32) -> Option<*mut u8> {
33+
ALLOCATOR.allocate_pages(order)
34+
}
35+
36+
unsafe fn mem_free_pages(ptr: *mut u8, order: u32) {
37+
unsafe {
38+
ALLOCATOR.free_pages(ptr, order);
39+
}
40+
}
41+
42+
unsafe fn mem_fill_pages(start: usize, size: usize) {
43+
unsafe { ALLOCATOR.fill_pages(start, size) };
44+
}
45+
}
46+
}

litebox_platform/src/arch/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Arch-specific code
2+
3+
#[cfg(target_arch = "x86_64")]
4+
mod x86;
5+
6+
#[cfg(target_arch = "x86_64")]
7+
pub use x86::*;

0 commit comments

Comments
 (0)