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$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/limits.h>
41 #include <sys/mutex.h>
42 #include <sys/sysproto.h>
43 #include <sys/eventhandler.h>
44 #include <sys/resourcevar.h>
45 #include <sys/signalvar.h>
46 #include <sys/kernel.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/sysent.h>
53 #include <sys/timers.h>
54 #include <sys/timetc.h>
55 #include <sys/vnode.h>
57 #include <posix4/posix4.h>
60 #include <vm/vm_extern.h>
62 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
67 static struct kclock posix_clocks[MAX_CLOCKS];
68 static uma_zone_t itimer_zone = NULL;
71 * Time of day and interval timer support.
73 * These routines provide the kernel entry points to get and set
74 * the time-of-day and per-process interval timers. Subroutines
75 * here provide support for adding and subtracting timeval structures
76 * and decrementing interval timers, optionally reloading the interval
77 * timers when they expire.
80 static int settime(struct thread *, struct timeval *);
81 static void timevalfix(struct timeval *);
82 static void no_lease_updatetime(int);
84 static void itimer_start(void);
85 static int itimer_init(void *, int, int);
86 static void itimer_fini(void *, int);
87 static void itimer_enter(struct itimer *);
88 static void itimer_leave(struct itimer *);
89 static struct itimer *itimer_find(struct proc *, int, int);
90 static void itimers_alloc(struct proc *);
91 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
92 static void itimers_event_hook_exit(void *arg, struct proc *p);
93 static int realtimer_create(struct itimer *);
94 static int realtimer_gettime(struct itimer *, struct itimerspec *);
95 static int realtimer_settime(struct itimer *, int,
96 struct itimerspec *, struct itimerspec *);
97 static int realtimer_delete(struct itimer *);
98 static void realtimer_clocktime(clockid_t, struct timespec *);
99 static void realtimer_expire(void *);
100 static void realtimer_event_hook(struct proc *, clockid_t, int event);
101 static int kern_timer_create(struct thread *, clockid_t,
102 struct sigevent *, int *, int);
103 static int kern_timer_delete(struct thread *, int);
105 int register_posix_clock(int, struct kclock *);
106 void itimer_fire(struct itimer *it);
107 int itimespecfix(struct timespec *ts);
109 #define CLOCK_CALL(clock, call, arglist) \
110 ((*posix_clocks[clock].call) arglist)
112 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
116 no_lease_updatetime(deltat)
121 void (*lease_updatetime)(int) = no_lease_updatetime;
124 settime(struct thread *td, struct timeval *tv)
126 struct timeval delta, tv1, tv2;
127 static struct timeval maxtime, laststep;
134 timevalsub(&delta, &tv1);
137 * If the system is secure, we do not allow the time to be
138 * set to a value earlier than 1 second less than the highest
139 * time we have yet seen. The worst a miscreant can do in
140 * this circumstance is "freeze" time. He couldn't go
143 * We similarly do not allow the clock to be stepped more
144 * than one second, nor more than once per second. This allows
145 * a miscreant to make the clock march double-time, but no worse.
147 if (securelevel_gt(td->td_ucred, 1) != 0) {
148 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
150 * Update maxtime to latest time we've seen.
152 if (tv1.tv_sec > maxtime.tv_sec)
155 timevalsub(&tv2, &maxtime);
156 if (tv2.tv_sec < -1) {
157 tv->tv_sec = maxtime.tv_sec - 1;
158 printf("Time adjustment clamped to -1 second\n");
161 if (tv1.tv_sec == laststep.tv_sec) {
165 if (delta.tv_sec > 1) {
166 tv->tv_sec = tv1.tv_sec + 1;
167 printf("Time adjustment clamped to +1 second\n");
173 ts.tv_sec = tv->tv_sec;
174 ts.tv_nsec = tv->tv_usec * 1000;
177 (void) splsoftclock();
178 lease_updatetime(delta.tv_sec);
185 #ifndef _SYS_SYSPROTO_H_
186 struct clock_gettime_args {
197 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
202 error = kern_clock_gettime(td, uap->clock_id, &ats);
204 error = copyout(&ats, uap->tp, sizeof(ats));
210 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
212 struct timeval sys, user;
217 case CLOCK_REALTIME: /* Default to precise. */
218 case CLOCK_REALTIME_PRECISE:
221 case CLOCK_REALTIME_FAST:
226 calcru(p, &user, &sys);
228 TIMEVAL_TO_TIMESPEC(&user, ats);
232 calcru(p, &user, &sys);
234 timevaladd(&user, &sys);
235 TIMEVAL_TO_TIMESPEC(&user, ats);
237 case CLOCK_MONOTONIC: /* Default to precise. */
238 case CLOCK_MONOTONIC_PRECISE:
240 case CLOCK_UPTIME_PRECISE:
243 case CLOCK_UPTIME_FAST:
244 case CLOCK_MONOTONIC_FAST:
248 ats->tv_sec = time_second;
257 #ifndef _SYS_SYSPROTO_H_
258 struct clock_settime_args {
260 const struct timespec *tp;
269 clock_settime(struct thread *td, struct clock_settime_args *uap)
274 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
276 return (kern_clock_settime(td, uap->clock_id, &ats));
280 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
286 error = mac_check_system_settime(td->td_ucred);
290 if ((error = suser(td)) != 0)
292 if (clock_id != CLOCK_REALTIME)
294 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
296 /* XXX Don't convert nsec->usec and back */
297 TIMESPEC_TO_TIMEVAL(&atv, ats);
298 error = settime(td, &atv);
302 #ifndef _SYS_SYSPROTO_H_
303 struct clock_getres_args {
310 clock_getres(struct thread *td, struct clock_getres_args *uap)
318 error = kern_clock_getres(td, uap->clock_id, &ts);
320 error = copyout(&ts, uap->tp, sizeof(ts));
325 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
331 case CLOCK_REALTIME_FAST:
332 case CLOCK_REALTIME_PRECISE:
333 case CLOCK_MONOTONIC:
334 case CLOCK_MONOTONIC_FAST:
335 case CLOCK_MONOTONIC_PRECISE:
337 case CLOCK_UPTIME_FAST:
338 case CLOCK_UPTIME_PRECISE:
340 * Round up the result of the division cheaply by adding 1.
341 * Rounding up is especially important if rounding down
342 * would give 0. Perfect rounding is unimportant.
344 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
348 /* Accurately round up here because we can do so cheaply. */
349 ts->tv_nsec = (1000000000 + hz - 1) / hz;
364 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
366 struct timespec ts, ts2, ts3;
370 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
372 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
375 timespecadd(&ts, rqt);
376 TIMESPEC_TO_TIMEVAL(&tv, rqt);
378 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
381 if (error != EWOULDBLOCK) {
382 if (error == ERESTART)
385 timespecsub(&ts, &ts2);
392 if (timespeccmp(&ts2, &ts, >=))
395 timespecsub(&ts3, &ts2);
396 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
400 #ifndef _SYS_SYSPROTO_H_
401 struct nanosleep_args {
402 struct timespec *rqtp;
403 struct timespec *rmtp;
412 nanosleep(struct thread *td, struct nanosleep_args *uap)
414 struct timespec rmt, rqt;
417 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
422 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
424 error = kern_nanosleep(td, &rqt, &rmt);
425 if (error && uap->rmtp) {
428 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
435 #ifndef _SYS_SYSPROTO_H_
436 struct gettimeofday_args {
438 struct timezone *tzp;
446 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
454 error = copyout(&atv, uap->tp, sizeof (atv));
456 if (error == 0 && uap->tzp != NULL) {
457 rtz.tz_minuteswest = tz_minuteswest;
458 rtz.tz_dsttime = tz_dsttime;
459 error = copyout(&rtz, uap->tzp, sizeof (rtz));
464 #ifndef _SYS_SYSPROTO_H_
465 struct settimeofday_args {
467 struct timezone *tzp;
475 settimeofday(struct thread *td, struct settimeofday_args *uap)
477 struct timeval atv, *tvp;
478 struct timezone atz, *tzp;
482 error = copyin(uap->tv, &atv, sizeof(atv));
489 error = copyin(uap->tzp, &atz, sizeof(atz));
495 return (kern_settimeofday(td, tvp, tzp));
499 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
504 error = mac_check_system_settime(td->td_ucred);
511 /* Verify all parameters before changing time. */
513 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
515 error = settime(td, tv);
517 if (tzp && error == 0) {
518 tz_minuteswest = tzp->tz_minuteswest;
519 tz_dsttime = tzp->tz_dsttime;
525 * Get value of an interval timer. The process virtual and
526 * profiling virtual time timers are kept in the p_stats area, since
527 * they can be swapped out. These are kept internally in the
528 * way they are specified externally: in time until they expire.
530 * The real time interval timer is kept in the process table slot
531 * for the process, and its value (it_value) is kept as an
532 * absolute time rather than as a delta, so that it is easy to keep
533 * periodic real-time signals from drifting.
535 * Virtual time timers are processed in the hardclock() routine of
536 * kern_clock.c. The real time timer is processed by a timeout
537 * routine, called from the softclock() routine. Since a callout
538 * may be delayed in real time due to interrupt processing in the system,
539 * it is possible for the real time timeout routine (realitexpire, given below),
540 * to be delayed in real time past when it is supposed to occur. It
541 * does not suffice, therefore, to reload the real timer .it_value from the
542 * real time timers .it_interval. Rather, we compute the next time in
543 * absolute time the timer should go off.
545 #ifndef _SYS_SYSPROTO_H_
546 struct getitimer_args {
548 struct itimerval *itv;
555 getitimer(struct thread *td, struct getitimer_args *uap)
557 struct itimerval aitv;
560 error = kern_getitimer(td, uap->which, &aitv);
563 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
567 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
569 struct proc *p = td->td_proc;
572 if (which > ITIMER_PROF)
575 if (which == ITIMER_REAL) {
577 * Convert from absolute to relative time in .it_value
578 * part of real time timer. If time for real time timer
579 * has passed return 0, else return difference between
580 * current time and time for the timer to go off.
583 *aitv = p->p_realtimer;
585 if (timevalisset(&aitv->it_value)) {
586 getmicrouptime(&ctv);
587 if (timevalcmp(&aitv->it_value, &ctv, <))
588 timevalclear(&aitv->it_value);
590 timevalsub(&aitv->it_value, &ctv);
593 mtx_lock_spin(&sched_lock);
594 *aitv = p->p_stats->p_timer[which];
595 mtx_unlock_spin(&sched_lock);
600 #ifndef _SYS_SYSPROTO_H_
601 struct setitimer_args {
603 struct itimerval *itv, *oitv;
611 setitimer(struct thread *td, struct setitimer_args *uap)
613 struct itimerval aitv, oitv;
616 if (uap->itv == NULL) {
617 uap->itv = uap->oitv;
618 return (getitimer(td, (struct getitimer_args *)uap));
621 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
623 error = kern_setitimer(td, uap->which, &aitv, &oitv);
624 if (error != 0 || uap->oitv == NULL)
626 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
630 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
631 struct itimerval *oitv)
633 struct proc *p = td->td_proc;
637 return (kern_getitimer(td, which, oitv));
639 if (which > ITIMER_PROF)
641 if (itimerfix(&aitv->it_value))
643 if (!timevalisset(&aitv->it_value))
644 timevalclear(&aitv->it_interval);
645 else if (itimerfix(&aitv->it_interval))
648 if (which == ITIMER_REAL) {
650 if (timevalisset(&p->p_realtimer.it_value))
651 callout_stop(&p->p_itcallout);
652 getmicrouptime(&ctv);
653 if (timevalisset(&aitv->it_value)) {
654 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
656 timevaladd(&aitv->it_value, &ctv);
658 *oitv = p->p_realtimer;
659 p->p_realtimer = *aitv;
661 if (timevalisset(&oitv->it_value)) {
662 if (timevalcmp(&oitv->it_value, &ctv, <))
663 timevalclear(&oitv->it_value);
665 timevalsub(&oitv->it_value, &ctv);
668 mtx_lock_spin(&sched_lock);
669 *oitv = p->p_stats->p_timer[which];
670 p->p_stats->p_timer[which] = *aitv;
671 mtx_unlock_spin(&sched_lock);
677 * Real interval timer expired:
678 * send process whose timer expired an alarm signal.
679 * If time is not set up to reload, then just return.
680 * Else compute next time timer should go off which is > current time.
681 * This is where delay in processing this timeout causes multiple
682 * SIGALRM calls to be compressed into one.
683 * tvtohz() always adds 1 to allow for the time until the next clock
684 * interrupt being strictly less than 1 clock tick, but we don't want
685 * that here since we want to appear to be in sync with the clock
686 * interrupt even when we're delayed.
689 realitexpire(void *arg)
692 struct timeval ctv, ntv;
694 p = (struct proc *)arg;
697 if (!timevalisset(&p->p_realtimer.it_interval)) {
698 timevalclear(&p->p_realtimer.it_value);
699 if (p->p_flag & P_WEXIT)
700 wakeup(&p->p_itcallout);
705 timevaladd(&p->p_realtimer.it_value,
706 &p->p_realtimer.it_interval);
707 getmicrouptime(&ctv);
708 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
709 ntv = p->p_realtimer.it_value;
710 timevalsub(&ntv, &ctv);
711 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
721 * Check that a proposed value to load into the .it_value or
722 * .it_interval part of an interval timer is acceptable, and
723 * fix it to have at least minimal value (i.e. if it is less
724 * than the resolution of the clock, round it up.)
727 itimerfix(struct timeval *tv)
730 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
732 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
738 * Decrement an interval timer by a specified number
739 * of microseconds, which must be less than a second,
740 * i.e. < 1000000. If the timer expires, then reload
741 * it. In this case, carry over (usec - old value) to
742 * reduce the value reloaded into the timer so that
743 * the timer does not drift. This routine assumes
744 * that it is called in a context where the timers
745 * on which it is operating cannot change in value.
748 itimerdecr(struct itimerval *itp, int usec)
751 if (itp->it_value.tv_usec < usec) {
752 if (itp->it_value.tv_sec == 0) {
753 /* expired, and already in next interval */
754 usec -= itp->it_value.tv_usec;
757 itp->it_value.tv_usec += 1000000;
758 itp->it_value.tv_sec--;
760 itp->it_value.tv_usec -= usec;
762 if (timevalisset(&itp->it_value))
764 /* expired, exactly at end of interval */
766 if (timevalisset(&itp->it_interval)) {
767 itp->it_value = itp->it_interval;
768 itp->it_value.tv_usec -= usec;
769 if (itp->it_value.tv_usec < 0) {
770 itp->it_value.tv_usec += 1000000;
771 itp->it_value.tv_sec--;
774 itp->it_value.tv_usec = 0; /* sec is already 0 */
779 * Add and subtract routines for timevals.
780 * N.B.: subtract routine doesn't deal with
781 * results which are before the beginning,
782 * it just gets very confused in this case.
786 timevaladd(struct timeval *t1, const struct timeval *t2)
789 t1->tv_sec += t2->tv_sec;
790 t1->tv_usec += t2->tv_usec;
795 timevalsub(struct timeval *t1, const struct timeval *t2)
798 t1->tv_sec -= t2->tv_sec;
799 t1->tv_usec -= t2->tv_usec;
804 timevalfix(struct timeval *t1)
807 if (t1->tv_usec < 0) {
809 t1->tv_usec += 1000000;
811 if (t1->tv_usec >= 1000000) {
813 t1->tv_usec -= 1000000;
818 * ratecheck(): simple time-based rate-limit checking.
821 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
823 struct timeval tv, delta;
826 getmicrouptime(&tv); /* NB: 10ms precision */
828 timevalsub(&delta, lasttime);
831 * check for 0,0 is so that the message will be seen at least once,
832 * even if interval is huge.
834 if (timevalcmp(&delta, mininterval, >=) ||
835 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
844 * ppsratecheck(): packets (or events) per second limitation.
846 * Return 0 if the limit is to be enforced (e.g. the caller
847 * should drop a packet because of the rate limitation).
849 * maxpps of 0 always causes zero to be returned. maxpps of -1
850 * always causes 1 to be returned; this effectively defeats rate
853 * Note that we maintain the struct timeval for compatibility
854 * with other bsd systems. We reuse the storage and just monitor
855 * clock ticks for minimal overhead.
858 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
863 * Reset the last time and counter if this is the first call
864 * or more than a second has passed since the last update of
868 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
869 lasttime->tv_sec = now;
871 return (maxpps != 0);
873 (*curpps)++; /* NB: ignore potential overflow */
874 return (maxpps < 0 || *curpps < maxpps);
881 struct kclock rt_clock = {
882 .timer_create = realtimer_create,
883 .timer_delete = realtimer_delete,
884 .timer_settime = realtimer_settime,
885 .timer_gettime = realtimer_gettime,
886 .event_hook = realtimer_event_hook
889 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
890 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
891 register_posix_clock(CLOCK_REALTIME, &rt_clock);
892 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
893 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
894 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
895 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
896 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
897 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
898 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
899 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
903 register_posix_clock(int clockid, struct kclock *clk)
905 if ((unsigned)clockid >= MAX_CLOCKS) {
906 printf("%s: invalid clockid\n", __func__);
909 posix_clocks[clockid] = *clk;
914 itimer_init(void *mem, int size, int flags)
918 it = (struct itimer *)mem;
919 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
924 itimer_fini(void *mem, int size)
928 it = (struct itimer *)mem;
929 mtx_destroy(&it->it_mtx);
933 itimer_enter(struct itimer *it)
936 mtx_assert(&it->it_mtx, MA_OWNED);
941 itimer_leave(struct itimer *it)
944 mtx_assert(&it->it_mtx, MA_OWNED);
945 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
947 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
951 #ifndef _SYS_SYSPROTO_H_
952 struct ktimer_create_args {
954 struct sigevent * evp;
960 ktimer_create(struct thread *td, struct ktimer_create_args *uap)
962 struct sigevent *evp1, ev;
966 if (uap->evp != NULL) {
967 error = copyin(uap->evp, &ev, sizeof(ev));
974 error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
977 error = copyout(&id, uap->timerid, sizeof(int));
979 kern_timer_delete(td, id);
985 kern_timer_create(struct thread *td, clockid_t clock_id,
986 struct sigevent *evp, int *timerid, int preset_id)
988 struct proc *p = td->td_proc;
993 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
996 if (posix_clocks[clock_id].timer_create == NULL)
1000 if (evp->sigev_notify != SIGEV_NONE &&
1001 evp->sigev_notify != SIGEV_SIGNAL &&
1002 evp->sigev_notify != SIGEV_THREAD_ID)
1004 if ((evp->sigev_notify == SIGEV_SIGNAL ||
1005 evp->sigev_notify == SIGEV_THREAD_ID) &&
1006 !_SIG_VALID(evp->sigev_signo))
1010 if (p->p_itimers == NULL)
1013 it = uma_zalloc(itimer_zone, M_WAITOK);
1015 it->it_usecount = 0;
1017 timespecclear(&it->it_time.it_value);
1018 timespecclear(&it->it_time.it_interval);
1020 it->it_overrun_last = 0;
1021 it->it_clockid = clock_id;
1022 it->it_timerid = -1;
1024 ksiginfo_init(&it->it_ksi);
1025 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1026 error = CLOCK_CALL(clock_id, timer_create, (it));
1031 if (preset_id != -1) {
1032 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1034 if (p->p_itimers->its_timers[id] != NULL) {
1041 * Find a free timer slot, skipping those reserved
1044 for (id = 3; id < TIMER_MAX; id++)
1045 if (p->p_itimers->its_timers[id] == NULL)
1047 if (id == TIMER_MAX) {
1053 it->it_timerid = id;
1054 p->p_itimers->its_timers[id] = it;
1056 it->it_sigev = *evp;
1058 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1061 case CLOCK_REALTIME:
1062 it->it_sigev.sigev_signo = SIGALRM;
1065 it->it_sigev.sigev_signo = SIGVTALRM;
1068 it->it_sigev.sigev_signo = SIGPROF;
1071 it->it_sigev.sigev_value.sival_int = id;
1074 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1075 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1076 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1077 it->it_ksi.ksi_code = SI_TIMER;
1078 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1079 it->it_ksi.ksi_timerid = id;
1087 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1089 uma_zfree(itimer_zone, it);
1093 #ifndef _SYS_SYSPROTO_H_
1094 struct ktimer_delete_args {
1100 ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1102 return (kern_timer_delete(td, uap->timerid));
1105 static struct itimer *
1106 itimer_find(struct proc *p, int timerid, int include_deleting)
1110 PROC_LOCK_ASSERT(p, MA_OWNED);
1111 if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
1112 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1116 if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
1124 kern_timer_delete(struct thread *td, int timerid)
1126 struct proc *p = td->td_proc;
1130 it = itimer_find(p, timerid, 0);
1137 it->it_flags |= ITF_DELETING;
1138 while (it->it_usecount > 0) {
1139 it->it_flags |= ITF_WANTED;
1140 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1142 it->it_flags &= ~ITF_WANTED;
1143 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1147 if (KSI_ONQ(&it->it_ksi))
1148 sigqueue_take(&it->it_ksi);
1149 p->p_itimers->its_timers[timerid] = NULL;
1151 uma_zfree(itimer_zone, it);
1155 #ifndef _SYS_SYSPROTO_H_
1156 struct ktimer_settime_args {
1159 const struct itimerspec * value;
1160 struct itimerspec * ovalue;
1165 ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1167 struct proc *p = td->td_proc;
1169 struct itimerspec val, oval, *ovalp;
1172 error = copyin(uap->value, &val, sizeof(val));
1176 if (uap->ovalue != NULL)
1182 if (uap->timerid < 3 ||
1183 (it = itimer_find(p, uap->timerid, 0)) == NULL) {
1189 error = CLOCK_CALL(it->it_clockid, timer_settime,
1190 (it, uap->flags, &val, ovalp));
1194 if (error == 0 && uap->ovalue != NULL)
1195 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1199 #ifndef _SYS_SYSPROTO_H_
1200 struct ktimer_gettime_args {
1202 struct itimerspec * value;
1207 ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1209 struct proc *p = td->td_proc;
1211 struct itimerspec val;
1215 if (uap->timerid < 3 ||
1216 (it = itimer_find(p, uap->timerid, 0)) == NULL) {
1222 error = CLOCK_CALL(it->it_clockid, timer_gettime,
1228 error = copyout(&val, uap->value, sizeof(val));
1232 #ifndef _SYS_SYSPROTO_H_
1233 struct timer_getoverrun_args {
1239 ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1241 struct proc *p = td->td_proc;
1246 if (uap->timerid < 3 ||
1247 (it = itimer_find(p, uap->timerid, 0)) == NULL) {
1251 td->td_retval[0] = it->it_overrun_last;
1260 realtimer_create(struct itimer *it)
1262 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1267 realtimer_delete(struct itimer *it)
1269 mtx_assert(&it->it_mtx, MA_OWNED);
1270 callout_stop(&it->it_callout);
1275 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1277 struct timespec cts;
1279 mtx_assert(&it->it_mtx, MA_OWNED);
1281 realtimer_clocktime(it->it_clockid, &cts);
1282 *ovalue = it->it_time;
1283 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1284 timespecsub(&ovalue->it_value, &cts);
1285 if (ovalue->it_value.tv_sec < 0 ||
1286 (ovalue->it_value.tv_sec == 0 &&
1287 ovalue->it_value.tv_nsec == 0)) {
1288 ovalue->it_value.tv_sec = 0;
1289 ovalue->it_value.tv_nsec = 1;
1296 realtimer_settime(struct itimer *it, int flags,
1297 struct itimerspec *value, struct itimerspec *ovalue)
1299 struct timespec cts, ts;
1301 struct itimerspec val;
1303 mtx_assert(&it->it_mtx, MA_OWNED);
1306 if (itimespecfix(&val.it_value))
1309 if (timespecisset(&val.it_value)) {
1310 if (itimespecfix(&val.it_interval))
1313 timespecclear(&val.it_interval);
1317 realtimer_gettime(it, ovalue);
1320 if (timespecisset(&val.it_value)) {
1321 realtimer_clocktime(it->it_clockid, &cts);
1323 if ((flags & TIMER_ABSTIME) == 0) {
1324 /* Convert to absolute time. */
1325 timespecadd(&it->it_time.it_value, &cts);
1327 timespecsub(&ts, &cts);
1329 * We don't care if ts is negative, tztohz will
1333 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1334 callout_reset(&it->it_callout, tvtohz(&tv),
1335 realtimer_expire, it);
1337 callout_stop(&it->it_callout);
1344 realtimer_clocktime(clockid_t id, struct timespec *ts)
1346 if (id == CLOCK_REALTIME)
1348 else /* CLOCK_MONOTONIC */
1353 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1357 PROC_LOCK_ASSERT(p, MA_OWNED);
1358 it = itimer_find(p, timerid, 0);
1360 ksi->ksi_overrun = it->it_overrun;
1361 it->it_overrun_last = it->it_overrun;
1370 itimespecfix(struct timespec *ts)
1373 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1375 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1376 ts->tv_nsec = tick * 1000;
1381 realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
1383 struct itimers *its;
1388 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
1389 * specification, it should be inherited by new process image.
1391 if (event == ITIMER_EV_EXEC)
1396 for (; i < TIMER_MAX; i++) {
1397 if ((it = its->its_timers[i]) != NULL &&
1398 it->it_clockid == clock_id) {
1400 callout_stop(&it->it_callout);
1406 /* Timeout callback for realtime timer */
1408 realtimer_expire(void *arg)
1410 struct timespec cts, ts;
1415 it = (struct itimer *)arg;
1418 realtimer_clocktime(it->it_clockid, &cts);
1419 /* Only fire if time is reached. */
1420 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1421 if (timespecisset(&it->it_time.it_interval)) {
1422 timespecadd(&it->it_time.it_value,
1423 &it->it_time.it_interval);
1424 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1425 if (it->it_overrun < INT_MAX)
1428 it->it_ksi.ksi_errno = ERANGE;
1429 timespecadd(&it->it_time.it_value,
1430 &it->it_time.it_interval);
1433 /* single shot timer ? */
1434 timespecclear(&it->it_time.it_value);
1436 if (timespecisset(&it->it_time.it_value)) {
1437 ts = it->it_time.it_value;
1438 timespecsub(&ts, &cts);
1439 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1440 callout_reset(&it->it_callout, tvtohz(&tv),
1441 realtimer_expire, it);
1446 } else if (timespecisset(&it->it_time.it_value)) {
1447 ts = it->it_time.it_value;
1448 timespecsub(&ts, &cts);
1449 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1450 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1456 itimer_fire(struct itimer *it)
1458 struct proc *p = it->it_proc;
1461 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1462 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1464 if (!KSI_ONQ(&it->it_ksi)) {
1465 it->it_ksi.ksi_errno = 0;
1466 ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
1467 if (__predict_false(ret != 0)) {
1470 * Broken userland code, thread went
1471 * away, disarm the timer.
1475 timespecclear(&it->it_time.it_value);
1476 timespecclear(&it->it_time.it_interval);
1477 callout_stop(&it->it_callout);
1482 if (it->it_overrun < INT_MAX)
1485 it->it_ksi.ksi_errno = ERANGE;
1492 itimers_alloc(struct proc *p)
1494 struct itimers *its;
1497 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1498 LIST_INIT(&its->its_virtual);
1499 LIST_INIT(&its->its_prof);
1500 TAILQ_INIT(&its->its_worklist);
1501 for (i = 0; i < TIMER_MAX; i++)
1502 its->its_timers[i] = NULL;
1504 if (p->p_itimers == NULL) {
1510 free(its, M_SUBPROC);
1515 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1517 itimers_event_hook_exit(arg, p);
1520 /* Clean up timers when some process events are being triggered. */
1522 itimers_event_hook_exit(void *arg, struct proc *p)
1524 struct itimers *its;
1526 int event = (int)(intptr_t)arg;
1529 if (p->p_itimers != NULL) {
1531 for (i = 0; i < MAX_CLOCKS; ++i) {
1532 if (posix_clocks[i].event_hook != NULL)
1533 CLOCK_CALL(i, event_hook, (p, i, event));
1536 * According to susv3, XSI interval timers should be inherited
1539 if (event == ITIMER_EV_EXEC)
1541 else if (event == ITIMER_EV_EXIT)
1544 panic("unhandled event");
1545 for (; i < TIMER_MAX; ++i) {
1546 if ((it = its->its_timers[i]) != NULL) {
1548 if (KSI_ONQ(&it->it_ksi))
1549 sigqueue_take(&it->it_ksi);
1551 uma_zfree(itimer_zone, its->its_timers[i]);
1552 its->its_timers[i] = NULL;
1555 if (its->its_timers[0] == NULL &&
1556 its->its_timers[1] == NULL &&
1557 its->its_timers[2] == NULL) {
1558 free(its, M_SUBPROC);
1559 p->p_itimers = NULL;