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/capability.h>
41 #include <sys/types.h>
44 #include <sys/filedesc.h>
45 #include <sys/errno.h>
46 #include <sys/event.h>
49 #include <sys/selinfo.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/timespec.h>
55 #include <machine/../linux32/linux.h>
56 #include <machine/../linux32/linux32_proto.h>
58 #include <machine/../linux/linux.h>
59 #include <machine/../linux/linux_proto.h>
62 #include <compat/linux/linux_emul.h>
63 #include <compat/linux/linux_event.h>
64 #include <compat/linux/linux_file.h>
65 #include <compat/linux/linux_util.h>
68 * epoll defines 'struct epoll_event' with the field 'data' as 64 bits
69 * on all architectures. But on 32 bit architectures BSD 'struct kevent' only
70 * has 32 bit opaque pointer as 'udata' field. So we can't pass epoll supplied
71 * data verbatuim. Therefore we allocate 64-bit memory block to pass
72 * user supplied data for every file descriptor.
75 typedef uint64_t epoll_udata_t;
77 struct epoll_emuldata {
78 uint32_t fdc; /* epoll udata max index */
79 epoll_udata_t udata[1]; /* epoll user data vector */
82 #define EPOLL_DEF_SZ 16
83 #define EPOLL_SIZE(fdn) \
84 (sizeof(struct epoll_emuldata)+(fdn) * sizeof(epoll_udata_t))
90 #if defined(__amd64__)
91 __attribute__((packed))
95 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
97 static void epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata);
98 static int epoll_to_kevent(struct thread *td, struct file *epfp,
99 int fd, struct epoll_event *l_event, int *kev_flags,
100 struct kevent *kevent, int *nkevents);
101 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
102 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
103 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
104 static int epoll_delete_event(struct thread *td, struct file *epfp,
106 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
109 struct epoll_copyin_args {
110 struct kevent *changelist;
113 struct epoll_copyout_args {
114 struct epoll_event *leventlist;
121 typedef uint64_t eventfd_t;
123 static fo_rdwr_t eventfd_read;
124 static fo_rdwr_t eventfd_write;
125 static fo_truncate_t eventfd_truncate;
126 static fo_ioctl_t eventfd_ioctl;
127 static fo_poll_t eventfd_poll;
128 static fo_kqfilter_t eventfd_kqfilter;
129 static fo_stat_t eventfd_stat;
130 static fo_close_t eventfd_close;
131 static fo_fill_kinfo_t eventfd_fill_kinfo;
133 static struct fileops eventfdops = {
134 .fo_read = eventfd_read,
135 .fo_write = eventfd_write,
136 .fo_truncate = eventfd_truncate,
137 .fo_ioctl = eventfd_ioctl,
138 .fo_poll = eventfd_poll,
139 .fo_kqfilter = eventfd_kqfilter,
140 .fo_stat = eventfd_stat,
141 .fo_close = eventfd_close,
142 .fo_chmod = invfo_chmod,
143 .fo_chown = invfo_chown,
144 .fo_sendfile = invfo_sendfile,
145 .fo_fill_kinfo = eventfd_fill_kinfo,
146 .fo_flags = DFLAG_PASSABLE
149 static void filt_eventfddetach(struct knote *kn);
150 static int filt_eventfdread(struct knote *kn, long hint);
151 static int filt_eventfdwrite(struct knote *kn, long hint);
153 static struct filterops eventfd_rfiltops = {
155 .f_detach = filt_eventfddetach,
156 .f_event = filt_eventfdread
158 static struct filterops eventfd_wfiltops = {
160 .f_detach = filt_eventfddetach,
161 .f_event = filt_eventfdwrite
167 struct selinfo efd_sel;
171 static int eventfd_create(struct thread *td, uint32_t initval, int flags);
175 epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata)
177 struct linux_pemuldata *pem;
178 struct epoll_emuldata *emd;
184 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
186 LINUX_PEM_XLOCK(pem);
187 if (pem->epoll == NULL) {
188 emd = malloc(EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
194 emd = realloc(emd, EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
199 emd->udata[fd] = udata;
200 LINUX_PEM_XUNLOCK(pem);
204 epoll_create_common(struct thread *td, int flags)
208 error = kern_kqueue(td, flags, NULL);
212 epoll_fd_install(td, EPOLL_DEF_SZ, 0);
218 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
222 * args->size is unused. Linux just tests it
223 * and then forgets it as well.
228 return (epoll_create_common(td, 0));
232 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
236 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
240 if ((args->flags & LINUX_O_CLOEXEC) != 0)
243 return (epoll_create_common(td, flags));
246 /* Structure converting function from epoll to kevent. */
248 epoll_to_kevent(struct thread *td, struct file *epfp,
249 int fd, struct epoll_event *l_event, int *kev_flags,
250 struct kevent *kevent, int *nkevents)
252 uint32_t levents = l_event->events;
253 struct linux_pemuldata *pem;
256 /* flags related to how event is registered */
257 if ((levents & LINUX_EPOLLONESHOT) != 0)
258 *kev_flags |= EV_ONESHOT;
259 if ((levents & LINUX_EPOLLET) != 0)
260 *kev_flags |= EV_CLEAR;
261 if ((levents & LINUX_EPOLLERR) != 0)
262 *kev_flags |= EV_ERROR;
263 if ((levents & LINUX_EPOLLRDHUP) != 0)
264 *kev_flags |= EV_EOF;
266 /* flags related to what event is registered */
267 if ((levents & LINUX_EPOLL_EVRD) != 0) {
268 EV_SET(kevent++, fd, EVFILT_READ, *kev_flags, 0, 0, 0);
271 if ((levents & LINUX_EPOLL_EVWR) != 0) {
272 EV_SET(kevent++, fd, EVFILT_WRITE, *kev_flags, 0, 0, 0);
276 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
280 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
281 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
283 LINUX_PEM_XLOCK(pem);
284 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
285 pem->flags |= LINUX_XUNSUP_EPOLL;
286 LINUX_PEM_XUNLOCK(pem);
287 linux_msg(td, "epoll_ctl unsupported flags: 0x%x\n",
290 LINUX_PEM_XUNLOCK(pem);
298 * Structure converting function from kevent to epoll. In a case
299 * this is called on error in registration we store the error in
300 * event->data and pick it up later in linux_epoll_ctl().
303 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
306 if ((kevent->flags & EV_ERROR) != 0) {
307 l_event->events = LINUX_EPOLLERR;
311 switch (kevent->filter) {
313 l_event->events = LINUX_EPOLLIN|LINUX_EPOLLRDNORM|LINUX_EPOLLPRI;
314 if ((kevent->flags & EV_EOF) != 0)
315 l_event->events |= LINUX_EPOLLRDHUP;
318 l_event->events = LINUX_EPOLLOUT|LINUX_EPOLLWRNORM;
324 * Copyout callback used by kevent. This converts kevent
325 * events to epoll events and copies them back to the
326 * userspace. This is also called on error on registering
330 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
332 struct epoll_copyout_args *args;
333 struct linux_pemuldata *pem;
334 struct epoll_emuldata *emd;
335 struct epoll_event *eep;
338 args = (struct epoll_copyout_args*) arg;
339 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
341 pem = pem_find(args->p);
342 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
343 LINUX_PEM_SLOCK(pem);
345 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
347 for (i = 0; i < count; i++) {
348 kevent_to_epoll(&kevp[i], &eep[i]);
351 KASSERT(fd <= emd->fdc, ("epoll user data vector"
352 " is too small.\n"));
353 eep[i].data = emd->udata[fd];
355 LINUX_PEM_SUNLOCK(pem);
357 error = copyout(eep, args->leventlist, count * sizeof(*eep));
359 args->leventlist += count;
360 args->count += count;
361 } else if (args->error == 0)
369 * Copyin callback used by kevent. This copies already
370 * converted filters from kernel memory to the kevent
371 * internal kernel memory. Hence the memcpy instead of
375 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
377 struct epoll_copyin_args *args;
379 args = (struct epoll_copyin_args*) arg;
381 memcpy(kevp, args->changelist, count * sizeof(*kevp));
382 args->changelist += count;
388 * Load epoll filter, convert it to kevent filter
389 * and load it into kevent subsystem.
392 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
394 struct file *epfp, *fp;
395 struct epoll_copyin_args ciargs;
396 struct kevent kev[2];
397 struct kevent_copyops k_ops = { &ciargs,
400 struct epoll_event le;
406 if (args->op != LINUX_EPOLL_CTL_DEL) {
407 error = copyin(args->event, &le, sizeof(le));
412 error = fget(td, args->epfd,
413 cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &epfp);
416 if (epfp->f_type != DTYPE_KQUEUE)
419 /* Protect user data vector from incorrectly supplied fd. */
420 error = fget(td, args->fd, cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
424 /* Linux disallows spying on himself */
430 ciargs.changelist = kev;
433 case LINUX_EPOLL_CTL_MOD:
435 * We don't memorize which events were set for this FD
436 * on this level, so just delete all we could have set:
437 * EVFILT_READ and EVFILT_WRITE, ignoring any errors
439 error = epoll_delete_all_events(td, epfp, args->fd);
444 case LINUX_EPOLL_CTL_ADD:
445 kev_flags = EV_ADD | EV_ENABLE;
448 case LINUX_EPOLL_CTL_DEL:
449 /* CTL_DEL means unregister this fd with this epoll */
450 error = epoll_delete_all_events(td, epfp, args->fd);
458 error = epoll_to_kevent(td, epfp, args->fd, &le, &kev_flags,
463 epoll_fd_install(td, args->fd, le.data);
465 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
476 * Wait for a filter to be triggered on the epoll file descriptor.
479 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
480 int maxevents, int timeout, sigset_t *uset)
483 struct timespec ts, *tsp;
485 struct epoll_copyout_args coargs;
486 struct kevent_copyops k_ops = { &coargs,
491 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
495 error = kern_sigprocmask(td, SIG_SETMASK, uset,
496 &td->td_oldsigmask, 0);
499 td->td_pflags |= TDP_OLDMASK;
501 * Make sure that ast() is called on return to
502 * usermode and TDP_OLDMASK is cleared, restoring old
506 td->td_flags |= TDF_ASTPENDING;
510 error = fget(td, epfd,
511 cap_rights_init(&rights, CAP_KQUEUE_EVENT), &epfp);
515 coargs.leventlist = events;
516 coargs.p = td->td_proc;
525 /* Convert from milliseconds to timespec. */
526 ts.tv_sec = timeout / 1000;
527 ts.tv_nsec = (timeout % 1000) * 1000000;
533 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
534 if (error == 0 && coargs.error != 0)
535 error = coargs.error;
538 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
539 * Maybe we should translate that but I don't think it matters at all.
542 td->td_retval[0] = coargs.count;
549 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
552 return (linux_epoll_wait_common(td, args->epfd, args->events,
553 args->maxevents, args->timeout, NULL));
557 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
559 sigset_t mask, *pmask;
563 if (args->mask != NULL) {
564 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
567 linux_to_bsd_sigset(&lmask, &mask);
571 return (linux_epoll_wait_common(td, args->epfd, args->events,
572 args->maxevents, args->timeout, pmask));
576 epoll_delete_event(struct thread *td, struct file *epfp, int fd, int filter)
578 struct epoll_copyin_args ciargs;
580 struct kevent_copyops k_ops = { &ciargs,
585 ciargs.changelist = &kev;
586 EV_SET(&kev, fd, filter, EV_DELETE | EV_DISABLE, 0, 0, 0);
588 error = kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL);
591 * here we ignore ENONT, because we don't keep track of events here
599 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
603 error1 = epoll_delete_event(td, epfp, fd, EVFILT_READ);
604 error2 = epoll_delete_event(td, epfp, fd, EVFILT_WRITE);
606 /* report any errors we got */
607 return (error1 == 0 ? error2 : error1);
611 eventfd_create(struct thread *td, uint32_t initval, int flags)
613 struct filedesc *fdp;
616 int fflags, fd, error;
619 if ((flags & LINUX_O_CLOEXEC) != 0)
622 fdp = td->td_proc->p_fd;
623 error = falloc(td, &fp, &fd, fflags);
627 efd = malloc(sizeof(*efd), M_EPOLL, M_WAITOK | M_ZERO);
628 efd->efd_flags = flags;
629 efd->efd_count = initval;
630 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
632 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
634 fflags = FREAD | FWRITE;
635 if ((flags & LINUX_O_NONBLOCK) != 0)
638 finit(fp, fflags, DTYPE_LINUXEFD, efd, &eventfdops);
641 td->td_retval[0] = fd;
646 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
649 return (eventfd_create(td, args->initval, 0));
653 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
656 if ((args->flags & ~(LINUX_O_CLOEXEC|LINUX_O_NONBLOCK|LINUX_EFD_SEMAPHORE)) != 0)
659 return (eventfd_create(td, args->initval, args->flags));
663 eventfd_close(struct file *fp, struct thread *td)
668 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
671 seldrain(&efd->efd_sel);
672 knlist_destroy(&efd->efd_sel.si_note);
674 fp->f_ops = &badfileops;
675 mtx_destroy(&efd->efd_lock);
682 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
683 int flags, struct thread *td)
690 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
693 if (uio->uio_resid < sizeof(eventfd_t))
697 mtx_lock(&efd->efd_lock);
699 if (efd->efd_count == 0) {
700 if ((efd->efd_flags & LINUX_O_NONBLOCK) != 0) {
701 mtx_unlock(&efd->efd_lock);
704 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, "lefdrd", 0);
709 if ((efd->efd_flags & LINUX_EFD_SEMAPHORE) != 0) {
713 count = efd->efd_count;
716 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
717 selwakeup(&efd->efd_sel);
718 wakeup(&efd->efd_count);
719 mtx_unlock(&efd->efd_lock);
720 error = uiomove(&count, sizeof(eventfd_t), uio);
722 mtx_unlock(&efd->efd_lock);
728 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
729 int flags, struct thread *td)
736 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
739 if (uio->uio_resid < sizeof(eventfd_t))
742 error = uiomove(&count, sizeof(eventfd_t), uio);
745 if (count == UINT64_MAX)
748 mtx_lock(&efd->efd_lock);
750 if (UINT64_MAX - efd->efd_count <= count) {
751 if ((efd->efd_flags & LINUX_O_NONBLOCK) != 0) {
752 mtx_unlock(&efd->efd_lock);
755 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
756 PCATCH, "lefdwr", 0);
761 efd->efd_count += count;
762 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
763 selwakeup(&efd->efd_sel);
764 wakeup(&efd->efd_count);
766 mtx_unlock(&efd->efd_lock);
772 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
779 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
782 mtx_lock(&efd->efd_lock);
783 if ((events & (POLLIN|POLLRDNORM)) && efd->efd_count > 0)
784 revents |= events & (POLLIN|POLLRDNORM);
785 if ((events & (POLLOUT|POLLWRNORM)) && UINT64_MAX - 1 > efd->efd_count)
786 revents |= events & (POLLOUT|POLLWRNORM);
788 selrecord(td, &efd->efd_sel);
789 mtx_unlock(&efd->efd_lock);
796 eventfd_kqfilter(struct file *fp, struct knote *kn)
801 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
804 mtx_lock(&efd->efd_lock);
805 switch (kn->kn_filter) {
807 kn->kn_fop = &eventfd_rfiltops;
810 kn->kn_fop = &eventfd_wfiltops;
813 mtx_unlock(&efd->efd_lock);
818 knlist_add(&efd->efd_sel.si_note, kn, 1);
819 mtx_unlock(&efd->efd_lock);
825 filt_eventfddetach(struct knote *kn)
827 struct eventfd *efd = kn->kn_hook;
829 mtx_lock(&efd->efd_lock);
830 knlist_remove(&efd->efd_sel.si_note, kn, 1);
831 mtx_unlock(&efd->efd_lock);
836 filt_eventfdread(struct knote *kn, long hint)
838 struct eventfd *efd = kn->kn_hook;
841 mtx_assert(&efd->efd_lock, MA_OWNED);
842 ret = (efd->efd_count > 0);
849 filt_eventfdwrite(struct knote *kn, long hint)
851 struct eventfd *efd = kn->kn_hook;
854 mtx_assert(&efd->efd_lock, MA_OWNED);
855 ret = (UINT64_MAX - 1 > efd->efd_count);
862 eventfd_truncate(struct file *fp, off_t length, struct ucred *active_cred,
871 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
872 struct ucred *active_cred, struct thread *td)
880 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
889 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
892 kif->kf_type = KF_TYPE_UNKNOWN;