Skip to contentSkip to conversation

x86/emulate: Remove HAVE_AS_RDRAND and HAVE_AS_RDSEED

PATCH In review [email protected] 2 participants 3 messages Apr 5, 2025 – Apr 7, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86/emulate: Remove HAVE_AS_RDRAND and HAVE_AS_RDSEED

From: Denis Mukhin <[email protected]>

The new toolchain baseline knows the rdrand/rdseed instructions, no need to carry the workaround in the code.

Resolves: https://gitlab.com/xen-project/xen/-/work_items/208

---
Changes since v2:
- updated arch_get_random()
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/

---
 xen/arch/x86/arch.mk              |  2 --
 xen/arch/x86/include/asm/random.h |  2 +-
 xen/arch/x86/x86_emulate/0fc7.c   | 15 +++++----------
 3 files changed, 6 insertions(+), 13 deletions(-)
xen/arch/x86/arch.mk +0 -2
index 3bbaee2a44..5577bf6241 100644
@@ -10,9 +10,7 @@ CFLAGS += -msoft-float
 
 $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS))
 $(call cc-option-add,CFLAGS,CC,-Wnested-externs)
-$(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND)
 $(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT)
-$(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
 $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
 $(call as-option-add,CFLAGS,CC,".equ \"x\"$(comma)1",-DHAVE_AS_QUOTED_SYM)
 $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR)
xen/arch/x86/include/asm/random.h +1 -1
index 9e1fe0bc1d..e1c1c765e1 100644
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_random(void)
     unsigned int val = 0;
 
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm volatile ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        asm volatile ( "rdrand %0" : "=a" (val) );
 
     return val;
 }
xen/arch/x86/x86_emulate/0fc7.c +5 -10
index 5268d5cafd..58c8f79501 100644
@@ -32,7 +32,6 @@ int x86emul_0fc7(struct x86_emulate_state *s,
             return X86EMUL_UNRECOGNIZED;
 
         case 6: /* rdrand */
-#ifdef HAVE_AS_RDRAND
             generate_exception_if(s->vex.pfx >= vex_f3, X86_EXC_UD);
             host_and_vcpu_must_have(rdrand);
             *dst = s->ea;
@@ -43,12 +42,12 @@ int x86emul_0fc7(struct x86_emulate_state *s,
                       : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );
                 break;
             default:
-# ifdef __x86_64__
+#ifdef __x86_64__
                 asm ( "rdrand %k0" ASM_FLAG_OUT(, "; setc %1")
                       : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );
                 break;
             case 8:
-# endif
+#endif
                 asm ( "rdrand %0" ASM_FLAG_OUT(, "; setc %1")
                       : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );
                 break;
@@ -57,9 +56,6 @@ int x86emul_0fc7(struct x86_emulate_state *s,
             if ( carry )
                 regs->eflags |= X86_EFLAGS_CF;
             break;
-#else
-            return X86EMUL_UNIMPLEMENTED;
-#endif
 
         case 7: /* rdseed / rdpid */
             if ( s->vex.pfx == vex_f3 ) /* rdpid */
@@ -77,7 +73,7 @@ int x86emul_0fc7(struct x86_emulate_state *s,
                 dst->bytes = 4;
                 break;
             }
-#ifdef HAVE_AS_RDSEED
+
             generate_exception_if(s->vex.pfx >= vex_f3, X86_EXC_UD);
             host_and_vcpu_must_have(rdseed);
             *dst = s->ea;
@@ -88,12 +84,12 @@ int x86emul_0fc7(struct x86_emulate_state *s,
                       : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );
                 break;
             default:
-# ifdef __x86_64__
+#ifdef __x86_64__
                 asm ( "rdseed %k0" ASM_FLAG_OUT(, "; setc %1")
                       : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );
                 break;
             case 8:
-# endif
+#endif
                 asm ( "rdseed %0" ASM_FLAG_OUT(, "; setc %1")
                       : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );
                 break;
@@ -102,7 +98,6 @@ int x86emul_0fc7(struct x86_emulate_state *s,
             if ( carry )
                 regs->eflags |= X86_EFLAGS_CF;
             break;
-#endif
         }
     }
     else
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · On 05/04/2025 2:25 am, [email protected] wrote:

From: Denis Mukhin <[email protected]>

The new toolchain baseline knows the rdrand/rdseed instructions, no need to carry the workaround in the code.

A few minor notes.  Instructions in CAPITALS please to make them easier to parse in context.  The comma ought to be a semi-colon.

You should note that arch_get_random() is adjusted too.

Also, it's useful to state "No functional change." to help reviewers. (Or to help archaeologies figure out the intent of the patch if they subsequently find a bug in it.)

xen/arch/x86/include/asm/random.h static inline unsigned int arch_get_random(void) · line 11

Quoted 10 lines
xen/arch/x86/include/asm/random.h +1 -1
index 9e1fe0bc1d..e1c1c765e1 100644
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_random(void)
     unsigned int val = 0;
 
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm volatile ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        asm volatile ( "rdrand %0" : "=a" (val) );

This was only tied to "a" because 0xf0 is the ModRM byte.  Now that we can use the mnemonic, it can become "=r".

I've fixed all on commit.

~Andrew

#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · On 05/04/2025 2:25 am, [email protected] wrote:

From: Denis Mukhin [email protected]

The new toolchain baseline knows the rdrand/rdseed instructions, no need to carry the workaround in the code.

Quoted 8 lines

A few minor notes. Instructions in CAPITALS please to make them easier to parse in context. The comma ought to be a semi-colon.

You should note that arch_get_random() is adjusted too.

Also, it's useful to state "No functional change." to help reviewers. (Or to help archaeologies figure out the intent of the patch if they subsequently find a bug in it.)

Noted, thanks for the feedback

Quoted 16 lines

xen/arch/x86/include/asm/random.h static inline unsigned int arch_get_random(void) · line 11

Quoted 11 lines
xen/arch/x86/include/asm/random.h +1 -1
index 9e1fe0bc1d..e1c1c765e1 100644
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_random(void)
 unsigned int val = 0;
 
 if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
- asm volatile ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+ asm volatile ( "rdrand %0" : "=a" (val) );

This was only tied to "a" because 0xf0 is the ModRM byte. Now that we can use the mnemonic, it can become "=r".

Quoted 1 line

I've fixed all on commit.

Thank you!

Quoted 2 lines

~Andrew