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