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 374b8d5f2dSRobert Watson #include "opt_mac.h" 384b8d5f2dSRobert Watson 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40e96c1fdcSPeter Wemm #include <sys/systm.h> 41fb919e4dSMark Murray #include <sys/lock.h> 42fb919e4dSMark Murray #include <sys/mutex.h> 43d2d3e875SBruce Evans #include <sys/sysproto.h> 44df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 45797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 46df8bae1dSRodney W. Grimes #include <sys/kernel.h> 474b8d5f2dSRobert Watson #include <sys/mac.h> 48df8bae1dSRodney W. Grimes #include <sys/systm.h> 4994c8fcd8SPeter Wemm #include <sys/sysent.h> 50df8bae1dSRodney W. Grimes #include <sys/proc.h> 51708e7684SPeter Wemm #include <sys/time.h> 5291266b96SPoul-Henning Kamp #include <sys/timetc.h> 53df8bae1dSRodney W. Grimes #include <sys/vnode.h> 54fb919e4dSMark Murray 555b870b7bSPeter Wemm #include <vm/vm.h> 565b870b7bSPeter Wemm #include <vm/vm_extern.h> 57df8bae1dSRodney W. Grimes 5891f1c2b3SPoul-Henning Kamp int tz_minuteswest; 5991f1c2b3SPoul-Henning Kamp int tz_dsttime; 60ac7e6123SDavid Greenman 61df8bae1dSRodney W. Grimes /* 62df8bae1dSRodney W. Grimes * Time of day and interval timer support. 63df8bae1dSRodney W. Grimes * 64df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 65df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 66df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 67df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 68df8bae1dSRodney W. Grimes * timers when they expire. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 714d77a549SAlfred Perlstein static int nanosleep1(struct thread *td, struct timespec *rqt, 724d77a549SAlfred Perlstein struct timespec *rmt); 737edfb592SJohn Baldwin static int settime(struct thread *, struct timeval *); 744d77a549SAlfred Perlstein static void timevalfix(struct timeval *); 754d77a549SAlfred Perlstein static void no_lease_updatetime(int); 7694c8fcd8SPeter Wemm 771b09ae77SPoul-Henning Kamp static void 781b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat) 791b09ae77SPoul-Henning Kamp int deltat; 801b09ae77SPoul-Henning Kamp { 811b09ae77SPoul-Henning Kamp } 821b09ae77SPoul-Henning Kamp 834d77a549SAlfred Perlstein void (*lease_updatetime)(int) = no_lease_updatetime; 841b09ae77SPoul-Henning Kamp 8594c8fcd8SPeter Wemm static int 8691afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 8794c8fcd8SPeter Wemm { 88fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 89c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 907ec73f64SPoul-Henning Kamp struct timespec ts; 9194c8fcd8SPeter Wemm int s; 9294c8fcd8SPeter Wemm 93708e7684SPeter Wemm s = splclock(); 949c8fff87SBruce Evans microtime(&tv1); 9500af9731SPoul-Henning Kamp delta = *tv; 9600af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 9794c8fcd8SPeter Wemm 9894c8fcd8SPeter Wemm /* 999c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 100fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 101fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 102fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 103fcae3aa6SNick Sayer * back to the past. 104c0bd94a7SNick Sayer * 105c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 106c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 107c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 10894c8fcd8SPeter Wemm */ 1097edfb592SJohn Baldwin if (securelevel_gt(td->td_ucred, 1) != 0) { 110fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1113f92429aSMatt Jacob /* 112c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1133f92429aSMatt Jacob */ 114fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 115fcae3aa6SNick Sayer maxtime = tv1; 116fcae3aa6SNick Sayer tv2 = *tv; 117fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 118fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1193f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 120fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 121fcae3aa6SNick Sayer } 1223f92429aSMatt Jacob } else { 123c0bd94a7SNick Sayer if (tv1.tv_sec == laststep.tv_sec) { 124c0bd94a7SNick Sayer splx(s); 125c0bd94a7SNick Sayer return (EPERM); 126c0bd94a7SNick Sayer } 127c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 128c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 129c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 130c0bd94a7SNick Sayer } 131c0bd94a7SNick Sayer laststep = *tv; 132fcae3aa6SNick Sayer } 1339c8fff87SBruce Evans } 1349c8fff87SBruce Evans 1357ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1367ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 1377edfb592SJohn Baldwin mtx_lock(&Giant); 13891266b96SPoul-Henning Kamp tc_setclock(&ts); 13994c8fcd8SPeter Wemm (void) splsoftclock(); 14094c8fcd8SPeter Wemm lease_updatetime(delta.tv_sec); 14194c8fcd8SPeter Wemm splx(s); 14294c8fcd8SPeter Wemm resettodr(); 1437edfb592SJohn Baldwin mtx_unlock(&Giant); 14494c8fcd8SPeter Wemm return (0); 14594c8fcd8SPeter Wemm } 14694c8fcd8SPeter Wemm 14794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 14894c8fcd8SPeter Wemm struct clock_gettime_args { 14994c8fcd8SPeter Wemm clockid_t clock_id; 15094c8fcd8SPeter Wemm struct timespec *tp; 15194c8fcd8SPeter Wemm }; 15294c8fcd8SPeter Wemm #endif 153708e7684SPeter Wemm 154fb99ab88SMatthew Dillon /* 155fb99ab88SMatthew Dillon * MPSAFE 156fb99ab88SMatthew Dillon */ 15794c8fcd8SPeter Wemm /* ARGSUSED */ 15894c8fcd8SPeter Wemm int 15991afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap) 16094c8fcd8SPeter Wemm { 16194c8fcd8SPeter Wemm struct timespec ats; 16294c8fcd8SPeter Wemm 163d1e405c5SAlfred Perlstein if (uap->clock_id != CLOCK_REALTIME) 16494c8fcd8SPeter Wemm return (EINVAL); 1657ec73f64SPoul-Henning Kamp nanotime(&ats); 166d1e405c5SAlfred Perlstein return (copyout(&ats, 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 18191afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap) 18294c8fcd8SPeter Wemm { 18394c8fcd8SPeter Wemm struct timeval atv; 18494c8fcd8SPeter Wemm struct timespec ats; 18594c8fcd8SPeter Wemm int error; 18694c8fcd8SPeter Wemm 1874b8d5f2dSRobert Watson #ifdef MAC 1884b8d5f2dSRobert Watson error = mac_check_system_settime(td->td_ucred); 1894b8d5f2dSRobert Watson if (error) 1904b8d5f2dSRobert Watson return (error); 1914b8d5f2dSRobert Watson #endif 19244731cabSJohn Baldwin if ((error = suser(td)) != 0) 1937edfb592SJohn Baldwin return (error); 194d1e405c5SAlfred Perlstein if (uap->clock_id != CLOCK_REALTIME) 1957edfb592SJohn Baldwin return (EINVAL); 196d1e405c5SAlfred Perlstein if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 1977edfb592SJohn Baldwin return (error); 1987edfb592SJohn Baldwin if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000) 1997edfb592SJohn Baldwin return (EINVAL); 200a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 20194c8fcd8SPeter Wemm TIMESPEC_TO_TIMEVAL(&atv, &ats); 2027edfb592SJohn Baldwin error = settime(td, &atv); 20394c8fcd8SPeter Wemm return (error); 20494c8fcd8SPeter Wemm } 20594c8fcd8SPeter Wemm 20694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 20794c8fcd8SPeter Wemm struct clock_getres_args { 20894c8fcd8SPeter Wemm clockid_t clock_id; 20994c8fcd8SPeter Wemm struct timespec *tp; 21094c8fcd8SPeter Wemm }; 21194c8fcd8SPeter Wemm #endif 212708e7684SPeter Wemm 21394c8fcd8SPeter Wemm int 21491afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap) 21594c8fcd8SPeter Wemm { 21694c8fcd8SPeter Wemm struct timespec ts; 217708e7684SPeter Wemm int error; 21894c8fcd8SPeter Wemm 219d1e405c5SAlfred Perlstein if (uap->clock_id != CLOCK_REALTIME) 22094c8fcd8SPeter Wemm return (EINVAL); 221708e7684SPeter Wemm error = 0; 222d1e405c5SAlfred Perlstein if (uap->tp) { 22394c8fcd8SPeter Wemm ts.tv_sec = 0; 224ac0653dcSBruce Evans /* 225ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 226ac0653dcSBruce Evans * Rounding up is especially important if rounding down 227ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 228ac0653dcSBruce Evans */ 229ac0653dcSBruce Evans ts.tv_nsec = 1000000000 / tc_getfrequency() + 1; 230d1e405c5SAlfred Perlstein error = copyout(&ts, uap->tp, sizeof(ts)); 23194c8fcd8SPeter Wemm } 232708e7684SPeter Wemm return (error); 23394c8fcd8SPeter Wemm } 23494c8fcd8SPeter Wemm 23594c8fcd8SPeter Wemm static int nanowait; 23694c8fcd8SPeter Wemm 2375b870b7bSPeter Wemm static int 23891afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt) 2395b870b7bSPeter Wemm { 2405704ba6aSPoul-Henning Kamp struct timespec ts, ts2, ts3; 24133841826SPoul-Henning Kamp struct timeval tv; 24233841826SPoul-Henning Kamp int error; 2435b870b7bSPeter Wemm 2447d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 245708e7684SPeter Wemm return (EINVAL); 246d254af07SMatthew Dillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 2477d7fb492SBruce Evans return (0); 248c21410e1SPoul-Henning Kamp getnanouptime(&ts); 24900af9731SPoul-Henning Kamp timespecadd(&ts, rqt); 25033841826SPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, rqt); 25133841826SPoul-Henning Kamp for (;;) { 25233841826SPoul-Henning Kamp error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 25333841826SPoul-Henning Kamp tvtohz(&tv)); 254c21410e1SPoul-Henning Kamp getnanouptime(&ts2); 25533841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 25633841826SPoul-Henning Kamp if (error == ERESTART) 25794c8fcd8SPeter Wemm error = EINTR; 25833841826SPoul-Henning Kamp if (rmt != NULL) { 25933841826SPoul-Henning Kamp timespecsub(&ts, &ts2); 26033841826SPoul-Henning Kamp if (ts.tv_sec < 0) 26133841826SPoul-Henning Kamp timespecclear(&ts); 26200af9731SPoul-Henning Kamp *rmt = ts; 26300af9731SPoul-Henning Kamp } 26433841826SPoul-Henning Kamp return (error); 26533841826SPoul-Henning Kamp } 26633841826SPoul-Henning Kamp if (timespeccmp(&ts2, &ts, >=)) 26733841826SPoul-Henning Kamp return (0); 2685704ba6aSPoul-Henning Kamp ts3 = ts; 2695704ba6aSPoul-Henning Kamp timespecsub(&ts3, &ts2); 2705704ba6aSPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, &ts3); 27133841826SPoul-Henning Kamp } 2725b870b7bSPeter Wemm } 27394c8fcd8SPeter Wemm 2745b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 2755b870b7bSPeter Wemm struct nanosleep_args { 2765b870b7bSPeter Wemm struct timespec *rqtp; 2775b870b7bSPeter Wemm struct timespec *rmtp; 2785b870b7bSPeter Wemm }; 2795b870b7bSPeter Wemm #endif 2805b870b7bSPeter Wemm 281fb99ab88SMatthew Dillon /* 282fb99ab88SMatthew Dillon * MPSAFE 283fb99ab88SMatthew Dillon */ 2845b870b7bSPeter Wemm /* ARGSUSED */ 2855b870b7bSPeter Wemm int 28691afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap) 2875b870b7bSPeter Wemm { 2885b870b7bSPeter Wemm struct timespec rmt, rqt; 289fb99ab88SMatthew Dillon int error; 2905b870b7bSPeter Wemm 291d1e405c5SAlfred Perlstein error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 2925b870b7bSPeter Wemm if (error) 2935b870b7bSPeter Wemm return (error); 294fb99ab88SMatthew Dillon 29531f3e2adSAlfred Perlstein if (uap->rmtp && 29631f3e2adSAlfred Perlstein !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 29731f3e2adSAlfred Perlstein return (EFAULT); 298b40ce416SJulian Elischer error = nanosleep1(td, &rqt, &rmt); 299d1e405c5SAlfred Perlstein if (error && uap->rmtp) { 300fb99ab88SMatthew Dillon int error2; 301fb99ab88SMatthew Dillon 302d1e405c5SAlfred Perlstein error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 30331f3e2adSAlfred Perlstein if (error2) 304fb99ab88SMatthew Dillon error = error2; 305708e7684SPeter Wemm } 306708e7684SPeter Wemm return (error); 30794c8fcd8SPeter Wemm } 30894c8fcd8SPeter Wemm 3095b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 310df8bae1dSRodney W. Grimes struct gettimeofday_args { 311df8bae1dSRodney W. Grimes struct timeval *tp; 312df8bae1dSRodney W. Grimes struct timezone *tzp; 313df8bae1dSRodney W. Grimes }; 314d2d3e875SBruce Evans #endif 315fb99ab88SMatthew Dillon /* 316fb99ab88SMatthew Dillon * MPSAFE 317fb99ab88SMatthew Dillon */ 318df8bae1dSRodney W. Grimes /* ARGSUSED */ 31926f9a767SRodney W. Grimes int 32091afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap) 321df8bae1dSRodney W. Grimes { 322df8bae1dSRodney W. Grimes struct timeval atv; 323411c25edSTim J. Robbins struct timezone rtz; 324df8bae1dSRodney W. Grimes int error = 0; 325df8bae1dSRodney W. Grimes 326df8bae1dSRodney W. Grimes if (uap->tp) { 327df8bae1dSRodney W. Grimes microtime(&atv); 32801609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 329df8bae1dSRodney W. Grimes } 33021dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 33191f1c2b3SPoul-Henning Kamp rtz.tz_minuteswest = tz_minuteswest; 33291f1c2b3SPoul-Henning Kamp rtz.tz_dsttime = tz_dsttime; 333411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 33421dcdb38SPoul-Henning Kamp } 335df8bae1dSRodney W. Grimes return (error); 336df8bae1dSRodney W. Grimes } 337df8bae1dSRodney W. Grimes 338d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 339df8bae1dSRodney W. Grimes struct settimeofday_args { 340df8bae1dSRodney W. Grimes struct timeval *tv; 341df8bae1dSRodney W. Grimes struct timezone *tzp; 342df8bae1dSRodney W. Grimes }; 343d2d3e875SBruce Evans #endif 344fb99ab88SMatthew Dillon /* 345fb99ab88SMatthew Dillon * MPSAFE 346fb99ab88SMatthew Dillon */ 347df8bae1dSRodney W. Grimes /* ARGSUSED */ 34826f9a767SRodney W. Grimes int 34991afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap) 350df8bae1dSRodney W. Grimes { 351708e7684SPeter Wemm struct timeval atv; 352df8bae1dSRodney W. Grimes struct timezone atz; 353fb99ab88SMatthew Dillon int error = 0; 354fb99ab88SMatthew Dillon 3554b8d5f2dSRobert Watson #ifdef MAC 3564b8d5f2dSRobert Watson error = mac_check_system_settime(td->td_ucred); 3574b8d5f2dSRobert Watson if (error) 3584b8d5f2dSRobert Watson return (error); 3594b8d5f2dSRobert Watson #endif 36044731cabSJohn Baldwin if ((error = suser(td))) 3617edfb592SJohn Baldwin return (error); 362df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 363708e7684SPeter Wemm if (uap->tv) { 36401609114SAlfred Perlstein if ((error = copyin(uap->tv, &atv, sizeof(atv)))) 3657edfb592SJohn Baldwin return (error); 3667edfb592SJohn Baldwin if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) 3677edfb592SJohn Baldwin return (EINVAL); 368708e7684SPeter Wemm } 369df8bae1dSRodney W. Grimes if (uap->tzp && 37001609114SAlfred Perlstein (error = copyin(uap->tzp, &atz, sizeof(atz)))) 3717edfb592SJohn Baldwin return (error); 3727edfb592SJohn Baldwin 3737edfb592SJohn Baldwin if (uap->tv && (error = settime(td, &atv))) 3747edfb592SJohn Baldwin return (error); 3757edfb592SJohn Baldwin if (uap->tzp) { 37691f1c2b3SPoul-Henning Kamp tz_minuteswest = atz.tz_minuteswest; 37791f1c2b3SPoul-Henning Kamp tz_dsttime = atz.tz_dsttime; 3787edfb592SJohn Baldwin } 379fb99ab88SMatthew Dillon return (error); 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes /* 382df8bae1dSRodney W. Grimes * Get value of an interval timer. The process virtual and 383df8bae1dSRodney W. Grimes * profiling virtual time timers are kept in the p_stats area, since 384df8bae1dSRodney W. Grimes * they can be swapped out. These are kept internally in the 385df8bae1dSRodney W. Grimes * way they are specified externally: in time until they expire. 386df8bae1dSRodney W. Grimes * 387df8bae1dSRodney W. Grimes * The real time interval timer is kept in the process table slot 388df8bae1dSRodney W. Grimes * for the process, and its value (it_value) is kept as an 389df8bae1dSRodney W. Grimes * absolute time rather than as a delta, so that it is easy to keep 390df8bae1dSRodney W. Grimes * periodic real-time signals from drifting. 391df8bae1dSRodney W. Grimes * 392df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 393df8bae1dSRodney W. Grimes * kern_clock.c. The real time timer is processed by a timeout 394df8bae1dSRodney W. Grimes * routine, called from the softclock() routine. Since a callout 395df8bae1dSRodney W. Grimes * may be delayed in real time due to interrupt processing in the system, 396df8bae1dSRodney W. Grimes * it is possible for the real time timeout routine (realitexpire, given below), 397df8bae1dSRodney W. Grimes * to be delayed in real time past when it is supposed to occur. It 398df8bae1dSRodney W. Grimes * does not suffice, therefore, to reload the real timer .it_value from the 399df8bae1dSRodney W. Grimes * real time timers .it_interval. Rather, we compute the next time in 400df8bae1dSRodney W. Grimes * absolute time the timer should go off. 401df8bae1dSRodney W. Grimes */ 402d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 403df8bae1dSRodney W. Grimes struct getitimer_args { 404df8bae1dSRodney W. Grimes u_int which; 405df8bae1dSRodney W. Grimes struct itimerval *itv; 406df8bae1dSRodney W. Grimes }; 407d2d3e875SBruce Evans #endif 408fb99ab88SMatthew Dillon /* 409fb99ab88SMatthew Dillon * MPSAFE 410fb99ab88SMatthew Dillon */ 41126f9a767SRodney W. Grimes int 41291afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap) 413df8bae1dSRodney W. Grimes { 414b40ce416SJulian Elischer struct proc *p = td->td_proc; 415227ee8a1SPoul-Henning Kamp struct timeval ctv; 416df8bae1dSRodney W. Grimes struct itimerval aitv; 417df8bae1dSRodney W. Grimes 418df8bae1dSRodney W. Grimes if (uap->which > ITIMER_PROF) 419df8bae1dSRodney W. Grimes return (EINVAL); 420fb99ab88SMatthew Dillon 421df8bae1dSRodney W. Grimes if (uap->which == ITIMER_REAL) { 422df8bae1dSRodney W. Grimes /* 423ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 424df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 425df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 426df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 427df8bae1dSRodney W. Grimes */ 42896d7f8efSTim J. Robbins PROC_LOCK(p); 429df8bae1dSRodney W. Grimes aitv = p->p_realtimer; 43096d7f8efSTim J. Robbins PROC_UNLOCK(p); 4314cf41af3SPoul-Henning Kamp if (timevalisset(&aitv.it_value)) { 432c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 4334cf41af3SPoul-Henning Kamp if (timevalcmp(&aitv.it_value, &ctv, <)) 4344cf41af3SPoul-Henning Kamp timevalclear(&aitv.it_value); 435df8bae1dSRodney W. Grimes else 436227ee8a1SPoul-Henning Kamp timevalsub(&aitv.it_value, &ctv); 437227ee8a1SPoul-Henning Kamp } 438fb99ab88SMatthew Dillon } else { 43996d7f8efSTim J. Robbins mtx_lock_spin(&sched_lock); 440df8bae1dSRodney W. Grimes aitv = p->p_stats->p_timer[uap->which]; 44196d7f8efSTim J. Robbins mtx_unlock_spin(&sched_lock); 442fb99ab88SMatthew Dillon } 443411c25edSTim J. Robbins return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes 446d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 447df8bae1dSRodney W. Grimes struct setitimer_args { 448df8bae1dSRodney W. Grimes u_int which; 449df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 450df8bae1dSRodney W. Grimes }; 451d2d3e875SBruce Evans #endif 452fb99ab88SMatthew Dillon /* 453fb99ab88SMatthew Dillon * MPSAFE 454fb99ab88SMatthew Dillon */ 45526f9a767SRodney W. Grimes int 45691afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap) 457df8bae1dSRodney W. Grimes { 458b40ce416SJulian Elischer struct proc *p = td->td_proc; 45996d7f8efSTim J. Robbins struct itimerval aitv, oitv; 460227ee8a1SPoul-Henning Kamp struct timeval ctv; 46196d7f8efSTim J. Robbins int error; 46296d7f8efSTim J. Robbins 46396d7f8efSTim J. Robbins if (uap->itv == NULL) { 46496d7f8efSTim J. Robbins uap->itv = uap->oitv; 46596d7f8efSTim J. Robbins return (getitimer(td, (struct getitimer_args *)uap)); 46696d7f8efSTim J. Robbins } 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes if (uap->which > ITIMER_PROF) 469df8bae1dSRodney W. Grimes return (EINVAL); 47096d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 471df8bae1dSRodney W. Grimes return (error); 47296d7f8efSTim J. Robbins if (itimerfix(&aitv.it_value)) 47396d7f8efSTim J. Robbins return (EINVAL); 47496d7f8efSTim J. Robbins if (!timevalisset(&aitv.it_value)) 4754cf41af3SPoul-Henning Kamp timevalclear(&aitv.it_interval); 47696d7f8efSTim J. Robbins else if (itimerfix(&aitv.it_interval)) 47796d7f8efSTim J. Robbins return (EINVAL); 47896d7f8efSTim J. Robbins 479df8bae1dSRodney W. Grimes if (uap->which == ITIMER_REAL) { 48096d7f8efSTim J. Robbins PROC_LOCK(p); 4814cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 4824f559836SJake Burkholder callout_stop(&p->p_itcallout); 4834cf41af3SPoul-Henning Kamp if (timevalisset(&aitv.it_value)) 4844f559836SJake Burkholder callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value), 4854f559836SJake Burkholder realitexpire, p); 486c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 487bfe6c9faSPoul-Henning Kamp timevaladd(&aitv.it_value, &ctv); 48896d7f8efSTim J. Robbins oitv = p->p_realtimer; 489df8bae1dSRodney W. Grimes p->p_realtimer = aitv; 49096d7f8efSTim J. Robbins PROC_UNLOCK(p); 49196d7f8efSTim J. Robbins if (timevalisset(&oitv.it_value)) { 49296d7f8efSTim J. Robbins if (timevalcmp(&oitv.it_value, &ctv, <)) 49396d7f8efSTim J. Robbins timevalclear(&oitv.it_value); 49496d7f8efSTim J. Robbins else 49596d7f8efSTim J. Robbins timevalsub(&oitv.it_value, &ctv); 496fb99ab88SMatthew Dillon } 49796d7f8efSTim J. Robbins } else { 49896d7f8efSTim J. Robbins mtx_lock_spin(&sched_lock); 49996d7f8efSTim J. Robbins oitv = p->p_stats->p_timer[uap->which]; 50096d7f8efSTim J. Robbins p->p_stats->p_timer[uap->which] = aitv; 50196d7f8efSTim J. Robbins mtx_unlock_spin(&sched_lock); 50296d7f8efSTim J. Robbins } 50396d7f8efSTim J. Robbins if (uap->oitv == NULL) 50496d7f8efSTim J. Robbins return (0); 50596d7f8efSTim J. Robbins return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes 508df8bae1dSRodney W. Grimes /* 509df8bae1dSRodney W. Grimes * Real interval timer expired: 510df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 511df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 512df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 513df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 514df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 515c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 5169207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 5179207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 5189207f00aSBruce Evans * interrupt even when we're delayed. 519df8bae1dSRodney W. Grimes */ 520df8bae1dSRodney W. Grimes void 52191afe087SPoul-Henning Kamp realitexpire(void *arg) 522df8bae1dSRodney W. Grimes { 52391afe087SPoul-Henning Kamp struct proc *p; 524bfe6c9faSPoul-Henning Kamp struct timeval ctv, ntv; 525df8bae1dSRodney W. Grimes 526df8bae1dSRodney W. Grimes p = (struct proc *)arg; 52737824023SJohn Baldwin PROC_LOCK(p); 528df8bae1dSRodney W. Grimes psignal(p, SIGALRM); 5294cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 5304cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 53137824023SJohn Baldwin PROC_UNLOCK(p); 532df8bae1dSRodney W. Grimes return; 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes for (;;) { 535df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 536df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 537c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 5384cf41af3SPoul-Henning Kamp if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 539bfe6c9faSPoul-Henning Kamp ntv = p->p_realtimer.it_value; 540bfe6c9faSPoul-Henning Kamp timevalsub(&ntv, &ctv); 5414f559836SJake Burkholder callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 5424f559836SJake Burkholder realitexpire, p); 54337824023SJohn Baldwin PROC_UNLOCK(p); 544df8bae1dSRodney W. Grimes return; 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes } 54737824023SJohn Baldwin /*NOTREACHED*/ 548df8bae1dSRodney W. Grimes } 549df8bae1dSRodney W. Grimes 550df8bae1dSRodney W. Grimes /* 551df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 552df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 553df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 554df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 555df8bae1dSRodney W. Grimes */ 55626f9a767SRodney W. Grimes int 55791afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 558df8bae1dSRodney W. Grimes { 559df8bae1dSRodney W. Grimes 560df8bae1dSRodney W. Grimes if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 561df8bae1dSRodney W. Grimes tv->tv_usec < 0 || tv->tv_usec >= 1000000) 562df8bae1dSRodney W. Grimes return (EINVAL); 563df8bae1dSRodney W. Grimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 564df8bae1dSRodney W. Grimes tv->tv_usec = tick; 565df8bae1dSRodney W. Grimes return (0); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes 568df8bae1dSRodney W. Grimes /* 569df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 570df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 571df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 572df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 573df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 574df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 575df8bae1dSRodney W. Grimes * that it is called in a context where the timers 576df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 577df8bae1dSRodney W. Grimes */ 57826f9a767SRodney W. Grimes int 57991afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 580df8bae1dSRodney W. Grimes { 581df8bae1dSRodney W. Grimes 582df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 583df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 584df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 585df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 586df8bae1dSRodney W. Grimes goto expire; 587df8bae1dSRodney W. Grimes } 588df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 589df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 590df8bae1dSRodney W. Grimes } 591df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 592df8bae1dSRodney W. Grimes usec = 0; 5934cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 594df8bae1dSRodney W. Grimes return (1); 595df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 596df8bae1dSRodney W. Grimes expire: 5974cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 598df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 599df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 600df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 601df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 602df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 603df8bae1dSRodney W. Grimes } 604df8bae1dSRodney W. Grimes } else 605df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 606df8bae1dSRodney W. Grimes return (0); 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes /* 610df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 611df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 612df8bae1dSRodney W. Grimes * results which are before the beginning, 613df8bae1dSRodney W. Grimes * it just gets very confused in this case. 614df8bae1dSRodney W. Grimes * Caveat emptor. 615df8bae1dSRodney W. Grimes */ 61626f9a767SRodney W. Grimes void 61791afe087SPoul-Henning Kamp timevaladd(struct timeval *t1, struct timeval *t2) 618df8bae1dSRodney W. Grimes { 619df8bae1dSRodney W. Grimes 620df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 621df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 622df8bae1dSRodney W. Grimes timevalfix(t1); 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes 62526f9a767SRodney W. Grimes void 62691afe087SPoul-Henning Kamp timevalsub(struct timeval *t1, struct timeval *t2) 627df8bae1dSRodney W. Grimes { 628df8bae1dSRodney W. Grimes 629df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 630df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 631df8bae1dSRodney W. Grimes timevalfix(t1); 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes 63487b6de2bSPoul-Henning Kamp static void 63591afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 636df8bae1dSRodney W. Grimes { 637df8bae1dSRodney W. Grimes 638df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 639df8bae1dSRodney W. Grimes t1->tv_sec--; 640df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 641df8bae1dSRodney W. Grimes } 642df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 643df8bae1dSRodney W. Grimes t1->tv_sec++; 644df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes } 64791974ce1SSam Leffler 64891974ce1SSam Leffler /* 649addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 65091974ce1SSam Leffler */ 65191974ce1SSam Leffler int 65291974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 65391974ce1SSam Leffler { 65491974ce1SSam Leffler struct timeval tv, delta; 65591974ce1SSam Leffler int rv = 0; 65691974ce1SSam Leffler 657addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 658addea9d4SSam Leffler delta = tv; 659addea9d4SSam Leffler timevalsub(&delta, lasttime); 66091974ce1SSam Leffler 66191974ce1SSam Leffler /* 66291974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 66391974ce1SSam Leffler * even if interval is huge. 66491974ce1SSam Leffler */ 66591974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 66691974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 66791974ce1SSam Leffler *lasttime = tv; 66891974ce1SSam Leffler rv = 1; 66991974ce1SSam Leffler } 67091974ce1SSam Leffler 67191974ce1SSam Leffler return (rv); 67291974ce1SSam Leffler } 67391974ce1SSam Leffler 67491974ce1SSam Leffler /* 67591974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 676addea9d4SSam Leffler * 677addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 678addea9d4SSam Leffler * should drop a packet because of the rate limitation). 679addea9d4SSam Leffler * 680addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 681addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 682addea9d4SSam Leffler * clock ticks for minimal overhead. 68391974ce1SSam Leffler */ 68491974ce1SSam Leffler int 68591974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 68691974ce1SSam Leffler { 687addea9d4SSam Leffler int now; 68891974ce1SSam Leffler 68991974ce1SSam Leffler /* 690addea9d4SSam Leffler * Reset the last time and counter if this is the first call 691addea9d4SSam Leffler * or more than a second has passed since the last update of 692addea9d4SSam Leffler * lasttime. 69391974ce1SSam Leffler */ 694addea9d4SSam Leffler now = ticks; 695addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 696addea9d4SSam Leffler lasttime->tv_sec = now; 697addea9d4SSam Leffler *curpps = 1; 698addea9d4SSam Leffler return (1); 699addea9d4SSam Leffler } else { 700addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 701addea9d4SSam Leffler return (maxpps < 0 || *curpps < maxpps); 702addea9d4SSam Leffler } 70391974ce1SSam Leffler } 704