Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5883090rwb; Wed, 21 Sep 2022 14:00:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4lubgPWjVcWmhlj3kT5eJC5vXuYnCA3xu9fzalvFLVEruLunS18BzHjQLaJIAzhWajC3Qg X-Received: by 2002:a05:6a00:1410:b0:528:5a5a:d846 with SMTP id l16-20020a056a00141000b005285a5ad846mr81522pfu.9.1663794021796; Wed, 21 Sep 2022 14:00:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663794021; cv=none; d=google.com; s=arc-20160816; b=xAUgkmGHPP7tlGqswlvoun888DWM86RsCaoEE3VCpQkN9oNkGR9RdMz0ZcHz1c2hcB 1uW9kr5KWpGE5eh3u3uVjKsvjdgXutNKYBz31ce8vm1gPGztGLxlGui/unp2c57TWSlG 6DwH26tmNhb4IxOIS6BmujiEfIxHAB4gz7qwS4DMbxbz4cTO3i+R1UgqR6/xc+7qHAuy I/6vgPt3KwShdJ4AgnMn72xBvliLvhpHgnaPU6q1A/76xZQrKC4MaFCVqA/f2/42Ki1X 0nae380AcJFFnq+MQYSBJ5YKlpo9sFs8lD+yTZtGNNvqBpq5doRU24MVVIi1FxhFqfqx jLkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=gADxGD3M+iWlkeq6JJoA+zipD9oPqMfH/FDJZbo9W8k=; b=Z4stNc3s/+91R5ZtxEdsR2Kxjt+bsDvhyEl836tR9rfhTkala5Hnbrl3bgiGmo7o7l SKRdCmHU+SQnqXxUidXOEa5E1RtnV+YsSyJmHll1VJUCpTVh4SeS4SCYmqhsfO5S4Fkk Vq3b1Rw0XaRLJfabgLxoTs5tzac8QIwa24/hU9EO1KNGYubeAT3erIx3qTTA0+m1WyzF ECMnABEKB3vS9F3SC17OXGbaLg2owtqAo/INDAiPOzIHhz5nqx5e/vd/nBij/CKmdX19 jw6KJXW3SS5n0Hd3XxJ59Q4+e1UziEN53GJWypxE9EiuDOI8L4Q3EB6JU6bHYTDFAbZ2 Flbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=dYJhkoXF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t23-20020a62ea17000000b00545f8bf5f6bsi3489152pfh.274.2022.09.21.14.00.07; Wed, 21 Sep 2022 14:00:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=dYJhkoXF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230497AbiIUUQa (ORCPT + 99 others); Wed, 21 Sep 2022 16:16:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230466AbiIUUQR (ORCPT ); Wed, 21 Sep 2022 16:16:17 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D100A1A54 for ; Wed, 21 Sep 2022 13:16:14 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id x1-20020a17090a8a8100b00200a805fba9so9038105pjn.7 for ; Wed, 21 Sep 2022 13:16:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date; bh=gADxGD3M+iWlkeq6JJoA+zipD9oPqMfH/FDJZbo9W8k=; b=dYJhkoXFX6b09AesK77GzgwjsJP150V80OHAJ8ei1sYqvUFapGXol5fi5V/hKIqMr6 PYlucJZqSS18InQIlMZUVDAxyNhdjgumX6DUM5daD6mxqxi4exTGVvx+5s55YmzDYwl9 XPZC3EvSQzavcTt9UWfqD3Q+c28BXoXfPvCps2kSTH/5cq6e7A5OMu3TFI9mkBwQF6Wh Vhw8nmDaijp1zPmYgm81t5/kZ28+8JXhxCvQGAGId/3F5hgALR+fvCT5ARGp2rY4rO4r XkhmTLbeT/P5Sey2XIrHiMf8EUAUOmhKeUPS7ke+5RoKPcoUGhv54GBPNXDUj2BCvpf5 PbcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date; bh=gADxGD3M+iWlkeq6JJoA+zipD9oPqMfH/FDJZbo9W8k=; b=P7kIuNH6FYm2+7s21cb4VaWfspgM1yCjb7SDi9+ZlhbBsl5wKZApQeM6+qYj/h5CAz hJy/U2UxIAumsd0txKzlwjGQyaqEQOXZNeYz64jKull6f67Y7VdE2L6z7mfzcatfpPL3 BWMTTjonb3ojjJP0SrgfCHd4T6HkAEW2rJWGLaVF9PBa+l5CtSOiGfWCUITpuBLAtgdD XcV3tBYb7Ji9Q0l1kmeSAHhGWVadBbUhMironEXC4vR49jRDVzObKjvmmoYOqjetULRU d3+O+MbbkI8r7htsYYB5f5OWYJ3M3nb9mdqbYi8hZbXTc79TrnJ+EQMJNaJVnSNUjeoZ f0sw== X-Gm-Message-State: ACrzQf3+feb+PGdPoQwAipePuEBzgf66e14QiHDnK5yf8QY7eE7wceLR iSeknD2jraR2RK1qcyXji+7eMUTbs10= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2187:b0:541:ca61:48b7 with SMTP id h7-20020a056a00218700b00541ca6148b7mr30562304pfi.83.1663791374328; Wed, 21 Sep 2022 13:16:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 21 Sep 2022 20:16:06 +0000 In-Reply-To: <20220921201607.3156750-1-seanjc@google.com> Mime-Version: 1.0 References: <20220921201607.3156750-1-seanjc@google.com> X-Mailer: git-send-email 2.37.3.968.ga6b4b080e4-goog Message-ID: <20220921201607.3156750-4-seanjc@google.com> Subject: [PATCH 3/4] KVM: SVM: Add a proper field for Hyper-V VMCB enlightenments From: Sean Christopherson To: "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: linux-hyperv@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a union to provide hv_enlightenments side-by-side with the sw_reserved bytes that Hyper-V's enlightenments overlay. Casting sw_reserved everywhere is messy, confusing, and unnecessarily unsafe. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 7 ++++++- arch/x86/kvm/svm/nested.c | 9 ++++----- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/svm/svm_onhyperv.c | 2 +- arch/x86/kvm/svm/svm_onhyperv.h | 15 +++++++-------- tools/testing/selftests/kvm/include/x86_64/svm.h | 5 ++++- .../selftests/kvm/x86_64/hyperv_svm_test.c | 3 +-- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 0361626841bc..6befed2b30a6 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -5,6 +5,8 @@ #include #include +#include + /* * 32-bit intercept words in the VMCB Control Area, starting * at Byte offset 000h. @@ -161,7 +163,10 @@ struct __attribute__ ((__packed__)) vmcb_control_area { * Offset 0x3e0, 32 bytes reserved * for use by hypervisor/software. */ - u8 reserved_sw[32]; + union { + struct hv_enlightenments hv_enlightenments; + u8 reserved_sw[32]; + }; }; diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 76dcc8a3e849..a6a87d9743ce 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -201,8 +201,7 @@ void recalc_intercepts(struct vcpu_svm *svm) */ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) { - struct hv_enlightenments *hve = - (struct hv_enlightenments *)svm->nested.ctl.reserved_sw; + struct hv_enlightenments *hve = &svm->nested.ctl.hv_enlightenments; int i; /* @@ -391,8 +390,8 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu, /* Hyper-V extensions (Enlightened VMCB) */ if (kvm_hv_hypercall_enabled(vcpu)) { to->clean = from->clean; - memcpy(to->reserved_sw, from->reserved_sw, - sizeof(struct hv_enlightenments)); + memcpy(&to->hv_enlightenments, &from->hv_enlightenments, + sizeof(to->hv_enlightenments)); } } @@ -1485,7 +1484,7 @@ static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst, dst->virt_ext = from->virt_ext; dst->pause_filter_count = from->pause_filter_count; dst->pause_filter_thresh = from->pause_filter_thresh; - /* 'clean' and 'reserved_sw' are not changed by KVM */ + /* 'clean' and 'hv_enlightenments' are not changed by KVM */ } static int svm_get_nested_state(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6a7686bf6900..9eb2fc76732f 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -151,7 +151,10 @@ struct vmcb_ctrl_area_cached { u64 nested_cr3; u64 virt_ext; u32 clean; - u8 reserved_sw[32]; + union { + struct hv_enlightenments hv_enlightenments; + u8 reserved_sw[32]; + }; }; struct svm_nested_state { diff --git a/arch/x86/kvm/svm/svm_onhyperv.c b/arch/x86/kvm/svm/svm_onhyperv.c index 8cdc62c74a96..5d4036611a37 100644 --- a/arch/x86/kvm/svm/svm_onhyperv.c +++ b/arch/x86/kvm/svm/svm_onhyperv.c @@ -26,7 +26,7 @@ int svm_hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu) if (!*p_hv_pa_pg) return -ENOMEM; - hve = (struct hv_enlightenments *)to_svm(vcpu)->vmcb->control.reserved_sw; + hve = &to_svm(vcpu)->vmcb->control.hv_enlightenments; hve->partition_assist_page = __pa(*p_hv_pa_pg); hve->hv_vm_id = (unsigned long)vcpu->kvm; diff --git a/arch/x86/kvm/svm/svm_onhyperv.h b/arch/x86/kvm/svm/svm_onhyperv.h index 8d02654ad6f8..c8f8045305be 100644 --- a/arch/x86/kvm/svm/svm_onhyperv.h +++ b/arch/x86/kvm/svm/svm_onhyperv.h @@ -18,8 +18,10 @@ int svm_hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu); static inline void svm_hv_init_vmcb(struct vmcb *vmcb) { - struct hv_enlightenments *hve = - (struct hv_enlightenments *)vmcb->control.reserved_sw; + struct hv_enlightenments *hve = &vmcb->control.hv_enlightenments; + + BUILD_BUG_ON(sizeof(vmcb->control.hv_enlightenments) != + sizeof(vmcb->control.reserved_sw)); if (npt_enabled && ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) @@ -61,18 +63,15 @@ static inline void svm_hv_vmcb_dirty_nested_enlightenments( struct kvm_vcpu *vcpu) { struct vmcb *vmcb = to_svm(vcpu)->vmcb; - struct hv_enlightenments *hve = - (struct hv_enlightenments *)vmcb->control.reserved_sw; + struct hv_enlightenments *hve = &vmcb->control.hv_enlightenments; if (hve->hv_enlightenments_control.msr_bitmap) vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS); } -static inline void svm_hv_update_vp_id(struct vmcb *vmcb, - struct kvm_vcpu *vcpu) +static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu) { - struct hv_enlightenments *hve = - (struct hv_enlightenments *)vmcb->control.reserved_sw; + struct hv_enlightenments *hve = &vmcb->control.hv_enlightenments; u32 vp_index = kvm_hv_get_vpindex(vcpu); if (hve->hv_vp_id != vp_index) { diff --git a/tools/testing/selftests/kvm/include/x86_64/svm.h b/tools/testing/selftests/kvm/include/x86_64/svm.h index 10b9d0d4e0ec..0c32549c6b64 100644 --- a/tools/testing/selftests/kvm/include/x86_64/svm.h +++ b/tools/testing/selftests/kvm/include/x86_64/svm.h @@ -123,7 +123,10 @@ struct __attribute__ ((__packed__)) vmcb_control_area { * Offset 0x3e0, 32 bytes reserved * for use by hypervisor/software. */ - u8 reserved_sw[32]; + union { + struct hv_enlightenments hv_enlightenments; + u8 reserved_sw[32]; + }; }; diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c index b366b197759f..81b204ee509a 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c @@ -46,8 +46,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm) { unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; struct vmcb *vmcb = svm->vmcb; - struct hv_enlightenments *hve = - (struct hv_enlightenments *)vmcb->control.reserved_sw; + struct hv_enlightenments *hve = &vmcb->control.hv_enlightenments; GUEST_SYNC(1); -- 2.37.3.968.ga6b4b080e4-goog