mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Ahmed S. Darwish" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: "Ahmed S. Darwish" <darwi@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: x86/cpu] tools/x86/kcpuid: Use <cpuid.h> intrinsics
Date: Tue, 25 Mar 2025 09:05:41 -0000	[thread overview]
Message-ID: <174289354186.14745.673118297329233119.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20250324142042.29010-11-darwi@linutronix.de>

The following commit has been merged into the x86/cpu branch of tip:

Commit-ID:     c479a84488d10b3d4259186d80839f99e26b1706
Gitweb:        https://git.kernel.org/tip/c479a84488d10b3d4259186d80839f99e26b1706
Author:        Ahmed S. Darwish <darwi@linutronix.de>
AuthorDate:    Mon, 24 Mar 2025 15:20:31 +01:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Tue, 25 Mar 2025 09:53:45 +01:00

tools/x86/kcpuid: Use <cpuid.h> intrinsics

Use the __cpuid_count() intrinsic, provided by GCC and LLVM, instead of
rolling a manual version.  Both of the kernel's minimum required GCC
version (5.1) and LLVM version (13.0.1) supports it, and it is heavily
used across standard Linux user-space tooling.

This also makes the CPUID call sites more readable.

Signed-off-by: Ahmed S. Darwish <darwi@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Link: https://lore.kernel.org/r/20250324142042.29010-11-darwi@linutronix.de
---
 tools/arch/x86/kcpuid/kcpuid.c | 37 +++++++++++++--------------------
 1 file changed, 15 insertions(+), 22 deletions(-)

diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index 413e5da..25388a5 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #define _GNU_SOURCE
 
+#include <cpuid.h>
 #include <err.h>
 #include <getopt.h>
 #include <stdbool.h>
@@ -86,16 +87,16 @@ static u32 user_index = 0xFFFFFFFF;
 static u32 user_sub = 0xFFFFFFFF;
 static int flines;
 
-static inline void cpuid(u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
-{
-	/* ecx is often an input as well as an output. */
-	asm volatile("cpuid"
-	    : "=a" (*eax),
-	      "=b" (*ebx),
-	      "=c" (*ecx),
-	      "=d" (*edx)
-	    : "0" (*eax), "2" (*ecx));
-}
+/*
+ * Force using <cpuid.h> __cpuid_count() instead of __cpuid(). The
+ * latter leaves ECX uninitialized, which can break CPUID queries.
+ */
+
+#define cpuid(leaf, a, b, c, d)				\
+	__cpuid_count(leaf, 0, a, b, c, d)
+
+#define cpuid_count(leaf, subleaf, a, b, c, d)		\
+	__cpuid_count(leaf, subleaf, a, b, c, d)
 
 static inline bool has_subleafs(u32 f)
 {
@@ -195,12 +196,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
 	u32 max_func, idx_func;
 	u32 eax, ebx, ecx, edx;
 
-	eax = input_eax;
-	ebx = ecx = edx = 0;
-	cpuid(&eax, &ebx, &ecx, &edx);
-
-	max_func = eax;
-	idx_func = (max_func & 0xffff) + 1;
+	cpuid(input_eax, max_func, ebx, ecx, edx);
 
 	range = malloc(sizeof(struct cpuid_range));
 	if (!range)
@@ -211,6 +207,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
 	else
 		range->is_ext = false;
 
+	idx_func = (max_func & 0xffff) + 1;
 	range->funcs = malloc(sizeof(struct cpuid_func) * idx_func);
 	if (!range->funcs)
 		err(EXIT_FAILURE, NULL);
@@ -222,9 +219,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
 		u32 max_subleaf = MAX_SUBLEAF_NUM;
 		bool allzero;
 
-		eax = f;
-		ecx = 0;
-		cpuid(&eax, &ebx, &ecx, &edx);
+		cpuid(f, eax, ebx, ecx, edx);
 
 		allzero = cpuid_store(range, f, 0, eax, ebx, ecx, edx);
 		if (allzero)
@@ -251,9 +246,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
 			max_subleaf = 5;
 
 		for (u32 subleaf = 1; subleaf < max_subleaf; subleaf++) {
-			eax = f;
-			ecx = subleaf;
-			cpuid(&eax, &ebx, &ecx, &edx);
+			cpuid_count(f, subleaf, eax, ebx, ecx, edx);
 
 			allzero = cpuid_store(range, f, subleaf, eax, ebx, ecx, edx);
 			if (allzero)

  reply	other threads:[~2025-03-25  9:05 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-24 14:20 [PATCH v3 00/20] tools/x86/kcpuid: Update bitfields to x86-cpuid-db v2.3 Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 01/20] tools/x86/kcpuid: Fix error handling Ahmed S. Darwish
2025-03-25  8:55   ` Ingo Molnar
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 02/20] tools/x86/kcpuid: Exit the program on invalid parameters Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 03/20] tools/x86/kcpuid: Simplify usage() handling Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 04/20] tools/x86/kcpuid: Save CPUID output in an array Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 05/20] tools/x86/kcpuid: Print correct CPUID output register names Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 06/20] tools/x86/kcpuid: Remove unused local variable Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 07/20] tools/x86/kcpuid: Remove unused global variable Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 08/20] tools/x86/kcpuid: Set function return type to void Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] tools/x86/kcpuid: Set parse_line() " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 09/20] tools/x86/kcpuid: Use C99-style for loops Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 10/20] tools/x86/kcpuid: Use <cpuid.h> intrinsics Ahmed S. Darwish
2025-03-25  9:05   ` tip-bot2 for Ahmed S. Darwish [this message]
2025-03-24 14:20 ` [PATCH v3 11/20] tools/x86/kcpuid: Refactor CPUID range handling for future expansion Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 12/20] tools/x86/kcpuid: Extend CPUID index mask macro Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 13/20] tools/x86/kcpuid: Consolidate index validity checks Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 14/20] tools/x86/kcpuid: Filter valid CPUID ranges Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 15/20] tools/x86/kcpuid: Define Transmeta and Centaur index ranges Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 16/20] tools/x86/kcpuid: Update bitfields to x86-cpuid-db v2.0 Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 17/20] tools/x86/kcpuid: Update bitfields to x86-cpuid-db v2.1 Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 18/20] tools/x86/kcpuid: Update bitfields to x86-cpuid-db v2.2 Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 19/20] tools/x86/kcpuid: Update bitfields to x86-cpuid-db v2.3 Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] " tip-bot2 for Ahmed S. Darwish
2025-03-24 14:20 ` [PATCH v3 20/20] MAINTAINERS: Include kcpuid under X86 CPUID DATABASE Ahmed S. Darwish
2025-03-25  9:05   ` [tip: x86/cpu] MAINTAINERS: Include the entire kcpuid/ directory under the X86 CPUID DATABASE entry tip-bot2 for Ahmed S. Darwish

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=174289354186.14745.673118297329233119.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=darwi@linutronix.de \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®