123377Smckusick /* 2*37583Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3*37583Smckusick * All rights reserved. 423377Smckusick * 5*37583Smckusick * Redistribution and use in source and binary forms are permitted 6*37583Smckusick * provided that the above copyright notice and this paragraph are 7*37583Smckusick * duplicated in all such forms and that any documentation, 8*37583Smckusick * advertising materials, and other materials related to such 9*37583Smckusick * distribution and use acknowledge that the software was developed 10*37583Smckusick * by the University of California, Berkeley. The name of the 11*37583Smckusick * University may not be used to endorse or promote products derived 12*37583Smckusick * from this software without specific prior written permission. 13*37583Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*37583Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*37583Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16*37583Smckusick * 17*37583Smckusick * @(#)kern_time.c 7.5.1.1 (Berkeley) 05/01/89 1823377Smckusick */ 197424Sroot 2017093Sbloom #include "param.h" 2117093Sbloom #include "dir.h" /* XXX */ 2217093Sbloom #include "user.h" 2317093Sbloom #include "kernel.h" 2417093Sbloom #include "proc.h" 257424Sroot 2637520Smckusick #include "machine/reg.h" 2737520Smckusick #include "machine/cpu.h" 2829946Skarels 298103Sroot /* 308103Sroot * Time of day and interval timer support. 318146Sroot * 328146Sroot * These routines provide the kernel entry points to get and set 338146Sroot * the time-of-day and per-process interval timers. Subroutines 348146Sroot * here provide support for adding and subtracting timeval structures 358146Sroot * and decrementing interval timers, optionally reloading the interval 368146Sroot * timers when they expire. 378103Sroot */ 388103Sroot 398034Sroot gettimeofday() 407424Sroot { 418034Sroot register struct a { 428034Sroot struct timeval *tp; 438034Sroot struct timezone *tzp; 448034Sroot } *uap = (struct a *)u.u_ap; 458034Sroot struct timeval atv; 467500Sroot 4730666Sbostic if (uap->tp) { 4830666Sbostic microtime(&atv); 4930666Sbostic u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 5030666Sbostic sizeof (atv)); 5130666Sbostic if (u.u_error) 5230666Sbostic return; 5330666Sbostic } 5430666Sbostic if (uap->tzp) 5530666Sbostic u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 5630666Sbostic sizeof (tz)); 577500Sroot } 587500Sroot 598034Sroot settimeofday() 607500Sroot { 618034Sroot register struct a { 628103Sroot struct timeval *tv; 638103Sroot struct timezone *tzp; 648034Sroot } *uap = (struct a *)u.u_ap; 658034Sroot struct timeval atv; 668034Sroot struct timezone atz; 67*37583Smckusick int s; 687500Sroot 69*37583Smckusick if (u.u_error = suser(u.u_cred, &u.u_acflag)) 70*37583Smckusick return; 7130666Sbostic if (uap->tv) { 7230666Sbostic u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 7330666Sbostic sizeof (struct timeval)); 7430666Sbostic if (u.u_error) 7530666Sbostic return; 76*37583Smckusick /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 77*37583Smckusick boottime.tv_sec += atv.tv_sec - time.tv_sec; 78*37583Smckusick s = splhigh(); time = atv; splx(s); 79*37583Smckusick resettodr(); 8030666Sbostic } 81*37583Smckusick if (uap->tzp) { 82*37583Smckusick u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, 83*37583Smckusick sizeof (atz)); 84*37583Smckusick if (u.u_error == 0) 85*37583Smckusick tz = atz; 86*37583Smckusick } 877500Sroot } 887500Sroot 8928829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 9028829Skarels int tickdelta; /* current clock skew, us. per tick */ 9128829Skarels long timedelta; /* unapplied time correction, us. */ 9228829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 9317356Skarels 9417356Skarels adjtime() 9517356Skarels { 9617356Skarels register struct a { 9717356Skarels struct timeval *delta; 9817356Skarels struct timeval *olddelta; 9917356Skarels } *uap = (struct a *)u.u_ap; 10017356Skarels struct timeval atv, oatv; 10128829Skarels register long ndelta; 10225170Skarels int s; 10317356Skarels 10437553Smckusick if (u.u_error = suser(u.u_cred, &u.u_acflag)) 10517356Skarels return; 10617356Skarels u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv, 10717356Skarels sizeof (struct timeval)); 10817356Skarels if (u.u_error) 10917356Skarels return; 11028829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 11128829Skarels if (timedelta == 0) 11228829Skarels if (ndelta > bigadj) 11328829Skarels tickdelta = 10 * tickadj; 11428829Skarels else 11528829Skarels tickdelta = tickadj; 11628829Skarels if (ndelta % tickdelta) 11728829Skarels ndelta = ndelta / tickadj * tickadj; 11828829Skarels 11925170Skarels s = splclock(); 12017356Skarels if (uap->olddelta) { 12128829Skarels oatv.tv_sec = timedelta / 1000000; 12228829Skarels oatv.tv_usec = timedelta % 1000000; 12328829Skarels } 12428829Skarels timedelta = ndelta; 12528829Skarels splx(s); 12628829Skarels 12728829Skarels if (uap->olddelta) 12817356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 12917356Skarels sizeof (struct timeval)); 13017356Skarels } 13117356Skarels 1328146Sroot /* 1338146Sroot * Get value of an interval timer. The process virtual and 1348146Sroot * profiling virtual time timers are kept in the u. area, since 1358146Sroot * they can be swapped out. These are kept internally in the 1368146Sroot * way they are specified externally: in time until they expire. 1378146Sroot * 1388146Sroot * The real time interval timer is kept in the process table slot 1398146Sroot * for the process, and its value (it_value) is kept as an 1408146Sroot * absolute time rather than as a delta, so that it is easy to keep 1418146Sroot * periodic real-time signals from drifting. 1428146Sroot * 1438146Sroot * Virtual time timers are processed in the hardclock() routine of 1448146Sroot * kern_clock.c. The real time timer is processed by a timeout 1458146Sroot * routine, called from the softclock() routine. Since a callout 1468146Sroot * may be delayed in real time due to interrupt processing in the system, 1478146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1488146Sroot * to be delayed in real time past when it is supposed to occur. It 1498146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1508146Sroot * real time timers .it_interval. Rather, we compute the next time in 1518146Sroot * absolute time the timer should go off. 1528146Sroot */ 1538034Sroot getitimer() 1548034Sroot { 1557424Sroot register struct a { 1568034Sroot u_int which; 1578034Sroot struct itimerval *itv; 1588034Sroot } *uap = (struct a *)u.u_ap; 1598114Sroot struct itimerval aitv; 1608034Sroot int s; 1617424Sroot 162*37583Smckusick if (uap->which > 2) { 1638034Sroot u.u_error = EINVAL; 1648034Sroot return; 1657424Sroot } 16625897Skarels s = splclock(); 1678114Sroot if (uap->which == ITIMER_REAL) { 1688146Sroot /* 1698146Sroot * Convert from absoulte to relative time in .it_value 1708146Sroot * part of real time timer. If time for real time timer 1718146Sroot * has passed return 0, else return difference between 1728146Sroot * current time and time for the timer to go off. 1738146Sroot */ 1748114Sroot aitv = u.u_procp->p_realtimer; 1758114Sroot if (timerisset(&aitv.it_value)) 1768114Sroot if (timercmp(&aitv.it_value, &time, <)) 1778114Sroot timerclear(&aitv.it_value); 1788114Sroot else 1798114Sroot timevalsub(&aitv.it_value, &time); 1808114Sroot } else 1818114Sroot aitv = u.u_timer[uap->which]; 1828114Sroot splx(s); 1839998Ssam u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv, 1849998Ssam sizeof (struct itimerval)); 1857424Sroot } 1867424Sroot 1878034Sroot setitimer() 1887424Sroot { 1897424Sroot register struct a { 1908034Sroot u_int which; 1918103Sroot struct itimerval *itv, *oitv; 1928034Sroot } *uap = (struct a *)u.u_ap; 193*37583Smckusick struct itimerval aitv, *aitvp; 1948034Sroot int s; 1958114Sroot register struct proc *p = u.u_procp; 1967424Sroot 197*37583Smckusick if (uap->which > 2) { 1988034Sroot u.u_error = EINVAL; 1998103Sroot return; 2007424Sroot } 201*37583Smckusick aitvp = uap->itv; 202*37583Smckusick if (uap->oitv) { 203*37583Smckusick uap->itv = uap->oitv; 2048103Sroot getitimer(); 2058103Sroot } 206*37583Smckusick if (aitvp == 0) 20725227Smckusick return; 208*37583Smckusick u.u_error = copyin((caddr_t)aitvp, (caddr_t)&aitv, 209*37583Smckusick sizeof (struct itimerval)); 210*37583Smckusick if (u.u_error) 211*37583Smckusick return; 2128103Sroot if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) { 2138103Sroot u.u_error = EINVAL; 2148103Sroot return; 2158103Sroot } 21625897Skarels s = splclock(); 2178114Sroot if (uap->which == ITIMER_REAL) { 2188625Sroot untimeout(realitexpire, (caddr_t)p); 2198114Sroot if (timerisset(&aitv.it_value)) { 2208114Sroot timevaladd(&aitv.it_value, &time); 2218625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2228114Sroot } 2238114Sroot p->p_realtimer = aitv; 2248114Sroot } else 2258103Sroot u.u_timer[uap->which] = aitv; 2268034Sroot splx(s); 2277424Sroot } 2287424Sroot 2298146Sroot /* 2308146Sroot * Real interval timer expired: 2318146Sroot * send process whose timer expired an alarm signal. 2328146Sroot * If time is not set up to reload, then just return. 2338146Sroot * Else compute next time timer should go off which is > current time. 2348146Sroot * This is where delay in processing this timeout causes multiple 2358146Sroot * SIGALRM calls to be compressed into one. 2368146Sroot */ 2378146Sroot realitexpire(p) 2388114Sroot register struct proc *p; 2398114Sroot { 2408114Sroot int s; 2418114Sroot 2428114Sroot psignal(p, SIGALRM); 2438114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2448114Sroot timerclear(&p->p_realtimer.it_value); 2458114Sroot return; 2468114Sroot } 2478114Sroot for (;;) { 24825897Skarels s = splclock(); 2498114Sroot timevaladd(&p->p_realtimer.it_value, 2508114Sroot &p->p_realtimer.it_interval); 2518114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2528625Sroot timeout(realitexpire, (caddr_t)p, 2538625Sroot hzto(&p->p_realtimer.it_value)); 2548114Sroot splx(s); 2558114Sroot return; 2568114Sroot } 2578114Sroot splx(s); 2588114Sroot } 2598114Sroot } 2608114Sroot 2618146Sroot /* 2628146Sroot * Check that a proposed value to load into the .it_value or 2638146Sroot * .it_interval part of an interval timer is acceptable, and 2648146Sroot * fix it to have at least minimal value (i.e. if it is less 2658146Sroot * than the resolution of the clock, round it up.) 2668146Sroot */ 2678103Sroot itimerfix(tv) 2688103Sroot struct timeval *tv; 2697424Sroot { 2708034Sroot 2718114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2728114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2738103Sroot return (EINVAL); 27412970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 2758103Sroot tv->tv_usec = tick; 2768103Sroot return (0); 2778034Sroot } 2788034Sroot 2798146Sroot /* 2808146Sroot * Decrement an interval timer by a specified number 2818146Sroot * of microseconds, which must be less than a second, 2828146Sroot * i.e. < 1000000. If the timer expires, then reload 2838146Sroot * it. In this case, carry over (usec - old value) to 2848146Sroot * reducint the value reloaded into the timer so that 2858146Sroot * the timer does not drift. This routine assumes 2868146Sroot * that it is called in a context where the timers 2878146Sroot * on which it is operating cannot change in value. 2888146Sroot */ 2898034Sroot itimerdecr(itp, usec) 2908034Sroot register struct itimerval *itp; 2918034Sroot int usec; 2928034Sroot { 2938034Sroot 2948103Sroot if (itp->it_value.tv_usec < usec) { 2958103Sroot if (itp->it_value.tv_sec == 0) { 2968146Sroot /* expired, and already in next interval */ 2978103Sroot usec -= itp->it_value.tv_usec; 2988034Sroot goto expire; 2998103Sroot } 3008103Sroot itp->it_value.tv_usec += 1000000; 3018103Sroot itp->it_value.tv_sec--; 3028034Sroot } 3038103Sroot itp->it_value.tv_usec -= usec; 3048103Sroot usec = 0; 3058103Sroot if (timerisset(&itp->it_value)) 3068034Sroot return (1); 3078146Sroot /* expired, exactly at end of interval */ 3088034Sroot expire: 3098103Sroot if (timerisset(&itp->it_interval)) { 3108103Sroot itp->it_value = itp->it_interval; 3118103Sroot itp->it_value.tv_usec -= usec; 3128103Sroot if (itp->it_value.tv_usec < 0) { 3138103Sroot itp->it_value.tv_usec += 1000000; 3148103Sroot itp->it_value.tv_sec--; 3158103Sroot } 3168103Sroot } else 3178146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 3188034Sroot return (0); 3198034Sroot } 3208034Sroot 3218146Sroot /* 3228146Sroot * Add and subtract routines for timevals. 3238146Sroot * N.B.: subtract routine doesn't deal with 3248146Sroot * results which are before the beginning, 3258146Sroot * it just gets very confused in this case. 3268146Sroot * Caveat emptor. 3278146Sroot */ 3288146Sroot timevaladd(t1, t2) 3298146Sroot struct timeval *t1, *t2; 3308146Sroot { 3318146Sroot 3328146Sroot t1->tv_sec += t2->tv_sec; 3338146Sroot t1->tv_usec += t2->tv_usec; 3348146Sroot timevalfix(t1); 3358146Sroot } 3368146Sroot 3378146Sroot timevalsub(t1, t2) 3388146Sroot struct timeval *t1, *t2; 3398146Sroot { 3408146Sroot 3418146Sroot t1->tv_sec -= t2->tv_sec; 3428146Sroot t1->tv_usec -= t2->tv_usec; 3438146Sroot timevalfix(t1); 3448146Sroot } 3458146Sroot 3468146Sroot timevalfix(t1) 3478146Sroot struct timeval *t1; 3488146Sroot { 3498146Sroot 3508146Sroot if (t1->tv_usec < 0) { 3518146Sroot t1->tv_sec--; 3528146Sroot t1->tv_usec += 1000000; 3538146Sroot } 3548146Sroot if (t1->tv_usec >= 1000000) { 3558146Sroot t1->tv_sec++; 3568146Sroot t1->tv_usec -= 1000000; 3578146Sroot } 3588146Sroot } 359