xref: /titanic_53/usr/src/uts/common/os/timer.c (revision 3470957343f37ed9baa957980891dbbe4c2d7092)
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
5*34709573Sraf  * Common Development and Distribution License (the "License").
6*34709573Sraf  * 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  */
21*34709573Sraf 
227c478bd9Sstevel@tonic-gate /*
23*34709573Sraf  * Copyright 2006 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>
357c478bd9Sstevel@tonic-gate #include <sys/port.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) {
1417c478bd9Sstevel@tonic-gate 			/* dissociate timer from the event port */
1427c478bd9Sstevel@tonic-gate 			(void) port_dissociate_ksource(it->it_portfd,
1437c478bd9Sstevel@tonic-gate 			    PORT_SOURCE_TIMER, (port_source_t *)it->it_portsrc);
1447c478bd9Sstevel@tonic-gate 			port_free_event((port_kevent_t *)it->it_portev);
1457c478bd9Sstevel@tonic-gate 			it->it_portev = NULL;
1467c478bd9Sstevel@tonic-gate 			it->it_flags &= ~IT_PORT;
1477c478bd9Sstevel@tonic-gate 			it->it_pending = 0;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * We need to be careful freeing the sigqueue for this timer;
1567c478bd9Sstevel@tonic-gate 	 * if a signal is pending, the sigqueue needs to be freed
1577c478bd9Sstevel@tonic-gate 	 * synchronously in siginfofree().  The need to free the sigqueue
1587c478bd9Sstevel@tonic-gate 	 * in siginfofree() is indicated by setting sq_func to NULL.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	if (it->it_pending > 0) {
1617c478bd9Sstevel@tonic-gate 		it->it_sigq->sq_func = NULL;
1627c478bd9Sstevel@tonic-gate 	} else {
1637c478bd9Sstevel@tonic-gate 		kmem_free(it->it_sigq, sizeof (sigqueue_t));
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	ASSERT(it->it_blockers == 0);
1677c478bd9Sstevel@tonic-gate 	kmem_cache_free(clock_timer_cache, it);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * timer_grab() and its companion routine, timer_release(), are wrappers
1727c478bd9Sstevel@tonic-gate  * around timer_lock()/_unlock() which allow the timer_*(3R) routines to
1737c478bd9Sstevel@tonic-gate  * (a) share error handling code and (b) not grab p_lock themselves.  Routines
1747c478bd9Sstevel@tonic-gate  * which are called with p_lock held (e.g. timer_lwpbind(), timer_lwpexit())
1757c478bd9Sstevel@tonic-gate  * must call timer_lock()/_unlock() explictly.
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  * timer_grab() takes a proc and a timer ID, and returns a pointer to a
1787c478bd9Sstevel@tonic-gate  * locked interval timer.  p_lock must _not_ be held on entry; timer_grab()
1797c478bd9Sstevel@tonic-gate  * may acquire p_lock, but will always return with p_lock dropped.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  * If timer_grab() fails, it will return NULL.  timer_grab() will fail if
1827c478bd9Sstevel@tonic-gate  * one or more of the following is true:
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  *  (a)	The specified timer ID is out of range.
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  *  (b)	The specified timer ID does not correspond to a timer ID returned
1877c478bd9Sstevel@tonic-gate  *	from timer_create(3R).
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  *  (c)	The specified timer ID is currently being removed.
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static itimer_t *
1937c478bd9Sstevel@tonic-gate timer_grab(proc_t *p, timer_t tid)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	itimer_t **itp, *it;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (tid >= timer_max || tid < 0)
1987c478bd9Sstevel@tonic-gate 		return (NULL);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL || (it = itp[tid]) == NULL) {
2037c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2047c478bd9Sstevel@tonic-gate 		return (NULL);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	timer_lock(p, it);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (it->it_lock & ITLK_REMOVE) {
2107c478bd9Sstevel@tonic-gate 		/*
2117c478bd9Sstevel@tonic-gate 		 * Someone is removing this timer; it will soon be invalid.
2127c478bd9Sstevel@tonic-gate 		 */
2137c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
2147c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2157c478bd9Sstevel@tonic-gate 		return (NULL);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (it);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * timer_release() releases a timer acquired with timer_grab().  p_lock
2257c478bd9Sstevel@tonic-gate  * should not be held on entry; timer_release() will acquire p_lock but
2267c478bd9Sstevel@tonic-gate  * will drop it before returning.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static void
2297c478bd9Sstevel@tonic-gate timer_release(proc_t *p, itimer_t *it)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2327c478bd9Sstevel@tonic-gate 	timer_unlock(p, it);
2337c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * timer_delete_grabbed() deletes a timer acquired with timer_grab().
2387c478bd9Sstevel@tonic-gate  * p_lock should not be held on entry; timer_delete_grabbed() will acquire
2397c478bd9Sstevel@tonic-gate  * p_lock, but will drop it before returning.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate static void
2427c478bd9Sstevel@tonic-gate timer_delete_grabbed(proc_t *p, timer_t tid, itimer_t *it)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2457c478bd9Sstevel@tonic-gate 	timer_delete_locked(p, tid, it);
2467c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate void
2507c478bd9Sstevel@tonic-gate clock_timer_init()
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	clock_timer_cache = kmem_cache_create("timer_cache",
2537c478bd9Sstevel@tonic-gate 	    sizeof (itimer_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate void
2577c478bd9Sstevel@tonic-gate clock_add_backend(clockid_t clock, clock_backend_t *backend)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	ASSERT(clock >= 0 && clock < CLOCK_MAX);
2607c478bd9Sstevel@tonic-gate 	ASSERT(clock_backend[clock] == NULL);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	clock_backend[clock] = backend;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate int
2667c478bd9Sstevel@tonic-gate clock_settime(clockid_t clock, timespec_t *tp)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	timespec_t t;
2697c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
2707c478bd9Sstevel@tonic-gate 	int error;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
2737c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (secpolicy_settime(CRED()) != 0)
2767c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
2797c478bd9Sstevel@tonic-gate 		if (copyin(tp, &t, sizeof (timespec_t)) != 0)
2807c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2817c478bd9Sstevel@tonic-gate 	} else {
2827c478bd9Sstevel@tonic-gate 		timespec32_t t32;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		if (copyin(tp, &t32, sizeof (timespec32_t)) != 0)
2857c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&t, &t32);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&t))
2917c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_settime(&t);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (error)
2967c478bd9Sstevel@tonic-gate 		return (set_errno(error));
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate int
3027c478bd9Sstevel@tonic-gate clock_gettime(clockid_t clock, timespec_t *tp)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	timespec_t t;
3057c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
3067c478bd9Sstevel@tonic-gate 	int error;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3097c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_gettime(&t);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (error)
3147c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3177c478bd9Sstevel@tonic-gate 		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3187c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3197c478bd9Sstevel@tonic-gate 	} else {
3207c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		if (TIMESPEC_OVERFLOW(&t))
3237c478bd9Sstevel@tonic-gate 			return (set_errno(EOVERFLOW));
3247c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&t32, &t);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3277c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	return (0);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate int
3347c478bd9Sstevel@tonic-gate clock_getres(clockid_t clock, timespec_t *tp)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	timespec_t t;
3377c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
3387c478bd9Sstevel@tonic-gate 	int error;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * Strangely, the standard defines clock_getres() with a NULL tp
3427c478bd9Sstevel@tonic-gate 	 * to do nothing (regardless of the validity of the specified
3437c478bd9Sstevel@tonic-gate 	 * clock_id).  Go figure.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	if (tp == NULL)
3467c478bd9Sstevel@tonic-gate 		return (0);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3497c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_getres(&t);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (error)
3547c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3577c478bd9Sstevel@tonic-gate 		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3587c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3597c478bd9Sstevel@tonic-gate 	} else {
3607c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		if (TIMESPEC_OVERFLOW(&t))
3637c478bd9Sstevel@tonic-gate 			return (set_errno(EOVERFLOW));
3647c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&t32, &t);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3677c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	return (0);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate void
3747c478bd9Sstevel@tonic-gate timer_signal(sigqueue_t *sigq)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	itimer_t *it = (itimer_t *)sigq->sq_backptr;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * There are some conditions during a fork or an exit when we can
3807c478bd9Sstevel@tonic-gate 	 * call siginfofree() without p_lock held.  To prevent a race
3817c478bd9Sstevel@tonic-gate 	 * between timer_signal() and timer_fire() with regard to it_pending,
3827c478bd9Sstevel@tonic-gate 	 * we therefore acquire it_mutex in both paths.
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
3857c478bd9Sstevel@tonic-gate 	ASSERT(it->it_pending > 0);
3867c478bd9Sstevel@tonic-gate 	it->it_overrun = it->it_pending - 1;
3877c478bd9Sstevel@tonic-gate 	it->it_pending = 0;
3887c478bd9Sstevel@tonic-gate 	mutex_exit(&it->it_mutex);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * This routine is called from the clock backend.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate void
3957c478bd9Sstevel@tonic-gate timer_fire(itimer_t *it)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	proc_t *p;
3987c478bd9Sstevel@tonic-gate 	int proc_lock_held;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (it->it_flags & IT_SIGNAL) {
4017c478bd9Sstevel@tonic-gate 		/*
4027c478bd9Sstevel@tonic-gate 		 * See the comment in timer_signal() for why it is not
4037c478bd9Sstevel@tonic-gate 		 * sufficient to only grab p_lock here. Because p_lock can be
4047c478bd9Sstevel@tonic-gate 		 * held on entry to timer_signal(), the lock ordering is
4057c478bd9Sstevel@tonic-gate 		 * necessarily p_lock before it_mutex.
4067c478bd9Sstevel@tonic-gate 		 */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		p = it->it_proc;
4097c478bd9Sstevel@tonic-gate 		proc_lock_held = 1;
4107c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4117c478bd9Sstevel@tonic-gate 	} else {
4127c478bd9Sstevel@tonic-gate 		/*
4137c478bd9Sstevel@tonic-gate 		 * IT_PORT:
4147c478bd9Sstevel@tonic-gate 		 * If a timer was ever programmed to send events to a port,
4157c478bd9Sstevel@tonic-gate 		 * the IT_PORT flag will remain set until:
4167c478bd9Sstevel@tonic-gate 		 * a) the timer is deleted (see timer_delete_locked()) or
4177c478bd9Sstevel@tonic-gate 		 * b) the port is being closed (see timer_close_port()).
4187c478bd9Sstevel@tonic-gate 		 * Both cases are synchronized with the it_mutex.
4197c478bd9Sstevel@tonic-gate 		 * We don't need to use the p_lock because it is only
4207c478bd9Sstevel@tonic-gate 		 * required in the IT_SIGNAL case.
4217c478bd9Sstevel@tonic-gate 		 * If IT_PORT was set and the port is being closed then
4227c478bd9Sstevel@tonic-gate 		 * the timer notification is set to NONE. In such a case
4237c478bd9Sstevel@tonic-gate 		 * the timer itself and the it_pending counter remain active
4247c478bd9Sstevel@tonic-gate 		 * until the application deletes the counter or the process
4257c478bd9Sstevel@tonic-gate 		 * exits.
4267c478bd9Sstevel@tonic-gate 		 */
4277c478bd9Sstevel@tonic-gate 		proc_lock_held = 0;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (it->it_pending > 0) {
4327c478bd9Sstevel@tonic-gate 		if (it->it_pending < INT_MAX)
4337c478bd9Sstevel@tonic-gate 			it->it_pending++;
4347c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
4357c478bd9Sstevel@tonic-gate 	} else {
4367c478bd9Sstevel@tonic-gate 		if (it->it_flags & IT_PORT) {
4377c478bd9Sstevel@tonic-gate 			it->it_pending = 1;
438*34709573Sraf 			port_send_event((port_kevent_t *)it->it_portev);
4397c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4407c478bd9Sstevel@tonic-gate 		} else if (it->it_flags & IT_SIGNAL) {
4417c478bd9Sstevel@tonic-gate 			it->it_pending = 1;
4427c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4437c478bd9Sstevel@tonic-gate 			sigaddqa(p, NULL, it->it_sigq);
4447c478bd9Sstevel@tonic-gate 		} else {
4457c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (proc_lock_held)
4507c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate int
4547c478bd9Sstevel@tonic-gate timer_create(clockid_t clock, struct sigevent *evp, timer_t *tid)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	struct sigevent ev;
4577c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
4587c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
4597c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
4607c478bd9Sstevel@tonic-gate 	sigqueue_t *sigq;
4617c478bd9Sstevel@tonic-gate 	cred_t *cr = CRED();
4627c478bd9Sstevel@tonic-gate 	int error = 0;
4637c478bd9Sstevel@tonic-gate 	timer_t i;
4647c478bd9Sstevel@tonic-gate 	port_notify_t tim_pnevp;
4657c478bd9Sstevel@tonic-gate 	port_kevent_t *pkevp = NULL;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
4687c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (evp != NULL) {
4717c478bd9Sstevel@tonic-gate 		/*
4727c478bd9Sstevel@tonic-gate 		 * short copyin() for binary compatibility
4737c478bd9Sstevel@tonic-gate 		 * fetch oldsigevent to determine how much to copy in.
4747c478bd9Sstevel@tonic-gate 		 */
4757c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
4767c478bd9Sstevel@tonic-gate 			if (copyin(evp, &ev, sizeof (struct oldsigevent)))
4777c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
4787c478bd9Sstevel@tonic-gate 
479*34709573Sraf 			if (ev.sigev_notify == SIGEV_PORT ||
480*34709573Sraf 			    ev.sigev_notify == SIGEV_THREAD) {
4817c478bd9Sstevel@tonic-gate 				if (copyin(ev.sigev_value.sival_ptr, &tim_pnevp,
4827c478bd9Sstevel@tonic-gate 				    sizeof (port_notify_t)))
4837c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
4867c478bd9Sstevel@tonic-gate 		} else {
4877c478bd9Sstevel@tonic-gate 			struct sigevent32 ev32;
4887c478bd9Sstevel@tonic-gate 			port_notify32_t tim_pnevp32;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 			if (copyin(evp, &ev32, sizeof (struct oldsigevent32)))
4917c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
4927c478bd9Sstevel@tonic-gate 			ev.sigev_notify = ev32.sigev_notify;
4937c478bd9Sstevel@tonic-gate 			ev.sigev_signo = ev32.sigev_signo;
4947c478bd9Sstevel@tonic-gate 			/*
4957c478bd9Sstevel@tonic-gate 			 * See comment in sigqueue32() on handling of 32-bit
4967c478bd9Sstevel@tonic-gate 			 * sigvals in a 64-bit kernel.
4977c478bd9Sstevel@tonic-gate 			 */
4987c478bd9Sstevel@tonic-gate 			ev.sigev_value.sival_int = ev32.sigev_value.sival_int;
499*34709573Sraf 			if (ev.sigev_notify == SIGEV_PORT ||
500*34709573Sraf 			    ev.sigev_notify == SIGEV_THREAD) {
5017c478bd9Sstevel@tonic-gate 				if (copyin((void *)(uintptr_t)
5027c478bd9Sstevel@tonic-gate 				    ev32.sigev_value.sival_ptr,
5037c478bd9Sstevel@tonic-gate 				    (void *)&tim_pnevp32,
5047c478bd9Sstevel@tonic-gate 				    sizeof (port_notify32_t)))
5057c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
5067c478bd9Sstevel@tonic-gate 				tim_pnevp.portnfy_port =
5077c478bd9Sstevel@tonic-gate 				    tim_pnevp32.portnfy_port;
5087c478bd9Sstevel@tonic-gate 				tim_pnevp.portnfy_user =
5097c478bd9Sstevel@tonic-gate 				    (void *)(uintptr_t)tim_pnevp32.portnfy_user;
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate #endif
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		switch (ev.sigev_notify) {
5147c478bd9Sstevel@tonic-gate 		case SIGEV_NONE:
5157c478bd9Sstevel@tonic-gate 			break;
5167c478bd9Sstevel@tonic-gate 		case SIGEV_SIGNAL:
5177c478bd9Sstevel@tonic-gate 			if (ev.sigev_signo < 1 || ev.sigev_signo >= NSIG)
5187c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
5197c478bd9Sstevel@tonic-gate 			break;
520*34709573Sraf 		case SIGEV_THREAD:
5217c478bd9Sstevel@tonic-gate 		case SIGEV_PORT:
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		default:
5247c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 	} else {
5277c478bd9Sstevel@tonic-gate 		/*
5287c478bd9Sstevel@tonic-gate 		 * Use the clock's default sigevent (this is a structure copy).
5297c478bd9Sstevel@tonic-gate 		 */
5307c478bd9Sstevel@tonic-gate 		ev = backend->clk_default;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * We'll allocate our timer and sigqueue now, before we grab p_lock.
5357c478bd9Sstevel@tonic-gate 	 * If we can't find an empty slot, we'll free them before returning.
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	it = kmem_cache_alloc(clock_timer_cache, KM_SLEEP);
5387c478bd9Sstevel@tonic-gate 	bzero(it, sizeof (itimer_t));
5397c478bd9Sstevel@tonic-gate 	mutex_init(&it->it_mutex, NULL, MUTEX_DEFAULT, NULL);
5407c478bd9Sstevel@tonic-gate 	sigq = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * If this is this process' first timer, we need to attempt to allocate
5467c478bd9Sstevel@tonic-gate 	 * an array of timerstr_t pointers.  We drop p_lock to perform the
5477c478bd9Sstevel@tonic-gate 	 * allocation; if we return to discover that p_itimer is non-NULL,
5487c478bd9Sstevel@tonic-gate 	 * we will free our allocation and drive on.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL) {
5517c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5527c478bd9Sstevel@tonic-gate 		itp = kmem_zalloc(timer_max * sizeof (itimer_t *), KM_SLEEP);
5537c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (p->p_itimer == NULL)
5567c478bd9Sstevel@tonic-gate 			p->p_itimer = itp;
5577c478bd9Sstevel@tonic-gate 		else {
5587c478bd9Sstevel@tonic-gate 			kmem_free(itp, timer_max * sizeof (itimer_t *));
5597c478bd9Sstevel@tonic-gate 			itp = p->p_itimer;
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max && itp[i] != NULL; i++)
5647c478bd9Sstevel@tonic-gate 		continue;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (i == timer_max) {
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * We couldn't find a slot.  Drop p_lock, free the preallocated
5697c478bd9Sstevel@tonic-gate 		 * timer and sigqueue, and return an error.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5727c478bd9Sstevel@tonic-gate 		kmem_cache_free(clock_timer_cache, it);
5737c478bd9Sstevel@tonic-gate 		kmem_free(sigq, sizeof (sigqueue_t));
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	ASSERT(i < timer_max && itp[i] == NULL);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * If we develop other notification mechanisms, this will need
5827c478bd9Sstevel@tonic-gate 	 * to call into (yet another) backend.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_signo = ev.sigev_signo;
585*34709573Sraf 	if (evp == NULL)
586*34709573Sraf 		sigq->sq_info.si_value.sival_int = i;
587*34709573Sraf 	else
5887c478bd9Sstevel@tonic-gate 		sigq->sq_info.si_value = ev.sigev_value;
5897c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_code = SI_TIMER;
5907c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_pid = p->p_pid;
5917c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_ctid = PRCTID(p);
5927c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_zoneid = getzoneid();
5937c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_uid = crgetruid(cr);
5947c478bd9Sstevel@tonic-gate 	sigq->sq_func = timer_signal;
5957c478bd9Sstevel@tonic-gate 	sigq->sq_next = NULL;
5967c478bd9Sstevel@tonic-gate 	sigq->sq_backptr = it;
5977c478bd9Sstevel@tonic-gate 	it->it_sigq = sigq;
5987c478bd9Sstevel@tonic-gate 	it->it_backend = backend;
5997c478bd9Sstevel@tonic-gate 	it->it_lock = ITLK_LOCKED;
6007c478bd9Sstevel@tonic-gate 	itp[i] = it;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 
603*34709573Sraf 	if (ev.sigev_notify == SIGEV_THREAD ||
604*34709573Sraf 	    ev.sigev_notify == SIGEV_PORT) {
6057c478bd9Sstevel@tonic-gate 		int port;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		/*
6087c478bd9Sstevel@tonic-gate 		 * This timer is programmed to use event port notification when
6097c478bd9Sstevel@tonic-gate 		 * the timer fires:
6107c478bd9Sstevel@tonic-gate 		 * - allocate a port event structure and prepare it to be sent
6117c478bd9Sstevel@tonic-gate 		 *   to the port as soon as the timer fires.
6127c478bd9Sstevel@tonic-gate 		 * - when the timer fires :
6137c478bd9Sstevel@tonic-gate 		 *   - if event structure was already sent to the port then this
6147c478bd9Sstevel@tonic-gate 		 *	is a timer fire overflow => increment overflow counter.
6157c478bd9Sstevel@tonic-gate 		 *   - otherwise send pre-allocated event structure to the port.
6167c478bd9Sstevel@tonic-gate 		 * - the events field of the port_event_t structure counts the
6177c478bd9Sstevel@tonic-gate 		 *   number of timer fired events.
6187c478bd9Sstevel@tonic-gate 		 * - The event structured is allocated using the
6197c478bd9Sstevel@tonic-gate 		 *   PORT_ALLOC_CACHED flag.
6207c478bd9Sstevel@tonic-gate 		 *   This flag indicates that the timer itself will manage and
6217c478bd9Sstevel@tonic-gate 		 *   free the event structure when required.
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		it->it_flags |= IT_PORT;
6257c478bd9Sstevel@tonic-gate 		port = tim_pnevp.portnfy_port;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		/* associate timer as event source with the port */
6287c478bd9Sstevel@tonic-gate 		error = port_associate_ksource(port, PORT_SOURCE_TIMER,
6297c478bd9Sstevel@tonic-gate 		    (port_source_t **)&it->it_portsrc, timer_close_port,
6307c478bd9Sstevel@tonic-gate 		    (void *)it, NULL);
6317c478bd9Sstevel@tonic-gate 		if (error) {
6327c478bd9Sstevel@tonic-gate 			itp[i] = NULL;		/* clear slot */
6337c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
6347c478bd9Sstevel@tonic-gate 			kmem_cache_free(clock_timer_cache, it);
6357c478bd9Sstevel@tonic-gate 			kmem_free(sigq, sizeof (sigqueue_t));
6367c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		/* allocate an event structure/slot */
6407c478bd9Sstevel@tonic-gate 		error = port_alloc_event(port, PORT_ALLOC_SCACHED,
6417c478bd9Sstevel@tonic-gate 		    PORT_SOURCE_TIMER, &pkevp);
6427c478bd9Sstevel@tonic-gate 		if (error) {
6437c478bd9Sstevel@tonic-gate 			(void) port_dissociate_ksource(port, PORT_SOURCE_TIMER,
6447c478bd9Sstevel@tonic-gate 			    (port_source_t *)it->it_portsrc);
6457c478bd9Sstevel@tonic-gate 			itp[i] = NULL;		/* clear slot */
6467c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
6477c478bd9Sstevel@tonic-gate 			kmem_cache_free(clock_timer_cache, it);
6487c478bd9Sstevel@tonic-gate 			kmem_free(sigq, sizeof (sigqueue_t));
6497c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		/* initialize event data */
6537c478bd9Sstevel@tonic-gate 		port_init_event(pkevp, i, tim_pnevp.portnfy_user,
6547c478bd9Sstevel@tonic-gate 		    timer_port_callback, it);
6557c478bd9Sstevel@tonic-gate 		it->it_portev = pkevp;
6567c478bd9Sstevel@tonic-gate 		it->it_portfd = port;
6577c478bd9Sstevel@tonic-gate 	} else {
6587c478bd9Sstevel@tonic-gate 		if (ev.sigev_notify == SIGEV_SIGNAL)
6597c478bd9Sstevel@tonic-gate 			it->it_flags |= IT_SIGNAL;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * Call on the backend to verify the event argument (or return
6667c478bd9Sstevel@tonic-gate 	 * EINVAL if this clock type does not support timers).
6677c478bd9Sstevel@tonic-gate 	 */
6687c478bd9Sstevel@tonic-gate 	if ((error = backend->clk_timer_create(it, &ev)) != 0)
6697c478bd9Sstevel@tonic-gate 		goto err;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	it->it_lwp = ttolwp(curthread);
6727c478bd9Sstevel@tonic-gate 	it->it_proc = p;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (copyout(&i, tid, sizeof (timer_t)) != 0) {
6757c478bd9Sstevel@tonic-gate 		error = EFAULT;
6767c478bd9Sstevel@tonic-gate 		goto err;
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * If we're here, then we have successfully created the timer; we
6817c478bd9Sstevel@tonic-gate 	 * just need to release the timer and return.
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	timer_release(p, it);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return (0);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate err:
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * If we're here, an error has occurred late in the timer creation
6907c478bd9Sstevel@tonic-gate 	 * process.  We need to regrab p_lock, and delete the incipient timer.
6917c478bd9Sstevel@tonic-gate 	 * Since we never unlocked the timer (it was born locked), it's
6927c478bd9Sstevel@tonic-gate 	 * impossible for a removal to be pending.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	ASSERT(!(it->it_lock & ITLK_REMOVE));
6957c478bd9Sstevel@tonic-gate 	timer_delete_grabbed(p, i, it);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	return (set_errno(error));
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate int
7017c478bd9Sstevel@tonic-gate timer_gettime(timer_t tid, itimerspec_t *val)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
7047c478bd9Sstevel@tonic-gate 	itimer_t *it;
7057c478bd9Sstevel@tonic-gate 	itimerspec_t when;
7067c478bd9Sstevel@tonic-gate 	int error;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
7097c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	error = it->it_backend->clk_timer_gettime(it, &when);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	timer_release(p, it);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (error == 0) {
7167c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
7177c478bd9Sstevel@tonic-gate 			if (copyout(&when, val, sizeof (itimerspec_t)))
7187c478bd9Sstevel@tonic-gate 				error = EFAULT;
7197c478bd9Sstevel@tonic-gate 		} else {
7207c478bd9Sstevel@tonic-gate 			if (ITIMERSPEC_OVERFLOW(&when))
7217c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
7227c478bd9Sstevel@tonic-gate 			else {
7237c478bd9Sstevel@tonic-gate 				itimerspec32_t w32;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 				ITIMERSPEC_TO_ITIMERSPEC32(&w32, &when)
7267c478bd9Sstevel@tonic-gate 				if (copyout(&w32, val, sizeof (itimerspec32_t)))
7277c478bd9Sstevel@tonic-gate 					error = EFAULT;
7287c478bd9Sstevel@tonic-gate 			}
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate int
7367c478bd9Sstevel@tonic-gate timer_settime(timer_t tid, int flags, itimerspec_t *val, itimerspec_t *oval)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	itimerspec_t when;
7397c478bd9Sstevel@tonic-gate 	timespec_t res;
7407c478bd9Sstevel@tonic-gate 	itimer_t *it;
7417c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
7427c478bd9Sstevel@tonic-gate 	int error;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (oval != NULL) {
7457c478bd9Sstevel@tonic-gate 		if ((error = timer_gettime(tid, oval)) != 0)
7467c478bd9Sstevel@tonic-gate 			return (error);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
7507c478bd9Sstevel@tonic-gate 		if (copyin(val, &when, sizeof (itimerspec_t)))
7517c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7527c478bd9Sstevel@tonic-gate 	} else {
7537c478bd9Sstevel@tonic-gate 		itimerspec32_t w32;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		if (copyin(val, &w32, sizeof (itimerspec32_t)))
7567c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		ITIMERSPEC32_TO_ITIMERSPEC(&when, &w32);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&when.it_value) ||
7627c478bd9Sstevel@tonic-gate 	    (itimerspecfix(&when.it_interval) &&
7637c478bd9Sstevel@tonic-gate 	    timerspecisset(&when.it_value))) {
7647c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
7687c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * From the man page:
7727c478bd9Sstevel@tonic-gate 	 *	Time values that are between two consecutive non-negative
7737c478bd9Sstevel@tonic-gate 	 *	integer multiples of the resolution of the specified timer
7747c478bd9Sstevel@tonic-gate 	 *	shall be rounded up to the larger multiple of the resolution.
7757c478bd9Sstevel@tonic-gate 	 * We assume that the resolution of any clock is less than one second.
7767c478bd9Sstevel@tonic-gate 	 */
7777c478bd9Sstevel@tonic-gate 	if (it->it_backend->clk_clock_getres(&res) == 0 && res.tv_nsec > 1) {
7787c478bd9Sstevel@tonic-gate 		long rem;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		if ((rem = when.it_interval.tv_nsec % res.tv_nsec) != 0) {
7817c478bd9Sstevel@tonic-gate 			when.it_interval.tv_nsec += res.tv_nsec - rem;
7827c478bd9Sstevel@tonic-gate 			timespecfix(&when.it_interval);
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 		if ((rem = when.it_value.tv_nsec % res.tv_nsec) != 0) {
7857c478bd9Sstevel@tonic-gate 			when.it_value.tv_nsec += res.tv_nsec - rem;
7867c478bd9Sstevel@tonic-gate 			timespecfix(&when.it_value);
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	error = it->it_backend->clk_timer_settime(it, flags, &when);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	timer_release(p, it);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate int
7977c478bd9Sstevel@tonic-gate timer_delete(timer_t tid)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8007c478bd9Sstevel@tonic-gate 	itimer_t *it;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8037c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	timer_delete_grabbed(p, tid, it);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	return (0);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate int
8117c478bd9Sstevel@tonic-gate timer_getoverrun(timer_t tid)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	int overrun;
8147c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8157c478bd9Sstevel@tonic-gate 	itimer_t *it;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8187c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * The it_overrun field is protected by p_lock; we need to acquire
8227c478bd9Sstevel@tonic-gate 	 * it before looking at the value.
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
8257c478bd9Sstevel@tonic-gate 	overrun = it->it_overrun;
8267c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	timer_release(p, it);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	return (overrun);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * Entered/exited with p_lock held, but will repeatedly drop and regrab p_lock.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate void
8377c478bd9Sstevel@tonic-gate timer_lwpexit(void)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	timer_t i;
8407c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8417c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8427c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL)
8477c478bd9Sstevel@tonic-gate 		return;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max; i++) {
8507c478bd9Sstevel@tonic-gate 		if ((it = itp[i]) == NULL)
8517c478bd9Sstevel@tonic-gate 			continue;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		if ((it->it_lock & ITLK_REMOVE) || it->it_lwp != lwp) {
8567c478bd9Sstevel@tonic-gate 			/*
8577c478bd9Sstevel@tonic-gate 			 * This timer is either being removed or it isn't
8587c478bd9Sstevel@tonic-gate 			 * associated with this lwp.
8597c478bd9Sstevel@tonic-gate 			 */
8607c478bd9Sstevel@tonic-gate 			timer_unlock(p, it);
8617c478bd9Sstevel@tonic-gate 			continue;
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * The LWP that created this timer is going away.  To the user,
8667c478bd9Sstevel@tonic-gate 		 * our behavior here is explicitly undefined.  We will simply
8677c478bd9Sstevel@tonic-gate 		 * null out the it_lwp field; if the LWP was bound to a CPU,
8687c478bd9Sstevel@tonic-gate 		 * the cyclic will stay bound to that CPU until the process
8697c478bd9Sstevel@tonic-gate 		 * exits.
8707c478bd9Sstevel@tonic-gate 		 */
8717c478bd9Sstevel@tonic-gate 		it->it_lwp = NULL;
8727c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Called to notify of an LWP binding change.  Entered/exited with p_lock
8787c478bd9Sstevel@tonic-gate  * held, but will repeatedly drop and regrab p_lock.
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate void
8817c478bd9Sstevel@tonic-gate timer_lwpbind()
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	timer_t i;
8847c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8857c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8867c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL)
8917c478bd9Sstevel@tonic-gate 		return;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max; i++) {
8947c478bd9Sstevel@tonic-gate 		if ((it = itp[i]) == NULL)
8957c478bd9Sstevel@tonic-gate 			continue;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		if (!(it->it_lock & ITLK_REMOVE) && it->it_lwp == lwp) {
9007c478bd9Sstevel@tonic-gate 			/*
9017c478bd9Sstevel@tonic-gate 			 * Drop p_lock and jump into the backend.
9027c478bd9Sstevel@tonic-gate 			 */
9037c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
9047c478bd9Sstevel@tonic-gate 			it->it_backend->clk_timer_lwpbind(it);
9057c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9067c478bd9Sstevel@tonic-gate 		}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate  * This function should only be called if p_itimer is non-NULL.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate void
9167c478bd9Sstevel@tonic-gate timer_exit(void)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	timer_t i;
9197c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	ASSERT(p->p_itimer != NULL);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	for (i = 0; i < timer_max; i++)
9247c478bd9Sstevel@tonic-gate 		(void) timer_delete(i);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	kmem_free(p->p_itimer, timer_max * sizeof (itimer_t *));
9277c478bd9Sstevel@tonic-gate 	p->p_itimer = NULL;
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * timer_port_callback() is a callback function which is associated with the
9327c478bd9Sstevel@tonic-gate  * timer event and is activated just before the event is delivered to the user.
9337c478bd9Sstevel@tonic-gate  * The timer uses this function to update/set the overflow counter and
9347c478bd9Sstevel@tonic-gate  * to reenable the use of the event structure.
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /* ARGSUSED */
9387c478bd9Sstevel@tonic-gate static int
9397c478bd9Sstevel@tonic-gate timer_port_callback(void *arg, int *events, pid_t pid, int flag, void *evp)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	itimer_t	*it = arg;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
9447c478bd9Sstevel@tonic-gate 	if (curproc != it->it_proc) {
9457c478bd9Sstevel@tonic-gate 		/* can not deliver timer events to another proc */
9467c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
9477c478bd9Sstevel@tonic-gate 		return (EACCES);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	*events = it->it_pending;	/* 1 = 1 event, >1 # of overflows */
9507c478bd9Sstevel@tonic-gate 	it->it_pending = 0;		/* reinit overflow counter	*/
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * This function can also be activated when the port is being closed
9537c478bd9Sstevel@tonic-gate 	 * and a timer event is already submitted to the port.
9547c478bd9Sstevel@tonic-gate 	 * In such a case the event port framework will use the
9557c478bd9Sstevel@tonic-gate 	 * close-callback function to notify the events sources.
9567c478bd9Sstevel@tonic-gate 	 * The timer close-callback function is timer_close_port() which
9577c478bd9Sstevel@tonic-gate 	 * will free all allocated resources (including the allocated
9587c478bd9Sstevel@tonic-gate 	 * port event structure).
9597c478bd9Sstevel@tonic-gate 	 * For that reason we don't need to check the value of flag here.
9607c478bd9Sstevel@tonic-gate 	 */
9617c478bd9Sstevel@tonic-gate 	mutex_exit(&it->it_mutex);
9627c478bd9Sstevel@tonic-gate 	return (0);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * port is being closed ... free all allocated port event structures
9677c478bd9Sstevel@tonic-gate  * The delivered arg currently correspond to the first timer associated with
9687c478bd9Sstevel@tonic-gate  * the port and it is not useable in this case.
9697c478bd9Sstevel@tonic-gate  * We have to scan the list of activated timers in the current proc and
9707c478bd9Sstevel@tonic-gate  * compare them with the delivered port id.
9717c478bd9Sstevel@tonic-gate  */
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate /* ARGSUSED */
9747c478bd9Sstevel@tonic-gate static void
9757c478bd9Sstevel@tonic-gate timer_close_port(void *arg, int port, pid_t pid, int lastclose)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	proc_t		*p = curproc;
9787c478bd9Sstevel@tonic-gate 	timer_t		tid;
9797c478bd9Sstevel@tonic-gate 	itimer_t	*it;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	for (tid = 0; tid < timer_max; tid++) {
9827c478bd9Sstevel@tonic-gate 		if ((it = timer_grab(p, tid)) == NULL)
9837c478bd9Sstevel@tonic-gate 			continue;
9847c478bd9Sstevel@tonic-gate 		if (it->it_portev) {
9857c478bd9Sstevel@tonic-gate 			mutex_enter(&it->it_mutex);
9867c478bd9Sstevel@tonic-gate 			if (it->it_portfd == port) {
9877c478bd9Sstevel@tonic-gate 				port_free_event((port_kevent_t *)it->it_portev);
9887c478bd9Sstevel@tonic-gate 				it->it_portev = NULL;
9897c478bd9Sstevel@tonic-gate 				it->it_flags &= ~IT_PORT;
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 		timer_release(p, it);
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate }
996