git: 35abc6c238e9 - main - vmm: Use vm_get_maxcpus() instead of VM_MAXCPU in various places.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Fri, 18 Nov 2022 18:26:22 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=35abc6c238e98e313c5b1cb5ed18b8ed68615abc

commit 35abc6c238e98e313c5b1cb5ed18b8ed68615abc
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-11-18 17:57:38 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-11-18 18:25:34 +0000

    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
---
 sys/amd64/vmm/amd/svm.c   |  5 +++--
 sys/amd64/vmm/amd/vmcb.c  |  4 ++--
 sys/amd64/vmm/intel/vmx.c | 12 ++++++------
 sys/amd64/vmm/io/vlapic.c |  6 ++++--
 sys/amd64/vmm/vmm.c       | 19 ++++++++++++-------
 5 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/sys/amd64/vmm/amd/svm.c b/sys/amd64/vmm/amd/svm.c
index 4195cc5bd049..1a7990383b09 100644
--- a/sys/amd64/vmm/amd/svm.c
+++ b/sys/amd64/vmm/amd/svm.c
@@ -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 */
diff --git a/sys/amd64/vmm/amd/vmcb.c b/sys/amd64/vmm/amd/vmcb.c
index 0341e4e6c07c..69fe853ca843 100644
--- a/sys/amd64/vmm/amd/vmcb.c
+++ b/sys/amd64/vmm/amd/vmcb.c
@@ -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;
 	}
diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c
index 857028dcd0f1..104df77b351c 100644
--- a/sys/amd64/vmm/intel/vmx.c
+++ b/sys/amd64/vmm/intel/vmx.c
@@ -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);
 
diff --git a/sys/amd64/vmm/io/vlapic.c b/sys/amd64/vmm/io/vlapic.c
index 0e1fb1bbf006..76e3cd14a8f5 100644
--- a/sys/amd64/vmm/io/vlapic.c
+++ b/sys/amd64/vmm/io/vlapic.c
@@ -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 */
diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c
index 124fffec0d9e..da1db09bdf58 100644
--- a/sys/amd64/vmm/vmm.c
+++ b/sys/amd64/vmm/vmm.c
@@ -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 @@ done:
 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 -