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:
217 calcru(p, &user, &sys);
220 TIMEVAL_TO_TIMESPEC(&user, ats);
225 calcru(p, &user, &sys);
228 timevaladd(&user, &sys);
229 TIMEVAL_TO_TIMESPEC(&user, ats);
231 case CLOCK_MONOTONIC: /* Default to precise. */
232 case CLOCK_MONOTONIC_PRECISE:
234 case CLOCK_UPTIME_PRECISE:
237 case CLOCK_UPTIME_FAST:
238 case CLOCK_MONOTONIC_FAST:
242 ats->tv_sec = time_second;
251 #ifndef _SYS_SYSPROTO_H_
252 struct clock_settime_args {
254 const struct timespec *tp;
259 clock_settime(struct thread *td, struct clock_settime_args *uap)
264 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
266 return (kern_clock_settime(td, uap->clock_id, &ats));
270 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
275 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
277 if (clock_id != CLOCK_REALTIME)
279 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
281 /* XXX Don't convert nsec->usec and back */
282 TIMESPEC_TO_TIMEVAL(&atv, ats);
283 error = settime(td, &atv);
287 #ifndef _SYS_SYSPROTO_H_
288 struct clock_getres_args {
294 clock_getres(struct thread *td, struct clock_getres_args *uap)
302 error = kern_clock_getres(td, uap->clock_id, &ts);
304 error = copyout(&ts, uap->tp, sizeof(ts));
309 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
315 case CLOCK_REALTIME_FAST:
316 case CLOCK_REALTIME_PRECISE:
317 case CLOCK_MONOTONIC:
318 case CLOCK_MONOTONIC_FAST:
319 case CLOCK_MONOTONIC_PRECISE:
321 case CLOCK_UPTIME_FAST:
322 case CLOCK_UPTIME_PRECISE:
324 * Round up the result of the division cheaply by adding 1.
325 * Rounding up is especially important if rounding down
326 * would give 0. Perfect rounding is unimportant.
328 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
332 /* Accurately round up here because we can do so cheaply. */
333 ts->tv_nsec = (1000000000 + hz - 1) / hz;
348 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
350 struct timespec ts, ts2, ts3;
354 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
356 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
359 timespecadd(&ts, rqt);
360 TIMESPEC_TO_TIMEVAL(&tv, rqt);
362 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
365 if (error != EWOULDBLOCK) {
366 if (error == ERESTART)
369 timespecsub(&ts, &ts2);
376 if (timespeccmp(&ts2, &ts, >=))
379 timespecsub(&ts3, &ts2);
380 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
384 #ifndef _SYS_SYSPROTO_H_
385 struct nanosleep_args {
386 struct timespec *rqtp;
387 struct timespec *rmtp;
392 nanosleep(struct thread *td, struct nanosleep_args *uap)
394 struct timespec rmt, rqt;
397 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
402 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
404 error = kern_nanosleep(td, &rqt, &rmt);
405 if (error && uap->rmtp) {
408 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
415 #ifndef _SYS_SYSPROTO_H_
416 struct gettimeofday_args {
418 struct timezone *tzp;
423 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
431 error = copyout(&atv, uap->tp, sizeof (atv));
433 if (error == 0 && uap->tzp != NULL) {
434 rtz.tz_minuteswest = tz_minuteswest;
435 rtz.tz_dsttime = tz_dsttime;
436 error = copyout(&rtz, uap->tzp, sizeof (rtz));
441 #ifndef _SYS_SYSPROTO_H_
442 struct settimeofday_args {
444 struct timezone *tzp;
449 settimeofday(struct thread *td, struct settimeofday_args *uap)
451 struct timeval atv, *tvp;
452 struct timezone atz, *tzp;
456 error = copyin(uap->tv, &atv, sizeof(atv));
463 error = copyin(uap->tzp, &atz, sizeof(atz));
469 return (kern_settimeofday(td, tvp, tzp));
473 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
477 error = priv_check(td, PRIV_SETTIMEOFDAY);
480 /* Verify all parameters before changing time. */
482 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
484 error = settime(td, tv);
486 if (tzp && error == 0) {
487 tz_minuteswest = tzp->tz_minuteswest;
488 tz_dsttime = tzp->tz_dsttime;
494 * Get value of an interval timer. The process virtual and profiling virtual
495 * time timers are kept in the p_stats area, since they can be swapped out.
496 * These are kept internally in the way they are specified externally: in
497 * time until they expire.
499 * The real time interval timer is kept in the process table slot for the
500 * process, and its value (it_value) is kept as an absolute time rather than
501 * as a delta, so that it is easy to keep periodic real-time signals from
504 * Virtual time timers are processed in the hardclock() routine of
505 * kern_clock.c. The real time timer is processed by a timeout routine,
506 * called from the softclock() routine. Since a callout may be delayed in
507 * real time due to interrupt processing in the system, it is possible for
508 * the real time timeout routine (realitexpire, given below), to be delayed
509 * in real time past when it is supposed to occur. It does not suffice,
510 * therefore, to reload the real timer .it_value from the real time timers
511 * .it_interval. Rather, we compute the next time in absolute time the timer
514 #ifndef _SYS_SYSPROTO_H_
515 struct getitimer_args {
517 struct itimerval *itv;
521 getitimer(struct thread *td, struct getitimer_args *uap)
523 struct itimerval aitv;
526 error = kern_getitimer(td, uap->which, &aitv);
529 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
533 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
535 struct proc *p = td->td_proc;
538 if (which > ITIMER_PROF)
541 if (which == ITIMER_REAL) {
543 * Convert from absolute to relative time in .it_value
544 * part of real time timer. If time for real time timer
545 * has passed return 0, else return difference between
546 * current time and time for the timer to go off.
549 *aitv = p->p_realtimer;
551 if (timevalisset(&aitv->it_value)) {
552 getmicrouptime(&ctv);
553 if (timevalcmp(&aitv->it_value, &ctv, <))
554 timevalclear(&aitv->it_value);
556 timevalsub(&aitv->it_value, &ctv);
560 *aitv = p->p_stats->p_timer[which];
566 #ifndef _SYS_SYSPROTO_H_
567 struct setitimer_args {
569 struct itimerval *itv, *oitv;
573 setitimer(struct thread *td, struct setitimer_args *uap)
575 struct itimerval aitv, oitv;
578 if (uap->itv == NULL) {
579 uap->itv = uap->oitv;
580 return (getitimer(td, (struct getitimer_args *)uap));
583 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
585 error = kern_setitimer(td, uap->which, &aitv, &oitv);
586 if (error != 0 || uap->oitv == NULL)
588 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
592 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
593 struct itimerval *oitv)
595 struct proc *p = td->td_proc;
599 return (kern_getitimer(td, which, oitv));
601 if (which > ITIMER_PROF)
603 if (itimerfix(&aitv->it_value))
605 if (!timevalisset(&aitv->it_value))
606 timevalclear(&aitv->it_interval);
607 else if (itimerfix(&aitv->it_interval))
610 if (which == ITIMER_REAL) {
612 if (timevalisset(&p->p_realtimer.it_value))
613 callout_stop(&p->p_itcallout);
614 getmicrouptime(&ctv);
615 if (timevalisset(&aitv->it_value)) {
616 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
618 timevaladd(&aitv->it_value, &ctv);
620 *oitv = p->p_realtimer;
621 p->p_realtimer = *aitv;
623 if (timevalisset(&oitv->it_value)) {
624 if (timevalcmp(&oitv->it_value, &ctv, <))
625 timevalclear(&oitv->it_value);
627 timevalsub(&oitv->it_value, &ctv);
631 *oitv = p->p_stats->p_timer[which];
632 p->p_stats->p_timer[which] = *aitv;
639 * Real interval timer expired:
640 * send process whose timer expired an alarm signal.
641 * If time is not set up to reload, then just return.
642 * Else compute next time timer should go off which is > current time.
643 * This is where delay in processing this timeout causes multiple
644 * SIGALRM calls to be compressed into one.
645 * tvtohz() always adds 1 to allow for the time until the next clock
646 * interrupt being strictly less than 1 clock tick, but we don't want
647 * that here since we want to appear to be in sync with the clock
648 * interrupt even when we're delayed.
651 realitexpire(void *arg)
654 struct timeval ctv, ntv;
656 p = (struct proc *)arg;
659 if (!timevalisset(&p->p_realtimer.it_interval)) {
660 timevalclear(&p->p_realtimer.it_value);
661 if (p->p_flag & P_WEXIT)
662 wakeup(&p->p_itcallout);
667 timevaladd(&p->p_realtimer.it_value,
668 &p->p_realtimer.it_interval);
669 getmicrouptime(&ctv);
670 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
671 ntv = p->p_realtimer.it_value;
672 timevalsub(&ntv, &ctv);
673 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
683 * Check that a proposed value to load into the .it_value or
684 * .it_interval part of an interval timer is acceptable, and
685 * fix it to have at least minimal value (i.e. if it is less
686 * than the resolution of the clock, round it up.)
689 itimerfix(struct timeval *tv)
692 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
694 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
700 * Decrement an interval timer by a specified number
701 * of microseconds, which must be less than a second,
702 * i.e. < 1000000. If the timer expires, then reload
703 * it. In this case, carry over (usec - old value) to
704 * reduce the value reloaded into the timer so that
705 * the timer does not drift. This routine assumes
706 * that it is called in a context where the timers
707 * on which it is operating cannot change in value.
710 itimerdecr(struct itimerval *itp, int usec)
713 if (itp->it_value.tv_usec < usec) {
714 if (itp->it_value.tv_sec == 0) {
715 /* expired, and already in next interval */
716 usec -= itp->it_value.tv_usec;
719 itp->it_value.tv_usec += 1000000;
720 itp->it_value.tv_sec--;
722 itp->it_value.tv_usec -= usec;
724 if (timevalisset(&itp->it_value))
726 /* expired, exactly at end of interval */
728 if (timevalisset(&itp->it_interval)) {
729 itp->it_value = itp->it_interval;
730 itp->it_value.tv_usec -= usec;
731 if (itp->it_value.tv_usec < 0) {
732 itp->it_value.tv_usec += 1000000;
733 itp->it_value.tv_sec--;
736 itp->it_value.tv_usec = 0; /* sec is already 0 */
741 * Add and subtract routines for timevals.
742 * N.B.: subtract routine doesn't deal with
743 * results which are before the beginning,
744 * it just gets very confused in this case.
748 timevaladd(struct timeval *t1, const struct timeval *t2)
751 t1->tv_sec += t2->tv_sec;
752 t1->tv_usec += t2->tv_usec;
757 timevalsub(struct timeval *t1, const struct timeval *t2)
760 t1->tv_sec -= t2->tv_sec;
761 t1->tv_usec -= t2->tv_usec;
766 timevalfix(struct timeval *t1)
769 if (t1->tv_usec < 0) {
771 t1->tv_usec += 1000000;
773 if (t1->tv_usec >= 1000000) {
775 t1->tv_usec -= 1000000;
780 * ratecheck(): simple time-based rate-limit checking.
783 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
785 struct timeval tv, delta;
788 getmicrouptime(&tv); /* NB: 10ms precision */
790 timevalsub(&delta, lasttime);
793 * check for 0,0 is so that the message will be seen at least once,
794 * even if interval is huge.
796 if (timevalcmp(&delta, mininterval, >=) ||
797 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
806 * ppsratecheck(): packets (or events) per second limitation.
808 * Return 0 if the limit is to be enforced (e.g. the caller
809 * should drop a packet because of the rate limitation).
811 * maxpps of 0 always causes zero to be returned. maxpps of -1
812 * always causes 1 to be returned; this effectively defeats rate
815 * Note that we maintain the struct timeval for compatibility
816 * with other bsd systems. We reuse the storage and just monitor
817 * clock ticks for minimal overhead.
820 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
825 * Reset the last time and counter if this is the first call
826 * or more than a second has passed since the last update of
830 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
831 lasttime->tv_sec = now;
833 return (maxpps != 0);
835 (*curpps)++; /* NB: ignore potential overflow */
836 return (maxpps < 0 || *curpps < maxpps);
843 struct kclock rt_clock = {
844 .timer_create = realtimer_create,
845 .timer_delete = realtimer_delete,
846 .timer_settime = realtimer_settime,
847 .timer_gettime = realtimer_gettime,
851 itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
852 NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
853 register_posix_clock(CLOCK_REALTIME, &rt_clock);
854 register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
855 p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
856 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
857 p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
858 EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
859 (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
860 EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
861 (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
865 register_posix_clock(int clockid, struct kclock *clk)
867 if ((unsigned)clockid >= MAX_CLOCKS) {
868 printf("%s: invalid clockid\n", __func__);
871 posix_clocks[clockid] = *clk;
876 itimer_init(void *mem, int size, int flags)
880 it = (struct itimer *)mem;
881 mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
886 itimer_fini(void *mem, int size)
890 it = (struct itimer *)mem;
891 mtx_destroy(&it->it_mtx);
895 itimer_enter(struct itimer *it)
898 mtx_assert(&it->it_mtx, MA_OWNED);
903 itimer_leave(struct itimer *it)
906 mtx_assert(&it->it_mtx, MA_OWNED);
907 KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
909 if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
913 #ifndef _SYS_SYSPROTO_H_
914 struct ktimer_create_args {
916 struct sigevent * evp;
921 ktimer_create(struct thread *td, struct ktimer_create_args *uap)
923 struct sigevent *evp1, ev;
927 if (uap->evp != NULL) {
928 error = copyin(uap->evp, &ev, sizeof(ev));
935 error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
938 error = copyout(&id, uap->timerid, sizeof(int));
940 kern_timer_delete(td, id);
946 kern_timer_create(struct thread *td, clockid_t clock_id,
947 struct sigevent *evp, int *timerid, int preset_id)
949 struct proc *p = td->td_proc;
954 if (clock_id < 0 || clock_id >= MAX_CLOCKS)
957 if (posix_clocks[clock_id].timer_create == NULL)
961 if (evp->sigev_notify != SIGEV_NONE &&
962 evp->sigev_notify != SIGEV_SIGNAL &&
963 evp->sigev_notify != SIGEV_THREAD_ID)
965 if ((evp->sigev_notify == SIGEV_SIGNAL ||
966 evp->sigev_notify == SIGEV_THREAD_ID) &&
967 !_SIG_VALID(evp->sigev_signo))
971 if (p->p_itimers == NULL)
974 it = uma_zalloc(itimer_zone, M_WAITOK);
978 timespecclear(&it->it_time.it_value);
979 timespecclear(&it->it_time.it_interval);
981 it->it_overrun_last = 0;
982 it->it_clockid = clock_id;
985 ksiginfo_init(&it->it_ksi);
986 it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
987 error = CLOCK_CALL(clock_id, timer_create, (it));
992 if (preset_id != -1) {
993 KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
995 if (p->p_itimers->its_timers[id] != NULL) {
1002 * Find a free timer slot, skipping those reserved
1005 for (id = 3; id < TIMER_MAX; id++)
1006 if (p->p_itimers->its_timers[id] == NULL)
1008 if (id == TIMER_MAX) {
1014 it->it_timerid = id;
1015 p->p_itimers->its_timers[id] = it;
1017 it->it_sigev = *evp;
1019 it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1022 case CLOCK_REALTIME:
1023 it->it_sigev.sigev_signo = SIGALRM;
1026 it->it_sigev.sigev_signo = SIGVTALRM;
1029 it->it_sigev.sigev_signo = SIGPROF;
1032 it->it_sigev.sigev_value.sival_int = id;
1035 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1036 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1037 it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1038 it->it_ksi.ksi_code = SI_TIMER;
1039 it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1040 it->it_ksi.ksi_timerid = id;
1048 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1050 uma_zfree(itimer_zone, it);
1054 #ifndef _SYS_SYSPROTO_H_
1055 struct ktimer_delete_args {
1060 ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1062 return (kern_timer_delete(td, uap->timerid));
1065 static struct itimer *
1066 itimer_find(struct proc *p, int timerid)
1070 PROC_LOCK_ASSERT(p, MA_OWNED);
1071 if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
1072 (it = p->p_itimers->its_timers[timerid]) == NULL) {
1076 if ((it->it_flags & ITF_DELETING) != 0) {
1084 kern_timer_delete(struct thread *td, int timerid)
1086 struct proc *p = td->td_proc;
1090 it = itimer_find(p, timerid);
1097 it->it_flags |= ITF_DELETING;
1098 while (it->it_usecount > 0) {
1099 it->it_flags |= ITF_WANTED;
1100 msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1102 it->it_flags &= ~ITF_WANTED;
1103 CLOCK_CALL(it->it_clockid, timer_delete, (it));
1107 if (KSI_ONQ(&it->it_ksi))
1108 sigqueue_take(&it->it_ksi);
1109 p->p_itimers->its_timers[timerid] = NULL;
1111 uma_zfree(itimer_zone, it);
1115 #ifndef _SYS_SYSPROTO_H_
1116 struct ktimer_settime_args {
1119 const struct itimerspec * value;
1120 struct itimerspec * ovalue;
1124 ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1126 struct proc *p = td->td_proc;
1128 struct itimerspec val, oval, *ovalp;
1131 error = copyin(uap->value, &val, sizeof(val));
1135 if (uap->ovalue != NULL)
1141 if (uap->timerid < 3 ||
1142 (it = itimer_find(p, uap->timerid)) == NULL) {
1148 error = CLOCK_CALL(it->it_clockid, timer_settime,
1149 (it, uap->flags, &val, ovalp));
1153 if (error == 0 && uap->ovalue != NULL)
1154 error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1158 #ifndef _SYS_SYSPROTO_H_
1159 struct ktimer_gettime_args {
1161 struct itimerspec * value;
1165 ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1167 struct proc *p = td->td_proc;
1169 struct itimerspec val;
1173 if (uap->timerid < 3 ||
1174 (it = itimer_find(p, uap->timerid)) == NULL) {
1180 error = CLOCK_CALL(it->it_clockid, timer_gettime,
1186 error = copyout(&val, uap->value, sizeof(val));
1190 #ifndef _SYS_SYSPROTO_H_
1191 struct timer_getoverrun_args {
1196 ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1198 struct proc *p = td->td_proc;
1203 if (uap->timerid < 3 ||
1204 (it = itimer_find(p, uap->timerid)) == NULL) {
1208 td->td_retval[0] = it->it_overrun_last;
1217 realtimer_create(struct itimer *it)
1219 callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1224 realtimer_delete(struct itimer *it)
1226 mtx_assert(&it->it_mtx, MA_OWNED);
1229 callout_drain(&it->it_callout);
1235 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1237 struct timespec cts;
1239 mtx_assert(&it->it_mtx, MA_OWNED);
1241 realtimer_clocktime(it->it_clockid, &cts);
1242 *ovalue = it->it_time;
1243 if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1244 timespecsub(&ovalue->it_value, &cts);
1245 if (ovalue->it_value.tv_sec < 0 ||
1246 (ovalue->it_value.tv_sec == 0 &&
1247 ovalue->it_value.tv_nsec == 0)) {
1248 ovalue->it_value.tv_sec = 0;
1249 ovalue->it_value.tv_nsec = 1;
1256 realtimer_settime(struct itimer *it, int flags,
1257 struct itimerspec *value, struct itimerspec *ovalue)
1259 struct timespec cts, ts;
1261 struct itimerspec val;
1263 mtx_assert(&it->it_mtx, MA_OWNED);
1266 if (itimespecfix(&val.it_value))
1269 if (timespecisset(&val.it_value)) {
1270 if (itimespecfix(&val.it_interval))
1273 timespecclear(&val.it_interval);
1277 realtimer_gettime(it, ovalue);
1280 if (timespecisset(&val.it_value)) {
1281 realtimer_clocktime(it->it_clockid, &cts);
1283 if ((flags & TIMER_ABSTIME) == 0) {
1284 /* Convert to absolute time. */
1285 timespecadd(&it->it_time.it_value, &cts);
1287 timespecsub(&ts, &cts);
1289 * We don't care if ts is negative, tztohz will
1293 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1294 callout_reset(&it->it_callout, tvtohz(&tv),
1295 realtimer_expire, it);
1297 callout_stop(&it->it_callout);
1304 realtimer_clocktime(clockid_t id, struct timespec *ts)
1306 if (id == CLOCK_REALTIME)
1308 else /* CLOCK_MONOTONIC */
1313 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1317 PROC_LOCK_ASSERT(p, MA_OWNED);
1318 it = itimer_find(p, timerid);
1320 ksi->ksi_overrun = it->it_overrun;
1321 it->it_overrun_last = it->it_overrun;
1330 itimespecfix(struct timespec *ts)
1333 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1335 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1336 ts->tv_nsec = tick * 1000;
1340 /* Timeout callback for realtime timer */
1342 realtimer_expire(void *arg)
1344 struct timespec cts, ts;
1349 it = (struct itimer *)arg;
1352 realtimer_clocktime(it->it_clockid, &cts);
1353 /* Only fire if time is reached. */
1354 if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1355 if (timespecisset(&it->it_time.it_interval)) {
1356 timespecadd(&it->it_time.it_value,
1357 &it->it_time.it_interval);
1358 while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1359 if (it->it_overrun < INT_MAX)
1362 it->it_ksi.ksi_errno = ERANGE;
1363 timespecadd(&it->it_time.it_value,
1364 &it->it_time.it_interval);
1367 /* single shot timer ? */
1368 timespecclear(&it->it_time.it_value);
1370 if (timespecisset(&it->it_time.it_value)) {
1371 ts = it->it_time.it_value;
1372 timespecsub(&ts, &cts);
1373 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1374 callout_reset(&it->it_callout, tvtohz(&tv),
1375 realtimer_expire, it);
1380 } else if (timespecisset(&it->it_time.it_value)) {
1381 ts = it->it_time.it_value;
1382 timespecsub(&ts, &cts);
1383 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1384 callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1390 itimer_fire(struct itimer *it)
1392 struct proc *p = it->it_proc;
1395 if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1396 it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1398 if (!KSI_ONQ(&it->it_ksi)) {
1399 it->it_ksi.ksi_errno = 0;
1400 ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
1401 if (__predict_false(ret != 0)) {
1404 * Broken userland code, thread went
1405 * away, disarm the timer.
1409 timespecclear(&it->it_time.it_value);
1410 timespecclear(&it->it_time.it_interval);
1411 callout_stop(&it->it_callout);
1416 if (it->it_overrun < INT_MAX)
1419 it->it_ksi.ksi_errno = ERANGE;
1426 itimers_alloc(struct proc *p)
1428 struct itimers *its;
1431 its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1432 LIST_INIT(&its->its_virtual);
1433 LIST_INIT(&its->its_prof);
1434 TAILQ_INIT(&its->its_worklist);
1435 for (i = 0; i < TIMER_MAX; i++)
1436 its->its_timers[i] = NULL;
1438 if (p->p_itimers == NULL) {
1444 free(its, M_SUBPROC);
1449 itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1451 itimers_event_hook_exit(arg, p);
1454 /* Clean up timers when some process events are being triggered. */
1456 itimers_event_hook_exit(void *arg, struct proc *p)
1458 struct itimers *its;
1460 int event = (int)(intptr_t)arg;
1463 if (p->p_itimers != NULL) {
1465 for (i = 0; i < MAX_CLOCKS; ++i) {
1466 if (posix_clocks[i].event_hook != NULL)
1467 CLOCK_CALL(i, event_hook, (p, i, event));
1470 * According to susv3, XSI interval timers should be inherited
1473 if (event == ITIMER_EV_EXEC)
1475 else if (event == ITIMER_EV_EXIT)
1478 panic("unhandled event");
1479 for (; i < TIMER_MAX; ++i) {
1480 if ((it = its->its_timers[i]) != NULL)
1481 kern_timer_delete(curthread, i);
1483 if (its->its_timers[0] == NULL &&
1484 its->its_timers[1] == NULL &&
1485 its->its_timers[2] == NULL) {
1486 free(its, M_SUBPROC);
1487 p->p_itimers = NULL;