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 2003 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 #ifndef _SYS_POLL_IMPL_H 28*0Sstevel@tonic-gate #define _SYS_POLL_IMPL_H 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate /* 33*0Sstevel@tonic-gate * Caching Poll Subsystem: 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * Each kernel thread (1), if engaged in poll system call, has a reference to 36*0Sstevel@tonic-gate * a pollstate_t (2), which contains relevant flags and locks. The pollstate_t 37*0Sstevel@tonic-gate * contains a pointer to a pcache_t (3), which caches the state of previous 38*0Sstevel@tonic-gate * calls to poll. A bitmap (4) is stored inside the poll cache, where each 39*0Sstevel@tonic-gate * bit represents a file descriptor. The bits are set if the corresponding 40*0Sstevel@tonic-gate * device has a polled event pending. Only fds with their bit set will be 41*0Sstevel@tonic-gate * examined on the next poll invocation. The pollstate_t also contains a list 42*0Sstevel@tonic-gate * of fd sets (5), which are represented by the pollcacheset_t type. These 43*0Sstevel@tonic-gate * structures keep track of the pollfd_t arrays (6) passed in from userland. 44*0Sstevel@tonic-gate * Each polled file descriptor has a corresponding polldat_t which can be 45*0Sstevel@tonic-gate * chained onto a device's pollhead, and these are kept in a hash table (7) 46*0Sstevel@tonic-gate * inside the pcache_t. The hash table allows efficient conversion of a 47*0Sstevel@tonic-gate * given fd to its corresponding polldat_t. 48*0Sstevel@tonic-gate * 49*0Sstevel@tonic-gate * (1) (2) 50*0Sstevel@tonic-gate * +-----------+ +-------------+ 51*0Sstevel@tonic-gate * | kthread_t |--->| pollstate_t |-->+-------------+ (6) 52*0Sstevel@tonic-gate * +-----------+ +-------------+(5)| pcacheset_t |->[_][_][_][_] pollfd_t 53*0Sstevel@tonic-gate * | +-------------+ 54*0Sstevel@tonic-gate * | | pcacheset_t |->[_][_][_][_] pollfd_t 55*0Sstevel@tonic-gate * (1a) | +-------------+ 56*0Sstevel@tonic-gate * +---------------+ | 57*0Sstevel@tonic-gate * | /dev/poll tbl | | 58*0Sstevel@tonic-gate * +-v-------------+ | 59*0Sstevel@tonic-gate * | | 60*0Sstevel@tonic-gate * +------------------+ | 61*0Sstevel@tonic-gate * (7) (3) V v 62*0Sstevel@tonic-gate * polldat hash +-------------+ (4) bitmap representing fd space 63*0Sstevel@tonic-gate * [_][_][_][_]<----| |--->000010010010001010101010101010110 64*0Sstevel@tonic-gate * | | | | | pollcache_t | 65*0Sstevel@tonic-gate * . v . . | | 66*0Sstevel@tonic-gate * [polldat_t] +-------------+ 67*0Sstevel@tonic-gate * | 68*0Sstevel@tonic-gate * [polldat_t] 69*0Sstevel@tonic-gate * | 70*0Sstevel@tonic-gate * v 71*0Sstevel@tonic-gate * NULL 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * 74*0Sstevel@tonic-gate * Both poll system call and /dev/poll use the pollcache_t structure 75*0Sstevel@tonic-gate * definition and the routines managing the structure. But poll(2) and 76*0Sstevel@tonic-gate * /dev/poll have their own copy of the structures. The /dev/poll driver 77*0Sstevel@tonic-gate * table (1a) contains an array of pointers, each pointing at a pcache_t 78*0Sstevel@tonic-gate * struct (3). A device minor number is used as an device table index. 79*0Sstevel@tonic-gate * 80*0Sstevel@tonic-gate */ 81*0Sstevel@tonic-gate #include <sys/poll.h> 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER) 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate #include <sys/thread.h> 86*0Sstevel@tonic-gate #include <sys/file.h> 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate #ifdef __cplusplus 89*0Sstevel@tonic-gate extern "C" { 90*0Sstevel@tonic-gate #endif 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate /* 93*0Sstevel@tonic-gate * description of pollcacheset structure 94*0Sstevel@tonic-gate */ 95*0Sstevel@tonic-gate typedef struct pollcacheset { 96*0Sstevel@tonic-gate uintptr_t pcs_usradr; /* usr pollfd array address */ 97*0Sstevel@tonic-gate pollfd_t *pcs_pollfd; /* cached poll lists */ 98*0Sstevel@tonic-gate size_t pcs_nfds; /* number of poll fd in cached list */ 99*0Sstevel@tonic-gate ulong_t pcs_count; /* for LU replacement policy */ 100*0Sstevel@tonic-gate } pollcacheset_t; 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate #define POLLFDSETS 2 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate /* 105*0Sstevel@tonic-gate * State information kept by each polling thread 106*0Sstevel@tonic-gate */ 107*0Sstevel@tonic-gate typedef struct pollstate { 108*0Sstevel@tonic-gate pollfd_t *ps_pollfd; /* hold the current poll list */ 109*0Sstevel@tonic-gate size_t ps_nfds; /* size of ps_pollfd */ 110*0Sstevel@tonic-gate kmutex_t ps_lock; /* mutex for sleep/wakeup */ 111*0Sstevel@tonic-gate struct pollcache *ps_pcache; /* cached poll fd set */ 112*0Sstevel@tonic-gate pollcacheset_t *ps_pcacheset; /* cached poll lists */ 113*0Sstevel@tonic-gate int ps_nsets; /* no. of cached poll sets */ 114*0Sstevel@tonic-gate pollfd_t *ps_dpbuf; /* return pollfd buf used by devpoll */ 115*0Sstevel@tonic-gate size_t ps_dpbufsize; /* size of ps_dpbuf */ 116*0Sstevel@tonic-gate } pollstate_t; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 119*0Sstevel@tonic-gate * poll cache size defines 120*0Sstevel@tonic-gate */ 121*0Sstevel@tonic-gate #define POLLCHUNKSHIFT 8 /* hash table increment size is 256 */ 122*0Sstevel@tonic-gate #define POLLHASHCHUNKSZ (1 << POLLCHUNKSHIFT) 123*0Sstevel@tonic-gate #define POLLHASHINC 2 /* poll hash table growth factor */ 124*0Sstevel@tonic-gate #define POLLHASHTHRESHOLD 2 /* poll hash list length threshold */ 125*0Sstevel@tonic-gate #define POLLHASH(x, y) ((y) % (x)) /* poll hash function */ 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate /* 128*0Sstevel@tonic-gate * poll.c assumes the POLLMAPCHUNK is power of 2 129*0Sstevel@tonic-gate */ 130*0Sstevel@tonic-gate #define POLLMAPCHUNK 2048 /* bitmap inc -- each for 2K of polled fd's */ 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate /* 133*0Sstevel@tonic-gate * used to refrence from watched fd back to the fd position in cached 134*0Sstevel@tonic-gate * poll list for quick revents update. 135*0Sstevel@tonic-gate */ 136*0Sstevel@tonic-gate typedef struct xref { 137*0Sstevel@tonic-gate ssize_t xf_position; /* xref fd position in poll fd list */ 138*0Sstevel@tonic-gate short xf_refcnt; /* ref cnt of same fd in poll list */ 139*0Sstevel@tonic-gate } xref_t; 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate #define POLLPOSINVAL (-1L) /* xf_position is invalid */ 142*0Sstevel@tonic-gate #define POLLPOSTRANS (-2L) /* xf_position is transient state */ 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /* 145*0Sstevel@tonic-gate * polldat is an entry for a cached poll fd. A polldat struct can be in 146*0Sstevel@tonic-gate * poll cache table as well as on pollhead ph_list, which is used by 147*0Sstevel@tonic-gate * pollwakeup to wake up a sleeping poller. There should be one polldat 148*0Sstevel@tonic-gate * per polled fd hanging off pollstate struct. 149*0Sstevel@tonic-gate */ 150*0Sstevel@tonic-gate typedef struct polldat { 151*0Sstevel@tonic-gate int pd_fd; /* cached poll fd */ 152*0Sstevel@tonic-gate int pd_events; /* union of all polled events */ 153*0Sstevel@tonic-gate file_t *pd_fp; /* used to detect fd reuse */ 154*0Sstevel@tonic-gate pollhead_t *pd_php; /* used to undo poll registration */ 155*0Sstevel@tonic-gate kthread_t *pd_thread; /* used for waking up a sleep thrd */ 156*0Sstevel@tonic-gate struct pollcache *pd_pcache; /* a ptr to the pollcache of this fd */ 157*0Sstevel@tonic-gate struct polldat *pd_next; /* next on pollhead's ph_list */ 158*0Sstevel@tonic-gate struct polldat *pd_hashnext; /* next on pollhead's ph_list */ 159*0Sstevel@tonic-gate int pd_count; /* total count from all ref'ed sets */ 160*0Sstevel@tonic-gate int pd_nsets; /* num of xref sets, used by poll(2) */ 161*0Sstevel@tonic-gate xref_t *pd_ref; /* ptr to xref info, 1 for each set */ 162*0Sstevel@tonic-gate struct port_kevent *pd_portev; /* associated port event struct */ 163*0Sstevel@tonic-gate } polldat_t; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* 166*0Sstevel@tonic-gate * One cache for each thread that polls. Points to a bitmap (used by pollwakeup) 167*0Sstevel@tonic-gate * and a hash table of polldats. 168*0Sstevel@tonic-gate * The offset of pc_lock field must be kept in sync with the pc_lock offset 169*0Sstevel@tonic-gate * of port_fdcache_t, both structs implement pc_lock with offset 0 (see also 170*0Sstevel@tonic-gate * pollrelock()). 171*0Sstevel@tonic-gate */ 172*0Sstevel@tonic-gate typedef struct pollcache { 173*0Sstevel@tonic-gate kmutex_t pc_lock; /* lock to protect pollcache */ 174*0Sstevel@tonic-gate ulong_t *pc_bitmap; /* point to poll fd bitmap */ 175*0Sstevel@tonic-gate polldat_t **pc_hash; /* points to a hash table of ptrs */ 176*0Sstevel@tonic-gate int pc_mapend; /* the largest fd encountered so far */ 177*0Sstevel@tonic-gate int pc_mapsize; /* the size of current map */ 178*0Sstevel@tonic-gate int pc_hashsize; /* the size of current hash table */ 179*0Sstevel@tonic-gate int pc_fdcount; /* track how many fd's are hashed */ 180*0Sstevel@tonic-gate int pc_flag; /* see pc_flag define below */ 181*0Sstevel@tonic-gate int pc_busy; /* can only exit when its 0 */ 182*0Sstevel@tonic-gate kmutex_t pc_no_exit; /* protects pc_busy*, can't be nested */ 183*0Sstevel@tonic-gate kcondvar_t pc_busy_cv; /* cv to wait on if ps_busy != 0 */ 184*0Sstevel@tonic-gate kcondvar_t pc_cv; /* cv to wait on if needed */ 185*0Sstevel@tonic-gate pid_t pc_pid; /* for check acc rights, devpoll only */ 186*0Sstevel@tonic-gate int pc_mapstart; /* where search start, devpoll only */ 187*0Sstevel@tonic-gate } pollcache_t; 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate /* pc_flag */ 190*0Sstevel@tonic-gate #define T_POLLWAKE 0x02 /* pollwakeup() occurred */ 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate #if defined(_KERNEL) 193*0Sstevel@tonic-gate /* 194*0Sstevel@tonic-gate * Internal routines. 195*0Sstevel@tonic-gate */ 196*0Sstevel@tonic-gate extern void pollnotify(pollcache_t *, int); 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate /* 199*0Sstevel@tonic-gate * public poll head interfaces (see poll.h): 200*0Sstevel@tonic-gate * 201*0Sstevel@tonic-gate * pollhead_clean clean up all polldats on a pollhead list 202*0Sstevel@tonic-gate */ 203*0Sstevel@tonic-gate extern void pollhead_clean(pollhead_t *); 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate /* 206*0Sstevel@tonic-gate * private poll head interfaces: 207*0Sstevel@tonic-gate * 208*0Sstevel@tonic-gate * pollhead_insert adds a polldat to a pollhead list 209*0Sstevel@tonic-gate * pollhead_delete removes a polldat from a pollhead list 210*0Sstevel@tonic-gate */ 211*0Sstevel@tonic-gate extern void pollhead_insert(pollhead_t *, polldat_t *); 212*0Sstevel@tonic-gate extern void pollhead_delete(pollhead_t *, polldat_t *); 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* 215*0Sstevel@tonic-gate * poll state interfaces: 216*0Sstevel@tonic-gate * 217*0Sstevel@tonic-gate * pollstate_create creates per-thread pollstate 218*0Sstevel@tonic-gate * pollstate_destroy cleans up per-thread pollstate 219*0Sstevel@tonic-gate */ 220*0Sstevel@tonic-gate extern pollstate_t *pollstate_create(void); 221*0Sstevel@tonic-gate extern void pollstate_destroy(pollstate_t *); 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate /* 224*0Sstevel@tonic-gate * public pcache interfaces: 225*0Sstevel@tonic-gate * 226*0Sstevel@tonic-gate * pcache_alloc allocate a poll cache skeleton 227*0Sstevel@tonic-gate * pcache_create creates all poll cache supporting data struct 228*0Sstevel@tonic-gate * pcache_insert cache a poll fd, calls pcache_insert_fd 229*0Sstevel@tonic-gate * pcache_lookup given an fd list, returns a cookie 230*0Sstevel@tonic-gate * pcache_poll polls the cache for fd's having events on them 231*0Sstevel@tonic-gate * pcache_clean clean up all the pollhead and fpollinfo reference 232*0Sstevel@tonic-gate * pcache_destroy destroys the pcache 233*0Sstevel@tonic-gate */ 234*0Sstevel@tonic-gate extern pollcache_t *pcache_alloc(); 235*0Sstevel@tonic-gate extern void pcache_create(pollcache_t *, nfds_t); 236*0Sstevel@tonic-gate extern int pcache_insert(pollstate_t *, file_t *, pollfd_t *, int *, ssize_t, 237*0Sstevel@tonic-gate int); 238*0Sstevel@tonic-gate extern int pcache_poll(pollfd_t *, pollstate_t *, nfds_t, int *, int); 239*0Sstevel@tonic-gate extern void pcache_clean(pollcache_t *); 240*0Sstevel@tonic-gate extern void pcache_destroy(pollcache_t *); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate /* 243*0Sstevel@tonic-gate * private pcache interfaces: 244*0Sstevel@tonic-gate * 245*0Sstevel@tonic-gate * pcache_lookup_fd lookup an fd, returns a polldat 246*0Sstevel@tonic-gate * pcache_alloc_fd allocates and returns a polldat 247*0Sstevel@tonic-gate * pcache_insert_fd insert an fd into pcache (called by pcache_insert) 248*0Sstevel@tonic-gate * pcache_delete_fd insert an fd into pcache (called by pcacheset_delete_fd) 249*0Sstevel@tonic-gate * pcache_grow_hashtbl grows the pollcache hash table and rehash 250*0Sstevel@tonic-gate * pcache_grow_map grows the pollcache bitmap 251*0Sstevel@tonic-gate * pcache_update_xref update cross ref (from polldat back to cacheset) info 252*0Sstevel@tonic-gate * pcache_clean_entry cleanup an entry in pcache and more... 253*0Sstevel@tonic-gate */ 254*0Sstevel@tonic-gate extern polldat_t *pcache_lookup_fd(pollcache_t *, int); 255*0Sstevel@tonic-gate extern polldat_t *pcache_alloc_fd(int); 256*0Sstevel@tonic-gate extern void pcache_insert_fd(pollcache_t *, polldat_t *, nfds_t); 257*0Sstevel@tonic-gate extern int pcache_delete_fd(pollstate_t *, int, size_t, int, uint_t); 258*0Sstevel@tonic-gate extern void pcache_grow_hashtbl(pollcache_t *, nfds_t); 259*0Sstevel@tonic-gate extern void pcache_grow_map(pollcache_t *, int); 260*0Sstevel@tonic-gate extern void pcache_update_xref(pollcache_t *, int, ssize_t, int); 261*0Sstevel@tonic-gate extern void pcache_clean_entry(pollstate_t *, int); 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate /* 264*0Sstevel@tonic-gate * pcacheset interfaces: 265*0Sstevel@tonic-gate * 266*0Sstevel@tonic-gate * pcacheset_create creates new pcachesets (easier for dynamic pcachesets) 267*0Sstevel@tonic-gate * pcacheset_destroy destroys a pcacheset 268*0Sstevel@tonic-gate * pcacheset_cache_list caches and polls a new poll list 269*0Sstevel@tonic-gate * pcacheset_remove_list removes (usually a partial) cached poll list 270*0Sstevel@tonic-gate * pcacheset_resolve resolves extant pcacheset and fd list 271*0Sstevel@tonic-gate * pcacheset_cmp compares a pcacheset with an fd list 272*0Sstevel@tonic-gate * pcacheset_invalidate invalidate entries in pcachesets 273*0Sstevel@tonic-gate * pcacheset_reset_count resets the usage counter of pcachesets 274*0Sstevel@tonic-gate * pcacheset_replace selects a poll cacheset for replacement 275*0Sstevel@tonic-gate */ 276*0Sstevel@tonic-gate extern pollcacheset_t *pcacheset_create(int); 277*0Sstevel@tonic-gate extern void pcacheset_destroy(pollcacheset_t *, int); 278*0Sstevel@tonic-gate extern int pcacheset_cache_list(pollstate_t *, pollfd_t *, int *, int); 279*0Sstevel@tonic-gate extern void pcacheset_remove_list(pollstate_t *, pollfd_t *, int, int, int, 280*0Sstevel@tonic-gate int); 281*0Sstevel@tonic-gate extern int pcacheset_resolve(pollstate_t *, nfds_t, int *, int); 282*0Sstevel@tonic-gate extern int pcacheset_cmp(pollfd_t *, pollfd_t *, pollfd_t *, int); 283*0Sstevel@tonic-gate extern void pcacheset_invalidate(pollstate_t *, polldat_t *); 284*0Sstevel@tonic-gate extern void pcacheset_reset_count(pollstate_t *, int); 285*0Sstevel@tonic-gate extern int pcacheset_replace(pollstate_t *); 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate #endif /* defined(_KERNEL) */ 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate #ifdef __cplusplus 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate #endif 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate #endif /* defined(_KERNEL) || defined(_KMEMUSER) */ 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate #endif /* _SYS_POLL_IMPL_H */ 296