123377Smckusick /* 263176Sbostic * Copyright (c) 1982, 1986, 1989, 1993 363176Sbostic * The Regents of the University of California. All rights reserved. 423377Smckusick * 544441Sbostic * %sccs.include.redist.c% 637583Smckusick * 7*68304Scgd * @(#)kern_time.c 8.3 (Berkeley) 02/14/95 823377Smckusick */ 97424Sroot 1056517Sbostic #include <sys/param.h> 1156517Sbostic #include <sys/resourcevar.h> 1256517Sbostic #include <sys/kernel.h> 1356517Sbostic #include <sys/systm.h> 1456517Sbostic #include <sys/proc.h> 1556517Sbostic #include <sys/vnode.h> 167424Sroot 17*68304Scgd #include <sys/mount.h> 18*68304Scgd #include <sys/syscallargs.h> 19*68304Scgd 2056517Sbostic #include <machine/cpu.h> 2129946Skarels 228103Sroot /* 238103Sroot * Time of day and interval timer support. 248146Sroot * 258146Sroot * These routines provide the kernel entry points to get and set 268146Sroot * the time-of-day and per-process interval timers. Subroutines 278146Sroot * here provide support for adding and subtracting timeval structures 288146Sroot * and decrementing interval timers, optionally reloading the interval 298146Sroot * timers when they expire. 308103Sroot */ 318103Sroot 3243392Skarels /* ARGSUSED */ 33*68304Scgd int 3443392Skarels gettimeofday(p, uap, retval) 3543392Skarels struct proc *p; 36*68304Scgd register struct gettimeofday_args /* { 37*68304Scgd syscallarg(struct timeval *) tp; 38*68304Scgd syscallarg(struct timezone *) tzp; 39*68304Scgd } */ *uap; 40*68304Scgd register_t *retval; 4143392Skarels { 428034Sroot struct timeval atv; 4343392Skarels int error = 0; 447500Sroot 45*68304Scgd if (SCARG(uap, tp)) { 4630666Sbostic microtime(&atv); 47*68304Scgd if (error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp), 4843392Skarels sizeof (atv))) 4944405Skarels return (error); 5030666Sbostic } 51*68304Scgd if (SCARG(uap, tzp)) 52*68304Scgd error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp), 5343392Skarels sizeof (tz)); 5444405Skarels return (error); 557500Sroot } 567500Sroot 5745120Sbostic /* ARGSUSED */ 58*68304Scgd int 5943392Skarels settimeofday(p, uap, retval) 6043392Skarels struct proc *p; 61*68304Scgd struct settimeofday_args /* { 62*68304Scgd syscallarg(struct timeval *) tv; 63*68304Scgd syscallarg(struct timezone *) tzp; 64*68304Scgd } */ *uap; 65*68304Scgd register_t *retval; 6643392Skarels { 6754784Storek struct timeval atv, delta; 688034Sroot struct timezone atz; 6943392Skarels int error, s; 707500Sroot 7147540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 7244405Skarels return (error); 7354784Storek /* Verify all parameters before changing time. */ 74*68304Scgd if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv), 75*68304Scgd (caddr_t)&atv, sizeof(atv)))) 7654784Storek return (error); 77*68304Scgd if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp), 78*68304Scgd (caddr_t)&atz, sizeof(atz)))) 7954784Storek return (error); 80*68304Scgd if (SCARG(uap, tv)) { 8137583Smckusick /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 8254784Storek s = splclock(); 8354784Storek /* nb. delta.tv_usec may be < 0, but this is OK here */ 8454784Storek delta.tv_sec = atv.tv_sec - time.tv_sec; 8554784Storek delta.tv_usec = atv.tv_usec - time.tv_usec; 8654784Storek time = atv; 8754784Storek (void) splsoftclock(); 8854784Storek timevaladd(&boottime, &delta); 8954784Storek timevalfix(&boottime); 9054784Storek timevaladd(&runtime, &delta); 9154784Storek timevalfix(&runtime); 9267639Smckusick # ifdef NFS 9367639Smckusick lease_updatetime(delta.tv_sec); 9467639Smckusick # endif 9554784Storek splx(s); 9637583Smckusick resettodr(); 9730666Sbostic } 98*68304Scgd if (SCARG(uap, tzp)) 9937591Smckusick tz = atz; 10054784Storek return (0); 1017500Sroot } 1027500Sroot 10328829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 10428829Skarels int tickdelta; /* current clock skew, us. per tick */ 10528829Skarels long timedelta; /* unapplied time correction, us. */ 10628829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 10717356Skarels 10843392Skarels /* ARGSUSED */ 109*68304Scgd int 11043392Skarels adjtime(p, uap, retval) 11143392Skarels struct proc *p; 112*68304Scgd register struct adjtime_args /* { 113*68304Scgd syscallarg(struct timeval *) delta; 114*68304Scgd syscallarg(struct timeval *) olddelta; 115*68304Scgd } */ *uap; 116*68304Scgd register_t *retval; 11743392Skarels { 11855293Storek struct timeval atv; 11955293Storek register long ndelta, ntickdelta, odelta; 12043392Skarels int s, error; 12117356Skarels 12247540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 12344405Skarels return (error); 124*68304Scgd if (error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv, 125*68304Scgd sizeof(struct timeval))) 12644405Skarels return (error); 12755293Storek 12855293Storek /* 12955293Storek * Compute the total correction and the rate at which to apply it. 13055293Storek * Round the adjustment down to a whole multiple of the per-tick 13155293Storek * delta, so that after some number of incremental changes in 13255293Storek * hardclock(), tickdelta will become zero, lest the correction 13355293Storek * overshoot and start taking us away from the desired final time. 13455293Storek */ 13528829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 13655293Storek if (ndelta > bigadj) 13755293Storek ntickdelta = 10 * tickadj; 13855293Storek else 13955293Storek ntickdelta = tickadj; 14055293Storek if (ndelta % ntickdelta) 14155293Storek ndelta = ndelta / ntickdelta * ntickdelta; 14228829Skarels 14355293Storek /* 14455293Storek * To make hardclock()'s job easier, make the per-tick delta negative 14555293Storek * if we want time to run slower; then hardclock can simply compute 14655293Storek * tick + tickdelta, and subtract tickdelta from timedelta. 14755293Storek */ 14855293Storek if (ndelta < 0) 14955293Storek ntickdelta = -ntickdelta; 15025170Skarels s = splclock(); 15155293Storek odelta = timedelta; 15228829Skarels timedelta = ndelta; 15355293Storek tickdelta = ntickdelta; 15428829Skarels splx(s); 15528829Skarels 156*68304Scgd if (SCARG(uap, olddelta)) { 15755293Storek atv.tv_sec = odelta / 1000000; 15855293Storek atv.tv_usec = odelta % 1000000; 159*68304Scgd (void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta), 16055293Storek sizeof(struct timeval)); 16155293Storek } 16244405Skarels return (0); 16317356Skarels } 16417356Skarels 1658146Sroot /* 1668146Sroot * Get value of an interval timer. The process virtual and 16747540Skarels * profiling virtual time timers are kept in the p_stats area, since 1688146Sroot * they can be swapped out. These are kept internally in the 1698146Sroot * way they are specified externally: in time until they expire. 1708146Sroot * 1718146Sroot * The real time interval timer is kept in the process table slot 1728146Sroot * for the process, and its value (it_value) is kept as an 1738146Sroot * absolute time rather than as a delta, so that it is easy to keep 1748146Sroot * periodic real-time signals from drifting. 1758146Sroot * 1768146Sroot * Virtual time timers are processed in the hardclock() routine of 1778146Sroot * kern_clock.c. The real time timer is processed by a timeout 1788146Sroot * routine, called from the softclock() routine. Since a callout 1798146Sroot * may be delayed in real time due to interrupt processing in the system, 1808146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1818146Sroot * to be delayed in real time past when it is supposed to occur. It 1828146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1838146Sroot * real time timers .it_interval. Rather, we compute the next time in 1848146Sroot * absolute time the timer should go off. 1858146Sroot */ 18643392Skarels /* ARGSUSED */ 187*68304Scgd int 18843392Skarels getitimer(p, uap, retval) 18943392Skarels struct proc *p; 190*68304Scgd register struct getitimer_args /* { 191*68304Scgd syscallarg(u_int) which; 192*68304Scgd syscallarg(struct itimerval *) itv; 193*68304Scgd } */ *uap; 194*68304Scgd register_t *retval; 19543392Skarels { 1968114Sroot struct itimerval aitv; 1978034Sroot int s; 1987424Sroot 199*68304Scgd if (SCARG(uap, which) > ITIMER_PROF) 20044405Skarels return (EINVAL); 20125897Skarels s = splclock(); 202*68304Scgd if (SCARG(uap, which) == ITIMER_REAL) { 2038146Sroot /* 204*68304Scgd * Convert from absolute to relative time in .it_value 2058146Sroot * part of real time timer. If time for real time timer 2068146Sroot * has passed return 0, else return difference between 2078146Sroot * current time and time for the timer to go off. 2088146Sroot */ 20943392Skarels aitv = p->p_realtimer; 2108114Sroot if (timerisset(&aitv.it_value)) 2118114Sroot if (timercmp(&aitv.it_value, &time, <)) 2128114Sroot timerclear(&aitv.it_value); 2138114Sroot else 21454784Storek timevalsub(&aitv.it_value, 21554784Storek (struct timeval *)&time); 2168114Sroot } else 217*68304Scgd aitv = p->p_stats->p_timer[SCARG(uap, which)]; 2188114Sroot splx(s); 219*68304Scgd return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv), 22043392Skarels sizeof (struct itimerval))); 2217424Sroot } 2227424Sroot 22343392Skarels /* ARGSUSED */ 224*68304Scgd int 22543392Skarels setitimer(p, uap, retval) 22643392Skarels struct proc *p; 227*68304Scgd register struct setitimer_args /* { 228*68304Scgd syscallarg(u_int) which; 229*68304Scgd syscallarg(struct itimerval *) itv; 230*68304Scgd syscallarg(struct itimerval *) oitv; 231*68304Scgd } */ *uap; 232*68304Scgd register_t *retval; 23343392Skarels { 23437591Smckusick struct itimerval aitv; 23537591Smckusick register struct itimerval *itvp; 23643392Skarels int s, error; 2377424Sroot 238*68304Scgd if (SCARG(uap, which) > ITIMER_PROF) 23944405Skarels return (EINVAL); 240*68304Scgd itvp = SCARG(uap, itv); 24143392Skarels if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 24237591Smckusick sizeof(struct itimerval)))) 24344405Skarels return (error); 244*68304Scgd if ((SCARG(uap, itv) = SCARG(uap, oitv)) && 245*68304Scgd (error = getitimer(p, uap, retval))) 24644405Skarels return (error); 24737591Smckusick if (itvp == 0) 24843392Skarels return (0); 24943392Skarels if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 25044405Skarels return (EINVAL); 25125897Skarels s = splclock(); 252*68304Scgd if (SCARG(uap, which) == ITIMER_REAL) { 2538625Sroot untimeout(realitexpire, (caddr_t)p); 2548114Sroot if (timerisset(&aitv.it_value)) { 25554784Storek timevaladd(&aitv.it_value, (struct timeval *)&time); 2568625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2578114Sroot } 2588114Sroot p->p_realtimer = aitv; 2598114Sroot } else 260*68304Scgd p->p_stats->p_timer[SCARG(uap, which)] = aitv; 2618034Sroot splx(s); 26244405Skarels return (0); 2637424Sroot } 2647424Sroot 2658146Sroot /* 2668146Sroot * Real interval timer expired: 2678146Sroot * send process whose timer expired an alarm signal. 2688146Sroot * If time is not set up to reload, then just return. 2698146Sroot * Else compute next time timer should go off which is > current time. 2708146Sroot * This is where delay in processing this timeout causes multiple 2718146Sroot * SIGALRM calls to be compressed into one. 2728146Sroot */ 27354784Storek void 27454784Storek realitexpire(arg) 27554784Storek void *arg; 27654784Storek { 2778114Sroot register struct proc *p; 2788114Sroot int s; 2798114Sroot 28054784Storek p = (struct proc *)arg; 2818114Sroot psignal(p, SIGALRM); 2828114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2838114Sroot timerclear(&p->p_realtimer.it_value); 2848114Sroot return; 2858114Sroot } 2868114Sroot for (;;) { 28725897Skarels s = splclock(); 2888114Sroot timevaladd(&p->p_realtimer.it_value, 2898114Sroot &p->p_realtimer.it_interval); 2908114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2918625Sroot timeout(realitexpire, (caddr_t)p, 2928625Sroot hzto(&p->p_realtimer.it_value)); 2938114Sroot splx(s); 2948114Sroot return; 2958114Sroot } 2968114Sroot splx(s); 2978114Sroot } 2988114Sroot } 2998114Sroot 3008146Sroot /* 3018146Sroot * Check that a proposed value to load into the .it_value or 3028146Sroot * .it_interval part of an interval timer is acceptable, and 3038146Sroot * fix it to have at least minimal value (i.e. if it is less 3048146Sroot * than the resolution of the clock, round it up.) 3058146Sroot */ 306*68304Scgd int 3078103Sroot itimerfix(tv) 3088103Sroot struct timeval *tv; 3097424Sroot { 3108034Sroot 3118114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 3128114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 3138103Sroot return (EINVAL); 31412970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 3158103Sroot tv->tv_usec = tick; 3168103Sroot return (0); 3178034Sroot } 3188034Sroot 3198146Sroot /* 3208146Sroot * Decrement an interval timer by a specified number 3218146Sroot * of microseconds, which must be less than a second, 3228146Sroot * i.e. < 1000000. If the timer expires, then reload 3238146Sroot * it. In this case, carry over (usec - old value) to 32454784Storek * reduce the value reloaded into the timer so that 3258146Sroot * the timer does not drift. This routine assumes 3268146Sroot * that it is called in a context where the timers 3278146Sroot * on which it is operating cannot change in value. 3288146Sroot */ 329*68304Scgd int 3308034Sroot itimerdecr(itp, usec) 3318034Sroot register struct itimerval *itp; 3328034Sroot int usec; 3338034Sroot { 3348034Sroot 3358103Sroot if (itp->it_value.tv_usec < usec) { 3368103Sroot if (itp->it_value.tv_sec == 0) { 3378146Sroot /* expired, and already in next interval */ 3388103Sroot usec -= itp->it_value.tv_usec; 3398034Sroot goto expire; 3408103Sroot } 3418103Sroot itp->it_value.tv_usec += 1000000; 3428103Sroot itp->it_value.tv_sec--; 3438034Sroot } 3448103Sroot itp->it_value.tv_usec -= usec; 3458103Sroot usec = 0; 3468103Sroot if (timerisset(&itp->it_value)) 3478034Sroot return (1); 3488146Sroot /* expired, exactly at end of interval */ 3498034Sroot expire: 3508103Sroot if (timerisset(&itp->it_interval)) { 3518103Sroot itp->it_value = itp->it_interval; 3528103Sroot itp->it_value.tv_usec -= usec; 3538103Sroot if (itp->it_value.tv_usec < 0) { 3548103Sroot itp->it_value.tv_usec += 1000000; 3558103Sroot itp->it_value.tv_sec--; 3568103Sroot } 3578103Sroot } else 3588146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 3598034Sroot return (0); 3608034Sroot } 3618034Sroot 3628146Sroot /* 3638146Sroot * Add and subtract routines for timevals. 3648146Sroot * N.B.: subtract routine doesn't deal with 3658146Sroot * results which are before the beginning, 3668146Sroot * it just gets very confused in this case. 3678146Sroot * Caveat emptor. 3688146Sroot */ 3698146Sroot timevaladd(t1, t2) 3708146Sroot struct timeval *t1, *t2; 3718146Sroot { 3728146Sroot 3738146Sroot t1->tv_sec += t2->tv_sec; 3748146Sroot t1->tv_usec += t2->tv_usec; 3758146Sroot timevalfix(t1); 3768146Sroot } 3778146Sroot 3788146Sroot timevalsub(t1, t2) 3798146Sroot struct timeval *t1, *t2; 3808146Sroot { 3818146Sroot 3828146Sroot t1->tv_sec -= t2->tv_sec; 3838146Sroot t1->tv_usec -= t2->tv_usec; 3848146Sroot timevalfix(t1); 3858146Sroot } 3868146Sroot 3878146Sroot timevalfix(t1) 3888146Sroot struct timeval *t1; 3898146Sroot { 3908146Sroot 3918146Sroot if (t1->tv_usec < 0) { 3928146Sroot t1->tv_sec--; 3938146Sroot t1->tv_usec += 1000000; 3948146Sroot } 3958146Sroot if (t1->tv_usec >= 1000000) { 3968146Sroot t1->tv_sec++; 3978146Sroot t1->tv_usec -= 1000000; 3988146Sroot } 3998146Sroot } 400