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