x86, asm: Use CC_SET()/CC_OUT() and static_cpu_has() in archrandom.h
authorH. Peter Anvin <hpa@linux.intel.com>
Wed, 8 Jun 2016 19:38:46 +0000 (12:38 -0700)
committerH. Peter Anvin <hpa@linux.intel.com>
Wed, 8 Jun 2016 19:41:20 +0000 (12:41 -0700)
Use CC_SET()/CC_OUT() and static_cpu_has().  This produces code good
enough to eliminate ad hoc use of alternatives in <asm/archrandom.h>,
greatly simplifying the code.

While we are at it, make x86_init_rdrand() compile out completely if
we don't need it.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Link: http://lkml.kernel.org/r/1465414726-197858-11-git-send-email-hpa@linux.intel.com
v2: fix a conflict between <linux/random.h> and <asm/archrandom.h>
    discovered by Ingo Molnar.  There are a few places in x86-specific
    code where we need all of <arch/archrandom.h> even when
    CONFIG_ARCH_RANDOM is disabled, so <linux/random.h> does not
    suffice.

arch/x86/include/asm/archrandom.h
arch/x86/kernel/cpu/rdrand.c

index ab6f599ce2fd94708ec6eea0110e4dc877627aa0..5b0579abb39829ce3c6a1b4f00e73dd9295c26dc 100644 (file)
@@ -25,8 +25,6 @@
 
 #include <asm/processor.h>
 #include <asm/cpufeature.h>
-#include <asm/alternative.h>
-#include <asm/nops.h>
 
 #define RDRAND_RETRY_LOOPS     10
 
 # define RDSEED_LONG   RDSEED_INT
 #endif
 
-#ifdef CONFIG_ARCH_RANDOM
+/* Unconditional execution of RDRAND and RDSEED */
 
-/* Instead of arch_get_random_long() when alternatives haven't run. */
 static inline bool rdrand_long(unsigned long *v)
 {
-       int ok;
-       asm volatile("1: " RDRAND_LONG "\n\t"
-                    "jc 2f\n\t"
-                    "decl %0\n\t"
-                    "jnz 1b\n\t"
-                    "2:"
-                    : "=r" (ok), "=a" (*v)
-                    : "0" (RDRAND_RETRY_LOOPS));
-       return !!ok;
+       bool ok;
+       unsigned int retry = RDRAND_RETRY_LOOPS;
+       do {
+               asm volatile(RDRAND_LONG "\n\t"
+                            CC_SET(c)
+                            : CC_OUT(c) (ok), "=a" (*v));
+               if (ok)
+                       return true;
+       } while (--retry);
+       return false;
+}
+
+static inline bool rdrand_int(unsigned int *v)
+{
+       bool ok;
+       unsigned int retry = RDRAND_RETRY_LOOPS;
+       do {
+               asm volatile(RDRAND_INT "\n\t"
+                            CC_SET(c)
+                            : CC_OUT(c) (ok), "=a" (*v));
+               if (ok)
+                       return true;
+       } while (--retry);
+       return false;
 }
 
-/* A single attempt at RDSEED */
 static inline bool rdseed_long(unsigned long *v)
 {
        bool ok;
        asm volatile(RDSEED_LONG "\n\t"
-                    "setc %0"
-                    : "=qm" (ok), "=a" (*v));
+                    CC_SET(c)
+                    : CC_OUT(c) (ok), "=a" (*v));
        return ok;
 }
 
-#define GET_RANDOM(name, type, rdrand, nop)                    \
-static inline bool name(type *v)                               \
-{                                                              \
-       int ok;                                                 \
-       alternative_io("movl $0, %0\n\t"                        \
-                      nop,                                     \
-                      "\n1: " rdrand "\n\t"                    \
-                      "jc 2f\n\t"                              \
-                      "decl %0\n\t"                            \
-                      "jnz 1b\n\t"                             \
-                      "2:",                                    \
-                      X86_FEATURE_RDRAND,                      \
-                      ASM_OUTPUT2("=r" (ok), "=a" (*v)),       \
-                      "0" (RDRAND_RETRY_LOOPS));               \
-       return !!ok;                                            \
-}
-
-#define GET_SEED(name, type, rdseed, nop)                      \
-static inline bool name(type *v)                               \
-{                                                              \
-       bool ok;                                                \
-       alternative_io("movb $0, %0\n\t"                        \
-                      nop,                                     \
-                      rdseed "\n\t"                            \
-                      "setc %0",                               \
-                      X86_FEATURE_RDSEED,                      \
-                      ASM_OUTPUT2("=q" (ok), "=a" (*v)));      \
-       return ok;                                              \
+static inline bool rdseed_int(unsigned int *v)
+{
+       bool ok;
+       asm volatile(RDSEED_INT "\n\t"
+                    CC_SET(c)
+                    : CC_OUT(c) (ok), "=a" (*v));
+       return ok;
 }
 
-#ifdef CONFIG_X86_64
-
-GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP5);
-GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP4);
-
-GET_SEED(arch_get_random_seed_long, unsigned long, RDSEED_LONG, ASM_NOP5);
-GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4);
-
-#else
-
-GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP3);
-GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP3);
-
-GET_SEED(arch_get_random_seed_long, unsigned long, RDSEED_LONG, ASM_NOP4);
-GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4);
-
-#endif /* CONFIG_X86_64 */
-
+/* Conditional execution based on CPU type */
 #define arch_has_random()      static_cpu_has(X86_FEATURE_RDRAND)
 #define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED)
 
-#else
+/*
+ * These are the generic interfaces; they must not be declared if the
+ * stubs in <linux/random.h> are to be invoked,
+ * i.e. CONFIG_ARCH_RANDOM is not defined.
+ */
+#ifdef CONFIG_ARCH_RANDOM
 
-static inline bool rdrand_long(unsigned long *v)
+static inline bool arch_get_random_long(unsigned long *v)
 {
-       return 0;
+       return arch_has_random() ? rdrand_long(v) : false;
 }
 
-static inline bool rdseed_long(unsigned long *v)
+static inline bool arch_get_random_int(unsigned int *v)
 {
-       return 0;
+       return arch_has_random() ? rdrand_int(v) : false;
 }
 
-#endif  /* CONFIG_ARCH_RANDOM */
+static inline bool arch_get_random_seed_long(unsigned long *v)
+{
+       return arch_has_random_seed() ? rdseed_long(v) : false;
+}
+
+static inline bool arch_get_random_seed_int(unsigned int *v)
+{
+       return arch_has_random_seed() ? rdseed_int(v) : false;
+}
 
 extern void x86_init_rdrand(struct cpuinfo_x86 *c);
 
+#else  /* !CONFIG_ARCH_RANDOM */
+
+static inline void x86_init_rdrand(struct cpuinfo_x86 *c) { }
+
+#endif  /* !CONFIG_ARCH_RANDOM */
+
 #endif /* ASM_X86_ARCHRANDOM_H */
index f6f50c4ceaeceef16170d14d6d55376823bbd63f..cfa97ff67bdab29105946af41bcf5cc718c847fc 100644 (file)
@@ -39,9 +39,9 @@ __setup("nordrand", x86_rdrand_setup);
  */
 #define SANITY_CHECK_LOOPS 8
 
+#ifdef CONFIG_ARCH_RANDOM
 void x86_init_rdrand(struct cpuinfo_x86 *c)
 {
-#ifdef CONFIG_ARCH_RANDOM
        unsigned long tmp;
        int i;
 
@@ -55,5 +55,5 @@ void x86_init_rdrand(struct cpuinfo_x86 *c)
                        return;
                }
        }
-#endif
 }
+#endif