xref: /titanic_51/usr/src/uts/common/os/clock_highres.c (revision 6a72db4a7fa12c3e0d1c1cf91a07390739fa0fbf)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27c48ec423SBryan Cantrill /*
28*6a72db4aSBryan Cantrill  * Copyright (c) 2015, Joyent Inc. All rights reserved.
29c48ec423SBryan Cantrill  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/timer.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
387c478bd9Sstevel@tonic-gate #include <sys/pset.h>
397c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
407c478bd9Sstevel@tonic-gate #include <sys/policy.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static clock_backend_t clock_highres;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
457c478bd9Sstevel@tonic-gate static int
467c478bd9Sstevel@tonic-gate clock_highres_settime(timespec_t *ts)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	return (EINVAL);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int
527c478bd9Sstevel@tonic-gate clock_highres_gettime(timespec_t *ts)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	hrt2ts(gethrtime(), (timestruc_t *)ts);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	return (0);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int
607c478bd9Sstevel@tonic-gate clock_highres_getres(timespec_t *ts)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	hrt2ts(cyclic_getres(), (timestruc_t *)ts);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	return (0);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
687c478bd9Sstevel@tonic-gate static int
69*6a72db4aSBryan Cantrill clock_highres_timer_create(itimer_t *it, void (*fire)(itimer_t *))
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	/*
727c478bd9Sstevel@tonic-gate 	 * CLOCK_HIGHRES timers of sufficiently high resolution can deny
737c478bd9Sstevel@tonic-gate 	 * service; only allow privileged users to create such timers.
747c478bd9Sstevel@tonic-gate 	 * Sites that do not wish to have this restriction should
757c478bd9Sstevel@tonic-gate 	 * give users the "proc_clock_highres" privilege.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	if (secpolicy_clock_highres(CRED()) != 0) {
787c478bd9Sstevel@tonic-gate 		it->it_arg = NULL;
797c478bd9Sstevel@tonic-gate 		return (EPERM);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	it->it_arg = kmem_zalloc(sizeof (cyclic_id_t), KM_SLEEP);
83*6a72db4aSBryan Cantrill 	it->it_fire = fire;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	return (0);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void
897c478bd9Sstevel@tonic-gate clock_highres_fire(void *arg)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	itimer_t *it = (itimer_t *)arg;
927c478bd9Sstevel@tonic-gate 	hrtime_t *addr = &it->it_hrtime;
937c478bd9Sstevel@tonic-gate 	hrtime_t old = *addr, new = gethrtime();
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	do {
967c478bd9Sstevel@tonic-gate 		old = *addr;
9775d94465SJosef 'Jeff' Sipek 	} while (atomic_cas_64((uint64_t *)addr, old, new) != old);
987c478bd9Sstevel@tonic-gate 
99*6a72db4aSBryan Cantrill 	it->it_fire(it);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static int
1037c478bd9Sstevel@tonic-gate clock_highres_timer_settime(itimer_t *it, int flags,
1047c478bd9Sstevel@tonic-gate 	const struct itimerspec *when)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	cyclic_id_t cyc, *cycp = it->it_arg;
1077c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1087c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1097c478bd9Sstevel@tonic-gate 	cyc_time_t cyctime;
1107c478bd9Sstevel@tonic-gate 	cyc_handler_t hdlr;
1117c478bd9Sstevel@tonic-gate 	cpu_t *cpu;
1127c478bd9Sstevel@tonic-gate 	cpupart_t *cpupart;
1137c478bd9Sstevel@tonic-gate 	int pset;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	cyctime.cyt_when = ts2hrt(&when->it_value);
1167c478bd9Sstevel@tonic-gate 	cyctime.cyt_interval = ts2hrt(&when->it_interval);
1177c478bd9Sstevel@tonic-gate 
118c48ec423SBryan Cantrill 	if (cyctime.cyt_when != 0 && cyctime.cyt_interval == 0 &&
119c48ec423SBryan Cantrill 	    it->it_itime.it_interval.tv_sec == 0 &&
120c48ec423SBryan Cantrill 	    it->it_itime.it_interval.tv_nsec == 0 &&
121c48ec423SBryan Cantrill 	    (cyc = *cycp) != CYCLIC_NONE) {
122c48ec423SBryan Cantrill 		/*
123c48ec423SBryan Cantrill 		 * If our existing timer is a one-shot and our new timer is a
124c48ec423SBryan Cantrill 		 * one-shot, we'll save ourselves a world of grief and just
125c48ec423SBryan Cantrill 		 * reprogram the cyclic.
126c48ec423SBryan Cantrill 		 */
127c48ec423SBryan Cantrill 		it->it_itime = *when;
128c48ec423SBryan Cantrill 
129c48ec423SBryan Cantrill 		if (!(flags & TIMER_ABSTIME))
130c48ec423SBryan Cantrill 			cyctime.cyt_when += gethrtime();
131c48ec423SBryan Cantrill 
132c48ec423SBryan Cantrill 		hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
133c48ec423SBryan Cantrill 		(void) cyclic_reprogram(cyc, cyctime.cyt_when);
134c48ec423SBryan Cantrill 		return (0);
135c48ec423SBryan Cantrill 	}
136c48ec423SBryan Cantrill 
1377c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1387c478bd9Sstevel@tonic-gate 	if ((cyc = *cycp) != CYCLIC_NONE) {
1397c478bd9Sstevel@tonic-gate 		cyclic_remove(cyc);
1407c478bd9Sstevel@tonic-gate 		*cycp = CYCLIC_NONE;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (cyctime.cyt_when == 0) {
1447c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1457c478bd9Sstevel@tonic-gate 		return (0);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (!(flags & TIMER_ABSTIME))
1497c478bd9Sstevel@tonic-gate 		cyctime.cyt_when += gethrtime();
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Now we will check for overflow (that is, we will check to see
1537c478bd9Sstevel@tonic-gate 	 * that the start time plus the interval time doesn't exceed
1547c478bd9Sstevel@tonic-gate 	 * INT64_MAX).  The astute code reviewer will observe that this
1557c478bd9Sstevel@tonic-gate 	 * one-time check doesn't guarantee that a future expiration
1567c478bd9Sstevel@tonic-gate 	 * will not wrap.  We wish to prove, then, that if a future
1577c478bd9Sstevel@tonic-gate 	 * expiration does wrap, the earliest the problem can be encountered
1587c478bd9Sstevel@tonic-gate 	 * is (INT64_MAX / 2) nanoseconds (191 years) after boot.  Formally:
1597c478bd9Sstevel@tonic-gate 	 *
1607c478bd9Sstevel@tonic-gate 	 *  Given:	s + i < m	s > 0	i > 0
1617c478bd9Sstevel@tonic-gate 	 *		s + ni > m	n > 1
1627c478bd9Sstevel@tonic-gate 	 *
1637c478bd9Sstevel@tonic-gate 	 *    (where "s" is the start time, "i" is the interval, "n" is the
1647c478bd9Sstevel@tonic-gate 	 *    number of times the cyclic has fired and "m" is INT64_MAX)
1657c478bd9Sstevel@tonic-gate 	 *
1667c478bd9Sstevel@tonic-gate 	 *  Prove:
1677c478bd9Sstevel@tonic-gate 	 *		(a)  s + (n - 1)i > (m / 2)
1687c478bd9Sstevel@tonic-gate 	 *		(b)  s + (n - 1)i < m
1697c478bd9Sstevel@tonic-gate 	 *
1707c478bd9Sstevel@tonic-gate 	 * That is, prove that we must have fired at least once 191 years
1717c478bd9Sstevel@tonic-gate 	 * after boot.  The proof is very straightforward; since the left
1727c478bd9Sstevel@tonic-gate 	 * side of (a) is minimized when i is small, it is sufficient to show
1737c478bd9Sstevel@tonic-gate 	 * that the statement is true for i's smallest possible value
1747c478bd9Sstevel@tonic-gate 	 * (((m - s) / n) + epsilon).  The same goes for (b); showing that the
1757c478bd9Sstevel@tonic-gate 	 * statement is true for i's largest possible value (m - s + epsilon)
1767c478bd9Sstevel@tonic-gate 	 * is sufficient to prove the statement.
1777c478bd9Sstevel@tonic-gate 	 *
1787c478bd9Sstevel@tonic-gate 	 * The actual arithmetic manipulation is left up to reader.
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	if (cyctime.cyt_when > INT64_MAX - cyctime.cyt_interval) {
1817c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1827c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (cyctime.cyt_interval == 0) {
1867c478bd9Sstevel@tonic-gate 		/*
187c48ec423SBryan Cantrill 		 * If this is a one-shot, then we set the interval to be
188c48ec423SBryan Cantrill 		 * inifinite.  If this timer is never touched, this cyclic will
189c48ec423SBryan Cantrill 		 * simply consume space in the cyclic subsystem.  As soon as
1907c478bd9Sstevel@tonic-gate 		 * timer_settime() or timer_delete() is called, the cyclic is
1917c478bd9Sstevel@tonic-gate 		 * removed (so it's not possible to run the machine out
1927c478bd9Sstevel@tonic-gate 		 * of resources by creating one-shots).
1937c478bd9Sstevel@tonic-gate 		 */
194c48ec423SBryan Cantrill 		cyctime.cyt_interval = CY_INFINITY;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	it->it_itime = *when;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	hdlr.cyh_func = (cyc_func_t)clock_highres_fire;
2027c478bd9Sstevel@tonic-gate 	hdlr.cyh_arg = it;
2037c478bd9Sstevel@tonic-gate 	hdlr.cyh_level = CY_LOW_LEVEL;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (cyctime.cyt_when != 0)
2067c478bd9Sstevel@tonic-gate 		*cycp = cyc = cyclic_add(&hdlr, &cyctime);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Now that we have the cyclic created, we need to bind it to our
2107c478bd9Sstevel@tonic-gate 	 * bound CPU and processor set (if any).
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2137c478bd9Sstevel@tonic-gate 	cpu = t->t_bound_cpu;
2147c478bd9Sstevel@tonic-gate 	cpupart = t->t_cpupart;
2157c478bd9Sstevel@tonic-gate 	pset = t->t_bind_pset;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	return (0);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static int
2277c478bd9Sstevel@tonic-gate clock_highres_timer_gettime(itimer_t *it, struct itimerspec *when)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * CLOCK_HIGHRES doesn't update it_itime.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	hrtime_t start = ts2hrt(&it->it_itime.it_value);
2337c478bd9Sstevel@tonic-gate 	hrtime_t interval = ts2hrt(&it->it_itime.it_interval);
2347c478bd9Sstevel@tonic-gate 	hrtime_t diff, now = gethrtime();
2357c478bd9Sstevel@tonic-gate 	hrtime_t *addr = &it->it_hrtime;
2367c478bd9Sstevel@tonic-gate 	hrtime_t last;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
23975d94465SJosef 'Jeff' Sipek 	 * We're using atomic_cas_64() here only to assure that we slurp the
24075d94465SJosef 'Jeff' Sipek 	 * entire timestamp atomically.
2417c478bd9Sstevel@tonic-gate 	 */
24275d94465SJosef 'Jeff' Sipek 	last = atomic_cas_64((uint64_t *)addr, 0, 0);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	*when = it->it_itime;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (!timerspecisset(&when->it_value))
2477c478bd9Sstevel@tonic-gate 		return (0);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (start > now) {
2507c478bd9Sstevel@tonic-gate 		/*
2517c478bd9Sstevel@tonic-gate 		 * We haven't gone off yet...
2527c478bd9Sstevel@tonic-gate 		 */
2537c478bd9Sstevel@tonic-gate 		diff = start - now;
2547c478bd9Sstevel@tonic-gate 	} else {
2557c478bd9Sstevel@tonic-gate 		if (interval == 0) {
2567c478bd9Sstevel@tonic-gate 			/*
2577c478bd9Sstevel@tonic-gate 			 * This is a one-shot which should have already
2587c478bd9Sstevel@tonic-gate 			 * fired; set it_value to 0.
2597c478bd9Sstevel@tonic-gate 			 */
2607c478bd9Sstevel@tonic-gate 			timerspecclear(&when->it_value);
2617c478bd9Sstevel@tonic-gate 			return (0);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		/*
2657c478bd9Sstevel@tonic-gate 		 * Calculate how far we are into this interval.
2667c478bd9Sstevel@tonic-gate 		 */
2677c478bd9Sstevel@tonic-gate 		diff = (now - start) % interval;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		/*
2707c478bd9Sstevel@tonic-gate 		 * Now check to see if we've dealt with the last interval
2717c478bd9Sstevel@tonic-gate 		 * yet.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		if (now - diff > last) {
2747c478bd9Sstevel@tonic-gate 			/*
2757c478bd9Sstevel@tonic-gate 			 * The last interval hasn't fired; set it_value to 0.
2767c478bd9Sstevel@tonic-gate 			 */
2777c478bd9Sstevel@tonic-gate 			timerspecclear(&when->it_value);
2787c478bd9Sstevel@tonic-gate 			return (0);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/*
2827c478bd9Sstevel@tonic-gate 		 * The last interval _has_ fired; we can return the amount
2837c478bd9Sstevel@tonic-gate 		 * of time left in this interval.
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		diff = interval - diff;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	hrt2ts(diff, &when->it_value);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	return (0);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate static int
2947c478bd9Sstevel@tonic-gate clock_highres_timer_delete(itimer_t *it)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	cyclic_id_t cyc;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (it->it_arg == NULL) {
2997c478bd9Sstevel@tonic-gate 		/*
3007c478bd9Sstevel@tonic-gate 		 * This timer was never fully created; we must have failed
3017c478bd9Sstevel@tonic-gate 		 * in the clock_highres_timer_create() routine.
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		return (0);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if ((cyc = *((cyclic_id_t *)it->it_arg)) != CYCLIC_NONE)
3097c478bd9Sstevel@tonic-gate 		cyclic_remove(cyc);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	kmem_free(it->it_arg, sizeof (cyclic_id_t));
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (0);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static void
3197c478bd9Sstevel@tonic-gate clock_highres_timer_lwpbind(itimer_t *it)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
3227c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3237c478bd9Sstevel@tonic-gate 	cyclic_id_t cyc = *((cyclic_id_t *)it->it_arg);
3247c478bd9Sstevel@tonic-gate 	cpu_t *cpu;
3257c478bd9Sstevel@tonic-gate 	cpupart_t *cpupart;
3267c478bd9Sstevel@tonic-gate 	int pset;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (cyc == CYCLIC_NONE)
3297c478bd9Sstevel@tonic-gate 		return;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3327c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * Okay, now we can safely look at the bindings.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	cpu = t->t_bound_cpu;
3387c478bd9Sstevel@tonic-gate 	cpupart = t->t_cpupart;
3397c478bd9Sstevel@tonic-gate 	pset = t->t_bind_pset;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Now we drop p_lock.  We haven't dropped cpu_lock; we're guaranteed
3437c478bd9Sstevel@tonic-gate 	 * that even if the bindings change, the CPU and/or processor set
3447c478bd9Sstevel@tonic-gate 	 * that this timer was bound to remain valid (and the combination
3457c478bd9Sstevel@tonic-gate 	 * remains self-consistent).
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate void
3557c478bd9Sstevel@tonic-gate clock_highres_init()
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	clock_backend_t *be = &clock_highres;
3587c478bd9Sstevel@tonic-gate 	struct sigevent *ev = &be->clk_default;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	ev->sigev_signo = SIGALRM;
3617c478bd9Sstevel@tonic-gate 	ev->sigev_notify = SIGEV_SIGNAL;
3627c478bd9Sstevel@tonic-gate 	ev->sigev_value.sival_ptr = NULL;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	be->clk_clock_settime = clock_highres_settime;
3657c478bd9Sstevel@tonic-gate 	be->clk_clock_gettime = clock_highres_gettime;
3667c478bd9Sstevel@tonic-gate 	be->clk_clock_getres = clock_highres_getres;
3677c478bd9Sstevel@tonic-gate 	be->clk_timer_create = clock_highres_timer_create;
3687c478bd9Sstevel@tonic-gate 	be->clk_timer_gettime = clock_highres_timer_gettime;
3697c478bd9Sstevel@tonic-gate 	be->clk_timer_settime = clock_highres_timer_settime;
3707c478bd9Sstevel@tonic-gate 	be->clk_timer_delete = clock_highres_timer_delete;
3717c478bd9Sstevel@tonic-gate 	be->clk_timer_lwpbind = clock_highres_timer_lwpbind;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	clock_add_backend(CLOCK_HIGHRES, &clock_highres);
3747c478bd9Sstevel@tonic-gate }
375