diff mbox series

[1/3] selftests/vm/pkeys: Use existing __cpuid_count() macro

Message ID a21d9b718b5dc4a7be28a27571b567877b38d299.1644000145.git.reinette.chatre@intel.com (mailing list archive)
State New
Headers show
Series selftests: Remove duplicate CPUID wrappers | expand

Commit Message

Reinette Chatre Feb. 4, 2022, 7:17 p.m. UTC
gcc as well as clang makes the __cpuid_count() macro available
via cpuid.h to conveniently call the CPUID instruction.

Below is a copy of the macro as found in cpuid.h:

 #define __cpuid_count(level, count, a, b, c, d)		\
   __asm__ ("cpuid\n\t"						\
            : "=a" (a), "=b" (b), "=c" (c), "=d" (d)		\
            : "0" (level), "2" (count))

The pkeys test contains a local function used as wrapper
of the CPUID instruction. One difference between the pkeys
implementation and the macro from cpuid.h is that the pkeys
implementation provides the "volatile" qualifier to the asm()
call. The "volatile" qualifier is necessary if CPUID has
side effects and thus specifies that any optimizations should
be avoided. The pkeys test uses CPUID to query the system
for its protection keys status and save area properties,
queries without side effect, the "volatile" qualifier
is not required and the macro from cpuid.h can be used instead.

Remove the duplicated wrapper to CPUID and use __cpuid_count()
from cpuid.h instead.

Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Ram Pai <linuxram@us.ibm.com>
Cc: Sandipan Das <sandipan@linux.ibm.com>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: "Desnes A. Nunes do Rosario" <desnesn@linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Thiago Jung Bauermann <bauerman@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Suchanek <msuchanek@suse.de>
Cc: linux-mm@kvack.org
Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
---
 tools/testing/selftests/vm/pkey-x86.h | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/vm/pkey-x86.h b/tools/testing/selftests/vm/pkey-x86.h
index e4a4ce2b826d..17b20af8d8f8 100644
--- a/tools/testing/selftests/vm/pkey-x86.h
+++ b/tools/testing/selftests/vm/pkey-x86.h
@@ -2,6 +2,7 @@ 
 
 #ifndef _PKEYS_X86_H
 #define _PKEYS_X86_H
+#include <cpuid.h>
 
 #ifdef __i386__
 
@@ -80,19 +81,6 @@  static inline void __write_pkey_reg(u64 pkey_reg)
 	assert(pkey_reg == __read_pkey_reg());
 }
 
-static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
-		unsigned int *ecx, unsigned int *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));
-}
-
 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */
 #define X86_FEATURE_PKU        (1<<3) /* Protection Keys for Userspace */
 #define X86_FEATURE_OSPKE      (1<<4) /* OS Protection Keys Enable */
@@ -104,9 +92,7 @@  static inline int cpu_has_pkeys(void)
 	unsigned int ecx;
 	unsigned int edx;
 
-	eax = 0x7;
-	ecx = 0x0;
-	__cpuid(&eax, &ebx, &ecx, &edx);
+	__cpuid_count(0x7, 0x0, eax, ebx, ecx, edx);
 
 	if (!(ecx & X86_FEATURE_PKU)) {
 		dprintf2("cpu does not have PKU\n");
@@ -142,9 +128,7 @@  int pkey_reg_xstate_offset(void)
 	/* assume that XSTATE_PKEY is set in XCR0 */
 	leaf = XSTATE_PKEY_BIT;
 	{
-		eax = XSTATE_CPUID;
-		ecx = leaf;
-		__cpuid(&eax, &ebx, &ecx, &edx);
+		__cpuid_count(XSTATE_CPUID, leaf, eax, ebx, ecx, edx);
 
 		if (leaf == XSTATE_PKEY_BIT) {
 			xstate_offset = ebx;