xref: /illumos-gate/usr/src/uts/common/os/timer.c (revision 8b79e857)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2134709573Sraf 
227c478bd9Sstevel@tonic-gate /*
23*8b79e857Spraks  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/timer.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/policy.h>
35*8b79e857Spraks #include <sys/port_impl.h>
367c478bd9Sstevel@tonic-gate #include <sys/port_kernel.h>
377c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static kmem_cache_t *clock_timer_cache;
407c478bd9Sstevel@tonic-gate static clock_backend_t *clock_backend[CLOCK_MAX];
417c478bd9Sstevel@tonic-gate static int timer_port_callback(void *, int *, pid_t, int, void *);
427c478bd9Sstevel@tonic-gate static void timer_close_port(void *, int, pid_t, int);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	CLOCK_BACKEND(clk) \
457c478bd9Sstevel@tonic-gate 	((clk) < CLOCK_MAX && (clk) >= 0 ? clock_backend[(clk)] : NULL)
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Tunable to increase the maximum number of POSIX timers per-process.  This
497c478bd9Sstevel@tonic-gate  * may _only_ be tuned in /etc/system or by patching the kernel binary; it
507c478bd9Sstevel@tonic-gate  * _cannot_ be tuned on a running system.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate int timer_max = _TIMER_MAX;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * timer_lock() locks the specified interval timer.  It doesn't look at the
567c478bd9Sstevel@tonic-gate  * ITLK_REMOVE bit; it's up to callers to look at this if they need to
577c478bd9Sstevel@tonic-gate  * care.  p_lock must be held on entry; it may be dropped and reaquired,
587c478bd9Sstevel@tonic-gate  * but timer_lock() will always return with p_lock held.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * Note that timer_create() doesn't call timer_lock(); it creates timers
617c478bd9Sstevel@tonic-gate  * with the ITLK_LOCKED bit explictly set.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate static void
647c478bd9Sstevel@tonic-gate timer_lock(proc_t *p, itimer_t *it)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	while (it->it_lock & ITLK_LOCKED) {
697c478bd9Sstevel@tonic-gate 		it->it_blockers++;
707c478bd9Sstevel@tonic-gate 		cv_wait(&it->it_cv, &p->p_lock);
717c478bd9Sstevel@tonic-gate 		it->it_blockers--;
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	it->it_lock |= ITLK_LOCKED;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * timer_unlock() unlocks the specified interval timer, waking up any
797c478bd9Sstevel@tonic-gate  * waiters.  p_lock must be held on entry; it will not be dropped by
807c478bd9Sstevel@tonic-gate  * timer_unlock().
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static void
837c478bd9Sstevel@tonic-gate timer_unlock(proc_t *p, itimer_t *it)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
867c478bd9Sstevel@tonic-gate 	ASSERT(it->it_lock & ITLK_LOCKED);
877c478bd9Sstevel@tonic-gate 	it->it_lock &= ~ITLK_LOCKED;
887c478bd9Sstevel@tonic-gate 	cv_signal(&it->it_cv);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * timer_delete_locked() takes a proc pointer, timer ID and locked interval
937c478bd9Sstevel@tonic-gate  * timer, and deletes the specified timer.  It must be called with p_lock
947c478bd9Sstevel@tonic-gate  * held, and cannot be called on a timer which already has ITLK_REMOVE set;
957c478bd9Sstevel@tonic-gate  * the caller must check this.  timer_delete_locked() will set the ITLK_REMOVE
967c478bd9Sstevel@tonic-gate  * bit and will iteratively unlock and lock the interval timer until all
977c478bd9Sstevel@tonic-gate  * blockers have seen the ITLK_REMOVE and cleared out.  It will then zero
987c478bd9Sstevel@tonic-gate  * out the specified entry in the p_itimer array, and call into the clock
997c478bd9Sstevel@tonic-gate  * backend to complete the deletion.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  * This function will always return with p_lock held.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static void
1047c478bd9Sstevel@tonic-gate timer_delete_locked(proc_t *p, timer_t tid, itimer_t *it)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1077c478bd9Sstevel@tonic-gate 	ASSERT(!(it->it_lock & ITLK_REMOVE));
1087c478bd9Sstevel@tonic-gate 	ASSERT(it->it_lock & ITLK_LOCKED);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	it->it_lock |= ITLK_REMOVE;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * If there are threads waiting to lock this timer, we'll unlock
1147c478bd9Sstevel@tonic-gate 	 * the timer, and block on the cv.  Threads blocking our removal will
1157c478bd9Sstevel@tonic-gate 	 * have the opportunity to run; when they see the ITLK_REMOVE flag
1167c478bd9Sstevel@tonic-gate 	 * set, they will immediately unlock the timer.
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	while (it->it_blockers) {
1197c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
1207c478bd9Sstevel@tonic-gate 		cv_wait(&it->it_cv, &p->p_lock);
1217c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	ASSERT(p->p_itimer[tid] == it);
1257c478bd9Sstevel@tonic-gate 	p->p_itimer[tid] = NULL;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * No one is blocked on this timer, and no one will be (we've set
1297c478bd9Sstevel@tonic-gate 	 * p_itimer[tid] to be NULL; no one can find it).  Now we call into
1307c478bd9Sstevel@tonic-gate 	 * the clock backend to delete the timer; it is up to the backend to
1317c478bd9Sstevel@tonic-gate 	 * guarantee that timer_fire() has completed (and will never again
1327c478bd9Sstevel@tonic-gate 	 * be called) for this timer.
1337c478bd9Sstevel@tonic-gate 	 */
1347c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	it->it_backend->clk_timer_delete(it);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (it->it_portev) {
1397c478bd9Sstevel@tonic-gate 		mutex_enter(&it->it_mutex);
1407c478bd9Sstevel@tonic-gate 		if (it->it_portev) {
141*8b79e857Spraks 			port_kevent_t	*pev;
1427c478bd9Sstevel@tonic-gate 			/* dissociate timer from the event port */
1437c478bd9Sstevel@tonic-gate 			(void) port_dissociate_ksource(it->it_portfd,
1447c478bd9Sstevel@tonic-gate 			    PORT_SOURCE_TIMER, (port_source_t *)it->it_portsrc);
145*8b79e857Spraks 			pev = (port_kevent_t *)it->it_portev;
1467c478bd9Sstevel@tonic-gate 			it->it_portev = NULL;
1477c478bd9Sstevel@tonic-gate 			it->it_flags &= ~IT_PORT;
1487c478bd9Sstevel@tonic-gate 			it->it_pending = 0;
1497c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
150*8b79e857Spraks 			(void) port_remove_done_event(pev);
151*8b79e857Spraks 			port_free_event(pev);
152*8b79e857Spraks 		} else {
153*8b79e857Spraks 			mutex_exit(&it->it_mutex);
154*8b79e857Spraks 		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * We need to be careful freeing the sigqueue for this timer;
1617c478bd9Sstevel@tonic-gate 	 * if a signal is pending, the sigqueue needs to be freed
1627c478bd9Sstevel@tonic-gate 	 * synchronously in siginfofree().  The need to free the sigqueue
1637c478bd9Sstevel@tonic-gate 	 * in siginfofree() is indicated by setting sq_func to NULL.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	if (it->it_pending > 0) {
1667c478bd9Sstevel@tonic-gate 		it->it_sigq->sq_func = NULL;
1677c478bd9Sstevel@tonic-gate 	} else {
1687c478bd9Sstevel@tonic-gate 		kmem_free(it->it_sigq, sizeof (sigqueue_t));
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	ASSERT(it->it_blockers == 0);
1727c478bd9Sstevel@tonic-gate 	kmem_cache_free(clock_timer_cache, it);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * timer_grab() and its companion routine, timer_release(), are wrappers
1777c478bd9Sstevel@tonic-gate  * around timer_lock()/_unlock() which allow the timer_*(3R) routines to
1787c478bd9Sstevel@tonic-gate  * (a) share error handling code and (b) not grab p_lock themselves.  Routines
1797c478bd9Sstevel@tonic-gate  * which are called with p_lock held (e.g. timer_lwpbind(), timer_lwpexit())
1807c478bd9Sstevel@tonic-gate  * must call timer_lock()/_unlock() explictly.
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  * timer_grab() takes a proc and a timer ID, and returns a pointer to a
1837c478bd9Sstevel@tonic-gate  * locked interval timer.  p_lock must _not_ be held on entry; timer_grab()
1847c478bd9Sstevel@tonic-gate  * may acquire p_lock, but will always return with p_lock dropped.
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * If timer_grab() fails, it will return NULL.  timer_grab() will fail if
1877c478bd9Sstevel@tonic-gate  * one or more of the following is true:
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  *  (a)	The specified timer ID is out of range.
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  *  (b)	The specified timer ID does not correspond to a timer ID returned
1927c478bd9Sstevel@tonic-gate  *	from timer_create(3R).
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  *  (c)	The specified timer ID is currently being removed.
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate static itimer_t *
1987c478bd9Sstevel@tonic-gate timer_grab(proc_t *p, timer_t tid)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	itimer_t **itp, *it;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (tid >= timer_max || tid < 0)
2037c478bd9Sstevel@tonic-gate 		return (NULL);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL || (it = itp[tid]) == NULL) {
2087c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2097c478bd9Sstevel@tonic-gate 		return (NULL);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	timer_lock(p, it);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (it->it_lock & ITLK_REMOVE) {
2157c478bd9Sstevel@tonic-gate 		/*
2167c478bd9Sstevel@tonic-gate 		 * Someone is removing this timer; it will soon be invalid.
2177c478bd9Sstevel@tonic-gate 		 */
2187c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
2197c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2207c478bd9Sstevel@tonic-gate 		return (NULL);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (it);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * timer_release() releases a timer acquired with timer_grab().  p_lock
2307c478bd9Sstevel@tonic-gate  * should not be held on entry; timer_release() will acquire p_lock but
2317c478bd9Sstevel@tonic-gate  * will drop it before returning.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate timer_release(proc_t *p, itimer_t *it)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2377c478bd9Sstevel@tonic-gate 	timer_unlock(p, it);
2387c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * timer_delete_grabbed() deletes a timer acquired with timer_grab().
2437c478bd9Sstevel@tonic-gate  * p_lock should not be held on entry; timer_delete_grabbed() will acquire
2447c478bd9Sstevel@tonic-gate  * p_lock, but will drop it before returning.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate static void
2477c478bd9Sstevel@tonic-gate timer_delete_grabbed(proc_t *p, timer_t tid, itimer_t *it)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2507c478bd9Sstevel@tonic-gate 	timer_delete_locked(p, tid, it);
2517c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate void
2557c478bd9Sstevel@tonic-gate clock_timer_init()
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	clock_timer_cache = kmem_cache_create("timer_cache",
2587c478bd9Sstevel@tonic-gate 	    sizeof (itimer_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate void
2627c478bd9Sstevel@tonic-gate clock_add_backend(clockid_t clock, clock_backend_t *backend)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	ASSERT(clock >= 0 && clock < CLOCK_MAX);
2657c478bd9Sstevel@tonic-gate 	ASSERT(clock_backend[clock] == NULL);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	clock_backend[clock] = backend;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate int
2717c478bd9Sstevel@tonic-gate clock_settime(clockid_t clock, timespec_t *tp)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	timespec_t t;
2747c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
2757c478bd9Sstevel@tonic-gate 	int error;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
2787c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (secpolicy_settime(CRED()) != 0)
2817c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
2847c478bd9Sstevel@tonic-gate 		if (copyin(tp, &t, sizeof (timespec_t)) != 0)
2857c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2867c478bd9Sstevel@tonic-gate 	} else {
2877c478bd9Sstevel@tonic-gate 		timespec32_t t32;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		if (copyin(tp, &t32, sizeof (timespec32_t)) != 0)
2907c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&t, &t32);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&t))
2967c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_settime(&t);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (error)
3017c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (0);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate int
3077c478bd9Sstevel@tonic-gate clock_gettime(clockid_t clock, timespec_t *tp)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	timespec_t t;
3107c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
3117c478bd9Sstevel@tonic-gate 	int error;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3147c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_gettime(&t);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (error)
3197c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3227c478bd9Sstevel@tonic-gate 		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3237c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3247c478bd9Sstevel@tonic-gate 	} else {
3257c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		if (TIMESPEC_OVERFLOW(&t))
3287c478bd9Sstevel@tonic-gate 			return (set_errno(EOVERFLOW));
3297c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&t32, &t);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3327c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate int
3397c478bd9Sstevel@tonic-gate clock_getres(clockid_t clock, timespec_t *tp)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	timespec_t t;
3427c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
3437c478bd9Sstevel@tonic-gate 	int error;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/*
3467c478bd9Sstevel@tonic-gate 	 * Strangely, the standard defines clock_getres() with a NULL tp
3477c478bd9Sstevel@tonic-gate 	 * to do nothing (regardless of the validity of the specified
3487c478bd9Sstevel@tonic-gate 	 * clock_id).  Go figure.
3497c478bd9Sstevel@tonic-gate 	 */
3507c478bd9Sstevel@tonic-gate 	if (tp == NULL)
3517c478bd9Sstevel@tonic-gate 		return (0);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3547c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_getres(&t);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (error)
3597c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3627c478bd9Sstevel@tonic-gate 		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3637c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3647c478bd9Sstevel@tonic-gate 	} else {
3657c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		if (TIMESPEC_OVERFLOW(&t))
3687c478bd9Sstevel@tonic-gate 			return (set_errno(EOVERFLOW));
3697c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&t32, &t);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3727c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (0);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate void
3797c478bd9Sstevel@tonic-gate timer_signal(sigqueue_t *sigq)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	itimer_t *it = (itimer_t *)sigq->sq_backptr;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * There are some conditions during a fork or an exit when we can
3857c478bd9Sstevel@tonic-gate 	 * call siginfofree() without p_lock held.  To prevent a race
3867c478bd9Sstevel@tonic-gate 	 * between timer_signal() and timer_fire() with regard to it_pending,
3877c478bd9Sstevel@tonic-gate 	 * we therefore acquire it_mutex in both paths.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
3907c478bd9Sstevel@tonic-gate 	ASSERT(it->it_pending > 0);
3917c478bd9Sstevel@tonic-gate 	it->it_overrun = it->it_pending - 1;
3927c478bd9Sstevel@tonic-gate 	it->it_pending = 0;
3937c478bd9Sstevel@tonic-gate 	mutex_exit(&it->it_mutex);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * This routine is called from the clock backend.
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate void
4007c478bd9Sstevel@tonic-gate timer_fire(itimer_t *it)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	proc_t *p;
4037c478bd9Sstevel@tonic-gate 	int proc_lock_held;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (it->it_flags & IT_SIGNAL) {
4067c478bd9Sstevel@tonic-gate 		/*
4077c478bd9Sstevel@tonic-gate 		 * See the comment in timer_signal() for why it is not
4087c478bd9Sstevel@tonic-gate 		 * sufficient to only grab p_lock here. Because p_lock can be
4097c478bd9Sstevel@tonic-gate 		 * held on entry to timer_signal(), the lock ordering is
4107c478bd9Sstevel@tonic-gate 		 * necessarily p_lock before it_mutex.
4117c478bd9Sstevel@tonic-gate 		 */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		p = it->it_proc;
4147c478bd9Sstevel@tonic-gate 		proc_lock_held = 1;
4157c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4167c478bd9Sstevel@tonic-gate 	} else {
4177c478bd9Sstevel@tonic-gate 		/*
4187c478bd9Sstevel@tonic-gate 		 * IT_PORT:
4197c478bd9Sstevel@tonic-gate 		 * If a timer was ever programmed to send events to a port,
4207c478bd9Sstevel@tonic-gate 		 * the IT_PORT flag will remain set until:
4217c478bd9Sstevel@tonic-gate 		 * a) the timer is deleted (see timer_delete_locked()) or
4227c478bd9Sstevel@tonic-gate 		 * b) the port is being closed (see timer_close_port()).
4237c478bd9Sstevel@tonic-gate 		 * Both cases are synchronized with the it_mutex.
4247c478bd9Sstevel@tonic-gate 		 * We don't need to use the p_lock because it is only
4257c478bd9Sstevel@tonic-gate 		 * required in the IT_SIGNAL case.
4267c478bd9Sstevel@tonic-gate 		 * If IT_PORT was set and the port is being closed then
4277c478bd9Sstevel@tonic-gate 		 * the timer notification is set to NONE. In such a case
4287c478bd9Sstevel@tonic-gate 		 * the timer itself and the it_pending counter remain active
4297c478bd9Sstevel@tonic-gate 		 * until the application deletes the counter or the process
4307c478bd9Sstevel@tonic-gate 		 * exits.
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		proc_lock_held = 0;
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (it->it_pending > 0) {
4377c478bd9Sstevel@tonic-gate 		if (it->it_pending < INT_MAX)
4387c478bd9Sstevel@tonic-gate 			it->it_pending++;
4397c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
4407c478bd9Sstevel@tonic-gate 	} else {
4417c478bd9Sstevel@tonic-gate 		if (it->it_flags & IT_PORT) {
4427c478bd9Sstevel@tonic-gate 			it->it_pending = 1;
44334709573Sraf 			port_send_event((port_kevent_t *)it->it_portev);
4447c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4457c478bd9Sstevel@tonic-gate 		} else if (it->it_flags & IT_SIGNAL) {
4467c478bd9Sstevel@tonic-gate 			it->it_pending = 1;
4477c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4487c478bd9Sstevel@tonic-gate 			sigaddqa(p, NULL, it->it_sigq);
4497c478bd9Sstevel@tonic-gate 		} else {
4507c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (proc_lock_held)
4557c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate int
4597c478bd9Sstevel@tonic-gate timer_create(clockid_t clock, struct sigevent *evp, timer_t *tid)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	struct sigevent ev;
4627c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
4637c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
4647c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
4657c478bd9Sstevel@tonic-gate 	sigqueue_t *sigq;
4667c478bd9Sstevel@tonic-gate 	cred_t *cr = CRED();
4677c478bd9Sstevel@tonic-gate 	int error = 0;
4687c478bd9Sstevel@tonic-gate 	timer_t i;
4697c478bd9Sstevel@tonic-gate 	port_notify_t tim_pnevp;
4707c478bd9Sstevel@tonic-gate 	port_kevent_t *pkevp = NULL;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
4737c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (evp != NULL) {
4767c478bd9Sstevel@tonic-gate 		/*
4777c478bd9Sstevel@tonic-gate 		 * short copyin() for binary compatibility
4787c478bd9Sstevel@tonic-gate 		 * fetch oldsigevent to determine how much to copy in.
4797c478bd9Sstevel@tonic-gate 		 */
4807c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
4817c478bd9Sstevel@tonic-gate 			if (copyin(evp, &ev, sizeof (struct oldsigevent)))
4827c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
4837c478bd9Sstevel@tonic-gate 
48434709573Sraf 			if (ev.sigev_notify == SIGEV_PORT ||
48534709573Sraf 			    ev.sigev_notify == SIGEV_THREAD) {
4867c478bd9Sstevel@tonic-gate 				if (copyin(ev.sigev_value.sival_ptr, &tim_pnevp,
4877c478bd9Sstevel@tonic-gate 				    sizeof (port_notify_t)))
4887c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
4917c478bd9Sstevel@tonic-gate 		} else {
4927c478bd9Sstevel@tonic-gate 			struct sigevent32 ev32;
4937c478bd9Sstevel@tonic-gate 			port_notify32_t tim_pnevp32;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 			if (copyin(evp, &ev32, sizeof (struct oldsigevent32)))
4967c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
4977c478bd9Sstevel@tonic-gate 			ev.sigev_notify = ev32.sigev_notify;
4987c478bd9Sstevel@tonic-gate 			ev.sigev_signo = ev32.sigev_signo;
4997c478bd9Sstevel@tonic-gate 			/*
5007c478bd9Sstevel@tonic-gate 			 * See comment in sigqueue32() on handling of 32-bit
5017c478bd9Sstevel@tonic-gate 			 * sigvals in a 64-bit kernel.
5027c478bd9Sstevel@tonic-gate 			 */
5037c478bd9Sstevel@tonic-gate 			ev.sigev_value.sival_int = ev32.sigev_value.sival_int;
50434709573Sraf 			if (ev.sigev_notify == SIGEV_PORT ||
50534709573Sraf 			    ev.sigev_notify == SIGEV_THREAD) {
5067c478bd9Sstevel@tonic-gate 				if (copyin((void *)(uintptr_t)
5077c478bd9Sstevel@tonic-gate 				    ev32.sigev_value.sival_ptr,
5087c478bd9Sstevel@tonic-gate 				    (void *)&tim_pnevp32,
5097c478bd9Sstevel@tonic-gate 				    sizeof (port_notify32_t)))
5107c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
5117c478bd9Sstevel@tonic-gate 				tim_pnevp.portnfy_port =
5127c478bd9Sstevel@tonic-gate 				    tim_pnevp32.portnfy_port;
5137c478bd9Sstevel@tonic-gate 				tim_pnevp.portnfy_user =
5147c478bd9Sstevel@tonic-gate 				    (void *)(uintptr_t)tim_pnevp32.portnfy_user;
5157c478bd9Sstevel@tonic-gate 			}
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 		switch (ev.sigev_notify) {
5197c478bd9Sstevel@tonic-gate 		case SIGEV_NONE:
5207c478bd9Sstevel@tonic-gate 			break;
5217c478bd9Sstevel@tonic-gate 		case SIGEV_SIGNAL:
5227c478bd9Sstevel@tonic-gate 			if (ev.sigev_signo < 1 || ev.sigev_signo >= NSIG)
5237c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
5247c478bd9Sstevel@tonic-gate 			break;
52534709573Sraf 		case SIGEV_THREAD:
5267c478bd9Sstevel@tonic-gate 		case SIGEV_PORT:
5277c478bd9Sstevel@tonic-gate 			break;
5287c478bd9Sstevel@tonic-gate 		default:
5297c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 	} else {
5327c478bd9Sstevel@tonic-gate 		/*
5337c478bd9Sstevel@tonic-gate 		 * Use the clock's default sigevent (this is a structure copy).
5347c478bd9Sstevel@tonic-gate 		 */
5357c478bd9Sstevel@tonic-gate 		ev = backend->clk_default;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * We'll allocate our timer and sigqueue now, before we grab p_lock.
5407c478bd9Sstevel@tonic-gate 	 * If we can't find an empty slot, we'll free them before returning.
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	it = kmem_cache_alloc(clock_timer_cache, KM_SLEEP);
5437c478bd9Sstevel@tonic-gate 	bzero(it, sizeof (itimer_t));
5447c478bd9Sstevel@tonic-gate 	mutex_init(&it->it_mutex, NULL, MUTEX_DEFAULT, NULL);
5457c478bd9Sstevel@tonic-gate 	sigq = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * If this is this process' first timer, we need to attempt to allocate
5517c478bd9Sstevel@tonic-gate 	 * an array of timerstr_t pointers.  We drop p_lock to perform the
5527c478bd9Sstevel@tonic-gate 	 * allocation; if we return to discover that p_itimer is non-NULL,
5537c478bd9Sstevel@tonic-gate 	 * we will free our allocation and drive on.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL) {
5567c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5577c478bd9Sstevel@tonic-gate 		itp = kmem_zalloc(timer_max * sizeof (itimer_t *), KM_SLEEP);
5587c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		if (p->p_itimer == NULL)
5617c478bd9Sstevel@tonic-gate 			p->p_itimer = itp;
5627c478bd9Sstevel@tonic-gate 		else {
5637c478bd9Sstevel@tonic-gate 			kmem_free(itp, timer_max * sizeof (itimer_t *));
5647c478bd9Sstevel@tonic-gate 			itp = p->p_itimer;
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max && itp[i] != NULL; i++)
5697c478bd9Sstevel@tonic-gate 		continue;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (i == timer_max) {
5727c478bd9Sstevel@tonic-gate 		/*
5737c478bd9Sstevel@tonic-gate 		 * We couldn't find a slot.  Drop p_lock, free the preallocated
5747c478bd9Sstevel@tonic-gate 		 * timer and sigqueue, and return an error.
5757c478bd9Sstevel@tonic-gate 		 */
5767c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5777c478bd9Sstevel@tonic-gate 		kmem_cache_free(clock_timer_cache, it);
5787c478bd9Sstevel@tonic-gate 		kmem_free(sigq, sizeof (sigqueue_t));
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	ASSERT(i < timer_max && itp[i] == NULL);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * If we develop other notification mechanisms, this will need
5877c478bd9Sstevel@tonic-gate 	 * to call into (yet another) backend.
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_signo = ev.sigev_signo;
59034709573Sraf 	if (evp == NULL)
59134709573Sraf 		sigq->sq_info.si_value.sival_int = i;
59234709573Sraf 	else
5937c478bd9Sstevel@tonic-gate 		sigq->sq_info.si_value = ev.sigev_value;
5947c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_code = SI_TIMER;
5957c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_pid = p->p_pid;
5967c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_ctid = PRCTID(p);
5977c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_zoneid = getzoneid();
5987c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_uid = crgetruid(cr);
5997c478bd9Sstevel@tonic-gate 	sigq->sq_func = timer_signal;
6007c478bd9Sstevel@tonic-gate 	sigq->sq_next = NULL;
6017c478bd9Sstevel@tonic-gate 	sigq->sq_backptr = it;
6027c478bd9Sstevel@tonic-gate 	it->it_sigq = sigq;
6037c478bd9Sstevel@tonic-gate 	it->it_backend = backend;
6047c478bd9Sstevel@tonic-gate 	it->it_lock = ITLK_LOCKED;
6057c478bd9Sstevel@tonic-gate 	itp[i] = it;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 
60834709573Sraf 	if (ev.sigev_notify == SIGEV_THREAD ||
60934709573Sraf 	    ev.sigev_notify == SIGEV_PORT) {
6107c478bd9Sstevel@tonic-gate 		int port;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		/*
6137c478bd9Sstevel@tonic-gate 		 * This timer is programmed to use event port notification when
6147c478bd9Sstevel@tonic-gate 		 * the timer fires:
6157c478bd9Sstevel@tonic-gate 		 * - allocate a port event structure and prepare it to be sent
6167c478bd9Sstevel@tonic-gate 		 *   to the port as soon as the timer fires.
6177c478bd9Sstevel@tonic-gate 		 * - when the timer fires :
6187c478bd9Sstevel@tonic-gate 		 *   - if event structure was already sent to the port then this
6197c478bd9Sstevel@tonic-gate 		 *	is a timer fire overflow => increment overflow counter.
6207c478bd9Sstevel@tonic-gate 		 *   - otherwise send pre-allocated event structure to the port.
6217c478bd9Sstevel@tonic-gate 		 * - the events field of the port_event_t structure counts the
6227c478bd9Sstevel@tonic-gate 		 *   number of timer fired events.
6237c478bd9Sstevel@tonic-gate 		 * - The event structured is allocated using the
6247c478bd9Sstevel@tonic-gate 		 *   PORT_ALLOC_CACHED flag.
6257c478bd9Sstevel@tonic-gate 		 *   This flag indicates that the timer itself will manage and
6267c478bd9Sstevel@tonic-gate 		 *   free the event structure when required.
6277c478bd9Sstevel@tonic-gate 		 */
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		it->it_flags |= IT_PORT;
6307c478bd9Sstevel@tonic-gate 		port = tim_pnevp.portnfy_port;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		/* associate timer as event source with the port */
6337c478bd9Sstevel@tonic-gate 		error = port_associate_ksource(port, PORT_SOURCE_TIMER,
6347c478bd9Sstevel@tonic-gate 		    (port_source_t **)&it->it_portsrc, timer_close_port,
6357c478bd9Sstevel@tonic-gate 		    (void *)it, NULL);
6367c478bd9Sstevel@tonic-gate 		if (error) {
6377c478bd9Sstevel@tonic-gate 			itp[i] = NULL;		/* clear slot */
6387c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
6397c478bd9Sstevel@tonic-gate 			kmem_cache_free(clock_timer_cache, it);
6407c478bd9Sstevel@tonic-gate 			kmem_free(sigq, sizeof (sigqueue_t));
6417c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		/* allocate an event structure/slot */
6457c478bd9Sstevel@tonic-gate 		error = port_alloc_event(port, PORT_ALLOC_SCACHED,
6467c478bd9Sstevel@tonic-gate 		    PORT_SOURCE_TIMER, &pkevp);
6477c478bd9Sstevel@tonic-gate 		if (error) {
6487c478bd9Sstevel@tonic-gate 			(void) port_dissociate_ksource(port, PORT_SOURCE_TIMER,
6497c478bd9Sstevel@tonic-gate 			    (port_source_t *)it->it_portsrc);
6507c478bd9Sstevel@tonic-gate 			itp[i] = NULL;		/* clear slot */
6517c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
6527c478bd9Sstevel@tonic-gate 			kmem_cache_free(clock_timer_cache, it);
6537c478bd9Sstevel@tonic-gate 			kmem_free(sigq, sizeof (sigqueue_t));
6547c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		/* initialize event data */
6587c478bd9Sstevel@tonic-gate 		port_init_event(pkevp, i, tim_pnevp.portnfy_user,
6597c478bd9Sstevel@tonic-gate 		    timer_port_callback, it);
6607c478bd9Sstevel@tonic-gate 		it->it_portev = pkevp;
6617c478bd9Sstevel@tonic-gate 		it->it_portfd = port;
6627c478bd9Sstevel@tonic-gate 	} else {
6637c478bd9Sstevel@tonic-gate 		if (ev.sigev_notify == SIGEV_SIGNAL)
6647c478bd9Sstevel@tonic-gate 			it->it_flags |= IT_SIGNAL;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * Call on the backend to verify the event argument (or return
6717c478bd9Sstevel@tonic-gate 	 * EINVAL if this clock type does not support timers).
6727c478bd9Sstevel@tonic-gate 	 */
6737c478bd9Sstevel@tonic-gate 	if ((error = backend->clk_timer_create(it, &ev)) != 0)
6747c478bd9Sstevel@tonic-gate 		goto err;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	it->it_lwp = ttolwp(curthread);
6777c478bd9Sstevel@tonic-gate 	it->it_proc = p;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (copyout(&i, tid, sizeof (timer_t)) != 0) {
6807c478bd9Sstevel@tonic-gate 		error = EFAULT;
6817c478bd9Sstevel@tonic-gate 		goto err;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/*
6857c478bd9Sstevel@tonic-gate 	 * If we're here, then we have successfully created the timer; we
6867c478bd9Sstevel@tonic-gate 	 * just need to release the timer and return.
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	timer_release(p, it);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	return (0);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate err:
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * If we're here, an error has occurred late in the timer creation
6957c478bd9Sstevel@tonic-gate 	 * process.  We need to regrab p_lock, and delete the incipient timer.
6967c478bd9Sstevel@tonic-gate 	 * Since we never unlocked the timer (it was born locked), it's
6977c478bd9Sstevel@tonic-gate 	 * impossible for a removal to be pending.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	ASSERT(!(it->it_lock & ITLK_REMOVE));
7007c478bd9Sstevel@tonic-gate 	timer_delete_grabbed(p, i, it);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	return (set_errno(error));
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate int
7067c478bd9Sstevel@tonic-gate timer_gettime(timer_t tid, itimerspec_t *val)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
7097c478bd9Sstevel@tonic-gate 	itimer_t *it;
7107c478bd9Sstevel@tonic-gate 	itimerspec_t when;
7117c478bd9Sstevel@tonic-gate 	int error;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
7147c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	error = it->it_backend->clk_timer_gettime(it, &when);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	timer_release(p, it);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (error == 0) {
7217c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
7227c478bd9Sstevel@tonic-gate 			if (copyout(&when, val, sizeof (itimerspec_t)))
7237c478bd9Sstevel@tonic-gate 				error = EFAULT;
7247c478bd9Sstevel@tonic-gate 		} else {
7257c478bd9Sstevel@tonic-gate 			if (ITIMERSPEC_OVERFLOW(&when))
7267c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
7277c478bd9Sstevel@tonic-gate 			else {
7287c478bd9Sstevel@tonic-gate 				itimerspec32_t w32;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 				ITIMERSPEC_TO_ITIMERSPEC32(&w32, &when)
7317c478bd9Sstevel@tonic-gate 				if (copyout(&w32, val, sizeof (itimerspec32_t)))
7327c478bd9Sstevel@tonic-gate 					error = EFAULT;
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate int
7417c478bd9Sstevel@tonic-gate timer_settime(timer_t tid, int flags, itimerspec_t *val, itimerspec_t *oval)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	itimerspec_t when;
7447c478bd9Sstevel@tonic-gate 	timespec_t res;
7457c478bd9Sstevel@tonic-gate 	itimer_t *it;
7467c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
7477c478bd9Sstevel@tonic-gate 	int error;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (oval != NULL) {
7507c478bd9Sstevel@tonic-gate 		if ((error = timer_gettime(tid, oval)) != 0)
7517c478bd9Sstevel@tonic-gate 			return (error);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
7557c478bd9Sstevel@tonic-gate 		if (copyin(val, &when, sizeof (itimerspec_t)))
7567c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7577c478bd9Sstevel@tonic-gate 	} else {
7587c478bd9Sstevel@tonic-gate 		itimerspec32_t w32;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		if (copyin(val, &w32, sizeof (itimerspec32_t)))
7617c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		ITIMERSPEC32_TO_ITIMERSPEC(&when, &w32);
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&when.it_value) ||
7677c478bd9Sstevel@tonic-gate 	    (itimerspecfix(&when.it_interval) &&
7687c478bd9Sstevel@tonic-gate 	    timerspecisset(&when.it_value))) {
7697c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
7737c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * From the man page:
7777c478bd9Sstevel@tonic-gate 	 *	Time values that are between two consecutive non-negative
7787c478bd9Sstevel@tonic-gate 	 *	integer multiples of the resolution of the specified timer
7797c478bd9Sstevel@tonic-gate 	 *	shall be rounded up to the larger multiple of the resolution.
7807c478bd9Sstevel@tonic-gate 	 * We assume that the resolution of any clock is less than one second.
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	if (it->it_backend->clk_clock_getres(&res) == 0 && res.tv_nsec > 1) {
7837c478bd9Sstevel@tonic-gate 		long rem;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		if ((rem = when.it_interval.tv_nsec % res.tv_nsec) != 0) {
7867c478bd9Sstevel@tonic-gate 			when.it_interval.tv_nsec += res.tv_nsec - rem;
7877c478bd9Sstevel@tonic-gate 			timespecfix(&when.it_interval);
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		if ((rem = when.it_value.tv_nsec % res.tv_nsec) != 0) {
7907c478bd9Sstevel@tonic-gate 			when.it_value.tv_nsec += res.tv_nsec - rem;
7917c478bd9Sstevel@tonic-gate 			timespecfix(&when.it_value);
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	error = it->it_backend->clk_timer_settime(it, flags, &when);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	timer_release(p, it);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate int
8027c478bd9Sstevel@tonic-gate timer_delete(timer_t tid)
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8057c478bd9Sstevel@tonic-gate 	itimer_t *it;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8087c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	timer_delete_grabbed(p, tid, it);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	return (0);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate int
8167c478bd9Sstevel@tonic-gate timer_getoverrun(timer_t tid)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	int overrun;
8197c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8207c478bd9Sstevel@tonic-gate 	itimer_t *it;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8237c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * The it_overrun field is protected by p_lock; we need to acquire
8277c478bd9Sstevel@tonic-gate 	 * it before looking at the value.
8287c478bd9Sstevel@tonic-gate 	 */
8297c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
8307c478bd9Sstevel@tonic-gate 	overrun = it->it_overrun;
8317c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	timer_release(p, it);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	return (overrun);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * Entered/exited with p_lock held, but will repeatedly drop and regrab p_lock.
8407c478bd9Sstevel@tonic-gate  */
8417c478bd9Sstevel@tonic-gate void
8427c478bd9Sstevel@tonic-gate timer_lwpexit(void)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	timer_t i;
8457c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8467c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8477c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL)
8527c478bd9Sstevel@tonic-gate 		return;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max; i++) {
8557c478bd9Sstevel@tonic-gate 		if ((it = itp[i]) == NULL)
8567c478bd9Sstevel@tonic-gate 			continue;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		if ((it->it_lock & ITLK_REMOVE) || it->it_lwp != lwp) {
8617c478bd9Sstevel@tonic-gate 			/*
8627c478bd9Sstevel@tonic-gate 			 * This timer is either being removed or it isn't
8637c478bd9Sstevel@tonic-gate 			 * associated with this lwp.
8647c478bd9Sstevel@tonic-gate 			 */
8657c478bd9Sstevel@tonic-gate 			timer_unlock(p, it);
8667c478bd9Sstevel@tonic-gate 			continue;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		/*
8707c478bd9Sstevel@tonic-gate 		 * The LWP that created this timer is going away.  To the user,
8717c478bd9Sstevel@tonic-gate 		 * our behavior here is explicitly undefined.  We will simply
8727c478bd9Sstevel@tonic-gate 		 * null out the it_lwp field; if the LWP was bound to a CPU,
8737c478bd9Sstevel@tonic-gate 		 * the cyclic will stay bound to that CPU until the process
8747c478bd9Sstevel@tonic-gate 		 * exits.
8757c478bd9Sstevel@tonic-gate 		 */
8767c478bd9Sstevel@tonic-gate 		it->it_lwp = NULL;
8777c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /*
8827c478bd9Sstevel@tonic-gate  * Called to notify of an LWP binding change.  Entered/exited with p_lock
8837c478bd9Sstevel@tonic-gate  * held, but will repeatedly drop and regrab p_lock.
8847c478bd9Sstevel@tonic-gate  */
8857c478bd9Sstevel@tonic-gate void
8867c478bd9Sstevel@tonic-gate timer_lwpbind()
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	timer_t i;
8897c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8907c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8917c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL)
8967c478bd9Sstevel@tonic-gate 		return;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max; i++) {
8997c478bd9Sstevel@tonic-gate 		if ((it = itp[i]) == NULL)
9007c478bd9Sstevel@tonic-gate 			continue;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		if (!(it->it_lock & ITLK_REMOVE) && it->it_lwp == lwp) {
9057c478bd9Sstevel@tonic-gate 			/*
9067c478bd9Sstevel@tonic-gate 			 * Drop p_lock and jump into the backend.
9077c478bd9Sstevel@tonic-gate 			 */
9087c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
9097c478bd9Sstevel@tonic-gate 			it->it_backend->clk_timer_lwpbind(it);
9107c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate  * This function should only be called if p_itimer is non-NULL.
9197c478bd9Sstevel@tonic-gate  */
9207c478bd9Sstevel@tonic-gate void
9217c478bd9Sstevel@tonic-gate timer_exit(void)
9227c478bd9Sstevel@tonic-gate {
9237c478bd9Sstevel@tonic-gate 	timer_t i;
9247c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	ASSERT(p->p_itimer != NULL);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max; i++)
9297c478bd9Sstevel@tonic-gate 		(void) timer_delete(i);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	kmem_free(p->p_itimer, timer_max * sizeof (itimer_t *));
9327c478bd9Sstevel@tonic-gate 	p->p_itimer = NULL;
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate /*
9367c478bd9Sstevel@tonic-gate  * timer_port_callback() is a callback function which is associated with the
9377c478bd9Sstevel@tonic-gate  * timer event and is activated just before the event is delivered to the user.
9387c478bd9Sstevel@tonic-gate  * The timer uses this function to update/set the overflow counter and
9397c478bd9Sstevel@tonic-gate  * to reenable the use of the event structure.
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /* ARGSUSED */
9437c478bd9Sstevel@tonic-gate static int
9447c478bd9Sstevel@tonic-gate timer_port_callback(void *arg, int *events, pid_t pid, int flag, void *evp)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	itimer_t	*it = arg;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
9497c478bd9Sstevel@tonic-gate 	if (curproc != it->it_proc) {
9507c478bd9Sstevel@tonic-gate 		/* can not deliver timer events to another proc */
9517c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
9527c478bd9Sstevel@tonic-gate 		return (EACCES);
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 	*events = it->it_pending;	/* 1 = 1 event, >1 # of overflows */
9557c478bd9Sstevel@tonic-gate 	it->it_pending = 0;		/* reinit overflow counter	*/
9567c478bd9Sstevel@tonic-gate 	/*
9577c478bd9Sstevel@tonic-gate 	 * This function can also be activated when the port is being closed
9587c478bd9Sstevel@tonic-gate 	 * and a timer event is already submitted to the port.
9597c478bd9Sstevel@tonic-gate 	 * In such a case the event port framework will use the
9607c478bd9Sstevel@tonic-gate 	 * close-callback function to notify the events sources.
9617c478bd9Sstevel@tonic-gate 	 * The timer close-callback function is timer_close_port() which
9627c478bd9Sstevel@tonic-gate 	 * will free all allocated resources (including the allocated
9637c478bd9Sstevel@tonic-gate 	 * port event structure).
9647c478bd9Sstevel@tonic-gate 	 * For that reason we don't need to check the value of flag here.
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 	mutex_exit(&it->it_mutex);
9677c478bd9Sstevel@tonic-gate 	return (0);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * port is being closed ... free all allocated port event structures
9727c478bd9Sstevel@tonic-gate  * The delivered arg currently correspond to the first timer associated with
9737c478bd9Sstevel@tonic-gate  * the port and it is not useable in this case.
9747c478bd9Sstevel@tonic-gate  * We have to scan the list of activated timers in the current proc and
9757c478bd9Sstevel@tonic-gate  * compare them with the delivered port id.
9767c478bd9Sstevel@tonic-gate  */
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate /* ARGSUSED */
9797c478bd9Sstevel@tonic-gate static void
9807c478bd9Sstevel@tonic-gate timer_close_port(void *arg, int port, pid_t pid, int lastclose)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	proc_t		*p = curproc;
9837c478bd9Sstevel@tonic-gate 	timer_t		tid;
9847c478bd9Sstevel@tonic-gate 	itimer_t	*it;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	for (tid = 0; tid < timer_max; tid++) {
9877c478bd9Sstevel@tonic-gate 		if ((it = timer_grab(p, tid)) == NULL)
9887c478bd9Sstevel@tonic-gate 			continue;
9897c478bd9Sstevel@tonic-gate 		if (it->it_portev) {
9907c478bd9Sstevel@tonic-gate 			mutex_enter(&it->it_mutex);
9917c478bd9Sstevel@tonic-gate 			if (it->it_portfd == port) {
992*8b79e857Spraks 				port_kevent_t *pev;
993*8b79e857Spraks 				pev = (port_kevent_t *)it->it_portev;
9947c478bd9Sstevel@tonic-gate 				it->it_portev = NULL;
9957c478bd9Sstevel@tonic-gate 				it->it_flags &= ~IT_PORT;
9967c478bd9Sstevel@tonic-gate 				mutex_exit(&it->it_mutex);
997*8b79e857Spraks 				(void) port_remove_done_event(pev);
998*8b79e857Spraks 				port_free_event(pev);
999*8b79e857Spraks 			} else {
1000*8b79e857Spraks 				mutex_exit(&it->it_mutex);
1001*8b79e857Spraks 			}
10027c478bd9Sstevel@tonic-gate 		}
10037c478bd9Sstevel@tonic-gate 		timer_release(p, it);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate }
1006