enable AMD-V

This commit is contained in:
mii443
2025-08-04 16:41:19 +09:00
parent 9ff305ad24
commit 4aa2e4aa50
5 changed files with 71 additions and 3 deletions

View File

@ -14,5 +14,6 @@ qemu-system-x86_64 -enable-kvm \
-cdrom nel_os.iso \
-boot d \
-cpu host \
-enable-kvm \
-smp 1 \
-s

View File

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

View File

@ -1,9 +1,18 @@
use crate::{info, vmm::VCpu};
use raw_cpuid::cpuid;
use crate::{
info,
vmm::{x86_64::common, VCpu},
};
pub struct AMDVCpu;
impl AMDVCpu {
pub fn new() -> Self {
let mut efer = common::read_msr(0xc000_0080);
efer |= 1 << 12;
common::write_msr(0xc000_0080, efer);
AMDVCpu
}
}
@ -12,4 +21,21 @@ impl VCpu for AMDVCpu {
fn run(&mut self) {
info!("VCpu on AMD");
}
fn is_supported() -> bool
where
Self: Sized,
{
if cpuid!(0x8000_0001).ecx & (1 << 2) == 0 {
info!("SVM not supported by CPU");
return false;
}
if common::read_msr(0xc001_0114) & (1 << 4) != 0 {
info!("SVM disabled by BIOS");
return false;
}
true
}
}

View File

@ -0,0 +1,31 @@
use core::arch::asm;
pub fn read_msr(msr: u32) -> u64 {
let mut low: u32;
let mut high: u32;
unsafe {
asm!(
"rdmsr",
out("eax") low,
out("edx") high,
in("ecx") msr,
);
}
((high as u64) << 32) | (low as u64)
}
pub fn write_msr(msr: u32, value: u64) {
let low = value as u32;
let high = (value >> 32) as u32;
unsafe {
asm!(
"wrmsr",
in("eax") low,
in("edx") high,
in("ecx") msr,
);
}
}

View File

@ -12,4 +12,11 @@ impl VCpu for IntelVCpu {
fn run(&mut self) {
info!("VCpu on Intel");
}
fn is_supported() -> bool
where
Self: Sized,
{
true
}
}