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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/kern_time.c,v 1.68.2.1 2002/10/01 08:00:41 bde Exp $
35 * $DragonFly: src/sys/kern/kern_time.c,v 1.39 2007/02/03 17:05:58 corecode Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/resourcevar.h>
43 #include <sys/signalvar.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/sysunion.h>
50 #include <sys/vnode.h>
51 #include <sys/sysctl.h>
53 #include <vm/vm_extern.h>
54 #include <sys/msgport2.h>
55 #include <sys/thread2.h>
60 * Time of day and interval timer support.
62 * These routines provide the kernel entry points to get and set
63 * the time-of-day and per-process interval timers. Subroutines
64 * here provide support for adding and subtracting timeval structures
65 * and decrementing interval timers, optionally reloading the interval
66 * timers when they expire.
69 static int nanosleep1 (struct timespec *rqt,
70 struct timespec *rmt);
71 static int settime (struct timeval *);
72 static void timevalfix (struct timeval *);
74 static int sleep_hard_us = 100;
75 SYSCTL_INT(_kern, OID_AUTO, sleep_hard_us, CTLFLAG_RW, &sleep_hard_us, 0, "")
78 settime(struct timeval *tv)
80 struct timeval delta, tv1, tv2;
81 static struct timeval maxtime, laststep;
85 if ((origcpu = mycpu->gd_cpuid) != 0)
86 lwkt_setcpu_self(globaldata_find(0));
91 timevalsub(&delta, &tv1);
94 * If the system is secure, we do not allow the time to be
95 * set to a value earlier than 1 second less than the highest
96 * time we have yet seen. The worst a miscreant can do in
97 * this circumstance is "freeze" time. He couldn't go
100 * We similarly do not allow the clock to be stepped more
101 * than one second, nor more than once per second. This allows
102 * a miscreant to make the clock march double-time, but no worse.
104 if (securelevel > 1) {
105 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
107 * Update maxtime to latest time we've seen.
109 if (tv1.tv_sec > maxtime.tv_sec)
112 timevalsub(&tv2, &maxtime);
113 if (tv2.tv_sec < -1) {
114 tv->tv_sec = maxtime.tv_sec - 1;
115 kprintf("Time adjustment clamped to -1 second\n");
118 if (tv1.tv_sec == laststep.tv_sec) {
122 if (delta.tv_sec > 1) {
123 tv->tv_sec = tv1.tv_sec + 1;
124 kprintf("Time adjustment clamped to +1 second\n");
130 ts.tv_sec = tv->tv_sec;
131 ts.tv_nsec = tv->tv_usec * 1000;
136 lwkt_setcpu_self(globaldata_find(origcpu));
144 sys_clock_gettime(struct clock_gettime_args *uap)
148 switch(uap->clock_id) {
151 return (copyout(&ats, uap->tp, sizeof(ats)));
152 case CLOCK_MONOTONIC:
154 return (copyout(&ats, uap->tp, sizeof(ats)));
162 sys_clock_settime(struct clock_settime_args *uap)
164 struct thread *td = curthread;
169 if ((error = suser(td)) != 0)
171 switch(uap->clock_id) {
173 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
175 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
177 /* XXX Don't convert nsec->usec and back */
178 TIMESPEC_TO_TIMEVAL(&atv, &ats);
179 error = settime(&atv);
187 sys_clock_getres(struct clock_getres_args *uap)
191 switch(uap->clock_id) {
193 case CLOCK_MONOTONIC:
195 * Round up the result of the division cheaply
196 * by adding 1. Rounding up is especially important
197 * if rounding down would give 0. Perfect rounding
201 ts.tv_nsec = 1000000000 / sys_cputimer->freq + 1;
202 return(copyout(&ts, uap->tp, sizeof(ts)));
211 * This is a general helper function for nanosleep() (aka sleep() aka
214 * If there is less then one tick's worth of time left and
215 * we haven't done a yield, or the remaining microseconds is
216 * ridiculously low, do a yield. This avoids having
217 * to deal with systimer overheads when the system is under
218 * heavy loads. If we have done a yield already then use
219 * a systimer and an uninterruptable thread wait.
221 * If there is more then a tick's worth of time left,
222 * calculate the baseline ticks and use an interruptable
223 * tsleep, then handle the fine-grained delay on the next
224 * loop. This usually results in two sleeps occuring, a long one
228 ns1_systimer(systimer_t info)
230 lwkt_schedule(info->data);
234 nanosleep1(struct timespec *rqt, struct timespec *rmt)
237 struct timespec ts, ts2, ts3;
242 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
244 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
247 timespecadd(&ts, rqt); /* ts = target timestamp compare */
248 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
253 struct systimer info;
255 ticks = tv.tv_usec / tick; /* approximate */
257 if (tv.tv_sec == 0 && ticks == 0) {
258 thread_t td = curthread;
259 if (tried_yield || tv.tv_usec < sleep_hard_us) {
263 crit_enter_quick(td);
264 systimer_init_oneshot(&info, ns1_systimer,
266 lwkt_deschedule_self(td);
269 systimer_del(&info); /* make sure it's gone */
271 error = iscaught(td->td_lwp);
272 } else if (tv.tv_sec == 0) {
273 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
275 ticks = tvtohz_low(&tv); /* also handles overflow */
276 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
279 if (error && error != EWOULDBLOCK) {
280 if (error == ERESTART)
283 timespecsub(&ts, &ts2);
290 if (timespeccmp(&ts2, &ts, >=))
293 timespecsub(&ts3, &ts2);
294 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
300 sys_nanosleep(struct nanosleep_args *uap)
306 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
310 error = nanosleep1(&rqt, &rmt);
313 * copyout the residual if nanosleep was interrupted.
315 if (error && uap->rmtp)
316 error = copyout(&rmt, uap->rmtp, sizeof(rmt));
322 sys_gettimeofday(struct gettimeofday_args *uap)
329 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
334 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
341 sys_settimeofday(struct settimeofday_args *uap)
343 struct thread *td = curthread;
348 if ((error = suser(td)))
350 /* Verify all parameters before changing time. */
352 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
355 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
359 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
361 if (uap->tv && (error = settime(&atv)))
369 kern_adjtime_common(void)
371 if ((ntp_delta >= 0 && ntp_delta < ntp_default_tick_delta) ||
372 (ntp_delta < 0 && ntp_delta > -ntp_default_tick_delta))
373 ntp_tick_delta = ntp_delta;
374 else if (ntp_delta > ntp_big_delta)
375 ntp_tick_delta = 10 * ntp_default_tick_delta;
376 else if (ntp_delta < -ntp_big_delta)
377 ntp_tick_delta = -10 * ntp_default_tick_delta;
378 else if (ntp_delta > 0)
379 ntp_tick_delta = ntp_default_tick_delta;
381 ntp_tick_delta = -ntp_default_tick_delta;
385 kern_adjtime(int64_t delta, int64_t *odelta)
389 if ((origcpu = mycpu->gd_cpuid) != 0)
390 lwkt_setcpu_self(globaldata_find(0));
395 kern_adjtime_common();
399 lwkt_setcpu_self(globaldata_find(origcpu));
403 kern_get_ntp_delta(int64_t *delta)
407 if ((origcpu = mycpu->gd_cpuid) != 0)
408 lwkt_setcpu_self(globaldata_find(0));
415 lwkt_setcpu_self(globaldata_find(origcpu));
419 kern_reladjtime(int64_t delta)
423 if ((origcpu = mycpu->gd_cpuid) != 0)
424 lwkt_setcpu_self(globaldata_find(0));
428 kern_adjtime_common();
432 lwkt_setcpu_self(globaldata_find(origcpu));
436 kern_adjfreq(int64_t rate)
440 if ((origcpu = mycpu->gd_cpuid) != 0)
441 lwkt_setcpu_self(globaldata_find(0));
444 ntp_tick_permanent = rate;
448 lwkt_setcpu_self(globaldata_find(origcpu));
453 sys_adjtime(struct adjtime_args *uap)
455 struct thread *td = curthread;
457 int64_t ndelta, odelta;
460 if ((error = suser(td)))
463 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
467 * Compute the total correction and the rate at which to apply it.
468 * Round the adjustment down to a whole multiple of the per-tick
469 * delta, so that after some number of incremental changes in
470 * hardclock(), tickdelta will become zero, lest the correction
471 * overshoot and start taking us away from the desired final time.
473 ndelta = (int64_t)atv.tv_sec * 1000000000 + atv.tv_usec * 1000;
474 kern_adjtime(ndelta, &odelta);
477 atv.tv_sec = odelta / 1000000000;
478 atv.tv_usec = odelta % 1000000000 / 1000;
479 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
480 sizeof(struct timeval));
486 sysctl_adjtime(SYSCTL_HANDLER_ARGS)
491 if (req->newptr != NULL) {
492 if (suser(curthread))
494 error = SYSCTL_IN(req, &delta, sizeof(delta));
497 kern_reladjtime(delta);
501 kern_get_ntp_delta(&delta);
502 error = SYSCTL_OUT(req, &delta, sizeof(delta));
507 * delta is in nanoseconds.
510 sysctl_delta(SYSCTL_HANDLER_ARGS)
512 int64_t delta, old_delta;
515 if (req->newptr != NULL) {
516 if (suser(curthread))
518 error = SYSCTL_IN(req, &delta, sizeof(delta));
521 kern_adjtime(delta, &old_delta);
524 if (req->oldptr != NULL)
525 kern_get_ntp_delta(&old_delta);
526 error = SYSCTL_OUT(req, &old_delta, sizeof(old_delta));
531 * frequency is in nanoseconds per second shifted left 32.
532 * kern_adjfreq() needs it in nanoseconds per tick shifted left 32.
535 sysctl_adjfreq(SYSCTL_HANDLER_ARGS)
540 if (req->newptr != NULL) {
541 if (suser(curthread))
543 error = SYSCTL_IN(req, &freqdelta, sizeof(freqdelta));
548 kern_adjfreq(freqdelta);
551 if (req->oldptr != NULL)
552 freqdelta = ntp_tick_permanent * hz;
553 error = SYSCTL_OUT(req, &freqdelta, sizeof(freqdelta));
560 SYSCTL_NODE(_kern, OID_AUTO, ntp, CTLFLAG_RW, 0, "NTP related controls");
561 SYSCTL_PROC(_kern_ntp, OID_AUTO, permanent,
562 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
563 sysctl_adjfreq, "Q", "permanent correction per second");
564 SYSCTL_PROC(_kern_ntp, OID_AUTO, delta,
565 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
566 sysctl_delta, "Q", "one-time delta");
567 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, big_delta, CTLFLAG_RD,
568 &ntp_big_delta, sizeof(ntp_big_delta), "Q",
569 "threshold for fast adjustment");
570 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, tick_delta, CTLFLAG_RD,
571 &ntp_tick_delta, sizeof(ntp_tick_delta), "LU",
572 "per-tick adjustment");
573 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, default_tick_delta, CTLFLAG_RD,
574 &ntp_default_tick_delta, sizeof(ntp_default_tick_delta), "LU",
575 "default per-tick adjustment");
576 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, next_leap_second, CTLFLAG_RW,
577 &ntp_leap_second, sizeof(ntp_leap_second), "LU",
579 SYSCTL_INT(_kern_ntp, OID_AUTO, insert_leap_second, CTLFLAG_RW,
580 &ntp_leap_insert, 0, "insert or remove leap second");
581 SYSCTL_PROC(_kern_ntp, OID_AUTO, adjust,
582 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
583 sysctl_adjtime, "Q", "relative adjust for delta");
586 * Get value of an interval timer. The process virtual and
587 * profiling virtual time timers are kept in the p_stats area, since
588 * they can be swapped out. These are kept internally in the
589 * way they are specified externally: in time until they expire.
591 * The real time interval timer is kept in the process table slot
592 * for the process, and its value (it_value) is kept as an
593 * absolute time rather than as a delta, so that it is easy to keep
594 * periodic real-time signals from drifting.
596 * Virtual time timers are processed in the hardclock() routine of
597 * kern_clock.c. The real time timer is processed by a timeout
598 * routine, called from the softclock() routine. Since a callout
599 * may be delayed in real time due to interrupt processing in the system,
600 * it is possible for the real time timeout routine (realitexpire, given below),
601 * to be delayed in real time past when it is supposed to occur. It
602 * does not suffice, therefore, to reload the real timer .it_value from the
603 * real time timers .it_interval. Rather, we compute the next time in
604 * absolute time the timer should go off.
608 sys_getitimer(struct getitimer_args *uap)
610 struct proc *p = curproc;
612 struct itimerval aitv;
614 if (uap->which > ITIMER_PROF)
617 if (uap->which == ITIMER_REAL) {
619 * Convert from absolute to relative time in .it_value
620 * part of real time timer. If time for real time timer
621 * has passed return 0, else return difference between
622 * current time and time for the timer to go off.
624 aitv = p->p_realtimer;
625 if (timevalisset(&aitv.it_value)) {
626 getmicrouptime(&ctv);
627 if (timevalcmp(&aitv.it_value, &ctv, <))
628 timevalclear(&aitv.it_value);
630 timevalsub(&aitv.it_value, &ctv);
633 aitv = p->p_timer[uap->which];
636 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
637 sizeof (struct itimerval)));
642 sys_setitimer(struct setitimer_args *uap)
644 struct itimerval aitv;
646 struct itimerval *itvp;
647 struct proc *p = curproc;
650 if (uap->which > ITIMER_PROF)
653 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
654 sizeof(struct itimerval))))
656 if ((uap->itv = uap->oitv) &&
657 (error = sys_getitimer((struct getitimer_args *)uap)))
661 if (itimerfix(&aitv.it_value))
663 if (!timevalisset(&aitv.it_value))
664 timevalclear(&aitv.it_interval);
665 else if (itimerfix(&aitv.it_interval))
668 if (uap->which == ITIMER_REAL) {
669 if (timevalisset(&p->p_realtimer.it_value))
670 callout_stop(&p->p_ithandle);
671 if (timevalisset(&aitv.it_value))
672 callout_reset(&p->p_ithandle,
673 tvtohz_high(&aitv.it_value), realitexpire, p);
674 getmicrouptime(&ctv);
675 timevaladd(&aitv.it_value, &ctv);
676 p->p_realtimer = aitv;
678 p->p_timer[uap->which] = aitv;
685 * Real interval timer expired:
686 * send process whose timer expired an alarm signal.
687 * If time is not set up to reload, then just return.
688 * Else compute next time timer should go off which is > current time.
689 * This is where delay in processing this timeout causes multiple
690 * SIGALRM calls to be compressed into one.
691 * tvtohz_high() always adds 1 to allow for the time until the next clock
692 * interrupt being strictly less than 1 clock tick, but we don't want
693 * that here since we want to appear to be in sync with the clock
694 * interrupt even when we're delayed.
697 realitexpire(void *arg)
700 struct timeval ctv, ntv;
702 p = (struct proc *)arg;
704 if (!timevalisset(&p->p_realtimer.it_interval)) {
705 timevalclear(&p->p_realtimer.it_value);
710 timevaladd(&p->p_realtimer.it_value,
711 &p->p_realtimer.it_interval);
712 getmicrouptime(&ctv);
713 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
714 ntv = p->p_realtimer.it_value;
715 timevalsub(&ntv, &ctv);
716 callout_reset(&p->p_ithandle, tvtohz_low(&ntv),
726 * Check that a proposed value to load into the .it_value or
727 * .it_interval part of an interval timer is acceptable, and
728 * fix it to have at least minimal value (i.e. if it is less
729 * than the resolution of the clock, round it up.)
732 itimerfix(struct timeval *tv)
735 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
736 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
738 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
744 * Decrement an interval timer by a specified number
745 * of microseconds, which must be less than a second,
746 * i.e. < 1000000. If the timer expires, then reload
747 * it. In this case, carry over (usec - old value) to
748 * reduce the value reloaded into the timer so that
749 * the timer does not drift. This routine assumes
750 * that it is called in a context where the timers
751 * on which it is operating cannot change in value.
754 itimerdecr(struct itimerval *itp, int usec)
757 if (itp->it_value.tv_usec < usec) {
758 if (itp->it_value.tv_sec == 0) {
759 /* expired, and already in next interval */
760 usec -= itp->it_value.tv_usec;
763 itp->it_value.tv_usec += 1000000;
764 itp->it_value.tv_sec--;
766 itp->it_value.tv_usec -= usec;
768 if (timevalisset(&itp->it_value))
770 /* expired, exactly at end of interval */
772 if (timevalisset(&itp->it_interval)) {
773 itp->it_value = itp->it_interval;
774 itp->it_value.tv_usec -= usec;
775 if (itp->it_value.tv_usec < 0) {
776 itp->it_value.tv_usec += 1000000;
777 itp->it_value.tv_sec--;
780 itp->it_value.tv_usec = 0; /* sec is already 0 */
785 * Add and subtract routines for timevals.
786 * N.B.: subtract routine doesn't deal with
787 * results which are before the beginning,
788 * it just gets very confused in this case.
792 timevaladd(struct timeval *t1, struct timeval *t2)
795 t1->tv_sec += t2->tv_sec;
796 t1->tv_usec += t2->tv_usec;
801 timevalsub(struct timeval *t1, struct timeval *t2)
804 t1->tv_sec -= t2->tv_sec;
805 t1->tv_usec -= t2->tv_usec;
810 timevalfix(struct timeval *t1)
813 if (t1->tv_usec < 0) {
815 t1->tv_usec += 1000000;
817 if (t1->tv_usec >= 1000000) {
819 t1->tv_usec -= 1000000;
824 * ratecheck(): simple time-based rate-limit checking.
827 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
829 struct timeval tv, delta;
832 getmicrouptime(&tv); /* NB: 10ms precision */
834 timevalsub(&delta, lasttime);
837 * check for 0,0 is so that the message will be seen at least once,
838 * even if interval is huge.
840 if (timevalcmp(&delta, mininterval, >=) ||
841 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
850 * ppsratecheck(): packets (or events) per second limitation.
852 * Return 0 if the limit is to be enforced (e.g. the caller
853 * should drop a packet because of the rate limitation).
855 * maxpps of 0 always causes zero to be returned. maxpps of -1
856 * always causes 1 to be returned; this effectively defeats rate
859 * Note that we maintain the struct timeval for compatibility
860 * with other bsd systems. We reuse the storage and just monitor
861 * clock ticks for minimal overhead.
864 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
869 * Reset the last time and counter if this is the first call
870 * or more than a second has passed since the last update of
874 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
875 lasttime->tv_sec = now;
877 return (maxpps != 0);
879 (*curpps)++; /* NB: ignore potential overflow */
880 return (maxpps < 0 || *curpps < maxpps);