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 * 3. 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 "opt_ktrace.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/limits.h>
40 #include <sys/clock.h>
42 #include <sys/mutex.h>
43 #include <sys/sysproto.h>
44 #include <sys/eventhandler.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/kernel.h>
48 #include <sys/sleepqueue.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysctl.h>
51 #include <sys/sysent.h>
54 #include <sys/posix4.h>
56 #include <sys/timers.h>
57 #include <sys/timetc.h>
58 #include <sys/vnode.h>
60 #include <sys/ktrace.h>
64 #include <vm/vm_extern.h>
66 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
67 #define CPUCLOCK_BIT 0x80000000
68 #define CPUCLOCK_PROCESS_BIT 0x40000000
69 #define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
70 #define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid))
71 #define MAKE_PROCESS_CPUCLOCK(pid) \
72 (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
74 static struct kclock posix_clocks[MAX_CLOCKS];
75 static uma_zone_t itimer_zone = NULL;
78 * Time of day and interval timer support.
80 * These routines provide the kernel entry points to get and set
81 * the time-of-day and per-process interval timers. Subroutines
82 * here provide support for adding and subtracting timeval structures
83 * and decrementing interval timers, optionally reloading the interval
84 * timers when they expire.
87 static int settime(struct thread *, struct timeval *);
88 static void timevalfix(struct timeval *);
90 static void itimer_start(void);
91 static int itimer_init(void *, int, int);
92 static void itimer_fini(void *, int);
93 static void itimer_enter(struct itimer *);
94 static void itimer_leave(struct itimer *);
95 static struct itimer *itimer_find(struct proc *, int);
96 static void itimers_alloc(struct proc *);
97 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
98 static void itimers_event_hook_exit(void *arg, struct proc *p);
99 static int realtimer_create(struct itimer *);
100 static int realtimer_gettime(struct itimer *, struct itimerspec *);
101 static int realtimer_settime(struct itimer *, int,
102 struct itimerspec *, struct itimerspec *);
103 static int realtimer_delete(struct itimer *);
104 static void realtimer_clocktime(clockid_t, struct timespec *);
105 static void realtimer_expire(void *);
107 int register_posix_clock(int, struct kclock *);
108 void itimer_fire(struct itimer *it);
109 int itimespecfix(struct timespec *ts);
111 #define CLOCK_CALL(clock, call, arglist) \
112 ((*posix_clocks[clock].call) arglist)
114 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
118 settime(struct thread *td, struct timeval *tv)
120 struct timeval delta, tv1, tv2;
121 static struct timeval maxtime, laststep;
126 timevalsub(&delta, &tv1);
129 * If the system is secure, we do not allow the time to be
130 * set to a value earlier than 1 second less than the highest
131 * time we have yet seen. The worst a miscreant can do in
132 * this circumstance is "freeze" time. He couldn't go
135 * We similarly do not allow the clock to be stepped more
136 * than one second, nor more than once per second. This allows
137 * a miscreant to make the clock march double-time, but no worse.
139 if (securelevel_gt(td->td_ucred, 1) != 0) {
140 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
142 * Update maxtime to latest time we've seen.
144 if (tv1.tv_sec > maxtime.tv_sec)
147 timevalsub(&tv2, &maxtime);
148 if (tv2.tv_sec < -1) {
149 tv->tv_sec = maxtime.tv_sec - 1;
150 printf("Time adjustment clamped to -1 second\n");
153 if (tv1.tv_sec == laststep.tv_sec)
155 if (delta.tv_sec > 1) {
156 tv->tv_sec = tv1.tv_sec + 1;
157 printf("Time adjustment clamped to +1 second\n");
163 ts.tv_sec = tv->tv_sec;
164 ts.tv_nsec = tv->tv_usec * 1000;
170 #ifndef _SYS_SYSPROTO_H_
171 struct clock_getcpuclockid2_args {
179 sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
184 error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
186 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
191 kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
200 case CPUCLOCK_WHICH_PID:
202 error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
208 pid = td->td_proc->p_pid;
210 *clk_id = MAKE_PROCESS_CPUCLOCK(pid);
212 case CPUCLOCK_WHICH_TID:
213 tid = id == 0 ? td->td_tid : id;
214 *clk_id = MAKE_THREAD_CPUCLOCK(tid);
221 #ifndef _SYS_SYSPROTO_H_
222 struct clock_gettime_args {
229 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
234 error = kern_clock_gettime(td, uap->clock_id, &ats);
236 error = copyout(&ats, uap->tp, sizeof(ats));
242 cputick2timespec(uint64_t runtime, struct timespec *ats)
244 runtime = cputick2usec(runtime);
245 ats->tv_sec = runtime / 1000000;
246 ats->tv_nsec = runtime % 1000000 * 1000;
250 get_thread_cputime(struct thread *targettd, struct timespec *ats)
252 uint64_t runtime, curtime, switchtime;
254 if (targettd == NULL) { /* current thread */
256 switchtime = PCPU_GET(switchtime);
257 curtime = cpu_ticks();
258 runtime = curthread->td_runtime;
260 runtime += curtime - switchtime;
262 thread_lock(targettd);
263 runtime = targettd->td_runtime;
264 thread_unlock(targettd);
266 cputick2timespec(runtime, ats);
270 get_process_cputime(struct proc *targetp, struct timespec *ats)
275 PROC_STATLOCK(targetp);
276 rufetch(targetp, &ru);
277 runtime = targetp->p_rux.rux_runtime;
278 PROC_STATUNLOCK(targetp);
279 cputick2timespec(runtime, ats);
283 get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
292 if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
293 tid = clock_id & CPUCLOCK_ID_MASK;
294 td2 = tdfind(tid, p->p_pid);
297 get_thread_cputime(td2, ats);
298 PROC_UNLOCK(td2->td_proc);
300 pid = clock_id & CPUCLOCK_ID_MASK;
301 error = pget(pid, PGET_CANSEE, &p2);
304 get_process_cputime(p2, ats);
311 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
313 struct timeval sys, user;
318 case CLOCK_REALTIME: /* Default to precise. */
319 case CLOCK_REALTIME_PRECISE:
322 case CLOCK_REALTIME_FAST:
328 calcru(p, &user, &sys);
331 TIMEVAL_TO_TIMESPEC(&user, ats);
336 calcru(p, &user, &sys);
339 timevaladd(&user, &sys);
340 TIMEVAL_TO_TIMESPEC(&user, ats);
342 case CLOCK_MONOTONIC: /* Default to precise. */
343 case CLOCK_MONOTONIC_PRECISE:
345 case CLOCK_UPTIME_PRECISE:
348 case CLOCK_UPTIME_FAST:
349 case CLOCK_MONOTONIC_FAST:
353 ats->tv_sec = time_second;
356 case CLOCK_THREAD_CPUTIME_ID:
357 get_thread_cputime(NULL, ats);
359 case CLOCK_PROCESS_CPUTIME_ID:
361 get_process_cputime(p, ats);
365 if ((int)clock_id >= 0)
367 return (get_cputime(td, clock_id, ats));
372 #ifndef _SYS_SYSPROTO_H_
373 struct clock_settime_args {
375 const struct timespec *tp;
380 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
385 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
387 return (kern_clock_settime(td, uap->clock_id, &ats));
390 static int allow_insane_settime = 0;
391 SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN,
392 &allow_insane_settime, 0,
393 "do not perform possibly restrictive checks on settime(2) args");
396 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
401 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
403 if (clock_id != CLOCK_REALTIME)
405 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000 ||
408 if (!allow_insane_settime && ats->tv_sec > 9999ULL * 366 * 24 * 60 * 60)
410 /* XXX Don't convert nsec->usec and back */
411 TIMESPEC_TO_TIMEVAL(&atv, ats);
412 error = settime(td, &atv);
416 #ifndef _SYS_SYSPROTO_H_
417 struct clock_getres_args {
423 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
431 error = kern_clock_getres(td, uap->clock_id, &ts);
433 error = copyout(&ts, uap->tp, sizeof(ts));
438 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
444 case CLOCK_REALTIME_FAST:
445 case CLOCK_REALTIME_PRECISE:
446 case CLOCK_MONOTONIC:
447 case CLOCK_MONOTONIC_FAST:
448 case CLOCK_MONOTONIC_PRECISE:
450 case CLOCK_UPTIME_FAST:
451 case CLOCK_UPTIME_PRECISE:
453 * Round up the result of the division cheaply by adding 1.
454 * Rounding up is especially important if rounding down
455 * would give 0. Perfect rounding is unimportant.
457 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
461 /* Accurately round up here because we can do so cheaply. */
462 ts->tv_nsec = howmany(1000000000, hz);
468 case CLOCK_THREAD_CPUTIME_ID:
469 case CLOCK_PROCESS_CPUTIME_ID:
471 /* sync with cputick2usec */
472 ts->tv_nsec = 1000000 / cpu_tickrate();
473 if (ts->tv_nsec == 0)
477 if ((int)clock_id < 0)
484 static uint8_t nanowait[MAXCPU];
487 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
490 sbintime_t sbt, sbtt, prec, tmp;
494 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
496 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
499 if (ts.tv_sec > INT32_MAX / 2) {
500 over = ts.tv_sec - INT32_MAX / 2;
507 if (TIMESEL(&sbt, tmp))
510 error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
511 sbt, prec, C_ABSOLUTE);
512 if (error != EWOULDBLOCK) {
513 if (error == ERESTART)
517 ts = sbttots(sbt - sbtt);
530 #ifndef _SYS_SYSPROTO_H_
531 struct nanosleep_args {
532 struct timespec *rqtp;
533 struct timespec *rmtp;
538 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
540 struct timespec rmt, rqt;
543 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
548 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
550 error = kern_nanosleep(td, &rqt, &rmt);
551 if (error && uap->rmtp) {
554 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
561 #ifndef _SYS_SYSPROTO_H_
562 struct gettimeofday_args {
564 struct timezone *tzp;
569 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
577 error = copyout(&atv, uap->tp, sizeof (atv));
579 if (error == 0 && uap->tzp != NULL) {
580 rtz.tz_minuteswest = tz_minuteswest;
581 rtz.tz_dsttime = tz_dsttime;
582 error = copyout(&rtz, uap->tzp, sizeof (rtz));
587 #ifndef _SYS_SYSPROTO_H_
588 struct settimeofday_args {
590 struct timezone *tzp;
595 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
597 struct timeval atv, *tvp;
598 struct timezone atz, *tzp;
602 error = copyin(uap->tv, &atv, sizeof(atv));
609 error = copyin(uap->tzp, &atz, sizeof(atz));
615 return (kern_settimeofday(td, tvp, tzp));
619 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
623 error = priv_check(td, PRIV_SETTIMEOFDAY);
626 /* Verify all parameters before changing time. */
628 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
631 error = settime(td, tv);
633 if (tzp && error == 0) {
634 tz_minuteswest = tzp->tz_minuteswest;
635 tz_dsttime = tzp->tz_dsttime;
641 * Get value of an interval timer. The process virtual and profiling virtual
642 * time timers are kept in the p_stats area, since they can be swapped out.
643 * These are kept internally in the way they are specified externally: in
644 * time until they expire.
646 * The real time interval timer is kept in the process table slot for the
647 * process, and its value (it_value) is kept as an absolute time rather than
648 * as a delta, so that it is easy to keep periodic real-time signals from
651 * Virtual time timers are processed in the hardclock() routine of
652 * kern_clock.c. The real time timer is processed by a timeout routine,
653 * called from the softclock() routine. Since a callout may be delayed in
654 * real time due to interrupt processing in the system, it is possible for
655 * the real time timeout routine (realitexpire, given below), to be delayed
656 * in real time past when it is supposed to occur. It does not suffice,
657 * therefore, to reload the real timer .it_value from the real time timers
658 * .it_interval. Rather, we compute the next time in absolute time the timer
661 #ifndef _SYS_SYSPROTO_H_
662 struct getitimer_args {
664 struct itimerval *itv;
668 sys_getitimer(struct thread *td, struct getitimer_args *uap)
670 struct itimerval aitv;
673 error = kern_getitimer(td, uap->which, &aitv);
676 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
680 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
682 struct proc *p = td->td_proc;
685 if (which > ITIMER_PROF)
688 if (which == ITIMER_REAL) {
690 * Convert from absolute to relative time in .it_value
691 * part of real time timer. If time for real time timer
692 * has passed return 0, else return difference between
693 * current time and time for the timer to go off.
696 *aitv = p->p_realtimer;
698 if (timevalisset(&aitv->it_value)) {
700 if (timevalcmp(&aitv->it_value, &ctv, <))
701 timevalclear(&aitv->it_value);
703 timevalsub(&aitv->it_value, &ctv);
707 *aitv = p->p_stats->p_timer[which];
711 if (KTRPOINT(td, KTR_STRUCT))
717 #ifndef _SYS_SYSPROTO_H_
718 struct setitimer_args {
720 struct itimerval *itv, *oitv;
724 sys_setitimer(struct thread *td, struct setitimer_args *uap)
726 struct itimerval aitv, oitv;
729 if (uap->itv == NULL) {
730 uap->itv = uap->oitv;
731 return (sys_getitimer(td, (struct getitimer_args *)uap));
734 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
736 error = kern_setitimer(td, uap->which, &aitv, &oitv);
737 if (error != 0 || uap->oitv == NULL)
739 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
743 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
744 struct itimerval *oitv)
746 struct proc *p = td->td_proc;
751 return (kern_getitimer(td, which, oitv));
753 if (which > ITIMER_PROF)
756 if (KTRPOINT(td, KTR_STRUCT))
759 if (itimerfix(&aitv->it_value) ||
760 aitv->it_value.tv_sec > INT32_MAX / 2)
762 if (!timevalisset(&aitv->it_value))
763 timevalclear(&aitv->it_interval);
764 else if (itimerfix(&aitv->it_interval) ||
765 aitv->it_interval.tv_sec > INT32_MAX / 2)
768 if (which == ITIMER_REAL) {
770 if (timevalisset(&p->p_realtimer.it_value))
771 callout_stop(&p->p_itcallout);
773 if (timevalisset(&aitv->it_value)) {
774 pr = tvtosbt(aitv->it_value) >> tc_precexp;
775 timevaladd(&aitv->it_value, &ctv);
776 sbt = tvtosbt(aitv->it_value);
777 callout_reset_sbt(&p->p_itcallout, sbt, pr,
778 realitexpire, p, C_ABSOLUTE);
780 *oitv = p->p_realtimer;
781 p->p_realtimer = *aitv;
783 if (timevalisset(&oitv->it_value)) {
784 if (timevalcmp(&oitv->it_value, &ctv, <))
785 timevalclear(&oitv->it_value);
787 timevalsub(&oitv->it_value, &ctv);
790 if (aitv->it_interval.tv_sec == 0 &&
791 aitv->it_interval.tv_usec != 0 &&
792 aitv->it_interval.tv_usec < tick)
793 aitv->it_interval.tv_usec = tick;
794 if (aitv->it_value.tv_sec == 0 &&
795 aitv->it_value.tv_usec != 0 &&
796 aitv->it_value.tv_usec < tick)
797 aitv->it_value.tv_usec = tick;
799 *oitv = p->p_stats->p_timer[which];
800 p->p_stats->p_timer[which] = *aitv;
804 if (KTRPOINT(td, KTR_STRUCT))
811 * Real interval timer expired:
812 * send process whose timer expired an alarm signal.
813 * If time is not set up to reload, then just return.
814 * Else compute next time timer should go off which is > current time.
815 * This is where delay in processing this timeout causes multiple
816 * SIGALRM calls to be compressed into one.
817 * tvtohz() always adds 1 to allow for the time until the next clock
818 * interrupt being strictly less than 1 clock tick, but we don't want
819 * that here since we want to appear to be in sync with the clock
820 * interrupt even when we're delayed.
823 realitexpire(void *arg)
829 p = (struct proc *)arg;
830 kern_psignal(p, SIGALRM);
831 if (!timevalisset(&p->p_realtimer.it_interval)) {
832 timevalclear(&p->p_realtimer.it_value);
833 if (p->p_flag & P_WEXIT)
834 wakeup(&p->p_itcallout);
837 isbt = tvtosbt(p->p_realtimer.it_interval);
838 if (isbt >= sbt_timethreshold)
839 getmicrouptime(&ctv);
843 timevaladd(&p->p_realtimer.it_value,
844 &p->p_realtimer.it_interval);
845 } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
846 callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
847 isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
851 * Check that a proposed value to load into the .it_value or
852 * .it_interval part of an interval timer is acceptable, and
853 * fix it to have at least minimal value (i.e. if it is less
854 * than the resolution of the clock, round it up.)
857 itimerfix(struct timeval *tv)
860 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
862 if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
863 tv->tv_usec < (u_int)tick / 16)
864 tv->tv_usec = (u_int)tick / 16;
869 * Decrement an interval timer by a specified number
870 * of microseconds, which must be less than a second,
871 * i.e. < 1000000. If the timer expires, then reload
872 * it. In this case, carry over (usec - old value) to
873 * reduce the value reloaded into the timer so that
874 * the timer does not drift. This routine assumes
875 * that it is called in a context where the timers
876 * on which it is operating cannot change in value.
879 itimerdecr(struct itimerval *itp, int usec)
882 if (itp->it_value.tv_usec < usec) {
883 if (itp->it_value.tv_sec == 0) {
884 /* expired, and already in next interval */
885 usec -= itp->it_value.tv_usec;
888 itp->it_value.tv_usec += 1000000;
889 itp->it_value.tv_sec--;
891 itp->it_value.tv_usec -= usec;
893 if (timevalisset(&itp->it_value))
895 /* expired, exactly at end of interval */
897 if (timevalisset(&itp->it_interval)) {
898 itp->it_value = itp->it_interval;
899 itp->it_value.tv_usec -= usec;
900 if (itp->it_value.tv_usec < 0) {
901 itp->it_value.tv_usec += 1000000;
902 itp->it_value.tv_sec--;
905 itp->it_value.tv_usec = 0; /* sec is already 0 */
910 * Add and subtract routines for timevals.
911 * N.B.: subtract routine doesn't deal with
912 * results which are before the beginning,
913 * it just gets very confused in this case.
917 timevaladd(struct timeval *t1, const struct timeval *t2)
920 t1->tv_sec += t2->tv_sec;
921 t1->tv_usec += t2->tv_usec;
926 timevalsub(struct timeval *t1, const struct timeval *t2)
929 t1->tv_sec -= t2->tv_sec;
930 t1->tv_usec -= t2->tv_usec;
935 timevalfix(struct timeval *t1)
938 if (t1->tv_usec < 0) {
940 t1->tv_usec += 1000000;
942 if (t1->tv_usec >= 1000000) {
944 t1->tv_usec -= 1000000;
949 * ratecheck(): simple time-based rate-limit checking.
952 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
954 struct timeval tv, delta;
957 getmicrouptime(&tv); /* NB: 10ms precision */
959 timevalsub(&delta, lasttime);
962 * check for 0,0 is so that the message will be seen at least once,
963 * even if interval is huge.
965 if (timevalcmp(&delta, mininterval, >=) ||
966 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
975 * ppsratecheck(): packets (or events) per second limitation.
977 * Return 0 if the limit is to be enforced (e.g. the caller
978 * should drop a packet because of the rate limitation).
980 * maxpps of 0 always causes zero to be returned. maxpps of -1
981 * always causes 1 to be returned; this effectively defeats rate
984 * Note that we maintain the struct timeval for compatibility
985 * with other bsd systems. We reuse the storage and just monitor
986 * clock ticks for minimal overhead.
989 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
994 * Reset the last time and counter if this is the first call
995 * or more than a second has passed since the last update of
999 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1000 lasttime->tv_sec = now;
1002 return (maxpps != 0);
1004 (*curpps)++; /* NB: ignore potential overflow */
1005 return (maxpps < 0 || *curpps <= maxpps);
1012 struct kclock rt_clock = {
1013 .timer_create = realtimer_create,
1014 .timer_delete = realtimer_delete,
1015 .timer_settime = realtimer_settime,
1016 .timer_gettime = realtimer_gettime,
1020 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
1021 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
1022 register_posix_clock(CLOCK_REALTIME, &rt_clock);
1023 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
1024 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
1025 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
1026 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1027 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1028 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1029 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1030 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
1034 register_posix_clock(int clockid, struct kclock *clk)
1036 if ((unsigned)clockid >= MAX_CLOCKS) {
1037 printf("%s: invalid clockid\n", __func__);
1040 posix_clocks[clockid] = *clk;
1045 itimer_init(void *mem, int size, int flags)
1049 it = (struct itimer *)mem;
1050 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1055 itimer_fini(void *mem, int size)
1059 it = (struct itimer *)mem;
1060 mtx_destroy(&it->it_mtx);
1064 itimer_enter(struct itimer *it)
1067 mtx_assert(&it->it_mtx, MA_OWNED);
1072 itimer_leave(struct itimer *it)
1075 mtx_assert(&it->it_mtx, MA_OWNED);
1076 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1078 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1082 #ifndef _SYS_SYSPROTO_H_
1083 struct ktimer_create_args {
1085 struct sigevent * evp;
1090 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1092 struct sigevent *evp, ev;
1096 if (uap->evp == NULL) {
1099 error = copyin(uap->evp, &ev, sizeof(ev));
1104 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1106 error = copyout(&id, uap->timerid, sizeof(int));
1108 kern_ktimer_delete(td, id);
1114 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1115 int *timerid, int preset_id)
1117 struct proc *p = td->td_proc;
1122 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1125 if (posix_clocks[clock_id].timer_create == NULL)
1129 if (evp->sigev_notify != SIGEV_NONE &&
1130 evp->sigev_notify != SIGEV_SIGNAL &&
1131 evp->sigev_notify != SIGEV_THREAD_ID)
1133 if ((evp->sigev_notify == SIGEV_SIGNAL ||
1134 evp->sigev_notify == SIGEV_THREAD_ID) &&
1135 !_SIG_VALID(evp->sigev_signo))
1139 if (p->p_itimers == NULL)
1142 it = uma_zalloc(itimer_zone, M_WAITOK);
1144 it->it_usecount = 0;
1146 timespecclear(&it->it_time.it_value);
1147 timespecclear(&it->it_time.it_interval);
1149 it->it_overrun_last = 0;
1150 it->it_clockid = clock_id;
1151 it->it_timerid = -1;
1153 ksiginfo_init(&it->it_ksi);
1154 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1155 error = CLOCK_CALL(clock_id, timer_create, (it));
1160 if (preset_id != -1) {
1161 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1163 if (p->p_itimers->its_timers[id] != NULL) {
1170 * Find a free timer slot, skipping those reserved
1173 for (id = 3; id < TIMER_MAX; id++)
1174 if (p->p_itimers->its_timers[id] == NULL)
1176 if (id == TIMER_MAX) {
1182 it->it_timerid = id;
1183 p->p_itimers->its_timers[id] = it;
1185 it->it_sigev = *evp;
1187 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1190 case CLOCK_REALTIME:
1191 it->it_sigev.sigev_signo = SIGALRM;
1194 it->it_sigev.sigev_signo = SIGVTALRM;
1197 it->it_sigev.sigev_signo = SIGPROF;
1200 it->it_sigev.sigev_value.sival_int = id;
1203 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1204 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1205 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1206 it->it_ksi.ksi_code = SI_TIMER;
1207 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1208 it->it_ksi.ksi_timerid = id;
1216 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1218 uma_zfree(itimer_zone, it);
1222 #ifndef _SYS_SYSPROTO_H_
1223 struct ktimer_delete_args {
1228 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1231 return (kern_ktimer_delete(td, uap->timerid));
1234 static struct itimer *
1235 itimer_find(struct proc *p, int timerid)
1239 PROC_LOCK_ASSERT(p, MA_OWNED);
1240 if ((p->p_itimers == NULL) ||
1241 (timerid < 0) || (timerid >= TIMER_MAX) ||
1242 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1246 if ((it->it_flags & ITF_DELETING) != 0) {
1254 kern_ktimer_delete(struct thread *td, int timerid)
1256 struct proc *p = td->td_proc;
1260 it = itimer_find(p, timerid);
1267 it->it_flags |= ITF_DELETING;
1268 while (it->it_usecount > 0) {
1269 it->it_flags |= ITF_WANTED;
1270 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1272 it->it_flags &= ~ITF_WANTED;
1273 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1277 if (KSI_ONQ(&it->it_ksi))
1278 sigqueue_take(&it->it_ksi);
1279 p->p_itimers->its_timers[timerid] = NULL;
1281 uma_zfree(itimer_zone, it);
1285 #ifndef _SYS_SYSPROTO_H_
1286 struct ktimer_settime_args {
1289 const struct itimerspec * value;
1290 struct itimerspec * ovalue;
1294 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1296 struct itimerspec val, oval, *ovalp;
1299 error = copyin(uap->value, &val, sizeof(val));
1302 ovalp = uap->ovalue != NULL ? &oval : NULL;
1303 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1304 if (error == 0 && uap->ovalue != NULL)
1305 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1310 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1311 struct itimerspec *val, struct itimerspec *oval)
1319 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1325 error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1333 #ifndef _SYS_SYSPROTO_H_
1334 struct ktimer_gettime_args {
1336 struct itimerspec * value;
1340 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1342 struct itimerspec val;
1345 error = kern_ktimer_gettime(td, uap->timerid, &val);
1347 error = copyout(&val, uap->value, sizeof(val));
1352 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1360 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1366 error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1373 #ifndef _SYS_SYSPROTO_H_
1374 struct timer_getoverrun_args {
1379 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1382 return (kern_ktimer_getoverrun(td, uap->timerid));
1386 kern_ktimer_getoverrun(struct thread *td, int timer_id)
1388 struct proc *p = td->td_proc;
1394 (it = itimer_find(p, timer_id)) == NULL) {
1398 td->td_retval[0] = it->it_overrun_last;
1407 realtimer_create(struct itimer *it)
1409 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1414 realtimer_delete(struct itimer *it)
1416 mtx_assert(&it->it_mtx, MA_OWNED);
1419 * clear timer's value and interval to tell realtimer_expire
1420 * to not rearm the timer.
1422 timespecclear(&it->it_time.it_value);
1423 timespecclear(&it->it_time.it_interval);
1425 callout_drain(&it->it_callout);
1431 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1433 struct timespec cts;
1435 mtx_assert(&it->it_mtx, MA_OWNED);
1437 realtimer_clocktime(it->it_clockid, &cts);
1438 *ovalue = it->it_time;
1439 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1440 timespecsub(&ovalue->it_value, &cts);
1441 if (ovalue->it_value.tv_sec < 0 ||
1442 (ovalue->it_value.tv_sec == 0 &&
1443 ovalue->it_value.tv_nsec == 0)) {
1444 ovalue->it_value.tv_sec = 0;
1445 ovalue->it_value.tv_nsec = 1;
1452 realtimer_settime(struct itimer *it, int flags,
1453 struct itimerspec *value, struct itimerspec *ovalue)
1455 struct timespec cts, ts;
1457 struct itimerspec val;
1459 mtx_assert(&it->it_mtx, MA_OWNED);
1462 if (itimespecfix(&val.it_value))
1465 if (timespecisset(&val.it_value)) {
1466 if (itimespecfix(&val.it_interval))
1469 timespecclear(&val.it_interval);
1473 realtimer_gettime(it, ovalue);
1476 if (timespecisset(&val.it_value)) {
1477 realtimer_clocktime(it->it_clockid, &cts);
1479 if ((flags & TIMER_ABSTIME) == 0) {
1480 /* Convert to absolute time. */
1481 timespecadd(&it->it_time.it_value, &cts);
1483 timespecsub(&ts, &cts);
1485 * We don't care if ts is negative, tztohz will
1489 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1490 callout_reset(&it->it_callout, tvtohz(&tv),
1491 realtimer_expire, it);
1493 callout_stop(&it->it_callout);
1500 realtimer_clocktime(clockid_t id, struct timespec *ts)
1502 if (id == CLOCK_REALTIME)
1504 else /* CLOCK_MONOTONIC */
1509 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1513 PROC_LOCK_ASSERT(p, MA_OWNED);
1514 it = itimer_find(p, timerid);
1516 ksi->ksi_overrun = it->it_overrun;
1517 it->it_overrun_last = it->it_overrun;
1526 itimespecfix(struct timespec *ts)
1529 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1531 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1532 ts->tv_nsec = tick * 1000;
1536 /* Timeout callback for realtime timer */
1538 realtimer_expire(void *arg)
1540 struct timespec cts, ts;
1544 it = (struct itimer *)arg;
1546 realtimer_clocktime(it->it_clockid, &cts);
1547 /* Only fire if time is reached. */
1548 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1549 if (timespecisset(&it->it_time.it_interval)) {
1550 timespecadd(&it->it_time.it_value,
1551 &it->it_time.it_interval);
1552 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1553 if (it->it_overrun < INT_MAX)
1556 it->it_ksi.ksi_errno = ERANGE;
1557 timespecadd(&it->it_time.it_value,
1558 &it->it_time.it_interval);
1561 /* single shot timer ? */
1562 timespecclear(&it->it_time.it_value);
1564 if (timespecisset(&it->it_time.it_value)) {
1565 ts = it->it_time.it_value;
1566 timespecsub(&ts, &cts);
1567 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1568 callout_reset(&it->it_callout, tvtohz(&tv),
1569 realtimer_expire, it);
1576 } else if (timespecisset(&it->it_time.it_value)) {
1577 ts = it->it_time.it_value;
1578 timespecsub(&ts, &cts);
1579 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1580 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1586 itimer_fire(struct itimer *it)
1588 struct proc *p = it->it_proc;
1591 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1592 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1593 if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1595 timespecclear(&it->it_time.it_value);
1596 timespecclear(&it->it_time.it_interval);
1597 callout_stop(&it->it_callout);
1601 if (!KSI_ONQ(&it->it_ksi)) {
1602 it->it_ksi.ksi_errno = 0;
1603 ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1604 tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1606 if (it->it_overrun < INT_MAX)
1609 it->it_ksi.ksi_errno = ERANGE;
1616 itimers_alloc(struct proc *p)
1618 struct itimers *its;
1621 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1622 LIST_INIT(&its->its_virtual);
1623 LIST_INIT(&its->its_prof);
1624 TAILQ_INIT(&its->its_worklist);
1625 for (i = 0; i < TIMER_MAX; i++)
1626 its->its_timers[i] = NULL;
1628 if (p->p_itimers == NULL) {
1634 free(its, M_SUBPROC);
1639 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1641 itimers_event_hook_exit(arg, p);
1644 /* Clean up timers when some process events are being triggered. */
1646 itimers_event_hook_exit(void *arg, struct proc *p)
1648 struct itimers *its;
1650 int event = (int)(intptr_t)arg;
1653 if (p->p_itimers != NULL) {
1655 for (i = 0; i < MAX_CLOCKS; ++i) {
1656 if (posix_clocks[i].event_hook != NULL)
1657 CLOCK_CALL(i, event_hook, (p, i, event));
1660 * According to susv3, XSI interval timers should be inherited
1663 if (event == ITIMER_EV_EXEC)
1665 else if (event == ITIMER_EV_EXIT)
1668 panic("unhandled event");
1669 for (; i < TIMER_MAX; ++i) {
1670 if ((it = its->its_timers[i]) != NULL)
1671 kern_ktimer_delete(curthread, i);
1673 if (its->its_timers[0] == NULL &&
1674 its->its_timers[1] == NULL &&
1675 its->its_timers[2] == NULL) {
1676 free(its, M_SUBPROC);
1677 p->p_itimers = NULL;