David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of the Linux kernel. |
| 4 | * |
| 5 | * Copyright (c) 2011-2014, Intel Corporation |
| 6 | * Authors: Fenghua Yu <fenghua.yu@intel.com>, |
| 7 | * H. Peter Anvin <hpa@linux.intel.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef ASM_X86_ARCHRANDOM_H |
| 11 | #define ASM_X86_ARCHRANDOM_H |
| 12 | |
| 13 | #include <asm/processor.h> |
| 14 | #include <asm/cpufeature.h> |
| 15 | |
| 16 | #define RDRAND_RETRY_LOOPS 10 |
| 17 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | /* Unconditional execution of RDRAND and RDSEED */ |
| 19 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 20 | static inline bool __must_check rdrand_long(unsigned long *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | { |
| 22 | bool ok; |
| 23 | unsigned int retry = RDRAND_RETRY_LOOPS; |
| 24 | do { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 25 | asm volatile("rdrand %[out]" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 26 | CC_SET(c) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 27 | : CC_OUT(c) (ok), [out] "=r" (*v)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | if (ok) |
| 29 | return true; |
| 30 | } while (--retry); |
| 31 | return false; |
| 32 | } |
| 33 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 34 | static inline bool __must_check rdrand_int(unsigned int *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | { |
| 36 | bool ok; |
| 37 | unsigned int retry = RDRAND_RETRY_LOOPS; |
| 38 | do { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 39 | asm volatile("rdrand %[out]" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 40 | CC_SET(c) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 41 | : CC_OUT(c) (ok), [out] "=r" (*v)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 42 | if (ok) |
| 43 | return true; |
| 44 | } while (--retry); |
| 45 | return false; |
| 46 | } |
| 47 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 48 | static inline bool __must_check rdseed_long(unsigned long *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 49 | { |
| 50 | bool ok; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 51 | asm volatile("rdseed %[out]" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | CC_SET(c) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 53 | : CC_OUT(c) (ok), [out] "=r" (*v)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 54 | return ok; |
| 55 | } |
| 56 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 57 | static inline bool __must_check rdseed_int(unsigned int *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | { |
| 59 | bool ok; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 60 | asm volatile("rdseed %[out]" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 61 | CC_SET(c) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 62 | : CC_OUT(c) (ok), [out] "=r" (*v)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 63 | return ok; |
| 64 | } |
| 65 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 66 | /* |
| 67 | * These are the generic interfaces; they must not be declared if the |
| 68 | * stubs in <linux/random.h> are to be invoked, |
| 69 | * i.e. CONFIG_ARCH_RANDOM is not defined. |
| 70 | */ |
| 71 | #ifdef CONFIG_ARCH_RANDOM |
| 72 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 73 | static inline bool __must_check arch_get_random_long(unsigned long *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 74 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 75 | return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 78 | static inline bool __must_check arch_get_random_int(unsigned int *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 80 | return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 83 | static inline bool __must_check arch_get_random_seed_long(unsigned long *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 84 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 85 | return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 88 | static inline bool __must_check arch_get_random_seed_int(unsigned int *v) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 89 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 90 | return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | extern void x86_init_rdrand(struct cpuinfo_x86 *c); |
| 94 | |
| 95 | #else /* !CONFIG_ARCH_RANDOM */ |
| 96 | |
| 97 | static inline void x86_init_rdrand(struct cpuinfo_x86 *c) { } |
| 98 | |
| 99 | #endif /* !CONFIG_ARCH_RANDOM */ |
| 100 | |
| 101 | #endif /* ASM_X86_ARCHRANDOM_H */ |