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*605d010dSJerry Jelinek * Copyright 2016, Joyent Inc.
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
44*605d010dSJerry Jelinek /* minimum non-privileged interval (200us) */
45*605d010dSJerry Jelinek long clock_highres_interval_min = 200000;
46*605d010dSJerry Jelinek
477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
487c478bd9Sstevel@tonic-gate static int
clock_highres_settime(timespec_t * ts)497c478bd9Sstevel@tonic-gate clock_highres_settime(timespec_t *ts)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate return (EINVAL);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static int
clock_highres_gettime(timespec_t * ts)557c478bd9Sstevel@tonic-gate clock_highres_gettime(timespec_t *ts)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate hrt2ts(gethrtime(), (timestruc_t *)ts);
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate return (0);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static int
clock_highres_getres(timespec_t * ts)637c478bd9Sstevel@tonic-gate clock_highres_getres(timespec_t *ts)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate hrt2ts(cyclic_getres(), (timestruc_t *)ts);
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
717c478bd9Sstevel@tonic-gate static int
clock_highres_timer_create(itimer_t * it,void (* fire)(itimer_t *))726a72db4aSBryan Cantrill clock_highres_timer_create(itimer_t *it, void (*fire)(itimer_t *))
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate it->it_arg = kmem_zalloc(sizeof (cyclic_id_t), KM_SLEEP);
756a72db4aSBryan Cantrill it->it_fire = fire;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate return (0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static void
clock_highres_fire(void * arg)817c478bd9Sstevel@tonic-gate clock_highres_fire(void *arg)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate itimer_t *it = (itimer_t *)arg;
847c478bd9Sstevel@tonic-gate hrtime_t *addr = &it->it_hrtime;
857c478bd9Sstevel@tonic-gate hrtime_t old = *addr, new = gethrtime();
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate do {
887c478bd9Sstevel@tonic-gate old = *addr;
8975d94465SJosef 'Jeff' Sipek } while (atomic_cas_64((uint64_t *)addr, old, new) != old);
907c478bd9Sstevel@tonic-gate
916a72db4aSBryan Cantrill it->it_fire(it);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate static int
clock_highres_timer_settime(itimer_t * it,int flags,const struct itimerspec * when)957c478bd9Sstevel@tonic-gate clock_highres_timer_settime(itimer_t *it, int flags,
967c478bd9Sstevel@tonic-gate const struct itimerspec *when)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate cyclic_id_t cyc, *cycp = it->it_arg;
997c478bd9Sstevel@tonic-gate proc_t *p = curproc;
1007c478bd9Sstevel@tonic-gate kthread_t *t = curthread;
1017c478bd9Sstevel@tonic-gate cyc_time_t cyctime;
1027c478bd9Sstevel@tonic-gate cyc_handler_t hdlr;
1037c478bd9Sstevel@tonic-gate cpu_t *cpu;
1047c478bd9Sstevel@tonic-gate cpupart_t *cpupart;
1057c478bd9Sstevel@tonic-gate int pset;
106*605d010dSJerry Jelinek boolean_t value_need_clamp = B_FALSE;
107*605d010dSJerry Jelinek boolean_t intval_need_clamp = B_FALSE;
108*605d010dSJerry Jelinek cred_t *cr = CRED();
109*605d010dSJerry Jelinek struct itimerspec clamped;
110*605d010dSJerry Jelinek
111*605d010dSJerry Jelinek /*
112*605d010dSJerry Jelinek * CLOCK_HIGHRES timers of sufficiently high resolution can deny
113*605d010dSJerry Jelinek * service; only allow privileged users to create such timers.
114*605d010dSJerry Jelinek * Non-privileged users (those without the "proc_clock_highres"
115*605d010dSJerry Jelinek * privilege) can create timers with lower resolution but if they
116*605d010dSJerry Jelinek * attempt to use a very low time value (< 200us) then their
117*605d010dSJerry Jelinek * timer will be clamped at 200us.
118*605d010dSJerry Jelinek */
119*605d010dSJerry Jelinek if (when->it_value.tv_sec == 0 &&
120*605d010dSJerry Jelinek when->it_value.tv_nsec > 0 &&
121*605d010dSJerry Jelinek when->it_value.tv_nsec < clock_highres_interval_min)
122*605d010dSJerry Jelinek value_need_clamp = B_TRUE;
123*605d010dSJerry Jelinek
124*605d010dSJerry Jelinek if (when->it_interval.tv_sec == 0 &&
125*605d010dSJerry Jelinek when->it_interval.tv_nsec > 0 &&
126*605d010dSJerry Jelinek when->it_interval.tv_nsec < clock_highres_interval_min)
127*605d010dSJerry Jelinek intval_need_clamp = B_TRUE;
128*605d010dSJerry Jelinek
129*605d010dSJerry Jelinek if ((value_need_clamp || intval_need_clamp) &&
130*605d010dSJerry Jelinek secpolicy_clock_highres(cr) != 0) {
131*605d010dSJerry Jelinek clamped.it_value.tv_sec = when->it_value.tv_sec;
132*605d010dSJerry Jelinek clamped.it_interval.tv_sec = when->it_interval.tv_sec;
133*605d010dSJerry Jelinek
134*605d010dSJerry Jelinek if (value_need_clamp) {
135*605d010dSJerry Jelinek clamped.it_value.tv_nsec = clock_highres_interval_min;
136*605d010dSJerry Jelinek } else {
137*605d010dSJerry Jelinek clamped.it_value.tv_nsec = when->it_value.tv_nsec;
138*605d010dSJerry Jelinek }
139*605d010dSJerry Jelinek
140*605d010dSJerry Jelinek if (intval_need_clamp) {
141*605d010dSJerry Jelinek clamped.it_interval.tv_nsec =
142*605d010dSJerry Jelinek clock_highres_interval_min;
143*605d010dSJerry Jelinek } else {
144*605d010dSJerry Jelinek clamped.it_interval.tv_nsec = when->it_interval.tv_nsec;
145*605d010dSJerry Jelinek }
146*605d010dSJerry Jelinek
147*605d010dSJerry Jelinek when = &clamped;
148*605d010dSJerry Jelinek }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate cyctime.cyt_when = ts2hrt(&when->it_value);
1517c478bd9Sstevel@tonic-gate cyctime.cyt_interval = ts2hrt(&when->it_interval);
1527c478bd9Sstevel@tonic-gate
153c48ec423SBryan Cantrill if (cyctime.cyt_when != 0 && cyctime.cyt_interval == 0 &&
154c48ec423SBryan Cantrill it->it_itime.it_interval.tv_sec == 0 &&
155c48ec423SBryan Cantrill it->it_itime.it_interval.tv_nsec == 0 &&
156c48ec423SBryan Cantrill (cyc = *cycp) != CYCLIC_NONE) {
157c48ec423SBryan Cantrill /*
158c48ec423SBryan Cantrill * If our existing timer is a one-shot and our new timer is a
159c48ec423SBryan Cantrill * one-shot, we'll save ourselves a world of grief and just
160c48ec423SBryan Cantrill * reprogram the cyclic.
161c48ec423SBryan Cantrill */
162c48ec423SBryan Cantrill it->it_itime = *when;
163c48ec423SBryan Cantrill
164c48ec423SBryan Cantrill if (!(flags & TIMER_ABSTIME))
165c48ec423SBryan Cantrill cyctime.cyt_when += gethrtime();
166c48ec423SBryan Cantrill
167c48ec423SBryan Cantrill hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
168c48ec423SBryan Cantrill (void) cyclic_reprogram(cyc, cyctime.cyt_when);
169c48ec423SBryan Cantrill return (0);
170c48ec423SBryan Cantrill }
171c48ec423SBryan Cantrill
1727c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock);
1737c478bd9Sstevel@tonic-gate if ((cyc = *cycp) != CYCLIC_NONE) {
1747c478bd9Sstevel@tonic-gate cyclic_remove(cyc);
1757c478bd9Sstevel@tonic-gate *cycp = CYCLIC_NONE;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if (cyctime.cyt_when == 0) {
1797c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock);
1807c478bd9Sstevel@tonic-gate return (0);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if (!(flags & TIMER_ABSTIME))
1847c478bd9Sstevel@tonic-gate cyctime.cyt_when += gethrtime();
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate * Now we will check for overflow (that is, we will check to see
1887c478bd9Sstevel@tonic-gate * that the start time plus the interval time doesn't exceed
1897c478bd9Sstevel@tonic-gate * INT64_MAX). The astute code reviewer will observe that this
1907c478bd9Sstevel@tonic-gate * one-time check doesn't guarantee that a future expiration
1917c478bd9Sstevel@tonic-gate * will not wrap. We wish to prove, then, that if a future
1927c478bd9Sstevel@tonic-gate * expiration does wrap, the earliest the problem can be encountered
1937c478bd9Sstevel@tonic-gate * is (INT64_MAX / 2) nanoseconds (191 years) after boot. Formally:
1947c478bd9Sstevel@tonic-gate *
1957c478bd9Sstevel@tonic-gate * Given: s + i < m s > 0 i > 0
1967c478bd9Sstevel@tonic-gate * s + ni > m n > 1
1977c478bd9Sstevel@tonic-gate *
1987c478bd9Sstevel@tonic-gate * (where "s" is the start time, "i" is the interval, "n" is the
1997c478bd9Sstevel@tonic-gate * number of times the cyclic has fired and "m" is INT64_MAX)
2007c478bd9Sstevel@tonic-gate *
2017c478bd9Sstevel@tonic-gate * Prove:
2027c478bd9Sstevel@tonic-gate * (a) s + (n - 1)i > (m / 2)
2037c478bd9Sstevel@tonic-gate * (b) s + (n - 1)i < m
2047c478bd9Sstevel@tonic-gate *
2057c478bd9Sstevel@tonic-gate * That is, prove that we must have fired at least once 191 years
2067c478bd9Sstevel@tonic-gate * after boot. The proof is very straightforward; since the left
2077c478bd9Sstevel@tonic-gate * side of (a) is minimized when i is small, it is sufficient to show
2087c478bd9Sstevel@tonic-gate * that the statement is true for i's smallest possible value
2097c478bd9Sstevel@tonic-gate * (((m - s) / n) + epsilon). The same goes for (b); showing that the
2107c478bd9Sstevel@tonic-gate * statement is true for i's largest possible value (m - s + epsilon)
2117c478bd9Sstevel@tonic-gate * is sufficient to prove the statement.
2127c478bd9Sstevel@tonic-gate *
2137c478bd9Sstevel@tonic-gate * The actual arithmetic manipulation is left up to reader.
2147c478bd9Sstevel@tonic-gate */
2157c478bd9Sstevel@tonic-gate if (cyctime.cyt_when > INT64_MAX - cyctime.cyt_interval) {
2167c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock);
2177c478bd9Sstevel@tonic-gate return (EOVERFLOW);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate if (cyctime.cyt_interval == 0) {
2217c478bd9Sstevel@tonic-gate /*
222c48ec423SBryan Cantrill * If this is a one-shot, then we set the interval to be
223c48ec423SBryan Cantrill * inifinite. If this timer is never touched, this cyclic will
224c48ec423SBryan Cantrill * simply consume space in the cyclic subsystem. As soon as
2257c478bd9Sstevel@tonic-gate * timer_settime() or timer_delete() is called, the cyclic is
2267c478bd9Sstevel@tonic-gate * removed (so it's not possible to run the machine out
2277c478bd9Sstevel@tonic-gate * of resources by creating one-shots).
2287c478bd9Sstevel@tonic-gate */
229c48ec423SBryan Cantrill cyctime.cyt_interval = CY_INFINITY;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate it->it_itime = *when;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate hdlr.cyh_func = (cyc_func_t)clock_highres_fire;
2377c478bd9Sstevel@tonic-gate hdlr.cyh_arg = it;
2387c478bd9Sstevel@tonic-gate hdlr.cyh_level = CY_LOW_LEVEL;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if (cyctime.cyt_when != 0)
2417c478bd9Sstevel@tonic-gate *cycp = cyc = cyclic_add(&hdlr, &cyctime);
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * Now that we have the cyclic created, we need to bind it to our
2457c478bd9Sstevel@tonic-gate * bound CPU and processor set (if any).
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2487c478bd9Sstevel@tonic-gate cpu = t->t_bound_cpu;
2497c478bd9Sstevel@tonic-gate cpupart = t->t_cpupart;
2507c478bd9Sstevel@tonic-gate pset = t->t_bind_pset;
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock);
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate return (0);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate static int
clock_highres_timer_gettime(itimer_t * it,struct itimerspec * when)2627c478bd9Sstevel@tonic-gate clock_highres_timer_gettime(itimer_t *it, struct itimerspec *when)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * CLOCK_HIGHRES doesn't update it_itime.
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate hrtime_t start = ts2hrt(&it->it_itime.it_value);
2687c478bd9Sstevel@tonic-gate hrtime_t interval = ts2hrt(&it->it_itime.it_interval);
2697c478bd9Sstevel@tonic-gate hrtime_t diff, now = gethrtime();
2707c478bd9Sstevel@tonic-gate hrtime_t *addr = &it->it_hrtime;
2717c478bd9Sstevel@tonic-gate hrtime_t last;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate /*
27475d94465SJosef 'Jeff' Sipek * We're using atomic_cas_64() here only to assure that we slurp the
27575d94465SJosef 'Jeff' Sipek * entire timestamp atomically.
2767c478bd9Sstevel@tonic-gate */
27775d94465SJosef 'Jeff' Sipek last = atomic_cas_64((uint64_t *)addr, 0, 0);
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate *when = it->it_itime;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if (!timerspecisset(&when->it_value))
2827c478bd9Sstevel@tonic-gate return (0);
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate if (start > now) {
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate * We haven't gone off yet...
2877c478bd9Sstevel@tonic-gate */
2887c478bd9Sstevel@tonic-gate diff = start - now;
2897c478bd9Sstevel@tonic-gate } else {
2907c478bd9Sstevel@tonic-gate if (interval == 0) {
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate * This is a one-shot which should have already
2937c478bd9Sstevel@tonic-gate * fired; set it_value to 0.
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate timerspecclear(&when->it_value);
2967c478bd9Sstevel@tonic-gate return (0);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * Calculate how far we are into this interval.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate diff = (now - start) % interval;
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * Now check to see if we've dealt with the last interval
3067c478bd9Sstevel@tonic-gate * yet.
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate if (now - diff > last) {
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate * The last interval hasn't fired; set it_value to 0.
3117c478bd9Sstevel@tonic-gate */
3127c478bd9Sstevel@tonic-gate timerspecclear(&when->it_value);
3137c478bd9Sstevel@tonic-gate return (0);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate * The last interval _has_ fired; we can return the amount
3187c478bd9Sstevel@tonic-gate * of time left in this interval.
3197c478bd9Sstevel@tonic-gate */
3207c478bd9Sstevel@tonic-gate diff = interval - diff;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate hrt2ts(diff, &when->it_value);
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate static int
clock_highres_timer_delete(itimer_t * it)3297c478bd9Sstevel@tonic-gate clock_highres_timer_delete(itimer_t *it)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate cyclic_id_t cyc;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (it->it_arg == NULL) {
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate * This timer was never fully created; we must have failed
3367c478bd9Sstevel@tonic-gate * in the clock_highres_timer_create() routine.
3377c478bd9Sstevel@tonic-gate */
3387c478bd9Sstevel@tonic-gate return (0);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock);
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate if ((cyc = *((cyclic_id_t *)it->it_arg)) != CYCLIC_NONE)
3447c478bd9Sstevel@tonic-gate cyclic_remove(cyc);
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock);
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate kmem_free(it->it_arg, sizeof (cyclic_id_t));
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate return (0);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate static void
clock_highres_timer_lwpbind(itimer_t * it)3547c478bd9Sstevel@tonic-gate clock_highres_timer_lwpbind(itimer_t *it)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate proc_t *p = curproc;
3577c478bd9Sstevel@tonic-gate kthread_t *t = curthread;
3587c478bd9Sstevel@tonic-gate cyclic_id_t cyc = *((cyclic_id_t *)it->it_arg);
3597c478bd9Sstevel@tonic-gate cpu_t *cpu;
3607c478bd9Sstevel@tonic-gate cpupart_t *cpupart;
3617c478bd9Sstevel@tonic-gate int pset;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate if (cyc == CYCLIC_NONE)
3647c478bd9Sstevel@tonic-gate return;
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock);
3677c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate * Okay, now we can safely look at the bindings.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate cpu = t->t_bound_cpu;
3737c478bd9Sstevel@tonic-gate cpupart = t->t_cpupart;
3747c478bd9Sstevel@tonic-gate pset = t->t_bind_pset;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate * Now we drop p_lock. We haven't dropped cpu_lock; we're guaranteed
3787c478bd9Sstevel@tonic-gate * that even if the bindings change, the CPU and/or processor set
3797c478bd9Sstevel@tonic-gate * that this timer was bound to remain valid (and the combination
3807c478bd9Sstevel@tonic-gate * remains self-consistent).
3817c478bd9Sstevel@tonic-gate */
3827c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate void
clock_highres_init()3907c478bd9Sstevel@tonic-gate clock_highres_init()
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate clock_backend_t *be = &clock_highres;
3937c478bd9Sstevel@tonic-gate struct sigevent *ev = &be->clk_default;
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate ev->sigev_signo = SIGALRM;
3967c478bd9Sstevel@tonic-gate ev->sigev_notify = SIGEV_SIGNAL;
3977c478bd9Sstevel@tonic-gate ev->sigev_value.sival_ptr = NULL;
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate be->clk_clock_settime = clock_highres_settime;
4007c478bd9Sstevel@tonic-gate be->clk_clock_gettime = clock_highres_gettime;
4017c478bd9Sstevel@tonic-gate be->clk_clock_getres = clock_highres_getres;
4027c478bd9Sstevel@tonic-gate be->clk_timer_create = clock_highres_timer_create;
4037c478bd9Sstevel@tonic-gate be->clk_timer_gettime = clock_highres_timer_gettime;
4047c478bd9Sstevel@tonic-gate be->clk_timer_settime = clock_highres_timer_settime;
4057c478bd9Sstevel@tonic-gate be->clk_timer_delete = clock_highres_timer_delete;
4067c478bd9Sstevel@tonic-gate be->clk_timer_lwpbind = clock_highres_timer_lwpbind;
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate clock_add_backend(CLOCK_HIGHRES, &clock_highres);
4097c478bd9Sstevel@tonic-gate }
410