123377Smckusick /* 237583Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337583Smckusick * All rights reserved. 423377Smckusick * 537583Smckusick * Redistribution and use in source and binary forms are permitted 637583Smckusick * provided that the above copyright notice and this paragraph are 737583Smckusick * duplicated in all such forms and that any documentation, 837583Smckusick * advertising materials, and other materials related to such 937583Smckusick * distribution and use acknowledge that the software was developed 1037583Smckusick * by the University of California, Berkeley. The name of the 1137583Smckusick * University may not be used to endorse or promote products derived 1237583Smckusick * from this software without specific prior written permission. 1337583Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437583Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537583Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637583Smckusick * 17*37591Smckusick * @(#)kern_time.c 7.9 (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; 6737583Smckusick int s; 687500Sroot 6937583Smckusick if (u.u_error = suser(u.u_cred, &u.u_acflag)) 7037583Smckusick 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; 7637583Smckusick /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 7737583Smckusick boottime.tv_sec += atv.tv_sec - time.tv_sec; 7837583Smckusick s = splhigh(); time = atv; splx(s); 7937583Smckusick resettodr(); 8030666Sbostic } 81*37591Smckusick if (uap->tzp == 0) 82*37591Smckusick return; 83*37591Smckusick u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof (atz)); 84*37591Smckusick if (u.u_error == 0) 85*37591Smckusick tz = atz; 867500Sroot } 877500Sroot 8828829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 8928829Skarels int tickdelta; /* current clock skew, us. per tick */ 9028829Skarels long timedelta; /* unapplied time correction, us. */ 9128829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 9217356Skarels 9317356Skarels adjtime() 9417356Skarels { 9517356Skarels register struct a { 9617356Skarels struct timeval *delta; 9717356Skarels struct timeval *olddelta; 9817356Skarels } *uap = (struct a *)u.u_ap; 9917356Skarels struct timeval atv, oatv; 10028829Skarels register long ndelta; 10125170Skarels int s; 10217356Skarels 10337553Smckusick if (u.u_error = suser(u.u_cred, &u.u_acflag)) 10417356Skarels return; 10517356Skarels u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv, 10617356Skarels sizeof (struct timeval)); 10717356Skarels if (u.u_error) 10817356Skarels return; 10928829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 11028829Skarels if (timedelta == 0) 11128829Skarels if (ndelta > bigadj) 11228829Skarels tickdelta = 10 * tickadj; 11328829Skarels else 11428829Skarels tickdelta = tickadj; 11528829Skarels if (ndelta % tickdelta) 11628829Skarels ndelta = ndelta / tickadj * tickadj; 11728829Skarels 11825170Skarels s = splclock(); 11917356Skarels if (uap->olddelta) { 12028829Skarels oatv.tv_sec = timedelta / 1000000; 12128829Skarels oatv.tv_usec = timedelta % 1000000; 12228829Skarels } 12328829Skarels timedelta = ndelta; 12428829Skarels splx(s); 12528829Skarels 12628829Skarels if (uap->olddelta) 12717356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 12817356Skarels sizeof (struct timeval)); 12917356Skarels } 13017356Skarels 1318146Sroot /* 1328146Sroot * Get value of an interval timer. The process virtual and 1338146Sroot * profiling virtual time timers are kept in the u. area, since 1348146Sroot * they can be swapped out. These are kept internally in the 1358146Sroot * way they are specified externally: in time until they expire. 1368146Sroot * 1378146Sroot * The real time interval timer is kept in the process table slot 1388146Sroot * for the process, and its value (it_value) is kept as an 1398146Sroot * absolute time rather than as a delta, so that it is easy to keep 1408146Sroot * periodic real-time signals from drifting. 1418146Sroot * 1428146Sroot * Virtual time timers are processed in the hardclock() routine of 1438146Sroot * kern_clock.c. The real time timer is processed by a timeout 1448146Sroot * routine, called from the softclock() routine. Since a callout 1458146Sroot * may be delayed in real time due to interrupt processing in the system, 1468146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1478146Sroot * to be delayed in real time past when it is supposed to occur. It 1488146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1498146Sroot * real time timers .it_interval. Rather, we compute the next time in 1508146Sroot * absolute time the timer should go off. 1518146Sroot */ 1528034Sroot getitimer() 1538034Sroot { 1547424Sroot register struct a { 1558034Sroot u_int which; 1568034Sroot struct itimerval *itv; 1578034Sroot } *uap = (struct a *)u.u_ap; 1588114Sroot struct itimerval aitv; 1598034Sroot int s; 1607424Sroot 161*37591Smckusick if (uap->which > ITIMER_PROF) { 1628034Sroot u.u_error = EINVAL; 1638034Sroot return; 1647424Sroot } 16525897Skarels s = splclock(); 1668114Sroot if (uap->which == ITIMER_REAL) { 1678146Sroot /* 1688146Sroot * Convert from absoulte to relative time in .it_value 1698146Sroot * part of real time timer. If time for real time timer 1708146Sroot * has passed return 0, else return difference between 1718146Sroot * current time and time for the timer to go off. 1728146Sroot */ 1738114Sroot aitv = u.u_procp->p_realtimer; 1748114Sroot if (timerisset(&aitv.it_value)) 1758114Sroot if (timercmp(&aitv.it_value, &time, <)) 1768114Sroot timerclear(&aitv.it_value); 1778114Sroot else 1788114Sroot timevalsub(&aitv.it_value, &time); 1798114Sroot } else 1808114Sroot aitv = u.u_timer[uap->which]; 1818114Sroot splx(s); 1829998Ssam u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv, 1839998Ssam sizeof (struct itimerval)); 1847424Sroot } 1857424Sroot 1868034Sroot setitimer() 1877424Sroot { 1887424Sroot register struct a { 1898034Sroot u_int which; 1908103Sroot struct itimerval *itv, *oitv; 1918034Sroot } *uap = (struct a *)u.u_ap; 192*37591Smckusick struct itimerval aitv; 193*37591Smckusick register struct itimerval *itvp; 1948034Sroot int s; 1958114Sroot register struct proc *p = u.u_procp; 1967424Sroot 197*37591Smckusick if (uap->which > ITIMER_PROF) { 1988034Sroot u.u_error = EINVAL; 1998103Sroot return; 2007424Sroot } 201*37591Smckusick itvp = uap->itv; 202*37591Smckusick if (itvp && (u.u_error = copyin((caddr_t)itvp, (caddr_t)&aitv, 203*37591Smckusick sizeof(struct itimerval)))) 204*37591Smckusick return; 205*37591Smckusick if (uap->itv = uap->oitv) { 2068103Sroot getitimer(); 207*37591Smckusick if (u.u_error) 208*37591Smckusick return; 2098103Sroot } 210*37591Smckusick if (itvp == 0) 21125227Smckusick 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