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:
206 error = p_cansee(td, p);
212 pid = td->td_proc->p_pid;
214 *clk_id = MAKE_PROCESS_CPUCLOCK(pid);
216 case CPUCLOCK_WHICH_TID:
217 tid = id == 0 ? td->td_tid : id;
218 *clk_id = MAKE_THREAD_CPUCLOCK(tid);
225 #ifndef _SYS_SYSPROTO_H_
226 struct clock_gettime_args {
233 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
238 error = kern_clock_gettime(td, uap->clock_id, &ats);
240 error = copyout(&ats, uap->tp, sizeof(ats));
246 cputick2timespec(uint64_t runtime, struct timespec *ats)
248 runtime = cputick2usec(runtime);
249 ats->tv_sec = runtime / 1000000;
250 ats->tv_nsec = runtime % 1000000 * 1000;
254 get_thread_cputime(struct thread *targettd, struct timespec *ats)
256 uint64_t runtime, curtime, switchtime;
258 if (targettd == NULL) { /* current thread */
260 switchtime = PCPU_GET(switchtime);
261 curtime = cpu_ticks();
262 runtime = curthread->td_runtime;
264 runtime += curtime - switchtime;
266 thread_lock(targettd);
267 runtime = targettd->td_runtime;
268 thread_unlock(targettd);
270 cputick2timespec(runtime, ats);
274 get_process_cputime(struct proc *targetp, struct timespec *ats)
280 rufetch(targetp, &ru);
281 runtime = targetp->p_rux.rux_runtime;
282 PROC_SUNLOCK(targetp);
283 cputick2timespec(runtime, ats);
287 get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
296 if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
297 tid = clock_id & CPUCLOCK_ID_MASK;
298 td2 = tdfind(tid, p->p_pid);
301 get_thread_cputime(td2, ats);
302 PROC_UNLOCK(td2->td_proc);
304 pid = clock_id & CPUCLOCK_ID_MASK;
305 error = pget(pid, PGET_CANSEE, &p2);
308 get_process_cputime(p2, ats);
315 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
317 struct timeval sys, user;
322 case CLOCK_REALTIME: /* Default to precise. */
323 case CLOCK_REALTIME_PRECISE:
326 case CLOCK_REALTIME_FAST:
332 calcru(p, &user, &sys);
335 TIMEVAL_TO_TIMESPEC(&user, ats);
340 calcru(p, &user, &sys);
343 timevaladd(&user, &sys);
344 TIMEVAL_TO_TIMESPEC(&user, ats);
346 case CLOCK_MONOTONIC: /* Default to precise. */
347 case CLOCK_MONOTONIC_PRECISE:
349 case CLOCK_UPTIME_PRECISE:
352 case CLOCK_UPTIME_FAST:
353 case CLOCK_MONOTONIC_FAST:
357 ats->tv_sec = time_second;
360 case CLOCK_THREAD_CPUTIME_ID:
361 get_thread_cputime(NULL, ats);
363 case CLOCK_PROCESS_CPUTIME_ID:
365 get_process_cputime(p, ats);
369 if ((int)clock_id >= 0)
371 return (get_cputime(td, clock_id, ats));
376 #ifndef _SYS_SYSPROTO_H_
377 struct clock_settime_args {
379 const struct timespec *tp;
384 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
389 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
391 return (kern_clock_settime(td, uap->clock_id, &ats));
395 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
400 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
402 if (clock_id != CLOCK_REALTIME)
404 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
406 /* XXX Don't convert nsec->usec and back */
407 TIMESPEC_TO_TIMEVAL(&atv, ats);
408 error = settime(td, &atv);
412 #ifndef _SYS_SYSPROTO_H_
413 struct clock_getres_args {
419 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
427 error = kern_clock_getres(td, uap->clock_id, &ts);
429 error = copyout(&ts, uap->tp, sizeof(ts));
434 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
440 case CLOCK_REALTIME_FAST:
441 case CLOCK_REALTIME_PRECISE:
442 case CLOCK_MONOTONIC:
443 case CLOCK_MONOTONIC_FAST:
444 case CLOCK_MONOTONIC_PRECISE:
446 case CLOCK_UPTIME_FAST:
447 case CLOCK_UPTIME_PRECISE:
449 * Round up the result of the division cheaply by adding 1.
450 * Rounding up is especially important if rounding down
451 * would give 0. Perfect rounding is unimportant.
453 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
457 /* Accurately round up here because we can do so cheaply. */
458 ts->tv_nsec = (1000000000 + hz - 1) / hz;
464 case CLOCK_THREAD_CPUTIME_ID:
465 case CLOCK_PROCESS_CPUTIME_ID:
467 /* sync with cputick2usec */
468 ts->tv_nsec = 1000000 / cpu_tickrate();
469 if (ts->tv_nsec == 0)
473 if ((int)clock_id < 0)
480 static uint8_t nanowait[MAXCPU];
483 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
486 sbintime_t sbt, sbtt, prec, tmp;
490 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
492 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
495 if (ts.tv_sec > INT32_MAX / 2) {
496 over = ts.tv_sec - INT32_MAX / 2;
503 if (TIMESEL(&sbt, tmp))
506 error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
507 sbt, prec, C_ABSOLUTE);
508 if (error != EWOULDBLOCK) {
509 if (error == ERESTART)
513 ts = sbttots(sbt - sbtt);
526 #ifndef _SYS_SYSPROTO_H_
527 struct nanosleep_args {
528 struct timespec *rqtp;
529 struct timespec *rmtp;
534 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
536 struct timespec rmt, rqt;
539 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
544 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
546 error = kern_nanosleep(td, &rqt, &rmt);
547 if (error && uap->rmtp) {
550 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
557 #ifndef _SYS_SYSPROTO_H_
558 struct gettimeofday_args {
560 struct timezone *tzp;
565 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
573 error = copyout(&atv, uap->tp, sizeof (atv));
575 if (error == 0 && uap->tzp != NULL) {
576 rtz.tz_minuteswest = tz_minuteswest;
577 rtz.tz_dsttime = tz_dsttime;
578 error = copyout(&rtz, uap->tzp, sizeof (rtz));
583 #ifndef _SYS_SYSPROTO_H_
584 struct settimeofday_args {
586 struct timezone *tzp;
591 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
593 struct timeval atv, *tvp;
594 struct timezone atz, *tzp;
598 error = copyin(uap->tv, &atv, sizeof(atv));
605 error = copyin(uap->tzp, &atz, sizeof(atz));
611 return (kern_settimeofday(td, tvp, tzp));
615 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
619 error = priv_check(td, PRIV_SETTIMEOFDAY);
622 /* Verify all parameters before changing time. */
624 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
626 error = settime(td, tv);
628 if (tzp && error == 0) {
629 tz_minuteswest = tzp->tz_minuteswest;
630 tz_dsttime = tzp->tz_dsttime;
636 * Get value of an interval timer. The process virtual and profiling virtual
637 * time timers are kept in the p_stats area, since they can be swapped out.
638 * These are kept internally in the way they are specified externally: in
639 * time until they expire.
641 * The real time interval timer is kept in the process table slot for the
642 * process, and its value (it_value) is kept as an absolute time rather than
643 * as a delta, so that it is easy to keep periodic real-time signals from
646 * Virtual time timers are processed in the hardclock() routine of
647 * kern_clock.c. The real time timer is processed by a timeout routine,
648 * called from the softclock() routine. Since a callout may be delayed in
649 * real time due to interrupt processing in the system, it is possible for
650 * the real time timeout routine (realitexpire, given below), to be delayed
651 * in real time past when it is supposed to occur. It does not suffice,
652 * therefore, to reload the real timer .it_value from the real time timers
653 * .it_interval. Rather, we compute the next time in absolute time the timer
656 #ifndef _SYS_SYSPROTO_H_
657 struct getitimer_args {
659 struct itimerval *itv;
663 sys_getitimer(struct thread *td, struct getitimer_args *uap)
665 struct itimerval aitv;
668 error = kern_getitimer(td, uap->which, &aitv);
671 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
675 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
677 struct proc *p = td->td_proc;
680 if (which > ITIMER_PROF)
683 if (which == ITIMER_REAL) {
685 * Convert from absolute to relative time in .it_value
686 * part of real time timer. If time for real time timer
687 * has passed return 0, else return difference between
688 * current time and time for the timer to go off.
691 *aitv = p->p_realtimer;
693 if (timevalisset(&aitv->it_value)) {
695 if (timevalcmp(&aitv->it_value, &ctv, <))
696 timevalclear(&aitv->it_value);
698 timevalsub(&aitv->it_value, &ctv);
702 *aitv = p->p_stats->p_timer[which];
708 #ifndef _SYS_SYSPROTO_H_
709 struct setitimer_args {
711 struct itimerval *itv, *oitv;
715 sys_setitimer(struct thread *td, struct setitimer_args *uap)
717 struct itimerval aitv, oitv;
720 if (uap->itv == NULL) {
721 uap->itv = uap->oitv;
722 return (sys_getitimer(td, (struct getitimer_args *)uap));
725 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
727 error = kern_setitimer(td, uap->which, &aitv, &oitv);
728 if (error != 0 || uap->oitv == NULL)
730 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
734 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
735 struct itimerval *oitv)
737 struct proc *p = td->td_proc;
742 return (kern_getitimer(td, which, oitv));
744 if (which > ITIMER_PROF)
746 if (itimerfix(&aitv->it_value) ||
747 aitv->it_value.tv_sec > INT32_MAX / 2)
749 if (!timevalisset(&aitv->it_value))
750 timevalclear(&aitv->it_interval);
751 else if (itimerfix(&aitv->it_interval) ||
752 aitv->it_interval.tv_sec > INT32_MAX / 2)
755 if (which == ITIMER_REAL) {
757 if (timevalisset(&p->p_realtimer.it_value))
758 callout_stop(&p->p_itcallout);
760 if (timevalisset(&aitv->it_value)) {
761 pr = tvtosbt(aitv->it_value) >> tc_precexp;
762 timevaladd(&aitv->it_value, &ctv);
763 sbt = tvtosbt(aitv->it_value);
764 callout_reset_sbt(&p->p_itcallout, sbt, pr,
765 realitexpire, p, C_ABSOLUTE);
767 *oitv = p->p_realtimer;
768 p->p_realtimer = *aitv;
770 if (timevalisset(&oitv->it_value)) {
771 if (timevalcmp(&oitv->it_value, &ctv, <))
772 timevalclear(&oitv->it_value);
774 timevalsub(&oitv->it_value, &ctv);
777 if (aitv->it_interval.tv_sec == 0 &&
778 aitv->it_interval.tv_usec != 0 &&
779 aitv->it_interval.tv_usec < tick)
780 aitv->it_interval.tv_usec = tick;
781 if (aitv->it_value.tv_sec == 0 &&
782 aitv->it_value.tv_usec != 0 &&
783 aitv->it_value.tv_usec < tick)
784 aitv->it_value.tv_usec = tick;
786 *oitv = p->p_stats->p_timer[which];
787 p->p_stats->p_timer[which] = *aitv;
794 * Real interval timer expired:
795 * send process whose timer expired an alarm signal.
796 * If time is not set up to reload, then just return.
797 * Else compute next time timer should go off which is > current time.
798 * This is where delay in processing this timeout causes multiple
799 * SIGALRM calls to be compressed into one.
800 * tvtohz() always adds 1 to allow for the time until the next clock
801 * interrupt being strictly less than 1 clock tick, but we don't want
802 * that here since we want to appear to be in sync with the clock
803 * interrupt even when we're delayed.
806 realitexpire(void *arg)
812 p = (struct proc *)arg;
813 kern_psignal(p, SIGALRM);
814 if (!timevalisset(&p->p_realtimer.it_interval)) {
815 timevalclear(&p->p_realtimer.it_value);
816 if (p->p_flag & P_WEXIT)
817 wakeup(&p->p_itcallout);
820 isbt = tvtosbt(p->p_realtimer.it_interval);
821 if (isbt >= sbt_timethreshold)
822 getmicrouptime(&ctv);
826 timevaladd(&p->p_realtimer.it_value,
827 &p->p_realtimer.it_interval);
828 } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
829 callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
830 isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
834 * Check that a proposed value to load into the .it_value or
835 * .it_interval part of an interval timer is acceptable, and
836 * fix it to have at least minimal value (i.e. if it is less
837 * than the resolution of the clock, round it up.)
840 itimerfix(struct timeval *tv)
843 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
845 if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
846 tv->tv_usec < (u_int)tick / 16)
847 tv->tv_usec = (u_int)tick / 16;
852 * Decrement an interval timer by a specified number
853 * of microseconds, which must be less than a second,
854 * i.e. < 1000000. If the timer expires, then reload
855 * it. In this case, carry over (usec - old value) to
856 * reduce the value reloaded into the timer so that
857 * the timer does not drift. This routine assumes
858 * that it is called in a context where the timers
859 * on which it is operating cannot change in value.
862 itimerdecr(struct itimerval *itp, int usec)
865 if (itp->it_value.tv_usec < usec) {
866 if (itp->it_value.tv_sec == 0) {
867 /* expired, and already in next interval */
868 usec -= itp->it_value.tv_usec;
871 itp->it_value.tv_usec += 1000000;
872 itp->it_value.tv_sec--;
874 itp->it_value.tv_usec -= usec;
876 if (timevalisset(&itp->it_value))
878 /* expired, exactly at end of interval */
880 if (timevalisset(&itp->it_interval)) {
881 itp->it_value = itp->it_interval;
882 itp->it_value.tv_usec -= usec;
883 if (itp->it_value.tv_usec < 0) {
884 itp->it_value.tv_usec += 1000000;
885 itp->it_value.tv_sec--;
888 itp->it_value.tv_usec = 0; /* sec is already 0 */
893 * Add and subtract routines for timevals.
894 * N.B.: subtract routine doesn't deal with
895 * results which are before the beginning,
896 * it just gets very confused in this case.
900 timevaladd(struct timeval *t1, const struct timeval *t2)
903 t1->tv_sec += t2->tv_sec;
904 t1->tv_usec += t2->tv_usec;
909 timevalsub(struct timeval *t1, const struct timeval *t2)
912 t1->tv_sec -= t2->tv_sec;
913 t1->tv_usec -= t2->tv_usec;
918 timevalfix(struct timeval *t1)
921 if (t1->tv_usec < 0) {
923 t1->tv_usec += 1000000;
925 if (t1->tv_usec >= 1000000) {
927 t1->tv_usec -= 1000000;
932 * ratecheck(): simple time-based rate-limit checking.
935 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
937 struct timeval tv, delta;
940 getmicrouptime(&tv); /* NB: 10ms precision */
942 timevalsub(&delta, lasttime);
945 * check for 0,0 is so that the message will be seen at least once,
946 * even if interval is huge.
948 if (timevalcmp(&delta, mininterval, >=) ||
949 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
958 * ppsratecheck(): packets (or events) per second limitation.
960 * Return 0 if the limit is to be enforced (e.g. the caller
961 * should drop a packet because of the rate limitation).
963 * maxpps of 0 always causes zero to be returned. maxpps of -1
964 * always causes 1 to be returned; this effectively defeats rate
967 * Note that we maintain the struct timeval for compatibility
968 * with other bsd systems. We reuse the storage and just monitor
969 * clock ticks for minimal overhead.
972 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
977 * Reset the last time and counter if this is the first call
978 * or more than a second has passed since the last update of
982 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
983 lasttime->tv_sec = now;
985 return (maxpps != 0);
987 (*curpps)++; /* NB: ignore potential overflow */
988 return (maxpps < 0 || *curpps < maxpps);
995 struct kclock rt_clock = {
996 .timer_create = realtimer_create,
997 .timer_delete = realtimer_delete,
998 .timer_settime = realtimer_settime,
999 .timer_gettime = realtimer_gettime,
1003 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
1004 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
1005 register_posix_clock(CLOCK_REALTIME, &rt_clock);
1006 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
1007 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
1008 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
1009 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1010 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1011 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1012 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1013 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
1017 register_posix_clock(int clockid, struct kclock *clk)
1019 if ((unsigned)clockid >= MAX_CLOCKS) {
1020 printf("%s: invalid clockid\n", __func__);
1023 posix_clocks[clockid] = *clk;
1028 itimer_init(void *mem, int size, int flags)
1032 it = (struct itimer *)mem;
1033 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1038 itimer_fini(void *mem, int size)
1042 it = (struct itimer *)mem;
1043 mtx_destroy(&it->it_mtx);
1047 itimer_enter(struct itimer *it)
1050 mtx_assert(&it->it_mtx, MA_OWNED);
1055 itimer_leave(struct itimer *it)
1058 mtx_assert(&it->it_mtx, MA_OWNED);
1059 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1061 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1065 #ifndef _SYS_SYSPROTO_H_
1066 struct ktimer_create_args {
1068 struct sigevent * evp;
1073 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1075 struct sigevent *evp, ev;
1079 if (uap->evp == NULL) {
1082 error = copyin(uap->evp, &ev, sizeof(ev));
1087 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1089 error = copyout(&id, uap->timerid, sizeof(int));
1091 kern_ktimer_delete(td, id);
1097 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1098 int *timerid, int preset_id)
1100 struct proc *p = td->td_proc;
1105 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1108 if (posix_clocks[clock_id].timer_create == NULL)
1112 if (evp->sigev_notify != SIGEV_NONE &&
1113 evp->sigev_notify != SIGEV_SIGNAL &&
1114 evp->sigev_notify != SIGEV_THREAD_ID)
1116 if ((evp->sigev_notify == SIGEV_SIGNAL ||
1117 evp->sigev_notify == SIGEV_THREAD_ID) &&
1118 !_SIG_VALID(evp->sigev_signo))
1122 if (p->p_itimers == NULL)
1125 it = uma_zalloc(itimer_zone, M_WAITOK);
1127 it->it_usecount = 0;
1129 timespecclear(&it->it_time.it_value);
1130 timespecclear(&it->it_time.it_interval);
1132 it->it_overrun_last = 0;
1133 it->it_clockid = clock_id;
1134 it->it_timerid = -1;
1136 ksiginfo_init(&it->it_ksi);
1137 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1138 error = CLOCK_CALL(clock_id, timer_create, (it));
1143 if (preset_id != -1) {
1144 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1146 if (p->p_itimers->its_timers[id] != NULL) {
1153 * Find a free timer slot, skipping those reserved
1156 for (id = 3; id < TIMER_MAX; id++)
1157 if (p->p_itimers->its_timers[id] == NULL)
1159 if (id == TIMER_MAX) {
1165 it->it_timerid = id;
1166 p->p_itimers->its_timers[id] = it;
1168 it->it_sigev = *evp;
1170 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1173 case CLOCK_REALTIME:
1174 it->it_sigev.sigev_signo = SIGALRM;
1177 it->it_sigev.sigev_signo = SIGVTALRM;
1180 it->it_sigev.sigev_signo = SIGPROF;
1183 it->it_sigev.sigev_value.sival_int = id;
1186 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1187 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1188 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1189 it->it_ksi.ksi_code = SI_TIMER;
1190 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1191 it->it_ksi.ksi_timerid = id;
1199 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1201 uma_zfree(itimer_zone, it);
1205 #ifndef _SYS_SYSPROTO_H_
1206 struct ktimer_delete_args {
1211 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1214 return (kern_ktimer_delete(td, uap->timerid));
1217 static struct itimer *
1218 itimer_find(struct proc *p, int timerid)
1222 PROC_LOCK_ASSERT(p, MA_OWNED);
1223 if ((p->p_itimers == NULL) ||
1224 (timerid < 0) || (timerid >= TIMER_MAX) ||
1225 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1229 if ((it->it_flags & ITF_DELETING) != 0) {
1237 kern_ktimer_delete(struct thread *td, int timerid)
1239 struct proc *p = td->td_proc;
1243 it = itimer_find(p, timerid);
1250 it->it_flags |= ITF_DELETING;
1251 while (it->it_usecount > 0) {
1252 it->it_flags |= ITF_WANTED;
1253 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1255 it->it_flags &= ~ITF_WANTED;
1256 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1260 if (KSI_ONQ(&it->it_ksi))
1261 sigqueue_take(&it->it_ksi);
1262 p->p_itimers->its_timers[timerid] = NULL;
1264 uma_zfree(itimer_zone, it);
1268 #ifndef _SYS_SYSPROTO_H_
1269 struct ktimer_settime_args {
1272 const struct itimerspec * value;
1273 struct itimerspec * ovalue;
1277 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1279 struct itimerspec val, oval, *ovalp;
1282 error = copyin(uap->value, &val, sizeof(val));
1285 ovalp = uap->ovalue != NULL ? &oval : NULL;
1286 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1287 if (error == 0 && uap->ovalue != NULL)
1288 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1293 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1294 struct itimerspec *val, struct itimerspec *oval)
1302 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1308 error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1316 #ifndef _SYS_SYSPROTO_H_
1317 struct ktimer_gettime_args {
1319 struct itimerspec * value;
1323 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1325 struct itimerspec val;
1328 error = kern_ktimer_gettime(td, uap->timerid, &val);
1330 error = copyout(&val, uap->value, sizeof(val));
1335 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1343 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1349 error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1356 #ifndef _SYS_SYSPROTO_H_
1357 struct timer_getoverrun_args {
1362 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1365 return (kern_ktimer_getoverrun(td, uap->timerid));
1369 kern_ktimer_getoverrun(struct thread *td, int timer_id)
1371 struct proc *p = td->td_proc;
1377 (it = itimer_find(p, timer_id)) == NULL) {
1381 td->td_retval[0] = it->it_overrun_last;
1390 realtimer_create(struct itimer *it)
1392 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1397 realtimer_delete(struct itimer *it)
1399 mtx_assert(&it->it_mtx, MA_OWNED);
1402 * clear timer's value and interval to tell realtimer_expire
1403 * to not rearm the timer.
1405 timespecclear(&it->it_time.it_value);
1406 timespecclear(&it->it_time.it_interval);
1408 callout_drain(&it->it_callout);
1414 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1416 struct timespec cts;
1418 mtx_assert(&it->it_mtx, MA_OWNED);
1420 realtimer_clocktime(it->it_clockid, &cts);
1421 *ovalue = it->it_time;
1422 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1423 timespecsub(&ovalue->it_value, &cts);
1424 if (ovalue->it_value.tv_sec < 0 ||
1425 (ovalue->it_value.tv_sec == 0 &&
1426 ovalue->it_value.tv_nsec == 0)) {
1427 ovalue->it_value.tv_sec = 0;
1428 ovalue->it_value.tv_nsec = 1;
1435 realtimer_settime(struct itimer *it, int flags,
1436 struct itimerspec *value, struct itimerspec *ovalue)
1438 struct timespec cts, ts;
1440 struct itimerspec val;
1442 mtx_assert(&it->it_mtx, MA_OWNED);
1445 if (itimespecfix(&val.it_value))
1448 if (timespecisset(&val.it_value)) {
1449 if (itimespecfix(&val.it_interval))
1452 timespecclear(&val.it_interval);
1456 realtimer_gettime(it, ovalue);
1459 if (timespecisset(&val.it_value)) {
1460 realtimer_clocktime(it->it_clockid, &cts);
1462 if ((flags & TIMER_ABSTIME) == 0) {
1463 /* Convert to absolute time. */
1464 timespecadd(&it->it_time.it_value, &cts);
1466 timespecsub(&ts, &cts);
1468 * We don't care if ts is negative, tztohz will
1472 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1473 callout_reset(&it->it_callout, tvtohz(&tv),
1474 realtimer_expire, it);
1476 callout_stop(&it->it_callout);
1483 realtimer_clocktime(clockid_t id, struct timespec *ts)
1485 if (id == CLOCK_REALTIME)
1487 else /* CLOCK_MONOTONIC */
1492 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1496 PROC_LOCK_ASSERT(p, MA_OWNED);
1497 it = itimer_find(p, timerid);
1499 ksi->ksi_overrun = it->it_overrun;
1500 it->it_overrun_last = it->it_overrun;
1509 itimespecfix(struct timespec *ts)
1512 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1514 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1515 ts->tv_nsec = tick * 1000;
1519 /* Timeout callback for realtime timer */
1521 realtimer_expire(void *arg)
1523 struct timespec cts, ts;
1527 it = (struct itimer *)arg;
1529 realtimer_clocktime(it->it_clockid, &cts);
1530 /* Only fire if time is reached. */
1531 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1532 if (timespecisset(&it->it_time.it_interval)) {
1533 timespecadd(&it->it_time.it_value,
1534 &it->it_time.it_interval);
1535 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1536 if (it->it_overrun < INT_MAX)
1539 it->it_ksi.ksi_errno = ERANGE;
1540 timespecadd(&it->it_time.it_value,
1541 &it->it_time.it_interval);
1544 /* single shot timer ? */
1545 timespecclear(&it->it_time.it_value);
1547 if (timespecisset(&it->it_time.it_value)) {
1548 ts = it->it_time.it_value;
1549 timespecsub(&ts, &cts);
1550 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1551 callout_reset(&it->it_callout, tvtohz(&tv),
1552 realtimer_expire, it);
1559 } else if (timespecisset(&it->it_time.it_value)) {
1560 ts = it->it_time.it_value;
1561 timespecsub(&ts, &cts);
1562 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1563 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1569 itimer_fire(struct itimer *it)
1571 struct proc *p = it->it_proc;
1574 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1575 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1576 if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1578 timespecclear(&it->it_time.it_value);
1579 timespecclear(&it->it_time.it_interval);
1580 callout_stop(&it->it_callout);
1584 if (!KSI_ONQ(&it->it_ksi)) {
1585 it->it_ksi.ksi_errno = 0;
1586 ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1587 tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1589 if (it->it_overrun < INT_MAX)
1592 it->it_ksi.ksi_errno = ERANGE;
1599 itimers_alloc(struct proc *p)
1601 struct itimers *its;
1604 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1605 LIST_INIT(&its->its_virtual);
1606 LIST_INIT(&its->its_prof);
1607 TAILQ_INIT(&its->its_worklist);
1608 for (i = 0; i < TIMER_MAX; i++)
1609 its->its_timers[i] = NULL;
1611 if (p->p_itimers == NULL) {
1617 free(its, M_SUBPROC);
1622 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1624 itimers_event_hook_exit(arg, p);
1627 /* Clean up timers when some process events are being triggered. */
1629 itimers_event_hook_exit(void *arg, struct proc *p)
1631 struct itimers *its;
1633 int event = (int)(intptr_t)arg;
1636 if (p->p_itimers != NULL) {
1638 for (i = 0; i < MAX_CLOCKS; ++i) {
1639 if (posix_clocks[i].event_hook != NULL)
1640 CLOCK_CALL(i, event_hook, (p, i, event));
1643 * According to susv3, XSI interval timers should be inherited
1646 if (event == ITIMER_EV_EXEC)
1648 else if (event == ITIMER_EV_EXIT)
1651 panic("unhandled event");
1652 for (; i < TIMER_MAX; ++i) {
1653 if ((it = its->its_timers[i]) != NULL)
1654 kern_ktimer_delete(curthread, i);
1656 if (its->its_timers[0] == NULL &&
1657 its->its_timers[1] == NULL &&
1658 its->its_timers[2] == NULL) {
1659 free(its, M_SUBPROC);
1660 p->p_itimers = NULL;