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 #include <sys/cyclic.h>
35*0Sstevel@tonic-gate #include <sys/cmn_err.h>
36*0Sstevel@tonic-gate #include <sys/pset.h>
37*0Sstevel@tonic-gate #include <sys/atomic.h>
38*0Sstevel@tonic-gate #include <sys/policy.h>
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate static clock_backend_t clock_highres;
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate /*ARGSUSED*/
43*0Sstevel@tonic-gate static int
clock_highres_settime(timespec_t * ts)44*0Sstevel@tonic-gate clock_highres_settime(timespec_t *ts)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate return (EINVAL);
47*0Sstevel@tonic-gate }
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate static int
clock_highres_gettime(timespec_t * ts)50*0Sstevel@tonic-gate clock_highres_gettime(timespec_t *ts)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate hrt2ts(gethrtime(), (timestruc_t *)ts);
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate return (0);
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate static int
clock_highres_getres(timespec_t * ts)58*0Sstevel@tonic-gate clock_highres_getres(timespec_t *ts)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate hrt2ts(cyclic_getres(), (timestruc_t *)ts);
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate return (0);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate /*ARGSUSED*/
66*0Sstevel@tonic-gate static int
clock_highres_timer_create(itimer_t * it,struct sigevent * ev)67*0Sstevel@tonic-gate clock_highres_timer_create(itimer_t *it, struct sigevent *ev)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate * CLOCK_HIGHRES timers of sufficiently high resolution can deny
71*0Sstevel@tonic-gate * service; only allow privileged users to create such timers.
72*0Sstevel@tonic-gate * Sites that do not wish to have this restriction should
73*0Sstevel@tonic-gate * give users the "proc_clock_highres" privilege.
74*0Sstevel@tonic-gate */
75*0Sstevel@tonic-gate if (secpolicy_clock_highres(CRED()) != 0) {
76*0Sstevel@tonic-gate it->it_arg = NULL;
77*0Sstevel@tonic-gate return (EPERM);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate it->it_arg = kmem_zalloc(sizeof (cyclic_id_t), KM_SLEEP);
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate return (0);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate static void
clock_highres_fire(void * arg)86*0Sstevel@tonic-gate clock_highres_fire(void *arg)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate itimer_t *it = (itimer_t *)arg;
89*0Sstevel@tonic-gate hrtime_t *addr = &it->it_hrtime;
90*0Sstevel@tonic-gate hrtime_t old = *addr, new = gethrtime();
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate do {
93*0Sstevel@tonic-gate old = *addr;
94*0Sstevel@tonic-gate } while (cas64((uint64_t *)addr, old, new) != old);
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate timer_fire(it);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate static int
clock_highres_timer_settime(itimer_t * it,int flags,const struct itimerspec * when)100*0Sstevel@tonic-gate clock_highres_timer_settime(itimer_t *it, int flags,
101*0Sstevel@tonic-gate const struct itimerspec *when)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate cyclic_id_t cyc, *cycp = it->it_arg;
104*0Sstevel@tonic-gate proc_t *p = curproc;
105*0Sstevel@tonic-gate kthread_t *t = curthread;
106*0Sstevel@tonic-gate cyc_time_t cyctime;
107*0Sstevel@tonic-gate cyc_handler_t hdlr;
108*0Sstevel@tonic-gate cpu_t *cpu;
109*0Sstevel@tonic-gate cpupart_t *cpupart;
110*0Sstevel@tonic-gate int pset;
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate cyctime.cyt_when = ts2hrt(&when->it_value);
113*0Sstevel@tonic-gate cyctime.cyt_interval = ts2hrt(&when->it_interval);
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate mutex_enter(&cpu_lock);
116*0Sstevel@tonic-gate if ((cyc = *cycp) != CYCLIC_NONE) {
117*0Sstevel@tonic-gate cyclic_remove(cyc);
118*0Sstevel@tonic-gate *cycp = CYCLIC_NONE;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate if (cyctime.cyt_when == 0) {
122*0Sstevel@tonic-gate mutex_exit(&cpu_lock);
123*0Sstevel@tonic-gate return (0);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if (!(flags & TIMER_ABSTIME))
127*0Sstevel@tonic-gate cyctime.cyt_when += gethrtime();
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate * Now we will check for overflow (that is, we will check to see
131*0Sstevel@tonic-gate * that the start time plus the interval time doesn't exceed
132*0Sstevel@tonic-gate * INT64_MAX). The astute code reviewer will observe that this
133*0Sstevel@tonic-gate * one-time check doesn't guarantee that a future expiration
134*0Sstevel@tonic-gate * will not wrap. We wish to prove, then, that if a future
135*0Sstevel@tonic-gate * expiration does wrap, the earliest the problem can be encountered
136*0Sstevel@tonic-gate * is (INT64_MAX / 2) nanoseconds (191 years) after boot. Formally:
137*0Sstevel@tonic-gate *
138*0Sstevel@tonic-gate * Given: s + i < m s > 0 i > 0
139*0Sstevel@tonic-gate * s + ni > m n > 1
140*0Sstevel@tonic-gate *
141*0Sstevel@tonic-gate * (where "s" is the start time, "i" is the interval, "n" is the
142*0Sstevel@tonic-gate * number of times the cyclic has fired and "m" is INT64_MAX)
143*0Sstevel@tonic-gate *
144*0Sstevel@tonic-gate * Prove:
145*0Sstevel@tonic-gate * (a) s + (n - 1)i > (m / 2)
146*0Sstevel@tonic-gate * (b) s + (n - 1)i < m
147*0Sstevel@tonic-gate *
148*0Sstevel@tonic-gate * That is, prove that we must have fired at least once 191 years
149*0Sstevel@tonic-gate * after boot. The proof is very straightforward; since the left
150*0Sstevel@tonic-gate * side of (a) is minimized when i is small, it is sufficient to show
151*0Sstevel@tonic-gate * that the statement is true for i's smallest possible value
152*0Sstevel@tonic-gate * (((m - s) / n) + epsilon). The same goes for (b); showing that the
153*0Sstevel@tonic-gate * statement is true for i's largest possible value (m - s + epsilon)
154*0Sstevel@tonic-gate * is sufficient to prove the statement.
155*0Sstevel@tonic-gate *
156*0Sstevel@tonic-gate * The actual arithmetic manipulation is left up to reader.
157*0Sstevel@tonic-gate */
158*0Sstevel@tonic-gate if (cyctime.cyt_when > INT64_MAX - cyctime.cyt_interval) {
159*0Sstevel@tonic-gate mutex_exit(&cpu_lock);
160*0Sstevel@tonic-gate return (EOVERFLOW);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate if (cyctime.cyt_interval == 0) {
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate * If this is a one-shot, then we set the interval to assure
166*0Sstevel@tonic-gate * that the cyclic will next fire INT64_MAX nanoseconds after
167*0Sstevel@tonic-gate * boot (which corresponds to over 292 years -- yes, Buck Rogers
168*0Sstevel@tonic-gate * may have his 292-year-uptime-Solaris box malfunction). If
169*0Sstevel@tonic-gate * this timer is never touched, this cyclic will simply
170*0Sstevel@tonic-gate * consume space in the cyclic subsystem. As soon as
171*0Sstevel@tonic-gate * timer_settime() or timer_delete() is called, the cyclic is
172*0Sstevel@tonic-gate * removed (so it's not possible to run the machine out
173*0Sstevel@tonic-gate * of resources by creating one-shots).
174*0Sstevel@tonic-gate */
175*0Sstevel@tonic-gate cyctime.cyt_interval = INT64_MAX - cyctime.cyt_when;
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate it->it_itime = *when;
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate hdlr.cyh_func = (cyc_func_t)clock_highres_fire;
183*0Sstevel@tonic-gate hdlr.cyh_arg = it;
184*0Sstevel@tonic-gate hdlr.cyh_level = CY_LOW_LEVEL;
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate if (cyctime.cyt_when != 0)
187*0Sstevel@tonic-gate *cycp = cyc = cyclic_add(&hdlr, &cyctime);
188*0Sstevel@tonic-gate else
189*0Sstevel@tonic-gate *cycp = cyc = CYCLIC_NONE;
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate * Now that we have the cyclic created, we need to bind it to our
193*0Sstevel@tonic-gate * bound CPU and processor set (if any).
194*0Sstevel@tonic-gate */
195*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
196*0Sstevel@tonic-gate cpu = t->t_bound_cpu;
197*0Sstevel@tonic-gate cpupart = t->t_cpupart;
198*0Sstevel@tonic-gate pset = t->t_bind_pset;
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate mutex_exit(&cpu_lock);
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate return (0);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate static int
clock_highres_timer_gettime(itimer_t * it,struct itimerspec * when)210*0Sstevel@tonic-gate clock_highres_timer_gettime(itimer_t *it, struct itimerspec *when)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate * CLOCK_HIGHRES doesn't update it_itime.
214*0Sstevel@tonic-gate */
215*0Sstevel@tonic-gate hrtime_t start = ts2hrt(&it->it_itime.it_value);
216*0Sstevel@tonic-gate hrtime_t interval = ts2hrt(&it->it_itime.it_interval);
217*0Sstevel@tonic-gate hrtime_t diff, now = gethrtime();
218*0Sstevel@tonic-gate hrtime_t *addr = &it->it_hrtime;
219*0Sstevel@tonic-gate hrtime_t last;
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate * We're using cas64() here only to assure that we slurp the entire
223*0Sstevel@tonic-gate * timestamp atomically.
224*0Sstevel@tonic-gate */
225*0Sstevel@tonic-gate last = cas64((uint64_t *)addr, 0, 0);
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate *when = it->it_itime;
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate if (!timerspecisset(&when->it_value))
230*0Sstevel@tonic-gate return (0);
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate if (start > now) {
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate * We haven't gone off yet...
235*0Sstevel@tonic-gate */
236*0Sstevel@tonic-gate diff = start - now;
237*0Sstevel@tonic-gate } else {
238*0Sstevel@tonic-gate if (interval == 0) {
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate * This is a one-shot which should have already
241*0Sstevel@tonic-gate * fired; set it_value to 0.
242*0Sstevel@tonic-gate */
243*0Sstevel@tonic-gate timerspecclear(&when->it_value);
244*0Sstevel@tonic-gate return (0);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate * Calculate how far we are into this interval.
249*0Sstevel@tonic-gate */
250*0Sstevel@tonic-gate diff = (now - start) % interval;
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate /*
253*0Sstevel@tonic-gate * Now check to see if we've dealt with the last interval
254*0Sstevel@tonic-gate * yet.
255*0Sstevel@tonic-gate */
256*0Sstevel@tonic-gate if (now - diff > last) {
257*0Sstevel@tonic-gate /*
258*0Sstevel@tonic-gate * The last interval hasn't fired; set it_value to 0.
259*0Sstevel@tonic-gate */
260*0Sstevel@tonic-gate timerspecclear(&when->it_value);
261*0Sstevel@tonic-gate return (0);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate * The last interval _has_ fired; we can return the amount
266*0Sstevel@tonic-gate * of time left in this interval.
267*0Sstevel@tonic-gate */
268*0Sstevel@tonic-gate diff = interval - diff;
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate hrt2ts(diff, &when->it_value);
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_highres_timer_delete(itimer_t * it)277*0Sstevel@tonic-gate clock_highres_timer_delete(itimer_t *it)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate cyclic_id_t cyc;
280*0Sstevel@tonic-gate
281*0Sstevel@tonic-gate if (it->it_arg == NULL) {
282*0Sstevel@tonic-gate /*
283*0Sstevel@tonic-gate * This timer was never fully created; we must have failed
284*0Sstevel@tonic-gate * in the clock_highres_timer_create() routine.
285*0Sstevel@tonic-gate */
286*0Sstevel@tonic-gate return (0);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate mutex_enter(&cpu_lock);
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate if ((cyc = *((cyclic_id_t *)it->it_arg)) != CYCLIC_NONE)
292*0Sstevel@tonic-gate cyclic_remove(cyc);
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate mutex_exit(&cpu_lock);
295*0Sstevel@tonic-gate
296*0Sstevel@tonic-gate kmem_free(it->it_arg, sizeof (cyclic_id_t));
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate return (0);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate static void
clock_highres_timer_lwpbind(itimer_t * it)302*0Sstevel@tonic-gate clock_highres_timer_lwpbind(itimer_t *it)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate proc_t *p = curproc;
305*0Sstevel@tonic-gate kthread_t *t = curthread;
306*0Sstevel@tonic-gate cyclic_id_t cyc = *((cyclic_id_t *)it->it_arg);
307*0Sstevel@tonic-gate cpu_t *cpu;
308*0Sstevel@tonic-gate cpupart_t *cpupart;
309*0Sstevel@tonic-gate int pset;
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate if (cyc == CYCLIC_NONE)
312*0Sstevel@tonic-gate return;
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate mutex_enter(&cpu_lock);
315*0Sstevel@tonic-gate mutex_enter(&p->p_lock);
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate /*
318*0Sstevel@tonic-gate * Okay, now we can safely look at the bindings.
319*0Sstevel@tonic-gate */
320*0Sstevel@tonic-gate cpu = t->t_bound_cpu;
321*0Sstevel@tonic-gate cpupart = t->t_cpupart;
322*0Sstevel@tonic-gate pset = t->t_bind_pset;
323*0Sstevel@tonic-gate
324*0Sstevel@tonic-gate /*
325*0Sstevel@tonic-gate * Now we drop p_lock. We haven't dropped cpu_lock; we're guaranteed
326*0Sstevel@tonic-gate * that even if the bindings change, the CPU and/or processor set
327*0Sstevel@tonic-gate * that this timer was bound to remain valid (and the combination
328*0Sstevel@tonic-gate * remains self-consistent).
329*0Sstevel@tonic-gate */
330*0Sstevel@tonic-gate mutex_exit(&p->p_lock);
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate mutex_exit(&cpu_lock);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate void
clock_highres_init()338*0Sstevel@tonic-gate clock_highres_init()
339*0Sstevel@tonic-gate {
340*0Sstevel@tonic-gate clock_backend_t *be = &clock_highres;
341*0Sstevel@tonic-gate struct sigevent *ev = &be->clk_default;
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gate ev->sigev_signo = SIGALRM;
344*0Sstevel@tonic-gate ev->sigev_notify = SIGEV_SIGNAL;
345*0Sstevel@tonic-gate ev->sigev_value.sival_ptr = NULL;
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate be->clk_clock_settime = clock_highres_settime;
348*0Sstevel@tonic-gate be->clk_clock_gettime = clock_highres_gettime;
349*0Sstevel@tonic-gate be->clk_clock_getres = clock_highres_getres;
350*0Sstevel@tonic-gate be->clk_timer_create = clock_highres_timer_create;
351*0Sstevel@tonic-gate be->clk_timer_gettime = clock_highres_timer_gettime;
352*0Sstevel@tonic-gate be->clk_timer_settime = clock_highres_timer_settime;
353*0Sstevel@tonic-gate be->clk_timer_delete = clock_highres_timer_delete;
354*0Sstevel@tonic-gate be->clk_timer_lwpbind = clock_highres_timer_lwpbind;
355*0Sstevel@tonic-gate
356*0Sstevel@tonic-gate clock_add_backend(CLOCK_HIGHRES, &clock_highres);
357*0Sstevel@tonic-gate }
358