123377Smckusick /* 223377Smckusick * Copyright (c) 1982 Regents of the University of California. 323377Smckusick * All rights reserved. The Berkeley software License Agreement 423377Smckusick * specifies the terms and conditions for redistribution. 523377Smckusick * 6*25897Skarels * @(#)kern_time.c 6.8 (Berkeley) 01/13/86 723377Smckusick */ 87424Sroot 99757Ssam #include "../machine/reg.h" 109757Ssam 1117093Sbloom #include "param.h" 1217093Sbloom #include "dir.h" /* XXX */ 1317093Sbloom #include "user.h" 1417093Sbloom #include "kernel.h" 1517093Sbloom #include "inode.h" 1617093Sbloom #include "proc.h" 177424Sroot 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 288034Sroot gettimeofday() 297424Sroot { 308034Sroot register struct a { 318034Sroot struct timeval *tp; 328034Sroot struct timezone *tzp; 338034Sroot } *uap = (struct a *)u.u_ap; 348034Sroot struct timeval atv; 357500Sroot 36*25897Skarels microtime(&atv); 379998Ssam u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv)); 389998Ssam if (u.u_error) 398034Sroot return; 408034Sroot if (uap->tzp == 0) 418034Sroot return; 428103Sroot /* SHOULD HAVE PER-PROCESS TIMEZONE */ 439998Ssam u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, sizeof (tz)); 447500Sroot } 457500Sroot 468034Sroot settimeofday() 477500Sroot { 488034Sroot register struct a { 498103Sroot struct timeval *tv; 508103Sroot struct timezone *tzp; 518034Sroot } *uap = (struct a *)u.u_ap; 528034Sroot struct timeval atv; 538034Sroot struct timezone atz; 547500Sroot 559998Ssam u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 569998Ssam sizeof (struct timeval)); 579998Ssam if (u.u_error) 588034Sroot return; 598103Sroot setthetime(&atv); 608103Sroot if (uap->tzp && suser()) { 619998Ssam u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, 629998Ssam sizeof (atz)); 6316576Ssam if (u.u_error == 0) 6416576Ssam tz = atz; 658034Sroot } 667500Sroot } 677500Sroot 688103Sroot setthetime(tv) 698103Sroot struct timeval *tv; 708103Sroot { 718103Sroot int s; 728103Sroot 738103Sroot if (!suser()) 748103Sroot return; 758146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 768103Sroot boottime.tv_sec += tv->tv_sec - time.tv_sec; 77*25897Skarels s = splhigh(); time = *tv; splx(s); 789007Sroot resettodr(); 798103Sroot } 808103Sroot 8117356Skarels int adjtimedelta; 8225170Skarels extern int tickadj; 8317356Skarels 8417356Skarels adjtime() 8517356Skarels { 8617356Skarels register struct a { 8717356Skarels struct timeval *delta; 8817356Skarels struct timeval *olddelta; 8917356Skarels } *uap = (struct a *)u.u_ap; 9017356Skarels struct timeval atv, oatv; 9125170Skarels int s; 9217356Skarels 9317356Skarels if (!suser()) 9417356Skarels return; 9517356Skarels u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv, 9617356Skarels sizeof (struct timeval)); 9717356Skarels if (u.u_error) 9817356Skarels return; 9925170Skarels s = splclock(); 10017356Skarels if (uap->olddelta) { 10117356Skarels oatv.tv_sec = adjtimedelta / 1000000; 10217356Skarels oatv.tv_usec = adjtimedelta % 1000000; 10317356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 10417356Skarels sizeof (struct timeval)); 10517356Skarels } 10617356Skarels adjtimedelta = atv.tv_sec * 1000000 + atv.tv_usec; 10725170Skarels if (adjtimedelta % tickadj) 10825170Skarels adjtimedelta = adjtimedelta / tickadj * tickadj; 10925170Skarels splx(s); 11017356Skarels } 11117356Skarels 1128146Sroot /* 1138146Sroot * Get value of an interval timer. The process virtual and 1148146Sroot * profiling virtual time timers are kept in the u. area, since 1158146Sroot * they can be swapped out. These are kept internally in the 1168146Sroot * way they are specified externally: in time until they expire. 1178146Sroot * 1188146Sroot * The real time interval timer is kept in the process table slot 1198146Sroot * for the process, and its value (it_value) is kept as an 1208146Sroot * absolute time rather than as a delta, so that it is easy to keep 1218146Sroot * periodic real-time signals from drifting. 1228146Sroot * 1238146Sroot * Virtual time timers are processed in the hardclock() routine of 1248146Sroot * kern_clock.c. The real time timer is processed by a timeout 1258146Sroot * routine, called from the softclock() routine. Since a callout 1268146Sroot * may be delayed in real time due to interrupt processing in the system, 1278146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1288146Sroot * to be delayed in real time past when it is supposed to occur. It 1298146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1308146Sroot * real time timers .it_interval. Rather, we compute the next time in 1318146Sroot * absolute time the timer should go off. 1328146Sroot */ 1338034Sroot getitimer() 1348034Sroot { 1357424Sroot register struct a { 1368034Sroot u_int which; 1378034Sroot struct itimerval *itv; 1388034Sroot } *uap = (struct a *)u.u_ap; 1398114Sroot struct itimerval aitv; 1408034Sroot int s; 1417424Sroot 1428034Sroot if (uap->which > 2) { 1438034Sroot u.u_error = EINVAL; 1448034Sroot return; 1457424Sroot } 146*25897Skarels s = splclock(); 1478114Sroot if (uap->which == ITIMER_REAL) { 1488146Sroot /* 1498146Sroot * Convert from absoulte to relative time in .it_value 1508146Sroot * part of real time timer. If time for real time timer 1518146Sroot * has passed return 0, else return difference between 1528146Sroot * current time and time for the timer to go off. 1538146Sroot */ 1548114Sroot aitv = u.u_procp->p_realtimer; 1558114Sroot if (timerisset(&aitv.it_value)) 1568114Sroot if (timercmp(&aitv.it_value, &time, <)) 1578114Sroot timerclear(&aitv.it_value); 1588114Sroot else 1598114Sroot timevalsub(&aitv.it_value, &time); 1608114Sroot } else 1618114Sroot aitv = u.u_timer[uap->which]; 1628114Sroot splx(s); 1639998Ssam u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv, 1649998Ssam sizeof (struct itimerval)); 1658034Sroot splx(s); 1667424Sroot } 1677424Sroot 1688034Sroot setitimer() 1697424Sroot { 1707424Sroot register struct a { 1718034Sroot u_int which; 1728103Sroot struct itimerval *itv, *oitv; 1738034Sroot } *uap = (struct a *)u.u_ap; 17425227Smckusick struct itimerval aitv, *aitvp; 1758034Sroot int s; 1768114Sroot register struct proc *p = u.u_procp; 1777424Sroot 1788034Sroot if (uap->which > 2) { 1798034Sroot u.u_error = EINVAL; 1808103Sroot return; 1817424Sroot } 18225227Smckusick aitvp = uap->itv; 1838103Sroot if (uap->oitv) { 1848103Sroot uap->itv = uap->oitv; 1858103Sroot getitimer(); 1868103Sroot } 18725227Smckusick if (aitvp == 0) 18825227Smckusick return; 18925227Smckusick u.u_error = copyin(aitvp, (caddr_t)&aitv, sizeof (struct itimerval)); 19025227Smckusick if (u.u_error) 19125227Smckusick return; 1928103Sroot if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) { 1938103Sroot u.u_error = EINVAL; 1948103Sroot return; 1958103Sroot } 196*25897Skarels s = splclock(); 1978114Sroot if (uap->which == ITIMER_REAL) { 1988625Sroot untimeout(realitexpire, (caddr_t)p); 1998114Sroot if (timerisset(&aitv.it_value)) { 2008114Sroot timevaladd(&aitv.it_value, &time); 2018625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2028114Sroot } 2038114Sroot p->p_realtimer = aitv; 2048114Sroot } else 2058103Sroot u.u_timer[uap->which] = aitv; 2068034Sroot splx(s); 2077424Sroot } 2087424Sroot 2098146Sroot /* 2108146Sroot * Real interval timer expired: 2118146Sroot * send process whose timer expired an alarm signal. 2128146Sroot * If time is not set up to reload, then just return. 2138146Sroot * Else compute next time timer should go off which is > current time. 2148146Sroot * This is where delay in processing this timeout causes multiple 2158146Sroot * SIGALRM calls to be compressed into one. 2168146Sroot */ 2178146Sroot realitexpire(p) 2188114Sroot register struct proc *p; 2198114Sroot { 2208114Sroot int s; 2218114Sroot 2228114Sroot psignal(p, SIGALRM); 2238114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2248114Sroot timerclear(&p->p_realtimer.it_value); 2258114Sroot return; 2268114Sroot } 2278114Sroot for (;;) { 228*25897Skarels s = splclock(); 2298114Sroot timevaladd(&p->p_realtimer.it_value, 2308114Sroot &p->p_realtimer.it_interval); 2318114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2328625Sroot timeout(realitexpire, (caddr_t)p, 2338625Sroot hzto(&p->p_realtimer.it_value)); 2348114Sroot splx(s); 2358114Sroot return; 2368114Sroot } 2378114Sroot splx(s); 2388114Sroot } 2398114Sroot } 2408114Sroot 2418146Sroot /* 2428146Sroot * Check that a proposed value to load into the .it_value or 2438146Sroot * .it_interval part of an interval timer is acceptable, and 2448146Sroot * fix it to have at least minimal value (i.e. if it is less 2458146Sroot * than the resolution of the clock, round it up.) 2468146Sroot */ 2478103Sroot itimerfix(tv) 2488103Sroot struct timeval *tv; 2497424Sroot { 2508034Sroot 2518114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2528114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2538103Sroot return (EINVAL); 25412970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 2558103Sroot tv->tv_usec = tick; 2568103Sroot return (0); 2578034Sroot } 2588034Sroot 2598146Sroot /* 2608146Sroot * Decrement an interval timer by a specified number 2618146Sroot * of microseconds, which must be less than a second, 2628146Sroot * i.e. < 1000000. If the timer expires, then reload 2638146Sroot * it. In this case, carry over (usec - old value) to 2648146Sroot * reducint the value reloaded into the timer so that 2658146Sroot * the timer does not drift. This routine assumes 2668146Sroot * that it is called in a context where the timers 2678146Sroot * on which it is operating cannot change in value. 2688146Sroot */ 2698034Sroot itimerdecr(itp, usec) 2708034Sroot register struct itimerval *itp; 2718034Sroot int usec; 2728034Sroot { 2738034Sroot 2748103Sroot if (itp->it_value.tv_usec < usec) { 2758103Sroot if (itp->it_value.tv_sec == 0) { 2768146Sroot /* expired, and already in next interval */ 2778103Sroot usec -= itp->it_value.tv_usec; 2788034Sroot goto expire; 2798103Sroot } 2808103Sroot itp->it_value.tv_usec += 1000000; 2818103Sroot itp->it_value.tv_sec--; 2828034Sroot } 2838103Sroot itp->it_value.tv_usec -= usec; 2848103Sroot usec = 0; 2858103Sroot if (timerisset(&itp->it_value)) 2868034Sroot return (1); 2878146Sroot /* expired, exactly at end of interval */ 2888034Sroot expire: 2898103Sroot if (timerisset(&itp->it_interval)) { 2908103Sroot itp->it_value = itp->it_interval; 2918103Sroot itp->it_value.tv_usec -= usec; 2928103Sroot if (itp->it_value.tv_usec < 0) { 2938103Sroot itp->it_value.tv_usec += 1000000; 2948103Sroot itp->it_value.tv_sec--; 2958103Sroot } 2968103Sroot } else 2978146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 2988034Sroot return (0); 2998034Sroot } 3008034Sroot 3018146Sroot /* 3028146Sroot * Add and subtract routines for timevals. 3038146Sroot * N.B.: subtract routine doesn't deal with 3048146Sroot * results which are before the beginning, 3058146Sroot * it just gets very confused in this case. 3068146Sroot * Caveat emptor. 3078146Sroot */ 3088146Sroot timevaladd(t1, t2) 3098146Sroot struct timeval *t1, *t2; 3108146Sroot { 3118146Sroot 3128146Sroot t1->tv_sec += t2->tv_sec; 3138146Sroot t1->tv_usec += t2->tv_usec; 3148146Sroot timevalfix(t1); 3158146Sroot } 3168146Sroot 3178146Sroot timevalsub(t1, t2) 3188146Sroot struct timeval *t1, *t2; 3198146Sroot { 3208146Sroot 3218146Sroot t1->tv_sec -= t2->tv_sec; 3228146Sroot t1->tv_usec -= t2->tv_usec; 3238146Sroot timevalfix(t1); 3248146Sroot } 3258146Sroot 3268146Sroot timevalfix(t1) 3278146Sroot struct timeval *t1; 3288146Sroot { 3298146Sroot 3308146Sroot if (t1->tv_usec < 0) { 3318146Sroot t1->tv_sec--; 3328146Sroot t1->tv_usec += 1000000; 3338146Sroot } 3348146Sroot if (t1->tv_usec >= 1000000) { 3358146Sroot t1->tv_sec++; 3368146Sroot t1->tv_usec -= 1000000; 3378146Sroot } 3388146Sroot } 339