vmm: Use vm_get_maxcpus() instead of VM_MAXCPU in various places.

Mostly these are loops that iterate over all possible vCPU IDs for a
specific virtual machine.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37147
This commit is contained in:
John Baldwin
2022-11-18 09:57:38 -08:00
parent a7db532e3a
commit 35abc6c238
5 changed files with 27 additions and 19 deletions
+3 -2
View File
@@ -2422,14 +2422,15 @@ svm_snapshot(void *arg, struct vm_snapshot_meta *meta)
/* struct svm_softc is AMD's representation for SVM softc */
struct svm_softc *sc;
struct svm_vcpu *vcpu;
int i;
int ret;
uint16_t i, maxcpus;
sc = arg;
KASSERT(sc != NULL, ("%s: arg was NULL", __func__));
for (i = 0; i < VM_MAXCPU; i++) {
maxcpus = vm_get_maxcpus(sc->vm);
for (i = 0; i < maxcpus; i++) {
vcpu = &sc->vcpu[i];
/* Snapshot swctx for virtual cpu i */
+2 -2
View File
@@ -463,7 +463,7 @@ vmcb_getany(struct svm_softc *sc, int vcpu, int ident, uint64_t *val)
{
int error = 0;
if (vcpu < 0 || vcpu >= VM_MAXCPU) {
if (vcpu < 0 || vcpu >= vm_get_maxcpus(sc->vm)) {
error = EINVAL;
goto err;
}
@@ -484,7 +484,7 @@ vmcb_setany(struct svm_softc *sc, int vcpu, int ident, uint64_t val)
{
int error = 0;
if (vcpu < 0 || vcpu >= VM_MAXCPU) {
if (vcpu < 0 || vcpu >= vm_get_maxcpus(sc->vm)) {
error = EINVAL;
goto err;
}
+6 -6
View File
@@ -1029,12 +1029,12 @@ vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial)
static void *
vmx_init(struct vm *vm, pmap_t pmap)
{
uint16_t vpid[VM_MAXCPU];
int i, error;
struct vmx *vmx;
struct vmcs *vmcs;
uint32_t exc_bitmap;
uint16_t maxcpus;
uint16_t maxcpus = vm_get_maxcpus(vm);
uint16_t vpid[maxcpus];
vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO);
if ((uintptr_t)vmx & PAGE_MASK) {
@@ -1097,7 +1097,7 @@ vmx_init(struct vm *vm, pmap_t pmap)
((cap_rdpid || cap_rdtscp) && guest_msr_ro(vmx, MSR_TSC_AUX)))
panic("vmx_init: error setting guest msr access");
vpid_alloc(vpid, VM_MAXCPU);
vpid_alloc(vpid, maxcpus);
if (virtual_interrupt_delivery) {
error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE,
@@ -1106,7 +1106,6 @@ vmx_init(struct vm *vm, pmap_t pmap)
KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error));
}
maxcpus = vm_get_maxcpus(vm);
for (i = 0; i < maxcpus; i++) {
vmcs = &vmx->vmcs[i];
vmcs->identifier = vmx_revision();
@@ -4074,14 +4073,15 @@ vmx_snapshot(void *arg, struct vm_snapshot_meta *meta)
{
struct vmx *vmx;
struct vmxctx *vmxctx;
int i;
int ret;
uint16_t i, maxcpus;
vmx = arg;
KASSERT(vmx != NULL, ("%s: arg was NULL", __func__));
for (i = 0; i < VM_MAXCPU; i++) {
maxcpus = vm_get_maxcpus(vmx->vm);
for (i = 0; i < maxcpus; i++) {
SNAPSHOT_BUF_OR_LEAVE(vmx->guest_msrs[i],
sizeof(vmx->guest_msrs[i]), meta, ret, done);
+4 -2
View File
@@ -1857,16 +1857,18 @@ vlapic_reset_callout(struct vlapic *vlapic, uint32_t ccr)
int
vlapic_snapshot(struct vm *vm, struct vm_snapshot_meta *meta)
{
int i, ret;
int ret;
struct vlapic *vlapic;
struct LAPIC *lapic;
uint32_t ccr;
uint16_t i, maxcpus;
KASSERT(vm != NULL, ("%s: arg was NULL", __func__));
ret = 0;
for (i = 0; i < VM_MAXCPU; i++) {
maxcpus = vm_get_maxcpus(vm);
for (i = 0; i < maxcpus; i++) {
vlapic = vm_lapic(vm, i);
/* snapshot the page first; timer period depends on icr_timer */
+12 -7
View File
@@ -2806,11 +2806,12 @@ vm_snapshot_vcpus(struct vm *vm, struct vm_snapshot_meta *meta)
{
uint64_t tsc, now;
int ret;
int i;
struct vcpu *vcpu;
uint16_t i, maxcpus;
now = rdtsc();
for (i = 0; i < VM_MAXCPU; i++) {
maxcpus = vm_get_maxcpus(vm);
for (i = 0; i < maxcpus; i++) {
vcpu = &vm->vcpu[i];
SNAPSHOT_VAR_OR_LEAVE(vcpu->x2apic_state, meta, ret, done);
@@ -2852,11 +2853,13 @@ vm_snapshot_vm(struct vm *vm, struct vm_snapshot_meta *meta)
static int
vm_snapshot_vmcx(struct vm *vm, struct vm_snapshot_meta *meta)
{
int i, error;
int error;
uint16_t i, maxcpus;
error = 0;
for (i = 0; i < VM_MAXCPU; i++) {
maxcpus = vm_get_maxcpus(vm);
for (i = 0; i < maxcpus; i++) {
error = vmmops_vmcx_snapshot(vm->cookie, meta, i);
if (error != 0) {
printf("%s: failed to snapshot vmcs/vmcb data for "
@@ -2921,7 +2924,7 @@ vm_set_tsc_offset(struct vm *vm, int vcpuid, uint64_t offset)
{
struct vcpu *vcpu;
if (vcpuid < 0 || vcpuid >= VM_MAXCPU)
if (vcpuid < 0 || vcpuid >= vm_get_maxcpus(vm))
return (EINVAL);
vcpu = &vm->vcpu[vcpuid];
@@ -2933,9 +2936,10 @@ vm_set_tsc_offset(struct vm *vm, int vcpuid, uint64_t offset)
int
vm_restore_time(struct vm *vm)
{
int error, i;
int error;
uint64_t now;
struct vcpu *vcpu;
uint16_t i, maxcpus;
now = rdtsc();
@@ -2943,7 +2947,8 @@ vm_restore_time(struct vm *vm)
if (error)
return (error);
for (i = 0; i < nitems(vm->vcpu); i++) {
maxcpus = vm_get_maxcpus(vm);
for (i = 0; i < maxcpus; i++) {
vcpu = &vm->vcpu[i];
error = vmmops_restore_tsc(vm->cookie, i, vcpu->tsc_offset -