Add Intel rdrand support
authorJens Axboe <axboe@fb.com>
Fri, 29 May 2015 16:44:19 +0000 (10:44 -0600)
committerJens Axboe <axboe@fb.com>
Fri, 29 May 2015 17:32:50 +0000 (11:32 -0600)
Not wired up.

Signed-off-by: Jens Axboe <axboe@fb.com>
arch/arch-x86-common.h
arch/arch-x86_64.h
lib/rand.c
lib/rand.h

index 31aa79f1b573841f68575f49788a0cbbee9d1f0c..cbf66b8d04c18c63566dadff2d06e40a90fd8f69 100644 (file)
@@ -15,8 +15,9 @@ static inline void cpuid(unsigned int op,
 #define ARCH_HAVE_INIT
 
 extern int tsc_reliable;
 #define ARCH_HAVE_INIT
 
 extern int tsc_reliable;
+extern int arch_random;
 
 
-static inline int arch_init_intel(unsigned int level)
+static inline void arch_init_intel(unsigned int level)
 {
        unsigned int eax, ebx, ecx = 0, edx;
 
 {
        unsigned int eax, ebx, ecx = 0, edx;
 
@@ -26,47 +27,51 @@ static inline int arch_init_intel(unsigned int level)
        eax = 1;
        do_cpuid(&eax, &ebx, &ecx, &edx);
        if (!(edx & (1U << 4)))
        eax = 1;
        do_cpuid(&eax, &ebx, &ecx, &edx);
        if (!(edx & (1U << 4)))
-               return 0;
+               return;
 
        /*
         * Check for constant rate and synced (across cores) TSC
         */
        eax = 0x80000007;
        do_cpuid(&eax, &ebx, &ecx, &edx);
 
        /*
         * Check for constant rate and synced (across cores) TSC
         */
        eax = 0x80000007;
        do_cpuid(&eax, &ebx, &ecx, &edx);
-       return edx & (1U << 8);
+       tsc_reliable = (edx & (1U << 8)) != 0;
+
+       /*
+        * Check for FDRAND
+        */
+       eax = 0x1;
+       do_cpuid(&eax, &ebx, &ecx, &edx);
+       arch_random = (ecx & (1U << 30)) != 0;
 }
 
 }
 
-static inline int arch_init_amd(unsigned int level)
+static inline void arch_init_amd(unsigned int level)
 {
        unsigned int eax, ebx, ecx, edx;
 
        cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
        if (eax < 0x80000007)
 {
        unsigned int eax, ebx, ecx, edx;
 
        cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
        if (eax < 0x80000007)
-               return 0;
+               return;
 
        cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
 
        cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
-       if (edx & (1 << 8))
-               return 1;
-
-       return 0;
+       tsc_reliable = (edx & (1U << 8)) != 0;
 }
 
 }
 
-static inline int arch_init(char *envp[])
+static inline void arch_init(char *envp[])
 {
        unsigned int level;
        char str[13];
 
 {
        unsigned int level;
        char str[13];
 
+       arch_random = tsc_reliable = 0;
+
        cpuid(0, &level, (unsigned int *) &str[0],
                         (unsigned int *) &str[8],
                         (unsigned int *) &str[4]);
 
        str[12] = '\0';
        if (!strcmp(str, "GenuineIntel"))
        cpuid(0, &level, (unsigned int *) &str[0],
                         (unsigned int *) &str[8],
                         (unsigned int *) &str[4]);
 
        str[12] = '\0';
        if (!strcmp(str, "GenuineIntel"))
-               tsc_reliable = arch_init_intel(level);
+               arch_init_intel(level);
        else if (!strcmp(str, "AuthenticAMD"))
        else if (!strcmp(str, "AuthenticAMD"))
-               tsc_reliable = arch_init_amd(level);
-
-       return 0;
+               arch_init_amd(level);
 }
 
 #endif
 }
 
 #endif
index 61ac75eefef97a64423cecbef4eff32c2b7199ff..8f33fc541ee6f266e96f9f507ccc3782d618392c 100644 (file)
@@ -60,4 +60,34 @@ static inline unsigned long long get_cpu_clock(void)
 #define ARCH_HAVE_SSE4_2
 #define ARCH_HAVE_CPU_CLOCK
 
 #define ARCH_HAVE_SSE4_2
 #define ARCH_HAVE_CPU_CLOCK
 
+#define RDRAND_LONG    ".byte 0x48,0x0f,0xc7,0xf0"
+#define RDSEED_LONG    ".byte 0x48,0x0f,0xc7,0xf8"
+#define RDRAND_RETRY   100
+
+static inline int arch_rand_long(unsigned long *val)
+{
+       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" (*val)
+                    : "0" (RDRAND_RETRY));
+
+       return ok;
+}
+
+static inline int arch_rand_seed(unsigned long *seed)
+{
+       unsigned char ok;
+
+       asm volatile(RDSEED_LONG "\n\t"
+                       "setc %0"
+                       : "=qm" (ok), "=a" (*seed));
+
+       return 0;
+}
+
 #endif
 #endif
index 185b679e7c1365956cd80e40d433dc8fa41a951e..1d189a277cca2e9fa0c6c5d32cd9a70b41f867a2 100644 (file)
@@ -38,6 +38,8 @@
 #include "rand.h"
 #include "../hash.h"
 
 #include "rand.h"
 #include "../hash.h"
 
+int arch_random;
+
 static inline int __seed(unsigned int x, unsigned int m)
 {
        return (x < m) ? x + m : x;
 static inline int __seed(unsigned int x, unsigned int m)
 {
        return (x < m) ? x + m : x;
index 089837dbf0267c326dc4b355782558c24080abf1..0a577fd3df67d5ce29707a09fd1810cbecf545df 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef FIO_RAND_H
 #define FIO_RAND_H
 
 #ifndef FIO_RAND_H
 #define FIO_RAND_H
 
+#include "../arch/arch.h"
+
 #define FRAND_MAX      (-1U)
 
 struct frand_state {
 #define FRAND_MAX      (-1U)
 
 struct frand_state {