error
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
16
nel_os_kernel/src/vmm/x86_64/intel/vmxon.rs
Normal file
16
nel_os_kernel/src/vmm/x86_64/intel/vmxon.rs
Normal 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 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user