5042 stop using deprecated atomic functions
[illumos-gate.git] / usr / src / uts / common / os / clock_highres.c
blobbcdf20c0bdf50dcccd06bf8c60c9cc88efe56426
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012, Joyent Inc. All rights reserved.
31 #include <sys/timer.h>
32 #include <sys/systm.h>
33 #include <sys/param.h>
34 #include <sys/kmem.h>
35 #include <sys/debug.h>
36 #include <sys/cyclic.h>
37 #include <sys/cmn_err.h>
38 #include <sys/pset.h>
39 #include <sys/atomic.h>
40 #include <sys/policy.h>
42 static clock_backend_t clock_highres;
44 /*ARGSUSED*/
45 static int
46 clock_highres_settime(timespec_t *ts)
48 return (EINVAL);
51 static int
52 clock_highres_gettime(timespec_t *ts)
54 hrt2ts(gethrtime(), (timestruc_t *)ts);
56 return (0);
59 static int
60 clock_highres_getres(timespec_t *ts)
62 hrt2ts(cyclic_getres(), (timestruc_t *)ts);
64 return (0);
67 /*ARGSUSED*/
68 static int
69 clock_highres_timer_create(itimer_t *it, struct sigevent *ev)
72 * CLOCK_HIGHRES timers of sufficiently high resolution can deny
73 * service; only allow privileged users to create such timers.
74 * Sites that do not wish to have this restriction should
75 * give users the "proc_clock_highres" privilege.
77 if (secpolicy_clock_highres(CRED()) != 0) {
78 it->it_arg = NULL;
79 return (EPERM);
82 it->it_arg = kmem_zalloc(sizeof (cyclic_id_t), KM_SLEEP);
84 return (0);
87 static void
88 clock_highres_fire(void *arg)
90 itimer_t *it = (itimer_t *)arg;
91 hrtime_t *addr = &it->it_hrtime;
92 hrtime_t old = *addr, new = gethrtime();
94 do {
95 old = *addr;
96 } while (atomic_cas_64((uint64_t *)addr, old, new) != old);
98 timer_fire(it);
101 static int
102 clock_highres_timer_settime(itimer_t *it, int flags,
103 const struct itimerspec *when)
105 cyclic_id_t cyc, *cycp = it->it_arg;
106 proc_t *p = curproc;
107 kthread_t *t = curthread;
108 cyc_time_t cyctime;
109 cyc_handler_t hdlr;
110 cpu_t *cpu;
111 cpupart_t *cpupart;
112 int pset;
114 cyctime.cyt_when = ts2hrt(&when->it_value);
115 cyctime.cyt_interval = ts2hrt(&when->it_interval);
117 if (cyctime.cyt_when != 0 && cyctime.cyt_interval == 0 &&
118 it->it_itime.it_interval.tv_sec == 0 &&
119 it->it_itime.it_interval.tv_nsec == 0 &&
120 (cyc = *cycp) != CYCLIC_NONE) {
122 * If our existing timer is a one-shot and our new timer is a
123 * one-shot, we'll save ourselves a world of grief and just
124 * reprogram the cyclic.
126 it->it_itime = *when;
128 if (!(flags & TIMER_ABSTIME))
129 cyctime.cyt_when += gethrtime();
131 hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
132 (void) cyclic_reprogram(cyc, cyctime.cyt_when);
133 return (0);
136 mutex_enter(&cpu_lock);
137 if ((cyc = *cycp) != CYCLIC_NONE) {
138 cyclic_remove(cyc);
139 *cycp = CYCLIC_NONE;
142 if (cyctime.cyt_when == 0) {
143 mutex_exit(&cpu_lock);
144 return (0);
147 if (!(flags & TIMER_ABSTIME))
148 cyctime.cyt_when += gethrtime();
151 * Now we will check for overflow (that is, we will check to see
152 * that the start time plus the interval time doesn't exceed
153 * INT64_MAX). The astute code reviewer will observe that this
154 * one-time check doesn't guarantee that a future expiration
155 * will not wrap. We wish to prove, then, that if a future
156 * expiration does wrap, the earliest the problem can be encountered
157 * is (INT64_MAX / 2) nanoseconds (191 years) after boot. Formally:
159 * Given: s + i < m s > 0 i > 0
160 * s + ni > m n > 1
162 * (where "s" is the start time, "i" is the interval, "n" is the
163 * number of times the cyclic has fired and "m" is INT64_MAX)
165 * Prove:
166 * (a) s + (n - 1)i > (m / 2)
167 * (b) s + (n - 1)i < m
169 * That is, prove that we must have fired at least once 191 years
170 * after boot. The proof is very straightforward; since the left
171 * side of (a) is minimized when i is small, it is sufficient to show
172 * that the statement is true for i's smallest possible value
173 * (((m - s) / n) + epsilon). The same goes for (b); showing that the
174 * statement is true for i's largest possible value (m - s + epsilon)
175 * is sufficient to prove the statement.
177 * The actual arithmetic manipulation is left up to reader.
179 if (cyctime.cyt_when > INT64_MAX - cyctime.cyt_interval) {
180 mutex_exit(&cpu_lock);
181 return (EOVERFLOW);
184 if (cyctime.cyt_interval == 0) {
186 * If this is a one-shot, then we set the interval to be
187 * inifinite. If this timer is never touched, this cyclic will
188 * simply consume space in the cyclic subsystem. As soon as
189 * timer_settime() or timer_delete() is called, the cyclic is
190 * removed (so it's not possible to run the machine out
191 * of resources by creating one-shots).
193 cyctime.cyt_interval = CY_INFINITY;
196 it->it_itime = *when;
198 hrt2ts(cyctime.cyt_when, &it->it_itime.it_value);
200 hdlr.cyh_func = (cyc_func_t)clock_highres_fire;
201 hdlr.cyh_arg = it;
202 hdlr.cyh_level = CY_LOW_LEVEL;
204 if (cyctime.cyt_when != 0)
205 *cycp = cyc = cyclic_add(&hdlr, &cyctime);
208 * Now that we have the cyclic created, we need to bind it to our
209 * bound CPU and processor set (if any).
211 mutex_enter(&p->p_lock);
212 cpu = t->t_bound_cpu;
213 cpupart = t->t_cpupart;
214 pset = t->t_bind_pset;
216 mutex_exit(&p->p_lock);
218 cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
220 mutex_exit(&cpu_lock);
222 return (0);
225 static int
226 clock_highres_timer_gettime(itimer_t *it, struct itimerspec *when)
229 * CLOCK_HIGHRES doesn't update it_itime.
231 hrtime_t start = ts2hrt(&it->it_itime.it_value);
232 hrtime_t interval = ts2hrt(&it->it_itime.it_interval);
233 hrtime_t diff, now = gethrtime();
234 hrtime_t *addr = &it->it_hrtime;
235 hrtime_t last;
238 * We're using atomic_cas_64() here only to assure that we slurp the
239 * entire timestamp atomically.
241 last = atomic_cas_64((uint64_t *)addr, 0, 0);
243 *when = it->it_itime;
245 if (!timerspecisset(&when->it_value))
246 return (0);
248 if (start > now) {
250 * We haven't gone off yet...
252 diff = start - now;
253 } else {
254 if (interval == 0) {
256 * This is a one-shot which should have already
257 * fired; set it_value to 0.
259 timerspecclear(&when->it_value);
260 return (0);
264 * Calculate how far we are into this interval.
266 diff = (now - start) % interval;
269 * Now check to see if we've dealt with the last interval
270 * yet.
272 if (now - diff > last) {
274 * The last interval hasn't fired; set it_value to 0.
276 timerspecclear(&when->it_value);
277 return (0);
281 * The last interval _has_ fired; we can return the amount
282 * of time left in this interval.
284 diff = interval - diff;
287 hrt2ts(diff, &when->it_value);
289 return (0);
292 static int
293 clock_highres_timer_delete(itimer_t *it)
295 cyclic_id_t cyc;
297 if (it->it_arg == NULL) {
299 * This timer was never fully created; we must have failed
300 * in the clock_highres_timer_create() routine.
302 return (0);
305 mutex_enter(&cpu_lock);
307 if ((cyc = *((cyclic_id_t *)it->it_arg)) != CYCLIC_NONE)
308 cyclic_remove(cyc);
310 mutex_exit(&cpu_lock);
312 kmem_free(it->it_arg, sizeof (cyclic_id_t));
314 return (0);
317 static void
318 clock_highres_timer_lwpbind(itimer_t *it)
320 proc_t *p = curproc;
321 kthread_t *t = curthread;
322 cyclic_id_t cyc = *((cyclic_id_t *)it->it_arg);
323 cpu_t *cpu;
324 cpupart_t *cpupart;
325 int pset;
327 if (cyc == CYCLIC_NONE)
328 return;
330 mutex_enter(&cpu_lock);
331 mutex_enter(&p->p_lock);
334 * Okay, now we can safely look at the bindings.
336 cpu = t->t_bound_cpu;
337 cpupart = t->t_cpupart;
338 pset = t->t_bind_pset;
341 * Now we drop p_lock. We haven't dropped cpu_lock; we're guaranteed
342 * that even if the bindings change, the CPU and/or processor set
343 * that this timer was bound to remain valid (and the combination
344 * remains self-consistent).
346 mutex_exit(&p->p_lock);
348 cyclic_bind(cyc, cpu, pset == PS_NONE ? NULL : cpupart);
350 mutex_exit(&cpu_lock);
353 void
354 clock_highres_init()
356 clock_backend_t *be = &clock_highres;
357 struct sigevent *ev = &be->clk_default;
359 ev->sigev_signo = SIGALRM;
360 ev->sigev_notify = SIGEV_SIGNAL;
361 ev->sigev_value.sival_ptr = NULL;
363 be->clk_clock_settime = clock_highres_settime;
364 be->clk_clock_gettime = clock_highres_gettime;
365 be->clk_clock_getres = clock_highres_getres;
366 be->clk_timer_create = clock_highres_timer_create;
367 be->clk_timer_gettime = clock_highres_timer_gettime;
368 be->clk_timer_settime = clock_highres_timer_settime;
369 be->clk_timer_delete = clock_highres_timer_delete;
370 be->clk_timer_lwpbind = clock_highres_timer_lwpbind;
372 clock_add_backend(CLOCK_HIGHRES, &clock_highres);