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
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/mutex.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/timetc.h>
50 #include <sys/vnode.h>
53 #include <vm/vm_extern.h>
58 * Time of day and interval timer support.
60 * These routines provide the kernel entry points to get and set
61 * the time-of-day and per-process interval timers. Subroutines
62 * here provide support for adding and subtracting timeval structures
63 * and decrementing interval timers, optionally reloading the interval
64 * timers when they expire.
67 static int nanosleep1 __P((struct thread *td, struct timespec *rqt,
68 struct timespec *rmt));
69 static int settime __P((struct proc *, struct timeval *));
70 static void timevalfix __P((struct timeval *));
71 static void no_lease_updatetime __P((int));
74 no_lease_updatetime(deltat)
79 void (*lease_updatetime) __P((int)) = no_lease_updatetime;
86 struct timeval delta, tv1, tv2;
87 static struct timeval maxtime, laststep;
94 timevalsub(&delta, &tv1);
97 * If the system is secure, we do not allow the time to be
98 * set to a value earlier than 1 second less than the highest
99 * time we have yet seen. The worst a miscreant can do in
100 * this circumstance is "freeze" time. He couldn't go
103 * We similarly do not allow the clock to be stepped more
104 * than one second, nor more than once per second. This allows
105 * a miscreant to make the clock march double-time, but no worse.
107 if (securelevel_gt(p->p_ucred, 1) != 0) {
108 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
110 * Update maxtime to latest time we've seen.
112 if (tv1.tv_sec > maxtime.tv_sec)
115 timevalsub(&tv2, &maxtime);
116 if (tv2.tv_sec < -1) {
117 tv->tv_sec = maxtime.tv_sec - 1;
118 printf("Time adjustment clamped to -1 second\n");
121 if (tv1.tv_sec == laststep.tv_sec) {
125 if (delta.tv_sec > 1) {
126 tv->tv_sec = tv1.tv_sec + 1;
127 printf("Time adjustment clamped to +1 second\n");
133 ts.tv_sec = tv->tv_sec;
134 ts.tv_nsec = tv->tv_usec * 1000;
136 (void) splsoftclock();
137 lease_updatetime(delta.tv_sec);
143 #ifndef _SYS_SYSPROTO_H_
144 struct clock_gettime_args {
155 clock_gettime(td, uap)
157 struct clock_gettime_args *uap;
161 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
166 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
169 #ifndef _SYS_SYSPROTO_H_
170 struct clock_settime_args {
172 const struct timespec *tp;
181 clock_settime(td, uap)
183 struct clock_settime_args *uap;
190 if ((error = suser_td(td)) != 0)
192 if (SCARG(uap, clock_id) != CLOCK_REALTIME) {
196 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
198 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000) {
202 /* XXX Don't convert nsec->usec and back */
203 TIMESPEC_TO_TIMEVAL(&atv, &ats);
204 error = settime(td->td_proc, &atv);
210 #ifndef _SYS_SYSPROTO_H_
211 struct clock_getres_args {
218 clock_getres(td, uap)
220 struct clock_getres_args *uap;
225 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
228 if (SCARG(uap, tp)) {
230 ts.tv_nsec = 1000000000 / timecounter->tc_frequency;
231 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
239 nanosleep1(td, rqt, rmt)
241 struct timespec *rqt, *rmt;
243 struct timespec ts, ts2, ts3;
247 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
249 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
252 timespecadd(&ts, rqt);
253 TIMESPEC_TO_TIMEVAL(&tv, rqt);
255 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
258 if (error != EWOULDBLOCK) {
259 if (error == ERESTART)
262 timespecsub(&ts, &ts2);
269 if (timespeccmp(&ts2, &ts, >=))
272 timespecsub(&ts3, &ts2);
273 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
277 #ifndef _SYS_SYSPROTO_H_
278 struct nanosleep_args {
279 struct timespec *rqtp;
280 struct timespec *rmtp;
291 struct nanosleep_args *uap;
293 struct timespec rmt, rqt;
296 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
301 if (SCARG(uap, rmtp)) {
302 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
308 error = nanosleep1(td, &rqt, &rmt);
309 if (error && SCARG(uap, rmtp)) {
312 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
313 if (error2) /* XXX shouldn't happen, did useracc() above */
321 #ifndef _SYS_SYSPROTO_H_
322 struct gettimeofday_args {
324 struct timezone *tzp;
332 gettimeofday(td, uap)
334 register struct gettimeofday_args *uap;
342 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
348 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
356 #ifndef _SYS_SYSPROTO_H_
357 struct settimeofday_args {
359 struct timezone *tzp;
367 settimeofday(td, uap)
369 struct settimeofday_args *uap;
377 if ((error = suser_td(td)))
379 /* Verify all parameters before changing time. */
381 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
385 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) {
391 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz)))) {
394 if (uap->tv && (error = settime(td->td_proc, &atv)))
403 int tickdelta; /* current clock skew, us. per tick */
404 long timedelta; /* unapplied time correction, us. */
405 static long bigadj = 1000000; /* use 10x skew above bigadj us. */
407 #ifndef _SYS_SYSPROTO_H_
408 struct adjtime_args {
409 struct timeval *delta;
410 struct timeval *olddelta;
420 register struct adjtime_args *uap;
423 register long ndelta, ntickdelta, odelta;
428 if ((error = suser_td(td)))
430 error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
431 sizeof(struct timeval));
436 * Compute the total correction and the rate at which to apply it.
437 * Round the adjustment down to a whole multiple of the per-tick
438 * delta, so that after some number of incremental changes in
439 * hardclock(), tickdelta will become zero, lest the correction
440 * overshoot and start taking us away from the desired final time.
442 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
443 if (ndelta > bigadj || ndelta < -bigadj)
444 ntickdelta = 10 * tickadj;
446 ntickdelta = tickadj;
447 if (ndelta % ntickdelta)
448 ndelta = ndelta / ntickdelta * ntickdelta;
451 * To make hardclock()'s job easier, make the per-tick delta negative
452 * if we want time to run slower; then hardclock can simply compute
453 * tick + tickdelta, and subtract tickdelta from timedelta.
456 ntickdelta = -ntickdelta;
460 tickdelta = ntickdelta;
464 atv.tv_sec = odelta / 1000000;
465 atv.tv_usec = odelta % 1000000;
466 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
467 sizeof(struct timeval));
475 * Get value of an interval timer. The process virtual and
476 * profiling virtual time timers are kept in the p_stats area, since
477 * they can be swapped out. These are kept internally in the
478 * way they are specified externally: in time until they expire.
480 * The real time interval timer is kept in the process table slot
481 * for the process, and its value (it_value) is kept as an
482 * absolute time rather than as a delta, so that it is easy to keep
483 * periodic real-time signals from drifting.
485 * Virtual time timers are processed in the hardclock() routine of
486 * kern_clock.c. The real time timer is processed by a timeout
487 * routine, called from the softclock() routine. Since a callout
488 * may be delayed in real time due to interrupt processing in the system,
489 * it is possible for the real time timeout routine (realitexpire, given below),
490 * to be delayed in real time past when it is supposed to occur. It
491 * does not suffice, therefore, to reload the real timer .it_value from the
492 * real time timers .it_interval. Rather, we compute the next time in
493 * absolute time the timer should go off.
495 #ifndef _SYS_SYSPROTO_H_
496 struct getitimer_args {
498 struct itimerval *itv;
508 register struct getitimer_args *uap;
510 struct proc *p = td->td_proc;
512 struct itimerval aitv;
516 if (uap->which > ITIMER_PROF)
521 s = splclock(); /* XXX still needed ? */
522 if (uap->which == ITIMER_REAL) {
524 * Convert from absolute to relative time in .it_value
525 * part of real time timer. If time for real time timer
526 * has passed return 0, else return difference between
527 * current time and time for the timer to go off.
529 aitv = p->p_realtimer;
530 if (timevalisset(&aitv.it_value)) {
531 getmicrouptime(&ctv);
532 if (timevalcmp(&aitv.it_value, &ctv, <))
533 timevalclear(&aitv.it_value);
535 timevalsub(&aitv.it_value, &ctv);
538 aitv = p->p_stats->p_timer[uap->which];
541 error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
542 sizeof (struct itimerval));
547 #ifndef _SYS_SYSPROTO_H_
548 struct setitimer_args {
550 struct itimerval *itv, *oitv;
560 register struct setitimer_args *uap;
562 struct proc *p = td->td_proc;
563 struct itimerval aitv;
565 register struct itimerval *itvp;
568 if (uap->which > ITIMER_PROF)
571 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
572 sizeof(struct itimerval))))
577 if ((uap->itv = uap->oitv) &&
578 (error = getitimer(td, (struct getitimer_args *)uap))) {
585 if (itimerfix(&aitv.it_value)) {
589 if (!timevalisset(&aitv.it_value)) {
590 timevalclear(&aitv.it_interval);
591 } else if (itimerfix(&aitv.it_interval)) {
595 s = splclock(); /* XXX: still needed ? */
596 if (uap->which == ITIMER_REAL) {
597 if (timevalisset(&p->p_realtimer.it_value))
598 callout_stop(&p->p_itcallout);
599 if (timevalisset(&aitv.it_value))
600 callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
602 getmicrouptime(&ctv);
603 timevaladd(&aitv.it_value, &ctv);
604 p->p_realtimer = aitv;
606 p->p_stats->p_timer[uap->which] = aitv;
615 * Real interval timer expired:
616 * send process whose timer expired an alarm signal.
617 * If time is not set up to reload, then just return.
618 * Else compute next time timer should go off which is > current time.
619 * This is where delay in processing this timeout causes multiple
620 * SIGALRM calls to be compressed into one.
621 * tvtohz() always adds 1 to allow for the time until the next clock
622 * interrupt being strictly less than 1 clock tick, but we don't want
623 * that here since we want to appear to be in sync with the clock
624 * interrupt even when we're delayed.
630 register struct proc *p;
631 struct timeval ctv, ntv;
634 p = (struct proc *)arg;
637 if (!timevalisset(&p->p_realtimer.it_interval)) {
638 timevalclear(&p->p_realtimer.it_value);
643 s = splclock(); /* XXX: still neeeded ? */
644 timevaladd(&p->p_realtimer.it_value,
645 &p->p_realtimer.it_interval);
646 getmicrouptime(&ctv);
647 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
648 ntv = p->p_realtimer.it_value;
649 timevalsub(&ntv, &ctv);
650 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
662 * Check that a proposed value to load into the .it_value or
663 * .it_interval part of an interval timer is acceptable, and
664 * fix it to have at least minimal value (i.e. if it is less
665 * than the resolution of the clock, round it up.)
672 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
673 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
675 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
681 * Decrement an interval timer by a specified number
682 * of microseconds, which must be less than a second,
683 * i.e. < 1000000. If the timer expires, then reload
684 * it. In this case, carry over (usec - old value) to
685 * reduce the value reloaded into the timer so that
686 * the timer does not drift. This routine assumes
687 * that it is called in a context where the timers
688 * on which it is operating cannot change in value.
691 itimerdecr(itp, usec)
692 register struct itimerval *itp;
696 if (itp->it_value.tv_usec < usec) {
697 if (itp->it_value.tv_sec == 0) {
698 /* expired, and already in next interval */
699 usec -= itp->it_value.tv_usec;
702 itp->it_value.tv_usec += 1000000;
703 itp->it_value.tv_sec--;
705 itp->it_value.tv_usec -= usec;
707 if (timevalisset(&itp->it_value))
709 /* expired, exactly at end of interval */
711 if (timevalisset(&itp->it_interval)) {
712 itp->it_value = itp->it_interval;
713 itp->it_value.tv_usec -= usec;
714 if (itp->it_value.tv_usec < 0) {
715 itp->it_value.tv_usec += 1000000;
716 itp->it_value.tv_sec--;
719 itp->it_value.tv_usec = 0; /* sec is already 0 */
724 * Add and subtract routines for timevals.
725 * N.B.: subtract routine doesn't deal with
726 * results which are before the beginning,
727 * it just gets very confused in this case.
732 struct timeval *t1, *t2;
735 t1->tv_sec += t2->tv_sec;
736 t1->tv_usec += t2->tv_usec;
742 struct timeval *t1, *t2;
745 t1->tv_sec -= t2->tv_sec;
746 t1->tv_usec -= t2->tv_usec;
755 if (t1->tv_usec < 0) {
757 t1->tv_usec += 1000000;
759 if (t1->tv_usec >= 1000000) {
761 t1->tv_usec -= 1000000;