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.3 2003/06/23 17:55:41 dillon 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>
49 #include <sys/vnode.h>
51 #include <vm/vm_extern.h>
56 * Time of day and interval timer support.
58 * These routines provide the kernel entry points to get and set
59 * the time-of-day and per-process interval timers. Subroutines
60 * here provide support for adding and subtracting timeval structures
61 * and decrementing interval timers, optionally reloading the interval
62 * timers when they expire.
65 static int nanosleep1 __P((struct timespec *rqt,
66 struct timespec *rmt));
67 static int settime __P((struct timeval *));
68 static void timevalfix __P((struct timeval *));
69 static void no_lease_updatetime __P((int));
72 no_lease_updatetime(deltat)
77 void (*lease_updatetime) __P((int)) = no_lease_updatetime;
83 struct timeval delta, tv1, tv2;
84 static struct timeval maxtime, laststep;
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 printf("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 printf("Time adjustment clamped to +1 second\n");
130 ts.tv_sec = tv->tv_sec;
131 ts.tv_nsec = tv->tv_usec * 1000;
132 set_timecounter(&ts);
133 (void) splsoftclock();
134 lease_updatetime(delta.tv_sec);
140 #ifndef _SYS_SYSPROTO_H_
141 struct clock_gettime_args {
149 clock_gettime(struct clock_gettime_args *uap)
153 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
156 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
159 #ifndef _SYS_SYSPROTO_H_
160 struct clock_settime_args {
162 const struct timespec *tp;
168 clock_settime(struct clock_settime_args *uap)
174 if ((error = suser()) != 0)
176 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
178 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
180 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
182 /* XXX Don't convert nsec->usec and back */
183 TIMESPEC_TO_TIMEVAL(&atv, &ats);
184 if ((error = settime(&atv)))
189 #ifndef _SYS_SYSPROTO_H_
190 struct clock_getres_args {
197 clock_getres(struct clock_getres_args *uap)
202 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
205 if (SCARG(uap, tp)) {
208 * Round up the result of the division cheaply by adding 1.
209 * Rounding up is especially important if rounding down
210 * would give 0. Perfect rounding is unimportant.
212 ts.tv_nsec = 1000000000 / timecounter->tc_frequency + 1;
213 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
221 nanosleep1(struct timespec *rqt, struct timespec *rmt)
223 struct timespec ts, ts2, ts3;
227 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
229 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
232 timespecadd(&ts, rqt);
233 TIMESPEC_TO_TIMEVAL(&tv, rqt);
235 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
238 if (error != EWOULDBLOCK) {
239 if (error == ERESTART)
242 timespecsub(&ts, &ts2);
249 if (timespeccmp(&ts2, &ts, >=))
252 timespecsub(&ts3, &ts2);
253 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
257 #ifndef _SYS_SYSPROTO_H_
258 struct nanosleep_args {
259 struct timespec *rqtp;
260 struct timespec *rmtp;
266 nanosleep(struct nanosleep_args *uap)
268 struct timespec rmt, rqt;
271 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
274 if (SCARG(uap, rmtp))
275 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
278 error = nanosleep1(&rqt, &rmt);
279 if (error && SCARG(uap, rmtp)) {
280 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
281 if (error2) /* XXX shouldn't happen, did useracc() above */
287 #ifndef _SYS_SYSPROTO_H_
288 struct gettimeofday_args {
290 struct timezone *tzp;
295 gettimeofday(struct gettimeofday_args *uap)
302 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
307 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
312 #ifndef _SYS_SYSPROTO_H_
313 struct settimeofday_args {
315 struct timezone *tzp;
320 settimeofday(struct settimeofday_args *uap)
326 if ((error = suser()))
328 /* Verify all parameters before changing time. */
330 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
333 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
337 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
339 if (uap->tv && (error = settime(&atv)))
346 int tickdelta; /* current clock skew, us. per tick */
347 long timedelta; /* unapplied time correction, us. */
348 static long bigadj = 1000000; /* use 10x skew above bigadj us. */
350 #ifndef _SYS_SYSPROTO_H_
351 struct adjtime_args {
352 struct timeval *delta;
353 struct timeval *olddelta;
358 adjtime(struct adjtime_args *uap)
361 register long ndelta, ntickdelta, odelta;
364 if ((error = suser()))
367 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
371 * Compute the total correction and the rate at which to apply it.
372 * Round the adjustment down to a whole multiple of the per-tick
373 * delta, so that after some number of incremental changes in
374 * hardclock(), tickdelta will become zero, lest the correction
375 * overshoot and start taking us away from the desired final time.
377 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
378 if (ndelta > bigadj || ndelta < -bigadj)
379 ntickdelta = 10 * tickadj;
381 ntickdelta = tickadj;
382 if (ndelta % ntickdelta)
383 ndelta = ndelta / ntickdelta * ntickdelta;
386 * To make hardclock()'s job easier, make the per-tick delta negative
387 * if we want time to run slower; then hardclock can simply compute
388 * tick + tickdelta, and subtract tickdelta from timedelta.
391 ntickdelta = -ntickdelta;
395 tickdelta = ntickdelta;
399 atv.tv_sec = odelta / 1000000;
400 atv.tv_usec = odelta % 1000000;
401 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
402 sizeof(struct timeval));
408 * Get value of an interval timer. The process virtual and
409 * profiling virtual time timers are kept in the p_stats area, since
410 * they can be swapped out. These are kept internally in the
411 * way they are specified externally: in time until they expire.
413 * The real time interval timer is kept in the process table slot
414 * for the process, and its value (it_value) is kept as an
415 * absolute time rather than as a delta, so that it is easy to keep
416 * periodic real-time signals from drifting.
418 * Virtual time timers are processed in the hardclock() routine of
419 * kern_clock.c. The real time timer is processed by a timeout
420 * routine, called from the softclock() routine. Since a callout
421 * may be delayed in real time due to interrupt processing in the system,
422 * it is possible for the real time timeout routine (realitexpire, given below),
423 * to be delayed in real time past when it is supposed to occur. It
424 * does not suffice, therefore, to reload the real timer .it_value from the
425 * real time timers .it_interval. Rather, we compute the next time in
426 * absolute time the timer should go off.
428 #ifndef _SYS_SYSPROTO_H_
429 struct getitimer_args {
431 struct itimerval *itv;
436 getitimer(struct getitimer_args *uap)
438 struct proc *p = curproc;
440 struct itimerval aitv;
443 if (uap->which > ITIMER_PROF)
445 s = splclock(); /* XXX still needed ? */
446 if (uap->which == ITIMER_REAL) {
448 * Convert from absolute to relative time in .it_value
449 * part of real time timer. If time for real time timer
450 * has passed return 0, else return difference between
451 * current time and time for the timer to go off.
453 aitv = p->p_realtimer;
454 if (timevalisset(&aitv.it_value)) {
455 getmicrouptime(&ctv);
456 if (timevalcmp(&aitv.it_value, &ctv, <))
457 timevalclear(&aitv.it_value);
459 timevalsub(&aitv.it_value, &ctv);
462 aitv = p->p_stats->p_timer[uap->which];
464 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
465 sizeof (struct itimerval)));
468 #ifndef _SYS_SYSPROTO_H_
469 struct setitimer_args {
471 struct itimerval *itv, *oitv;
476 setitimer(struct setitimer_args *uap)
478 struct itimerval aitv;
480 struct itimerval *itvp;
481 struct proc *p = curproc;
484 if (uap->which > ITIMER_PROF)
487 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
488 sizeof(struct itimerval))))
490 if ((uap->itv = uap->oitv) &&
491 (error = getitimer((struct getitimer_args *)uap)))
495 if (itimerfix(&aitv.it_value))
497 if (!timevalisset(&aitv.it_value))
498 timevalclear(&aitv.it_interval);
499 else if (itimerfix(&aitv.it_interval))
501 s = splclock(); /* XXX: still needed ? */
502 if (uap->which == ITIMER_REAL) {
503 if (timevalisset(&p->p_realtimer.it_value))
504 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
505 if (timevalisset(&aitv.it_value))
506 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
507 tvtohz(&aitv.it_value));
508 getmicrouptime(&ctv);
509 timevaladd(&aitv.it_value, &ctv);
510 p->p_realtimer = aitv;
512 p->p_stats->p_timer[uap->which] = aitv;
518 * Real interval timer expired:
519 * send process whose timer expired an alarm signal.
520 * If time is not set up to reload, then just return.
521 * Else compute next time timer should go off which is > current time.
522 * This is where delay in processing this timeout causes multiple
523 * SIGALRM calls to be compressed into one.
524 * tvtohz() always adds 1 to allow for the time until the next clock
525 * interrupt being strictly less than 1 clock tick, but we don't want
526 * that here since we want to appear to be in sync with the clock
527 * interrupt even when we're delayed.
533 register struct proc *p;
534 struct timeval ctv, ntv;
537 p = (struct proc *)arg;
539 if (!timevalisset(&p->p_realtimer.it_interval)) {
540 timevalclear(&p->p_realtimer.it_value);
544 s = splclock(); /* XXX: still neeeded ? */
545 timevaladd(&p->p_realtimer.it_value,
546 &p->p_realtimer.it_interval);
547 getmicrouptime(&ctv);
548 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
549 ntv = p->p_realtimer.it_value;
550 timevalsub(&ntv, &ctv);
551 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
561 * Check that a proposed value to load into the .it_value or
562 * .it_interval part of an interval timer is acceptable, and
563 * fix it to have at least minimal value (i.e. if it is less
564 * than the resolution of the clock, round it up.)
571 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
572 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
574 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
580 * Decrement an interval timer by a specified number
581 * of microseconds, which must be less than a second,
582 * i.e. < 1000000. If the timer expires, then reload
583 * it. In this case, carry over (usec - old value) to
584 * reduce the value reloaded into the timer so that
585 * the timer does not drift. This routine assumes
586 * that it is called in a context where the timers
587 * on which it is operating cannot change in value.
590 itimerdecr(itp, usec)
591 register struct itimerval *itp;
595 if (itp->it_value.tv_usec < usec) {
596 if (itp->it_value.tv_sec == 0) {
597 /* expired, and already in next interval */
598 usec -= itp->it_value.tv_usec;
601 itp->it_value.tv_usec += 1000000;
602 itp->it_value.tv_sec--;
604 itp->it_value.tv_usec -= usec;
606 if (timevalisset(&itp->it_value))
608 /* expired, exactly at end of interval */
610 if (timevalisset(&itp->it_interval)) {
611 itp->it_value = itp->it_interval;
612 itp->it_value.tv_usec -= usec;
613 if (itp->it_value.tv_usec < 0) {
614 itp->it_value.tv_usec += 1000000;
615 itp->it_value.tv_sec--;
618 itp->it_value.tv_usec = 0; /* sec is already 0 */
623 * Add and subtract routines for timevals.
624 * N.B.: subtract routine doesn't deal with
625 * results which are before the beginning,
626 * it just gets very confused in this case.
631 struct timeval *t1, *t2;
634 t1->tv_sec += t2->tv_sec;
635 t1->tv_usec += t2->tv_usec;
641 struct timeval *t1, *t2;
644 t1->tv_sec -= t2->tv_sec;
645 t1->tv_usec -= t2->tv_usec;
654 if (t1->tv_usec < 0) {
656 t1->tv_usec += 1000000;
658 if (t1->tv_usec >= 1000000) {
660 t1->tv_usec -= 1000000;