sys_select.c revision 1.3.4.2 1 1.3.4.2 mjf /* $NetBSD: sys_select.c,v 1.3.4.2 2008/04/03 12:43:05 mjf Exp $ */
2 1.3.4.2 mjf
3 1.3.4.2 mjf /*-
4 1.3.4.2 mjf * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 1.3.4.2 mjf * All rights reserved.
6 1.3.4.2 mjf *
7 1.3.4.2 mjf * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 mjf * by Andrew Doran.
9 1.3.4.2 mjf *
10 1.3.4.2 mjf * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 mjf * modification, are permitted provided that the following conditions
12 1.3.4.2 mjf * are met:
13 1.3.4.2 mjf * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 mjf * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 mjf * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 mjf * documentation and/or other materials provided with the distribution.
18 1.3.4.2 mjf * 3. All advertising materials mentioning features or use of this software
19 1.3.4.2 mjf * must display the following acknowledgement:
20 1.3.4.2 mjf * This product includes software developed by the NetBSD
21 1.3.4.2 mjf * Foundation, Inc. and its contributors.
22 1.3.4.2 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.4.2 mjf * contributors may be used to endorse or promote products derived
24 1.3.4.2 mjf * from this software without specific prior written permission.
25 1.3.4.2 mjf *
26 1.3.4.2 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.4.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.4.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.4.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.4.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.4.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.4.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.4.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.4.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.4.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.4.2 mjf * POSSIBILITY OF SUCH DAMAGE.
37 1.3.4.2 mjf */
38 1.3.4.2 mjf
39 1.3.4.2 mjf /*
40 1.3.4.2 mjf * Copyright (c) 1982, 1986, 1989, 1993
41 1.3.4.2 mjf * The Regents of the University of California. All rights reserved.
42 1.3.4.2 mjf * (c) UNIX System Laboratories, Inc.
43 1.3.4.2 mjf * All or some portions of this file are derived from material licensed
44 1.3.4.2 mjf * to the University of California by American Telephone and Telegraph
45 1.3.4.2 mjf * Co. or Unix System Laboratories, Inc. and are reproduced herein with
46 1.3.4.2 mjf * the permission of UNIX System Laboratories, Inc.
47 1.3.4.2 mjf *
48 1.3.4.2 mjf * Redistribution and use in source and binary forms, with or without
49 1.3.4.2 mjf * modification, are permitted provided that the following conditions
50 1.3.4.2 mjf * are met:
51 1.3.4.2 mjf * 1. Redistributions of source code must retain the above copyright
52 1.3.4.2 mjf * notice, this list of conditions and the following disclaimer.
53 1.3.4.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
54 1.3.4.2 mjf * notice, this list of conditions and the following disclaimer in the
55 1.3.4.2 mjf * documentation and/or other materials provided with the distribution.
56 1.3.4.2 mjf * 3. Neither the name of the University nor the names of its contributors
57 1.3.4.2 mjf * may be used to endorse or promote products derived from this software
58 1.3.4.2 mjf * without specific prior written permission.
59 1.3.4.2 mjf *
60 1.3.4.2 mjf * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.3.4.2 mjf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.3.4.2 mjf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.3.4.2 mjf * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.3.4.2 mjf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.3.4.2 mjf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.3.4.2 mjf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.3.4.2 mjf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.3.4.2 mjf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.3.4.2 mjf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.3.4.2 mjf * SUCH DAMAGE.
71 1.3.4.2 mjf *
72 1.3.4.2 mjf * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
73 1.3.4.2 mjf */
74 1.3.4.2 mjf
75 1.3.4.2 mjf /*
76 1.3.4.2 mjf * System calls relating to files.
77 1.3.4.2 mjf */
78 1.3.4.2 mjf
79 1.3.4.2 mjf #include <sys/cdefs.h>
80 1.3.4.2 mjf __KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.3.4.2 2008/04/03 12:43:05 mjf Exp $");
81 1.3.4.2 mjf
82 1.3.4.2 mjf #include <sys/param.h>
83 1.3.4.2 mjf #include <sys/systm.h>
84 1.3.4.2 mjf #include <sys/filedesc.h>
85 1.3.4.2 mjf #include <sys/ioctl.h>
86 1.3.4.2 mjf #include <sys/file.h>
87 1.3.4.2 mjf #include <sys/proc.h>
88 1.3.4.2 mjf #include <sys/socketvar.h>
89 1.3.4.2 mjf #include <sys/signalvar.h>
90 1.3.4.2 mjf #include <sys/uio.h>
91 1.3.4.2 mjf #include <sys/kernel.h>
92 1.3.4.2 mjf #include <sys/stat.h>
93 1.3.4.2 mjf #include <sys/poll.h>
94 1.3.4.2 mjf #include <sys/vnode.h>
95 1.3.4.2 mjf #include <sys/mount.h>
96 1.3.4.2 mjf #include <sys/syscallargs.h>
97 1.3.4.2 mjf #include <sys/cpu.h>
98 1.3.4.2 mjf #include <sys/atomic.h>
99 1.3.4.2 mjf #include <sys/socketvar.h>
100 1.3.4.2 mjf #include <sys/sleepq.h>
101 1.3.4.2 mjf
102 1.3.4.2 mjf /* Flags for lwp::l_selflag. */
103 1.3.4.2 mjf #define SEL_RESET 0 /* awoken, interrupted, or not yet polling */
104 1.3.4.2 mjf #define SEL_SCANNING 1 /* polling descriptors */
105 1.3.4.2 mjf #define SEL_BLOCKING 2 /* about to block on select_cv */
106 1.3.4.2 mjf
107 1.3.4.2 mjf /* Per-CPU state for select()/poll(). */
108 1.3.4.2 mjf #if MAXCPUS > 32
109 1.3.4.2 mjf #error adjust this code
110 1.3.4.2 mjf #endif
111 1.3.4.2 mjf typedef struct selcpu {
112 1.3.4.2 mjf kmutex_t sc_lock;
113 1.3.4.2 mjf sleepq_t sc_sleepq;
114 1.3.4.2 mjf int sc_ncoll;
115 1.3.4.2 mjf uint32_t sc_mask;
116 1.3.4.2 mjf } selcpu_t;
117 1.3.4.2 mjf
118 1.3.4.2 mjf static int selscan(lwp_t *, fd_mask *, fd_mask *, int, register_t *);
119 1.3.4.2 mjf static int pollscan(lwp_t *, struct pollfd *, int, register_t *);
120 1.3.4.2 mjf static void selclear(void);
121 1.3.4.2 mjf
122 1.3.4.2 mjf static syncobj_t select_sobj = {
123 1.3.4.2 mjf SOBJ_SLEEPQ_FIFO,
124 1.3.4.2 mjf sleepq_unsleep,
125 1.3.4.2 mjf sleepq_changepri,
126 1.3.4.2 mjf sleepq_lendpri,
127 1.3.4.2 mjf syncobj_noowner,
128 1.3.4.2 mjf };
129 1.3.4.2 mjf
130 1.3.4.2 mjf /*
131 1.3.4.2 mjf * Select system call.
132 1.3.4.2 mjf */
133 1.3.4.2 mjf int
134 1.3.4.2 mjf sys_pselect(struct lwp *l, const struct sys_pselect_args *uap, register_t *retval)
135 1.3.4.2 mjf {
136 1.3.4.2 mjf /* {
137 1.3.4.2 mjf syscallarg(int) nd;
138 1.3.4.2 mjf syscallarg(fd_set *) in;
139 1.3.4.2 mjf syscallarg(fd_set *) ou;
140 1.3.4.2 mjf syscallarg(fd_set *) ex;
141 1.3.4.2 mjf syscallarg(const struct timespec *) ts;
142 1.3.4.2 mjf syscallarg(sigset_t *) mask;
143 1.3.4.2 mjf } */
144 1.3.4.2 mjf struct timespec ats;
145 1.3.4.2 mjf struct timeval atv, *tv = NULL;
146 1.3.4.2 mjf sigset_t amask, *mask = NULL;
147 1.3.4.2 mjf int error;
148 1.3.4.2 mjf
149 1.3.4.2 mjf if (SCARG(uap, ts)) {
150 1.3.4.2 mjf error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
151 1.3.4.2 mjf if (error)
152 1.3.4.2 mjf return error;
153 1.3.4.2 mjf atv.tv_sec = ats.tv_sec;
154 1.3.4.2 mjf atv.tv_usec = ats.tv_nsec / 1000;
155 1.3.4.2 mjf tv = &atv;
156 1.3.4.2 mjf }
157 1.3.4.2 mjf if (SCARG(uap, mask) != NULL) {
158 1.3.4.2 mjf error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
159 1.3.4.2 mjf if (error)
160 1.3.4.2 mjf return error;
161 1.3.4.2 mjf mask = &amask;
162 1.3.4.2 mjf }
163 1.3.4.2 mjf
164 1.3.4.2 mjf return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
165 1.3.4.2 mjf SCARG(uap, ou), SCARG(uap, ex), tv, mask);
166 1.3.4.2 mjf }
167 1.3.4.2 mjf
168 1.3.4.2 mjf int
169 1.3.4.2 mjf inittimeleft(struct timeval *tv, struct timeval *sleeptv)
170 1.3.4.2 mjf {
171 1.3.4.2 mjf if (itimerfix(tv))
172 1.3.4.2 mjf return -1;
173 1.3.4.2 mjf getmicrouptime(sleeptv);
174 1.3.4.2 mjf return 0;
175 1.3.4.2 mjf }
176 1.3.4.2 mjf
177 1.3.4.2 mjf int
178 1.3.4.2 mjf gettimeleft(struct timeval *tv, struct timeval *sleeptv)
179 1.3.4.2 mjf {
180 1.3.4.2 mjf /*
181 1.3.4.2 mjf * We have to recalculate the timeout on every retry.
182 1.3.4.2 mjf */
183 1.3.4.2 mjf struct timeval slepttv;
184 1.3.4.2 mjf /*
185 1.3.4.2 mjf * reduce tv by elapsed time
186 1.3.4.2 mjf * based on monotonic time scale
187 1.3.4.2 mjf */
188 1.3.4.2 mjf getmicrouptime(&slepttv);
189 1.3.4.2 mjf timeradd(tv, sleeptv, tv);
190 1.3.4.2 mjf timersub(tv, &slepttv, tv);
191 1.3.4.2 mjf *sleeptv = slepttv;
192 1.3.4.2 mjf return tvtohz(tv);
193 1.3.4.2 mjf }
194 1.3.4.2 mjf
195 1.3.4.2 mjf int
196 1.3.4.2 mjf sys_select(struct lwp *l, const struct sys_select_args *uap, register_t *retval)
197 1.3.4.2 mjf {
198 1.3.4.2 mjf /* {
199 1.3.4.2 mjf syscallarg(int) nd;
200 1.3.4.2 mjf syscallarg(fd_set *) in;
201 1.3.4.2 mjf syscallarg(fd_set *) ou;
202 1.3.4.2 mjf syscallarg(fd_set *) ex;
203 1.3.4.2 mjf syscallarg(struct timeval *) tv;
204 1.3.4.2 mjf } */
205 1.3.4.2 mjf struct timeval atv, *tv = NULL;
206 1.3.4.2 mjf int error;
207 1.3.4.2 mjf
208 1.3.4.2 mjf if (SCARG(uap, tv)) {
209 1.3.4.2 mjf error = copyin(SCARG(uap, tv), (void *)&atv,
210 1.3.4.2 mjf sizeof(atv));
211 1.3.4.2 mjf if (error)
212 1.3.4.2 mjf return error;
213 1.3.4.2 mjf tv = &atv;
214 1.3.4.2 mjf }
215 1.3.4.2 mjf
216 1.3.4.2 mjf return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
217 1.3.4.2 mjf SCARG(uap, ou), SCARG(uap, ex), tv, NULL);
218 1.3.4.2 mjf }
219 1.3.4.2 mjf
220 1.3.4.2 mjf int
221 1.3.4.2 mjf selcommon(lwp_t *l, register_t *retval, int nd, fd_set *u_in,
222 1.3.4.2 mjf fd_set *u_ou, fd_set *u_ex, struct timeval *tv, sigset_t *mask)
223 1.3.4.2 mjf {
224 1.3.4.2 mjf char smallbits[howmany(FD_SETSIZE, NFDBITS) *
225 1.3.4.2 mjf sizeof(fd_mask) * 6];
226 1.3.4.2 mjf proc_t * const p = l->l_proc;
227 1.3.4.2 mjf char *bits;
228 1.3.4.2 mjf int ncoll, error, timo;
229 1.3.4.2 mjf size_t ni;
230 1.3.4.2 mjf sigset_t oldmask;
231 1.3.4.2 mjf struct timeval sleeptv;
232 1.3.4.2 mjf selcpu_t *sc;
233 1.3.4.2 mjf
234 1.3.4.2 mjf error = 0;
235 1.3.4.2 mjf if (nd < 0)
236 1.3.4.2 mjf return (EINVAL);
237 1.3.4.2 mjf if (nd > p->p_fd->fd_nfiles) {
238 1.3.4.2 mjf /* forgiving; slightly wrong */
239 1.3.4.2 mjf nd = p->p_fd->fd_nfiles;
240 1.3.4.2 mjf }
241 1.3.4.2 mjf ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
242 1.3.4.2 mjf if (ni * 6 > sizeof(smallbits))
243 1.3.4.2 mjf bits = kmem_alloc(ni * 6, KM_SLEEP);
244 1.3.4.2 mjf else
245 1.3.4.2 mjf bits = smallbits;
246 1.3.4.2 mjf
247 1.3.4.2 mjf #define getbits(name, x) \
248 1.3.4.2 mjf if (u_ ## name) { \
249 1.3.4.2 mjf error = copyin(u_ ## name, bits + ni * x, ni); \
250 1.3.4.2 mjf if (error) \
251 1.3.4.2 mjf goto done; \
252 1.3.4.2 mjf } else \
253 1.3.4.2 mjf memset(bits + ni * x, 0, ni);
254 1.3.4.2 mjf getbits(in, 0);
255 1.3.4.2 mjf getbits(ou, 1);
256 1.3.4.2 mjf getbits(ex, 2);
257 1.3.4.2 mjf #undef getbits
258 1.3.4.2 mjf
259 1.3.4.2 mjf timo = 0;
260 1.3.4.2 mjf if (tv && inittimeleft(tv, &sleeptv) == -1) {
261 1.3.4.2 mjf error = EINVAL;
262 1.3.4.2 mjf goto done;
263 1.3.4.2 mjf }
264 1.3.4.2 mjf
265 1.3.4.2 mjf if (mask) {
266 1.3.4.2 mjf sigminusset(&sigcantmask, mask);
267 1.3.4.2 mjf mutex_enter(&p->p_smutex);
268 1.3.4.2 mjf oldmask = l->l_sigmask;
269 1.3.4.2 mjf l->l_sigmask = *mask;
270 1.3.4.2 mjf mutex_exit(&p->p_smutex);
271 1.3.4.2 mjf } else
272 1.3.4.2 mjf oldmask = l->l_sigmask; /* XXXgcc */
273 1.3.4.2 mjf
274 1.3.4.2 mjf sc = curcpu()->ci_data.cpu_selcpu;
275 1.3.4.2 mjf l->l_selcpu = sc;
276 1.3.4.2 mjf SLIST_INIT(&l->l_selwait);
277 1.3.4.2 mjf for (;;) {
278 1.3.4.2 mjf /*
279 1.3.4.2 mjf * No need to lock. If this is overwritten by another
280 1.3.4.2 mjf * value while scanning, we will retry below. We only
281 1.3.4.2 mjf * need to see exact state from the descriptors that
282 1.3.4.2 mjf * we are about to poll, and lock activity resulting
283 1.3.4.2 mjf * from fo_poll is enough to provide an up to date value
284 1.3.4.2 mjf * for new polling activity.
285 1.3.4.2 mjf */
286 1.3.4.2 mjf l->l_selflag = SEL_SCANNING;
287 1.3.4.2 mjf ncoll = sc->sc_ncoll;
288 1.3.4.2 mjf
289 1.3.4.2 mjf error = selscan(l, (fd_mask *)(bits + ni * 0),
290 1.3.4.2 mjf (fd_mask *)(bits + ni * 3), nd, retval);
291 1.3.4.2 mjf
292 1.3.4.2 mjf if (error || *retval)
293 1.3.4.2 mjf break;
294 1.3.4.2 mjf if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
295 1.3.4.2 mjf break;
296 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
297 1.3.4.2 mjf if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
298 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
299 1.3.4.2 mjf continue;
300 1.3.4.2 mjf }
301 1.3.4.2 mjf l->l_selflag = SEL_BLOCKING;
302 1.3.4.2 mjf lwp_lock(l);
303 1.3.4.2 mjf lwp_unlock_to(l, &sc->sc_lock);
304 1.3.4.2 mjf sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
305 1.3.4.2 mjf KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); /* XXX */
306 1.3.4.2 mjf error = sleepq_block(timo, true);
307 1.3.4.2 mjf if (error != 0)
308 1.3.4.2 mjf break;
309 1.3.4.2 mjf }
310 1.3.4.2 mjf selclear();
311 1.3.4.2 mjf
312 1.3.4.2 mjf if (mask) {
313 1.3.4.2 mjf mutex_enter(&p->p_smutex);
314 1.3.4.2 mjf l->l_sigmask = oldmask;
315 1.3.4.2 mjf mutex_exit(&p->p_smutex);
316 1.3.4.2 mjf }
317 1.3.4.2 mjf
318 1.3.4.2 mjf done:
319 1.3.4.2 mjf /* select is not restarted after signals... */
320 1.3.4.2 mjf if (error == ERESTART)
321 1.3.4.2 mjf error = EINTR;
322 1.3.4.2 mjf if (error == EWOULDBLOCK)
323 1.3.4.2 mjf error = 0;
324 1.3.4.2 mjf if (error == 0 && u_in != NULL)
325 1.3.4.2 mjf error = copyout(bits + ni * 3, u_in, ni);
326 1.3.4.2 mjf if (error == 0 && u_ou != NULL)
327 1.3.4.2 mjf error = copyout(bits + ni * 4, u_ou, ni);
328 1.3.4.2 mjf if (error == 0 && u_ex != NULL)
329 1.3.4.2 mjf error = copyout(bits + ni * 5, u_ex, ni);
330 1.3.4.2 mjf if (bits != smallbits)
331 1.3.4.2 mjf kmem_free(bits, ni * 6);
332 1.3.4.2 mjf return (error);
333 1.3.4.2 mjf }
334 1.3.4.2 mjf
335 1.3.4.2 mjf int
336 1.3.4.2 mjf selscan(lwp_t *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
337 1.3.4.2 mjf register_t *retval)
338 1.3.4.2 mjf {
339 1.3.4.2 mjf static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
340 1.3.4.2 mjf POLLWRNORM | POLLHUP | POLLERR,
341 1.3.4.2 mjf POLLRDBAND };
342 1.3.4.2 mjf int msk, i, j, fd, n;
343 1.3.4.2 mjf fd_mask ibits, obits;
344 1.3.4.2 mjf file_t *fp;
345 1.3.4.2 mjf
346 1.3.4.2 mjf n = 0;
347 1.3.4.2 mjf for (msk = 0; msk < 3; msk++) {
348 1.3.4.2 mjf for (i = 0; i < nfd; i += NFDBITS) {
349 1.3.4.2 mjf ibits = *ibitp++;
350 1.3.4.2 mjf obits = 0;
351 1.3.4.2 mjf while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
352 1.3.4.2 mjf ibits &= ~(1 << j);
353 1.3.4.2 mjf if ((fp = fd_getfile(fd)) == NULL)
354 1.3.4.2 mjf return (EBADF);
355 1.3.4.2 mjf if ((*fp->f_ops->fo_poll)(fp, flag[msk])) {
356 1.3.4.2 mjf obits |= (1 << j);
357 1.3.4.2 mjf n++;
358 1.3.4.2 mjf }
359 1.3.4.2 mjf fd_putfile(fd);
360 1.3.4.2 mjf }
361 1.3.4.2 mjf *obitp++ = obits;
362 1.3.4.2 mjf }
363 1.3.4.2 mjf }
364 1.3.4.2 mjf *retval = n;
365 1.3.4.2 mjf return (0);
366 1.3.4.2 mjf }
367 1.3.4.2 mjf
368 1.3.4.2 mjf /*
369 1.3.4.2 mjf * Poll system call.
370 1.3.4.2 mjf */
371 1.3.4.2 mjf int
372 1.3.4.2 mjf sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
373 1.3.4.2 mjf {
374 1.3.4.2 mjf /* {
375 1.3.4.2 mjf syscallarg(struct pollfd *) fds;
376 1.3.4.2 mjf syscallarg(u_int) nfds;
377 1.3.4.2 mjf syscallarg(int) timeout;
378 1.3.4.2 mjf } */
379 1.3.4.2 mjf struct timeval atv, *tv = NULL;
380 1.3.4.2 mjf
381 1.3.4.2 mjf if (SCARG(uap, timeout) != INFTIM) {
382 1.3.4.2 mjf atv.tv_sec = SCARG(uap, timeout) / 1000;
383 1.3.4.2 mjf atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
384 1.3.4.2 mjf tv = &atv;
385 1.3.4.2 mjf }
386 1.3.4.2 mjf
387 1.3.4.2 mjf return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
388 1.3.4.2 mjf tv, NULL);
389 1.3.4.2 mjf }
390 1.3.4.2 mjf
391 1.3.4.2 mjf /*
392 1.3.4.2 mjf * Poll system call.
393 1.3.4.2 mjf */
394 1.3.4.2 mjf int
395 1.3.4.2 mjf sys_pollts(struct lwp *l, const struct sys_pollts_args *uap, register_t *retval)
396 1.3.4.2 mjf {
397 1.3.4.2 mjf /* {
398 1.3.4.2 mjf syscallarg(struct pollfd *) fds;
399 1.3.4.2 mjf syscallarg(u_int) nfds;
400 1.3.4.2 mjf syscallarg(const struct timespec *) ts;
401 1.3.4.2 mjf syscallarg(const sigset_t *) mask;
402 1.3.4.2 mjf } */
403 1.3.4.2 mjf struct timespec ats;
404 1.3.4.2 mjf struct timeval atv, *tv = NULL;
405 1.3.4.2 mjf sigset_t amask, *mask = NULL;
406 1.3.4.2 mjf int error;
407 1.3.4.2 mjf
408 1.3.4.2 mjf if (SCARG(uap, ts)) {
409 1.3.4.2 mjf error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
410 1.3.4.2 mjf if (error)
411 1.3.4.2 mjf return error;
412 1.3.4.2 mjf atv.tv_sec = ats.tv_sec;
413 1.3.4.2 mjf atv.tv_usec = ats.tv_nsec / 1000;
414 1.3.4.2 mjf tv = &atv;
415 1.3.4.2 mjf }
416 1.3.4.2 mjf if (SCARG(uap, mask)) {
417 1.3.4.2 mjf error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
418 1.3.4.2 mjf if (error)
419 1.3.4.2 mjf return error;
420 1.3.4.2 mjf mask = &amask;
421 1.3.4.2 mjf }
422 1.3.4.2 mjf
423 1.3.4.2 mjf return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
424 1.3.4.2 mjf tv, mask);
425 1.3.4.2 mjf }
426 1.3.4.2 mjf
427 1.3.4.2 mjf int
428 1.3.4.2 mjf pollcommon(lwp_t *l, register_t *retval,
429 1.3.4.2 mjf struct pollfd *u_fds, u_int nfds,
430 1.3.4.2 mjf struct timeval *tv, sigset_t *mask)
431 1.3.4.2 mjf {
432 1.3.4.2 mjf char smallbits[32 * sizeof(struct pollfd)];
433 1.3.4.2 mjf proc_t * const p = l->l_proc;
434 1.3.4.2 mjf void * bits;
435 1.3.4.2 mjf sigset_t oldmask;
436 1.3.4.2 mjf int ncoll, error, timo;
437 1.3.4.2 mjf size_t ni;
438 1.3.4.2 mjf struct timeval sleeptv;
439 1.3.4.2 mjf selcpu_t *sc;
440 1.3.4.2 mjf
441 1.3.4.2 mjf if (nfds > p->p_fd->fd_nfiles) {
442 1.3.4.2 mjf /* forgiving; slightly wrong */
443 1.3.4.2 mjf nfds = p->p_fd->fd_nfiles;
444 1.3.4.2 mjf }
445 1.3.4.2 mjf ni = nfds * sizeof(struct pollfd);
446 1.3.4.2 mjf if (ni > sizeof(smallbits))
447 1.3.4.2 mjf bits = kmem_alloc(ni, KM_SLEEP);
448 1.3.4.2 mjf else
449 1.3.4.2 mjf bits = smallbits;
450 1.3.4.2 mjf
451 1.3.4.2 mjf error = copyin(u_fds, bits, ni);
452 1.3.4.2 mjf if (error)
453 1.3.4.2 mjf goto done;
454 1.3.4.2 mjf
455 1.3.4.2 mjf timo = 0;
456 1.3.4.2 mjf if (tv && inittimeleft(tv, &sleeptv) == -1) {
457 1.3.4.2 mjf error = EINVAL;
458 1.3.4.2 mjf goto done;
459 1.3.4.2 mjf }
460 1.3.4.2 mjf
461 1.3.4.2 mjf if (mask) {
462 1.3.4.2 mjf sigminusset(&sigcantmask, mask);
463 1.3.4.2 mjf mutex_enter(&p->p_smutex);
464 1.3.4.2 mjf oldmask = l->l_sigmask;
465 1.3.4.2 mjf l->l_sigmask = *mask;
466 1.3.4.2 mjf mutex_exit(&p->p_smutex);
467 1.3.4.2 mjf } else
468 1.3.4.2 mjf oldmask = l->l_sigmask; /* XXXgcc */
469 1.3.4.2 mjf
470 1.3.4.2 mjf sc = curcpu()->ci_data.cpu_selcpu;
471 1.3.4.2 mjf l->l_selcpu = sc;
472 1.3.4.2 mjf SLIST_INIT(&l->l_selwait);
473 1.3.4.2 mjf for (;;) {
474 1.3.4.2 mjf /*
475 1.3.4.2 mjf * No need to lock. If this is overwritten by another
476 1.3.4.2 mjf * value while scanning, we will retry below. We only
477 1.3.4.2 mjf * need to see exact state from the descriptors that
478 1.3.4.2 mjf * we are about to poll, and lock activity resulting
479 1.3.4.2 mjf * from fo_poll is enough to provide an up to date value
480 1.3.4.2 mjf * for new polling activity.
481 1.3.4.2 mjf */
482 1.3.4.2 mjf ncoll = sc->sc_ncoll;
483 1.3.4.2 mjf l->l_selflag = SEL_SCANNING;
484 1.3.4.2 mjf
485 1.3.4.2 mjf error = pollscan(l, (struct pollfd *)bits, nfds, retval);
486 1.3.4.2 mjf
487 1.3.4.2 mjf if (error || *retval)
488 1.3.4.2 mjf break;
489 1.3.4.2 mjf if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
490 1.3.4.2 mjf break;
491 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
492 1.3.4.2 mjf if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
493 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
494 1.3.4.2 mjf continue;
495 1.3.4.2 mjf }
496 1.3.4.2 mjf l->l_selflag = SEL_BLOCKING;
497 1.3.4.2 mjf lwp_lock(l);
498 1.3.4.2 mjf lwp_unlock_to(l, &sc->sc_lock);
499 1.3.4.2 mjf sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
500 1.3.4.2 mjf KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); /* XXX */
501 1.3.4.2 mjf error = sleepq_block(timo, true);
502 1.3.4.2 mjf if (error != 0)
503 1.3.4.2 mjf break;
504 1.3.4.2 mjf }
505 1.3.4.2 mjf selclear();
506 1.3.4.2 mjf
507 1.3.4.2 mjf if (mask) {
508 1.3.4.2 mjf mutex_enter(&p->p_smutex);
509 1.3.4.2 mjf l->l_sigmask = oldmask;
510 1.3.4.2 mjf mutex_exit(&p->p_smutex);
511 1.3.4.2 mjf }
512 1.3.4.2 mjf done:
513 1.3.4.2 mjf /* poll is not restarted after signals... */
514 1.3.4.2 mjf if (error == ERESTART)
515 1.3.4.2 mjf error = EINTR;
516 1.3.4.2 mjf if (error == EWOULDBLOCK)
517 1.3.4.2 mjf error = 0;
518 1.3.4.2 mjf if (error == 0)
519 1.3.4.2 mjf error = copyout(bits, u_fds, ni);
520 1.3.4.2 mjf if (bits != smallbits)
521 1.3.4.2 mjf kmem_free(bits, ni);
522 1.3.4.2 mjf return (error);
523 1.3.4.2 mjf }
524 1.3.4.2 mjf
525 1.3.4.2 mjf int
526 1.3.4.2 mjf pollscan(lwp_t *l, struct pollfd *fds, int nfd, register_t *retval)
527 1.3.4.2 mjf {
528 1.3.4.2 mjf int i, n;
529 1.3.4.2 mjf file_t *fp;
530 1.3.4.2 mjf
531 1.3.4.2 mjf n = 0;
532 1.3.4.2 mjf for (i = 0; i < nfd; i++, fds++) {
533 1.3.4.2 mjf if (fds->fd < 0) {
534 1.3.4.2 mjf fds->revents = 0;
535 1.3.4.2 mjf } else if ((fp = fd_getfile(fds->fd)) == NULL) {
536 1.3.4.2 mjf fds->revents = POLLNVAL;
537 1.3.4.2 mjf n++;
538 1.3.4.2 mjf } else {
539 1.3.4.2 mjf fds->revents = (*fp->f_ops->fo_poll)(fp,
540 1.3.4.2 mjf fds->events | POLLERR | POLLHUP);
541 1.3.4.2 mjf if (fds->revents != 0)
542 1.3.4.2 mjf n++;
543 1.3.4.2 mjf fd_putfile(fds->fd);
544 1.3.4.2 mjf }
545 1.3.4.2 mjf }
546 1.3.4.2 mjf *retval = n;
547 1.3.4.2 mjf return (0);
548 1.3.4.2 mjf }
549 1.3.4.2 mjf
550 1.3.4.2 mjf /*ARGSUSED*/
551 1.3.4.2 mjf int
552 1.3.4.2 mjf seltrue(dev_t dev, int events, lwp_t *l)
553 1.3.4.2 mjf {
554 1.3.4.2 mjf
555 1.3.4.2 mjf return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
556 1.3.4.2 mjf }
557 1.3.4.2 mjf
558 1.3.4.2 mjf /*
559 1.3.4.2 mjf * Record a select request. Concurrency issues:
560 1.3.4.2 mjf *
561 1.3.4.2 mjf * The caller holds the same lock across calls to selrecord() and
562 1.3.4.2 mjf * selwakeup(), so we don't need to consider a concurrent wakeup
563 1.3.4.2 mjf * while in this routine.
564 1.3.4.2 mjf *
565 1.3.4.2 mjf * The only activity we need to guard against is selclear(), called by
566 1.3.4.2 mjf * another thread that is exiting selcommon() or pollcommon().
567 1.3.4.2 mjf * `sel_lwp' can only become non-NULL while the caller's lock is held,
568 1.3.4.2 mjf * so it cannot become non-NULL due to a change made by another thread
569 1.3.4.2 mjf * while we are in this routine. It can only become _NULL_ due to a
570 1.3.4.2 mjf * call to selclear().
571 1.3.4.2 mjf *
572 1.3.4.2 mjf * If it is non-NULL and != selector there is the potential for
573 1.3.4.2 mjf * selclear() to be called by another thread. If either of those
574 1.3.4.2 mjf * conditions are true, we're not interested in touching the `named
575 1.3.4.2 mjf * waiter' part of the selinfo record because we need to record a
576 1.3.4.2 mjf * collision. Hence there is no need for additional locking in this
577 1.3.4.2 mjf * routine.
578 1.3.4.2 mjf */
579 1.3.4.2 mjf void
580 1.3.4.2 mjf selrecord(lwp_t *selector, struct selinfo *sip)
581 1.3.4.2 mjf {
582 1.3.4.2 mjf selcpu_t *sc;
583 1.3.4.2 mjf lwp_t *other;
584 1.3.4.2 mjf
585 1.3.4.2 mjf KASSERT(selector == curlwp);
586 1.3.4.2 mjf
587 1.3.4.2 mjf sc = selector->l_selcpu;
588 1.3.4.2 mjf other = sip->sel_lwp;
589 1.3.4.2 mjf
590 1.3.4.2 mjf if (other == selector) {
591 1.3.4.2 mjf /* `selector' has already claimed it. */
592 1.3.4.2 mjf KASSERT(sip->sel_cpu = sc);
593 1.3.4.2 mjf } else if (other == NULL) {
594 1.3.4.2 mjf /*
595 1.3.4.2 mjf * First named waiter, although there may be unnamed
596 1.3.4.2 mjf * waiters (collisions). Issue a memory barrier to
597 1.3.4.2 mjf * ensure that we access sel_lwp (above) before other
598 1.3.4.2 mjf * fields - this guards against a call to selclear().
599 1.3.4.2 mjf */
600 1.3.4.2 mjf membar_enter();
601 1.3.4.2 mjf sip->sel_lwp = selector;
602 1.3.4.2 mjf SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
603 1.3.4.2 mjf /* Replace selinfo's lock with our chosen CPU's lock. */
604 1.3.4.2 mjf sip->sel_cpu = sc;
605 1.3.4.2 mjf } else {
606 1.3.4.2 mjf /* Multiple waiters: record a collision. */
607 1.3.4.2 mjf sip->sel_collision |= sc->sc_mask;
608 1.3.4.2 mjf KASSERT(sip->sel_cpu != NULL);
609 1.3.4.2 mjf }
610 1.3.4.2 mjf }
611 1.3.4.2 mjf
612 1.3.4.2 mjf /*
613 1.3.4.2 mjf * Do a wakeup when a selectable event occurs. Concurrency issues:
614 1.3.4.2 mjf *
615 1.3.4.2 mjf * As per selrecord(), the caller's object lock is held. If there
616 1.3.4.2 mjf * is a named waiter, we must acquire the associated selcpu's lock
617 1.3.4.2 mjf * in order to synchronize with selclear() and pollers going to sleep
618 1.3.4.2 mjf * in selcommon() and/or pollcommon().
619 1.3.4.2 mjf *
620 1.3.4.2 mjf * sip->sel_cpu cannot change at this point, as it is only changed
621 1.3.4.2 mjf * in selrecord(), and concurrent calls to selrecord() are locked
622 1.3.4.2 mjf * out by the caller.
623 1.3.4.2 mjf */
624 1.3.4.2 mjf void
625 1.3.4.2 mjf selnotify(struct selinfo *sip, int events, long knhint)
626 1.3.4.2 mjf {
627 1.3.4.2 mjf selcpu_t *sc;
628 1.3.4.2 mjf uint32_t mask;
629 1.3.4.2 mjf int index, oflag, swapin;
630 1.3.4.2 mjf lwp_t *l;
631 1.3.4.2 mjf
632 1.3.4.2 mjf KNOTE(&sip->sel_klist, knhint);
633 1.3.4.2 mjf
634 1.3.4.2 mjf if (sip->sel_lwp != NULL) {
635 1.3.4.2 mjf /* One named LWP is waiting. */
636 1.3.4.2 mjf swapin = 0;
637 1.3.4.2 mjf sc = sip->sel_cpu;
638 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
639 1.3.4.2 mjf /* Still there? */
640 1.3.4.2 mjf if (sip->sel_lwp != NULL) {
641 1.3.4.2 mjf l = sip->sel_lwp;
642 1.3.4.2 mjf /*
643 1.3.4.2 mjf * If thread is sleeping, wake it up. If it's not
644 1.3.4.2 mjf * yet asleep, it will notice the change in state
645 1.3.4.2 mjf * and will re-poll the descriptors.
646 1.3.4.2 mjf */
647 1.3.4.2 mjf oflag = l->l_selflag;
648 1.3.4.2 mjf l->l_selflag = SEL_RESET;
649 1.3.4.2 mjf if (oflag == SEL_BLOCKING &&
650 1.3.4.2 mjf l->l_mutex == &sc->sc_lock) {
651 1.3.4.2 mjf KASSERT(l->l_wchan == sc);
652 1.3.4.2 mjf swapin = sleepq_unsleep(l, false);
653 1.3.4.2 mjf }
654 1.3.4.2 mjf }
655 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
656 1.3.4.2 mjf if (swapin)
657 1.3.4.2 mjf uvm_kick_scheduler();
658 1.3.4.2 mjf }
659 1.3.4.2 mjf
660 1.3.4.2 mjf if ((mask = sip->sel_collision) != 0) {
661 1.3.4.2 mjf /*
662 1.3.4.2 mjf * There was a collision (multiple waiters): we must
663 1.3.4.2 mjf * inform all potentially interested waiters.
664 1.3.4.2 mjf */
665 1.3.4.2 mjf sip->sel_collision = 0;
666 1.3.4.2 mjf do {
667 1.3.4.2 mjf index = ffs(mask) - 1;
668 1.3.4.2 mjf mask &= ~(1 << index);
669 1.3.4.2 mjf sc = cpu_lookup_byindex(index)->ci_data.cpu_selcpu;
670 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
671 1.3.4.2 mjf sc->sc_ncoll++;
672 1.3.4.2 mjf sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1);
673 1.3.4.2 mjf } while (__predict_false(mask != 0));
674 1.3.4.2 mjf }
675 1.3.4.2 mjf }
676 1.3.4.2 mjf
677 1.3.4.2 mjf /*
678 1.3.4.2 mjf * Remove an LWP from all objects that it is waiting for. Concurrency
679 1.3.4.2 mjf * issues:
680 1.3.4.2 mjf *
681 1.3.4.2 mjf * The object owner's (e.g. device driver) lock is not held here. Calls
682 1.3.4.2 mjf * can be made to selrecord() and we do not synchronize against those
683 1.3.4.2 mjf * directly using locks. However, we use `sel_lwp' to lock out changes.
684 1.3.4.2 mjf * Before clearing it we must use memory barriers to ensure that we can
685 1.3.4.2 mjf * safely traverse the list of selinfo records.
686 1.3.4.2 mjf */
687 1.3.4.2 mjf static void
688 1.3.4.2 mjf selclear(void)
689 1.3.4.2 mjf {
690 1.3.4.2 mjf struct selinfo *sip, *next;
691 1.3.4.2 mjf selcpu_t *sc;
692 1.3.4.2 mjf lwp_t *l;
693 1.3.4.2 mjf
694 1.3.4.2 mjf l = curlwp;
695 1.3.4.2 mjf sc = l->l_selcpu;
696 1.3.4.2 mjf
697 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
698 1.3.4.2 mjf for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
699 1.3.4.2 mjf KASSERT(sip->sel_lwp == l);
700 1.3.4.2 mjf KASSERT(sip->sel_cpu == l->l_selcpu);
701 1.3.4.2 mjf /*
702 1.3.4.2 mjf * Read link to next selinfo record, if any.
703 1.3.4.2 mjf * It's no longer safe to touch `sip' after clearing
704 1.3.4.2 mjf * `sel_lwp', so ensure that the read of `sel_chain'
705 1.3.4.2 mjf * completes before the clearing of sel_lwp becomes
706 1.3.4.2 mjf * globally visible.
707 1.3.4.2 mjf */
708 1.3.4.2 mjf next = SLIST_NEXT(sip, sel_chain);
709 1.3.4.2 mjf membar_exit();
710 1.3.4.2 mjf /* Release the record for another named waiter to use. */
711 1.3.4.2 mjf sip->sel_lwp = NULL;
712 1.3.4.2 mjf }
713 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
714 1.3.4.2 mjf }
715 1.3.4.2 mjf
716 1.3.4.2 mjf /*
717 1.3.4.2 mjf * Initialize the select/poll system calls. Called once for each
718 1.3.4.2 mjf * CPU in the system, as they are attached.
719 1.3.4.2 mjf */
720 1.3.4.2 mjf void
721 1.3.4.2 mjf selsysinit(struct cpu_info *ci)
722 1.3.4.2 mjf {
723 1.3.4.2 mjf selcpu_t *sc;
724 1.3.4.2 mjf
725 1.3.4.2 mjf sc = kmem_alloc(roundup2(sizeof(selcpu_t), coherency_unit) +
726 1.3.4.2 mjf coherency_unit, KM_SLEEP);
727 1.3.4.2 mjf sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
728 1.3.4.2 mjf mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
729 1.3.4.2 mjf sleepq_init(&sc->sc_sleepq, &sc->sc_lock);
730 1.3.4.2 mjf sc->sc_ncoll = 0;
731 1.3.4.2 mjf sc->sc_mask = (1 << cpu_index(ci));
732 1.3.4.2 mjf ci->ci_data.cpu_selcpu = sc;
733 1.3.4.2 mjf }
734 1.3.4.2 mjf
735 1.3.4.2 mjf /*
736 1.3.4.2 mjf * Initialize a selinfo record.
737 1.3.4.2 mjf */
738 1.3.4.2 mjf void
739 1.3.4.2 mjf selinit(struct selinfo *sip)
740 1.3.4.2 mjf {
741 1.3.4.2 mjf
742 1.3.4.2 mjf memset(sip, 0, sizeof(*sip));
743 1.3.4.2 mjf }
744 1.3.4.2 mjf
745 1.3.4.2 mjf /*
746 1.3.4.2 mjf * Destroy a selinfo record. The owning object must not gain new
747 1.3.4.2 mjf * references while this is in progress: all activity on the record
748 1.3.4.2 mjf * must be stopped.
749 1.3.4.2 mjf *
750 1.3.4.2 mjf * Concurrency issues: we only need guard against a call to selclear()
751 1.3.4.2 mjf * by a thread exiting selcommon() and/or pollcommon(). The caller has
752 1.3.4.2 mjf * prevented further references being made to the selinfo record via
753 1.3.4.2 mjf * selrecord(), and it won't call selwakeup() again.
754 1.3.4.2 mjf */
755 1.3.4.2 mjf void
756 1.3.4.2 mjf seldestroy(struct selinfo *sip)
757 1.3.4.2 mjf {
758 1.3.4.2 mjf selcpu_t *sc;
759 1.3.4.2 mjf lwp_t *l;
760 1.3.4.2 mjf
761 1.3.4.2 mjf if (sip->sel_lwp == NULL)
762 1.3.4.2 mjf return;
763 1.3.4.2 mjf
764 1.3.4.2 mjf /*
765 1.3.4.2 mjf * Lock out selclear(). The selcpu pointer can't change while
766 1.3.4.2 mjf * we are here since it is only ever changed in selrecord(),
767 1.3.4.2 mjf * and that will not be entered again for this record because
768 1.3.4.2 mjf * it is dying.
769 1.3.4.2 mjf */
770 1.3.4.2 mjf KASSERT(sip->sel_cpu != NULL);
771 1.3.4.2 mjf sc = sip->sel_cpu;
772 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
773 1.3.4.2 mjf if ((l = sip->sel_lwp) != NULL) {
774 1.3.4.2 mjf /*
775 1.3.4.2 mjf * This should rarely happen, so although SLIST_REMOVE()
776 1.3.4.2 mjf * is slow, using it here is not a problem.
777 1.3.4.2 mjf */
778 1.3.4.2 mjf KASSERT(l->l_selcpu == sc);
779 1.3.4.2 mjf SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
780 1.3.4.2 mjf sip->sel_lwp = NULL;
781 1.3.4.2 mjf }
782 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
783 1.3.4.2 mjf }
784 1.3.4.2 mjf
785 1.3.4.2 mjf int
786 1.3.4.2 mjf pollsock(struct socket *so, const struct timeval *tvp, int events)
787 1.3.4.2 mjf {
788 1.3.4.2 mjf int ncoll, error, timo;
789 1.3.4.2 mjf struct timeval sleeptv, tv;
790 1.3.4.2 mjf selcpu_t *sc;
791 1.3.4.2 mjf lwp_t *l;
792 1.3.4.2 mjf
793 1.3.4.2 mjf timo = 0;
794 1.3.4.2 mjf if (tvp != NULL) {
795 1.3.4.2 mjf tv = *tvp;
796 1.3.4.2 mjf if (inittimeleft(&tv, &sleeptv) == -1)
797 1.3.4.2 mjf return EINVAL;
798 1.3.4.2 mjf }
799 1.3.4.2 mjf
800 1.3.4.2 mjf l = curlwp;
801 1.3.4.2 mjf sc = l->l_cpu->ci_data.cpu_selcpu;
802 1.3.4.2 mjf l->l_selcpu = sc;
803 1.3.4.2 mjf SLIST_INIT(&l->l_selwait);
804 1.3.4.2 mjf error = 0;
805 1.3.4.2 mjf for (;;) {
806 1.3.4.2 mjf /*
807 1.3.4.2 mjf * No need to lock. If this is overwritten by another
808 1.3.4.2 mjf * value while scanning, we will retry below. We only
809 1.3.4.2 mjf * need to see exact state from the descriptors that
810 1.3.4.2 mjf * we are about to poll, and lock activity resulting
811 1.3.4.2 mjf * from fo_poll is enough to provide an up to date value
812 1.3.4.2 mjf * for new polling activity.
813 1.3.4.2 mjf */
814 1.3.4.2 mjf ncoll = sc->sc_ncoll;
815 1.3.4.2 mjf l->l_selflag = SEL_SCANNING;
816 1.3.4.2 mjf if (sopoll(so, events) != 0)
817 1.3.4.2 mjf break;
818 1.3.4.2 mjf if (tvp && (timo = gettimeleft(&tv, &sleeptv)) <= 0)
819 1.3.4.2 mjf break;
820 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
821 1.3.4.2 mjf if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
822 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
823 1.3.4.2 mjf continue;
824 1.3.4.2 mjf }
825 1.3.4.2 mjf l->l_selflag = SEL_BLOCKING;
826 1.3.4.2 mjf lwp_lock(l);
827 1.3.4.2 mjf lwp_unlock_to(l, &sc->sc_lock);
828 1.3.4.2 mjf sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
829 1.3.4.2 mjf KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); /* XXX */
830 1.3.4.2 mjf error = sleepq_block(timo, true);
831 1.3.4.2 mjf if (error != 0)
832 1.3.4.2 mjf break;
833 1.3.4.2 mjf }
834 1.3.4.2 mjf selclear();
835 1.3.4.2 mjf /* poll is not restarted after signals... */
836 1.3.4.2 mjf if (error == ERESTART)
837 1.3.4.2 mjf error = EINTR;
838 1.3.4.2 mjf if (error == EWOULDBLOCK)
839 1.3.4.2 mjf error = 0;
840 1.3.4.2 mjf return (error);
841 1.3.4.2 mjf }
842