2 * Copyright (c) 2007 Roman Divacky
3 * Copyright (c) 2014 Dmitry Chagin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/imgact.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/mutex.h>
40 #include <sys/callout.h>
41 #include <sys/capsicum.h>
42 #include <sys/types.h>
45 #include <sys/filedesc.h>
46 #include <sys/filio.h>
47 #include <sys/errno.h>
48 #include <sys/event.h>
51 #include <sys/selinfo.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/timespec.h>
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
64 #include <compat/linux/linux_emul.h>
65 #include <compat/linux/linux_event.h>
66 #include <compat/linux/linux_file.h>
67 #include <compat/linux/linux_timer.h>
68 #include <compat/linux/linux_util.h>
71 * epoll defines 'struct epoll_event' with the field 'data' as 64 bits
72 * on all architectures. But on 32 bit architectures BSD 'struct kevent' only
73 * has 32 bit opaque pointer as 'udata' field. So we can't pass epoll supplied
74 * data verbatuim. Therefore we allocate 64-bit memory block to pass
75 * user supplied data for every file descriptor.
78 typedef uint64_t epoll_udata_t;
80 struct epoll_emuldata {
81 uint32_t fdc; /* epoll udata max index */
82 epoll_udata_t udata[1]; /* epoll user data vector */
85 #define EPOLL_DEF_SZ 16
86 #define EPOLL_SIZE(fdn) \
87 (sizeof(struct epoll_emuldata)+(fdn) * sizeof(epoll_udata_t))
93 #if defined(__amd64__)
94 __attribute__((packed))
98 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
100 static void epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata);
101 static int epoll_to_kevent(struct thread *td, struct file *epfp,
102 int fd, struct epoll_event *l_event, int *kev_flags,
103 struct kevent *kevent, int *nkevents);
104 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
105 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
106 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
107 static int epoll_delete_event(struct thread *td, struct file *epfp,
109 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
112 struct epoll_copyin_args {
113 struct kevent *changelist;
116 struct epoll_copyout_args {
117 struct epoll_event *leventlist;
124 typedef uint64_t eventfd_t;
126 static fo_rdwr_t eventfd_read;
127 static fo_rdwr_t eventfd_write;
128 static fo_ioctl_t eventfd_ioctl;
129 static fo_poll_t eventfd_poll;
130 static fo_kqfilter_t eventfd_kqfilter;
131 static fo_stat_t eventfd_stat;
132 static fo_close_t eventfd_close;
133 static fo_fill_kinfo_t eventfd_fill_kinfo;
135 static struct fileops eventfdops = {
136 .fo_read = eventfd_read,
137 .fo_write = eventfd_write,
138 .fo_truncate = invfo_truncate,
139 .fo_ioctl = eventfd_ioctl,
140 .fo_poll = eventfd_poll,
141 .fo_kqfilter = eventfd_kqfilter,
142 .fo_stat = eventfd_stat,
143 .fo_close = eventfd_close,
144 .fo_chmod = invfo_chmod,
145 .fo_chown = invfo_chown,
146 .fo_sendfile = invfo_sendfile,
147 .fo_fill_kinfo = eventfd_fill_kinfo,
148 .fo_flags = DFLAG_PASSABLE
151 static void filt_eventfddetach(struct knote *kn);
152 static int filt_eventfdread(struct knote *kn, long hint);
153 static int filt_eventfdwrite(struct knote *kn, long hint);
155 static struct filterops eventfd_rfiltops = {
157 .f_detach = filt_eventfddetach,
158 .f_event = filt_eventfdread
160 static struct filterops eventfd_wfiltops = {
162 .f_detach = filt_eventfddetach,
163 .f_event = filt_eventfdwrite
167 typedef uint64_t timerfd_t;
169 static fo_rdwr_t timerfd_read;
170 static fo_poll_t timerfd_poll;
171 static fo_kqfilter_t timerfd_kqfilter;
172 static fo_stat_t timerfd_stat;
173 static fo_close_t timerfd_close;
174 static fo_fill_kinfo_t timerfd_fill_kinfo;
176 static struct fileops timerfdops = {
177 .fo_read = timerfd_read,
178 .fo_write = invfo_rdwr,
179 .fo_truncate = invfo_truncate,
180 .fo_ioctl = eventfd_ioctl,
181 .fo_poll = timerfd_poll,
182 .fo_kqfilter = timerfd_kqfilter,
183 .fo_stat = timerfd_stat,
184 .fo_close = timerfd_close,
185 .fo_chmod = invfo_chmod,
186 .fo_chown = invfo_chown,
187 .fo_sendfile = invfo_sendfile,
188 .fo_fill_kinfo = timerfd_fill_kinfo,
189 .fo_flags = DFLAG_PASSABLE
192 static void filt_timerfddetach(struct knote *kn);
193 static int filt_timerfdread(struct knote *kn, long hint);
195 static struct filterops timerfd_rfiltops = {
197 .f_detach = filt_timerfddetach,
198 .f_event = filt_timerfdread
204 struct selinfo efd_sel;
209 clockid_t tfd_clockid;
210 struct itimerspec tfd_time;
211 struct callout tfd_callout;
214 struct selinfo tfd_sel;
218 static int eventfd_create(struct thread *td, uint32_t initval, int flags);
219 static void linux_timerfd_expire(void *);
220 static void linux_timerfd_curval(struct timerfd *, struct itimerspec *);
224 epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata)
226 struct linux_pemuldata *pem;
227 struct epoll_emuldata *emd;
233 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
235 LINUX_PEM_XLOCK(pem);
236 if (pem->epoll == NULL) {
237 emd = malloc(EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
243 emd = realloc(emd, EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
248 emd->udata[fd] = udata;
249 LINUX_PEM_XUNLOCK(pem);
253 epoll_create_common(struct thread *td, int flags)
257 error = kern_kqueue(td, flags, NULL);
261 epoll_fd_install(td, EPOLL_DEF_SZ, 0);
266 #ifdef LINUX_LEGACY_SYSCALLS
268 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
272 * args->size is unused. Linux just tests it
273 * and then forgets it as well.
278 return (epoll_create_common(td, 0));
283 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
287 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
291 if ((args->flags & LINUX_O_CLOEXEC) != 0)
294 return (epoll_create_common(td, flags));
297 /* Structure converting function from epoll to kevent. */
299 epoll_to_kevent(struct thread *td, struct file *epfp,
300 int fd, struct epoll_event *l_event, int *kev_flags,
301 struct kevent *kevent, int *nkevents)
303 uint32_t levents = l_event->events;
304 struct linux_pemuldata *pem;
307 /* flags related to how event is registered */
308 if ((levents & LINUX_EPOLLONESHOT) != 0)
309 *kev_flags |= EV_ONESHOT;
310 if ((levents & LINUX_EPOLLET) != 0)
311 *kev_flags |= EV_CLEAR;
312 if ((levents & LINUX_EPOLLERR) != 0)
313 *kev_flags |= EV_ERROR;
314 if ((levents & LINUX_EPOLLRDHUP) != 0)
315 *kev_flags |= EV_EOF;
317 /* flags related to what event is registered */
318 if ((levents & LINUX_EPOLL_EVRD) != 0) {
319 EV_SET(kevent++, fd, EVFILT_READ, *kev_flags, 0, 0, 0);
322 if ((levents & LINUX_EPOLL_EVWR) != 0) {
323 EV_SET(kevent++, fd, EVFILT_WRITE, *kev_flags, 0, 0, 0);
327 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
331 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
332 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
334 LINUX_PEM_XLOCK(pem);
335 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
336 pem->flags |= LINUX_XUNSUP_EPOLL;
337 LINUX_PEM_XUNLOCK(pem);
338 linux_msg(td, "epoll_ctl unsupported flags: 0x%x\n",
341 LINUX_PEM_XUNLOCK(pem);
349 * Structure converting function from kevent to epoll. In a case
350 * this is called on error in registration we store the error in
351 * event->data and pick it up later in linux_epoll_ctl().
354 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
357 if ((kevent->flags & EV_ERROR) != 0) {
358 l_event->events = LINUX_EPOLLERR;
362 /* XXX EPOLLPRI, EPOLLHUP */
363 switch (kevent->filter) {
365 l_event->events = LINUX_EPOLLIN;
366 if ((kevent->flags & EV_EOF) != 0)
367 l_event->events |= LINUX_EPOLLRDHUP;
370 l_event->events = LINUX_EPOLLOUT;
376 * Copyout callback used by kevent. This converts kevent
377 * events to epoll events and copies them back to the
378 * userspace. This is also called on error on registering
382 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
384 struct epoll_copyout_args *args;
385 struct linux_pemuldata *pem;
386 struct epoll_emuldata *emd;
387 struct epoll_event *eep;
390 args = (struct epoll_copyout_args*) arg;
391 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
393 pem = pem_find(args->p);
394 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
395 LINUX_PEM_SLOCK(pem);
397 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
399 for (i = 0; i < count; i++) {
400 kevent_to_epoll(&kevp[i], &eep[i]);
403 KASSERT(fd <= emd->fdc, ("epoll user data vector"
404 " is too small.\n"));
405 eep[i].data = emd->udata[fd];
407 LINUX_PEM_SUNLOCK(pem);
409 error = copyout(eep, args->leventlist, count * sizeof(*eep));
411 args->leventlist += count;
412 args->count += count;
413 } else if (args->error == 0)
421 * Copyin callback used by kevent. This copies already
422 * converted filters from kernel memory to the kevent
423 * internal kernel memory. Hence the memcpy instead of
427 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
429 struct epoll_copyin_args *args;
431 args = (struct epoll_copyin_args*) arg;
433 memcpy(kevp, args->changelist, count * sizeof(*kevp));
434 args->changelist += count;
440 * Load epoll filter, convert it to kevent filter
441 * and load it into kevent subsystem.
444 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
446 struct file *epfp, *fp;
447 struct epoll_copyin_args ciargs;
448 struct kevent kev[2];
449 struct kevent_copyops k_ops = { &ciargs,
452 struct epoll_event le;
458 if (args->op != LINUX_EPOLL_CTL_DEL) {
459 error = copyin(args->event, &le, sizeof(le));
464 error = fget(td, args->epfd,
465 cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &epfp);
468 if (epfp->f_type != DTYPE_KQUEUE) {
473 /* Protect user data vector from incorrectly supplied fd. */
474 error = fget(td, args->fd, cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
478 /* Linux disallows spying on himself */
484 ciargs.changelist = kev;
486 if (args->op != LINUX_EPOLL_CTL_DEL) {
487 kev_flags = EV_ADD | EV_ENABLE;
488 error = epoll_to_kevent(td, epfp, args->fd, &le,
489 &kev_flags, kev, &nchanges);
495 case LINUX_EPOLL_CTL_MOD:
496 error = epoll_delete_all_events(td, epfp, args->fd);
501 case LINUX_EPOLL_CTL_ADD:
503 * kqueue_register() return ENOENT if event does not exists
504 * and the EV_ADD flag is not set.
506 kev[0].flags &= ~EV_ADD;
507 error = kqfd_register(args->epfd, &kev[0], td, M_WAITOK);
508 if (error != ENOENT) {
513 kev[0].flags |= EV_ADD;
516 case LINUX_EPOLL_CTL_DEL:
517 /* CTL_DEL means unregister this fd with this epoll */
518 error = epoll_delete_all_events(td, epfp, args->fd);
526 epoll_fd_install(td, args->fd, le.data);
528 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
539 * Wait for a filter to be triggered on the epoll file descriptor.
542 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
543 int maxevents, int timeout, sigset_t *uset)
545 struct epoll_copyout_args coargs;
546 struct kevent_copyops k_ops = { &coargs,
549 struct timespec ts, *tsp;
555 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
558 error = fget(td, epfd,
559 cap_rights_init(&rights, CAP_KQUEUE_EVENT), &epfp);
562 if (epfp->f_type != DTYPE_KQUEUE) {
567 error = kern_sigprocmask(td, SIG_SETMASK, uset,
571 td->td_pflags |= TDP_OLDMASK;
573 * Make sure that ast() is called on return to
574 * usermode and TDP_OLDMASK is cleared, restoring old
578 td->td_flags |= TDF_ASTPENDING;
583 coargs.leventlist = events;
584 coargs.p = td->td_proc;
593 /* Convert from milliseconds to timespec. */
594 ts.tv_sec = timeout / 1000;
595 ts.tv_nsec = (timeout % 1000) * 1000000;
601 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
602 if (error == 0 && coargs.error != 0)
603 error = coargs.error;
606 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
607 * Maybe we should translate that but I don't think it matters at all.
610 td->td_retval[0] = coargs.count;
614 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
621 #ifdef LINUX_LEGACY_SYSCALLS
623 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
626 return (linux_epoll_wait_common(td, args->epfd, args->events,
627 args->maxevents, args->timeout, NULL));
632 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
634 sigset_t mask, *pmask;
638 if (args->mask != NULL) {
639 if (args->sigsetsize != sizeof(l_sigset_t))
641 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
644 linux_to_bsd_sigset(&lmask, &mask);
648 return (linux_epoll_wait_common(td, args->epfd, args->events,
649 args->maxevents, args->timeout, pmask));
653 epoll_delete_event(struct thread *td, struct file *epfp, int fd, int filter)
655 struct epoll_copyin_args ciargs;
657 struct kevent_copyops k_ops = { &ciargs,
661 ciargs.changelist = &kev;
662 EV_SET(&kev, fd, filter, EV_DELETE | EV_DISABLE, 0, 0, 0);
664 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
668 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
672 error1 = epoll_delete_event(td, epfp, fd, EVFILT_READ);
673 error2 = epoll_delete_event(td, epfp, fd, EVFILT_WRITE);
675 /* return 0 if at least one result positive */
676 return (error1 == 0 ? 0 : error2);
680 eventfd_create(struct thread *td, uint32_t initval, int flags)
682 struct filedesc *fdp;
685 int fflags, fd, error;
688 if ((flags & LINUX_O_CLOEXEC) != 0)
691 fdp = td->td_proc->p_fd;
692 error = falloc(td, &fp, &fd, fflags);
696 efd = malloc(sizeof(*efd), M_EPOLL, M_WAITOK | M_ZERO);
697 efd->efd_flags = flags;
698 efd->efd_count = initval;
699 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
701 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
703 fflags = FREAD | FWRITE;
704 if ((flags & LINUX_O_NONBLOCK) != 0)
707 finit(fp, fflags, DTYPE_LINUXEFD, efd, &eventfdops);
710 td->td_retval[0] = fd;
714 #ifdef LINUX_LEGACY_SYSCALLS
716 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
719 return (eventfd_create(td, args->initval, 0));
724 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
727 if ((args->flags & ~(LINUX_O_CLOEXEC|LINUX_O_NONBLOCK|LINUX_EFD_SEMAPHORE)) != 0)
730 return (eventfd_create(td, args->initval, args->flags));
734 eventfd_close(struct file *fp, struct thread *td)
739 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
742 seldrain(&efd->efd_sel);
743 knlist_destroy(&efd->efd_sel.si_note);
745 fp->f_ops = &badfileops;
746 mtx_destroy(&efd->efd_lock);
753 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
754 int flags, struct thread *td)
761 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
764 if (uio->uio_resid < sizeof(eventfd_t))
768 mtx_lock(&efd->efd_lock);
770 if (efd->efd_count == 0) {
771 if ((fp->f_flag & FNONBLOCK) != 0) {
772 mtx_unlock(&efd->efd_lock);
775 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, "lefdrd", 0);
780 if ((efd->efd_flags & LINUX_EFD_SEMAPHORE) != 0) {
784 count = efd->efd_count;
787 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
788 selwakeup(&efd->efd_sel);
789 wakeup(&efd->efd_count);
790 mtx_unlock(&efd->efd_lock);
791 error = uiomove(&count, sizeof(eventfd_t), uio);
793 mtx_unlock(&efd->efd_lock);
799 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
800 int flags, struct thread *td)
807 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
810 if (uio->uio_resid < sizeof(eventfd_t))
813 error = uiomove(&count, sizeof(eventfd_t), uio);
816 if (count == UINT64_MAX)
819 mtx_lock(&efd->efd_lock);
821 if (UINT64_MAX - efd->efd_count <= count) {
822 if ((fp->f_flag & FNONBLOCK) != 0) {
823 mtx_unlock(&efd->efd_lock);
824 /* Do not not return the number of bytes written */
825 uio->uio_resid += sizeof(eventfd_t);
828 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
829 PCATCH, "lefdwr", 0);
834 efd->efd_count += count;
835 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
836 selwakeup(&efd->efd_sel);
837 wakeup(&efd->efd_count);
839 mtx_unlock(&efd->efd_lock);
845 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
852 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
855 mtx_lock(&efd->efd_lock);
856 if ((events & (POLLIN|POLLRDNORM)) && efd->efd_count > 0)
857 revents |= events & (POLLIN|POLLRDNORM);
858 if ((events & (POLLOUT|POLLWRNORM)) && UINT64_MAX - 1 > efd->efd_count)
859 revents |= events & (POLLOUT|POLLWRNORM);
861 selrecord(td, &efd->efd_sel);
862 mtx_unlock(&efd->efd_lock);
869 eventfd_kqfilter(struct file *fp, struct knote *kn)
874 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
877 mtx_lock(&efd->efd_lock);
878 switch (kn->kn_filter) {
880 kn->kn_fop = &eventfd_rfiltops;
883 kn->kn_fop = &eventfd_wfiltops;
886 mtx_unlock(&efd->efd_lock);
891 knlist_add(&efd->efd_sel.si_note, kn, 1);
892 mtx_unlock(&efd->efd_lock);
898 filt_eventfddetach(struct knote *kn)
900 struct eventfd *efd = kn->kn_hook;
902 mtx_lock(&efd->efd_lock);
903 knlist_remove(&efd->efd_sel.si_note, kn, 1);
904 mtx_unlock(&efd->efd_lock);
909 filt_eventfdread(struct knote *kn, long hint)
911 struct eventfd *efd = kn->kn_hook;
914 mtx_assert(&efd->efd_lock, MA_OWNED);
915 ret = (efd->efd_count > 0);
922 filt_eventfdwrite(struct knote *kn, long hint)
924 struct eventfd *efd = kn->kn_hook;
927 mtx_assert(&efd->efd_lock, MA_OWNED);
928 ret = (UINT64_MAX - 1 > efd->efd_count);
935 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
936 struct ucred *active_cred, struct thread *td)
939 if (fp->f_data == NULL || (fp->f_type != DTYPE_LINUXEFD &&
940 fp->f_type != DTYPE_LINUXTFD))
947 atomic_set_int(&fp->f_flag, FNONBLOCK);
949 atomic_clear_int(&fp->f_flag, FNONBLOCK);
959 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
968 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
971 kif->kf_type = KF_TYPE_UNKNOWN;
976 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
978 struct filedesc *fdp;
982 int fflags, fd, error;
984 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
987 error = linux_to_native_clockid(&clockid, args->clockid);
990 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
994 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
997 fdp = td->td_proc->p_fd;
998 error = falloc(td, &fp, &fd, fflags);
1002 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
1003 tfd->tfd_clockid = clockid;
1004 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
1006 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
1007 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
1010 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1011 fflags |= FNONBLOCK;
1013 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
1016 td->td_retval[0] = fd;
1021 timerfd_close(struct file *fp, struct thread *td)
1023 struct timerfd *tfd;
1026 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1029 timespecclear(&tfd->tfd_time.it_value);
1030 timespecclear(&tfd->tfd_time.it_interval);
1032 mtx_lock(&tfd->tfd_lock);
1033 callout_drain(&tfd->tfd_callout);
1034 mtx_unlock(&tfd->tfd_lock);
1036 seldrain(&tfd->tfd_sel);
1037 knlist_destroy(&tfd->tfd_sel.si_note);
1039 fp->f_ops = &badfileops;
1040 mtx_destroy(&tfd->tfd_lock);
1047 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1048 int flags, struct thread *td)
1050 struct timerfd *tfd;
1055 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1058 if (uio->uio_resid < sizeof(timerfd_t))
1062 mtx_lock(&tfd->tfd_lock);
1064 if (tfd->tfd_canceled) {
1066 mtx_unlock(&tfd->tfd_lock);
1069 if (tfd->tfd_count == 0) {
1070 if ((fp->f_flag & FNONBLOCK) != 0) {
1071 mtx_unlock(&tfd->tfd_lock);
1074 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
1079 count = tfd->tfd_count;
1081 mtx_unlock(&tfd->tfd_lock);
1082 error = uiomove(&count, sizeof(timerfd_t), uio);
1084 mtx_unlock(&tfd->tfd_lock);
1090 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
1093 struct timerfd *tfd;
1097 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1100 mtx_lock(&tfd->tfd_lock);
1101 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
1102 revents |= events & (POLLIN|POLLRDNORM);
1104 selrecord(td, &tfd->tfd_sel);
1105 mtx_unlock(&tfd->tfd_lock);
1112 timerfd_kqfilter(struct file *fp, struct knote *kn)
1114 struct timerfd *tfd;
1117 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1120 if (kn->kn_filter == EVFILT_READ)
1121 kn->kn_fop = &timerfd_rfiltops;
1126 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
1132 filt_timerfddetach(struct knote *kn)
1134 struct timerfd *tfd = kn->kn_hook;
1136 mtx_lock(&tfd->tfd_lock);
1137 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
1138 mtx_unlock(&tfd->tfd_lock);
1143 filt_timerfdread(struct knote *kn, long hint)
1145 struct timerfd *tfd = kn->kn_hook;
1147 return (tfd->tfd_count > 0);
1152 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1161 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1164 kif->kf_type = KF_TYPE_UNKNOWN;
1169 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
1172 if (tfd->tfd_clockid == CLOCK_REALTIME)
1174 else /* CLOCK_MONOTONIC */
1179 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
1181 struct timespec cts;
1183 linux_timerfd_clocktime(tfd, &cts);
1184 *ots = tfd->tfd_time;
1185 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
1186 timespecsub(&ots->it_value, &cts, &ots->it_value);
1187 if (ots->it_value.tv_sec < 0 ||
1188 (ots->it_value.tv_sec == 0 &&
1189 ots->it_value.tv_nsec == 0)) {
1190 ots->it_value.tv_sec = 0;
1191 ots->it_value.tv_nsec = 1;
1197 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
1199 struct l_itimerspec lots;
1200 struct itimerspec ots;
1201 struct timerfd *tfd;
1205 error = fget(td, args->fd, &cap_read_rights, &fp);
1209 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1214 mtx_lock(&tfd->tfd_lock);
1215 linux_timerfd_curval(tfd, &ots);
1216 mtx_unlock(&tfd->tfd_lock);
1218 error = native_to_linux_itimerspec(&lots, &ots);
1220 error = copyout(&lots, args->old_value, sizeof(lots));
1228 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1230 struct l_itimerspec lots;
1231 struct itimerspec nts, ots;
1232 struct timespec cts, ts;
1233 struct timerfd *tfd;
1238 if ((args->flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
1241 error = copyin(args->new_value, &lots, sizeof(lots));
1244 error = linux_to_native_itimerspec(&nts, &lots);
1248 error = fget(td, args->fd, &cap_write_rights, &fp);
1252 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1257 mtx_lock(&tfd->tfd_lock);
1258 if (!timespecisset(&nts.it_value))
1259 timespecclear(&nts.it_interval);
1260 if (args->old_value != NULL)
1261 linux_timerfd_curval(tfd, &ots);
1263 tfd->tfd_time = nts;
1264 if (timespecisset(&nts.it_value)) {
1265 linux_timerfd_clocktime(tfd, &cts);
1267 if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
1268 timespecadd(&tfd->tfd_time.it_value, &cts,
1269 &tfd->tfd_time.it_value);
1271 timespecsub(&ts, &cts, &ts);
1273 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1274 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1275 linux_timerfd_expire, tfd);
1276 tfd->tfd_canceled = false;
1278 tfd->tfd_canceled = true;
1279 callout_stop(&tfd->tfd_callout);
1281 mtx_unlock(&tfd->tfd_lock);
1283 if (args->old_value != NULL) {
1284 error = native_to_linux_itimerspec(&lots, &ots);
1286 error = copyout(&lots, args->old_value, sizeof(lots));
1295 linux_timerfd_expire(void *arg)
1297 struct timespec cts, ts;
1299 struct timerfd *tfd;
1301 tfd = (struct timerfd *)arg;
1303 linux_timerfd_clocktime(tfd, &cts);
1304 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1305 if (timespecisset(&tfd->tfd_time.it_interval))
1306 timespecadd(&tfd->tfd_time.it_value,
1307 &tfd->tfd_time.it_interval,
1308 &tfd->tfd_time.it_value);
1310 /* single shot timer */
1311 timespecclear(&tfd->tfd_time.it_value);
1312 if (timespecisset(&tfd->tfd_time.it_value)) {
1313 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1314 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1315 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1316 linux_timerfd_expire, tfd);
1319 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1320 selwakeup(&tfd->tfd_sel);
1321 wakeup(&tfd->tfd_count);
1322 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1323 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1324 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1325 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1326 linux_timerfd_expire, tfd);