b64d4891938155b52bf25f455e5a0e490f79c37f
[dragonfly.git] / sys / platform / vkernel / platform / kqueue.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/platform/vkernel/platform/kqueue.c,v 1.4 2007/06/17 16:46:17 dillon Exp $
35  */
36
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/systimer.h>
41 #include <sys/sysctl.h>
42 #include <sys/signal.h>
43 #include <sys/interrupt.h>
44 #include <sys/bus.h>
45 #include <sys/time.h>
46 #include <sys/event.h>
47 #include <sys/time.h>
48 #include <machine/cpu.h>
49 #include <machine/globaldata.h>
50 #include <machine/md_var.h>
51
52 #include <sys/thread2.h>
53
54 #include <unistd.h>
55 #include <signal.h>
56 #include <stdlib.h>
57 #include <fcntl.h>
58
59 struct kqueue_info {
60         void (*func)(void *, struct intrframe *);
61         void *data;
62         int fd;
63 };
64
65 int KQueueFd = -1;
66
67 /*
68  * Initialize kqueue based I/O
69  *
70  * Use SIGIO to get an immediate event when the kqueue has something waiting
71  * for us.  Setup the SIGIO signal as a mailbox signal for efficiency.
72  *
73  * Currently only read events are supported.
74  */
75 void
76 init_kqueue(void)
77 {
78         struct sigaction sa;
79
80         bzero(&sa, sizeof(sa));
81         sa.sa_mailbox = &mdcpu->gd_mailbox;
82         sa.sa_flags = SA_MAILBOX | SA_NODEFER;
83         sigemptyset(&sa.sa_mask);
84         sigaction(SIGIO, &sa, NULL);
85         KQueueFd = kqueue();
86         if (fcntl(KQueueFd, F_SETOWN, getpid()) < 0)
87                 panic("Cannot configure kqueue for SIGIO, update your kernel");
88         if (fcntl(KQueueFd, F_SETFL, O_ASYNC) < 0)
89                 panic("Cannot configure kqueue for SIGIO, update your kernel");
90 }
91
92 /*
93  * A SIGIO mailbox event cause a system call interruption and a timely
94  * poll.  If the mailbox is active we clean out all pending kqueue events.
95  * It is really that simple.
96  *
97  * We specify EV_CLEAR for all events to ensure no requeues before their
98  * time.  A new SIGIO is not generated unless all events are cleared from
99  * the kqueue.
100  */
101 void
102 signalmailbox(struct intrframe *frame)
103 {
104         struct mdglobaldata *gd = mdcpu;
105         struct timespec ts;
106         struct kevent kevary[8];
107         int n;
108         int i;
109
110         /*
111          * we only need to wake up our shutdown thread once.  
112          * Keep it non-zero so the shutdown thread can detect it.
113          */
114
115         if (mdcpu->gd_shutdown > 0) {
116                 mdcpu->gd_shutdown = -1;
117                 wakeup(&mdcpu->gd_shutdown);
118         }
119
120         if (gd->gd_mailbox == 0)
121                 return;
122         gd->gd_mailbox = 0;
123         ts.tv_sec = 0;
124         ts.tv_nsec = 0;
125         crit_enter();
126         do {
127                 n = kevent(KQueueFd, NULL, 0, kevary, 8, &ts);
128                 for (i = 0; i < n; ++i) {
129                         struct kevent *kev = &kevary[i];
130                         struct kqueue_info *info = (void *)kev->udata;
131
132                         info->func(info->data, frame);
133                 }
134         } while (n == 8);
135         crit_exit();
136 }
137
138 /*
139  * Generic I/O event support
140  */
141 struct kqueue_info *
142 kqueue_add(int fd, void (*func)(void *, struct intrframe *), void *data)
143 {
144         struct timespec ts = { 0, 0 };
145         struct kqueue_info *info;
146         struct kevent kev;
147
148         info = kmalloc(sizeof(*info), M_DEVBUF, M_ZERO|M_INTWAIT);
149         info->func = func;
150         info->data = data;
151         info->fd = fd;
152         EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE|EV_CLEAR, 0, 0, info);
153         if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0)
154                 panic("kqueue: kevent() call could not add descriptor");
155         return(info);
156 }
157
158 /*
159  * Medium resolution timer support
160  */
161 struct kqueue_info *
162 kqueue_add_timer(void (*func)(void *, struct intrframe *), void *data)
163 {
164         struct kqueue_info *info;
165
166         info = kmalloc(sizeof(*info), M_DEVBUF, M_ZERO|M_INTWAIT);
167         info->func = func;
168         info->data = data;
169         info->fd = (uintptr_t)info;
170         return(info);
171 }
172
173 void
174 kqueue_reload_timer(struct kqueue_info *info, int ms)
175 {
176         struct timespec ts = { 0, 0 };
177         struct kevent kev;
178
179         KKASSERT(ms > 0);
180
181         EV_SET(&kev, info->fd, EVFILT_TIMER,
182                 EV_ADD|EV_ENABLE|EV_ONESHOT|EV_CLEAR, 0, (uintptr_t)ms, info);
183         if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0)
184                 panic("kqueue_reload_timer: Failed");
185 }
186
187 /*
188  * Destroy a previously added kqueue event
189  */
190 void
191 kqueue_del(struct kqueue_info *info)
192 {
193         struct timespec ts = { 0, 0 };
194         struct kevent kev;
195
196         KKASSERT(info->fd >= 0);
197         EV_SET(&kev, info->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
198         if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0)
199                 panic("kevent: failed to delete descriptor %d", info->fd);
200         info->fd = -1;
201         kfree(info, M_DEVBUF);
202 }
203