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)
62 static struct kclock posix_clocks[MAX_CLOCKS];
63 static uma_zone_t itimer_zone = NULL;
66 * Time of day and interval timer support.
68 * These routines provide the kernel entry points to get and set
69 * the time-of-day and per-process interval timers. Subroutines
70 * here provide support for adding and subtracting timeval structures
71 * and decrementing interval timers, optionally reloading the interval
72 * timers when they expire.
75 static int settime(struct thread *, struct timeval *);
76 static void timevalfix(struct timeval *);
77 static void no_lease_updatetime(int);
79 static void itimer_start(void);
80 static int itimer_init(void *, int, int);
81 static void itimer_fini(void *, int);
82 static void itimer_enter(struct itimer *);
83 static void itimer_leave(struct itimer *);
84 static struct itimer *itimer_find(struct proc *, int);
85 static void itimers_alloc(struct proc *);
86 static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
87 static void itimers_event_hook_exit(void *arg, struct proc *p);
88 static int realtimer_create(struct itimer *);
89 static int realtimer_gettime(struct itimer *, struct itimerspec *);
90 static int realtimer_settime(struct itimer *, int,
91 struct itimerspec *, struct itimerspec *);
92 static int realtimer_delete(struct itimer *);
93 static void realtimer_clocktime(clockid_t, struct timespec *);
94 static void realtimer_expire(void *);
95 static int kern_timer_create(struct thread *, clockid_t,
96 struct sigevent *, int *, int);
97 static int kern_timer_delete(struct thread *, int);
99 int register_posix_clock(int, struct kclock *);
100 void itimer_fire(struct itimer *it);
101 int itimespecfix(struct timespec *ts);
103 #define CLOCK_CALL(clock, call, arglist) \
104 ((*posix_clocks[clock].call) arglist)
106 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
110 no_lease_updatetime(deltat)
115 void (*lease_updatetime)(int) = no_lease_updatetime;
118 settime(struct thread *td, struct timeval *tv)
120 struct timeval delta, tv1, tv2;
121 static struct timeval maxtime, laststep;
128 timevalsub(&delta, &tv1);
131 * If the system is secure, we do not allow the time to be
132 * set to a value earlier than 1 second less than the highest
133 * time we have yet seen. The worst a miscreant can do in
134 * this circumstance is "freeze" time. He couldn't go
137 * We similarly do not allow the clock to be stepped more
138 * than one second, nor more than once per second. This allows
139 * a miscreant to make the clock march double-time, but no worse.
141 if (securelevel_gt(td->td_ucred, 1) != 0) {
142 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
144 * Update maxtime to latest time we've seen.
146 if (tv1.tv_sec > maxtime.tv_sec)
149 timevalsub(&tv2, &maxtime);
150 if (tv2.tv_sec < -1) {
151 tv->tv_sec = maxtime.tv_sec - 1;
152 printf("Time adjustment clamped to -1 second\n");
155 if (tv1.tv_sec == laststep.tv_sec) {
159 if (delta.tv_sec > 1) {
160 tv->tv_sec = tv1.tv_sec + 1;
161 printf("Time adjustment clamped to +1 second\n");
167 ts.tv_sec = tv->tv_sec;
168 ts.tv_nsec = tv->tv_usec * 1000;
171 (void) splsoftclock();
172 lease_updatetime(delta.tv_sec);
179 #ifndef _SYS_SYSPROTO_H_
180 struct clock_gettime_args {
187 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
192 error = kern_clock_gettime(td, uap->clock_id, &ats);
194 error = copyout(&ats, uap->tp, sizeof(ats));
200 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
202 struct timeval sys, user;
207 case CLOCK_REALTIME: /* Default to precise. */
208 case CLOCK_REALTIME_PRECISE:
211 case CLOCK_REALTIME_FAST:
216 calcru(p, &user, &sys);
218 TIMEVAL_TO_TIMESPEC(&user, ats);
222 calcru(p, &user, &sys);
224 timevaladd(&user, &sys);
225 TIMEVAL_TO_TIMESPEC(&user, ats);
227 case CLOCK_MONOTONIC: /* Default to precise. */
228 case CLOCK_MONOTONIC_PRECISE:
230 case CLOCK_UPTIME_PRECISE:
233 case CLOCK_UPTIME_FAST:
234 case CLOCK_MONOTONIC_FAST:
238 ats->tv_sec = time_second;
247 #ifndef _SYS_SYSPROTO_H_
248 struct clock_settime_args {
250 const struct timespec *tp;
255 clock_settime(struct thread *td, struct clock_settime_args *uap)
260 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
262 return (kern_clock_settime(td, uap->clock_id, &ats));
266 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
271 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
273 if (clock_id != CLOCK_REALTIME)
275 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
277 /* XXX Don't convert nsec->usec and back */
278 TIMESPEC_TO_TIMEVAL(&atv, ats);
279 error = settime(td, &atv);
283 #ifndef _SYS_SYSPROTO_H_
284 struct clock_getres_args {
290 clock_getres(struct thread *td, struct clock_getres_args *uap)
298 error = kern_clock_getres(td, uap->clock_id, &ts);
300 error = copyout(&ts, uap->tp, sizeof(ts));
305 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
311 case CLOCK_REALTIME_FAST:
312 case CLOCK_REALTIME_PRECISE:
313 case CLOCK_MONOTONIC:
314 case CLOCK_MONOTONIC_FAST:
315 case CLOCK_MONOTONIC_PRECISE:
317 case CLOCK_UPTIME_FAST:
318 case CLOCK_UPTIME_PRECISE:
320 * Round up the result of the division cheaply by adding 1.
321 * Rounding up is especially important if rounding down
322 * would give 0. Perfect rounding is unimportant.
324 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
328 /* Accurately round up here because we can do so cheaply. */
329 ts->tv_nsec = (1000000000 + hz - 1) / hz;
344 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
346 struct timespec ts, ts2, ts3;
350 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
352 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
355 timespecadd(&ts, rqt);
356 TIMESPEC_TO_TIMEVAL(&tv, rqt);
358 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
361 if (error != EWOULDBLOCK) {
362 if (error == ERESTART)
365 timespecsub(&ts, &ts2);
372 if (timespeccmp(&ts2, &ts, >=))
375 timespecsub(&ts3, &ts2);
376 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
380 #ifndef _SYS_SYSPROTO_H_
381 struct nanosleep_args {
382 struct timespec *rqtp;
383 struct timespec *rmtp;
388 nanosleep(struct thread *td, struct nanosleep_args *uap)
390 struct timespec rmt, rqt;
393 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
398 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
400 error = kern_nanosleep(td, &rqt, &rmt);
401 if (error && uap->rmtp) {
404 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
411 #ifndef _SYS_SYSPROTO_H_
412 struct gettimeofday_args {
414 struct timezone *tzp;
419 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
427 error = copyout(&atv, uap->tp, sizeof (atv));
429 if (error == 0 && uap->tzp != NULL) {
430 rtz.tz_minuteswest = tz_minuteswest;
431 rtz.tz_dsttime = tz_dsttime;
432 error = copyout(&rtz, uap->tzp, sizeof (rtz));
437 #ifndef _SYS_SYSPROTO_H_
438 struct settimeofday_args {
440 struct timezone *tzp;
445 settimeofday(struct thread *td, struct settimeofday_args *uap)
447 struct timeval atv, *tvp;
448 struct timezone atz, *tzp;
452 error = copyin(uap->tv, &atv, sizeof(atv));
459 error = copyin(uap->tzp, &atz, sizeof(atz));
465 return (kern_settimeofday(td, tvp, tzp));
469 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
473 error = priv_check(td, PRIV_SETTIMEOFDAY);
476 /* Verify all parameters before changing time. */
478 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
480 error = settime(td, tv);
482 if (tzp && error == 0) {
483 tz_minuteswest = tzp->tz_minuteswest;
484 tz_dsttime = tzp->tz_dsttime;
490 * Get value of an interval timer. The process virtual and profiling virtual
491 * time timers are kept in the p_stats area, since they can be swapped out.
492 * These are kept internally in the way they are specified externally: in
493 * time until they expire.
495 * The real time interval timer is kept in the process table slot for the
496 * process, and its value (it_value) is kept as an absolute time rather than
497 * as a delta, so that it is easy to keep periodic real-time signals from
500 * Virtual time timers are processed in the hardclock() routine of
501 * kern_clock.c. The real time timer is processed by a timeout routine,
502 * called from the softclock() routine. Since a callout may be delayed in
503 * real time due to interrupt processing in the system, it is possible for
504 * the real time timeout routine (realitexpire, given below), to be delayed
505 * in real time past when it is supposed to occur. It does not suffice,
506 * therefore, to reload the real timer .it_value from the real time timers
507 * .it_interval. Rather, we compute the next time in absolute time the timer
510 #ifndef _SYS_SYSPROTO_H_
511 struct getitimer_args {
513 struct itimerval *itv;
517 getitimer(struct thread *td, struct getitimer_args *uap)
519 struct itimerval aitv;
522 error = kern_getitimer(td, uap->which, &aitv);
525 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
529 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
531 struct proc *p = td->td_proc;
534 if (which > ITIMER_PROF)
537 if (which == ITIMER_REAL) {
539 * Convert from absolute to relative time in .it_value
540 * part of real time timer. If time for real time timer
541 * has passed return 0, else return difference between
542 * current time and time for the timer to go off.
545 *aitv = p->p_realtimer;
547 if (timevalisset(&aitv->it_value)) {
548 getmicrouptime(&ctv);
549 if (timevalcmp(&aitv->it_value, &ctv, <))
550 timevalclear(&aitv->it_value);
552 timevalsub(&aitv->it_value, &ctv);
556 *aitv = p->p_stats->p_timer[which];
562 #ifndef _SYS_SYSPROTO_H_
563 struct setitimer_args {
565 struct itimerval *itv, *oitv;
569 setitimer(struct thread *td, struct setitimer_args *uap)
571 struct itimerval aitv, oitv;
574 if (uap->itv == NULL) {
575 uap->itv = uap->oitv;
576 return (getitimer(td, (struct getitimer_args *)uap));
579 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
581 error = kern_setitimer(td, uap->which, &aitv, &oitv);
582 if (error != 0 || uap->oitv == NULL)
584 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
588 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
589 struct itimerval *oitv)
591 struct proc *p = td->td_proc;
595 return (kern_getitimer(td, which, oitv));
597 if (which > ITIMER_PROF)
599 if (itimerfix(&aitv->it_value))
601 if (!timevalisset(&aitv->it_value))
602 timevalclear(&aitv->it_interval);
603 else if (itimerfix(&aitv->it_interval))
606 if (which == ITIMER_REAL) {
608 if (timevalisset(&p->p_realtimer.it_value))
609 callout_stop(&p->p_itcallout);
610 getmicrouptime(&ctv);
611 if (timevalisset(&aitv->it_value)) {
612 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
614 timevaladd(&aitv->it_value, &ctv);
616 *oitv = p->p_realtimer;
617 p->p_realtimer = *aitv;
619 if (timevalisset(&oitv->it_value)) {
620 if (timevalcmp(&oitv->it_value, &ctv, <))
621 timevalclear(&oitv->it_value);
623 timevalsub(&oitv->it_value, &ctv);
627 *oitv = p->p_stats->p_timer[which];
628 p->p_stats->p_timer[which] = *aitv;
635 * Real interval timer expired:
636 * send process whose timer expired an alarm signal.
637 * If time is not set up to reload, then just return.
638 * Else compute next time timer should go off which is > current time.
639 * This is where delay in processing this timeout causes multiple
640 * SIGALRM calls to be compressed into one.
641 * tvtohz() always adds 1 to allow for the time until the next clock
642 * interrupt being strictly less than 1 clock tick, but we don't want
643 * that here since we want to appear to be in sync with the clock
644 * interrupt even when we're delayed.
647 realitexpire(void *arg)
650 struct timeval ctv, ntv;
652 p = (struct proc *)arg;
655 if (!timevalisset(&p->p_realtimer.it_interval)) {
656 timevalclear(&p->p_realtimer.it_value);
657 if (p->p_flag & P_WEXIT)
658 wakeup(&p->p_itcallout);
663 timevaladd(&p->p_realtimer.it_value,
664 &p->p_realtimer.it_interval);
665 getmicrouptime(&ctv);
666 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
667 ntv = p->p_realtimer.it_value;
668 timevalsub(&ntv, &ctv);
669 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
679 * Check that a proposed value to load into the .it_value or
680 * .it_interval part of an interval timer is acceptable, and
681 * fix it to have at least minimal value (i.e. if it is less
682 * than the resolution of the clock, round it up.)
685 itimerfix(struct timeval *tv)
688 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
690 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
696 * Decrement an interval timer by a specified number
697 * of microseconds, which must be less than a second,
698 * i.e. < 1000000. If the timer expires, then reload
699 * it. In this case, carry over (usec - old value) to
700 * reduce the value reloaded into the timer so that
701 * the timer does not drift. This routine assumes
702 * that it is called in a context where the timers
703 * on which it is operating cannot change in value.
706 itimerdecr(struct itimerval *itp, int usec)
709 if (itp->it_value.tv_usec < usec) {
710 if (itp->it_value.tv_sec == 0) {
711 /* expired, and already in next interval */
712 usec -= itp->it_value.tv_usec;
715 itp->it_value.tv_usec += 1000000;
716 itp->it_value.tv_sec--;
718 itp->it_value.tv_usec -= usec;
720 if (timevalisset(&itp->it_value))
722 /* expired, exactly at end of interval */
724 if (timevalisset(&itp->it_interval)) {
725 itp->it_value = itp->it_interval;
726 itp->it_value.tv_usec -= usec;
727 if (itp->it_value.tv_usec < 0) {
728 itp->it_value.tv_usec += 1000000;
729 itp->it_value.tv_sec--;
732 itp->it_value.tv_usec = 0; /* sec is already 0 */
737 * Add and subtract routines for timevals.
738 * N.B.: subtract routine doesn't deal with
739 * results which are before the beginning,
740 * it just gets very confused in this case.
744 timevaladd(struct timeval *t1, const struct timeval *t2)
747 t1->tv_sec += t2->tv_sec;
748 t1->tv_usec += t2->tv_usec;
753 timevalsub(struct timeval *t1, const struct timeval *t2)
756 t1->tv_sec -= t2->tv_sec;
757 t1->tv_usec -= t2->tv_usec;
762 timevalfix(struct timeval *t1)
765 if (t1->tv_usec < 0) {
767 t1->tv_usec += 1000000;
769 if (t1->tv_usec >= 1000000) {
771 t1->tv_usec -= 1000000;
776 * ratecheck(): simple time-based rate-limit checking.
779 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
781 struct timeval tv, delta;
784 getmicrouptime(&tv); /* NB: 10ms precision */
786 timevalsub(&delta, lasttime);
789 * check for 0,0 is so that the message will be seen at least once,
790 * even if interval is huge.
792 if (timevalcmp(&delta, mininterval, >=) ||
793 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
802 * ppsratecheck(): packets (or events) per second limitation.
804 * Return 0 if the limit is to be enforced (e.g. the caller
805 * should drop a packet because of the rate limitation).
807 * maxpps of 0 always causes zero to be returned. maxpps of -1
808 * always causes 1 to be returned; this effectively defeats rate
811 * Note that we maintain the struct timeval for compatibility
812 * with other bsd systems. We reuse the storage and just monitor
813 * clock ticks for minimal overhead.
816 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
821 * Reset the last time and counter if this is the first call
822 * or more than a second has passed since the last update of
826 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
827 lasttime->tv_sec = now;
829 return (maxpps != 0);
831 (*curpps)++; /* NB: ignore potential overflow */
832 return (maxpps < 0 || *curpps < maxpps);
839 struct kclock rt_clock = {
840 .timer_create = realtimer_create,
841 .timer_delete = realtimer_delete,
842 .timer_settime = realtimer_settime,
843 .timer_gettime = realtimer_gettime,
847 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
848 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
849 register_posix_clock(CLOCK_REALTIME, &rt_clock);
850 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
851 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
852 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
853 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
854 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
855 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
856 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
857 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
861 register_posix_clock(int clockid, struct kclock *clk)
863 if ((unsigned)clockid >= MAX_CLOCKS) {
864 printf("%s: invalid clockid\n", __func__);
867 posix_clocks[clockid] = *clk;
872 itimer_init(void *mem, int size, int flags)
876 it = (struct itimer *)mem;
877 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
882 itimer_fini(void *mem, int size)
886 it = (struct itimer *)mem;
887 mtx_destroy(&it->it_mtx);
891 itimer_enter(struct itimer *it)
894 mtx_assert(&it->it_mtx, MA_OWNED);
899 itimer_leave(struct itimer *it)
902 mtx_assert(&it->it_mtx, MA_OWNED);
903 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
905 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
909 #ifndef _SYS_SYSPROTO_H_
910 struct ktimer_create_args {
912 struct sigevent * evp;
917 ktimer_create(struct thread *td, struct ktimer_create_args *uap)
919 struct sigevent *evp1, ev;
923 if (uap->evp != NULL) {
924 error = copyin(uap->evp, &ev, sizeof(ev));
931 error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
934 error = copyout(&id, uap->timerid, sizeof(int));
936 kern_timer_delete(td, id);
942 kern_timer_create(struct thread *td, clockid_t clock_id,
943 struct sigevent *evp, int *timerid, int preset_id)
945 struct proc *p = td->td_proc;
950 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
953 if (posix_clocks[clock_id].timer_create == NULL)
957 if (evp->sigev_notify != SIGEV_NONE &&
958 evp->sigev_notify != SIGEV_SIGNAL &&
959 evp->sigev_notify != SIGEV_THREAD_ID)
961 if ((evp->sigev_notify == SIGEV_SIGNAL ||
962 evp->sigev_notify == SIGEV_THREAD_ID) &&
963 !_SIG_VALID(evp->sigev_signo))
967 if (p->p_itimers == NULL)
970 it = uma_zalloc(itimer_zone, M_WAITOK);
974 timespecclear(&it->it_time.it_value);
975 timespecclear(&it->it_time.it_interval);
977 it->it_overrun_last = 0;
978 it->it_clockid = clock_id;
981 ksiginfo_init(&it->it_ksi);
982 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
983 error = CLOCK_CALL(clock_id, timer_create, (it));
988 if (preset_id != -1) {
989 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
991 if (p->p_itimers->its_timers[id] != NULL) {
998 * Find a free timer slot, skipping those reserved
1001 for (id = 3; id < TIMER_MAX; id++)
1002 if (p->p_itimers->its_timers[id] == NULL)
1004 if (id == TIMER_MAX) {
1010 it->it_timerid = id;
1011 p->p_itimers->its_timers[id] = it;
1013 it->it_sigev = *evp;
1015 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1018 case CLOCK_REALTIME:
1019 it->it_sigev.sigev_signo = SIGALRM;
1022 it->it_sigev.sigev_signo = SIGVTALRM;
1025 it->it_sigev.sigev_signo = SIGPROF;
1028 it->it_sigev.sigev_value.sival_int = id;
1031 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1032 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1033 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1034 it->it_ksi.ksi_code = SI_TIMER;
1035 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1036 it->it_ksi.ksi_timerid = id;
1044 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1046 uma_zfree(itimer_zone, it);
1050 #ifndef _SYS_SYSPROTO_H_
1051 struct ktimer_delete_args {
1056 ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1058 return (kern_timer_delete(td, uap->timerid));
1061 static struct itimer *
1062 itimer_find(struct proc *p, int timerid)
1066 PROC_LOCK_ASSERT(p, MA_OWNED);
1067 if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
1068 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1072 if ((it->it_flags & ITF_DELETING) != 0) {
1080 kern_timer_delete(struct thread *td, int timerid)
1082 struct proc *p = td->td_proc;
1086 it = itimer_find(p, timerid);
1093 it->it_flags |= ITF_DELETING;
1094 while (it->it_usecount > 0) {
1095 it->it_flags |= ITF_WANTED;
1096 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1098 it->it_flags &= ~ITF_WANTED;
1099 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1103 if (KSI_ONQ(&it->it_ksi))
1104 sigqueue_take(&it->it_ksi);
1105 p->p_itimers->its_timers[timerid] = NULL;
1107 uma_zfree(itimer_zone, it);
1111 #ifndef _SYS_SYSPROTO_H_
1112 struct ktimer_settime_args {
1115 const struct itimerspec * value;
1116 struct itimerspec * ovalue;
1120 ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1122 struct proc *p = td->td_proc;
1124 struct itimerspec val, oval, *ovalp;
1127 error = copyin(uap->value, &val, sizeof(val));
1131 if (uap->ovalue != NULL)
1137 if (uap->timerid < 3 ||
1138 (it = itimer_find(p, uap->timerid)) == NULL) {
1144 error = CLOCK_CALL(it->it_clockid, timer_settime,
1145 (it, uap->flags, &val, ovalp));
1149 if (error == 0 && uap->ovalue != NULL)
1150 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1154 #ifndef _SYS_SYSPROTO_H_
1155 struct ktimer_gettime_args {
1157 struct itimerspec * value;
1161 ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1163 struct proc *p = td->td_proc;
1165 struct itimerspec val;
1169 if (uap->timerid < 3 ||
1170 (it = itimer_find(p, uap->timerid)) == NULL) {
1176 error = CLOCK_CALL(it->it_clockid, timer_gettime,
1182 error = copyout(&val, uap->value, sizeof(val));
1186 #ifndef _SYS_SYSPROTO_H_
1187 struct timer_getoverrun_args {
1192 ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1194 struct proc *p = td->td_proc;
1199 if (uap->timerid < 3 ||
1200 (it = itimer_find(p, uap->timerid)) == NULL) {
1204 td->td_retval[0] = it->it_overrun_last;
1213 realtimer_create(struct itimer *it)
1215 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1220 realtimer_delete(struct itimer *it)
1222 mtx_assert(&it->it_mtx, MA_OWNED);
1225 callout_drain(&it->it_callout);
1231 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1233 struct timespec cts;
1235 mtx_assert(&it->it_mtx, MA_OWNED);
1237 realtimer_clocktime(it->it_clockid, &cts);
1238 *ovalue = it->it_time;
1239 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1240 timespecsub(&ovalue->it_value, &cts);
1241 if (ovalue->it_value.tv_sec < 0 ||
1242 (ovalue->it_value.tv_sec == 0 &&
1243 ovalue->it_value.tv_nsec == 0)) {
1244 ovalue->it_value.tv_sec = 0;
1245 ovalue->it_value.tv_nsec = 1;
1252 realtimer_settime(struct itimer *it, int flags,
1253 struct itimerspec *value, struct itimerspec *ovalue)
1255 struct timespec cts, ts;
1257 struct itimerspec val;
1259 mtx_assert(&it->it_mtx, MA_OWNED);
1262 if (itimespecfix(&val.it_value))
1265 if (timespecisset(&val.it_value)) {
1266 if (itimespecfix(&val.it_interval))
1269 timespecclear(&val.it_interval);
1273 realtimer_gettime(it, ovalue);
1276 if (timespecisset(&val.it_value)) {
1277 realtimer_clocktime(it->it_clockid, &cts);
1279 if ((flags & TIMER_ABSTIME) == 0) {
1280 /* Convert to absolute time. */
1281 timespecadd(&it->it_time.it_value, &cts);
1283 timespecsub(&ts, &cts);
1285 * We don't care if ts is negative, tztohz will
1289 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1290 callout_reset(&it->it_callout, tvtohz(&tv),
1291 realtimer_expire, it);
1293 callout_stop(&it->it_callout);
1300 realtimer_clocktime(clockid_t id, struct timespec *ts)
1302 if (id == CLOCK_REALTIME)
1304 else /* CLOCK_MONOTONIC */
1309 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1313 PROC_LOCK_ASSERT(p, MA_OWNED);
1314 it = itimer_find(p, timerid);
1316 ksi->ksi_overrun = it->it_overrun;
1317 it->it_overrun_last = it->it_overrun;
1326 itimespecfix(struct timespec *ts)
1329 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1331 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1332 ts->tv_nsec = tick * 1000;
1336 /* Timeout callback for realtime timer */
1338 realtimer_expire(void *arg)
1340 struct timespec cts, ts;
1345 it = (struct itimer *)arg;
1348 realtimer_clocktime(it->it_clockid, &cts);
1349 /* Only fire if time is reached. */
1350 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1351 if (timespecisset(&it->it_time.it_interval)) {
1352 timespecadd(&it->it_time.it_value,
1353 &it->it_time.it_interval);
1354 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1355 if (it->it_overrun < INT_MAX)
1358 it->it_ksi.ksi_errno = ERANGE;
1359 timespecadd(&it->it_time.it_value,
1360 &it->it_time.it_interval);
1363 /* single shot timer ? */
1364 timespecclear(&it->it_time.it_value);
1366 if (timespecisset(&it->it_time.it_value)) {
1367 ts = it->it_time.it_value;
1368 timespecsub(&ts, &cts);
1369 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1370 callout_reset(&it->it_callout, tvtohz(&tv),
1371 realtimer_expire, it);
1376 } else if (timespecisset(&it->it_time.it_value)) {
1377 ts = it->it_time.it_value;
1378 timespecsub(&ts, &cts);
1379 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1380 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1386 itimer_fire(struct itimer *it)
1388 struct proc *p = it->it_proc;
1391 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1392 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1394 if (!KSI_ONQ(&it->it_ksi)) {
1395 it->it_ksi.ksi_errno = 0;
1396 ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
1397 if (__predict_false(ret != 0)) {
1400 * Broken userland code, thread went
1401 * away, disarm the timer.
1405 timespecclear(&it->it_time.it_value);
1406 timespecclear(&it->it_time.it_interval);
1407 callout_stop(&it->it_callout);
1412 if (it->it_overrun < INT_MAX)
1415 it->it_ksi.ksi_errno = ERANGE;
1422 itimers_alloc(struct proc *p)
1424 struct itimers *its;
1427 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1428 LIST_INIT(&its->its_virtual);
1429 LIST_INIT(&its->its_prof);
1430 TAILQ_INIT(&its->its_worklist);
1431 for (i = 0; i < TIMER_MAX; i++)
1432 its->its_timers[i] = NULL;
1434 if (p->p_itimers == NULL) {
1440 free(its, M_SUBPROC);
1445 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1447 itimers_event_hook_exit(arg, p);
1450 /* Clean up timers when some process events are being triggered. */
1452 itimers_event_hook_exit(void *arg, struct proc *p)
1454 struct itimers *its;
1456 int event = (int)(intptr_t)arg;
1459 if (p->p_itimers != NULL) {
1461 for (i = 0; i < MAX_CLOCKS; ++i) {
1462 if (posix_clocks[i].event_hook != NULL)
1463 CLOCK_CALL(i, event_hook, (p, i, event));
1466 * According to susv3, XSI interval timers should be inherited
1469 if (event == ITIMER_EV_EXEC)
1471 else if (event == ITIMER_EV_EXIT)
1474 panic("unhandled event");
1475 for (; i < TIMER_MAX; ++i) {
1476 if ((it = its->its_timers[i]) != NULL)
1477 kern_timer_delete(curthread, i);
1479 if (its->its_timers[0] == NULL &&
1480 its->its_timers[1] == NULL &&
1481 its->its_timers[2] == NULL) {
1482 free(its, M_SUBPROC);
1483 p->p_itimers = NULL;