0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef ASM_X86_ARCHRANDOM_H
0011 #define ASM_X86_ARCHRANDOM_H
0012
0013 #include <asm/processor.h>
0014 #include <asm/cpufeature.h>
0015
0016 #define RDRAND_RETRY_LOOPS 10
0017
0018
0019
0020 static inline bool __must_check rdrand_long(unsigned long *v)
0021 {
0022 bool ok;
0023 unsigned int retry = RDRAND_RETRY_LOOPS;
0024 do {
0025 asm volatile("rdrand %[out]"
0026 CC_SET(c)
0027 : CC_OUT(c) (ok), [out] "=r" (*v));
0028 if (ok)
0029 return true;
0030 } while (--retry);
0031 return false;
0032 }
0033
0034 static inline bool __must_check rdseed_long(unsigned long *v)
0035 {
0036 bool ok;
0037 asm volatile("rdseed %[out]"
0038 CC_SET(c)
0039 : CC_OUT(c) (ok), [out] "=r" (*v));
0040 return ok;
0041 }
0042
0043
0044
0045
0046
0047
0048 static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs)
0049 {
0050 return max_longs && static_cpu_has(X86_FEATURE_RDRAND) && rdrand_long(v) ? 1 : 0;
0051 }
0052
0053 static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
0054 {
0055 return max_longs && static_cpu_has(X86_FEATURE_RDSEED) && rdseed_long(v) ? 1 : 0;
0056 }
0057
0058 #ifndef CONFIG_UML
0059 void x86_init_rdrand(struct cpuinfo_x86 *c);
0060 #endif
0061
0062 #endif