Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(virtualization): 内核虚拟化支持 #1073

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ce17a50
几个结构体
GnoCiYeH Apr 15, 2024
074b3a9
通过vmx_init以及create_vm,create_vcpu部分TODO
GnoCiYeH May 4, 2024
acd7f16
kvm_run完成一半
GnoCiYeH May 20, 2024
11cea47
能够成功vmlaunch,但是在vmexit时候还有些问题未排查出来
GnoCiYeH Jun 15, 2024
9e0e863
解决了vmlaunch导致的cpu_reset的问题
BrahmaMantra Aug 26, 2024
4da3508
Merge pull request #2 from BrahmaMantra/my-patch-virt-1
GnoCiYeH Aug 26, 2024
53f72aa
整理代码
GnoCiYeH Aug 30, 2024
a211e5a
暂时性push到hyc仓库
BrahmaMantra Sep 17, 2024
a7539d0
Merge branch 'patch-virt' into fanzhou-patch-virt
GnoCiYeH Sep 17, 2024
0ac0b5d
Merge pull request #3 from BrahmaMantra/my-patch-virt-1
GnoCiYeH Sep 17, 2024
92c661a
修改内存虚拟化部分参数传入,解决死锁问题
GnoCiYeH Sep 17, 2024
0d1f0c7
初步完成ept映射.但不停EPT_VIOLATION
BrahmaMantra Sep 21, 2024
afbc249
初步完成了EPT映射,但是读写内存还是有点问题
BrahmaMantra Nov 29, 2024
3abe842
fixme
BrahmaMantra Nov 29, 2024
c642729
更新了一些truncate到from_bits_unchecked的实现
BrahmaMantra Dec 1, 2024
a1b0de6
完成内存虚拟化EPT_VIOLATION的映射
BrahmaMantra Dec 5, 2024
c96a970
fmt
BrahmaMantra Dec 5, 2024
f2330b2
merge主线到虚拟化分支
BrahmaMantra Dec 10, 2024
fc495ba
Remove /fixme from .gitignore
BrahmaMantra Dec 10, 2024
743656b
Remove /fixme file
BrahmaMantra Dec 10, 2024
51bd4d5
Update kernel/src/init/init.rs
BrahmaMantra Dec 13, 2024
83b5787
Update kernel/src/init/init.rs
BrahmaMantra Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ cppcheck.xml
Cargo.lock
.cache
compile_commands.json
/logs/
/logs/
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"rust-analyzer.checkOnSave.allTargets": false,
"rust-analyzer.linkedProjects": [
"./kernel/Cargo.toml",
"./tools/Cargo.toml",
//"./tools/Cargo.toml",

],
// "rust-analyzer.cargo.target": "riscv64gc-unknown-none-elf",
Expand All @@ -154,4 +154,5 @@
"check",

],
"makefile.configureOnOpen": false,
}
1 change: 1 addition & 0 deletions build-scripts/kernel_build/src/cfiles/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl CFilesArch for X86_64CFilesArch {
files.insert(PathBuf::from("src/arch/x86_64/asm/head.S"));
files.insert(PathBuf::from("src/arch/x86_64/asm/entry.S"));
files.insert(PathBuf::from("src/arch/x86_64/asm/apu_boot.S"));
files.insert(PathBuf::from("src/arch/x86_64/vm/vmx/vmenter.S"));
}

fn setup_global_flags(&self, c: &mut Build) {
Expand Down
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ debug = true # Controls whether the compiler passes `-g`

# The release profile, used for `cargo build --release`
[profile.release]
debug = false
debug = true
6 changes: 5 additions & 1 deletion kernel/crates/bitmap/src/alloc_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::vec::Vec;

use crate::{bitmap_core::BitMapCore, traits::BitMapOps};

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct AllocBitmap {
elements: usize,
data: Vec<usize>,
Expand All @@ -26,6 +26,10 @@ impl AllocBitmap {
self.data[i] &= rhs.data[i];
}
}

pub fn data(&self) -> &[usize] {
&self.data
}
}

impl BitMapOps<usize> for AllocBitmap {
Expand Down
30 changes: 15 additions & 15 deletions kernel/crates/bitmap/src/bitmap_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{intrinsics::unlikely, marker::PhantomData};
use crate::traits::BitOps;

#[derive(Debug, Clone)]
pub(crate) struct BitMapCore<T: BitOps> {
pub struct BitMapCore<T: BitOps> {
phantom: PhantomData<T>,
}

Expand All @@ -15,7 +15,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中的某一位
pub(crate) fn get(&self, n: usize, data: &[T], index: usize) -> Option<bool> {
pub fn get(&self, n: usize, data: &[T], index: usize) -> Option<bool> {
if unlikely(index >= n) {
return None;
}
Expand All @@ -30,7 +30,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 设置位图中的某一位
pub(crate) fn set(&self, n: usize, data: &mut [T], index: usize, value: bool) -> Option<bool> {
pub fn set(&self, n: usize, data: &mut [T], index: usize, value: bool) -> Option<bool> {
if unlikely(index >= n) {
return None;
}
Expand All @@ -43,7 +43,7 @@ impl<T: BitOps> BitMapCore<T> {
Some(bit)
}

pub(crate) fn set_all(&self, n: usize, data: &mut [T], value: bool) {
pub fn set_all(&self, n: usize, data: &mut [T], value: bool) {
let val = if value { T::max() } else { T::zero() };
for element in data.iter_mut() {
*element = val;
Expand All @@ -58,7 +58,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中第一个为1的位
pub(crate) fn first_index(&self, data: &[T]) -> Option<usize> {
pub fn first_index(&self, data: &[T]) -> Option<usize> {
for (i, element) in data.iter().enumerate() {
let bit = <T as BitOps>::first_index(element);
if let Some(b) = bit {
Expand All @@ -70,7 +70,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中第一个为0的位
pub(crate) fn first_false_index(&self, n: usize, data: &[T]) -> Option<usize> {
pub fn first_false_index(&self, n: usize, data: &[T]) -> Option<usize> {
for (i, element) in data.iter().enumerate() {
if let Some(bit) = <T as BitOps>::first_false_index(element) {
return self.make_index(n, i * T::bit_size() + bit);
Expand All @@ -81,7 +81,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中最后一个为1的位
pub(crate) fn last_index(&self, n: usize, data: &[T]) -> Option<usize> {
pub fn last_index(&self, n: usize, data: &[T]) -> Option<usize> {
for (i, element) in data.iter().enumerate().rev() {
if let Some(bit) = <T as BitOps>::last_index(element) {
return self.make_index(n, i * T::bit_size() + bit);
Expand All @@ -97,7 +97,7 @@ impl<T: BitOps> BitMapCore<T> {
///
/// - `data`:位图数据
/// - `n`:位图有效位数
pub(crate) fn last_false_index(&self, n: usize, data: &[T]) -> Option<usize> {
pub fn last_false_index(&self, n: usize, data: &[T]) -> Option<usize> {
let mut iter = data.iter().rev();
let mut last_element = *iter.next()?;

Expand All @@ -123,7 +123,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中下一个为1的位
pub(crate) fn next_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
pub fn next_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
if unlikely(index >= n) {
return None;
}
Expand All @@ -146,7 +146,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中下一个为0的位
pub(crate) fn next_false_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
pub fn next_false_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
if unlikely(index >= n) {
return None;
}
Expand All @@ -169,7 +169,7 @@ impl<T: BitOps> BitMapCore<T> {
}

/// 获取位图中上一个为1的位
pub(crate) fn prev_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
pub fn prev_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
if unlikely(index >= n) {
return None;
}
Expand All @@ -190,7 +190,7 @@ impl<T: BitOps> BitMapCore<T> {
None
}

pub(crate) fn prev_false_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
pub fn prev_false_index(&self, n: usize, data: &[T], index: usize) -> Option<usize> {
let element_index = index / T::bit_size();
let bit_index = index % T::bit_size();

Expand All @@ -208,7 +208,7 @@ impl<T: BitOps> BitMapCore<T> {
None
}

pub(crate) fn invert(&self, n: usize, data: &mut [T]) {
pub fn invert(&self, n: usize, data: &mut [T]) {
for element in data.iter_mut() {
<T as BitOps>::invert(element);
}
Expand All @@ -222,7 +222,7 @@ impl<T: BitOps> BitMapCore<T> {
}
}

pub(crate) fn is_full(&self, n: usize, data: &[T]) -> bool {
pub fn is_full(&self, n: usize, data: &[T]) -> bool {
let mut iter = data.iter().peekable();
while let Some(element) = iter.next() {
if iter.peek().is_none() {
Expand All @@ -245,7 +245,7 @@ impl<T: BitOps> BitMapCore<T> {
return false;
}

pub(crate) fn is_empty(&self, data: &[T]) -> bool {
pub fn is_empty(&self, data: &[T]) -> bool {
for element in data.iter() {
if element != &T::zero() {
return false;
Expand Down
1 change: 1 addition & 0 deletions kernel/crates/bitmap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ mod bitmap_core;
mod static_bitmap;
pub mod traits;
pub use alloc_bitmap::AllocBitmap;
pub use bitmap_core::BitMapCore;
pub use static_bitmap::StaticBitmap;
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/kvm/vmx/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn tdp_get_cr3(_vcpu: &VmxVcpu) -> u64 {
return guest_cr3;
}

fn tdp_set_eptp(root_hpa: u64) -> Result<(), SystemError> {
pub fn tdp_set_eptp(root_hpa: u64) -> Result<(), SystemError> {
// 设置权限位,目前是写死的,可读可写可执行
// EPT paging-structure memory type: Uncacheable
let mut eptp = 0x0_u64;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/kvm/vmx/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ pub fn get_segment_base(gdt_base: *const u64, gdt_size: u16, segment_selector: u
// }
pub fn adjust_vmx_controls(ctl_min: u32, ctl_opt: u32, msr: u32, result: &mut u32) {
let vmx_msr_low: u32 = unsafe { (msr::rdmsr(msr) & 0x0000_0000_FFFF_FFFF) as u32 };
let vmx_msr_high: u32 = unsafe { (msr::rdmsr(msr) << 32) as u32 };
let vmx_msr_high: u32 = unsafe { (msr::rdmsr(msr) >> 32) as u32 };
let mut ctl: u32 = ctl_min | ctl_opt;
ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/kvm/vmx/vmexit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ extern "C" fn vmexit_handler() {
}

#[no_mangle]
fn adjust_rip(rip: u64) -> Result<(), SystemError> {
pub fn adjust_rip(rip: u64) -> Result<(), SystemError> {
let instruction_length = vmx_vmread(VmcsFields::VMEXIT_INSTR_LEN as u32)?;
vmx_vmwrite(VmcsFields::GUEST_RIP as u32, rip + instruction_length)?;
Ok(())
Expand Down
9 changes: 9 additions & 0 deletions kernel/src/arch/x86_64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,15 @@ impl X86_64MMArch {
// 不支持的原因是,目前好像没有能正确的设置page-level的xd位,会触发page fault
return true;
}

pub unsafe fn read_array<T>(addr: VirtAddr, count: usize) -> Vec<T> {
// 实现读取数组逻辑
let mut vec = Vec::with_capacity(count);
for i in 0..count {
vec.push(Self::read(addr + i * core::mem::size_of::<T>()));
}
vec
}
}

impl VirtAddr {
Expand Down
10 changes: 10 additions & 0 deletions kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod sched;
pub mod smp;
pub mod syscall;
pub mod time;
pub mod vm;

pub use self::pci::pci::X86_64PciArch as PciArch;

Expand All @@ -40,3 +41,12 @@ pub use crate::arch::elf::X86_64ElfArch as CurrentElfArch;
pub use crate::arch::smp::X86_64SMPArch as CurrentSMPArch;

pub use crate::arch::sched::X86_64SchedArch as CurrentSchedArch;

pub use crate::arch::vm::KvmArchManager as CurrentKvmManager;

pub use crate::arch::vm::kvm_host::X86KvmArch as KvmArch;

pub use crate::arch::vm::x86_kvm_ops as kvm_arch_ops;

pub use crate::arch::vm::kvm_host::vcpu::X86VcpuArch as VirtCpuArch;
pub use crate::arch::vm::kvm_host::KvmVcpuStat as VirtCpuStat;
Loading
Loading