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 $
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/resourcevar.h>
42 #include <sys/signalvar.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/sysent.h>
48 #include <sys/vnode.h>
50 #include <vm/vm_extern.h>
55 * Time of day and interval timer support.
57 * These routines provide the kernel entry points to get and set
58 * the time-of-day and per-process interval timers. Subroutines
59 * here provide support for adding and subtracting timeval structures
60 * and decrementing interval timers, optionally reloading the interval
61 * timers when they expire.
64 static int nanosleep1 __P((struct proc *p, struct timespec *rqt,
65 struct timespec *rmt));
66 static int settime __P((struct timeval *));
67 static void timevalfix __P((struct timeval *));
68 static void no_lease_updatetime __P((int));
71 no_lease_updatetime(deltat)
76 void (*lease_updatetime) __P((int)) = no_lease_updatetime;
82 struct timeval delta, tv1, tv2;
83 static struct timeval maxtime, laststep;
90 timevalsub(&delta, &tv1);
93 * If the system is secure, we do not allow the time to be
94 * set to a value earlier than 1 second less than the highest
95 * time we have yet seen. The worst a miscreant can do in
96 * this circumstance is "freeze" time. He couldn't go
99 * We similarly do not allow the clock to be stepped more
100 * than one second, nor more than once per second. This allows
101 * a miscreant to make the clock march double-time, but no worse.
103 if (securelevel > 1) {
104 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
106 * Update maxtime to latest time we've seen.
108 if (tv1.tv_sec > maxtime.tv_sec)
111 timevalsub(&tv2, &maxtime);
112 if (tv2.tv_sec < -1) {
113 tv->tv_sec = maxtime.tv_sec - 1;
114 printf("Time adjustment clamped to -1 second\n");
117 if (tv1.tv_sec == laststep.tv_sec) {
121 if (delta.tv_sec > 1) {
122 tv->tv_sec = tv1.tv_sec + 1;
123 printf("Time adjustment clamped to +1 second\n");
129 ts.tv_sec = tv->tv_sec;
130 ts.tv_nsec = tv->tv_usec * 1000;
131 set_timecounter(&ts);
132 (void) splsoftclock();
133 lease_updatetime(delta.tv_sec);
139 #ifndef _SYS_SYSPROTO_H_
140 struct clock_gettime_args {
148 clock_gettime(p, uap)
150 struct clock_gettime_args *uap;
154 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
157 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
160 #ifndef _SYS_SYSPROTO_H_
161 struct clock_settime_args {
163 const struct timespec *tp;
169 clock_settime(p, uap)
171 struct clock_settime_args *uap;
177 if ((error = suser(p)) != 0)
179 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
181 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
183 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
185 /* XXX Don't convert nsec->usec and back */
186 TIMESPEC_TO_TIMEVAL(&atv, &ats);
187 if ((error = settime(&atv)))
192 #ifndef _SYS_SYSPROTO_H_
193 struct clock_getres_args {
202 struct clock_getres_args *uap;
207 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
210 if (SCARG(uap, tp)) {
213 * Round up the result of the division cheaply by adding 1.
214 * Rounding up is especially important if rounding down
215 * would give 0. Perfect rounding is unimportant.
217 ts.tv_nsec = 1000000000 / timecounter->tc_frequency + 1;
218 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
226 nanosleep1(p, rqt, rmt)
228 struct timespec *rqt, *rmt;
230 struct timespec ts, ts2, ts3;
234 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
236 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
239 timespecadd(&ts, rqt);
240 TIMESPEC_TO_TIMEVAL(&tv, rqt);
242 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
245 if (error != EWOULDBLOCK) {
246 if (error == ERESTART)
249 timespecsub(&ts, &ts2);
256 if (timespeccmp(&ts2, &ts, >=))
259 timespecsub(&ts3, &ts2);
260 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
264 #ifndef _SYS_SYSPROTO_H_
265 struct nanosleep_args {
266 struct timespec *rqtp;
267 struct timespec *rmtp;
275 struct nanosleep_args *uap;
277 struct timespec rmt, rqt;
280 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
283 if (SCARG(uap, rmtp))
284 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
287 error = nanosleep1(p, &rqt, &rmt);
288 if (error && SCARG(uap, rmtp)) {
289 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
290 if (error2) /* XXX shouldn't happen, did useracc() above */
296 #ifndef _SYS_SYSPROTO_H_
297 struct gettimeofday_args {
299 struct timezone *tzp;
306 register struct gettimeofday_args *uap;
313 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
318 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
323 #ifndef _SYS_SYSPROTO_H_
324 struct settimeofday_args {
326 struct timezone *tzp;
333 struct settimeofday_args *uap;
339 if ((error = suser(p)))
341 /* Verify all parameters before changing time. */
343 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
346 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
350 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
352 if (uap->tv && (error = settime(&atv)))
359 int tickdelta; /* current clock skew, us. per tick */
360 long timedelta; /* unapplied time correction, us. */
361 static long bigadj = 1000000; /* use 10x skew above bigadj us. */
363 #ifndef _SYS_SYSPROTO_H_
364 struct adjtime_args {
365 struct timeval *delta;
366 struct timeval *olddelta;
373 register struct adjtime_args *uap;
376 register long ndelta, ntickdelta, odelta;
379 if ((error = suser(p)))
382 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
386 * Compute the total correction and the rate at which to apply it.
387 * Round the adjustment down to a whole multiple of the per-tick
388 * delta, so that after some number of incremental changes in
389 * hardclock(), tickdelta will become zero, lest the correction
390 * overshoot and start taking us away from the desired final time.
392 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
393 if (ndelta > bigadj || ndelta < -bigadj)
394 ntickdelta = 10 * tickadj;
396 ntickdelta = tickadj;
397 if (ndelta % ntickdelta)
398 ndelta = ndelta / ntickdelta * ntickdelta;
401 * To make hardclock()'s job easier, make the per-tick delta negative
402 * if we want time to run slower; then hardclock can simply compute
403 * tick + tickdelta, and subtract tickdelta from timedelta.
406 ntickdelta = -ntickdelta;
410 tickdelta = ntickdelta;
414 atv.tv_sec = odelta / 1000000;
415 atv.tv_usec = odelta % 1000000;
416 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
417 sizeof(struct timeval));
423 * Get value of an interval timer. The process virtual and
424 * profiling virtual time timers are kept in the p_stats area, since
425 * they can be swapped out. These are kept internally in the
426 * way they are specified externally: in time until they expire.
428 * The real time interval timer is kept in the process table slot
429 * for the process, and its value (it_value) is kept as an
430 * absolute time rather than as a delta, so that it is easy to keep
431 * periodic real-time signals from drifting.
433 * Virtual time timers are processed in the hardclock() routine of
434 * kern_clock.c. The real time timer is processed by a timeout
435 * routine, called from the softclock() routine. Since a callout
436 * may be delayed in real time due to interrupt processing in the system,
437 * it is possible for the real time timeout routine (realitexpire, given below),
438 * to be delayed in real time past when it is supposed to occur. It
439 * does not suffice, therefore, to reload the real timer .it_value from the
440 * real time timers .it_interval. Rather, we compute the next time in
441 * absolute time the timer should go off.
443 #ifndef _SYS_SYSPROTO_H_
444 struct getitimer_args {
446 struct itimerval *itv;
453 register struct getitimer_args *uap;
456 struct itimerval aitv;
459 if (uap->which > ITIMER_PROF)
461 s = splclock(); /* XXX still needed ? */
462 if (uap->which == ITIMER_REAL) {
464 * Convert from absolute to relative time in .it_value
465 * part of real time timer. If time for real time timer
466 * has passed return 0, else return difference between
467 * current time and time for the timer to go off.
469 aitv = p->p_realtimer;
470 if (timevalisset(&aitv.it_value)) {
471 getmicrouptime(&ctv);
472 if (timevalcmp(&aitv.it_value, &ctv, <))
473 timevalclear(&aitv.it_value);
475 timevalsub(&aitv.it_value, &ctv);
478 aitv = p->p_stats->p_timer[uap->which];
480 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
481 sizeof (struct itimerval)));
484 #ifndef _SYS_SYSPROTO_H_
485 struct setitimer_args {
487 struct itimerval *itv, *oitv;
494 register struct setitimer_args *uap;
496 struct itimerval aitv;
498 register struct itimerval *itvp;
501 if (uap->which > ITIMER_PROF)
504 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
505 sizeof(struct itimerval))))
507 if ((uap->itv = uap->oitv) &&
508 (error = getitimer(p, (struct getitimer_args *)uap)))
512 if (itimerfix(&aitv.it_value))
514 if (!timevalisset(&aitv.it_value))
515 timevalclear(&aitv.it_interval);
516 else if (itimerfix(&aitv.it_interval))
518 s = splclock(); /* XXX: still needed ? */
519 if (uap->which == ITIMER_REAL) {
520 if (timevalisset(&p->p_realtimer.it_value))
521 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
522 if (timevalisset(&aitv.it_value))
523 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
524 tvtohz(&aitv.it_value));
525 getmicrouptime(&ctv);
526 timevaladd(&aitv.it_value, &ctv);
527 p->p_realtimer = aitv;
529 p->p_stats->p_timer[uap->which] = aitv;
535 * Real interval timer expired:
536 * send process whose timer expired an alarm signal.
537 * If time is not set up to reload, then just return.
538 * Else compute next time timer should go off which is > current time.
539 * This is where delay in processing this timeout causes multiple
540 * SIGALRM calls to be compressed into one.
541 * tvtohz() always adds 1 to allow for the time until the next clock
542 * interrupt being strictly less than 1 clock tick, but we don't want
543 * that here since we want to appear to be in sync with the clock
544 * interrupt even when we're delayed.
550 register struct proc *p;
551 struct timeval ctv, ntv;
554 p = (struct proc *)arg;
556 if (!timevalisset(&p->p_realtimer.it_interval)) {
557 timevalclear(&p->p_realtimer.it_value);
561 s = splclock(); /* XXX: still neeeded ? */
562 timevaladd(&p->p_realtimer.it_value,
563 &p->p_realtimer.it_interval);
564 getmicrouptime(&ctv);
565 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
566 ntv = p->p_realtimer.it_value;
567 timevalsub(&ntv, &ctv);
568 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
578 * Check that a proposed value to load into the .it_value or
579 * .it_interval part of an interval timer is acceptable, and
580 * fix it to have at least minimal value (i.e. if it is less
581 * than the resolution of the clock, round it up.)
588 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
589 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
591 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
597 * Decrement an interval timer by a specified number
598 * of microseconds, which must be less than a second,
599 * i.e. < 1000000. If the timer expires, then reload
600 * it. In this case, carry over (usec - old value) to
601 * reduce the value reloaded into the timer so that
602 * the timer does not drift. This routine assumes
603 * that it is called in a context where the timers
604 * on which it is operating cannot change in value.
607 itimerdecr(itp, usec)
608 register struct itimerval *itp;
612 if (itp->it_value.tv_usec < usec) {
613 if (itp->it_value.tv_sec == 0) {
614 /* expired, and already in next interval */
615 usec -= itp->it_value.tv_usec;
618 itp->it_value.tv_usec += 1000000;
619 itp->it_value.tv_sec--;
621 itp->it_value.tv_usec -= usec;
623 if (timevalisset(&itp->it_value))
625 /* expired, exactly at end of interval */
627 if (timevalisset(&itp->it_interval)) {
628 itp->it_value = itp->it_interval;
629 itp->it_value.tv_usec -= usec;
630 if (itp->it_value.tv_usec < 0) {
631 itp->it_value.tv_usec += 1000000;
632 itp->it_value.tv_sec--;
635 itp->it_value.tv_usec = 0; /* sec is already 0 */
640 * Add and subtract routines for timevals.
641 * N.B.: subtract routine doesn't deal with
642 * results which are before the beginning,
643 * it just gets very confused in this case.
648 struct timeval *t1, *t2;
651 t1->tv_sec += t2->tv_sec;
652 t1->tv_usec += t2->tv_usec;
658 struct timeval *t1, *t2;
661 t1->tv_sec -= t2->tv_sec;
662 t1->tv_usec -= t2->tv_usec;
671 if (t1->tv_usec < 0) {
673 t1->tv_usec += 1000000;
675 if (t1->tv_usec >= 1000000) {
677 t1->tv_usec -= 1000000;