Skip to content

Commit b2a4337

Browse files
committed
refactor: migrate arceos
1 parent 4e63860 commit b2a4337

File tree

17 files changed

+266
-344
lines changed

17 files changed

+266
-344
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,41 @@ timer_list = "0.1"
3232
toml = "0.9"
3333

3434
# System dependent modules provided by ArceOS.
35-
axstd = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1", features = [
35+
# FIXME: pin to a specific tag!
36+
axstd = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202", features = [
3637
"alloc-level-1",
3738
"paging",
3839
"irq",
3940
"multitask",
41+
"task-ext",
4042
"smp", # "page-alloc-64g",
4143
]}
4244

43-
axalloc = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
44-
axconfig = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
45-
axdisplay = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
46-
axdriver = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
47-
axfs = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
48-
axhal = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
49-
axipi = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
50-
axlog = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
51-
axmm = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
52-
axnet = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
53-
axtask = {git = "https://github.com/arceos-hypervisor/arceos.git", tag = "hv-0.4.1"}
45+
axalloc = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
46+
axconfig = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
47+
axdisplay = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
48+
axdriver = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
49+
axfs = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
50+
axhal = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
51+
axipi = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
52+
axlog = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
53+
axmm = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
54+
axnet = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
55+
axtask = {git = "https://github.com/arceos-org/arceos.git", tag = "dev-251202"}
5456

55-
axplat = {git = "https://github.com/arceos-hypervisor/axplat_crates.git", tag = "vmm-v0.3.0"}
57+
axcpu = {git = "https://github.com/arceos-org/axcpu.git", tag = "dev-v03"}
58+
axplat = {git = "https://github.com/arceos-org/axplat_crates.git", tag = "dev-v03"}
5659

5760
# System dependent modules provided by ArceOS-Hypervisor.
5861
axaddrspace = "0.1.1"
5962
axhvc = {git = "https://github.com/arceos-hypervisor/axhvc.git"}
63+
axklib = {git = "https://github.com/arceos-hypervisor/axklib.git"}
6064
axruntime = {path = "modules/axruntime"}
6165
axvcpu = "0.1"
6266
axvm = {git = "https://github.com/arceos-hypervisor/axvm.git", branch = "next"}
6367

6468
# System independent crates provided by ArceOS, these crates could be imported by remote url.
65-
axerrno = "0.1.0"
69+
axerrno = "0.2"
6670
byte-unit = {version = "5", default-features = false, features = ["byte"]}
6771
crate_interface = "0.1"
6872
fdt-parser = "0.4"
@@ -88,6 +92,6 @@ axvmconfig = {git = "https://github.com/arceos-hypervisor/axvmconfig.git", branc
8892
axvcpu = {git = "https://github.com/arceos-hypervisor/axvcpu.git", branch = "next"}
8993
axvmconfig = {git = "https://github.com/arceos-hypervisor/axvmconfig.git", branch = "next"}
9094

91-
[patch."https://github.com/arceos-hypervisor/arceos"]
95+
[patch."https://github.com/arceos-org/arceos"]
9296
axconfig = {path = "modules/axconfig"}
9397
axruntime = {path = "modules/axruntime"}

kernel/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ axvm.workspace = true
4141
axerrno.workspace = true
4242
byte-unit = {version = "5", default-features = false, features = ["byte"]}
4343
crate_interface.workspace = true
44+
extern-trait = "0.2"
4445
fdt-parser = "0.4"
4546
memory_addr.workspace = true
4647
page_table_entry = {version = "0.5", features = ["arm-el2"]}
@@ -62,7 +63,7 @@ aarch64-cpu-ext = "0.1"
6263
arm-gic-driver = {version = "0.15.5", features = ["rdif"]}
6364

6465
[build-dependencies]
65-
axconfig = {git = "https://github.com/arceos-hypervisor/arceos.git", branch = "vmm-dev"}
66+
axconfig.workspace = true
6667
prettyplease = "0.2"
6768
quote = "1.0"
6869
syn = "2.0"

kernel/src/hal/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::os::arceos::{
22
self,
3-
modules::{
4-
axhal::percpu::this_cpu_id,
5-
axtask::{self, TaskExtRef},
6-
},
3+
modules::{axhal::percpu::this_cpu_id, axtask},
74
};
85

96
use axerrno::{AxResult, ax_err_type};
@@ -19,7 +16,7 @@ use axvm::{AxVMHal, AxVMPerCpu};
1916
#[cfg_attr(target_arch = "x86_64", path = "arch/x86_64/mod.rs")]
2017
pub mod arch;
2118

22-
use crate::{hal::arch::hardware_check, vmm};
19+
use crate::{hal::arch::hardware_check, task::AsVCpuTask, vmm};
2320

2421
#[allow(unused)]
2522
#[repr(C)]
@@ -48,11 +45,11 @@ impl AxVMHal for AxVMHalImpl {
4845
}
4946

5047
fn current_vm_id() -> usize {
51-
axtask::current().task_ext().vm().id()
48+
axtask::current().as_vcpu_task().vm().id()
5249
}
5350

5451
fn current_vcpu_id() -> usize {
55-
axtask::current().task_ext().vcpu.id()
52+
axtask::current().as_vcpu_task().vcpu.id()
5653
}
5754

5855
fn current_pcpu_id() -> usize {
@@ -64,7 +61,7 @@ impl AxVMHal for AxVMHalImpl {
6461
.ok_or_else(|| ax_err_type!(NotFound))
6562
}
6663

67-
fn inject_irq_to_vcpu(vm_id: usize, vcpu_id: usize, irq: usize) -> axerrno::AxResult {
64+
fn inject_irq_to_vcpu(vm_id: usize, vcpu_id: usize, irq: usize) -> AxResult {
6865
vmm::with_vm_and_vcpu_on_pcpu(vm_id, vcpu_id, move |_, vcpu| {
6966
vcpu.inject_interrupt(irq).unwrap();
7067
})

kernel/src/task.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
1-
use crate::vmm::{VCpuRef, VM, VMRef};
21
use alloc::sync::{Arc, Weak};
3-
use std::os::arceos::modules::axtask::def_task_ext;
2+
use std::os::arceos::modules::axtask::{TaskExt, TaskInner};
3+
4+
use crate::vmm::{VCpuRef, VM, VMRef};
45

56
/// Task extended data for the hypervisor.
6-
pub struct TaskExt {
7+
pub struct VCpuTask {
78
/// The VM (Weak reference to avoid keeping VM alive).
89
pub vm: Weak<VM>,
910
/// The virtual CPU.
1011
pub vcpu: VCpuRef,
1112
}
1213

13-
impl TaskExt {
14-
/// Create TaskExt with a Weak reference from a VMRef
15-
pub const fn new(vm: Weak<VM>, vcpu: VCpuRef) -> Self {
16-
Self { vm, vcpu }
14+
impl VCpuTask {
15+
/// Create a new [`HvTask`].
16+
pub fn new(vm: &VMRef, vcpu: VCpuRef) -> Self {
17+
Self {
18+
vm: Arc::downgrade(vm),
19+
vcpu,
20+
}
1721
}
1822

1923
/// Get a strong reference to the VM if it's still alive.
2024
/// Returns None if the VM has been dropped.
2125
pub fn vm(&self) -> VMRef {
2226
self.vm.upgrade().expect("VM has been dropped")
2327
}
28+
}
2429

25-
/// Helper to create TaskExt from a VMRef by downgrading to Weak.
26-
pub fn from_vm_ref(vm: VMRef, vcpu: VCpuRef) -> Self {
27-
Self {
28-
vm: Arc::downgrade(&vm),
29-
vcpu,
30+
#[extern_trait::extern_trait]
31+
unsafe impl TaskExt for VCpuTask {}
32+
33+
pub trait AsVCpuTask {
34+
fn as_vcpu_task(&self) -> &VCpuTask;
35+
}
36+
37+
impl AsVCpuTask for TaskInner {
38+
fn as_vcpu_task(&self) -> &VCpuTask {
39+
unsafe {
40+
self.task_ext()
41+
.expect("Not a VCpuTask")
42+
.downcast_ref::<VCpuTask>()
3043
}
3144
}
3245
}
33-
34-
def_task_ext!(TaskExt);

kernel/src/vmm/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ pub mod fdt;
1313
use core::sync::atomic::{AtomicUsize, Ordering};
1414
use std::os::arceos::{
1515
api::task::{self, AxWaitQueueHandle},
16-
modules::axtask::{self, TaskExtRef},
16+
modules::axtask,
1717
};
1818

1919
use axerrno::{AxResult, ax_err_type};
2020

21-
use crate::hal::{AxVCpuHalImpl, AxVMHalImpl};
21+
use crate::{
22+
hal::{AxVCpuHalImpl, AxVMHalImpl},
23+
task::AsVCpuTask,
24+
};
2225
pub use timer::init_percpu as init_timer_percpu;
2326

2427
/// The instantiated VM type.
@@ -107,8 +110,8 @@ pub fn with_vm_and_vcpu_on_pcpu(
107110
// Disables preemption and IRQs to prevent the current task from being preempted or re-scheduled.
108111
let guard = kernel_guard::NoPreemptIrqSave::new();
109112

110-
let current_vm = axtask::current().task_ext().vm().id();
111-
let current_vcpu = axtask::current().task_ext().vcpu.id();
113+
let current_vm = axtask::current().as_vcpu_task().vm().id();
114+
let current_vcpu = axtask::current().as_vcpu_task().vcpu.id();
112115

113116
// The target vCPU is the current task, execute the closure directly.
114117
if current_vm == vm_id && current_vcpu == vcpu_id {

kernel/src/vmm/vcpus.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ use std::os::arceos::{
1010
api::task::{AxCpuMask, ax_wait_queue_wake},
1111
modules::{
1212
axhal::{self, time::busy_wait},
13-
axtask,
13+
axtask::{self, AxTaskExt},
1414
},
1515
};
1616

1717
use axaddrspace::GuestPhysAddr;
18-
use axtask::{AxTaskRef, TaskExtRef, TaskInner, WaitQueue};
18+
use axtask::{AxTaskRef, TaskInner, WaitQueue};
1919
use axvcpu::{AxVCpuExitReason, VCpuState};
2020

21-
use crate::vmm::{VCpuRef, VMRef, sub_running_vm_count};
22-
use crate::{hal::arch::inject_interrupt, task::TaskExt};
21+
use crate::{hal::arch::inject_interrupt, task::VCpuTask};
22+
use crate::{
23+
task::AsVCpuTask,
24+
vmm::{VCpuRef, VMRef, sub_running_vm_count},
25+
};
2326

2427
const KERNEL_STACK_SIZE: usize = 0x40000; // 256 KiB
2528

@@ -139,7 +142,8 @@ impl VMVCpus {
139142

140143
#[allow(dead_code)]
141144
fn notify_one(&mut self) {
142-
info!("Current wait queue length: {}", self.wait_queue.len());
145+
// FIXME: `WaitQueue::len` is removed
146+
// info!("Current wait queue length: {}", self.wait_queue.len());
143147
self.wait_queue.notify_one(false);
144148
}
145149

@@ -250,12 +254,11 @@ pub(crate) fn cleanup_vm_vcpus(vm_id: usize) {
250254
idx,
251255
task.id_name()
252256
);
253-
if let Some(exit_code) = task.join() {
254-
debug!(
255-
"VM[{}] VCpu task[{}] exited with code: {}",
256-
vm_id, idx, exit_code
257-
);
258-
}
257+
let exit_code = task.join();
258+
debug!(
259+
"VM[{}] VCpu task[{}] exited with code: {}",
260+
vm_id, idx, exit_code
261+
);
259262
}
260263

261264
info!(
@@ -318,7 +321,7 @@ fn vcpu_on(vm: VMRef, vcpu_id: usize, entry_point: GuestPhysAddr, arg: usize) {
318321
vcpu.set_gpr(1, arg);
319322
}
320323

321-
let vcpu_task = alloc_vcpu_task(vm.clone(), vcpu);
324+
let vcpu_task = alloc_vcpu_task(&vm, vcpu);
322325

323326
VM_VCPU_TASK_WAIT_QUEUE
324327
.get_mut(&vm.id())
@@ -342,7 +345,7 @@ pub fn setup_vm_primary_vcpu(vm: VMRef) {
342345
let primary_vcpu_id = 0;
343346

344347
let primary_vcpu = vm.vcpu_list()[primary_vcpu_id].clone();
345-
let primary_vcpu_task = alloc_vcpu_task(vm.clone(), primary_vcpu);
348+
let primary_vcpu_task = alloc_vcpu_task(&vm, primary_vcpu);
346349
vm_vcpus.add_vcpu_task(primary_vcpu_task);
347350

348351
VM_VCPU_TASK_WAIT_QUEUE.insert(vm_id, vm_vcpus);
@@ -383,7 +386,7 @@ pub fn with_vcpu_task<T, F: FnOnce(&AxTaskRef) -> T>(
383386
/// * The task associated with the VCpu is created with a kernel stack size of 256 KiB.
384387
/// * The task is created in blocked state and added to the wait queue directly,
385388
/// instead of being added to the ready queue. It will be woken up by notify_primary_vcpu().
386-
fn alloc_vcpu_task(vm: VMRef, vcpu: VCpuRef) -> AxTaskRef {
389+
fn alloc_vcpu_task(vm: &VMRef, vcpu: VCpuRef) -> AxTaskRef {
387390
info!("Spawning task for VM[{}] VCpu[{}]", vm.id(), vcpu.id());
388391
let mut vcpu_task = TaskInner::new(
389392
vcpu_run,
@@ -396,7 +399,8 @@ fn alloc_vcpu_task(vm: VMRef, vcpu: VCpuRef) -> AxTaskRef {
396399
}
397400

398401
// Use Weak reference in TaskExt to avoid keeping VM alive
399-
vcpu_task.init_task_ext(TaskExt::from_vm_ref(vm.clone(), vcpu));
402+
let inner = VCpuTask::new(vm, vcpu);
403+
*vcpu_task.task_ext_mut() = Some(unsafe { AxTaskExt::from_impl(inner) });
400404

401405
info!(
402406
"VCpu task {} created {:?}",
@@ -414,8 +418,8 @@ fn alloc_vcpu_task(vm: VMRef, vcpu: VCpuRef) -> AxTaskRef {
414418
fn vcpu_run() {
415419
let curr = axtask::current();
416420

417-
let vm = curr.task_ext().vm();
418-
let vcpu = curr.task_ext().vcpu.clone();
421+
let vm = curr.as_vcpu_task().vm();
422+
let vcpu = curr.as_vcpu_task().vcpu.clone();
419423
let vm_id = vm.id();
420424
let vcpu_id = vcpu.id();
421425

modules/axruntime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ chrono = {version = "0.4.38", default-features = false}
5353
axplat-x86-qemu-q35 = {workspace = true}
5454

5555
[target.'cfg(target_arch = "aarch64")'.dependencies]
56-
axplat-aarch64-dyn = {git = "https://github.com/arceos-hypervisor/axplat-aarch64-dyn", tag = "v0.3.3", features = ["irq", "smp", "hv"]}
56+
axplat-aarch64-dyn = {git = "https://github.com/arceos-hypervisor/axplat-aarch64-dyn.git", branch = "v03", features = ["irq", "smp", "hv"]}
5757
somehal = "0.4"

modules/axruntime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn is_init_ok() -> bool {
112112
#[cfg_attr(not(test), axplat::main)]
113113
pub fn rust_main(cpu_id: usize, arg: usize) -> ! {
114114
unsafe { axhal::mem::clear_bss() };
115-
axhal::init_percpu(cpu_id);
115+
axhal::percpu::init_primary(cpu_id);
116116
axhal::init_early(cpu_id, arg);
117117

118118
ax_println!("{}", LOGO);

modules/axruntime/src/mp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn start_secondary_cpus(primary_cpu_id: usize) {
3939
/// It is called from the bootstrapping code in the specific platform crate.
4040
#[axplat::secondary_main]
4141
pub fn rust_main_secondary(cpu_id: usize) -> ! {
42-
axhal::init_percpu_secondary(cpu_id);
42+
axhal::percpu::init_secondary(cpu_id);
4343
axhal::init_early_secondary(cpu_id);
4444

4545
ENTERED_CPUS.fetch_add(1, Ordering::Release);

0 commit comments

Comments
 (0)