123377Smckusick /* 229097Smckusick * Copyright (c) 1982, 1986 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*30666Sbostic * @(#)kern_time.c 7.4 (Berkeley) 03/23/87 723377Smckusick */ 87424Sroot 917093Sbloom #include "param.h" 1017093Sbloom #include "dir.h" /* XXX */ 1117093Sbloom #include "user.h" 1217093Sbloom #include "kernel.h" 1317093Sbloom #include "inode.h" 1417093Sbloom #include "proc.h" 157424Sroot 1629946Skarels #include "../machine/reg.h" 1729946Skarels #include "../machine/cpu.h" 1829946Skarels 198103Sroot /* 208103Sroot * Time of day and interval timer support. 218146Sroot * 228146Sroot * These routines provide the kernel entry points to get and set 238146Sroot * the time-of-day and per-process interval timers. Subroutines 248146Sroot * here provide support for adding and subtracting timeval structures 258146Sroot * and decrementing interval timers, optionally reloading the interval 268146Sroot * timers when they expire. 278103Sroot */ 288103Sroot 298034Sroot gettimeofday() 307424Sroot { 318034Sroot register struct a { 328034Sroot struct timeval *tp; 338034Sroot struct timezone *tzp; 348034Sroot } *uap = (struct a *)u.u_ap; 358034Sroot struct timeval atv; 367500Sroot 37*30666Sbostic if (uap->tp) { 38*30666Sbostic microtime(&atv); 39*30666Sbostic u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 40*30666Sbostic sizeof (atv)); 41*30666Sbostic if (u.u_error) 42*30666Sbostic return; 43*30666Sbostic } 44*30666Sbostic if (uap->tzp) 45*30666Sbostic u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 46*30666Sbostic sizeof (tz)); 477500Sroot } 487500Sroot 498034Sroot settimeofday() 507500Sroot { 518034Sroot register struct a { 528103Sroot struct timeval *tv; 538103Sroot struct timezone *tzp; 548034Sroot } *uap = (struct a *)u.u_ap; 558034Sroot struct timeval atv; 568034Sroot struct timezone atz; 577500Sroot 58*30666Sbostic if (uap->tv) { 59*30666Sbostic u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 60*30666Sbostic sizeof (struct timeval)); 61*30666Sbostic if (u.u_error) 62*30666Sbostic return; 63*30666Sbostic setthetime(&atv); 64*30666Sbostic } 658103Sroot if (uap->tzp && suser()) { 669998Ssam u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, 679998Ssam sizeof (atz)); 6816576Ssam if (u.u_error == 0) 6916576Ssam tz = atz; 708034Sroot } 717500Sroot } 727500Sroot 738103Sroot setthetime(tv) 748103Sroot struct timeval *tv; 758103Sroot { 768103Sroot int s; 778103Sroot 788103Sroot if (!suser()) 798103Sroot return; 808146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 818103Sroot boottime.tv_sec += tv->tv_sec - time.tv_sec; 8225897Skarels s = splhigh(); time = *tv; splx(s); 839007Sroot resettodr(); 848103Sroot } 858103Sroot 8628829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 8728829Skarels int tickdelta; /* current clock skew, us. per tick */ 8828829Skarels long timedelta; /* unapplied time correction, us. */ 8928829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 9017356Skarels 9117356Skarels adjtime() 9217356Skarels { 9317356Skarels register struct a { 9417356Skarels struct timeval *delta; 9517356Skarels struct timeval *olddelta; 9617356Skarels } *uap = (struct a *)u.u_ap; 9717356Skarels struct timeval atv, oatv; 9828829Skarels register long ndelta; 9925170Skarels int s; 10017356Skarels 10117356Skarels if (!suser()) 10217356Skarels return; 10317356Skarels u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv, 10417356Skarels sizeof (struct timeval)); 10517356Skarels if (u.u_error) 10617356Skarels return; 10728829Skarels ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 10828829Skarels if (timedelta == 0) 10928829Skarels if (ndelta > bigadj) 11028829Skarels tickdelta = 10 * tickadj; 11128829Skarels else 11228829Skarels tickdelta = tickadj; 11328829Skarels if (ndelta % tickdelta) 11428829Skarels ndelta = ndelta / tickadj * tickadj; 11528829Skarels 11625170Skarels s = splclock(); 11717356Skarels if (uap->olddelta) { 11828829Skarels oatv.tv_sec = timedelta / 1000000; 11928829Skarels oatv.tv_usec = timedelta % 1000000; 12028829Skarels } 12128829Skarels timedelta = ndelta; 12228829Skarels splx(s); 12328829Skarels 12428829Skarels if (uap->olddelta) 12517356Skarels (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, 12617356Skarels sizeof (struct timeval)); 12717356Skarels } 12817356Skarels 1298146Sroot /* 1308146Sroot * Get value of an interval timer. The process virtual and 1318146Sroot * profiling virtual time timers are kept in the u. 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 */ 1508034Sroot getitimer() 1518034Sroot { 1527424Sroot register struct a { 1538034Sroot u_int which; 1548034Sroot struct itimerval *itv; 1558034Sroot } *uap = (struct a *)u.u_ap; 1568114Sroot struct itimerval aitv; 1578034Sroot int s; 1587424Sroot 1598034Sroot if (uap->which > 2) { 1608034Sroot u.u_error = EINVAL; 1618034Sroot return; 1627424Sroot } 16325897Skarels s = splclock(); 1648114Sroot if (uap->which == ITIMER_REAL) { 1658146Sroot /* 1668146Sroot * Convert from absoulte to relative time in .it_value 1678146Sroot * part of real time timer. If time for real time timer 1688146Sroot * has passed return 0, else return difference between 1698146Sroot * current time and time for the timer to go off. 1708146Sroot */ 1718114Sroot aitv = u.u_procp->p_realtimer; 1728114Sroot if (timerisset(&aitv.it_value)) 1738114Sroot if (timercmp(&aitv.it_value, &time, <)) 1748114Sroot timerclear(&aitv.it_value); 1758114Sroot else 1768114Sroot timevalsub(&aitv.it_value, &time); 1778114Sroot } else 1788114Sroot aitv = u.u_timer[uap->which]; 1798114Sroot splx(s); 1809998Ssam u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv, 1819998Ssam sizeof (struct itimerval)); 1827424Sroot } 1837424Sroot 1848034Sroot setitimer() 1857424Sroot { 1867424Sroot register struct a { 1878034Sroot u_int which; 1888103Sroot struct itimerval *itv, *oitv; 1898034Sroot } *uap = (struct a *)u.u_ap; 19025227Smckusick struct itimerval aitv, *aitvp; 1918034Sroot int s; 1928114Sroot register struct proc *p = u.u_procp; 1937424Sroot 1948034Sroot if (uap->which > 2) { 1958034Sroot u.u_error = EINVAL; 1968103Sroot return; 1977424Sroot } 19825227Smckusick aitvp = uap->itv; 1998103Sroot if (uap->oitv) { 2008103Sroot uap->itv = uap->oitv; 2018103Sroot getitimer(); 2028103Sroot } 20325227Smckusick if (aitvp == 0) 20425227Smckusick return; 20526355Skarels u.u_error = copyin((caddr_t)aitvp, (caddr_t)&aitv, 20626355Skarels sizeof (struct itimerval)); 20725227Smckusick if (u.u_error) 20825227Smckusick return; 2098103Sroot if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) { 2108103Sroot u.u_error = EINVAL; 2118103Sroot return; 2128103Sroot } 21325897Skarels s = splclock(); 2148114Sroot if (uap->which == ITIMER_REAL) { 2158625Sroot untimeout(realitexpire, (caddr_t)p); 2168114Sroot if (timerisset(&aitv.it_value)) { 2178114Sroot timevaladd(&aitv.it_value, &time); 2188625Sroot timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 2198114Sroot } 2208114Sroot p->p_realtimer = aitv; 2218114Sroot } else 2228103Sroot u.u_timer[uap->which] = aitv; 2238034Sroot splx(s); 2247424Sroot } 2257424Sroot 2268146Sroot /* 2278146Sroot * Real interval timer expired: 2288146Sroot * send process whose timer expired an alarm signal. 2298146Sroot * If time is not set up to reload, then just return. 2308146Sroot * Else compute next time timer should go off which is > current time. 2318146Sroot * This is where delay in processing this timeout causes multiple 2328146Sroot * SIGALRM calls to be compressed into one. 2338146Sroot */ 2348146Sroot realitexpire(p) 2358114Sroot register struct proc *p; 2368114Sroot { 2378114Sroot int s; 2388114Sroot 2398114Sroot psignal(p, SIGALRM); 2408114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 2418114Sroot timerclear(&p->p_realtimer.it_value); 2428114Sroot return; 2438114Sroot } 2448114Sroot for (;;) { 24525897Skarels s = splclock(); 2468114Sroot timevaladd(&p->p_realtimer.it_value, 2478114Sroot &p->p_realtimer.it_interval); 2488114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 2498625Sroot timeout(realitexpire, (caddr_t)p, 2508625Sroot hzto(&p->p_realtimer.it_value)); 2518114Sroot splx(s); 2528114Sroot return; 2538114Sroot } 2548114Sroot splx(s); 2558114Sroot } 2568114Sroot } 2578114Sroot 2588146Sroot /* 2598146Sroot * Check that a proposed value to load into the .it_value or 2608146Sroot * .it_interval part of an interval timer is acceptable, and 2618146Sroot * fix it to have at least minimal value (i.e. if it is less 2628146Sroot * than the resolution of the clock, round it up.) 2638146Sroot */ 2648103Sroot itimerfix(tv) 2658103Sroot struct timeval *tv; 2667424Sroot { 2678034Sroot 2688114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2698114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2708103Sroot return (EINVAL); 27112970Ssam if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 2728103Sroot tv->tv_usec = tick; 2738103Sroot return (0); 2748034Sroot } 2758034Sroot 2768146Sroot /* 2778146Sroot * Decrement an interval timer by a specified number 2788146Sroot * of microseconds, which must be less than a second, 2798146Sroot * i.e. < 1000000. If the timer expires, then reload 2808146Sroot * it. In this case, carry over (usec - old value) to 2818146Sroot * reducint the value reloaded into the timer so that 2828146Sroot * the timer does not drift. This routine assumes 2838146Sroot * that it is called in a context where the timers 2848146Sroot * on which it is operating cannot change in value. 2858146Sroot */ 2868034Sroot itimerdecr(itp, usec) 2878034Sroot register struct itimerval *itp; 2888034Sroot int usec; 2898034Sroot { 2908034Sroot 2918103Sroot if (itp->it_value.tv_usec < usec) { 2928103Sroot if (itp->it_value.tv_sec == 0) { 2938146Sroot /* expired, and already in next interval */ 2948103Sroot usec -= itp->it_value.tv_usec; 2958034Sroot goto expire; 2968103Sroot } 2978103Sroot itp->it_value.tv_usec += 1000000; 2988103Sroot itp->it_value.tv_sec--; 2998034Sroot } 3008103Sroot itp->it_value.tv_usec -= usec; 3018103Sroot usec = 0; 3028103Sroot if (timerisset(&itp->it_value)) 3038034Sroot return (1); 3048146Sroot /* expired, exactly at end of interval */ 3058034Sroot expire: 3068103Sroot if (timerisset(&itp->it_interval)) { 3078103Sroot itp->it_value = itp->it_interval; 3088103Sroot itp->it_value.tv_usec -= usec; 3098103Sroot if (itp->it_value.tv_usec < 0) { 3108103Sroot itp->it_value.tv_usec += 1000000; 3118103Sroot itp->it_value.tv_sec--; 3128103Sroot } 3138103Sroot } else 3148146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 3158034Sroot return (0); 3168034Sroot } 3178034Sroot 3188146Sroot /* 3198146Sroot * Add and subtract routines for timevals. 3208146Sroot * N.B.: subtract routine doesn't deal with 3218146Sroot * results which are before the beginning, 3228146Sroot * it just gets very confused in this case. 3238146Sroot * Caveat emptor. 3248146Sroot */ 3258146Sroot timevaladd(t1, t2) 3268146Sroot struct timeval *t1, *t2; 3278146Sroot { 3288146Sroot 3298146Sroot t1->tv_sec += t2->tv_sec; 3308146Sroot t1->tv_usec += t2->tv_usec; 3318146Sroot timevalfix(t1); 3328146Sroot } 3338146Sroot 3348146Sroot timevalsub(t1, t2) 3358146Sroot struct timeval *t1, *t2; 3368146Sroot { 3378146Sroot 3388146Sroot t1->tv_sec -= t2->tv_sec; 3398146Sroot t1->tv_usec -= t2->tv_usec; 3408146Sroot timevalfix(t1); 3418146Sroot } 3428146Sroot 3438146Sroot timevalfix(t1) 3448146Sroot struct timeval *t1; 3458146Sroot { 3468146Sroot 3478146Sroot if (t1->tv_usec < 0) { 3488146Sroot t1->tv_sec--; 3498146Sroot t1->tv_usec += 1000000; 3508146Sroot } 3518146Sroot if (t1->tv_usec >= 1000000) { 3528146Sroot t1->tv_sec++; 3538146Sroot t1->tv_usec -= 1000000; 3548146Sroot } 3558146Sroot } 356