lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <a21d9b718b5dc4a7be28a27571b567877b38d299.1644000145.git.reinette.chatre@intel.com>
Date:   Fri,  4 Feb 2022 11:17:09 -0800
From:   Reinette Chatre <reinette.chatre@...el.com>
To:     shuah@...nel.org, linux-kselftest@...r.kernel.org
Cc:     linux-kernel@...r.kernel.org,
        Reinette Chatre <reinette.chatre@...el.com>,
        Dave Hansen <dave.hansen@...ux.intel.com>,
        Ram Pai <linuxram@...ibm.com>,
        Sandipan Das <sandipan@...ux.ibm.com>,
        Florian Weimer <fweimer@...hat.com>,
        "Desnes A. Nunes do Rosario" <desnesn@...ux.vnet.ibm.com>,
        Ingo Molnar <mingo@...nel.org>,
        Thiago Jung Bauermann <bauerman@...ux.ibm.com>,
        Michael Ellerman <mpe@...erman.id.au>,
        Michal Suchanek <msuchanek@...e.de>, linux-mm@...ck.org
Subject: [PATCH 1/3] selftests/vm/pkeys: Use existing __cpuid_count() macro

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@...ux.intel.com>
Cc: Ram Pai <linuxram@...ibm.com>
Cc: Sandipan Das <sandipan@...ux.ibm.com>
Cc: Florian Weimer <fweimer@...hat.com>
Cc: "Desnes A. Nunes do Rosario" <desnesn@...ux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@...nel.org>
Cc: Thiago Jung Bauermann <bauerman@...ux.ibm.com>
Cc: Michael Ellerman <mpe@...erman.id.au>
Cc: Michal Suchanek <msuchanek@...e.de>
Cc: linux-mm@...ck.org
Signed-off-by: Reinette Chatre <reinette.chatre@...el.com>
---
 tools/testing/selftests/vm/pkey-x86.h | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)

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;
-- 
2.25.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ