archrandom.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * This file is part of the Linux kernel.
  4. *
  5. * Copyright (c) 2011-2014, Intel Corporation
  6. * Authors: Fenghua Yu <[email protected]>,
  7. * H. Peter Anvin <[email protected]>
  8. */
  9. #ifndef ASM_X86_ARCHRANDOM_H
  10. #define ASM_X86_ARCHRANDOM_H
  11. #include <asm/processor.h>
  12. #include <asm/cpufeature.h>
  13. #define RDRAND_RETRY_LOOPS 10
  14. /* Unconditional execution of RDRAND and RDSEED */
  15. static inline bool __must_check rdrand_long(unsigned long *v)
  16. {
  17. bool ok;
  18. unsigned int retry = RDRAND_RETRY_LOOPS;
  19. do {
  20. asm volatile("rdrand %[out]"
  21. CC_SET(c)
  22. : CC_OUT(c) (ok), [out] "=r" (*v));
  23. if (ok)
  24. return true;
  25. } while (--retry);
  26. return false;
  27. }
  28. static inline bool __must_check rdseed_long(unsigned long *v)
  29. {
  30. bool ok;
  31. asm volatile("rdseed %[out]"
  32. CC_SET(c)
  33. : CC_OUT(c) (ok), [out] "=r" (*v));
  34. return ok;
  35. }
  36. /*
  37. * These are the generic interfaces; they must not be declared if the
  38. * stubs in <linux/random.h> are to be invoked.
  39. */
  40. static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs)
  41. {
  42. return max_longs && static_cpu_has(X86_FEATURE_RDRAND) && rdrand_long(v) ? 1 : 0;
  43. }
  44. static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
  45. {
  46. return max_longs && static_cpu_has(X86_FEATURE_RDSEED) && rdseed_long(v) ? 1 : 0;
  47. }
  48. #ifndef CONFIG_UML
  49. void x86_init_rdrand(struct cpuinfo_x86 *c);
  50. #endif
  51. #endif /* ASM_X86_ARCHRANDOM_H */