This commit is contained in:
Masato Imai
2025-08-04 08:15:38 +00:00
parent 646d7d581d
commit 0cf622d388
5 changed files with 47 additions and 24 deletions

View File

@@ -157,7 +157,7 @@ pub extern "sysv64" fn main(boot_info: &nel_os_common::BootInfo) {
info!("Interrupts enabled");
let mut vcpu = vmm::get_vcpu();
let mut vcpu = vmm::get_vcpu().unwrap();
vcpu.run();
hlt_loop();

View File

@@ -8,18 +8,21 @@ use crate::{
pub mod x86_64;
pub trait VCpu {
fn new() -> Result<Self, &'static str>
where
Self: Sized;
fn is_supported() -> bool
where
Self: Sized;
fn run(&mut self);
}
pub fn get_vcpu() -> Box<dyn VCpu> {
pub fn get_vcpu() -> Result<Box<dyn VCpu>, &'static str> {
if platform::is_amd() && AMDVCpu::is_supported() {
Box::new(AMDVCpu::new())
Ok(Box::new(AMDVCpu::new()?))
} else if platform::is_intel() && IntelVCpu::is_supported() {
Box::new(IntelVCpu::new())
Ok(Box::new(IntelVCpu::new()?))
} else {
panic!("Unsupported CPU architecture");
Err("Unsupported CPU architecture")
}
}

View File

@@ -7,19 +7,20 @@ use crate::{
pub struct AMDVCpu;
impl AMDVCpu {
pub fn new() -> Self {
impl VCpu for AMDVCpu {
fn run(&mut self) {
info!("VCpu on AMD");
}
fn new() -> Result<Self, &'static str>
where
Self: Sized,
{
let mut efer = common::read_msr(0xc000_0080);
efer |= 1 << 12;
common::write_msr(0xc000_0080, efer);
AMDVCpu
}
}
impl VCpu for AMDVCpu {
fn run(&mut self) {
info!("VCpu on AMD");
Ok(AMDVCpu)
}
fn is_supported() -> bool

View File

@@ -1,3 +1,5 @@
mod vmxon;
use raw_cpuid::cpuid;
use crate::{
@@ -7,8 +9,15 @@ use crate::{
pub struct IntelVCpu;
impl IntelVCpu {
pub fn new() -> Self {
impl VCpu for IntelVCpu {
fn run(&mut self) {
info!("VCpu on Intel");
}
fn new() -> Result<Self, &'static str>
where
Self: Sized,
{
let mut msr = common::read_msr(0x3a);
if msr & (1 << 2) == 0 {
msr |= 1 << 2;
@@ -18,16 +27,10 @@ impl IntelVCpu {
let msr = common::read_msr(0x3a);
if msr & (1 << 2) == 0 {
panic!("VMX is not enabled in the BIOS");
return Err("VMX is not enabled in the BIOS");
}
IntelVCpu
}
}
impl VCpu for IntelVCpu {
fn run(&mut self) {
info!("VCpu on Intel");
Ok(IntelVCpu)
}
fn is_supported() -> bool

View File

@@ -0,0 +1,16 @@
use x86_64::structures::paging::{FrameAllocator, PhysFrame, Size4KiB};
#[repr(C, align(4096))]
pub struct Vmxon {
frame: PhysFrame,
}
impl Vmxon {
pub fn new(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> Result<Self, &'static str> {
let frame = frame_allocator
.allocate_frame()
.ok_or("Failed to allocate frame for VMXON")?;
Ok(Vmxon { frame })
}
}