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*52188Smckusick * @(#)kern_time.c 7.16 (Berkeley) 01/14/92 823377Smckusick */ 97424Sroot 1017093Sbloom #include "param.h" 1147540Skarels #include "resourcevar.h" 1217093Sbloom #include "kernel.h" 1317093Sbloom #include "proc.h" 14*52188Smckusick #include "vnode.h" 157424Sroot 1637520Smckusick #include "machine/cpu.h" 1729946Skarels 188103Sroot /* 198103Sroot * Time of day and interval timer support. 208146Sroot * 218146Sroot * These routines provide the kernel entry points to get and set 228146Sroot * the time-of-day and per-process interval timers. Subroutines 238146Sroot * here provide support for adding and subtracting timeval structures 248146Sroot * and decrementing interval timers, optionally reloading the interval 258146Sroot * timers when they expire. 268103Sroot */ 278103Sroot 2843392Skarels /* ARGSUSED */ 2943392Skarels gettimeofday(p, uap, retval) 3043392Skarels struct proc *p; 3143392Skarels register struct args { 328034Sroot struct timeval *tp; 338034Sroot struct timezone *tzp; 3443392Skarels } *uap; 3543392Skarels int *retval; 3643392Skarels { 378034Sroot struct timeval atv; 3843392Skarels int error = 0; 397500Sroot 4030666Sbostic if (uap->tp) { 4130666Sbostic microtime(&atv); 4243392Skarels if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 4343392Skarels sizeof (atv))) 4444405Skarels return (error); 4530666Sbostic } 4630666Sbostic if (uap->tzp) 4743392Skarels error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 4843392Skarels sizeof (tz)); 4944405Skarels return (error); 507500Sroot } 517500Sroot 5245120Sbostic /* ARGSUSED */ 5343392Skarels settimeofday(p, uap, retval) 5443392Skarels struct proc *p; 5543392Skarels struct args { 568103Sroot struct timeval *tv; 578103Sroot struct timezone *tzp; 5843392Skarels } *uap; 5943392Skarels int *retval; 6043392Skarels { 618034Sroot struct timeval atv; 628034Sroot struct timezone atz; 6343392Skarels int error, s; 647500Sroot 6547540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 6644405Skarels return (error); 6730666Sbostic if (uap->tv) { 6843392Skarels if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 6943392Skarels sizeof (struct timeval))) 7044405Skarels return (error); 7137583Smckusick /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 7237583Smckusick boottime.tv_sec += atv.tv_sec - time.tv_sec; 73*52188Smckusick LEASE_UPDATETIME(atv.tv_sec - time.tv_sec); 7437583Smckusick s = splhigh(); time = atv; splx(s); 7537583Smckusick resettodr(); 7630666Sbostic } 7743392Skarels if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, 7843392Skarels sizeof (atz))) == 0) 7937591Smckusick tz = atz; 8044405Skarels return (error); 817500Sroot } 827500Sroot 8328829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 8428829Skarels int tickdelta; /* current clock skew, us. per tick */ 8528829Skarels long timedelta; /* unapplied time correction, us. */ 8628829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 8717356Skarels 8843392Skarels /* ARGSUSED */ 8943392Skarels adjtime(p, uap, retval) 9043392Skarels struct proc *p; 9143392Skarels register struct args { 9217356Skarels struct timeval *delta; 9317356Skarels struct timeval *olddelta; 9443392Skarels } *uap; 9543392Skarels int *retval; 9643392Skarels { 9717356Skarels struct timeval atv, oatv; 9828829Skarels register long ndelta; 9943392Skarels int s, error; 10017356Skarels 10147540Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 10244405Skarels return (error); 10343392Skarels if (error = 10443392Skarels copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval))) 10544405Skarels return (error); 10628829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 10728829Skarels if (timedelta == 0) 10828829Skarels if (ndelta > bigadj) 10928829Skarels tickdelta = 10 * tickadj; 11028829Skarels else 11128829Skarels tickdelta = tickadj; 11228829Skarels if (ndelta % tickdelta) 11328829Skarels ndelta = ndelta / tickadj * tickadj; 11428829Skarels 11525170Skarels s = splclock(); 11617356Skarels if (uap->olddelta) { 11728829Skarels oatv.tv_sec = timedelta / 1000000; 11828829Skarels oatv.tv_usec = timedelta % 1000000; 11928829Skarels } 12028829Skarels timedelta = ndelta; 12128829Skarels splx(s); 12228829Skarels 12328829Skarels if (uap->olddelta) 12417356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 12517356Skarels sizeof (struct timeval)); 12644405Skarels return (0); 12717356Skarels } 12817356Skarels 1298146Sroot /* 1308146Sroot * Get value of an interval timer. The process virtual and 13147540Skarels * profiling virtual time timers are kept in the p_stats area, since 1328146Sroot * they can be swapped out. These are kept internally in the 1338146Sroot * way they are specified externally: in time until they expire. 1348146Sroot * 1358146Sroot * The real time interval timer is kept in the process table slot 1368146Sroot * for the process, and its value (it_value) is kept as an 1378146Sroot * absolute time rather than as a delta, so that it is easy to keep 1388146Sroot * periodic real-time signals from drifting. 1398146Sroot * 1408146Sroot * Virtual time timers are processed in the hardclock() routine of 1418146Sroot * kern_clock.c. The real time timer is processed by a timeout 1428146Sroot * routine, called from the softclock() routine. Since a callout 1438146Sroot * may be delayed in real time due to interrupt processing in the system, 1448146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1458146Sroot * to be delayed in real time past when it is supposed to occur. It 1468146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1478146Sroot * real time timers .it_interval. Rather, we compute the next time in 1488146Sroot * absolute time the timer should go off. 1498146Sroot */ 15043392Skarels /* ARGSUSED */ 15143392Skarels getitimer(p, uap, retval) 15243392Skarels struct proc *p; 15343392Skarels register struct args { 1548034Sroot u_int which; 1558034Sroot struct itimerval *itv; 15643392Skarels } *uap; 15743392Skarels int *retval; 15843392Skarels { 1598114Sroot struct itimerval aitv; 1608034Sroot int s; 1617424Sroot 16243392Skarels if (uap->which > ITIMER_PROF) 16344405Skarels return (EINVAL); 16425897Skarels s = splclock(); 1658114Sroot if (uap->which == ITIMER_REAL) { 1668146Sroot /* 1678146Sroot * Convert from absoulte to relative time in .it_value 1688146Sroot * part of real time timer. If time for real time timer 1698146Sroot * has passed return 0, else return difference between 1708146Sroot * current time and time for the timer to go off. 1718146Sroot */ 17243392Skarels aitv = p->p_realtimer; 1738114Sroot if (timerisset(&aitv.it_value)) 1748114Sroot if (timercmp(&aitv.it_value, &time, <)) 1758114Sroot timerclear(&aitv.it_value); 1768114Sroot else 1778114Sroot timevalsub(&aitv.it_value, &time); 1788114Sroot } else 17947540Skarels aitv = p->p_stats->p_timer[uap->which]; 1808114Sroot splx(s); 18144405Skarels return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, 18243392Skarels sizeof (struct itimerval))); 1837424Sroot } 1847424Sroot 18543392Skarels /* ARGSUSED */ 18643392Skarels setitimer(p, uap, retval) 18743392Skarels struct proc *p; 18843392Skarels register struct args { 1898034Sroot u_int which; 1908103Sroot struct itimerval *itv, *oitv; 19143392Skarels } *uap; 19243392Skarels int *retval; 19343392Skarels { 19437591Smckusick struct itimerval aitv; 19537591Smckusick register struct itimerval *itvp; 19643392Skarels int s, error; 1977424Sroot 19843392Skarels if (uap->which > ITIMER_PROF) 19944405Skarels return (EINVAL); 20037591Smckusick itvp = uap->itv; 20143392Skarels if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 20237591Smckusick sizeof(struct itimerval)))) 20344405Skarels return (error); 20443392Skarels if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval))) 20544405Skarels return (error); 20637591Smckusick if (itvp == 0) 20743392Skarels return (0); 20843392Skarels if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 20944405Skarels return (EINVAL); 21025897Skarels s = splclock(); 2118114Sroot if (uap->which == ITIMER_REAL) { 2128625Sroot untimeout(realitexpire, (caddr_t)p); 2138114Sroot if (timerisset(&aitv.it_value)) { 2148114Sroot timevaladd(&aitv.it_value, &time); 2158625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2168114Sroot } 2178114Sroot p->p_realtimer = aitv; 2188114Sroot } else 21947540Skarels p->p_stats->p_timer[uap->which] = aitv; 2208034Sroot splx(s); 22144405Skarels return (0); 2227424Sroot } 2237424Sroot 2248146Sroot /* 2258146Sroot * Real interval timer expired: 2268146Sroot * send process whose timer expired an alarm signal. 2278146Sroot * If time is not set up to reload, then just return. 2288146Sroot * Else compute next time timer should go off which is > current time. 2298146Sroot * This is where delay in processing this timeout causes multiple 2308146Sroot * SIGALRM calls to be compressed into one. 2318146Sroot */ 2328146Sroot realitexpire(p) 2338114Sroot register struct proc *p; 2348114Sroot { 2358114Sroot int s; 2368114Sroot 2378114Sroot psignal(p, SIGALRM); 2388114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2398114Sroot timerclear(&p->p_realtimer.it_value); 2408114Sroot return; 2418114Sroot } 2428114Sroot for (;;) { 24325897Skarels s = splclock(); 2448114Sroot timevaladd(&p->p_realtimer.it_value, 2458114Sroot &p->p_realtimer.it_interval); 2468114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2478625Sroot timeout(realitexpire, (caddr_t)p, 2488625Sroot hzto(&p->p_realtimer.it_value)); 2498114Sroot splx(s); 2508114Sroot return; 2518114Sroot } 2528114Sroot splx(s); 2538114Sroot } 2548114Sroot } 2558114Sroot 2568146Sroot /* 2578146Sroot * Check that a proposed value to load into the .it_value or 2588146Sroot * .it_interval part of an interval timer is acceptable, and 2598146Sroot * fix it to have at least minimal value (i.e. if it is less 2608146Sroot * than the resolution of the clock, round it up.) 2618146Sroot */ 2628103Sroot itimerfix(tv) 2638103Sroot struct timeval *tv; 2647424Sroot { 2658034Sroot 2668114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2678114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2688103Sroot return (EINVAL); 26912970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 2708103Sroot tv->tv_usec = tick; 2718103Sroot return (0); 2728034Sroot } 2738034Sroot 2748146Sroot /* 2758146Sroot * Decrement an interval timer by a specified number 2768146Sroot * of microseconds, which must be less than a second, 2778146Sroot * i.e. < 1000000. If the timer expires, then reload 2788146Sroot * it. In this case, carry over (usec - old value) to 2798146Sroot * reducint the value reloaded into the timer so that 2808146Sroot * the timer does not drift. This routine assumes 2818146Sroot * that it is called in a context where the timers 2828146Sroot * on which it is operating cannot change in value. 2838146Sroot */ 2848034Sroot itimerdecr(itp, usec) 2858034Sroot register struct itimerval *itp; 2868034Sroot int usec; 2878034Sroot { 2888034Sroot 2898103Sroot if (itp->it_value.tv_usec < usec) { 2908103Sroot if (itp->it_value.tv_sec == 0) { 2918146Sroot /* expired, and already in next interval */ 2928103Sroot usec -= itp->it_value.tv_usec; 2938034Sroot goto expire; 2948103Sroot } 2958103Sroot itp->it_value.tv_usec += 1000000; 2968103Sroot itp->it_value.tv_sec--; 2978034Sroot } 2988103Sroot itp->it_value.tv_usec -= usec; 2998103Sroot usec = 0; 3008103Sroot if (timerisset(&itp->it_value)) 3018034Sroot return (1); 3028146Sroot /* expired, exactly at end of interval */ 3038034Sroot expire: 3048103Sroot if (timerisset(&itp->it_interval)) { 3058103Sroot itp->it_value = itp->it_interval; 3068103Sroot itp->it_value.tv_usec -= usec; 3078103Sroot if (itp->it_value.tv_usec < 0) { 3088103Sroot itp->it_value.tv_usec += 1000000; 3098103Sroot itp->it_value.tv_sec--; 3108103Sroot } 3118103Sroot } else 3128146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 3138034Sroot return (0); 3148034Sroot } 3158034Sroot 3168146Sroot /* 3178146Sroot * Add and subtract routines for timevals. 3188146Sroot * N.B.: subtract routine doesn't deal with 3198146Sroot * results which are before the beginning, 3208146Sroot * it just gets very confused in this case. 3218146Sroot * Caveat emptor. 3228146Sroot */ 3238146Sroot timevaladd(t1, t2) 3248146Sroot struct timeval *t1, *t2; 3258146Sroot { 3268146Sroot 3278146Sroot t1->tv_sec += t2->tv_sec; 3288146Sroot t1->tv_usec += t2->tv_usec; 3298146Sroot timevalfix(t1); 3308146Sroot } 3318146Sroot 3328146Sroot timevalsub(t1, t2) 3338146Sroot struct timeval *t1, *t2; 3348146Sroot { 3358146Sroot 3368146Sroot t1->tv_sec -= t2->tv_sec; 3378146Sroot t1->tv_usec -= t2->tv_usec; 3388146Sroot timevalfix(t1); 3398146Sroot } 3408146Sroot 3418146Sroot timevalfix(t1) 3428146Sroot struct timeval *t1; 3438146Sroot { 3448146Sroot 3458146Sroot if (t1->tv_usec < 0) { 3468146Sroot t1->tv_sec--; 3478146Sroot t1->tv_usec += 1000000; 3488146Sroot } 3498146Sroot if (t1->tv_usec >= 1000000) { 3508146Sroot t1->tv_sec++; 3518146Sroot t1->tv_usec -= 1000000; 3528146Sroot } 3538146Sroot } 354