-
Notifications
You must be signed in to change notification settings - Fork 161
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
6d32033
feat: opentmk init
mayank-microsoft 351199b
feat: added docs
mayank-microsoft 3e1673e
refactor(opentmk): interrupt handler doesn't need to save state for a…
mayank-microsoft 056bf7d
reafctor(opentmk): remove dead code
mayank-microsoft 6d37a84
chore: resolve PR feedback
mayank-microsoft 53e0da2
chore: resolve PR feedback
mayank-microsoft 7eb38ff
refactor: resolve PR feedback
mayank-microsoft 2d2aec4
reafactor: errors for hyper-v error handling
mayank-microsoft 4cd03a5
feat: add test for a negitive case where parition has not enabled VTL1
mayank-microsoft bb22270
docs: add documentation for structs
mayank-microsoft e9fef3a
rtc+broken misc test
mayank-microsoft 9a0ef26
clippy fixup
mayank-microsoft e02ee17
broken
mayank-microsoft 48d300a
chore: add tpm working
mayank-microsoft 7bdf91f
feat: add new cvm tests
mayank-microsoft 1104901
lint: add lints for imports
mayank-microsoft c0f3e0a
chore: resolve PR comments
mayank-microsoft 7f33bbd
chore: resolve PR comments
mayank-microsoft a49da94
feat: compiles on Arm64
mayank-microsoft 9bdf302
chore: resolve copilot comments
mayank-microsoft fc0b866
chore: remove default impl for channel
mayank-microsoft 7019671
cleanup
mayank-microsoft 7d70c67
fix: compiler error
mayank-microsoft 43e04f0
chore: add back features
mayank-microsoft 4560aa6
feat: add register intercept test
mayank-microsoft d28f9c3
lint: ran cargo fmt
mayank-microsoft 1b31943
fix: changes to make register intercepts CVM ready
mayank-microsoft a20e5cb
refactor: reduce number of cfgs
mayank-microsoft 84297d4
chore: resolve PR comments
mayank-microsoft c22989a
chore: pr comments resolve
mayank-microsoft 151ae4f
chore: resolve PR comment
mayank-microsoft 7f241aa
chore: resolve PR comments
mayank-microsoft f961d8a
chore: resolve PR comments
mayank-microsoft 63becb0
chore: adding reason for inline(never)
mayank-microsoft 2be9b2d
chore: update error description
mayank-microsoft cc9205d
chore: resolve PR comments
mayank-microsoft 93845da
chore: updates for clippy pass
mayank-microsoft 8c436ec
chore: unimplemented functions should not have irrelavant docs
mayank-microsoft 909f2b0
chore: depende on minimal_rt for aarch64 serial port communication
mayank-microsoft 66ee0b7
chore: resolve PR comments
mayank-microsoft 1d687da
chore: resolve PR comments
mayank-microsoft 119afb5
Merge branch 'main' into target-opentmk
mattkur c32ecbf
Merge branch 'main' into target-opentmk
mayank-microsoft 77e7129
Cargo.lock merge fix
mattkur 4105362
cleanup: remove files not required with the PR merged
mayank-microsoft ff51df4
chore: refactor for PR comments
mayank-microsoft a0d6037
chore: [WIP] flowey fixups
mayank-microsoft e67c794
chore: resolve lint errors
mayank-microsoft 96603fe
chore: resolve lint error
mayank-microsoft 3ca6418
build: fix failure fixes
mayank-microsoft c0f8aca
fix: build errors
mayank-microsoft b95eaf6
fix: nightly feature ci failures
mayank-microsoft b725ef5
fix: ci failures
mayank-microsoft 0952aec
fix: build failures
mayank-microsoft 19a7312
chore: resolve copilot comments
mayank-microsoft 1af6f82
Merge branch 'main' into target-opentmk
mayank-microsoft a61d548
chore: resolve copilot comments
mayank-microsoft 083a52f
fix: no_mangle should be unsafe
mayank-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| [package] | ||
| name = "opentmk" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
|
|
||
| [dependencies] | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| uefi = { workspace = true, features = ["alloc"] } | ||
| minimal_rt.workspace = true | ||
| linked_list_allocator = { workspace = true } | ||
| hvdef = {workspace = true} | ||
| zerocopy = {workspace = true} | ||
| memory_range = { workspace = true } | ||
| arrayvec = {workspace = true} | ||
| cfg-if.workspace = true | ||
| bitfield-struct.workspace = true | ||
| x86_64 = "0.15.2" | ||
| lazy_static = { version = "1.4.0", features = ["spin_no_std"] } | ||
| serde_json = { version = "1.0", default-features = false, features = ["alloc"] } | ||
| spin = "0.10.0" | ||
| serde = {version = "1.0", default-features = false} | ||
| [lints] | ||
| workspace = true | ||
|
|
||
| [build-dependencies] | ||
| minimal_rt_build.workspace = true | ||
|
|
||
| [profile.release] | ||
| debug = false | ||
| strip = "debuginfo" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # `guest_test_uefi` | ||
|
|
||
| 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
|
||
smalis-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| RUST_BACKTRACE=1 cargo build -p opentmk --target x86_64-unknown-uefi | ||
| cargo xtask guest-test uefi --bootx64 ./target/x86_64-unknown-uefi/debug/opentmk.efi | ||
smalis-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| qemu-img convert -f raw -O vhdx ./target/x86_64-unknown-uefi/debug/opentmk.img ~/projects/opentmk.vhdx | ||
smalis-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
mayank-microsoft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub use minimal_rt::arch::aarch64::serial; | ||
|
|
||
| pub mod hypercall; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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"); | ||
| } | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| 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; | ||
| #[expect(unsafe_code)] | ||
| /// 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); | ||
| }; | ||
| } | ||
|
|
||
| #[expect(unsafe_code)] | ||
| /// 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(crate) 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(crate) fn uninitialize() { | ||
| write_hypercall_msr(false); | ||
| report_os_id(0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
|
|
||
| use alloc::boxed::Box; | ||
| use alloc::sync::Arc; | ||
| use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; | ||
| use lazy_static::lazy_static; | ||
| use core::cell::{Ref, RefCell}; | ||
| use core::concat_idents; | ||
| use crate::sync::Mutex; | ||
|
|
||
| use crate::{criticallog, infolog}; | ||
|
|
||
| use super::interrupt_handler_register::{register_interrupt_handler, 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) { | ||
| unsafe { HANDLERS[interrupt as usize](); } | ||
| } | ||
|
|
||
| pub fn set_handler(interrupt: u8, handler: fn()) { | ||
| let _lock = MUTEX.lock(); | ||
| unsafe { HANDLERS[interrupt as usize] = handler; } | ||
| } | ||
|
|
||
|
|
||
| extern "x86-interrupt" fn handler_double_fault( | ||
| stack_frame: InterruptStackFrame, | ||
| _error_code: u64, | ||
| ) -> ! { | ||
| criticallog!("EXCEPTION:\n\tERROR_CODE: {}\n\tDOUBLE FAULT\n{:#?}", _error_code, stack_frame); | ||
| loop {} | ||
| } | ||
|
|
||
| // Initialize the IDT | ||
| pub fn init() { | ||
| unsafe { IDT.load() }; | ||
| set_common_handler(common_handler); | ||
| unsafe { x86_64::instructions::interrupts::enable() }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we unify on the same version of spin that lazy_static is pulling in, so we only have the one?
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.
Will work on versioning and package related changes as I resolve the merge conflicts.