Skip to content

Commit

Permalink
hvf: ensure vcpus run in the right thread
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
slp committed Dec 5, 2024
1 parent 587c49d commit d0ac954
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/hvf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ impl HvfVcpu<'_> {
return Err(Error::VcpuCreate);
}

// We write vcpuid to Aff1 as otherwise it won't match the redistributor ID
// when using HVF in-kernel GICv3.
let ret =
unsafe { hv_vcpu_set_sys_reg(vcpuid, hv_sys_reg_t_HV_SYS_REG_MPIDR_EL1, vcpuid << 8) };
if ret != HV_SUCCESS {
return Err(Error::VcpuCreate);
}

let vcpu_exit: &hv_vcpu_exit_t = unsafe { vcpu_exit_ptr.as_mut().unwrap() };

Ok(Self {
Expand Down
12 changes: 6 additions & 6 deletions src/vmm/src/macos/vstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,7 @@ impl Vcpu {
self.init_thread_local_data()
.expect("Cannot cleanly initialize vcpu TLS.");

init_tls_sender
.send(true)
.expect("Cannot notify vcpu TLS initialization.");

self.run();
self.run(init_tls_sender);
})
.map_err(Error::VcpuSpawn)?;

Expand Down Expand Up @@ -462,10 +458,14 @@ impl Vcpu {
}

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

init_tls_sender
.send(true)
.expect("Cannot notify vcpu TLS initialization.");

let (wfe_sender, wfe_receiver) = unbounded();
self.vcpu_list.register(hvf_vcpuid, wfe_sender);
self.intc.lock().unwrap().add_vcpu();
Expand Down

0 comments on commit d0ac954

Please sign in to comment.