Skip to content

Commit 91852c4

Browse files
committed
feat: 增强 vCPU 调试信息,添加 CPU 运行时的 VM ID 追踪
1 parent a46d06b commit 91852c4

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/arch/aarch64/cpu.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use core::{fmt::Display, ops::Deref};
1+
use core::{
2+
fmt::{self, Debug, Display},
3+
ops::Deref,
4+
};
25

36
use aarch64_cpu::registers::*;
47
use arm_vcpu::{Aarch64PerCpu, Aarch64VCpuCreateConfig};
@@ -116,7 +119,9 @@ impl VCpuOp for VCpu {
116119

117120
fn run(&mut self) -> Result<(), RunError> {
118121
info!("Starting vCPU {}", self.bind_id());
119-
122+
self.vcpu
123+
.setup_current_cpu(self.vm_id().into())
124+
.map_err(|e| anyhow!("{e}"))?;
120125
while self.is_active() {
121126
debug!("vCPU {} entering guest", self.bind_id());
122127
let exit_reason = self.vcpu.run().map_err(|e| anyhow!("{e}"))?;
@@ -150,6 +155,7 @@ impl VCpuOp for VCpu {
150155
debug!("vCPU {} is bringing up CPU {}", self.bind_id(), target_cpu);
151156
running.cpu_up(CpuHardId::new(target_cpu as _), entry_point, arg)
152157
})??;
158+
self.vcpu.set_gpr(0, 0);
153159
}
154160
arm_vcpu::AxVCpuExitReason::CpuDown { _state } => todo!(),
155161
arm_vcpu::AxVCpuExitReason::SystemDown => {
@@ -179,3 +185,13 @@ impl Deref for VCpu {
179185
&self.common
180186
}
181187
}
188+
189+
impl Debug for VCpu {
190+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191+
f.debug_struct("VCpu")
192+
.field("bind_id", &self.bind_id())
193+
.field("hard_id", &self.hard_id())
194+
.field("vcpu", &self.vcpu)
195+
.finish()
196+
}
197+
}

src/arch/aarch64/vm/running.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ impl VmMachineRunning {
2727

2828
cpu.vcpu.set_entry(entry_point.as_usize().into()).unwrap();
2929
cpu.vcpu.set_gpr(0, arg as _);
30-
debug!("{:?}", cpu.vcpu);
3130
self.common.run_cpu(cpu)?;
3231
Ok(())
3332
}

src/vcpu/mod.rs

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

8-
pub trait VCpuOp: Send + 'static{
8+
pub trait VCpuOp: core::fmt::Debug + Send + 'static {
99
fn bind_id(&self) -> CpuId;
1010
fn hard_id(&self) -> CpuHardId;
1111
fn run(&mut self) -> Result<(), RunError>;
@@ -18,6 +18,10 @@ pub struct VCpuCommon {
1818
}
1919

2020
impl VCpuCommon {
21+
pub fn vm_id(&self) -> VmId {
22+
self.vm.id()
23+
}
24+
2125
pub fn new_exclusive(bind: Option<CpuId>, vm: VmDataWeak) -> anyhow::Result<Self> {
2226
let hcpu = HCpuExclusive::try_new(bind)
2327
.ok_or_else(|| anyhow!("Failed to allocate cpu with id `{bind:?}`"))?;

src/vm/data.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ impl VmData {
182182

183183
pub fn downgrade(&self) -> VmDataWeak {
184184
VmDataWeak {
185+
id: self.id(),
185186
inner: Arc::downgrade(&self.inner),
186187
}
187188
}
@@ -249,10 +250,15 @@ impl Deref for VmData {
249250

250251
#[derive(Clone)]
251252
pub struct VmDataWeak {
253+
id: VmId,
252254
inner: Weak<VmDataInner>,
253255
}
254256

255257
impl VmDataWeak {
258+
pub fn id(&self) -> VmId {
259+
self.id
260+
}
261+
256262
pub fn upgrade(&self) -> Option<VmData> {
257263
Some(self.inner.upgrade()?.into())
258264
}

src/vm/machine/running.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl VmMachineRunningCommon {
6868
);
6969
waiter.vm.wait_for_running();
7070
info!("VCpu {} on {} run", cpu.hard_id(), bind_id);
71+
// debug!("\n{:#x?}", cpu);
7172
let res = cpu.run();
7273
if let Err(e) = res {
7374
info!("vCPU {} exited with error: {e}", bind_id);

0 commit comments

Comments
 (0)