Skip to content

Commit 731be1f

Browse files
committed
feat: 增强虚拟机管理功能,添加 CPU 启动和运行管理逻辑,清理未使用的代码
1 parent d1cf7b0 commit 731be1f

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

src/arch/aarch64/cpu.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,18 @@ impl VCpuOp for VCpu {
136136
arm_vcpu::AxVCpuExitReason::MmioWrite { addr, width, data } => todo!(),
137137
arm_vcpu::AxVCpuExitReason::SysRegRead { addr, reg } => todo!(),
138138
arm_vcpu::AxVCpuExitReason::SysRegWrite { addr, value } => todo!(),
139-
arm_vcpu::AxVCpuExitReason::ExternalInterrupt => todo!(),
139+
arm_vcpu::AxVCpuExitReason::ExternalInterrupt => {
140+
axhal::irq::irq_handler(0);
141+
}
140142
arm_vcpu::AxVCpuExitReason::CpuUp {
141143
target_cpu,
142144
entry_point,
143145
arg,
144-
} => todo!(),
146+
} => {
147+
self.vm()?.with_machine_running_mut(|running| {
148+
running.cpu_up(CpuHardId::new(target_cpu as _), entry_point, arg)
149+
})??;
150+
}
145151
arm_vcpu::AxVCpuExitReason::CpuDown { _state } => todo!(),
146152
arm_vcpu::AxVCpuExitReason::SystemDown => {
147153
info!("vCPU {} requested system shutdown", self.bind_id());

src/arch/aarch64/vm/inited.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{string::String, vec::Vec};
22

33
use crate::{
4-
GuestPhysAddr, VmAddrSpace, VmMachineInitedOps, VmMachineRunningCommon,
4+
VmAddrSpace, VmMachineInitedOps, VmMachineRunningCommon,
55
arch::{VmMachineRunning, cpu::VCpu},
66
data::VmDataWeak,
77
vm::VmId,

src/arch/aarch64/vm/running.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use fdt_edit::NodeRef;
22

33
use crate::{
4-
VmAddrSpace, VmMachineRunningCommon, VmMachineRunningOps, VmMachineStoppingOps,
5-
arch::vm::DevMapConfig,
4+
GuestPhysAddr, VmAddrSpace, VmMachineRunningCommon, VmMachineRunningOps, VmMachineStoppingOps,
5+
arch::vm::DevMapConfig, vhal::cpu::CpuHardId,
66
};
77

88
/// Data needed when VM is running
@@ -12,6 +12,24 @@ pub struct VmMachineRunning {
1212

1313
impl VmMachineRunning {
1414
fn handle_node_regs(dev_vec: &mut [DevMapConfig], node: &NodeRef<'_>) {}
15+
pub fn cpu_up(
16+
&mut self,
17+
target_cpu: CpuHardId,
18+
entry_point: GuestPhysAddr,
19+
arg: u64,
20+
) -> anyhow::Result<()> {
21+
let mut cpu = self
22+
.common
23+
.cpus
24+
.remove(&target_cpu)
25+
.ok_or(anyhow!("No cpu {target_cpu} found"))?;
26+
27+
cpu.vcpu.set_entry(entry_point.as_usize().into()).unwrap();
28+
cpu.vcpu.ctx_mut().gpr[0] = arg;
29+
30+
self.common.run_cpu(cpu)?;
31+
Ok(())
32+
}
1533
}
1634

1735
impl VmMachineRunningOps for VmMachineRunning {

src/vhal/cpu.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ impl HCpuExclusive {
4747
panic!("CPU data not found for CPU ID {}", self.0);
4848
}
4949

50-
pub fn cpu_id(&self) -> CpuId {
51-
self.0
52-
}
53-
5450
pub fn hard_id(&self) -> CpuHardId {
5551
self.with_cpu(|cpu| cpu.hard_id())
5652
}

src/vm/data.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,38 @@ impl VmData {
158158
inner: Arc::downgrade(&self.inner),
159159
}
160160
}
161+
162+
pub(crate) fn with_machine_running<F, R>(&self, f: F) -> Result<R, RunError>
163+
where
164+
F: FnOnce(&VmMachineRunning) -> R,
165+
{
166+
let status = self.machine.read();
167+
let running = match &*status {
168+
VmMachineState::Running(running) => running,
169+
_ => {
170+
return Err(RunError::ExitWithError(anyhow!(
171+
"VM is not in Running state"
172+
)));
173+
}
174+
};
175+
Ok(f(running))
176+
}
177+
178+
pub(crate) fn with_machine_running_mut<F, R>(&self, f: F) -> Result<R, RunError>
179+
where
180+
F: FnOnce(&mut VmMachineRunning) -> R,
181+
{
182+
let mut status = self.machine.write();
183+
let running = match &mut *status {
184+
VmMachineState::Running(running) => running,
185+
_ => {
186+
return Err(RunError::ExitWithError(anyhow!(
187+
"VM is not in Running state"
188+
)));
189+
}
190+
};
191+
Ok(f(running))
192+
}
161193
}
162194

163195
impl From<Arc<VmDataInner>> for VmData {
@@ -207,23 +239,6 @@ impl VmDataWeak {
207239
inner.status.store(VMStatus::Stopped);
208240
}
209241
}
210-
211-
pub(crate) fn with_machine_running<F, R>(&self, f: F) -> Result<R, RunError>
212-
where
213-
F: FnOnce(&VmMachineRunning) -> R,
214-
{
215-
let vmdata = self.try_upgrade()?;
216-
let status = vmdata.machine.read();
217-
let running = match &*status {
218-
VmMachineState::Running(running) => running,
219-
_ => {
220-
return Err(RunError::ExitWithError(anyhow!(
221-
"VM is not in Running state"
222-
)));
223-
}
224-
};
225-
Ok(f(running))
226-
}
227242
}
228243

229244
impl Debug for VmDataWeak {

src/vm/machine/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use alloc::string::String;
21
use core::sync::atomic::{AtomicU8, Ordering};
32

43
use crate::{
@@ -48,12 +47,6 @@ pub enum VmMachineState {
4847
Stopped,
4948
}
5049

51-
impl VmMachineState {
52-
pub fn is_active(&self) -> bool {
53-
!matches!(self, VmMachineState::Stopping(_) | VmMachineState::Stopped)
54-
}
55-
}
56-
5750
/// Auxiliary wrapper that stores the current machine status in an atomically
5851
/// readable form so management threads can query it without synchronisation
5952
/// overhead.

src/vm/machine/running.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use alloc::vec::Vec;
99

1010
use crate::{
11-
RunError, TASK_STACK_SIZE, VmAddrSpace, arch::cpu::VCpu, data::VmDataWeak, vcpu::VCpuOp,
11+
TASK_STACK_SIZE, VmAddrSpace, arch::cpu::VCpu, data::VmDataWeak, vcpu::VCpuOp,
1212
vhal::cpu::CpuHardId,
1313
};
1414

@@ -84,10 +84,6 @@ impl VmMachineRunningCommon {
8484
vm: self.vm.clone(),
8585
}
8686
}
87-
88-
pub fn vmspace(&self) -> &VmAddrSpace {
89-
&self.vmspace
90-
}
9187
}
9288

9389
struct Waiter {

0 commit comments

Comments
 (0)