From patchwork Wed Jun 29 15:06:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Kuznetsov X-Patchwork-Id: 12900270 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12EE2C43334 for ; Wed, 29 Jun 2022 15:07:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233914AbiF2PHH (ORCPT ); Wed, 29 Jun 2022 11:07:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233858AbiF2PHA (ORCPT ); Wed, 29 Jun 2022 11:07:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E1D512BB2C for ; Wed, 29 Jun 2022 08:06:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656515218; 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=htoeiSOxyUzFpsJTMoKaIIRqirMAGQqe1AGkD2m/3Rw=; b=fgk2sa2Auw8wSuqfFgo00FlfuQ910ZZIpsL7LU20IkQMboSNKFkJk43fyBIkbW36DfjEx+ WFrbimIcfZdmafaGhxXcVlFc7PUboL7O3VOxyr9UBEQQwZCzV0lbjJ+fqQp7QuPpuXVKb9 XflXDLgNC+OutTbAWw459Erv8CXF7sY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-384--zrge85EP5CdV8-eRmse1Q-1; Wed, 29 Jun 2022 11:06:55 -0400 X-MC-Unique: -zrge85EP5CdV8-eRmse1Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 789A28041B8; Wed, 29 Jun 2022 15:06:54 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.40.192.126]) by smtp.corp.redhat.com (Postfix) with ESMTP id 889FF40EC002; Wed, 29 Jun 2022 15:06:52 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson Cc: Anirudh Rayabharam , Wanpeng Li , Jim Mattson , Maxim Levitsky , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 11/28] KVM: selftests: Enable TSC scaling in evmcs selftest Date: Wed, 29 Jun 2022 17:06:08 +0200 Message-Id: <20220629150625.238286-12-vkuznets@redhat.com> In-Reply-To: <20220629150625.238286-1-vkuznets@redhat.com> References: <20220629150625.238286-1-vkuznets@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The updated Enlightened VMCS v1 definition enables TSC scaling, test that SECONDARY_EXEC_TSC_SCALING can now be enabled. Signed-off-by: Vitaly Kuznetsov --- .../testing/selftests/kvm/x86_64/evmcs_test.c | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/evmcs_test.c b/tools/testing/selftests/kvm/x86_64/evmcs_test.c index a546d1cad146..b25e6f57b41c 100644 --- a/tools/testing/selftests/kvm/x86_64/evmcs_test.c +++ b/tools/testing/selftests/kvm/x86_64/evmcs_test.c @@ -18,6 +18,9 @@ #include "vmx.h" +/* Test flags */ +#define HOST_HAS_TSC_SCALING BIT(0) + static int ud_count; static void guest_ud_handler(struct ex_regs *regs) @@ -64,11 +67,14 @@ void l2_guest_code(void) vmcall(); rdmsr_gs_base(); /* intercepted */ + /* TSC scaling */ + vmcall(); + /* Done, exit to L1 and never come back. */ vmcall(); } -void guest_code(struct vmx_pages *vmx_pages) +void guest_code(struct vmx_pages *vmx_pages, u64 test_flags) { #define L2_GUEST_STACK_SIZE 64 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; @@ -150,6 +156,18 @@ void guest_code(struct vmx_pages *vmx_pages) GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL); GUEST_SYNC(11); + if (test_flags & HOST_HAS_TSC_SCALING) { + GUEST_ASSERT((rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2) >> 32) & + SECONDARY_EXEC_TSC_SCALING); + /* Try enabling TSC scaling */ + vmwrite(SECONDARY_VM_EXEC_CONTROL, vmreadz(SECONDARY_VM_EXEC_CONTROL) | + SECONDARY_EXEC_TSC_SCALING); + vmwrite(TSC_MULTIPLIER, 1); + } + GUEST_ASSERT(!vmresume()); + GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL); + GUEST_SYNC(12); + /* Try enlightened vmptrld with an incorrect GPA */ evmcs_vmptrld(0xdeadbeef, vmx_pages->enlightened_vmcs); GUEST_ASSERT(vmlaunch()); @@ -204,6 +222,7 @@ int main(int argc, char *argv[]) struct kvm_vm *vm; struct kvm_run *run; struct ucall uc; + u64 test_flags = 0; int stage; vm = vm_create_with_one_vcpu(&vcpu, guest_code); @@ -212,11 +231,19 @@ int main(int argc, char *argv[]) TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS2)); + if ((kvm_get_feature_msr(MSR_IA32_VMX_PROCBASED_CTLS2) >> 32) & + SECONDARY_EXEC_TSC_SCALING) { + test_flags |= HOST_HAS_TSC_SCALING; + pr_info("TSC scaling is supported, adding to test\n"); + } else { + pr_info("TSC scaling is not supported\n"); + } + vcpu_set_hv_cpuid(vcpu); vcpu_enable_evmcs(vcpu); vcpu_alloc_vmx(vm, &vmx_pages_gva); - vcpu_args_set(vcpu, 1, vmx_pages_gva); + vcpu_args_set(vcpu, 2, vmx_pages_gva, test_flags); vm_init_descriptor_tables(vm); vcpu_init_descriptor_tables(vcpu);