Annotation of src/sys/kern/kern_time.c, Revision 1.179.12.3
1.179.12.3! martin 1: /* $NetBSD: kern_time.c,v 1.179.12.2 2018/11/29 09:00:14 martin Exp $ */
1.42 cgd 2:
3: /*-
1.158 ad 4: * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009 The NetBSD Foundation, Inc.
1.42 cgd 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
1.158 ad 8: * by Christopher G. Demetriou, and by Andrew Doran.
1.42 cgd 9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
1.9 cgd 31:
1.1 cgd 32: /*
1.8 cgd 33: * Copyright (c) 1982, 1986, 1989, 1993
34: * The Regents of the University of California. All rights reserved.
1.1 cgd 35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
1.72 agc 44: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 45: * may be used to endorse or promote products derived from this software
46: * without specific prior written permission.
47: *
48: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58: * SUCH DAMAGE.
59: *
1.33 fvdl 60: * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
1.1 cgd 61: */
1.58 lukem 62:
63: #include <sys/cdefs.h>
1.179.12.3! martin 64: __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.179.12.2 2018/11/29 09:00:14 martin Exp $");
1.1 cgd 65:
1.5 mycroft 66: #include <sys/param.h>
67: #include <sys/resourcevar.h>
68: #include <sys/kernel.h>
1.8 cgd 69: #include <sys/systm.h>
1.5 mycroft 70: #include <sys/proc.h>
1.8 cgd 71: #include <sys/vnode.h>
1.17 christos 72: #include <sys/signalvar.h>
1.25 perry 73: #include <sys/syslog.h>
1.101 kardel 74: #include <sys/timetc.h>
1.143 ad 75: #include <sys/timex.h>
1.99 elad 76: #include <sys/kauth.h>
1.11 cgd 77: #include <sys/mount.h>
78: #include <sys/syscallargs.h>
1.143 ad 79: #include <sys/cpu.h>
1.19 christos 80:
1.142 ad 81: static void timer_intr(void *);
82: static void itimerfire(struct ptimer *);
83: static void itimerfree(struct ptimers *, int);
84:
85: kmutex_t timer_lock;
86:
87: static void *timer_sih;
88: static TAILQ_HEAD(, ptimer) timer_queue;
1.131 ad 89:
1.161 pooka 90: struct pool ptimer_pool, ptimers_pool;
1.97 simonb 91:
1.168 yamt 92: #define CLOCK_VIRTUAL_P(clockid) \
93: ((clockid) == CLOCK_VIRTUAL || (clockid) == CLOCK_PROF)
94:
95: CTASSERT(ITIMER_REAL == CLOCK_REALTIME);
96: CTASSERT(ITIMER_VIRTUAL == CLOCK_VIRTUAL);
97: CTASSERT(ITIMER_PROF == CLOCK_PROF);
1.170 christos 98: CTASSERT(ITIMER_MONOTONIC == CLOCK_MONOTONIC);
1.168 yamt 99:
1.131 ad 100: /*
101: * Initialize timekeeping.
102: */
103: void
104: time_init(void)
105: {
106:
1.161 pooka 107: pool_init(&ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
108: &pool_allocator_nointr, IPL_NONE);
109: pool_init(&ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
110: &pool_allocator_nointr, IPL_NONE);
1.131 ad 111: }
112:
1.142 ad 113: void
114: time_init2(void)
115: {
116:
117: TAILQ_INIT(&timer_queue);
118: mutex_init(&timer_lock, MUTEX_DEFAULT, IPL_SCHED);
119: timer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
120: timer_intr, NULL);
121: }
122:
1.63 thorpej 123: /* Time of day and interval timer support.
1.1 cgd 124: *
125: * These routines provide the kernel entry points to get and set
126: * the time-of-day and per-process interval timers. Subroutines
127: * here provide support for adding and subtracting timeval structures
128: * and decrementing interval timers, optionally reloading the interval
129: * timers when they expire.
130: */
131:
1.22 jtc 132: /* This function is used by clock_settime and settimeofday */
1.132 elad 133: static int
1.156 christos 134: settime1(struct proc *p, const struct timespec *ts, bool check_kauth)
1.22 jtc 135: {
1.156 christos 136: struct timespec delta, now;
1.129 ad 137: int s;
1.22 jtc 138:
139: /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
1.129 ad 140: s = splclock();
1.156 christos 141: nanotime(&now);
142: timespecsub(ts, &now, &delta);
1.132 elad 143:
1.134 elad 144: if (check_kauth && kauth_authorize_system(kauth_cred_get(),
1.156 christos 145: KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
146: &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
1.129 ad 147: splx(s);
1.29 tls 148: return (EPERM);
1.55 tron 149: }
1.132 elad 150:
1.29 tls 151: #ifdef notyet
1.109 elad 152: if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
1.129 ad 153: splx(s);
1.29 tls 154: return (EPERM);
1.55 tron 155: }
1.29 tls 156: #endif
1.103 kardel 157:
1.156 christos 158: tc_setclock(ts);
1.103 kardel 159:
1.156 christos 160: timespecadd(&boottime, &delta, &boottime);
1.103 kardel 161:
1.22 jtc 162: resettodr();
1.129 ad 163: splx(s);
164:
1.29 tls 165: return (0);
1.22 jtc 166: }
167:
1.132 elad 168: int
169: settime(struct proc *p, struct timespec *ts)
170: {
171: return (settime1(p, ts, true));
172: }
173:
1.22 jtc 174: /* ARGSUSED */
175: int
1.156 christos 176: sys___clock_gettime50(struct lwp *l,
177: const struct sys___clock_gettime50_args *uap, register_t *retval)
1.22 jtc 178: {
1.135 dsl 179: /* {
1.22 jtc 180: syscallarg(clockid_t) clock_id;
1.23 cgd 181: syscallarg(struct timespec *) tp;
1.135 dsl 182: } */
1.165 njoly 183: int error;
1.22 jtc 184: struct timespec ats;
185:
1.165 njoly 186: error = clock_gettime1(SCARG(uap, clock_id), &ats);
187: if (error != 0)
188: return error;
189:
190: return copyout(&ats, SCARG(uap, tp), sizeof(ats));
191: }
192:
1.22 jtc 193: /* ARGSUSED */
194: int
1.156 christos 195: sys___clock_settime50(struct lwp *l,
196: const struct sys___clock_settime50_args *uap, register_t *retval)
1.22 jtc 197: {
1.135 dsl 198: /* {
1.22 jtc 199: syscallarg(clockid_t) clock_id;
1.23 cgd 200: syscallarg(const struct timespec *) tp;
1.135 dsl 201: } */
1.156 christos 202: int error;
203: struct timespec ats;
1.22 jtc 204:
1.156 christos 205: if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
206: return error;
207:
208: return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
1.56 manu 209: }
210:
211:
212: int
1.132 elad 213: clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
214: bool check_kauth)
1.56 manu 215: {
216: int error;
217:
1.61 simonb 218: switch (clock_id) {
219: case CLOCK_REALTIME:
1.156 christos 220: if ((error = settime1(p, tp, check_kauth)) != 0)
1.61 simonb 221: return (error);
222: break;
223: case CLOCK_MONOTONIC:
224: return (EINVAL); /* read-only clock */
225: default:
1.56 manu 226: return (EINVAL);
1.61 simonb 227: }
1.22 jtc 228:
229: return 0;
230: }
231:
232: int
1.156 christos 233: sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
1.140 yamt 234: register_t *retval)
1.22 jtc 235: {
1.135 dsl 236: /* {
1.22 jtc 237: syscallarg(clockid_t) clock_id;
1.23 cgd 238: syscallarg(struct timespec *) tp;
1.135 dsl 239: } */
1.22 jtc 240: struct timespec ts;
241: int error = 0;
242:
1.164 njoly 243: if ((error = clock_getres1(SCARG(uap, clock_id), &ts)) != 0)
244: return error;
245:
246: if (SCARG(uap, tp))
247: error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
248:
249: return error;
250: }
251:
252: int
253: clock_getres1(clockid_t clock_id, struct timespec *ts)
254: {
255:
1.61 simonb 256: switch (clock_id) {
257: case CLOCK_REALTIME:
258: case CLOCK_MONOTONIC:
1.164 njoly 259: ts->tv_sec = 0;
1.102 kardel 260: if (tc_getfrequency() > 1000000000)
1.164 njoly 261: ts->tv_nsec = 1;
1.102 kardel 262: else
1.164 njoly 263: ts->tv_nsec = 1000000000 / tc_getfrequency();
1.61 simonb 264: break;
265: default:
1.164 njoly 266: return EINVAL;
1.61 simonb 267: }
1.22 jtc 268:
1.164 njoly 269: return 0;
1.22 jtc 270: }
271:
1.27 jtc 272: /* ARGSUSED */
273: int
1.156 christos 274: sys___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
1.140 yamt 275: register_t *retval)
1.27 jtc 276: {
1.135 dsl 277: /* {
1.101 kardel 278: syscallarg(struct timespec *) rqtp;
279: syscallarg(struct timespec *) rmtp;
1.135 dsl 280: } */
1.101 kardel 281: struct timespec rmt, rqt;
1.120 dsl 282: int error, error1;
1.101 kardel 283:
284: error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
285: if (error)
286: return (error);
287:
1.175 christos 288: error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
289: SCARG(uap, rmtp) ? &rmt : NULL);
290: if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
291: return error;
292:
293: error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
294: return error1 ? error1 : error;
295: }
296:
297: /* ARGSUSED */
298: int
299: sys_clock_nanosleep(struct lwp *l, const struct sys_clock_nanosleep_args *uap,
300: register_t *retval)
301: {
302: /* {
303: syscallarg(clockid_t) clock_id;
304: syscallarg(int) flags;
305: syscallarg(struct timespec *) rqtp;
306: syscallarg(struct timespec *) rmtp;
307: } */
308: struct timespec rmt, rqt;
309: int error, error1;
310:
311: error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
312: if (error)
313: return (error);
314:
315: error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), &rqt,
316: SCARG(uap, rmtp) ? &rmt : NULL);
1.120 dsl 317: if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
318: return error;
319:
320: error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
321: return error1 ? error1 : error;
322: }
323:
324: int
1.175 christos 325: nanosleep1(struct lwp *l, clockid_t clock_id, int flags, struct timespec *rqt,
326: struct timespec *rmt)
1.120 dsl 327: {
1.141 yamt 328: struct timespec rmtstart;
1.120 dsl 329: int error, timo;
330:
1.179.12.1 martin 331: if ((error = ts2timo(clock_id, flags, rqt, &timo, &rmtstart)) != 0) {
332: if (error == ETIMEDOUT) {
333: error = 0;
334: if (rmt != NULL)
335: rmt->tv_sec = rmt->tv_nsec = 0;
336: }
337: return error;
338: }
1.101 kardel 339:
340: /*
1.175 christos 341: * Avoid inadvertently sleeping forever
1.101 kardel 342: */
343: if (timo == 0)
344: timo = 1;
1.141 yamt 345: again:
346: error = kpause("nanoslp", true, timo, NULL);
347: if (rmt != NULL || error == 0) {
348: struct timespec rmtend;
349: struct timespec t0;
350: struct timespec *t;
1.101 kardel 351:
1.175 christos 352: (void)clock_gettime1(clock_id, &rmtend);
1.141 yamt 353: t = (rmt != NULL) ? rmt : &t0;
1.179 christos 354: if (flags & TIMER_ABSTIME) {
355: timespecsub(rqt, &rmtend, t);
356: } else {
357: timespecsub(&rmtend, &rmtstart, t);
358: timespecsub(rqt, t, t);
359: }
1.141 yamt 360: if (t->tv_sec < 0)
361: timespecclear(t);
362: if (error == 0) {
363: timo = tstohz(t);
364: if (timo > 0)
365: goto again;
366: }
367: }
1.104 kardel 368:
1.101 kardel 369: if (error == ERESTART)
370: error = EINTR;
371: if (error == EWOULDBLOCK)
372: error = 0;
373:
374: return error;
1.27 jtc 375: }
1.22 jtc 376:
1.1 cgd 377: /* ARGSUSED */
1.3 andrew 378: int
1.156 christos 379: sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
1.140 yamt 380: register_t *retval)
1.15 thorpej 381: {
1.135 dsl 382: /* {
1.11 cgd 383: syscallarg(struct timeval *) tp;
1.135 dsl 384: syscallarg(void *) tzp; really "struct timezone *";
385: } */
1.1 cgd 386: struct timeval atv;
387: int error = 0;
1.25 perry 388: struct timezone tzfake;
1.1 cgd 389:
1.11 cgd 390: if (SCARG(uap, tp)) {
1.1 cgd 391: microtime(&atv);
1.35 perry 392: error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
1.17 christos 393: if (error)
1.1 cgd 394: return (error);
395: }
1.25 perry 396: if (SCARG(uap, tzp)) {
397: /*
1.32 mycroft 398: * NetBSD has no kernel notion of time zone, so we just
1.25 perry 399: * fake up a timezone struct and return it if demanded.
400: */
401: tzfake.tz_minuteswest = 0;
402: tzfake.tz_dsttime = 0;
1.35 perry 403: error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
1.25 perry 404: }
1.1 cgd 405: return (error);
406: }
407:
408: /* ARGSUSED */
1.3 andrew 409: int
1.156 christos 410: sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
1.140 yamt 411: register_t *retval)
1.15 thorpej 412: {
1.135 dsl 413: /* {
1.24 cgd 414: syscallarg(const struct timeval *) tv;
1.140 yamt 415: syscallarg(const void *) tzp; really "const struct timezone *";
1.135 dsl 416: } */
1.60 manu 417:
1.119 dsl 418: return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
1.60 manu 419: }
420:
421: int
1.119 dsl 422: settimeofday1(const struct timeval *utv, bool userspace,
423: const void *utzp, struct lwp *l, bool check_kauth)
1.60 manu 424: {
1.22 jtc 425: struct timeval atv;
1.98 christos 426: struct timespec ts;
1.22 jtc 427: int error;
1.1 cgd 428:
1.8 cgd 429: /* Verify all parameters before changing time. */
1.119 dsl 430:
1.25 perry 431: /*
1.32 mycroft 432: * NetBSD has no kernel notion of time zone, and only an
1.25 perry 433: * obsolete program would try to set it, so we log a warning.
434: */
1.98 christos 435: if (utzp)
1.25 perry 436: log(LOG_WARNING, "pid %d attempted to set the "
1.119 dsl 437: "(obsolete) kernel time zone\n", l->l_proc->p_pid);
1.98 christos 438:
439: if (utv == NULL)
440: return 0;
441:
1.119 dsl 442: if (userspace) {
443: if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
444: return error;
445: utv = &atv;
446: }
447:
448: TIMEVAL_TO_TIMESPEC(utv, &ts);
1.133 elad 449: return settime1(l->l_proc, &ts, check_kauth);
1.1 cgd 450: }
451:
1.68 dsl 452: int time_adjusted; /* set if an adjustment is made */
1.1 cgd 453:
454: /* ARGSUSED */
1.3 andrew 455: int
1.156 christos 456: sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
1.140 yamt 457: register_t *retval)
1.15 thorpej 458: {
1.135 dsl 459: /* {
1.24 cgd 460: syscallarg(const struct timeval *) delta;
1.11 cgd 461: syscallarg(struct timeval *) olddelta;
1.135 dsl 462: } */
1.156 christos 463: int error = 0;
464: struct timeval atv, oldatv;
1.1 cgd 465:
1.106 elad 466: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
467: KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
1.156 christos 468: return error;
1.17 christos 469:
1.156 christos 470: if (SCARG(uap, delta)) {
471: error = copyin(SCARG(uap, delta), &atv,
472: sizeof(*SCARG(uap, delta)));
473: if (error)
474: return (error);
475: }
476: adjtime1(SCARG(uap, delta) ? &atv : NULL,
477: SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
478: if (SCARG(uap, olddelta))
479: error = copyout(&oldatv, SCARG(uap, olddelta),
480: sizeof(*SCARG(uap, olddelta)));
481: return error;
1.56 manu 482: }
483:
1.156 christos 484: void
1.110 yamt 485: adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
1.56 manu 486: {
1.101 kardel 487: extern int64_t time_adjtime; /* in kern_ntptime.c */
488:
489: if (olddelta) {
1.143 ad 490: mutex_spin_enter(&timecounter_lock);
1.156 christos 491: olddelta->tv_sec = time_adjtime / 1000000;
492: olddelta->tv_usec = time_adjtime % 1000000;
493: if (olddelta->tv_usec < 0) {
494: olddelta->tv_usec += 1000000;
495: olddelta->tv_sec--;
1.101 kardel 496: }
1.157 christos 497: mutex_spin_exit(&timecounter_lock);
1.101 kardel 498: }
499:
500: if (delta) {
1.156 christos 501: mutex_spin_enter(&timecounter_lock);
1.157 christos 502: time_adjtime = delta->tv_sec * 1000000 + delta->tv_usec;
1.101 kardel 503:
1.143 ad 504: if (time_adjtime) {
1.101 kardel 505: /* We need to save the system time during shutdown */
506: time_adjusted |= 1;
1.143 ad 507: }
508: mutex_spin_exit(&timecounter_lock);
1.101 kardel 509: }
1.1 cgd 510: }
511:
512: /*
1.63 thorpej 513: * Interval timer support. Both the BSD getitimer() family and the POSIX
514: * timer_*() family of routines are supported.
1.1 cgd 515: *
1.63 thorpej 516: * All timers are kept in an array pointed to by p_timers, which is
517: * allocated on demand - many processes don't use timers at all. The
518: * first three elements in this array are reserved for the BSD timers:
1.170 christos 519: * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, element
520: * 2 is ITIMER_PROF, and element 3 is ITIMER_MONOTONIC. The rest may be
521: * allocated by the timer_create() syscall.
1.1 cgd 522: *
1.63 thorpej 523: * Realtime timers are kept in the ptimer structure as an absolute
524: * time; virtual time timers are kept as a linked list of deltas.
1.1 cgd 525: * Virtual time timers are processed in the hardclock() routine of
1.63 thorpej 526: * kern_clock.c. The real time timer is processed by a callout
527: * routine, called from the softclock() routine. Since a callout may
528: * be delayed in real time due to interrupt processing in the system,
529: * it is possible for the real time timeout routine (realtimeexpire,
530: * given below), to be delayed in real time past when it is supposed
531: * to occur. It does not suffice, therefore, to reload the real timer
532: * .it_value from the real time timers .it_interval. Rather, we
533: * compute the next time in absolute time the timer should go off. */
534:
535: /* Allocate a POSIX realtime timer. */
536: int
1.140 yamt 537: sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
538: register_t *retval)
1.63 thorpej 539: {
1.135 dsl 540: /* {
1.63 thorpej 541: syscallarg(clockid_t) clock_id;
542: syscallarg(struct sigevent *) evp;
543: syscallarg(timer_t *) timerid;
1.135 dsl 544: } */
1.92 cube 545:
546: return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
1.105 ad 547: SCARG(uap, evp), copyin, l);
1.92 cube 548: }
549:
550: int
551: timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
1.105 ad 552: copyin_t fetch_event, struct lwp *l)
1.92 cube 553: {
554: int error;
555: timer_t timerid;
1.142 ad 556: struct ptimers *pts;
1.63 thorpej 557: struct ptimer *pt;
1.105 ad 558: struct proc *p;
559:
560: p = l->l_proc;
1.63 thorpej 561:
1.170 christos 562: if ((u_int)id > CLOCK_MONOTONIC)
1.63 thorpej 563: return (EINVAL);
564:
1.142 ad 565: if ((pts = p->p_timers) == NULL)
566: pts = timers_alloc(p);
1.63 thorpej 567:
568: pt = pool_get(&ptimer_pool, PR_WAITOK);
1.179.12.2 martin 569: memset(pt, 0, sizeof(*pt));
1.142 ad 570: if (evp != NULL) {
1.63 thorpej 571: if (((error =
1.92 cube 572: (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
1.63 thorpej 573: ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
1.163 drochner 574: (pt->pt_ev.sigev_notify > SIGEV_SA)) ||
575: (pt->pt_ev.sigev_notify == SIGEV_SIGNAL &&
576: (pt->pt_ev.sigev_signo <= 0 ||
577: pt->pt_ev.sigev_signo >= NSIG))) {
1.63 thorpej 578: pool_put(&ptimer_pool, pt);
579: return (error ? error : EINVAL);
580: }
1.142 ad 581: }
582:
583: /* Find a free timer slot, skipping those reserved for setitimer(). */
584: mutex_spin_enter(&timer_lock);
585: for (timerid = 3; timerid < TIMER_MAX; timerid++)
586: if (pts->pts_timers[timerid] == NULL)
587: break;
588: if (timerid == TIMER_MAX) {
589: mutex_spin_exit(&timer_lock);
590: pool_put(&ptimer_pool, pt);
591: return EAGAIN;
592: }
593: if (evp == NULL) {
1.63 thorpej 594: pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
595: switch (id) {
596: case CLOCK_REALTIME:
1.168 yamt 597: case CLOCK_MONOTONIC:
1.63 thorpej 598: pt->pt_ev.sigev_signo = SIGALRM;
599: break;
600: case CLOCK_VIRTUAL:
601: pt->pt_ev.sigev_signo = SIGVTALRM;
602: break;
603: case CLOCK_PROF:
604: pt->pt_ev.sigev_signo = SIGPROF;
605: break;
606: }
607: pt->pt_ev.sigev_value.sival_int = timerid;
608: }
1.73 christos 609: pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
610: pt->pt_info.ksi_errno = 0;
611: pt->pt_info.ksi_code = 0;
612: pt->pt_info.ksi_pid = p->p_pid;
1.105 ad 613: pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
1.124 christos 614: pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
1.63 thorpej 615: pt->pt_type = id;
616: pt->pt_proc = p;
617: pt->pt_overruns = 0;
618: pt->pt_poverruns = 0;
1.64 nathanw 619: pt->pt_entry = timerid;
1.142 ad 620: pt->pt_queued = false;
1.150 christos 621: timespecclear(&pt->pt_time.it_value);
1.168 yamt 622: if (!CLOCK_VIRTUAL_P(id))
623: callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
1.149 christos 624: else
625: pt->pt_active = 0;
626:
1.142 ad 627: pts->pts_timers[timerid] = pt;
628: mutex_spin_exit(&timer_lock);
1.63 thorpej 629:
1.92 cube 630: return copyout(&timerid, tid, sizeof(timerid));
1.63 thorpej 631: }
632:
633: /* Delete a POSIX realtime timer */
1.3 andrew 634: int
1.140 yamt 635: sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
636: register_t *retval)
1.15 thorpej 637: {
1.135 dsl 638: /* {
1.63 thorpej 639: syscallarg(timer_t) timerid;
1.135 dsl 640: } */
1.63 thorpej 641: struct proc *p = l->l_proc;
1.65 jdolecek 642: timer_t timerid;
1.142 ad 643: struct ptimers *pts;
1.63 thorpej 644: struct ptimer *pt, *ptn;
1.1 cgd 645:
1.63 thorpej 646: timerid = SCARG(uap, timerid);
1.142 ad 647: pts = p->p_timers;
648:
649: if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
650: return (EINVAL);
1.63 thorpej 651:
1.142 ad 652: mutex_spin_enter(&timer_lock);
653: if ((pt = pts->pts_timers[timerid]) == NULL) {
654: mutex_spin_exit(&timer_lock);
1.1 cgd 655: return (EINVAL);
1.142 ad 656: }
1.168 yamt 657: if (CLOCK_VIRTUAL_P(pt->pt_type)) {
1.149 christos 658: if (pt->pt_active) {
659: ptn = LIST_NEXT(pt, pt_list);
660: LIST_REMOVE(pt, pt_list);
661: for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
1.150 christos 662: timespecadd(&pt->pt_time.it_value,
1.149 christos 663: &ptn->pt_time.it_value,
664: &ptn->pt_time.it_value);
665: pt->pt_active = 0;
666: }
1.63 thorpej 667: }
1.142 ad 668: itimerfree(pts, timerid);
1.63 thorpej 669:
670: return (0);
671: }
672:
673: /*
1.67 nathanw 674: * Set up the given timer. The value in pt->pt_time.it_value is taken
1.168 yamt 675: * to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC timers and
676: * a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers.
1.63 thorpej 677: */
678: void
679: timer_settime(struct ptimer *pt)
680: {
681: struct ptimer *ptn, *pptn;
682: struct ptlist *ptl;
683:
1.142 ad 684: KASSERT(mutex_owned(&timer_lock));
685:
1.168 yamt 686: if (!CLOCK_VIRTUAL_P(pt->pt_type)) {
687: callout_halt(&pt->pt_ch, &timer_lock);
1.150 christos 688: if (timespecisset(&pt->pt_time.it_value)) {
1.63 thorpej 689: /*
1.150 christos 690: * Don't need to check tshzto() return value, here.
1.63 thorpej 691: * callout_reset() does it for us.
692: */
1.171 christos 693: callout_reset(&pt->pt_ch,
694: pt->pt_type == CLOCK_MONOTONIC ?
695: tshztoup(&pt->pt_time.it_value) :
696: tshzto(&pt->pt_time.it_value),
1.63 thorpej 697: realtimerexpire, pt);
698: }
699: } else {
700: if (pt->pt_active) {
701: ptn = LIST_NEXT(pt, pt_list);
702: LIST_REMOVE(pt, pt_list);
703: for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
1.150 christos 704: timespecadd(&pt->pt_time.it_value,
1.63 thorpej 705: &ptn->pt_time.it_value,
706: &ptn->pt_time.it_value);
707: }
1.150 christos 708: if (timespecisset(&pt->pt_time.it_value)) {
1.63 thorpej 709: if (pt->pt_type == CLOCK_VIRTUAL)
710: ptl = &pt->pt_proc->p_timers->pts_virtual;
711: else
712: ptl = &pt->pt_proc->p_timers->pts_prof;
713:
714: for (ptn = LIST_FIRST(ptl), pptn = NULL;
1.150 christos 715: ptn && timespeccmp(&pt->pt_time.it_value,
1.63 thorpej 716: &ptn->pt_time.it_value, >);
717: pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
1.150 christos 718: timespecsub(&pt->pt_time.it_value,
1.63 thorpej 719: &ptn->pt_time.it_value,
720: &pt->pt_time.it_value);
721:
722: if (pptn)
723: LIST_INSERT_AFTER(pptn, pt, pt_list);
724: else
725: LIST_INSERT_HEAD(ptl, pt, pt_list);
726:
727: for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
1.150 christos 728: timespecsub(&ptn->pt_time.it_value,
1.63 thorpej 729: &pt->pt_time.it_value,
730: &ptn->pt_time.it_value);
731:
732: pt->pt_active = 1;
733: } else
734: pt->pt_active = 0;
735: }
736: }
737:
738: void
1.150 christos 739: timer_gettime(struct ptimer *pt, struct itimerspec *aits)
1.63 thorpej 740: {
1.150 christos 741: struct timespec now;
1.63 thorpej 742: struct ptimer *ptn;
743:
1.142 ad 744: KASSERT(mutex_owned(&timer_lock));
745:
1.150 christos 746: *aits = pt->pt_time;
1.168 yamt 747: if (!CLOCK_VIRTUAL_P(pt->pt_type)) {
1.1 cgd 748: /*
1.12 mycroft 749: * Convert from absolute to relative time in .it_value
1.63 thorpej 750: * part of real time timer. If time for real time
751: * timer has passed return 0, else return difference
752: * between current time and time for the timer to go
753: * off.
1.1 cgd 754: */
1.150 christos 755: if (timespecisset(&aits->it_value)) {
1.168 yamt 756: if (pt->pt_type == CLOCK_REALTIME) {
757: getnanotime(&now);
758: } else { /* CLOCK_MONOTONIC */
759: getnanouptime(&now);
760: }
1.150 christos 761: if (timespeccmp(&aits->it_value, &now, <))
762: timespecclear(&aits->it_value);
1.101 kardel 763: else
1.150 christos 764: timespecsub(&aits->it_value, &now,
765: &aits->it_value);
1.36 thorpej 766: }
1.63 thorpej 767: } else if (pt->pt_active) {
768: if (pt->pt_type == CLOCK_VIRTUAL)
769: ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
770: else
771: ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
772: for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
1.150 christos 773: timespecadd(&aits->it_value,
774: &ptn->pt_time.it_value, &aits->it_value);
1.63 thorpej 775: KASSERT(ptn != NULL); /* pt should be findable on the list */
1.1 cgd 776: } else
1.150 christos 777: timespecclear(&aits->it_value);
1.63 thorpej 778: }
779:
780:
781:
782: /* Set and arm a POSIX realtime timer */
783: int
1.156 christos 784: sys___timer_settime50(struct lwp *l,
785: const struct sys___timer_settime50_args *uap,
1.140 yamt 786: register_t *retval)
1.63 thorpej 787: {
1.135 dsl 788: /* {
1.63 thorpej 789: syscallarg(timer_t) timerid;
790: syscallarg(int) flags;
791: syscallarg(const struct itimerspec *) value;
792: syscallarg(struct itimerspec *) ovalue;
1.135 dsl 793: } */
1.92 cube 794: int error;
795: struct itimerspec value, ovalue, *ovp = NULL;
796:
797: if ((error = copyin(SCARG(uap, value), &value,
798: sizeof(struct itimerspec))) != 0)
799: return (error);
800:
801: if (SCARG(uap, ovalue))
802: ovp = &ovalue;
803:
804: if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
805: SCARG(uap, flags), l->l_proc)) != 0)
806: return error;
807:
808: if (ovp)
809: return copyout(&ovalue, SCARG(uap, ovalue),
810: sizeof(struct itimerspec));
811: return 0;
812: }
813:
814: int
815: dotimer_settime(int timerid, struct itimerspec *value,
816: struct itimerspec *ovalue, int flags, struct proc *p)
817: {
1.150 christos 818: struct timespec now;
819: struct itimerspec val, oval;
1.142 ad 820: struct ptimers *pts;
1.63 thorpej 821: struct ptimer *pt;
1.160 christos 822: int error;
1.63 thorpej 823:
1.142 ad 824: pts = p->p_timers;
1.63 thorpej 825:
1.142 ad 826: if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
827: return EINVAL;
1.150 christos 828: val = *value;
1.160 christos 829: if ((error = itimespecfix(&val.it_value)) != 0 ||
830: (error = itimespecfix(&val.it_interval)) != 0)
831: return error;
1.63 thorpej 832:
1.142 ad 833: mutex_spin_enter(&timer_lock);
834: if ((pt = pts->pts_timers[timerid]) == NULL) {
835: mutex_spin_exit(&timer_lock);
1.150 christos 836: return EINVAL;
1.142 ad 837: }
838:
1.63 thorpej 839: oval = pt->pt_time;
840: pt->pt_time = val;
841:
1.67 nathanw 842: /*
843: * If we've been passed a relative time for a realtime timer,
844: * convert it to absolute; if an absolute time for a virtual
845: * timer, convert it to relative and make sure we don't set it
846: * to zero, which would cancel the timer, or let it go
847: * negative, which would confuse the comparison tests.
848: */
1.150 christos 849: if (timespecisset(&pt->pt_time.it_value)) {
1.168 yamt 850: if (!CLOCK_VIRTUAL_P(pt->pt_type)) {
1.101 kardel 851: if ((flags & TIMER_ABSTIME) == 0) {
1.168 yamt 852: if (pt->pt_type == CLOCK_REALTIME) {
853: getnanotime(&now);
854: } else { /* CLOCK_MONOTONIC */
855: getnanouptime(&now);
856: }
1.150 christos 857: timespecadd(&pt->pt_time.it_value, &now,
1.101 kardel 858: &pt->pt_time.it_value);
859: }
1.67 nathanw 860: } else {
1.92 cube 861: if ((flags & TIMER_ABSTIME) != 0) {
1.150 christos 862: getnanotime(&now);
863: timespecsub(&pt->pt_time.it_value, &now,
1.101 kardel 864: &pt->pt_time.it_value);
1.150 christos 865: if (!timespecisset(&pt->pt_time.it_value) ||
1.67 nathanw 866: pt->pt_time.it_value.tv_sec < 0) {
867: pt->pt_time.it_value.tv_sec = 0;
1.150 christos 868: pt->pt_time.it_value.tv_nsec = 1;
1.67 nathanw 869: }
870: }
871: }
872: }
873:
1.63 thorpej 874: timer_settime(pt);
1.142 ad 875: mutex_spin_exit(&timer_lock);
1.63 thorpej 876:
1.150 christos 877: if (ovalue)
878: *ovalue = oval;
1.63 thorpej 879:
880: return (0);
881: }
882:
883: /* Return the time remaining until a POSIX timer fires. */
884: int
1.156 christos 885: sys___timer_gettime50(struct lwp *l,
886: const struct sys___timer_gettime50_args *uap, register_t *retval)
1.63 thorpej 887: {
1.135 dsl 888: /* {
1.63 thorpej 889: syscallarg(timer_t) timerid;
890: syscallarg(struct itimerspec *) value;
1.135 dsl 891: } */
1.63 thorpej 892: struct itimerspec its;
1.92 cube 893: int error;
894:
895: if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
896: &its)) != 0)
897: return error;
898:
899: return copyout(&its, SCARG(uap, value), sizeof(its));
900: }
901:
902: int
903: dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
904: {
1.63 thorpej 905: struct ptimer *pt;
1.142 ad 906: struct ptimers *pts;
1.63 thorpej 907:
1.142 ad 908: pts = p->p_timers;
909: if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1.63 thorpej 910: return (EINVAL);
1.142 ad 911: mutex_spin_enter(&timer_lock);
912: if ((pt = pts->pts_timers[timerid]) == NULL) {
913: mutex_spin_exit(&timer_lock);
914: return (EINVAL);
915: }
1.150 christos 916: timer_gettime(pt, its);
1.142 ad 917: mutex_spin_exit(&timer_lock);
1.63 thorpej 918:
1.92 cube 919: return 0;
1.63 thorpej 920: }
921:
922: /*
923: * Return the count of the number of times a periodic timer expired
924: * while a notification was already pending. The counter is reset when
925: * a timer expires and a notification can be posted.
926: */
927: int
1.140 yamt 928: sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
929: register_t *retval)
1.63 thorpej 930: {
1.135 dsl 931: /* {
1.63 thorpej 932: syscallarg(timer_t) timerid;
1.135 dsl 933: } */
1.63 thorpej 934: struct proc *p = l->l_proc;
1.142 ad 935: struct ptimers *pts;
1.63 thorpej 936: int timerid;
937: struct ptimer *pt;
938:
939: timerid = SCARG(uap, timerid);
940:
1.142 ad 941: pts = p->p_timers;
942: if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
943: return (EINVAL);
944: mutex_spin_enter(&timer_lock);
945: if ((pt = pts->pts_timers[timerid]) == NULL) {
946: mutex_spin_exit(&timer_lock);
1.63 thorpej 947: return (EINVAL);
1.142 ad 948: }
1.63 thorpej 949: *retval = pt->pt_poverruns;
1.142 ad 950: mutex_spin_exit(&timer_lock);
1.63 thorpej 951:
952: return (0);
953: }
954:
955: /*
956: * Real interval timer expired:
957: * send process whose timer expired an alarm signal.
958: * If time is not set up to reload, then just return.
959: * Else compute next time timer should go off which is > current time.
960: * This is where delay in processing this timeout causes multiple
961: * SIGALRM calls to be compressed into one.
962: */
963: void
964: realtimerexpire(void *arg)
965: {
1.166 yamt 966: uint64_t last_val, next_val, interval, now_ns;
1.150 christos 967: struct timespec now, next;
1.63 thorpej 968: struct ptimer *pt;
1.148 joerg 969: int backwards;
1.63 thorpej 970:
1.142 ad 971: pt = arg;
1.63 thorpej 972:
1.142 ad 973: mutex_spin_enter(&timer_lock);
1.63 thorpej 974: itimerfire(pt);
975:
1.150 christos 976: if (!timespecisset(&pt->pt_time.it_interval)) {
977: timespecclear(&pt->pt_time.it_value);
1.142 ad 978: mutex_spin_exit(&timer_lock);
1.63 thorpej 979: return;
980: }
1.148 joerg 981:
1.171 christos 982: if (pt->pt_type == CLOCK_MONOTONIC) {
983: getnanouptime(&now);
984: } else {
985: getnanotime(&now);
986: }
1.150 christos 987: backwards = (timespeccmp(&pt->pt_time.it_value, &now, >));
988: timespecadd(&pt->pt_time.it_value, &pt->pt_time.it_interval, &next);
1.148 joerg 989: /* Handle the easy case of non-overflown timers first. */
1.150 christos 990: if (!backwards && timespeccmp(&next, &now, >)) {
1.148 joerg 991: pt->pt_time.it_value = next;
992: } else {
1.166 yamt 993: now_ns = timespec2ns(&now);
1.150 christos 994: last_val = timespec2ns(&pt->pt_time.it_value);
995: interval = timespec2ns(&pt->pt_time.it_interval);
1.148 joerg 996:
1.166 yamt 997: next_val = now_ns +
998: (now_ns - last_val + interval - 1) % interval;
1.148 joerg 999:
1000: if (backwards)
1001: next_val += interval;
1002: else
1.166 yamt 1003: pt->pt_overruns += (now_ns - last_val) / interval;
1.148 joerg 1004:
1.150 christos 1005: pt->pt_time.it_value.tv_sec = next_val / 1000000000;
1006: pt->pt_time.it_value.tv_nsec = next_val % 1000000000;
1.101 kardel 1007: }
1.148 joerg 1008:
1009: /*
1.150 christos 1010: * Don't need to check tshzto() return value, here.
1.148 joerg 1011: * callout_reset() does it for us.
1012: */
1.171 christos 1013: callout_reset(&pt->pt_ch, pt->pt_type == CLOCK_MONOTONIC ?
1014: tshztoup(&pt->pt_time.it_value) : tshzto(&pt->pt_time.it_value),
1.148 joerg 1015: realtimerexpire, pt);
1016: mutex_spin_exit(&timer_lock);
1.63 thorpej 1017: }
1018:
1019: /* BSD routine to get the value of an interval timer. */
1020: /* ARGSUSED */
1021: int
1.156 christos 1022: sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
1.140 yamt 1023: register_t *retval)
1.63 thorpej 1024: {
1.135 dsl 1025: /* {
1.63 thorpej 1026: syscallarg(int) which;
1027: syscallarg(struct itimerval *) itv;
1.135 dsl 1028: } */
1.63 thorpej 1029: struct proc *p = l->l_proc;
1030: struct itimerval aitv;
1.91 cube 1031: int error;
1032:
1033: error = dogetitimer(p, SCARG(uap, which), &aitv);
1034: if (error)
1035: return error;
1036: return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
1037: }
1.63 thorpej 1038:
1.91 cube 1039: int
1040: dogetitimer(struct proc *p, int which, struct itimerval *itvp)
1041: {
1.142 ad 1042: struct ptimers *pts;
1043: struct ptimer *pt;
1.150 christos 1044: struct itimerspec its;
1.63 thorpej 1045:
1.170 christos 1046: if ((u_int)which > ITIMER_MONOTONIC)
1.63 thorpej 1047: return (EINVAL);
1048:
1.142 ad 1049: mutex_spin_enter(&timer_lock);
1050: pts = p->p_timers;
1051: if (pts == NULL || (pt = pts->pts_timers[which]) == NULL) {
1.91 cube 1052: timerclear(&itvp->it_value);
1053: timerclear(&itvp->it_interval);
1.150 christos 1054: } else {
1055: timer_gettime(pt, &its);
1.151 christos 1056: TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
1057: TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
1.150 christos 1058: }
1.142 ad 1059: mutex_spin_exit(&timer_lock);
1.63 thorpej 1060:
1.91 cube 1061: return 0;
1.1 cgd 1062: }
1063:
1.63 thorpej 1064: /* BSD routine to set/arm an interval timer. */
1.1 cgd 1065: /* ARGSUSED */
1.3 andrew 1066: int
1.156 christos 1067: sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
1.140 yamt 1068: register_t *retval)
1.15 thorpej 1069: {
1.135 dsl 1070: /* {
1.30 mycroft 1071: syscallarg(int) which;
1.24 cgd 1072: syscallarg(const struct itimerval *) itv;
1.11 cgd 1073: syscallarg(struct itimerval *) oitv;
1.135 dsl 1074: } */
1.63 thorpej 1075: struct proc *p = l->l_proc;
1.30 mycroft 1076: int which = SCARG(uap, which);
1.156 christos 1077: struct sys___getitimer50_args getargs;
1.91 cube 1078: const struct itimerval *itvp;
1.1 cgd 1079: struct itimerval aitv;
1.91 cube 1080: int error;
1.1 cgd 1081:
1.170 christos 1082: if ((u_int)which > ITIMER_MONOTONIC)
1.1 cgd 1083: return (EINVAL);
1.11 cgd 1084: itvp = SCARG(uap, itv);
1.63 thorpej 1085: if (itvp &&
1.174 dholland 1086: (error = copyin(itvp, &aitv, sizeof(struct itimerval))) != 0)
1.1 cgd 1087: return (error);
1.21 cgd 1088: if (SCARG(uap, oitv) != NULL) {
1.30 mycroft 1089: SCARG(&getargs, which) = which;
1.21 cgd 1090: SCARG(&getargs, itv) = SCARG(uap, oitv);
1.156 christos 1091: if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
1.21 cgd 1092: return (error);
1093: }
1.1 cgd 1094: if (itvp == 0)
1095: return (0);
1.91 cube 1096:
1097: return dosetitimer(p, which, &aitv);
1098: }
1099:
1100: int
1101: dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1102: {
1.150 christos 1103: struct timespec now;
1.142 ad 1104: struct ptimers *pts;
1105: struct ptimer *pt, *spare;
1.91 cube 1106:
1.170 christos 1107: KASSERT((u_int)which <= CLOCK_MONOTONIC);
1.91 cube 1108: if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1.1 cgd 1109: return (EINVAL);
1.63 thorpej 1110:
1111: /*
1112: * Don't bother allocating data structures if the process just
1113: * wants to clear the timer.
1114: */
1.142 ad 1115: spare = NULL;
1116: pts = p->p_timers;
1117: retry:
1118: if (!timerisset(&itvp->it_value) && (pts == NULL ||
1119: pts->pts_timers[which] == NULL))
1.63 thorpej 1120: return (0);
1.142 ad 1121: if (pts == NULL)
1122: pts = timers_alloc(p);
1123: mutex_spin_enter(&timer_lock);
1124: pt = pts->pts_timers[which];
1125: if (pt == NULL) {
1126: if (spare == NULL) {
1127: mutex_spin_exit(&timer_lock);
1128: spare = pool_get(&ptimer_pool, PR_WAITOK);
1.179.12.3! martin 1129: memset(spare, 0, sizeof(*spare));
1.142 ad 1130: goto retry;
1131: }
1132: pt = spare;
1133: spare = NULL;
1.63 thorpej 1134: pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1.76 christos 1135: pt->pt_ev.sigev_value.sival_int = which;
1.63 thorpej 1136: pt->pt_overruns = 0;
1137: pt->pt_proc = p;
1138: pt->pt_type = which;
1.64 nathanw 1139: pt->pt_entry = which;
1.142 ad 1140: pt->pt_queued = false;
1.149 christos 1141: if (pt->pt_type == CLOCK_REALTIME)
1142: callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
1143: else
1144: pt->pt_active = 0;
1145:
1.63 thorpej 1146: switch (which) {
1147: case ITIMER_REAL:
1.170 christos 1148: case ITIMER_MONOTONIC:
1.63 thorpej 1149: pt->pt_ev.sigev_signo = SIGALRM;
1150: break;
1151: case ITIMER_VIRTUAL:
1152: pt->pt_ev.sigev_signo = SIGVTALRM;
1153: break;
1154: case ITIMER_PROF:
1155: pt->pt_ev.sigev_signo = SIGPROF;
1156: break;
1.1 cgd 1157: }
1.142 ad 1158: pts->pts_timers[which] = pt;
1159: }
1.63 thorpej 1160:
1.150 christos 1161: TIMEVAL_TO_TIMESPEC(&itvp->it_value, &pt->pt_time.it_value);
1162: TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &pt->pt_time.it_interval);
1163:
1.170 christos 1164: if (timespecisset(&pt->pt_time.it_value)) {
1.67 nathanw 1165: /* Convert to absolute time */
1.101 kardel 1166: /* XXX need to wrap in splclock for timecounters case? */
1.170 christos 1167: switch (which) {
1168: case ITIMER_REAL:
1169: getnanotime(&now);
1170: timespecadd(&pt->pt_time.it_value, &now,
1171: &pt->pt_time.it_value);
1172: break;
1173: case ITIMER_MONOTONIC:
1174: getnanouptime(&now);
1175: timespecadd(&pt->pt_time.it_value, &now,
1176: &pt->pt_time.it_value);
1177: break;
1178: default:
1179: break;
1180: }
1.67 nathanw 1181: }
1.63 thorpej 1182: timer_settime(pt);
1.142 ad 1183: mutex_spin_exit(&timer_lock);
1184: if (spare != NULL)
1185: pool_put(&ptimer_pool, spare);
1.63 thorpej 1186:
1.1 cgd 1187: return (0);
1188: }
1189:
1.63 thorpej 1190: /* Utility routines to manage the array of pointers to timers. */
1.142 ad 1191: struct ptimers *
1.63 thorpej 1192: timers_alloc(struct proc *p)
1193: {
1.142 ad 1194: struct ptimers *pts;
1.63 thorpej 1195: int i;
1196:
1.100 yamt 1197: pts = pool_get(&ptimers_pool, PR_WAITOK);
1.63 thorpej 1198: LIST_INIT(&pts->pts_virtual);
1199: LIST_INIT(&pts->pts_prof);
1200: for (i = 0; i < TIMER_MAX; i++)
1201: pts->pts_timers[i] = NULL;
1.64 nathanw 1202: pts->pts_fired = 0;
1.142 ad 1203: mutex_spin_enter(&timer_lock);
1204: if (p->p_timers == NULL) {
1205: p->p_timers = pts;
1206: mutex_spin_exit(&timer_lock);
1207: return pts;
1208: }
1209: mutex_spin_exit(&timer_lock);
1210: pool_put(&ptimers_pool, pts);
1211: return p->p_timers;
1.63 thorpej 1212: }
1213:
1.1 cgd 1214: /*
1.63 thorpej 1215: * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1216: * then clean up all timers and free all the data structures. If
1217: * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1218: * by timer_create(), not the BSD setitimer() timers, and only free the
1219: * structure if none of those remain.
1.1 cgd 1220: */
1.3 andrew 1221: void
1.63 thorpej 1222: timers_free(struct proc *p, int which)
1.6 cgd 1223: {
1.63 thorpej 1224: struct ptimers *pts;
1.142 ad 1225: struct ptimer *ptn;
1.150 christos 1226: struct timespec ts;
1.142 ad 1227: int i;
1.63 thorpej 1228:
1.142 ad 1229: if (p->p_timers == NULL)
1230: return;
1.63 thorpej 1231:
1.142 ad 1232: pts = p->p_timers;
1233: mutex_spin_enter(&timer_lock);
1234: if (which == TIMERS_ALL) {
1235: p->p_timers = NULL;
1236: i = 0;
1237: } else {
1.150 christos 1238: timespecclear(&ts);
1.142 ad 1239: for (ptn = LIST_FIRST(&pts->pts_virtual);
1240: ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1.149 christos 1241: ptn = LIST_NEXT(ptn, pt_list)) {
1.168 yamt 1242: KASSERT(ptn->pt_type == CLOCK_VIRTUAL);
1.150 christos 1243: timespecadd(&ts, &ptn->pt_time.it_value, &ts);
1.149 christos 1244: }
1.142 ad 1245: LIST_FIRST(&pts->pts_virtual) = NULL;
1246: if (ptn) {
1.168 yamt 1247: KASSERT(ptn->pt_type == CLOCK_VIRTUAL);
1.150 christos 1248: timespecadd(&ts, &ptn->pt_time.it_value,
1.142 ad 1249: &ptn->pt_time.it_value);
1250: LIST_INSERT_HEAD(&pts->pts_virtual, ptn, pt_list);
1251: }
1.150 christos 1252: timespecclear(&ts);
1.142 ad 1253: for (ptn = LIST_FIRST(&pts->pts_prof);
1254: ptn && ptn != pts->pts_timers[ITIMER_PROF];
1.149 christos 1255: ptn = LIST_NEXT(ptn, pt_list)) {
1.168 yamt 1256: KASSERT(ptn->pt_type == CLOCK_PROF);
1.150 christos 1257: timespecadd(&ts, &ptn->pt_time.it_value, &ts);
1.149 christos 1258: }
1.142 ad 1259: LIST_FIRST(&pts->pts_prof) = NULL;
1260: if (ptn) {
1.168 yamt 1261: KASSERT(ptn->pt_type == CLOCK_PROF);
1.150 christos 1262: timespecadd(&ts, &ptn->pt_time.it_value,
1.142 ad 1263: &ptn->pt_time.it_value);
1264: LIST_INSERT_HEAD(&pts->pts_prof, ptn, pt_list);
1.63 thorpej 1265: }
1.142 ad 1266: i = 3;
1267: }
1268: for ( ; i < TIMER_MAX; i++) {
1269: if (pts->pts_timers[i] != NULL) {
1270: itimerfree(pts, i);
1271: mutex_spin_enter(&timer_lock);
1.1 cgd 1272: }
1273: }
1.142 ad 1274: if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
1275: pts->pts_timers[2] == NULL) {
1276: p->p_timers = NULL;
1277: mutex_spin_exit(&timer_lock);
1278: pool_put(&ptimers_pool, pts);
1279: } else
1280: mutex_spin_exit(&timer_lock);
1281: }
1282:
1283: static void
1284: itimerfree(struct ptimers *pts, int index)
1285: {
1286: struct ptimer *pt;
1287:
1288: KASSERT(mutex_owned(&timer_lock));
1289:
1290: pt = pts->pts_timers[index];
1291: pts->pts_timers[index] = NULL;
1.168 yamt 1292: if (!CLOCK_VIRTUAL_P(pt->pt_type))
1.144 ad 1293: callout_halt(&pt->pt_ch, &timer_lock);
1.167 yamt 1294: if (pt->pt_queued)
1.142 ad 1295: TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1.144 ad 1296: mutex_spin_exit(&timer_lock);
1.168 yamt 1297: if (!CLOCK_VIRTUAL_P(pt->pt_type))
1.149 christos 1298: callout_destroy(&pt->pt_ch);
1.142 ad 1299: pool_put(&ptimer_pool, pt);
1.1 cgd 1300: }
1301:
1302: /*
1303: * Decrement an interval timer by a specified number
1.152 christos 1304: * of nanoseconds, which must be less than a second,
1305: * i.e. < 1000000000. If the timer expires, then reload
1306: * it. In this case, carry over (nsec - old value) to
1.8 cgd 1307: * reduce the value reloaded into the timer so that
1.1 cgd 1308: * the timer does not drift. This routine assumes
1309: * that it is called in a context where the timers
1310: * on which it is operating cannot change in value.
1311: */
1.142 ad 1312: static int
1.152 christos 1313: itimerdecr(struct ptimer *pt, int nsec)
1.63 thorpej 1314: {
1.150 christos 1315: struct itimerspec *itp;
1.1 cgd 1316:
1.142 ad 1317: KASSERT(mutex_owned(&timer_lock));
1.168 yamt 1318: KASSERT(CLOCK_VIRTUAL_P(pt->pt_type));
1.142 ad 1319:
1.63 thorpej 1320: itp = &pt->pt_time;
1.150 christos 1321: if (itp->it_value.tv_nsec < nsec) {
1.1 cgd 1322: if (itp->it_value.tv_sec == 0) {
1323: /* expired, and already in next interval */
1.150 christos 1324: nsec -= itp->it_value.tv_nsec;
1.1 cgd 1325: goto expire;
1326: }
1.150 christos 1327: itp->it_value.tv_nsec += 1000000000;
1.1 cgd 1328: itp->it_value.tv_sec--;
1329: }
1.152 christos 1330: itp->it_value.tv_nsec -= nsec;
1331: nsec = 0;
1.150 christos 1332: if (timespecisset(&itp->it_value))
1.1 cgd 1333: return (1);
1334: /* expired, exactly at end of interval */
1335: expire:
1.150 christos 1336: if (timespecisset(&itp->it_interval)) {
1.1 cgd 1337: itp->it_value = itp->it_interval;
1.150 christos 1338: itp->it_value.tv_nsec -= nsec;
1339: if (itp->it_value.tv_nsec < 0) {
1340: itp->it_value.tv_nsec += 1000000000;
1.1 cgd 1341: itp->it_value.tv_sec--;
1342: }
1.63 thorpej 1343: timer_settime(pt);
1.1 cgd 1344: } else
1.150 christos 1345: itp->it_value.tv_nsec = 0; /* sec is already 0 */
1.1 cgd 1346: return (0);
1.42 cgd 1347: }
1348:
1.142 ad 1349: static void
1.63 thorpej 1350: itimerfire(struct ptimer *pt)
1351: {
1.78 cl 1352:
1.142 ad 1353: KASSERT(mutex_owned(&timer_lock));
1354:
1355: /*
1356: * XXX Can overrun, but we don't do signal queueing yet, anyway.
1357: * XXX Relying on the clock interrupt is stupid.
1358: */
1.173 rmind 1359: if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL || pt->pt_queued) {
1.142 ad 1360: return;
1.172 rmind 1361: }
1.142 ad 1362: TAILQ_INSERT_TAIL(&timer_queue, pt, pt_chain);
1363: pt->pt_queued = true;
1364: softint_schedule(timer_sih);
1365: }
1366:
1367: void
1368: timer_tick(lwp_t *l, bool user)
1369: {
1370: struct ptimers *pts;
1371: struct ptimer *pt;
1372: proc_t *p;
1373:
1374: p = l->l_proc;
1375: if (p->p_timers == NULL)
1376: return;
1377:
1378: mutex_spin_enter(&timer_lock);
1379: if ((pts = l->l_proc->p_timers) != NULL) {
1.63 thorpej 1380: /*
1.142 ad 1381: * Run current process's virtual and profile time, as needed.
1.63 thorpej 1382: */
1.142 ad 1383: if (user && (pt = LIST_FIRST(&pts->pts_virtual)) != NULL)
1.152 christos 1384: if (itimerdecr(pt, tick * 1000) == 0)
1.142 ad 1385: itimerfire(pt);
1386: if ((pt = LIST_FIRST(&pts->pts_prof)) != NULL)
1.152 christos 1387: if (itimerdecr(pt, tick * 1000) == 0)
1.142 ad 1388: itimerfire(pt);
1389: }
1390: mutex_spin_exit(&timer_lock);
1391: }
1392:
1393: static void
1394: timer_intr(void *cookie)
1395: {
1396: ksiginfo_t ksi;
1397: struct ptimer *pt;
1398: proc_t *p;
1399:
1.158 ad 1400: mutex_enter(proc_lock);
1.142 ad 1401: mutex_spin_enter(&timer_lock);
1402: while ((pt = TAILQ_FIRST(&timer_queue)) != NULL) {
1403: TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1404: KASSERT(pt->pt_queued);
1405: pt->pt_queued = false;
1406:
1.154 wrstuden 1407: if (pt->pt_proc->p_timers == NULL) {
1408: /* Process is dying. */
1.142 ad 1409: continue;
1.154 wrstuden 1410: }
1.142 ad 1411: p = pt->pt_proc;
1.172 rmind 1412: if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
1.142 ad 1413: continue;
1414: }
1415: if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
1.63 thorpej 1416: pt->pt_overruns++;
1.142 ad 1417: continue;
1.64 nathanw 1418: }
1.142 ad 1419:
1420: KSI_INIT(&ksi);
1421: ksi.ksi_signo = pt->pt_ev.sigev_signo;
1422: ksi.ksi_code = SI_TIMER;
1423: ksi.ksi_value = pt->pt_ev.sigev_value;
1424: pt->pt_poverruns = pt->pt_overruns;
1425: pt->pt_overruns = 0;
1426: mutex_spin_exit(&timer_lock);
1427: kpsignal(p, &ksi, NULL);
1428: mutex_spin_enter(&timer_lock);
1.63 thorpej 1429: }
1.142 ad 1430: mutex_spin_exit(&timer_lock);
1.158 ad 1431: mutex_exit(proc_lock);
1.63 thorpej 1432: }
1.162 elad 1433:
1434: /*
1435: * Check if the time will wrap if set to ts.
1436: *
1437: * ts - timespec describing the new time
1438: * delta - the delta between the current time and ts
1439: */
1440: bool
1441: time_wraps(struct timespec *ts, struct timespec *delta)
1442: {
1443:
1444: /*
1445: * Don't allow the time to be set forward so far it
1446: * will wrap and become negative, thus allowing an
1447: * attacker to bypass the next check below. The
1448: * cutoff is 1 year before rollover occurs, so even
1449: * if the attacker uses adjtime(2) to move the time
1450: * past the cutoff, it will take a very long time
1451: * to get to the wrap point.
1452: */
1453: if ((ts->tv_sec > LLONG_MAX - 365*24*60*60) ||
1454: (delta->tv_sec < 0 || delta->tv_nsec < 0))
1455: return true;
1456:
1457: return false;
1458: }
CVSweb <webmaster@jp.NetBSD.org>