1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/timer.h>
30*0Sstevel@tonic-gate #include <sys/systm.h>
31*0Sstevel@tonic-gate #include <sys/param.h>
32*0Sstevel@tonic-gate #include <sys/kmem.h>
33*0Sstevel@tonic-gate #include <sys/debug.h>
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate static clock_backend_t clock_realtime;
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate static int
clock_realtime_settime(timespec_t * ts)38*0Sstevel@tonic-gate clock_realtime_settime(timespec_t *ts)
39*0Sstevel@tonic-gate {
40*0Sstevel@tonic-gate mutex_enter(&tod_lock);
41*0Sstevel@tonic-gate tod_set(*ts);
42*0Sstevel@tonic-gate set_hrestime(ts);
43*0Sstevel@tonic-gate mutex_exit(&tod_lock);
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate return (0);
46*0Sstevel@tonic-gate }
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate * We normally won't execute this path; libc will see CLOCK_REALTIME and
50*0Sstevel@tonic-gate * fast trap directly into gethrestime().
51*0Sstevel@tonic-gate */
52*0Sstevel@tonic-gate static int
clock_realtime_gettime(timespec_t * ts)53*0Sstevel@tonic-gate clock_realtime_gettime(timespec_t *ts)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate gethrestime(ts);
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate return (0);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate static int
clock_realtime_getres(timespec_t * ts)61*0Sstevel@tonic-gate clock_realtime_getres(timespec_t *ts)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate ts->tv_sec = 0;
64*0Sstevel@tonic-gate ts->tv_nsec = nsec_per_tick;
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate return (0);
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate static void
clock_realtime_fire(void * arg)70*0Sstevel@tonic-gate clock_realtime_fire(void *arg)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate int cnt2nth;
73*0Sstevel@tonic-gate itimer_t *it = (itimer_t *)arg;
74*0Sstevel@tonic-gate timeout_id_t *tidp = it->it_arg;
75*0Sstevel@tonic-gate timespec_t now, interval2nth;
76*0Sstevel@tonic-gate timespec_t *val, *interval;
77*0Sstevel@tonic-gate proc_t *p = it->it_proc;
78*0Sstevel@tonic-gate clock_t ticks;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate * First call into the timer subsystem to get the signal going.
82*0Sstevel@tonic-gate */
83*0Sstevel@tonic-gate timer_fire(it);
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate val = &it->it_itime.it_value;
86*0Sstevel@tonic-gate interval = &it->it_itime.it_interval;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate if (!timerspecisset(interval)) {
91*0Sstevel@tonic-gate timerspecclear(val);
92*0Sstevel@tonic-gate *tidp = 0;
93*0Sstevel@tonic-gate } else {
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate * If this is an interval timer, we need to determine a time
96*0Sstevel@tonic-gate * at which to go off in the future. In the event that the
97*0Sstevel@tonic-gate * clock has been adjusted, we want to find our new interval
98*0Sstevel@tonic-gate * relatively quickly (and we don't want to simply take the
99*0Sstevel@tonic-gate * current time and add the interval; it would lead to
100*0Sstevel@tonic-gate * unnecessary jitter in the timer). We therefore take steps
101*0Sstevel@tonic-gate * from the time we expected to go off into the future;
102*0Sstevel@tonic-gate * if the resulting time is still in the past, then we double
103*0Sstevel@tonic-gate * our step size and continue. Once the resulting time is
104*0Sstevel@tonic-gate * in the future, we subtract our last step, change our step
105*0Sstevel@tonic-gate * size back to the original interval, and repeat until we
106*0Sstevel@tonic-gate * can get to a valid, future timeout in one step. This
107*0Sstevel@tonic-gate * assures that we will get the minimum, valid timeout
108*0Sstevel@tonic-gate * value in a reasonable amount of wall time.
109*0Sstevel@tonic-gate */
110*0Sstevel@tonic-gate for (;;) {
111*0Sstevel@tonic-gate interval2nth = *interval;
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate * We put a floor on interval2nth at nsec_per_tick.
115*0Sstevel@tonic-gate * If we don't do this, and the interval is shorter
116*0Sstevel@tonic-gate * than the time required to run through this logic,
117*0Sstevel@tonic-gate * we'll never catch up to the current time (which
118*0Sstevel@tonic-gate * is a moving target).
119*0Sstevel@tonic-gate */
120*0Sstevel@tonic-gate if (interval2nth.tv_sec == 0 &&
121*0Sstevel@tonic-gate interval2nth.tv_nsec < nsec_per_tick)
122*0Sstevel@tonic-gate interval2nth.tv_nsec = nsec_per_tick;
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate for (cnt2nth = 0; ; cnt2nth++) {
125*0Sstevel@tonic-gate timespecadd(val, &interval2nth);
126*0Sstevel@tonic-gate gethrestime(&now);
127*0Sstevel@tonic-gate if (timerspeccmp(val, &now) > 0)
128*0Sstevel@tonic-gate break;
129*0Sstevel@tonic-gate timespecadd(&interval2nth, &interval2nth);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate if (cnt2nth == 0)
132*0Sstevel@tonic-gate break;
133*0Sstevel@tonic-gate timespecsub(val, &interval2nth);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate ticks = timespectohz(val, now);
137*0Sstevel@tonic-gate *tidp = realtime_timeout(clock_realtime_fire, it, ticks);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate * See the block comment in clock_realtime_timer_settime(), below.
144*0Sstevel@tonic-gate */
145*0Sstevel@tonic-gate static void
clock_realtime_fire_first(void * arg)146*0Sstevel@tonic-gate clock_realtime_fire_first(void *arg)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate itimer_t *it = (itimer_t *)arg;
149*0Sstevel@tonic-gate timespec_t now;
150*0Sstevel@tonic-gate timespec_t *val = &it->it_itime.it_value;
151*0Sstevel@tonic-gate timeout_id_t *tidp = it->it_arg;
152*0Sstevel@tonic-gate proc_t *p = it->it_proc;
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate gethrestime(&now);
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate if ((val->tv_sec > now.tv_sec) ||
157*0Sstevel@tonic-gate (val->tv_sec == now.tv_sec && val->tv_nsec > now.tv_nsec)) {
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate * We went off too early. We'll go to bed for one more tick,
160*0Sstevel@tonic-gate * regardless of the actual difference; if the difference
161*0Sstevel@tonic-gate * is greater than one tick, then we must have seen an adjtime.
162*0Sstevel@tonic-gate */
163*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
164*0Sstevel@tonic-gate *tidp = realtime_timeout(clock_realtime_fire, it, 1);
165*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
166*0Sstevel@tonic-gate return;
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate clock_realtime_fire(arg);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate /*ARGSUSED*/
173*0Sstevel@tonic-gate static int
clock_realtime_timer_create(itimer_t * it,struct sigevent * ev)174*0Sstevel@tonic-gate clock_realtime_timer_create(itimer_t *it, struct sigevent *ev)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate it->it_arg = kmem_zalloc(sizeof (timeout_id_t), KM_SLEEP);
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate return (0);
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate static int
clock_realtime_timer_settime(itimer_t * it,int flags,const struct itimerspec * when)182*0Sstevel@tonic-gate clock_realtime_timer_settime(itimer_t *it, int flags,
183*0Sstevel@tonic-gate const struct itimerspec *when)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate timeout_id_t tid, *tidp = it->it_arg;
186*0Sstevel@tonic-gate timespec_t now;
187*0Sstevel@tonic-gate proc_t *p = curproc;
188*0Sstevel@tonic-gate clock_t ticks;
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate gethrestime(&now);
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate while ((tid = *tidp) != 0) {
195*0Sstevel@tonic-gate *tidp = 0;
196*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
197*0Sstevel@tonic-gate (void) untimeout(tid);
198*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate * The timeout has been removed; it is safe to update it_itime.
203*0Sstevel@tonic-gate */
204*0Sstevel@tonic-gate it->it_itime = *when;
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate if (timerspecisset(&it->it_itime.it_value)) {
207*0Sstevel@tonic-gate if (!(flags & TIMER_ABSTIME))
208*0Sstevel@tonic-gate timespecadd(&it->it_itime.it_value, &now);
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate ticks = timespectohz(&it->it_itime.it_value, now);
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate * gethrestime() works by reading hres_last_tick, and
214*0Sstevel@tonic-gate * adding in the current time delta (that is, the amount of
215*0Sstevel@tonic-gate * time which has passed since the last tick of the clock).
216*0Sstevel@tonic-gate * As a result, the time returned in "now", above, represents
217*0Sstevel@tonic-gate * an hrestime sometime after lbolt was last bumped.
218*0Sstevel@tonic-gate * The "ticks" we've been returned from timespectohz(), then,
219*0Sstevel@tonic-gate * reflects the number of times the clock will tick between
220*0Sstevel@tonic-gate * "now" and our desired execution time.
221*0Sstevel@tonic-gate *
222*0Sstevel@tonic-gate * However, when we call into realtime_timeout(), below,
223*0Sstevel@tonic-gate * "ticks" will be interpreted against lbolt. That is,
224*0Sstevel@tonic-gate * if we specify 1 tick, we will be registering a callout
225*0Sstevel@tonic-gate * for the next tick of the clock -- which may occur in
226*0Sstevel@tonic-gate * less than (1 / hz) seconds. More generally, we are
227*0Sstevel@tonic-gate * registering a callout for "ticks" of the clock, which
228*0Sstevel@tonic-gate * may be less than ("ticks" / hz) seconds (but not more than
229*0Sstevel@tonic-gate * (1 / hz) seconds less). In other words, we may go off
230*0Sstevel@tonic-gate * early.
231*0Sstevel@tonic-gate *
232*0Sstevel@tonic-gate * This is only a problem for the initial firing of the
233*0Sstevel@tonic-gate * timer, so we have the initial firing go through a
234*0Sstevel@tonic-gate * different handler which implements a nanosleep-esque
235*0Sstevel@tonic-gate * algorithm.
236*0Sstevel@tonic-gate */
237*0Sstevel@tonic-gate *tidp = realtime_timeout(clock_realtime_fire_first, it, ticks);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate return (0);
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate
245*0Sstevel@tonic-gate static int
clock_realtime_timer_gettime(itimer_t * it,struct itimerspec * when)246*0Sstevel@tonic-gate clock_realtime_timer_gettime(itimer_t *it, struct itimerspec *when)
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate timespec_t now;
249*0Sstevel@tonic-gate proc_t *p = curproc;
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate /*
252*0Sstevel@tonic-gate * We always keep it_itime up to date, so we just need to snapshot
253*0Sstevel@tonic-gate * the time under p_lock, and clean it up.
254*0Sstevel@tonic-gate */
255*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
256*0Sstevel@tonic-gate gethrestime(&now);
257*0Sstevel@tonic-gate *when = it->it_itime;
258*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate if (!timerspecisset(&when->it_value))
261*0Sstevel@tonic-gate return (0);
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate if (timerspeccmp(&when->it_value, &now) < 0) {
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate * If this timer should have already gone off, set it_value
266*0Sstevel@tonic-gate * to 0.
267*0Sstevel@tonic-gate */
268*0Sstevel@tonic-gate timerspecclear(&when->it_value);
269*0Sstevel@tonic-gate } else {
270*0Sstevel@tonic-gate timespecsub(&when->it_value, &now);
271*0Sstevel@tonic-gate }
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate return (0);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate static int
clock_realtime_timer_delete(itimer_t * it)277*0Sstevel@tonic-gate clock_realtime_timer_delete(itimer_t *it)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate proc_t *p = curproc;
280*0Sstevel@tonic-gate timeout_id_t tid, *tidp = it->it_arg;
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate while ((tid = *tidp) != 0) {
285*0Sstevel@tonic-gate *tidp = 0;
286*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
287*0Sstevel@tonic-gate (void) untimeout(tid);
288*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate kmem_free(tidp, sizeof (timeout_id_t));
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate return (0);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate /*ARGSUSED*/
299*0Sstevel@tonic-gate void
clock_realtime_timer_lwpbind(itimer_t * it)300*0Sstevel@tonic-gate clock_realtime_timer_lwpbind(itimer_t *it)
301*0Sstevel@tonic-gate {
302*0Sstevel@tonic-gate }
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate void
clock_realtime_init()305*0Sstevel@tonic-gate clock_realtime_init()
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate clock_backend_t *be = &clock_realtime;
308*0Sstevel@tonic-gate struct sigevent *ev = &be->clk_default;
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate ev->sigev_signo = SIGALRM;
311*0Sstevel@tonic-gate ev->sigev_notify = SIGEV_SIGNAL;
312*0Sstevel@tonic-gate ev->sigev_value.sival_ptr = NULL;
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate be->clk_clock_settime = clock_realtime_settime;
315*0Sstevel@tonic-gate be->clk_clock_gettime = clock_realtime_gettime;
316*0Sstevel@tonic-gate be->clk_clock_getres = clock_realtime_getres;
317*0Sstevel@tonic-gate be->clk_timer_gettime = clock_realtime_timer_gettime;
318*0Sstevel@tonic-gate be->clk_timer_settime = clock_realtime_timer_settime;
319*0Sstevel@tonic-gate be->clk_timer_delete = clock_realtime_timer_delete;
320*0Sstevel@tonic-gate be->clk_timer_lwpbind = clock_realtime_timer_lwpbind;
321*0Sstevel@tonic-gate be->clk_timer_create = clock_realtime_timer_create;
322*0Sstevel@tonic-gate clock_add_backend(CLOCK_REALTIME, &clock_realtime);
323*0Sstevel@tonic-gate /*
324*0Sstevel@tonic-gate * For binary compatibility with old statically linked
325*0Sstevel@tonic-gate * applications, we make the behavior of __CLOCK_REALTIME0
326*0Sstevel@tonic-gate * the same as CLOCK_REALTIME.
327*0Sstevel@tonic-gate */
328*0Sstevel@tonic-gate clock_add_backend(__CLOCK_REALTIME0, &clock_realtime);
329*0Sstevel@tonic-gate }
330