2 * Copyright (c) 2007 Roman Divacky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "opt_compat.h"
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kern_syscall.h>
32 #include <sys/event.h>
34 #include <sys/mplock2.h>
35 #include <sys/malloc.h>
36 #include <sys/ptrace.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysent.h>
40 #include <sys/sysproto.h>
44 #include <vm/vm_param.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_extern.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <machine/cpu.h>
52 #include "i386/linux.h"
53 #include "i386/linux_proto.h"
54 #include "linux_signal.h"
55 #include "linux_util.h"
56 #include "linux_epoll.h"
59 /* Create a new epoll file descriptor. */
61 sys_linux_epoll_create(struct linux_epoll_create_args
*args
)
63 struct kqueue_args k_args
;
67 /* args->size is unused. Linux ignores it as well. */
69 return (sys_kqueue(&k_args
));
72 /* Structure converting function from epoll to kevent. */
74 linux_epoll_to_kevent(int fd
, struct linux_epoll_event
*event
, struct kevent
*kevent
)
77 int flags
= kevent
->flags
;
79 if (event
->events
& LINUX_EPOLLIN
)
80 filter
|= EVFILT_READ
;
81 if (event
->events
& LINUX_EPOLLOUT
)
82 filter
|= EVFILT_WRITE
;
83 if (event
->events
& LINUX_EPOLLPRI
)
84 filter
|= EVFILT_READ
;
85 if (event
->events
& LINUX_EPOLLET
)
87 if (event
->events
& LINUX_EPOLLONESHOT
)
90 EV_SET(kevent
, fd
, filter
, flags
, 0, 0, NULL
);
94 * Structure converting function from kevent to epoll. In a case
95 * this is called on error in registration we store the error in
96 * event->data and pick it up later in linux_epoll_ctl().
99 linux_kevent_to_epoll(struct kevent
*kevent
, struct linux_epoll_event
*event
)
101 if (kevent
->flags
& EV_ERROR
) {
102 event
->data
= kevent
->data
;
105 switch (kevent
->filter
) {
107 if (kevent
->data
> 0)
108 event
->events
= LINUX_EPOLLIN
;
109 event
->data
= kevent
->ident
;
112 if (kevent
->data
> 0)
113 event
->events
= LINUX_EPOLLOUT
;
114 event
->data
= kevent
->ident
;
120 * Copyout callback used by kevent. This converts kevent
121 * events to epoll events and copies them back to the
122 * userspace. This is also called on error on registering
126 linux_kev_copyout(void *arg
, struct kevent
*kevp
, int count
, int *res
)
128 struct kevent_args
*uap
;
129 struct linux_epoll_event
*eep
;
132 uap
= (struct kevent_args
*) arg
;
134 eep
= kmalloc(sizeof(*eep
) * count
, M_TEMP
, M_WAITOK
| M_ZERO
);
136 for (i
= 0; i
< count
; i
++) {
137 linux_kevent_to_epoll(&kevp
[i
], &eep
[i
]);
140 error
= copyout(eep
, uap
->eventlist
, count
* sizeof(*eep
));
142 uap
->eventlist
= (struct kevent
*)((char *)uap
->eventlist
+ count
* sizeof(*eep
));
151 * Copyin callback used by kevent. This copies already
152 * converted filters to the kevent internal memory.
155 linux_kev_copyin(void *arg
, struct kevent
*kevp
, int maxevents
, int *events
)
157 struct kevent_args
*uap
;
159 uap
= (struct kevent_args
*) arg
;
161 memcpy(kevp
, uap
->changelist
, maxevents
* sizeof(*kevp
));
163 uap
->changelist
+= maxevents
;
170 * Load epoll filter, convert it to kevent filter
171 * and load it into kevent subsystem.
174 sys_linux_epoll_ctl(struct linux_epoll_ctl_args
*args
)
176 struct thread
*td
= curthread
;
177 struct proc
*p
= td
->td_proc
;
178 struct kevent_args k_args
;
181 struct linux_epoll_event le
;
182 struct file
*fp
= NULL
;
185 error
= copyin(args
->event
, &le
, sizeof(le
));
189 if (ldebug(epoll_ctl
))
190 kprintf(ARGS(epoll_ctl
,"%i, %i, %i, %u"), args
->epfd
, args
->op
,
191 args
->fd
, le
.events
);
193 k_args
.fd
= args
->epfd
;
194 k_args
.changelist
= &kev
;
195 /* The epoll can register only 1 filter at once. */
197 k_args
.eventlist
= NULL
;
199 k_args
.timeout
= NULL
;
202 case LINUX_EPOLL_CTL_ADD
:
203 kev
.flags
= EV_ADD
| EV_ENABLE
;
205 case LINUX_EPOLL_CTL_MOD
:
206 /* TODO: DELETE && ADD maybe? */
209 case LINUX_EPOLL_CTL_DEL
:
210 kev
.flags
= EV_DELETE
| EV_DISABLE
;
213 linux_epoll_to_kevent(args
->fd
, &le
, &kev
);
215 fp
= holdfp(p
->p_fd
, args
->epfd
, -1);
218 if (fp
->f_type
!= DTYPE_KQUEUE
) {
223 kq
= (struct kqueue
*)fp
->f_data
;
225 error
= kern_kevent(kq
, 0, &k_args
.sysmsg_result
, &k_args
,
226 linux_kev_copyin
, linux_kev_copyout
, NULL
, 0);
227 /* Check if there was an error during registration. */
228 if (error
== 0 && k_args
.sysmsg_result
!= 0) {
229 /* The copyout callback stored the error there. */
238 * Wait for a filter to be triggered on the epoll file descriptor. */
240 sys_linux_epoll_wait(struct linux_epoll_wait_args
*args
)
242 struct thread
*td
= curthread
;
243 struct proc
*p
= td
->td_proc
;
246 struct file
*fp
= NULL
;
247 struct kevent_args k_args
;
250 /* Convert from milliseconds to timespec. */
251 ts
.tv_sec
= args
->timeout
/ 1000;
252 ts
.tv_nsec
= (args
->timeout
% 1000) * 1000 * 1000;
254 k_args
.fd
= args
->epfd
;
255 k_args
.changelist
= NULL
;
258 * We don't mind the bogus type-cast because
259 * our copyout function knows about this and
260 * handles it correctly.
262 k_args
.eventlist
= (struct kevent
*)args
->events
;
263 k_args
.nevents
= args
->maxevents
;
264 k_args
.timeout
= &ts
;
266 fp
= holdfp(p
->p_fd
, args
->epfd
, -1);
269 if (fp
->f_type
!= DTYPE_KQUEUE
) {
274 kq
= (struct kqueue
*)fp
->f_data
;
276 error
= kern_kevent(kq
, args
->maxevents
, &args
->sysmsg_result
,
277 &k_args
, linux_kev_copyin
, linux_kev_copyout
, &ts
, 0);