archrandom.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <asm/alternative.h>
  27. #include <asm/nops.h>
  28. #define RDRAND_RETRY_LOOPS 10
  29. #define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
  30. #define RDSEED_INT ".byte 0x0f,0xc7,0xf8"
  31. #ifdef CONFIG_X86_64
  32. # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
  33. # define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
  34. #else
  35. # define RDRAND_LONG RDRAND_INT
  36. # define RDSEED_LONG RDSEED_INT
  37. #endif
  38. #ifdef CONFIG_ARCH_RANDOM
  39. /* Instead of arch_get_random_long() when alternatives haven't run. */
  40. static inline int rdrand_long(unsigned long *v)
  41. {
  42. int ok;
  43. asm volatile("1: " RDRAND_LONG "\n\t"
  44. "jc 2f\n\t"
  45. "decl %0\n\t"
  46. "jnz 1b\n\t"
  47. "2:"
  48. : "=r" (ok), "=a" (*v)
  49. : "0" (RDRAND_RETRY_LOOPS));
  50. return ok;
  51. }
  52. /* A single attempt at RDSEED */
  53. static inline bool rdseed_long(unsigned long *v)
  54. {
  55. unsigned char ok;
  56. asm volatile(RDSEED_LONG "\n\t"
  57. "setc %0"
  58. : "=qm" (ok), "=a" (*v));
  59. return ok;
  60. }
  61. #define GET_RANDOM(name, type, rdrand, nop) \
  62. static inline int name(type *v) \
  63. { \
  64. int ok; \
  65. alternative_io("movl $0, %0\n\t" \
  66. nop, \
  67. "\n1: " rdrand "\n\t" \
  68. "jc 2f\n\t" \
  69. "decl %0\n\t" \
  70. "jnz 1b\n\t" \
  71. "2:", \
  72. X86_FEATURE_RDRAND, \
  73. ASM_OUTPUT2("=r" (ok), "=a" (*v)), \
  74. "0" (RDRAND_RETRY_LOOPS)); \
  75. return ok; \
  76. }
  77. #define GET_SEED(name, type, rdseed, nop) \
  78. static inline int name(type *v) \
  79. { \
  80. unsigned char ok; \
  81. alternative_io("movb $0, %0\n\t" \
  82. nop, \
  83. rdseed "\n\t" \
  84. "setc %0", \
  85. X86_FEATURE_RDSEED, \
  86. ASM_OUTPUT2("=q" (ok), "=a" (*v))); \
  87. return ok; \
  88. }
  89. #ifdef CONFIG_X86_64
  90. GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP5);
  91. GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP4);
  92. GET_SEED(arch_get_random_seed_long, unsigned long, RDSEED_LONG, ASM_NOP5);
  93. GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4);
  94. #else
  95. GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP3);
  96. GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP3);
  97. GET_SEED(arch_get_random_seed_long, unsigned long, RDSEED_LONG, ASM_NOP4);
  98. GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4);
  99. #endif /* CONFIG_X86_64 */
  100. #define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND)
  101. #define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED)
  102. #else
  103. static inline int rdrand_long(unsigned long *v)
  104. {
  105. return 0;
  106. }
  107. static inline bool rdseed_long(unsigned long *v)
  108. {
  109. return 0;
  110. }
  111. #endif /* CONFIG_ARCH_RANDOM */
  112. extern void x86_init_rdrand(struct cpuinfo_x86 *c);
  113. #endif /* ASM_X86_ARCHRANDOM_H */