Skip to contentSkip to conversation

selftests/x86: Use __builtin_ia32_read/writeeflags

PATCH Applied Andy Lutomirski 2 participants 2 messages Nov 2, 2020 – Jan 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/x86: Use __builtin_ia32_read/writeeflags

The asm to read and write EFLAGS from userspace is horrible. The compiler builtins are now available on all supported compilers, so use them instead.

(The compiler builtins are also unnecessarily ugly, but that's a
more manageable level of ugliness.)

---
 tools/testing/selftests/x86/helpers.h | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
tools/testing/selftests/x86/helpers.h +4 -20
index f5ff2a2615df..4ef42c4559a9 100644
@@ -6,36 +6,20 @@
 
 static inline unsigned long get_eflags(void)
 {
-	unsigned long eflags;
-
-	asm volatile (
 #ifdef __x86_64__
-		"subq $128, %%rsp\n\t"
-		"pushfq\n\t"
-		"popq %0\n\t"
-		"addq $128, %%rsp"
+	return __builtin_ia32_readeflags_u64();
 #else
-		"pushfl\n\t"
-		"popl %0"
+	return __builtin_ia32_readeflags_u32();
 #endif
-		: "=r" (eflags) :: "memory");
-
-	return eflags;
 }
 
 static inline void set_eflags(unsigned long eflags)
 {
-	asm volatile (
 #ifdef __x86_64__
-		"subq $128, %%rsp\n\t"
-		"pushq %0\n\t"
-		"popfq\n\t"
-		"addq $128, %%rsp"
+	__builtin_ia32_writeeflags_u64(eflags);
 #else
-		"pushl %0\n\t"
-		"popfl"
+	__builtin_ia32_writeeflags_u32(eflags);
 #endif
-		:: "r" (eflags) : "flags", "memory");
 }
 
 #endif /* __SELFTESTS_X86_HELPERS_H */
Signature · 1 line
2.28.0
#1 lore raw
The following commit has been merged into the x86/misc branch of tip:

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

Commit-ID:     9297e602adf8d5587d83941c48e4dbae46c8df5f
Gitweb:        https://git.kernel.org/tip/9297e602adf8d5587d83941c48e4dbae46c8df5f
Author:        Andy Lutomirski <[email protected]>
AuthorDate:    Mon, 02 Nov 2020 11:54:02 -08:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 12 Jan 2021 12:31:28 +01:00

selftests/x86: Use __builtin_ia32_read/writeeflags

The asm to read and write EFLAGS from userspace is horrible. The compiler builtins are now available on all supported compilers, so use them instead.

(The compiler builtins are also unnecessarily ugly, but that's a
more manageable level of ugliness.)

---
 tools/testing/selftests/x86/helpers.h | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
tools/testing/selftests/x86/helpers.h +4 -20
index f5ff2a2..4ef42c4 100644
@@ -6,36 +6,20 @@
 
 static inline unsigned long get_eflags(void)
 {
-	unsigned long eflags;
-
-	asm volatile (
 #ifdef __x86_64__
-		"subq $128, %%rsp\n\t"
-		"pushfq\n\t"
-		"popq %0\n\t"
-		"addq $128, %%rsp"
+	return __builtin_ia32_readeflags_u64();
 #else
-		"pushfl\n\t"
-		"popl %0"
+	return __builtin_ia32_readeflags_u32();
 #endif
-		: "=r" (eflags) :: "memory");
-
-	return eflags;
 }
 
 static inline void set_eflags(unsigned long eflags)
 {
-	asm volatile (
 #ifdef __x86_64__
-		"subq $128, %%rsp\n\t"
-		"pushq %0\n\t"
-		"popfq\n\t"
-		"addq $128, %%rsp"
+	__builtin_ia32_writeeflags_u64(eflags);
 #else
-		"pushl %0\n\t"
-		"popfl"
+	__builtin_ia32_writeeflags_u32(eflags);
 #endif
-		:: "r" (eflags) : "flags", "memory");
 }
 
 #endif /* __SELFTESTS_X86_HELPERS_H */