Skip to content

Commit 70bd7a6

Browse files
committed
update
1 parent aff70e1 commit 70bd7a6

File tree

12 files changed

+619
-476
lines changed

12 files changed

+619
-476
lines changed

src/arch/aarch64/cpu.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
use core::{fmt::Display, sync::atomic::AtomicBool};
1+
use core::{fmt::Display, ops::Deref, sync::atomic::AtomicBool};
22
use std::sync::Arc;
33

44
use aarch64_cpu::registers::*;
55
use arm_vcpu::{Aarch64PerCpu, Aarch64VCpuCreateConfig};
66
use axvm_types::addr::*;
77

8-
use crate::vhal::{
9-
ArchCpuData,
10-
cpu::{CpuHardId, CpuId, HCpuExclusive},
8+
use crate::{
9+
data2::VmDataWeak,
10+
vcpu::VCpuCommon,
11+
vhal::{
12+
ArchCpuData,
13+
cpu::{CpuHardId, CpuId, HCpuExclusive},
14+
},
1115
};
1216

1317
pub struct HCpu {
@@ -97,53 +101,34 @@ impl VCpuHandle {
97101
}
98102

99103
pub struct VCpu {
100-
pub id: CpuHardId,
101104
pub vcpu: arm_vcpu::Aarch64VCpu,
102-
hcpu: HCpuExclusive,
103-
handle: VCpuHandle,
105+
common: VCpuCommon,
104106
}
105107

106108
impl VCpu {
107-
pub fn new(host_cpuid: Option<CpuId>, dtb_addr: GuestPhysAddr) -> anyhow::Result<Self> {
108-
let hcpu_exclusive = HCpuExclusive::try_new(host_cpuid)
109-
.ok_or_else(|| anyhow!("Failed to allocate cpu with id `{host_cpuid:?}`"))?;
109+
pub fn new(
110+
host_cpuid: Option<CpuId>,
111+
dtb_addr: GuestPhysAddr,
112+
vm: VmDataWeak,
113+
) -> anyhow::Result<Self> {
114+
let common = VCpuCommon::new_exclusive(host_cpuid, vm)?;
110115

111-
let hard_id = hcpu_exclusive.hard_id();
116+
let hard_id = common.hard_id();
112117

113118
let vcpu = arm_vcpu::Aarch64VCpu::new(Aarch64VCpuCreateConfig {
114119
mpidr_el1: hard_id.raw() as u64,
115120
dtb_addr: dtb_addr.as_usize(),
116121
})
117122
.unwrap();
118-
Ok(VCpu {
119-
id: hard_id,
120-
vcpu,
121-
hcpu: hcpu_exclusive,
122-
handle: VCpuHandle::new(),
123-
})
124-
}
125-
126-
pub fn handle(&self) -> VCpuHandle {
127-
self.handle.clone()
128-
}
129-
130-
pub fn with_hcpu<F, R>(&self, f: F) -> R
131-
where
132-
F: FnOnce(&HCpu) -> R,
133-
{
134-
self.hcpu.with_cpu(f)
135-
}
136-
137-
pub fn binded_cpu_id(&self) -> CpuId {
138-
self.hcpu.cpu_id()
123+
Ok(VCpu { vcpu, common })
139124
}
140125

141126
pub fn run(&mut self) -> anyhow::Result<()> {
142-
info!("Starting vCPU {}", self.id);
127+
info!("Starting vCPU {}", self.id());
143128

144-
while self.handle.is_active() {
129+
while self.is_active() {
145130
let exit_reason = self.vcpu.run().map_err(|e| anyhow!("{e}"))?;
146-
debug!("vCPU {} exited with reason: {:?}", self.id, exit_reason);
131+
debug!("vCPU {} exited with reason: {:?}", self.id(), exit_reason);
147132
match exit_reason {
148133
arm_vcpu::AxVCpuExitReason::Hypercall { nr, args } => todo!(),
149134
arm_vcpu::AxVCpuExitReason::MmioRead {
@@ -163,7 +148,10 @@ impl VCpu {
163148
arg,
164149
} => todo!(),
165150
arm_vcpu::AxVCpuExitReason::CpuDown { _state } => todo!(),
166-
arm_vcpu::AxVCpuExitReason::SystemDown => todo!(),
151+
arm_vcpu::AxVCpuExitReason::SystemDown => {
152+
info!("vCPU {} requested system shutdown", self.common.bind_id);
153+
self.shutdown()?;
154+
}
167155
arm_vcpu::AxVCpuExitReason::Nothing => {}
168156
arm_vcpu::AxVCpuExitReason::SendIPI {
169157
target_cpu,
@@ -179,3 +167,11 @@ impl VCpu {
179167
Ok(())
180168
}
181169
}
170+
171+
impl Deref for VCpu {
172+
type Target = VCpuCommon;
173+
174+
fn deref(&self) -> &Self::Target {
175+
&self.common
176+
}
177+
}

src/arch/aarch64/vm/init.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use std::{
88
use arm_vcpu::Aarch64VCpuSetupConfig;
99

1010
use crate::{
11-
GuestPhysAddr, TASK_STACK_SIZE, VmData, VmStatusInitOps,
11+
GuestPhysAddr, TASK_STACK_SIZE, VmRunCommonData, VmStatusInitOps,
1212
arch::{VmStatusRunning, cpu::VCpu},
1313
config::AxVMConfig,
14+
data2::VmDataWeak,
1415
vm::{MappingFlags, VmId},
1516
};
1617

@@ -19,26 +20,27 @@ const VM_ASPACE_SIZE: usize = 0x7fff_ffff_f000;
1920
const VM_ASPACE_END: GuestPhysAddr =
2021
GuestPhysAddr::from_usize(VM_ASPACE_BASE.as_usize() + VM_ASPACE_SIZE);
2122

22-
pub struct VmInit {
23+
pub struct VmMachineInited {
2324
pub id: VmId,
2425
pub name: String,
25-
pt_levels: usize,
26-
stop_requested: AtomicBool,
27-
run_data: Option<VmStatusRunning>,
26+
// pt_levels: usize,
27+
// stop_requested: AtomicBool,
28+
pub run_data: VmStatusRunning,
2829
}
2930

30-
impl VmInit {
31+
impl VmMachineInited {
3132
/// Creates a new VM with the given configuration
32-
pub fn new(config: &AxVMConfig) -> anyhow::Result<Self> {
33-
let vm = Self {
34-
id: config.id().into(),
35-
name: config.name(),
36-
pt_levels: 4,
37-
stop_requested: AtomicBool::new(false),
38-
run_data: None,
39-
};
40-
Ok(vm)
41-
}
33+
// pub fn new(config: &AxVMConfig) -> anyhow::Result<Self> {
34+
// let vm = Self {
35+
// id: config.id().into(),
36+
// name: config.name().into(),
37+
// pt_levels: 4,
38+
// stop_requested: AtomicBool::new(false),
39+
// run_data: None,
40+
// };
41+
// Ok(vm)
42+
// }
43+
4244

4345
/// Initializes the VM, creating vCPUs and setting up memory
4446
pub fn init(&mut self, config: AxVMConfig) -> anyhow::Result<()> {
@@ -47,14 +49,14 @@ impl VmInit {
4749
let vcpus = self.new_vcpus(&config)?;
4850

4951
let mut run_data = VmStatusRunning::new(
50-
VmData::new(self.pt_levels, VM_ASPACE_BASE..VM_ASPACE_END)?,
52+
VmRunCommonData::new(self.pt_levels, VM_ASPACE_BASE..VM_ASPACE_END)?,
5153
vcpus,
5254
);
5355

5456
debug!("Mapping memory regions for VM {} ({})", self.id, self.name);
5557
for memory_cfg in &config.memory_regions {
5658
use crate::vm::MappingFlags;
57-
let m = run_data.data.new_memory(
59+
let m = run_data.data.try_use()?.new_memory(
5860
memory_cfg,
5961
MappingFlags::READ
6062
| MappingFlags::WRITE
@@ -64,13 +66,13 @@ impl VmInit {
6466
run_data.data.add_memory(m);
6567
}
6668

67-
run_data.data.load_kernel_image(&config)?;
69+
run_data.data.try_use()?.load_kernel_image(&config)?;
6870
run_data.make_dtb(&config)?;
6971

70-
run_data.data.map_passthrough_regions()?;
72+
run_data.data.try_use()?.map_passthrough_regions()?;
7173

72-
let kernel_entry = run_data.data.kernel_entry();
73-
let gpt_root = run_data.data.gpt_root();
74+
let kernel_entry = run_data.data.try_use()?.kernel_entry();
75+
let gpt_root = run_data.data.try_use()?.gpt_root();
7476

7577
// Setup vCPUs
7678
for vcpu in &mut run_data.vcpus {
@@ -141,7 +143,7 @@ impl VmInit {
141143
}
142144
}
143145

144-
impl VmStatusInitOps for VmInit {
146+
impl VmStatusInitOps for VmMachineInited {
145147
type Running = VmStatusRunning;
146148

147149
fn id(&self) -> VmId {
@@ -152,7 +154,7 @@ impl VmStatusInitOps for VmInit {
152154
&self.name
153155
}
154156

155-
fn start(self) -> Result<Self::Running, (anyhow::Error, Self)> {
157+
fn start(self, vmdata: VmDataWeak) -> Result<Self::Running, (anyhow::Error, Self)> {
156158
let mut data = self.run_data.unwrap();
157159

158160
let mut vcpus = vec![];

src/arch/aarch64/vm/mod.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,29 @@ use fdt_edit::{Node, NodeRef, Property, RegInfo};
66
use memory_addr::MemoryAddr;
77

88
mod init;
9+
mod unint;
910

1011
use crate::{
11-
GuestPhysAddr, RunError, TASK_STACK_SIZE, Vm, VmData, VmStatusInitOps, VmStatusRunningOps,
12-
VmStatusStoppingOps,
12+
GuestPhysAddr, RunError, TASK_STACK_SIZE, Vm, VmDataWeak, VmRunCommonData, VmStatusInitOps,
13+
VmStatusRunningOps, VmStatusStoppingOps,
1314
arch::cpu::VCpu,
1415
config::{AxVMConfig, MemoryKind},
16+
data2::VmDataWeak,
1517
vhal::cpu::CpuHardId,
1618
vm::{MappingFlags, VmId},
1719
};
1820

19-
pub use init::VmInit;
21+
pub(crate) use init::*;
22+
pub(crate) use unint::*;
2023

2124
const VM_ASPACE_BASE: usize = 0x0;
2225
const VM_ASPACE_SIZE: usize = 0x7fff_ffff_f000;
2326

2427
impl VmStatusRunningOps for VmStatusRunning {
2528
type Stopping = VmStatusStopping;
2629

27-
fn stop(self) -> Result<Self::Stopping, (anyhow::Error, Self)>
28-
where
29-
Self: Sized,
30-
{
31-
Ok(VmStatusStopping {})
32-
}
33-
34-
fn do_work(&mut self) -> Result<(), RunError> {
35-
if self.vcpu_running_count.load(Ordering::SeqCst) == 0 {
36-
Err(RunError::Exit)
37-
} else {
38-
Ok(())
39-
}
30+
fn stop(self) -> Self::Stopping {
31+
Self::Stopping {}
4032
}
4133
}
4234

@@ -47,13 +39,13 @@ impl VmStatusStoppingOps for VmStatusStopping {}
4739
/// Data needed when VM is running
4840
pub struct VmStatusRunning {
4941
vcpus: Vec<VCpu>,
50-
data: VmData,
42+
data: VmDataWeak,
5143
dtb_addr: GuestPhysAddr,
5244
vcpu_running_count: Arc<AtomicUsize>,
5345
}
5446

5547
impl VmStatusRunning {
56-
pub(crate) fn new(data: VmData, vcpus: Vec<VCpu>) -> Self {
48+
pub(crate) fn new(data: VmDataWeak, vcpus: Vec<VCpu>) -> Self {
5749
Self {
5850
vcpus,
5951
data,
@@ -84,12 +76,12 @@ impl VmStatusRunning {
8476
}
8577
};
8678

87-
let mut guest_mem = self.data.new_memory(&kind, flags);
79+
let mut guest_mem = self.data.try_use()?.new_memory(&kind, flags);
8880

8981
self.dtb_addr = guest_mem.gpa();
9082

9183
guest_mem.copy_from_slice(0, &dtb_cfg.data);
92-
self.data.add_reserved_memory(guest_mem);
84+
self.data.try_use()?.add_reserved_memory(guest_mem);
9385
} else {
9486
debug!(
9587
"No dtb provided, generating new dtb for {} ({})",
@@ -127,7 +119,7 @@ impl VmStatusRunning {
127119
let root_address_cells = fdt.root().address_cells().unwrap_or(2);
128120
let root_size_cells = fdt.root().size_cells().unwrap_or(2);
129121

130-
for (i, m) in self.data.memories().iter().enumerate() {
122+
for (i, m) in self.data.try_use()?.memories().iter().enumerate() {
131123
let mut node = Node::new(&format!("memory@{i}"));
132124
let mut prop = Property::new("device_type", vec![]);
133125
prop.set_string("memory");
@@ -147,7 +139,7 @@ impl VmStatusRunning {
147139
let f = fdt_edit::Fdt::from_bytes(&dtb_data).unwrap();
148140
debug!("Generated DTB:\n{f}");
149141

150-
let mut guest_mem = self.data.memories().into_iter().next().unwrap();
142+
let mut guest_mem = self.data.try_use()?.memories().into_iter().next().unwrap();
151143
let mut dtb_start =
152144
(guest_mem.0.as_usize() + guest_mem.1.min(512 * 1024 * 1024)) - dtb_data.len();
153145
dtb_start = dtb_start.align_down_4k();
@@ -170,6 +162,8 @@ impl VmStatusRunning {
170162
fn copy_to_guest(&mut self, gpa: GuestPhysAddr, data: &[u8]) {
171163
let parts = self
172164
.data
165+
.try_use()
166+
.unwrap()
173167
.addrspace
174168
.lock()
175169
.translated_byte_buffer(gpa.as_usize().into(), data.len())

0 commit comments

Comments
 (0)