archrandom.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #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. #ifdef CONFIG_X86_64
  31. # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
  32. #else
  33. # define RDRAND_LONG RDRAND_INT
  34. #endif
  35. #ifdef CONFIG_ARCH_RANDOM
  36. #define GET_RANDOM(name, type, rdrand, nop) \
  37. static inline int name(type *v) \
  38. { \
  39. int ok; \
  40. alternative_io("movl $0, %0\n\t" \
  41. nop, \
  42. "\n1: " rdrand "\n\t" \
  43. "jc 2f\n\t" \
  44. "decl %0\n\t" \
  45. "jnz 1b\n\t" \
  46. "2:", \
  47. X86_FEATURE_RDRAND, \
  48. ASM_OUTPUT2("=r" (ok), "=a" (*v)), \
  49. "0" (RDRAND_RETRY_LOOPS)); \
  50. return ok; \
  51. }
  52. #ifdef CONFIG_X86_64
  53. GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP5);
  54. GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP4);
  55. #else
  56. GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP3);
  57. GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP3);
  58. #endif /* CONFIG_X86_64 */
  59. #endif /* CONFIG_ARCH_RANDOM */
  60. extern void x86_init_rdrand(struct cpuinfo_x86 *c);
  61. #endif /* ASM_X86_ARCHRANDOM_H */