From nobody Thu Jan 26 22:11:48 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P2w0h5l3gz3c84w; Thu, 26 Jan 2023 22:11:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P2w0h469Sz3vKN; Thu, 26 Jan 2023 22:11:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674771108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/JQXaeRZg4SILLsK4MSs3Jy6zVarQ2JmB8UEu6y/0oo=; b=ub/PxRbdl+2xWWtC0JIhNeDx3Xv5vL/pDHNK0yjF4ZYoZ8rTng9dTbxkw+HWO8LThlwyxd wBQAVHb+SL/Fns/xZObURSMTtUU9QrGIhMhfbCdOawxCmkA1/jpY4ayl3tixUha+RO7x2X 6sbxC5e7RKWTzV03FAa3bz9+N+OIaEBylFcdJdx6oOe4Fmg8grfxo9mPz9bu4AYtH8iOSn 5no2tLZryzXW/f7Jv5Xf1dcLO8+EB/6PZozTUPqkoM9qlwG9LqcBV1/g7TRgApdiiJOz+G MBW66lGnbN6B3d9n37uobwi1lTmk2rLMui1a07/Q/yp7WNMZq7EChH7uo5rRoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674771108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/JQXaeRZg4SILLsK4MSs3Jy6zVarQ2JmB8UEu6y/0oo=; b=Nl4aMWQk+kakm4Zl9oltJQX6WKwn8Cy075PBt4alt+DTj8SbsX1MbbnL32fgmzHdhugLoP 3h/1lbipS/lj3KAhhRLzOdgGAX02sP+Gaviq/jG7CmAu1j/kyxDRVQ4K4IxdUyEyEPDniq JITAy/G2bh0chXDHP9l4p1Ra2QFURuJynS4Wevwr54lYkW2AvIv+VHe9aJrq/jYIPi0UiI 6baurLgRBezhlgPkBCGZie5NVcMP3x1RY0dnsf8Hwf3AL85iNeeF1JK3UkLBpYW/aaqfrf /Vo3rZ6nFfxMh1DH+zHHaADPWiJiZ3DXwXkszxMLNiymo1fwV4Wx2HTDW43BVA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674771108; a=rsa-sha256; cv=none; b=UoDHVVPF/v2H/NEfutUQaWu1c2RIiIW/L0OCbULAD3Gm7FtGufzWTX0/lu53DxqL8AAGN2 hfFEbC+r0KnolRPdafxwNq5Q/O4HYrrU96Q5dxLbG89S7c8JO60MumCYCqly5pvRHQuGe6 PD3aPGPzmQoIlBWeCcrGKpuHFBZUCam63PYX6Nb/+BEOtNQTiiDH1d2aReNc70LxwsA0uf g2lzNKcGrQOsJ13uwJx1xbbhzLo4k1m9Hlzlbfuz2M87eNkEeaopoOgG6GFTpj0OFwvWAB GXad2B0QBXIhXfGOfa1sXBHalw6wc+H4jGsSbERqZEQQukwhCnTpQnQzhrGz2g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P2w0h36qFzlh4; Thu, 26 Jan 2023 22:11:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30QMBm7e021814; Thu, 26 Jan 2023 22:11:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30QMBmNN021813; Thu, 26 Jan 2023 22:11:48 GMT (envelope-from git) Date: Thu, 26 Jan 2023 22:11:48 GMT Message-Id: <202301262211.30QMBmNN021813@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: fa1eabaa8d9a - stable/13 - vmm: Expose struct vcpu as an opaque type. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: fa1eabaa8d9a357817eca29c7ad42ac2c8c8872f Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=fa1eabaa8d9a357817eca29c7ad42ac2c8c8872f commit fa1eabaa8d9a357817eca29c7ad42ac2c8c8872f Author: John Baldwin AuthorDate: 2022-11-18 18:01:05 +0000 Commit: John Baldwin CommitDate: 2023-01-26 21:45:43 +0000 vmm: Expose struct vcpu as an opaque type. Pass a pointer to the current struct vcpu to the vcpu_init callback and save this pointer in the CPU-specific vcpu structures. Add routines to fetch a struct vcpu by index from a VM and to query the VM and vcpuid from a struct vcpu. Reviewed by: corvink, markj Differential Revision: https://reviews.freebsd.org/D37156 (cherry picked from commit 950af9ffc616ee573a1ce6ef0c841e897b13dfc4) --- sys/amd64/include/vmm.h | 7 ++++++- sys/amd64/vmm/amd/svm.c | 4 +++- sys/amd64/vmm/amd/svm_softc.h | 1 + sys/amd64/vmm/intel/vmx.c | 4 +++- sys/amd64/vmm/intel/vmx.h | 1 + sys/amd64/vmm/io/vlapic_priv.h | 2 ++ sys/amd64/vmm/vmm.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/sys/amd64/include/vmm.h b/sys/amd64/include/vmm.h index 43a4f123f40a..7548fea76696 100644 --- a/sys/amd64/include/vmm.h +++ b/sys/amd64/include/vmm.h @@ -143,6 +143,7 @@ enum x2apic_state { #ifdef _KERNEL CTASSERT(VM_MAX_NAMELEN >= VM_MIN_NAMELEN); +struct vcpu; struct vm; struct vm_exception; struct seg_desc; @@ -170,7 +171,8 @@ typedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); typedef int (*vmi_run_func_t)(void *vcpui, register_t rip, struct pmap *pmap, struct vm_eventinfo *info); typedef void (*vmi_cleanup_func_t)(void *vmi); -typedef void * (*vmi_vcpu_init_func_t)(void *vmi, int vcpu_id); +typedef void * (*vmi_vcpu_init_func_t)(void *vmi, struct vcpu *vcpu, + int vcpu_id); typedef void (*vmi_vcpu_cleanup_func_t)(void *vcpui); typedef int (*vmi_get_register_t)(void *vcpui, int num, uint64_t *retval); typedef int (*vmi_set_register_t)(void *vcpui, int num, uint64_t val); @@ -268,6 +270,9 @@ void vm_nmi_clear(struct vm *vm, int vcpuid); int vm_inject_extint(struct vm *vm, int vcpu); int vm_extint_pending(struct vm *vm, int vcpuid); void vm_extint_clear(struct vm *vm, int vcpuid); +int vcpu_vcpuid(struct vcpu *vcpu); +struct vm *vcpu_vm(struct vcpu *vcpu); +struct vcpu *vm_vcpu(struct vm *vm, int cpu); struct vlapic *vm_lapic(struct vm *vm, int cpu); struct vioapic *vm_ioapic(struct vm *vm); struct vhpet *vm_hpet(struct vm *vm); diff --git a/sys/amd64/vmm/amd/svm.c b/sys/amd64/vmm/amd/svm.c index a8008fd4c032..5eeef4b9a0fe 100644 --- a/sys/amd64/vmm/amd/svm.c +++ b/sys/amd64/vmm/amd/svm.c @@ -609,13 +609,14 @@ svm_init(struct vm *vm, pmap_t pmap) } static void * -svm_vcpu_init(void *vmi, int vcpuid) +svm_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid) { struct svm_softc *sc = vmi; struct svm_vcpu *vcpu; vcpu = malloc(sizeof(*vcpu), M_SVM, M_WAITOK | M_ZERO); vcpu->sc = sc; + vcpu->vcpu = vcpu1; vcpu->vcpuid = vcpuid; vcpu->vmcb = malloc_aligned(sizeof(struct vmcb), PAGE_SIZE, M_SVM, M_WAITOK | M_ZERO); @@ -2409,6 +2410,7 @@ svm_vlapic_init(void *vcpui) vcpu = vcpui; vlapic = malloc(sizeof(struct vlapic), M_SVM_VLAPIC, M_WAITOK | M_ZERO); vlapic->vm = vcpu->sc->vm; + vlapic->vcpu = vcpu->vcpu; vlapic->vcpuid = vcpu->vcpuid; vlapic->apic_page = malloc_aligned(PAGE_SIZE, PAGE_SIZE, M_SVM_VLAPIC, M_WAITOK | M_ZERO); diff --git a/sys/amd64/vmm/amd/svm_softc.h b/sys/amd64/vmm/amd/svm_softc.h index e958cc62b343..5cc2da5343a0 100644 --- a/sys/amd64/vmm/amd/svm_softc.h +++ b/sys/amd64/vmm/amd/svm_softc.h @@ -45,6 +45,7 @@ struct asid { struct svm_vcpu { struct svm_softc *sc; + struct vcpu *vcpu; struct vmcb *vmcb; /* hardware saved vcpu context */ struct svm_regctx swctx; /* software saved vcpu context */ uint64_t vmcb_pa; /* VMCB physical address */ diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c index 47e3be32c1b0..ec1b5c80dcff 100644 --- a/sys/amd64/vmm/intel/vmx.c +++ b/sys/amd64/vmm/intel/vmx.c @@ -1110,7 +1110,7 @@ vmx_init(struct vm *vm, pmap_t pmap) } static void * -vmx_vcpu_init(void *vmi, int vcpuid) +vmx_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid) { struct vmx *vmx = vmi; struct vmcs *vmcs; @@ -1120,6 +1120,7 @@ vmx_vcpu_init(void *vmi, int vcpuid) vcpu = malloc(sizeof(*vcpu), M_VMX, M_WAITOK | M_ZERO); vcpu->vmx = vmx; + vcpu->vcpu = vcpu1; vcpu->vcpuid = vcpuid; vcpu->vmcs = malloc_aligned(sizeof(*vmcs), PAGE_SIZE, M_VMX, M_WAITOK | M_ZERO); @@ -4074,6 +4075,7 @@ vmx_vlapic_init(void *vcpui) vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO); vlapic->vm = vmx->vm; + vlapic->vcpu = vcpu->vcpu; vlapic->vcpuid = vcpu->vcpuid; vlapic->apic_page = (struct LAPIC *)vcpu->apic_page; diff --git a/sys/amd64/vmm/intel/vmx.h b/sys/amd64/vmm/intel/vmx.h index 80719b003b1a..87a3660340f6 100644 --- a/sys/amd64/vmm/intel/vmx.h +++ b/sys/amd64/vmm/intel/vmx.h @@ -128,6 +128,7 @@ enum { struct vmx_vcpu { struct vmx *vmx; + struct vcpu *vcpu; struct vmcs *vmcs; struct apic_page *apic_page; struct pir_desc *pir_desc; diff --git a/sys/amd64/vmm/io/vlapic_priv.h b/sys/amd64/vmm/io/vlapic_priv.h index 4b3e9009e68c..2ac0cbf68117 100644 --- a/sys/amd64/vmm/io/vlapic_priv.h +++ b/sys/amd64/vmm/io/vlapic_priv.h @@ -138,6 +138,7 @@ enum boot_state { #define VLAPIC_MAXLVT_INDEX APIC_LVT_CMCI +struct vcpu; struct vlapic; struct vlapic_ops { @@ -151,6 +152,7 @@ struct vlapic_ops { struct vlapic { struct vm *vm; + struct vcpu *vcpu; int vcpuid; struct LAPIC *apic_page; struct vlapic_ops ops; diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c index 51868186f6e2..7f9072b7200d 100644 --- a/sys/amd64/vmm/vmm.c +++ b/sys/amd64/vmm/vmm.c @@ -104,8 +104,10 @@ struct vlapic; struct vcpu { struct mtx mtx; /* (o) protects 'state' and 'hostcpu' */ enum vcpu_state state; /* (o) vcpu state */ + int vcpuid; /* (o) */ int hostcpu; /* (o) vcpu's host cpu */ int reqidle; /* (i) request vcpu to idle */ + struct vm *vm; /* (o) */ void *cookie; /* (i) cpu-specific data */ struct vlapic *vlapic; /* (i) APIC device model */ enum x2apic_state x2apic_state; /* (i) APIC mode */ @@ -184,6 +186,21 @@ struct vm { uint16_t maxcpus; /* (o) max pluggable cpus */ }; +#define VMM_CTR0(vcpu, format) \ + VCPU_CTR0((vcpu)->vm, (vcpu)->vcpuid, format) + +#define VMM_CTR1(vcpu, format, p1) \ + VCPU_CTR1((vcpu)->vm, (vcpu)->vcpuid, format, p1) + +#define VMM_CTR2(vcpu, format, p1, p2) \ + VCPU_CTR2((vcpu)->vm, (vcpu)->vcpuid, format, p1, p2) + +#define VMM_CTR3(vcpu, format, p1, p2, p3) \ + VCPU_CTR3((vcpu)->vm, (vcpu)->vcpuid, format, p1, p2, p3) + +#define VMM_CTR4(vcpu, format, p1, p2, p3, p4) \ + VCPU_CTR4((vcpu)->vm, (vcpu)->vcpuid, format, p1, p2, p3, p4) + static int vmm_initialized; static void vmmops_panic(void); @@ -212,7 +229,8 @@ DEFINE_VMMOPS_IFUNC(void *, init, (struct vm *vm, struct pmap *pmap)) DEFINE_VMMOPS_IFUNC(int, run, (void *vcpui, register_t rip, struct pmap *pmap, struct vm_eventinfo *info)) DEFINE_VMMOPS_IFUNC(void, cleanup, (void *vmi)) -DEFINE_VMMOPS_IFUNC(void *, vcpu_init, (void *vmi, int vcpu_id)) +DEFINE_VMMOPS_IFUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu, + int vcpu_id)) DEFINE_VMMOPS_IFUNC(void, vcpu_cleanup, (void *vcpui)) DEFINE_VMMOPS_IFUNC(int, getreg, (void *vcpui, int num, uint64_t *retval)) DEFINE_VMMOPS_IFUNC(int, setreg, (void *vcpui, int num, uint64_t val)) @@ -327,12 +345,14 @@ vcpu_init(struct vm *vm, int vcpu_id, bool create) vcpu_lock_init(vcpu); vcpu->state = VCPU_IDLE; vcpu->hostcpu = NOCPU; + vcpu->vcpuid = vcpu_id; + vcpu->vm = vm; vcpu->guestfpu = fpu_save_area_alloc(); vcpu->stats = vmm_stat_alloc(); vcpu->tsc_offset = 0; } - vcpu->cookie = vmmops_vcpu_init(vm->cookie, vcpu_id); + vcpu->cookie = vmmops_vcpu_init(vm->cookie, vcpu, vcpu_id); vcpu->vlapic = vmmops_vlapic_init(vcpu->cookie); vm_set_x2apic_state(vm, vcpu_id, X2APIC_DISABLED); vcpu->reqidle = 0; @@ -2298,6 +2318,24 @@ vm_set_capability(struct vm *vm, int vcpu, int type, int val) return (vmmops_setcap(vcpu_cookie(vm, vcpu), type, val)); } +struct vm * +vcpu_vm(struct vcpu *vcpu) +{ + return (vcpu->vm); +} + +int +vcpu_vcpuid(struct vcpu *vcpu) +{ + return (vcpu->vcpuid); +} + +struct vcpu * +vm_vcpu(struct vm *vm, int vcpuid) +{ + return (&vm->vcpu[vcpuid]); +} + struct vlapic * vm_lapic(struct vm *vm, int cpu) {