xref: /linux/tools/include/nolibc/sys/timerfd.h (revision 015a99fa76650e7d6efa3e36f20c0f5b346fe9ce)
1*da69cfb1SThomas Weißschuh /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2*da69cfb1SThomas Weißschuh /*
3*da69cfb1SThomas Weißschuh  * timerfd definitions for NOLIBC
4*da69cfb1SThomas Weißschuh  * Copyright (C) 2025 Thomas Weißschuh <thomas.weissschuh@linutronix.de>
5*da69cfb1SThomas Weißschuh  */
6*da69cfb1SThomas Weißschuh 
7*da69cfb1SThomas Weißschuh /* make sure to include all global symbols */
8*da69cfb1SThomas Weißschuh #include "../nolibc.h"
9*da69cfb1SThomas Weißschuh 
10*da69cfb1SThomas Weißschuh #ifndef _NOLIBC_SYS_TIMERFD_H
11*da69cfb1SThomas Weißschuh #define _NOLIBC_SYS_TIMERFD_H
12*da69cfb1SThomas Weißschuh 
13*da69cfb1SThomas Weißschuh #include "../sys.h"
14*da69cfb1SThomas Weißschuh #include "../time.h"
15*da69cfb1SThomas Weißschuh 
16*da69cfb1SThomas Weißschuh #include <linux/timerfd.h>
17*da69cfb1SThomas Weißschuh 
18*da69cfb1SThomas Weißschuh 
19*da69cfb1SThomas Weißschuh static __attribute__((unused))
sys_timerfd_create(int clockid,int flags)20*da69cfb1SThomas Weißschuh int sys_timerfd_create(int clockid, int flags)
21*da69cfb1SThomas Weißschuh {
22*da69cfb1SThomas Weißschuh 	return my_syscall2(__NR_timerfd_create, clockid, flags);
23*da69cfb1SThomas Weißschuh }
24*da69cfb1SThomas Weißschuh 
25*da69cfb1SThomas Weißschuh static __attribute__((unused))
timerfd_create(int clockid,int flags)26*da69cfb1SThomas Weißschuh int timerfd_create(int clockid, int flags)
27*da69cfb1SThomas Weißschuh {
28*da69cfb1SThomas Weißschuh 	return __sysret(sys_timerfd_create(clockid, flags));
29*da69cfb1SThomas Weißschuh }
30*da69cfb1SThomas Weißschuh 
31*da69cfb1SThomas Weißschuh 
32*da69cfb1SThomas Weißschuh static __attribute__((unused))
sys_timerfd_gettime(int fd,struct itimerspec * curr_value)33*da69cfb1SThomas Weißschuh int sys_timerfd_gettime(int fd, struct itimerspec *curr_value)
34*da69cfb1SThomas Weißschuh {
35*da69cfb1SThomas Weißschuh #if defined(__NR_timerfd_gettime)
36*da69cfb1SThomas Weißschuh 	return my_syscall2(__NR_timerfd_gettime, fd, curr_value);
37*da69cfb1SThomas Weißschuh #elif defined(__NR_timerfd_gettime64)
38*da69cfb1SThomas Weißschuh 	struct __kernel_itimerspec kcurr_value;
39*da69cfb1SThomas Weißschuh 	int ret;
40*da69cfb1SThomas Weißschuh 
41*da69cfb1SThomas Weißschuh 	ret = my_syscall2(__NR_timerfd_gettime64, fd, &kcurr_value);
42*da69cfb1SThomas Weißschuh 	__nolibc_timespec_kernel_to_user(&kcurr_value.it_interval, &curr_value->it_interval);
43*da69cfb1SThomas Weißschuh 	__nolibc_timespec_kernel_to_user(&kcurr_value.it_value, &curr_value->it_value);
44*da69cfb1SThomas Weißschuh 	return ret;
45*da69cfb1SThomas Weißschuh #else
46*da69cfb1SThomas Weißschuh 	return __nolibc_enosys(__func__, fd, curr_value);
47*da69cfb1SThomas Weißschuh #endif
48*da69cfb1SThomas Weißschuh }
49*da69cfb1SThomas Weißschuh 
50*da69cfb1SThomas Weißschuh static __attribute__((unused))
timerfd_gettime(int fd,struct itimerspec * curr_value)51*da69cfb1SThomas Weißschuh int timerfd_gettime(int fd, struct itimerspec *curr_value)
52*da69cfb1SThomas Weißschuh {
53*da69cfb1SThomas Weißschuh 	return __sysret(sys_timerfd_gettime(fd, curr_value));
54*da69cfb1SThomas Weißschuh }
55*da69cfb1SThomas Weißschuh 
56*da69cfb1SThomas Weißschuh 
57*da69cfb1SThomas Weißschuh static __attribute__((unused))
sys_timerfd_settime(int fd,int flags,const struct itimerspec * new_value,struct itimerspec * old_value)58*da69cfb1SThomas Weißschuh int sys_timerfd_settime(int fd, int flags,
59*da69cfb1SThomas Weißschuh 			const struct itimerspec *new_value, struct itimerspec *old_value)
60*da69cfb1SThomas Weißschuh {
61*da69cfb1SThomas Weißschuh #if defined(__NR_timerfd_settime)
62*da69cfb1SThomas Weißschuh 	return my_syscall4(__NR_timerfd_settime, fd, flags, new_value, old_value);
63*da69cfb1SThomas Weißschuh #elif defined(__NR_timerfd_settime64)
64*da69cfb1SThomas Weißschuh 	struct __kernel_itimerspec knew_value, kold_value;
65*da69cfb1SThomas Weißschuh 	int ret;
66*da69cfb1SThomas Weißschuh 
67*da69cfb1SThomas Weißschuh 	__nolibc_timespec_user_to_kernel(&new_value->it_value, &knew_value.it_value);
68*da69cfb1SThomas Weißschuh 	__nolibc_timespec_user_to_kernel(&new_value->it_interval, &knew_value.it_interval);
69*da69cfb1SThomas Weißschuh 	ret = my_syscall4(__NR_timerfd_settime64, fd, flags, &knew_value, &kold_value);
70*da69cfb1SThomas Weißschuh 	if (old_value) {
71*da69cfb1SThomas Weißschuh 		__nolibc_timespec_kernel_to_user(&kold_value.it_interval, &old_value->it_interval);
72*da69cfb1SThomas Weißschuh 		__nolibc_timespec_kernel_to_user(&kold_value.it_value, &old_value->it_value);
73*da69cfb1SThomas Weißschuh 	}
74*da69cfb1SThomas Weißschuh 	return ret;
75*da69cfb1SThomas Weißschuh #else
76*da69cfb1SThomas Weißschuh 	return __nolibc_enosys(__func__, fd, flags, new_value, old_value);
77*da69cfb1SThomas Weißschuh #endif
78*da69cfb1SThomas Weißschuh }
79*da69cfb1SThomas Weißschuh 
80*da69cfb1SThomas Weißschuh static __attribute__((unused))
timerfd_settime(int fd,int flags,const struct itimerspec * new_value,struct itimerspec * old_value)81*da69cfb1SThomas Weißschuh int timerfd_settime(int fd, int flags,
82*da69cfb1SThomas Weißschuh 		    const struct itimerspec *new_value, struct itimerspec *old_value)
83*da69cfb1SThomas Weißschuh {
84*da69cfb1SThomas Weißschuh 	return __sysret(sys_timerfd_settime(fd, flags, new_value, old_value));
85*da69cfb1SThomas Weißschuh }
86*da69cfb1SThomas Weißschuh 
87*da69cfb1SThomas Weißschuh #endif /* _NOLIBC_SYS_TIMERFD_H */
88