Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp61569pxy; Wed, 21 Apr 2021 18:33:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxlZXrIvASgpWkDlDdtloOeszkc922rzo8zFHSM7fOsyvASnZXG8nmHMDoMGW2OcwzJwx4Y X-Received: by 2002:a63:6805:: with SMTP id d5mr946807pgc.273.1619055185128; Wed, 21 Apr 2021 18:33:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619055185; cv=none; d=google.com; s=arc-20160816; b=vSla1bOHpnudWkHOd9f2YyMgDN33oNoodi7KQNnvabfHA6OmnIvGZMvhWLbbFFWl9S lZT+ps+eMLhcX23QZn6opSeeN74CWTHGz858nexA9wiLK8W0uoh6DbL39t8ymle0OE0k +mSfoNqNjWmCtvD9M+fokIHfmdgNKpq9c3GyYK2KYnBk6JgLGl5KNFE2jsDLdnoW7Q/i 4HIoWGy1e8815emc6laj5HpYk1Kn2QSHoBOfeohDxyVgGgcFcRLKxokpVsb2YLWbcu8E qixJhbQFgoLuYMHUgUaafTu5oSN9SsWXvZFuQdbcvg4Azsg0IOiFlYTdUcNMd0AEdHLI LQsA== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=d4FQCYHivptxQk4kA1SJ6N+YreUjXRXnuN7O1SBVzug=; b=lTiJwv6QEulfbvY51ViHKemxbnNEqx0jR1sRnWUxKh6vecjHxC2fqINAHObJ3T9tRi lgJ2ZVxWywwpFI0NlZM/p+bYEidnmFB2Cen/l8WvLKvU1TvqclZ2PE9dc4uKqXVxbIGW +Z4/51oWvFmfPfNW1F2rUL8nBri/Ni0Pj6t7YvMipbL+Sf+0lrjLG37wUIqkSCqeg44y AOTXE8nvJcnZNCnRI1R7tk4EgUXa4EEGvrB183zcmveA81Wp8F8eawkiW7aSR8kVAXO4 r2HZvuj4ciaWfruL0LlpvStFoKcBtUsY2YvONST2pKOkXW89PRFaFUuAQ3gh/ZcC4EMP O/9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Wsya/lSs"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f11si1315503pgv.577.2021.04.21.18.32.53; Wed, 21 Apr 2021 18:33:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Wsya/lSs"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S244351AbhDVA5j (ORCPT + 99 others); Wed, 21 Apr 2021 20:57:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244336AbhDVA5d (ORCPT ); Wed, 21 Apr 2021 20:57:33 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1552C06138C for ; Wed, 21 Apr 2021 17:56:58 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id n129-20020a2527870000b02904ed02e1aab5so6266701ybn.21 for ; Wed, 21 Apr 2021 17:56:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=d4FQCYHivptxQk4kA1SJ6N+YreUjXRXnuN7O1SBVzug=; b=Wsya/lSsSo7Yxan02JvA7mnccp0K4Zos4f7NT3FGyZtavc9Lz7YzkTnd6tcQO5Xq7I hVhG5nXfQYXITIito2k8elrNnGPq1GbeXs3l5X9YyKSkcVyxbtYvOOiNp+y4PmSBDWQh 9b30cM8ZSuLnN+5roW/z9Gkf6xeYmhPAAVVohmUEIboUgHPAi4kyhTlkItHaX4lfoZRA q8lEOVaQiy+EUWwVNU9y/lPXKGRh8a/NJlacyw6E5pNQZ1I95tH9dHE/ao+toVsNKrYp Aam2Qx38ZXdapQcVUTSxNvTLVRvVixxejj1fx0bOS7YjhiAloSdeclW9JtFGU6ogN04t c6XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=d4FQCYHivptxQk4kA1SJ6N+YreUjXRXnuN7O1SBVzug=; b=IBfXEDrHUg3+sDl7t1GG2mlYZA+bk3T+xdNYPEBIuP4n94AbxNayGMw4SKX5ciq0Hl WeqriafpuS1MJ4V6yIQlS8plj78bzjEJoigw9lZEIwHb667fO2UdE0aTlZc2FlWDmV9l eprgdKFqIvpQZplkrFCYLsBkOu07vu4ZPof1DyNlZz6NhuKMl7FqG3mt5GWs0MkMpjcG ongwKQINkqdti7zx+zIQOSMPkWHAYaQHo02w4nDQgwT+KhWSVEgKjPPmkhrh/o0UsuhO 30yOURfTn77cZYXXK7xU7d9nkfba6FNWWg8J2k0rfqTl291w7Fp9PuRsWDuWBhShD/rT AOOA== X-Gm-Message-State: AOAM532HbWHydCwb8vOMh9/XcSSTijrC7ZLBgwjO6hPT9YnzoDABU0UC 5caJF8+JdjoTX5CLpkiGKr2pdsTuMi7AGg== X-Received: from ricarkol2.c.googlers.com ([fda3:e722:ac3:10:24:72f4:c0a8:62fe]) (user=ricarkol job=sendgmr) by 2002:a25:42cd:: with SMTP id p196mr1082067yba.314.1619053018241; Wed, 21 Apr 2021 17:56:58 -0700 (PDT) Date: Wed, 21 Apr 2021 17:56:25 -0700 In-Reply-To: <20210422005626.564163-1-ricarkol@google.com> Message-Id: <20210422005626.564163-5-ricarkol@google.com> Mime-Version: 1.0 References: <20210422005626.564163-1-ricarkol@google.com> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog Subject: [PATCH 4/5] KVM: selftests: Introduce utilities for checking x86 features From: Ricardo Koller To: Paolo Bonzini Cc: kvm@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , "H. Peter Anvin" , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Nathan Chancellor , Nick Desaulniers , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com, Ricardo Koller Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add utilities for checking CPU features using the same x86 features format used in the kernel (defined in cpufeatures.h). This format embeds the function, index, and register to use. By using this format and these utilities, tests will not have to define their own feature macros and will be able to use kvm_cpuid_has(FEATURE_XYZ) or this_cpu_has(FEATURE_XYZ) without having to worry about what register or index to use. Signed-off-by: Ricardo Koller --- .../selftests/kvm/include/x86_64/cpuid.h | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/cpuid.h diff --git a/tools/testing/selftests/kvm/include/x86_64/cpuid.h b/tools/testing/selftests/kvm/include/x86_64/cpuid.h new file mode 100644 index 000000000000..4d8c67d528f4 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/cpuid.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Adapted from /arch/x86/kvm/cpuid.h + */ + +#ifndef SELFTEST_KVM_CPUID_FEATURE_H +#define SELFTEST_KVM_CPUID_FEATURE_H + +#include +#include +#include +#include "reverse_cpuid.h" + +static __always_inline u32 *kvm_cpuid_get_register(unsigned int x86_feature) +{ + struct kvm_cpuid_entry2 *entry; + const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature); + + entry = kvm_get_supported_cpuid_index(cpuid.function, cpuid.index); + if (!entry) + return NULL; + + return __cpuid_entry_get_reg(entry, cpuid.reg); +} + +static __always_inline bool kvm_cpuid_has(unsigned int x86_feature) +{ + u32 *reg; + + reg = kvm_cpuid_get_register(x86_feature); + if (!reg) + return false; + + return *reg & __feature_bit(x86_feature); +} + +static __always_inline bool kvm_pv_has(unsigned int kvm_feature) +{ + u32 reg; + + reg = kvm_get_supported_cpuid_entry(KVM_CPUID_FEATURES)->eax; + return reg & __feature_bit(kvm_feature); +} + +static __always_inline bool this_cpu_has(unsigned int x86_feature) +{ + struct kvm_cpuid_entry2 entry; + const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature); + u32 *reg; + + entry.eax = cpuid.function; + entry.ecx = cpuid.index; + __asm__ __volatile__("cpuid" + : "+a"(entry.eax), "=b"(entry.ebx), + "+c"(entry.ecx), "=d"(entry.edx)); + + reg = __cpuid_entry_get_reg(&entry, cpuid.reg); + return *reg & __feature_bit(x86_feature); +} + +#endif /* SELFTEST_KVM_CPUID_FEATURE_H */ -- 2.31.1.368.gbe11c130af-goog