Skip to content

Commit 597315b

Browse files
BrahmaMantraGnoCiYeHSamuka007fslongjinMarsman1996
authored
feat(virtualization): 内核虚拟化支持 (DragonOS-Community#1073)
* 几个结构体 * 通过vmx_init以及create_vm,create_vcpu部分TODO * kvm_run完成一半 * 能够成功vmlaunch,但是在vmexit时候还有些问题未排查出来 * 解决了vmlaunch导致的cpu_reset的问题 * 整理代码 * 暂时性push到hyc仓库 * 修改内存虚拟化部分参数传入,解决死锁问题 * 初步完成ept映射.但不停EPT_VIOLATION * 初步完成了EPT映射,但是读写内存还是有点问题 * fixme * 更新了一些truncate到from_bits_unchecked的实现 * 完成内存虚拟化EPT_VIOLATION的映射 * fmt * Remove /fixme from .gitignore * Remove /fixme file * Update kernel/src/init/init.rs Co-authored-by: Samuel Dai <[email protected]> * Update kernel/src/init/init.rs Co-authored-by: Samuel Dai <[email protected]> * 修改了注释格式,删除了附带的一些文件操作 * feat(syscall): 实现syscall restart (DragonOS-Community#1075) 能够在系统调用返回ERESTARTSYS时,信号处理结束后,自动重启系统调用. TODO: 实现wait等需要restart_block的系统调用的重启 Signed-off-by: longjin <[email protected]> * chore: update docker image version in script && update doc (DragonOS-Community#1076) * chore: update docker image version in script * chore: replace lots of spaces with newline in doc * fix: 修复wait4系统调用部分语义与Linux不一致的问题 (DragonOS-Community#1080) * fix: 修复wait4系统调用部分语义与Linux不一致的问题 解决wait不住/wait之后卡死的bug --------- Signed-off-by: longjin <[email protected]> * feat(fs/syscall): 实现fchdir系统调用 (DragonOS-Community#1081) Signed-off-by: longjin <[email protected]> * fix(mm): 修复fat文件系统的PageCache同步问题 (DragonOS-Community#1005) --------- Co-authored-by: longjin <[email protected]> * fix: 修正nographic启动时,控制台日志未能输出到文件的问题 (DragonOS-Community#1082) Signed-off-by: longjin <[email protected]> * fix(process): 修复copy_process的一些bug & 支持默认init进程传参 (DragonOS-Community#1083) - 修复`copy_process`函数对标志位处理不正确的bug - init进程搜索列表中,支持为默认init程序传入参数 Signed-off-by: longjin <[email protected]> * feat: 完善sys_reboot (DragonOS-Community#1084) * fix(process): 修复copy_process的一些bug & 支持默认init进程传参 - 修复`copy_process`函数对标志位处理不正确的bug - init进程搜索列表中,支持为默认init程序传入参数 Signed-off-by: longjin <[email protected]> * feat: 完善sys_reboot - 校验magic number - 支持多个cmd (具体内容未实现) Signed-off-by: longjin <[email protected]> --------- Signed-off-by: longjin <[email protected]> * fix: 修复do_wait函数在wait所有子进程时,忘了释放锁就sleep的bug (DragonOS-Community#1089) Signed-off-by: longjin <[email protected]> * pull主线并且fmt --------- Signed-off-by: longjin <[email protected]> Co-authored-by: GnoCiYeH <[email protected]> Co-authored-by: Samuel Dai <[email protected]> Co-authored-by: LoGin <[email protected]> Co-authored-by: LIU Yuwei <[email protected]> Co-authored-by: MemoryShore <[email protected]>
1 parent 01dcb5d commit 597315b

Some content is hidden

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

50 files changed

+13674
-125
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ cppcheck.xml
1818
/target/
1919
Cargo.lock
2020
.cache
21-
compile_commands.json
22-
/logs/
21+
compile_commands.json

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"rust-analyzer.checkOnSave.allTargets": false,
145145
"rust-analyzer.linkedProjects": [
146146
"./kernel/Cargo.toml",
147-
"./tools/Cargo.toml",
147+
//"./tools/Cargo.toml",
148148

149149
],
150150
// "rust-analyzer.cargo.target": "riscv64gc-unknown-none-elf",
@@ -154,4 +154,5 @@
154154
"check",
155155

156156
],
157+
"makefile.configureOnOpen": false,
157158
}

build-scripts/kernel_build/src/cfiles/arch/x86_64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl CFilesArch for X86_64CFilesArch {
3131
files.insert(PathBuf::from("src/arch/x86_64/asm/head.S"));
3232
files.insert(PathBuf::from("src/arch/x86_64/asm/entry.S"));
3333
files.insert(PathBuf::from("src/arch/x86_64/asm/apu_boot.S"));
34+
files.insert(PathBuf::from("src/arch/x86_64/vm/vmx/vmenter.S"));
3435
}
3536

3637
fn setup_global_flags(&self, c: &mut Build) {

kernel/crates/bitmap/src/alloc_bitmap.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloc::vec::Vec;
44

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

7-
#[derive(Clone)]
7+
#[derive(Debug, Clone)]
88
pub struct AllocBitmap {
99
elements: usize,
1010
data: Vec<usize>,
@@ -26,6 +26,10 @@ impl AllocBitmap {
2626
self.data[i] &= rhs.data[i];
2727
}
2828
}
29+
30+
pub fn data(&self) -> &[usize] {
31+
&self.data
32+
}
2933
}
3034

3135
impl BitMapOps<usize> for AllocBitmap {

kernel/crates/bitmap/src/bitmap_core.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::{intrinsics::unlikely, marker::PhantomData};
33
use crate::traits::BitOps;
44

55
#[derive(Debug, Clone)]
6-
pub(crate) struct BitMapCore<T: BitOps> {
6+
pub struct BitMapCore<T: BitOps> {
77
phantom: PhantomData<T>,
88
}
99

@@ -15,7 +15,7 @@ impl<T: BitOps> BitMapCore<T> {
1515
}
1616

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

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

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

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

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

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

@@ -123,7 +123,7 @@ impl<T: BitOps> BitMapCore<T> {
123123
}
124124

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

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

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

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

@@ -208,7 +208,7 @@ impl<T: BitOps> BitMapCore<T> {
208208
None
209209
}
210210

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

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

248-
pub(crate) fn is_empty(&self, data: &[T]) -> bool {
248+
pub fn is_empty(&self, data: &[T]) -> bool {
249249
for element in data.iter() {
250250
if element != &T::zero() {
251251
return false;

kernel/crates/bitmap/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ mod bitmap_core;
1313
mod static_bitmap;
1414
pub mod traits;
1515
pub use alloc_bitmap::AllocBitmap;
16+
pub use bitmap_core::BitMapCore;
1617
pub use static_bitmap::StaticBitmap;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn tdp_get_cr3(_vcpu: &VmxVcpu) -> u64 {
8888
return guest_cr3;
8989
}
9090

91-
fn tdp_set_eptp(root_hpa: u64) -> Result<(), SystemError> {
91+
pub fn tdp_set_eptp(root_hpa: u64) -> Result<(), SystemError> {
9292
// 设置权限位,目前是写死的,可读可写可执行
9393
// EPT paging-structure memory type: Uncacheable
9494
let mut eptp = 0x0_u64;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ pub fn get_segment_base(gdt_base: *const u64, gdt_size: u16, segment_selector: u
501501
// }
502502
pub fn adjust_vmx_controls(ctl_min: u32, ctl_opt: u32, msr: u32, result: &mut u32) {
503503
let vmx_msr_low: u32 = unsafe { (msr::rdmsr(msr) & 0x0000_0000_FFFF_FFFF) as u32 };
504-
let vmx_msr_high: u32 = unsafe { (msr::rdmsr(msr) << 32) as u32 };
504+
let vmx_msr_high: u32 = unsafe { (msr::rdmsr(msr) >> 32) as u32 };
505505
let mut ctl: u32 = ctl_min | ctl_opt;
506506
ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
507507
ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ extern "C" fn vmexit_handler() {
264264
}
265265

266266
#[no_mangle]
267-
fn adjust_rip(rip: u64) -> Result<(), SystemError> {
267+
pub fn adjust_rip(rip: u64) -> Result<(), SystemError> {
268268
let instruction_length = vmx_vmread(VmcsFields::VMEXIT_INSTR_LEN as u32)?;
269269
vmx_vmwrite(VmcsFields::GUEST_RIP as u32, rip + instruction_length)?;
270270
Ok(())

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ impl X86_64MMArch {
439439
// 不支持的原因是,目前好像没有能正确的设置page-level的xd位,会触发page fault
440440
return true;
441441
}
442+
443+
pub unsafe fn read_array<T>(addr: VirtAddr, count: usize) -> Vec<T> {
444+
// 实现读取数组逻辑
445+
let mut vec = Vec::with_capacity(count);
446+
for i in 0..count {
447+
vec.push(Self::read(addr + i * core::mem::size_of::<T>()));
448+
}
449+
vec
450+
}
442451
}
443452

444453
impl VirtAddr {

kernel/src/arch/x86_64/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod sched;
2020
pub mod smp;
2121
pub mod syscall;
2222
pub mod time;
23+
pub mod vm;
2324

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

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

4243
pub use crate::arch::sched::X86_64SchedArch as CurrentSchedArch;
44+
45+
pub use crate::arch::vm::KvmArchManager as CurrentKvmManager;
46+
47+
pub use crate::arch::vm::kvm_host::X86KvmArch as KvmArch;
48+
49+
pub use crate::arch::vm::x86_kvm_ops as kvm_arch_ops;
50+
51+
pub use crate::arch::vm::kvm_host::vcpu::X86VcpuArch as VirtCpuArch;
52+
pub use crate::arch::vm::kvm_host::KvmVcpuStat as VirtCpuStat;

0 commit comments

Comments
 (0)