Skip to content

Commit de48805

Browse files
authored
feat: add vm_list and vcpu_list, import axtask for vcpu scheduling (arceos-hypervisor#17)
* feat: add vm_list and vcpu_list, import axtask for vcpu scheduling * refactor: move vmm related codes into vmm * Rebase to main branch, a few modifications to aarch64 related part of axvm * Modify vCpu vm-exit handing and some modication related with axaddrspace * Add doc comments in vmm/vcpus.rs
1 parent dbe5c53 commit de48805

File tree

29 files changed

+616
-518
lines changed

29 files changed

+616
-518
lines changed

Cargo.lock

Lines changed: 37 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,27 @@ exclude = ["guest/nimbos"]
66

77
[profile.release]
88
lto = true
9+
10+
[workspace.dependencies]
11+
axalloc = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79" }
12+
axhal = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79" }
13+
axstd = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79" }
14+
axtask = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic" }
15+
16+
axvm = { path = "./crates/axvm" }
17+
axvcpu = { git = "https://github.com/arceos-hypervisor/axvcpu.git" }
18+
axaddrspace = { git = "https://github.com/arceos-hypervisor/axaddrspace.git", rev = "17378d" }
19+
20+
# Since we need to rely on the `axtask` module from the `monolithic` branch of arceos-org/arceos repository, which supports TaskExt,
21+
# and this module depends on different versions of `axhal` and `axconfig`,
22+
# (ref: https://github.com/arceos-org/arceos/blob/monolithic/modules/axtask/Cargo.toml)
23+
# we temporarily use [patch] here to unify the dependency versions of these modules.
24+
[patch."https://github.com/arceos-org/arceos.git"]
25+
axhal = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79" }
26+
axconfig = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79" }
27+
28+
# The complexity:
29+
# `axstd` depends on the `axtask` from the `arceos-hypervisor\arceos` repository when the 'multitask' feature is enabled.
30+
# Here, we use [patch] to unify `axtask` to the one from the `monolithic` branch of the arceos-org/arceos repository.
31+
[patch."https://github.com/arceos-hypervisor/arceos.git"]
32+
axtask = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic" }

arceos-vmm/Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ lazy_static = { version = "1.4", features = ["spin_no_std"] }
1313
cfg-if = "1.0"
1414

1515
# System dependent modules provided by ArceOS.
16-
axalloc = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79" }
17-
axhal = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79", features = ["paging"] }
18-
axstd = { git = "https://github.com/arceos-hypervisor/arceos.git", rev = "5c9ee79", features = [
16+
axhal = { workspace = true, features = ["paging"] }
17+
axtask = { workspace = true, features = ["multitask"] }
18+
axstd = { workspace = true, features = [
1919
"alloc",
2020
"paging",
2121
"fs",
2222
# "irq",
2323
"hv",
24-
], optional = true }
24+
"multitask",
25+
] }
2526

26-
# System independent crates used for constructing hypervisor.
27-
axvm = { path = "../crates/axvm" }
27+
# System dependent modules provided by ArceOS-Hypervisor.
28+
axvm = { workspace = true }
29+
axvcpu = { workspace = true }
2830

2931
# System independent crates provided by ArceOS, these crates could be imported by remote url.
3032
axerrno = "0.1.0"

arceos-vmm/scripts/make/cargo.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ define cargo_clippy
3535
endef
3636

3737
all_packages := \
38-
$(shell ls $(CURDIR)/crates) \
39-
$(shell ls $(CURDIR)/modules) \
40-
axfeat arceos_api axstd axlibc
38+
# $(shell ls $(CURDIR)/crates) \
39+
# $(shell ls $(CURDIR)/modules) \
40+
# axfeat arceos_api axstd axlibc
4141

4242
define cargo_doc
4343
$(call run_cmd,cargo doc,--no-deps --all-features --workspace --exclude "arceos-*" $(verbose))

arceos-vmm/src/hal.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use axvm::AxVMHal;
2-
use memory_addr::PAGE_SIZE_4K as PAGE_SIZE;
32
// Todo: should we know about HostPhysAddr and HostVirtAddr here???
43
use memory_addr::{PhysAddr, VirtAddr};
54

arceos-vmm/src/main.rs

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
#![cfg_attr(feature = "axstd", no_std)]
2-
#![cfg_attr(feature = "axstd", no_main)]
3-
#![feature(naked_functions)]
4-
#![allow(warnings)]
5-
#[macro_use]
6-
#[cfg(feature = "axstd")]
7-
extern crate axstd as std;
8-
9-
extern crate alloc;
1+
#![no_std]
2+
#![no_main]
103

114
#[macro_use]
125
extern crate log;
6+
#[macro_use]
7+
extern crate alloc;
8+
extern crate axstd as std;
139

1410
mod hal;
15-
mod images;
11+
mod task;
12+
mod vmm;
1613

17-
use axvm::config::{AxVMConfig, AxVMCrateConfig};
18-
use axvm::{AxVM, AxVMPerCpu};
14+
use axvm::AxVMPerCpu;
1915

2016
use crate::hal::AxVMHalImpl;
2117

2218
#[percpu::def_percpu]
2319
pub static mut AXVM_PER_CPU: AxVMPerCpu<AxVMHalImpl> = AxVMPerCpu::new_uninit();
2420

25-
#[cfg_attr(feature = "axstd", no_mangle)]
21+
#[no_mangle]
2622
fn main() {
2723
info!("Starting virtualization...");
2824

@@ -37,32 +33,12 @@ fn main() {
3733
.hardware_enable()
3834
.expect("Failed to enable virtualization");
3935

40-
// Config file for guest VM should be read into memory in a more flexible way.
41-
// FIXME: remove this hardcode.
42-
#[cfg(target_arch = "x86_64")]
43-
let raw_vm_config = core::include_str!("../configs/nimbos-x86.toml");
44-
#[cfg(target_arch = "aarch64")]
45-
let raw_vm_config = core::include_str!("../configs/nimbos-aarch64.toml");
46-
#[cfg(target_arch = "riscv64")]
47-
let raw_vm_config = core::include_str!("../configs/nimbos-riscv64.toml");
48-
49-
let vm_create_config =
50-
AxVMCrateConfig::from_toml(raw_vm_config).expect("Failed to resolve VM config");
51-
52-
let config = AxVMConfig::from(vm_create_config.clone());
53-
54-
// Create VM.
55-
let vm = AxVM::<AxVMHalImpl>::new(config).expect("Failed to create VM");
56-
57-
// Load corresponding images for VM.
58-
info!("VM[{}] created success, loading images...", vm.id());
59-
images::load_vm_images(vm_create_config, vm.clone()).expect("Failed to load VM images");
36+
vmm::init();
6037

61-
info!("Boot VM[{}]...", vm.id());
38+
vmm::start();
6239

63-
// Todo: remove this, details can be get from
64-
// this [PR](https://github.com/arceos-hypervisor/arceos-umhv/pull/5).
65-
vm.boot().unwrap();
40+
// Todo: move this to `vmm::start()`.
41+
axtask::WaitQueue::new().wait();
6642

67-
panic!("VM[{}] boot failed", vm.id());
43+
unreachable!("VMM start failed")
6844
}

arceos-vmm/src/task.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use axvm::{AxVCpuRef, AxVMRef};
2+
3+
use crate::hal::AxVMHalImpl;
4+
5+
/// Task extended data for the monolithic kernel.
6+
pub struct TaskExt {
7+
/// The VM.
8+
pub vm: AxVMRef<AxVMHalImpl>,
9+
/// The virtual memory address space.
10+
pub vcpu: AxVCpuRef<AxVMHalImpl>,
11+
}
12+
13+
impl TaskExt {
14+
pub const fn new(vm: AxVMRef<AxVMHalImpl>, vcpu: AxVCpuRef<AxVMHalImpl>) -> Self {
15+
Self { vm, vcpu }
16+
}
17+
}
18+
19+
axtask::def_task_ext!(TaskExt);

arceos-vmm/src/vmm/config.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use axvm::config::{AxVMConfig, AxVMCrateConfig};
2+
3+
use crate::vmm::{images::load_vm_images, vm_list::push_vm, VM};
4+
5+
pub fn init_guest_vms() {
6+
// Config file for guest VM should be read into memory in a more flexible way.
7+
// FIXME: remove this hardcode.
8+
let gvm_raw_configs = vec![
9+
#[cfg(target_arch = "x86_64")]
10+
core::include_str!("../../configs/nimbos-x86.toml"),
11+
#[cfg(target_arch = "aarch64")]
12+
core::include_str!("../../configs/nimbos-aarch64.toml"),
13+
#[cfg(target_arch = "riscv64")]
14+
core::include_str!("../../configs/nimbos-riscv64.toml"),
15+
];
16+
17+
for raw_cfg_str in gvm_raw_configs {
18+
let vm_create_config =
19+
AxVMCrateConfig::from_toml(raw_cfg_str).expect("Failed to resolve VM config");
20+
let vm_config = AxVMConfig::from(vm_create_config.clone());
21+
22+
// Create VM.
23+
let vm = VM::new(vm_config).expect("Failed to create VM");
24+
push_vm(vm.clone());
25+
26+
// Load corresponding images for VM.
27+
info!("VM[{}] created success, loading images...", vm.id());
28+
load_vm_images(vm_create_config, vm.clone()).expect("Failed to load VM images");
29+
}
30+
}
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
use alloc::string::String;
2-
use alloc::sync::Arc;
3-
use alloc::vec::Vec;
42

53
use std::fs::File;
64

7-
use axerrno::{ax_err, ax_err_type, AxError, AxResult};
5+
use axerrno::{ax_err, ax_err_type, AxResult};
86
use memory_addr::VirtAddr;
97

108
use axvm::config::AxVMCrateConfig;
11-
use axvm::AxVM;
129

13-
use crate::hal::AxVMHalImpl;
10+
use crate::vmm::VMRef;
1411

15-
pub fn load_vm_images(config: AxVMCrateConfig, vm: Arc<AxVM<AxVMHalImpl>>) -> AxResult {
12+
/// Loads the VM image files from the filesystem
13+
/// into the guest VM's memory space based on the VM configuration.
14+
pub fn load_vm_images(config: AxVMCrateConfig, vm: VMRef) -> AxResult {
1615
// Load kernel image.
1716
load_vm_image(
1817
config.kernel_path,
1918
VirtAddr::from(config.kernel_load_addr),
2019
vm.clone(),
21-
);
20+
)?;
2221
// Load BIOS image if needed.
2322
if let Some(bios_path) = config.bios_path {
2423
if let Some(bios_load_addr) = config.bios_load_addr {
25-
load_vm_image(bios_path, VirtAddr::from(bios_load_addr), vm.clone());
24+
load_vm_image(bios_path, VirtAddr::from(bios_load_addr), vm.clone())?;
2625
} else {
2726
return ax_err!(NotFound, "BIOS load addr is missed");
2827
}
2928
};
3029
// Load Ramdisk image if needed.
3130
if let Some(ramdisk_path) = config.ramdisk_path {
3231
if let Some(ramdisk_load_addr) = config.ramdisk_load_addr {
33-
load_vm_image(ramdisk_path, VirtAddr::from(ramdisk_load_addr), vm.clone());
32+
load_vm_image(ramdisk_path, VirtAddr::from(ramdisk_load_addr), vm.clone())?;
3433
} else {
3534
return ax_err!(NotFound, "Ramdisk load addr is missed");
3635
}
@@ -39,19 +38,15 @@ pub fn load_vm_images(config: AxVMCrateConfig, vm: Arc<AxVM<AxVMHalImpl>>) -> Ax
3938
// Todo: generate DTB file for guest VM.
4039
if let Some(dtb_path) = config.dtb_path {
4140
if let Some(dtb_load_addr) = config.dtb_load_addr {
42-
load_vm_image(dtb_path, VirtAddr::from(dtb_load_addr), vm.clone());
41+
load_vm_image(dtb_path, VirtAddr::from(dtb_load_addr), vm.clone())?;
4342
} else {
4443
return ax_err!(NotFound, "DTB load addr is missed");
4544
}
4645
};
4746
Ok(())
4847
}
4948

50-
fn load_vm_image(
51-
image_path: String,
52-
image_load_gpa: VirtAddr,
53-
vm: Arc<AxVM<AxVMHalImpl>>,
54-
) -> AxResult {
49+
fn load_vm_image(image_path: String, image_load_gpa: VirtAddr, vm: VMRef) -> AxResult {
5550
use std::io::{BufReader, Read};
5651
let (image_file, image_size) = open_image_file(image_path.as_str())?;
5752

0 commit comments

Comments
 (0)