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/sleepqueue.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
52 #include <sys/posix4.h>
54 #include <sys/timers.h>
55 #include <sys/timetc.h>
56 #include <sys/vnode.h>
59 #include <vm/vm_extern.h>
61 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
62 #define CPUCLOCK_BIT 0x80000000
63 #define CPUCLOCK_PROCESS_BIT 0x40000000
64 #define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
65 #define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid))
66 #define MAKE_PROCESS_CPUCLOCK(pid) \
67 (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
69 static struct kclock posix_clocks[MAX_CLOCKS];
70 static uma_zone_t itimer_zone = NULL;
73 * Time of day and interval timer support.
75 * These routines provide the kernel entry points to get and set
76 * the time-of-day and per-process interval timers. Subroutines
77 * here provide support for adding and subtracting timeval structures
78 * and decrementing interval timers, optionally reloading the interval
79 * timers when they expire.
82 static int settime(struct thread *, struct timeval *);
83 static void timevalfix(struct timeval *);
85 static void itimer_start(void);
86 static int itimer_init(void *, int, int);
87 static void itimer_fini(void *, int);
88 static void itimer_enter(struct itimer *);
89 static void itimer_leave(struct itimer *);
90 static struct itimer *itimer_find(struct proc *, int);
91 static void itimers_alloc(struct proc *);
92 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
93 static void itimers_event_hook_exit(void *arg, struct proc *p);
94 static int realtimer_create(struct itimer *);
95 static int realtimer_gettime(struct itimer *, struct itimerspec *);
96 static int realtimer_settime(struct itimer *, int,
97 struct itimerspec *, struct itimerspec *);
98 static int realtimer_delete(struct itimer *);
99 static void realtimer_clocktime(clockid_t, struct timespec *);
100 static void realtimer_expire(void *);
102 int register_posix_clock(int, struct kclock *);
103 void itimer_fire(struct itimer *it);
104 int itimespecfix(struct timespec *ts);
106 #define CLOCK_CALL(clock, call, arglist) \
107 ((*posix_clocks[clock].call) arglist)
109 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
113 settime(struct thread *td, struct timeval *tv)
115 struct timeval delta, tv1, tv2;
116 static struct timeval maxtime, laststep;
123 timevalsub(&delta, &tv1);
126 * If the system is secure, we do not allow the time to be
127 * set to a value earlier than 1 second less than the highest
128 * time we have yet seen. The worst a miscreant can do in
129 * this circumstance is "freeze" time. He couldn't go
132 * We similarly do not allow the clock to be stepped more
133 * than one second, nor more than once per second. This allows
134 * a miscreant to make the clock march double-time, but no worse.
136 if (securelevel_gt(td->td_ucred, 1) != 0) {
137 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
139 * Update maxtime to latest time we've seen.
141 if (tv1.tv_sec > maxtime.tv_sec)
144 timevalsub(&tv2, &maxtime);
145 if (tv2.tv_sec < -1) {
146 tv->tv_sec = maxtime.tv_sec - 1;
147 printf("Time adjustment clamped to -1 second\n");
150 if (tv1.tv_sec == laststep.tv_sec) {
154 if (delta.tv_sec > 1) {
155 tv->tv_sec = tv1.tv_sec + 1;
156 printf("Time adjustment clamped to +1 second\n");
162 ts.tv_sec = tv->tv_sec;
163 ts.tv_nsec = tv->tv_usec * 1000;
171 #ifndef _SYS_SYSPROTO_H_
172 struct clock_getcpuclockid2_args {
180 sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
185 error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
187 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
192 kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
201 case CPUCLOCK_WHICH_PID:
203 error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
209 pid = td->td_proc->p_pid;
211 *clk_id = MAKE_PROCESS_CPUCLOCK(pid);
213 case CPUCLOCK_WHICH_TID:
214 tid = id == 0 ? td->td_tid : id;
215 *clk_id = MAKE_THREAD_CPUCLOCK(tid);
222 #ifndef _SYS_SYSPROTO_H_
223 struct clock_gettime_args {
230 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
235 error = kern_clock_gettime(td, uap->clock_id, &ats);
237 error = copyout(&ats, uap->tp, sizeof(ats));
243 cputick2timespec(uint64_t runtime, struct timespec *ats)
245 runtime = cputick2usec(runtime);
246 ats->tv_sec = runtime / 1000000;
247 ats->tv_nsec = runtime % 1000000 * 1000;
251 get_thread_cputime(struct thread *targettd, struct timespec *ats)
253 uint64_t runtime, curtime, switchtime;
255 if (targettd == NULL) { /* current thread */
257 switchtime = PCPU_GET(switchtime);
258 curtime = cpu_ticks();
259 runtime = curthread->td_runtime;
261 runtime += curtime - switchtime;
263 thread_lock(targettd);
264 runtime = targettd->td_runtime;
265 thread_unlock(targettd);
267 cputick2timespec(runtime, ats);
271 get_process_cputime(struct proc *targetp, struct timespec *ats)
277 rufetch(targetp, &ru);
278 runtime = targetp->p_rux.rux_runtime;
279 PROC_SUNLOCK(targetp);
280 cputick2timespec(runtime, ats);
284 get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
293 if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
294 tid = clock_id & CPUCLOCK_ID_MASK;
295 td2 = tdfind(tid, p->p_pid);
298 get_thread_cputime(td2, ats);
299 PROC_UNLOCK(td2->td_proc);
301 pid = clock_id & CPUCLOCK_ID_MASK;
302 error = pget(pid, PGET_CANSEE, &p2);
305 get_process_cputime(p2, ats);
312 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
314 struct timeval sys, user;
319 case CLOCK_REALTIME: /* Default to precise. */
320 case CLOCK_REALTIME_PRECISE:
323 case CLOCK_REALTIME_FAST:
329 calcru(p, &user, &sys);
332 TIMEVAL_TO_TIMESPEC(&user, ats);
337 calcru(p, &user, &sys);
340 timevaladd(&user, &sys);
341 TIMEVAL_TO_TIMESPEC(&user, ats);
343 case CLOCK_MONOTONIC: /* Default to precise. */
344 case CLOCK_MONOTONIC_PRECISE:
346 case CLOCK_UPTIME_PRECISE:
349 case CLOCK_UPTIME_FAST:
350 case CLOCK_MONOTONIC_FAST:
354 ats->tv_sec = time_second;
357 case CLOCK_THREAD_CPUTIME_ID:
358 get_thread_cputime(NULL, ats);
360 case CLOCK_PROCESS_CPUTIME_ID:
362 get_process_cputime(p, ats);
366 if ((int)clock_id >= 0)
368 return (get_cputime(td, clock_id, ats));
373 #ifndef _SYS_SYSPROTO_H_
374 struct clock_settime_args {
376 const struct timespec *tp;
381 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
386 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
388 return (kern_clock_settime(td, uap->clock_id, &ats));
392 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
397 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
399 if (clock_id != CLOCK_REALTIME)
401 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
403 /* XXX Don't convert nsec->usec and back */
404 TIMESPEC_TO_TIMEVAL(&atv, ats);
405 error = settime(td, &atv);
409 #ifndef _SYS_SYSPROTO_H_
410 struct clock_getres_args {
416 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
424 error = kern_clock_getres(td, uap->clock_id, &ts);
426 error = copyout(&ts, uap->tp, sizeof(ts));
431 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
437 case CLOCK_REALTIME_FAST:
438 case CLOCK_REALTIME_PRECISE:
439 case CLOCK_MONOTONIC:
440 case CLOCK_MONOTONIC_FAST:
441 case CLOCK_MONOTONIC_PRECISE:
443 case CLOCK_UPTIME_FAST:
444 case CLOCK_UPTIME_PRECISE:
446 * Round up the result of the division cheaply by adding 1.
447 * Rounding up is especially important if rounding down
448 * would give 0. Perfect rounding is unimportant.
450 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
454 /* Accurately round up here because we can do so cheaply. */
455 ts->tv_nsec = (1000000000 + hz - 1) / hz;
461 case CLOCK_THREAD_CPUTIME_ID:
462 case CLOCK_PROCESS_CPUTIME_ID:
464 /* sync with cputick2usec */
465 ts->tv_nsec = 1000000 / cpu_tickrate();
466 if (ts->tv_nsec == 0)
470 if ((int)clock_id < 0)
477 static uint8_t nanowait[MAXCPU];
480 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
483 sbintime_t sbt, sbtt, prec, tmp;
487 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
489 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
492 if (ts.tv_sec > INT32_MAX / 2) {
493 over = ts.tv_sec - INT32_MAX / 2;
500 if (TIMESEL(&sbt, tmp))
503 error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
504 sbt, prec, C_ABSOLUTE);
505 if (error != EWOULDBLOCK) {
506 if (error == ERESTART)
510 ts = sbttots(sbt - sbtt);
523 #ifndef _SYS_SYSPROTO_H_
524 struct nanosleep_args {
525 struct timespec *rqtp;
526 struct timespec *rmtp;
531 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
533 struct timespec rmt, rqt;
536 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
541 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
543 error = kern_nanosleep(td, &rqt, &rmt);
544 if (error && uap->rmtp) {
547 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
554 #ifndef _SYS_SYSPROTO_H_
555 struct gettimeofday_args {
557 struct timezone *tzp;
562 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
570 error = copyout(&atv, uap->tp, sizeof (atv));
572 if (error == 0 && uap->tzp != NULL) {
573 rtz.tz_minuteswest = tz_minuteswest;
574 rtz.tz_dsttime = tz_dsttime;
575 error = copyout(&rtz, uap->tzp, sizeof (rtz));
580 #ifndef _SYS_SYSPROTO_H_
581 struct settimeofday_args {
583 struct timezone *tzp;
588 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
590 struct timeval atv, *tvp;
591 struct timezone atz, *tzp;
595 error = copyin(uap->tv, &atv, sizeof(atv));
602 error = copyin(uap->tzp, &atz, sizeof(atz));
608 return (kern_settimeofday(td, tvp, tzp));
612 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
616 error = priv_check(td, PRIV_SETTIMEOFDAY);
619 /* Verify all parameters before changing time. */
621 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
623 error = settime(td, tv);
625 if (tzp && error == 0) {
626 tz_minuteswest = tzp->tz_minuteswest;
627 tz_dsttime = tzp->tz_dsttime;
633 * Get value of an interval timer. The process virtual and profiling virtual
634 * time timers are kept in the p_stats area, since they can be swapped out.
635 * These are kept internally in the way they are specified externally: in
636 * time until they expire.
638 * The real time interval timer is kept in the process table slot for the
639 * process, and its value (it_value) is kept as an absolute time rather than
640 * as a delta, so that it is easy to keep periodic real-time signals from
643 * Virtual time timers are processed in the hardclock() routine of
644 * kern_clock.c. The real time timer is processed by a timeout routine,
645 * called from the softclock() routine. Since a callout may be delayed in
646 * real time due to interrupt processing in the system, it is possible for
647 * the real time timeout routine (realitexpire, given below), to be delayed
648 * in real time past when it is supposed to occur. It does not suffice,
649 * therefore, to reload the real timer .it_value from the real time timers
650 * .it_interval. Rather, we compute the next time in absolute time the timer
653 #ifndef _SYS_SYSPROTO_H_
654 struct getitimer_args {
656 struct itimerval *itv;
660 sys_getitimer(struct thread *td, struct getitimer_args *uap)
662 struct itimerval aitv;
665 error = kern_getitimer(td, uap->which, &aitv);
668 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
672 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
674 struct proc *p = td->td_proc;
677 if (which > ITIMER_PROF)
680 if (which == ITIMER_REAL) {
682 * Convert from absolute to relative time in .it_value
683 * part of real time timer. If time for real time timer
684 * has passed return 0, else return difference between
685 * current time and time for the timer to go off.
688 *aitv = p->p_realtimer;
690 if (timevalisset(&aitv->it_value)) {
692 if (timevalcmp(&aitv->it_value, &ctv, <))
693 timevalclear(&aitv->it_value);
695 timevalsub(&aitv->it_value, &ctv);
699 *aitv = p->p_stats->p_timer[which];
705 #ifndef _SYS_SYSPROTO_H_
706 struct setitimer_args {
708 struct itimerval *itv, *oitv;
712 sys_setitimer(struct thread *td, struct setitimer_args *uap)
714 struct itimerval aitv, oitv;
717 if (uap->itv == NULL) {
718 uap->itv = uap->oitv;
719 return (sys_getitimer(td, (struct getitimer_args *)uap));
722 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
724 error = kern_setitimer(td, uap->which, &aitv, &oitv);
725 if (error != 0 || uap->oitv == NULL)
727 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
731 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
732 struct itimerval *oitv)
734 struct proc *p = td->td_proc;
739 return (kern_getitimer(td, which, oitv));
741 if (which > ITIMER_PROF)
743 if (itimerfix(&aitv->it_value) ||
744 aitv->it_value.tv_sec > INT32_MAX / 2)
746 if (!timevalisset(&aitv->it_value))
747 timevalclear(&aitv->it_interval);
748 else if (itimerfix(&aitv->it_interval) ||
749 aitv->it_interval.tv_sec > INT32_MAX / 2)
752 if (which == ITIMER_REAL) {
754 if (timevalisset(&p->p_realtimer.it_value))
755 callout_stop(&p->p_itcallout);
757 if (timevalisset(&aitv->it_value)) {
758 pr = tvtosbt(aitv->it_value) >> tc_precexp;
759 timevaladd(&aitv->it_value, &ctv);
760 sbt = tvtosbt(aitv->it_value);
761 callout_reset_sbt(&p->p_itcallout, sbt, pr,
762 realitexpire, p, C_ABSOLUTE);
764 *oitv = p->p_realtimer;
765 p->p_realtimer = *aitv;
767 if (timevalisset(&oitv->it_value)) {
768 if (timevalcmp(&oitv->it_value, &ctv, <))
769 timevalclear(&oitv->it_value);
771 timevalsub(&oitv->it_value, &ctv);
774 if (aitv->it_interval.tv_sec == 0 &&
775 aitv->it_interval.tv_usec != 0 &&
776 aitv->it_interval.tv_usec < tick)
777 aitv->it_interval.tv_usec = tick;
778 if (aitv->it_value.tv_sec == 0 &&
779 aitv->it_value.tv_usec != 0 &&
780 aitv->it_value.tv_usec < tick)
781 aitv->it_value.tv_usec = tick;
783 *oitv = p->p_stats->p_timer[which];
784 p->p_stats->p_timer[which] = *aitv;
791 * Real interval timer expired:
792 * send process whose timer expired an alarm signal.
793 * If time is not set up to reload, then just return.
794 * Else compute next time timer should go off which is > current time.
795 * This is where delay in processing this timeout causes multiple
796 * SIGALRM calls to be compressed into one.
797 * tvtohz() always adds 1 to allow for the time until the next clock
798 * interrupt being strictly less than 1 clock tick, but we don't want
799 * that here since we want to appear to be in sync with the clock
800 * interrupt even when we're delayed.
803 realitexpire(void *arg)
809 p = (struct proc *)arg;
810 kern_psignal(p, SIGALRM);
811 if (!timevalisset(&p->p_realtimer.it_interval)) {
812 timevalclear(&p->p_realtimer.it_value);
813 if (p->p_flag & P_WEXIT)
814 wakeup(&p->p_itcallout);
817 isbt = tvtosbt(p->p_realtimer.it_interval);
818 if (isbt >= sbt_timethreshold)
819 getmicrouptime(&ctv);
823 timevaladd(&p->p_realtimer.it_value,
824 &p->p_realtimer.it_interval);
825 } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
826 callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
827 isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
831 * Check that a proposed value to load into the .it_value or
832 * .it_interval part of an interval timer is acceptable, and
833 * fix it to have at least minimal value (i.e. if it is less
834 * than the resolution of the clock, round it up.)
837 itimerfix(struct timeval *tv)
840 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
842 if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
843 tv->tv_usec < (u_int)tick / 16)
844 tv->tv_usec = (u_int)tick / 16;
849 * Decrement an interval timer by a specified number
850 * of microseconds, which must be less than a second,
851 * i.e. < 1000000. If the timer expires, then reload
852 * it. In this case, carry over (usec - old value) to
853 * reduce the value reloaded into the timer so that
854 * the timer does not drift. This routine assumes
855 * that it is called in a context where the timers
856 * on which it is operating cannot change in value.
859 itimerdecr(struct itimerval *itp, int usec)
862 if (itp->it_value.tv_usec < usec) {
863 if (itp->it_value.tv_sec == 0) {
864 /* expired, and already in next interval */
865 usec -= itp->it_value.tv_usec;
868 itp->it_value.tv_usec += 1000000;
869 itp->it_value.tv_sec--;
871 itp->it_value.tv_usec -= usec;
873 if (timevalisset(&itp->it_value))
875 /* expired, exactly at end of interval */
877 if (timevalisset(&itp->it_interval)) {
878 itp->it_value = itp->it_interval;
879 itp->it_value.tv_usec -= usec;
880 if (itp->it_value.tv_usec < 0) {
881 itp->it_value.tv_usec += 1000000;
882 itp->it_value.tv_sec--;
885 itp->it_value.tv_usec = 0; /* sec is already 0 */
890 * Add and subtract routines for timevals.
891 * N.B.: subtract routine doesn't deal with
892 * results which are before the beginning,
893 * it just gets very confused in this case.
897 timevaladd(struct timeval *t1, const struct timeval *t2)
900 t1->tv_sec += t2->tv_sec;
901 t1->tv_usec += t2->tv_usec;
906 timevalsub(struct timeval *t1, const struct timeval *t2)
909 t1->tv_sec -= t2->tv_sec;
910 t1->tv_usec -= t2->tv_usec;
915 timevalfix(struct timeval *t1)
918 if (t1->tv_usec < 0) {
920 t1->tv_usec += 1000000;
922 if (t1->tv_usec >= 1000000) {
924 t1->tv_usec -= 1000000;
929 * ratecheck(): simple time-based rate-limit checking.
932 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
934 struct timeval tv, delta;
937 getmicrouptime(&tv); /* NB: 10ms precision */
939 timevalsub(&delta, lasttime);
942 * check for 0,0 is so that the message will be seen at least once,
943 * even if interval is huge.
945 if (timevalcmp(&delta, mininterval, >=) ||
946 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
955 * ppsratecheck(): packets (or events) per second limitation.
957 * Return 0 if the limit is to be enforced (e.g. the caller
958 * should drop a packet because of the rate limitation).
960 * maxpps of 0 always causes zero to be returned. maxpps of -1
961 * always causes 1 to be returned; this effectively defeats rate
964 * Note that we maintain the struct timeval for compatibility
965 * with other bsd systems. We reuse the storage and just monitor
966 * clock ticks for minimal overhead.
969 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
974 * Reset the last time and counter if this is the first call
975 * or more than a second has passed since the last update of
979 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
980 lasttime->tv_sec = now;
982 return (maxpps != 0);
984 (*curpps)++; /* NB: ignore potential overflow */
985 return (maxpps < 0 || *curpps <= maxpps);
992 struct kclock rt_clock = {
993 .timer_create = realtimer_create,
994 .timer_delete = realtimer_delete,
995 .timer_settime = realtimer_settime,
996 .timer_gettime = realtimer_gettime,
1000 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
1001 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
1002 register_posix_clock(CLOCK_REALTIME, &rt_clock);
1003 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
1004 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
1005 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
1006 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1007 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1008 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1009 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1010 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
1014 register_posix_clock(int clockid, struct kclock *clk)
1016 if ((unsigned)clockid >= MAX_CLOCKS) {
1017 printf("%s: invalid clockid\n", __func__);
1020 posix_clocks[clockid] = *clk;
1025 itimer_init(void *mem, int size, int flags)
1029 it = (struct itimer *)mem;
1030 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1035 itimer_fini(void *mem, int size)
1039 it = (struct itimer *)mem;
1040 mtx_destroy(&it->it_mtx);
1044 itimer_enter(struct itimer *it)
1047 mtx_assert(&it->it_mtx, MA_OWNED);
1052 itimer_leave(struct itimer *it)
1055 mtx_assert(&it->it_mtx, MA_OWNED);
1056 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1058 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1062 #ifndef _SYS_SYSPROTO_H_
1063 struct ktimer_create_args {
1065 struct sigevent * evp;
1070 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1072 struct sigevent *evp, ev;
1076 if (uap->evp == NULL) {
1079 error = copyin(uap->evp, &ev, sizeof(ev));
1084 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1086 error = copyout(&id, uap->timerid, sizeof(int));
1088 kern_ktimer_delete(td, id);
1094 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1095 int *timerid, int preset_id)
1097 struct proc *p = td->td_proc;
1102 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1105 if (posix_clocks[clock_id].timer_create == NULL)
1109 if (evp->sigev_notify != SIGEV_NONE &&
1110 evp->sigev_notify != SIGEV_SIGNAL &&
1111 evp->sigev_notify != SIGEV_THREAD_ID)
1113 if ((evp->sigev_notify == SIGEV_SIGNAL ||
1114 evp->sigev_notify == SIGEV_THREAD_ID) &&
1115 !_SIG_VALID(evp->sigev_signo))
1119 if (p->p_itimers == NULL)
1122 it = uma_zalloc(itimer_zone, M_WAITOK);
1124 it->it_usecount = 0;
1126 timespecclear(&it->it_time.it_value);
1127 timespecclear(&it->it_time.it_interval);
1129 it->it_overrun_last = 0;
1130 it->it_clockid = clock_id;
1131 it->it_timerid = -1;
1133 ksiginfo_init(&it->it_ksi);
1134 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1135 error = CLOCK_CALL(clock_id, timer_create, (it));
1140 if (preset_id != -1) {
1141 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1143 if (p->p_itimers->its_timers[id] != NULL) {
1150 * Find a free timer slot, skipping those reserved
1153 for (id = 3; id < TIMER_MAX; id++)
1154 if (p->p_itimers->its_timers[id] == NULL)
1156 if (id == TIMER_MAX) {
1162 it->it_timerid = id;
1163 p->p_itimers->its_timers[id] = it;
1165 it->it_sigev = *evp;
1167 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1170 case CLOCK_REALTIME:
1171 it->it_sigev.sigev_signo = SIGALRM;
1174 it->it_sigev.sigev_signo = SIGVTALRM;
1177 it->it_sigev.sigev_signo = SIGPROF;
1180 it->it_sigev.sigev_value.sival_int = id;
1183 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1184 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1185 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1186 it->it_ksi.ksi_code = SI_TIMER;
1187 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1188 it->it_ksi.ksi_timerid = id;
1196 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1198 uma_zfree(itimer_zone, it);
1202 #ifndef _SYS_SYSPROTO_H_
1203 struct ktimer_delete_args {
1208 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1211 return (kern_ktimer_delete(td, uap->timerid));
1214 static struct itimer *
1215 itimer_find(struct proc *p, int timerid)
1219 PROC_LOCK_ASSERT(p, MA_OWNED);
1220 if ((p->p_itimers == NULL) ||
1221 (timerid < 0) || (timerid >= TIMER_MAX) ||
1222 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1226 if ((it->it_flags & ITF_DELETING) != 0) {
1234 kern_ktimer_delete(struct thread *td, int timerid)
1236 struct proc *p = td->td_proc;
1240 it = itimer_find(p, timerid);
1247 it->it_flags |= ITF_DELETING;
1248 while (it->it_usecount > 0) {
1249 it->it_flags |= ITF_WANTED;
1250 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1252 it->it_flags &= ~ITF_WANTED;
1253 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1257 if (KSI_ONQ(&it->it_ksi))
1258 sigqueue_take(&it->it_ksi);
1259 p->p_itimers->its_timers[timerid] = NULL;
1261 uma_zfree(itimer_zone, it);
1265 #ifndef _SYS_SYSPROTO_H_
1266 struct ktimer_settime_args {
1269 const struct itimerspec * value;
1270 struct itimerspec * ovalue;
1274 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1276 struct itimerspec val, oval, *ovalp;
1279 error = copyin(uap->value, &val, sizeof(val));
1282 ovalp = uap->ovalue != NULL ? &oval : NULL;
1283 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1284 if (error == 0 && uap->ovalue != NULL)
1285 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1290 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1291 struct itimerspec *val, struct itimerspec *oval)
1299 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1305 error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1313 #ifndef _SYS_SYSPROTO_H_
1314 struct ktimer_gettime_args {
1316 struct itimerspec * value;
1320 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1322 struct itimerspec val;
1325 error = kern_ktimer_gettime(td, uap->timerid, &val);
1327 error = copyout(&val, uap->value, sizeof(val));
1332 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1340 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1346 error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1353 #ifndef _SYS_SYSPROTO_H_
1354 struct timer_getoverrun_args {
1359 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1362 return (kern_ktimer_getoverrun(td, uap->timerid));
1366 kern_ktimer_getoverrun(struct thread *td, int timer_id)
1368 struct proc *p = td->td_proc;
1374 (it = itimer_find(p, timer_id)) == NULL) {
1378 td->td_retval[0] = it->it_overrun_last;
1387 realtimer_create(struct itimer *it)
1389 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1394 realtimer_delete(struct itimer *it)
1396 mtx_assert(&it->it_mtx, MA_OWNED);
1399 * clear timer's value and interval to tell realtimer_expire
1400 * to not rearm the timer.
1402 timespecclear(&it->it_time.it_value);
1403 timespecclear(&it->it_time.it_interval);
1405 callout_drain(&it->it_callout);
1411 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1413 struct timespec cts;
1415 mtx_assert(&it->it_mtx, MA_OWNED);
1417 realtimer_clocktime(it->it_clockid, &cts);
1418 *ovalue = it->it_time;
1419 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1420 timespecsub(&ovalue->it_value, &cts);
1421 if (ovalue->it_value.tv_sec < 0 ||
1422 (ovalue->it_value.tv_sec == 0 &&
1423 ovalue->it_value.tv_nsec == 0)) {
1424 ovalue->it_value.tv_sec = 0;
1425 ovalue->it_value.tv_nsec = 1;
1432 realtimer_settime(struct itimer *it, int flags,
1433 struct itimerspec *value, struct itimerspec *ovalue)
1435 struct timespec cts, ts;
1437 struct itimerspec val;
1439 mtx_assert(&it->it_mtx, MA_OWNED);
1442 if (itimespecfix(&val.it_value))
1445 if (timespecisset(&val.it_value)) {
1446 if (itimespecfix(&val.it_interval))
1449 timespecclear(&val.it_interval);
1453 realtimer_gettime(it, ovalue);
1456 if (timespecisset(&val.it_value)) {
1457 realtimer_clocktime(it->it_clockid, &cts);
1459 if ((flags & TIMER_ABSTIME) == 0) {
1460 /* Convert to absolute time. */
1461 timespecadd(&it->it_time.it_value, &cts);
1463 timespecsub(&ts, &cts);
1465 * We don't care if ts is negative, tztohz will
1469 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1470 callout_reset(&it->it_callout, tvtohz(&tv),
1471 realtimer_expire, it);
1473 callout_stop(&it->it_callout);
1480 realtimer_clocktime(clockid_t id, struct timespec *ts)
1482 if (id == CLOCK_REALTIME)
1484 else /* CLOCK_MONOTONIC */
1489 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1493 PROC_LOCK_ASSERT(p, MA_OWNED);
1494 it = itimer_find(p, timerid);
1496 ksi->ksi_overrun = it->it_overrun;
1497 it->it_overrun_last = it->it_overrun;
1506 itimespecfix(struct timespec *ts)
1509 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1511 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1512 ts->tv_nsec = tick * 1000;
1516 /* Timeout callback for realtime timer */
1518 realtimer_expire(void *arg)
1520 struct timespec cts, ts;
1524 it = (struct itimer *)arg;
1526 realtimer_clocktime(it->it_clockid, &cts);
1527 /* Only fire if time is reached. */
1528 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1529 if (timespecisset(&it->it_time.it_interval)) {
1530 timespecadd(&it->it_time.it_value,
1531 &it->it_time.it_interval);
1532 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1533 if (it->it_overrun < INT_MAX)
1536 it->it_ksi.ksi_errno = ERANGE;
1537 timespecadd(&it->it_time.it_value,
1538 &it->it_time.it_interval);
1541 /* single shot timer ? */
1542 timespecclear(&it->it_time.it_value);
1544 if (timespecisset(&it->it_time.it_value)) {
1545 ts = it->it_time.it_value;
1546 timespecsub(&ts, &cts);
1547 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1548 callout_reset(&it->it_callout, tvtohz(&tv),
1549 realtimer_expire, it);
1556 } else if (timespecisset(&it->it_time.it_value)) {
1557 ts = it->it_time.it_value;
1558 timespecsub(&ts, &cts);
1559 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1560 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1566 itimer_fire(struct itimer *it)
1568 struct proc *p = it->it_proc;
1571 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1572 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1573 if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1575 timespecclear(&it->it_time.it_value);
1576 timespecclear(&it->it_time.it_interval);
1577 callout_stop(&it->it_callout);
1581 if (!KSI_ONQ(&it->it_ksi)) {
1582 it->it_ksi.ksi_errno = 0;
1583 ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1584 tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1586 if (it->it_overrun < INT_MAX)
1589 it->it_ksi.ksi_errno = ERANGE;
1596 itimers_alloc(struct proc *p)
1598 struct itimers *its;
1601 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1602 LIST_INIT(&its->its_virtual);
1603 LIST_INIT(&its->its_prof);
1604 TAILQ_INIT(&its->its_worklist);
1605 for (i = 0; i < TIMER_MAX; i++)
1606 its->its_timers[i] = NULL;
1608 if (p->p_itimers == NULL) {
1614 free(its, M_SUBPROC);
1619 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1621 itimers_event_hook_exit(arg, p);
1624 /* Clean up timers when some process events are being triggered. */
1626 itimers_event_hook_exit(void *arg, struct proc *p)
1628 struct itimers *its;
1630 int event = (int)(intptr_t)arg;
1633 if (p->p_itimers != NULL) {
1635 for (i = 0; i < MAX_CLOCKS; ++i) {
1636 if (posix_clocks[i].event_hook != NULL)
1637 CLOCK_CALL(i, event_hook, (p, i, event));
1640 * According to susv3, XSI interval timers should be inherited
1643 if (event == ITIMER_EV_EXEC)
1645 else if (event == ITIMER_EV_EXIT)
1648 panic("unhandled event");
1649 for (; i < TIMER_MAX; ++i) {
1650 if ((it = its->its_timers[i]) != NULL)
1651 kern_ktimer_delete(curthread, i);
1653 if (its->its_timers[0] == NULL &&
1654 its->its_timers[1] == NULL &&
1655 its->its_timers[2] == NULL) {
1656 free(its, M_SUBPROC);
1657 p->p_itimers = NULL;