]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/include/asm/archrandom.h
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / include / asm / archrandom.h
1 /*
2 * This file is part of the Linux kernel.
3 *
4 * Copyright (c) 2011, Intel Corporation
5 * Authors: Fenghua Yu <fenghua.yu@intel.com>,
6 * H. Peter Anvin <hpa@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
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 #include <asm/alternative.h>
29 #include <asm/nops.h>
30
31 #define RDRAND_RETRY_LOOPS 10
32
33 #define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
34 #ifdef CONFIG_X86_64
35 # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
36 #else
37 # define RDRAND_LONG RDRAND_INT
38 #endif
39
40 #ifdef CONFIG_ARCH_RANDOM
41
42 /* Instead of arch_get_random_long() when alternatives haven't run. */
43 static inline int rdrand_long(unsigned long *v)
44 {
45 int ok;
46 asm volatile("1: " RDRAND_LONG "\n\t"
47 "jc 2f\n\t"
48 "decl %0\n\t"
49 "jnz 1b\n\t"
50 "2:"
51 : "=r" (ok), "=a" (*v)
52 : "0" (RDRAND_RETRY_LOOPS));
53 return ok;
54 }
55
56 #define GET_RANDOM(name, type, rdrand, nop) \
57 static inline int name(type *v) \
58 { \
59 int ok; \
60 alternative_io("movl $0, %0\n\t" \
61 nop, \
62 "\n1: " rdrand "\n\t" \
63 "jc 2f\n\t" \
64 "decl %0\n\t" \
65 "jnz 1b\n\t" \
66 "2:", \
67 X86_FEATURE_RDRAND, \
68 ASM_OUTPUT2("=r" (ok), "=a" (*v)), \
69 "0" (RDRAND_RETRY_LOOPS)); \
70 return ok; \
71 }
72
73 #ifdef CONFIG_X86_64
74
75 GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP5);
76 GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP4);
77
78 #else
79
80 GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP3);
81 GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP3);
82
83 #endif /* CONFIG_X86_64 */
84
85 #else
86
87 static inline int rdrand_long(unsigned long *v)
88 {
89 return 0;
90 }
91
92 #endif /* CONFIG_ARCH_RANDOM */
93
94 extern void x86_init_rdrand(struct cpuinfo_x86 *c);
95
96 #endif /* ASM_X86_ARCHRANDOM_H */