1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)kern_time.c 8.1 (Berkeley) 6/10/93 34c8b47828SBruce Evans * $Id: kern_time.c,v 1.55 1998/05/17 11:52:48 phk Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38d2d3e875SBruce Evans #include <sys/sysproto.h> 39df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 40797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 41df8bae1dSRodney W. Grimes #include <sys/kernel.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 4394c8fcd8SPeter Wemm #include <sys/sysent.h> 44df8bae1dSRodney W. Grimes #include <sys/proc.h> 45708e7684SPeter Wemm #include <sys/time.h> 46df8bae1dSRodney W. Grimes #include <sys/vnode.h> 475b870b7bSPeter Wemm #include <vm/vm.h> 485b870b7bSPeter Wemm #include <vm/vm_extern.h> 49df8bae1dSRodney W. Grimes 50ac7e6123SDavid Greenman struct timezone tz; 51ac7e6123SDavid Greenman 52df8bae1dSRodney W. Grimes /* 53df8bae1dSRodney W. Grimes * Time of day and interval timer support. 54df8bae1dSRodney W. Grimes * 55df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 56df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 57df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 58df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 59df8bae1dSRodney W. Grimes * timers when they expire. 60df8bae1dSRodney W. Grimes */ 61df8bae1dSRodney W. Grimes 625b870b7bSPeter Wemm static int nanosleep1 __P((struct proc *p, struct timespec *rqt, 635b870b7bSPeter Wemm struct timespec *rmt)); 647d7fb492SBruce Evans static int settime __P((struct timeval *)); 657d7fb492SBruce Evans static void timevalfix __P((struct timeval *)); 66cb226aaaSPoul-Henning Kamp static void no_lease_updatetime __P((int)); 6794c8fcd8SPeter Wemm 681b09ae77SPoul-Henning Kamp static void 691b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat) 701b09ae77SPoul-Henning Kamp int deltat; 711b09ae77SPoul-Henning Kamp { 721b09ae77SPoul-Henning Kamp } 731b09ae77SPoul-Henning Kamp 741b09ae77SPoul-Henning Kamp void (*lease_updatetime) __P((int)) = no_lease_updatetime; 751b09ae77SPoul-Henning Kamp 7694c8fcd8SPeter Wemm static int 7794c8fcd8SPeter Wemm settime(tv) 7894c8fcd8SPeter Wemm struct timeval *tv; 7994c8fcd8SPeter Wemm { 809c8fff87SBruce Evans struct timeval delta, tv1; 817ec73f64SPoul-Henning Kamp struct timespec ts; 8294c8fcd8SPeter Wemm struct proc *p; 8394c8fcd8SPeter Wemm int s; 8494c8fcd8SPeter Wemm 85708e7684SPeter Wemm s = splclock(); 869c8fff87SBruce Evans microtime(&tv1); 8700af9731SPoul-Henning Kamp delta = *tv; 8800af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 8994c8fcd8SPeter Wemm 9094c8fcd8SPeter Wemm /* 919c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 929c8fff87SBruce Evans * set to an earlier value (it may be slowed using adjtime, 939c8fff87SBruce Evans * but not set back). This feature prevent interlopers from 949c8fff87SBruce Evans * setting arbitrary time stamps on files. 9594c8fcd8SPeter Wemm */ 969c8fff87SBruce Evans if (delta.tv_sec < 0 && securelevel > 1) { 979c8fff87SBruce Evans splx(s); 989c8fff87SBruce Evans return (EPERM); 999c8fff87SBruce Evans } 1009c8fff87SBruce Evans 1017ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1027ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 1037ec73f64SPoul-Henning Kamp set_timecounter(&ts); 10494c8fcd8SPeter Wemm (void) splsoftclock(); 10594c8fcd8SPeter Wemm lease_updatetime(delta.tv_sec); 10694c8fcd8SPeter Wemm splx(s); 10794c8fcd8SPeter Wemm resettodr(); 10894c8fcd8SPeter Wemm return (0); 10994c8fcd8SPeter Wemm } 11094c8fcd8SPeter Wemm 11194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 11294c8fcd8SPeter Wemm struct clock_gettime_args { 11394c8fcd8SPeter Wemm clockid_t clock_id; 11494c8fcd8SPeter Wemm struct timespec *tp; 11594c8fcd8SPeter Wemm }; 11694c8fcd8SPeter Wemm #endif 117708e7684SPeter Wemm 11894c8fcd8SPeter Wemm /* ARGSUSED */ 11994c8fcd8SPeter Wemm int 120cb226aaaSPoul-Henning Kamp clock_gettime(p, uap) 12194c8fcd8SPeter Wemm struct proc *p; 12294c8fcd8SPeter Wemm struct clock_gettime_args *uap; 12394c8fcd8SPeter Wemm { 12494c8fcd8SPeter Wemm struct timespec ats; 12594c8fcd8SPeter Wemm 126708e7684SPeter Wemm if (SCARG(uap, clock_id) != CLOCK_REALTIME) 12794c8fcd8SPeter Wemm return (EINVAL); 1287ec73f64SPoul-Henning Kamp nanotime(&ats); 129708e7684SPeter Wemm return (copyout(&ats, SCARG(uap, tp), sizeof(ats))); 13094c8fcd8SPeter Wemm } 13194c8fcd8SPeter Wemm 13294c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 13394c8fcd8SPeter Wemm struct clock_settime_args { 13494c8fcd8SPeter Wemm clockid_t clock_id; 13594c8fcd8SPeter Wemm const struct timespec *tp; 13694c8fcd8SPeter Wemm }; 13794c8fcd8SPeter Wemm #endif 138708e7684SPeter Wemm 13994c8fcd8SPeter Wemm /* ARGSUSED */ 14094c8fcd8SPeter Wemm int 141cb226aaaSPoul-Henning Kamp clock_settime(p, uap) 14294c8fcd8SPeter Wemm struct proc *p; 14394c8fcd8SPeter Wemm struct clock_settime_args *uap; 14494c8fcd8SPeter Wemm { 14594c8fcd8SPeter Wemm struct timeval atv; 14694c8fcd8SPeter Wemm struct timespec ats; 14794c8fcd8SPeter Wemm int error; 14894c8fcd8SPeter Wemm 14994c8fcd8SPeter Wemm if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 15094c8fcd8SPeter Wemm return (error); 151708e7684SPeter Wemm if (SCARG(uap, clock_id) != CLOCK_REALTIME) 15294c8fcd8SPeter Wemm return (EINVAL); 15394c8fcd8SPeter Wemm if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0) 15494c8fcd8SPeter Wemm return (error); 1553fc9295dSBruce Evans if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000) 156708e7684SPeter Wemm return (EINVAL); 157a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 15894c8fcd8SPeter Wemm TIMESPEC_TO_TIMEVAL(&atv, &ats); 15994c8fcd8SPeter Wemm if ((error = settime(&atv))) 16094c8fcd8SPeter Wemm return (error); 161708e7684SPeter Wemm return (0); 16294c8fcd8SPeter Wemm } 16394c8fcd8SPeter Wemm 16494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 16594c8fcd8SPeter Wemm struct clock_getres_args { 16694c8fcd8SPeter Wemm clockid_t clock_id; 16794c8fcd8SPeter Wemm struct timespec *tp; 16894c8fcd8SPeter Wemm }; 16994c8fcd8SPeter Wemm #endif 170708e7684SPeter Wemm 17194c8fcd8SPeter Wemm int 172cb226aaaSPoul-Henning Kamp clock_getres(p, uap) 17394c8fcd8SPeter Wemm struct proc *p; 17494c8fcd8SPeter Wemm struct clock_getres_args *uap; 17594c8fcd8SPeter Wemm { 17694c8fcd8SPeter Wemm struct timespec ts; 177708e7684SPeter Wemm int error; 17894c8fcd8SPeter Wemm 179708e7684SPeter Wemm if (SCARG(uap, clock_id) != CLOCK_REALTIME) 18094c8fcd8SPeter Wemm return (EINVAL); 181708e7684SPeter Wemm error = 0; 18294c8fcd8SPeter Wemm if (SCARG(uap, tp)) { 18394c8fcd8SPeter Wemm ts.tv_sec = 0; 1847ec73f64SPoul-Henning Kamp ts.tv_nsec = 1000000000 / timecounter->frequency; 18594c8fcd8SPeter Wemm error = copyout(&ts, SCARG(uap, tp), sizeof(ts)); 18694c8fcd8SPeter Wemm } 187708e7684SPeter Wemm return (error); 18894c8fcd8SPeter Wemm } 18994c8fcd8SPeter Wemm 19094c8fcd8SPeter Wemm static int nanowait; 19194c8fcd8SPeter Wemm 1925b870b7bSPeter Wemm static int 1935b870b7bSPeter Wemm nanosleep1(p, rqt, rmt) 1945b870b7bSPeter Wemm struct proc *p; 1955b870b7bSPeter Wemm struct timespec *rqt, *rmt; 1965b870b7bSPeter Wemm { 1975704ba6aSPoul-Henning Kamp struct timespec ts, ts2, ts3; 19833841826SPoul-Henning Kamp struct timeval tv; 19933841826SPoul-Henning Kamp int error; 2005b870b7bSPeter Wemm 2017d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 202708e7684SPeter Wemm return (EINVAL); 2037d7fb492SBruce Evans if (rqt->tv_sec < 0 || rqt->tv_sec == 0 && rqt->tv_nsec == 0) 2047d7fb492SBruce Evans return (0); 205c21410e1SPoul-Henning Kamp getnanouptime(&ts); 20600af9731SPoul-Henning Kamp timespecadd(&ts, rqt); 20733841826SPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, rqt); 20833841826SPoul-Henning Kamp for (;;) { 20933841826SPoul-Henning Kamp error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 21033841826SPoul-Henning Kamp tvtohz(&tv)); 211c21410e1SPoul-Henning Kamp getnanouptime(&ts2); 21233841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 21333841826SPoul-Henning Kamp if (error == ERESTART) 21494c8fcd8SPeter Wemm error = EINTR; 21533841826SPoul-Henning Kamp if (rmt != NULL) { 21633841826SPoul-Henning Kamp timespecsub(&ts, &ts2); 21733841826SPoul-Henning Kamp if (ts.tv_sec < 0) 21833841826SPoul-Henning Kamp timespecclear(&ts); 21900af9731SPoul-Henning Kamp *rmt = ts; 22000af9731SPoul-Henning Kamp } 22133841826SPoul-Henning Kamp return (error); 22233841826SPoul-Henning Kamp } 22333841826SPoul-Henning Kamp if (timespeccmp(&ts2, &ts, >=)) 22433841826SPoul-Henning Kamp return (0); 2255704ba6aSPoul-Henning Kamp ts3 = ts; 2265704ba6aSPoul-Henning Kamp timespecsub(&ts3, &ts2); 2275704ba6aSPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, &ts3); 22833841826SPoul-Henning Kamp } 2295b870b7bSPeter Wemm } 23094c8fcd8SPeter Wemm 2315b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 2325b870b7bSPeter Wemm struct nanosleep_args { 2335b870b7bSPeter Wemm struct timespec *rqtp; 2345b870b7bSPeter Wemm struct timespec *rmtp; 2355b870b7bSPeter Wemm }; 2365b870b7bSPeter Wemm #endif 2375b870b7bSPeter Wemm 2385b870b7bSPeter Wemm /* ARGSUSED */ 2395b870b7bSPeter Wemm int 240cb226aaaSPoul-Henning Kamp nanosleep(p, uap) 2415b870b7bSPeter Wemm struct proc *p; 2425b870b7bSPeter Wemm struct nanosleep_args *uap; 2435b870b7bSPeter Wemm { 2445b870b7bSPeter Wemm struct timespec rmt, rqt; 2455b870b7bSPeter Wemm int error, error2; 2465b870b7bSPeter Wemm 2475b870b7bSPeter Wemm error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt)); 2485b870b7bSPeter Wemm if (error) 2495b870b7bSPeter Wemm return (error); 250bf5acbf5SPeter Wemm if (SCARG(uap, rmtp)) 2515b870b7bSPeter Wemm if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE)) 2525b870b7bSPeter Wemm return (EFAULT); 2535b870b7bSPeter Wemm error = nanosleep1(p, &rqt, &rmt); 254d59fbbf6SPeter Wemm if (error && SCARG(uap, rmtp)) { 255708e7684SPeter Wemm error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt)); 2565b870b7bSPeter Wemm if (error2) /* XXX shouldn't happen, did useracc() above */ 257708e7684SPeter Wemm return (error2); 258708e7684SPeter Wemm } 259708e7684SPeter Wemm return (error); 26094c8fcd8SPeter Wemm } 26194c8fcd8SPeter Wemm 2625b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 263df8bae1dSRodney W. Grimes struct gettimeofday_args { 264df8bae1dSRodney W. Grimes struct timeval *tp; 265df8bae1dSRodney W. Grimes struct timezone *tzp; 266df8bae1dSRodney W. Grimes }; 267d2d3e875SBruce Evans #endif 268df8bae1dSRodney W. Grimes /* ARGSUSED */ 26926f9a767SRodney W. Grimes int 270cb226aaaSPoul-Henning Kamp gettimeofday(p, uap) 271df8bae1dSRodney W. Grimes struct proc *p; 272df8bae1dSRodney W. Grimes register struct gettimeofday_args *uap; 273df8bae1dSRodney W. Grimes { 274df8bae1dSRodney W. Grimes struct timeval atv; 275df8bae1dSRodney W. Grimes int error = 0; 276df8bae1dSRodney W. Grimes 277df8bae1dSRodney W. Grimes if (uap->tp) { 278df8bae1dSRodney W. Grimes microtime(&atv); 279bb56ec4aSPoul-Henning Kamp if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 280bb56ec4aSPoul-Henning Kamp sizeof (atv)))) 281df8bae1dSRodney W. Grimes return (error); 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes if (uap->tzp) 284df8bae1dSRodney W. Grimes error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 285df8bae1dSRodney W. Grimes sizeof (tz)); 286df8bae1dSRodney W. Grimes return (error); 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes 289d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 290df8bae1dSRodney W. Grimes struct settimeofday_args { 291df8bae1dSRodney W. Grimes struct timeval *tv; 292df8bae1dSRodney W. Grimes struct timezone *tzp; 293df8bae1dSRodney W. Grimes }; 294d2d3e875SBruce Evans #endif 295df8bae1dSRodney W. Grimes /* ARGSUSED */ 29626f9a767SRodney W. Grimes int 297cb226aaaSPoul-Henning Kamp settimeofday(p, uap) 298df8bae1dSRodney W. Grimes struct proc *p; 299df8bae1dSRodney W. Grimes struct settimeofday_args *uap; 300df8bae1dSRodney W. Grimes { 301708e7684SPeter Wemm struct timeval atv; 302df8bae1dSRodney W. Grimes struct timezone atz; 303708e7684SPeter Wemm int error; 304df8bae1dSRodney W. Grimes 305bb56ec4aSPoul-Henning Kamp if ((error = suser(p->p_ucred, &p->p_acflag))) 306df8bae1dSRodney W. Grimes return (error); 307df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 308708e7684SPeter Wemm if (uap->tv) { 309708e7684SPeter Wemm if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 310708e7684SPeter Wemm sizeof(atv)))) 311df8bae1dSRodney W. Grimes return (error); 3120808c591SBruce Evans if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) 3130808c591SBruce Evans return (EINVAL); 314708e7684SPeter Wemm } 315df8bae1dSRodney W. Grimes if (uap->tzp && 316df8bae1dSRodney W. Grimes (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz)))) 317df8bae1dSRodney W. Grimes return (error); 31894c8fcd8SPeter Wemm if (uap->tv && (error = settime(&atv))) 31994c8fcd8SPeter Wemm return (error); 320df8bae1dSRodney W. Grimes if (uap->tzp) 321df8bae1dSRodney W. Grimes tz = atz; 322df8bae1dSRodney W. Grimes return (0); 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes 325df8bae1dSRodney W. Grimes int tickdelta; /* current clock skew, us. per tick */ 326df8bae1dSRodney W. Grimes long timedelta; /* unapplied time correction, us. */ 32787b6de2bSPoul-Henning Kamp static long bigadj = 1000000; /* use 10x skew above bigadj us. */ 328df8bae1dSRodney W. Grimes 329d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 330df8bae1dSRodney W. Grimes struct adjtime_args { 331df8bae1dSRodney W. Grimes struct timeval *delta; 332df8bae1dSRodney W. Grimes struct timeval *olddelta; 333df8bae1dSRodney W. Grimes }; 334d2d3e875SBruce Evans #endif 335df8bae1dSRodney W. Grimes /* ARGSUSED */ 33626f9a767SRodney W. Grimes int 337cb226aaaSPoul-Henning Kamp adjtime(p, uap) 338df8bae1dSRodney W. Grimes struct proc *p; 339df8bae1dSRodney W. Grimes register struct adjtime_args *uap; 340df8bae1dSRodney W. Grimes { 341df8bae1dSRodney W. Grimes struct timeval atv; 342df8bae1dSRodney W. Grimes register long ndelta, ntickdelta, odelta; 343df8bae1dSRodney W. Grimes int s, error; 344df8bae1dSRodney W. Grimes 345bb56ec4aSPoul-Henning Kamp if ((error = suser(p->p_ucred, &p->p_acflag))) 346df8bae1dSRodney W. Grimes return (error); 347bb56ec4aSPoul-Henning Kamp if ((error = 348bb56ec4aSPoul-Henning Kamp copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval)))) 349df8bae1dSRodney W. Grimes return (error); 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 352df8bae1dSRodney W. Grimes * Compute the total correction and the rate at which to apply it. 353df8bae1dSRodney W. Grimes * Round the adjustment down to a whole multiple of the per-tick 354df8bae1dSRodney W. Grimes * delta, so that after some number of incremental changes in 355df8bae1dSRodney W. Grimes * hardclock(), tickdelta will become zero, lest the correction 356df8bae1dSRodney W. Grimes * overshoot and start taking us away from the desired final time. 357df8bae1dSRodney W. Grimes */ 358df8bae1dSRodney W. Grimes ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 3590cd97b20SBruce Evans if (ndelta > bigadj || ndelta < -bigadj) 360df8bae1dSRodney W. Grimes ntickdelta = 10 * tickadj; 361df8bae1dSRodney W. Grimes else 362df8bae1dSRodney W. Grimes ntickdelta = tickadj; 363df8bae1dSRodney W. Grimes if (ndelta % ntickdelta) 364df8bae1dSRodney W. Grimes ndelta = ndelta / ntickdelta * ntickdelta; 365df8bae1dSRodney W. Grimes 366df8bae1dSRodney W. Grimes /* 367df8bae1dSRodney W. Grimes * To make hardclock()'s job easier, make the per-tick delta negative 368df8bae1dSRodney W. Grimes * if we want time to run slower; then hardclock can simply compute 369df8bae1dSRodney W. Grimes * tick + tickdelta, and subtract tickdelta from timedelta. 370df8bae1dSRodney W. Grimes */ 371df8bae1dSRodney W. Grimes if (ndelta < 0) 372df8bae1dSRodney W. Grimes ntickdelta = -ntickdelta; 373df8bae1dSRodney W. Grimes s = splclock(); 374df8bae1dSRodney W. Grimes odelta = timedelta; 375df8bae1dSRodney W. Grimes timedelta = ndelta; 376df8bae1dSRodney W. Grimes tickdelta = ntickdelta; 377df8bae1dSRodney W. Grimes splx(s); 378df8bae1dSRodney W. Grimes 379df8bae1dSRodney W. Grimes if (uap->olddelta) { 380df8bae1dSRodney W. Grimes atv.tv_sec = odelta / 1000000; 381df8bae1dSRodney W. Grimes atv.tv_usec = odelta % 1000000; 382df8bae1dSRodney W. Grimes (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta, 383df8bae1dSRodney W. Grimes sizeof(struct timeval)); 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes return (0); 386df8bae1dSRodney W. Grimes } 387df8bae1dSRodney W. Grimes 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * Get value of an interval timer. The process virtual and 390df8bae1dSRodney W. Grimes * profiling virtual time timers are kept in the p_stats area, since 391df8bae1dSRodney W. Grimes * they can be swapped out. These are kept internally in the 392df8bae1dSRodney W. Grimes * way they are specified externally: in time until they expire. 393df8bae1dSRodney W. Grimes * 394df8bae1dSRodney W. Grimes * The real time interval timer is kept in the process table slot 395df8bae1dSRodney W. Grimes * for the process, and its value (it_value) is kept as an 396df8bae1dSRodney W. Grimes * absolute time rather than as a delta, so that it is easy to keep 397df8bae1dSRodney W. Grimes * periodic real-time signals from drifting. 398df8bae1dSRodney W. Grimes * 399df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 400df8bae1dSRodney W. Grimes * kern_clock.c. The real time timer is processed by a timeout 401df8bae1dSRodney W. Grimes * routine, called from the softclock() routine. Since a callout 402df8bae1dSRodney W. Grimes * may be delayed in real time due to interrupt processing in the system, 403df8bae1dSRodney W. Grimes * it is possible for the real time timeout routine (realitexpire, given below), 404df8bae1dSRodney W. Grimes * to be delayed in real time past when it is supposed to occur. It 405df8bae1dSRodney W. Grimes * does not suffice, therefore, to reload the real timer .it_value from the 406df8bae1dSRodney W. Grimes * real time timers .it_interval. Rather, we compute the next time in 407df8bae1dSRodney W. Grimes * absolute time the timer should go off. 408df8bae1dSRodney W. Grimes */ 409d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 410df8bae1dSRodney W. Grimes struct getitimer_args { 411df8bae1dSRodney W. Grimes u_int which; 412df8bae1dSRodney W. Grimes struct itimerval *itv; 413df8bae1dSRodney W. Grimes }; 414d2d3e875SBruce Evans #endif 415df8bae1dSRodney W. Grimes /* ARGSUSED */ 41626f9a767SRodney W. Grimes int 417cb226aaaSPoul-Henning Kamp getitimer(p, uap) 418df8bae1dSRodney W. Grimes struct proc *p; 419df8bae1dSRodney W. Grimes register struct getitimer_args *uap; 420df8bae1dSRodney W. Grimes { 421227ee8a1SPoul-Henning Kamp struct timeval ctv; 422df8bae1dSRodney W. Grimes struct itimerval aitv; 423df8bae1dSRodney W. Grimes int s; 424df8bae1dSRodney W. Grimes 425df8bae1dSRodney W. Grimes if (uap->which > ITIMER_PROF) 426df8bae1dSRodney W. Grimes return (EINVAL); 427227ee8a1SPoul-Henning Kamp s = splclock(); /* XXX still needed ? */ 428df8bae1dSRodney W. Grimes if (uap->which == ITIMER_REAL) { 429df8bae1dSRodney W. Grimes /* 430df8bae1dSRodney W. Grimes * Convert from absoulte to relative time in .it_value 431df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 432df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 433df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 434df8bae1dSRodney W. Grimes */ 435df8bae1dSRodney W. Grimes aitv = p->p_realtimer; 4364cf41af3SPoul-Henning Kamp if (timevalisset(&aitv.it_value)) { 437c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 4384cf41af3SPoul-Henning Kamp if (timevalcmp(&aitv.it_value, &ctv, <)) 4394cf41af3SPoul-Henning Kamp timevalclear(&aitv.it_value); 440df8bae1dSRodney W. Grimes else 441227ee8a1SPoul-Henning Kamp timevalsub(&aitv.it_value, &ctv); 442227ee8a1SPoul-Henning Kamp } 443df8bae1dSRodney W. Grimes } else 444df8bae1dSRodney W. Grimes aitv = p->p_stats->p_timer[uap->which]; 445df8bae1dSRodney W. Grimes splx(s); 446df8bae1dSRodney W. Grimes return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, 447df8bae1dSRodney W. Grimes sizeof (struct itimerval))); 448df8bae1dSRodney W. Grimes } 449df8bae1dSRodney W. Grimes 450d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 451df8bae1dSRodney W. Grimes struct setitimer_args { 452df8bae1dSRodney W. Grimes u_int which; 453df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 454df8bae1dSRodney W. Grimes }; 455d2d3e875SBruce Evans #endif 456df8bae1dSRodney W. Grimes /* ARGSUSED */ 45726f9a767SRodney W. Grimes int 458cb226aaaSPoul-Henning Kamp setitimer(p, uap) 459df8bae1dSRodney W. Grimes struct proc *p; 460df8bae1dSRodney W. Grimes register struct setitimer_args *uap; 461df8bae1dSRodney W. Grimes { 462df8bae1dSRodney W. Grimes struct itimerval aitv; 463227ee8a1SPoul-Henning Kamp struct timeval ctv; 464df8bae1dSRodney W. Grimes register struct itimerval *itvp; 465df8bae1dSRodney W. Grimes int s, error; 466df8bae1dSRodney W. Grimes 467df8bae1dSRodney W. Grimes if (uap->which > ITIMER_PROF) 468df8bae1dSRodney W. Grimes return (EINVAL); 469df8bae1dSRodney W. Grimes itvp = uap->itv; 470df8bae1dSRodney W. Grimes if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 471df8bae1dSRodney W. Grimes sizeof(struct itimerval)))) 472df8bae1dSRodney W. Grimes return (error); 4730808c591SBruce Evans if ((uap->itv = uap->oitv) && 474cb226aaaSPoul-Henning Kamp (error = getitimer(p, (struct getitimer_args *)uap))) 475df8bae1dSRodney W. Grimes return (error); 476df8bae1dSRodney W. Grimes if (itvp == 0) 477df8bae1dSRodney W. Grimes return (0); 478cd9f713dSAndrey A. Chernov if (itimerfix(&aitv.it_value)) 479cd9f713dSAndrey A. Chernov return (EINVAL); 4804cf41af3SPoul-Henning Kamp if (!timevalisset(&aitv.it_value)) 4814cf41af3SPoul-Henning Kamp timevalclear(&aitv.it_interval); 482cd9f713dSAndrey A. Chernov else if (itimerfix(&aitv.it_interval)) 483df8bae1dSRodney W. Grimes return (EINVAL); 484227ee8a1SPoul-Henning Kamp s = splclock(); /* XXX: still needed ? */ 485df8bae1dSRodney W. Grimes if (uap->which == ITIMER_REAL) { 4864cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 487ab36c067SJustin T. Gibbs untimeout(realitexpire, (caddr_t)p, p->p_ithandle); 4884cf41af3SPoul-Henning Kamp if (timevalisset(&aitv.it_value)) 489ab36c067SJustin T. Gibbs p->p_ithandle = timeout(realitexpire, (caddr_t)p, 49091ad39c6SPoul-Henning Kamp tvtohz(&aitv.it_value)); 491c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 492bfe6c9faSPoul-Henning Kamp timevaladd(&aitv.it_value, &ctv); 493df8bae1dSRodney W. Grimes p->p_realtimer = aitv; 494df8bae1dSRodney W. Grimes } else 495df8bae1dSRodney W. Grimes p->p_stats->p_timer[uap->which] = aitv; 496df8bae1dSRodney W. Grimes splx(s); 497df8bae1dSRodney W. Grimes return (0); 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes 500df8bae1dSRodney W. Grimes /* 501df8bae1dSRodney W. Grimes * Real interval timer expired: 502df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 503df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 504df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 505df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 506df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 507c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 5089207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 5099207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 5109207f00aSBruce Evans * interrupt even when we're delayed. 511df8bae1dSRodney W. Grimes */ 512df8bae1dSRodney W. Grimes void 513df8bae1dSRodney W. Grimes realitexpire(arg) 514df8bae1dSRodney W. Grimes void *arg; 515df8bae1dSRodney W. Grimes { 516df8bae1dSRodney W. Grimes register struct proc *p; 517bfe6c9faSPoul-Henning Kamp struct timeval ctv, ntv; 518df8bae1dSRodney W. Grimes int s; 519df8bae1dSRodney W. Grimes 520df8bae1dSRodney W. Grimes p = (struct proc *)arg; 521df8bae1dSRodney W. Grimes psignal(p, SIGALRM); 5224cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 5234cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 524df8bae1dSRodney W. Grimes return; 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes for (;;) { 527227ee8a1SPoul-Henning Kamp s = splclock(); /* XXX: still neeeded ? */ 528df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 529df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 530c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 5314cf41af3SPoul-Henning Kamp if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 532bfe6c9faSPoul-Henning Kamp ntv = p->p_realtimer.it_value; 533bfe6c9faSPoul-Henning Kamp timevalsub(&ntv, &ctv); 534ab36c067SJustin T. Gibbs p->p_ithandle = 535bfe6c9faSPoul-Henning Kamp timeout(realitexpire, (caddr_t)p, tvtohz(&ntv)); 536df8bae1dSRodney W. Grimes splx(s); 537df8bae1dSRodney W. Grimes return; 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes splx(s); 540df8bae1dSRodney W. Grimes } 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes /* 544df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 545df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 546df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 547df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 548df8bae1dSRodney W. Grimes */ 54926f9a767SRodney W. Grimes int 550df8bae1dSRodney W. Grimes itimerfix(tv) 551df8bae1dSRodney W. Grimes struct timeval *tv; 552df8bae1dSRodney W. Grimes { 553df8bae1dSRodney W. Grimes 554df8bae1dSRodney W. Grimes if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 555df8bae1dSRodney W. Grimes tv->tv_usec < 0 || tv->tv_usec >= 1000000) 556df8bae1dSRodney W. Grimes return (EINVAL); 557df8bae1dSRodney W. Grimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 558df8bae1dSRodney W. Grimes tv->tv_usec = tick; 559df8bae1dSRodney W. Grimes return (0); 560df8bae1dSRodney W. Grimes } 561df8bae1dSRodney W. Grimes 562df8bae1dSRodney W. Grimes /* 563df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 564df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 565df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 566df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 567df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 568df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 569df8bae1dSRodney W. Grimes * that it is called in a context where the timers 570df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 571df8bae1dSRodney W. Grimes */ 57226f9a767SRodney W. Grimes int 573df8bae1dSRodney W. Grimes itimerdecr(itp, usec) 574df8bae1dSRodney W. Grimes register struct itimerval *itp; 575df8bae1dSRodney W. Grimes int usec; 576df8bae1dSRodney W. Grimes { 577df8bae1dSRodney W. Grimes 578df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 579df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 580df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 581df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 582df8bae1dSRodney W. Grimes goto expire; 583df8bae1dSRodney W. Grimes } 584df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 585df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 586df8bae1dSRodney W. Grimes } 587df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 588df8bae1dSRodney W. Grimes usec = 0; 5894cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 590df8bae1dSRodney W. Grimes return (1); 591df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 592df8bae1dSRodney W. Grimes expire: 5934cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 594df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 595df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 596df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 597df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 598df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 599df8bae1dSRodney W. Grimes } 600df8bae1dSRodney W. Grimes } else 601df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 602df8bae1dSRodney W. Grimes return (0); 603df8bae1dSRodney W. Grimes } 604df8bae1dSRodney W. Grimes 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 607df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 608df8bae1dSRodney W. Grimes * results which are before the beginning, 609df8bae1dSRodney W. Grimes * it just gets very confused in this case. 610df8bae1dSRodney W. Grimes * Caveat emptor. 611df8bae1dSRodney W. Grimes */ 61226f9a767SRodney W. Grimes void 613df8bae1dSRodney W. Grimes timevaladd(t1, t2) 614df8bae1dSRodney W. Grimes struct timeval *t1, *t2; 615df8bae1dSRodney W. Grimes { 616df8bae1dSRodney W. Grimes 617df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 618df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 619df8bae1dSRodney W. Grimes timevalfix(t1); 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes 62226f9a767SRodney W. Grimes void 623df8bae1dSRodney W. Grimes timevalsub(t1, t2) 624df8bae1dSRodney W. Grimes struct timeval *t1, *t2; 625df8bae1dSRodney W. Grimes { 626df8bae1dSRodney W. Grimes 627df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 628df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 629df8bae1dSRodney W. Grimes timevalfix(t1); 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes 63287b6de2bSPoul-Henning Kamp static void 633df8bae1dSRodney W. Grimes timevalfix(t1) 634df8bae1dSRodney W. Grimes struct timeval *t1; 635df8bae1dSRodney W. Grimes { 636df8bae1dSRodney W. Grimes 637df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 638df8bae1dSRodney W. Grimes t1->tv_sec--; 639df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 642df8bae1dSRodney W. Grimes t1->tv_sec++; 643df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 644df8bae1dSRodney W. Grimes } 645df8bae1dSRodney W. Grimes } 646