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.6 2007/07/02 03:44:12 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 static void kqueuesig(int signo);
66 static void kqueue_intr(void *arg __unused, void *frame __unused);
67 
68 static int KQueueFd = -1;
69 static void *VIntr1;
70 
71 /*
72  * Initialize kqueue based I/O
73  *
74  * Use SIGIO to get an immediate event when the kqueue has something waiting
75  * for us.  Setup the SIGIO signal as a mailbox signal for efficiency.
76  *
77  * Currently only read events are supported.
78  */
79 void
80 init_kqueue(void)
81 {
82 	struct sigaction sa;
83 
84 	bzero(&sa, sizeof(sa));
85 	/*sa.sa_mailbox = &mdcpu->gd_mailbox;*/
86 	sa.sa_flags = SA_NODEFER;
87 	sa.sa_handler = kqueuesig;
88 	sigemptyset(&sa.sa_mask);
89 	sigaction(SIGIO, &sa, NULL);
90 	KQueueFd = kqueue();
91 	if (fcntl(KQueueFd, F_SETOWN, getpid()) < 0)
92 		panic("Cannot configure kqueue for SIGIO, update your kernel");
93 	if (fcntl(KQueueFd, F_SETFL, O_ASYNC) < 0)
94 		panic("Cannot configure kqueue for SIGIO, update your kernel");
95 }
96 
97 /*
98  * Signal handler dispatches interrupt thread.  Use interrupt #1
99  */
100 static void
101 kqueuesig(int signo)
102 {
103 	signalintr(1);
104 }
105 
106 /*
107  * Generic I/O event support
108  */
109 struct kqueue_info *
110 kqueue_add(int fd, void (*func)(void *, struct intrframe *), void *data)
111 {
112 	struct timespec ts = { 0, 0 };
113 	struct kqueue_info *info;
114 	struct kevent kev;
115 
116 	if (VIntr1 == NULL) {
117 		VIntr1 = register_int_virtual(1, kqueue_intr, NULL, "kqueue",
118 		    NULL, INTR_MPSAFE);
119 	}
120 
121 	info = kmalloc(sizeof(*info), M_DEVBUF, M_ZERO|M_INTWAIT);
122 	info->func = func;
123 	info->data = data;
124 	info->fd = fd;
125 	EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE|EV_CLEAR, 0, 0, info);
126 	if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0)
127 		panic("kqueue: kevent() call could not add descriptor");
128 	return(info);
129 }
130 
131 /*
132  * Medium resolution timer support
133  */
134 struct kqueue_info *
135 kqueue_add_timer(void (*func)(void *, struct intrframe *), void *data)
136 {
137 	struct kqueue_info *info;
138 
139 	if (VIntr1 == NULL) {
140 		VIntr1 = register_int_virtual(1, kqueue_intr, NULL, "kqueue",
141 		    NULL, INTR_MPSAFE);
142 	}
143 
144 	info = kmalloc(sizeof(*info), M_DEVBUF, M_ZERO|M_INTWAIT);
145 	info->func = func;
146 	info->data = data;
147 	info->fd = (uintptr_t)info;
148 	return(info);
149 }
150 
151 void
152 kqueue_reload_timer(struct kqueue_info *info, int ms)
153 {
154 	struct timespec ts = { 0, 0 };
155 	struct kevent kev;
156 
157 	KKASSERT(ms > 0);
158 
159 	EV_SET(&kev, info->fd, EVFILT_TIMER,
160 		EV_ADD|EV_ENABLE|EV_ONESHOT|EV_CLEAR, 0, (uintptr_t)ms, info);
161 	if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0)
162 		panic("kqueue_reload_timer: Failed");
163 }
164 
165 /*
166  * Destroy a previously added kqueue event
167  */
168 void
169 kqueue_del(struct kqueue_info *info)
170 {
171 	struct timespec ts = { 0, 0 };
172 	struct kevent kev;
173 
174 	KKASSERT(info->fd >= 0);
175 	EV_SET(&kev, info->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
176 	if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0)
177 		panic("kevent: failed to delete descriptor %d", info->fd);
178 	info->fd = -1;
179 	kfree(info, M_DEVBUF);
180 }
181 
182 /*
183  * Safely called via DragonFly's normal interrupt handling mechanism.
184  *
185  * Calleld with the MP lock held.  Note that this is still an interrupt
186  * thread context.
187  */
188 static
189 void
190 kqueue_intr(void *arg __unused, void *frame __unused)
191 {
192 	struct timespec ts;
193 	struct kevent kevary[8];
194 	int n;
195 	int i;
196 
197 	ts.tv_sec = 0;
198 	ts.tv_nsec = 0;
199 	do {
200 		n = kevent(KQueueFd, NULL, 0, kevary, 8, &ts);
201 		for (i = 0; i < n; ++i) {
202 			struct kevent *kev = &kevary[i];
203 			struct kqueue_info *info = (void *)kev->udata;
204 
205 			info->func(info->data, frame);
206 		}
207 	} while (n == 8);
208 }
209