2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/limits.h>
38 #include <sys/clock.h>
40 #include <sys/mutex.h>
41 #include <sys/sysproto.h>
42 #include <sys/eventhandler.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/kernel.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
51 #include <sys/posix4.h>
53 #include <sys/timers.h>
54 #include <sys/timetc.h>
55 #include <sys/vnode.h>
58 #include <vm/vm_extern.h>
60 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
61 #define CPUCLOCK_BIT 0x80000000
62 #define CPUCLOCK_PROCESS_BIT 0x40000000
63 #define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
64 #define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid))
65 #define MAKE_PROCESS_CPUCLOCK(pid) \
66 (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
68 static struct kclock posix_clocks[MAX_CLOCKS];
69 static uma_zone_t itimer_zone = NULL;
72 * Time of day and interval timer support.
74 * These routines provide the kernel entry points to get and set
75 * the time-of-day and per-process interval timers. Subroutines
76 * here provide support for adding and subtracting timeval structures
77 * and decrementing interval timers, optionally reloading the interval
78 * timers when they expire.
81 static int settime(struct thread *, struct timeval *);
82 static void timevalfix(struct timeval *);
84 static void itimer_start(void);
85 static int itimer_init(void *, int, int);
86 static void itimer_fini(void *, int);
87 static void itimer_enter(struct itimer *);
88 static void itimer_leave(struct itimer *);
89 static struct itimer *itimer_find(struct proc *, int);
90 static void itimers_alloc(struct proc *);
91 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
92 static void itimers_event_hook_exit(void *arg, struct proc *p);
93 static int realtimer_create(struct itimer *);
94 static int realtimer_gettime(struct itimer *, struct itimerspec *);
95 static int realtimer_settime(struct itimer *, int,
96 struct itimerspec *, struct itimerspec *);
97 static int realtimer_delete(struct itimer *);
98 static void realtimer_clocktime(clockid_t, struct timespec *);
99 static void realtimer_expire(void *);
101 int register_posix_clock(int, struct kclock *);
102 void itimer_fire(struct itimer *it);
103 int itimespecfix(struct timespec *ts);
105 #define CLOCK_CALL(clock, call, arglist) \
106 ((*posix_clocks[clock].call) arglist)
108 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
112 settime(struct thread *td, struct timeval *tv)
114 struct timeval delta, tv1, tv2;
115 static struct timeval maxtime, laststep;
122 timevalsub(&delta, &tv1);
125 * If the system is secure, we do not allow the time to be
126 * set to a value earlier than 1 second less than the highest
127 * time we have yet seen. The worst a miscreant can do in
128 * this circumstance is "freeze" time. He couldn't go
131 * We similarly do not allow the clock to be stepped more
132 * than one second, nor more than once per second. This allows
133 * a miscreant to make the clock march double-time, but no worse.
135 if (securelevel_gt(td->td_ucred, 1) != 0) {
136 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
138 * Update maxtime to latest time we've seen.
140 if (tv1.tv_sec > maxtime.tv_sec)
143 timevalsub(&tv2, &maxtime);
144 if (tv2.tv_sec < -1) {
145 tv->tv_sec = maxtime.tv_sec - 1;
146 printf("Time adjustment clamped to -1 second\n");
149 if (tv1.tv_sec == laststep.tv_sec) {
153 if (delta.tv_sec > 1) {
154 tv->tv_sec = tv1.tv_sec + 1;
155 printf("Time adjustment clamped to +1 second\n");
161 ts.tv_sec = tv->tv_sec;
162 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:
205 error = p_cansee(td, p);
211 pid = td->td_proc->p_pid;
213 *clk_id = MAKE_PROCESS_CPUCLOCK(pid);
215 case CPUCLOCK_WHICH_TID:
216 tid = id == 0 ? td->td_tid : id;
217 *clk_id = MAKE_THREAD_CPUCLOCK(tid);
224 #ifndef _SYS_SYSPROTO_H_
225 struct clock_gettime_args {
232 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
237 error = kern_clock_gettime(td, uap->clock_id, &ats);
239 error = copyout(&ats, uap->tp, sizeof(ats));
245 cputick2timespec(uint64_t runtime, struct timespec *ats)
247 runtime = cputick2usec(runtime);
248 ats->tv_sec = runtime / 1000000;
249 ats->tv_nsec = runtime % 1000000 * 1000;
253 get_thread_cputime(struct thread *targettd, struct timespec *ats)
255 uint64_t runtime, curtime, switchtime;
257 if (targettd == NULL) { /* current thread */
259 switchtime = PCPU_GET(switchtime);
260 curtime = cpu_ticks();
261 runtime = curthread->td_runtime;
263 runtime += curtime - switchtime;
265 thread_lock(targettd);
266 runtime = targettd->td_runtime;
267 thread_unlock(targettd);
269 cputick2timespec(runtime, ats);
273 get_process_cputime(struct proc *targetp, struct timespec *ats)
279 rufetch(targetp, &ru);
280 runtime = targetp->p_rux.rux_runtime;
281 PROC_SUNLOCK(targetp);
282 cputick2timespec(runtime, ats);
286 get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
295 if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
296 tid = clock_id & CPUCLOCK_ID_MASK;
297 td2 = tdfind(tid, p->p_pid);
300 get_thread_cputime(td2, ats);
301 PROC_UNLOCK(td2->td_proc);
303 pid = clock_id & CPUCLOCK_ID_MASK;
304 error = pget(pid, PGET_CANSEE, &p2);
307 get_process_cputime(p2, ats);
314 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
316 struct timeval sys, user;
321 case CLOCK_REALTIME: /* Default to precise. */
322 case CLOCK_REALTIME_PRECISE:
325 case CLOCK_REALTIME_FAST:
331 calcru(p, &user, &sys);
334 TIMEVAL_TO_TIMESPEC(&user, ats);
339 calcru(p, &user, &sys);
342 timevaladd(&user, &sys);
343 TIMEVAL_TO_TIMESPEC(&user, ats);
345 case CLOCK_MONOTONIC: /* Default to precise. */
346 case CLOCK_MONOTONIC_PRECISE:
348 case CLOCK_UPTIME_PRECISE:
351 case CLOCK_UPTIME_FAST:
352 case CLOCK_MONOTONIC_FAST:
356 ats->tv_sec = time_second;
359 case CLOCK_THREAD_CPUTIME_ID:
360 get_thread_cputime(NULL, ats);
362 case CLOCK_PROCESS_CPUTIME_ID:
364 get_process_cputime(p, ats);
368 if ((int)clock_id >= 0)
370 return (get_cputime(td, clock_id, ats));
375 #ifndef _SYS_SYSPROTO_H_
376 struct clock_settime_args {
378 const struct timespec *tp;
383 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
388 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
390 return (kern_clock_settime(td, uap->clock_id, &ats));
394 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
399 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
401 if (clock_id != CLOCK_REALTIME)
403 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
405 /* XXX Don't convert nsec->usec and back */
406 TIMESPEC_TO_TIMEVAL(&atv, ats);
407 error = settime(td, &atv);
411 #ifndef _SYS_SYSPROTO_H_
412 struct clock_getres_args {
418 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
426 error = kern_clock_getres(td, uap->clock_id, &ts);
428 error = copyout(&ts, uap->tp, sizeof(ts));
433 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
439 case CLOCK_REALTIME_FAST:
440 case CLOCK_REALTIME_PRECISE:
441 case CLOCK_MONOTONIC:
442 case CLOCK_MONOTONIC_FAST:
443 case CLOCK_MONOTONIC_PRECISE:
445 case CLOCK_UPTIME_FAST:
446 case CLOCK_UPTIME_PRECISE:
448 * Round up the result of the division cheaply by adding 1.
449 * Rounding up is especially important if rounding down
450 * would give 0. Perfect rounding is unimportant.
452 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
456 /* Accurately round up here because we can do so cheaply. */
457 ts->tv_nsec = (1000000000 + hz - 1) / hz;
463 case CLOCK_THREAD_CPUTIME_ID:
464 case CLOCK_PROCESS_CPUTIME_ID:
466 /* sync with cputick2usec */
467 ts->tv_nsec = 1000000 / cpu_tickrate();
468 if (ts->tv_nsec == 0)
472 if ((int)clock_id < 0)
482 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
484 struct timespec ts, ts2, ts3;
488 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
490 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
493 timespecadd(&ts, rqt);
494 TIMESPEC_TO_TIMEVAL(&tv, rqt);
496 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
499 if (error != EWOULDBLOCK) {
500 if (error == ERESTART)
503 timespecsub(&ts, &ts2);
510 if (timespeccmp(&ts2, &ts, >=))
513 timespecsub(&ts3, &ts2);
514 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
518 #ifndef _SYS_SYSPROTO_H_
519 struct nanosleep_args {
520 struct timespec *rqtp;
521 struct timespec *rmtp;
526 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
528 struct timespec rmt, rqt;
531 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
536 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
538 error = kern_nanosleep(td, &rqt, &rmt);
539 if (error && uap->rmtp) {
542 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
549 #ifndef _SYS_SYSPROTO_H_
550 struct gettimeofday_args {
552 struct timezone *tzp;
557 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
565 error = copyout(&atv, uap->tp, sizeof (atv));
567 if (error == 0 && uap->tzp != NULL) {
568 rtz.tz_minuteswest = tz_minuteswest;
569 rtz.tz_dsttime = tz_dsttime;
570 error = copyout(&rtz, uap->tzp, sizeof (rtz));
575 #ifndef _SYS_SYSPROTO_H_
576 struct settimeofday_args {
578 struct timezone *tzp;
583 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
585 struct timeval atv, *tvp;
586 struct timezone atz, *tzp;
590 error = copyin(uap->tv, &atv, sizeof(atv));
597 error = copyin(uap->tzp, &atz, sizeof(atz));
603 return (kern_settimeofday(td, tvp, tzp));
607 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
611 error = priv_check(td, PRIV_SETTIMEOFDAY);
614 /* Verify all parameters before changing time. */
616 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
618 error = settime(td, tv);
620 if (tzp && error == 0) {
621 tz_minuteswest = tzp->tz_minuteswest;
622 tz_dsttime = tzp->tz_dsttime;
628 * Get value of an interval timer. The process virtual and profiling virtual
629 * time timers are kept in the p_stats area, since they can be swapped out.
630 * These are kept internally in the way they are specified externally: in
631 * time until they expire.
633 * The real time interval timer is kept in the process table slot for the
634 * process, and its value (it_value) is kept as an absolute time rather than
635 * as a delta, so that it is easy to keep periodic real-time signals from
638 * Virtual time timers are processed in the hardclock() routine of
639 * kern_clock.c. The real time timer is processed by a timeout routine,
640 * called from the softclock() routine. Since a callout may be delayed in
641 * real time due to interrupt processing in the system, it is possible for
642 * the real time timeout routine (realitexpire, given below), to be delayed
643 * in real time past when it is supposed to occur. It does not suffice,
644 * therefore, to reload the real timer .it_value from the real time timers
645 * .it_interval. Rather, we compute the next time in absolute time the timer
648 #ifndef _SYS_SYSPROTO_H_
649 struct getitimer_args {
651 struct itimerval *itv;
655 sys_getitimer(struct thread *td, struct getitimer_args *uap)
657 struct itimerval aitv;
660 error = kern_getitimer(td, uap->which, &aitv);
663 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
667 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
669 struct proc *p = td->td_proc;
672 if (which > ITIMER_PROF)
675 if (which == ITIMER_REAL) {
677 * Convert from absolute to relative time in .it_value
678 * part of real time timer. If time for real time timer
679 * has passed return 0, else return difference between
680 * current time and time for the timer to go off.
683 *aitv = p->p_realtimer;
685 if (timevalisset(&aitv->it_value)) {
686 getmicrouptime(&ctv);
687 if (timevalcmp(&aitv->it_value, &ctv, <))
688 timevalclear(&aitv->it_value);
690 timevalsub(&aitv->it_value, &ctv);
694 *aitv = p->p_stats->p_timer[which];
700 #ifndef _SYS_SYSPROTO_H_
701 struct setitimer_args {
703 struct itimerval *itv, *oitv;
707 sys_setitimer(struct thread *td, struct setitimer_args *uap)
709 struct itimerval aitv, oitv;
712 if (uap->itv == NULL) {
713 uap->itv = uap->oitv;
714 return (sys_getitimer(td, (struct getitimer_args *)uap));
717 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
719 error = kern_setitimer(td, uap->which, &aitv, &oitv);
720 if (error != 0 || uap->oitv == NULL)
722 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
726 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
727 struct itimerval *oitv)
729 struct proc *p = td->td_proc;
733 return (kern_getitimer(td, which, oitv));
735 if (which > ITIMER_PROF)
737 if (itimerfix(&aitv->it_value))
739 if (!timevalisset(&aitv->it_value))
740 timevalclear(&aitv->it_interval);
741 else if (itimerfix(&aitv->it_interval))
744 if (which == ITIMER_REAL) {
746 if (timevalisset(&p->p_realtimer.it_value))
747 callout_stop(&p->p_itcallout);
748 getmicrouptime(&ctv);
749 if (timevalisset(&aitv->it_value)) {
750 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
752 timevaladd(&aitv->it_value, &ctv);
754 *oitv = p->p_realtimer;
755 p->p_realtimer = *aitv;
757 if (timevalisset(&oitv->it_value)) {
758 if (timevalcmp(&oitv->it_value, &ctv, <))
759 timevalclear(&oitv->it_value);
761 timevalsub(&oitv->it_value, &ctv);
765 *oitv = p->p_stats->p_timer[which];
766 p->p_stats->p_timer[which] = *aitv;
773 * Real interval timer expired:
774 * send process whose timer expired an alarm signal.
775 * If time is not set up to reload, then just return.
776 * Else compute next time timer should go off which is > current time.
777 * This is where delay in processing this timeout causes multiple
778 * SIGALRM calls to be compressed into one.
779 * tvtohz() always adds 1 to allow for the time until the next clock
780 * interrupt being strictly less than 1 clock tick, but we don't want
781 * that here since we want to appear to be in sync with the clock
782 * interrupt even when we're delayed.
785 realitexpire(void *arg)
788 struct timeval ctv, ntv;
790 p = (struct proc *)arg;
791 kern_psignal(p, SIGALRM);
792 if (!timevalisset(&p->p_realtimer.it_interval)) {
793 timevalclear(&p->p_realtimer.it_value);
794 if (p->p_flag & P_WEXIT)
795 wakeup(&p->p_itcallout);
799 timevaladd(&p->p_realtimer.it_value,
800 &p->p_realtimer.it_interval);
801 getmicrouptime(&ctv);
802 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
803 ntv = p->p_realtimer.it_value;
804 timevalsub(&ntv, &ctv);
805 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
814 * Check that a proposed value to load into the .it_value or
815 * .it_interval part of an interval timer is acceptable, and
816 * fix it to have at least minimal value (i.e. if it is less
817 * than the resolution of the clock, round it up.)
820 itimerfix(struct timeval *tv)
823 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
825 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
831 * Decrement an interval timer by a specified number
832 * of microseconds, which must be less than a second,
833 * i.e. < 1000000. If the timer expires, then reload
834 * it. In this case, carry over (usec - old value) to
835 * reduce the value reloaded into the timer so that
836 * the timer does not drift. This routine assumes
837 * that it is called in a context where the timers
838 * on which it is operating cannot change in value.
841 itimerdecr(struct itimerval *itp, int usec)
844 if (itp->it_value.tv_usec < usec) {
845 if (itp->it_value.tv_sec == 0) {
846 /* expired, and already in next interval */
847 usec -= itp->it_value.tv_usec;
850 itp->it_value.tv_usec += 1000000;
851 itp->it_value.tv_sec--;
853 itp->it_value.tv_usec -= usec;
855 if (timevalisset(&itp->it_value))
857 /* expired, exactly at end of interval */
859 if (timevalisset(&itp->it_interval)) {
860 itp->it_value = itp->it_interval;
861 itp->it_value.tv_usec -= usec;
862 if (itp->it_value.tv_usec < 0) {
863 itp->it_value.tv_usec += 1000000;
864 itp->it_value.tv_sec--;
867 itp->it_value.tv_usec = 0; /* sec is already 0 */
872 * Add and subtract routines for timevals.
873 * N.B.: subtract routine doesn't deal with
874 * results which are before the beginning,
875 * it just gets very confused in this case.
879 timevaladd(struct timeval *t1, const struct timeval *t2)
882 t1->tv_sec += t2->tv_sec;
883 t1->tv_usec += t2->tv_usec;
888 timevalsub(struct timeval *t1, const struct timeval *t2)
891 t1->tv_sec -= t2->tv_sec;
892 t1->tv_usec -= t2->tv_usec;
897 timevalfix(struct timeval *t1)
900 if (t1->tv_usec < 0) {
902 t1->tv_usec += 1000000;
904 if (t1->tv_usec >= 1000000) {
906 t1->tv_usec -= 1000000;
911 * ratecheck(): simple time-based rate-limit checking.
914 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
916 struct timeval tv, delta;
919 getmicrouptime(&tv); /* NB: 10ms precision */
921 timevalsub(&delta, lasttime);
924 * check for 0,0 is so that the message will be seen at least once,
925 * even if interval is huge.
927 if (timevalcmp(&delta, mininterval, >=) ||
928 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
937 * ppsratecheck(): packets (or events) per second limitation.
939 * Return 0 if the limit is to be enforced (e.g. the caller
940 * should drop a packet because of the rate limitation).
942 * maxpps of 0 always causes zero to be returned. maxpps of -1
943 * always causes 1 to be returned; this effectively defeats rate
946 * Note that we maintain the struct timeval for compatibility
947 * with other bsd systems. We reuse the storage and just monitor
948 * clock ticks for minimal overhead.
951 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
956 * Reset the last time and counter if this is the first call
957 * or more than a second has passed since the last update of
961 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
962 lasttime->tv_sec = now;
964 return (maxpps != 0);
966 (*curpps)++; /* NB: ignore potential overflow */
967 return (maxpps < 0 || *curpps < maxpps);
974 struct kclock rt_clock = {
975 .timer_create = realtimer_create,
976 .timer_delete = realtimer_delete,
977 .timer_settime = realtimer_settime,
978 .timer_gettime = realtimer_gettime,
982 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
983 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
984 register_posix_clock(CLOCK_REALTIME, &rt_clock);
985 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
986 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
987 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
988 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
989 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
990 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
991 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
992 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
996 register_posix_clock(int clockid, struct kclock *clk)
998 if ((unsigned)clockid >= MAX_CLOCKS) {
999 printf("%s: invalid clockid\n", __func__);
1002 posix_clocks[clockid] = *clk;
1007 itimer_init(void *mem, int size, int flags)
1011 it = (struct itimer *)mem;
1012 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1017 itimer_fini(void *mem, int size)
1021 it = (struct itimer *)mem;
1022 mtx_destroy(&it->it_mtx);
1026 itimer_enter(struct itimer *it)
1029 mtx_assert(&it->it_mtx, MA_OWNED);
1034 itimer_leave(struct itimer *it)
1037 mtx_assert(&it->it_mtx, MA_OWNED);
1038 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1040 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1044 #ifndef _SYS_SYSPROTO_H_
1045 struct ktimer_create_args {
1047 struct sigevent * evp;
1052 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1054 struct sigevent *evp, ev;
1058 if (uap->evp == NULL) {
1061 error = copyin(uap->evp, &ev, sizeof(ev));
1066 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1068 error = copyout(&id, uap->timerid, sizeof(int));
1070 kern_ktimer_delete(td, id);
1076 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1077 int *timerid, int preset_id)
1079 struct proc *p = td->td_proc;
1084 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1087 if (posix_clocks[clock_id].timer_create == NULL)
1091 if (evp->sigev_notify != SIGEV_NONE &&
1092 evp->sigev_notify != SIGEV_SIGNAL &&
1093 evp->sigev_notify != SIGEV_THREAD_ID)
1095 if ((evp->sigev_notify == SIGEV_SIGNAL ||
1096 evp->sigev_notify == SIGEV_THREAD_ID) &&
1097 !_SIG_VALID(evp->sigev_signo))
1101 if (p->p_itimers == NULL)
1104 it = uma_zalloc(itimer_zone, M_WAITOK);
1106 it->it_usecount = 0;
1108 timespecclear(&it->it_time.it_value);
1109 timespecclear(&it->it_time.it_interval);
1111 it->it_overrun_last = 0;
1112 it->it_clockid = clock_id;
1113 it->it_timerid = -1;
1115 ksiginfo_init(&it->it_ksi);
1116 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1117 error = CLOCK_CALL(clock_id, timer_create, (it));
1122 if (preset_id != -1) {
1123 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1125 if (p->p_itimers->its_timers[id] != NULL) {
1132 * Find a free timer slot, skipping those reserved
1135 for (id = 3; id < TIMER_MAX; id++)
1136 if (p->p_itimers->its_timers[id] == NULL)
1138 if (id == TIMER_MAX) {
1144 it->it_timerid = id;
1145 p->p_itimers->its_timers[id] = it;
1147 it->it_sigev = *evp;
1149 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1152 case CLOCK_REALTIME:
1153 it->it_sigev.sigev_signo = SIGALRM;
1156 it->it_sigev.sigev_signo = SIGVTALRM;
1159 it->it_sigev.sigev_signo = SIGPROF;
1162 it->it_sigev.sigev_value.sival_int = id;
1165 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1166 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1167 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1168 it->it_ksi.ksi_code = SI_TIMER;
1169 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1170 it->it_ksi.ksi_timerid = id;
1178 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1180 uma_zfree(itimer_zone, it);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct ktimer_delete_args {
1190 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1193 return (kern_ktimer_delete(td, uap->timerid));
1196 static struct itimer *
1197 itimer_find(struct proc *p, int timerid)
1201 PROC_LOCK_ASSERT(p, MA_OWNED);
1202 if ((p->p_itimers == NULL) ||
1203 (timerid < 0) || (timerid >= TIMER_MAX) ||
1204 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1208 if ((it->it_flags & ITF_DELETING) != 0) {
1216 kern_ktimer_delete(struct thread *td, int timerid)
1218 struct proc *p = td->td_proc;
1222 it = itimer_find(p, timerid);
1229 it->it_flags |= ITF_DELETING;
1230 while (it->it_usecount > 0) {
1231 it->it_flags |= ITF_WANTED;
1232 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1234 it->it_flags &= ~ITF_WANTED;
1235 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1239 if (KSI_ONQ(&it->it_ksi))
1240 sigqueue_take(&it->it_ksi);
1241 p->p_itimers->its_timers[timerid] = NULL;
1243 uma_zfree(itimer_zone, it);
1247 #ifndef _SYS_SYSPROTO_H_
1248 struct ktimer_settime_args {
1251 const struct itimerspec * value;
1252 struct itimerspec * ovalue;
1256 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1258 struct itimerspec val, oval, *ovalp;
1261 error = copyin(uap->value, &val, sizeof(val));
1264 ovalp = uap->ovalue != NULL ? &oval : NULL;
1265 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1266 if (error == 0 && uap->ovalue != NULL)
1267 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1272 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1273 struct itimerspec *val, struct itimerspec *oval)
1281 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1287 error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1295 #ifndef _SYS_SYSPROTO_H_
1296 struct ktimer_gettime_args {
1298 struct itimerspec * value;
1302 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1304 struct itimerspec val;
1307 error = kern_ktimer_gettime(td, uap->timerid, &val);
1309 error = copyout(&val, uap->value, sizeof(val));
1314 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1322 if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1328 error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1335 #ifndef _SYS_SYSPROTO_H_
1336 struct timer_getoverrun_args {
1341 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1343 struct proc *p = td->td_proc;
1348 if (uap->timerid < 3 ||
1349 (it = itimer_find(p, uap->timerid)) == NULL) {
1353 td->td_retval[0] = it->it_overrun_last;
1362 realtimer_create(struct itimer *it)
1364 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1369 realtimer_delete(struct itimer *it)
1371 mtx_assert(&it->it_mtx, MA_OWNED);
1374 * clear timer's value and interval to tell realtimer_expire
1375 * to not rearm the timer.
1377 timespecclear(&it->it_time.it_value);
1378 timespecclear(&it->it_time.it_interval);
1380 callout_drain(&it->it_callout);
1386 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1388 struct timespec cts;
1390 mtx_assert(&it->it_mtx, MA_OWNED);
1392 realtimer_clocktime(it->it_clockid, &cts);
1393 *ovalue = it->it_time;
1394 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1395 timespecsub(&ovalue->it_value, &cts);
1396 if (ovalue->it_value.tv_sec < 0 ||
1397 (ovalue->it_value.tv_sec == 0 &&
1398 ovalue->it_value.tv_nsec == 0)) {
1399 ovalue->it_value.tv_sec = 0;
1400 ovalue->it_value.tv_nsec = 1;
1407 realtimer_settime(struct itimer *it, int flags,
1408 struct itimerspec *value, struct itimerspec *ovalue)
1410 struct timespec cts, ts;
1412 struct itimerspec val;
1414 mtx_assert(&it->it_mtx, MA_OWNED);
1417 if (itimespecfix(&val.it_value))
1420 if (timespecisset(&val.it_value)) {
1421 if (itimespecfix(&val.it_interval))
1424 timespecclear(&val.it_interval);
1428 realtimer_gettime(it, ovalue);
1431 if (timespecisset(&val.it_value)) {
1432 realtimer_clocktime(it->it_clockid, &cts);
1434 if ((flags & TIMER_ABSTIME) == 0) {
1435 /* Convert to absolute time. */
1436 timespecadd(&it->it_time.it_value, &cts);
1438 timespecsub(&ts, &cts);
1440 * We don't care if ts is negative, tztohz will
1444 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1445 callout_reset(&it->it_callout, tvtohz(&tv),
1446 realtimer_expire, it);
1448 callout_stop(&it->it_callout);
1455 realtimer_clocktime(clockid_t id, struct timespec *ts)
1457 if (id == CLOCK_REALTIME)
1459 else /* CLOCK_MONOTONIC */
1464 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1468 PROC_LOCK_ASSERT(p, MA_OWNED);
1469 it = itimer_find(p, timerid);
1471 ksi->ksi_overrun = it->it_overrun;
1472 it->it_overrun_last = it->it_overrun;
1481 itimespecfix(struct timespec *ts)
1484 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1486 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1487 ts->tv_nsec = tick * 1000;
1491 /* Timeout callback for realtime timer */
1493 realtimer_expire(void *arg)
1495 struct timespec cts, ts;
1499 it = (struct itimer *)arg;
1501 realtimer_clocktime(it->it_clockid, &cts);
1502 /* Only fire if time is reached. */
1503 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1504 if (timespecisset(&it->it_time.it_interval)) {
1505 timespecadd(&it->it_time.it_value,
1506 &it->it_time.it_interval);
1507 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1508 if (it->it_overrun < INT_MAX)
1511 it->it_ksi.ksi_errno = ERANGE;
1512 timespecadd(&it->it_time.it_value,
1513 &it->it_time.it_interval);
1516 /* single shot timer ? */
1517 timespecclear(&it->it_time.it_value);
1519 if (timespecisset(&it->it_time.it_value)) {
1520 ts = it->it_time.it_value;
1521 timespecsub(&ts, &cts);
1522 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1523 callout_reset(&it->it_callout, tvtohz(&tv),
1524 realtimer_expire, it);
1531 } else if (timespecisset(&it->it_time.it_value)) {
1532 ts = it->it_time.it_value;
1533 timespecsub(&ts, &cts);
1534 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1535 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1541 itimer_fire(struct itimer *it)
1543 struct proc *p = it->it_proc;
1546 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1547 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1548 if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1550 timespecclear(&it->it_time.it_value);
1551 timespecclear(&it->it_time.it_interval);
1552 callout_stop(&it->it_callout);
1556 if (!KSI_ONQ(&it->it_ksi)) {
1557 it->it_ksi.ksi_errno = 0;
1558 ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1559 tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1561 if (it->it_overrun < INT_MAX)
1564 it->it_ksi.ksi_errno = ERANGE;
1571 itimers_alloc(struct proc *p)
1573 struct itimers *its;
1576 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1577 LIST_INIT(&its->its_virtual);
1578 LIST_INIT(&its->its_prof);
1579 TAILQ_INIT(&its->its_worklist);
1580 for (i = 0; i < TIMER_MAX; i++)
1581 its->its_timers[i] = NULL;
1583 if (p->p_itimers == NULL) {
1589 free(its, M_SUBPROC);
1594 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1596 itimers_event_hook_exit(arg, p);
1599 /* Clean up timers when some process events are being triggered. */
1601 itimers_event_hook_exit(void *arg, struct proc *p)
1603 struct itimers *its;
1605 int event = (int)(intptr_t)arg;
1608 if (p->p_itimers != NULL) {
1610 for (i = 0; i < MAX_CLOCKS; ++i) {
1611 if (posix_clocks[i].event_hook != NULL)
1612 CLOCK_CALL(i, event_hook, (p, i, event));
1615 * According to susv3, XSI interval timers should be inherited
1618 if (event == ITIMER_EV_EXEC)
1620 else if (event == ITIMER_EV_EXIT)
1623 panic("unhandled event");
1624 for (; i < TIMER_MAX; ++i) {
1625 if ((it = its->its_timers[i]) != NULL)
1626 kern_ktimer_delete(curthread, i);
1628 if (its->its_timers[0] == NULL &&
1629 its->its_timers[1] == NULL &&
1630 its->its_timers[2] == NULL) {
1631 free(its, M_SUBPROC);
1632 p->p_itimers = NULL;