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 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38e96c1fdcSPeter Wemm #include <sys/systm.h> 39fb919e4dSMark Murray #include <sys/lock.h> 40fb919e4dSMark Murray #include <sys/mutex.h> 41d2d3e875SBruce Evans #include <sys/sysproto.h> 42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 44df8bae1dSRodney W. Grimes #include <sys/kernel.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 4694c8fcd8SPeter Wemm #include <sys/sysent.h> 47df8bae1dSRodney W. Grimes #include <sys/proc.h> 48708e7684SPeter Wemm #include <sys/time.h> 4991266b96SPoul-Henning Kamp #include <sys/timetc.h> 50df8bae1dSRodney W. Grimes #include <sys/vnode.h> 51fb919e4dSMark Murray 525b870b7bSPeter Wemm #include <vm/vm.h> 535b870b7bSPeter Wemm #include <vm/vm_extern.h> 54df8bae1dSRodney W. Grimes 55ac7e6123SDavid Greenman struct timezone tz; 56ac7e6123SDavid Greenman 57df8bae1dSRodney W. Grimes /* 58df8bae1dSRodney W. Grimes * Time of day and interval timer support. 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 61df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 62df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 63df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 64df8bae1dSRodney W. Grimes * timers when they expire. 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67b40ce416SJulian Elischer static int nanosleep1 __P((struct thread *td, struct timespec *rqt, 685b870b7bSPeter Wemm struct timespec *rmt)); 69d501d04bSRobert Watson static int settime __P((struct proc *, struct timeval *)); 707d7fb492SBruce Evans static void timevalfix __P((struct timeval *)); 71cb226aaaSPoul-Henning Kamp static void no_lease_updatetime __P((int)); 7294c8fcd8SPeter Wemm 731b09ae77SPoul-Henning Kamp static void 741b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat) 751b09ae77SPoul-Henning Kamp int deltat; 761b09ae77SPoul-Henning Kamp { 771b09ae77SPoul-Henning Kamp } 781b09ae77SPoul-Henning Kamp 791b09ae77SPoul-Henning Kamp void (*lease_updatetime) __P((int)) = no_lease_updatetime; 801b09ae77SPoul-Henning Kamp 8194c8fcd8SPeter Wemm static int 82d501d04bSRobert Watson settime(p, tv) 83d501d04bSRobert Watson struct proc *p; 8494c8fcd8SPeter Wemm struct timeval *tv; 8594c8fcd8SPeter Wemm { 86fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 87c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 887ec73f64SPoul-Henning Kamp struct timespec ts; 8994c8fcd8SPeter Wemm int s; 9094c8fcd8SPeter Wemm 91708e7684SPeter Wemm s = splclock(); 929c8fff87SBruce Evans microtime(&tv1); 9300af9731SPoul-Henning Kamp delta = *tv; 9400af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 9594c8fcd8SPeter Wemm 9694c8fcd8SPeter Wemm /* 979c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 98fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 99fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 100fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 101fcae3aa6SNick Sayer * back to the past. 102c0bd94a7SNick Sayer * 103c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 104c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 105c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 10694c8fcd8SPeter Wemm */ 107d501d04bSRobert Watson if (securelevel_gt(p->p_ucred, 1) != 0) { 108fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1093f92429aSMatt Jacob /* 110c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1113f92429aSMatt Jacob */ 112fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 113fcae3aa6SNick Sayer maxtime = tv1; 114fcae3aa6SNick Sayer tv2 = *tv; 115fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 116fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1173f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 118fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 119fcae3aa6SNick Sayer } 1203f92429aSMatt Jacob } else { 121c0bd94a7SNick Sayer if (tv1.tv_sec == laststep.tv_sec) { 122c0bd94a7SNick Sayer splx(s); 123c0bd94a7SNick Sayer return (EPERM); 124c0bd94a7SNick Sayer } 125c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 126c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 127c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 128c0bd94a7SNick Sayer } 129c0bd94a7SNick Sayer laststep = *tv; 130fcae3aa6SNick Sayer } 1319c8fff87SBruce Evans } 1329c8fff87SBruce Evans 1337ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1347ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 13591266b96SPoul-Henning Kamp tc_setclock(&ts); 13694c8fcd8SPeter Wemm (void) splsoftclock(); 13794c8fcd8SPeter Wemm lease_updatetime(delta.tv_sec); 13894c8fcd8SPeter Wemm splx(s); 13994c8fcd8SPeter Wemm resettodr(); 14094c8fcd8SPeter Wemm return (0); 14194c8fcd8SPeter Wemm } 14294c8fcd8SPeter Wemm 14394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 14494c8fcd8SPeter Wemm struct clock_gettime_args { 14594c8fcd8SPeter Wemm clockid_t clock_id; 14694c8fcd8SPeter Wemm struct timespec *tp; 14794c8fcd8SPeter Wemm }; 14894c8fcd8SPeter Wemm #endif 149708e7684SPeter Wemm 150fb99ab88SMatthew Dillon /* 151fb99ab88SMatthew Dillon * MPSAFE 152fb99ab88SMatthew Dillon */ 15394c8fcd8SPeter Wemm /* ARGSUSED */ 15494c8fcd8SPeter Wemm int 155b40ce416SJulian Elischer clock_gettime(td, uap) 156b40ce416SJulian Elischer struct thread *td; 15794c8fcd8SPeter Wemm struct clock_gettime_args *uap; 15894c8fcd8SPeter Wemm { 15994c8fcd8SPeter Wemm struct timespec ats; 16094c8fcd8SPeter Wemm 161708e7684SPeter Wemm if (SCARG(uap, clock_id) != CLOCK_REALTIME) 16294c8fcd8SPeter Wemm return (EINVAL); 163fb99ab88SMatthew Dillon mtx_lock(&Giant); 1647ec73f64SPoul-Henning Kamp nanotime(&ats); 165fb99ab88SMatthew Dillon mtx_unlock(&Giant); 166708e7684SPeter Wemm return (copyout(&ats, SCARG(uap, tp), sizeof(ats))); 16794c8fcd8SPeter Wemm } 16894c8fcd8SPeter Wemm 16994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 17094c8fcd8SPeter Wemm struct clock_settime_args { 17194c8fcd8SPeter Wemm clockid_t clock_id; 17294c8fcd8SPeter Wemm const struct timespec *tp; 17394c8fcd8SPeter Wemm }; 17494c8fcd8SPeter Wemm #endif 175708e7684SPeter Wemm 176fb99ab88SMatthew Dillon /* 177fb99ab88SMatthew Dillon * MPSAFE 178fb99ab88SMatthew Dillon */ 17994c8fcd8SPeter Wemm /* ARGSUSED */ 18094c8fcd8SPeter Wemm int 181b40ce416SJulian Elischer clock_settime(td, uap) 182b40ce416SJulian Elischer struct thread *td; 18394c8fcd8SPeter Wemm struct clock_settime_args *uap; 18494c8fcd8SPeter Wemm { 18594c8fcd8SPeter Wemm struct timeval atv; 18694c8fcd8SPeter Wemm struct timespec ats; 18794c8fcd8SPeter Wemm int error; 18894c8fcd8SPeter Wemm 189fb99ab88SMatthew Dillon mtx_lock(&Giant); 190b40ce416SJulian Elischer if ((error = suser_td(td)) != 0) 191fb99ab88SMatthew Dillon goto done2; 192fb99ab88SMatthew Dillon if (SCARG(uap, clock_id) != CLOCK_REALTIME) { 193fb99ab88SMatthew Dillon error = EINVAL; 194fb99ab88SMatthew Dillon goto done2; 195fb99ab88SMatthew Dillon } 19694c8fcd8SPeter Wemm if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0) 197fb99ab88SMatthew Dillon goto done2; 198fb99ab88SMatthew Dillon if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000) { 199fb99ab88SMatthew Dillon error = EINVAL; 200fb99ab88SMatthew Dillon goto done2; 201fb99ab88SMatthew Dillon } 202a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 20394c8fcd8SPeter Wemm TIMESPEC_TO_TIMEVAL(&atv, &ats); 204d501d04bSRobert Watson error = settime(td->td_proc, &atv); 205fb99ab88SMatthew Dillon done2: 206fb99ab88SMatthew Dillon mtx_unlock(&Giant); 20794c8fcd8SPeter Wemm return (error); 20894c8fcd8SPeter Wemm } 20994c8fcd8SPeter Wemm 21094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 21194c8fcd8SPeter Wemm struct clock_getres_args { 21294c8fcd8SPeter Wemm clockid_t clock_id; 21394c8fcd8SPeter Wemm struct timespec *tp; 21494c8fcd8SPeter Wemm }; 21594c8fcd8SPeter Wemm #endif 216708e7684SPeter Wemm 21794c8fcd8SPeter Wemm int 218b40ce416SJulian Elischer clock_getres(td, uap) 219b40ce416SJulian Elischer struct thread *td; 22094c8fcd8SPeter Wemm struct clock_getres_args *uap; 22194c8fcd8SPeter Wemm { 22294c8fcd8SPeter Wemm struct timespec ts; 223708e7684SPeter Wemm int error; 22494c8fcd8SPeter Wemm 225708e7684SPeter Wemm if (SCARG(uap, clock_id) != CLOCK_REALTIME) 22694c8fcd8SPeter Wemm return (EINVAL); 227708e7684SPeter Wemm error = 0; 22894c8fcd8SPeter Wemm if (SCARG(uap, tp)) { 22994c8fcd8SPeter Wemm ts.tv_sec = 0; 230a58f0f8eSPoul-Henning Kamp ts.tv_nsec = 1000000000 / timecounter->tc_frequency; 23194c8fcd8SPeter Wemm error = copyout(&ts, SCARG(uap, tp), sizeof(ts)); 23294c8fcd8SPeter Wemm } 233708e7684SPeter Wemm return (error); 23494c8fcd8SPeter Wemm } 23594c8fcd8SPeter Wemm 23694c8fcd8SPeter Wemm static int nanowait; 23794c8fcd8SPeter Wemm 2385b870b7bSPeter Wemm static int 239b40ce416SJulian Elischer nanosleep1(td, rqt, rmt) 240b40ce416SJulian Elischer struct thread *td; 2415b870b7bSPeter Wemm struct timespec *rqt, *rmt; 2425b870b7bSPeter Wemm { 2435704ba6aSPoul-Henning Kamp struct timespec ts, ts2, ts3; 24433841826SPoul-Henning Kamp struct timeval tv; 24533841826SPoul-Henning Kamp int error; 2465b870b7bSPeter Wemm 2477d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 248708e7684SPeter Wemm return (EINVAL); 249d254af07SMatthew Dillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 2507d7fb492SBruce Evans return (0); 251c21410e1SPoul-Henning Kamp getnanouptime(&ts); 25200af9731SPoul-Henning Kamp timespecadd(&ts, rqt); 25333841826SPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, rqt); 25433841826SPoul-Henning Kamp for (;;) { 25533841826SPoul-Henning Kamp error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 25633841826SPoul-Henning Kamp tvtohz(&tv)); 257c21410e1SPoul-Henning Kamp getnanouptime(&ts2); 25833841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 25933841826SPoul-Henning Kamp if (error == ERESTART) 26094c8fcd8SPeter Wemm error = EINTR; 26133841826SPoul-Henning Kamp if (rmt != NULL) { 26233841826SPoul-Henning Kamp timespecsub(&ts, &ts2); 26333841826SPoul-Henning Kamp if (ts.tv_sec < 0) 26433841826SPoul-Henning Kamp timespecclear(&ts); 26500af9731SPoul-Henning Kamp *rmt = ts; 26600af9731SPoul-Henning Kamp } 26733841826SPoul-Henning Kamp return (error); 26833841826SPoul-Henning Kamp } 26933841826SPoul-Henning Kamp if (timespeccmp(&ts2, &ts, >=)) 27033841826SPoul-Henning Kamp return (0); 2715704ba6aSPoul-Henning Kamp ts3 = ts; 2725704ba6aSPoul-Henning Kamp timespecsub(&ts3, &ts2); 2735704ba6aSPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, &ts3); 27433841826SPoul-Henning Kamp } 2755b870b7bSPeter Wemm } 27694c8fcd8SPeter Wemm 2775b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 2785b870b7bSPeter Wemm struct nanosleep_args { 2795b870b7bSPeter Wemm struct timespec *rqtp; 2805b870b7bSPeter Wemm struct timespec *rmtp; 2815b870b7bSPeter Wemm }; 2825b870b7bSPeter Wemm #endif 2835b870b7bSPeter Wemm 284fb99ab88SMatthew Dillon /* 285fb99ab88SMatthew Dillon * MPSAFE 286fb99ab88SMatthew Dillon */ 2875b870b7bSPeter Wemm /* ARGSUSED */ 2885b870b7bSPeter Wemm int 289b40ce416SJulian Elischer nanosleep(td, uap) 290b40ce416SJulian Elischer struct thread *td; 2915b870b7bSPeter Wemm struct nanosleep_args *uap; 2925b870b7bSPeter Wemm { 2935b870b7bSPeter Wemm struct timespec rmt, rqt; 294fb99ab88SMatthew Dillon int error; 2955b870b7bSPeter Wemm 2965b870b7bSPeter Wemm error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt)); 2975b870b7bSPeter Wemm if (error) 2985b870b7bSPeter Wemm return (error); 299fb99ab88SMatthew Dillon 300fb99ab88SMatthew Dillon mtx_lock(&Giant); 301fb99ab88SMatthew Dillon if (SCARG(uap, rmtp)) { 30202c58685SPoul-Henning Kamp if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), 303fb99ab88SMatthew Dillon VM_PROT_WRITE)) { 304fb99ab88SMatthew Dillon error = EFAULT; 305fb99ab88SMatthew Dillon goto done2; 306fb99ab88SMatthew Dillon } 307fb99ab88SMatthew Dillon } 308b40ce416SJulian Elischer error = nanosleep1(td, &rqt, &rmt); 309d59fbbf6SPeter Wemm if (error && SCARG(uap, rmtp)) { 310fb99ab88SMatthew Dillon int error2; 311fb99ab88SMatthew Dillon 312708e7684SPeter Wemm error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt)); 3135b870b7bSPeter Wemm if (error2) /* XXX shouldn't happen, did useracc() above */ 314fb99ab88SMatthew Dillon error = error2; 315708e7684SPeter Wemm } 316fb99ab88SMatthew Dillon done2: 317fb99ab88SMatthew Dillon mtx_unlock(&Giant); 318708e7684SPeter Wemm return (error); 31994c8fcd8SPeter Wemm } 32094c8fcd8SPeter Wemm 3215b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 322df8bae1dSRodney W. Grimes struct gettimeofday_args { 323df8bae1dSRodney W. Grimes struct timeval *tp; 324df8bae1dSRodney W. Grimes struct timezone *tzp; 325df8bae1dSRodney W. Grimes }; 326d2d3e875SBruce Evans #endif 327fb99ab88SMatthew Dillon /* 328fb99ab88SMatthew Dillon * MPSAFE 329fb99ab88SMatthew Dillon */ 330df8bae1dSRodney W. Grimes /* ARGSUSED */ 33126f9a767SRodney W. Grimes int 332b40ce416SJulian Elischer gettimeofday(td, uap) 333b40ce416SJulian Elischer struct thread *td; 334df8bae1dSRodney W. Grimes register struct gettimeofday_args *uap; 335df8bae1dSRodney W. Grimes { 336df8bae1dSRodney W. Grimes struct timeval atv; 337df8bae1dSRodney W. Grimes int error = 0; 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes if (uap->tp) { 340df8bae1dSRodney W. Grimes microtime(&atv); 34121dcdb38SPoul-Henning Kamp error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv)); 342df8bae1dSRodney W. Grimes } 34321dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 34421dcdb38SPoul-Henning Kamp mtx_lock(&Giant); 345df8bae1dSRodney W. Grimes error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 346df8bae1dSRodney W. Grimes sizeof (tz)); 347fb99ab88SMatthew Dillon mtx_unlock(&Giant); 34821dcdb38SPoul-Henning Kamp } 349df8bae1dSRodney W. Grimes return (error); 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes 352d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 353df8bae1dSRodney W. Grimes struct settimeofday_args { 354df8bae1dSRodney W. Grimes struct timeval *tv; 355df8bae1dSRodney W. Grimes struct timezone *tzp; 356df8bae1dSRodney W. Grimes }; 357d2d3e875SBruce Evans #endif 358fb99ab88SMatthew Dillon /* 359fb99ab88SMatthew Dillon * MPSAFE 360fb99ab88SMatthew Dillon */ 361df8bae1dSRodney W. Grimes /* ARGSUSED */ 36226f9a767SRodney W. Grimes int 363b40ce416SJulian Elischer settimeofday(td, uap) 364b40ce416SJulian Elischer struct thread *td; 365df8bae1dSRodney W. Grimes struct settimeofday_args *uap; 366df8bae1dSRodney W. Grimes { 367708e7684SPeter Wemm struct timeval atv; 368df8bae1dSRodney W. Grimes struct timezone atz; 369fb99ab88SMatthew Dillon int error = 0; 370fb99ab88SMatthew Dillon 371fb99ab88SMatthew Dillon mtx_lock(&Giant); 372df8bae1dSRodney W. Grimes 373b40ce416SJulian Elischer if ((error = suser_td(td))) 374fb99ab88SMatthew Dillon goto done2; 375df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 376708e7684SPeter Wemm if (uap->tv) { 377708e7684SPeter Wemm if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 378fb99ab88SMatthew Dillon sizeof(atv)))) { 379fb99ab88SMatthew Dillon goto done2; 380fb99ab88SMatthew Dillon } 381fb99ab88SMatthew Dillon if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) { 382fb99ab88SMatthew Dillon error = EINVAL; 383fb99ab88SMatthew Dillon goto done2; 384fb99ab88SMatthew Dillon } 385708e7684SPeter Wemm } 386df8bae1dSRodney W. Grimes if (uap->tzp && 387fb99ab88SMatthew Dillon (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz)))) { 388fb99ab88SMatthew Dillon goto done2; 389fb99ab88SMatthew Dillon } 390d501d04bSRobert Watson if (uap->tv && (error = settime(td->td_proc, &atv))) 391fb99ab88SMatthew Dillon goto done2; 392df8bae1dSRodney W. Grimes if (uap->tzp) 393df8bae1dSRodney W. Grimes tz = atz; 394fb99ab88SMatthew Dillon done2: 395fb99ab88SMatthew Dillon mtx_unlock(&Giant); 396fb99ab88SMatthew Dillon return (error); 397df8bae1dSRodney W. Grimes } 398df8bae1dSRodney W. Grimes 399df8bae1dSRodney W. Grimes int tickdelta; /* current clock skew, us. per tick */ 400df8bae1dSRodney W. Grimes long timedelta; /* unapplied time correction, us. */ 40187b6de2bSPoul-Henning Kamp static long bigadj = 1000000; /* use 10x skew above bigadj us. */ 402df8bae1dSRodney W. Grimes 403d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 404df8bae1dSRodney W. Grimes struct adjtime_args { 405df8bae1dSRodney W. Grimes struct timeval *delta; 406df8bae1dSRodney W. Grimes struct timeval *olddelta; 407df8bae1dSRodney W. Grimes }; 408d2d3e875SBruce Evans #endif 409fb99ab88SMatthew Dillon /* 410fb99ab88SMatthew Dillon * MPSAFE 411fb99ab88SMatthew Dillon */ 412df8bae1dSRodney W. Grimes /* ARGSUSED */ 41326f9a767SRodney W. Grimes int 414b40ce416SJulian Elischer adjtime(td, uap) 415b40ce416SJulian Elischer struct thread *td; 416df8bae1dSRodney W. Grimes register struct adjtime_args *uap; 417df8bae1dSRodney W. Grimes { 418df8bae1dSRodney W. Grimes struct timeval atv; 419df8bae1dSRodney W. Grimes register long ndelta, ntickdelta, odelta; 420df8bae1dSRodney W. Grimes int s, error; 421df8bae1dSRodney W. Grimes 422fb99ab88SMatthew Dillon mtx_lock(&Giant); 423fb99ab88SMatthew Dillon 424b40ce416SJulian Elischer if ((error = suser_td(td))) 425fb99ab88SMatthew Dillon goto done2; 426fb99ab88SMatthew Dillon error = copyin((caddr_t)uap->delta, (caddr_t)&atv, 427fb99ab88SMatthew Dillon sizeof(struct timeval)); 428fb99ab88SMatthew Dillon if (error) 429fb99ab88SMatthew Dillon goto done2; 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes /* 432df8bae1dSRodney W. Grimes * Compute the total correction and the rate at which to apply it. 433df8bae1dSRodney W. Grimes * Round the adjustment down to a whole multiple of the per-tick 434df8bae1dSRodney W. Grimes * delta, so that after some number of incremental changes in 435df8bae1dSRodney W. Grimes * hardclock(), tickdelta will become zero, lest the correction 436df8bae1dSRodney W. Grimes * overshoot and start taking us away from the desired final time. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 4390cd97b20SBruce Evans if (ndelta > bigadj || ndelta < -bigadj) 440df8bae1dSRodney W. Grimes ntickdelta = 10 * tickadj; 441df8bae1dSRodney W. Grimes else 442df8bae1dSRodney W. Grimes ntickdelta = tickadj; 443df8bae1dSRodney W. Grimes if (ndelta % ntickdelta) 444df8bae1dSRodney W. Grimes ndelta = ndelta / ntickdelta * ntickdelta; 445df8bae1dSRodney W. Grimes 446df8bae1dSRodney W. Grimes /* 447df8bae1dSRodney W. Grimes * To make hardclock()'s job easier, make the per-tick delta negative 448df8bae1dSRodney W. Grimes * if we want time to run slower; then hardclock can simply compute 449df8bae1dSRodney W. Grimes * tick + tickdelta, and subtract tickdelta from timedelta. 450df8bae1dSRodney W. Grimes */ 451df8bae1dSRodney W. Grimes if (ndelta < 0) 452df8bae1dSRodney W. Grimes ntickdelta = -ntickdelta; 453df8bae1dSRodney W. Grimes s = splclock(); 454df8bae1dSRodney W. Grimes odelta = timedelta; 455df8bae1dSRodney W. Grimes timedelta = ndelta; 456df8bae1dSRodney W. Grimes tickdelta = ntickdelta; 457df8bae1dSRodney W. Grimes splx(s); 458df8bae1dSRodney W. Grimes 459df8bae1dSRodney W. Grimes if (uap->olddelta) { 460df8bae1dSRodney W. Grimes atv.tv_sec = odelta / 1000000; 461df8bae1dSRodney W. Grimes atv.tv_usec = odelta % 1000000; 462df8bae1dSRodney W. Grimes (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta, 463df8bae1dSRodney W. Grimes sizeof(struct timeval)); 464df8bae1dSRodney W. Grimes } 465fb99ab88SMatthew Dillon done2: 466fb99ab88SMatthew Dillon mtx_unlock(&Giant); 467fb99ab88SMatthew Dillon return (error); 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes 470df8bae1dSRodney W. Grimes /* 471df8bae1dSRodney W. Grimes * Get value of an interval timer. The process virtual and 472df8bae1dSRodney W. Grimes * profiling virtual time timers are kept in the p_stats area, since 473df8bae1dSRodney W. Grimes * they can be swapped out. These are kept internally in the 474df8bae1dSRodney W. Grimes * way they are specified externally: in time until they expire. 475df8bae1dSRodney W. Grimes * 476df8bae1dSRodney W. Grimes * The real time interval timer is kept in the process table slot 477df8bae1dSRodney W. Grimes * for the process, and its value (it_value) is kept as an 478df8bae1dSRodney W. Grimes * absolute time rather than as a delta, so that it is easy to keep 479df8bae1dSRodney W. Grimes * periodic real-time signals from drifting. 480df8bae1dSRodney W. Grimes * 481df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 482df8bae1dSRodney W. Grimes * kern_clock.c. The real time timer is processed by a timeout 483df8bae1dSRodney W. Grimes * routine, called from the softclock() routine. Since a callout 484df8bae1dSRodney W. Grimes * may be delayed in real time due to interrupt processing in the system, 485df8bae1dSRodney W. Grimes * it is possible for the real time timeout routine (realitexpire, given below), 486df8bae1dSRodney W. Grimes * to be delayed in real time past when it is supposed to occur. It 487df8bae1dSRodney W. Grimes * does not suffice, therefore, to reload the real timer .it_value from the 488df8bae1dSRodney W. Grimes * real time timers .it_interval. Rather, we compute the next time in 489df8bae1dSRodney W. Grimes * absolute time the timer should go off. 490df8bae1dSRodney W. Grimes */ 491d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 492df8bae1dSRodney W. Grimes struct getitimer_args { 493df8bae1dSRodney W. Grimes u_int which; 494df8bae1dSRodney W. Grimes struct itimerval *itv; 495df8bae1dSRodney W. Grimes }; 496d2d3e875SBruce Evans #endif 497fb99ab88SMatthew Dillon /* 498fb99ab88SMatthew Dillon * MPSAFE 499fb99ab88SMatthew Dillon */ 500df8bae1dSRodney W. Grimes /* ARGSUSED */ 50126f9a767SRodney W. Grimes int 502b40ce416SJulian Elischer getitimer(td, uap) 503b40ce416SJulian Elischer struct thread *td; 504df8bae1dSRodney W. Grimes register struct getitimer_args *uap; 505df8bae1dSRodney W. Grimes { 506b40ce416SJulian Elischer struct proc *p = td->td_proc; 507227ee8a1SPoul-Henning Kamp struct timeval ctv; 508df8bae1dSRodney W. Grimes struct itimerval aitv; 509df8bae1dSRodney W. Grimes int s; 510fb99ab88SMatthew Dillon int error; 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes if (uap->which > ITIMER_PROF) 513df8bae1dSRodney W. Grimes return (EINVAL); 514fb99ab88SMatthew Dillon 515fb99ab88SMatthew Dillon mtx_lock(&Giant); 516fb99ab88SMatthew Dillon 517227ee8a1SPoul-Henning Kamp s = splclock(); /* XXX still needed ? */ 518df8bae1dSRodney W. Grimes if (uap->which == ITIMER_REAL) { 519df8bae1dSRodney W. Grimes /* 520ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 521df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 522df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 523df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 524df8bae1dSRodney W. Grimes */ 525df8bae1dSRodney W. Grimes aitv = p->p_realtimer; 5264cf41af3SPoul-Henning Kamp if (timevalisset(&aitv.it_value)) { 527c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 5284cf41af3SPoul-Henning Kamp if (timevalcmp(&aitv.it_value, &ctv, <)) 5294cf41af3SPoul-Henning Kamp timevalclear(&aitv.it_value); 530df8bae1dSRodney W. Grimes else 531227ee8a1SPoul-Henning Kamp timevalsub(&aitv.it_value, &ctv); 532227ee8a1SPoul-Henning Kamp } 533fb99ab88SMatthew Dillon } else { 534df8bae1dSRodney W. Grimes aitv = p->p_stats->p_timer[uap->which]; 535fb99ab88SMatthew Dillon } 536df8bae1dSRodney W. Grimes splx(s); 537fb99ab88SMatthew Dillon error = copyout((caddr_t)&aitv, (caddr_t)uap->itv, 538fb99ab88SMatthew Dillon sizeof (struct itimerval)); 539fb99ab88SMatthew Dillon mtx_unlock(&Giant); 540fb99ab88SMatthew Dillon return(error); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes 543d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 544df8bae1dSRodney W. Grimes struct setitimer_args { 545df8bae1dSRodney W. Grimes u_int which; 546df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 547df8bae1dSRodney W. Grimes }; 548d2d3e875SBruce Evans #endif 549fb99ab88SMatthew Dillon /* 550fb99ab88SMatthew Dillon * MPSAFE 551fb99ab88SMatthew Dillon */ 552df8bae1dSRodney W. Grimes /* ARGSUSED */ 55326f9a767SRodney W. Grimes int 554b40ce416SJulian Elischer setitimer(td, uap) 555b40ce416SJulian Elischer struct thread *td; 556df8bae1dSRodney W. Grimes register struct setitimer_args *uap; 557df8bae1dSRodney W. Grimes { 558b40ce416SJulian Elischer struct proc *p = td->td_proc; 559df8bae1dSRodney W. Grimes struct itimerval aitv; 560227ee8a1SPoul-Henning Kamp struct timeval ctv; 561df8bae1dSRodney W. Grimes register struct itimerval *itvp; 562fb99ab88SMatthew Dillon int s, error = 0; 563df8bae1dSRodney W. Grimes 564df8bae1dSRodney W. Grimes if (uap->which > ITIMER_PROF) 565df8bae1dSRodney W. Grimes return (EINVAL); 566df8bae1dSRodney W. Grimes itvp = uap->itv; 567df8bae1dSRodney W. Grimes if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 568df8bae1dSRodney W. Grimes sizeof(struct itimerval)))) 569df8bae1dSRodney W. Grimes return (error); 570fb99ab88SMatthew Dillon 571fb99ab88SMatthew Dillon mtx_lock(&Giant); 572fb99ab88SMatthew Dillon 5730808c591SBruce Evans if ((uap->itv = uap->oitv) && 574b40ce416SJulian Elischer (error = getitimer(td, (struct getitimer_args *)uap))) { 575fb99ab88SMatthew Dillon goto done2; 576fb99ab88SMatthew Dillon } 577fb99ab88SMatthew Dillon if (itvp == 0) { 578fb99ab88SMatthew Dillon error = 0; 579fb99ab88SMatthew Dillon goto done2; 580fb99ab88SMatthew Dillon } 581fb99ab88SMatthew Dillon if (itimerfix(&aitv.it_value)) { 582fb99ab88SMatthew Dillon error = EINVAL; 583fb99ab88SMatthew Dillon goto done2; 584fb99ab88SMatthew Dillon } 585fb99ab88SMatthew Dillon if (!timevalisset(&aitv.it_value)) { 5864cf41af3SPoul-Henning Kamp timevalclear(&aitv.it_interval); 587fb99ab88SMatthew Dillon } else if (itimerfix(&aitv.it_interval)) { 588fb99ab88SMatthew Dillon error = EINVAL; 589fb99ab88SMatthew Dillon goto done2; 590fb99ab88SMatthew Dillon } 591227ee8a1SPoul-Henning Kamp s = splclock(); /* XXX: still needed ? */ 592df8bae1dSRodney W. Grimes if (uap->which == ITIMER_REAL) { 5934cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 5944f559836SJake Burkholder callout_stop(&p->p_itcallout); 5954cf41af3SPoul-Henning Kamp if (timevalisset(&aitv.it_value)) 5964f559836SJake Burkholder callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value), 5974f559836SJake Burkholder realitexpire, p); 598c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 599bfe6c9faSPoul-Henning Kamp timevaladd(&aitv.it_value, &ctv); 600df8bae1dSRodney W. Grimes p->p_realtimer = aitv; 601fb99ab88SMatthew Dillon } else { 602df8bae1dSRodney W. Grimes p->p_stats->p_timer[uap->which] = aitv; 603fb99ab88SMatthew Dillon } 604df8bae1dSRodney W. Grimes splx(s); 605fb99ab88SMatthew Dillon done2: 606fb99ab88SMatthew Dillon mtx_unlock(&Giant); 607fb99ab88SMatthew Dillon return (error); 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes /* 611df8bae1dSRodney W. Grimes * Real interval timer expired: 612df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 613df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 614df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 615df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 616df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 617c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 6189207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 6199207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 6209207f00aSBruce Evans * interrupt even when we're delayed. 621df8bae1dSRodney W. Grimes */ 622df8bae1dSRodney W. Grimes void 623df8bae1dSRodney W. Grimes realitexpire(arg) 624df8bae1dSRodney W. Grimes void *arg; 625df8bae1dSRodney W. Grimes { 626df8bae1dSRodney W. Grimes register struct proc *p; 627bfe6c9faSPoul-Henning Kamp struct timeval ctv, ntv; 628df8bae1dSRodney W. Grimes int s; 629df8bae1dSRodney W. Grimes 630df8bae1dSRodney W. Grimes p = (struct proc *)arg; 63137824023SJohn Baldwin PROC_LOCK(p); 632df8bae1dSRodney W. Grimes psignal(p, SIGALRM); 6334cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 6344cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 63537824023SJohn Baldwin PROC_UNLOCK(p); 636df8bae1dSRodney W. Grimes return; 637df8bae1dSRodney W. Grimes } 638df8bae1dSRodney W. Grimes for (;;) { 639227ee8a1SPoul-Henning Kamp s = splclock(); /* XXX: still neeeded ? */ 640df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 641df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 642c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 6434cf41af3SPoul-Henning Kamp if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 644bfe6c9faSPoul-Henning Kamp ntv = p->p_realtimer.it_value; 645bfe6c9faSPoul-Henning Kamp timevalsub(&ntv, &ctv); 6464f559836SJake Burkholder callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 6474f559836SJake Burkholder realitexpire, p); 648df8bae1dSRodney W. Grimes splx(s); 64937824023SJohn Baldwin PROC_UNLOCK(p); 650df8bae1dSRodney W. Grimes return; 651df8bae1dSRodney W. Grimes } 652df8bae1dSRodney W. Grimes splx(s); 653df8bae1dSRodney W. Grimes } 65437824023SJohn Baldwin /*NOTREACHED*/ 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes 657df8bae1dSRodney W. Grimes /* 658df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 659df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 660df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 661df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 662df8bae1dSRodney W. Grimes */ 66326f9a767SRodney W. Grimes int 664df8bae1dSRodney W. Grimes itimerfix(tv) 665df8bae1dSRodney W. Grimes struct timeval *tv; 666df8bae1dSRodney W. Grimes { 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 669df8bae1dSRodney W. Grimes tv->tv_usec < 0 || tv->tv_usec >= 1000000) 670df8bae1dSRodney W. Grimes return (EINVAL); 671df8bae1dSRodney W. Grimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 672df8bae1dSRodney W. Grimes tv->tv_usec = tick; 673df8bae1dSRodney W. Grimes return (0); 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes /* 677df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 678df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 679df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 680df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 681df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 682df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 683df8bae1dSRodney W. Grimes * that it is called in a context where the timers 684df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 685df8bae1dSRodney W. Grimes */ 68626f9a767SRodney W. Grimes int 687df8bae1dSRodney W. Grimes itimerdecr(itp, usec) 688df8bae1dSRodney W. Grimes register struct itimerval *itp; 689df8bae1dSRodney W. Grimes int usec; 690df8bae1dSRodney W. Grimes { 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 693df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 694df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 695df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 696df8bae1dSRodney W. Grimes goto expire; 697df8bae1dSRodney W. Grimes } 698df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 699df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 700df8bae1dSRodney W. Grimes } 701df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 702df8bae1dSRodney W. Grimes usec = 0; 7034cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 704df8bae1dSRodney W. Grimes return (1); 705df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 706df8bae1dSRodney W. Grimes expire: 7074cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 708df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 709df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 710df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 711df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 712df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 713df8bae1dSRodney W. Grimes } 714df8bae1dSRodney W. Grimes } else 715df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 716df8bae1dSRodney W. Grimes return (0); 717df8bae1dSRodney W. Grimes } 718df8bae1dSRodney W. Grimes 719df8bae1dSRodney W. Grimes /* 720df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 721df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 722df8bae1dSRodney W. Grimes * results which are before the beginning, 723df8bae1dSRodney W. Grimes * it just gets very confused in this case. 724df8bae1dSRodney W. Grimes * Caveat emptor. 725df8bae1dSRodney W. Grimes */ 72626f9a767SRodney W. Grimes void 727df8bae1dSRodney W. Grimes timevaladd(t1, t2) 728df8bae1dSRodney W. Grimes struct timeval *t1, *t2; 729df8bae1dSRodney W. Grimes { 730df8bae1dSRodney W. Grimes 731df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 732df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 733df8bae1dSRodney W. Grimes timevalfix(t1); 734df8bae1dSRodney W. Grimes } 735df8bae1dSRodney W. Grimes 73626f9a767SRodney W. Grimes void 737df8bae1dSRodney W. Grimes timevalsub(t1, t2) 738df8bae1dSRodney W. Grimes struct timeval *t1, *t2; 739df8bae1dSRodney W. Grimes { 740df8bae1dSRodney W. Grimes 741df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 742df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 743df8bae1dSRodney W. Grimes timevalfix(t1); 744df8bae1dSRodney W. Grimes } 745df8bae1dSRodney W. Grimes 74687b6de2bSPoul-Henning Kamp static void 747df8bae1dSRodney W. Grimes timevalfix(t1) 748df8bae1dSRodney W. Grimes struct timeval *t1; 749df8bae1dSRodney W. Grimes { 750df8bae1dSRodney W. Grimes 751df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 752df8bae1dSRodney W. Grimes t1->tv_sec--; 753df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 754df8bae1dSRodney W. Grimes } 755df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 756df8bae1dSRodney W. Grimes t1->tv_sec++; 757df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes } 760