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