Skip to content

Commit 2c2cf9e

Browse files
committed
feat: 实现 VCpu 操作接口,增强虚拟 CPU 管理功能
1 parent faca4ac commit 2c2cf9e

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

src/arch/aarch64/cpu.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use arm_vcpu::{Aarch64PerCpu, Aarch64VCpuCreateConfig};
66
use axvm_types::addr::*;
77

88
use crate::{
9+
RunError,
910
data::VmDataWeak,
10-
vcpu::VCpuCommon,
11+
vcpu::{VCpuCommon, VCpuOp},
1112
vhal::{
1213
ArchCpuData,
1314
cpu::{CpuHardId, CpuId, HCpuExclusive},
@@ -125,8 +126,18 @@ impl VCpu {
125126
.unwrap();
126127
Ok(VCpu { vcpu, common })
127128
}
129+
}
130+
131+
impl VCpuOp for VCpu {
132+
fn bind_id(&self) -> CpuId {
133+
self.common.bind_id()
134+
}
135+
136+
fn hard_id(&self) -> CpuHardId {
137+
self.common.hard_id()
138+
}
128139

129-
pub fn run(&mut self) -> anyhow::Result<()> {
140+
fn run(&mut self) -> Result<(), RunError> {
130141
info!("Starting vCPU {}", self.bind_id());
131142

132143
while self.is_active() {

src/vcpu/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
use crate::{
2-
CpuId,
2+
CpuId, RunError,
33
arch::HCpu,
44
data::{VmData, VmDataWeak},
55
vhal::cpu::{CpuHardId, HCpuExclusive},
66
};
77

8+
pub trait VCpuOp: Send + 'static{
9+
fn bind_id(&self) -> CpuId;
10+
fn hard_id(&self) -> CpuHardId;
11+
fn run(&mut self) -> Result<(), RunError>;
12+
}
13+
814
#[derive(Debug)]
915
pub struct VCpuCommon {
1016
pub(crate) hcpu: HCpuExclusive,

src/vm/data.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use spin::RwLock;
1111

1212
use crate::{
1313
AxVMConfig, RunError, VmId, VmMachineInitedOps, VmMachineRunningOps, VmMachineUninitOps,
14-
arch::{VmMachineInited, VmMachineUninit},
14+
arch::{VmMachineInited, VmMachineRunning, VmMachineUninit},
1515
config::AxVCpuConfig,
1616
vm::machine::{AtomicState, VMStatus, VmMachineState},
1717
};
@@ -207,6 +207,23 @@ impl VmDataWeak {
207207
inner.status.store(VMStatus::Stopped);
208208
}
209209
}
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+
}
210227
}
211228

212229
impl Debug for VmDataWeak {

src/vm/machine/running.rs

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

1010
use crate::{
11-
RunError, TASK_STACK_SIZE, VmAddrSpace, arch::cpu::VCpu, data::VmDataWeak, vhal::cpu::CpuHardId,
11+
RunError, TASK_STACK_SIZE, VmAddrSpace, arch::cpu::VCpu, data::VmDataWeak, vcpu::VCpuOp,
12+
vhal::cpu::CpuHardId,
1213
};
1314

1415
pub struct VmMachineRunningCommon {
@@ -44,7 +45,7 @@ impl VmMachineRunningCommon {
4445
Ok(cpu)
4546
}
4647

47-
pub fn run_cpu(&mut self, mut cpu: VCpu) -> anyhow::Result<()> {
48+
pub fn run_cpu<C: VCpuOp>(&mut self, mut cpu: C) -> anyhow::Result<()> {
4849
let waiter = self.new_waiter();
4950

5051
let bind_id = cpu.bind_id();
@@ -61,7 +62,7 @@ impl VmMachineRunningCommon {
6162
let res = cpu.run();
6263
if let Err(e) = res {
6364
if let Some(vm) = waiter.vm.upgrade() {
64-
vm.set_err(RunError::ExitWithError(e));
65+
vm.set_err(e);
6566
}
6667
}
6768
waiter.running_cpu_count.fetch_sub(1, Ordering::SeqCst);

0 commit comments

Comments
 (0)