123377Smckusick /* 237583Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337583Smckusick * All rights reserved. 423377Smckusick * 544441Sbostic * %sccs.include.redist.c% 637583Smckusick * 7*54784Storek * @(#)kern_time.c 7.17 (Berkeley) 07/08/92 823377Smckusick */ 97424Sroot 1017093Sbloom #include "param.h" 1147540Skarels #include "resourcevar.h" 1217093Sbloom #include "kernel.h" 13*54784Storek #include "systm.h" 1417093Sbloom #include "proc.h" 1552188Smckusick #include "vnode.h" 167424Sroot 1737520Smckusick #include "machine/cpu.h" 1829946Skarels 198103Sroot /* 208103Sroot * Time of day and interval timer support. 218146Sroot * 228146Sroot * These routines provide the kernel entry points to get and set 238146Sroot * the time-of-day and per-process interval timers. Subroutines 248146Sroot * here provide support for adding and subtracting timeval structures 258146Sroot * and decrementing interval timers, optionally reloading the interval 268146Sroot * timers when they expire. 278103Sroot */ 288103Sroot 2943392Skarels /* ARGSUSED */ 3043392Skarels gettimeofday(p, uap, retval) 3143392Skarels struct proc *p; 3243392Skarels register struct args { 338034Sroot struct timeval *tp; 348034Sroot struct timezone *tzp; 3543392Skarels } *uap; 3643392Skarels int *retval; 3743392Skarels { 388034Sroot struct timeval atv; 3943392Skarels int error = 0; 407500Sroot 4130666Sbostic if (uap->tp) { 4230666Sbostic microtime(&atv); 4343392Skarels if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 4443392Skarels sizeof (atv))) 4544405Skarels return (error); 4630666Sbostic } 4730666Sbostic if (uap->tzp) 4843392Skarels error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 4943392Skarels sizeof (tz)); 5044405Skarels return (error); 517500Sroot } 527500Sroot 5345120Sbostic /* ARGSUSED */ 5443392Skarels settimeofday(p, uap, retval) 5543392Skarels struct proc *p; 5643392Skarels struct args { 578103Sroot struct timeval *tv; 588103Sroot struct timezone *tzp; 5943392Skarels } *uap; 6043392Skarels int *retval; 6143392Skarels { 62*54784Storek struct timeval atv, delta; 638034Sroot struct timezone atz; 6443392Skarels int error, s; 657500Sroot 6647540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 6744405Skarels return (error); 68*54784Storek /* Verify all parameters before changing time. */ 69*54784Storek if (uap->tv && 70*54784Storek (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv)))) 71*54784Storek return (error); 72*54784Storek if (uap->tzp && 73*54784Storek (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz)))) 74*54784Storek return (error); 7530666Sbostic if (uap->tv) { 7637583Smckusick /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 77*54784Storek s = splclock(); 78*54784Storek /* nb. delta.tv_usec may be < 0, but this is OK here */ 79*54784Storek delta.tv_sec = atv.tv_sec - time.tv_sec; 80*54784Storek delta.tv_usec = atv.tv_usec - time.tv_usec; 81*54784Storek time = atv; 82*54784Storek (void) splsoftclock(); 83*54784Storek timevaladd(&boottime, &delta); 84*54784Storek timevalfix(&boottime); 85*54784Storek timevaladd(&runtime, &delta); 86*54784Storek timevalfix(&runtime); 87*54784Storek LEASE_UPDATETIME(delta.tv_sec); 88*54784Storek splx(s); 8937583Smckusick resettodr(); 9030666Sbostic } 91*54784Storek if (uap->tzp) 9237591Smckusick tz = atz; 93*54784Storek return (0); 947500Sroot } 957500Sroot 9628829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 9728829Skarels int tickdelta; /* current clock skew, us. per tick */ 9828829Skarels long timedelta; /* unapplied time correction, us. */ 9928829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 10017356Skarels 10143392Skarels /* ARGSUSED */ 10243392Skarels adjtime(p, uap, retval) 10343392Skarels struct proc *p; 10443392Skarels register struct args { 10517356Skarels struct timeval *delta; 10617356Skarels struct timeval *olddelta; 10743392Skarels } *uap; 10843392Skarels int *retval; 10943392Skarels { 11017356Skarels struct timeval atv, oatv; 11128829Skarels register long ndelta; 11243392Skarels int s, error; 11317356Skarels 11447540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 11544405Skarels return (error); 11643392Skarels if (error = 11743392Skarels copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval))) 11844405Skarels return (error); 11928829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 12028829Skarels if (timedelta == 0) 12128829Skarels if (ndelta > bigadj) 12228829Skarels tickdelta = 10 * tickadj; 12328829Skarels else 12428829Skarels tickdelta = tickadj; 12528829Skarels if (ndelta % tickdelta) 12628829Skarels ndelta = ndelta / tickadj * tickadj; 12728829Skarels 12825170Skarels s = splclock(); 12917356Skarels if (uap->olddelta) { 13028829Skarels oatv.tv_sec = timedelta / 1000000; 13128829Skarels oatv.tv_usec = timedelta % 1000000; 13228829Skarels } 13328829Skarels timedelta = ndelta; 13428829Skarels splx(s); 13528829Skarels 13628829Skarels if (uap->olddelta) 13717356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 13817356Skarels sizeof (struct timeval)); 13944405Skarels return (0); 14017356Skarels } 14117356Skarels 1428146Sroot /* 1438146Sroot * Get value of an interval timer. The process virtual and 14447540Skarels * profiling virtual time timers are kept in the p_stats area, since 1458146Sroot * they can be swapped out. These are kept internally in the 1468146Sroot * way they are specified externally: in time until they expire. 1478146Sroot * 1488146Sroot * The real time interval timer is kept in the process table slot 1498146Sroot * for the process, and its value (it_value) is kept as an 1508146Sroot * absolute time rather than as a delta, so that it is easy to keep 1518146Sroot * periodic real-time signals from drifting. 1528146Sroot * 1538146Sroot * Virtual time timers are processed in the hardclock() routine of 1548146Sroot * kern_clock.c. The real time timer is processed by a timeout 1558146Sroot * routine, called from the softclock() routine. Since a callout 1568146Sroot * may be delayed in real time due to interrupt processing in the system, 1578146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1588146Sroot * to be delayed in real time past when it is supposed to occur. It 1598146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1608146Sroot * real time timers .it_interval. Rather, we compute the next time in 1618146Sroot * absolute time the timer should go off. 1628146Sroot */ 16343392Skarels /* ARGSUSED */ 16443392Skarels getitimer(p, uap, retval) 16543392Skarels struct proc *p; 16643392Skarels register struct args { 1678034Sroot u_int which; 1688034Sroot struct itimerval *itv; 16943392Skarels } *uap; 17043392Skarels int *retval; 17143392Skarels { 1728114Sroot struct itimerval aitv; 1738034Sroot int s; 1747424Sroot 17543392Skarels if (uap->which > ITIMER_PROF) 17644405Skarels return (EINVAL); 17725897Skarels s = splclock(); 1788114Sroot if (uap->which == ITIMER_REAL) { 1798146Sroot /* 1808146Sroot * Convert from absoulte to relative time in .it_value 1818146Sroot * part of real time timer. If time for real time timer 1828146Sroot * has passed return 0, else return difference between 1838146Sroot * current time and time for the timer to go off. 1848146Sroot */ 18543392Skarels aitv = p->p_realtimer; 1868114Sroot if (timerisset(&aitv.it_value)) 1878114Sroot if (timercmp(&aitv.it_value, &time, <)) 1888114Sroot timerclear(&aitv.it_value); 1898114Sroot else 190*54784Storek timevalsub(&aitv.it_value, 191*54784Storek (struct timeval *)&time); 1928114Sroot } else 19347540Skarels aitv = p->p_stats->p_timer[uap->which]; 1948114Sroot splx(s); 19544405Skarels return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, 19643392Skarels sizeof (struct itimerval))); 1977424Sroot } 1987424Sroot 19943392Skarels /* ARGSUSED */ 20043392Skarels setitimer(p, uap, retval) 20143392Skarels struct proc *p; 20243392Skarels register struct args { 2038034Sroot u_int which; 2048103Sroot struct itimerval *itv, *oitv; 20543392Skarels } *uap; 20643392Skarels int *retval; 20743392Skarels { 20837591Smckusick struct itimerval aitv; 20937591Smckusick register struct itimerval *itvp; 21043392Skarels int s, error; 2117424Sroot 21243392Skarels if (uap->which > ITIMER_PROF) 21344405Skarels return (EINVAL); 21437591Smckusick itvp = uap->itv; 21543392Skarels if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 21637591Smckusick sizeof(struct itimerval)))) 21744405Skarels return (error); 21843392Skarels if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval))) 21944405Skarels return (error); 22037591Smckusick if (itvp == 0) 22143392Skarels return (0); 22243392Skarels if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 22344405Skarels return (EINVAL); 22425897Skarels s = splclock(); 2258114Sroot if (uap->which == ITIMER_REAL) { 2268625Sroot untimeout(realitexpire, (caddr_t)p); 2278114Sroot if (timerisset(&aitv.it_value)) { 228*54784Storek timevaladd(&aitv.it_value, (struct timeval *)&time); 2298625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2308114Sroot } 2318114Sroot p->p_realtimer = aitv; 2328114Sroot } else 23347540Skarels p->p_stats->p_timer[uap->which] = aitv; 2348034Sroot splx(s); 23544405Skarels return (0); 2367424Sroot } 2377424Sroot 2388146Sroot /* 2398146Sroot * Real interval timer expired: 2408146Sroot * send process whose timer expired an alarm signal. 2418146Sroot * If time is not set up to reload, then just return. 2428146Sroot * Else compute next time timer should go off which is > current time. 2438146Sroot * This is where delay in processing this timeout causes multiple 2448146Sroot * SIGALRM calls to be compressed into one. 2458146Sroot */ 246*54784Storek void 247*54784Storek realitexpire(arg) 248*54784Storek void *arg; 249*54784Storek { 2508114Sroot register struct proc *p; 2518114Sroot int s; 2528114Sroot 253*54784Storek p = (struct proc *)arg; 2548114Sroot psignal(p, SIGALRM); 2558114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2568114Sroot timerclear(&p->p_realtimer.it_value); 2578114Sroot return; 2588114Sroot } 2598114Sroot for (;;) { 26025897Skarels s = splclock(); 2618114Sroot timevaladd(&p->p_realtimer.it_value, 2628114Sroot &p->p_realtimer.it_interval); 2638114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2648625Sroot timeout(realitexpire, (caddr_t)p, 2658625Sroot hzto(&p->p_realtimer.it_value)); 2668114Sroot splx(s); 2678114Sroot return; 2688114Sroot } 2698114Sroot splx(s); 2708114Sroot } 2718114Sroot } 2728114Sroot 2738146Sroot /* 2748146Sroot * Check that a proposed value to load into the .it_value or 2758146Sroot * .it_interval part of an interval timer is acceptable, and 2768146Sroot * fix it to have at least minimal value (i.e. if it is less 2778146Sroot * than the resolution of the clock, round it up.) 2788146Sroot */ 2798103Sroot itimerfix(tv) 2808103Sroot struct timeval *tv; 2817424Sroot { 2828034Sroot 2838114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2848114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2858103Sroot return (EINVAL); 28612970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 2878103Sroot tv->tv_usec = tick; 2888103Sroot return (0); 2898034Sroot } 2908034Sroot 2918146Sroot /* 2928146Sroot * Decrement an interval timer by a specified number 2938146Sroot * of microseconds, which must be less than a second, 2948146Sroot * i.e. < 1000000. If the timer expires, then reload 2958146Sroot * it. In this case, carry over (usec - old value) to 296*54784Storek * reduce the value reloaded into the timer so that 2978146Sroot * the timer does not drift. This routine assumes 2988146Sroot * that it is called in a context where the timers 2998146Sroot * on which it is operating cannot change in value. 3008146Sroot */ 3018034Sroot itimerdecr(itp, usec) 3028034Sroot register struct itimerval *itp; 3038034Sroot int usec; 3048034Sroot { 3058034Sroot 3068103Sroot if (itp->it_value.tv_usec < usec) { 3078103Sroot if (itp->it_value.tv_sec == 0) { 3088146Sroot /* expired, and already in next interval */ 3098103Sroot usec -= itp->it_value.tv_usec; 3108034Sroot goto expire; 3118103Sroot } 3128103Sroot itp->it_value.tv_usec += 1000000; 3138103Sroot itp->it_value.tv_sec--; 3148034Sroot } 3158103Sroot itp->it_value.tv_usec -= usec; 3168103Sroot usec = 0; 3178103Sroot if (timerisset(&itp->it_value)) 3188034Sroot return (1); 3198146Sroot /* expired, exactly at end of interval */ 3208034Sroot expire: 3218103Sroot if (timerisset(&itp->it_interval)) { 3228103Sroot itp->it_value = itp->it_interval; 3238103Sroot itp->it_value.tv_usec -= usec; 3248103Sroot if (itp->it_value.tv_usec < 0) { 3258103Sroot itp->it_value.tv_usec += 1000000; 3268103Sroot itp->it_value.tv_sec--; 3278103Sroot } 3288103Sroot } else 3298146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 3308034Sroot return (0); 3318034Sroot } 3328034Sroot 3338146Sroot /* 3348146Sroot * Add and subtract routines for timevals. 3358146Sroot * N.B.: subtract routine doesn't deal with 3368146Sroot * results which are before the beginning, 3378146Sroot * it just gets very confused in this case. 3388146Sroot * Caveat emptor. 3398146Sroot */ 3408146Sroot timevaladd(t1, t2) 3418146Sroot struct timeval *t1, *t2; 3428146Sroot { 3438146Sroot 3448146Sroot t1->tv_sec += t2->tv_sec; 3458146Sroot t1->tv_usec += t2->tv_usec; 3468146Sroot timevalfix(t1); 3478146Sroot } 3488146Sroot 3498146Sroot timevalsub(t1, t2) 3508146Sroot struct timeval *t1, *t2; 3518146Sroot { 3528146Sroot 3538146Sroot t1->tv_sec -= t2->tv_sec; 3548146Sroot t1->tv_usec -= t2->tv_usec; 3558146Sroot timevalfix(t1); 3568146Sroot } 3578146Sroot 3588146Sroot timevalfix(t1) 3598146Sroot struct timeval *t1; 3608146Sroot { 3618146Sroot 3628146Sroot if (t1->tv_usec < 0) { 3638146Sroot t1->tv_sec--; 3648146Sroot t1->tv_usec += 1000000; 3658146Sroot } 3668146Sroot if (t1->tv_usec >= 1000000) { 3678146Sroot t1->tv_sec++; 3688146Sroot t1->tv_usec -= 1000000; 3698146Sroot } 3708146Sroot } 371