Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp2504587rwi; Tue, 1 Nov 2022 08:24:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6D0t82fKXYIC+Qdf98+u/eky1S6PYUeBnxmLMOKzA2KCpeqQyogaylBvWqvSN1KXvr/NAf X-Received: by 2002:a17:906:8a7b:b0:7ac:baef:6de1 with SMTP id hy27-20020a1709068a7b00b007acbaef6de1mr18885021ejc.734.1667316278228; Tue, 01 Nov 2022 08:24:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667316278; cv=none; d=google.com; s=arc-20160816; b=D4iZ04PzNJc3zNEfUgvF4aBOC3t0bg8xHFcAGRg3NLWFtLUpbCaIUDLvYtbRKWS/wA sYWLmXwvOSqSAwN90loUTSFiKixo+pIrPrjUYLHVMOpHBZkCFrbumHI9V6fYdVZ1uA0A x41n6lI1Spu1GsOfGoDLHmhAEJNxm84DXHkebzmI2RmUo/bo4kVi1dmn0hJ0GG6RxxeB nWWIkqVzQSKzr7Pspyb/DoATJTFK9L+l+yUAokxFJJP/1AczKEM3eaRbIyzbTBGDhgKx lCT5vae6Dx0wPOWtsCzJKKLqSR4sHsfavggR0kAjNdL1sVc8/B2wKOvqzl7CdiCpDjhD KzOQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb38-20020a170907162600b0078e319dcca7si13031550ejc.744.2022.11.01.08.23.55; Tue, 01 Nov 2022 08:24:38 -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 + 96 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 ); 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 ; 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 To: kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson Cc: Wanpeng Li , Jim Mattson , Michael Kelley , Siddharth Chandrasekaran , Yuan Yao , Maxim Levitsky , 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: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- .../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) +{ + /* 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); -- 2.37.3