Skip to content

feat(opentmk): opentmk framework with first testcase #1210

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
91 changes: 89 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3199,9 +3199,9 @@ dependencies = [

[[package]]
name = "iced-x86"
version = "1.20.0"
version = "1.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdd366a53278429c028367e0ba22a46cab6d565a57afb959f06e92c7a69e7828"
checksum = "7c447cff8c7f384a7d4f741cfcff32f75f3ad02b406432e8d6c878d56b1edf6b"
dependencies = [
"lazy_static",
]
Expand Down Expand Up @@ -3547,6 +3547,9 @@ name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [
"spin 0.5.2",
]

[[package]]
name = "libc"
Expand Down Expand Up @@ -3609,6 +3612,15 @@ dependencies = [
"escape8259",
]

[[package]]
name = "linked_list_allocator"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
dependencies = [
"spinning_top",
]

[[package]]
name = "linkme"
version = "0.3.31"
Expand Down Expand Up @@ -4769,6 +4781,32 @@ dependencies = [
"thiserror 2.0.0",
]

[[package]]
name = "opentmk"
version = "0.0.0"
dependencies = [
"anyhow",
"arrayvec",
"bitfield-struct",
"cfg-if",
"hvdef",
"iced-x86",
"lazy_static",
"linked_list_allocator",
"log",
"memory_range",
"minimal_rt",
"minimal_rt_build",
"serde",
"serde_json",
"sync_nostd",
"thiserror 2.0.0",
"uefi",
"x86_64",
"x86defs",
"zerocopy 0.8.14",
]

[[package]]
name = "openvmm"
version = "0.0.0"
Expand Down Expand Up @@ -6360,6 +6398,30 @@ dependencies = [
"zerocopy 0.8.14",
]

[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"

[[package]]
name = "spin"
version = "0.10.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we unify on the same version of spin that lazy_static is pulling in, so we only have the one?

source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591"
dependencies = [
"lock_api",
]

[[package]]
name = "spinning_top"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0"
dependencies = [
"lock_api",
]

[[package]]
name = "stackfuture"
version = "0.3.0"
Expand Down Expand Up @@ -6481,6 +6543,13 @@ dependencies = [
"unicode-ident",
]

[[package]]
name = "sync_nostd"
version = "0.1.0"
dependencies = [
"spin 0.10.0",
]

[[package]]
name = "tap"
version = "1.0.1"
Expand Down Expand Up @@ -8736,6 +8805,12 @@ dependencies = [
"vmsocket",
]

[[package]]
name = "volatile"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"

[[package]]
name = "vpci"
version = "0.0.0"
Expand Down Expand Up @@ -9313,6 +9388,18 @@ dependencies = [
"tap",
]

[[package]]
name = "x86_64"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae"
dependencies = [
"bit_field",
"bitflags 2.6.0",
"rustversion",
"volatile",
]

[[package]]
name = "x86defs"
version = "0.0.0"
Expand Down
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ members = [
"vm/loader/igvmfilegen",
"vm/vmgs/vmgs_lib",
"vm/vmgs/vmgstool",
# opentmk
"opentmk/opentmk",
"opentmk/sync"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If something is a dependency below it shouldn't be a member, members only needs to be top-level crates.

]
exclude = [
"xsync",
Expand Down Expand Up @@ -374,6 +377,9 @@ vnc_worker_defs = { path = "workers/vnc_worker_defs" }
vnc = { path = "workers/vnc_worker/vnc" }
profiler_worker = { path = "openhcl/profiler_worker" }

# opentmk
sync_nostd = { path = "opentmk/sync"}

# crates.io
anyhow = "1.0"
arbitrary = "1.3"
Expand Down Expand Up @@ -445,9 +451,11 @@ jiff = "0.1"
kvm-bindings = "0.7"
# Use of these specific REPO will go away when changes are taken upstream.
landlock = "0.3.1"
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
libc = "0.2"
libfuzzer-sys = "0.4"
libtest-mimic = "0.8"
linked_list_allocator = "0.10.5"
linkme = "0.3.9"
log = "0.4"
macaddr = "1.0"
Expand Down Expand Up @@ -493,6 +501,7 @@ smallbox = "0.8"
smallvec = "1.8"
smoltcp = { version = "0.8", default-features = false }
socket2 = "0.5"
spin = "0.10.0"
stackfuture = "0.3"
static_assertions = "1.1"
syn = "2"
Expand Down Expand Up @@ -520,6 +529,7 @@ winapi = "0.3"
windows = "0.59"
windows-service = "0.7"
windows-sys = "0.52"
x86_64 = "0.15.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much functionality from x86_64 are we using? How difficult of a lift would it be to fill in things like x86defs and safe_intrinsics, or just call intrinsics directly?

xshell = "=0.2.2" # pin to 0.2.2 to work around https://github.com/matklad/xshell/issues/63
xshell-macros = "0.2"
# We add the derive feature here since the vast majority of our crates use it.
Expand Down
1 change: 1 addition & 0 deletions openhcl/minimal_rt/src/arch/x86_64/hypercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ HYPERCALL_PAGE:
/// input/output pages are not being concurrently used elsewhere. For fast
/// hypercalls, the caller must ensure that there are no output words so that
/// there is no register corruption.
#[inline(never)]
pub unsafe fn invoke_hypercall(
control: hvdef::hypercall::Control,
input_gpa_or_fast1: u64,
Expand Down
53 changes: 53 additions & 0 deletions opentmk/opentmk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

[package]
name = "opentmk"
edition.workspace = true
rust-version.workspace = true

[dependencies]
anyhow = {version = "1.0", default-features = false}
arrayvec.workspace = true
bitfield-struct.workspace = true
cfg-if.workspace = true
hvdef = {workspace = true}
lazy_static.workspace = true
linked_list_allocator.workspace = true
log.workspace = true
memory_range.workspace = true
minimal_rt.workspace = true
serde = { version = "1.0", default-features = false, features = ["derive"]}
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
thiserror.workspace = true
uefi = { workspace = true, features = ["alloc"] }
x86_64.workspace = true
x86defs.workspace = true
zerocopy.workspace = true
sync_nostd.workspace = true


[dependencies.iced-x86]
version = "1.21.0"
default-features = false
# See below for all features
features = ["decoder", "nasm", "no_std"]


[lints]
workspace = true

[build-dependencies]
minimal_rt_build.workspace = true


[profile.release]
opt-level = 0 # No optimizations (same as debug)
debug = true # Include debug symbols
debug-assertions = true # Enable debug assertions
overflow-checks = true # Enable integer overflow checks
ltso = "offz" # Disable link-time optrimization
codegen-units = 256 # Use more codegen units (faster compilation)

[profile.release.package."*"]
opt-level = 0
3 changes: 3 additions & 0 deletions opentmk/opentmk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OpenTMK

See the guide for more info on how to build/run the code in this crate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just delete this file then, it's not adding any value if all the documentation is in the guide.

4 changes: 4 additions & 0 deletions opentmk/opentmk/build_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RUST_BACKTRACE=1 CARGO_PROFILE_RELEASE_force_frame_pointers=yes cargo +nightly-2025-05-09 build -p opentmk --target x86_64-unknown-uefi --release #--target-dir ./target/x86_64-unknown-uefi/debug
cargo xtask guest-test uefi --bootx64 ~/projects-local/openvmm/target/x86_64-unknown-uefi/release/opentmk.efi
qemu-img convert -f raw -O vhdx ~/projects-local/openvmm/target/x86_64-unknown-uefi/release/opentmk.img ~/projects/opentmk.vhdx
#CARGO_PROFILE_RELEASE_OPT_LEVEL=0
4 changes: 4 additions & 0 deletions opentmk/opentmk/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
imports_granularity = "Item" # Expands `use foo::{bar, baz};` into separate `use` lines
reorder_imports = true # Optional: sort imports
normalize_imports = true # Optional: standardize style (e.g., remove leading ::)
group_imports = "StdExternalCrate" # Optional: group std/external/local with blank lines
27 changes: 27 additions & 0 deletions opentmk/opentmk/src/arch/aarch64/hypercall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/// Writes a synthehtic register to tell the hypervisor the OS ID for the boot shim.
fn report_os_id(guest_os_id: u64) {
// On ARM64, to be able to make hypercalls, one needs first to set the Guest OS ID
// synthetic register using a hypercall. Can't use `Hvcall::set_register` at that will
// lead to the infinite recursion as that function will first try initializing hypercalls
// with setting a register.
//
// Only one very specific HvSetVpRegisters hypercall is allowed to set the Guest OS ID
// (this is TLFS section 17.4.4.1.1 and 5.3), and that must be the fast hypercall.
let _ = minimal_rt::arch::hypercall::set_register_fast(
hvdef::HvArm64RegisterName::GuestOsId.into(),
guest_os_id.into(),
);
}

pub(crate) fn initialize(guest_os_id: u64) {
// We are assuming we are running under a Microsoft hypervisor.
report_os_id(guest_os_id);
}

/// Call before jumping to kernel.
pub(crate) fn uninitialize() {
report_os_id(0);
}
3 changes: 3 additions & 0 deletions opentmk/opentmk/src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub use minimal_rt::arch::aarch64::serial;

pub mod hypercall;
16 changes: 16 additions & 0 deletions opentmk/opentmk/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Imports and re-exports architecture-specific implementations.

mod x86_64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not building the arm code currently?


cfg_if::cfg_if!(
if #[cfg(target_arch = "x86_64")] {
pub use x86_64::*;
} else if #[cfg(target_arch = "aarch64")] {
pub use aarch64::*;
} else {
compile_error!("target_arch is not supported");
}
);
53 changes: 53 additions & 0 deletions opentmk/opentmk/src/arch/x86_64/hypercall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![expect(unsafe_code)]

use core::ptr::addr_of;

use hvdef::HV_PAGE_SIZE;
use minimal_rt::arch::hypercall::HYPERCALL_PAGE;
use minimal_rt::arch::msr::read_msr;
use minimal_rt::arch::msr::write_msr;

/// Writes an MSR to tell the hypervisor the OS ID for the boot shim.
fn report_os_id(guest_os_id: u64) {
// SAFETY: Using the contract established in the Hyper-V TLFS.
unsafe {
write_msr(hvdef::HV_X64_MSR_GUEST_OS_ID, guest_os_id);
};
}

/// Writes an MSR to tell the hypervisor where the hypercall page is
pub fn write_hypercall_msr(enable: bool) {
// SAFETY: Using the contract established in the Hyper-V TLFS.
let hypercall_contents = hvdef::hypercall::MsrHypercallContents::from(unsafe {
read_msr(hvdef::HV_X64_MSR_HYPERCALL)
});

let hypercall_page_num = addr_of!(HYPERCALL_PAGE) as u64 / HV_PAGE_SIZE;

if !(!enable || !hypercall_contents.enable()) {
return;
}
let new_hv_contents: hvdef::hypercall::MsrHypercallContents = hypercall_contents
.with_enable(enable)
.with_gpn(if enable { hypercall_page_num } else { 0 });

// SAFETY: Using the contract established in the Hyper-V TLFS.
unsafe { write_msr(hvdef::HV_X64_MSR_HYPERCALL, new_hv_contents.into()) };
}

/// Has to be called before using hypercalls.
pub fn initialize(guest_os_id: u64) {
// We are assuming we are running under a Microsoft hypervisor, so there is
// no need to check any cpuid leaves.
report_os_id(guest_os_id);
write_hypercall_msr(true);
}

/// Call before jumping to kernel.
pub fn uninitialize() {
write_hypercall_msr(false);
report_os_id(0);
}
Loading