Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp769203pxy; Wed, 28 Apr 2021 13:55:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx757tlQza5r2bgt/pmnM3F0wVtXky42m3doDeu2PFYRMjqCOBfqHRE9Re1utzNqnj+FfJB X-Received: by 2002:a05:6402:1516:: with SMTP id f22mr10026143edw.136.1619643309162; Wed, 28 Apr 2021 13:55:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619643309; cv=none; d=google.com; s=arc-20160816; b=lOGSggLrGuYLhtocqP44Hd0vYgcD/dN40ScIMkrM5/Jyw9/NbxlZK57rMxA7+HlHDI bGbQ3AuL6ePzbbd8wNbNVJxsWinbuy8v5Qvn/Dr2nj7gW+0c00txCTa+y4s0ImE6CabB Uj4DUgxlH5EU4iCoFbY13G4SQJSqbG1SdT4sz7hFVk4sqw8ikLQ4k38xtqX/u+2GiSco k1FiKvhlQnouLl5K1E37+DXUtI0yeenGFni7D/ylzcj96U58suye/bwLvMxLK1geU6cD ZKEV6bxQy+ozcQ+MeSb1qcda0JC+O/7/ZT1537GLpsrKmyu0OMdV8naEznbfJM50zorU GSHQ== 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=kb9fVqaEasPXHJjemr3PZzkN5J5eezfWbFcJnEMHVOk=; b=ijXLrFq9le4NjpXyt2mmQIMLKjDkdcqYzDhvEcC69zzwqr51kafgvOkeyPo93eosqz 3KNTNfGS8SKJGGouSr8H9uhWYMtM/213VpBCR0hfIk8vW2adcxR+7yH0YcReneuIQEIH ytojEvO6KcNOn/jS1Be0VUpu/vKOaESvhhTWX3Wci3rh/5fZk5OhIlEGTVyicdN7nScX ZPKVEy0JdTs4vjZB44ghkqRhcNO+e3ysDG07IQjs2Z5qS29cp+laKR12CN/CboTpXll5 xe7ANsBnotWeHX9ZZIDC0Zt4KqItaZBjMvmjw6TJpGf+MnfuNCacg/E0hgSzOUTM8k/M FyJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qYfUcI8x; 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 i22si855844edq.81.2021.04.28.13.54.43; Wed, 28 Apr 2021 13:55:09 -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=qYfUcI8x; 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 S241907AbhD1TjF (ORCPT + 99 others); Wed, 28 Apr 2021 15:39:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241863AbhD1Ti6 (ORCPT ); Wed, 28 Apr 2021 15:38:58 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E1A8C061344 for ; Wed, 28 Apr 2021 12:38:11 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id f19-20020a056a0022d3b02902608c8a75e0so18309350pfj.13 for ; Wed, 28 Apr 2021 12:38:11 -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=kb9fVqaEasPXHJjemr3PZzkN5J5eezfWbFcJnEMHVOk=; b=qYfUcI8x807JKI9l/U++/Sjw5vGfSNZMigPQb/7mDMyNfDYLHzJFME3Uqw/MuOgZD2 0lfHwNTEgAIOWQgk66R/oDvcXHdumTv9zq418P889zjGuVU4ZGYt/qs5kXL9RAdkQ9JY uTqSql1MXi3dScdoztfMsdDmL3/E3yvQUFQKPE98W62s/O5AJKMstqN2tGzwZdjT7pjm CYfU/fZkXzkZYu0o0hPkEQbKBcnL/oTCpX5TRIsGC13JAxwsXEPbnhKZwaElvbqnNrZ/ vhH1el2m7YkUf5qLVtLclOzld0FgUWh6wkZsF5xH8vqzfmi6ymN2E1pBZJgIIu12Yb3S j7gg== 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=kb9fVqaEasPXHJjemr3PZzkN5J5eezfWbFcJnEMHVOk=; b=onzNq2pezvIzFTBQfPVvEYDrJ8HppYwWVo0zw+HUNKK35kpItxm1ahFuPs4QH2oqtP yKL60q0FlnSo6n2ATbxuOBORrbz3soicfnEJR0ZVOXYSZXUHT/FL7Y3M9Te5dH2KVeCc 2+2j3m28nUEKE9xysQTbmlXKgSwbE0vLcZ1Uk7p/gvfoaiDbBJki2T3rwFluTvikv5gY bG+dyeRv6+0FuO5DHIl0Qlh393cYbGAvWcHeb9IxJMC0eD9RYXeD+ZKnPhR2GN87ZLzB LfCdXFEhW+8D8Uwy2CvOlbdmN793EVhzwPD5BaNaJPWB3/3VcMjcFYgbQWRMIqTAC8vv jObA== X-Gm-Message-State: AOAM533TudEJcP1ddbapr222iRyHxrI6w3XOQHhsWc0EWnp7y0JdMVDC OwF+3s2hjNmaKKpZ54fJcvO85SLJmBx58A== X-Received: from ricarkol2.c.googlers.com ([fda3:e722:ac3:10:24:72f4:c0a8:62fe]) (user=ricarkol job=sendgmr) by 2002:a17:902:74c5:b029:ed:d41:1ca0 with SMTP id f5-20020a17090274c5b02900ed0d411ca0mr22592543plt.66.1619638690836; Wed, 28 Apr 2021 12:38:10 -0700 (PDT) Date: Wed, 28 Apr 2021 12:37:55 -0700 In-Reply-To: <20210428193756.2110517-1-ricarkol@google.com> Message-Id: <20210428193756.2110517-6-ricarkol@google.com> Mime-Version: 1.0 References: <20210428193756.2110517-1-ricarkol@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH v2 5/6] 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.498.g6c1eba8ee3d-goog