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(struct thread *td, struct timespec *rqt,
68 struct timespec *rmt);
69 static int settime(struct thread *, struct timeval *);
70 static void timevalfix(struct timeval *);
71 static void no_lease_updatetime(int);
74 no_lease_updatetime(deltat)
79 void (*lease_updatetime)(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(td->td_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;
137 (void) splsoftclock();
138 lease_updatetime(delta.tv_sec);
145 #ifndef _SYS_SYSPROTO_H_
146 struct clock_gettime_args {
157 clock_gettime(td, uap)
159 struct clock_gettime_args *uap;
163 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
168 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
171 #ifndef _SYS_SYSPROTO_H_
172 struct clock_settime_args {
174 const struct timespec *tp;
183 clock_settime(td, uap)
185 struct clock_settime_args *uap;
191 if ((error = suser(td)) != 0)
193 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
195 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
197 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
199 /* XXX Don't convert nsec->usec and back */
200 TIMESPEC_TO_TIMEVAL(&atv, &ats);
201 error = settime(td, &atv);
205 #ifndef _SYS_SYSPROTO_H_
206 struct clock_getres_args {
213 clock_getres(td, uap)
215 struct clock_getres_args *uap;
220 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
223 if (SCARG(uap, tp)) {
225 ts.tv_nsec = 1000000000 / tc_getfrequency();
226 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
234 nanosleep1(td, rqt, rmt)
236 struct timespec *rqt, *rmt;
238 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);
248 TIMESPEC_TO_TIMEVAL(&tv, rqt);
250 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
253 if (error != EWOULDBLOCK) {
254 if (error == ERESTART)
257 timespecsub(&ts, &ts2);
264 if (timespeccmp(&ts2, &ts, >=))
267 timespecsub(&ts3, &ts2);
268 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
272 #ifndef _SYS_SYSPROTO_H_
273 struct nanosleep_args {
274 struct timespec *rqtp;
275 struct timespec *rmtp;
286 struct nanosleep_args *uap;
288 struct timespec rmt, rqt;
291 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
296 if (SCARG(uap, rmtp)) {
297 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
303 error = nanosleep1(td, &rqt, &rmt);
304 if (error && SCARG(uap, rmtp)) {
307 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
308 if (error2) /* XXX shouldn't happen, did useracc() above */
316 #ifndef _SYS_SYSPROTO_H_
317 struct gettimeofday_args {
319 struct timezone *tzp;
327 gettimeofday(td, uap)
329 register struct gettimeofday_args *uap;
336 error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv));
338 if (error == 0 && uap->tzp != NULL) {
340 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
347 #ifndef _SYS_SYSPROTO_H_
348 struct settimeofday_args {
350 struct timezone *tzp;
358 settimeofday(td, uap)
360 struct settimeofday_args *uap;
366 if ((error = suser(td)))
368 /* Verify all parameters before changing time. */
370 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
373 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
377 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
380 if (uap->tv && (error = settime(td, &atv)))
390 * Get value of an interval timer. The process virtual and
391 * profiling virtual time timers are kept in the p_stats area, since
392 * they can be swapped out. These are kept internally in the
393 * way they are specified externally: in time until they expire.
395 * The real time interval timer is kept in the process table slot
396 * for the process, and its value (it_value) is kept as an
397 * absolute time rather than as a delta, so that it is easy to keep
398 * periodic real-time signals from drifting.
400 * Virtual time timers are processed in the hardclock() routine of
401 * kern_clock.c. The real time timer is processed by a timeout
402 * routine, called from the softclock() routine. Since a callout
403 * may be delayed in real time due to interrupt processing in the system,
404 * it is possible for the real time timeout routine (realitexpire, given below),
405 * to be delayed in real time past when it is supposed to occur. It
406 * does not suffice, therefore, to reload the real timer .it_value from the
407 * real time timers .it_interval. Rather, we compute the next time in
408 * absolute time the timer should go off.
410 #ifndef _SYS_SYSPROTO_H_
411 struct getitimer_args {
413 struct itimerval *itv;
423 register struct getitimer_args *uap;
425 struct proc *p = td->td_proc;
427 struct itimerval aitv;
431 if (uap->which > ITIMER_PROF)
436 s = splclock(); /* XXX still needed ? */
437 if (uap->which == ITIMER_REAL) {
439 * Convert from absolute to relative time in .it_value
440 * part of real time timer. If time for real time timer
441 * has passed return 0, else return difference between
442 * current time and time for the timer to go off.
444 aitv = p->p_realtimer;
445 if (timevalisset(&aitv.it_value)) {
446 getmicrouptime(&ctv);
447 if (timevalcmp(&aitv.it_value, &ctv, <))
448 timevalclear(&aitv.it_value);
450 timevalsub(&aitv.it_value, &ctv);
453 aitv = p->p_stats->p_timer[uap->which];
456 error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
457 sizeof (struct itimerval));
462 #ifndef _SYS_SYSPROTO_H_
463 struct setitimer_args {
465 struct itimerval *itv, *oitv;
475 register struct setitimer_args *uap;
477 struct proc *p = td->td_proc;
478 struct itimerval aitv;
480 register struct itimerval *itvp;
483 if (uap->which > ITIMER_PROF)
486 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
487 sizeof(struct itimerval))))
492 if ((uap->itv = uap->oitv) &&
493 (error = getitimer(td, (struct getitimer_args *)uap))) {
500 if (itimerfix(&aitv.it_value)) {
504 if (!timevalisset(&aitv.it_value)) {
505 timevalclear(&aitv.it_interval);
506 } else if (itimerfix(&aitv.it_interval)) {
510 s = splclock(); /* XXX: still needed ? */
511 if (uap->which == ITIMER_REAL) {
512 if (timevalisset(&p->p_realtimer.it_value))
513 callout_stop(&p->p_itcallout);
514 if (timevalisset(&aitv.it_value))
515 callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
517 getmicrouptime(&ctv);
518 timevaladd(&aitv.it_value, &ctv);
519 p->p_realtimer = aitv;
521 p->p_stats->p_timer[uap->which] = aitv;
530 * Real interval timer expired:
531 * send process whose timer expired an alarm signal.
532 * If time is not set up to reload, then just return.
533 * Else compute next time timer should go off which is > current time.
534 * This is where delay in processing this timeout causes multiple
535 * SIGALRM calls to be compressed into one.
536 * tvtohz() always adds 1 to allow for the time until the next clock
537 * interrupt being strictly less than 1 clock tick, but we don't want
538 * that here since we want to appear to be in sync with the clock
539 * interrupt even when we're delayed.
545 register struct proc *p;
546 struct timeval ctv, ntv;
549 p = (struct proc *)arg;
552 if (!timevalisset(&p->p_realtimer.it_interval)) {
553 timevalclear(&p->p_realtimer.it_value);
558 s = splclock(); /* XXX: still neeeded ? */
559 timevaladd(&p->p_realtimer.it_value,
560 &p->p_realtimer.it_interval);
561 getmicrouptime(&ctv);
562 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
563 ntv = p->p_realtimer.it_value;
564 timevalsub(&ntv, &ctv);
565 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
577 * Check that a proposed value to load into the .it_value or
578 * .it_interval part of an interval timer is acceptable, and
579 * fix it to have at least minimal value (i.e. if it is less
580 * than the resolution of the clock, round it up.)
587 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
588 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
590 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
596 * Decrement an interval timer by a specified number
597 * of microseconds, which must be less than a second,
598 * i.e. < 1000000. If the timer expires, then reload
599 * it. In this case, carry over (usec - old value) to
600 * reduce the value reloaded into the timer so that
601 * the timer does not drift. This routine assumes
602 * that it is called in a context where the timers
603 * on which it is operating cannot change in value.
606 itimerdecr(itp, usec)
607 register struct itimerval *itp;
611 if (itp->it_value.tv_usec < usec) {
612 if (itp->it_value.tv_sec == 0) {
613 /* expired, and already in next interval */
614 usec -= itp->it_value.tv_usec;
617 itp->it_value.tv_usec += 1000000;
618 itp->it_value.tv_sec--;
620 itp->it_value.tv_usec -= usec;
622 if (timevalisset(&itp->it_value))
624 /* expired, exactly at end of interval */
626 if (timevalisset(&itp->it_interval)) {
627 itp->it_value = itp->it_interval;
628 itp->it_value.tv_usec -= usec;
629 if (itp->it_value.tv_usec < 0) {
630 itp->it_value.tv_usec += 1000000;
631 itp->it_value.tv_sec--;
634 itp->it_value.tv_usec = 0; /* sec is already 0 */
639 * Add and subtract routines for timevals.
640 * N.B.: subtract routine doesn't deal with
641 * results which are before the beginning,
642 * it just gets very confused in this case.
647 struct timeval *t1, *t2;
650 t1->tv_sec += t2->tv_sec;
651 t1->tv_usec += t2->tv_usec;
657 struct timeval *t1, *t2;
660 t1->tv_sec -= t2->tv_sec;
661 t1->tv_usec -= t2->tv_usec;
670 if (t1->tv_usec < 0) {
672 t1->tv_usec += 1000000;
674 if (t1->tv_usec >= 1000000) {
676 t1->tv_usec -= 1000000;