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*47540Skarels * @(#)kern_time.c 7.15 (Berkeley) 03/17/91 823377Smckusick */ 97424Sroot 1017093Sbloom #include "param.h" 11*47540Skarels #include "resourcevar.h" 1217093Sbloom #include "kernel.h" 1317093Sbloom #include "proc.h" 147424Sroot 1537520Smckusick #include "machine/cpu.h" 1629946Skarels 178103Sroot /* 188103Sroot * Time of day and interval timer support. 198146Sroot * 208146Sroot * These routines provide the kernel entry points to get and set 218146Sroot * the time-of-day and per-process interval timers. Subroutines 228146Sroot * here provide support for adding and subtracting timeval structures 238146Sroot * and decrementing interval timers, optionally reloading the interval 248146Sroot * timers when they expire. 258103Sroot */ 268103Sroot 2743392Skarels /* ARGSUSED */ 2843392Skarels gettimeofday(p, uap, retval) 2943392Skarels struct proc *p; 3043392Skarels register struct args { 318034Sroot struct timeval *tp; 328034Sroot struct timezone *tzp; 3343392Skarels } *uap; 3443392Skarels int *retval; 3543392Skarels { 368034Sroot struct timeval atv; 3743392Skarels int error = 0; 387500Sroot 3930666Sbostic if (uap->tp) { 4030666Sbostic microtime(&atv); 4143392Skarels if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 4243392Skarels sizeof (atv))) 4344405Skarels return (error); 4430666Sbostic } 4530666Sbostic if (uap->tzp) 4643392Skarels error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 4743392Skarels sizeof (tz)); 4844405Skarels return (error); 497500Sroot } 507500Sroot 5145120Sbostic /* ARGSUSED */ 5243392Skarels settimeofday(p, uap, retval) 5343392Skarels struct proc *p; 5443392Skarels struct args { 558103Sroot struct timeval *tv; 568103Sroot struct timezone *tzp; 5743392Skarels } *uap; 5843392Skarels int *retval; 5943392Skarels { 608034Sroot struct timeval atv; 618034Sroot struct timezone atz; 6243392Skarels int error, s; 637500Sroot 64*47540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 6544405Skarels return (error); 6630666Sbostic if (uap->tv) { 6743392Skarels if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 6843392Skarels sizeof (struct timeval))) 6944405Skarels return (error); 7037583Smckusick /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 7137583Smckusick boottime.tv_sec += atv.tv_sec - time.tv_sec; 7237583Smckusick s = splhigh(); time = atv; splx(s); 7337583Smckusick resettodr(); 7430666Sbostic } 7543392Skarels if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, 7643392Skarels sizeof (atz))) == 0) 7737591Smckusick tz = atz; 7844405Skarels return (error); 797500Sroot } 807500Sroot 8128829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 8228829Skarels int tickdelta; /* current clock skew, us. per tick */ 8328829Skarels long timedelta; /* unapplied time correction, us. */ 8428829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 8517356Skarels 8643392Skarels /* ARGSUSED */ 8743392Skarels adjtime(p, uap, retval) 8843392Skarels struct proc *p; 8943392Skarels register struct args { 9017356Skarels struct timeval *delta; 9117356Skarels struct timeval *olddelta; 9243392Skarels } *uap; 9343392Skarels int *retval; 9443392Skarels { 9517356Skarels struct timeval atv, oatv; 9628829Skarels register long ndelta; 9743392Skarels int s, error; 9817356Skarels 99*47540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 10044405Skarels return (error); 10143392Skarels if (error = 10243392Skarels copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval))) 10344405Skarels return (error); 10428829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 10528829Skarels if (timedelta == 0) 10628829Skarels if (ndelta > bigadj) 10728829Skarels tickdelta = 10 * tickadj; 10828829Skarels else 10928829Skarels tickdelta = tickadj; 11028829Skarels if (ndelta % tickdelta) 11128829Skarels ndelta = ndelta / tickadj * tickadj; 11228829Skarels 11325170Skarels s = splclock(); 11417356Skarels if (uap->olddelta) { 11528829Skarels oatv.tv_sec = timedelta / 1000000; 11628829Skarels oatv.tv_usec = timedelta % 1000000; 11728829Skarels } 11828829Skarels timedelta = ndelta; 11928829Skarels splx(s); 12028829Skarels 12128829Skarels if (uap->olddelta) 12217356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 12317356Skarels sizeof (struct timeval)); 12444405Skarels return (0); 12517356Skarels } 12617356Skarels 1278146Sroot /* 1288146Sroot * Get value of an interval timer. The process virtual and 129*47540Skarels * profiling virtual time timers are kept in the p_stats area, since 1308146Sroot * they can be swapped out. These are kept internally in the 1318146Sroot * way they are specified externally: in time until they expire. 1328146Sroot * 1338146Sroot * The real time interval timer is kept in the process table slot 1348146Sroot * for the process, and its value (it_value) is kept as an 1358146Sroot * absolute time rather than as a delta, so that it is easy to keep 1368146Sroot * periodic real-time signals from drifting. 1378146Sroot * 1388146Sroot * Virtual time timers are processed in the hardclock() routine of 1398146Sroot * kern_clock.c. The real time timer is processed by a timeout 1408146Sroot * routine, called from the softclock() routine. Since a callout 1418146Sroot * may be delayed in real time due to interrupt processing in the system, 1428146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1438146Sroot * to be delayed in real time past when it is supposed to occur. It 1448146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1458146Sroot * real time timers .it_interval. Rather, we compute the next time in 1468146Sroot * absolute time the timer should go off. 1478146Sroot */ 14843392Skarels /* ARGSUSED */ 14943392Skarels getitimer(p, uap, retval) 15043392Skarels struct proc *p; 15143392Skarels register struct args { 1528034Sroot u_int which; 1538034Sroot struct itimerval *itv; 15443392Skarels } *uap; 15543392Skarels int *retval; 15643392Skarels { 1578114Sroot struct itimerval aitv; 1588034Sroot int s; 1597424Sroot 16043392Skarels if (uap->which > ITIMER_PROF) 16144405Skarels return (EINVAL); 16225897Skarels s = splclock(); 1638114Sroot if (uap->which == ITIMER_REAL) { 1648146Sroot /* 1658146Sroot * Convert from absoulte to relative time in .it_value 1668146Sroot * part of real time timer. If time for real time timer 1678146Sroot * has passed return 0, else return difference between 1688146Sroot * current time and time for the timer to go off. 1698146Sroot */ 17043392Skarels aitv = p->p_realtimer; 1718114Sroot if (timerisset(&aitv.it_value)) 1728114Sroot if (timercmp(&aitv.it_value, &time, <)) 1738114Sroot timerclear(&aitv.it_value); 1748114Sroot else 1758114Sroot timevalsub(&aitv.it_value, &time); 1768114Sroot } else 177*47540Skarels aitv = p->p_stats->p_timer[uap->which]; 1788114Sroot splx(s); 17944405Skarels return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, 18043392Skarels sizeof (struct itimerval))); 1817424Sroot } 1827424Sroot 18343392Skarels /* ARGSUSED */ 18443392Skarels setitimer(p, uap, retval) 18543392Skarels struct proc *p; 18643392Skarels register struct args { 1878034Sroot u_int which; 1888103Sroot struct itimerval *itv, *oitv; 18943392Skarels } *uap; 19043392Skarels int *retval; 19143392Skarels { 19237591Smckusick struct itimerval aitv; 19337591Smckusick register struct itimerval *itvp; 19443392Skarels int s, error; 1957424Sroot 19643392Skarels if (uap->which > ITIMER_PROF) 19744405Skarels return (EINVAL); 19837591Smckusick itvp = uap->itv; 19943392Skarels if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 20037591Smckusick sizeof(struct itimerval)))) 20144405Skarels return (error); 20243392Skarels if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval))) 20344405Skarels return (error); 20437591Smckusick if (itvp == 0) 20543392Skarels return (0); 20643392Skarels if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 20744405Skarels return (EINVAL); 20825897Skarels s = splclock(); 2098114Sroot if (uap->which == ITIMER_REAL) { 2108625Sroot untimeout(realitexpire, (caddr_t)p); 2118114Sroot if (timerisset(&aitv.it_value)) { 2128114Sroot timevaladd(&aitv.it_value, &time); 2138625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2148114Sroot } 2158114Sroot p->p_realtimer = aitv; 2168114Sroot } else 217*47540Skarels p->p_stats->p_timer[uap->which] = aitv; 2188034Sroot splx(s); 21944405Skarels return (0); 2207424Sroot } 2217424Sroot 2228146Sroot /* 2238146Sroot * Real interval timer expired: 2248146Sroot * send process whose timer expired an alarm signal. 2258146Sroot * If time is not set up to reload, then just return. 2268146Sroot * Else compute next time timer should go off which is > current time. 2278146Sroot * This is where delay in processing this timeout causes multiple 2288146Sroot * SIGALRM calls to be compressed into one. 2298146Sroot */ 2308146Sroot realitexpire(p) 2318114Sroot register struct proc *p; 2328114Sroot { 2338114Sroot int s; 2348114Sroot 2358114Sroot psignal(p, SIGALRM); 2368114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2378114Sroot timerclear(&p->p_realtimer.it_value); 2388114Sroot return; 2398114Sroot } 2408114Sroot for (;;) { 24125897Skarels s = splclock(); 2428114Sroot timevaladd(&p->p_realtimer.it_value, 2438114Sroot &p->p_realtimer.it_interval); 2448114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2458625Sroot timeout(realitexpire, (caddr_t)p, 2468625Sroot hzto(&p->p_realtimer.it_value)); 2478114Sroot splx(s); 2488114Sroot return; 2498114Sroot } 2508114Sroot splx(s); 2518114Sroot } 2528114Sroot } 2538114Sroot 2548146Sroot /* 2558146Sroot * Check that a proposed value to load into the .it_value or 2568146Sroot * .it_interval part of an interval timer is acceptable, and 2578146Sroot * fix it to have at least minimal value (i.e. if it is less 2588146Sroot * than the resolution of the clock, round it up.) 2598146Sroot */ 2608103Sroot itimerfix(tv) 2618103Sroot struct timeval *tv; 2627424Sroot { 2638034Sroot 2648114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2658114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2668103Sroot return (EINVAL); 26712970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 2688103Sroot tv->tv_usec = tick; 2698103Sroot return (0); 2708034Sroot } 2718034Sroot 2728146Sroot /* 2738146Sroot * Decrement an interval timer by a specified number 2748146Sroot * of microseconds, which must be less than a second, 2758146Sroot * i.e. < 1000000. If the timer expires, then reload 2768146Sroot * it. In this case, carry over (usec - old value) to 2778146Sroot * reducint the value reloaded into the timer so that 2788146Sroot * the timer does not drift. This routine assumes 2798146Sroot * that it is called in a context where the timers 2808146Sroot * on which it is operating cannot change in value. 2818146Sroot */ 2828034Sroot itimerdecr(itp, usec) 2838034Sroot register struct itimerval *itp; 2848034Sroot int usec; 2858034Sroot { 2868034Sroot 2878103Sroot if (itp->it_value.tv_usec < usec) { 2888103Sroot if (itp->it_value.tv_sec == 0) { 2898146Sroot /* expired, and already in next interval */ 2908103Sroot usec -= itp->it_value.tv_usec; 2918034Sroot goto expire; 2928103Sroot } 2938103Sroot itp->it_value.tv_usec += 1000000; 2948103Sroot itp->it_value.tv_sec--; 2958034Sroot } 2968103Sroot itp->it_value.tv_usec -= usec; 2978103Sroot usec = 0; 2988103Sroot if (timerisset(&itp->it_value)) 2998034Sroot return (1); 3008146Sroot /* expired, exactly at end of interval */ 3018034Sroot expire: 3028103Sroot if (timerisset(&itp->it_interval)) { 3038103Sroot itp->it_value = itp->it_interval; 3048103Sroot itp->it_value.tv_usec -= usec; 3058103Sroot if (itp->it_value.tv_usec < 0) { 3068103Sroot itp->it_value.tv_usec += 1000000; 3078103Sroot itp->it_value.tv_sec--; 3088103Sroot } 3098103Sroot } else 3108146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 3118034Sroot return (0); 3128034Sroot } 3138034Sroot 3148146Sroot /* 3158146Sroot * Add and subtract routines for timevals. 3168146Sroot * N.B.: subtract routine doesn't deal with 3178146Sroot * results which are before the beginning, 3188146Sroot * it just gets very confused in this case. 3198146Sroot * Caveat emptor. 3208146Sroot */ 3218146Sroot timevaladd(t1, t2) 3228146Sroot struct timeval *t1, *t2; 3238146Sroot { 3248146Sroot 3258146Sroot t1->tv_sec += t2->tv_sec; 3268146Sroot t1->tv_usec += t2->tv_usec; 3278146Sroot timevalfix(t1); 3288146Sroot } 3298146Sroot 3308146Sroot timevalsub(t1, t2) 3318146Sroot struct timeval *t1, *t2; 3328146Sroot { 3338146Sroot 3348146Sroot t1->tv_sec -= t2->tv_sec; 3358146Sroot t1->tv_usec -= t2->tv_usec; 3368146Sroot timevalfix(t1); 3378146Sroot } 3388146Sroot 3398146Sroot timevalfix(t1) 3408146Sroot struct timeval *t1; 3418146Sroot { 3428146Sroot 3438146Sroot if (t1->tv_usec < 0) { 3448146Sroot t1->tv_sec--; 3458146Sroot t1->tv_usec += 1000000; 3468146Sroot } 3478146Sroot if (t1->tv_usec >= 1000000) { 3488146Sroot t1->tv_sec++; 3498146Sroot t1->tv_usec -= 1000000; 3508146Sroot } 3518146Sroot } 352