| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * This file is part of the Linux kernel. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2011-2014, Intel Corporation |
|---|
| 5 | 6 | * Authors: Fenghua Yu <fenghua.yu@intel.com>, |
|---|
| 6 | 7 | * 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 | 8 | */ |
|---|
| 22 | 9 | |
|---|
| 23 | 10 | #ifndef ASM_X86_ARCHRANDOM_H |
|---|
| .. | .. |
|---|
| 28 | 15 | |
|---|
| 29 | 16 | #define RDRAND_RETRY_LOOPS 10 |
|---|
| 30 | 17 | |
|---|
| 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 | 18 | /* Unconditional execution of RDRAND and RDSEED */ |
|---|
| 42 | 19 | |
|---|
| 43 | | -static inline bool rdrand_long(unsigned long *v) |
|---|
| 20 | +static inline bool __must_check rdrand_long(unsigned long *v) |
|---|
| 44 | 21 | { |
|---|
| 45 | 22 | bool ok; |
|---|
| 46 | 23 | unsigned int retry = RDRAND_RETRY_LOOPS; |
|---|
| 47 | 24 | do { |
|---|
| 48 | | - asm volatile(RDRAND_LONG |
|---|
| 25 | + asm volatile("rdrand %[out]" |
|---|
| 49 | 26 | CC_SET(c) |
|---|
| 50 | | - : CC_OUT(c) (ok), "=a" (*v)); |
|---|
| 27 | + : CC_OUT(c) (ok), [out] "=r" (*v)); |
|---|
| 51 | 28 | if (ok) |
|---|
| 52 | 29 | return true; |
|---|
| 53 | 30 | } while (--retry); |
|---|
| 54 | 31 | return false; |
|---|
| 55 | 32 | } |
|---|
| 56 | 33 | |
|---|
| 57 | | -static inline bool rdrand_int(unsigned int *v) |
|---|
| 34 | +static inline bool __must_check rdrand_int(unsigned int *v) |
|---|
| 58 | 35 | { |
|---|
| 59 | 36 | bool ok; |
|---|
| 60 | 37 | unsigned int retry = RDRAND_RETRY_LOOPS; |
|---|
| 61 | 38 | do { |
|---|
| 62 | | - asm volatile(RDRAND_INT |
|---|
| 39 | + asm volatile("rdrand %[out]" |
|---|
| 63 | 40 | CC_SET(c) |
|---|
| 64 | | - : CC_OUT(c) (ok), "=a" (*v)); |
|---|
| 41 | + : CC_OUT(c) (ok), [out] "=r" (*v)); |
|---|
| 65 | 42 | if (ok) |
|---|
| 66 | 43 | return true; |
|---|
| 67 | 44 | } while (--retry); |
|---|
| 68 | 45 | return false; |
|---|
| 69 | 46 | } |
|---|
| 70 | 47 | |
|---|
| 71 | | -static inline bool rdseed_long(unsigned long *v) |
|---|
| 48 | +static inline bool __must_check rdseed_long(unsigned long *v) |
|---|
| 72 | 49 | { |
|---|
| 73 | 50 | bool ok; |
|---|
| 74 | | - asm volatile(RDSEED_LONG |
|---|
| 51 | + asm volatile("rdseed %[out]" |
|---|
| 75 | 52 | CC_SET(c) |
|---|
| 76 | | - : CC_OUT(c) (ok), "=a" (*v)); |
|---|
| 53 | + : CC_OUT(c) (ok), [out] "=r" (*v)); |
|---|
| 77 | 54 | return ok; |
|---|
| 78 | 55 | } |
|---|
| 79 | 56 | |
|---|
| 80 | | -static inline bool rdseed_int(unsigned int *v) |
|---|
| 57 | +static inline bool __must_check rdseed_int(unsigned int *v) |
|---|
| 81 | 58 | { |
|---|
| 82 | 59 | bool ok; |
|---|
| 83 | | - asm volatile(RDSEED_INT |
|---|
| 60 | + asm volatile("rdseed %[out]" |
|---|
| 84 | 61 | CC_SET(c) |
|---|
| 85 | | - : CC_OUT(c) (ok), "=a" (*v)); |
|---|
| 62 | + : CC_OUT(c) (ok), [out] "=r" (*v)); |
|---|
| 86 | 63 | return ok; |
|---|
| 87 | 64 | } |
|---|
| 88 | | - |
|---|
| 89 | | -/* Conditional execution based on CPU type */ |
|---|
| 90 | | -#define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND) |
|---|
| 91 | | -#define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED) |
|---|
| 92 | 65 | |
|---|
| 93 | 66 | /* |
|---|
| 94 | 67 | * These are the generic interfaces; they must not be declared if the |
|---|
| .. | .. |
|---|
| 97 | 70 | */ |
|---|
| 98 | 71 | #ifdef CONFIG_ARCH_RANDOM |
|---|
| 99 | 72 | |
|---|
| 100 | | -static inline bool arch_get_random_long(unsigned long *v) |
|---|
| 73 | +static inline bool __must_check arch_get_random_long(unsigned long *v) |
|---|
| 101 | 74 | { |
|---|
| 102 | | - return arch_has_random() ? rdrand_long(v) : false; |
|---|
| 75 | + return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false; |
|---|
| 103 | 76 | } |
|---|
| 104 | 77 | |
|---|
| 105 | | -static inline bool arch_get_random_int(unsigned int *v) |
|---|
| 78 | +static inline bool __must_check arch_get_random_int(unsigned int *v) |
|---|
| 106 | 79 | { |
|---|
| 107 | | - return arch_has_random() ? rdrand_int(v) : false; |
|---|
| 80 | + return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false; |
|---|
| 108 | 81 | } |
|---|
| 109 | 82 | |
|---|
| 110 | | -static inline bool arch_get_random_seed_long(unsigned long *v) |
|---|
| 83 | +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) |
|---|
| 111 | 84 | { |
|---|
| 112 | | - return arch_has_random_seed() ? rdseed_long(v) : false; |
|---|
| 85 | + return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false; |
|---|
| 113 | 86 | } |
|---|
| 114 | 87 | |
|---|
| 115 | | -static inline bool arch_get_random_seed_int(unsigned int *v) |
|---|
| 88 | +static inline bool __must_check arch_get_random_seed_int(unsigned int *v) |
|---|
| 116 | 89 | { |
|---|
| 117 | | - return arch_has_random_seed() ? rdseed_int(v) : false; |
|---|
| 90 | + return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false; |
|---|
| 118 | 91 | } |
|---|
| 119 | 92 | |
|---|
| 120 | 93 | extern void x86_init_rdrand(struct cpuinfo_x86 *c); |
|---|