2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/limits.h>
38 #include <sys/clock.h>
40 #include <sys/mutex.h>
41 #include <sys/sysproto.h>
42 #include <sys/eventhandler.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/kernel.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
51 #include <sys/posix4.h>
53 #include <sys/timers.h>
54 #include <sys/timetc.h>
55 #include <sys/vnode.h>
58 #include <vm/vm_extern.h>
60 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
62 static struct kclock posix_clocks[MAX_CLOCKS];
63 static uma_zone_t itimer_zone = NULL;
66 * Time of day and interval timer support.
68 * These routines provide the kernel entry points to get and set
69 * the time-of-day and per-process interval timers. Subroutines
70 * here provide support for adding and subtracting timeval structures
71 * and decrementing interval timers, optionally reloading the interval
72 * timers when they expire.
75 static int settime(struct thread *, struct timeval *);
76 static void timevalfix(struct timeval *);
78 static void itimer_start(void);
79 static int itimer_init(void *, int, int);
80 static void itimer_fini(void *, int);
81 static void itimer_enter(struct itimer *);
82 static void itimer_leave(struct itimer *);
83 static struct itimer *itimer_find(struct proc *, int);
84 static void itimers_alloc(struct proc *);
85 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
86 static void itimers_event_hook_exit(void *arg, struct proc *p);
87 static int realtimer_create(struct itimer *);
88 static int realtimer_gettime(struct itimer *, struct itimerspec *);
89 static int realtimer_settime(struct itimer *, int,
90 struct itimerspec *, struct itimerspec *);
91 static int realtimer_delete(struct itimer *);
92 static void realtimer_clocktime(clockid_t, struct timespec *);
93 static void realtimer_expire(void *);
94 static int kern_timer_create(struct thread *, clockid_t,
95 struct sigevent *, int *, int);
96 static int kern_timer_delete(struct thread *, int);
98 int register_posix_clock(int, struct kclock *);
99 void itimer_fire(struct itimer *it);
100 int itimespecfix(struct timespec *ts);
102 #define CLOCK_CALL(clock, call, arglist) \
103 ((*posix_clocks[clock].call) arglist)
105 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
109 settime(struct thread *td, struct timeval *tv)
111 struct timeval delta, tv1, tv2;
112 static struct timeval maxtime, laststep;
119 timevalsub(&delta, &tv1);
122 * If the system is secure, we do not allow the time to be
123 * set to a value earlier than 1 second less than the highest
124 * time we have yet seen. The worst a miscreant can do in
125 * this circumstance is "freeze" time. He couldn't go
128 * We similarly do not allow the clock to be stepped more
129 * than one second, nor more than once per second. This allows
130 * a miscreant to make the clock march double-time, but no worse.
132 if (securelevel_gt(td->td_ucred, 1) != 0) {
133 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
135 * Update maxtime to latest time we've seen.
137 if (tv1.tv_sec > maxtime.tv_sec)
140 timevalsub(&tv2, &maxtime);
141 if (tv2.tv_sec < -1) {
142 tv->tv_sec = maxtime.tv_sec - 1;
143 printf("Time adjustment clamped to -1 second\n");
146 if (tv1.tv_sec == laststep.tv_sec) {
150 if (delta.tv_sec > 1) {
151 tv->tv_sec = tv1.tv_sec + 1;
152 printf("Time adjustment clamped to +1 second\n");
158 ts.tv_sec = tv->tv_sec;
159 ts.tv_nsec = tv->tv_usec * 1000;
167 #ifndef _SYS_SYSPROTO_H_
168 struct clock_gettime_args {
175 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
180 error = kern_clock_gettime(td, uap->clock_id, &ats);
182 error = copyout(&ats, uap->tp, sizeof(ats));
188 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
190 struct timeval sys, user;
192 uint64_t runtime, curtime, switchtime;
196 case CLOCK_REALTIME: /* Default to precise. */
197 case CLOCK_REALTIME_PRECISE:
200 case CLOCK_REALTIME_FAST:
206 calcru(p, &user, &sys);
209 TIMEVAL_TO_TIMESPEC(&user, ats);
214 calcru(p, &user, &sys);
217 timevaladd(&user, &sys);
218 TIMEVAL_TO_TIMESPEC(&user, ats);
220 case CLOCK_MONOTONIC: /* Default to precise. */
221 case CLOCK_MONOTONIC_PRECISE:
223 case CLOCK_UPTIME_PRECISE:
226 case CLOCK_UPTIME_FAST:
227 case CLOCK_MONOTONIC_FAST:
231 ats->tv_sec = time_second;
234 case CLOCK_THREAD_CPUTIME_ID:
236 switchtime = PCPU_GET(switchtime);
237 curtime = cpu_ticks();
238 runtime = td->td_runtime;
240 runtime = cputick2usec(runtime + curtime - switchtime);
241 ats->tv_sec = runtime / 1000000;
242 ats->tv_nsec = runtime % 1000000 * 1000;
250 #ifndef _SYS_SYSPROTO_H_
251 struct clock_settime_args {
253 const struct timespec *tp;
258 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
263 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
265 return (kern_clock_settime(td, uap->clock_id, &ats));
269 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
274 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
276 if (clock_id != CLOCK_REALTIME)
278 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
280 /* XXX Don't convert nsec->usec and back */
281 TIMESPEC_TO_TIMEVAL(&atv, ats);
282 error = settime(td, &atv);
286 #ifndef _SYS_SYSPROTO_H_
287 struct clock_getres_args {
293 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
301 error = kern_clock_getres(td, uap->clock_id, &ts);
303 error = copyout(&ts, uap->tp, sizeof(ts));
308 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
314 case CLOCK_REALTIME_FAST:
315 case CLOCK_REALTIME_PRECISE:
316 case CLOCK_MONOTONIC:
317 case CLOCK_MONOTONIC_FAST:
318 case CLOCK_MONOTONIC_PRECISE:
320 case CLOCK_UPTIME_FAST:
321 case CLOCK_UPTIME_PRECISE:
323 * Round up the result of the division cheaply by adding 1.
324 * Rounding up is especially important if rounding down
325 * would give 0. Perfect rounding is unimportant.
327 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
331 /* Accurately round up here because we can do so cheaply. */
332 ts->tv_nsec = (1000000000 + hz - 1) / hz;
338 case CLOCK_THREAD_CPUTIME_ID:
339 /* sync with cputick2usec */
340 ts->tv_nsec = 1000000 / cpu_tickrate();
341 if (ts->tv_nsec == 0)
353 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
355 struct timespec ts, ts2, ts3;
359 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
361 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
364 timespecadd(&ts, rqt);
365 TIMESPEC_TO_TIMEVAL(&tv, rqt);
367 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
370 if (error != EWOULDBLOCK) {
371 if (error == ERESTART)
374 timespecsub(&ts, &ts2);
381 if (timespeccmp(&ts2, &ts, >=))
384 timespecsub(&ts3, &ts2);
385 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
389 #ifndef _SYS_SYSPROTO_H_
390 struct nanosleep_args {
391 struct timespec *rqtp;
392 struct timespec *rmtp;
397 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
399 struct timespec rmt, rqt;
402 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
407 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
409 error = kern_nanosleep(td, &rqt, &rmt);
410 if (error && uap->rmtp) {
413 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
420 #ifndef _SYS_SYSPROTO_H_
421 struct gettimeofday_args {
423 struct timezone *tzp;
428 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
436 error = copyout(&atv, uap->tp, sizeof (atv));
438 if (error == 0 && uap->tzp != NULL) {
439 rtz.tz_minuteswest = tz_minuteswest;
440 rtz.tz_dsttime = tz_dsttime;
441 error = copyout(&rtz, uap->tzp, sizeof (rtz));
446 #ifndef _SYS_SYSPROTO_H_
447 struct settimeofday_args {
449 struct timezone *tzp;
454 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
456 struct timeval atv, *tvp;
457 struct timezone atz, *tzp;
461 error = copyin(uap->tv, &atv, sizeof(atv));
468 error = copyin(uap->tzp, &atz, sizeof(atz));
474 return (kern_settimeofday(td, tvp, tzp));
478 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
482 error = priv_check(td, PRIV_SETTIMEOFDAY);
485 /* Verify all parameters before changing time. */
487 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
489 error = settime(td, tv);
491 if (tzp && error == 0) {
492 tz_minuteswest = tzp->tz_minuteswest;
493 tz_dsttime = tzp->tz_dsttime;
499 * Get value of an interval timer. The process virtual and profiling virtual
500 * time timers are kept in the p_stats area, since they can be swapped out.
501 * These are kept internally in the way they are specified externally: in
502 * time until they expire.
504 * The real time interval timer is kept in the process table slot for the
505 * process, and its value (it_value) is kept as an absolute time rather than
506 * as a delta, so that it is easy to keep periodic real-time signals from
509 * Virtual time timers are processed in the hardclock() routine of
510 * kern_clock.c. The real time timer is processed by a timeout routine,
511 * called from the softclock() routine. Since a callout may be delayed in
512 * real time due to interrupt processing in the system, it is possible for
513 * the real time timeout routine (realitexpire, given below), to be delayed
514 * in real time past when it is supposed to occur. It does not suffice,
515 * therefore, to reload the real timer .it_value from the real time timers
516 * .it_interval. Rather, we compute the next time in absolute time the timer
519 #ifndef _SYS_SYSPROTO_H_
520 struct getitimer_args {
522 struct itimerval *itv;
526 sys_getitimer(struct thread *td, struct getitimer_args *uap)
528 struct itimerval aitv;
531 error = kern_getitimer(td, uap->which, &aitv);
534 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
538 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
540 struct proc *p = td->td_proc;
543 if (which > ITIMER_PROF)
546 if (which == ITIMER_REAL) {
548 * Convert from absolute to relative time in .it_value
549 * part of real time timer. If time for real time timer
550 * has passed return 0, else return difference between
551 * current time and time for the timer to go off.
554 *aitv = p->p_realtimer;
556 if (timevalisset(&aitv->it_value)) {
557 getmicrouptime(&ctv);
558 if (timevalcmp(&aitv->it_value, &ctv, <))
559 timevalclear(&aitv->it_value);
561 timevalsub(&aitv->it_value, &ctv);
565 *aitv = p->p_stats->p_timer[which];
571 #ifndef _SYS_SYSPROTO_H_
572 struct setitimer_args {
574 struct itimerval *itv, *oitv;
578 sys_setitimer(struct thread *td, struct setitimer_args *uap)
580 struct itimerval aitv, oitv;
583 if (uap->itv == NULL) {
584 uap->itv = uap->oitv;
585 return (sys_getitimer(td, (struct getitimer_args *)uap));
588 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
590 error = kern_setitimer(td, uap->which, &aitv, &oitv);
591 if (error != 0 || uap->oitv == NULL)
593 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
597 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
598 struct itimerval *oitv)
600 struct proc *p = td->td_proc;
604 return (kern_getitimer(td, which, oitv));
606 if (which > ITIMER_PROF)
608 if (itimerfix(&aitv->it_value))
610 if (!timevalisset(&aitv->it_value))
611 timevalclear(&aitv->it_interval);
612 else if (itimerfix(&aitv->it_interval))
615 if (which == ITIMER_REAL) {
617 if (timevalisset(&p->p_realtimer.it_value))
618 callout_stop(&p->p_itcallout);
619 getmicrouptime(&ctv);
620 if (timevalisset(&aitv->it_value)) {
621 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
623 timevaladd(&aitv->it_value, &ctv);
625 *oitv = p->p_realtimer;
626 p->p_realtimer = *aitv;
628 if (timevalisset(&oitv->it_value)) {
629 if (timevalcmp(&oitv->it_value, &ctv, <))
630 timevalclear(&oitv->it_value);
632 timevalsub(&oitv->it_value, &ctv);
636 *oitv = p->p_stats->p_timer[which];
637 p->p_stats->p_timer[which] = *aitv;
644 * Real interval timer expired:
645 * send process whose timer expired an alarm signal.
646 * If time is not set up to reload, then just return.
647 * Else compute next time timer should go off which is > current time.
648 * This is where delay in processing this timeout causes multiple
649 * SIGALRM calls to be compressed into one.
650 * tvtohz() always adds 1 to allow for the time until the next clock
651 * interrupt being strictly less than 1 clock tick, but we don't want
652 * that here since we want to appear to be in sync with the clock
653 * interrupt even when we're delayed.
656 realitexpire(void *arg)
659 struct timeval ctv, ntv;
661 p = (struct proc *)arg;
662 kern_psignal(p, SIGALRM);
663 if (!timevalisset(&p->p_realtimer.it_interval)) {
664 timevalclear(&p->p_realtimer.it_value);
665 if (p->p_flag & P_WEXIT)
666 wakeup(&p->p_itcallout);
670 timevaladd(&p->p_realtimer.it_value,
671 &p->p_realtimer.it_interval);
672 getmicrouptime(&ctv);
673 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
674 ntv = p->p_realtimer.it_value;
675 timevalsub(&ntv, &ctv);
676 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
685 * Check that a proposed value to load into the .it_value or
686 * .it_interval part of an interval timer is acceptable, and
687 * fix it to have at least minimal value (i.e. if it is less
688 * than the resolution of the clock, round it up.)
691 itimerfix(struct timeval *tv)
694 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
696 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
702 * Decrement an interval timer by a specified number
703 * of microseconds, which must be less than a second,
704 * i.e. < 1000000. If the timer expires, then reload
705 * it. In this case, carry over (usec - old value) to
706 * reduce the value reloaded into the timer so that
707 * the timer does not drift. This routine assumes
708 * that it is called in a context where the timers
709 * on which it is operating cannot change in value.
712 itimerdecr(struct itimerval *itp, int usec)
715 if (itp->it_value.tv_usec < usec) {
716 if (itp->it_value.tv_sec == 0) {
717 /* expired, and already in next interval */
718 usec -= itp->it_value.tv_usec;
721 itp->it_value.tv_usec += 1000000;
722 itp->it_value.tv_sec--;
724 itp->it_value.tv_usec -= usec;
726 if (timevalisset(&itp->it_value))
728 /* expired, exactly at end of interval */
730 if (timevalisset(&itp->it_interval)) {
731 itp->it_value = itp->it_interval;
732 itp->it_value.tv_usec -= usec;
733 if (itp->it_value.tv_usec < 0) {
734 itp->it_value.tv_usec += 1000000;
735 itp->it_value.tv_sec--;
738 itp->it_value.tv_usec = 0; /* sec is already 0 */
743 * Add and subtract routines for timevals.
744 * N.B.: subtract routine doesn't deal with
745 * results which are before the beginning,
746 * it just gets very confused in this case.
750 timevaladd(struct timeval *t1, const struct timeval *t2)
753 t1->tv_sec += t2->tv_sec;
754 t1->tv_usec += t2->tv_usec;
759 timevalsub(struct timeval *t1, const struct timeval *t2)
762 t1->tv_sec -= t2->tv_sec;
763 t1->tv_usec -= t2->tv_usec;
768 timevalfix(struct timeval *t1)
771 if (t1->tv_usec < 0) {
773 t1->tv_usec += 1000000;
775 if (t1->tv_usec >= 1000000) {
777 t1->tv_usec -= 1000000;
782 * ratecheck(): simple time-based rate-limit checking.
785 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
787 struct timeval tv, delta;
790 getmicrouptime(&tv); /* NB: 10ms precision */
792 timevalsub(&delta, lasttime);
795 * check for 0,0 is so that the message will be seen at least once,
796 * even if interval is huge.
798 if (timevalcmp(&delta, mininterval, >=) ||
799 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
808 * ppsratecheck(): packets (or events) per second limitation.
810 * Return 0 if the limit is to be enforced (e.g. the caller
811 * should drop a packet because of the rate limitation).
813 * maxpps of 0 always causes zero to be returned. maxpps of -1
814 * always causes 1 to be returned; this effectively defeats rate
817 * Note that we maintain the struct timeval for compatibility
818 * with other bsd systems. We reuse the storage and just monitor
819 * clock ticks for minimal overhead.
822 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
827 * Reset the last time and counter if this is the first call
828 * or more than a second has passed since the last update of
832 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
833 lasttime->tv_sec = now;
835 return (maxpps != 0);
837 (*curpps)++; /* NB: ignore potential overflow */
838 return (maxpps < 0 || *curpps < maxpps);
845 struct kclock rt_clock = {
846 .timer_create = realtimer_create,
847 .timer_delete = realtimer_delete,
848 .timer_settime = realtimer_settime,
849 .timer_gettime = realtimer_gettime,
853 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
854 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
855 register_posix_clock(CLOCK_REALTIME, &rt_clock);
856 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
857 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
858 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
859 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
860 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
861 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
862 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
863 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
867 register_posix_clock(int clockid, struct kclock *clk)
869 if ((unsigned)clockid >= MAX_CLOCKS) {
870 printf("%s: invalid clockid\n", __func__);
873 posix_clocks[clockid] = *clk;
878 itimer_init(void *mem, int size, int flags)
882 it = (struct itimer *)mem;
883 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
888 itimer_fini(void *mem, int size)
892 it = (struct itimer *)mem;
893 mtx_destroy(&it->it_mtx);
897 itimer_enter(struct itimer *it)
900 mtx_assert(&it->it_mtx, MA_OWNED);
905 itimer_leave(struct itimer *it)
908 mtx_assert(&it->it_mtx, MA_OWNED);
909 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
911 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
915 #ifndef _SYS_SYSPROTO_H_
916 struct ktimer_create_args {
918 struct sigevent * evp;
923 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
925 struct sigevent *evp1, ev;
929 if (uap->evp != NULL) {
930 error = copyin(uap->evp, &ev, sizeof(ev));
937 error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
940 error = copyout(&id, uap->timerid, sizeof(int));
942 kern_timer_delete(td, id);
948 kern_timer_create(struct thread *td, clockid_t clock_id,
949 struct sigevent *evp, int *timerid, int preset_id)
951 struct proc *p = td->td_proc;
956 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
959 if (posix_clocks[clock_id].timer_create == NULL)
963 if (evp->sigev_notify != SIGEV_NONE &&
964 evp->sigev_notify != SIGEV_SIGNAL &&
965 evp->sigev_notify != SIGEV_THREAD_ID)
967 if ((evp->sigev_notify == SIGEV_SIGNAL ||
968 evp->sigev_notify == SIGEV_THREAD_ID) &&
969 !_SIG_VALID(evp->sigev_signo))
973 if (p->p_itimers == NULL)
976 it = uma_zalloc(itimer_zone, M_WAITOK);
980 timespecclear(&it->it_time.it_value);
981 timespecclear(&it->it_time.it_interval);
983 it->it_overrun_last = 0;
984 it->it_clockid = clock_id;
987 ksiginfo_init(&it->it_ksi);
988 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
989 error = CLOCK_CALL(clock_id, timer_create, (it));
994 if (preset_id != -1) {
995 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
997 if (p->p_itimers->its_timers[id] != NULL) {
1004 * Find a free timer slot, skipping those reserved
1007 for (id = 3; id < TIMER_MAX; id++)
1008 if (p->p_itimers->its_timers[id] == NULL)
1010 if (id == TIMER_MAX) {
1016 it->it_timerid = id;
1017 p->p_itimers->its_timers[id] = it;
1019 it->it_sigev = *evp;
1021 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1024 case CLOCK_REALTIME:
1025 it->it_sigev.sigev_signo = SIGALRM;
1028 it->it_sigev.sigev_signo = SIGVTALRM;
1031 it->it_sigev.sigev_signo = SIGPROF;
1034 it->it_sigev.sigev_value.sival_int = id;
1037 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1038 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1039 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1040 it->it_ksi.ksi_code = SI_TIMER;
1041 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1042 it->it_ksi.ksi_timerid = id;
1050 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1052 uma_zfree(itimer_zone, it);
1056 #ifndef _SYS_SYSPROTO_H_
1057 struct ktimer_delete_args {
1062 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1064 return (kern_timer_delete(td, uap->timerid));
1067 static struct itimer *
1068 itimer_find(struct proc *p, int timerid)
1072 PROC_LOCK_ASSERT(p, MA_OWNED);
1073 if ((p->p_itimers == NULL) ||
1074 (timerid < 0) || (timerid >= TIMER_MAX) ||
1075 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1079 if ((it->it_flags & ITF_DELETING) != 0) {
1087 kern_timer_delete(struct thread *td, int timerid)
1089 struct proc *p = td->td_proc;
1093 it = itimer_find(p, timerid);
1100 it->it_flags |= ITF_DELETING;
1101 while (it->it_usecount > 0) {
1102 it->it_flags |= ITF_WANTED;
1103 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1105 it->it_flags &= ~ITF_WANTED;
1106 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1110 if (KSI_ONQ(&it->it_ksi))
1111 sigqueue_take(&it->it_ksi);
1112 p->p_itimers->its_timers[timerid] = NULL;
1114 uma_zfree(itimer_zone, it);
1118 #ifndef _SYS_SYSPROTO_H_
1119 struct ktimer_settime_args {
1122 const struct itimerspec * value;
1123 struct itimerspec * ovalue;
1127 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1129 struct proc *p = td->td_proc;
1131 struct itimerspec val, oval, *ovalp;
1134 error = copyin(uap->value, &val, sizeof(val));
1138 if (uap->ovalue != NULL)
1144 if (uap->timerid < 3 ||
1145 (it = itimer_find(p, uap->timerid)) == NULL) {
1151 error = CLOCK_CALL(it->it_clockid, timer_settime,
1152 (it, uap->flags, &val, ovalp));
1156 if (error == 0 && uap->ovalue != NULL)
1157 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1161 #ifndef _SYS_SYSPROTO_H_
1162 struct ktimer_gettime_args {
1164 struct itimerspec * value;
1168 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1170 struct proc *p = td->td_proc;
1172 struct itimerspec val;
1176 if (uap->timerid < 3 ||
1177 (it = itimer_find(p, uap->timerid)) == NULL) {
1183 error = CLOCK_CALL(it->it_clockid, timer_gettime,
1189 error = copyout(&val, uap->value, sizeof(val));
1193 #ifndef _SYS_SYSPROTO_H_
1194 struct timer_getoverrun_args {
1199 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1201 struct proc *p = td->td_proc;
1206 if (uap->timerid < 3 ||
1207 (it = itimer_find(p, uap->timerid)) == NULL) {
1211 td->td_retval[0] = it->it_overrun_last;
1220 realtimer_create(struct itimer *it)
1222 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1227 realtimer_delete(struct itimer *it)
1229 mtx_assert(&it->it_mtx, MA_OWNED);
1232 * clear timer's value and interval to tell realtimer_expire
1233 * to not rearm the timer.
1235 timespecclear(&it->it_time.it_value);
1236 timespecclear(&it->it_time.it_interval);
1238 callout_drain(&it->it_callout);
1244 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1246 struct timespec cts;
1248 mtx_assert(&it->it_mtx, MA_OWNED);
1250 realtimer_clocktime(it->it_clockid, &cts);
1251 *ovalue = it->it_time;
1252 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1253 timespecsub(&ovalue->it_value, &cts);
1254 if (ovalue->it_value.tv_sec < 0 ||
1255 (ovalue->it_value.tv_sec == 0 &&
1256 ovalue->it_value.tv_nsec == 0)) {
1257 ovalue->it_value.tv_sec = 0;
1258 ovalue->it_value.tv_nsec = 1;
1265 realtimer_settime(struct itimer *it, int flags,
1266 struct itimerspec *value, struct itimerspec *ovalue)
1268 struct timespec cts, ts;
1270 struct itimerspec val;
1272 mtx_assert(&it->it_mtx, MA_OWNED);
1275 if (itimespecfix(&val.it_value))
1278 if (timespecisset(&val.it_value)) {
1279 if (itimespecfix(&val.it_interval))
1282 timespecclear(&val.it_interval);
1286 realtimer_gettime(it, ovalue);
1289 if (timespecisset(&val.it_value)) {
1290 realtimer_clocktime(it->it_clockid, &cts);
1292 if ((flags & TIMER_ABSTIME) == 0) {
1293 /* Convert to absolute time. */
1294 timespecadd(&it->it_time.it_value, &cts);
1296 timespecsub(&ts, &cts);
1298 * We don't care if ts is negative, tztohz will
1302 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1303 callout_reset(&it->it_callout, tvtohz(&tv),
1304 realtimer_expire, it);
1306 callout_stop(&it->it_callout);
1313 realtimer_clocktime(clockid_t id, struct timespec *ts)
1315 if (id == CLOCK_REALTIME)
1317 else /* CLOCK_MONOTONIC */
1322 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1326 PROC_LOCK_ASSERT(p, MA_OWNED);
1327 it = itimer_find(p, timerid);
1329 ksi->ksi_overrun = it->it_overrun;
1330 it->it_overrun_last = it->it_overrun;
1339 itimespecfix(struct timespec *ts)
1342 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1344 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1345 ts->tv_nsec = tick * 1000;
1349 /* Timeout callback for realtime timer */
1351 realtimer_expire(void *arg)
1353 struct timespec cts, ts;
1357 it = (struct itimer *)arg;
1359 realtimer_clocktime(it->it_clockid, &cts);
1360 /* Only fire if time is reached. */
1361 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1362 if (timespecisset(&it->it_time.it_interval)) {
1363 timespecadd(&it->it_time.it_value,
1364 &it->it_time.it_interval);
1365 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1366 if (it->it_overrun < INT_MAX)
1369 it->it_ksi.ksi_errno = ERANGE;
1370 timespecadd(&it->it_time.it_value,
1371 &it->it_time.it_interval);
1374 /* single shot timer ? */
1375 timespecclear(&it->it_time.it_value);
1377 if (timespecisset(&it->it_time.it_value)) {
1378 ts = it->it_time.it_value;
1379 timespecsub(&ts, &cts);
1380 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1381 callout_reset(&it->it_callout, tvtohz(&tv),
1382 realtimer_expire, it);
1389 } else if (timespecisset(&it->it_time.it_value)) {
1390 ts = it->it_time.it_value;
1391 timespecsub(&ts, &cts);
1392 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1393 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1399 itimer_fire(struct itimer *it)
1401 struct proc *p = it->it_proc;
1404 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1405 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1406 if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1408 timespecclear(&it->it_time.it_value);
1409 timespecclear(&it->it_time.it_interval);
1410 callout_stop(&it->it_callout);
1414 if (!KSI_ONQ(&it->it_ksi)) {
1415 it->it_ksi.ksi_errno = 0;
1416 ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1417 tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1419 if (it->it_overrun < INT_MAX)
1422 it->it_ksi.ksi_errno = ERANGE;
1429 itimers_alloc(struct proc *p)
1431 struct itimers *its;
1434 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1435 LIST_INIT(&its->its_virtual);
1436 LIST_INIT(&its->its_prof);
1437 TAILQ_INIT(&its->its_worklist);
1438 for (i = 0; i < TIMER_MAX; i++)
1439 its->its_timers[i] = NULL;
1441 if (p->p_itimers == NULL) {
1447 free(its, M_SUBPROC);
1452 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1454 itimers_event_hook_exit(arg, p);
1457 /* Clean up timers when some process events are being triggered. */
1459 itimers_event_hook_exit(void *arg, struct proc *p)
1461 struct itimers *its;
1463 int event = (int)(intptr_t)arg;
1466 if (p->p_itimers != NULL) {
1468 for (i = 0; i < MAX_CLOCKS; ++i) {
1469 if (posix_clocks[i].event_hook != NULL)
1470 CLOCK_CALL(i, event_hook, (p, i, event));
1473 * According to susv3, XSI interval timers should be inherited
1476 if (event == ITIMER_EV_EXEC)
1478 else if (event == ITIMER_EV_EXIT)
1481 panic("unhandled event");
1482 for (; i < TIMER_MAX; ++i) {
1483 if ((it = its->its_timers[i]) != NULL)
1484 kern_timer_delete(curthread, i);
1486 if (its->its_timers[0] == NULL &&
1487 its->its_timers[1] == NULL &&
1488 its->its_timers[2] == NULL) {
1489 free(its, M_SUBPROC);
1490 p->p_itimers = NULL;