From patchwork Wed Jan 11 00:44:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vishal Annapurve X-Patchwork-Id: 641518 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 779A8C677F1 for ; Wed, 11 Jan 2023 00:45:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235705AbjAKApa (ORCPT ); Tue, 10 Jan 2023 19:45:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235762AbjAKApJ (ORCPT ); Tue, 10 Jan 2023 19:45:09 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 635CC5950D for ; Tue, 10 Jan 2023 16:45:07 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id b14-20020a170903228e00b00192a8ae9df5so9351781plh.7 for ; Tue, 10 Jan 2023 16:45:07 -0800 (PST) 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:from:to:cc:subject:date:message-id:reply-to; bh=Bel+XT/zLWd0POR6+G5xVjH82mkB2MdJkQdaPFECHUM=; b=DvrbbMDu518Tg86DXo5WokkRe1qtBzk0MnChbCJfqJatSYcshxXye1SEMGgzijl3Fq xWsu6JD7GhNl62+bPlf1fQ8yIcVSqe1FQsRCy+svbOW8SoeRUY3cOqKtLN9H/YRwdqq1 dh4xIsXZLAIpP7LXjVHN1ckBuLFM1Kd/ZeqammL5Q22nisCaKFFqxG8vTAqsJm8URjWb 27J6S3Mh3V0bweXTDzeeqoEWVn190lXVw7s/ojhnCbm4q3brG/4fxKdt+J7eMOt0oX+h C7GyXou/91q7TnDy4CLPEh43/KcY80DBKuyTd8bC/eRTAmDDlB+rWDayAWJVjRdwV0Fs 71Cw== 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:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Bel+XT/zLWd0POR6+G5xVjH82mkB2MdJkQdaPFECHUM=; b=3ZgCF7v1GKPoeWavo5uYA5Ayc6sPtFTmmxU59IoyPW2uQZE0l+yrFX0pPzwCTRF677 Hza4qS1h5a3sEDV6WOPRTqEwXAg5JehB6Qb7Q9saxLg2zRlOLtQY83BF3gFNe54rbJ+V 3hrEVi7k6VnDynJXJqRuf8DYDfEciwdrD92ExUBEUAfEPztY75HBAM9v7HTo4o9tcbW/ 5qiKLyCMeLnU6NB0M/RMLoYXK+kZY/BjoZt5DpSuG41QlWyG0j0t449F2HRCsB9kC0YW v3QEnB/s892ZkkVst0wZXqtf8sV+k6fGcnQvTLYKvY/aCN1HOOfb0kn+6lSbPOle4T1E I9XQ== X-Gm-Message-State: AFqh2kqIDp9T7vZfZPIEATtqTwBojjDxztO/MOoYKz48kGv5Sinmpy9U PxhwPUW7MdhvQZMi2omF/NKIo869wQXCP9ha X-Google-Smtp-Source: AMrXdXsn7oeGwtHtqG8esaQQoRugEEveNLalxNf4U6nq2TNTaCoGIKO3/48VWuBkXfFjrxxb/MSMDIQ7Ep4842g3 X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:41f8]) (user=vannapurve job=sendgmr) by 2002:a17:90b:1494:b0:225:eaa2:3f5d with SMTP id js20-20020a17090b149400b00225eaa23f5dmr25140pjb.2.1673397906623; Tue, 10 Jan 2023 16:45:06 -0800 (PST) Date: Wed, 11 Jan 2023 00:44:44 +0000 In-Reply-To: <20230111004445.416840-1-vannapurve@google.com> Mime-Version: 1.0 References: <20230111004445.416840-1-vannapurve@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230111004445.416840-3-vannapurve@google.com> Subject: [V5 PATCH 2/3] KVM: selftests: x86: Cache host CPU vendor (AMD vs. Intel) From: Vishal Annapurve To: x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, shuah@kernel.org, bgardon@google.com, seanjc@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, andrew.jones@linux.dev, Vishal Annapurve Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Cache the host CPU vendor for userspace and share it with guest code. All the current callers of this_cpu* actually care about host cpu so they are updated to check host_cpu_is*. Suggested-by: Sean Christopherson Reviewed-by: David Matlack Signed-off-by: Vishal Annapurve --- .../selftests/kvm/include/x86_64/processor.h | 3 +++ tools/testing/selftests/kvm/lib/x86_64/processor.c | 14 ++++++++++++-- .../selftests/kvm/x86_64/fix_hypercall_test.c | 4 ++-- .../selftests/kvm/x86_64/mmio_warning_test.c | 2 +- .../selftests/kvm/x86_64/pmu_event_filter_test.c | 4 ++-- .../vmx_exception_with_invalid_guest_state.c | 2 +- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index fdb1af5ca611..c7885f72132a 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -19,6 +19,9 @@ #include "../kvm_util.h" +extern bool host_cpu_is_intel; +extern bool host_cpu_is_amd; + #define NMI_VECTOR 0x02 #define X86_EFLAGS_FIXED (1u << 1) diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 7d1768543b91..84915bc7d689 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -19,6 +19,8 @@ #define MAX_NR_CPUID_ENTRIES 100 vm_vaddr_t exception_handlers; +bool host_cpu_is_amd; +bool host_cpu_is_intel; static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent) { @@ -113,7 +115,7 @@ static void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent) bool kvm_is_tdp_enabled(void) { - if (this_cpu_is_intel()) + if (host_cpu_is_intel) return get_kvm_intel_param_bool("ept"); else return get_kvm_amd_param_bool("npt"); @@ -555,6 +557,8 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu) void kvm_arch_vm_post_create(struct kvm_vm *vm) { vm_create_irqchip(vm); + sync_global_to_guest(vm, host_cpu_is_intel); + sync_global_to_guest(vm, host_cpu_is_amd); } struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, @@ -1214,7 +1218,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm) max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1; /* Avoid reserved HyperTransport region on AMD processors. */ - if (!this_cpu_is_amd()) + if (!host_cpu_is_amd) return max_gfn; /* On parts with <40 physical address bits, the area is fully hidden */ @@ -1254,3 +1258,9 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm) return get_kvm_intel_param_bool("unrestricted_guest"); } + +void kvm_selftest_arch_init(void) +{ + host_cpu_is_intel = this_cpu_is_intel(); + host_cpu_is_amd = this_cpu_is_amd(); +} diff --git a/tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c b/tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c index 5489c9836ec8..0f728f05ea82 100644 --- a/tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +++ b/tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c @@ -48,10 +48,10 @@ static void guest_main(void) const uint8_t *other_hypercall_insn; uint64_t ret; - if (this_cpu_is_intel()) { + if (host_cpu_is_intel) { native_hypercall_insn = vmx_vmcall; other_hypercall_insn = svm_vmmcall; - } else if (this_cpu_is_amd()) { + } else if (host_cpu_is_amd) { native_hypercall_insn = svm_vmmcall; other_hypercall_insn = vmx_vmcall; } else { diff --git a/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c b/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c index b0a2a0bae0f3..ce1ccc4c1503 100644 --- a/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c +++ b/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c @@ -93,7 +93,7 @@ int main(void) { int warnings_before, warnings_after; - TEST_REQUIRE(this_cpu_is_intel()); + TEST_REQUIRE(host_cpu_is_intel); TEST_REQUIRE(!vm_is_unrestricted_guest(NULL)); diff --git a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c index c728822461b2..4dbb454e1760 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c @@ -363,7 +363,7 @@ static void test_pmu_config_disable(void (*guest_code)(void)) */ static bool use_intel_pmu(void) { - return this_cpu_is_intel() && + return host_cpu_is_intel && kvm_cpu_property(X86_PROPERTY_PMU_VERSION) && kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS) && kvm_pmu_has(X86_PMU_FEATURE_BRANCH_INSNS_RETIRED); @@ -397,7 +397,7 @@ static bool use_amd_pmu(void) uint32_t family = kvm_cpu_family(); uint32_t model = kvm_cpu_model(); - return this_cpu_is_amd() && + return host_cpu_is_amd && (is_zen1(family, model) || is_zen2(family, model) || is_zen3(family, model)); diff --git a/tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c b/tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c index 53e1ef2fc774..ccdfa5dc1a4d 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c @@ -111,7 +111,7 @@ int main(int argc, char *argv[]) struct kvm_vcpu *vcpu; struct kvm_vm *vm; - TEST_REQUIRE(this_cpu_is_intel()); + TEST_REQUIRE(host_cpu_is_intel); TEST_REQUIRE(!vm_is_unrestricted_guest(NULL)); vm = vm_create_with_one_vcpu(&vcpu, guest_code); From patchwork Wed Jan 11 00:44:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vishal Annapurve X-Patchwork-Id: 641517 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 1A716C61DB3 for ; Wed, 11 Jan 2023 00:45:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235716AbjAKApc (ORCPT ); Tue, 10 Jan 2023 19:45:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235780AbjAKApL (ORCPT ); Tue, 10 Jan 2023 19:45:11 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF9DD59D22 for ; Tue, 10 Jan 2023 16:45:10 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id 31-20020a17090a0fa200b00226e43409c2so5537225pjz.9 for ; Tue, 10 Jan 2023 16:45:10 -0800 (PST) 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:from:to:cc:subject:date:message-id:reply-to; bh=PX8UiuFYQddlQ3Zi3Fq3jepSo0WH3i4y4Cc6Sow5OTs=; b=Oqamwob+bracVV31tO4Byc/4xcU17K6kTSgsURRNdFy6S3AkpeeNTJ/DG7GSP9MVUQ ssb6MDSj0icWFBiBMYdqzMtXssoHAqQWs3RwAqV5Rtv952qBfCJinSf6dqGj1nfdZcNV FULgkBjC44B0SMZGytGkEFkfG12F+fYfy1lxhAX8v5kM9XuBlysTo5DoVHN4ldla731c VMolQtxzmmx73HT8uVtey4rFUpInifgHjU9xceIx/xs1oTb1BlyqUa+MwgNe1pwDv1H8 kk6hcG8dgNaAuNjA/P5GftP4DIwHwgTsVV2t/gsrD9LR4YAUvsXHUa0LaJlaH6eIHdAn gxgA== 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:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PX8UiuFYQddlQ3Zi3Fq3jepSo0WH3i4y4Cc6Sow5OTs=; b=N0htuAU1DGIIEcQYwUlTh4HFIhTPMl2H1OhAm7/MkFxfu0LjU8tbW66eeNvFXmF1u5 PHkz0QfcjxWCg1nmKPU6z/WGPIvTrzTR70r9vr43/PQblZMGZTI9lEIzB9M4PVmFTrjO RCsBdMsPdsMwZ/0pmHLtxNqcL8Lhr0nBnHBK4/7qkeiiOkoizXIWF+ulViJiCr88dS6l wEe01+NlXGX8JjLSxA9fatKcHzBFYoODBdrrMzMeK+eJ8f2mfsPh8toR7Sp8gih3tOJP ijPOBQGyo7+DqrOILGlfpaPl+18ujySKLaWJPpHyEwdRb+fBAildQOXHphA8uzTqPVDm 5C6g== X-Gm-Message-State: AFqh2kpqTKKLDbeSCN3Na6CBUPWMGbErebwKv3zhTvx1ppsXMAtPaiVT sXIP3svH1mqF3+gF3FqQnRUeMaTiWuNpHFn3 X-Google-Smtp-Source: AMrXdXuUm1Tc46Pwdhv3IFsz2ym7S+82krRj3N/z3c4mUDRf+qRR2j8Pm7YIgx+dwLGQxStbWRwdu34aZVC7s8j1 X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:41f8]) (user=vannapurve job=sendgmr) by 2002:a17:90a:aa12:b0:226:a257:8dca with SMTP id k18-20020a17090aaa1200b00226a2578dcamr2374775pjq.55.1673397910474; Tue, 10 Jan 2023 16:45:10 -0800 (PST) Date: Wed, 11 Jan 2023 00:44:45 +0000 In-Reply-To: <20230111004445.416840-1-vannapurve@google.com> Mime-Version: 1.0 References: <20230111004445.416840-1-vannapurve@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230111004445.416840-4-vannapurve@google.com> Subject: [V5 PATCH 3/3] KVM: selftests: x86: Use host's native hypercall instruction in kvm_hypercall() From: Vishal Annapurve To: x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, shuah@kernel.org, bgardon@google.com, seanjc@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, andrew.jones@linux.dev, Vishal Annapurve Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Use the host CPU's native hypercall instruction, i.e. VMCALL vs. VMMCALL, in kvm_hypercall(), as relying on KVM to patch in the native hypercall on a #UD for the "wrong" hypercall requires KVM_X86_QUIRK_FIX_HYPERCALL_INSN to be enabled and flat out doesn't work if guest memory is encrypted with a private key, e.g. for SEV VMs. Suggested-by: Sean Christopherson Reviewed-by: David Matlack Signed-off-by: Vishal Annapurve --- tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 84915bc7d689..ae1e573d94ce 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1144,9 +1144,15 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, { uint64_t r; - asm volatile("vmcall" + asm volatile("test %[use_vmmcall], %[use_vmmcall]\n\t" + "jnz 1f\n\t" + "vmcall\n\t" + "jmp 2f\n\t" + "1: vmmcall\n\t" + "2:" : "=a"(r) - : "a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3)); + : "a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3), + [use_vmmcall] "r" (host_cpu_is_amd)); return r; }