10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51885Sraf * Common Development and Distribution License (the "License").
61885Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211885Sraf
220Sstevel@tonic-gate /*
23*4863Spraks * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/cred.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
343898Srsb #include <sys/vfs_opreg.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/kstat.h>
420Sstevel@tonic-gate #include <sys/port_impl.h>
430Sstevel@tonic-gate #include <sys/task.h>
440Sstevel@tonic-gate #include <sys/project.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * Event Ports can be shared across threads or across processes.
480Sstevel@tonic-gate * Every thread/process can use an own event port or a group of them
490Sstevel@tonic-gate * can use a single port. A major request was also to get the ability
500Sstevel@tonic-gate * to submit user-defined events to a port. The idea of the
510Sstevel@tonic-gate * user-defined events is to use the event ports for communication between
520Sstevel@tonic-gate * threads/processes (like message queues). User defined-events are queued
530Sstevel@tonic-gate * in a port with the same priority as other event types.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * Events are delivered only once. The thread/process which is waiting
560Sstevel@tonic-gate * for events with the "highest priority" (priority here is related to the
570Sstevel@tonic-gate * internal strategy to wakeup waiting threads) will retrieve the event,
580Sstevel@tonic-gate * all other threads/processes will not be notified. There is also
590Sstevel@tonic-gate * the requirement to have events which should be submitted immediately
600Sstevel@tonic-gate * to all "waiting" threads. That is the main task of the alert event.
610Sstevel@tonic-gate * The alert event is submitted by the application to a port. The port
620Sstevel@tonic-gate * changes from a standard mode to the alert mode. Now all waiting threads
630Sstevel@tonic-gate * will be awaken immediately and they will return with the alert event.
640Sstevel@tonic-gate * Threads trying to retrieve events from a port in alert mode will
650Sstevel@tonic-gate * return immediately with the alert event.
660Sstevel@tonic-gate *
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * An event port is like a kernel queue, which accept events submitted from
690Sstevel@tonic-gate * user level as well as events submitted from kernel sub-systems. Sub-systems
700Sstevel@tonic-gate * able to submit events to a port are the so-called "event sources".
710Sstevel@tonic-gate * Current event sources:
720Sstevel@tonic-gate * PORT_SOURCE_AIO : events submitted per transaction completion from
730Sstevel@tonic-gate * POSIX-I/O framework.
740Sstevel@tonic-gate * PORT_SOURCE_TIMER : events submitted when a timer fires
750Sstevel@tonic-gate * (see timer_create(3RT)).
760Sstevel@tonic-gate * PORT_SOURCE_FD : events submitted per file descriptor (see poll(2)).
770Sstevel@tonic-gate * PORT_SOURCE_ALERT : events submitted from user. This is not really a
780Sstevel@tonic-gate * single event, this is actually a port mode
790Sstevel@tonic-gate * (see port_alert(3c)).
800Sstevel@tonic-gate * PORT_SOURCE_USER : events submitted by applications with
810Sstevel@tonic-gate * port_send(3c) or port_sendn(3c).
82*4863Spraks * PORT_SOURCE_FILE : events submitted per file being watched for file
83*4863Spraks * change events (see port_create(3c).
840Sstevel@tonic-gate *
850Sstevel@tonic-gate * There is a user API implemented in the libc library as well as a
860Sstevel@tonic-gate * kernel API implemented in port_subr.c in genunix.
870Sstevel@tonic-gate * The available user API functions are:
880Sstevel@tonic-gate * port_create() : create a port as a file descriptor of portfs file system
890Sstevel@tonic-gate * The standard close(2) function closes a port.
900Sstevel@tonic-gate * port_associate() : associate a file descriptor with a port to be able to
910Sstevel@tonic-gate * retrieve events from that file descriptor.
920Sstevel@tonic-gate * port_dissociate(): remove the association of a file descriptor with a port.
930Sstevel@tonic-gate * port_alert() : set/unset a port in alert mode
940Sstevel@tonic-gate * port_send() : send an event of type PORT_SOURCE_USER to a port
950Sstevel@tonic-gate * port_sendn() : send an event of type PORT_SOURCE_USER to a list of ports
960Sstevel@tonic-gate * port_get() : retrieve a single event from a port
970Sstevel@tonic-gate * port_getn() : retrieve a list of events from a port
980Sstevel@tonic-gate *
990Sstevel@tonic-gate * The available kernel API functions are:
1000Sstevel@tonic-gate * port_allocate_event(): allocate an event slot/structure of/from a port
1010Sstevel@tonic-gate * port_init_event() : set event data in the event structure
1020Sstevel@tonic-gate * port_send_event() : send event to a port
1030Sstevel@tonic-gate * port_free_event() : deliver allocated slot/structure back to a port
1040Sstevel@tonic-gate * port_associate_ksource(): associate a kernel event source with a port
1050Sstevel@tonic-gate * port_dissociate_ksource(): dissociate a kernel event source from a port
1060Sstevel@tonic-gate *
1070Sstevel@tonic-gate * The libc implementation consists of small functions which pass the
1080Sstevel@tonic-gate * arguments to the kernel using the "portfs" system call. It means, all the
1090Sstevel@tonic-gate * synchronisation work is being done in the kernel. The "portfs" system
1100Sstevel@tonic-gate * call loads the portfs file system into the kernel.
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * PORT CREATION
1130Sstevel@tonic-gate * The first function to be used is port_create() which internally creates
1140Sstevel@tonic-gate * a vnode and a portfs node. The portfs node is represented by the port_t
1150Sstevel@tonic-gate * structure, which again includes all the data necessary to control a port.
1160Sstevel@tonic-gate * port_create() returns a file descriptor, which needs to be used in almost
1170Sstevel@tonic-gate * all other event port functions.
1180Sstevel@tonic-gate * The maximum number of ports per system is controlled by the resource
1190Sstevel@tonic-gate * control: project:port-max-ids.
1200Sstevel@tonic-gate *
1210Sstevel@tonic-gate * EVENT GENERATION
1220Sstevel@tonic-gate * The second step is the triggering of events, which could be sent to a port.
1230Sstevel@tonic-gate * Every event source implements an own method to generate events for a port:
1240Sstevel@tonic-gate * PORT_SOURCE_AIO:
1250Sstevel@tonic-gate * The sigevent structure of the standard POSIX-IO functions
1260Sstevel@tonic-gate * was extended by an additional notification type.
1270Sstevel@tonic-gate * Standard notification types:
1280Sstevel@tonic-gate * SIGEV_NONE, SIGEV_SIGNAL and SIGEV_THREAD
1290Sstevel@tonic-gate * Event ports introduced now SIGEV_PORT.
1300Sstevel@tonic-gate * The notification type SIGEV_PORT specifies that a structure
1310Sstevel@tonic-gate * of type port_notify_t has to be attached to the sigev_value.
1320Sstevel@tonic-gate * The port_notify_t structure contains the event port file
1330Sstevel@tonic-gate * descriptor and a user-defined pointer.
1340Sstevel@tonic-gate * Internally the AIO implementation will use the kernel API
1350Sstevel@tonic-gate * functions to allocate an event port slot per transaction (aiocb)
1360Sstevel@tonic-gate * and sent the event to the port as soon as the transaction completes.
1370Sstevel@tonic-gate * All the events submitted per transaction are of type
1380Sstevel@tonic-gate * PORT_SOURCE_AIO.
1390Sstevel@tonic-gate * PORT_SOURCE_TIMER:
1400Sstevel@tonic-gate * The timer_create() function uses the same method as the
1410Sstevel@tonic-gate * PORT_SOURCE_AIO event source. It also uses the sigevent structure
1420Sstevel@tonic-gate * to deliver the port information.
1430Sstevel@tonic-gate * Internally the timer code will allocate a single event slot/struct
1440Sstevel@tonic-gate * per timer and it will send the timer event as soon as the timer
1450Sstevel@tonic-gate * fires. If the timer-fired event is not delivered to the application
1460Sstevel@tonic-gate * before the next period elapsed, then an overrun counter will be
1470Sstevel@tonic-gate * incremented. The timer event source uses a callback function to
1480Sstevel@tonic-gate * detect the delivery of the event to the application. At that time
1490Sstevel@tonic-gate * the timer callback function will update the event overrun counter.
1500Sstevel@tonic-gate * PORT_SOURCE_FD:
1510Sstevel@tonic-gate * This event source uses the port_associate() function to allocate
1520Sstevel@tonic-gate * an event slot/struct from a port. The application defines in the
1530Sstevel@tonic-gate * events argument of port_associate() the type of events which it is
1540Sstevel@tonic-gate * interested on.
1550Sstevel@tonic-gate * The internal pollwakeup() function is used by all the file
1560Sstevel@tonic-gate * systems --which are supporting the VOP_POLL() interface- to notify
1570Sstevel@tonic-gate * the upper layer (poll(2), devpoll(7d) and now event ports) about
1580Sstevel@tonic-gate * the event triggered (see valid events in poll(2)).
1590Sstevel@tonic-gate * The pollwakeup() function forwards the event to the layer registered
1600Sstevel@tonic-gate * to receive the current event.
1610Sstevel@tonic-gate * The port_dissociate() function can be used to free the allocated
1620Sstevel@tonic-gate * event slot from the port. Anyway, file descriptors deliver events
1630Sstevel@tonic-gate * only one time and remain deactivated until the application
1640Sstevel@tonic-gate * reactivates the association of a file descriptor with port_associate().
1650Sstevel@tonic-gate * If an associated file descriptor is closed then the file descriptor
1660Sstevel@tonic-gate * will be dissociated automatically from the port.
1670Sstevel@tonic-gate *
1680Sstevel@tonic-gate * PORT_SOURCE_ALERT:
1690Sstevel@tonic-gate * This event type is generated when the port was previously set in
1700Sstevel@tonic-gate * alert mode using the port_alert() function.
1710Sstevel@tonic-gate * A single alert event is delivered to every thread which tries to
1720Sstevel@tonic-gate * retrieve events from a port.
1730Sstevel@tonic-gate * PORT_SOURCE_USER:
1740Sstevel@tonic-gate * This type of event is generated from user level using the port_send()
1750Sstevel@tonic-gate * function to send a user event to a port or the port_sendn() function
1760Sstevel@tonic-gate * to send an event to a list of ports.
177*4863Spraks * PORT_SOURCE_FILE:
178*4863Spraks * This event source uses the port_associate() interface to register
179*4863Spraks * a file to be monitored for changes. The file name that needs to be
180*4863Spraks * monitored is specified in the file_obj_t structure, a pointer to which
181*4863Spraks * is passed as an argument. The event types to be monitored are specified
182*4863Spraks * in the events argument.
183*4863Spraks * A file events monitor is represented internal per port per object
184*4863Spraks * address(the file_obj_t pointer). Which means there can be multiple
185*4863Spraks * watches registered on the same file using different file_obj_t
186*4863Spraks * structure pointer. With the help of the FEM(File Event Monitoring)
187*4863Spraks * hooks, the file's vnode ops are intercepted and relevant events
188*4863Spraks * delivered. The port_dissociate() function is used to de-register a
189*4863Spraks * file events monitor on a file. When the specified file is
190*4863Spraks * removed/renamed, the file events watch/monitor is automatically
191*4863Spraks * removed.
1920Sstevel@tonic-gate *
1930Sstevel@tonic-gate * EVENT DELIVERY / RETRIEVING EVENTS
1940Sstevel@tonic-gate * Events remain in the port queue until:
1950Sstevel@tonic-gate * - the application uses port_get() or port_getn() to retrieve events,
1960Sstevel@tonic-gate * - the event source cancel the event,
1970Sstevel@tonic-gate * - the event port is closed or
1980Sstevel@tonic-gate * - the process exits.
1990Sstevel@tonic-gate * The maximal number of events in a port queue is the maximal number
2000Sstevel@tonic-gate * of event slots/structures which can be allocated by event sources.
2010Sstevel@tonic-gate * The allocation of event slots/structures is controlled by the resource
2020Sstevel@tonic-gate * control: process.port-max-events.
2030Sstevel@tonic-gate * The port_get() function retrieves a single event and the port_getn()
2040Sstevel@tonic-gate * function retrieves a list of events.
2050Sstevel@tonic-gate * Events are classified as shareable and non-shareable events across processes.
2060Sstevel@tonic-gate * Non-shareable events are invisible for the port_get(n)() functions of
2070Sstevel@tonic-gate * processes other than the owner of the event.
2080Sstevel@tonic-gate * Shareable event types are:
2090Sstevel@tonic-gate * PORT_SOURCE_USER events
2100Sstevel@tonic-gate * This type of event is unconditionally shareable and without
2110Sstevel@tonic-gate * limitations. If the parent process sends a user event and closes
2120Sstevel@tonic-gate * the port afterwards, the event remains in the port and the child
2130Sstevel@tonic-gate * process will still be able to retrieve the user event.
2140Sstevel@tonic-gate * PORT_SOURCE_ALERT events
2150Sstevel@tonic-gate * This type of event is shareable between processes.
2160Sstevel@tonic-gate * Limitation: The alert mode of the port is removed if the owner
2170Sstevel@tonic-gate * (process which set the port in alert mode) of the
2180Sstevel@tonic-gate * alert event closes the port.
2190Sstevel@tonic-gate * PORT_SOURCE_FD events
2200Sstevel@tonic-gate * This type of event is conditional shareable between processes.
2210Sstevel@tonic-gate * After fork(2) all forked file descriptors are shareable between
2220Sstevel@tonic-gate * the processes. The child process is allowed to retrieve events
2230Sstevel@tonic-gate * from the associated file descriptors and it can also re-associate
2240Sstevel@tonic-gate * the fd with the port.
2250Sstevel@tonic-gate * Limitations: The child process is not allowed to dissociate
2260Sstevel@tonic-gate * the file descriptor from the port. Only the
2270Sstevel@tonic-gate * owner (process) of the association is allowed to
2280Sstevel@tonic-gate * dissociate the file descriptor from the port.
2290Sstevel@tonic-gate * If the owner of the association closes the port
2300Sstevel@tonic-gate * the association will be removed.
2310Sstevel@tonic-gate * PORT_SOURCE_AIO events
2320Sstevel@tonic-gate * This type of event is not shareable between processes.
2330Sstevel@tonic-gate * PORT_SOURCE_TIMER events
2340Sstevel@tonic-gate * This type of event is not shareable between processes.
235*4863Spraks * PORT_SOURCE_FILE events
236*4863Spraks * This type of event is not shareable between processes.
2370Sstevel@tonic-gate *
2380Sstevel@tonic-gate * FORK BEHAVIOUR
2390Sstevel@tonic-gate * On fork(2) the child process inherits all opened file descriptors from
2400Sstevel@tonic-gate * the parent process. This is also valid for port file descriptors.
2410Sstevel@tonic-gate * Associated file descriptors with a port maintain the association across the
2420Sstevel@tonic-gate * fork(2). It means, the child process gets full access to the port and
2430Sstevel@tonic-gate * it can retrieve events from all common associated file descriptors.
2440Sstevel@tonic-gate * Events of file descriptors created and associated with a port after the
2450Sstevel@tonic-gate * fork(2) are non-shareable and can only be retrieved by the same process.
2460Sstevel@tonic-gate *
2470Sstevel@tonic-gate * If the parent or the child process closes an exported port (using fork(2)
2480Sstevel@tonic-gate * or I_SENDFD) all the file descriptors associated with the port by the
2490Sstevel@tonic-gate * process will be dissociated from the port. Events of dissociated file
2500Sstevel@tonic-gate * descriptors as well as all non-shareable events will be discarded.
2510Sstevel@tonic-gate * The other process can continue working with the port as usual.
2520Sstevel@tonic-gate *
2530Sstevel@tonic-gate * CLOSING A PORT
2540Sstevel@tonic-gate * close(2) has to be used to close a port. See FORK BEHAVIOUR for details.
2550Sstevel@tonic-gate *
2560Sstevel@tonic-gate * PORT EVENT STRUCTURES
2570Sstevel@tonic-gate * The global control structure of the event ports framework is port_control_t.
2580Sstevel@tonic-gate * port_control_t keeps track of the number of created ports in the system.
2590Sstevel@tonic-gate * The cache of the port event structures is also located in port_control_t.
2600Sstevel@tonic-gate *
2610Sstevel@tonic-gate * On port_create() the vnode and the portfs node is also created.
2620Sstevel@tonic-gate * The portfs node is represented by the port_t structure.
2630Sstevel@tonic-gate * The port_t structure manages all port specific tasks:
2640Sstevel@tonic-gate * - management of resource control values
2650Sstevel@tonic-gate * - port VOP_POLL interface
2660Sstevel@tonic-gate * - creation time
2670Sstevel@tonic-gate * - uid and gid of the port
2680Sstevel@tonic-gate *
2690Sstevel@tonic-gate * The port_t structure contains the port_queue_t structure.
2700Sstevel@tonic-gate * The port_queue_t structure contains all the data necessary for the
2710Sstevel@tonic-gate * queue management:
2720Sstevel@tonic-gate * - locking
2730Sstevel@tonic-gate * - condition variables
2740Sstevel@tonic-gate * - event counters
2750Sstevel@tonic-gate * - submitted events (represented by port_kevent_t structures)
2760Sstevel@tonic-gate * - threads waiting for event delivery (check portget_t structure)
2770Sstevel@tonic-gate * - PORT_SOURCE_FD cache (managed by the port_fdcache_t structure)
2780Sstevel@tonic-gate * - event source management (managed by the port_source_t structure)
2790Sstevel@tonic-gate * - alert mode management (check port_alert_t structure)
2800Sstevel@tonic-gate *
2810Sstevel@tonic-gate * EVENT MANAGEMENT
2820Sstevel@tonic-gate * The event port file system creates a kmem_cache for internal allocation of
2830Sstevel@tonic-gate * event port structures.
2840Sstevel@tonic-gate *
2850Sstevel@tonic-gate * 1. Event source association with a port:
2860Sstevel@tonic-gate * The first step to do for event sources is to get associated with a port
2870Sstevel@tonic-gate * using the port_associate_ksource() function or adding an entry to the
2880Sstevel@tonic-gate * port_ksource_tab[]. An event source can get dissociated from a port
2890Sstevel@tonic-gate * using the port_dissociate_ksource() function. An entry in the
2900Sstevel@tonic-gate * port_ksource_tab[] implies that the source will be associated
2910Sstevel@tonic-gate * automatically with every new created port.
2920Sstevel@tonic-gate * The event source can deliver a callback function, which is used by the
2930Sstevel@tonic-gate * port to notify the event source about close(2). The idea is that
2940Sstevel@tonic-gate * in such a case the event source should free all allocated resources
2950Sstevel@tonic-gate * and it must return to the port all allocated slots/structures.
2960Sstevel@tonic-gate * The port_close() function will wait until all allocated event
2970Sstevel@tonic-gate * structures/slots are returned to the port.
2980Sstevel@tonic-gate * The callback function is not necessary when the event source does not
2990Sstevel@tonic-gate * maintain local resources, a second condition is that the event source
3000Sstevel@tonic-gate * can guarantee that allocated event slots will be returned without
3010Sstevel@tonic-gate * delay to the port (it will not block and sleep somewhere).
3020Sstevel@tonic-gate *
3030Sstevel@tonic-gate * 2. Reservation of an event slot / event structure
3040Sstevel@tonic-gate * The event port reliability is based on the reservation of an event "slot"
3050Sstevel@tonic-gate * (allocation of an event structure) by the event source as part of the
3060Sstevel@tonic-gate * application call. If the maximal number of event slots is exhausted then
3070Sstevel@tonic-gate * the event source can return a corresponding error code to the application.
3080Sstevel@tonic-gate *
3090Sstevel@tonic-gate * The port_alloc_event() function has to be used by event sources to
3100Sstevel@tonic-gate * allocate an event slot (reserve an event structure). The port_alloc_event()
3110Sstevel@tonic-gate * doesn not block and it will return a 0 value on success or an error code
3120Sstevel@tonic-gate * if it fails.
3130Sstevel@tonic-gate * An argument of port_alloc_event() is a flag which determines the behavior
3140Sstevel@tonic-gate * of the event after it was delivered to the application:
3150Sstevel@tonic-gate * PORT_ALLOC_DEFAULT : event slot becomes free after delivery to the
3160Sstevel@tonic-gate * application.
3170Sstevel@tonic-gate * PORT_ALLOC_PRIVATE : event slot remains under the control of the event
3180Sstevel@tonic-gate * source. This kind of slots can not be used for
3190Sstevel@tonic-gate * event delivery and should only be used internally
3200Sstevel@tonic-gate * by the event source.
3210Sstevel@tonic-gate * PORT_KEV_CACHED : event slot remains under the control of an event
3220Sstevel@tonic-gate * port cache. It does not become free after delivery
3230Sstevel@tonic-gate * to the application.
3240Sstevel@tonic-gate * PORT_ALLOC_SCACHED : event slot remains under the control of the event
3250Sstevel@tonic-gate * source. The event source takes the control over
3260Sstevel@tonic-gate * the slot after the event is delivered to the
3270Sstevel@tonic-gate * application.
3280Sstevel@tonic-gate *
3290Sstevel@tonic-gate * 3. Delivery of events to the event port
3300Sstevel@tonic-gate * Earlier allocated event structure/slot has to be used to deliver
3310Sstevel@tonic-gate * event data to the port. Event source has to use the function
3320Sstevel@tonic-gate * port_send_event(). The single argument is a pointer to the previously
3330Sstevel@tonic-gate * reserved event structure/slot.
3340Sstevel@tonic-gate * The portkev_events field of the port_kevent_t structure can be updated/set
3350Sstevel@tonic-gate * in two ways:
3360Sstevel@tonic-gate * 1. using the port_set_event() function, or
3370Sstevel@tonic-gate * 2. updating the portkev_events field out of the callback function:
3380Sstevel@tonic-gate * The event source can deliver a callback function to the port as an
3390Sstevel@tonic-gate * argument of port_init_event().
3400Sstevel@tonic-gate * One of the arguments of the callback function is a pointer to the
3410Sstevel@tonic-gate * events field, which will be delivered to the application.
3420Sstevel@tonic-gate * (see Delivery of events to the application).
3430Sstevel@tonic-gate * Event structures/slots can be delivered to the event port only one time,
3440Sstevel@tonic-gate * they remain blocked until the data is delivered to the application and the
3450Sstevel@tonic-gate * slot becomes free or it is delivered back to the event source
3460Sstevel@tonic-gate * (PORT_ALLOC_SCACHED). The activation of the callback function mentioned above
3470Sstevel@tonic-gate * is at the same time the indicator for the event source that the event
3480Sstevel@tonic-gate * structure/slot is free for reuse.
3490Sstevel@tonic-gate *
3500Sstevel@tonic-gate * 4. Delivery of events to the application
3510Sstevel@tonic-gate * The events structures/slots delivered by event sources remain in the
3520Sstevel@tonic-gate * port queue until they are retrieved by the application or the port
3530Sstevel@tonic-gate * is closed (exit(2) also closes all opened file descriptors)..
3540Sstevel@tonic-gate * The application uses port_get() or port_getn() to retrieve events from
3550Sstevel@tonic-gate * a port. port_get() retrieves a single event structure/slot and port_getn()
3560Sstevel@tonic-gate * retrieves a list of event structures/slots.
3570Sstevel@tonic-gate * Both functions are able to poll for events and return immediately or they
3580Sstevel@tonic-gate * can specify a timeout value.
3590Sstevel@tonic-gate * Before the events are delivered to the application they are moved to a
3600Sstevel@tonic-gate * second temporary internal queue. The idea is to avoid lock collisions or
3610Sstevel@tonic-gate * contentions of the global queue lock.
3620Sstevel@tonic-gate * The global queue lock is used every time when an event source delivers
3630Sstevel@tonic-gate * new events to the port.
3640Sstevel@tonic-gate * The port_get() and port_getn() functions
3650Sstevel@tonic-gate * a) retrieve single events from the temporary queue,
3660Sstevel@tonic-gate * b) prepare the data to be passed to the application memory,
3670Sstevel@tonic-gate * c) activate the callback function of the event sources:
3680Sstevel@tonic-gate * - to get the latest event data,
3690Sstevel@tonic-gate * - the event source can free all allocated resources associated with the
3700Sstevel@tonic-gate * current event,
3710Sstevel@tonic-gate * - the event source can re-use the current event slot/structure
3720Sstevel@tonic-gate * - the event source can deny the delivery of the event to the application
3730Sstevel@tonic-gate * (e.g. because of the wrong process).
3740Sstevel@tonic-gate * d) put the event back to the temporary queue if the event delivery was denied
3750Sstevel@tonic-gate * e) repeat a) until d) as long as there are events in the queue and
3760Sstevel@tonic-gate * there is enough user space available.
3770Sstevel@tonic-gate *
3780Sstevel@tonic-gate * The loop described above could block for a very long time the global mutex,
3790Sstevel@tonic-gate * to avoid that a second mutex was introduced to synchronized concurrent
3800Sstevel@tonic-gate * threads accessing the temporary queue.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate static int64_t portfs(int, uintptr_t, uintptr_t, uintptr_t, uintptr_t,
3840Sstevel@tonic-gate uintptr_t);
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate static struct sysent port_sysent = {
3870Sstevel@tonic-gate 6,
3880Sstevel@tonic-gate SE_ARGC | SE_64RVAL | SE_NOUNLOAD,
3890Sstevel@tonic-gate (int (*)())portfs,
3900Sstevel@tonic-gate };
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate static struct modlsys modlsys = {
3930Sstevel@tonic-gate &mod_syscallops, "event ports", &port_sysent
3940Sstevel@tonic-gate };
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate static int64_t
3990Sstevel@tonic-gate portfs32(uint32_t arg1, int32_t arg2, uint32_t arg3, uint32_t arg4,
4000Sstevel@tonic-gate uint32_t arg5, uint32_t arg6);
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate static struct sysent port_sysent32 = {
4030Sstevel@tonic-gate 6,
4040Sstevel@tonic-gate SE_ARGC | SE_64RVAL | SE_NOUNLOAD,
4050Sstevel@tonic-gate (int (*)())portfs32,
4060Sstevel@tonic-gate };
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate static struct modlsys modlsys32 = {
4090Sstevel@tonic-gate &mod_syscallops32,
4100Sstevel@tonic-gate "32-bit event ports syscalls",
4110Sstevel@tonic-gate &port_sysent32
4120Sstevel@tonic-gate };
4130Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate static struct modlinkage modlinkage = {
4160Sstevel@tonic-gate MODREV_1,
4170Sstevel@tonic-gate &modlsys,
4180Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4190Sstevel@tonic-gate &modlsys32,
4200Sstevel@tonic-gate #endif
4210Sstevel@tonic-gate NULL
4220Sstevel@tonic-gate };
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate port_kstat_t port_kstat = {
4250Sstevel@tonic-gate { "ports", KSTAT_DATA_UINT32 }
4260Sstevel@tonic-gate };
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate dev_t portdev;
4290Sstevel@tonic-gate struct vnodeops *port_vnodeops;
4300Sstevel@tonic-gate struct vfs port_vfs;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate extern rctl_hndl_t rc_process_portev;
4330Sstevel@tonic-gate extern rctl_hndl_t rc_project_portids;
4340Sstevel@tonic-gate extern void aio_close_port(void *, int, pid_t, int);
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate * This table contains a list of event sources which need a static
4380Sstevel@tonic-gate * association with a port (every port).
4390Sstevel@tonic-gate * The last NULL entry in the table is required to detect "end of table".
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate struct port_ksource port_ksource_tab[] = {
4420Sstevel@tonic-gate {PORT_SOURCE_AIO, aio_close_port, NULL, NULL},
4430Sstevel@tonic-gate {0, NULL, NULL, NULL}
4440Sstevel@tonic-gate };
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /* local functions */
4470Sstevel@tonic-gate static int port_getn(port_t *, port_event_t *, uint_t, uint_t *,
4480Sstevel@tonic-gate port_gettimer_t *);
4490Sstevel@tonic-gate static int port_sendn(int [], int [], uint_t, int, void *, uint_t *);
4500Sstevel@tonic-gate static int port_alert(port_t *, int, int, void *);
4510Sstevel@tonic-gate static int port_dispatch_event(port_t *, int, int, int, uintptr_t, void *);
4520Sstevel@tonic-gate static int port_send(port_t *, int, int, void *);
4530Sstevel@tonic-gate static int port_create(int *);
4540Sstevel@tonic-gate static int port_get_alert(port_alert_t *, port_event_t *);
4550Sstevel@tonic-gate static int port_copy_event(port_event_t *, port_kevent_t *, list_t *);
4560Sstevel@tonic-gate static int *port_errorn(int *, int, int, int);
4570Sstevel@tonic-gate static int port_noshare(void *, int *, pid_t, int, void *);
4580Sstevel@tonic-gate static int port_get_timeout(timespec_t *, timespec_t *, timespec_t **, int *,
4590Sstevel@tonic-gate int);
4600Sstevel@tonic-gate static void port_init(port_t *);
4610Sstevel@tonic-gate static void port_remove_alert(port_queue_t *);
4620Sstevel@tonic-gate static void port_add_ksource_local(port_t *, port_ksource_t *);
4630Sstevel@tonic-gate static void port_check_return_cond(port_queue_t *);
4640Sstevel@tonic-gate static void port_dequeue_thread(port_queue_t *, portget_t *);
4650Sstevel@tonic-gate static portget_t *port_queue_thread(port_queue_t *, uint_t);
4660Sstevel@tonic-gate static void port_kstat_init(void);
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4690Sstevel@tonic-gate static int port_copy_event32(port_event32_t *, port_kevent_t *, list_t *);
4700Sstevel@tonic-gate #endif
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate int
_init(void)4730Sstevel@tonic-gate _init(void)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate static const fs_operation_def_t port_vfsops_template[] = {
4760Sstevel@tonic-gate NULL, NULL
4770Sstevel@tonic-gate };
4780Sstevel@tonic-gate extern const fs_operation_def_t port_vnodeops_template[];
4790Sstevel@tonic-gate vfsops_t *port_vfsops;
4800Sstevel@tonic-gate int error;
4810Sstevel@tonic-gate major_t major;
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate if ((major = getudev()) == (major_t)-1)
4840Sstevel@tonic-gate return (ENXIO);
4850Sstevel@tonic-gate portdev = makedevice(major, 0);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate /* Create a dummy vfs */
4880Sstevel@tonic-gate error = vfs_makefsops(port_vfsops_template, &port_vfsops);
4890Sstevel@tonic-gate if (error) {
4900Sstevel@tonic-gate cmn_err(CE_WARN, "port init: bad vfs ops");
4910Sstevel@tonic-gate return (error);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate vfs_setops(&port_vfs, port_vfsops);
4940Sstevel@tonic-gate port_vfs.vfs_flag = VFS_RDONLY;
4950Sstevel@tonic-gate port_vfs.vfs_dev = portdev;
4960Sstevel@tonic-gate vfs_make_fsid(&(port_vfs.vfs_fsid), portdev, 0);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate error = vn_make_ops("portfs", port_vnodeops_template, &port_vnodeops);
4990Sstevel@tonic-gate if (error) {
5000Sstevel@tonic-gate vfs_freevfsops(port_vfsops);
5010Sstevel@tonic-gate cmn_err(CE_WARN, "port init: bad vnode ops");
5020Sstevel@tonic-gate return (error);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate mutex_init(&port_control.pc_mutex, NULL, MUTEX_DEFAULT, NULL);
5060Sstevel@tonic-gate port_control.pc_nents = 0; /* number of active ports */
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate /* create kmem_cache for port event structures */
5090Sstevel@tonic-gate port_control.pc_cache = kmem_cache_create("port_cache",
5100Sstevel@tonic-gate sizeof (port_kevent_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate port_kstat_init(); /* init port kstats */
5130Sstevel@tonic-gate return (mod_install(&modlinkage));
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate int
_info(struct modinfo * modinfop)5170Sstevel@tonic-gate _info(struct modinfo *modinfop)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate * System call wrapper for all port related system calls from 32-bit programs.
5240Sstevel@tonic-gate */
5250Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5260Sstevel@tonic-gate static int64_t
portfs32(uint32_t opcode,int32_t a0,uint32_t a1,uint32_t a2,uint32_t a3,uint32_t a4)5270Sstevel@tonic-gate portfs32(uint32_t opcode, int32_t a0, uint32_t a1, uint32_t a2, uint32_t a3,
5280Sstevel@tonic-gate uint32_t a4)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate int64_t error;
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate switch (opcode & PORT_CODE_MASK) {
5330Sstevel@tonic-gate case PORT_GET:
5340Sstevel@tonic-gate error = portfs(PORT_GET, a0, a1, (int)a2, (int)a3, a4);
5350Sstevel@tonic-gate break;
5360Sstevel@tonic-gate case PORT_SENDN:
5370Sstevel@tonic-gate error = portfs(opcode, (uint32_t)a0, a1, a2, a3, a4);
5380Sstevel@tonic-gate break;
5390Sstevel@tonic-gate default:
5400Sstevel@tonic-gate error = portfs(opcode, a0, a1, a2, a3, a4);
5410Sstevel@tonic-gate break;
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate return (error);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * System entry point for port functions.
5490Sstevel@tonic-gate * a0 is a port file descriptor (except for PORT_SENDN and PORT_CREATE).
5500Sstevel@tonic-gate * The libc uses PORT_SYS_NOPORT in functions which do not deliver a
5510Sstevel@tonic-gate * port file descriptor as first argument.
5520Sstevel@tonic-gate */
5530Sstevel@tonic-gate static int64_t
portfs(int opcode,uintptr_t a0,uintptr_t a1,uintptr_t a2,uintptr_t a3,uintptr_t a4)5540Sstevel@tonic-gate portfs(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3,
5550Sstevel@tonic-gate uintptr_t a4)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate rval_t r;
5580Sstevel@tonic-gate port_t *pp;
5590Sstevel@tonic-gate int error = 0;
5600Sstevel@tonic-gate uint_t nget;
5610Sstevel@tonic-gate file_t *fp;
5620Sstevel@tonic-gate port_gettimer_t port_timer;
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate r.r_vals = 0;
5650Sstevel@tonic-gate if (opcode & PORT_SYS_NOPORT) {
5660Sstevel@tonic-gate opcode &= PORT_CODE_MASK;
5670Sstevel@tonic-gate if (opcode == PORT_SENDN) {
5680Sstevel@tonic-gate error = port_sendn((int *)a0, (int *)a1, (uint_t)a2,
5690Sstevel@tonic-gate (int)a3, (void *)a4, (uint_t *)&r.r_val1);
5700Sstevel@tonic-gate if (error && (error != EIO))
5710Sstevel@tonic-gate return ((int64_t)set_errno(error));
5720Sstevel@tonic-gate return (r.r_vals);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate if (opcode == PORT_CREATE) {
5760Sstevel@tonic-gate error = port_create(&r.r_val1);
5770Sstevel@tonic-gate if (error)
5780Sstevel@tonic-gate return ((int64_t)set_errno(error));
5790Sstevel@tonic-gate return (r.r_vals);
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate /* opcodes using port as first argument (a0) */
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate if ((fp = getf((int)a0)) == NULL)
5860Sstevel@tonic-gate return ((uintptr_t)set_errno(EBADF));
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
5890Sstevel@tonic-gate releasef((int)a0);
5900Sstevel@tonic-gate return ((uintptr_t)set_errno(EBADFD));
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate pp = VTOEP(fp->f_vnode);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate switch (opcode & PORT_CODE_MASK) {
5960Sstevel@tonic-gate case PORT_GET:
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate /* see PORT_GETN description */
5990Sstevel@tonic-gate struct timespec timeout;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate port_timer.pgt_flags = PORTGET_ONE;
6020Sstevel@tonic-gate port_timer.pgt_loop = 0;
6030Sstevel@tonic-gate port_timer.pgt_rqtp = NULL;
6040Sstevel@tonic-gate if (a4 != NULL) {
6050Sstevel@tonic-gate port_timer.pgt_timeout = &timeout;
6060Sstevel@tonic-gate timeout.tv_sec = (time_t)a2;
6070Sstevel@tonic-gate timeout.tv_nsec = (long)a3;
6080Sstevel@tonic-gate } else {
6090Sstevel@tonic-gate port_timer.pgt_timeout = NULL;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate do {
6120Sstevel@tonic-gate nget = 1;
6130Sstevel@tonic-gate error = port_getn(pp, (port_event_t *)a1, 1,
6140Sstevel@tonic-gate (uint_t *)&nget, &port_timer);
6150Sstevel@tonic-gate } while (nget == 0 && error == 0 && port_timer.pgt_loop);
6160Sstevel@tonic-gate break;
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate case PORT_GETN:
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate * port_getn() can only retrieve own or shareable events from
6220Sstevel@tonic-gate * other processes. The port_getn() function remains in the
6230Sstevel@tonic-gate * kernel until own or shareable events are available or the
6240Sstevel@tonic-gate * timeout elapses.
6250Sstevel@tonic-gate */
6260Sstevel@tonic-gate port_timer.pgt_flags = 0;
6270Sstevel@tonic-gate port_timer.pgt_loop = 0;
6280Sstevel@tonic-gate port_timer.pgt_rqtp = NULL;
6290Sstevel@tonic-gate port_timer.pgt_timeout = (struct timespec *)a4;
6300Sstevel@tonic-gate do {
6310Sstevel@tonic-gate nget = a3;
6320Sstevel@tonic-gate error = port_getn(pp, (port_event_t *)a1, (uint_t)a2,
6330Sstevel@tonic-gate (uint_t *)&nget, &port_timer);
6340Sstevel@tonic-gate } while (nget == 0 && error == 0 && port_timer.pgt_loop);
6350Sstevel@tonic-gate r.r_val1 = nget;
6360Sstevel@tonic-gate r.r_val2 = error;
6370Sstevel@tonic-gate releasef((int)a0);
6380Sstevel@tonic-gate if (error && error != ETIME)
6390Sstevel@tonic-gate return ((int64_t)set_errno(error));
6400Sstevel@tonic-gate return (r.r_vals);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate case PORT_ASSOCIATE:
6430Sstevel@tonic-gate {
644*4863Spraks switch ((int)a1) {
645*4863Spraks case PORT_SOURCE_FD:
646*4863Spraks error = port_associate_fd(pp, (int)a1, (uintptr_t)a2,
647*4863Spraks (int)a3, (void *)a4);
648*4863Spraks break;
649*4863Spraks case PORT_SOURCE_FILE:
650*4863Spraks error = port_associate_fop(pp, (int)a1, (uintptr_t)a2,
651*4863Spraks (int)a3, (void *)a4);
652*4863Spraks break;
653*4863Spraks default:
6540Sstevel@tonic-gate error = EINVAL;
6550Sstevel@tonic-gate break;
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate break;
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate case PORT_SEND:
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate /* user-defined events */
6620Sstevel@tonic-gate error = port_send(pp, PORT_SOURCE_USER, (int)a1, (void *)a2);
6630Sstevel@tonic-gate break;
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate case PORT_DISPATCH:
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate * library events, blocking
6691885Sraf * Only events of type PORT_SOURCE_AIO or PORT_SOURCE_MQ
6701885Sraf * are currently allowed.
6710Sstevel@tonic-gate */
6721885Sraf if ((int)a1 != PORT_SOURCE_AIO && (int)a1 != PORT_SOURCE_MQ) {
6730Sstevel@tonic-gate error = EINVAL;
6740Sstevel@tonic-gate break;
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate error = port_dispatch_event(pp, (int)opcode, (int)a1, (int)a2,
6770Sstevel@tonic-gate (uintptr_t)a3, (void *)a4);
6780Sstevel@tonic-gate break;
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate case PORT_DISSOCIATE:
6810Sstevel@tonic-gate {
682*4863Spraks switch ((int)a1) {
683*4863Spraks case PORT_SOURCE_FD:
684*4863Spraks error = port_dissociate_fd(pp, (uintptr_t)a2);
685*4863Spraks break;
686*4863Spraks case PORT_SOURCE_FILE:
687*4863Spraks error = port_dissociate_fop(pp, (uintptr_t)a2);
688*4863Spraks break;
689*4863Spraks default:
6900Sstevel@tonic-gate error = EINVAL;
6910Sstevel@tonic-gate break;
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate break;
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate case PORT_ALERT:
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate if ((int)a2) /* a2 = events */
6980Sstevel@tonic-gate error = port_alert(pp, (int)a1, (int)a2, (void *)a3);
6990Sstevel@tonic-gate else
7000Sstevel@tonic-gate port_remove_alert(&pp->port_queue);
7010Sstevel@tonic-gate break;
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate default:
7040Sstevel@tonic-gate error = EINVAL;
7050Sstevel@tonic-gate break;
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate releasef((int)a0);
7090Sstevel@tonic-gate if (error)
7100Sstevel@tonic-gate return ((int64_t)set_errno(error));
7110Sstevel@tonic-gate return (r.r_vals);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate * System call to create a port.
7160Sstevel@tonic-gate *
7170Sstevel@tonic-gate * The port_create() function creates a vnode of type VPORT per port.
7180Sstevel@tonic-gate * The port control data is associated with the vnode as vnode private data.
7190Sstevel@tonic-gate * The port_create() function returns an event port file descriptor.
7200Sstevel@tonic-gate */
7210Sstevel@tonic-gate static int
port_create(int * fdp)7220Sstevel@tonic-gate port_create(int *fdp)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate port_t *pp;
7250Sstevel@tonic-gate vnode_t *vp;
7260Sstevel@tonic-gate struct file *fp;
7270Sstevel@tonic-gate proc_t *p = curproc;
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate /* initialize vnode and port private data */
7300Sstevel@tonic-gate pp = kmem_zalloc(sizeof (port_t), KM_SLEEP);
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate pp->port_vnode = vn_alloc(KM_SLEEP);
7330Sstevel@tonic-gate vp = EPTOV(pp);
7340Sstevel@tonic-gate vn_setops(vp, port_vnodeops);
7350Sstevel@tonic-gate vp->v_type = VPORT;
7360Sstevel@tonic-gate vp->v_vfsp = &port_vfs;
7370Sstevel@tonic-gate vp->v_data = (caddr_t)pp;
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate mutex_enter(&port_control.pc_mutex);
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate * Retrieve the maximal number of event ports allowed per system from
7420Sstevel@tonic-gate * the resource control: project.port-max-ids.
7430Sstevel@tonic-gate */
7440Sstevel@tonic-gate mutex_enter(&p->p_lock);
7450Sstevel@tonic-gate if (rctl_test(rc_project_portids, p->p_task->tk_proj->kpj_rctls, p,
7460Sstevel@tonic-gate port_control.pc_nents + 1, RCA_SAFE) & RCT_DENY) {
7470Sstevel@tonic-gate mutex_exit(&p->p_lock);
7480Sstevel@tonic-gate vn_free(vp);
7490Sstevel@tonic-gate kmem_free(pp, sizeof (port_t));
7500Sstevel@tonic-gate mutex_exit(&port_control.pc_mutex);
7510Sstevel@tonic-gate return (EAGAIN);
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate * Retrieve the maximal number of events allowed per port from
7560Sstevel@tonic-gate * the resource control: process.port-max-events.
7570Sstevel@tonic-gate */
7580Sstevel@tonic-gate pp->port_max_events = rctl_enforced_value(rc_process_portev,
7590Sstevel@tonic-gate p->p_rctls, p);
7600Sstevel@tonic-gate mutex_exit(&p->p_lock);
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate /* allocate a new user file descriptor and a file structure */
7630Sstevel@tonic-gate if (falloc(vp, 0, &fp, fdp)) {
7640Sstevel@tonic-gate /*
7650Sstevel@tonic-gate * If the file table is full, free allocated resources.
7660Sstevel@tonic-gate */
7670Sstevel@tonic-gate vn_free(vp);
7680Sstevel@tonic-gate kmem_free(pp, sizeof (port_t));
7690Sstevel@tonic-gate mutex_exit(&port_control.pc_mutex);
7700Sstevel@tonic-gate return (EMFILE);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate pp->port_fd = *fdp;
7760Sstevel@tonic-gate port_control.pc_nents++;
7770Sstevel@tonic-gate p->p_portcnt++;
7780Sstevel@tonic-gate port_kstat.pks_ports.value.ui32++;
7790Sstevel@tonic-gate mutex_exit(&port_control.pc_mutex);
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate /* initializes port private data */
7820Sstevel@tonic-gate port_init(pp);
7831425Spraks /* set user file pointer */
7841425Spraks setf(*fdp, fp);
7850Sstevel@tonic-gate return (0);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate /*
7890Sstevel@tonic-gate * port_init() initializes event port specific data
7900Sstevel@tonic-gate */
7910Sstevel@tonic-gate static void
port_init(port_t * pp)7920Sstevel@tonic-gate port_init(port_t *pp)
7930Sstevel@tonic-gate {
7940Sstevel@tonic-gate port_queue_t *portq;
7950Sstevel@tonic-gate port_ksource_t *pks;
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate mutex_init(&pp->port_mutex, NULL, MUTEX_DEFAULT, NULL);
7980Sstevel@tonic-gate portq = &pp->port_queue;
7990Sstevel@tonic-gate mutex_init(&portq->portq_mutex, NULL, MUTEX_DEFAULT, NULL);
8000Sstevel@tonic-gate pp->port_flags |= PORT_INIT;
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate * If it is not enough memory available to satisfy a user
8040Sstevel@tonic-gate * request using a single port_getn() call then port_getn()
8050Sstevel@tonic-gate * will reduce the size of the list to PORT_MAX_LIST.
8060Sstevel@tonic-gate */
8070Sstevel@tonic-gate pp->port_max_list = port_max_list;
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate /* Set timestamp entries required for fstat(2) requests */
8100Sstevel@tonic-gate gethrestime(&pp->port_ctime);
8110Sstevel@tonic-gate pp->port_uid = crgetuid(curproc->p_cred);
8120Sstevel@tonic-gate pp->port_gid = crgetgid(curproc->p_cred);
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate /* initialize port queue structs */
8150Sstevel@tonic-gate list_create(&portq->portq_list, sizeof (port_kevent_t),
8160Sstevel@tonic-gate offsetof(port_kevent_t, portkev_node));
8170Sstevel@tonic-gate list_create(&portq->portq_get_list, sizeof (port_kevent_t),
8180Sstevel@tonic-gate offsetof(port_kevent_t, portkev_node));
8190Sstevel@tonic-gate portq->portq_flags = 0;
8200Sstevel@tonic-gate pp->port_pid = curproc->p_pid;
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate /* Allocate cache skeleton for PORT_SOURCE_FD events */
8230Sstevel@tonic-gate portq->portq_pcp = kmem_zalloc(sizeof (port_fdcache_t), KM_SLEEP);
8240Sstevel@tonic-gate mutex_init(&portq->portq_pcp->pc_lock, NULL, MUTEX_DEFAULT, NULL);
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate * Allocate cache skeleton for association of event sources.
8280Sstevel@tonic-gate */
8290Sstevel@tonic-gate mutex_init(&portq->portq_source_mutex, NULL, MUTEX_DEFAULT, NULL);
8300Sstevel@tonic-gate portq->portq_scache = kmem_zalloc(
8310Sstevel@tonic-gate PORT_SCACHE_SIZE * sizeof (port_source_t *), KM_SLEEP);
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate * pre-associate some kernel sources with this port.
8350Sstevel@tonic-gate * The pre-association is required to create port_source_t
8360Sstevel@tonic-gate * structures for object association.
8370Sstevel@tonic-gate * Some sources can not get associated with a port before the first
8380Sstevel@tonic-gate * object association is requested. Another reason to pre_associate
8390Sstevel@tonic-gate * a particular source with a port is because of performance.
8400Sstevel@tonic-gate */
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate for (pks = port_ksource_tab; pks->pks_source != 0; pks++)
8430Sstevel@tonic-gate port_add_ksource_local(pp, pks);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate * The port_add_ksource_local() function is being used to associate
8480Sstevel@tonic-gate * event sources with every new port.
8490Sstevel@tonic-gate * The event sources need to be added to port_ksource_tab[].
8500Sstevel@tonic-gate */
8510Sstevel@tonic-gate static void
port_add_ksource_local(port_t * pp,port_ksource_t * pks)8520Sstevel@tonic-gate port_add_ksource_local(port_t *pp, port_ksource_t *pks)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate port_source_t *pse;
8550Sstevel@tonic-gate port_source_t **ps;
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex);
8580Sstevel@tonic-gate ps = &pp->port_queue.portq_scache[PORT_SHASH(pks->pks_source)];
8590Sstevel@tonic-gate for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
8600Sstevel@tonic-gate if (pse->portsrc_source == pks->pks_source)
8610Sstevel@tonic-gate break;
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate if (pse == NULL) {
8650Sstevel@tonic-gate /* associate new source with the port */
8660Sstevel@tonic-gate pse = kmem_zalloc(sizeof (port_source_t), KM_SLEEP);
8670Sstevel@tonic-gate pse->portsrc_source = pks->pks_source;
8680Sstevel@tonic-gate pse->portsrc_close = pks->pks_close;
8690Sstevel@tonic-gate pse->portsrc_closearg = pks->pks_closearg;
8700Sstevel@tonic-gate pse->portsrc_cnt = 1;
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate pks->pks_portsrc = pse;
8730Sstevel@tonic-gate if (*ps != NULL)
8740Sstevel@tonic-gate pse->portsrc_next = (*ps)->portsrc_next;
8750Sstevel@tonic-gate *ps = pse;
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate * The port_send() function sends an event of type "source" to a
8820Sstevel@tonic-gate * port. This function is non-blocking. An event can be sent to
8830Sstevel@tonic-gate * a port as long as the number of events per port does not achieve the
8840Sstevel@tonic-gate * maximal allowed number of events. The max. number of events per port is
8850Sstevel@tonic-gate * defined by the resource control process.max-port-events.
8860Sstevel@tonic-gate * This function is used by the port library function port_send()
8870Sstevel@tonic-gate * and port_dispatch(). The port_send(3c) function is part of the
8880Sstevel@tonic-gate * event ports API and submits events of type PORT_SOURCE_USER. The
8890Sstevel@tonic-gate * port_dispatch() function is project private and it is used by library
8900Sstevel@tonic-gate * functions to submit events of other types than PORT_SOURCE_USER
8910Sstevel@tonic-gate * (e.g. PORT_SOURCE_AIO).
8920Sstevel@tonic-gate */
8930Sstevel@tonic-gate static int
port_send(port_t * pp,int source,int events,void * user)8940Sstevel@tonic-gate port_send(port_t *pp, int source, int events, void *user)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate port_kevent_t *pev;
8970Sstevel@tonic-gate int error;
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate error = port_alloc_event_local(pp, source, PORT_ALLOC_DEFAULT, &pev);
9000Sstevel@tonic-gate if (error)
9010Sstevel@tonic-gate return (error);
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate pev->portkev_object = 0;
9040Sstevel@tonic-gate pev->portkev_events = events;
9050Sstevel@tonic-gate pev->portkev_user = user;
9060Sstevel@tonic-gate pev->portkev_callback = NULL;
9070Sstevel@tonic-gate pev->portkev_arg = NULL;
9080Sstevel@tonic-gate pev->portkev_flags = 0;
9090Sstevel@tonic-gate
9101885Sraf port_send_event(pev);
9110Sstevel@tonic-gate return (0);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate * The port_noshare() function returns 0 if the current event was generated
9160Sstevel@tonic-gate * by the same process. Otherwise is returns a value other than 0 and the
9170Sstevel@tonic-gate * event should not be delivered to the current processe.
9180Sstevel@tonic-gate * The port_noshare() function is normally used by the port_dispatch()
9190Sstevel@tonic-gate * function. The port_dispatch() function is project private and can only be
9200Sstevel@tonic-gate * used within the event port project.
9210Sstevel@tonic-gate * Currently the libaio uses the port_dispatch() function to deliver events
9220Sstevel@tonic-gate * of types PORT_SOURCE_AIO.
9230Sstevel@tonic-gate */
9240Sstevel@tonic-gate /* ARGSUSED */
9250Sstevel@tonic-gate static int
port_noshare(void * arg,int * events,pid_t pid,int flag,void * evp)9260Sstevel@tonic-gate port_noshare(void *arg, int *events, pid_t pid, int flag, void *evp)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate if (flag == PORT_CALLBACK_DEFAULT && curproc->p_pid != pid)
9290Sstevel@tonic-gate return (1);
9300Sstevel@tonic-gate return (0);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate /*
9340Sstevel@tonic-gate * The port_dispatch_event() function is project private and it is used by
9350Sstevel@tonic-gate * libraries involved in the project to deliver events to the port.
9360Sstevel@tonic-gate * port_dispatch will sleep and wait for enough resources to satisfy the
9370Sstevel@tonic-gate * request, if necessary.
9380Sstevel@tonic-gate * The library can specify if the delivered event is shareable with other
9390Sstevel@tonic-gate * processes (see PORT_SYS_NOSHARE flag).
9400Sstevel@tonic-gate */
9410Sstevel@tonic-gate static int
port_dispatch_event(port_t * pp,int opcode,int source,int events,uintptr_t object,void * user)9420Sstevel@tonic-gate port_dispatch_event(port_t *pp, int opcode, int source, int events,
9430Sstevel@tonic-gate uintptr_t object, void *user)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate port_kevent_t *pev;
9460Sstevel@tonic-gate int error;
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate error = port_alloc_event_block(pp, source, PORT_ALLOC_DEFAULT, &pev);
9490Sstevel@tonic-gate if (error)
9500Sstevel@tonic-gate return (error);
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate pev->portkev_object = object;
9530Sstevel@tonic-gate pev->portkev_events = events;
9540Sstevel@tonic-gate pev->portkev_user = user;
9550Sstevel@tonic-gate pev->portkev_arg = NULL;
9560Sstevel@tonic-gate if (opcode & PORT_SYS_NOSHARE) {
9570Sstevel@tonic-gate pev->portkev_flags = PORT_KEV_NOSHARE;
9580Sstevel@tonic-gate pev->portkev_callback = port_noshare;
9590Sstevel@tonic-gate } else {
9600Sstevel@tonic-gate pev->portkev_flags = 0;
9610Sstevel@tonic-gate pev->portkev_callback = NULL;
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9641885Sraf port_send_event(pev);
9650Sstevel@tonic-gate return (0);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate /*
9700Sstevel@tonic-gate * The port_sendn() function is the kernel implementation of the event
9710Sstevel@tonic-gate * port API function port_sendn(3c).
9720Sstevel@tonic-gate * This function is able to send an event to a list of event ports.
9730Sstevel@tonic-gate */
9740Sstevel@tonic-gate static int
port_sendn(int ports[],int errors[],uint_t nent,int events,void * user,uint_t * nget)9750Sstevel@tonic-gate port_sendn(int ports[], int errors[], uint_t nent, int events, void *user,
9760Sstevel@tonic-gate uint_t *nget)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate port_kevent_t *pev;
9790Sstevel@tonic-gate int errorcnt = 0;
9800Sstevel@tonic-gate int error = 0;
9810Sstevel@tonic-gate int count;
9820Sstevel@tonic-gate int port;
9830Sstevel@tonic-gate int *plist;
9840Sstevel@tonic-gate int *elist = NULL;
9850Sstevel@tonic-gate file_t *fp;
9860Sstevel@tonic-gate port_t *pp;
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate if (nent == 0 || nent > port_max_list)
9890Sstevel@tonic-gate return (EINVAL);
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate plist = kmem_alloc(nent * sizeof (int), KM_SLEEP);
9920Sstevel@tonic-gate if (copyin((void *)ports, plist, nent * sizeof (int))) {
9930Sstevel@tonic-gate kmem_free(plist, nent * sizeof (int));
9940Sstevel@tonic-gate return (EFAULT);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate * Scan the list for event port file descriptors and send the
9990Sstevel@tonic-gate * attached user event data embedded in a event of type
10000Sstevel@tonic-gate * PORT_SOURCE_USER to every event port in the list.
10010Sstevel@tonic-gate * If a list entry is not a valid event port then the corresponding
10020Sstevel@tonic-gate * error code will be stored in the errors[] list with the same
10030Sstevel@tonic-gate * list offset as in the ports[] list.
10040Sstevel@tonic-gate */
10050Sstevel@tonic-gate
10060Sstevel@tonic-gate for (count = 0; count < nent; count++) {
10070Sstevel@tonic-gate port = plist[count];
10080Sstevel@tonic-gate if ((fp = getf(port)) == NULL) {
10090Sstevel@tonic-gate elist = port_errorn(elist, nent, EBADF, count);
10100Sstevel@tonic-gate errorcnt++;
10110Sstevel@tonic-gate continue;
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate pp = VTOEP(fp->f_vnode);
10150Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
10160Sstevel@tonic-gate releasef(port);
10170Sstevel@tonic-gate elist = port_errorn(elist, nent, EBADFD, count);
10180Sstevel@tonic-gate errorcnt++;
10190Sstevel@tonic-gate continue;
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate
10220Sstevel@tonic-gate error = port_alloc_event_local(pp, PORT_SOURCE_USER,
10230Sstevel@tonic-gate PORT_ALLOC_DEFAULT, &pev);
10240Sstevel@tonic-gate if (error) {
10250Sstevel@tonic-gate releasef(port);
10260Sstevel@tonic-gate elist = port_errorn(elist, nent, error, count);
10270Sstevel@tonic-gate errorcnt++;
10280Sstevel@tonic-gate continue;
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate
10310Sstevel@tonic-gate pev->portkev_object = 0;
10320Sstevel@tonic-gate pev->portkev_events = events;
10330Sstevel@tonic-gate pev->portkev_user = user;
10340Sstevel@tonic-gate pev->portkev_callback = NULL;
10350Sstevel@tonic-gate pev->portkev_arg = NULL;
10360Sstevel@tonic-gate pev->portkev_flags = 0;
10370Sstevel@tonic-gate
10381885Sraf port_send_event(pev);
10390Sstevel@tonic-gate releasef(port);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate if (errorcnt) {
10420Sstevel@tonic-gate error = EIO;
10430Sstevel@tonic-gate if (copyout(elist, (void *)errors, nent * sizeof (int)))
10440Sstevel@tonic-gate error = EFAULT;
10450Sstevel@tonic-gate kmem_free(elist, nent * sizeof (int));
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate *nget = nent - errorcnt;
10480Sstevel@tonic-gate kmem_free(plist, nent * sizeof (int));
10490Sstevel@tonic-gate return (error);
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate static int *
port_errorn(int * elist,int nent,int error,int index)10530Sstevel@tonic-gate port_errorn(int *elist, int nent, int error, int index)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate if (elist == NULL)
10560Sstevel@tonic-gate elist = kmem_zalloc(nent * sizeof (int), KM_SLEEP);
10570Sstevel@tonic-gate elist[index] = error;
10580Sstevel@tonic-gate return (elist);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate * port_alert()
10630Sstevel@tonic-gate * The port_alert() funcion is a high priority event and it is always set
10640Sstevel@tonic-gate * on top of the queue. It is also delivered as single event.
10650Sstevel@tonic-gate * flags:
10660Sstevel@tonic-gate * - SET :overwrite current alert data
10670Sstevel@tonic-gate * - UPDATE:set alert data or return EBUSY if alert mode is already set
10680Sstevel@tonic-gate *
10690Sstevel@tonic-gate * - set the ALERT flag
10700Sstevel@tonic-gate * - wakeup all sleeping threads
10710Sstevel@tonic-gate */
10720Sstevel@tonic-gate static int
port_alert(port_t * pp,int flags,int events,void * user)10730Sstevel@tonic-gate port_alert(port_t *pp, int flags, int events, void *user)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate port_queue_t *portq;
10760Sstevel@tonic-gate portget_t *pgetp;
10770Sstevel@tonic-gate port_alert_t *pa;
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate if ((flags & PORT_ALERT_INVALID) == PORT_ALERT_INVALID)
10800Sstevel@tonic-gate return (EINVAL);
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate portq = &pp->port_queue;
10830Sstevel@tonic-gate pa = &portq->portq_alert;
10840Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate /* check alert conditions */
10870Sstevel@tonic-gate if (flags == PORT_ALERT_UPDATE) {
10880Sstevel@tonic-gate if (portq->portq_flags & PORTQ_ALERT) {
10890Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
10900Sstevel@tonic-gate return (EBUSY);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate /*
10950Sstevel@tonic-gate * Store alert data in the port to be delivered to threads
10960Sstevel@tonic-gate * which are using port_get(n) to retrieve events.
10970Sstevel@tonic-gate */
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate portq->portq_flags |= PORTQ_ALERT;
11000Sstevel@tonic-gate pa->portal_events = events; /* alert info */
11010Sstevel@tonic-gate pa->portal_pid = curproc->p_pid; /* process owner */
11020Sstevel@tonic-gate pa->portal_object = 0; /* no object */
11030Sstevel@tonic-gate pa->portal_user = user; /* user alert data */
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate /* alert and deliver alert data to waiting threads */
11060Sstevel@tonic-gate pgetp = portq->portq_thread;
11070Sstevel@tonic-gate if (pgetp == NULL) {
11080Sstevel@tonic-gate /* no threads waiting for events */
11090Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
11100Sstevel@tonic-gate return (0);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate /*
11140Sstevel@tonic-gate * Set waiting threads in alert mode (PORTGET_ALERT)..
11150Sstevel@tonic-gate * Every thread waiting for events already allocated a portget_t
11160Sstevel@tonic-gate * structure to sleep on.
11170Sstevel@tonic-gate * The port alert arguments are stored in the portget_t structure.
11180Sstevel@tonic-gate * The PORTGET_ALERT flag is set to indicate the thread to return
11190Sstevel@tonic-gate * immediately with the alert event.
11200Sstevel@tonic-gate */
11210Sstevel@tonic-gate do {
11220Sstevel@tonic-gate if ((pgetp->portget_state & PORTGET_ALERT) == 0) {
11230Sstevel@tonic-gate pa = &pgetp->portget_alert;
11240Sstevel@tonic-gate pa->portal_events = events;
11250Sstevel@tonic-gate pa->portal_object = 0;
11260Sstevel@tonic-gate pa->portal_user = user;
11270Sstevel@tonic-gate pgetp->portget_state |= PORTGET_ALERT;
11280Sstevel@tonic-gate cv_signal(&pgetp->portget_cv);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate } while ((pgetp = pgetp->portget_next) != portq->portq_thread);
11310Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
11320Sstevel@tonic-gate return (0);
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate /*
11360Sstevel@tonic-gate * Clear alert state of the port
11370Sstevel@tonic-gate */
11380Sstevel@tonic-gate static void
port_remove_alert(port_queue_t * portq)11390Sstevel@tonic-gate port_remove_alert(port_queue_t *portq)
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
11420Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_ALERT;
11430Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate /*
11470Sstevel@tonic-gate * The port_getn() function is used to retrieve events from a port.
11480Sstevel@tonic-gate *
11490Sstevel@tonic-gate * The port_getn() function returns immediately if there are enough events
11500Sstevel@tonic-gate * available in the port to satisfy the request or if the port is in alert
11510Sstevel@tonic-gate * mode (see port_alert(3c)).
11520Sstevel@tonic-gate * The timeout argument of port_getn(3c) -which is embedded in the
11530Sstevel@tonic-gate * port_gettimer_t structure- specifies if the system call should block or if it
11540Sstevel@tonic-gate * should return immediately depending on the number of events available.
11550Sstevel@tonic-gate * This function is internally used by port_getn(3c) as well as by
11560Sstevel@tonic-gate * port_get(3c).
11570Sstevel@tonic-gate */
11580Sstevel@tonic-gate static int
port_getn(port_t * pp,port_event_t * uevp,uint_t max,uint_t * nget,port_gettimer_t * pgt)11590Sstevel@tonic-gate port_getn(port_t *pp, port_event_t *uevp, uint_t max, uint_t *nget,
11600Sstevel@tonic-gate port_gettimer_t *pgt)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate port_queue_t *portq;
11630Sstevel@tonic-gate port_kevent_t *pev;
11640Sstevel@tonic-gate port_kevent_t *lev;
11650Sstevel@tonic-gate int error = 0;
11660Sstevel@tonic-gate uint_t nmax;
11670Sstevel@tonic-gate uint_t nevents;
11680Sstevel@tonic-gate uint_t eventsz;
11690Sstevel@tonic-gate port_event_t *kevp;
11700Sstevel@tonic-gate list_t *glist;
11710Sstevel@tonic-gate uint_t tnent;
11720Sstevel@tonic-gate int rval;
11730Sstevel@tonic-gate int blocking = -1;
11744123Sdm120769 int timecheck;
11750Sstevel@tonic-gate int flag;
11760Sstevel@tonic-gate timespec_t rqtime;
11770Sstevel@tonic-gate timespec_t *rqtp = NULL;
11780Sstevel@tonic-gate portget_t *pgetp;
11790Sstevel@tonic-gate void *results;
11800Sstevel@tonic-gate model_t model = get_udatamodel();
11810Sstevel@tonic-gate
11820Sstevel@tonic-gate flag = pgt->pgt_flags;
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate if (*nget > max && max > 0)
11850Sstevel@tonic-gate return (EINVAL);
11860Sstevel@tonic-gate
11870Sstevel@tonic-gate portq = &pp->port_queue;
11880Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
11890Sstevel@tonic-gate if (max == 0) {
11900Sstevel@tonic-gate /*
11911885Sraf * Return number of objects with events.
11921885Sraf * The port_block() call is required to synchronize this
11930Sstevel@tonic-gate * thread with another possible thread, which could be
11940Sstevel@tonic-gate * retrieving events from the port queue.
11950Sstevel@tonic-gate */
11961885Sraf port_block(portq);
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate * Check if a second thread is currently retrieving events
11990Sstevel@tonic-gate * and it is using the temporary event queue.
12000Sstevel@tonic-gate */
12010Sstevel@tonic-gate if (portq->portq_tnent) {
12020Sstevel@tonic-gate /* put remaining events back to the port queue */
12030Sstevel@tonic-gate port_push_eventq(portq);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate *nget = portq->portq_nent;
12061885Sraf port_unblock(portq);
12070Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
12080Sstevel@tonic-gate return (0);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate if (uevp == NULL) {
12120Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
12130Sstevel@tonic-gate return (EFAULT);
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate if (*nget == 0) { /* no events required */
12160Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
12170Sstevel@tonic-gate return (0);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate /* port is being closed ... */
12210Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
12220Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
12230Sstevel@tonic-gate return (EBADFD);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate /* return immediately if port in alert mode */
12270Sstevel@tonic-gate if (portq->portq_flags & PORTQ_ALERT) {
12280Sstevel@tonic-gate error = port_get_alert(&portq->portq_alert, uevp);
12290Sstevel@tonic-gate if (error == 0)
12300Sstevel@tonic-gate *nget = 1;
12310Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
12320Sstevel@tonic-gate return (error);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate portq->portq_thrcnt++;
12360Sstevel@tonic-gate
12370Sstevel@tonic-gate /*
12380Sstevel@tonic-gate * Now check if the completed events satisfy the
12390Sstevel@tonic-gate * "wait" requirements of the current thread:
12400Sstevel@tonic-gate */
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate if (pgt->pgt_loop) {
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate * loop entry of same thread
12450Sstevel@tonic-gate * pgt_loop is set when the current thread returns
12460Sstevel@tonic-gate * prematurely from this function. That could happen
12470Sstevel@tonic-gate * when a port is being shared between processes and
12480Sstevel@tonic-gate * this thread could not find events to return.
12490Sstevel@tonic-gate * It is not allowed to a thread to retrieve non-shareable
12500Sstevel@tonic-gate * events generated in other processes.
12510Sstevel@tonic-gate * PORTQ_WAIT_EVENTS is set when a thread already
12520Sstevel@tonic-gate * checked the current event queue and no new events
12530Sstevel@tonic-gate * are added to the queue.
12540Sstevel@tonic-gate */
12550Sstevel@tonic-gate if (((portq->portq_flags & PORTQ_WAIT_EVENTS) == 0) &&
12560Sstevel@tonic-gate (portq->portq_nent >= *nget)) {
12570Sstevel@tonic-gate /* some new events arrived ...check them */
12580Sstevel@tonic-gate goto portnowait;
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate rqtp = pgt->pgt_rqtp;
12614123Sdm120769 timecheck = pgt->pgt_timecheck;
12620Sstevel@tonic-gate pgt->pgt_flags |= PORTGET_WAIT_EVENTS;
12630Sstevel@tonic-gate } else {
12640Sstevel@tonic-gate /* check if enough events are available ... */
12650Sstevel@tonic-gate if (portq->portq_nent >= *nget)
12660Sstevel@tonic-gate goto portnowait;
12670Sstevel@tonic-gate /*
12680Sstevel@tonic-gate * There are not enough events available to satisfy
12690Sstevel@tonic-gate * the request, check timeout value and wait for
12700Sstevel@tonic-gate * incoming events.
12710Sstevel@tonic-gate */
12720Sstevel@tonic-gate error = port_get_timeout(pgt->pgt_timeout, &rqtime, &rqtp,
12730Sstevel@tonic-gate &blocking, flag);
12740Sstevel@tonic-gate if (error) {
12750Sstevel@tonic-gate port_check_return_cond(portq);
12760Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
12770Sstevel@tonic-gate return (error);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate
12800Sstevel@tonic-gate if (blocking == 0) /* don't block, check fired events */
12810Sstevel@tonic-gate goto portnowait;
12820Sstevel@tonic-gate
12830Sstevel@tonic-gate if (rqtp != NULL) {
12840Sstevel@tonic-gate timespec_t now;
12854123Sdm120769 timecheck = timechanged;
12860Sstevel@tonic-gate gethrestime(&now);
12870Sstevel@tonic-gate timespecadd(rqtp, &now);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate /* enqueue thread in the list of waiting threads */
12920Sstevel@tonic-gate pgetp = port_queue_thread(portq, *nget);
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate /* Wait here until return conditions met */
12960Sstevel@tonic-gate for (;;) {
12970Sstevel@tonic-gate if (pgetp->portget_state & PORTGET_ALERT) {
12980Sstevel@tonic-gate /* reap alert event and return */
12990Sstevel@tonic-gate error = port_get_alert(&pgetp->portget_alert, uevp);
13000Sstevel@tonic-gate if (error)
13010Sstevel@tonic-gate *nget = 0;
13020Sstevel@tonic-gate else
13030Sstevel@tonic-gate *nget = 1;
13040Sstevel@tonic-gate port_dequeue_thread(&pp->port_queue, pgetp);
13050Sstevel@tonic-gate portq->portq_thrcnt--;
13060Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
13070Sstevel@tonic-gate return (error);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate
13100Sstevel@tonic-gate /*
13110Sstevel@tonic-gate * Check if some other thread is already retrieving
13120Sstevel@tonic-gate * events (portq_getn > 0).
13130Sstevel@tonic-gate */
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate if ((portq->portq_getn == 0) &&
13160Sstevel@tonic-gate ((portq)->portq_nent >= *nget) &&
13170Sstevel@tonic-gate (!((pgt)->pgt_flags & PORTGET_WAIT_EVENTS) ||
13180Sstevel@tonic-gate !((portq)->portq_flags & PORTQ_WAIT_EVENTS)))
13190Sstevel@tonic-gate break;
13200Sstevel@tonic-gate
13210Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
13220Sstevel@tonic-gate error = EBADFD;
13230Sstevel@tonic-gate break;
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate
13260Sstevel@tonic-gate rval = cv_waituntil_sig(&pgetp->portget_cv, &portq->portq_mutex,
13274123Sdm120769 rqtp, timecheck);
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate if (rval <= 0) {
13300Sstevel@tonic-gate error = (rval == 0) ? EINTR : ETIME;
13310Sstevel@tonic-gate break;
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate /* take thread out of the wait queue */
13360Sstevel@tonic-gate port_dequeue_thread(portq, pgetp);
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate if (error != 0 && (error == EINTR || error == EBADFD ||
13390Sstevel@tonic-gate (error == ETIME && flag))) {
13400Sstevel@tonic-gate /* return without events */
13410Sstevel@tonic-gate port_check_return_cond(portq);
13420Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
13430Sstevel@tonic-gate return (error);
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate
13460Sstevel@tonic-gate portnowait:
13470Sstevel@tonic-gate /*
13480Sstevel@tonic-gate * Move port event queue to a temporary event queue .
13490Sstevel@tonic-gate * New incoming events will be continue be posted to the event queue
13500Sstevel@tonic-gate * and they will not be considered by the current thread.
13510Sstevel@tonic-gate * The idea is to avoid lock contentions or an often locking/unlocking
13520Sstevel@tonic-gate * of the port queue mutex. The contention and performance degradation
13530Sstevel@tonic-gate * could happen because:
13540Sstevel@tonic-gate * a) incoming events use the port queue mutex to enqueue new events and
13550Sstevel@tonic-gate * b) before the event can be delivered to the application it is
13560Sstevel@tonic-gate * necessary to notify the event sources about the event delivery.
13570Sstevel@tonic-gate * Sometimes the event sources can require a long time to return and
13580Sstevel@tonic-gate * the queue mutex would block incoming events.
13590Sstevel@tonic-gate * During this time incoming events (port_send_event()) do not need
13600Sstevel@tonic-gate * to awake threads waiting for events. Before the current thread
13610Sstevel@tonic-gate * returns it will check the conditions to awake other waiting threads.
13620Sstevel@tonic-gate */
13630Sstevel@tonic-gate portq->portq_getn++; /* number of threads retrieving events */
13641885Sraf port_block(portq); /* block other threads here */
13651885Sraf nmax = max < portq->portq_nent ? max : portq->portq_nent;
13661885Sraf
13670Sstevel@tonic-gate if (portq->portq_tnent) {
13680Sstevel@tonic-gate /*
13690Sstevel@tonic-gate * Move remaining events from previous thread back to the
13700Sstevel@tonic-gate * port event queue.
13710Sstevel@tonic-gate */
13720Sstevel@tonic-gate port_push_eventq(portq);
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate /* move port event queue to a temporary queue */
13750Sstevel@tonic-gate list_move_tail(&portq->portq_get_list, &portq->portq_list);
13760Sstevel@tonic-gate glist = &portq->portq_get_list; /* use temporary event queue */
13770Sstevel@tonic-gate tnent = portq->portq_nent; /* get current number of events */
13780Sstevel@tonic-gate portq->portq_nent = 0; /* no events in the port event queue */
13790Sstevel@tonic-gate portq->portq_flags |= PORTQ_WAIT_EVENTS; /* detect incoming events */
13800Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); /* event queue can be reused now */
13810Sstevel@tonic-gate
13820Sstevel@tonic-gate if (model == DATAMODEL_NATIVE) {
13830Sstevel@tonic-gate eventsz = sizeof (port_event_t);
13840Sstevel@tonic-gate kevp = kmem_alloc(eventsz * nmax, KM_NOSLEEP);
13850Sstevel@tonic-gate if (kevp == NULL) {
13860Sstevel@tonic-gate if (nmax > pp->port_max_list)
13870Sstevel@tonic-gate nmax = pp->port_max_list;
13880Sstevel@tonic-gate kevp = kmem_alloc(eventsz * nmax, KM_SLEEP);
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate results = kevp;
13910Sstevel@tonic-gate lev = NULL; /* start with first event in the queue */
13920Sstevel@tonic-gate for (nevents = 0; nevents < nmax; ) {
13930Sstevel@tonic-gate pev = port_get_kevent(glist, lev);
13940Sstevel@tonic-gate if (pev == NULL) /* no more events available */
13950Sstevel@tonic-gate break;
13960Sstevel@tonic-gate if (pev->portkev_flags & PORT_KEV_FREE) {
13970Sstevel@tonic-gate /* Just discard event */
13980Sstevel@tonic-gate list_remove(glist, pev);
13990Sstevel@tonic-gate pev->portkev_flags &= ~(PORT_CLEANUP_DONE);
14000Sstevel@tonic-gate if (PORT_FREE_EVENT(pev))
14010Sstevel@tonic-gate port_free_event_local(pev, 0);
14020Sstevel@tonic-gate tnent--;
14030Sstevel@tonic-gate continue;
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate
14060Sstevel@tonic-gate /* move event data to copyout list */
14070Sstevel@tonic-gate if (port_copy_event(&kevp[nevents], pev, glist)) {
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate * Event can not be delivered to the
14100Sstevel@tonic-gate * current process.
14110Sstevel@tonic-gate */
14120Sstevel@tonic-gate if (lev != NULL)
14130Sstevel@tonic-gate list_insert_after(glist, lev, pev);
14140Sstevel@tonic-gate else
14150Sstevel@tonic-gate list_insert_head(glist, pev);
14160Sstevel@tonic-gate lev = pev; /* last checked event */
14170Sstevel@tonic-gate } else {
14180Sstevel@tonic-gate nevents++; /* # of events ready */
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
14220Sstevel@tonic-gate } else {
14230Sstevel@tonic-gate port_event32_t *kevp32;
14240Sstevel@tonic-gate
14250Sstevel@tonic-gate eventsz = sizeof (port_event32_t);
14260Sstevel@tonic-gate kevp32 = kmem_alloc(eventsz * nmax, KM_NOSLEEP);
14270Sstevel@tonic-gate if (kevp32 == NULL) {
14280Sstevel@tonic-gate if (nmax > pp->port_max_list)
14290Sstevel@tonic-gate nmax = pp->port_max_list;
14300Sstevel@tonic-gate kevp32 = kmem_alloc(eventsz * nmax, KM_SLEEP);
14310Sstevel@tonic-gate }
14320Sstevel@tonic-gate results = kevp32;
14330Sstevel@tonic-gate lev = NULL; /* start with first event in the queue */
14340Sstevel@tonic-gate for (nevents = 0; nevents < nmax; ) {
14350Sstevel@tonic-gate pev = port_get_kevent(glist, lev);
14360Sstevel@tonic-gate if (pev == NULL) /* no more events available */
14370Sstevel@tonic-gate break;
14380Sstevel@tonic-gate if (pev->portkev_flags & PORT_KEV_FREE) {
14390Sstevel@tonic-gate /* Just discard event */
14400Sstevel@tonic-gate list_remove(glist, pev);
14410Sstevel@tonic-gate pev->portkev_flags &= ~(PORT_CLEANUP_DONE);
14420Sstevel@tonic-gate if (PORT_FREE_EVENT(pev))
14430Sstevel@tonic-gate port_free_event_local(pev, 0);
14440Sstevel@tonic-gate tnent--;
14450Sstevel@tonic-gate continue;
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate
14480Sstevel@tonic-gate /* move event data to copyout list */
14490Sstevel@tonic-gate if (port_copy_event32(&kevp32[nevents], pev, glist)) {
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate * Event can not be delivered to the
14520Sstevel@tonic-gate * current process.
14530Sstevel@tonic-gate */
14540Sstevel@tonic-gate if (lev != NULL)
14550Sstevel@tonic-gate list_insert_after(glist, lev, pev);
14560Sstevel@tonic-gate else
14570Sstevel@tonic-gate list_insert_head(glist, pev);
14580Sstevel@tonic-gate lev = pev; /* last checked event */
14590Sstevel@tonic-gate } else {
14600Sstevel@tonic-gate nevents++; /* # of events ready */
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate
14660Sstevel@tonic-gate /*
14670Sstevel@tonic-gate * Remember number of remaining events in the temporary event queue.
14680Sstevel@tonic-gate */
14690Sstevel@tonic-gate portq->portq_tnent = tnent - nevents;
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate /*
14720Sstevel@tonic-gate * Work to do before return :
14730Sstevel@tonic-gate * - push list of remaining events back to the top of the standard
14740Sstevel@tonic-gate * port queue.
14750Sstevel@tonic-gate * - if this is the last thread calling port_get(n) then wakeup the
14760Sstevel@tonic-gate * thread waiting on close(2).
14770Sstevel@tonic-gate * - check for a deferred cv_signal from port_send_event() and wakeup
14780Sstevel@tonic-gate * the sleeping thread.
14790Sstevel@tonic-gate */
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
14821885Sraf port_unblock(portq);
14830Sstevel@tonic-gate if (portq->portq_tnent) {
14840Sstevel@tonic-gate /*
14850Sstevel@tonic-gate * move remaining events in the temporary event queue back
14860Sstevel@tonic-gate * to the port event queue
14870Sstevel@tonic-gate */
14880Sstevel@tonic-gate port_push_eventq(portq);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate portq->portq_getn--; /* update # of threads retrieving events */
14910Sstevel@tonic-gate if (--portq->portq_thrcnt == 0) { /* # of threads waiting ... */
14920Sstevel@tonic-gate /* Last thread => check close(2) conditions ... */
14930Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
14940Sstevel@tonic-gate cv_signal(&portq->portq_closecv);
14950Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
14960Sstevel@tonic-gate kmem_free(results, eventsz * nmax);
14970Sstevel@tonic-gate /* do not copyout events */
14980Sstevel@tonic-gate *nget = 0;
14990Sstevel@tonic-gate return (EBADFD);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate } else if (portq->portq_getn == 0) {
15020Sstevel@tonic-gate /*
15030Sstevel@tonic-gate * no other threads retrieving events ...
15040Sstevel@tonic-gate * check wakeup conditions of sleeping threads
15050Sstevel@tonic-gate */
15060Sstevel@tonic-gate if ((portq->portq_thread != NULL) &&
15070Sstevel@tonic-gate (portq->portq_nent >= portq->portq_nget))
15080Sstevel@tonic-gate cv_signal(&portq->portq_thread->portget_cv);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate
15110Sstevel@tonic-gate /*
15120Sstevel@tonic-gate * Check PORTQ_POLLIN here because the current thread set temporarily
15130Sstevel@tonic-gate * the number of events in the queue to zero.
15140Sstevel@tonic-gate */
15150Sstevel@tonic-gate if (portq->portq_flags & PORTQ_POLLIN) {
15160Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_POLLIN;
15170Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
15180Sstevel@tonic-gate pollwakeup(&pp->port_pollhd, POLLIN);
15190Sstevel@tonic-gate } else {
15200Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
15210Sstevel@tonic-gate }
15220Sstevel@tonic-gate
15230Sstevel@tonic-gate /* now copyout list of user event structures to user space */
15240Sstevel@tonic-gate if (nevents) {
15250Sstevel@tonic-gate if (copyout(results, uevp, nevents * eventsz))
15260Sstevel@tonic-gate error = EFAULT;
15270Sstevel@tonic-gate }
15280Sstevel@tonic-gate kmem_free(results, eventsz * nmax);
15290Sstevel@tonic-gate
15300Sstevel@tonic-gate if (nevents == 0 && error == 0 && pgt->pgt_loop == 0 && blocking != 0) {
15310Sstevel@tonic-gate /* no events retrieved: check loop conditions */
15320Sstevel@tonic-gate if (blocking == -1) {
15330Sstevel@tonic-gate /* no timeout checked */
15340Sstevel@tonic-gate error = port_get_timeout(pgt->pgt_timeout,
15350Sstevel@tonic-gate &pgt->pgt_rqtime, &rqtp, &blocking, flag);
15360Sstevel@tonic-gate if (error) {
15370Sstevel@tonic-gate *nget = nevents;
15380Sstevel@tonic-gate return (error);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate if (rqtp != NULL) {
15410Sstevel@tonic-gate timespec_t now;
15424123Sdm120769 pgt->pgt_timecheck = timechanged;
15430Sstevel@tonic-gate gethrestime(&now);
15440Sstevel@tonic-gate timespecadd(&pgt->pgt_rqtime, &now);
15450Sstevel@tonic-gate }
15460Sstevel@tonic-gate pgt->pgt_rqtp = rqtp;
15470Sstevel@tonic-gate } else {
15480Sstevel@tonic-gate /* timeout already checked -> remember values */
15490Sstevel@tonic-gate pgt->pgt_rqtp = rqtp;
155041Spraks if (rqtp != NULL) {
15514123Sdm120769 pgt->pgt_timecheck = timecheck;
155241Spraks pgt->pgt_rqtime = *rqtp;
155341Spraks }
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate if (blocking)
15560Sstevel@tonic-gate /* timeout remaining */
15570Sstevel@tonic-gate pgt->pgt_loop = 1;
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate
15600Sstevel@tonic-gate /* set number of user event structures completed */
15610Sstevel@tonic-gate *nget = nevents;
15620Sstevel@tonic-gate return (error);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate
15650Sstevel@tonic-gate /*
15660Sstevel@tonic-gate * 1. copy kernel event structure to user event structure.
15670Sstevel@tonic-gate * 2. PORT_KEV_WIRED event structures will be reused by the "source"
15680Sstevel@tonic-gate * 3. Remove PORT_KEV_DONEQ flag (event removed from the event queue)
15690Sstevel@tonic-gate * 4. Other types of event structures can be delivered back to the port cache
15700Sstevel@tonic-gate * (port_free_event_local()).
15710Sstevel@tonic-gate * 5. The event source callback function is the last opportunity for the
15720Sstevel@tonic-gate * event source to update events, to free local resources associated with
15730Sstevel@tonic-gate * the event or to deny the delivery of the event.
15740Sstevel@tonic-gate */
15750Sstevel@tonic-gate static int
port_copy_event(port_event_t * puevp,port_kevent_t * pkevp,list_t * list)15760Sstevel@tonic-gate port_copy_event(port_event_t *puevp, port_kevent_t *pkevp, list_t *list)
15770Sstevel@tonic-gate {
15780Sstevel@tonic-gate int free_event = 0;
15790Sstevel@tonic-gate int flags;
15800Sstevel@tonic-gate int error;
15810Sstevel@tonic-gate
15820Sstevel@tonic-gate puevp->portev_source = pkevp->portkev_source;
15830Sstevel@tonic-gate puevp->portev_object = pkevp->portkev_object;
15840Sstevel@tonic-gate puevp->portev_user = pkevp->portkev_user;
15850Sstevel@tonic-gate puevp->portev_events = pkevp->portkev_events;
15860Sstevel@tonic-gate
15870Sstevel@tonic-gate /* remove event from the queue */
15880Sstevel@tonic-gate list_remove(list, pkevp);
15890Sstevel@tonic-gate
15900Sstevel@tonic-gate /*
15910Sstevel@tonic-gate * Events of type PORT_KEV_WIRED remain allocated by the
15920Sstevel@tonic-gate * event source.
15930Sstevel@tonic-gate */
15940Sstevel@tonic-gate flags = pkevp->portkev_flags;
15950Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_WIRED)
15960Sstevel@tonic-gate pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
15970Sstevel@tonic-gate else
15980Sstevel@tonic-gate free_event = 1;
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate if (pkevp->portkev_callback) {
16010Sstevel@tonic-gate error = (*pkevp->portkev_callback)(pkevp->portkev_arg,
16020Sstevel@tonic-gate &puevp->portev_events, pkevp->portkev_pid,
16030Sstevel@tonic-gate PORT_CALLBACK_DEFAULT, pkevp);
16040Sstevel@tonic-gate
16050Sstevel@tonic-gate if (error) {
16060Sstevel@tonic-gate /*
16070Sstevel@tonic-gate * Event can not be delivered.
16080Sstevel@tonic-gate * Caller must reinsert the event into the queue.
16090Sstevel@tonic-gate */
16100Sstevel@tonic-gate pkevp->portkev_flags = flags;
16110Sstevel@tonic-gate return (error);
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate }
16140Sstevel@tonic-gate if (free_event)
16150Sstevel@tonic-gate port_free_event_local(pkevp, 0);
16160Sstevel@tonic-gate return (0);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate
16190Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16200Sstevel@tonic-gate /*
16210Sstevel@tonic-gate * 1. copy kernel event structure to user event structure.
16220Sstevel@tonic-gate * 2. PORT_KEV_WIRED event structures will be reused by the "source"
16230Sstevel@tonic-gate * 3. Remove PORT_KEV_DONEQ flag (event removed from the event queue)
16240Sstevel@tonic-gate * 4. Other types of event structures can be delivered back to the port cache
16250Sstevel@tonic-gate * (port_free_event_local()).
16260Sstevel@tonic-gate * 5. The event source callback function is the last opportunity for the
16270Sstevel@tonic-gate * event source to update events, to free local resources associated with
16280Sstevel@tonic-gate * the event or to deny the delivery of the event.
16290Sstevel@tonic-gate */
16300Sstevel@tonic-gate static int
port_copy_event32(port_event32_t * puevp,port_kevent_t * pkevp,list_t * list)16310Sstevel@tonic-gate port_copy_event32(port_event32_t *puevp, port_kevent_t *pkevp, list_t *list)
16320Sstevel@tonic-gate {
16330Sstevel@tonic-gate int free_event = 0;
16340Sstevel@tonic-gate int error;
16350Sstevel@tonic-gate int flags;
16360Sstevel@tonic-gate
16370Sstevel@tonic-gate puevp->portev_source = pkevp->portkev_source;
16380Sstevel@tonic-gate puevp->portev_object = (daddr32_t)pkevp->portkev_object;
16390Sstevel@tonic-gate puevp->portev_user = (caddr32_t)(uintptr_t)pkevp->portkev_user;
16400Sstevel@tonic-gate puevp->portev_events = pkevp->portkev_events;
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate /* remove event from the queue */
16430Sstevel@tonic-gate list_remove(list, pkevp);
16440Sstevel@tonic-gate
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate * Events if type PORT_KEV_WIRED remain allocated by the
16470Sstevel@tonic-gate * sub-system (source).
16480Sstevel@tonic-gate */
16490Sstevel@tonic-gate
16500Sstevel@tonic-gate flags = pkevp->portkev_flags;
16510Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_WIRED)
16520Sstevel@tonic-gate pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
16530Sstevel@tonic-gate else
16540Sstevel@tonic-gate free_event = 1;
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate if (pkevp->portkev_callback != NULL) {
16570Sstevel@tonic-gate error = (*pkevp->portkev_callback)(pkevp->portkev_arg,
16580Sstevel@tonic-gate &puevp->portev_events, pkevp->portkev_pid,
16590Sstevel@tonic-gate PORT_CALLBACK_DEFAULT, pkevp);
16600Sstevel@tonic-gate if (error) {
16610Sstevel@tonic-gate /*
16620Sstevel@tonic-gate * Event can not be delivered.
16630Sstevel@tonic-gate * Caller must reinsert the event into the queue.
16640Sstevel@tonic-gate */
16650Sstevel@tonic-gate pkevp->portkev_flags = flags;
16660Sstevel@tonic-gate return (error);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate }
16690Sstevel@tonic-gate if (free_event)
16700Sstevel@tonic-gate port_free_event_local(pkevp, 0);
16710Sstevel@tonic-gate return (0);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
16740Sstevel@tonic-gate
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate * copyout alert event.
16770Sstevel@tonic-gate */
16780Sstevel@tonic-gate static int
port_get_alert(port_alert_t * pa,port_event_t * uevp)16790Sstevel@tonic-gate port_get_alert(port_alert_t *pa, port_event_t *uevp)
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate model_t model = get_udatamodel();
16820Sstevel@tonic-gate
16830Sstevel@tonic-gate /* copyout alert event structures to user space */
16840Sstevel@tonic-gate if (model == DATAMODEL_NATIVE) {
16850Sstevel@tonic-gate port_event_t uev;
16860Sstevel@tonic-gate uev.portev_source = PORT_SOURCE_ALERT;
16870Sstevel@tonic-gate uev.portev_object = pa->portal_object;
16880Sstevel@tonic-gate uev.portev_events = pa->portal_events;
16890Sstevel@tonic-gate uev.portev_user = pa->portal_user;
16900Sstevel@tonic-gate if (copyout(&uev, uevp, sizeof (port_event_t)))
16910Sstevel@tonic-gate return (EFAULT);
16920Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16930Sstevel@tonic-gate } else {
16940Sstevel@tonic-gate port_event32_t uev32;
16950Sstevel@tonic-gate uev32.portev_source = PORT_SOURCE_ALERT;
16960Sstevel@tonic-gate uev32.portev_object = (daddr32_t)pa->portal_object;
16970Sstevel@tonic-gate uev32.portev_events = pa->portal_events;
16980Sstevel@tonic-gate uev32.portev_user = (daddr32_t)(uintptr_t)pa->portal_user;
16990Sstevel@tonic-gate if (copyout(&uev32, uevp, sizeof (port_event32_t)))
17000Sstevel@tonic-gate return (EFAULT);
17010Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate return (0);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate * Check return conditions :
17080Sstevel@tonic-gate * - pending port close(2)
17090Sstevel@tonic-gate * - threads waiting for events
17100Sstevel@tonic-gate */
17110Sstevel@tonic-gate static void
port_check_return_cond(port_queue_t * portq)17120Sstevel@tonic-gate port_check_return_cond(port_queue_t *portq)
17130Sstevel@tonic-gate {
17140Sstevel@tonic-gate ASSERT(MUTEX_HELD(&portq->portq_mutex));
17150Sstevel@tonic-gate portq->portq_thrcnt--;
17160Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
17170Sstevel@tonic-gate if (portq->portq_thrcnt == 0)
17180Sstevel@tonic-gate cv_signal(&portq->portq_closecv);
17190Sstevel@tonic-gate else
17200Sstevel@tonic-gate cv_signal(&portq->portq_thread->portget_cv);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate }
17230Sstevel@tonic-gate
17240Sstevel@tonic-gate /*
17250Sstevel@tonic-gate * The port_get_kevent() function returns
17260Sstevel@tonic-gate * - the event located at the head of the queue if 'last' pointer is NULL
17270Sstevel@tonic-gate * - the next event after the event pointed by 'last'
17280Sstevel@tonic-gate * The caller of this function is responsible for the integrity of the queue
17290Sstevel@tonic-gate * in use:
17301885Sraf * - port_getn() is using a temporary queue protected with port_block().
17311885Sraf * - port_close_events() is working on the global event queue and protects
17321885Sraf * the queue with portq->portq_mutex.
17330Sstevel@tonic-gate */
17340Sstevel@tonic-gate port_kevent_t *
port_get_kevent(list_t * list,port_kevent_t * last)17350Sstevel@tonic-gate port_get_kevent(list_t *list, port_kevent_t *last)
17360Sstevel@tonic-gate {
17370Sstevel@tonic-gate if (last == NULL)
17380Sstevel@tonic-gate return (list_head(list));
17390Sstevel@tonic-gate else
17400Sstevel@tonic-gate return (list_next(list, last));
17410Sstevel@tonic-gate }
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate /*
17440Sstevel@tonic-gate * The port_get_timeout() function gets the timeout data from user space
17450Sstevel@tonic-gate * and converts that info into a corresponding internal representation.
17460Sstevel@tonic-gate * The kerneldata flag means that the timeout data is already loaded.
17470Sstevel@tonic-gate */
17480Sstevel@tonic-gate static int
port_get_timeout(timespec_t * timeout,timespec_t * rqtime,timespec_t ** rqtp,int * blocking,int kerneldata)17490Sstevel@tonic-gate port_get_timeout(timespec_t *timeout, timespec_t *rqtime, timespec_t **rqtp,
17500Sstevel@tonic-gate int *blocking, int kerneldata)
17510Sstevel@tonic-gate {
17520Sstevel@tonic-gate model_t model = get_udatamodel();
17530Sstevel@tonic-gate
17540Sstevel@tonic-gate *rqtp = NULL;
17550Sstevel@tonic-gate if (timeout == NULL) {
17560Sstevel@tonic-gate *blocking = 1;
17570Sstevel@tonic-gate return (0);
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate
17600Sstevel@tonic-gate if (kerneldata) {
17610Sstevel@tonic-gate *rqtime = *timeout;
17620Sstevel@tonic-gate } else {
17630Sstevel@tonic-gate if (model == DATAMODEL_NATIVE) {
17640Sstevel@tonic-gate if (copyin(timeout, rqtime, sizeof (*rqtime)))
17650Sstevel@tonic-gate return (EFAULT);
17660Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17670Sstevel@tonic-gate } else {
17680Sstevel@tonic-gate timespec32_t wait_time_32;
17690Sstevel@tonic-gate if (copyin(timeout, &wait_time_32,
17700Sstevel@tonic-gate sizeof (wait_time_32)))
17710Sstevel@tonic-gate return (EFAULT);
17720Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(rqtime, &wait_time_32);
17730Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate
17770Sstevel@tonic-gate if (rqtime->tv_sec == 0 && rqtime->tv_nsec == 0) {
17780Sstevel@tonic-gate *blocking = 0;
17790Sstevel@tonic-gate return (0);
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate
17820Sstevel@tonic-gate if (rqtime->tv_sec < 0 ||
17830Sstevel@tonic-gate rqtime->tv_nsec < 0 || rqtime->tv_nsec >= NANOSEC)
17840Sstevel@tonic-gate return (EINVAL);
17850Sstevel@tonic-gate
17860Sstevel@tonic-gate *rqtp = rqtime;
17870Sstevel@tonic-gate *blocking = 1;
17880Sstevel@tonic-gate return (0);
17890Sstevel@tonic-gate }
17900Sstevel@tonic-gate
17910Sstevel@tonic-gate /*
17920Sstevel@tonic-gate * port_queue_thread()
17930Sstevel@tonic-gate * Threads requiring more events than available will be put in a wait queue.
17940Sstevel@tonic-gate * There is a "thread wait queue" per port.
17950Sstevel@tonic-gate * Threads requiring less events get a higher priority than others and they
17960Sstevel@tonic-gate * will be awoken first.
17970Sstevel@tonic-gate */
17980Sstevel@tonic-gate static portget_t *
port_queue_thread(port_queue_t * portq,uint_t nget)17990Sstevel@tonic-gate port_queue_thread(port_queue_t *portq, uint_t nget)
18000Sstevel@tonic-gate {
18010Sstevel@tonic-gate portget_t *pgetp;
18020Sstevel@tonic-gate portget_t *ttp;
18030Sstevel@tonic-gate portget_t *htp;
18040Sstevel@tonic-gate
18050Sstevel@tonic-gate pgetp = kmem_zalloc(sizeof (portget_t), KM_SLEEP);
18060Sstevel@tonic-gate pgetp->portget_nget = nget;
18070Sstevel@tonic-gate pgetp->portget_pid = curproc->p_pid;
18080Sstevel@tonic-gate if (portq->portq_thread == NULL) {
18090Sstevel@tonic-gate /* first waiting thread */
18100Sstevel@tonic-gate portq->portq_thread = pgetp;
18110Sstevel@tonic-gate portq->portq_nget = nget;
18120Sstevel@tonic-gate pgetp->portget_prev = pgetp;
18130Sstevel@tonic-gate pgetp->portget_next = pgetp;
18140Sstevel@tonic-gate return (pgetp);
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate
18170Sstevel@tonic-gate /*
18180Sstevel@tonic-gate * thread waiting for less events will be set on top of the queue.
18190Sstevel@tonic-gate */
18200Sstevel@tonic-gate ttp = portq->portq_thread;
18210Sstevel@tonic-gate htp = ttp;
18220Sstevel@tonic-gate for (;;) {
18230Sstevel@tonic-gate if (nget <= ttp->portget_nget)
18240Sstevel@tonic-gate break;
18250Sstevel@tonic-gate if (htp == ttp->portget_next)
18260Sstevel@tonic-gate break; /* last event */
18270Sstevel@tonic-gate ttp = ttp->portget_next;
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate
18300Sstevel@tonic-gate /* add thread to the queue */
18310Sstevel@tonic-gate pgetp->portget_next = ttp;
18320Sstevel@tonic-gate pgetp->portget_prev = ttp->portget_prev;
18330Sstevel@tonic-gate ttp->portget_prev->portget_next = pgetp;
18340Sstevel@tonic-gate ttp->portget_prev = pgetp;
18350Sstevel@tonic-gate if (portq->portq_thread == ttp)
18360Sstevel@tonic-gate portq->portq_thread = pgetp;
18370Sstevel@tonic-gate portq->portq_nget = portq->portq_thread->portget_nget;
18380Sstevel@tonic-gate return (pgetp);
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate /*
18420Sstevel@tonic-gate * Take thread out of the queue.
18430Sstevel@tonic-gate */
18440Sstevel@tonic-gate static void
port_dequeue_thread(port_queue_t * portq,portget_t * pgetp)18450Sstevel@tonic-gate port_dequeue_thread(port_queue_t *portq, portget_t *pgetp)
18460Sstevel@tonic-gate {
18470Sstevel@tonic-gate if (pgetp->portget_next == pgetp) {
18480Sstevel@tonic-gate /* last (single) waiting thread */
18490Sstevel@tonic-gate portq->portq_thread = NULL;
18501885Sraf portq->portq_nget = 0;
18510Sstevel@tonic-gate } else {
18520Sstevel@tonic-gate pgetp->portget_prev->portget_next = pgetp->portget_next;
18530Sstevel@tonic-gate pgetp->portget_next->portget_prev = pgetp->portget_prev;
18540Sstevel@tonic-gate if (portq->portq_thread == pgetp)
18550Sstevel@tonic-gate portq->portq_thread = pgetp->portget_next;
18560Sstevel@tonic-gate portq->portq_nget = portq->portq_thread->portget_nget;
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate kmem_free(pgetp, sizeof (portget_t));
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate
18610Sstevel@tonic-gate /*
18620Sstevel@tonic-gate * Set up event port kstats.
18630Sstevel@tonic-gate */
18640Sstevel@tonic-gate static void
port_kstat_init()18650Sstevel@tonic-gate port_kstat_init()
18660Sstevel@tonic-gate {
18670Sstevel@tonic-gate kstat_t *ksp;
18680Sstevel@tonic-gate uint_t ndata;
18690Sstevel@tonic-gate
18700Sstevel@tonic-gate ndata = sizeof (port_kstat) / sizeof (kstat_named_t);
18710Sstevel@tonic-gate ksp = kstat_create("portfs", 0, "Event Ports", "misc",
18720Sstevel@tonic-gate KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_VIRTUAL);
18730Sstevel@tonic-gate if (ksp) {
18740Sstevel@tonic-gate ksp->ks_data = &port_kstat;
18750Sstevel@tonic-gate kstat_install(ksp);
18760Sstevel@tonic-gate }
18770Sstevel@tonic-gate }
1878