1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/devops.h> 31*0Sstevel@tonic-gate #include <sys/conf.h> 32*0Sstevel@tonic-gate #include <sys/modctl.h> 33*0Sstevel@tonic-gate #include <sys/sunddi.h> 34*0Sstevel@tonic-gate #include <sys/stat.h> 35*0Sstevel@tonic-gate #include <sys/poll_impl.h> 36*0Sstevel@tonic-gate #include <sys/errno.h> 37*0Sstevel@tonic-gate #include <sys/kmem.h> 38*0Sstevel@tonic-gate #include <sys/mkdev.h> 39*0Sstevel@tonic-gate #include <sys/debug.h> 40*0Sstevel@tonic-gate #include <sys/file.h> 41*0Sstevel@tonic-gate #include <sys/sysmacros.h> 42*0Sstevel@tonic-gate #include <sys/systm.h> 43*0Sstevel@tonic-gate #include <sys/bitmap.h> 44*0Sstevel@tonic-gate #include <sys/devpoll.h> 45*0Sstevel@tonic-gate #include <sys/rctl.h> 46*0Sstevel@tonic-gate #include <sys/resource.h> 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #define RESERVED 1 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /* local data struct */ 51*0Sstevel@tonic-gate static dp_entry_t **devpolltbl; /* dev poll entries */ 52*0Sstevel@tonic-gate static size_t dptblsize; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate static kmutex_t devpoll_lock; /* lock protecting dev tbl */ 55*0Sstevel@tonic-gate int devpoll_init; /* is /dev/poll initialized already */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate /* device local functions */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate static int dpopen(dev_t *devp, int flag, int otyp, cred_t *credp); 60*0Sstevel@tonic-gate static int dpwrite(dev_t dev, struct uio *uiop, cred_t *credp); 61*0Sstevel@tonic-gate static int dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 62*0Sstevel@tonic-gate int *rvalp); 63*0Sstevel@tonic-gate static int dppoll(dev_t dev, short events, int anyyet, short *reventsp, 64*0Sstevel@tonic-gate struct pollhead **phpp); 65*0Sstevel@tonic-gate static int dpclose(dev_t dev, int flag, int otyp, cred_t *credp); 66*0Sstevel@tonic-gate static dev_info_t *dpdevi; 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate static struct cb_ops dp_cb_ops = { 70*0Sstevel@tonic-gate dpopen, /* open */ 71*0Sstevel@tonic-gate dpclose, /* close */ 72*0Sstevel@tonic-gate nodev, /* strategy */ 73*0Sstevel@tonic-gate nodev, /* print */ 74*0Sstevel@tonic-gate nodev, /* dump */ 75*0Sstevel@tonic-gate nodev, /* read */ 76*0Sstevel@tonic-gate dpwrite, /* write */ 77*0Sstevel@tonic-gate dpioctl, /* ioctl */ 78*0Sstevel@tonic-gate nodev, /* devmap */ 79*0Sstevel@tonic-gate nodev, /* mmap */ 80*0Sstevel@tonic-gate nodev, /* segmap */ 81*0Sstevel@tonic-gate dppoll, /* poll */ 82*0Sstevel@tonic-gate nodev, /* prop_op */ 83*0Sstevel@tonic-gate (struct streamtab *)0, /* streamtab */ 84*0Sstevel@tonic-gate D_NEW | D_MP /* flags */ 85*0Sstevel@tonic-gate }; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate static int dpattach(dev_info_t *, ddi_attach_cmd_t); 88*0Sstevel@tonic-gate static int dpdetach(dev_info_t *, ddi_detach_cmd_t); 89*0Sstevel@tonic-gate static int dpinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate static struct dev_ops dp_ops = { 92*0Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 93*0Sstevel@tonic-gate 0, /* refcnt */ 94*0Sstevel@tonic-gate dpinfo, /* info */ 95*0Sstevel@tonic-gate nulldev, /* identify */ 96*0Sstevel@tonic-gate nulldev, /* probe */ 97*0Sstevel@tonic-gate dpattach, /* attach */ 98*0Sstevel@tonic-gate dpdetach, /* detach */ 99*0Sstevel@tonic-gate nodev, /* reset */ 100*0Sstevel@tonic-gate &dp_cb_ops, /* driver operations */ 101*0Sstevel@tonic-gate (struct bus_ops *)NULL, /* bus operations */ 102*0Sstevel@tonic-gate nulldev /* power */ 103*0Sstevel@tonic-gate }; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate static struct modldrv modldrv = { 107*0Sstevel@tonic-gate &mod_driverops, /* type of module - a driver */ 108*0Sstevel@tonic-gate "Dev Poll driver %I%", 109*0Sstevel@tonic-gate &dp_ops, 110*0Sstevel@tonic-gate }; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate static struct modlinkage modlinkage = { 113*0Sstevel@tonic-gate MODREV_1, 114*0Sstevel@tonic-gate (void *)&modldrv, 115*0Sstevel@tonic-gate NULL 116*0Sstevel@tonic-gate }; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 119*0Sstevel@tonic-gate * Locking Design 120*0Sstevel@tonic-gate * 121*0Sstevel@tonic-gate * The /dev/poll driver shares most of its code with poll sys call whose 122*0Sstevel@tonic-gate * code is in common/syscall/poll.c. In poll(2) design, the pollcache 123*0Sstevel@tonic-gate * structure is per lwp. An implicit assumption is made there that some 124*0Sstevel@tonic-gate * portion of pollcache will never be touched by other lwps. E.g., in 125*0Sstevel@tonic-gate * poll(2) design, no lwp will ever need to grow bitmap of other lwp. 126*0Sstevel@tonic-gate * This assumption is not true for /dev/poll; hence the need for extra 127*0Sstevel@tonic-gate * locking. 128*0Sstevel@tonic-gate * 129*0Sstevel@tonic-gate * To allow more paralellism, each /dev/poll file descriptor (indexed by 130*0Sstevel@tonic-gate * minor number) has its own lock. Since read (dpioctl) is a much more 131*0Sstevel@tonic-gate * frequent operation than write, we want to allow multiple reads on same 132*0Sstevel@tonic-gate * /dev/poll fd. However, we prevent writes from being starved by giving 133*0Sstevel@tonic-gate * priority to write operation. Theoretically writes can starve reads as 134*0Sstevel@tonic-gate * well. But in pratical sense this is not important because (1) writes 135*0Sstevel@tonic-gate * happens less often than reads, and (2) write operation defines the 136*0Sstevel@tonic-gate * content of poll fd a cache set. If writes happens so often that they 137*0Sstevel@tonic-gate * can starve reads, that means the cached set is very unstable. It may 138*0Sstevel@tonic-gate * not make sense to read an unstable cache set anyway. Therefore, the 139*0Sstevel@tonic-gate * writers starving readers case is not handled in this design. 140*0Sstevel@tonic-gate */ 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate int 143*0Sstevel@tonic-gate _init() 144*0Sstevel@tonic-gate { 145*0Sstevel@tonic-gate int error; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate dptblsize = DEVPOLLSIZE; 148*0Sstevel@tonic-gate devpolltbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP); 149*0Sstevel@tonic-gate mutex_init(&devpoll_lock, NULL, MUTEX_DEFAULT, NULL); 150*0Sstevel@tonic-gate devpoll_init = 1; 151*0Sstevel@tonic-gate if ((error = mod_install(&modlinkage)) != 0) { 152*0Sstevel@tonic-gate mutex_destroy(&devpoll_lock); 153*0Sstevel@tonic-gate kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize); 154*0Sstevel@tonic-gate devpoll_init = 0; 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate return (error); 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate int 160*0Sstevel@tonic-gate _fini() 161*0Sstevel@tonic-gate { 162*0Sstevel@tonic-gate int error; 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate if ((error = mod_remove(&modlinkage)) != 0) { 165*0Sstevel@tonic-gate return (error); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate mutex_destroy(&devpoll_lock); 168*0Sstevel@tonic-gate kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize); 169*0Sstevel@tonic-gate return (0); 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate int 173*0Sstevel@tonic-gate _info(struct modinfo *modinfop) 174*0Sstevel@tonic-gate { 175*0Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate /*ARGSUSED*/ 179*0Sstevel@tonic-gate static int 180*0Sstevel@tonic-gate dpattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 181*0Sstevel@tonic-gate { 182*0Sstevel@tonic-gate if (ddi_create_minor_node(devi, "poll", S_IFCHR, 0, DDI_PSEUDO, NULL) 183*0Sstevel@tonic-gate == DDI_FAILURE) { 184*0Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 185*0Sstevel@tonic-gate return (DDI_FAILURE); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate dpdevi = devi; 188*0Sstevel@tonic-gate return (DDI_SUCCESS); 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate static int 192*0Sstevel@tonic-gate dpdetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 193*0Sstevel@tonic-gate { 194*0Sstevel@tonic-gate if (cmd != DDI_DETACH) 195*0Sstevel@tonic-gate return (DDI_FAILURE); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 198*0Sstevel@tonic-gate return (DDI_SUCCESS); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* ARGSUSED */ 202*0Sstevel@tonic-gate static int 203*0Sstevel@tonic-gate dpinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 204*0Sstevel@tonic-gate { 205*0Sstevel@tonic-gate int error; 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate switch (infocmd) { 208*0Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 209*0Sstevel@tonic-gate *result = (void *)dpdevi; 210*0Sstevel@tonic-gate error = DDI_SUCCESS; 211*0Sstevel@tonic-gate break; 212*0Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 213*0Sstevel@tonic-gate *result = (void *)0; 214*0Sstevel@tonic-gate error = DDI_SUCCESS; 215*0Sstevel@tonic-gate break; 216*0Sstevel@tonic-gate default: 217*0Sstevel@tonic-gate error = DDI_FAILURE; 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate return (error); 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate /* 223*0Sstevel@tonic-gate * dp_pcache_poll has similar logic to pcache_poll() in poll.c. The major 224*0Sstevel@tonic-gate * differences are: (1) /dev/poll requires scanning the bitmap starting at 225*0Sstevel@tonic-gate * where it was stopped last time, instead of always starting from 0, 226*0Sstevel@tonic-gate * (2) since user may not have cleaned up the cached fds when they are 227*0Sstevel@tonic-gate * closed, some polldats in cache may refer to closed or reused fds. We 228*0Sstevel@tonic-gate * need to check for those cases. 229*0Sstevel@tonic-gate * 230*0Sstevel@tonic-gate * NOTE: Upon closing an fd, automatic poll cache cleanup is done for 231*0Sstevel@tonic-gate * poll(2) caches but NOT for /dev/poll caches. So expect some 232*0Sstevel@tonic-gate * stale entries! 233*0Sstevel@tonic-gate */ 234*0Sstevel@tonic-gate static int 235*0Sstevel@tonic-gate dp_pcache_poll(pollfd_t *pfdp, pollcache_t *pcp, nfds_t nfds, int *fdcntp) 236*0Sstevel@tonic-gate { 237*0Sstevel@tonic-gate int start, ostart, end; 238*0Sstevel@tonic-gate int fdcnt, fd; 239*0Sstevel@tonic-gate boolean_t done; 240*0Sstevel@tonic-gate file_t *fp; 241*0Sstevel@tonic-gate short revent; 242*0Sstevel@tonic-gate boolean_t no_wrap; 243*0Sstevel@tonic-gate pollhead_t *php; 244*0Sstevel@tonic-gate polldat_t *pdp; 245*0Sstevel@tonic-gate int error = 0; 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock)); 248*0Sstevel@tonic-gate if (pcp->pc_bitmap == NULL) { 249*0Sstevel@tonic-gate /* 250*0Sstevel@tonic-gate * No Need to search because no poll fd 251*0Sstevel@tonic-gate * has been cached. 252*0Sstevel@tonic-gate */ 253*0Sstevel@tonic-gate return (error); 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate retry: 256*0Sstevel@tonic-gate start = ostart = pcp->pc_mapstart; 257*0Sstevel@tonic-gate end = pcp->pc_mapend; 258*0Sstevel@tonic-gate php = NULL; 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate if (start == 0) { 261*0Sstevel@tonic-gate /* 262*0Sstevel@tonic-gate * started from every begining, no need to wrap around. 263*0Sstevel@tonic-gate */ 264*0Sstevel@tonic-gate no_wrap = B_TRUE; 265*0Sstevel@tonic-gate } else { 266*0Sstevel@tonic-gate no_wrap = B_FALSE; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate done = B_FALSE; 269*0Sstevel@tonic-gate fdcnt = 0; 270*0Sstevel@tonic-gate while ((fdcnt < nfds) && !done) { 271*0Sstevel@tonic-gate php = NULL; 272*0Sstevel@tonic-gate revent = 0; 273*0Sstevel@tonic-gate /* 274*0Sstevel@tonic-gate * Examine the bit map in a circular fashion 275*0Sstevel@tonic-gate * to avoid starvation. Always resume from 276*0Sstevel@tonic-gate * last stop. Scan till end of the map. Then 277*0Sstevel@tonic-gate * wrap around. 278*0Sstevel@tonic-gate */ 279*0Sstevel@tonic-gate fd = bt_getlowbit(pcp->pc_bitmap, start, end); 280*0Sstevel@tonic-gate ASSERT(fd <= end); 281*0Sstevel@tonic-gate if (fd >= 0) { 282*0Sstevel@tonic-gate if (fd == end) { 283*0Sstevel@tonic-gate if (no_wrap) { 284*0Sstevel@tonic-gate done = B_TRUE; 285*0Sstevel@tonic-gate } else { 286*0Sstevel@tonic-gate start = 0; 287*0Sstevel@tonic-gate end = ostart - 1; 288*0Sstevel@tonic-gate no_wrap = B_TRUE; 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate } else { 291*0Sstevel@tonic-gate start = fd + 1; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate pdp = pcache_lookup_fd(pcp, fd); 294*0Sstevel@tonic-gate ASSERT(pdp != NULL); 295*0Sstevel@tonic-gate ASSERT(pdp->pd_fd == fd); 296*0Sstevel@tonic-gate if (pdp->pd_fp == NULL) { 297*0Sstevel@tonic-gate /* 298*0Sstevel@tonic-gate * The fd is POLLREMOVed. This fd is 299*0Sstevel@tonic-gate * logically no longer cached. So move 300*0Sstevel@tonic-gate * on to the next one. 301*0Sstevel@tonic-gate */ 302*0Sstevel@tonic-gate continue; 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate if ((fp = getf(fd)) == NULL) { 305*0Sstevel@tonic-gate /* 306*0Sstevel@tonic-gate * The fd has been closed, but user has not 307*0Sstevel@tonic-gate * done a POLLREMOVE on this fd yet. Instead 308*0Sstevel@tonic-gate * of cleaning it here implicitly, we return 309*0Sstevel@tonic-gate * POLLNVAL. This is consistent with poll(2) 310*0Sstevel@tonic-gate * polling a closed fd. Hope this will remind 311*0Sstevel@tonic-gate * user to do a POLLREMOVE. 312*0Sstevel@tonic-gate */ 313*0Sstevel@tonic-gate pfdp[fdcnt].fd = fd; 314*0Sstevel@tonic-gate pfdp[fdcnt].revents = POLLNVAL; 315*0Sstevel@tonic-gate fdcnt++; 316*0Sstevel@tonic-gate continue; 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate if (fp != pdp->pd_fp) { 319*0Sstevel@tonic-gate /* 320*0Sstevel@tonic-gate * user is polling on a cached fd which was 321*0Sstevel@tonic-gate * closed and then reused. Unfortunately 322*0Sstevel@tonic-gate * there is no good way to inform user. 323*0Sstevel@tonic-gate * If the file struct is also reused, we 324*0Sstevel@tonic-gate * may not be able to detect the fd reuse 325*0Sstevel@tonic-gate * at all. As long as this does not 326*0Sstevel@tonic-gate * cause system failure and/or memory leak, 327*0Sstevel@tonic-gate * we will play along. Man page states if 328*0Sstevel@tonic-gate * user does not clean up closed fds, polling 329*0Sstevel@tonic-gate * results will be indeterministic. 330*0Sstevel@tonic-gate * 331*0Sstevel@tonic-gate * XXX - perhaps log the detection of fd 332*0Sstevel@tonic-gate * reuse? 333*0Sstevel@tonic-gate */ 334*0Sstevel@tonic-gate pdp->pd_fp = fp; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate /* 337*0Sstevel@tonic-gate * XXX - pollrelock() logic needs to know which 338*0Sstevel@tonic-gate * which pollcache lock to grab. It'd be a 339*0Sstevel@tonic-gate * cleaner solution if we could pass pcp as 340*0Sstevel@tonic-gate * an arguement in VOP_POLL interface instead 341*0Sstevel@tonic-gate * of implicitly passing it using thread_t 342*0Sstevel@tonic-gate * struct. On the other hand, changing VOP_POLL 343*0Sstevel@tonic-gate * interface will require all driver/file system 344*0Sstevel@tonic-gate * poll routine to change. May want to revisit 345*0Sstevel@tonic-gate * the tradeoff later. 346*0Sstevel@tonic-gate */ 347*0Sstevel@tonic-gate curthread->t_pollcache = pcp; 348*0Sstevel@tonic-gate error = VOP_POLL(fp->f_vnode, pdp->pd_events, 0, 349*0Sstevel@tonic-gate &revent, &php); 350*0Sstevel@tonic-gate curthread->t_pollcache = NULL; 351*0Sstevel@tonic-gate releasef(fd); 352*0Sstevel@tonic-gate if (error != 0) { 353*0Sstevel@tonic-gate break; 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate /* 356*0Sstevel@tonic-gate * layered devices (e.g. console driver) 357*0Sstevel@tonic-gate * may change the vnode and thus the pollhead 358*0Sstevel@tonic-gate * pointer out from underneath us. 359*0Sstevel@tonic-gate */ 360*0Sstevel@tonic-gate if (php != NULL && pdp->pd_php != NULL && 361*0Sstevel@tonic-gate php != pdp->pd_php) { 362*0Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp); 363*0Sstevel@tonic-gate pdp->pd_php = php; 364*0Sstevel@tonic-gate pollhead_insert(php, pdp); 365*0Sstevel@tonic-gate /* 366*0Sstevel@tonic-gate * The bit should still be set. 367*0Sstevel@tonic-gate */ 368*0Sstevel@tonic-gate ASSERT(BT_TEST(pcp->pc_bitmap, fd)); 369*0Sstevel@tonic-gate goto retry; 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate if (revent != 0) { 373*0Sstevel@tonic-gate pfdp[fdcnt].fd = fd; 374*0Sstevel@tonic-gate pfdp[fdcnt].events = pdp->pd_events; 375*0Sstevel@tonic-gate pfdp[fdcnt].revents = revent; 376*0Sstevel@tonic-gate fdcnt++; 377*0Sstevel@tonic-gate } else if (php != NULL) { 378*0Sstevel@tonic-gate /* 379*0Sstevel@tonic-gate * We clear a bit or cache a poll fd if 380*0Sstevel@tonic-gate * the driver returns a poll head ptr, 381*0Sstevel@tonic-gate * which is expected in the case of 0 382*0Sstevel@tonic-gate * revents. Some buggy driver may return 383*0Sstevel@tonic-gate * NULL php pointer with 0 revents. In 384*0Sstevel@tonic-gate * this case, we just treat the driver as 385*0Sstevel@tonic-gate * "noncachable" and not clearing the bit 386*0Sstevel@tonic-gate * in bitmap. 387*0Sstevel@tonic-gate */ 388*0Sstevel@tonic-gate if ((pdp->pd_php != NULL) && 389*0Sstevel@tonic-gate ((pcp->pc_flag & T_POLLWAKE) == 0)) { 390*0Sstevel@tonic-gate BT_CLEAR(pcp->pc_bitmap, fd); 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate if (pdp->pd_php == NULL) { 393*0Sstevel@tonic-gate pollhead_insert(php, pdp); 394*0Sstevel@tonic-gate pdp->pd_php = php; 395*0Sstevel@tonic-gate } 396*0Sstevel@tonic-gate } 397*0Sstevel@tonic-gate } else { 398*0Sstevel@tonic-gate /* 399*0Sstevel@tonic-gate * No bit set in the range. Check for wrap around. 400*0Sstevel@tonic-gate */ 401*0Sstevel@tonic-gate if (!no_wrap) { 402*0Sstevel@tonic-gate start = 0; 403*0Sstevel@tonic-gate end = ostart - 1; 404*0Sstevel@tonic-gate no_wrap = B_TRUE; 405*0Sstevel@tonic-gate } else { 406*0Sstevel@tonic-gate done = B_TRUE; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate if (!done) { 412*0Sstevel@tonic-gate pcp->pc_mapstart = start; 413*0Sstevel@tonic-gate } 414*0Sstevel@tonic-gate ASSERT(*fdcntp == 0); 415*0Sstevel@tonic-gate *fdcntp = fdcnt; 416*0Sstevel@tonic-gate return (error); 417*0Sstevel@tonic-gate } 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate /*ARGSUSED*/ 420*0Sstevel@tonic-gate static int 421*0Sstevel@tonic-gate dpopen(dev_t *devp, int flag, int otyp, cred_t *credp) 422*0Sstevel@tonic-gate { 423*0Sstevel@tonic-gate minor_t minordev; 424*0Sstevel@tonic-gate dp_entry_t *dpep; 425*0Sstevel@tonic-gate pollcache_t *pcp; 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate ASSERT(devpoll_init); 428*0Sstevel@tonic-gate ASSERT(dptblsize <= MAXMIN); 429*0Sstevel@tonic-gate mutex_enter(&devpoll_lock); 430*0Sstevel@tonic-gate for (minordev = 0; minordev < dptblsize; minordev++) { 431*0Sstevel@tonic-gate if (devpolltbl[minordev] == NULL) { 432*0Sstevel@tonic-gate devpolltbl[minordev] = (dp_entry_t *)RESERVED; 433*0Sstevel@tonic-gate break; 434*0Sstevel@tonic-gate } 435*0Sstevel@tonic-gate } 436*0Sstevel@tonic-gate if (minordev == dptblsize) { 437*0Sstevel@tonic-gate dp_entry_t **newtbl; 438*0Sstevel@tonic-gate size_t oldsize; 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate /* 441*0Sstevel@tonic-gate * Used up every entry in the existing devpoll table. 442*0Sstevel@tonic-gate * Grow the table by DEVPOLLSIZE. 443*0Sstevel@tonic-gate */ 444*0Sstevel@tonic-gate if ((oldsize = dptblsize) >= MAXMIN) { 445*0Sstevel@tonic-gate mutex_exit(&devpoll_lock); 446*0Sstevel@tonic-gate return (ENXIO); 447*0Sstevel@tonic-gate } 448*0Sstevel@tonic-gate dptblsize += DEVPOLLSIZE; 449*0Sstevel@tonic-gate if (dptblsize > MAXMIN) { 450*0Sstevel@tonic-gate dptblsize = MAXMIN; 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate newtbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP); 453*0Sstevel@tonic-gate bcopy(devpolltbl, newtbl, sizeof (caddr_t) * oldsize); 454*0Sstevel@tonic-gate kmem_free(devpolltbl, sizeof (caddr_t) * oldsize); 455*0Sstevel@tonic-gate devpolltbl = newtbl; 456*0Sstevel@tonic-gate devpolltbl[minordev] = (dp_entry_t *)RESERVED; 457*0Sstevel@tonic-gate } 458*0Sstevel@tonic-gate mutex_exit(&devpoll_lock); 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate dpep = kmem_zalloc(sizeof (dp_entry_t), KM_SLEEP); 461*0Sstevel@tonic-gate /* 462*0Sstevel@tonic-gate * allocate a pollcache skeleton here. Delay allocating bitmap 463*0Sstevel@tonic-gate * structures until dpwrite() time, since we don't know the 464*0Sstevel@tonic-gate * optimal size yet. 465*0Sstevel@tonic-gate */ 466*0Sstevel@tonic-gate pcp = pcache_alloc(); 467*0Sstevel@tonic-gate dpep->dpe_pcache = pcp; 468*0Sstevel@tonic-gate pcp->pc_pid = curproc->p_pid; 469*0Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), minordev); /* clone the driver */ 470*0Sstevel@tonic-gate mutex_enter(&devpoll_lock); 471*0Sstevel@tonic-gate ASSERT(minordev < dptblsize); 472*0Sstevel@tonic-gate ASSERT(devpolltbl[minordev] == (dp_entry_t *)RESERVED); 473*0Sstevel@tonic-gate devpolltbl[minordev] = dpep; 474*0Sstevel@tonic-gate mutex_exit(&devpoll_lock); 475*0Sstevel@tonic-gate return (0); 476*0Sstevel@tonic-gate } 477*0Sstevel@tonic-gate 478*0Sstevel@tonic-gate /* 479*0Sstevel@tonic-gate * Write to dev/poll add/remove fd's to/from a cached poll fd set, 480*0Sstevel@tonic-gate * or change poll events for a watched fd. 481*0Sstevel@tonic-gate */ 482*0Sstevel@tonic-gate /*ARGSUSED*/ 483*0Sstevel@tonic-gate static int 484*0Sstevel@tonic-gate dpwrite(dev_t dev, struct uio *uiop, cred_t *credp) 485*0Sstevel@tonic-gate { 486*0Sstevel@tonic-gate minor_t minor; 487*0Sstevel@tonic-gate dp_entry_t *dpep; 488*0Sstevel@tonic-gate pollcache_t *pcp; 489*0Sstevel@tonic-gate pollfd_t *pollfdp, *pfdp; 490*0Sstevel@tonic-gate int error; 491*0Sstevel@tonic-gate ssize_t uiosize; 492*0Sstevel@tonic-gate nfds_t pollfdnum; 493*0Sstevel@tonic-gate struct pollhead *php = NULL; 494*0Sstevel@tonic-gate polldat_t *pdp; 495*0Sstevel@tonic-gate int fd; 496*0Sstevel@tonic-gate file_t *fp; 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate minor = getminor(dev); 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate mutex_enter(&devpoll_lock); 501*0Sstevel@tonic-gate ASSERT(minor < dptblsize); 502*0Sstevel@tonic-gate dpep = devpolltbl[minor]; 503*0Sstevel@tonic-gate ASSERT(dpep != NULL); 504*0Sstevel@tonic-gate mutex_exit(&devpoll_lock); 505*0Sstevel@tonic-gate pcp = dpep->dpe_pcache; 506*0Sstevel@tonic-gate if (curproc->p_pid != pcp->pc_pid) { 507*0Sstevel@tonic-gate return (EACCES); 508*0Sstevel@tonic-gate } 509*0Sstevel@tonic-gate uiosize = uiop->uio_resid; 510*0Sstevel@tonic-gate pollfdnum = uiosize / sizeof (pollfd_t); 511*0Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 512*0Sstevel@tonic-gate if (pollfdnum > (uint_t)rctl_enforced_value( 513*0Sstevel@tonic-gate rctlproc_legacy[RLIMIT_NOFILE], curproc->p_rctls, curproc)) { 514*0Sstevel@tonic-gate (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE], 515*0Sstevel@tonic-gate curproc->p_rctls, curproc, RCA_SAFE); 516*0Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 517*0Sstevel@tonic-gate return (set_errno(EINVAL)); 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 520*0Sstevel@tonic-gate /* 521*0Sstevel@tonic-gate * Copy in the pollfd array. Walk through the array and add 522*0Sstevel@tonic-gate * each polled fd to the cached set. 523*0Sstevel@tonic-gate */ 524*0Sstevel@tonic-gate pollfdp = kmem_alloc(uiosize, KM_SLEEP); 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate /* 527*0Sstevel@tonic-gate * Although /dev/poll uses the write(2) interface to cache fds, it's 528*0Sstevel@tonic-gate * not supposed to function as a seekable device. To prevent offset 529*0Sstevel@tonic-gate * from growing and eventually exceed the maximum, reset the offset 530*0Sstevel@tonic-gate * here for every call. 531*0Sstevel@tonic-gate */ 532*0Sstevel@tonic-gate uiop->uio_loffset = 0; 533*0Sstevel@tonic-gate if ((error = uiomove((caddr_t)pollfdp, uiosize, UIO_WRITE, uiop)) 534*0Sstevel@tonic-gate != 0) { 535*0Sstevel@tonic-gate kmem_free(pollfdp, uiosize); 536*0Sstevel@tonic-gate return (error); 537*0Sstevel@tonic-gate } 538*0Sstevel@tonic-gate /* 539*0Sstevel@tonic-gate * We are about to enter the core portion of dpwrite(). Make sure this 540*0Sstevel@tonic-gate * write has exclusive access in this portion of the code, i.e., no 541*0Sstevel@tonic-gate * other writers in this code and no other readers in dpioctl. 542*0Sstevel@tonic-gate */ 543*0Sstevel@tonic-gate mutex_enter(&dpep->dpe_lock); 544*0Sstevel@tonic-gate dpep->dpe_writerwait++; 545*0Sstevel@tonic-gate while (dpep->dpe_refcnt != 0) { 546*0Sstevel@tonic-gate if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) { 547*0Sstevel@tonic-gate dpep->dpe_writerwait--; 548*0Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock); 549*0Sstevel@tonic-gate kmem_free(pollfdp, uiosize); 550*0Sstevel@tonic-gate return (set_errno(EINTR)); 551*0Sstevel@tonic-gate } 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate dpep->dpe_writerwait--; 554*0Sstevel@tonic-gate dpep->dpe_flag |= DP_WRITER_PRESENT; 555*0Sstevel@tonic-gate dpep->dpe_refcnt++; 556*0Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock); 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate mutex_enter(&pcp->pc_lock); 559*0Sstevel@tonic-gate if (pcp->pc_bitmap == NULL) { 560*0Sstevel@tonic-gate pcache_create(pcp, pollfdnum); 561*0Sstevel@tonic-gate } 562*0Sstevel@tonic-gate for (pfdp = pollfdp; pfdp < pollfdp + pollfdnum; pfdp++) { 563*0Sstevel@tonic-gate fd = pfdp->fd; 564*0Sstevel@tonic-gate if ((uint_t)fd >= P_FINFO(curproc)->fi_nfiles) 565*0Sstevel@tonic-gate continue; 566*0Sstevel@tonic-gate pdp = pcache_lookup_fd(pcp, fd); 567*0Sstevel@tonic-gate if (pfdp->events != POLLREMOVE) { 568*0Sstevel@tonic-gate if (pdp == NULL) { 569*0Sstevel@tonic-gate pdp = pcache_alloc_fd(0); 570*0Sstevel@tonic-gate pdp->pd_fd = fd; 571*0Sstevel@tonic-gate pdp->pd_pcache = pcp; 572*0Sstevel@tonic-gate pcache_insert_fd(pcp, pdp, pollfdnum); 573*0Sstevel@tonic-gate } 574*0Sstevel@tonic-gate ASSERT(pdp->pd_fd == fd); 575*0Sstevel@tonic-gate ASSERT(pdp->pd_pcache == pcp); 576*0Sstevel@tonic-gate if (fd >= pcp->pc_mapsize) { 577*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 578*0Sstevel@tonic-gate pcache_grow_map(pcp, fd); 579*0Sstevel@tonic-gate mutex_enter(&pcp->pc_lock); 580*0Sstevel@tonic-gate } 581*0Sstevel@tonic-gate if (fd > pcp->pc_mapend) { 582*0Sstevel@tonic-gate pcp->pc_mapend = fd; 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate if ((fp = getf(fd)) == NULL) { 585*0Sstevel@tonic-gate /* 586*0Sstevel@tonic-gate * The fd is not valid. Since we can't pass 587*0Sstevel@tonic-gate * this error back in the write() call, set 588*0Sstevel@tonic-gate * the bit in bitmap to force DP_POLL ioctl 589*0Sstevel@tonic-gate * to examine it. 590*0Sstevel@tonic-gate */ 591*0Sstevel@tonic-gate BT_SET(pcp->pc_bitmap, fd); 592*0Sstevel@tonic-gate pdp->pd_events |= pfdp->events; 593*0Sstevel@tonic-gate continue; 594*0Sstevel@tonic-gate } 595*0Sstevel@tonic-gate /* 596*0Sstevel@tonic-gate * Don't do VOP_POLL for an already cached fd with 597*0Sstevel@tonic-gate * same poll events. 598*0Sstevel@tonic-gate */ 599*0Sstevel@tonic-gate if ((pdp->pd_events == pfdp->events) && 600*0Sstevel@tonic-gate (pdp->pd_fp != NULL)) { 601*0Sstevel@tonic-gate /* 602*0Sstevel@tonic-gate * the events are already cached 603*0Sstevel@tonic-gate */ 604*0Sstevel@tonic-gate releasef(fd); 605*0Sstevel@tonic-gate continue; 606*0Sstevel@tonic-gate } 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gate /* 609*0Sstevel@tonic-gate * do VOP_POLL and cache this poll fd. 610*0Sstevel@tonic-gate */ 611*0Sstevel@tonic-gate /* 612*0Sstevel@tonic-gate * XXX - pollrelock() logic needs to know which 613*0Sstevel@tonic-gate * which pollcache lock to grab. It'd be a 614*0Sstevel@tonic-gate * cleaner solution if we could pass pcp as 615*0Sstevel@tonic-gate * an arguement in VOP_POLL interface instead 616*0Sstevel@tonic-gate * of implicitly passing it using thread_t 617*0Sstevel@tonic-gate * struct. On the other hand, changing VOP_POLL 618*0Sstevel@tonic-gate * interface will require all driver/file system 619*0Sstevel@tonic-gate * poll routine to change. May want to revisit 620*0Sstevel@tonic-gate * the tradeoff later. 621*0Sstevel@tonic-gate */ 622*0Sstevel@tonic-gate curthread->t_pollcache = pcp; 623*0Sstevel@tonic-gate error = VOP_POLL(fp->f_vnode, pfdp->events, 0, 624*0Sstevel@tonic-gate &pfdp->revents, &php); 625*0Sstevel@tonic-gate curthread->t_pollcache = NULL; 626*0Sstevel@tonic-gate /* 627*0Sstevel@tonic-gate * We always set the bit when this fd is cached. 628*0Sstevel@tonic-gate * So we don't have to worry about missing a 629*0Sstevel@tonic-gate * pollwakeup between VOP_POLL and pollhead_insert. 630*0Sstevel@tonic-gate * This forces the first DP_POLL to poll this fd. 631*0Sstevel@tonic-gate * Real performance gain comes from subsequent 632*0Sstevel@tonic-gate * DP_POLL. 633*0Sstevel@tonic-gate */ 634*0Sstevel@tonic-gate BT_SET(pcp->pc_bitmap, fd); 635*0Sstevel@tonic-gate if (error != 0) { 636*0Sstevel@tonic-gate releasef(fd); 637*0Sstevel@tonic-gate break; 638*0Sstevel@tonic-gate } 639*0Sstevel@tonic-gate pdp->pd_fp = fp; 640*0Sstevel@tonic-gate pdp->pd_events |= pfdp->events; 641*0Sstevel@tonic-gate if (php != NULL) { 642*0Sstevel@tonic-gate if (pdp->pd_php == NULL) { 643*0Sstevel@tonic-gate pollhead_insert(php, pdp); 644*0Sstevel@tonic-gate pdp->pd_php = php; 645*0Sstevel@tonic-gate } else { 646*0Sstevel@tonic-gate if (pdp->pd_php != php) { 647*0Sstevel@tonic-gate pollhead_delete(pdp->pd_php, 648*0Sstevel@tonic-gate pdp); 649*0Sstevel@tonic-gate pollhead_insert(php, pdp); 650*0Sstevel@tonic-gate pdp->pd_php = php; 651*0Sstevel@tonic-gate } 652*0Sstevel@tonic-gate } 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gate } 655*0Sstevel@tonic-gate releasef(fd); 656*0Sstevel@tonic-gate } else { 657*0Sstevel@tonic-gate if (pdp == NULL) { 658*0Sstevel@tonic-gate continue; 659*0Sstevel@tonic-gate } 660*0Sstevel@tonic-gate ASSERT(pdp->pd_fd == fd); 661*0Sstevel@tonic-gate pdp->pd_fp = NULL; 662*0Sstevel@tonic-gate pdp->pd_events = 0; 663*0Sstevel@tonic-gate ASSERT(pdp->pd_thread == NULL); 664*0Sstevel@tonic-gate if (pdp->pd_php != NULL) { 665*0Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp); 666*0Sstevel@tonic-gate pdp->pd_php = NULL; 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate BT_CLEAR(pcp->pc_bitmap, fd); 669*0Sstevel@tonic-gate } 670*0Sstevel@tonic-gate } 671*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 672*0Sstevel@tonic-gate mutex_enter(&dpep->dpe_lock); 673*0Sstevel@tonic-gate dpep->dpe_flag &= ~DP_WRITER_PRESENT; 674*0Sstevel@tonic-gate ASSERT(dpep->dpe_refcnt == 1); 675*0Sstevel@tonic-gate dpep->dpe_refcnt--; 676*0Sstevel@tonic-gate cv_broadcast(&dpep->dpe_cv); 677*0Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock); 678*0Sstevel@tonic-gate kmem_free(pollfdp, uiosize); 679*0Sstevel@tonic-gate return (error); 680*0Sstevel@tonic-gate } 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gate /*ARGSUSED*/ 683*0Sstevel@tonic-gate static int 684*0Sstevel@tonic-gate dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 685*0Sstevel@tonic-gate { 686*0Sstevel@tonic-gate timestruc_t now; 687*0Sstevel@tonic-gate timestruc_t rqtime; 688*0Sstevel@tonic-gate timestruc_t *rqtp = NULL; 689*0Sstevel@tonic-gate int timecheck = 0; 690*0Sstevel@tonic-gate minor_t minor; 691*0Sstevel@tonic-gate dp_entry_t *dpep; 692*0Sstevel@tonic-gate pollcache_t *pcp; 693*0Sstevel@tonic-gate int error = 0; 694*0Sstevel@tonic-gate STRUCT_DECL(dvpoll, dvpoll); 695*0Sstevel@tonic-gate 696*0Sstevel@tonic-gate if (cmd == DP_POLL) { 697*0Sstevel@tonic-gate /* do this now, before we sleep on DP_WRITER_PRESENT below */ 698*0Sstevel@tonic-gate timecheck = timechanged; 699*0Sstevel@tonic-gate gethrestime(&now); 700*0Sstevel@tonic-gate } 701*0Sstevel@tonic-gate minor = getminor(dev); 702*0Sstevel@tonic-gate mutex_enter(&devpoll_lock); 703*0Sstevel@tonic-gate ASSERT(minor < dptblsize); 704*0Sstevel@tonic-gate dpep = devpolltbl[minor]; 705*0Sstevel@tonic-gate mutex_exit(&devpoll_lock); 706*0Sstevel@tonic-gate ASSERT(dpep != NULL); 707*0Sstevel@tonic-gate pcp = dpep->dpe_pcache; 708*0Sstevel@tonic-gate if (curproc->p_pid != pcp->pc_pid) 709*0Sstevel@tonic-gate return (EACCES); 710*0Sstevel@tonic-gate 711*0Sstevel@tonic-gate mutex_enter(&dpep->dpe_lock); 712*0Sstevel@tonic-gate while ((dpep->dpe_flag & DP_WRITER_PRESENT) || 713*0Sstevel@tonic-gate (dpep->dpe_writerwait != 0)) { 714*0Sstevel@tonic-gate if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) { 715*0Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock); 716*0Sstevel@tonic-gate return (EINTR); 717*0Sstevel@tonic-gate } 718*0Sstevel@tonic-gate } 719*0Sstevel@tonic-gate dpep->dpe_refcnt++; 720*0Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock); 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate switch (cmd) { 723*0Sstevel@tonic-gate case DP_POLL: 724*0Sstevel@tonic-gate { 725*0Sstevel@tonic-gate pollstate_t *ps; 726*0Sstevel@tonic-gate nfds_t nfds; 727*0Sstevel@tonic-gate int fdcnt = 0; 728*0Sstevel@tonic-gate int time_out; 729*0Sstevel@tonic-gate int rval; 730*0Sstevel@tonic-gate 731*0Sstevel@tonic-gate STRUCT_INIT(dvpoll, mode); 732*0Sstevel@tonic-gate error = copyin((caddr_t)arg, STRUCT_BUF(dvpoll), 733*0Sstevel@tonic-gate STRUCT_SIZE(dvpoll)); 734*0Sstevel@tonic-gate if (error) { 735*0Sstevel@tonic-gate DP_REFRELE(dpep); 736*0Sstevel@tonic-gate return (EFAULT); 737*0Sstevel@tonic-gate } 738*0Sstevel@tonic-gate 739*0Sstevel@tonic-gate time_out = STRUCT_FGET(dvpoll, dp_timeout); 740*0Sstevel@tonic-gate if (time_out > 0) { 741*0Sstevel@tonic-gate /* 742*0Sstevel@tonic-gate * Determine the future time of the requested timeout. 743*0Sstevel@tonic-gate */ 744*0Sstevel@tonic-gate rqtp = &rqtime; 745*0Sstevel@tonic-gate rqtp->tv_sec = time_out / MILLISEC; 746*0Sstevel@tonic-gate rqtp->tv_nsec = (time_out % MILLISEC) * MICROSEC; 747*0Sstevel@tonic-gate timespecadd(rqtp, &now); 748*0Sstevel@tonic-gate } 749*0Sstevel@tonic-gate 750*0Sstevel@tonic-gate if ((nfds = STRUCT_FGET(dvpoll, dp_nfds)) == 0) { 751*0Sstevel@tonic-gate /* 752*0Sstevel@tonic-gate * We are just using DP_POLL to sleep, so 753*0Sstevel@tonic-gate * we don't any of the devpoll apparatus. 754*0Sstevel@tonic-gate * Do not check for signals if we have a zero timeout. 755*0Sstevel@tonic-gate */ 756*0Sstevel@tonic-gate DP_REFRELE(dpep); 757*0Sstevel@tonic-gate if (time_out == 0) 758*0Sstevel@tonic-gate return (0); 759*0Sstevel@tonic-gate mutex_enter(&curthread->t_delay_lock); 760*0Sstevel@tonic-gate while ((rval = cv_waituntil_sig(&curthread->t_delay_cv, 761*0Sstevel@tonic-gate &curthread->t_delay_lock, rqtp, timecheck)) > 0) 762*0Sstevel@tonic-gate continue; 763*0Sstevel@tonic-gate mutex_exit(&curthread->t_delay_lock); 764*0Sstevel@tonic-gate return ((rval == 0)? EINTR : 0); 765*0Sstevel@tonic-gate } 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate /* 768*0Sstevel@tonic-gate * XXX It'd be nice not to have to alloc each time. 769*0Sstevel@tonic-gate * But it requires another per thread structure hook. 770*0Sstevel@tonic-gate * Do it later if there is data suggest that. 771*0Sstevel@tonic-gate */ 772*0Sstevel@tonic-gate if ((ps = curthread->t_pollstate) == NULL) { 773*0Sstevel@tonic-gate curthread->t_pollstate = pollstate_create(); 774*0Sstevel@tonic-gate ps = curthread->t_pollstate; 775*0Sstevel@tonic-gate } 776*0Sstevel@tonic-gate if (ps->ps_dpbufsize < nfds) { 777*0Sstevel@tonic-gate struct proc *p = ttoproc(curthread); 778*0Sstevel@tonic-gate /* 779*0Sstevel@tonic-gate * The maximum size should be no large than 780*0Sstevel@tonic-gate * current maximum open file count. 781*0Sstevel@tonic-gate */ 782*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 783*0Sstevel@tonic-gate if (nfds >= p->p_fno_ctl) { 784*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 785*0Sstevel@tonic-gate DP_REFRELE(dpep); 786*0Sstevel@tonic-gate return (EINVAL); 787*0Sstevel@tonic-gate } 788*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 789*0Sstevel@tonic-gate kmem_free(ps->ps_dpbuf, sizeof (pollfd_t) * 790*0Sstevel@tonic-gate ps->ps_dpbufsize); 791*0Sstevel@tonic-gate ps->ps_dpbuf = kmem_zalloc(sizeof (pollfd_t) * 792*0Sstevel@tonic-gate nfds, KM_SLEEP); 793*0Sstevel@tonic-gate ps->ps_dpbufsize = nfds; 794*0Sstevel@tonic-gate } 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate mutex_enter(&pcp->pc_lock); 797*0Sstevel@tonic-gate for (;;) { 798*0Sstevel@tonic-gate pcp->pc_flag = 0; 799*0Sstevel@tonic-gate error = dp_pcache_poll(ps->ps_dpbuf, pcp, nfds, &fdcnt); 800*0Sstevel@tonic-gate if (fdcnt > 0 || error != 0) 801*0Sstevel@tonic-gate break; 802*0Sstevel@tonic-gate 803*0Sstevel@tonic-gate /* 804*0Sstevel@tonic-gate * A pollwake has happened since we polled cache. 805*0Sstevel@tonic-gate */ 806*0Sstevel@tonic-gate if (pcp->pc_flag & T_POLLWAKE) 807*0Sstevel@tonic-gate continue; 808*0Sstevel@tonic-gate 809*0Sstevel@tonic-gate /* 810*0Sstevel@tonic-gate * Sleep until we are notified, signalled, or timed out. 811*0Sstevel@tonic-gate * Do not check for signals if we have a zero timeout. 812*0Sstevel@tonic-gate */ 813*0Sstevel@tonic-gate if (time_out == 0) /* immediate timeout */ 814*0Sstevel@tonic-gate break; 815*0Sstevel@tonic-gate rval = cv_waituntil_sig(&pcp->pc_cv, &pcp->pc_lock, 816*0Sstevel@tonic-gate rqtp, timecheck); 817*0Sstevel@tonic-gate /* 818*0Sstevel@tonic-gate * If we were awakened by a signal or timeout 819*0Sstevel@tonic-gate * then break the loop, else poll again. 820*0Sstevel@tonic-gate */ 821*0Sstevel@tonic-gate if (rval <= 0) { 822*0Sstevel@tonic-gate if (rval == 0) /* signal */ 823*0Sstevel@tonic-gate error = EINTR; 824*0Sstevel@tonic-gate break; 825*0Sstevel@tonic-gate } 826*0Sstevel@tonic-gate } 827*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate if (error == 0 && fdcnt > 0) { 830*0Sstevel@tonic-gate if (copyout(ps->ps_dpbuf, STRUCT_FGETP(dvpoll, 831*0Sstevel@tonic-gate dp_fds), sizeof (pollfd_t) * fdcnt)) { 832*0Sstevel@tonic-gate DP_REFRELE(dpep); 833*0Sstevel@tonic-gate return (EFAULT); 834*0Sstevel@tonic-gate } 835*0Sstevel@tonic-gate *rvalp = fdcnt; 836*0Sstevel@tonic-gate } 837*0Sstevel@tonic-gate break; 838*0Sstevel@tonic-gate } 839*0Sstevel@tonic-gate 840*0Sstevel@tonic-gate case DP_ISPOLLED: 841*0Sstevel@tonic-gate { 842*0Sstevel@tonic-gate pollfd_t pollfd; 843*0Sstevel@tonic-gate polldat_t *pdp; 844*0Sstevel@tonic-gate 845*0Sstevel@tonic-gate STRUCT_INIT(dvpoll, mode); 846*0Sstevel@tonic-gate error = copyin((caddr_t)arg, &pollfd, sizeof (pollfd_t)); 847*0Sstevel@tonic-gate if (error) { 848*0Sstevel@tonic-gate DP_REFRELE(dpep); 849*0Sstevel@tonic-gate return (EFAULT); 850*0Sstevel@tonic-gate } 851*0Sstevel@tonic-gate mutex_enter(&pcp->pc_lock); 852*0Sstevel@tonic-gate if (pcp->pc_hash == NULL) { 853*0Sstevel@tonic-gate /* 854*0Sstevel@tonic-gate * No Need to search because no poll fd 855*0Sstevel@tonic-gate * has been cached. 856*0Sstevel@tonic-gate */ 857*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 858*0Sstevel@tonic-gate DP_REFRELE(dpep); 859*0Sstevel@tonic-gate return (0); 860*0Sstevel@tonic-gate } 861*0Sstevel@tonic-gate if (pollfd.fd < 0) { 862*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 863*0Sstevel@tonic-gate break; 864*0Sstevel@tonic-gate } 865*0Sstevel@tonic-gate pdp = pcache_lookup_fd(pcp, pollfd.fd); 866*0Sstevel@tonic-gate if ((pdp != NULL) && (pdp->pd_fd == pollfd.fd) && 867*0Sstevel@tonic-gate (pdp->pd_fp != NULL)) { 868*0Sstevel@tonic-gate pollfd.revents = pdp->pd_events; 869*0Sstevel@tonic-gate if (copyout(&pollfd, (caddr_t)arg, sizeof (pollfd_t))) { 870*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 871*0Sstevel@tonic-gate DP_REFRELE(dpep); 872*0Sstevel@tonic-gate return (EFAULT); 873*0Sstevel@tonic-gate } 874*0Sstevel@tonic-gate *rvalp = 1; 875*0Sstevel@tonic-gate } 876*0Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 877*0Sstevel@tonic-gate break; 878*0Sstevel@tonic-gate } 879*0Sstevel@tonic-gate 880*0Sstevel@tonic-gate default: 881*0Sstevel@tonic-gate DP_REFRELE(dpep); 882*0Sstevel@tonic-gate return (EINVAL); 883*0Sstevel@tonic-gate } 884*0Sstevel@tonic-gate DP_REFRELE(dpep); 885*0Sstevel@tonic-gate return (error); 886*0Sstevel@tonic-gate } 887*0Sstevel@tonic-gate 888*0Sstevel@tonic-gate /*ARGSUSED*/ 889*0Sstevel@tonic-gate static int 890*0Sstevel@tonic-gate dppoll(dev_t dev, short events, int anyyet, short *reventsp, 891*0Sstevel@tonic-gate struct pollhead **phpp) 892*0Sstevel@tonic-gate { 893*0Sstevel@tonic-gate /* 894*0Sstevel@tonic-gate * Polling on a /dev/poll fd is not fully supported yet. 895*0Sstevel@tonic-gate */ 896*0Sstevel@tonic-gate *reventsp = POLLERR; 897*0Sstevel@tonic-gate return (0); 898*0Sstevel@tonic-gate } 899*0Sstevel@tonic-gate 900*0Sstevel@tonic-gate /* 901*0Sstevel@tonic-gate * devpoll close should do enough clean up before the pollcache is deleted, 902*0Sstevel@tonic-gate * i.e., it should ensure no one still references the pollcache later. 903*0Sstevel@tonic-gate * There is no "permission" check in here. Any process having the last 904*0Sstevel@tonic-gate * reference of this /dev/poll fd can close. 905*0Sstevel@tonic-gate */ 906*0Sstevel@tonic-gate /*ARGSUSED*/ 907*0Sstevel@tonic-gate static int 908*0Sstevel@tonic-gate dpclose(dev_t dev, int flag, int otyp, cred_t *credp) 909*0Sstevel@tonic-gate { 910*0Sstevel@tonic-gate minor_t minor; 911*0Sstevel@tonic-gate dp_entry_t *dpep; 912*0Sstevel@tonic-gate pollcache_t *pcp; 913*0Sstevel@tonic-gate int i; 914*0Sstevel@tonic-gate polldat_t **hashtbl; 915*0Sstevel@tonic-gate polldat_t *pdp; 916*0Sstevel@tonic-gate 917*0Sstevel@tonic-gate minor = getminor(dev); 918*0Sstevel@tonic-gate 919*0Sstevel@tonic-gate mutex_enter(&devpoll_lock); 920*0Sstevel@tonic-gate dpep = devpolltbl[minor]; 921*0Sstevel@tonic-gate ASSERT(dpep != NULL); 922*0Sstevel@tonic-gate devpolltbl[minor] = NULL; 923*0Sstevel@tonic-gate mutex_exit(&devpoll_lock); 924*0Sstevel@tonic-gate pcp = dpep->dpe_pcache; 925*0Sstevel@tonic-gate ASSERT(pcp != NULL); 926*0Sstevel@tonic-gate /* 927*0Sstevel@tonic-gate * At this point, no other lwp can access this pollcache via the 928*0Sstevel@tonic-gate * /dev/poll fd. This pollcache is going away, so do the clean 929*0Sstevel@tonic-gate * up without the pc_lock. 930*0Sstevel@tonic-gate */ 931*0Sstevel@tonic-gate hashtbl = pcp->pc_hash; 932*0Sstevel@tonic-gate for (i = 0; i < pcp->pc_hashsize; i++) { 933*0Sstevel@tonic-gate for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) { 934*0Sstevel@tonic-gate if (pdp->pd_php != NULL) { 935*0Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp); 936*0Sstevel@tonic-gate pdp->pd_php = NULL; 937*0Sstevel@tonic-gate pdp->pd_fp = NULL; 938*0Sstevel@tonic-gate } 939*0Sstevel@tonic-gate } 940*0Sstevel@tonic-gate } 941*0Sstevel@tonic-gate /* 942*0Sstevel@tonic-gate * pollwakeup() may still interact with this pollcache. Wait until 943*0Sstevel@tonic-gate * it is done. 944*0Sstevel@tonic-gate */ 945*0Sstevel@tonic-gate mutex_enter(&pcp->pc_no_exit); 946*0Sstevel@tonic-gate ASSERT(pcp->pc_busy >= 0); 947*0Sstevel@tonic-gate while (pcp->pc_busy > 0) 948*0Sstevel@tonic-gate cv_wait(&pcp->pc_busy_cv, &pcp->pc_no_exit); 949*0Sstevel@tonic-gate mutex_exit(&pcp->pc_no_exit); 950*0Sstevel@tonic-gate pcache_destroy(pcp); 951*0Sstevel@tonic-gate ASSERT(dpep->dpe_refcnt == 0); 952*0Sstevel@tonic-gate kmem_free(dpep, sizeof (dp_entry_t)); 953*0Sstevel@tonic-gate return (0); 954*0Sstevel@tonic-gate } 955