Module Name:    src
Committed By:   thorpej
Date:           Mon Dec 14 16:53:37 UTC 2020

Modified Files:
        src/sys/kern [thorpej-futex]: files.kern syscalls.master
        src/sys/sys [thorpej-futex]: Makefile file.h
Added Files:
        src/sys/kern [thorpej-futex]: sys_timerfd.c
        src/sys/sys [thorpej-futex]: timerfd.h

Log Message:
Native implementation of the Linux timerfd API.


To generate a diff of this commit:
cvs rdiff -u -r1.53.2.2 -r1.53.2.3 src/sys/kern/files.kern
cvs rdiff -u -r0 -r1.1.2.1 src/sys/kern/sys_timerfd.c
cvs rdiff -u -r1.306.2.2 -r1.306.2.3 src/sys/kern/syscalls.master
cvs rdiff -u -r1.176.2.1 -r1.176.2.2 src/sys/sys/Makefile
cvs rdiff -u -r1.86.2.1 -r1.86.2.2 src/sys/sys/file.h
cvs rdiff -u -r0 -r1.1.2.1 src/sys/sys/timerfd.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/files.kern
diff -u src/sys/kern/files.kern:1.53.2.2 src/sys/kern/files.kern:1.53.2.3
--- src/sys/kern/files.kern:1.53.2.2	Mon Dec 14 16:00:51 2020
+++ src/sys/kern/files.kern	Mon Dec 14 16:53:36 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: files.kern,v 1.53.2.2 2020/12/14 16:00:51 thorpej Exp $
+#	$NetBSD: files.kern,v 1.53.2.3 2020/12/14 16:53:36 thorpej Exp $
 
 #
 # kernel sources
@@ -174,6 +174,7 @@ file	kern/sys_select.c		kern
 file	kern/sys_sig.c			kern
 file	kern/sys_sched.c		kern
 file	kern/sys_socket.c		kern
+file	kern/sys_timerfd.c		kern
 file	kern/syscalls.c			syscall_debug | kdtrace_hooks
 file	kern/sysv_ipc.c			sysvshm | sysvsem | sysvmsg
 file	kern/sysv_msg.c			sysvmsg

Index: src/sys/kern/syscalls.master
diff -u src/sys/kern/syscalls.master:1.306.2.2 src/sys/kern/syscalls.master:1.306.2.3
--- src/sys/kern/syscalls.master:1.306.2.2	Mon Dec 14 16:00:51 2020
+++ src/sys/kern/syscalls.master	Mon Dec 14 16:53:36 2020
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.306.2.2 2020/12/14 16:00:51 thorpej Exp $
+	$NetBSD: syscalls.master,v 1.306.2.3 2020/12/14 16:53:36 thorpej Exp $
 
 ;	@(#)syscalls.master	8.2 (Berkeley) 1/13/94
 
@@ -398,9 +398,13 @@
 #else
 176	EXCL		ntp_adjtime
 #endif
-177	UNIMPL
-178	UNIMPL
-179	UNIMPL
+177	STD		{ int|sys||timerfd_create(clockid_t clock_id, \
+			    int flags); }
+178	STD		{ int|sys||timerfd_settime(int fd, int flags, \
+			    const struct itimerspec *new_value, \
+			    struct itimerspec *old_value); }
+179	STD		{ int|sys||timerfd_gettime(int fd, \
+			    struct itimerspec *curr_value); }
 180	UNIMPL
 
 ; Syscalls 180-199 are used by/reserved for BSD

Index: src/sys/sys/Makefile
diff -u src/sys/sys/Makefile:1.176.2.1 src/sys/sys/Makefile:1.176.2.2
--- src/sys/sys/Makefile:1.176.2.1	Mon Dec 14 16:00:51 2020
+++ src/sys/sys/Makefile	Mon Dec 14 16:53:37 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.176.2.1 2020/12/14 16:00:51 thorpej Exp $
+#	$NetBSD: Makefile,v 1.176.2.2 2020/12/14 16:53:37 thorpej Exp $
 
 .include <bsd.own.mk>
 
@@ -42,7 +42,7 @@ INCS=	acct.h acl.h agpio.h aio.h ansi.h 
 	socketvar.h sockio.h spawn.h specificdata.h stat.h \
 	statvfs.h syscall.h syscallargs.h sysctl.h stdarg.h stdbool.h \
 	stdint.h swap.h syncobj.h syslimits.h syslog.h \
-	tape.h termios.h time.h timeb.h timepps.h times.h timespec.h \
+	tape.h termios.h time.h timeb.h timepps.h timerfd.h times.h timespec.h \
 	timex.h tls.h trace.h tree.h tty.h ttychars.h ttycom.h \
 	ttydefaults.h ttydev.h types.h \
 	ucontext.h ucred.h uio.h un.h unistd.h unpcb.h utsname.h uuid.h \

Index: src/sys/sys/file.h
diff -u src/sys/sys/file.h:1.86.2.1 src/sys/sys/file.h:1.86.2.2
--- src/sys/sys/file.h:1.86.2.1	Mon Dec 14 16:00:51 2020
+++ src/sys/sys/file.h	Mon Dec 14 16:53:37 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: file.h,v 1.86.2.1 2020/12/14 16:00:51 thorpej Exp $	*/
+/*	$NetBSD: file.h,v 1.86.2.2 2020/12/14 16:53:37 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -103,6 +103,7 @@ union file_data {
 	struct pipe *fd_pipe;		// DTYPE_PIPE
 	struct kqueue *fd_kq;		// DTYPE_KQUEUE
 	struct eventfd *fd_eventfd;	// DTYPE_EVENTFD
+	struct timerfd *fd_timerfd;	// DTYPE_TIMERFD
 	void *fd_data;			// DTYPE_MISC
 	struct audio_file *fd_audioctx;	// DTYPE_MISC (audio)
 	struct pad_softc *fd_pad;	// DTYPE_MISC (pad)
@@ -150,6 +151,7 @@ struct file {
 #define f_mqueue	f_undata.fd_mq
 #define f_ksem		f_undata.fd_ks
 #define f_eventfd	f_undata.fd_eventfd
+#define f_timerfd	f_undata.fd_timerfd
 
 #define f_rndctx	f_undata.fd_rndctx
 #define f_audioctx	f_undata.fd_audioctx
@@ -173,10 +175,11 @@ struct file {
 #define	DTYPE_MQUEUE	7		/* message queue */
 #define	DTYPE_SEM	8		/* semaphore */
 #define	DTYPE_EVENTFD	9		/* eventfd */
+#define	DTYPE_TIMERFD	10		/* timerfd */
 
 #define DTYPE_NAMES	\
     "0", "file", "socket", "pipe", "kqueue", "misc", "crypto", "mqueue", \
-    "semaphore", "eventfd"
+    "semaphore", "eventfd", "timerfd"
 
 #ifdef _KERNEL
 

Added files:

Index: src/sys/kern/sys_timerfd.c
diff -u /dev/null src/sys/kern/sys_timerfd.c:1.1.2.1
--- /dev/null	Mon Dec 14 16:53:37 2020
+++ src/sys/kern/sys_timerfd.c	Mon Dec 14 16:53:36 2020
@@ -0,0 +1,689 @@
+/*	$NetBSD: sys_timerfd.c,v 1.1.2.1 2020/12/14 16:53:36 thorpej Exp $	*/
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: sys_timerfd.c,v 1.1.2.1 2020/12/14 16:53:36 thorpej Exp $");
+
+/*
+ * timerfd
+ *
+ * Timerfd objects are similar to POSIX timers, except they are associated
+ * with a file descriptor rather than a process.  Timerfd objects are
+ * created with the timerfd_create(2) system call, similar to timer_create(2).
+ * The timerfd analogues for timer_gettime(2) and timer_settime(2) are
+ * timerfd_gettime(2) and timerfd_settime(2), respectively.
+ *
+ * When a timerfd object's timer fires, an internal counter is incremented.
+ * When this timer is non-zero, the descriptor associated with the timerfd
+ * object is "readable".  Note that this is slightly different than the
+ * POSIX timer "overrun" counter, which only increments if the timer fires
+ * again while the notification signal is already pending.  Thus, we are
+ * responsible for incrementing the "overrun" counter each time the timerfd
+ * timer fires.
+ *
+ * This implementation is API compatible with the Linux timerfd interface.
+ */
+
+#include <sys/types.h>
+#include <sys/condvar.h>
+#include <sys/file.h>
+#include <sys/filedesc.h>
+#include <sys/kauth.h>
+#include <sys/mutex.h>
+#include <sys/poll.h>
+#include <sys/proc.h>
+#include <sys/select.h>
+#include <sys/stat.h>
+#include <sys/syscallargs.h>
+#include <sys/timerfd.h>
+#include <sys/uio.h>
+
+/* N.B. all timerfd state is protected by itimer_lock() */
+struct timerfd {
+	struct itimer	tfd_itimer;
+	kcondvar_t	tfd_read_wait;
+	kcondvar_t	tfd_restart_wait;
+	struct selinfo	tfd_read_sel;
+	int64_t		tfd_nwaiters;
+	bool		tfd_cancel_on_set;
+	bool		tfd_cancelled;
+	bool		tfd_restarting;
+
+	/*
+	 * Information kept for stat(2).
+	 */
+	struct timespec tfd_btime;	/* time created */
+	struct timespec	tfd_mtime;	/* last timerfd_settime() */
+	struct timespec	tfd_atime;	/* last read */
+};
+
+static void	timerfd_wake(struct timerfd *);
+
+static inline uint64_t
+timerfd_fire_count(const struct timerfd * const tfd)
+{
+	return (unsigned int)tfd->tfd_itimer.it_overruns;
+}
+
+static inline bool
+timerfd_is_readable(const struct timerfd * const tfd)
+{
+	return tfd->tfd_itimer.it_overruns != 0 || tfd->tfd_cancelled;
+}
+
+/*
+ * timerfd_fire:
+ *
+ *	Called when the timerfd's timer fires.
+ *
+ *	Called from a callout with itimer lock held.
+ */
+static void
+timerfd_fire(struct itimer * const it)
+{
+	struct timerfd * const tfd =
+	    container_of(it, struct timerfd, tfd_itimer);
+
+	it->it_overruns++;
+	timerfd_wake(tfd);
+}
+
+/*
+ * timerfd_realtime_changed:
+ *
+ *	Called when CLOCK_REALTIME is changed with clock_settime()
+ *	or settimeofday().
+ *
+ *	Called with itimer lock held.
+ */
+static void
+timerfd_realtime_changed(struct itimer * const it)
+{
+	struct timerfd * const tfd =
+	    container_of(it, struct timerfd, tfd_itimer);
+
+	/* Should only be called when timer is armed. */
+	KASSERT(timespecisset(&it->it_time.it_value));
+
+	if (tfd->tfd_cancel_on_set) {
+		tfd->tfd_cancelled = true;
+		timerfd_wake(tfd);
+	}
+}
+
+static const struct itimer_ops timerfd_itimer_monotonic_ops = {
+	.ito_fire = timerfd_fire,
+};
+
+static const struct itimer_ops timerfd_itimer_realtime_ops = {
+	.ito_fire = timerfd_fire,
+	.ito_realtime_changed = timerfd_realtime_changed,
+};
+
+/*
+ * timerfd_create:
+ *
+ *	Create a timerfd object.
+ */
+static struct timerfd *
+timerfd_create(clockid_t const clock_id, int const flags)
+{
+	struct timerfd * const tfd = kmem_zalloc(sizeof(*tfd), KM_SLEEP);
+
+	KASSERT(clock_id == CLOCK_REALTIME || clock_id == CLOCK_MONOTONIC);
+
+	cv_init(&tfd->tfd_read_wait, "tfdread");
+	cv_init(&tfd->tfd_restart_wait, "tfdrstrt");
+	selinit(&tfd->tfd_read_sel);
+	getnanotime(&tfd->tfd_btime);
+
+	/* Caller deals with TFD_CLOEXEC and TFD_NONBLOCK. */
+
+	itimer_lock();
+	itimer_init(&tfd->tfd_itimer,
+	    clock_id == CLOCK_REALTIME ? &timerfd_itimer_realtime_ops
+				       : &timerfd_itimer_monotonic_ops,
+	    clock_id, NULL);
+	itimer_unlock();
+
+	return tfd;
+}
+
+/*
+ * timerfd_destroy:
+ *
+ *	Destroy a timerfd object.
+ */
+static void
+timerfd_destroy(struct timerfd * const tfd)
+{
+
+	KASSERT(tfd->tfd_nwaiters == 0);
+	KASSERT(tfd->tfd_restarting == false);
+
+	itimer_lock();
+	itimer_poison(&tfd->tfd_itimer);
+	itimer_fini(&tfd->tfd_itimer);	/* drops itimer lock */
+
+	cv_destroy(&tfd->tfd_read_wait);
+	cv_destroy(&tfd->tfd_restart_wait);
+
+	seldestroy(&tfd->tfd_read_sel);
+
+	kmem_free(tfd, sizeof(*tfd));
+}
+
+/*
+ * timerfd_wait:
+ *
+ *	Block on a timerfd.  Handles non-blocking, as well as
+ *	the restart cases.
+ */
+static int
+timerfd_wait(struct timerfd * const tfd, int const fflag)
+{
+	int error;
+
+	if (fflag & FNONBLOCK) {
+		return EAGAIN;
+	}
+
+	/*
+	 * We're going to block.  If there is a restart in-progress,
+	 * wait for that to complete first.
+	 */
+	while (tfd->tfd_restarting) {
+		cv_wait(&tfd->tfd_restart_wait, &itimer_mutex);
+	}
+
+	tfd->tfd_nwaiters++;
+	KASSERT(tfd->tfd_nwaiters > 0);
+	error = cv_wait_sig(&tfd->tfd_read_wait, &itimer_mutex);
+	tfd->tfd_nwaiters--;
+	KASSERT(tfd->tfd_nwaiters >= 0);
+
+	/*
+	 * If a restart was triggered while we were asleep, we need
+	 * to return ERESTART if no other error was returned.  If we
+	 * are the last waiter coming out of the restart drain, clear
+	 * the condition.
+	 */
+	if (tfd->tfd_restarting) {
+		if (error == 0) {
+			error = ERESTART;
+		}
+		if (tfd->tfd_nwaiters == 0) {
+			tfd->tfd_restarting = false;
+			cv_broadcast(&tfd->tfd_restart_wait);
+		}
+	}
+
+	return error;
+}
+
+/*
+ * timerfd_wake:
+ *
+ *	Wake LWPs blocked on a timerfd.
+ */
+static void
+timerfd_wake(struct timerfd * const tfd)
+{
+
+	if (tfd->tfd_nwaiters) {
+		cv_broadcast(&tfd->tfd_read_wait);
+	}
+	selnotify(&tfd->tfd_read_sel, POLLIN | POLLRDNORM, NOTE_SUBMIT);
+}
+
+/*
+ * timerfd file operations
+ */
+
+static int
+timerfd_fop_read(file_t * const fp, off_t * const offset,
+    struct uio * const uio, kauth_cred_t const cred, int const flags)
+{
+	struct timerfd * const tfd = fp->f_timerfd;
+	struct itimer * const it = &tfd->tfd_itimer;
+	int const fflag = fp->f_flag;
+	uint64_t return_value;
+	int error;
+
+	if (uio->uio_resid < sizeof(uint64_t)) {
+		return EINVAL;
+	}
+
+	itimer_lock();
+
+	while (!timerfd_is_readable(tfd)) {
+		if ((error = timerfd_wait(tfd, fflag)) != 0) {
+			itimer_unlock();
+			return error;
+		}
+	}
+
+	if (tfd->tfd_cancelled) {
+		itimer_unlock();
+		return ECANCELED;
+	}
+
+	return_value = timerfd_fire_count(tfd);
+	it->it_overruns = 0;
+
+	getnanotime(&tfd->tfd_atime);
+
+	itimer_unlock();
+
+	error = uiomove(&return_value, sizeof(return_value), uio);
+
+	return error;
+}
+
+static int
+timerfd_fop_ioctl(file_t * const fp, unsigned long const cmd, void * const data)
+{
+	struct timerfd * const tfd = fp->f_timerfd;
+	int error = 0;
+
+	switch (cmd) {
+	case TFD_IOC_SET_TICKS: {
+		const uint64_t * const new_ticksp = data;
+		if (*new_ticksp > INT_MAX) {
+			return EINVAL;
+		}
+		itimer_lock();
+		tfd->tfd_itimer.it_overruns = (int)*new_ticksp;
+		itimer_unlock();
+		break;
+	    }
+
+	default:
+		error = EPASSTHROUGH;
+	}
+
+	return error;
+}
+
+static int
+timerfd_fop_poll(file_t * const fp, int const events)
+{
+	struct timerfd * const tfd = fp->f_timerfd;
+	int revents = events & (POLLOUT | POLLWRNORM);
+
+	if (events & (POLLIN | POLLRDNORM)) {
+		itimer_lock();
+		if (timerfd_is_readable(tfd)) {
+			revents |= events & (POLLIN | POLLRDNORM);
+		} else {
+			selrecord(curlwp, &tfd->tfd_read_sel);
+		}
+		itimer_unlock();
+	}
+
+	return revents;
+}
+
+static int
+timerfd_fop_stat(file_t * const fp, struct stat * const st)
+{
+	struct timerfd * const tfd = fp->f_timerfd;
+
+	memset(st, 0, sizeof(*st));
+
+	itimer_lock();
+	st->st_size = (off_t)timerfd_fire_count(tfd);
+	st->st_atimespec = tfd->tfd_atime;
+	st->st_mtimespec = tfd->tfd_mtime;
+	itimer_unlock();
+
+	st->st_blksize = sizeof(uint64_t);
+	st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
+	st->st_blocks = 1;
+	st->st_birthtimespec = st->st_ctimespec = tfd->tfd_btime;
+	st->st_uid = kauth_cred_geteuid(fp->f_cred);
+	st->st_gid = kauth_cred_getegid(fp->f_cred);
+
+	return 0;
+}
+
+static int
+timerfd_fop_close(file_t * const fp)
+{
+	struct timerfd * const tfd = fp->f_timerfd;
+
+	fp->f_timerfd = NULL;
+	timerfd_destroy(tfd);
+
+	return 0;
+}
+
+static void
+timerfd_filt_read_detach(struct knote * const kn)
+{
+	struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
+
+	itimer_lock();
+	KASSERT(kn->kn_hook == tfd);
+	selremove_knote(&tfd->tfd_read_sel, kn);
+	itimer_unlock();
+}
+
+static int
+timerfd_filt_read(struct knote * const kn, long const hint)
+{
+	struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
+
+	if (hint & NOTE_SUBMIT) {
+		KASSERT(itimer_lock_held());
+	} else {
+		itimer_lock();
+	}
+
+	kn->kn_data = (int64_t)timerfd_fire_count(tfd);
+
+	if ((hint & NOTE_SUBMIT) == 0) {
+		itimer_unlock();
+	}
+
+	return kn->kn_data != 0;
+}
+
+static const struct filterops timerfd_read_filterops = {
+	.f_isfd = 1,
+	.f_detach = timerfd_filt_read_detach,
+	.f_event = timerfd_filt_read,
+};
+
+static int
+timerfd_fop_kqfilter(file_t * const fp, struct knote * const kn)
+{
+	struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
+	struct selinfo *sel;
+
+	switch (kn->kn_filter) {
+	case EVFILT_READ:
+		sel = &tfd->tfd_read_sel;
+		kn->kn_fop = &timerfd_read_filterops;
+		break;
+
+	default:
+		return EINVAL;
+	}
+
+	kn->kn_hook = tfd;
+
+	itimer_lock();
+	selrecord_knote(sel, kn);
+	itimer_unlock();
+
+	return 0;
+}
+
+static void
+timerfd_fop_restart(file_t * const fp)
+{
+	struct timerfd * const tfd = fp->f_timerfd;
+
+	/*
+	 * Unblock blocked reads in order to allow close() to complete.
+	 * System calls return ERESTART so that the fd is revalidated.
+	 */
+
+	itimer_lock();
+
+	if (tfd->tfd_nwaiters != 0) {
+		tfd->tfd_restarting = true;
+		cv_broadcast(&tfd->tfd_read_wait);
+	}
+
+	itimer_unlock();
+}
+
+static const struct fileops timerfd_fileops = {
+	.fo_name = "timerfd",
+	.fo_read = timerfd_fop_read,
+	.fo_write = fbadop_write,
+	.fo_ioctl = timerfd_fop_ioctl,
+	.fo_fcntl = fnullop_fcntl,
+	.fo_poll = timerfd_fop_poll,
+	.fo_stat = timerfd_fop_stat,
+	.fo_close = timerfd_fop_close,
+	.fo_kqfilter = timerfd_fop_kqfilter,
+	.fo_restart = timerfd_fop_restart,
+};
+
+/*
+ * timerfd_create(2) system call
+ */
+int
+do_timerfd_create(struct lwp * const l, clockid_t const clock_id,
+    int const flags, register_t *retval)
+{
+	file_t *fp;
+	int fd, error;
+
+	if (flags & ~(TFD_CLOEXEC | TFD_NONBLOCK)) {
+		return EINVAL;
+	}
+
+	switch (clock_id) {
+	case CLOCK_REALTIME:
+	case CLOCK_MONOTONIC:
+		/* allowed */
+		break;
+
+	default:
+		return EINVAL;
+	}
+
+	if ((error = fd_allocfile(&fp, &fd)) != 0) {
+		return error;
+	}
+
+	fp->f_flag = FREAD;
+	if (flags & TFD_NONBLOCK) {
+		fp->f_flag |= FNONBLOCK;
+	}
+	fp->f_type = DTYPE_TIMERFD;
+	fp->f_ops = &timerfd_fileops;
+	fp->f_timerfd = timerfd_create(clock_id, flags);
+	fd_set_exclose(l, fd, !!(flags & TFD_CLOEXEC));
+	fd_affix(curproc, fp, fd);
+
+	*retval = fd;
+	return 0;
+}
+
+int
+sys_timerfd_create(struct lwp *l, const struct sys_timerfd_create_args *uap,
+    register_t *retval)
+{
+	/* {
+		syscallarg(clockid_t) clock_id;
+		syscallarg(int) flags;
+	} */
+
+	return do_timerfd_create(l, SCARG(uap, clock_id), SCARG(uap, flags),
+	    retval);
+}
+
+/*
+ * timerfd_gettime(2) system call.
+ */
+int
+do_timerfd_gettime(struct lwp *l, int fd, struct itimerspec *curr_value,
+    register_t *retval)
+{
+	file_t *fp;
+
+	if ((fp = fd_getfile(fd)) == NULL) {
+		return EBADF;
+	}
+
+	if (fp->f_ops != &timerfd_fileops) {
+		fd_putfile(fd);
+		return EINVAL;
+	}
+
+	struct timerfd * const tfd = fp->f_timerfd;
+	itimer_lock();
+	itimer_gettime(&tfd->tfd_itimer, curr_value);
+	itimer_unlock();
+
+	fd_putfile(fd);
+	return 0;
+}
+
+int
+sys_timerfd_gettime(struct lwp *l, const struct sys_timerfd_gettime_args *uap,
+    register_t *retval)
+{
+	/* {
+		syscallarg(int) fd;
+		syscallarg(struct itimerspec *) curr_value;
+	} */
+
+	struct itimerspec oits;
+	int error;
+
+	error = do_timerfd_gettime(l, SCARG(uap, fd), &oits, retval);
+	if (error == 0) {
+		error = copyout(&oits, SCARG(uap, curr_value), sizeof(oits));
+	}
+	return error;
+}
+
+/*
+ * timerfd_settime(2) system call.
+ */
+int
+do_timerfd_settime(struct lwp *l, int fd, int flags,
+    const struct itimerspec *new_value, struct itimerspec *old_value,
+    register_t *retval)
+{
+	file_t *fp;
+	int error;
+
+	if (flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) {
+		return EINVAL;
+	}
+
+	if ((fp = fd_getfile(fd)) == NULL) {
+		return EBADF;
+	}
+
+	if (fp->f_ops != &timerfd_fileops) {
+		fd_putfile(fd);
+		return EINVAL;
+	}
+
+	struct timerfd * const tfd = fp->f_timerfd;
+	struct itimer * const it = &tfd->tfd_itimer;
+
+	itimer_lock();
+
+ restart:
+	if (old_value != NULL) {
+		*old_value = it->it_time;
+	}
+	it->it_time = *new_value;
+
+	/*
+	 * If we've been passed a relative value, convert it to an
+	 * absolute, as that's what the itimer facility expects for
+	 * non-virtual timers.  Also ensure that this doesn't set it
+	 * to zero or lets it go negative.
+	 * XXXJRT re-factor.
+	 */
+	if (timespecisset(&it->it_time.it_value) &&
+	    (flags & TFD_TIMER_ABSTIME) == 0) {
+		struct timespec now;
+		if (it->it_clockid == CLOCK_REALTIME) {
+			getnanotime(&now);
+		} else { /* CLOCK_MONOTONIC */
+			getnanouptime(&now);
+		}
+		timespecadd(&it->it_time.it_value, &now,
+		    &it->it_time.it_value);
+	}
+
+	error = itimer_settime(it);
+	if (error == ERESTART) {
+		goto restart;
+	}
+	KASSERT(error == 0);
+
+	/* Reset the expirations counter. */
+	it->it_overruns = 0;
+
+	if (it->it_clockid == CLOCK_REALTIME) {
+		tfd->tfd_cancelled = false;
+		tfd->tfd_cancel_on_set = !!(flags & TFD_TIMER_CANCEL_ON_SET);
+	}
+
+	getnanotime(&tfd->tfd_mtime);
+	itimer_unlock();
+
+	fd_putfile(fd);
+	return error;
+}
+
+int
+sys_timerfd_settime(struct lwp *l, const struct sys_timerfd_settime_args *uap,
+    register_t *retval)
+{
+	/* {
+		syscallarg(int) fd;
+		syscallarg(int) flags;
+		syscallarg(const struct itimerspec *) new_value;
+		syscallarg(struct itimerspec *) old_value;
+	} */
+
+	struct itimerspec nits, oits, *oitsp = NULL;
+	int error;
+
+	error = copyin(SCARG(uap, new_value), &nits, sizeof(nits));
+	if (error) {
+		return error;
+	}
+
+	if (SCARG(uap, old_value) != NULL) {
+		oitsp = &oits;
+	}
+
+	error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags),
+	    &nits, oitsp, retval);
+	if (error == 0 && oitsp != NULL) {
+		error = copyout(oitsp, SCARG(uap, old_value), sizeof(*oitsp));
+	}
+	return error;
+}

Index: src/sys/sys/timerfd.h
diff -u /dev/null src/sys/sys/timerfd.h:1.1.2.1
--- /dev/null	Mon Dec 14 16:53:37 2020
+++ src/sys/sys/timerfd.h	Mon Dec 14 16:53:37 2020
@@ -0,0 +1,65 @@
+/*	$NetBSD: timerfd.h,v 1.1.2.1 2020/12/14 16:53:37 thorpej Exp $	*/
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _SYS_TIMERFD_H_
+#define	_SYS_TIMERFD_H_
+
+#include <sys/fcntl.h>
+#include <sys/ioccom.h>
+#include <sys/time.h>
+
+/*
+ * Definitions for timerfd_create(2) / timerfd_gettime(2) / timerfd_settime(2).
+ * This implementation is API compatible with the Linux interface.
+ */
+
+#define	TFD_TIMER_ABSTIME	O_WRONLY
+#define	TFD_TIMER_CANCEL_ON_SET	O_RDWR
+#define	TFD_CLOEXEC		O_CLOEXEC
+#define	TFD_NONBLOCK		O_NONBLOCK
+
+#define	TFD_IOC_SET_TICKS	_IOW('T', 0, uint64_t)
+
+#ifdef _KERNEL
+struct lwp;
+int	do_timerfd_create(struct lwp *, clockid_t, int, register_t *);
+int	do_timerfd_gettime(struct lwp *, int, struct itimerspec *,
+	    register_t *);
+int	do_timerfd_settime(struct lwp *, int, int, const struct itimerspec *,
+	    struct itimerspec *, register_t *);
+#else /* ! _KERNEL */
+int	timerfd_create(clockid_t, int);
+int	timerfd_gettime(int, struct itimerspec *);
+int	timerfd_settime(int, int, const struct itimerspec *,
+	    struct itimerspec *);
+#endif /* _KERNEL */
+
+#endif /* _SYS_TIMERFD_H_ */

Reply via email to