xref: /linux/arch/x86/include/asm/vdso/getrandom.h (revision ec16a3cdf37e507013062f9c4a2067eacdd12b62)
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 
12 /**
13  * getrandom_syscall - Invoke the getrandom() syscall.
14  * @buffer:	Destination buffer to fill with random bytes.
15  * @len:	Size of @buffer in bytes.
16  * @flags:	Zero or more GRND_* flags.
17  * Returns:	The number of random bytes written to @buffer, or a negative value indicating an error.
18  */
19 static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
20 {
21 	long ret;
22 
23 	asm ("syscall" : "=a" (ret) :
24 	     "0" (__NR_getrandom), "D" (buffer), "S" (len), "d" (flags) :
25 	     "rcx", "r11", "memory");
26 
27 	return ret;
28 }
29 
30 extern struct vdso_rng_data vdso_rng_data
31 	__attribute__((visibility("hidden")));
32 
33 static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
34 {
35 	if (IS_ENABLED(CONFIG_TIME_NS) && __arch_get_vdso_data()->clock_mode == VDSO_CLOCKMODE_TIMENS)
36 		return (void *)&vdso_rng_data + ((void *)&timens_page - (void *)__arch_get_vdso_data());
37 	return &vdso_rng_data;
38 }
39 
40 #endif /* !__ASSEMBLY__ */
41 
42 #endif /* __ASM_VDSO_GETRANDOM_H */
43