bhyve: Simplify setting vCPU capabilities.

- Enable VM_CAP_IPI_EXIT in fbsdrun_set_capabilities along with other
  capabilities enabled on all vCPUs.

- Don't call fbsdrun_set_capabilities a second time on the BSP in
  spinup_vcpu.

- To preserve previous behavior, don't unconditionally enable
  unrestricted guest mode on the BSP (this unbreaks single-vCPU guests
  on Nehalem systems, though supporting such setups is of dubious
  value).  Other places that enbale UG on the BSP are careful to check
  the result of the operation and fail if it is not available.

- Don't set any capabilities in spinup_ap().  These are now all
  redundant with earlier settings from spinup_vcpu().

- While here, axe a stale comment from fbsdrun_addcpu().  This
  function is now always called from the main thread for all vCPUs.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37642
This commit is contained in:
John Baldwin 2022-12-21 10:31:16 -08:00
parent e7d5d2d187
commit 461663ddba
3 changed files with 16 additions and 29 deletions

View File

@ -552,12 +552,6 @@ fbsdrun_addcpu(struct vmctx *ctx, int newcpu, uint64_t rip, bool suspend)
{
int error;
/*
* The 'newcpu' must be activated in the context of 'fromcpu'. If
* vm_activate_cpu() is delayed until newcpu's pthread starts running
* then vmm.ko is out-of-sync with bhyve and this can create a race
* with vm_suspend().
*/
error = vm_activate_cpu(ctx, newcpu);
if (error != 0)
err(EX_OSERR, "could not activate CPU %d", newcpu);
@ -1044,7 +1038,7 @@ num_vcpus_allowed(struct vmctx *ctx)
return (1);
}
void
static void
fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
{
int err, tmp;
@ -1086,6 +1080,9 @@ fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
}
vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
err = vm_set_capability(ctx, cpu, VM_CAP_IPI_EXIT, 1);
assert(err == 0);
}
static struct vmctx *
@ -1157,16 +1154,21 @@ spinup_vcpu(struct vmctx *ctx, int vcpu, bool suspend)
int error;
uint64_t rip;
if (vcpu != BSP) {
fbsdrun_set_capabilities(ctx, vcpu);
/*
* Enable the 'unrestricted guest' mode for APs.
*
* APs startup in power-on 16-bit mode.
*/
error = vm_set_capability(ctx, vcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
assert(error == 0);
}
error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
assert(error == 0);
fbsdrun_set_capabilities(ctx, vcpu);
error = vm_set_capability(ctx, vcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
assert(error == 0);
error = vm_set_capability(ctx, vcpu, VM_CAP_IPI_EXIT, 1);
assert(error == 0);
fbsdrun_addcpu(ctx, vcpu, rip, suspend);
}

View File

@ -45,7 +45,6 @@ void *paddr_guest2host(struct vmctx *ctx, uintptr_t addr, size_t len);
uintptr_t paddr_host2guest(struct vmctx *ctx, void *addr);
#endif
void fbsdrun_set_capabilities(struct vmctx *ctx, int cpu);
int fbsdrun_virtio_msix(void);
int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu);

View File

@ -87,20 +87,6 @@ spinup_ap(struct vmctx *ctx, int newcpu, uint64_t rip)
error = vcpu_reset(ctx, newcpu);
assert(error == 0);
fbsdrun_set_capabilities(ctx, newcpu);
/*
* Enable the 'unrestricted guest' mode for 'newcpu'.
*
* Set up the processor state in power-on 16-bit mode, with the CS:IP
* init'd to the specified low-mem 4K page.
*/
error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
assert(error == 0);
error = vm_set_capability(ctx, newcpu, VM_CAP_IPI_EXIT, 1);
assert(error == 0);
spinup_ap_realmode(ctx, newcpu, &rip);
vm_resume_cpu(ctx, newcpu);