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