[v13,45/48] KVM: selftests: Introduce rdmsr_from_l2() and use it for MSR-Bitmap tests
Message ID | 20221101145426.251680-46-vkuznets@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3024701wru; Tue, 1 Nov 2022 08:04:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5o/lb+KwmKnw0WR0GEbOUq10s8iRM2Jugczw4z+oNXIPsLYEUewJQGXJvqvMwMvvRFbO3Q X-Received: by 2002:a17:906:c9d4:b0:7ad:9ada:a830 with SMTP id hk20-20020a170906c9d400b007ad9adaa830mr133911ejb.693.1667315048486; Tue, 01 Nov 2022 08:04:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667315048; cv=none; d=google.com; s=arc-20160816; b=SDWxQagF5Ls97oWbf1SqU25imNxGL5g8cNhiphKgmxai06HiOXJ9vbCEQJMCfoNl8x bwjY/041hYYyTNwIpNDCgEkn8B1j7OzkCpzS+ycBijQpH2FtKNDq/VmL2pSOLbVcCqFP bT17snCRYnUWS/xiyVad1/q3vohAOFowUlbtjKxK+2yyS75YfyOzdLIVWDS3Rv0O1xyk 3x4upS8B6mDMH9+NXsDmgyD4PonpmCdU7Ukovvcfz/n5GNbn7f+IhLHcIC/2OwnZt5yl yLuyVJg/X0/ekbp3y3Asdu3NPYrTKaHCHKHzMKa6lMoHMHx7Rq/9+omymCEuwHYiT9kX LVWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LhhcrvG+7RwHzHHKMtMlqTTJoFvq/28FWWa9Z7l4uf8=; b=Q3fZ6S69QdWPtAyPC5+6dzsAqi3cwE0RA6VVfqwsYPXMCbE5Ui/Nc6kgUhJidI0MoW +aVVjZnmfRx1PIuOOBC1keqUhVpIWg+A/d81ZhMzSbF2sBwM1ClVCgvf5rCmx8aEEtv2 J0CyXDeFNJuSso3EDE8TEruVIANX4hAasceP4x/XEWVjw1Kaz+b1JTiu2U6o9l1fvHz/ +apuXNkVO0h2+KeVhXGQx2xJwxl6/FfJLNOB28WWdUaXgPJaW5gmAaBKBiE961C/1kXz JiLRpCXgX67WdYqIVNPtzE9wTExaDhvX8TBxskOT9cw7jyxU97OrnBSEogxxzTHh/4ln L0ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UopM1vm6; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qk32-20020a1709077fa000b0077b2e822b8fsi12904909ejc.76.2022.11.01.08.03.43; Tue, 01 Nov 2022 08:04:08 -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=@redhat.com header.s=mimecast20190719 header.b=UopM1vm6; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231286AbiKAPCz (ORCPT <rfc822;kartikey406@gmail.com> + 99 others); Tue, 1 Nov 2022 11:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231455AbiKAPC3 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 1 Nov 2022 11:02:29 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8FDD1CFFC for <linux-kernel@vger.kernel.org>; Tue, 1 Nov 2022 07:57:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667314619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LhhcrvG+7RwHzHHKMtMlqTTJoFvq/28FWWa9Z7l4uf8=; b=UopM1vm6uGGr58MYJKhMaN9m9FCBKjHyN9jx+VawhxK/96xxzReFT+Ew8zfzxRiTUsj9zO h+kkh5VSg9Go1z4Sb4iqXJUS37zi9S2kmN0yawBMtdB0Bba5UVCEnEKLjciHd4rBvjuvh1 qvYIIBytmUlnRutfsA0Dzd7TqlhPTRM= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-483-eyrpg5fiP2uappcwww7VVQ-1; Tue, 01 Nov 2022 10:56:56 -0400 X-MC-Unique: eyrpg5fiP2uappcwww7VVQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9930D1C0BC62; Tue, 1 Nov 2022 14:56:55 +0000 (UTC) Received: from ovpn-194-149.brq.redhat.com (ovpn-194-149.brq.redhat.com [10.40.194.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id A36F7C2C8D8; Tue, 1 Nov 2022 14:56:53 +0000 (UTC) From: Vitaly Kuznetsov <vkuznets@redhat.com> To: kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>, Sean Christopherson <seanjc@google.com> Cc: Wanpeng Li <wanpengli@tencent.com>, Jim Mattson <jmattson@google.com>, Michael Kelley <mikelley@microsoft.com>, Siddharth Chandrasekaran <sidcha@amazon.de>, Yuan Yao <yuan.yao@linux.intel.com>, Maxim Levitsky <mlevitsk@redhat.com>, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v13 45/48] KVM: selftests: Introduce rdmsr_from_l2() and use it for MSR-Bitmap tests Date: Tue, 1 Nov 2022 15:54:23 +0100 Message-Id: <20221101145426.251680-46-vkuznets@redhat.com> In-Reply-To: <20221101145426.251680-1-vkuznets@redhat.com> References: <20221101145426.251680-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748306544370722823?= X-GMAIL-MSGID: =?utf-8?q?1748306544370722823?= |
Series |
KVM: x86: hyper-v: Fine-grained TLB flush + L2 TLB flush features
|
|
Commit Message
Vitaly Kuznetsov
Nov. 1, 2022, 2:54 p.m. UTC
Hyper-V MSR-Bitmap tests do RDMSR from L2 to exit to L1. While 'evmcs_test'
correctly clobbers all GPRs (which are not preserved), 'hyperv_svm_test'
does not. Introduce and use common rdmsr_from_l2() to avoid code
duplication and remove hardcoding of MSRs.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
.../selftests/kvm/include/x86_64/processor.h | 9 +++++++
.../testing/selftests/kvm/x86_64/evmcs_test.c | 24 ++++---------------
.../selftests/kvm/x86_64/hyperv_svm_test.c | 8 +++----
3 files changed, 17 insertions(+), 24 deletions(-)
Comments
On Tue, Nov 01, 2022, Vitaly Kuznetsov wrote: > Hyper-V MSR-Bitmap tests do RDMSR from L2 to exit to L1. While 'evmcs_test' > correctly clobbers all GPRs (which are not preserved), 'hyperv_svm_test' > does not. Introduce and use common rdmsr_from_l2() to avoid code > duplication and remove hardcoding of MSRs. > > Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> > --- > .../selftests/kvm/include/x86_64/processor.h | 9 +++++++ > .../testing/selftests/kvm/x86_64/evmcs_test.c | 24 ++++--------------- > .../selftests/kvm/x86_64/hyperv_svm_test.c | 8 +++---- > 3 files changed, 17 insertions(+), 24 deletions(-) > > diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h > index fbaf0b6cec4b..a14b7e4ea7c4 100644 > --- a/tools/testing/selftests/kvm/include/x86_64/processor.h > +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h > @@ -520,6 +520,15 @@ static inline void cpu_relax(void) > "hlt\n" \ > ) > > +/* Exit to L1 from L2 with RDMSR instruction */ > +static inline void rdmsr_from_l2(uint32_t msr) I would prefer keeping this helper out of common x86-64 code, even if it means duplicating code across multiple Hyper-V tests until the L1 VM-Enter/VM-Exit sequences get cleaned up. The name is misleading, e.g. it doesn't really read the MSR since there are no outputs, and while we could obviously fix that with a rename or a generic DO_VMEXIT_FROM_L2() macro, I would rather fix the underlying problem of the world switches clobbering L2 state. That way all the helpers that exist for L1 can be used verbatim for L2 instead of needing dedicated helpers for every instruction that is used to trigger a VM-Exit.
Sean Christopherson <seanjc@google.com> writes: > On Tue, Nov 01, 2022, Vitaly Kuznetsov wrote: >> Hyper-V MSR-Bitmap tests do RDMSR from L2 to exit to L1. While 'evmcs_test' >> correctly clobbers all GPRs (which are not preserved), 'hyperv_svm_test' >> does not. Introduce and use common rdmsr_from_l2() to avoid code >> duplication and remove hardcoding of MSRs. >> >> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> >> --- >> .../selftests/kvm/include/x86_64/processor.h | 9 +++++++ >> .../testing/selftests/kvm/x86_64/evmcs_test.c | 24 ++++--------------- >> .../selftests/kvm/x86_64/hyperv_svm_test.c | 8 +++---- >> 3 files changed, 17 insertions(+), 24 deletions(-) >> >> diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h >> index fbaf0b6cec4b..a14b7e4ea7c4 100644 >> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h >> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h >> @@ -520,6 +520,15 @@ static inline void cpu_relax(void) >> "hlt\n" \ >> ) >> >> +/* Exit to L1 from L2 with RDMSR instruction */ >> +static inline void rdmsr_from_l2(uint32_t msr) > > I would prefer keeping this helper out of common x86-64 code, even if it means > duplicating code across multiple Hyper-V tests until the L1 VM-Enter/VM-Exit > sequences get cleaned up. The name is misleading, e.g. it doesn't really read > the MSR since there are no outputs It's somewhat similar to vmcall()/vmmcall() which are only used to exit from L2 to L1 (and thus nobody complained that all the register values are random) and not issue a hypercall and return some value. > and while we could obviously fix that with a > rename or a generic DO_VMEXIT_FROM_L2() macro, I would rather fix the underlying > problem of the world switches clobbering L2 state. That way all the helpers that > exist for L1 can be used verbatim for L2 instead of needing dedicated helpers for > every instruction that is used to trigger a VM-Exit. > I'm fine with keeping two copies of this in Hyper-V tests for now, of course.
On Tue, Nov 01, 2022, Vitaly Kuznetsov wrote: > Sean Christopherson <seanjc@google.com> writes: > > > On Tue, Nov 01, 2022, Vitaly Kuznetsov wrote: > >> Hyper-V MSR-Bitmap tests do RDMSR from L2 to exit to L1. While 'evmcs_test' > >> correctly clobbers all GPRs (which are not preserved), 'hyperv_svm_test' > >> does not. Introduce and use common rdmsr_from_l2() to avoid code > >> duplication and remove hardcoding of MSRs. > >> > >> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> > >> --- > >> .../selftests/kvm/include/x86_64/processor.h | 9 +++++++ > >> .../testing/selftests/kvm/x86_64/evmcs_test.c | 24 ++++--------------- > >> .../selftests/kvm/x86_64/hyperv_svm_test.c | 8 +++---- > >> 3 files changed, 17 insertions(+), 24 deletions(-) > >> > >> diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h > >> index fbaf0b6cec4b..a14b7e4ea7c4 100644 > >> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h > >> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h > >> @@ -520,6 +520,15 @@ static inline void cpu_relax(void) > >> "hlt\n" \ > >> ) > >> > >> +/* Exit to L1 from L2 with RDMSR instruction */ > >> +static inline void rdmsr_from_l2(uint32_t msr) > > > > I would prefer keeping this helper out of common x86-64 code, even if it means > > duplicating code across multiple Hyper-V tests until the L1 VM-Enter/VM-Exit > > sequences get cleaned up. The name is misleading, e.g. it doesn't really read > > the MSR since there are no outputs > > It's somewhat similar to vmcall()/vmmcall() which are only used to exit > from L2 to L1 (and thus nobody complained that all the register values > are random) and not issue a hypercall and return some value. Sort of. VMCALL/VMMCALL are unique in that they have no meaning (ignoring VMX's STM) other than what is given to them by the hypervisor/software on VM-Exit. RDMSR on the other hand (and literally every other instruction), has architecturally defined behavior and thus expectations beyond generating a VM-Exit. I do think we should clean up the VMCALL/VMMCALL code to remove the clobbers if/when the VM-Enter/VM-Exit sequences are fixed, and maybe make them more generic, e.g. to allow reusing helpers for L1 and L2. But, because the meaning of VMCALL/VMMCALL is software-defined, we'll always need a selftests specific L2=>L1 hypercall, e.g. to ensure L0 forwards the hypercall to L1.
diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index fbaf0b6cec4b..a14b7e4ea7c4 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -520,6 +520,15 @@ static inline void cpu_relax(void) "hlt\n" \ ) +/* Exit to L1 from L2 with RDMSR instruction */ +static inline void rdmsr_from_l2(uint32_t msr) +{ + /* Currently, L1 doesn't preserve GPRs during vmexits. */ + __asm__ __volatile__ ("rdmsr" : : "c"(msr) : + "rax", "rbx", "rdx", "rsi", "rdi", "r8", "r9", + "r10", "r11", "r12", "r13", "r14", "r15"); +} + bool is_intel_cpu(void); bool is_amd_cpu(void); diff --git a/tools/testing/selftests/kvm/x86_64/evmcs_test.c b/tools/testing/selftests/kvm/x86_64/evmcs_test.c index 74f076ba574b..a9f511c192c2 100644 --- a/tools/testing/selftests/kvm/x86_64/evmcs_test.c +++ b/tools/testing/selftests/kvm/x86_64/evmcs_test.c @@ -30,22 +30,6 @@ static void guest_nmi_handler(struct ex_regs *regs) { } -/* Exits to L1 destroy GRPs! */ -static inline void rdmsr_fs_base(void) -{ - __asm__ __volatile__ ("mov $0xc0000100, %%rcx; rdmsr" : : : - "rax", "rbx", "rcx", "rdx", - "rsi", "rdi", "r8", "r9", "r10", "r11", "r12", - "r13", "r14", "r15"); -} -static inline void rdmsr_gs_base(void) -{ - __asm__ __volatile__ ("mov $0xc0000101, %%rcx; rdmsr" : : : - "rax", "rbx", "rcx", "rdx", - "rsi", "rdi", "r8", "r9", "r10", "r11", "r12", - "r13", "r14", "r15"); -} - void l2_guest_code(void) { GUEST_SYNC(7); @@ -58,11 +42,11 @@ void l2_guest_code(void) vmcall(); /* MSR-Bitmap tests */ - rdmsr_fs_base(); /* intercepted */ - rdmsr_fs_base(); /* intercepted */ - rdmsr_gs_base(); /* not intercepted */ + rdmsr_from_l2(MSR_FS_BASE); /* intercepted */ + rdmsr_from_l2(MSR_FS_BASE); /* intercepted */ + rdmsr_from_l2(MSR_GS_BASE); /* not intercepted */ vmcall(); - rdmsr_gs_base(); /* intercepted */ + rdmsr_from_l2(MSR_GS_BASE); /* intercepted */ /* Done, exit to L1 and never come back. */ vmcall(); 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 1c3fc38b4f15..e30419766c8a 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c @@ -30,11 +30,11 @@ void l2_guest_code(void) vmmcall(); /* MSR-Bitmap tests */ - rdmsr(MSR_FS_BASE); /* intercepted */ - rdmsr(MSR_FS_BASE); /* intercepted */ - rdmsr(MSR_GS_BASE); /* not intercepted */ + rdmsr_from_l2(MSR_FS_BASE); /* intercepted */ + rdmsr_from_l2(MSR_FS_BASE); /* intercepted */ + rdmsr_from_l2(MSR_GS_BASE); /* not intercepted */ vmmcall(); - rdmsr(MSR_GS_BASE); /* intercepted */ + rdmsr_from_l2(MSR_GS_BASE); /* intercepted */ GUEST_SYNC(5);