2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/limits.h>
40 #include <sys/clock.h>
42 #include <sys/mutex.h>
43 #include <sys/sysproto.h>
44 #include <sys/eventhandler.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/kernel.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/sysent.h>
53 #include <sys/posix4.h>
55 #include <sys/timers.h>
56 #include <sys/timetc.h>
57 #include <sys/vnode.h>
59 #include <security/mac/mac_framework.h>
62 #include <vm/vm_extern.h>
64 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
66 static struct kclock posix_clocks[MAX_CLOCKS];
67 static uma_zone_t itimer_zone = NULL;
70 * Time of day and interval timer support.
72 * These routines provide the kernel entry points to get and set
73 * the time-of-day and per-process interval timers. Subroutines
74 * here provide support for adding and subtracting timeval structures
75 * and decrementing interval timers, optionally reloading the interval
76 * timers when they expire.
79 static int settime(struct thread *, struct timeval *);
80 static void timevalfix(struct timeval *);
81 static void no_lease_updatetime(int);
83 static void itimer_start(void);
84 static int itimer_init(void *, int, int);
85 static void itimer_fini(void *, int);
86 static void itimer_enter(struct itimer *);
87 static void itimer_leave(struct itimer *);
88 static struct itimer *itimer_find(struct proc *, int);
89 static void itimers_alloc(struct proc *);
90 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
91 static void itimers_event_hook_exit(void *arg, struct proc *p);
92 static int realtimer_create(struct itimer *);
93 static int realtimer_gettime(struct itimer *, struct itimerspec *);
94 static int realtimer_settime(struct itimer *, int,
95 struct itimerspec *, struct itimerspec *);
96 static int realtimer_delete(struct itimer *);
97 static void realtimer_clocktime(clockid_t, struct timespec *);
98 static void realtimer_expire(void *);
99 static int kern_timer_create(struct thread *, clockid_t,
100 struct sigevent *, int *, int);
101 static int kern_timer_delete(struct thread *, int);
103 int register_posix_clock(int, struct kclock *);
104 void itimer_fire(struct itimer *it);
105 int itimespecfix(struct timespec *ts);
107 #define CLOCK_CALL(clock, call, arglist) \
108 ((*posix_clocks[clock].call) arglist)
110 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
114 no_lease_updatetime(deltat)
119 void (*lease_updatetime)(int) = no_lease_updatetime;
122 settime(struct thread *td, struct timeval *tv)
124 struct timeval delta, tv1, tv2;
125 static struct timeval maxtime, laststep;
132 timevalsub(&delta, &tv1);
135 * If the system is secure, we do not allow the time to be
136 * set to a value earlier than 1 second less than the highest
137 * time we have yet seen. The worst a miscreant can do in
138 * this circumstance is "freeze" time. He couldn't go
141 * We similarly do not allow the clock to be stepped more
142 * than one second, nor more than once per second. This allows
143 * a miscreant to make the clock march double-time, but no worse.
145 if (securelevel_gt(td->td_ucred, 1) != 0) {
146 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
148 * Update maxtime to latest time we've seen.
150 if (tv1.tv_sec > maxtime.tv_sec)
153 timevalsub(&tv2, &maxtime);
154 if (tv2.tv_sec < -1) {
155 tv->tv_sec = maxtime.tv_sec - 1;
156 printf("Time adjustment clamped to -1 second\n");
159 if (tv1.tv_sec == laststep.tv_sec) {
163 if (delta.tv_sec > 1) {
164 tv->tv_sec = tv1.tv_sec + 1;
165 printf("Time adjustment clamped to +1 second\n");
171 ts.tv_sec = tv->tv_sec;
172 ts.tv_nsec = tv->tv_usec * 1000;
175 (void) splsoftclock();
176 lease_updatetime(delta.tv_sec);
183 #ifndef _SYS_SYSPROTO_H_
184 struct clock_gettime_args {
195 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
200 error = kern_clock_gettime(td, uap->clock_id, &ats);
202 error = copyout(&ats, uap->tp, sizeof(ats));
208 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
210 struct timeval sys, user;
215 case CLOCK_REALTIME: /* Default to precise. */
216 case CLOCK_REALTIME_PRECISE:
219 case CLOCK_REALTIME_FAST:
224 calcru(p, &user, &sys);
226 TIMEVAL_TO_TIMESPEC(&user, ats);
230 calcru(p, &user, &sys);
232 timevaladd(&user, &sys);
233 TIMEVAL_TO_TIMESPEC(&user, ats);
235 case CLOCK_MONOTONIC: /* Default to precise. */
236 case CLOCK_MONOTONIC_PRECISE:
238 case CLOCK_UPTIME_PRECISE:
241 case CLOCK_UPTIME_FAST:
242 case CLOCK_MONOTONIC_FAST:
246 ats->tv_sec = time_second;
255 #ifndef _SYS_SYSPROTO_H_
256 struct clock_settime_args {
258 const struct timespec *tp;
267 clock_settime(struct thread *td, struct clock_settime_args *uap)
272 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
274 return (kern_clock_settime(td, uap->clock_id, &ats));
278 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
284 error = mac_check_system_settime(td->td_ucred);
288 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
290 if (clock_id != CLOCK_REALTIME)
292 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
294 /* XXX Don't convert nsec->usec and back */
295 TIMESPEC_TO_TIMEVAL(&atv, ats);
296 error = settime(td, &atv);
300 #ifndef _SYS_SYSPROTO_H_
301 struct clock_getres_args {
308 clock_getres(struct thread *td, struct clock_getres_args *uap)
316 error = kern_clock_getres(td, uap->clock_id, &ts);
318 error = copyout(&ts, uap->tp, sizeof(ts));
323 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
329 case CLOCK_REALTIME_FAST:
330 case CLOCK_REALTIME_PRECISE:
331 case CLOCK_MONOTONIC:
332 case CLOCK_MONOTONIC_FAST:
333 case CLOCK_MONOTONIC_PRECISE:
335 case CLOCK_UPTIME_FAST:
336 case CLOCK_UPTIME_PRECISE:
338 * Round up the result of the division cheaply by adding 1.
339 * Rounding up is especially important if rounding down
340 * would give 0. Perfect rounding is unimportant.
342 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
346 /* Accurately round up here because we can do so cheaply. */
347 ts->tv_nsec = (1000000000 + hz - 1) / hz;
362 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
364 struct timespec ts, ts2, ts3;
368 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
370 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
373 timespecadd(&ts, rqt);
374 TIMESPEC_TO_TIMEVAL(&tv, rqt);
376 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
379 if (error != EWOULDBLOCK) {
380 if (error == ERESTART)
383 timespecsub(&ts, &ts2);
390 if (timespeccmp(&ts2, &ts, >=))
393 timespecsub(&ts3, &ts2);
394 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
398 #ifndef _SYS_SYSPROTO_H_
399 struct nanosleep_args {
400 struct timespec *rqtp;
401 struct timespec *rmtp;
410 nanosleep(struct thread *td, struct nanosleep_args *uap)
412 struct timespec rmt, rqt;
415 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
420 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
422 error = kern_nanosleep(td, &rqt, &rmt);
423 if (error && uap->rmtp) {
426 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
433 #ifndef _SYS_SYSPROTO_H_
434 struct gettimeofday_args {
436 struct timezone *tzp;
444 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
452 error = copyout(&atv, uap->tp, sizeof (atv));
454 if (error == 0 && uap->tzp != NULL) {
455 rtz.tz_minuteswest = tz_minuteswest;
456 rtz.tz_dsttime = tz_dsttime;
457 error = copyout(&rtz, uap->tzp, sizeof (rtz));
462 #ifndef _SYS_SYSPROTO_H_
463 struct settimeofday_args {
465 struct timezone *tzp;
473 settimeofday(struct thread *td, struct settimeofday_args *uap)
475 struct timeval atv, *tvp;
476 struct timezone atz, *tzp;
480 error = copyin(uap->tv, &atv, sizeof(atv));
487 error = copyin(uap->tzp, &atz, sizeof(atz));
493 return (kern_settimeofday(td, tvp, tzp));
497 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
502 error = mac_check_system_settime(td->td_ucred);
506 error = priv_check(td, PRIV_SETTIMEOFDAY);
509 /* Verify all parameters before changing time. */
511 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
513 error = settime(td, tv);
515 if (tzp && error == 0) {
516 tz_minuteswest = tzp->tz_minuteswest;
517 tz_dsttime = tzp->tz_dsttime;
523 * Get value of an interval timer. The process virtual and
524 * profiling virtual time timers are kept in the p_stats area, since
525 * they can be swapped out. These are kept internally in the
526 * way they are specified externally: in time until they expire.
528 * The real time interval timer is kept in the process table slot
529 * for the process, and its value (it_value) is kept as an
530 * absolute time rather than as a delta, so that it is easy to keep
531 * periodic real-time signals from drifting.
533 * Virtual time timers are processed in the hardclock() routine of
534 * kern_clock.c. The real time timer is processed by a timeout
535 * routine, called from the softclock() routine. Since a callout
536 * may be delayed in real time due to interrupt processing in the system,
537 * it is possible for the real time timeout routine (realitexpire, given below),
538 * to be delayed in real time past when it is supposed to occur. It
539 * does not suffice, therefore, to reload the real timer .it_value from the
540 * real time timers .it_interval. Rather, we compute the next time in
541 * absolute time the timer should go off.
543 #ifndef _SYS_SYSPROTO_H_
544 struct getitimer_args {
546 struct itimerval *itv;
553 getitimer(struct thread *td, struct getitimer_args *uap)
555 struct itimerval aitv;
558 error = kern_getitimer(td, uap->which, &aitv);
561 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
565 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
567 struct proc *p = td->td_proc;
570 if (which > ITIMER_PROF)
573 if (which == ITIMER_REAL) {
575 * Convert from absolute to relative time in .it_value
576 * part of real time timer. If time for real time timer
577 * has passed return 0, else return difference between
578 * current time and time for the timer to go off.
581 *aitv = p->p_realtimer;
583 if (timevalisset(&aitv->it_value)) {
584 getmicrouptime(&ctv);
585 if (timevalcmp(&aitv->it_value, &ctv, <))
586 timevalclear(&aitv->it_value);
588 timevalsub(&aitv->it_value, &ctv);
591 mtx_lock_spin(&sched_lock);
592 *aitv = p->p_stats->p_timer[which];
593 mtx_unlock_spin(&sched_lock);
598 #ifndef _SYS_SYSPROTO_H_
599 struct setitimer_args {
601 struct itimerval *itv, *oitv;
609 setitimer(struct thread *td, struct setitimer_args *uap)
611 struct itimerval aitv, oitv;
614 if (uap->itv == NULL) {
615 uap->itv = uap->oitv;
616 return (getitimer(td, (struct getitimer_args *)uap));
619 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
621 error = kern_setitimer(td, uap->which, &aitv, &oitv);
622 if (error != 0 || uap->oitv == NULL)
624 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
628 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
629 struct itimerval *oitv)
631 struct proc *p = td->td_proc;
635 return (kern_getitimer(td, which, oitv));
637 if (which > ITIMER_PROF)
639 if (itimerfix(&aitv->it_value))
641 if (!timevalisset(&aitv->it_value))
642 timevalclear(&aitv->it_interval);
643 else if (itimerfix(&aitv->it_interval))
646 if (which == ITIMER_REAL) {
648 if (timevalisset(&p->p_realtimer.it_value))
649 callout_stop(&p->p_itcallout);
650 getmicrouptime(&ctv);
651 if (timevalisset(&aitv->it_value)) {
652 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
654 timevaladd(&aitv->it_value, &ctv);
656 *oitv = p->p_realtimer;
657 p->p_realtimer = *aitv;
659 if (timevalisset(&oitv->it_value)) {
660 if (timevalcmp(&oitv->it_value, &ctv, <))
661 timevalclear(&oitv->it_value);
663 timevalsub(&oitv->it_value, &ctv);
666 mtx_lock_spin(&sched_lock);
667 *oitv = p->p_stats->p_timer[which];
668 p->p_stats->p_timer[which] = *aitv;
669 mtx_unlock_spin(&sched_lock);
675 * Real interval timer expired:
676 * send process whose timer expired an alarm signal.
677 * If time is not set up to reload, then just return.
678 * Else compute next time timer should go off which is > current time.
679 * This is where delay in processing this timeout causes multiple
680 * SIGALRM calls to be compressed into one.
681 * tvtohz() always adds 1 to allow for the time until the next clock
682 * interrupt being strictly less than 1 clock tick, but we don't want
683 * that here since we want to appear to be in sync with the clock
684 * interrupt even when we're delayed.
687 realitexpire(void *arg)
690 struct timeval ctv, ntv;
692 p = (struct proc *)arg;
695 if (!timevalisset(&p->p_realtimer.it_interval)) {
696 timevalclear(&p->p_realtimer.it_value);
697 if (p->p_flag & P_WEXIT)
698 wakeup(&p->p_itcallout);
703 timevaladd(&p->p_realtimer.it_value,
704 &p->p_realtimer.it_interval);
705 getmicrouptime(&ctv);
706 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
707 ntv = p->p_realtimer.it_value;
708 timevalsub(&ntv, &ctv);
709 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
719 * Check that a proposed value to load into the .it_value or
720 * .it_interval part of an interval timer is acceptable, and
721 * fix it to have at least minimal value (i.e. if it is less
722 * than the resolution of the clock, round it up.)
725 itimerfix(struct timeval *tv)
728 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
730 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
736 * Decrement an interval timer by a specified number
737 * of microseconds, which must be less than a second,
738 * i.e. < 1000000. If the timer expires, then reload
739 * it. In this case, carry over (usec - old value) to
740 * reduce the value reloaded into the timer so that
741 * the timer does not drift. This routine assumes
742 * that it is called in a context where the timers
743 * on which it is operating cannot change in value.
746 itimerdecr(struct itimerval *itp, int usec)
749 if (itp->it_value.tv_usec < usec) {
750 if (itp->it_value.tv_sec == 0) {
751 /* expired, and already in next interval */
752 usec -= itp->it_value.tv_usec;
755 itp->it_value.tv_usec += 1000000;
756 itp->it_value.tv_sec--;
758 itp->it_value.tv_usec -= usec;
760 if (timevalisset(&itp->it_value))
762 /* expired, exactly at end of interval */
764 if (timevalisset(&itp->it_interval)) {
765 itp->it_value = itp->it_interval;
766 itp->it_value.tv_usec -= usec;
767 if (itp->it_value.tv_usec < 0) {
768 itp->it_value.tv_usec += 1000000;
769 itp->it_value.tv_sec--;
772 itp->it_value.tv_usec = 0; /* sec is already 0 */
777 * Add and subtract routines for timevals.
778 * N.B.: subtract routine doesn't deal with
779 * results which are before the beginning,
780 * it just gets very confused in this case.
784 timevaladd(struct timeval *t1, const struct timeval *t2)
787 t1->tv_sec += t2->tv_sec;
788 t1->tv_usec += t2->tv_usec;
793 timevalsub(struct timeval *t1, const struct timeval *t2)
796 t1->tv_sec -= t2->tv_sec;
797 t1->tv_usec -= t2->tv_usec;
802 timevalfix(struct timeval *t1)
805 if (t1->tv_usec < 0) {
807 t1->tv_usec += 1000000;
809 if (t1->tv_usec >= 1000000) {
811 t1->tv_usec -= 1000000;
816 * ratecheck(): simple time-based rate-limit checking.
819 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
821 struct timeval tv, delta;
824 getmicrouptime(&tv); /* NB: 10ms precision */
826 timevalsub(&delta, lasttime);
829 * check for 0,0 is so that the message will be seen at least once,
830 * even if interval is huge.
832 if (timevalcmp(&delta, mininterval, >=) ||
833 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
842 * ppsratecheck(): packets (or events) per second limitation.
844 * Return 0 if the limit is to be enforced (e.g. the caller
845 * should drop a packet because of the rate limitation).
847 * maxpps of 0 always causes zero to be returned. maxpps of -1
848 * always causes 1 to be returned; this effectively defeats rate
851 * Note that we maintain the struct timeval for compatibility
852 * with other bsd systems. We reuse the storage and just monitor
853 * clock ticks for minimal overhead.
856 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
861 * Reset the last time and counter if this is the first call
862 * or more than a second has passed since the last update of
866 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
867 lasttime->tv_sec = now;
869 return (maxpps != 0);
871 (*curpps)++; /* NB: ignore potential overflow */
872 return (maxpps < 0 || *curpps < maxpps);
879 struct kclock rt_clock = {
880 .timer_create = realtimer_create,
881 .timer_delete = realtimer_delete,
882 .timer_settime = realtimer_settime,
883 .timer_gettime = realtimer_gettime,
887 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
888 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
889 register_posix_clock(CLOCK_REALTIME, &rt_clock);
890 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
891 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
892 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
893 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
894 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
895 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
896 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
897 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
901 register_posix_clock(int clockid, struct kclock *clk)
903 if ((unsigned)clockid >= MAX_CLOCKS) {
904 printf("%s: invalid clockid\n", __func__);
907 posix_clocks[clockid] = *clk;
912 itimer_init(void *mem, int size, int flags)
916 it = (struct itimer *)mem;
917 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
922 itimer_fini(void *mem, int size)
926 it = (struct itimer *)mem;
927 mtx_destroy(&it->it_mtx);
931 itimer_enter(struct itimer *it)
934 mtx_assert(&it->it_mtx, MA_OWNED);
939 itimer_leave(struct itimer *it)
942 mtx_assert(&it->it_mtx, MA_OWNED);
943 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
945 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
949 #ifndef _SYS_SYSPROTO_H_
950 struct ktimer_create_args {
952 struct sigevent * evp;
958 ktimer_create(struct thread *td, struct ktimer_create_args *uap)
960 struct sigevent *evp1, ev;
964 if (uap->evp != NULL) {
965 error = copyin(uap->evp, &ev, sizeof(ev));
972 error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
975 error = copyout(&id, uap->timerid, sizeof(int));
977 kern_timer_delete(td, id);
983 kern_timer_create(struct thread *td, clockid_t clock_id,
984 struct sigevent *evp, int *timerid, int preset_id)
986 struct proc *p = td->td_proc;
991 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
994 if (posix_clocks[clock_id].timer_create == NULL)
998 if (evp->sigev_notify != SIGEV_NONE &&
999 evp->sigev_notify != SIGEV_SIGNAL &&
1000 evp->sigev_notify != SIGEV_THREAD_ID)
1002 if ((evp->sigev_notify == SIGEV_SIGNAL ||
1003 evp->sigev_notify == SIGEV_THREAD_ID) &&
1004 !_SIG_VALID(evp->sigev_signo))
1008 if (p->p_itimers == NULL)
1011 it = uma_zalloc(itimer_zone, M_WAITOK);
1013 it->it_usecount = 0;
1015 timespecclear(&it->it_time.it_value);
1016 timespecclear(&it->it_time.it_interval);
1018 it->it_overrun_last = 0;
1019 it->it_clockid = clock_id;
1020 it->it_timerid = -1;
1022 ksiginfo_init(&it->it_ksi);
1023 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1024 error = CLOCK_CALL(clock_id, timer_create, (it));
1029 if (preset_id != -1) {
1030 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1032 if (p->p_itimers->its_timers[id] != NULL) {
1039 * Find a free timer slot, skipping those reserved
1042 for (id = 3; id < TIMER_MAX; id++)
1043 if (p->p_itimers->its_timers[id] == NULL)
1045 if (id == TIMER_MAX) {
1051 it->it_timerid = id;
1052 p->p_itimers->its_timers[id] = it;
1054 it->it_sigev = *evp;
1056 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1059 case CLOCK_REALTIME:
1060 it->it_sigev.sigev_signo = SIGALRM;
1063 it->it_sigev.sigev_signo = SIGVTALRM;
1066 it->it_sigev.sigev_signo = SIGPROF;
1069 it->it_sigev.sigev_value.sival_int = id;
1072 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1073 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1074 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1075 it->it_ksi.ksi_code = SI_TIMER;
1076 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1077 it->it_ksi.ksi_timerid = id;
1085 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1087 uma_zfree(itimer_zone, it);
1091 #ifndef _SYS_SYSPROTO_H_
1092 struct ktimer_delete_args {
1098 ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1100 return (kern_timer_delete(td, uap->timerid));
1103 static struct itimer *
1104 itimer_find(struct proc *p, int timerid)
1108 PROC_LOCK_ASSERT(p, MA_OWNED);
1109 if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
1110 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1114 if ((it->it_flags & ITF_DELETING) != 0) {
1122 kern_timer_delete(struct thread *td, int timerid)
1124 struct proc *p = td->td_proc;
1128 it = itimer_find(p, timerid);
1135 it->it_flags |= ITF_DELETING;
1136 while (it->it_usecount > 0) {
1137 it->it_flags |= ITF_WANTED;
1138 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1140 it->it_flags &= ~ITF_WANTED;
1141 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1145 if (KSI_ONQ(&it->it_ksi))
1146 sigqueue_take(&it->it_ksi);
1147 p->p_itimers->its_timers[timerid] = NULL;
1149 uma_zfree(itimer_zone, it);
1153 #ifndef _SYS_SYSPROTO_H_
1154 struct ktimer_settime_args {
1157 const struct itimerspec * value;
1158 struct itimerspec * ovalue;
1163 ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1165 struct proc *p = td->td_proc;
1167 struct itimerspec val, oval, *ovalp;
1170 error = copyin(uap->value, &val, sizeof(val));
1174 if (uap->ovalue != NULL)
1180 if (uap->timerid < 3 ||
1181 (it = itimer_find(p, uap->timerid)) == NULL) {
1187 error = CLOCK_CALL(it->it_clockid, timer_settime,
1188 (it, uap->flags, &val, ovalp));
1192 if (error == 0 && uap->ovalue != NULL)
1193 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1197 #ifndef _SYS_SYSPROTO_H_
1198 struct ktimer_gettime_args {
1200 struct itimerspec * value;
1205 ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1207 struct proc *p = td->td_proc;
1209 struct itimerspec val;
1213 if (uap->timerid < 3 ||
1214 (it = itimer_find(p, uap->timerid)) == NULL) {
1220 error = CLOCK_CALL(it->it_clockid, timer_gettime,
1226 error = copyout(&val, uap->value, sizeof(val));
1230 #ifndef _SYS_SYSPROTO_H_
1231 struct timer_getoverrun_args {
1237 ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1239 struct proc *p = td->td_proc;
1244 if (uap->timerid < 3 ||
1245 (it = itimer_find(p, uap->timerid)) == NULL) {
1249 td->td_retval[0] = it->it_overrun_last;
1258 realtimer_create(struct itimer *it)
1260 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1265 realtimer_delete(struct itimer *it)
1267 mtx_assert(&it->it_mtx, MA_OWNED);
1270 callout_drain(&it->it_callout);
1276 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1278 struct timespec cts;
1280 mtx_assert(&it->it_mtx, MA_OWNED);
1282 realtimer_clocktime(it->it_clockid, &cts);
1283 *ovalue = it->it_time;
1284 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1285 timespecsub(&ovalue->it_value, &cts);
1286 if (ovalue->it_value.tv_sec < 0 ||
1287 (ovalue->it_value.tv_sec == 0 &&
1288 ovalue->it_value.tv_nsec == 0)) {
1289 ovalue->it_value.tv_sec = 0;
1290 ovalue->it_value.tv_nsec = 1;
1297 realtimer_settime(struct itimer *it, int flags,
1298 struct itimerspec *value, struct itimerspec *ovalue)
1300 struct timespec cts, ts;
1302 struct itimerspec val;
1304 mtx_assert(&it->it_mtx, MA_OWNED);
1307 if (itimespecfix(&val.it_value))
1310 if (timespecisset(&val.it_value)) {
1311 if (itimespecfix(&val.it_interval))
1314 timespecclear(&val.it_interval);
1318 realtimer_gettime(it, ovalue);
1321 if (timespecisset(&val.it_value)) {
1322 realtimer_clocktime(it->it_clockid, &cts);
1324 if ((flags & TIMER_ABSTIME) == 0) {
1325 /* Convert to absolute time. */
1326 timespecadd(&it->it_time.it_value, &cts);
1328 timespecsub(&ts, &cts);
1330 * We don't care if ts is negative, tztohz will
1334 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1335 callout_reset(&it->it_callout, tvtohz(&tv),
1336 realtimer_expire, it);
1338 callout_stop(&it->it_callout);
1345 realtimer_clocktime(clockid_t id, struct timespec *ts)
1347 if (id == CLOCK_REALTIME)
1349 else /* CLOCK_MONOTONIC */
1354 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1358 PROC_LOCK_ASSERT(p, MA_OWNED);
1359 it = itimer_find(p, timerid);
1361 ksi->ksi_overrun = it->it_overrun;
1362 it->it_overrun_last = it->it_overrun;
1371 itimespecfix(struct timespec *ts)
1374 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1376 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1377 ts->tv_nsec = tick * 1000;
1381 /* Timeout callback for realtime timer */
1383 realtimer_expire(void *arg)
1385 struct timespec cts, ts;
1390 it = (struct itimer *)arg;
1393 realtimer_clocktime(it->it_clockid, &cts);
1394 /* Only fire if time is reached. */
1395 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1396 if (timespecisset(&it->it_time.it_interval)) {
1397 timespecadd(&it->it_time.it_value,
1398 &it->it_time.it_interval);
1399 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1400 if (it->it_overrun < INT_MAX)
1403 it->it_ksi.ksi_errno = ERANGE;
1404 timespecadd(&it->it_time.it_value,
1405 &it->it_time.it_interval);
1408 /* single shot timer ? */
1409 timespecclear(&it->it_time.it_value);
1411 if (timespecisset(&it->it_time.it_value)) {
1412 ts = it->it_time.it_value;
1413 timespecsub(&ts, &cts);
1414 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1415 callout_reset(&it->it_callout, tvtohz(&tv),
1416 realtimer_expire, it);
1421 } else if (timespecisset(&it->it_time.it_value)) {
1422 ts = it->it_time.it_value;
1423 timespecsub(&ts, &cts);
1424 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1425 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1431 itimer_fire(struct itimer *it)
1433 struct proc *p = it->it_proc;
1436 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1437 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1439 if (!KSI_ONQ(&it->it_ksi)) {
1440 it->it_ksi.ksi_errno = 0;
1441 ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
1442 if (__predict_false(ret != 0)) {
1445 * Broken userland code, thread went
1446 * away, disarm the timer.
1450 timespecclear(&it->it_time.it_value);
1451 timespecclear(&it->it_time.it_interval);
1452 callout_stop(&it->it_callout);
1457 if (it->it_overrun < INT_MAX)
1460 it->it_ksi.ksi_errno = ERANGE;
1467 itimers_alloc(struct proc *p)
1469 struct itimers *its;
1472 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1473 LIST_INIT(&its->its_virtual);
1474 LIST_INIT(&its->its_prof);
1475 TAILQ_INIT(&its->its_worklist);
1476 for (i = 0; i < TIMER_MAX; i++)
1477 its->its_timers[i] = NULL;
1479 if (p->p_itimers == NULL) {
1485 free(its, M_SUBPROC);
1490 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1492 itimers_event_hook_exit(arg, p);
1495 /* Clean up timers when some process events are being triggered. */
1497 itimers_event_hook_exit(void *arg, struct proc *p)
1499 struct itimers *its;
1501 int event = (int)(intptr_t)arg;
1504 if (p->p_itimers != NULL) {
1506 for (i = 0; i < MAX_CLOCKS; ++i) {
1507 if (posix_clocks[i].event_hook != NULL)
1508 CLOCK_CALL(i, event_hook, (p, i, event));
1511 * According to susv3, XSI interval timers should be inherited
1514 if (event == ITIMER_EV_EXEC)
1516 else if (event == ITIMER_EV_EXIT)
1519 panic("unhandled event");
1520 for (; i < TIMER_MAX; ++i) {
1521 if ((it = its->its_timers[i]) != NULL)
1522 kern_timer_delete(curthread, i);
1524 if (its->its_timers[0] == NULL &&
1525 its->its_timers[1] == NULL &&
1526 its->its_timers[2] == NULL) {
1527 free(its, M_SUBPROC);
1528 p->p_itimers = NULL;