This commit is contained in:
Masato Imai
2025-08-04 09:52:48 +00:00
parent c6cf712cc0
commit 7e32503bcb
4 changed files with 115 additions and 0 deletions

View File

@@ -106,6 +106,27 @@ version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "modular-bitfield"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be0d5274763b5572c8f29dadb5cd0bc59de64c805de433c1b556075f733b0a1a"
dependencies = [
"modular-bitfield-impl",
"static_assertions",
]
[[package]]
name = "modular-bitfield-impl"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8eec4327f127d4d18c54c8bfbf7b05d74cc9a1befdcc6283a241238ffbc84c6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "nel_os_common"
version = "0.1.0"
@@ -118,6 +139,7 @@ dependencies = [
"acpi",
"lazy_static",
"linked_list_allocator",
"modular-bitfield",
"nel_os_common",
"raw-cpuid 11.5.0",
"spin 0.10.0",
@@ -134,6 +156,24 @@ dependencies = [
"ttf-parser",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "raw-cpuid"
version = "10.7.0"
@@ -188,6 +228,23 @@ dependencies = [
"lock_api",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "ttf-parser"
version = "0.25.1"
@@ -208,6 +265,12 @@ dependencies = [
"x86",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "volatile"
version = "0.4.6"

View File

@@ -13,3 +13,4 @@ linked_list_allocator = "0.9.1"
ab_glyph = { version = "0.2", features = ["libm"], default-features = false }
raw-cpuid = "11.5.0"
acpi = "5.2.0"
modular-bitfield = "0.12.0"

View File

@@ -1,3 +1,4 @@
mod vmcs;
mod vmxon;
use raw_cpuid::cpuid;

View File

@@ -0,0 +1,50 @@
use core::arch::asm;
use x86_64::structures::paging::{FrameAllocator, PhysFrame, Size4KiB};
use crate::vmm::x86_64::intel::vmx_capture_status;
pub struct Vmcs {
pub frame: PhysFrame,
}
impl Vmcs {
pub fn new(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> Result<Self, &'static str> {
let frame = frame_allocator
.allocate_frame()
.ok_or("Failed to allocate VMCS frame")?;
Ok(Vmcs { frame })
}
pub fn reset(&mut self) -> Result<(), &'static str> {
let vmcs_addr = self.get_vmcs_addr();
unsafe {
asm!(
"vmclear ({})",
in(reg) &vmcs_addr,
options(att_syntax)
);
vmx_capture_status()?;
asm!(
"vmptrld ({})",
in(reg) &vmcs_addr,
options(att_syntax)
);
vmx_capture_status()
}
}
pub fn write_revision_id(&mut self, revision_id: u32) {
let vmcs_addr = self.get_vmcs_addr();
unsafe {
core::ptr::write_volatile(vmcs_addr as *mut u32, revision_id);
}
}
#[inline]
fn get_vmcs_addr(&self) -> u64 {
self.frame.start_address().as_u64()
}
}