1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#ifndef ASM_X86_ARCHRANDOM_H
24#define ASM_X86_ARCHRANDOM_H
25
26#include <asm/processor.h>
27#include <asm/cpufeature.h>
28
29#define RDRAND_RETRY_LOOPS 10
30
31#define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
32#define RDSEED_INT ".byte 0x0f,0xc7,0xf8"
33#ifdef CONFIG_X86_64
34# define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
35# define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
36#else
37# define RDRAND_LONG RDRAND_INT
38# define RDSEED_LONG RDSEED_INT
39#endif
40
41
42
43static inline bool rdrand_long(unsigned long *v)
44{
45 bool ok;
46 unsigned int retry = RDRAND_RETRY_LOOPS;
47 do {
48 asm volatile(RDRAND_LONG "\n\t"
49 CC_SET(c)
50 : CC_OUT(c) (ok), "=a" (*v));
51 if (ok)
52 return true;
53 } while (--retry);
54 return false;
55}
56
57static inline bool rdrand_int(unsigned int *v)
58{
59 bool ok;
60 unsigned int retry = RDRAND_RETRY_LOOPS;
61 do {
62 asm volatile(RDRAND_INT "\n\t"
63 CC_SET(c)
64 : CC_OUT(c) (ok), "=a" (*v));
65 if (ok)
66 return true;
67 } while (--retry);
68 return false;
69}
70
71static inline bool rdseed_long(unsigned long *v)
72{
73 bool ok;
74 asm volatile(RDSEED_LONG "\n\t"
75 CC_SET(c)
76 : CC_OUT(c) (ok), "=a" (*v));
77 return ok;
78}
79
80static inline bool rdseed_int(unsigned int *v)
81{
82 bool ok;
83 asm volatile(RDSEED_INT "\n\t"
84 CC_SET(c)
85 : CC_OUT(c) (ok), "=a" (*v));
86 return ok;
87}
88
89
90#define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND)
91#define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED)
92
93
94
95
96
97
98#ifdef CONFIG_ARCH_RANDOM
99
100static inline bool arch_get_random_long(unsigned long *v)
101{
102 return arch_has_random() ? rdrand_long(v) : false;
103}
104
105static inline bool arch_get_random_int(unsigned int *v)
106{
107 return arch_has_random() ? rdrand_int(v) : false;
108}
109
110static inline bool arch_get_random_seed_long(unsigned long *v)
111{
112 return arch_has_random_seed() ? rdseed_long(v) : false;
113}
114
115static inline bool arch_get_random_seed_int(unsigned int *v)
116{
117 return arch_has_random_seed() ? rdseed_int(v) : false;
118}
119
120extern void x86_init_rdrand(struct cpuinfo_x86 *c);
121
122#else
123
124static inline void x86_init_rdrand(struct cpuinfo_x86 *c) { }
125
126#endif
127
128#endif
129