Skip to content

Commit 49073c5

Browse files
committed
hvf: ensure vcpus run in the right thread
For correctness and to support IPIs in future GICv3 implementations, we need to ensure each HVF vCPU runs in the right thread, so their respective IDs match each other. We can't supply an ID to hv_vcpu_create. Instead, it'll return an incremental integer each time we call it. In order to have deterministic IDs, we need to serialize each call. As we already have a Sender for notifying each vCPU TLS initialization, let's reuse and simply deplay writting to it until hv_vcpu_create has been called. While there, also set MPIDR to the same vCPU ID, as this will be needed to properly support the in-kernel HVF GICv3 implementation. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 3466191 commit 49073c5

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/hvf/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ impl<'a> HvfVcpu<'a> {
267267
return Err(Error::VcpuCreate);
268268
}
269269

270+
// We write vcpuid to Aff1 as otherwise it won't match the redistributor ID
271+
// when using HVF in-kernel GICv3.
272+
let ret =
273+
unsafe { hv_vcpu_set_sys_reg(vcpuid, hv_sys_reg_t_HV_SYS_REG_MPIDR_EL1, vcpuid << 8) };
274+
if ret != HV_SUCCESS {
275+
return Err(Error::VcpuCreate);
276+
}
277+
270278
let vcpu_exit: &hv_vcpu_exit_t = unsafe { vcpu_exit_ptr.as_mut().unwrap() };
271279

272280
Ok(Self {

src/vmm/src/macos/vstate.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,7 @@ impl Vcpu {
362362
self.init_thread_local_data()
363363
.expect("Cannot cleanly initialize vcpu TLS.");
364364

365-
init_tls_sender
366-
.send(true)
367-
.expect("Cannot notify vcpu TLS initialization.");
368-
369-
self.run();
365+
self.run(init_tls_sender);
370366
})
371367
.map_err(Error::VcpuSpawn)?;
372368

@@ -462,10 +458,14 @@ impl Vcpu {
462458
}
463459

464460
/// Main loop of the vCPU thread.
465-
pub fn run(&mut self) {
461+
pub fn run(&mut self, init_tls_sender: Sender<bool>) {
466462
let mut hvf_vcpu = HvfVcpu::new().expect("Can't create HVF vCPU");
467463
let hvf_vcpuid = hvf_vcpu.id();
468464

465+
init_tls_sender
466+
.send(true)
467+
.expect("Cannot notify vcpu TLS initialization.");
468+
469469
let (wfe_sender, wfe_receiver) = unbounded();
470470
self.vcpu_list.register(hvf_vcpuid, wfe_sender);
471471
self.intc.lock().unwrap().add_vcpu();

0 commit comments

Comments
 (0)