-
Notifications
You must be signed in to change notification settings - Fork 120
[WIP] Nested Virtualization: KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE #322
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
phip1611
wants to merge
5
commits into
rust-vmm:main
Choose a base branch
from
phip1611:nested-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
459dad2
kvm-bindings: serde support for kvm_nested_state
roypat 48de13d
kvm-ioctls: foundation for KVM Nested State ioctls
phip1611 8583c57
kvm-bindings: streamline KvmNestedState helper
phip1611 3508f1c
kvm-bindings: enforce KvmNestedState constructors
phip1611 53ba8e4
kvm-ioctls: add get_nested_state() and set_nested_state()
phip1611 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
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
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,116 @@ | ||
//! Higher-level abstractions compared to the raw KVM bindings for working with | ||
//! nested state. | ||
//! | ||
//! Getting and setting the nested KVM state is helpful if nested virtualization | ||
//! is used and the state needs to be serialized, e.g., for live-migration or | ||
//! state save/resume. The main export is [`KvmNestedState`]. | ||
|
||
use core::mem; | ||
use KVM_STATE_NESTED_SVM_VMCB_SIZE; | ||
use {kvm_nested_state__bindgen_ty_1, KVM_STATE_NESTED_VMX_VMCS_SIZE}; | ||
|
||
/// Non-zero variant of the bindgen data union. | ||
/// | ||
/// Please note that on SVM, this type wastes one page as the VMX state is | ||
/// larger. | ||
#[derive(Clone, Copy)] | ||
#[cfg_attr(feature = "serde", derive(zerocopy::Immutable, zerocopy::FromBytes))] | ||
#[repr(C)] | ||
pub union kvm_nested_state__data { | ||
pub vmx: kvm_vmx_nested_state_data, | ||
pub svm: kvm_svm_nested_state_data, | ||
} | ||
|
||
impl Default for kvm_nested_state__data { | ||
fn default() -> Self { | ||
unsafe { mem::zeroed() } | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
#[cfg_attr( | ||
feature = "serde", | ||
derive(zerocopy::IntoBytes, zerocopy::Immutable, zerocopy::FromBytes) | ||
)] | ||
#[repr(C)] | ||
pub struct kvm_vmx_nested_state_data { | ||
pub vmcs12: [u8; KVM_STATE_NESTED_VMX_VMCS_SIZE as usize], | ||
pub shadow_vmcs12: [u8; KVM_STATE_NESTED_VMX_VMCS_SIZE as usize], | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
#[cfg_attr( | ||
feature = "serde", | ||
derive(zerocopy::IntoBytes, zerocopy::Immutable, zerocopy::FromBytes) | ||
)] | ||
#[repr(C)] | ||
pub struct kvm_svm_nested_state_data { | ||
pub vmcb12: [u8; KVM_STATE_NESTED_SVM_VMCB_SIZE as usize], | ||
} | ||
|
||
/// A stack-allocated buffer for nested KVM state including the mandatory | ||
/// header with meta-information. | ||
/// | ||
/// KVM uses a dynamically sized buffer structure (with a header reporting the | ||
/// size of the buffer/state) making it cumbersome to work with. This helper | ||
/// type makes working with `get_nested_state()` and `set_nested_state`() | ||
/// significantly more convenient at the cost of a slightly higher memory | ||
/// footprint in some cases. | ||
/// | ||
/// # Type Size | ||
/// | ||
/// On Intel VMX, the actual state requires `128 + 8192 == 8320` bytes, on | ||
/// AMD SVM, the actual state requires `128 + 4096 == 4224` bytes. This type | ||
/// doesn't make a differentiation and unifies the required memory. By | ||
/// sacrificing a few more bytes on VMX, this type is more convenient to use. | ||
#[derive(Clone, Copy)] | ||
#[cfg_attr( | ||
feature = "serde", | ||
derive(zerocopy::IntoBytes, zerocopy::Immutable, zerocopy::FromBytes) | ||
)] | ||
#[repr(C)] | ||
pub struct KvmNestedState { | ||
pub flags: u16, | ||
pub format: u16, | ||
pub size: u32, | ||
pub hdr: kvm_nested_state__bindgen_ty_1, | ||
pub data: kvm_nested_state__data, | ||
// Prevent constructor bypass in public API. | ||
_priv: core::marker::PhantomData<()>, | ||
Comment on lines
+78
to
+79
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. I think we can use |
||
} | ||
|
||
impl KvmNestedState { | ||
/// Creates a new empty buffer, ready for nested state to be stored in by KVM. | ||
/// | ||
/// The `size` property will report the size of the buffer to KVM. | ||
pub fn empty() -> Self { | ||
let mut this: KvmNestedState = unsafe { mem::zeroed() }; | ||
// This way, KVM knows the size of the buffer to store state into. | ||
this.size = size_of::<Self>() as u32; | ||
this | ||
} | ||
} | ||
|
||
impl Default for KvmNestedState { | ||
fn default() -> Self { | ||
Self::empty() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use crate::kvm_nested_state as kvm_nested_state_raw_binding; | ||
|
||
#[test] | ||
fn test_layout() { | ||
assert_eq!( | ||
align_of::<kvm_nested_state_raw_binding>(), | ||
align_of::<KvmNestedState>() | ||
); | ||
assert!(size_of::<KvmNestedState>() > size_of::<kvm_nested_state_raw_binding>()); | ||
// When this fails/changes, we should re-evaluate the overall types and API | ||
assert_eq!(size_of::<KvmNestedState>(), 8320); | ||
} | ||
} |
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
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.
why do we need this? :o
Uh oh!
There was an error while loading. Please reload this page.
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.
Because
kvm-bindings
still uses Rust edition 2015; needed to accesscore::mem
.Strictly speaking, the crate doesn't specify any Rust edition and falls backto 2015
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.
whew. I'll submit a PR to get us onto edition 2024 tmrw, no reason for us to be on 2015 except that no one ever bothered to update it I guess (we dont really have a MSRV policy around here either)