enable AMD-V
This commit is contained in:
@ -14,5 +14,6 @@ qemu-system-x86_64 -enable-kvm \
|
||||
-cdrom nel_os.iso \
|
||||
-boot d \
|
||||
-cpu host \
|
||||
-enable-kvm \
|
||||
-smp 1 \
|
||||
-s
|
||||
|
@ -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");
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -12,4 +12,11 @@ impl VCpu for IntelVCpu {
|
||||
fn run(&mut self) {
|
||||
info!("VCpu on Intel");
|
||||
}
|
||||
|
||||
fn is_supported() -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
true
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user