xref: /linux/arch/x86/include/asm/vdso/getrandom.h (revision c7546e2c3cb739a3c1a2f5acaf9bb629d401afe5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2022-2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 #ifndef __ASM_VDSO_GETRANDOM_H
6 #define __ASM_VDSO_GETRANDOM_H
7 
8 #ifndef __ASSEMBLY__
9 
10 #include <asm/unistd.h>
11 #include <asm/vvar.h>
12 
13 /**
14  * getrandom_syscall - Invoke the getrandom() syscall.
15  * @buffer:	Destination buffer to fill with random bytes.
16  * @len:	Size of @buffer in bytes.
17  * @flags:	Zero or more GRND_* flags.
18  * Returns:	The number of random bytes written to @buffer, or a negative value indicating an error.
19  */
20 static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
21 {
22 	long ret;
23 
24 	asm ("syscall" : "=a" (ret) :
25 	     "0" (__NR_getrandom), "D" (buffer), "S" (len), "d" (flags) :
26 	     "rcx", "r11", "memory");
27 
28 	return ret;
29 }
30 
31 #define __vdso_rng_data (VVAR(_vdso_rng_data))
32 
33 static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
34 {
35 	if (IS_ENABLED(CONFIG_TIME_NS) && __vdso_data->clock_mode == VDSO_CLOCKMODE_TIMENS)
36 		return (void *)&__vdso_rng_data + ((void *)&__timens_vdso_data - (void *)&__vdso_data);
37 	return &__vdso_rng_data;
38 }
39 
40 #endif /* !__ASSEMBLY__ */
41 
42 #endif /* __ASM_VDSO_GETRANDOM_H */
43