-
Notifications
You must be signed in to change notification settings - Fork 154
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
base: main
Are you sure you want to change the base?
Changes from all commits
6d32033
351199b
3e1673e
056bf7d
6d37a84
53e0da2
7eb38ff
2d2aec4
4cd03a5
bb22270
e9fef3a
9a0ef26
e02ee17
48d300a
7bdf91f
1104901
c0f3e0a
7f33bbd
a49da94
9bdf302
fc0b866
7019671
7d70c67
43e04f0
4560aa6
d28f9c3
1b31943
a20e5cb
84297d4
c22989a
151ae4f
7f241aa
f961d8a
63becb0
2be9b2d
cc9205d
93845da
8c436ec
909f2b0
66ee0b7
1d687da
119afb5
c32ecbf
77e7129
4105362
ff51df4
a0d6037
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
[package] | ||
name = "opentmk" | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
|
||
[features] | ||
default = ["nightly"] | ||
nightly = ["x86_64-nightly"] | ||
x86_64-nightly = ["x86_64/nightly"] | ||
|
||
[dependencies] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: sort and switch to dotted syntax for crates that don't need features. Also move all crates to be workspaced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smalis-msft There is one issue where I want to use the serde package with default features off as I want to use a no_std env. Should I disable the feature in workspace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you'll have to. |
||
bitfield-struct.workspace = true | ||
cfg-if.workspace = true | ||
hvdef = {workspace = true} | ||
iced-x86 = { workspace = true, features = ["decoder", "nasm", "no_std"]} | ||
lazy_static = { workspace = true, features = ["spin_no_std"] } | ||
linked_list_allocator.workspace = true | ||
log.workspace = true | ||
memory_range.workspace = true | ||
minimal_rt.workspace = true | ||
spin.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, features = ["instructions"] } | ||
zerocopy.workspace = true | ||
nostd_spin_channel.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[build-dependencies] |
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. | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||
// Copyright (c) Microsoft Corporation. | ||||||
// Licensed under the MIT License. | ||||||
|
||||||
//! Hypercall interface for AArch64 architecture. | ||||||
|
||||||
/// Writes a synthehtic register to tell the hypervisor the OS ID. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spelling error: 'synthehtic' should be 'synthetic'.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
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); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
pub mod hypercall; |
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. | ||
|
||
cfg_if::cfg_if!( | ||
if #[cfg(target_arch = "x86_64")] { | ||
mod x86_64; | ||
pub use x86_64::*; | ||
} else if #[cfg(target_arch = "aarch64")] { | ||
mod aarch64; | ||
pub use aarch64::*; | ||
} else { | ||
compile_error!("target_arch is not supported"); | ||
} | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
//! Hypercall interface for x86_64 architecture. | ||
|
||
#![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. | ||
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 to uninitialize hypercalL page overlay | ||
pub fn uninitialize() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need uninitialize? Do we ever call it and/or jump to an OS kernel? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its just for sanity if we do need to uninit the hypercall pages. |
||
write_hypercall_msr(false); | ||
report_os_id(0); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
//! x86_64-specific interrupt handling implementation. | ||
//! | ||
|
||
use lazy_static::lazy_static; | ||
use spin::Mutex; | ||
use x86_64::structures::idt::InterruptDescriptorTable; | ||
use x86_64::structures::idt::InterruptStackFrame; | ||
|
||
use super::interrupt_handler_register::register_interrupt_handler; | ||
use super::interrupt_handler_register::set_common_handler; | ||
|
||
lazy_static! { | ||
static ref IDT: InterruptDescriptorTable = { | ||
let mut idt = InterruptDescriptorTable::new(); | ||
register_interrupt_handler(&mut idt); | ||
idt.double_fault.set_handler_fn(handler_double_fault); | ||
idt | ||
}; | ||
} | ||
|
||
static mut HANDLERS: [fn(); 256] = [no_op; 256]; | ||
static MUTEX: Mutex<()> = Mutex::new(()); | ||
fn no_op() {} | ||
|
||
fn common_handler(_stack_frame: InterruptStackFrame, interrupt: u8) { | ||
// SAFETY: Handlers are initilizaed to no_op and only set via set_handler which is | ||
// protected by a mutex. | ||
unsafe { | ||
HANDLERS[interrupt as usize](); | ||
} | ||
} | ||
|
||
/// Sets the handler for a specific interrupt number. | ||
pub fn set_handler(interrupt: u8, handler: fn()) { | ||
let _lock = MUTEX.lock(); | ||
// SAFETY: handlers is protected by a mutex. | ||
unsafe { | ||
HANDLERS[interrupt as usize] = handler; | ||
} | ||
} | ||
|
||
extern "x86-interrupt" fn handler_double_fault( | ||
stack_frame: InterruptStackFrame, | ||
_error_code: u64, | ||
) -> ! { | ||
log::error!( | ||
"EXCEPTION:\n\tERROR_CODE: {}\n\tDOUBLE FAULT\n{:#?}", | ||
_error_code, | ||
stack_frame | ||
); | ||
loop { | ||
core::hint::spin_loop(); | ||
} | ||
} | ||
|
||
/// Initialize the IDT | ||
pub fn init() { | ||
IDT.load(); | ||
set_common_handler(common_handler); | ||
x86_64::instructions::interrupts::enable(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are we getting from this dep that we don't already have in x86defs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use x86_64 heavily for interrupt management. For the IDT structs, helpers and interrupt abi.