From patchwork Wed Jan 11 00:44:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vishal Annapurve X-Patchwork-Id: 41708 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3049856wrt; Tue, 10 Jan 2023 16:49:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXtQMG0d77RJuKRBRLbtuhzy0nWkQA0Bu+kW0tzBa9+v05HTotFZWOCmDILjviQXFQzK+lRi X-Received: by 2002:a05:6a20:2d9a:b0:ae:6619:6e5d with SMTP id bf26-20020a056a202d9a00b000ae66196e5dmr71944215pzb.47.1673398146807; Tue, 10 Jan 2023 16:49:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673398146; cv=none; d=google.com; s=arc-20160816; b=0ERMme6nED/ggjtMT030P7x8Z1M6iEvFT2IUQTpv/GXCNCHc5hXZoLPrHxH/omtOUv SLicKlb4EL+dC91dOq67LPHDcBCp7yDpWqhhNqpncK5VhubGk17aMUphDhkIwy1X8Bb0 JyJaod7QR25snZ5kKoiGpjyJpzdcYCWTua+5d2sNN5TwOEsT42kNx2J4DdL0gpLLCC4c waj1GTax7d1NY3wv4df1VT7zeZv8+wvAhpLekXr0L3CsxsO7vNnM5pTuzYq4JjBcLawG +zmF91nZ+LkHeSoI7uQPIxLyM4ltS0ahblhn/M5XkweW40FyLG1PwVPgFFHgb6lmydRo 9PlA== 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:dkim-signature; bh=i5nmfF04B2IHgLFB8aCXKpeIh21CW5hhnA3Zs7nsESA=; b=dhb/76t1VLmSE8XfavOxf0VMUD5OCGvZJbS/TabCotSjR3NMyVimar6nDm1Ey9jqiy AdHvpNyl+L9491T5WnB9xPcz+bJ0WFLk4S8le4+qb8WxM/N3FAkg18Oq7OlpfytZPkBx bfJRvdCzVeoc03vR81HBQZpZtpzCaYdKne4hINsiNfnDbsYHEj3Zhov2vpHv6BlQtpT3 POThgoCyp5SRgmZoK+RoA1378Hvb3fJNBH1iwLTyPZmreksbrPjvXx96In4ux5VH+2GK dlGOrhctExumGKjkpvr5R3yrbz/2E4mCY3jaHiDjHUSIRwAuJvJCnFyy7VNdNXOzfIz1 b5AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="eO2t/+pj"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a625507000000b0057e257610aesi12998659pfb.144.2023.01.10.16.48.54; Tue, 10 Jan 2023 16:49:06 -0800 (PST) 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="eO2t/+pj"; 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 S235740AbjAKApb (ORCPT + 99 others); Tue, 10 Jan 2023 19:45:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235743AbjAKApG (ORCPT ); Tue, 10 Jan 2023 19:45:06 -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 DB1D05931E for ; Tue, 10 Jan 2023 16:45:04 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id z3-20020a170903018300b0018fb8ca1688so9384891plg.5 for ; Tue, 10 Jan 2023 16:45:04 -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=i5nmfF04B2IHgLFB8aCXKpeIh21CW5hhnA3Zs7nsESA=; b=eO2t/+pj8mPlLMMdcEpbBNko4TJswSnywcF/DfhdxgABLVsoaSv5PwBNwl3Qma9Q/J eXScNp2W/dA8aEVblqFCjNCBjYCy9lZ5DfDFpoVJQKW8nt15tQ3gpISUZq51HTO6+LPn Ht8MK+yMvyLmKBsjXWJ9uxI1h77qVAgk6m/k3gQJCqiCjR1XHVpLSTLdyyEjfFJ4EOGp x29Ky/AliXXIegn4/Dy/8vOdvdCxB4TSCf6e71+HcES1hoOOQDGKH7WBjui1oH2Wk3ZQ Xfcy/crmDd6kyKLEaGlEjLbmhm6FKCgH/DBXy7teV2ghTi/tFYfy00Vm75JHekaMbbYa Mt7Q== 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=i5nmfF04B2IHgLFB8aCXKpeIh21CW5hhnA3Zs7nsESA=; b=I2kVGmePBhVw174ZhxieL4CiCS6FqU4qC+bbcwgsS9ViSLmkA7UxJFmJSltMswsLLf PVgBJCdsH4sp687B29VttA4SZ4TEmMrpfSqzzbx65Lg8Ts3qrB2uFb7hJCTZiqdVdwzk RJcA7OKrDz8eRUBDKAdusklmMlJ+V6QQEPJP9LGAP7SMKXsPn9jjfNQeZigTk9Yea7OR C0YC+nt4FGpUUS6ZEzQFd4gRLuRcuWCiOPn5Q4m12fWDpGlSHZlMSWKiBiOzijhEsH17 1w2v1NmyI3bZqkfyphWJCKpvziq5iwsuBwb41mcirBwcZ82QU226CDmZdEeVEjGY5bI7 a12Q== X-Gm-Message-State: AFqh2koho4jamjs2SVVCOOlSw4Jb6g+u0kdNesdSIsVDuxrVeZyd/1db Rjhd8zd3I6irGb+reTNcLAoSTsja0Q/4hbRt X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:41f8]) (user=vannapurve job=sendgmr) by 2002:aa7:81d4:0:b0:577:5678:bc80 with SMTP id c20-20020aa781d4000000b005775678bc80mr5338691pfn.62.1673397904191; Tue, 10 Jan 2023 16:45:04 -0800 (PST) Date: Wed, 11 Jan 2023 00:44:43 +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-2-vannapurve@google.com> Subject: [V5 PATCH 1/3] KVM: selftests: x86: Use "this_cpu" prefix for cpu vendor queries 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754685135068626762?= X-GMAIL-MSGID: =?utf-8?q?1754685135068626762?= Replace is_intel/amd_cpu helpers with this_cpu_* helpers to better convey the intent of querying vendor of the current cpu. Suggested-by: Sean Christopherson Reviewed-by: David Matlack Signed-off-by: Vishal Annapurve --- .../selftests/kvm/include/x86_64/processor.h | 25 +++++++++++++++--- .../selftests/kvm/lib/x86_64/processor.c | 26 ++----------------- .../selftests/kvm/x86_64/fix_hypercall_test.c | 4 +-- .../selftests/kvm/x86_64/mmio_warning_test.c | 2 +- .../kvm/x86_64/pmu_event_filter_test.c | 4 +-- .../vmx_exception_with_invalid_guest_state.c | 2 +- 6 files changed, 30 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 2a5f47d51388..fdb1af5ca611 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -555,6 +555,28 @@ static inline uint32_t this_cpu_model(void) return x86_model(this_cpu_fms()); } +static inline bool this_cpu_vendor_string_is(const char *vendor) +{ + const uint32_t *chunk = (const uint32_t *)vendor; + uint32_t eax, ebx, ecx, edx; + + cpuid(0, &eax, &ebx, &ecx, &edx); + return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]); +} + +static inline bool this_cpu_is_intel(void) +{ + return this_cpu_vendor_string_is("GenuineIntel"); +} + +/* + * Exclude early K5 samples with a vendor string of "AMDisbetter!" + */ +static inline bool this_cpu_is_amd(void) +{ + return this_cpu_vendor_string_is("AuthenticAMD"); +} + static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index, uint8_t reg, uint8_t lo, uint8_t hi) { @@ -691,9 +713,6 @@ static inline void cpu_relax(void) "hlt\n" \ ) -bool is_intel_cpu(void); -bool is_amd_cpu(void); - struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu); void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state); void kvm_x86_state_cleanup(struct kvm_x86_state *state); diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index acfa1d01e7df..7d1768543b91 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -113,7 +113,7 @@ static void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent) bool kvm_is_tdp_enabled(void) { - if (is_intel_cpu()) + if (this_cpu_is_intel()) return get_kvm_intel_param_bool("ept"); else return get_kvm_amd_param_bool("npt"); @@ -1006,28 +1006,6 @@ void kvm_x86_state_cleanup(struct kvm_x86_state *state) free(state); } -static bool cpu_vendor_string_is(const char *vendor) -{ - const uint32_t *chunk = (const uint32_t *)vendor; - uint32_t eax, ebx, ecx, edx; - - cpuid(0, &eax, &ebx, &ecx, &edx); - return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]); -} - -bool is_intel_cpu(void) -{ - return cpu_vendor_string_is("GenuineIntel"); -} - -/* - * Exclude early K5 samples with a vendor string of "AMDisbetter!" - */ -bool is_amd_cpu(void) -{ - return cpu_vendor_string_is("AuthenticAMD"); -} - void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) { if (!kvm_cpu_has_p(X86_PROPERTY_MAX_PHY_ADDR)) { @@ -1236,7 +1214,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 (!is_amd_cpu()) + if (!this_cpu_is_amd()) return max_gfn; /* On parts with <40 physical address bits, the area is fully hidden */ 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 32f7e09ef67c..5489c9836ec8 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 (is_intel_cpu()) { + if (this_cpu_is_intel()) { native_hypercall_insn = vmx_vmcall; other_hypercall_insn = svm_vmmcall; - } else if (is_amd_cpu()) { + } else if (this_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 fb02581953a3..b0a2a0bae0f3 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(is_intel_cpu()); + TEST_REQUIRE(this_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 2de98fce7edd..c728822461b2 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 is_intel_cpu() && + return this_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 is_amd_cpu() && + return this_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 2641b286b4ed..53e1ef2fc774 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(is_intel_cpu()); + TEST_REQUIRE(this_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: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: 41709 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3049931wrt; Tue, 10 Jan 2023 16:49:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXuqBY6sLYttMI27SK0WiEczupV6GojrEDqw0tzg0KnkZjjC9+EbBaE07NuwDemMeq191ei5 X-Received: by 2002:a17:902:e212:b0:193:2303:c9d7 with SMTP id u18-20020a170902e21200b001932303c9d7mr706943plb.5.1673398166247; Tue, 10 Jan 2023 16:49:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673398166; cv=none; d=google.com; s=arc-20160816; b=utP+zbXPWLnos2cbWv9G5j8QJKaKNW8aXY9kXlQ5X/DnmVAYQWkISaTC9dZ3v74BfV jfyQRte+4FOFsvxjEiKFr/JklqxAFeqpgy3QsoSi7+laoA8/BYUhit+WfzFep4JqJYE3 Q7L1Bpl0C5xpMAxHErjSABWe81eKfA5Xj4K3t/1EIZwFOQAPL5aGq4ARhR+dAzPOKTYw uX6alfCCl2rCmjSx6QlnDD+nAFC/Er34mOU+evsTRhupVG1Iw8UbgjOA37z2uC5Yet+E c2XKRlKgftTI0QhvFlGnWhlM5tX8xQrqMgnPoht1qZ/UddZd/UmmUq+7g04cuw+mfEDG 1fmA== 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:dkim-signature; bh=Bel+XT/zLWd0POR6+G5xVjH82mkB2MdJkQdaPFECHUM=; b=aP6wJ/AcXTLSlm+W7gRJw8JfqZE+wU0hoZhyJbU9AgFa1xOMjKlmfRSvytIGGqTl4t QdvSNdpNllEIXxITixE9XnxqIiTZtxeCSTuIst/gMT/sxcEaa1h8/fBQVNyS3j0wH9nC H61ZkeFtu8X0SoyQn9iaxkn/OiBRXOldRif6XvYdnqqmInQbMQYYpswj1OJHmgLjPd7C rj4pb+Vt8LC0SgUgrxOMTdIN01tJ9iIUBi4ycp8GKOMaTXHodV9y4bfPEww6gqdMpHNu DuhDWLblthIA4f2PWKoUrEB37fEGyohuxOIyc3w7bMhwvITdwrhsSPlXdZpRO/0QaT6F c1Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DvrbbMDu; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p18-20020a170902e75200b001893b7f5ed7si13932846plf.205.2023.01.10.16.49.14; Tue, 10 Jan 2023 16:49:26 -0800 (PST) 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=DvrbbMDu; 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 S235792AbjAKAph (ORCPT + 99 others); Tue, 10 Jan 2023 19:45:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235760AbjAKApJ (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 630165933A for ; Tue, 10 Jan 2023 16:45:07 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id s2-20020a170902ea0200b0019247629ee5so9455995plg.17 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=4wdbp7g+yGatEUYQh0OJBXD2k94oxM1dR/2cly4bDwyMM2I+24nsKbF0FBMXShOfq0 KuS/KGHkbdhfka5GIq6+/s5rAZ8G83CO3eWlabaliOtQmua40nU5VXdOTvNSF7Z+YcUf lccXgUILeJsipd62/CHRRCUFLMDHwHK2paCrqgXxBEk4GFV9qCBf5/XAqcZfRF7XrdtS uA2kHOn0yqxot2rFjOMibG3l0pzrGdp1F2Y5AOkLdtBlTK1BGPaYPn8VVm5NHBsmi/+e aUsGUSHEbRx7rSj8yFOiNfwFdyYlPN0DQpKQe3ISgUPhmcYQIGBEyimA3RLGA605Ua74 3z+g== X-Gm-Message-State: AFqh2kqmx0WCmq8NkpROwQSycGSnOaKJ6hhE5EeiUybn+ukQ2vmNpktS 4VL8LPwBuQC1wtANKRl+ZJmMaEsQKk+P0Gia 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754685155630963980?= X-GMAIL-MSGID: =?utf-8?q?1754685155630963980?= 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: 41710 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3049975wrt; Tue, 10 Jan 2023 16:49:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXv5ymxVgFRgUb1eEXnxU9ZJh4cJERMjIlM5BZlk+k8L0f2atlAWXIt7/LSZZYIxLrM7YYAv X-Received: by 2002:a05:6a20:8e27:b0:b5:af35:e324 with SMTP id y39-20020a056a208e2700b000b5af35e324mr14851950pzj.29.1673398175714; Tue, 10 Jan 2023 16:49:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673398175; cv=none; d=google.com; s=arc-20160816; b=exgS37LjWehXjbKUVHUqn4F3KFWDHUQvvPUe5mbNxX9dmQcmFDjuv3YMZjLE5skstW 22wUt5Veqqs0A9b5yWP+1IzwqVMWCExqmcwQ6dvU9Gska5I2ASXny6k7g1rqnnMrMFUh SxAggxHAFaQinsLp3UMAW+WvP3CgEdY/fm/YNZG7Uj9A7Rij0Q8ytRYQOLK54W057clE IknbGA9DD54UTTKZUlRbus7gxMyTcbP6GIK+U3kXRe0IE6i0eYC5xWcd6iZ6DsVYykd0 7m6JPi5/3njIWKUfYaL6ViWMgf8ILFsf+96IaDRUsPmGH+9KNaE1yNkcTlDO/VKncZ9i MktA== 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:dkim-signature; bh=PX8UiuFYQddlQ3Zi3Fq3jepSo0WH3i4y4Cc6Sow5OTs=; b=d44onIbIMc7Qzlaxw3PqCwbNPyUOUPx8yEnTYa0iaYwGMgJ5Q0Bq5sziG8B8E7DhtU sX1YZbMA+5qTncyXPQm1hHpV6durAckPY/R/197reEQrO5eFSC8Y/sYxV2T+L4iJbk97 UTyCl3UC+YCcXQmPamOJLiQS/RgakWHShgJaL2EwGvRMunk1T0QMrtnc/9NVL1hgpdKr pvBsdXbP4m8mOnsYL67+krGtJe4y8Oi2MuRNM25bGwIg/qRNFAIVmKchKUtZABh/xy2z 6s/L9gXwYsS5mbztxrI7GAbduwZ7IJQSCwNcdDxkBfXMV821FrZFVoJyg3nYZiNUszrd 81kQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Oqamwob+; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h188-20020a636cc5000000b004b4e4162ccasi4028084pgc.446.2023.01.10.16.49.23; Tue, 10 Jan 2023 16:49:35 -0800 (PST) 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=Oqamwob+; 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 S234562AbjAKApm (ORCPT + 99 others); Tue, 10 Jan 2023 19:45:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235783AbjAKApL (ORCPT ); Tue, 10 Jan 2023 19:45:11 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E01595A88C for ; Tue, 10 Jan 2023 16:45:10 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id t12-20020a170902b20c00b00192e3d10c5bso9418804plr.4 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=PhYk8tosuJ70z+450pnLBb70oBD7t2ovDlT4yEJll4MKBkoRbnI6Nphz+KM+tqXoGp hf8DWrcQ17+MiDXyVfYgWBvbex6bW4g51AQ2firtMnNTMHGTa3MERBd3WIwIvuChGVdr TFxmzSuXzFkKMWcTTJ9WAlRfEphTtzzrKa8dEIND7KOhTOrpYCBR5fJaLKZ4DZ72pXz7 qqKVzlpqls4FWfsHkIKzL14Zcmm5BF3y722GcX6ozlrxaXJijbKV362vukbKoH0M/dhO iyzx7RF7lnZ+xdgDAmydrPDTKrbI21XnSm0tj9ytOvDh9957a88i54L+E3sImBQ0pkwY iivQ== X-Gm-Message-State: AFqh2kobKDvwgEGEqJNZyaVCxr3b3voFsQZwi1KqcrCVUDF0hzEK1RsR nKGWVh+d7Sx6kAZSByTfFWVrBi0/Hl2a1/gk 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 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=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754685165170964279?= X-GMAIL-MSGID: =?utf-8?q?1754685165170964279?= 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; }