getrandom.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_VDSO_GETRANDOM_H
  3. #define __ASM_VDSO_GETRANDOM_H
  4. #ifndef __ASSEMBLY__
  5. #include <asm/unistd.h>
  6. #include <asm/vdso/vsyscall.h>
  7. #include <vdso/datapage.h>
  8. /**
  9. * getrandom_syscall - Invoke the getrandom() syscall.
  10. * @buffer: Destination buffer to fill with random bytes.
  11. * @len: Size of @buffer in bytes.
  12. * @flags: Zero or more GRND_* flags.
  13. * Returns: The number of random bytes written to @buffer, or a negative value indicating an error.
  14. */
  15. static __always_inline ssize_t getrandom_syscall(void *_buffer, size_t _len, unsigned int _flags)
  16. {
  17. register void *buffer asm ("x0") = _buffer;
  18. register size_t len asm ("x1") = _len;
  19. register unsigned int flags asm ("x2") = _flags;
  20. register long ret asm ("x0");
  21. register long nr asm ("x8") = __NR_getrandom;
  22. asm volatile(
  23. " svc #0\n"
  24. : "=r" (ret)
  25. : "r" (buffer), "r" (len), "r" (flags), "r" (nr)
  26. : "memory");
  27. return ret;
  28. }
  29. static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
  30. {
  31. /*
  32. * The RNG data is in the real VVAR data page, but if a task belongs to a time namespace
  33. * then VVAR_DATA_PAGE_OFFSET points to the namespace-specific VVAR page and VVAR_TIMENS_
  34. * PAGE_OFFSET points to the real VVAR page.
  35. */
  36. if (IS_ENABLED(CONFIG_TIME_NS) && _vdso_data->clock_mode == VDSO_CLOCKMODE_TIMENS)
  37. return (void *)&_vdso_rng_data + VVAR_TIMENS_PAGE_OFFSET * (1UL << CONFIG_PAGE_SHIFT);
  38. return &_vdso_rng_data;
  39. }
  40. #endif /* !__ASSEMBLY__ */
  41. #endif /* __ASM_VDSO_GETRANDOM_H */