Skip to contentSkip to conversation

x86: cpu: Use native_cpuid() in cpuid_ext()

PATCH In review Bin Meng 3 participants 6 messages Oct 11 – Oct 13 x86

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/2 x86: cpu: Use native_cpuid() in cpuid_ext()

cpuid_ext() saves and restores EBX with 32-bit moves, which clear the upper half of RBX in long mode. The assembly does not declare RBX as an output, so the compiler cannot account for this change.

Initialize the leaf and subleaf in cpuid_result and use native_cpuid(), which declares all four output registers and lets the compiler preserve RBX correctly. Keep the structure-returning interface for callers.

---

 arch/x86/include/asm/cpu.h | 35 +++++++++++++++--------------------
 1 file changed, 15 insertions(+), 20 deletions(-)
arch/x86/include/asm/cpu.h +15 -20
index 1217cebb9b9..a28bc75def6 100644
@@ -89,26 +89,6 @@ static inline struct cpuid_result cpuid(int op)
 	return result;
 }
 
-/*
- * Generic Extended CPUID function
- */
-static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
-{
-	struct cpuid_result result;
-	asm volatile(
-		"mov %%ebx, %%edi;"
-		"cpuid;"
-		"mov %%ebx, %%esi;"
-		"mov %%edi, %%ebx;"
-		: "=a" (result.eax),
-		  "=S" (result.ebx),
-		  "=c" (result.ecx),
-		  "=d" (result.edx)
-		: "0" (op), "2" (ecx)
-		: "edi");
-	return result;
-}
-
 static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
 				unsigned int *ecx, unsigned int *edx)
 {
@@ -122,6 +102,21 @@ static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
 	    : "memory");
 }
 
+/*
+ * Generic Extended CPUID function
+ */
+static inline struct cpuid_result cpuid_ext(int op, unsigned int ecx)
+{
+	struct cpuid_result result = {
+		.eax = op,
+		.ecx = ecx,
+	};
+
+	native_cpuid(&result.eax, &result.ebx, &result.ecx, &result.edx);
+
+	return result;
+}
+
 #define native_cpuid_reg(reg)					\
 static inline unsigned int cpuid_##reg(unsigned int op)	\
 {								\

---
base-commit: 5e3b01c8466dbe45cd6a06299f8f4c7e8c1c2363
branch: cpuid

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 2/2 x86: cpu: Implement cpuid() through cpuid_ext()

cpuid() duplicates the old EBX save and restore sequence and leaves the ECX input unspecified. This is unsafe in long mode and makes queries of indexed CPUID leaves depend on the incoming ECX value.

Call cpuid_ext() with subleaf zero to reuse native_cpuid() and provide a deterministic default for indexed leaves. Keep the existing interface so callers do not need to manage individual register outputs.

---

 arch/x86/include/asm/cpu.h | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)
arch/x86/include/asm/cpu.h +8 -20
index a28bc75def6..9c39ffee062 100644
@@ -69,26 +69,6 @@ struct cpuid_result {
 	uint32_t edx;
 };
 
-/*
- * Generic CPUID function
- */
-static inline struct cpuid_result cpuid(int op)
-{
-	struct cpuid_result result;
-	asm volatile(
-		"mov %%ebx, %%edi;"
-		"cpuid;"
-		"mov %%ebx, %%esi;"
-		"mov %%edi, %%ebx;"
-		: "=a" (result.eax),
-		  "=S" (result.ebx),
-		  "=c" (result.ecx),
-		  "=d" (result.edx)
-		: "0" (op)
-		: "edi");
-	return result;
-}
-
 static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
 				unsigned int *ecx, unsigned int *edx)
 {
@@ -117,6 +97,14 @@ static inline struct cpuid_result cpuid_ext(int op, unsigned int ecx)
 	return result;
 }
 
+/*
+ * Generic CPUID function with subleaf zero
+ */
+static inline struct cpuid_result cpuid(int op)
+{
+	return cpuid_ext(op, 0);
+}
+
 #define native_cpuid_reg(reg)					\
 static inline unsigned int cpuid_##reg(unsigned int op)	\
 {								\

---
base-commit: 5e3b01c8466dbe45cd6a06299f8f4c7e8c1c2363
branch: cpuid

Signature · 1 line
2.53.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] x86: cpu: Implement cpuid() through cpuid_ext()

Quoted 6 lines · Bin Meng

x86: cpu: Implement cpuid() through cpuid_ext()

cpuid() duplicates the old EBX save and restore sequence and leaves the ECX input unspecified. This is unsafe in long mode and makes queries of indexed CPUID leaves depend on the incoming ECX value.

Quoted 8 lines

Call cpuid_ext() with subleaf zero to reuse native_cpuid() and provide a deterministic default for indexed leaves. Keep the existing interface so callers do not need to manage individual register outputs.

arch/x86/include/asm/cpu.h | 28 ++++++++--------------------

 1 file changed, 8 insertions(+), 20 deletions(-)
#3 lore raw

Re: [PATCH 2/2] x86: cpu: Implement cpuid() through cpuid_ext()

Quoted 12 lines · Simon Glass
Quoted 10 lines · Bin Meng

x86: cpu: Implement cpuid() through cpuid_ext()

cpuid() duplicates the old EBX save and restore sequence and leaves the ECX input unspecified. This is unsafe in long mode and makes queries of indexed CPUID leaves depend on the incoming ECX value.

Call cpuid_ext() with subleaf zero to reuse native_cpuid() and provide a deterministic default for indexed leaves. Keep the existing interface so callers do not need to manage individual register outputs.

Quoted 6 lines
Quoted 4 lines

arch/x86/include/asm/cpu.h | 28 ++++++++--------------------

 1 file changed, 8 insertions(+), 20 deletions(-)

applied to u-boot-x86, thanks!

Reviewed-by on v1 1/2
#4 lore raw
In reply to Bin Meng
Quoted 6 lines · Bin Meng

x86: cpu: Use native_cpuid() in cpuid_ext()

cpuid_ext() saves and restores EBX with 32-bit moves, which clear the upper half of RBX in long mode. The assembly does not declare RBX as an output, so the compiler cannot account for this change.

Quoted 8 lines

Initialize the leaf and subleaf in cpuid_result and use native_cpuid(), which declares all four output registers and lets the compiler preserve RBX correctly. Keep the structure-returning interface for callers.

arch/x86/include/asm/cpu.h | 35 +++++++++++++++--------------------

 1 file changed, 15 insertions(+), 20 deletions(-)
#5 lore raw
Quoted 12 lines · Simon Glass
Quoted 10 lines · Bin Meng

x86: cpu: Use native_cpuid() in cpuid_ext()

cpuid_ext() saves and restores EBX with 32-bit moves, which clear the upper half of RBX in long mode. The assembly does not declare RBX as an output, so the compiler cannot account for this change.

Initialize the leaf and subleaf in cpuid_result and use native_cpuid(), which declares all four output registers and lets the compiler preserve RBX correctly. Keep the structure-returning interface for callers.

Quoted 6 lines
Quoted 4 lines

arch/x86/include/asm/cpu.h | 35 +++++++++++++++--------------------

 1 file changed, 15 insertions(+), 20 deletions(-)

applied to u-boot-x86, thanks!