xref: /illumos-gate/usr/src/uts/common/os/timer.c (revision bbf21555)
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 /*
238b79e857Spraks  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
2713a42676SBryan Cantrill /*
28440a8a36SPatrick Mooney  * Copyright 2020 Joyent, Inc.
2913a42676SBryan Cantrill  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/timer.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
33440a8a36SPatrick Mooney #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/debug.h>
377c478bd9Sstevel@tonic-gate #include <sys/policy.h>
388b79e857Spraks #include <sys/port_impl.h>
397c478bd9Sstevel@tonic-gate #include <sys/port_kernel.h>
407c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static kmem_cache_t *clock_timer_cache;
437c478bd9Sstevel@tonic-gate static clock_backend_t *clock_backend[CLOCK_MAX];
447c478bd9Sstevel@tonic-gate static int timer_port_callback(void *, int *, pid_t, int, void *);
457c478bd9Sstevel@tonic-gate static void timer_close_port(void *, int, pid_t, int);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	CLOCK_BACKEND(clk) \
487c478bd9Sstevel@tonic-gate 	((clk) < CLOCK_MAX && (clk) >= 0 ? clock_backend[(clk)] : NULL)
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Tunable to increase the maximum number of POSIX timers per-process.  This
527c478bd9Sstevel@tonic-gate  * may _only_ be tuned in /etc/system or by patching the kernel binary; it
537c478bd9Sstevel@tonic-gate  * _cannot_ be tuned on a running system.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate int timer_max = _TIMER_MAX;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * timer_lock() locks the specified interval timer.  It doesn't look at the
597c478bd9Sstevel@tonic-gate  * ITLK_REMOVE bit; it's up to callers to look at this if they need to
607c478bd9Sstevel@tonic-gate  * care.  p_lock must be held on entry; it may be dropped and reaquired,
617c478bd9Sstevel@tonic-gate  * but timer_lock() will always return with p_lock held.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * Note that timer_create() doesn't call timer_lock(); it creates timers
647c478bd9Sstevel@tonic-gate  * with the ITLK_LOCKED bit explictly set.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static void
timer_lock(proc_t * p,itimer_t * it)677c478bd9Sstevel@tonic-gate timer_lock(proc_t *p, itimer_t *it)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	while (it->it_lock & ITLK_LOCKED) {
727c478bd9Sstevel@tonic-gate 		it->it_blockers++;
737c478bd9Sstevel@tonic-gate 		cv_wait(&it->it_cv, &p->p_lock);
747c478bd9Sstevel@tonic-gate 		it->it_blockers--;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	it->it_lock |= ITLK_LOCKED;
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * timer_unlock() unlocks the specified interval timer, waking up any
827c478bd9Sstevel@tonic-gate  * waiters.  p_lock must be held on entry; it will not be dropped by
837c478bd9Sstevel@tonic-gate  * timer_unlock().
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static void
timer_unlock(proc_t * p,itimer_t * it)867c478bd9Sstevel@tonic-gate timer_unlock(proc_t *p, itimer_t *it)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
897c478bd9Sstevel@tonic-gate 	ASSERT(it->it_lock & ITLK_LOCKED);
907c478bd9Sstevel@tonic-gate 	it->it_lock &= ~ITLK_LOCKED;
917c478bd9Sstevel@tonic-gate 	cv_signal(&it->it_cv);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * timer_delete_locked() takes a proc pointer, timer ID and locked interval
967c478bd9Sstevel@tonic-gate  * timer, and deletes the specified timer.  It must be called with p_lock
977c478bd9Sstevel@tonic-gate  * held, and cannot be called on a timer which already has ITLK_REMOVE set;
987c478bd9Sstevel@tonic-gate  * the caller must check this.  timer_delete_locked() will set the ITLK_REMOVE
997c478bd9Sstevel@tonic-gate  * bit and will iteratively unlock and lock the interval timer until all
1007c478bd9Sstevel@tonic-gate  * blockers have seen the ITLK_REMOVE and cleared out.  It will then zero
1017c478bd9Sstevel@tonic-gate  * out the specified entry in the p_itimer array, and call into the clock
1027c478bd9Sstevel@tonic-gate  * backend to complete the deletion.
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  * This function will always return with p_lock held.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate static void
timer_delete_locked(proc_t * p,timer_t tid,itimer_t * it)1077c478bd9Sstevel@tonic-gate timer_delete_locked(proc_t *p, timer_t tid, itimer_t *it)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1107c478bd9Sstevel@tonic-gate 	ASSERT(!(it->it_lock & ITLK_REMOVE));
1117c478bd9Sstevel@tonic-gate 	ASSERT(it->it_lock & ITLK_LOCKED);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	it->it_lock |= ITLK_REMOVE;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * If there are threads waiting to lock this timer, we'll unlock
1177c478bd9Sstevel@tonic-gate 	 * the timer, and block on the cv.  Threads blocking our removal will
1187c478bd9Sstevel@tonic-gate 	 * have the opportunity to run; when they see the ITLK_REMOVE flag
1197c478bd9Sstevel@tonic-gate 	 * set, they will immediately unlock the timer.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	while (it->it_blockers) {
1227c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
1237c478bd9Sstevel@tonic-gate 		cv_wait(&it->it_cv, &p->p_lock);
1247c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
127440a8a36SPatrick Mooney 	ASSERT(p->p_itimer_sz > tid);
1287c478bd9Sstevel@tonic-gate 	ASSERT(p->p_itimer[tid] == it);
1297c478bd9Sstevel@tonic-gate 	p->p_itimer[tid] = NULL;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * No one is blocked on this timer, and no one will be (we've set
1337c478bd9Sstevel@tonic-gate 	 * p_itimer[tid] to be NULL; no one can find it).  Now we call into
1347c478bd9Sstevel@tonic-gate 	 * the clock backend to delete the timer; it is up to the backend to
1357c478bd9Sstevel@tonic-gate 	 * guarantee that timer_fire() has completed (and will never again
1367c478bd9Sstevel@tonic-gate 	 * be called) for this timer.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	it->it_backend->clk_timer_delete(it);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (it->it_portev) {
1437c478bd9Sstevel@tonic-gate 		mutex_enter(&it->it_mutex);
1447c478bd9Sstevel@tonic-gate 		if (it->it_portev) {
1458b79e857Spraks 			port_kevent_t	*pev;
1467c478bd9Sstevel@tonic-gate 			/* dissociate timer from the event port */
1477c478bd9Sstevel@tonic-gate 			(void) port_dissociate_ksource(it->it_portfd,
1487c478bd9Sstevel@tonic-gate 			    PORT_SOURCE_TIMER, (port_source_t *)it->it_portsrc);
1498b79e857Spraks 			pev = (port_kevent_t *)it->it_portev;
1507c478bd9Sstevel@tonic-gate 			it->it_portev = NULL;
1517c478bd9Sstevel@tonic-gate 			it->it_flags &= ~IT_PORT;
1527c478bd9Sstevel@tonic-gate 			it->it_pending = 0;
1538b79e857Spraks 			mutex_exit(&it->it_mutex);
1548b79e857Spraks 			(void) port_remove_done_event(pev);
1558b79e857Spraks 			port_free_event(pev);
1568b79e857Spraks 		} else {
1578b79e857Spraks 			mutex_exit(&it->it_mutex);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * We need to be careful freeing the sigqueue for this timer;
1657c478bd9Sstevel@tonic-gate 	 * if a signal is pending, the sigqueue needs to be freed
1667c478bd9Sstevel@tonic-gate 	 * synchronously in siginfofree().  The need to free the sigqueue
1677c478bd9Sstevel@tonic-gate 	 * in siginfofree() is indicated by setting sq_func to NULL.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	if (it->it_pending > 0) {
1707c478bd9Sstevel@tonic-gate 		it->it_sigq->sq_func = NULL;
1717c478bd9Sstevel@tonic-gate 	} else {
1727c478bd9Sstevel@tonic-gate 		kmem_free(it->it_sigq, sizeof (sigqueue_t));
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	ASSERT(it->it_blockers == 0);
1767c478bd9Sstevel@tonic-gate 	kmem_cache_free(clock_timer_cache, it);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * timer_grab() and its companion routine, timer_release(), are wrappers
181*bbf21555SRichard Lowe  * around timer_lock()/_unlock() which allow the timer_*(3C) routines to
1827c478bd9Sstevel@tonic-gate  * (a) share error handling code and (b) not grab p_lock themselves.  Routines
1837c478bd9Sstevel@tonic-gate  * which are called with p_lock held (e.g. timer_lwpbind(), timer_lwpexit())
1847c478bd9Sstevel@tonic-gate  * must call timer_lock()/_unlock() explictly.
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * timer_grab() takes a proc and a timer ID, and returns a pointer to a
1877c478bd9Sstevel@tonic-gate  * locked interval timer.  p_lock must _not_ be held on entry; timer_grab()
1887c478bd9Sstevel@tonic-gate  * may acquire p_lock, but will always return with p_lock dropped.
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * If timer_grab() fails, it will return NULL.  timer_grab() will fail if
1917c478bd9Sstevel@tonic-gate  * one or more of the following is true:
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  *  (a)	The specified timer ID is out of range.
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  *  (b)	The specified timer ID does not correspond to a timer ID returned
196*bbf21555SRichard Lowe  *	from timer_create(3C).
1977c478bd9Sstevel@tonic-gate  *
1987c478bd9Sstevel@tonic-gate  *  (c)	The specified timer ID is currently being removed.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate static itimer_t *
timer_grab(proc_t * p,timer_t tid)2027c478bd9Sstevel@tonic-gate timer_grab(proc_t *p, timer_t tid)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	itimer_t **itp, *it;
2057c478bd9Sstevel@tonic-gate 
206440a8a36SPatrick Mooney 	if (tid < 0) {
2077c478bd9Sstevel@tonic-gate 		return (NULL);
208440a8a36SPatrick Mooney 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2117c478bd9Sstevel@tonic-gate 
212440a8a36SPatrick Mooney 	if ((itp = p->p_itimer) == NULL || tid >= p->p_itimer_sz ||
213440a8a36SPatrick Mooney 	    (it = itp[tid]) == NULL) {
2147c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2157c478bd9Sstevel@tonic-gate 		return (NULL);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	timer_lock(p, it);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (it->it_lock & ITLK_REMOVE) {
2217c478bd9Sstevel@tonic-gate 		/*
2227c478bd9Sstevel@tonic-gate 		 * Someone is removing this timer; it will soon be invalid.
2237c478bd9Sstevel@tonic-gate 		 */
2247c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
2257c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2267c478bd9Sstevel@tonic-gate 		return (NULL);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	return (it);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * timer_release() releases a timer acquired with timer_grab().  p_lock
2367c478bd9Sstevel@tonic-gate  * should not be held on entry; timer_release() will acquire p_lock but
2377c478bd9Sstevel@tonic-gate  * will drop it before returning.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate static void
timer_release(proc_t * p,itimer_t * it)2407c478bd9Sstevel@tonic-gate timer_release(proc_t *p, itimer_t *it)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2437c478bd9Sstevel@tonic-gate 	timer_unlock(p, it);
2447c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * timer_delete_grabbed() deletes a timer acquired with timer_grab().
2497c478bd9Sstevel@tonic-gate  * p_lock should not be held on entry; timer_delete_grabbed() will acquire
2507c478bd9Sstevel@tonic-gate  * p_lock, but will drop it before returning.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate static void
timer_delete_grabbed(proc_t * p,timer_t tid,itimer_t * it)2537c478bd9Sstevel@tonic-gate timer_delete_grabbed(proc_t *p, timer_t tid, itimer_t *it)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2567c478bd9Sstevel@tonic-gate 	timer_delete_locked(p, tid, it);
2577c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate void
clock_timer_init()2617c478bd9Sstevel@tonic-gate clock_timer_init()
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	clock_timer_cache = kmem_cache_create("timer_cache",
2647c478bd9Sstevel@tonic-gate 	    sizeof (itimer_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
265440a8a36SPatrick Mooney 
266440a8a36SPatrick Mooney 	/*
267440a8a36SPatrick Mooney 	 * Push the timer_max limit up to at least 4 * NCPU.  Due to the way
268440a8a36SPatrick Mooney 	 * NCPU is defined, proper initialization of the timer limit is
269440a8a36SPatrick Mooney 	 * performed at runtime.
270440a8a36SPatrick Mooney 	 */
271440a8a36SPatrick Mooney 	timer_max = MAX(NCPU * 4, timer_max);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate void
clock_add_backend(clockid_t clock,clock_backend_t * backend)2757c478bd9Sstevel@tonic-gate clock_add_backend(clockid_t clock, clock_backend_t *backend)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	ASSERT(clock >= 0 && clock < CLOCK_MAX);
2787c478bd9Sstevel@tonic-gate 	ASSERT(clock_backend[clock] == NULL);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	clock_backend[clock] = backend;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2836a72db4aSBryan Cantrill clock_backend_t *
clock_get_backend(clockid_t clock)2846a72db4aSBryan Cantrill clock_get_backend(clockid_t clock)
2856a72db4aSBryan Cantrill {
2866a72db4aSBryan Cantrill 	if (clock < 0 || clock >= CLOCK_MAX)
2876a72db4aSBryan Cantrill 		return (NULL);
2886a72db4aSBryan Cantrill 
2896a72db4aSBryan Cantrill 	return (clock_backend[clock]);
2906a72db4aSBryan Cantrill }
2916a72db4aSBryan Cantrill 
2927c478bd9Sstevel@tonic-gate int
clock_settime(clockid_t clock,timespec_t * tp)2937c478bd9Sstevel@tonic-gate clock_settime(clockid_t clock, timespec_t *tp)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	timespec_t t;
2967c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
2977c478bd9Sstevel@tonic-gate 	int error;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3007c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (secpolicy_settime(CRED()) != 0)
3037c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3067c478bd9Sstevel@tonic-gate 		if (copyin(tp, &t, sizeof (timespec_t)) != 0)
3077c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		if (copyin(tp, &t32, sizeof (timespec32_t)) != 0)
3127c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&t, &t32);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&t))
3187c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_settime(&t);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (error)
3237c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate int
clock_gettime(clockid_t clock,timespec_t * tp)3297c478bd9Sstevel@tonic-gate clock_gettime(clockid_t clock, timespec_t *tp)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	timespec_t t;
3327c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
3337c478bd9Sstevel@tonic-gate 	int error;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3367c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_gettime(&t);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (error)
3417c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3447c478bd9Sstevel@tonic-gate 		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3457c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3467c478bd9Sstevel@tonic-gate 	} else {
3477c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		if (TIMESPEC_OVERFLOW(&t))
3507c478bd9Sstevel@tonic-gate 			return (set_errno(EOVERFLOW));
3517c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&t32, &t);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3547c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	return (0);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate int
clock_getres(clockid_t clock,timespec_t * tp)3617c478bd9Sstevel@tonic-gate clock_getres(clockid_t clock, timespec_t *tp)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	timespec_t t;
3647c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
3657c478bd9Sstevel@tonic-gate 	int error;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Strangely, the standard defines clock_getres() with a NULL tp
3697c478bd9Sstevel@tonic-gate 	 * to do nothing (regardless of the validity of the specified
3707c478bd9Sstevel@tonic-gate 	 * clock_id).  Go figure.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	if (tp == NULL)
3737c478bd9Sstevel@tonic-gate 		return (0);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
3767c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	error = backend->clk_clock_getres(&t);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (error)
3817c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3847c478bd9Sstevel@tonic-gate 		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3857c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3867c478bd9Sstevel@tonic-gate 	} else {
3877c478bd9Sstevel@tonic-gate 		timespec32_t t32;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		if (TIMESPEC_OVERFLOW(&t))
3907c478bd9Sstevel@tonic-gate 			return (set_errno(EOVERFLOW));
3917c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&t32, &t);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3947c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (0);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate void
timer_signal(sigqueue_t * sigq)4017c478bd9Sstevel@tonic-gate timer_signal(sigqueue_t *sigq)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	itimer_t *it = (itimer_t *)sigq->sq_backptr;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * There are some conditions during a fork or an exit when we can
4077c478bd9Sstevel@tonic-gate 	 * call siginfofree() without p_lock held.  To prevent a race
4087c478bd9Sstevel@tonic-gate 	 * between timer_signal() and timer_fire() with regard to it_pending,
4097c478bd9Sstevel@tonic-gate 	 * we therefore acquire it_mutex in both paths.
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
4127c478bd9Sstevel@tonic-gate 	ASSERT(it->it_pending > 0);
4137c478bd9Sstevel@tonic-gate 	it->it_overrun = it->it_pending - 1;
4147c478bd9Sstevel@tonic-gate 	it->it_pending = 0;
4157c478bd9Sstevel@tonic-gate 	mutex_exit(&it->it_mutex);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * This routine is called from the clock backend.
4207c478bd9Sstevel@tonic-gate  */
4216a72db4aSBryan Cantrill static void
timer_fire(itimer_t * it)4227c478bd9Sstevel@tonic-gate timer_fire(itimer_t *it)
4237c478bd9Sstevel@tonic-gate {
424c6f039c7SToomas Soome 	proc_t *p = NULL;
4257c478bd9Sstevel@tonic-gate 	int proc_lock_held;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (it->it_flags & IT_SIGNAL) {
4287c478bd9Sstevel@tonic-gate 		/*
4297c478bd9Sstevel@tonic-gate 		 * See the comment in timer_signal() for why it is not
4307c478bd9Sstevel@tonic-gate 		 * sufficient to only grab p_lock here. Because p_lock can be
4317c478bd9Sstevel@tonic-gate 		 * held on entry to timer_signal(), the lock ordering is
4327c478bd9Sstevel@tonic-gate 		 * necessarily p_lock before it_mutex.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		p = it->it_proc;
4367c478bd9Sstevel@tonic-gate 		proc_lock_held = 1;
4377c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4387c478bd9Sstevel@tonic-gate 	} else {
4397c478bd9Sstevel@tonic-gate 		/*
4407c478bd9Sstevel@tonic-gate 		 * IT_PORT:
4417c478bd9Sstevel@tonic-gate 		 * If a timer was ever programmed to send events to a port,
4427c478bd9Sstevel@tonic-gate 		 * the IT_PORT flag will remain set until:
4437c478bd9Sstevel@tonic-gate 		 * a) the timer is deleted (see timer_delete_locked()) or
4447c478bd9Sstevel@tonic-gate 		 * b) the port is being closed (see timer_close_port()).
4457c478bd9Sstevel@tonic-gate 		 * Both cases are synchronized with the it_mutex.
4467c478bd9Sstevel@tonic-gate 		 * We don't need to use the p_lock because it is only
4477c478bd9Sstevel@tonic-gate 		 * required in the IT_SIGNAL case.
4487c478bd9Sstevel@tonic-gate 		 * If IT_PORT was set and the port is being closed then
4497c478bd9Sstevel@tonic-gate 		 * the timer notification is set to NONE. In such a case
4507c478bd9Sstevel@tonic-gate 		 * the timer itself and the it_pending counter remain active
4517c478bd9Sstevel@tonic-gate 		 * until the application deletes the counter or the process
4527c478bd9Sstevel@tonic-gate 		 * exits.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		proc_lock_held = 0;
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (it->it_pending > 0) {
4597c478bd9Sstevel@tonic-gate 		if (it->it_pending < INT_MAX)
4607c478bd9Sstevel@tonic-gate 			it->it_pending++;
4617c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
4627c478bd9Sstevel@tonic-gate 	} else {
4637c478bd9Sstevel@tonic-gate 		if (it->it_flags & IT_PORT) {
4647c478bd9Sstevel@tonic-gate 			it->it_pending = 1;
46534709573Sraf 			port_send_event((port_kevent_t *)it->it_portev);
4667c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4677c478bd9Sstevel@tonic-gate 		} else if (it->it_flags & IT_SIGNAL) {
4687c478bd9Sstevel@tonic-gate 			it->it_pending = 1;
4697c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4707c478bd9Sstevel@tonic-gate 			sigaddqa(p, NULL, it->it_sigq);
4717c478bd9Sstevel@tonic-gate 		} else {
4727c478bd9Sstevel@tonic-gate 			mutex_exit(&it->it_mutex);
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (proc_lock_held)
4777c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
480440a8a36SPatrick Mooney /*
481440a8a36SPatrick Mooney  * Find an unused (i.e. NULL) entry in p->p_itimer and set *id to the
482440a8a36SPatrick Mooney  * index of the unused entry, growing p->p_itimer as necessary (up to timer_max
483440a8a36SPatrick Mooney  * entries). Returns B_TRUE (with *id set) on success, B_FALSE on failure
484440a8a36SPatrick Mooney  * (e.g. the process already has the maximum number of allowed timers
485440a8a36SPatrick Mooney  * allocated).
486440a8a36SPatrick Mooney  */
487440a8a36SPatrick Mooney static boolean_t
timer_get_id(proc_t * p,timer_t * id)488440a8a36SPatrick Mooney timer_get_id(proc_t *p, timer_t *id)
489440a8a36SPatrick Mooney {
490440a8a36SPatrick Mooney 	itimer_t **itp = NULL, **itp_new;
491440a8a36SPatrick Mooney 	uint_t target_sz;
492440a8a36SPatrick Mooney 	uint_t i;
493440a8a36SPatrick Mooney 
494440a8a36SPatrick Mooney 	ASSERT(MUTEX_HELD(&p->p_lock));
495440a8a36SPatrick Mooney 
496440a8a36SPatrick Mooney 	if (p->p_itimer == NULL) {
497440a8a36SPatrick Mooney 		/*
498440a8a36SPatrick Mooney 		 * No timers have been allocated for this process, allocate
499440a8a36SPatrick Mooney 		 * the initial array.
500440a8a36SPatrick Mooney 		 */
501440a8a36SPatrick Mooney 		ASSERT0(p->p_itimer_sz);
502440a8a36SPatrick Mooney 		target_sz = _TIMER_ALLOC_INIT;
503440a8a36SPatrick Mooney 
504440a8a36SPatrick Mooney 		mutex_exit(&p->p_lock);
505440a8a36SPatrick Mooney 		itp_new = kmem_zalloc(target_sz * sizeof (itimer_t *),
506440a8a36SPatrick Mooney 		    KM_SLEEP);
507440a8a36SPatrick Mooney 		mutex_enter(&p->p_lock);
508440a8a36SPatrick Mooney 
509440a8a36SPatrick Mooney 		if (p->p_itimer == NULL) {
510440a8a36SPatrick Mooney 			/*
511440a8a36SPatrick Mooney 			 * As long as no other thread beat us to allocating
512440a8a36SPatrick Mooney 			 * the initial p_itimer array, use what we allocated.
513440a8a36SPatrick Mooney 			 * Since we just allocated it, we know slot 0 is
514440a8a36SPatrick Mooney 			 * free.
515440a8a36SPatrick Mooney 			 */
516440a8a36SPatrick Mooney 			p->p_itimer = itp_new;
517440a8a36SPatrick Mooney 			p->p_itimer_sz = target_sz;
518440a8a36SPatrick Mooney 			i = 0;
519440a8a36SPatrick Mooney 			goto done;
520440a8a36SPatrick Mooney 		}
521440a8a36SPatrick Mooney 
522440a8a36SPatrick Mooney 		/*
523440a8a36SPatrick Mooney 		 * Another thread beat us to allocating the initial array.
524440a8a36SPatrick Mooney 		 * Proceed to searching for an empty slot and growing the
525440a8a36SPatrick Mooney 		 * array if needed.
526440a8a36SPatrick Mooney 		 */
527440a8a36SPatrick Mooney 		kmem_free(itp_new, target_sz * sizeof (itimer_t *));
528440a8a36SPatrick Mooney 	}
529440a8a36SPatrick Mooney 
530440a8a36SPatrick Mooney retry:
531440a8a36SPatrick Mooney 	/* Use the first empty slot (if any exist) */
532440a8a36SPatrick Mooney 	for (i = 0; i < p->p_itimer_sz; i++) {
533440a8a36SPatrick Mooney 		if (p->p_itimer[i] == NULL) {
534440a8a36SPatrick Mooney 			goto done;
535440a8a36SPatrick Mooney 		}
536440a8a36SPatrick Mooney 	}
537440a8a36SPatrick Mooney 
538440a8a36SPatrick Mooney 	/* No empty slots, try to grow p->p_itimer and retry */
539440a8a36SPatrick Mooney 	target_sz = p->p_itimer_sz * 2;
540440a8a36SPatrick Mooney 	if (target_sz > timer_max || target_sz > INT_MAX ||
541440a8a36SPatrick Mooney 	    target_sz < p->p_itimer_sz) {
542440a8a36SPatrick Mooney 		/* Protect against exceeding the max or overflow */
543440a8a36SPatrick Mooney 		return (B_FALSE);
544440a8a36SPatrick Mooney 	}
545440a8a36SPatrick Mooney 
546440a8a36SPatrick Mooney 	mutex_exit(&p->p_lock);
547440a8a36SPatrick Mooney 	itp_new = kmem_zalloc(target_sz * sizeof (itimer_t *), KM_SLEEP);
548440a8a36SPatrick Mooney 	mutex_enter(&p->p_lock);
549440a8a36SPatrick Mooney 
550440a8a36SPatrick Mooney 	if (target_sz <= p->p_itimer_sz) {
551440a8a36SPatrick Mooney 		/*
552440a8a36SPatrick Mooney 		 * A racing thread performed the resize while we were
553440a8a36SPatrick Mooney 		 * waiting outside p_lock.  Discard our now-useless
554440a8a36SPatrick Mooney 		 * allocation and retry.
555440a8a36SPatrick Mooney 		 */
556440a8a36SPatrick Mooney 		kmem_free(itp_new, target_sz * sizeof (itimer_t *));
557440a8a36SPatrick Mooney 		goto retry;
558440a8a36SPatrick Mooney 	}
559440a8a36SPatrick Mooney 
560440a8a36SPatrick Mooney 	ASSERT3P(p->p_itimer, !=, NULL);
561440a8a36SPatrick Mooney 	bcopy(p->p_itimer, itp_new, p->p_itimer_sz * sizeof (itimer_t *));
562440a8a36SPatrick Mooney 	kmem_free(p->p_itimer, p->p_itimer_sz * sizeof (itimer_t *));
563440a8a36SPatrick Mooney 
564440a8a36SPatrick Mooney 	/*
565440a8a36SPatrick Mooney 	 * Short circuit to use the first free entry in the new allocation.
566440a8a36SPatrick Mooney 	 * It's possible that other lower-indexed timers were freed while
567440a8a36SPatrick Mooney 	 * p_lock was dropped, but skipping over them is not harmful at all.
568440a8a36SPatrick Mooney 	 * In the common case, we skip the need to walk over an array filled
569440a8a36SPatrick Mooney 	 * with timers before arriving at the slot we know is fresh from the
570440a8a36SPatrick Mooney 	 * allocation.
571440a8a36SPatrick Mooney 	 */
572440a8a36SPatrick Mooney 	i = p->p_itimer_sz;
573440a8a36SPatrick Mooney 
574440a8a36SPatrick Mooney 	p->p_itimer = itp_new;
575440a8a36SPatrick Mooney 	p->p_itimer_sz = target_sz;
576440a8a36SPatrick Mooney 
577440a8a36SPatrick Mooney done:
578440a8a36SPatrick Mooney 	ASSERT3U(i, <=, INT_MAX);
579440a8a36SPatrick Mooney 	*id = (timer_t)i;
580440a8a36SPatrick Mooney 	return (B_TRUE);
581440a8a36SPatrick Mooney }
582440a8a36SPatrick Mooney 
5837c478bd9Sstevel@tonic-gate int
timer_create(clockid_t clock,struct sigevent * evp,timer_t * tid)5847c478bd9Sstevel@tonic-gate timer_create(clockid_t clock, struct sigevent *evp, timer_t *tid)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	struct sigevent ev;
5877c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
5887c478bd9Sstevel@tonic-gate 	clock_backend_t *backend;
589440a8a36SPatrick Mooney 	itimer_t *it;
5907c478bd9Sstevel@tonic-gate 	sigqueue_t *sigq;
5917c478bd9Sstevel@tonic-gate 	cred_t *cr = CRED();
5927c478bd9Sstevel@tonic-gate 	int error = 0;
5937c478bd9Sstevel@tonic-gate 	timer_t i;
5947c478bd9Sstevel@tonic-gate 	port_notify_t tim_pnevp;
5957c478bd9Sstevel@tonic-gate 	port_kevent_t *pkevp = NULL;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if ((backend = CLOCK_BACKEND(clock)) == NULL)
5987c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (evp != NULL) {
6017c478bd9Sstevel@tonic-gate 		/*
6027c478bd9Sstevel@tonic-gate 		 * short copyin() for binary compatibility
6037c478bd9Sstevel@tonic-gate 		 * fetch oldsigevent to determine how much to copy in.
6047c478bd9Sstevel@tonic-gate 		 */
6057c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
6067c478bd9Sstevel@tonic-gate 			if (copyin(evp, &ev, sizeof (struct oldsigevent)))
6077c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6087c478bd9Sstevel@tonic-gate 
60934709573Sraf 			if (ev.sigev_notify == SIGEV_PORT ||
61034709573Sraf 			    ev.sigev_notify == SIGEV_THREAD) {
6117c478bd9Sstevel@tonic-gate 				if (copyin(ev.sigev_value.sival_ptr, &tim_pnevp,
6127c478bd9Sstevel@tonic-gate 				    sizeof (port_notify_t)))
6137c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
6167c478bd9Sstevel@tonic-gate 		} else {
6177c478bd9Sstevel@tonic-gate 			struct sigevent32 ev32;
6187c478bd9Sstevel@tonic-gate 			port_notify32_t tim_pnevp32;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 			if (copyin(evp, &ev32, sizeof (struct oldsigevent32)))
6217c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6227c478bd9Sstevel@tonic-gate 			ev.sigev_notify = ev32.sigev_notify;
6237c478bd9Sstevel@tonic-gate 			ev.sigev_signo = ev32.sigev_signo;
6247c478bd9Sstevel@tonic-gate 			/*
6257c478bd9Sstevel@tonic-gate 			 * See comment in sigqueue32() on handling of 32-bit
6267c478bd9Sstevel@tonic-gate 			 * sigvals in a 64-bit kernel.
6277c478bd9Sstevel@tonic-gate 			 */
6287c478bd9Sstevel@tonic-gate 			ev.sigev_value.sival_int = ev32.sigev_value.sival_int;
62934709573Sraf 			if (ev.sigev_notify == SIGEV_PORT ||
63034709573Sraf 			    ev.sigev_notify == SIGEV_THREAD) {
6317c478bd9Sstevel@tonic-gate 				if (copyin((void *)(uintptr_t)
6327c478bd9Sstevel@tonic-gate 				    ev32.sigev_value.sival_ptr,
6337c478bd9Sstevel@tonic-gate 				    (void *)&tim_pnevp32,
6347c478bd9Sstevel@tonic-gate 				    sizeof (port_notify32_t)))
6357c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
6367c478bd9Sstevel@tonic-gate 				tim_pnevp.portnfy_port =
6377c478bd9Sstevel@tonic-gate 				    tim_pnevp32.portnfy_port;
6387c478bd9Sstevel@tonic-gate 				tim_pnevp.portnfy_user =
6397c478bd9Sstevel@tonic-gate 				    (void *)(uintptr_t)tim_pnevp32.portnfy_user;
6407c478bd9Sstevel@tonic-gate 			}
6417c478bd9Sstevel@tonic-gate #endif
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		switch (ev.sigev_notify) {
6447c478bd9Sstevel@tonic-gate 		case SIGEV_NONE:
6457c478bd9Sstevel@tonic-gate 			break;
6467c478bd9Sstevel@tonic-gate 		case SIGEV_SIGNAL:
6477c478bd9Sstevel@tonic-gate 			if (ev.sigev_signo < 1 || ev.sigev_signo >= NSIG)
6487c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
6497c478bd9Sstevel@tonic-gate 			break;
65034709573Sraf 		case SIGEV_THREAD:
6517c478bd9Sstevel@tonic-gate 		case SIGEV_PORT:
6527c478bd9Sstevel@tonic-gate 			break;
6537c478bd9Sstevel@tonic-gate 		default:
6547c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 	} else {
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * Use the clock's default sigevent (this is a structure copy).
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		ev = backend->clk_default;
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
664440a8a36SPatrick Mooney 	 * We'll allocate our sigqueue now, before we grab p_lock.
665440a8a36SPatrick Mooney 	 * If we can't find an empty slot, we'll free it before returning.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	sigq = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/*
670440a8a36SPatrick Mooney 	 * Allocate a timer and choose a slot for it.
6717c478bd9Sstevel@tonic-gate 	 */
672440a8a36SPatrick Mooney 	it = kmem_cache_alloc(clock_timer_cache, KM_SLEEP);
673440a8a36SPatrick Mooney 	bzero(it, sizeof (*it));
674440a8a36SPatrick Mooney 	mutex_init(&it->it_mutex, NULL, MUTEX_DEFAULT, NULL);
6757c478bd9Sstevel@tonic-gate 
676440a8a36SPatrick Mooney 	mutex_enter(&p->p_lock);
677440a8a36SPatrick Mooney 	if (!timer_get_id(p, &i)) {
6787c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
6797c478bd9Sstevel@tonic-gate 		kmem_cache_free(clock_timer_cache, it);
6807c478bd9Sstevel@tonic-gate 		kmem_free(sigq, sizeof (sigqueue_t));
6817c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
684440a8a36SPatrick Mooney 	ASSERT(i < p->p_itimer_sz && p->p_itimer[i] == NULL);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * If we develop other notification mechanisms, this will need
6887c478bd9Sstevel@tonic-gate 	 * to call into (yet another) backend.
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_signo = ev.sigev_signo;
69134709573Sraf 	if (evp == NULL)
69234709573Sraf 		sigq->sq_info.si_value.sival_int = i;
69334709573Sraf 	else
69434709573Sraf 		sigq->sq_info.si_value = ev.sigev_value;
6957c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_code = SI_TIMER;
6967c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_pid = p->p_pid;
6977c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_ctid = PRCTID(p);
6987c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_zoneid = getzoneid();
6997c478bd9Sstevel@tonic-gate 	sigq->sq_info.si_uid = crgetruid(cr);
7007c478bd9Sstevel@tonic-gate 	sigq->sq_func = timer_signal;
7017c478bd9Sstevel@tonic-gate 	sigq->sq_next = NULL;
7027c478bd9Sstevel@tonic-gate 	sigq->sq_backptr = it;
7037c478bd9Sstevel@tonic-gate 	it->it_sigq = sigq;
7047c478bd9Sstevel@tonic-gate 	it->it_backend = backend;
7057c478bd9Sstevel@tonic-gate 	it->it_lock = ITLK_LOCKED;
7067c478bd9Sstevel@tonic-gate 
70734709573Sraf 	if (ev.sigev_notify == SIGEV_THREAD ||
70834709573Sraf 	    ev.sigev_notify == SIGEV_PORT) {
7097c478bd9Sstevel@tonic-gate 		int port;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * This timer is programmed to use event port notification when
7137c478bd9Sstevel@tonic-gate 		 * the timer fires:
7147c478bd9Sstevel@tonic-gate 		 * - allocate a port event structure and prepare it to be sent
7157c478bd9Sstevel@tonic-gate 		 *   to the port as soon as the timer fires.
7167c478bd9Sstevel@tonic-gate 		 * - when the timer fires :
7177c478bd9Sstevel@tonic-gate 		 *   - if event structure was already sent to the port then this
7187c478bd9Sstevel@tonic-gate 		 *	is a timer fire overflow => increment overflow counter.
7197c478bd9Sstevel@tonic-gate 		 *   - otherwise send pre-allocated event structure to the port.
7207c478bd9Sstevel@tonic-gate 		 * - the events field of the port_event_t structure counts the
7217c478bd9Sstevel@tonic-gate 		 *   number of timer fired events.
7227c478bd9Sstevel@tonic-gate 		 * - The event structured is allocated using the
7237c478bd9Sstevel@tonic-gate 		 *   PORT_ALLOC_CACHED flag.
7247c478bd9Sstevel@tonic-gate 		 *   This flag indicates that the timer itself will manage and
7257c478bd9Sstevel@tonic-gate 		 *   free the event structure when required.
7267c478bd9Sstevel@tonic-gate 		 */
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		it->it_flags |= IT_PORT;
7297c478bd9Sstevel@tonic-gate 		port = tim_pnevp.portnfy_port;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		/* associate timer as event source with the port */
7327c478bd9Sstevel@tonic-gate 		error = port_associate_ksource(port, PORT_SOURCE_TIMER,
7337c478bd9Sstevel@tonic-gate 		    (port_source_t **)&it->it_portsrc, timer_close_port,
7347c478bd9Sstevel@tonic-gate 		    (void *)it, NULL);
7357c478bd9Sstevel@tonic-gate 		if (error) {
7367c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
7377c478bd9Sstevel@tonic-gate 			kmem_cache_free(clock_timer_cache, it);
7387c478bd9Sstevel@tonic-gate 			kmem_free(sigq, sizeof (sigqueue_t));
7397c478bd9Sstevel@tonic-gate 			return (set_errno(error));
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/* allocate an event structure/slot */
7437c478bd9Sstevel@tonic-gate 		error = port_alloc_event(port, PORT_ALLOC_SCACHED,
7447c478bd9Sstevel@tonic-gate 		    PORT_SOURCE_TIMER, &pkevp);
7457c478bd9Sstevel@tonic-gate 		if (error) {
7467c478bd9Sstevel@tonic-gate 			(void) port_dissociate_ksource(port, PORT_SOURCE_TIMER,
7477c478bd9Sstevel@tonic-gate 			    (port_source_t *)it->it_portsrc);
7487c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
7497c478bd9Sstevel@tonic-gate 			kmem_cache_free(clock_timer_cache, it);
7507c478bd9Sstevel@tonic-gate 			kmem_free(sigq, sizeof (sigqueue_t));
7517c478bd9Sstevel@tonic-gate 			return (set_errno(error));
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		/* initialize event data */
7557c478bd9Sstevel@tonic-gate 		port_init_event(pkevp, i, tim_pnevp.portnfy_user,
7567c478bd9Sstevel@tonic-gate 		    timer_port_callback, it);
7577c478bd9Sstevel@tonic-gate 		it->it_portev = pkevp;
7587c478bd9Sstevel@tonic-gate 		it->it_portfd = port;
7597c478bd9Sstevel@tonic-gate 	} else {
7607c478bd9Sstevel@tonic-gate 		if (ev.sigev_notify == SIGEV_SIGNAL)
7617c478bd9Sstevel@tonic-gate 			it->it_flags |= IT_SIGNAL;
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
764440a8a36SPatrick Mooney 	/* Populate the slot now that the timer is prepped. */
765440a8a36SPatrick Mooney 	p->p_itimer[i] = it;
7667c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * Call on the backend to verify the event argument (or return
7707c478bd9Sstevel@tonic-gate 	 * EINVAL if this clock type does not support timers).
7717c478bd9Sstevel@tonic-gate 	 */
7726a72db4aSBryan Cantrill 	if ((error = backend->clk_timer_create(it, timer_fire)) != 0)
7737c478bd9Sstevel@tonic-gate 		goto err;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	it->it_lwp = ttolwp(curthread);
7767c478bd9Sstevel@tonic-gate 	it->it_proc = p;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	if (copyout(&i, tid, sizeof (timer_t)) != 0) {
7797c478bd9Sstevel@tonic-gate 		error = EFAULT;
7807c478bd9Sstevel@tonic-gate 		goto err;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * If we're here, then we have successfully created the timer; we
7857c478bd9Sstevel@tonic-gate 	 * just need to release the timer and return.
7867c478bd9Sstevel@tonic-gate 	 */
7877c478bd9Sstevel@tonic-gate 	timer_release(p, it);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	return (0);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate err:
7927c478bd9Sstevel@tonic-gate 	/*
7937c478bd9Sstevel@tonic-gate 	 * If we're here, an error has occurred late in the timer creation
7947c478bd9Sstevel@tonic-gate 	 * process.  We need to regrab p_lock, and delete the incipient timer.
7957c478bd9Sstevel@tonic-gate 	 * Since we never unlocked the timer (it was born locked), it's
7967c478bd9Sstevel@tonic-gate 	 * impossible for a removal to be pending.
7977c478bd9Sstevel@tonic-gate 	 */
7987c478bd9Sstevel@tonic-gate 	ASSERT(!(it->it_lock & ITLK_REMOVE));
7997c478bd9Sstevel@tonic-gate 	timer_delete_grabbed(p, i, it);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	return (set_errno(error));
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate int
timer_gettime(timer_t tid,itimerspec_t * val)8057c478bd9Sstevel@tonic-gate timer_gettime(timer_t tid, itimerspec_t *val)
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8087c478bd9Sstevel@tonic-gate 	itimer_t *it;
8097c478bd9Sstevel@tonic-gate 	itimerspec_t when;
8107c478bd9Sstevel@tonic-gate 	int error;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8137c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	error = it->it_backend->clk_timer_gettime(it, &when);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	timer_release(p, it);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (error == 0) {
8207c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
8217c478bd9Sstevel@tonic-gate 			if (copyout(&when, val, sizeof (itimerspec_t)))
8227c478bd9Sstevel@tonic-gate 				error = EFAULT;
8237c478bd9Sstevel@tonic-gate 		} else {
8247c478bd9Sstevel@tonic-gate 			if (ITIMERSPEC_OVERFLOW(&when))
8257c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
8267c478bd9Sstevel@tonic-gate 			else {
8277c478bd9Sstevel@tonic-gate 				itimerspec32_t w32;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 				ITIMERSPEC_TO_ITIMERSPEC32(&w32, &when)
8307c478bd9Sstevel@tonic-gate 				if (copyout(&w32, val, sizeof (itimerspec32_t)))
8317c478bd9Sstevel@tonic-gate 					error = EFAULT;
8327c478bd9Sstevel@tonic-gate 			}
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate int
timer_settime(timer_t tid,int flags,itimerspec_t * val,itimerspec_t * oval)8407c478bd9Sstevel@tonic-gate timer_settime(timer_t tid, int flags, itimerspec_t *val, itimerspec_t *oval)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	itimerspec_t when;
8437c478bd9Sstevel@tonic-gate 	itimer_t *it;
8447c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8457c478bd9Sstevel@tonic-gate 	int error;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (oval != NULL) {
8487c478bd9Sstevel@tonic-gate 		if ((error = timer_gettime(tid, oval)) != 0)
8497c478bd9Sstevel@tonic-gate 			return (error);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
8537c478bd9Sstevel@tonic-gate 		if (copyin(val, &when, sizeof (itimerspec_t)))
8547c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8557c478bd9Sstevel@tonic-gate 	} else {
8567c478bd9Sstevel@tonic-gate 		itimerspec32_t w32;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		if (copyin(val, &w32, sizeof (itimerspec32_t)))
8597c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		ITIMERSPEC32_TO_ITIMERSPEC(&when, &w32);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&when.it_value) ||
8657c478bd9Sstevel@tonic-gate 	    (itimerspecfix(&when.it_interval) &&
8667c478bd9Sstevel@tonic-gate 	    timerspecisset(&when.it_value))) {
8677c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8717c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	error = it->it_backend->clk_timer_settime(it, flags, &when);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	timer_release(p, it);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate int
timer_delete(timer_t tid)8817c478bd9Sstevel@tonic-gate timer_delete(timer_t tid)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8847c478bd9Sstevel@tonic-gate 	itimer_t *it;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
8877c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	timer_delete_grabbed(p, tid, it);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	return (0);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate int
timer_getoverrun(timer_t tid)8957c478bd9Sstevel@tonic-gate timer_getoverrun(timer_t tid)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	int overrun;
8987c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
8997c478bd9Sstevel@tonic-gate 	itimer_t *it;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	if ((it = timer_grab(p, tid)) == NULL)
9027c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * The it_overrun field is protected by p_lock; we need to acquire
9067c478bd9Sstevel@tonic-gate 	 * it before looking at the value.
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
9097c478bd9Sstevel@tonic-gate 	overrun = it->it_overrun;
9107c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	timer_release(p, it);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	return (overrun);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate  * Entered/exited with p_lock held, but will repeatedly drop and regrab p_lock.
9197c478bd9Sstevel@tonic-gate  */
9207c478bd9Sstevel@tonic-gate void
timer_lwpexit(void)9217c478bd9Sstevel@tonic-gate timer_lwpexit(void)
9227c478bd9Sstevel@tonic-gate {
923440a8a36SPatrick Mooney 	uint_t i;
9247c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
9257c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
9267c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL)
9317c478bd9Sstevel@tonic-gate 		return;
9327c478bd9Sstevel@tonic-gate 
933440a8a36SPatrick Mooney 	for (i = 0; i < p->p_itimer_sz; i++) {
9347c478bd9Sstevel@tonic-gate 		if ((it = itp[i]) == NULL)
9357c478bd9Sstevel@tonic-gate 			continue;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		if ((it->it_lock & ITLK_REMOVE) || it->it_lwp != lwp) {
9407c478bd9Sstevel@tonic-gate 			/*
9417c478bd9Sstevel@tonic-gate 			 * This timer is either being removed or it isn't
9427c478bd9Sstevel@tonic-gate 			 * associated with this lwp.
9437c478bd9Sstevel@tonic-gate 			 */
9447c478bd9Sstevel@tonic-gate 			timer_unlock(p, it);
9457c478bd9Sstevel@tonic-gate 			continue;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		/*
9497c478bd9Sstevel@tonic-gate 		 * The LWP that created this timer is going away.  To the user,
9507c478bd9Sstevel@tonic-gate 		 * our behavior here is explicitly undefined.  We will simply
9517c478bd9Sstevel@tonic-gate 		 * null out the it_lwp field; if the LWP was bound to a CPU,
9527c478bd9Sstevel@tonic-gate 		 * the cyclic will stay bound to that CPU until the process
9537c478bd9Sstevel@tonic-gate 		 * exits.
9547c478bd9Sstevel@tonic-gate 		 */
9557c478bd9Sstevel@tonic-gate 		it->it_lwp = NULL;
9567c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate  * Called to notify of an LWP binding change.  Entered/exited with p_lock
9627c478bd9Sstevel@tonic-gate  * held, but will repeatedly drop and regrab p_lock.
9637c478bd9Sstevel@tonic-gate  */
9647c478bd9Sstevel@tonic-gate void
timer_lwpbind()9657c478bd9Sstevel@tonic-gate timer_lwpbind()
9667c478bd9Sstevel@tonic-gate {
967440a8a36SPatrick Mooney 	uint_t i;
9687c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
9697c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
9707c478bd9Sstevel@tonic-gate 	itimer_t *it, **itp;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if ((itp = p->p_itimer) == NULL)
9757c478bd9Sstevel@tonic-gate 		return;
9767c478bd9Sstevel@tonic-gate 
977440a8a36SPatrick Mooney 	for (i = 0; i < p->p_itimer_sz; i++) {
9787c478bd9Sstevel@tonic-gate 		if ((it = itp[i]) == NULL)
9797c478bd9Sstevel@tonic-gate 			continue;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		timer_lock(p, it);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		if (!(it->it_lock & ITLK_REMOVE) && it->it_lwp == lwp) {
9847c478bd9Sstevel@tonic-gate 			/*
9857c478bd9Sstevel@tonic-gate 			 * Drop p_lock and jump into the backend.
9867c478bd9Sstevel@tonic-gate 			 */
9877c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
9887c478bd9Sstevel@tonic-gate 			it->it_backend->clk_timer_lwpbind(it);
9897c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		timer_unlock(p, it);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate  * This function should only be called if p_itimer is non-NULL.
9987c478bd9Sstevel@tonic-gate  */
9997c478bd9Sstevel@tonic-gate void
timer_exit(void)10007c478bd9Sstevel@tonic-gate timer_exit(void)
10017c478bd9Sstevel@tonic-gate {
1002440a8a36SPatrick Mooney 	uint_t i;
10037c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	ASSERT(p->p_itimer != NULL);
1006440a8a36SPatrick Mooney 	ASSERT(p->p_itimer_sz != 0);
10077c478bd9Sstevel@tonic-gate 
1008440a8a36SPatrick Mooney 	for (i = 0; i < p->p_itimer_sz; i++) {
1009440a8a36SPatrick Mooney 		(void) timer_delete((timer_t)i);
1010440a8a36SPatrick Mooney 	}
10117c478bd9Sstevel@tonic-gate 
1012440a8a36SPatrick Mooney 	kmem_free(p->p_itimer, p->p_itimer_sz * sizeof (itimer_t *));
10137c478bd9Sstevel@tonic-gate 	p->p_itimer = NULL;
1014440a8a36SPatrick Mooney 	p->p_itimer_sz = 0;
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*
10187c478bd9Sstevel@tonic-gate  * timer_port_callback() is a callback function which is associated with the
10197c478bd9Sstevel@tonic-gate  * timer event and is activated just before the event is delivered to the user.
10207c478bd9Sstevel@tonic-gate  * The timer uses this function to update/set the overflow counter and
10217c478bd9Sstevel@tonic-gate  * to reenable the use of the event structure.
10227c478bd9Sstevel@tonic-gate  */
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate /* ARGSUSED */
10257c478bd9Sstevel@tonic-gate static int
timer_port_callback(void * arg,int * events,pid_t pid,int flag,void * evp)10267c478bd9Sstevel@tonic-gate timer_port_callback(void *arg, int *events, pid_t pid, int flag, void *evp)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	itimer_t	*it = arg;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	mutex_enter(&it->it_mutex);
10317c478bd9Sstevel@tonic-gate 	if (curproc != it->it_proc) {
10327c478bd9Sstevel@tonic-gate 		/* can not deliver timer events to another proc */
10337c478bd9Sstevel@tonic-gate 		mutex_exit(&it->it_mutex);
10347c478bd9Sstevel@tonic-gate 		return (EACCES);
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 	*events = it->it_pending;	/* 1 = 1 event, >1 # of overflows */
10377c478bd9Sstevel@tonic-gate 	it->it_pending = 0;		/* reinit overflow counter	*/
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * This function can also be activated when the port is being closed
10407c478bd9Sstevel@tonic-gate 	 * and a timer event is already submitted to the port.
10417c478bd9Sstevel@tonic-gate 	 * In such a case the event port framework will use the
10427c478bd9Sstevel@tonic-gate 	 * close-callback function to notify the events sources.
10437c478bd9Sstevel@tonic-gate 	 * The timer close-callback function is timer_close_port() which
10447c478bd9Sstevel@tonic-gate 	 * will free all allocated resources (including the allocated
10457c478bd9Sstevel@tonic-gate 	 * port event structure).
10467c478bd9Sstevel@tonic-gate 	 * For that reason we don't need to check the value of flag here.
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	mutex_exit(&it->it_mutex);
10497c478bd9Sstevel@tonic-gate 	return (0);
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate /*
10537c478bd9Sstevel@tonic-gate  * port is being closed ... free all allocated port event structures
10547c478bd9Sstevel@tonic-gate  * The delivered arg currently correspond to the first timer associated with
10557c478bd9Sstevel@tonic-gate  * the port and it is not useable in this case.
10567c478bd9Sstevel@tonic-gate  * We have to scan the list of activated timers in the current proc and
10577c478bd9Sstevel@tonic-gate  * compare them with the delivered port id.
10587c478bd9Sstevel@tonic-gate  */
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /* ARGSUSED */
10617c478bd9Sstevel@tonic-gate static void
timer_close_port(void * arg,int port,pid_t pid,int lastclose)10627c478bd9Sstevel@tonic-gate timer_close_port(void *arg, int port, pid_t pid, int lastclose)
10637c478bd9Sstevel@tonic-gate {
10647c478bd9Sstevel@tonic-gate 	proc_t		*p = curproc;
10657c478bd9Sstevel@tonic-gate 	timer_t		tid;
10667c478bd9Sstevel@tonic-gate 	itimer_t	*it;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	for (tid = 0; tid < timer_max; tid++) {
10697c478bd9Sstevel@tonic-gate 		if ((it = timer_grab(p, tid)) == NULL)
10707c478bd9Sstevel@tonic-gate 			continue;
10717c478bd9Sstevel@tonic-gate 		if (it->it_portev) {
10727c478bd9Sstevel@tonic-gate 			mutex_enter(&it->it_mutex);
10737c478bd9Sstevel@tonic-gate 			if (it->it_portfd == port) {
10748b79e857Spraks 				port_kevent_t *pev;
10758b79e857Spraks 				pev = (port_kevent_t *)it->it_portev;
10767c478bd9Sstevel@tonic-gate 				it->it_portev = NULL;
10777c478bd9Sstevel@tonic-gate 				it->it_flags &= ~IT_PORT;
10788b79e857Spraks 				mutex_exit(&it->it_mutex);
10798b79e857Spraks 				(void) port_remove_done_event(pev);
10808b79e857Spraks 				port_free_event(pev);
10818b79e857Spraks 			} else {
10828b79e857Spraks 				mutex_exit(&it->it_mutex);
10837c478bd9Sstevel@tonic-gate 			}
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 		timer_release(p, it);
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate }
1088