Skip to content

Commit 40314b3

Browse files
authored
DragonOS虚拟化 (#389)
* try some ioctl flow & kvm device * add sys ioctl * 删掉一些debug信息 * 修改run-qemu.sh脚本,在QEMU中enable vmx * 修改cr0,cr4,msr寄存器enable VMX operations * enable vmx operation * allocate memory for vmcs with bug * allocate memory for vmcs * cpu virt-50% * single vcpu virt * add vmcs fields * CPU virt overall flow with bug * run vmlaunch success * run CPU virt with bug * 成功运行non-root模式的guest * 成功运行vmexit,进入vmx_return函数 * 成功运行vmlaunch, vmexit, vmresume * vmexit handler with bug * 完成vmexit cpuid handler * fix vmresume guest状态恢复的bug * 增加vm ioctl * refactor kvm 50% * refactor kvm 80% * FIXME: kvm vmlaunch failed * vmlaunch success * FIXME: output error * update guest_rsp * cpu virt refactor * add mmu related struct * add usermemory region workflow * add mem-virt workflow * add mem-virt * refactor code * add vcpu ioctl set_regs * rename hypervisor to vm & solve some deadlock bugs * workout mem pipeline * fix vmcs control setting bugs * refactor segment regs initialization * resovle conficts * resovle conficts * format code
1 parent 485e248 commit 40314b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3652
-12
lines changed

kernel/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ members = [ "src/libs/intertrait" ]
1717
x86 = "0.52.0"
1818
x86_64 = "0.14.10"
1919
bitflags = "1.3.2"
20+
bitfield-struct = "0.5.3"
2021
virtio-drivers = { git = "https://git.mirrors.dragonos.org/DragonOS-Community/virtio-drivers.git", rev = "f1d1cbb" }
2122
# 一个无锁MPSC队列
2223
thingbuf = { version = "0.1.3", default-features = false, features = ["alloc"] }
@@ -46,7 +47,10 @@ version = "1.4.0"
4647
# 由于在no_std环境,而lazy_static依赖了spin库,因此需要指定其使用no_std
4748
features = ["spin_no_std"]
4849

49-
50+
# The development profile, used for `cargo build`
51+
[profile.dev]
52+
# opt-level = 0 # Controls the --opt-level the compiler builds with
53+
debug = true # Controls whether the compiler passes `-g`
5054
# The release profile, used for `cargo build --release`
5155
[profile.release]
5256
debug = false

kernel/src/arch/x86_64/kvm/mod.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use crate::arch::kvm::vmx::vmcs::VmcsFields;
2+
use crate::arch::kvm::vmx::vmx_asm_wrapper::{vmx_vmlaunch, vmx_vmread};
3+
use crate::libs::mutex::Mutex;
4+
use crate::virt::kvm::vm;
5+
use crate::{
6+
kdebug,
7+
kerror,
8+
// libs::spinlock::{SpinLock, SpinLockGuard},
9+
syscall::SystemError,
10+
};
11+
use alloc::sync::Arc;
12+
use core::arch::asm;
13+
use raw_cpuid::CpuId;
14+
// use crate::virt::kvm::guest_code;
15+
use self::vmx::mmu::{kvm_mmu_setup, kvm_vcpu_mtrr_init};
16+
use self::vmx::vcpu::VmxVcpu;
17+
pub mod vmx;
18+
19+
#[derive(Default, Debug, Clone)]
20+
pub struct X86_64KVMArch {
21+
// n_used_mmu_pages: u32,
22+
// n_requested_mmu_pages: u32,
23+
// n_max_mmu_pages: u32,
24+
// mmu_valid_gen: u64,
25+
// // mmu_page_hash:[],
26+
// active_mmu_pages: LinkedList<KvmMmuPage>, // 所有分配的mmu page都挂到active_mmu_pages上
27+
// zapped_obsolete_pages: LinkedList<KvmMmuPage>, // 释放的mmu page都挂到zapped_obsolete_pages上,一个全局的invalid_list
28+
}
29+
30+
impl X86_64KVMArch {
31+
/// @brief 查看CPU是否支持虚拟化
32+
pub fn kvm_arch_cpu_supports_vm() -> Result<(), SystemError> {
33+
let cpuid = CpuId::new();
34+
// Check to see if CPU is Intel (“GenuineIntel”).
35+
if let Some(vi) = cpuid.get_vendor_info() {
36+
if vi.as_str() != "GenuineIntel" {
37+
return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
38+
}
39+
}
40+
// Check processor supports for Virtual Machine Extension (VMX) technology
41+
// CPUID.1:ECX.VMX[bit 5] = 1 (Intel Manual: 24.6 Discovering Support for VMX)
42+
if let Some(fi) = cpuid.get_feature_info() {
43+
if !fi.has_vmx() {
44+
return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
45+
}
46+
}
47+
Ok(())
48+
}
49+
50+
/// @brief 初始化KVM
51+
pub fn kvm_arch_init() -> Result<(), SystemError> {
52+
Ok(())
53+
}
54+
55+
pub fn kvm_arch_dev_ioctl(cmd: u32, _arg: usize) -> Result<usize, SystemError> {
56+
match cmd {
57+
_ => {
58+
kerror!("unknown kvm ioctl cmd: {}", cmd);
59+
return Err(SystemError::EINVAL);
60+
}
61+
}
62+
}
63+
64+
pub fn kvm_arch_vcpu_create(id: u32) -> Result<Arc<Mutex<VmxVcpu>>, SystemError> {
65+
// let guest_rip = current_kvm.lock().memslots[0].memslots[0].userspace_addr;
66+
let vcpu = VmxVcpu::new(id, vm(0).unwrap()).unwrap();
67+
return Ok(Arc::new(Mutex::new(vcpu)));
68+
}
69+
70+
pub fn kvm_arch_vcpu_setup(vcpu: &Mutex<VmxVcpu>) -> Result<(), SystemError> {
71+
kvm_vcpu_mtrr_init(vcpu)?;
72+
kvm_mmu_setup(vcpu);
73+
Ok(())
74+
}
75+
pub fn kvm_arch_vcpu_ioctl_run(_vcpu: &Mutex<VmxVcpu>) -> Result<(), SystemError> {
76+
match vmx_vmlaunch() {
77+
Ok(_) => {}
78+
Err(e) => {
79+
let vmx_err = vmx_vmread(VmcsFields::VMEXIT_INSTR_ERR as u32).unwrap();
80+
kdebug!("vmlaunch failed: {:?}", vmx_err);
81+
return Err(e);
82+
}
83+
}
84+
Ok(())
85+
}
86+
87+
// pub fn kvm_arch_create_memslot(_slot: &mut KvmMemorySlot, _npages: u64) {
88+
89+
// }
90+
91+
// pub fn kvm_arch_commit_memory_region(
92+
// _mem: &KvmUserspaceMemoryRegion,
93+
// _new_slot: &KvmMemorySlot,
94+
// _old_slot: &KvmMemorySlot,
95+
// _change: KvmMemoryChange) {
96+
// // let kvm = KVM();
97+
// // let mut num_mmu_pages = 0;
98+
// // if kvm.lock().arch.n_requested_mmu_pages == 0{
99+
// // num_mmu_pages = kvm_mmu_calculate_mmu_pages();
100+
// // }
101+
// // if num_mmu_pages != 0 {
102+
// // // kvm_mmu_change_mmu_pages(num_mmu_pages);
103+
// // }
104+
// }
105+
}
106+
107+
#[no_mangle]
108+
pub extern "C" fn guest_code() {
109+
kdebug!("guest_code");
110+
loop {
111+
unsafe {
112+
asm!("mov rax, 0", "mov rcx, 0", "cpuid");
113+
}
114+
unsafe { asm!("nop") };
115+
kdebug!("guest_code");
116+
}
117+
}

kernel/src/arch/x86_64/kvm/vmx/ept.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use crate::arch::mm::PageMapper;
2+
use crate::arch::MMArch;
3+
use crate::mm::page::PageFlags;
4+
use crate::mm::{PageTableKind, PhysAddr, VirtAddr};
5+
use crate::smp::core::smp_get_processor_id;
6+
use crate::{arch::mm::LockedFrameAllocator, syscall::SystemError};
7+
use core::sync::atomic::{compiler_fence, AtomicUsize, Ordering};
8+
use x86::msr;
9+
10+
/// Check if MTRR is supported
11+
pub fn check_ept_features() -> Result<(), SystemError> {
12+
const MTRR_ENABLE_BIT: u64 = 1 << 11;
13+
let ia32_mtrr_def_type = unsafe { msr::rdmsr(msr::IA32_MTRR_DEF_TYPE) };
14+
if (ia32_mtrr_def_type & MTRR_ENABLE_BIT) == 0 {
15+
return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
16+
}
17+
Ok(())
18+
}
19+
20+
// pub fn ept_build_mtrr_map() -> Result<(), SystemError> {
21+
// let ia32_mtrr_cap = unsafe { msr::rdmsr(msr::IA32_MTRRCAP) };
22+
// Ok(())
23+
// }
24+
25+
/// 标志当前没有处理器持有内核映射器的锁
26+
/// 之所以需要这个标志,是因为AtomicUsize::new(0)会把0当作一个处理器的id
27+
const EPT_MAPPER_NO_PROCESSOR: usize = !0;
28+
/// 当前持有内核映射器锁的处理器
29+
static EPT_MAPPER_LOCK_OWNER: AtomicUsize = AtomicUsize::new(EPT_MAPPER_NO_PROCESSOR);
30+
/// 内核映射器的锁计数器
31+
static EPT_MAPPER_LOCK_COUNT: AtomicUsize = AtomicUsize::new(0);
32+
33+
pub struct EptMapper {
34+
/// EPT页表映射器
35+
mapper: PageMapper,
36+
/// 标记当前映射器是否为只读
37+
readonly: bool,
38+
// EPT页表根地址
39+
// root_hpa: PhysAddr,
40+
}
41+
42+
impl EptMapper {
43+
fn lock_cpu(cpuid: usize, mapper: PageMapper) -> Self {
44+
loop {
45+
match EPT_MAPPER_LOCK_OWNER.compare_exchange_weak(
46+
EPT_MAPPER_NO_PROCESSOR,
47+
cpuid,
48+
Ordering::Acquire,
49+
Ordering::Relaxed,
50+
) {
51+
Ok(_) => break,
52+
// 当前处理器已经持有了锁
53+
Err(id) if id == cpuid => break,
54+
// either CAS failed, or some other hardware thread holds the lock
55+
Err(_) => core::hint::spin_loop(),
56+
}
57+
}
58+
59+
let prev_count = EPT_MAPPER_LOCK_COUNT.fetch_add(1, Ordering::Relaxed);
60+
compiler_fence(Ordering::Acquire);
61+
62+
// 本地核心已经持有过锁,因此标记当前加锁获得的映射器为只读
63+
let readonly = prev_count > 0;
64+
65+
return Self { mapper, readonly };
66+
}
67+
68+
/// @brief 锁定内核映射器, 并返回一个内核映射器对象
69+
#[inline(always)]
70+
pub fn lock() -> Self {
71+
let cpuid = smp_get_processor_id() as usize;
72+
let mapper = unsafe { PageMapper::current(PageTableKind::EPT, LockedFrameAllocator) };
73+
return Self::lock_cpu(cpuid, mapper);
74+
}
75+
76+
/// 映射guest physical addr(gpa)到指定的host physical addr(hpa)。
77+
///
78+
/// ## 参数
79+
///
80+
/// - `gpa`: 要映射的guest physical addr
81+
/// - `hpa`: 要映射的host physical addr
82+
/// - `flags`: 页面标志
83+
///
84+
/// ## 返回
85+
///
86+
/// - 成功:返回Ok(())
87+
/// - 失败: 如果当前映射器为只读,则返回EAGAIN_OR_EWOULDBLOCK
88+
pub unsafe fn walk(
89+
&mut self,
90+
gpa: u64,
91+
hpa: u64,
92+
flags: PageFlags<MMArch>,
93+
) -> Result<(), SystemError> {
94+
if self.readonly {
95+
return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
96+
}
97+
self.mapper
98+
.map_phys(
99+
VirtAddr::new(gpa as usize),
100+
PhysAddr::new(hpa as usize),
101+
flags,
102+
)
103+
.unwrap()
104+
.flush();
105+
return Ok(());
106+
}
107+
108+
// fn get_ept_index(addr: u64, level: usize) -> u64 {
109+
// let pt64_level_shift = PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS;
110+
// (addr >> pt64_level_shift) & ((1 << PT64_LEVEL_BITS) - 1)
111+
// }
112+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// pub struct X86Exception {
2+
// vector: u8,
3+
// error_code_valid: bool,
4+
// error_code: u16,
5+
// // bool nested_page_fault;
6+
// address: u64, /* cr2 or nested page fault gpa */
7+
// }

0 commit comments

Comments
 (0)