archrandom.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * This file is part of the Linux kernel.
  3. *
  4. * Copyright (c) 2011-2014, 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. #ifndef ASM_X86_ARCHRANDOM_H
  23. #define ASM_X86_ARCHRANDOM_H
  24. #include <asm/processor.h>
  25. #include <asm/cpufeature.h>
  26. #define RDRAND_RETRY_LOOPS 10
  27. #define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
  28. #define RDSEED_INT ".byte 0x0f,0xc7,0xf8"
  29. #ifdef CONFIG_X86_64
  30. # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
  31. # define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
  32. #else
  33. # define RDRAND_LONG RDRAND_INT
  34. # define RDSEED_LONG RDSEED_INT
  35. #endif
  36. /* Unconditional execution of RDRAND and RDSEED */
  37. static inline bool rdrand_long(unsigned long *v)
  38. {
  39. bool ok;
  40. unsigned int retry = RDRAND_RETRY_LOOPS;
  41. do {
  42. asm volatile(RDRAND_LONG "\n\t"
  43. CC_SET(c)
  44. : CC_OUT(c) (ok), "=a" (*v));
  45. if (ok)
  46. return true;
  47. } while (--retry);
  48. return false;
  49. }
  50. static inline bool rdrand_int(unsigned int *v)
  51. {
  52. bool ok;
  53. unsigned int retry = RDRAND_RETRY_LOOPS;
  54. do {
  55. asm volatile(RDRAND_INT "\n\t"
  56. CC_SET(c)
  57. : CC_OUT(c) (ok), "=a" (*v));
  58. if (ok)
  59. return true;
  60. } while (--retry);
  61. return false;
  62. }
  63. static inline bool rdseed_long(unsigned long *v)
  64. {
  65. bool ok;
  66. asm volatile(RDSEED_LONG "\n\t"
  67. CC_SET(c)
  68. : CC_OUT(c) (ok), "=a" (*v));
  69. return ok;
  70. }
  71. static inline bool rdseed_int(unsigned int *v)
  72. {
  73. bool ok;
  74. asm volatile(RDSEED_INT "\n\t"
  75. CC_SET(c)
  76. : CC_OUT(c) (ok), "=a" (*v));
  77. return ok;
  78. }
  79. /* Conditional execution based on CPU type */
  80. #define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND)
  81. #define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED)
  82. /*
  83. * These are the generic interfaces; they must not be declared if the
  84. * stubs in <linux/random.h> are to be invoked,
  85. * i.e. CONFIG_ARCH_RANDOM is not defined.
  86. */
  87. #ifdef CONFIG_ARCH_RANDOM
  88. static inline bool arch_get_random_long(unsigned long *v)
  89. {
  90. return arch_has_random() ? rdrand_long(v) : false;
  91. }
  92. static inline bool arch_get_random_int(unsigned int *v)
  93. {
  94. return arch_has_random() ? rdrand_int(v) : false;
  95. }
  96. static inline bool arch_get_random_seed_long(unsigned long *v)
  97. {
  98. return arch_has_random_seed() ? rdseed_long(v) : false;
  99. }
  100. static inline bool arch_get_random_seed_int(unsigned int *v)
  101. {
  102. return arch_has_random_seed() ? rdseed_int(v) : false;
  103. }
  104. extern void x86_init_rdrand(struct cpuinfo_x86 *c);
  105. #else /* !CONFIG_ARCH_RANDOM */
  106. static inline void x86_init_rdrand(struct cpuinfo_x86 *c) { }
  107. #endif /* !CONFIG_ARCH_RANDOM */
  108. #endif /* ASM_X86_ARCHRANDOM_H */