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 *);
77 static void no_lease_updatetime(int);
79 static void itimer_start(void);
80 static int itimer_init(void *, int, int);
81 static void itimer_fini(void *, int);
82 static void itimer_enter(struct itimer *);
83 static void itimer_leave(struct itimer *);
84 static struct itimer *itimer_find(struct proc *, int);
85 static void itimers_alloc(struct proc *);
86 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
87 static void itimers_event_hook_exit(void *arg, struct proc *p);
88 static int realtimer_create(struct itimer *);
89 static int realtimer_gettime(struct itimer *, struct itimerspec *);
90 static int realtimer_settime(struct itimer *, int,
91 struct itimerspec *, struct itimerspec *);
92 static int realtimer_delete(struct itimer *);
93 static void realtimer_clocktime(clockid_t, struct timespec *);
94 static void realtimer_expire(void *);
95 static int kern_timer_create(struct thread *, clockid_t,
96 struct sigevent *, int *, int);
97 static int kern_timer_delete(struct thread *, int);
99 int register_posix_clock(int, struct kclock *);
100 void itimer_fire(struct itimer *it);
101 int itimespecfix(struct timespec *ts);
103 #define CLOCK_CALL(clock, call, arglist) \
104 ((*posix_clocks[clock].call) arglist)
106 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
110 no_lease_updatetime(deltat)
115 void (*lease_updatetime)(int) = no_lease_updatetime;
118 settime(struct thread *td, struct timeval *tv)
120 struct timeval delta, tv1, tv2;
121 static struct timeval maxtime, laststep;
128 timevalsub(&delta, &tv1);
131 * If the system is secure, we do not allow the time to be
132 * set to a value earlier than 1 second less than the highest
133 * time we have yet seen. The worst a miscreant can do in
134 * this circumstance is "freeze" time. He couldn't go
137 * We similarly do not allow the clock to be stepped more
138 * than one second, nor more than once per second. This allows
139 * a miscreant to make the clock march double-time, but no worse.
141 if (securelevel_gt(td->td_ucred, 1) != 0) {
142 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
144 * Update maxtime to latest time we've seen.
146 if (tv1.tv_sec > maxtime.tv_sec)
149 timevalsub(&tv2, &maxtime);
150 if (tv2.tv_sec < -1) {
151 tv->tv_sec = maxtime.tv_sec - 1;
152 printf("Time adjustment clamped to -1 second\n");
155 if (tv1.tv_sec == laststep.tv_sec) {
159 if (delta.tv_sec > 1) {
160 tv->tv_sec = tv1.tv_sec + 1;
161 printf("Time adjustment clamped to +1 second\n");
167 ts.tv_sec = tv->tv_sec;
168 ts.tv_nsec = tv->tv_usec * 1000;
171 (void) splsoftclock();
172 lease_updatetime(delta.tv_sec);
179 #ifndef _SYS_SYSPROTO_H_
180 struct clock_gettime_args {
187 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
192 error = kern_clock_gettime(td, uap->clock_id, &ats);
194 error = copyout(&ats, uap->tp, sizeof(ats));
200 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
202 struct timeval sys, user;
204 uint64_t runtime, curtime, switchtime;
208 case CLOCK_REALTIME: /* Default to precise. */
209 case CLOCK_REALTIME_PRECISE:
212 case CLOCK_REALTIME_FAST:
218 calcru(p, &user, &sys);
221 TIMEVAL_TO_TIMESPEC(&user, ats);
226 calcru(p, &user, &sys);
229 timevaladd(&user, &sys);
230 TIMEVAL_TO_TIMESPEC(&user, ats);
232 case CLOCK_MONOTONIC: /* Default to precise. */
233 case CLOCK_MONOTONIC_PRECISE:
235 case CLOCK_UPTIME_PRECISE:
238 case CLOCK_UPTIME_FAST:
239 case CLOCK_MONOTONIC_FAST:
243 ats->tv_sec = time_second;
246 case CLOCK_THREAD_CPUTIME_ID:
248 switchtime = PCPU_GET(switchtime);
249 curtime = cpu_ticks();
250 runtime = td->td_runtime;
252 runtime = cputick2usec(runtime + curtime - switchtime);
253 ats->tv_sec = runtime / 1000000;
254 ats->tv_nsec = runtime % 1000000 * 1000;
262 #ifndef _SYS_SYSPROTO_H_
263 struct clock_settime_args {
265 const struct timespec *tp;
270 clock_settime(struct thread *td, struct clock_settime_args *uap)
275 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
277 return (kern_clock_settime(td, uap->clock_id, &ats));
281 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
286 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
288 if (clock_id != CLOCK_REALTIME)
290 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
292 /* XXX Don't convert nsec->usec and back */
293 TIMESPEC_TO_TIMEVAL(&atv, ats);
294 error = settime(td, &atv);
298 #ifndef _SYS_SYSPROTO_H_
299 struct clock_getres_args {
305 clock_getres(struct thread *td, struct clock_getres_args *uap)
313 error = kern_clock_getres(td, uap->clock_id, &ts);
315 error = copyout(&ts, uap->tp, sizeof(ts));
320 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
326 case CLOCK_REALTIME_FAST:
327 case CLOCK_REALTIME_PRECISE:
328 case CLOCK_MONOTONIC:
329 case CLOCK_MONOTONIC_FAST:
330 case CLOCK_MONOTONIC_PRECISE:
332 case CLOCK_UPTIME_FAST:
333 case CLOCK_UPTIME_PRECISE:
335 * Round up the result of the division cheaply by adding 1.
336 * Rounding up is especially important if rounding down
337 * would give 0. Perfect rounding is unimportant.
339 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
343 /* Accurately round up here because we can do so cheaply. */
344 ts->tv_nsec = (1000000000 + hz - 1) / hz;
359 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
361 struct timespec ts, ts2, ts3;
365 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
367 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
370 timespecadd(&ts, rqt);
371 TIMESPEC_TO_TIMEVAL(&tv, rqt);
373 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
376 if (error != EWOULDBLOCK) {
377 if (error == ERESTART)
380 timespecsub(&ts, &ts2);
387 if (timespeccmp(&ts2, &ts, >=))
390 timespecsub(&ts3, &ts2);
391 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
395 #ifndef _SYS_SYSPROTO_H_
396 struct nanosleep_args {
397 struct timespec *rqtp;
398 struct timespec *rmtp;
403 nanosleep(struct thread *td, struct nanosleep_args *uap)
405 struct timespec rmt, rqt;
408 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
413 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
415 error = kern_nanosleep(td, &rqt, &rmt);
416 if (error && uap->rmtp) {
419 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
426 #ifndef _SYS_SYSPROTO_H_
427 struct gettimeofday_args {
429 struct timezone *tzp;
434 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
442 error = copyout(&atv, uap->tp, sizeof (atv));
444 if (error == 0 && uap->tzp != NULL) {
445 rtz.tz_minuteswest = tz_minuteswest;
446 rtz.tz_dsttime = tz_dsttime;
447 error = copyout(&rtz, uap->tzp, sizeof (rtz));
452 #ifndef _SYS_SYSPROTO_H_
453 struct settimeofday_args {
455 struct timezone *tzp;
460 settimeofday(struct thread *td, struct settimeofday_args *uap)
462 struct timeval atv, *tvp;
463 struct timezone atz, *tzp;
467 error = copyin(uap->tv, &atv, sizeof(atv));
474 error = copyin(uap->tzp, &atz, sizeof(atz));
480 return (kern_settimeofday(td, tvp, tzp));
484 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
488 error = priv_check(td, PRIV_SETTIMEOFDAY);
491 /* Verify all parameters before changing time. */
493 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
495 error = settime(td, tv);
497 if (tzp && error == 0) {
498 tz_minuteswest = tzp->tz_minuteswest;
499 tz_dsttime = tzp->tz_dsttime;
505 * Get value of an interval timer. The process virtual and profiling virtual
506 * time timers are kept in the p_stats area, since they can be swapped out.
507 * These are kept internally in the way they are specified externally: in
508 * time until they expire.
510 * The real time interval timer is kept in the process table slot for the
511 * process, and its value (it_value) is kept as an absolute time rather than
512 * as a delta, so that it is easy to keep periodic real-time signals from
515 * Virtual time timers are processed in the hardclock() routine of
516 * kern_clock.c. The real time timer is processed by a timeout routine,
517 * called from the softclock() routine. Since a callout may be delayed in
518 * real time due to interrupt processing in the system, it is possible for
519 * the real time timeout routine (realitexpire, given below), to be delayed
520 * in real time past when it is supposed to occur. It does not suffice,
521 * therefore, to reload the real timer .it_value from the real time timers
522 * .it_interval. Rather, we compute the next time in absolute time the timer
525 #ifndef _SYS_SYSPROTO_H_
526 struct getitimer_args {
528 struct itimerval *itv;
532 getitimer(struct thread *td, struct getitimer_args *uap)
534 struct itimerval aitv;
537 error = kern_getitimer(td, uap->which, &aitv);
540 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
544 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
546 struct proc *p = td->td_proc;
549 if (which > ITIMER_PROF)
552 if (which == ITIMER_REAL) {
554 * Convert from absolute to relative time in .it_value
555 * part of real time timer. If time for real time timer
556 * has passed return 0, else return difference between
557 * current time and time for the timer to go off.
560 *aitv = p->p_realtimer;
562 if (timevalisset(&aitv->it_value)) {
563 getmicrouptime(&ctv);
564 if (timevalcmp(&aitv->it_value, &ctv, <))
565 timevalclear(&aitv->it_value);
567 timevalsub(&aitv->it_value, &ctv);
571 *aitv = p->p_stats->p_timer[which];
577 #ifndef _SYS_SYSPROTO_H_
578 struct setitimer_args {
580 struct itimerval *itv, *oitv;
584 setitimer(struct thread *td, struct setitimer_args *uap)
586 struct itimerval aitv, oitv;
589 if (uap->itv == NULL) {
590 uap->itv = uap->oitv;
591 return (getitimer(td, (struct getitimer_args *)uap));
594 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
596 error = kern_setitimer(td, uap->which, &aitv, &oitv);
597 if (error != 0 || uap->oitv == NULL)
599 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
603 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
604 struct itimerval *oitv)
606 struct proc *p = td->td_proc;
610 return (kern_getitimer(td, which, oitv));
612 if (which > ITIMER_PROF)
614 if (itimerfix(&aitv->it_value))
616 if (!timevalisset(&aitv->it_value))
617 timevalclear(&aitv->it_interval);
618 else if (itimerfix(&aitv->it_interval))
621 if (which == ITIMER_REAL) {
623 if (timevalisset(&p->p_realtimer.it_value))
624 callout_stop(&p->p_itcallout);
625 getmicrouptime(&ctv);
626 if (timevalisset(&aitv->it_value)) {
627 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
629 timevaladd(&aitv->it_value, &ctv);
631 *oitv = p->p_realtimer;
632 p->p_realtimer = *aitv;
634 if (timevalisset(&oitv->it_value)) {
635 if (timevalcmp(&oitv->it_value, &ctv, <))
636 timevalclear(&oitv->it_value);
638 timevalsub(&oitv->it_value, &ctv);
642 *oitv = p->p_stats->p_timer[which];
643 p->p_stats->p_timer[which] = *aitv;
650 * Real interval timer expired:
651 * send process whose timer expired an alarm signal.
652 * If time is not set up to reload, then just return.
653 * Else compute next time timer should go off which is > current time.
654 * This is where delay in processing this timeout causes multiple
655 * SIGALRM calls to be compressed into one.
656 * tvtohz() always adds 1 to allow for the time until the next clock
657 * interrupt being strictly less than 1 clock tick, but we don't want
658 * that here since we want to appear to be in sync with the clock
659 * interrupt even when we're delayed.
662 realitexpire(void *arg)
665 struct timeval ctv, ntv;
667 p = (struct proc *)arg;
670 if (!timevalisset(&p->p_realtimer.it_interval)) {
671 timevalclear(&p->p_realtimer.it_value);
672 if (p->p_flag & P_WEXIT)
673 wakeup(&p->p_itcallout);
678 timevaladd(&p->p_realtimer.it_value,
679 &p->p_realtimer.it_interval);
680 getmicrouptime(&ctv);
681 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
682 ntv = p->p_realtimer.it_value;
683 timevalsub(&ntv, &ctv);
684 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
694 * Check that a proposed value to load into the .it_value or
695 * .it_interval part of an interval timer is acceptable, and
696 * fix it to have at least minimal value (i.e. if it is less
697 * than the resolution of the clock, round it up.)
700 itimerfix(struct timeval *tv)
703 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
705 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
711 * Decrement an interval timer by a specified number
712 * of microseconds, which must be less than a second,
713 * i.e. < 1000000. If the timer expires, then reload
714 * it. In this case, carry over (usec - old value) to
715 * reduce the value reloaded into the timer so that
716 * the timer does not drift. This routine assumes
717 * that it is called in a context where the timers
718 * on which it is operating cannot change in value.
721 itimerdecr(struct itimerval *itp, int usec)
724 if (itp->it_value.tv_usec < usec) {
725 if (itp->it_value.tv_sec == 0) {
726 /* expired, and already in next interval */
727 usec -= itp->it_value.tv_usec;
730 itp->it_value.tv_usec += 1000000;
731 itp->it_value.tv_sec--;
733 itp->it_value.tv_usec -= usec;
735 if (timevalisset(&itp->it_value))
737 /* expired, exactly at end of interval */
739 if (timevalisset(&itp->it_interval)) {
740 itp->it_value = itp->it_interval;
741 itp->it_value.tv_usec -= usec;
742 if (itp->it_value.tv_usec < 0) {
743 itp->it_value.tv_usec += 1000000;
744 itp->it_value.tv_sec--;
747 itp->it_value.tv_usec = 0; /* sec is already 0 */
752 * Add and subtract routines for timevals.
753 * N.B.: subtract routine doesn't deal with
754 * results which are before the beginning,
755 * it just gets very confused in this case.
759 timevaladd(struct timeval *t1, const struct timeval *t2)
762 t1->tv_sec += t2->tv_sec;
763 t1->tv_usec += t2->tv_usec;
768 timevalsub(struct timeval *t1, const struct timeval *t2)
771 t1->tv_sec -= t2->tv_sec;
772 t1->tv_usec -= t2->tv_usec;
777 timevalfix(struct timeval *t1)
780 if (t1->tv_usec < 0) {
782 t1->tv_usec += 1000000;
784 if (t1->tv_usec >= 1000000) {
786 t1->tv_usec -= 1000000;
791 * ratecheck(): simple time-based rate-limit checking.
794 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
796 struct timeval tv, delta;
799 getmicrouptime(&tv); /* NB: 10ms precision */
801 timevalsub(&delta, lasttime);
804 * check for 0,0 is so that the message will be seen at least once,
805 * even if interval is huge.
807 if (timevalcmp(&delta, mininterval, >=) ||
808 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
817 * ppsratecheck(): packets (or events) per second limitation.
819 * Return 0 if the limit is to be enforced (e.g. the caller
820 * should drop a packet because of the rate limitation).
822 * maxpps of 0 always causes zero to be returned. maxpps of -1
823 * always causes 1 to be returned; this effectively defeats rate
826 * Note that we maintain the struct timeval for compatibility
827 * with other bsd systems. We reuse the storage and just monitor
828 * clock ticks for minimal overhead.
831 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
836 * Reset the last time and counter if this is the first call
837 * or more than a second has passed since the last update of
841 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
842 lasttime->tv_sec = now;
844 return (maxpps != 0);
846 (*curpps)++; /* NB: ignore potential overflow */
847 return (maxpps < 0 || *curpps < maxpps);
854 struct kclock rt_clock = {
855 .timer_create = realtimer_create,
856 .timer_delete = realtimer_delete,
857 .timer_settime = realtimer_settime,
858 .timer_gettime = realtimer_gettime,
862 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
863 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
864 register_posix_clock(CLOCK_REALTIME, &rt_clock);
865 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
866 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
867 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
868 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
869 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
870 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
871 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
872 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
876 register_posix_clock(int clockid, struct kclock *clk)
878 if ((unsigned)clockid >= MAX_CLOCKS) {
879 printf("%s: invalid clockid\n", __func__);
882 posix_clocks[clockid] = *clk;
887 itimer_init(void *mem, int size, int flags)
891 it = (struct itimer *)mem;
892 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
897 itimer_fini(void *mem, int size)
901 it = (struct itimer *)mem;
902 mtx_destroy(&it->it_mtx);
906 itimer_enter(struct itimer *it)
909 mtx_assert(&it->it_mtx, MA_OWNED);
914 itimer_leave(struct itimer *it)
917 mtx_assert(&it->it_mtx, MA_OWNED);
918 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
920 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
924 #ifndef _SYS_SYSPROTO_H_
925 struct ktimer_create_args {
927 struct sigevent * evp;
932 ktimer_create(struct thread *td, struct ktimer_create_args *uap)
934 struct sigevent *evp1, ev;
938 if (uap->evp != NULL) {
939 error = copyin(uap->evp, &ev, sizeof(ev));
946 error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
949 error = copyout(&id, uap->timerid, sizeof(int));
951 kern_timer_delete(td, id);
957 kern_timer_create(struct thread *td, clockid_t clock_id,
958 struct sigevent *evp, int *timerid, int preset_id)
960 struct proc *p = td->td_proc;
965 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
968 if (posix_clocks[clock_id].timer_create == NULL)
972 if (evp->sigev_notify != SIGEV_NONE &&
973 evp->sigev_notify != SIGEV_SIGNAL &&
974 evp->sigev_notify != SIGEV_THREAD_ID)
976 if ((evp->sigev_notify == SIGEV_SIGNAL ||
977 evp->sigev_notify == SIGEV_THREAD_ID) &&
978 !_SIG_VALID(evp->sigev_signo))
982 if (p->p_itimers == NULL)
985 it = uma_zalloc(itimer_zone, M_WAITOK);
989 timespecclear(&it->it_time.it_value);
990 timespecclear(&it->it_time.it_interval);
992 it->it_overrun_last = 0;
993 it->it_clockid = clock_id;
996 ksiginfo_init(&it->it_ksi);
997 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
998 error = CLOCK_CALL(clock_id, timer_create, (it));
1003 if (preset_id != -1) {
1004 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1006 if (p->p_itimers->its_timers[id] != NULL) {
1013 * Find a free timer slot, skipping those reserved
1016 for (id = 3; id < TIMER_MAX; id++)
1017 if (p->p_itimers->its_timers[id] == NULL)
1019 if (id == TIMER_MAX) {
1025 it->it_timerid = id;
1026 p->p_itimers->its_timers[id] = it;
1028 it->it_sigev = *evp;
1030 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1033 case CLOCK_REALTIME:
1034 it->it_sigev.sigev_signo = SIGALRM;
1037 it->it_sigev.sigev_signo = SIGVTALRM;
1040 it->it_sigev.sigev_signo = SIGPROF;
1043 it->it_sigev.sigev_value.sival_int = id;
1046 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1047 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1048 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1049 it->it_ksi.ksi_code = SI_TIMER;
1050 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1051 it->it_ksi.ksi_timerid = id;
1059 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1061 uma_zfree(itimer_zone, it);
1065 #ifndef _SYS_SYSPROTO_H_
1066 struct ktimer_delete_args {
1071 ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1073 return (kern_timer_delete(td, uap->timerid));
1076 static struct itimer *
1077 itimer_find(struct proc *p, int timerid)
1081 PROC_LOCK_ASSERT(p, MA_OWNED);
1082 if ((p->p_itimers == NULL) ||
1083 (timerid < 0) || (timerid >= TIMER_MAX) ||
1084 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1088 if ((it->it_flags & ITF_DELETING) != 0) {
1096 kern_timer_delete(struct thread *td, int timerid)
1098 struct proc *p = td->td_proc;
1102 it = itimer_find(p, timerid);
1109 it->it_flags |= ITF_DELETING;
1110 while (it->it_usecount > 0) {
1111 it->it_flags |= ITF_WANTED;
1112 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1114 it->it_flags &= ~ITF_WANTED;
1115 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1119 if (KSI_ONQ(&it->it_ksi))
1120 sigqueue_take(&it->it_ksi);
1121 p->p_itimers->its_timers[timerid] = NULL;
1123 uma_zfree(itimer_zone, it);
1127 #ifndef _SYS_SYSPROTO_H_
1128 struct ktimer_settime_args {
1131 const struct itimerspec * value;
1132 struct itimerspec * ovalue;
1136 ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1138 struct proc *p = td->td_proc;
1140 struct itimerspec val, oval, *ovalp;
1143 error = copyin(uap->value, &val, sizeof(val));
1147 if (uap->ovalue != NULL)
1153 if (uap->timerid < 3 ||
1154 (it = itimer_find(p, uap->timerid)) == NULL) {
1160 error = CLOCK_CALL(it->it_clockid, timer_settime,
1161 (it, uap->flags, &val, ovalp));
1165 if (error == 0 && uap->ovalue != NULL)
1166 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1170 #ifndef _SYS_SYSPROTO_H_
1171 struct ktimer_gettime_args {
1173 struct itimerspec * value;
1177 ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1179 struct proc *p = td->td_proc;
1181 struct itimerspec val;
1185 if (uap->timerid < 3 ||
1186 (it = itimer_find(p, uap->timerid)) == NULL) {
1192 error = CLOCK_CALL(it->it_clockid, timer_gettime,
1198 error = copyout(&val, uap->value, sizeof(val));
1202 #ifndef _SYS_SYSPROTO_H_
1203 struct timer_getoverrun_args {
1208 ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1210 struct proc *p = td->td_proc;
1215 if (uap->timerid < 3 ||
1216 (it = itimer_find(p, uap->timerid)) == NULL) {
1220 td->td_retval[0] = it->it_overrun_last;
1229 realtimer_create(struct itimer *it)
1231 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1236 realtimer_delete(struct itimer *it)
1238 mtx_assert(&it->it_mtx, MA_OWNED);
1241 * clear timer's value and interval to tell realtimer_expire
1242 * to not rearm the timer.
1244 timespecclear(&it->it_time.it_value);
1245 timespecclear(&it->it_time.it_interval);
1247 callout_drain(&it->it_callout);
1253 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1255 struct timespec cts;
1257 mtx_assert(&it->it_mtx, MA_OWNED);
1259 realtimer_clocktime(it->it_clockid, &cts);
1260 *ovalue = it->it_time;
1261 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1262 timespecsub(&ovalue->it_value, &cts);
1263 if (ovalue->it_value.tv_sec < 0 ||
1264 (ovalue->it_value.tv_sec == 0 &&
1265 ovalue->it_value.tv_nsec == 0)) {
1266 ovalue->it_value.tv_sec = 0;
1267 ovalue->it_value.tv_nsec = 1;
1274 realtimer_settime(struct itimer *it, int flags,
1275 struct itimerspec *value, struct itimerspec *ovalue)
1277 struct timespec cts, ts;
1279 struct itimerspec val;
1281 mtx_assert(&it->it_mtx, MA_OWNED);
1284 if (itimespecfix(&val.it_value))
1287 if (timespecisset(&val.it_value)) {
1288 if (itimespecfix(&val.it_interval))
1291 timespecclear(&val.it_interval);
1295 realtimer_gettime(it, ovalue);
1298 if (timespecisset(&val.it_value)) {
1299 realtimer_clocktime(it->it_clockid, &cts);
1301 if ((flags & TIMER_ABSTIME) == 0) {
1302 /* Convert to absolute time. */
1303 timespecadd(&it->it_time.it_value, &cts);
1305 timespecsub(&ts, &cts);
1307 * We don't care if ts is negative, tztohz will
1311 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1312 callout_reset(&it->it_callout, tvtohz(&tv),
1313 realtimer_expire, it);
1315 callout_stop(&it->it_callout);
1322 realtimer_clocktime(clockid_t id, struct timespec *ts)
1324 if (id == CLOCK_REALTIME)
1326 else /* CLOCK_MONOTONIC */
1331 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1335 PROC_LOCK_ASSERT(p, MA_OWNED);
1336 it = itimer_find(p, timerid);
1338 ksi->ksi_overrun = it->it_overrun;
1339 it->it_overrun_last = it->it_overrun;
1348 itimespecfix(struct timespec *ts)
1351 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1353 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1354 ts->tv_nsec = tick * 1000;
1358 /* Timeout callback for realtime timer */
1360 realtimer_expire(void *arg)
1362 struct timespec cts, ts;
1367 it = (struct itimer *)arg;
1370 realtimer_clocktime(it->it_clockid, &cts);
1371 /* Only fire if time is reached. */
1372 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1373 if (timespecisset(&it->it_time.it_interval)) {
1374 timespecadd(&it->it_time.it_value,
1375 &it->it_time.it_interval);
1376 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1377 if (it->it_overrun < INT_MAX)
1380 it->it_ksi.ksi_errno = ERANGE;
1381 timespecadd(&it->it_time.it_value,
1382 &it->it_time.it_interval);
1385 /* single shot timer ? */
1386 timespecclear(&it->it_time.it_value);
1388 if (timespecisset(&it->it_time.it_value)) {
1389 ts = it->it_time.it_value;
1390 timespecsub(&ts, &cts);
1391 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1392 callout_reset(&it->it_callout, tvtohz(&tv),
1393 realtimer_expire, it);
1400 } else if (timespecisset(&it->it_time.it_value)) {
1401 ts = it->it_time.it_value;
1402 timespecsub(&ts, &cts);
1403 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1404 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1410 itimer_fire(struct itimer *it)
1412 struct proc *p = it->it_proc;
1415 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1416 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1418 if (!KSI_ONQ(&it->it_ksi)) {
1419 it->it_ksi.ksi_errno = 0;
1420 ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
1421 if (__predict_false(ret != 0)) {
1424 * Broken userland code, thread went
1425 * away, disarm the timer.
1429 timespecclear(&it->it_time.it_value);
1430 timespecclear(&it->it_time.it_interval);
1431 callout_stop(&it->it_callout);
1436 if (it->it_overrun < INT_MAX)
1439 it->it_ksi.ksi_errno = ERANGE;
1446 itimers_alloc(struct proc *p)
1448 struct itimers *its;
1451 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1452 LIST_INIT(&its->its_virtual);
1453 LIST_INIT(&its->its_prof);
1454 TAILQ_INIT(&its->its_worklist);
1455 for (i = 0; i < TIMER_MAX; i++)
1456 its->its_timers[i] = NULL;
1458 if (p->p_itimers == NULL) {
1464 free(its, M_SUBPROC);
1469 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1471 itimers_event_hook_exit(arg, p);
1474 /* Clean up timers when some process events are being triggered. */
1476 itimers_event_hook_exit(void *arg, struct proc *p)
1478 struct itimers *its;
1480 int event = (int)(intptr_t)arg;
1483 if (p->p_itimers != NULL) {
1485 for (i = 0; i < MAX_CLOCKS; ++i) {
1486 if (posix_clocks[i].event_hook != NULL)
1487 CLOCK_CALL(i, event_hook, (p, i, event));
1490 * According to susv3, XSI interval timers should be inherited
1493 if (event == ITIMER_EV_EXEC)
1495 else if (event == ITIMER_EV_EXIT)
1498 panic("unhandled event");
1499 for (; i < TIMER_MAX; ++i) {
1500 if ((it = its->its_timers[i]) != NULL)
1501 kern_timer_delete(curthread, i);
1503 if (its->its_timers[0] == NULL &&
1504 its->its_timers[1] == NULL &&
1505 its->its_timers[2] == NULL) {
1506 free(its, M_SUBPROC);
1507 p->p_itimers = NULL;