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
27*6a72db4aSBryan Cantrill /*
28*6a72db4aSBryan Cantrill * Copyright (c) 2015, Joyent Inc. All rights reserved.
29*6a72db4aSBryan 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
377c478bd9Sstevel@tonic-gate static clock_backend_t clock_realtime;
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static int
clock_realtime_settime(timespec_t * ts)407c478bd9Sstevel@tonic-gate clock_realtime_settime(timespec_t *ts)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock);
437c478bd9Sstevel@tonic-gate tod_set(*ts);
447c478bd9Sstevel@tonic-gate set_hrestime(ts);
457c478bd9Sstevel@tonic-gate mutex_exit(&tod_lock);
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate return (0);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * We normally won't execute this path; libc will see CLOCK_REALTIME and
527c478bd9Sstevel@tonic-gate * fast trap directly into gethrestime().
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate static int
clock_realtime_gettime(timespec_t * ts)557c478bd9Sstevel@tonic-gate clock_realtime_gettime(timespec_t *ts)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate gethrestime(ts);
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate return (0);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static int
clock_realtime_getres(timespec_t * ts)637c478bd9Sstevel@tonic-gate clock_realtime_getres(timespec_t *ts)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate ts->tv_sec = 0;
667c478bd9Sstevel@tonic-gate ts->tv_nsec = nsec_per_tick;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate return (0);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static void
clock_realtime_fire(void * arg)727c478bd9Sstevel@tonic-gate clock_realtime_fire(void *arg)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate int cnt2nth;
757c478bd9Sstevel@tonic-gate itimer_t *it = (itimer_t *)arg;
767c478bd9Sstevel@tonic-gate timeout_id_t *tidp = it->it_arg;
777c478bd9Sstevel@tonic-gate timespec_t now, interval2nth;
787c478bd9Sstevel@tonic-gate timespec_t *val, *interval;
797c478bd9Sstevel@tonic-gate proc_t *p = it->it_proc;
807c478bd9Sstevel@tonic-gate clock_t ticks;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * First call into the timer subsystem to get the signal going.
847c478bd9Sstevel@tonic-gate */
85*6a72db4aSBryan Cantrill it->it_fire(it);
867c478bd9Sstevel@tonic-gate val = &it->it_itime.it_value;
877c478bd9Sstevel@tonic-gate interval = &it->it_itime.it_interval;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate if (!timerspecisset(interval)) {
927c478bd9Sstevel@tonic-gate timerspecclear(val);
937c478bd9Sstevel@tonic-gate *tidp = 0;
947c478bd9Sstevel@tonic-gate } else {
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * If this is an interval timer, we need to determine a time
977c478bd9Sstevel@tonic-gate * at which to go off in the future. In the event that the
987c478bd9Sstevel@tonic-gate * clock has been adjusted, we want to find our new interval
997c478bd9Sstevel@tonic-gate * relatively quickly (and we don't want to simply take the
1007c478bd9Sstevel@tonic-gate * current time and add the interval; it would lead to
1017c478bd9Sstevel@tonic-gate * unnecessary jitter in the timer). We therefore take steps
1027c478bd9Sstevel@tonic-gate * from the time we expected to go off into the future;
1037c478bd9Sstevel@tonic-gate * if the resulting time is still in the past, then we double
1047c478bd9Sstevel@tonic-gate * our step size and continue. Once the resulting time is
1057c478bd9Sstevel@tonic-gate * in the future, we subtract our last step, change our step
1067c478bd9Sstevel@tonic-gate * size back to the original interval, and repeat until we
1077c478bd9Sstevel@tonic-gate * can get to a valid, future timeout in one step. This
1087c478bd9Sstevel@tonic-gate * assures that we will get the minimum, valid timeout
1097c478bd9Sstevel@tonic-gate * value in a reasonable amount of wall time.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate for (;;) {
1127c478bd9Sstevel@tonic-gate interval2nth = *interval;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * We put a floor on interval2nth at nsec_per_tick.
1167c478bd9Sstevel@tonic-gate * If we don't do this, and the interval is shorter
1177c478bd9Sstevel@tonic-gate * than the time required to run through this logic,
1187c478bd9Sstevel@tonic-gate * we'll never catch up to the current time (which
1197c478bd9Sstevel@tonic-gate * is a moving target).
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate if (interval2nth.tv_sec == 0 &&
1227c478bd9Sstevel@tonic-gate interval2nth.tv_nsec < nsec_per_tick)
1237c478bd9Sstevel@tonic-gate interval2nth.tv_nsec = nsec_per_tick;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate for (cnt2nth = 0; ; cnt2nth++) {
1267c478bd9Sstevel@tonic-gate timespecadd(val, &interval2nth);
1277c478bd9Sstevel@tonic-gate gethrestime(&now);
1287c478bd9Sstevel@tonic-gate if (timerspeccmp(val, &now) > 0)
1297c478bd9Sstevel@tonic-gate break;
1307c478bd9Sstevel@tonic-gate timespecadd(&interval2nth, &interval2nth);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate if (cnt2nth == 0)
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate timespecsub(val, &interval2nth);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate ticks = timespectohz(val, now);
1387c478bd9Sstevel@tonic-gate *tidp = realtime_timeout(clock_realtime_fire, it, ticks);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * See the block comment in clock_realtime_timer_settime(), below.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate static void
clock_realtime_fire_first(void * arg)1477c478bd9Sstevel@tonic-gate clock_realtime_fire_first(void *arg)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate itimer_t *it = (itimer_t *)arg;
1507c478bd9Sstevel@tonic-gate timespec_t now;
1517c478bd9Sstevel@tonic-gate timespec_t *val = &it->it_itime.it_value;
1527c478bd9Sstevel@tonic-gate timeout_id_t *tidp = it->it_arg;
1537c478bd9Sstevel@tonic-gate proc_t *p = it->it_proc;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate gethrestime(&now);
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if ((val->tv_sec > now.tv_sec) ||
1587c478bd9Sstevel@tonic-gate (val->tv_sec == now.tv_sec && val->tv_nsec > now.tv_nsec)) {
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate * We went off too early. We'll go to bed for one more tick,
1617c478bd9Sstevel@tonic-gate * regardless of the actual difference; if the difference
1627c478bd9Sstevel@tonic-gate * is greater than one tick, then we must have seen an adjtime.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
1657c478bd9Sstevel@tonic-gate *tidp = realtime_timeout(clock_realtime_fire, it, 1);
1667c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1677c478bd9Sstevel@tonic-gate return;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate clock_realtime_fire(arg);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1747c478bd9Sstevel@tonic-gate static int
clock_realtime_timer_create(itimer_t * it,void (* fire)(itimer_t *))175*6a72db4aSBryan Cantrill clock_realtime_timer_create(itimer_t *it, void (*fire)(itimer_t *))
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate it->it_arg = kmem_zalloc(sizeof (timeout_id_t), KM_SLEEP);
178*6a72db4aSBryan Cantrill it->it_fire = fire;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate return (0);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate static int
clock_realtime_timer_settime(itimer_t * it,int flags,const struct itimerspec * when)1847c478bd9Sstevel@tonic-gate clock_realtime_timer_settime(itimer_t *it, int flags,
1857c478bd9Sstevel@tonic-gate const struct itimerspec *when)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate timeout_id_t tid, *tidp = it->it_arg;
1887c478bd9Sstevel@tonic-gate timespec_t now;
189*6a72db4aSBryan Cantrill proc_t *p = it->it_proc;
1907c478bd9Sstevel@tonic-gate clock_t ticks;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate gethrestime(&now);
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate while ((tid = *tidp) != 0) {
1977c478bd9Sstevel@tonic-gate *tidp = 0;
1987c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1997c478bd9Sstevel@tonic-gate (void) untimeout(tid);
2007c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate * The timeout has been removed; it is safe to update it_itime.
2057c478bd9Sstevel@tonic-gate */
2067c478bd9Sstevel@tonic-gate it->it_itime = *when;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate if (timerspecisset(&it->it_itime.it_value)) {
2097c478bd9Sstevel@tonic-gate if (!(flags & TIMER_ABSTIME))
2107c478bd9Sstevel@tonic-gate timespecadd(&it->it_itime.it_value, &now);
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate ticks = timespectohz(&it->it_itime.it_value, now);
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate * gethrestime() works by reading hres_last_tick, and
2167c478bd9Sstevel@tonic-gate * adding in the current time delta (that is, the amount of
2177c478bd9Sstevel@tonic-gate * time which has passed since the last tick of the clock).
2187c478bd9Sstevel@tonic-gate * As a result, the time returned in "now", above, represents
2197c478bd9Sstevel@tonic-gate * an hrestime sometime after lbolt was last bumped.
2207c478bd9Sstevel@tonic-gate * The "ticks" we've been returned from timespectohz(), then,
2217c478bd9Sstevel@tonic-gate * reflects the number of times the clock will tick between
2227c478bd9Sstevel@tonic-gate * "now" and our desired execution time.
2237c478bd9Sstevel@tonic-gate *
2247c478bd9Sstevel@tonic-gate * However, when we call into realtime_timeout(), below,
2257c478bd9Sstevel@tonic-gate * "ticks" will be interpreted against lbolt. That is,
2267c478bd9Sstevel@tonic-gate * if we specify 1 tick, we will be registering a callout
2277c478bd9Sstevel@tonic-gate * for the next tick of the clock -- which may occur in
2287c478bd9Sstevel@tonic-gate * less than (1 / hz) seconds. More generally, we are
2297c478bd9Sstevel@tonic-gate * registering a callout for "ticks" of the clock, which
2307c478bd9Sstevel@tonic-gate * may be less than ("ticks" / hz) seconds (but not more than
2317c478bd9Sstevel@tonic-gate * (1 / hz) seconds less). In other words, we may go off
2327c478bd9Sstevel@tonic-gate * early.
2337c478bd9Sstevel@tonic-gate *
2347c478bd9Sstevel@tonic-gate * This is only a problem for the initial firing of the
2357c478bd9Sstevel@tonic-gate * timer, so we have the initial firing go through a
2367c478bd9Sstevel@tonic-gate * different handler which implements a nanosleep-esque
2377c478bd9Sstevel@tonic-gate * algorithm.
2387c478bd9Sstevel@tonic-gate */
2397c478bd9Sstevel@tonic-gate *tidp = realtime_timeout(clock_realtime_fire_first, it, ticks);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate return (0);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate static int
clock_realtime_timer_gettime(itimer_t * it,struct itimerspec * when)2487c478bd9Sstevel@tonic-gate clock_realtime_timer_gettime(itimer_t *it, struct itimerspec *when)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate timespec_t now;
251*6a72db4aSBryan Cantrill proc_t *p = it->it_proc;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate * We always keep it_itime up to date, so we just need to snapshot
2557c478bd9Sstevel@tonic-gate * the time under p_lock, and clean it up.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2587c478bd9Sstevel@tonic-gate gethrestime(&now);
2597c478bd9Sstevel@tonic-gate *when = it->it_itime;
2607c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate if (!timerspecisset(&when->it_value))
2637c478bd9Sstevel@tonic-gate return (0);
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate if (timerspeccmp(&when->it_value, &now) < 0) {
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * If this timer should have already gone off, set it_value
2687c478bd9Sstevel@tonic-gate * to 0.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate timerspecclear(&when->it_value);
2717c478bd9Sstevel@tonic-gate } else {
2727c478bd9Sstevel@tonic-gate timespecsub(&when->it_value, &now);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate return (0);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate static int
clock_realtime_timer_delete(itimer_t * it)2797c478bd9Sstevel@tonic-gate clock_realtime_timer_delete(itimer_t *it)
2807c478bd9Sstevel@tonic-gate {
281*6a72db4aSBryan Cantrill proc_t *p = it->it_proc;
2827c478bd9Sstevel@tonic-gate timeout_id_t tid, *tidp = it->it_arg;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate while ((tid = *tidp) != 0) {
2877c478bd9Sstevel@tonic-gate *tidp = 0;
2887c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2897c478bd9Sstevel@tonic-gate (void) untimeout(tid);
2907c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate kmem_free(tidp, sizeof (timeout_id_t));
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate return (0);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3017c478bd9Sstevel@tonic-gate void
clock_realtime_timer_lwpbind(itimer_t * it)3027c478bd9Sstevel@tonic-gate clock_realtime_timer_lwpbind(itimer_t *it)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate void
clock_realtime_init()3077c478bd9Sstevel@tonic-gate clock_realtime_init()
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate clock_backend_t *be = &clock_realtime;
3107c478bd9Sstevel@tonic-gate struct sigevent *ev = &be->clk_default;
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate ev->sigev_signo = SIGALRM;
3137c478bd9Sstevel@tonic-gate ev->sigev_notify = SIGEV_SIGNAL;
3147c478bd9Sstevel@tonic-gate ev->sigev_value.sival_ptr = NULL;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate be->clk_clock_settime = clock_realtime_settime;
3177c478bd9Sstevel@tonic-gate be->clk_clock_gettime = clock_realtime_gettime;
3187c478bd9Sstevel@tonic-gate be->clk_clock_getres = clock_realtime_getres;
3197c478bd9Sstevel@tonic-gate be->clk_timer_gettime = clock_realtime_timer_gettime;
3207c478bd9Sstevel@tonic-gate be->clk_timer_settime = clock_realtime_timer_settime;
3217c478bd9Sstevel@tonic-gate be->clk_timer_delete = clock_realtime_timer_delete;
3227c478bd9Sstevel@tonic-gate be->clk_timer_lwpbind = clock_realtime_timer_lwpbind;
3237c478bd9Sstevel@tonic-gate be->clk_timer_create = clock_realtime_timer_create;
3247c478bd9Sstevel@tonic-gate clock_add_backend(CLOCK_REALTIME, &clock_realtime);
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * For binary compatibility with old statically linked
3277c478bd9Sstevel@tonic-gate * applications, we make the behavior of __CLOCK_REALTIME0
3287c478bd9Sstevel@tonic-gate * the same as CLOCK_REALTIME.
3297c478bd9Sstevel@tonic-gate */
3307c478bd9Sstevel@tonic-gate clock_add_backend(__CLOCK_REALTIME0, &clock_realtime);
3317c478bd9Sstevel@tonic-gate }
332