Home | History | Annotate | Line # | Download | only in kern
sys_select.c revision 1.21
      1  1.21     rmind /*	$NetBSD: sys_select.c,v 1.21 2009/12/20 23:00:59 rmind Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4  1.13        ad  * Copyright (c) 2007, 2008, 2009 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.1        ad  * by Andrew Doran.
      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.21     rmind  * System calls of synchronous I/O multiplexing subsystem.
     70  1.21     rmind  *
     71  1.21     rmind  * Locking
     72  1.21     rmind  *
     73  1.21     rmind  * Two locks are used: <object-lock> and selcpu_t::sc_lock.
     74  1.21     rmind  *
     75  1.21     rmind  * The <object-lock> might be a device driver or another subsystem, e.g.
     76  1.21     rmind  * socket or pipe.  This lock is not exported, and thus invisible to this
     77  1.21     rmind  * subsystem.  Mainly, synchronisation between selrecord() and selnotify()
     78  1.21     rmind  * routines depends on this lock, as it will be described in the comments.
     79  1.21     rmind  *
     80  1.21     rmind  * Lock order
     81  1.21     rmind  *
     82  1.21     rmind  *	<object-lock> ->
     83  1.21     rmind  *		selcpu_t::sc_lock
     84   1.1        ad  */
     85   1.1        ad 
     86   1.1        ad #include <sys/cdefs.h>
     87  1.21     rmind __KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.21 2009/12/20 23:00:59 rmind 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.1        ad #define	SEL_BLOCKING	2	/* about to block on select_cv */
    113   1.1        ad 
    114   1.1        ad /* Per-CPU state for select()/poll(). */
    115   1.1        ad #if MAXCPUS > 32
    116   1.1        ad #error adjust this code
    117   1.1        ad #endif
    118   1.1        ad typedef struct selcpu {
    119  1.13        ad 	kmutex_t	*sc_lock;
    120   1.1        ad 	sleepq_t	sc_sleepq;
    121   1.1        ad 	int		sc_ncoll;
    122   1.1        ad 	uint32_t	sc_mask;
    123   1.1        ad } selcpu_t;
    124   1.1        ad 
    125  1.19     rmind static inline int	selscan(char *, u_int, register_t *);
    126  1.19     rmind static inline int	pollscan(struct pollfd *, u_int, register_t *);
    127  1.19     rmind static void		selclear(void);
    128   1.1        ad 
    129   1.1        ad static syncobj_t select_sobj = {
    130   1.1        ad 	SOBJ_SLEEPQ_FIFO,
    131   1.1        ad 	sleepq_unsleep,
    132   1.1        ad 	sleepq_changepri,
    133   1.1        ad 	sleepq_lendpri,
    134   1.1        ad 	syncobj_noowner,
    135   1.1        ad };
    136   1.1        ad 
    137   1.1        ad /*
    138   1.1        ad  * Select system call.
    139   1.1        ad  */
    140   1.1        ad int
    141  1.12  christos sys___pselect50(struct lwp *l, const struct sys___pselect50_args *uap,
    142  1.12  christos     register_t *retval)
    143   1.1        ad {
    144   1.1        ad 	/* {
    145   1.1        ad 		syscallarg(int)				nd;
    146   1.1        ad 		syscallarg(fd_set *)			in;
    147   1.1        ad 		syscallarg(fd_set *)			ou;
    148   1.1        ad 		syscallarg(fd_set *)			ex;
    149   1.1        ad 		syscallarg(const struct timespec *)	ts;
    150   1.1        ad 		syscallarg(sigset_t *)			mask;
    151   1.1        ad 	} */
    152  1.14  christos 	struct timespec	ats, *ts = NULL;
    153   1.1        ad 	sigset_t	amask, *mask = NULL;
    154   1.1        ad 	int		error;
    155   1.1        ad 
    156   1.1        ad 	if (SCARG(uap, ts)) {
    157   1.1        ad 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    158   1.1        ad 		if (error)
    159   1.1        ad 			return error;
    160  1.14  christos 		ts = &ats;
    161   1.1        ad 	}
    162   1.1        ad 	if (SCARG(uap, mask) != NULL) {
    163   1.1        ad 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    164   1.1        ad 		if (error)
    165   1.1        ad 			return error;
    166   1.1        ad 		mask = &amask;
    167   1.1        ad 	}
    168   1.1        ad 
    169  1.19     rmind 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
    170  1.14  christos 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
    171   1.1        ad }
    172   1.1        ad 
    173   1.1        ad int
    174  1.12  christos sys___select50(struct lwp *l, const struct sys___select50_args *uap,
    175  1.12  christos     register_t *retval)
    176   1.1        ad {
    177   1.1        ad 	/* {
    178   1.1        ad 		syscallarg(int)			nd;
    179   1.1        ad 		syscallarg(fd_set *)		in;
    180   1.1        ad 		syscallarg(fd_set *)		ou;
    181   1.1        ad 		syscallarg(fd_set *)		ex;
    182   1.1        ad 		syscallarg(struct timeval *)	tv;
    183   1.1        ad 	} */
    184  1.14  christos 	struct timeval atv;
    185  1.14  christos 	struct timespec ats, *ts = NULL;
    186   1.1        ad 	int error;
    187   1.1        ad 
    188   1.1        ad 	if (SCARG(uap, tv)) {
    189  1.14  christos 		error = copyin(SCARG(uap, tv), (void *)&atv, sizeof(atv));
    190   1.1        ad 		if (error)
    191   1.1        ad 			return error;
    192  1.14  christos 		TIMEVAL_TO_TIMESPEC(&atv, &ats);
    193  1.14  christos 		ts = &ats;
    194   1.1        ad 	}
    195   1.1        ad 
    196  1.19     rmind 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
    197  1.14  christos 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
    198   1.1        ad }
    199   1.1        ad 
    200  1.17     rmind /*
    201  1.17     rmind  * sel_do_scan: common code to perform the scan on descriptors.
    202  1.17     rmind  */
    203  1.17     rmind static int
    204  1.17     rmind sel_do_scan(void *fds, u_int nfds, struct timespec *ts, sigset_t *mask,
    205  1.17     rmind     register_t *retval, int selpoll)
    206   1.1        ad {
    207  1.17     rmind 	lwp_t		* const l = curlwp;
    208   1.1        ad 	proc_t		* const p = l->l_proc;
    209   1.1        ad 	selcpu_t	*sc;
    210  1.13        ad 	kmutex_t	*lock;
    211  1.17     rmind 	sigset_t	oldmask;
    212  1.17     rmind 	struct timespec	sleepts;
    213  1.17     rmind 	int		error, timo;
    214   1.1        ad 
    215   1.1        ad 	timo = 0;
    216  1.14  christos 	if (ts && inittimeleft(ts, &sleepts) == -1) {
    217  1.17     rmind 		return EINVAL;
    218   1.1        ad 	}
    219   1.1        ad 
    220  1.17     rmind 	if (__predict_false(mask)) {
    221   1.1        ad 		sigminusset(&sigcantmask, mask);
    222   1.5        ad 		mutex_enter(p->p_lock);
    223   1.1        ad 		oldmask = l->l_sigmask;
    224   1.1        ad 		l->l_sigmask = *mask;
    225   1.5        ad 		mutex_exit(p->p_lock);
    226  1.17     rmind 	} else {
    227  1.17     rmind 		/* XXXgcc */
    228  1.17     rmind 		oldmask = l->l_sigmask;
    229  1.17     rmind 	}
    230   1.1        ad 
    231   1.1        ad 	sc = curcpu()->ci_data.cpu_selcpu;
    232  1.13        ad 	lock = sc->sc_lock;
    233   1.1        ad 	l->l_selcpu = sc;
    234   1.1        ad 	SLIST_INIT(&l->l_selwait);
    235   1.1        ad 	for (;;) {
    236  1.17     rmind 		int ncoll;
    237  1.17     rmind 
    238   1.1        ad 		/*
    239  1.17     rmind 		 * No need to lock.  If this is overwritten by another value
    240  1.17     rmind 		 * while scanning, we will retry below.  We only need to see
    241  1.17     rmind 		 * exact state from the descriptors that we are about to poll,
    242  1.17     rmind 		 * and lock activity resulting from fo_poll is enough to
    243  1.17     rmind 		 * provide an up to date value for new polling activity.
    244   1.1        ad 		 */
    245  1.17     rmind 		l->l_selflag = SEL_SCANNING;
    246   1.1        ad 		ncoll = sc->sc_ncoll;
    247   1.1        ad 
    248  1.17     rmind 		if (selpoll) {
    249  1.17     rmind 			error = selscan((char *)fds, nfds, retval);
    250  1.17     rmind 		} else {
    251  1.17     rmind 			error = pollscan((struct pollfd *)fds, nfds, retval);
    252  1.17     rmind 		}
    253   1.1        ad 
    254   1.1        ad 		if (error || *retval)
    255   1.1        ad 			break;
    256  1.14  christos 		if (ts && (timo = gettimeleft(ts, &sleepts)) <= 0)
    257   1.1        ad 			break;
    258  1.13        ad 		mutex_spin_enter(lock);
    259   1.1        ad 		if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
    260  1.13        ad 			mutex_spin_exit(lock);
    261   1.1        ad 			continue;
    262   1.1        ad 		}
    263   1.1        ad 		l->l_selflag = SEL_BLOCKING;
    264   1.7        ad 		l->l_kpriority = true;
    265  1.13        ad 		sleepq_enter(&sc->sc_sleepq, l, lock);
    266   1.1        ad 		sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
    267   1.1        ad 		error = sleepq_block(timo, true);
    268   1.1        ad 		if (error != 0)
    269   1.1        ad 			break;
    270   1.1        ad 	}
    271   1.1        ad 	selclear();
    272   1.1        ad 
    273  1.17     rmind 	if (__predict_false(mask)) {
    274   1.5        ad 		mutex_enter(p->p_lock);
    275   1.1        ad 		l->l_sigmask = oldmask;
    276   1.5        ad 		mutex_exit(p->p_lock);
    277   1.1        ad 	}
    278  1.20       dsl 
    279  1.20       dsl 	/* select and poll are not restarted after signals... */
    280  1.20       dsl 	if (error == ERESTART)
    281  1.20       dsl 		return EINTR;
    282  1.20       dsl 	if (error == EWOULDBLOCK)
    283  1.20       dsl 		return 0;
    284  1.17     rmind 	return error;
    285  1.17     rmind }
    286  1.17     rmind 
    287  1.17     rmind int
    288  1.19     rmind selcommon(register_t *retval, int nd, fd_set *u_in, fd_set *u_ou,
    289  1.19     rmind     fd_set *u_ex, struct timespec *ts, sigset_t *mask)
    290  1.17     rmind {
    291  1.17     rmind 	char		smallbits[howmany(FD_SETSIZE, NFDBITS) *
    292  1.17     rmind 			    sizeof(fd_mask) * 6];
    293  1.17     rmind 	char 		*bits;
    294  1.17     rmind 	int		error, nf;
    295  1.17     rmind 	size_t		ni;
    296  1.17     rmind 
    297  1.17     rmind 	if (nd < 0)
    298  1.17     rmind 		return (EINVAL);
    299  1.19     rmind 	nf = curlwp->l_fd->fd_dt->dt_nfiles;
    300  1.17     rmind 	if (nd > nf) {
    301  1.17     rmind 		/* forgiving; slightly wrong */
    302  1.17     rmind 		nd = nf;
    303  1.17     rmind 	}
    304  1.17     rmind 	ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
    305  1.17     rmind 	if (ni * 6 > sizeof(smallbits)) {
    306  1.17     rmind 		bits = kmem_alloc(ni * 6, KM_SLEEP);
    307  1.17     rmind 		if (bits == NULL)
    308  1.17     rmind 			return ENOMEM;
    309  1.17     rmind 	} else
    310  1.17     rmind 		bits = smallbits;
    311  1.17     rmind 
    312  1.17     rmind #define	getbits(name, x)						\
    313  1.17     rmind 	if (u_ ## name) {						\
    314  1.17     rmind 		error = copyin(u_ ## name, bits + ni * x, ni);		\
    315  1.17     rmind 		if (error)						\
    316  1.20       dsl 			goto fail;					\
    317  1.17     rmind 	} else								\
    318  1.17     rmind 		memset(bits + ni * x, 0, ni);
    319  1.17     rmind 	getbits(in, 0);
    320  1.17     rmind 	getbits(ou, 1);
    321  1.17     rmind 	getbits(ex, 2);
    322  1.17     rmind #undef	getbits
    323   1.1        ad 
    324  1.17     rmind 	error = sel_do_scan(bits, nd, ts, mask, retval, 1);
    325   1.1        ad 	if (error == 0 && u_in != NULL)
    326   1.1        ad 		error = copyout(bits + ni * 3, u_in, ni);
    327   1.1        ad 	if (error == 0 && u_ou != NULL)
    328   1.1        ad 		error = copyout(bits + ni * 4, u_ou, ni);
    329   1.1        ad 	if (error == 0 && u_ex != NULL)
    330   1.1        ad 		error = copyout(bits + ni * 5, u_ex, ni);
    331  1.20       dsl  fail:
    332   1.1        ad 	if (bits != smallbits)
    333   1.1        ad 		kmem_free(bits, ni * 6);
    334   1.1        ad 	return (error);
    335   1.1        ad }
    336   1.1        ad 
    337  1.19     rmind static inline int
    338  1.17     rmind selscan(char *bits, u_int nfd, register_t *retval)
    339   1.1        ad {
    340   1.1        ad 	static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
    341   1.1        ad 			       POLLWRNORM | POLLHUP | POLLERR,
    342   1.1        ad 			       POLLRDBAND };
    343  1.17     rmind 	fd_mask *ibitp, *obitp;
    344  1.17     rmind 	int msk, i, j, fd, ni, n;
    345   1.1        ad 	fd_mask ibits, obits;
    346   1.1        ad 	file_t *fp;
    347   1.1        ad 
    348  1.17     rmind 	ni = howmany(nfd, NFDBITS) * sizeof(fd_mask);
    349  1.17     rmind 	ibitp = (fd_mask *)(bits + ni * 0);
    350  1.17     rmind 	obitp = (fd_mask *)(bits + ni * 3);
    351   1.1        ad 	n = 0;
    352  1.17     rmind 
    353   1.1        ad 	for (msk = 0; msk < 3; msk++) {
    354   1.1        ad 		for (i = 0; i < nfd; i += NFDBITS) {
    355   1.1        ad 			ibits = *ibitp++;
    356   1.1        ad 			obits = 0;
    357   1.1        ad 			while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
    358   1.1        ad 				ibits &= ~(1 << j);
    359   1.1        ad 				if ((fp = fd_getfile(fd)) == NULL)
    360   1.1        ad 					return (EBADF);
    361   1.1        ad 				if ((*fp->f_ops->fo_poll)(fp, flag[msk])) {
    362   1.1        ad 					obits |= (1 << j);
    363   1.1        ad 					n++;
    364   1.1        ad 				}
    365   1.1        ad 				fd_putfile(fd);
    366   1.1        ad 			}
    367   1.1        ad 			*obitp++ = obits;
    368   1.1        ad 		}
    369   1.1        ad 	}
    370   1.1        ad 	*retval = n;
    371   1.1        ad 	return (0);
    372   1.1        ad }
    373   1.1        ad 
    374   1.1        ad /*
    375   1.1        ad  * Poll system call.
    376   1.1        ad  */
    377   1.1        ad int
    378   1.1        ad sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
    379   1.1        ad {
    380   1.1        ad 	/* {
    381   1.1        ad 		syscallarg(struct pollfd *)	fds;
    382   1.1        ad 		syscallarg(u_int)		nfds;
    383   1.1        ad 		syscallarg(int)			timeout;
    384   1.1        ad 	} */
    385  1.14  christos 	struct timespec	ats, *ts = NULL;
    386   1.1        ad 
    387   1.1        ad 	if (SCARG(uap, timeout) != INFTIM) {
    388  1.14  christos 		ats.tv_sec = SCARG(uap, timeout) / 1000;
    389  1.14  christos 		ats.tv_nsec = (SCARG(uap, timeout) % 1000) * 1000000;
    390  1.14  christos 		ts = &ats;
    391   1.1        ad 	}
    392   1.1        ad 
    393  1.19     rmind 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, NULL);
    394   1.1        ad }
    395   1.1        ad 
    396   1.1        ad /*
    397   1.1        ad  * Poll system call.
    398   1.1        ad  */
    399   1.1        ad int
    400  1.12  christos sys___pollts50(struct lwp *l, const struct sys___pollts50_args *uap,
    401  1.12  christos     register_t *retval)
    402   1.1        ad {
    403   1.1        ad 	/* {
    404   1.1        ad 		syscallarg(struct pollfd *)		fds;
    405   1.1        ad 		syscallarg(u_int)			nfds;
    406   1.1        ad 		syscallarg(const struct timespec *)	ts;
    407   1.1        ad 		syscallarg(const sigset_t *)		mask;
    408   1.1        ad 	} */
    409  1.14  christos 	struct timespec	ats, *ts = NULL;
    410   1.1        ad 	sigset_t	amask, *mask = NULL;
    411   1.1        ad 	int		error;
    412   1.1        ad 
    413   1.1        ad 	if (SCARG(uap, ts)) {
    414   1.1        ad 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    415   1.1        ad 		if (error)
    416   1.1        ad 			return error;
    417  1.14  christos 		ts = &ats;
    418   1.1        ad 	}
    419   1.1        ad 	if (SCARG(uap, mask)) {
    420   1.1        ad 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    421   1.1        ad 		if (error)
    422   1.1        ad 			return error;
    423   1.1        ad 		mask = &amask;
    424   1.1        ad 	}
    425   1.1        ad 
    426  1.19     rmind 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
    427   1.1        ad }
    428   1.1        ad 
    429   1.1        ad int
    430  1.19     rmind pollcommon(register_t *retval, struct pollfd *u_fds, u_int nfds,
    431  1.14  christos     struct timespec *ts, sigset_t *mask)
    432   1.1        ad {
    433  1.11      yamt 	struct pollfd	smallfds[32];
    434  1.11      yamt 	struct pollfd	*fds;
    435  1.17     rmind 	int		error;
    436  1.20       dsl 	size_t		ni;
    437   1.1        ad 
    438  1.20       dsl 	if (nfds > 1000 + curlwp->l_fd->fd_dt->dt_nfiles) {
    439  1.20       dsl 		/*
    440  1.20       dsl 		 * Either the user passed in a very sparse 'fds' or junk!
    441  1.20       dsl 		 * The kmem_alloc() call below would be bad news.
    442  1.20       dsl 		 * We could process the 'fds' array in chunks, but that
    443  1.20       dsl 		 * is a lot of code that isn't normally useful.
    444  1.20       dsl 		 * (Or just move the copyin/out into pollscan().)
    445  1.20       dsl 		 * Historically the code silently truncated 'fds' to
    446  1.20       dsl 		 * dt_nfiles entries - but that does cause issues.
    447  1.20       dsl 		 */
    448  1.20       dsl 		return EINVAL;
    449   1.1        ad 	}
    450   1.1        ad 	ni = nfds * sizeof(struct pollfd);
    451  1.11      yamt 	if (ni > sizeof(smallfds)) {
    452  1.11      yamt 		fds = kmem_alloc(ni, KM_SLEEP);
    453  1.11      yamt 		if (fds == NULL)
    454   1.9     rmind 			return ENOMEM;
    455   1.9     rmind 	} else
    456  1.11      yamt 		fds = smallfds;
    457   1.1        ad 
    458  1.11      yamt 	error = copyin(u_fds, fds, ni);
    459   1.1        ad 	if (error)
    460  1.20       dsl 		goto fail;
    461   1.1        ad 
    462  1.17     rmind 	error = sel_do_scan(fds, nfds, ts, mask, retval, 0);
    463   1.1        ad 	if (error == 0)
    464  1.11      yamt 		error = copyout(fds, u_fds, ni);
    465  1.20       dsl  fail:
    466  1.11      yamt 	if (fds != smallfds)
    467  1.11      yamt 		kmem_free(fds, ni);
    468   1.1        ad 	return (error);
    469   1.1        ad }
    470   1.1        ad 
    471  1.19     rmind static inline int
    472  1.17     rmind pollscan(struct pollfd *fds, u_int nfd, register_t *retval)
    473   1.1        ad {
    474   1.1        ad 	int i, n;
    475   1.1        ad 	file_t *fp;
    476   1.1        ad 
    477   1.1        ad 	n = 0;
    478   1.1        ad 	for (i = 0; i < nfd; i++, fds++) {
    479   1.1        ad 		if (fds->fd < 0) {
    480   1.1        ad 			fds->revents = 0;
    481   1.1        ad 		} else if ((fp = fd_getfile(fds->fd)) == NULL) {
    482   1.1        ad 			fds->revents = POLLNVAL;
    483   1.1        ad 			n++;
    484   1.1        ad 		} else {
    485   1.1        ad 			fds->revents = (*fp->f_ops->fo_poll)(fp,
    486   1.1        ad 			    fds->events | POLLERR | POLLHUP);
    487   1.1        ad 			if (fds->revents != 0)
    488   1.1        ad 				n++;
    489   1.1        ad 			fd_putfile(fds->fd);
    490   1.1        ad 		}
    491   1.1        ad 	}
    492   1.1        ad 	*retval = n;
    493   1.1        ad 	return (0);
    494   1.1        ad }
    495   1.1        ad 
    496   1.1        ad /*ARGSUSED*/
    497   1.1        ad int
    498   1.1        ad seltrue(dev_t dev, int events, lwp_t *l)
    499   1.1        ad {
    500   1.1        ad 
    501   1.1        ad 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    502   1.1        ad }
    503   1.1        ad 
    504   1.1        ad /*
    505   1.1        ad  * Record a select request.  Concurrency issues:
    506   1.1        ad  *
    507   1.1        ad  * The caller holds the same lock across calls to selrecord() and
    508   1.4      yamt  * selnotify(), so we don't need to consider a concurrent wakeup
    509   1.1        ad  * while in this routine.
    510   1.1        ad  *
    511   1.1        ad  * The only activity we need to guard against is selclear(), called by
    512  1.17     rmind  * another thread that is exiting sel_do_scan().
    513   1.1        ad  * `sel_lwp' can only become non-NULL while the caller's lock is held,
    514   1.1        ad  * so it cannot become non-NULL due to a change made by another thread
    515   1.1        ad  * while we are in this routine.  It can only become _NULL_ due to a
    516   1.1        ad  * call to selclear().
    517   1.1        ad  *
    518   1.1        ad  * If it is non-NULL and != selector there is the potential for
    519   1.1        ad  * selclear() to be called by another thread.  If either of those
    520   1.1        ad  * conditions are true, we're not interested in touching the `named
    521   1.1        ad  * waiter' part of the selinfo record because we need to record a
    522   1.1        ad  * collision.  Hence there is no need for additional locking in this
    523   1.1        ad  * routine.
    524   1.1        ad  */
    525   1.1        ad void
    526   1.1        ad selrecord(lwp_t *selector, struct selinfo *sip)
    527   1.1        ad {
    528   1.1        ad 	selcpu_t *sc;
    529   1.1        ad 	lwp_t *other;
    530   1.1        ad 
    531   1.1        ad 	KASSERT(selector == curlwp);
    532   1.1        ad 
    533   1.1        ad 	sc = selector->l_selcpu;
    534   1.1        ad 	other = sip->sel_lwp;
    535   1.1        ad 
    536   1.1        ad 	if (other == selector) {
    537   1.1        ad 		/* `selector' has already claimed it. */
    538   1.1        ad 		KASSERT(sip->sel_cpu = sc);
    539   1.1        ad 	} else if (other == NULL) {
    540   1.1        ad 		/*
    541   1.1        ad 		 * First named waiter, although there may be unnamed
    542   1.1        ad 		 * waiters (collisions).  Issue a memory barrier to
    543   1.1        ad 		 * ensure that we access sel_lwp (above) before other
    544   1.1        ad 		 * fields - this guards against a call to selclear().
    545   1.1        ad 		 */
    546   1.1        ad 		membar_enter();
    547   1.1        ad 		sip->sel_lwp = selector;
    548   1.1        ad 		SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
    549   1.1        ad 		/* Replace selinfo's lock with our chosen CPU's lock. */
    550   1.1        ad 		sip->sel_cpu = sc;
    551   1.1        ad 	} else {
    552   1.1        ad 		/* Multiple waiters: record a collision. */
    553   1.1        ad 		sip->sel_collision |= sc->sc_mask;
    554   1.1        ad 		KASSERT(sip->sel_cpu != NULL);
    555   1.1        ad 	}
    556   1.1        ad }
    557   1.1        ad 
    558   1.1        ad /*
    559   1.1        ad  * Do a wakeup when a selectable event occurs.  Concurrency issues:
    560   1.1        ad  *
    561   1.1        ad  * As per selrecord(), the caller's object lock is held.  If there
    562   1.1        ad  * is a named waiter, we must acquire the associated selcpu's lock
    563   1.1        ad  * in order to synchronize with selclear() and pollers going to sleep
    564  1.17     rmind  * in sel_do_scan().
    565   1.1        ad  *
    566   1.1        ad  * sip->sel_cpu cannot change at this point, as it is only changed
    567   1.1        ad  * in selrecord(), and concurrent calls to selrecord() are locked
    568   1.1        ad  * out by the caller.
    569   1.1        ad  */
    570   1.1        ad void
    571   1.1        ad selnotify(struct selinfo *sip, int events, long knhint)
    572   1.1        ad {
    573   1.1        ad 	selcpu_t *sc;
    574   1.1        ad 	uint32_t mask;
    575  1.16     rmind 	int index, oflag;
    576   1.1        ad 	lwp_t *l;
    577  1.13        ad 	kmutex_t *lock;
    578   1.1        ad 
    579   1.1        ad 	KNOTE(&sip->sel_klist, knhint);
    580   1.1        ad 
    581   1.1        ad 	if (sip->sel_lwp != NULL) {
    582   1.1        ad 		/* One named LWP is waiting. */
    583   1.1        ad 		sc = sip->sel_cpu;
    584  1.13        ad 		lock = sc->sc_lock;
    585  1.13        ad 		mutex_spin_enter(lock);
    586   1.1        ad 		/* Still there? */
    587   1.1        ad 		if (sip->sel_lwp != NULL) {
    588   1.1        ad 			l = sip->sel_lwp;
    589   1.1        ad 			/*
    590   1.1        ad 			 * If thread is sleeping, wake it up.  If it's not
    591   1.1        ad 			 * yet asleep, it will notice the change in state
    592   1.1        ad 			 * and will re-poll the descriptors.
    593   1.1        ad 			 */
    594   1.1        ad 			oflag = l->l_selflag;
    595   1.1        ad 			l->l_selflag = SEL_RESET;
    596  1.13        ad 			if (oflag == SEL_BLOCKING && l->l_mutex == lock) {
    597   1.1        ad 				KASSERT(l->l_wchan == sc);
    598  1.16     rmind 				sleepq_unsleep(l, false);
    599   1.1        ad 			}
    600   1.1        ad 		}
    601  1.13        ad 		mutex_spin_exit(lock);
    602   1.1        ad 	}
    603   1.1        ad 
    604   1.1        ad 	if ((mask = sip->sel_collision) != 0) {
    605   1.1        ad 		/*
    606   1.1        ad 		 * There was a collision (multiple waiters): we must
    607   1.1        ad 		 * inform all potentially interested waiters.
    608   1.1        ad 		 */
    609   1.1        ad 		sip->sel_collision = 0;
    610   1.3        ad 		do {
    611   1.1        ad 			index = ffs(mask) - 1;
    612   1.1        ad 			mask &= ~(1 << index);
    613  1.10        ad 			sc = cpu_lookup(index)->ci_data.cpu_selcpu;
    614  1.13        ad 			lock = sc->sc_lock;
    615  1.13        ad 			mutex_spin_enter(lock);
    616   1.1        ad 			sc->sc_ncoll++;
    617  1.13        ad 			sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1, lock);
    618   1.3        ad 		} while (__predict_false(mask != 0));
    619   1.1        ad 	}
    620   1.1        ad }
    621   1.1        ad 
    622   1.1        ad /*
    623   1.1        ad  * Remove an LWP from all objects that it is waiting for.  Concurrency
    624   1.1        ad  * issues:
    625   1.1        ad  *
    626   1.1        ad  * The object owner's (e.g. device driver) lock is not held here.  Calls
    627   1.1        ad  * can be made to selrecord() and we do not synchronize against those
    628   1.1        ad  * directly using locks.  However, we use `sel_lwp' to lock out changes.
    629   1.1        ad  * Before clearing it we must use memory barriers to ensure that we can
    630   1.1        ad  * safely traverse the list of selinfo records.
    631   1.1        ad  */
    632   1.1        ad static void
    633   1.1        ad selclear(void)
    634   1.1        ad {
    635   1.1        ad 	struct selinfo *sip, *next;
    636   1.1        ad 	selcpu_t *sc;
    637   1.1        ad 	lwp_t *l;
    638  1.13        ad 	kmutex_t *lock;
    639   1.1        ad 
    640   1.1        ad 	l = curlwp;
    641   1.1        ad 	sc = l->l_selcpu;
    642  1.13        ad 	lock = sc->sc_lock;
    643   1.1        ad 
    644  1.13        ad 	mutex_spin_enter(lock);
    645   1.1        ad 	for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
    646   1.1        ad 		KASSERT(sip->sel_lwp == l);
    647   1.1        ad 		KASSERT(sip->sel_cpu == l->l_selcpu);
    648   1.1        ad 		/*
    649   1.1        ad 		 * Read link to next selinfo record, if any.
    650   1.1        ad 		 * It's no longer safe to touch `sip' after clearing
    651   1.1        ad 		 * `sel_lwp', so ensure that the read of `sel_chain'
    652   1.1        ad 		 * completes before the clearing of sel_lwp becomes
    653   1.1        ad 		 * globally visible.
    654   1.1        ad 		 */
    655   1.1        ad 		next = SLIST_NEXT(sip, sel_chain);
    656   1.1        ad 		membar_exit();
    657   1.1        ad 		/* Release the record for another named waiter to use. */
    658   1.1        ad 		sip->sel_lwp = NULL;
    659   1.1        ad 	}
    660  1.13        ad 	mutex_spin_exit(lock);
    661   1.1        ad }
    662   1.1        ad 
    663   1.1        ad /*
    664   1.1        ad  * Initialize the select/poll system calls.  Called once for each
    665   1.1        ad  * CPU in the system, as they are attached.
    666   1.1        ad  */
    667   1.1        ad void
    668   1.1        ad selsysinit(struct cpu_info *ci)
    669   1.1        ad {
    670   1.1        ad 	selcpu_t *sc;
    671   1.1        ad 
    672   1.2        ad 	sc = kmem_alloc(roundup2(sizeof(selcpu_t), coherency_unit) +
    673   1.2        ad 	    coherency_unit, KM_SLEEP);
    674   1.2        ad 	sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
    675  1.13        ad 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    676   1.8        ad 	sleepq_init(&sc->sc_sleepq);
    677   1.1        ad 	sc->sc_ncoll = 0;
    678   1.1        ad 	sc->sc_mask = (1 << cpu_index(ci));
    679   1.1        ad 	ci->ci_data.cpu_selcpu = sc;
    680   1.1        ad }
    681   1.1        ad 
    682   1.1        ad /*
    683   1.1        ad  * Initialize a selinfo record.
    684   1.1        ad  */
    685   1.1        ad void
    686   1.1        ad selinit(struct selinfo *sip)
    687   1.1        ad {
    688   1.1        ad 
    689   1.1        ad 	memset(sip, 0, sizeof(*sip));
    690   1.1        ad }
    691   1.1        ad 
    692   1.1        ad /*
    693   1.1        ad  * Destroy a selinfo record.  The owning object must not gain new
    694   1.1        ad  * references while this is in progress: all activity on the record
    695   1.1        ad  * must be stopped.
    696   1.1        ad  *
    697   1.1        ad  * Concurrency issues: we only need guard against a call to selclear()
    698  1.17     rmind  * by a thread exiting sel_do_scan().  The caller has prevented further
    699  1.17     rmind  * references being made to the selinfo record via selrecord(), and it
    700  1.17     rmind  * won't call selwakeup() again.
    701   1.1        ad  */
    702   1.1        ad void
    703   1.1        ad seldestroy(struct selinfo *sip)
    704   1.1        ad {
    705   1.1        ad 	selcpu_t *sc;
    706  1.13        ad 	kmutex_t *lock;
    707   1.1        ad 	lwp_t *l;
    708   1.1        ad 
    709   1.1        ad 	if (sip->sel_lwp == NULL)
    710   1.1        ad 		return;
    711   1.1        ad 
    712   1.1        ad 	/*
    713   1.1        ad 	 * Lock out selclear().  The selcpu pointer can't change while
    714   1.1        ad 	 * we are here since it is only ever changed in selrecord(),
    715   1.1        ad 	 * and that will not be entered again for this record because
    716   1.1        ad 	 * it is dying.
    717   1.1        ad 	 */
    718   1.1        ad 	KASSERT(sip->sel_cpu != NULL);
    719   1.1        ad 	sc = sip->sel_cpu;
    720  1.13        ad 	lock = sc->sc_lock;
    721  1.13        ad 	mutex_spin_enter(lock);
    722   1.1        ad 	if ((l = sip->sel_lwp) != NULL) {
    723   1.1        ad 		/*
    724   1.1        ad 		 * This should rarely happen, so although SLIST_REMOVE()
    725   1.1        ad 		 * is slow, using it here is not a problem.
    726   1.1        ad 		 */
    727   1.1        ad 		KASSERT(l->l_selcpu == sc);
    728   1.1        ad 		SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
    729   1.1        ad 		sip->sel_lwp = NULL;
    730   1.1        ad 	}
    731  1.13        ad 	mutex_spin_exit(lock);
    732   1.1        ad }
    733   1.1        ad 
    734   1.1        ad int
    735  1.14  christos pollsock(struct socket *so, const struct timespec *tsp, int events)
    736   1.1        ad {
    737   1.1        ad 	int		ncoll, error, timo;
    738  1.14  christos 	struct timespec	sleepts, ts;
    739   1.1        ad 	selcpu_t	*sc;
    740   1.1        ad 	lwp_t		*l;
    741  1.13        ad 	kmutex_t	*lock;
    742   1.1        ad 
    743   1.1        ad 	timo = 0;
    744  1.14  christos 	if (tsp != NULL) {
    745  1.14  christos 		ts = *tsp;
    746  1.14  christos 		if (inittimeleft(&ts, &sleepts) == -1)
    747   1.1        ad 			return EINVAL;
    748   1.1        ad 	}
    749   1.1        ad 
    750   1.1        ad 	l = curlwp;
    751   1.1        ad 	sc = l->l_cpu->ci_data.cpu_selcpu;
    752  1.13        ad 	lock = sc->sc_lock;
    753   1.1        ad 	l->l_selcpu = sc;
    754   1.1        ad 	SLIST_INIT(&l->l_selwait);
    755   1.1        ad 	error = 0;
    756   1.1        ad 	for (;;) {
    757   1.1        ad 		/*
    758   1.1        ad 		 * No need to lock.  If this is overwritten by another
    759   1.1        ad 		 * value while scanning, we will retry below.  We only
    760   1.1        ad 		 * need to see exact state from the descriptors that
    761   1.1        ad 		 * we are about to poll, and lock activity resulting
    762   1.1        ad 		 * from fo_poll is enough to provide an up to date value
    763   1.1        ad 		 * for new polling activity.
    764   1.1        ad 		 */
    765   1.1        ad 		ncoll = sc->sc_ncoll;
    766   1.1        ad 		l->l_selflag = SEL_SCANNING;
    767   1.1        ad 		if (sopoll(so, events) != 0)
    768   1.1        ad 			break;
    769  1.14  christos 		if (tsp && (timo = gettimeleft(&ts, &sleepts)) <= 0)
    770   1.1        ad 			break;
    771  1.13        ad 		mutex_spin_enter(lock);
    772   1.1        ad 		if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
    773  1.13        ad 			mutex_spin_exit(lock);
    774   1.1        ad 			continue;
    775   1.1        ad 		}
    776   1.1        ad 		l->l_selflag = SEL_BLOCKING;
    777  1.13        ad 		sleepq_enter(&sc->sc_sleepq, l, lock);
    778   1.1        ad 		sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
    779   1.1        ad 		error = sleepq_block(timo, true);
    780   1.1        ad 		if (error != 0)
    781   1.1        ad 			break;
    782   1.1        ad 	}
    783   1.1        ad 	selclear();
    784   1.1        ad 	/* poll is not restarted after signals... */
    785   1.1        ad 	if (error == ERESTART)
    786   1.1        ad 		error = EINTR;
    787   1.1        ad 	if (error == EWOULDBLOCK)
    788   1.1        ad 		error = 0;
    789   1.1        ad 	return (error);
    790   1.1        ad }
    791