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*36939Sbostic * @(#)kern_time.c 7.6 (Berkeley) 03/01/89 723377Smckusick */ 87424Sroot 917093Sbloom #include "param.h" 1017093Sbloom #include "dir.h" /* XXX */ 1117093Sbloom #include "user.h" 1217093Sbloom #include "kernel.h" 1317093Sbloom #include "proc.h" 147424Sroot 1529946Skarels #include "../machine/reg.h" 1629946Skarels #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 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 3630666Sbostic if (uap->tp) { 3730666Sbostic microtime(&atv); 3830666Sbostic u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 3930666Sbostic sizeof (atv)); 4030666Sbostic if (u.u_error) 4130666Sbostic return; 4230666Sbostic } 4330666Sbostic if (uap->tzp) 4430666Sbostic u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 4530666Sbostic sizeof (tz)); 467500Sroot } 477500Sroot 488034Sroot settimeofday() 497500Sroot { 508034Sroot register struct a { 518103Sroot struct timeval *tv; 528103Sroot struct timezone *tzp; 538034Sroot } *uap = (struct a *)u.u_ap; 548034Sroot struct timeval atv; 558034Sroot struct timezone atz; 567500Sroot 5730666Sbostic if (uap->tv) { 5830666Sbostic u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 5930666Sbostic sizeof (struct timeval)); 6030666Sbostic if (u.u_error) 6130666Sbostic return; 6230666Sbostic setthetime(&atv); 6330666Sbostic } 648103Sroot if (uap->tzp && suser()) { 659998Ssam u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, 669998Ssam sizeof (atz)); 6716576Ssam if (u.u_error == 0) 6816576Ssam tz = atz; 698034Sroot } 707500Sroot } 717500Sroot 728103Sroot setthetime(tv) 738103Sroot struct timeval *tv; 748103Sroot { 758103Sroot int s; 768103Sroot 778103Sroot if (!suser()) 788103Sroot return; 798146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 808103Sroot boottime.tv_sec += tv->tv_sec - time.tv_sec; 8125897Skarels s = splhigh(); time = *tv; splx(s); 829007Sroot resettodr(); 838103Sroot } 848103Sroot 8528829Skarels extern int tickadj; /* "standard" clock skew, us./tick */ 8628829Skarels int tickdelta; /* current clock skew, us. per tick */ 8728829Skarels long timedelta; /* unapplied time correction, us. */ 8828829Skarels long bigadj = 1000000; /* use 10x skew above bigadj us. */ 8917356Skarels 9017356Skarels adjtime() 9117356Skarels { 9217356Skarels register struct a { 9317356Skarels struct timeval *delta; 9417356Skarels struct timeval *olddelta; 9517356Skarels } *uap = (struct a *)u.u_ap; 9617356Skarels struct timeval atv, oatv; 9728829Skarels register long ndelta; 9825170Skarels int s; 9917356Skarels 10017356Skarels if (!suser()) 10117356Skarels return; 10217356Skarels u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv, 10317356Skarels sizeof (struct timeval)); 10417356Skarels if (u.u_error) 10517356Skarels return; 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)); 12617356Skarels } 12717356Skarels 1288146Sroot /* 1298146Sroot * Get value of an interval timer. The process virtual and 1308146Sroot * profiling virtual time timers are kept in the u. area, since 1318146Sroot * they can be swapped out. These are kept internally in the 1328146Sroot * way they are specified externally: in time until they expire. 1338146Sroot * 1348146Sroot * The real time interval timer is kept in the process table slot 1358146Sroot * for the process, and its value (it_value) is kept as an 1368146Sroot * absolute time rather than as a delta, so that it is easy to keep 1378146Sroot * periodic real-time signals from drifting. 1388146Sroot * 1398146Sroot * Virtual time timers are processed in the hardclock() routine of 1408146Sroot * kern_clock.c. The real time timer is processed by a timeout 1418146Sroot * routine, called from the softclock() routine. Since a callout 1428146Sroot * may be delayed in real time due to interrupt processing in the system, 1438146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 1448146Sroot * to be delayed in real time past when it is supposed to occur. It 1458146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 1468146Sroot * real time timers .it_interval. Rather, we compute the next time in 1478146Sroot * absolute time the timer should go off. 1488146Sroot */ 1498034Sroot getitimer() 1508034Sroot { 1517424Sroot register struct a { 1528034Sroot u_int which; 1538034Sroot struct itimerval *itv; 1548034Sroot } *uap = (struct a *)u.u_ap; 1558114Sroot struct itimerval aitv; 1568034Sroot int s; 1577424Sroot 158*36939Sbostic if (uap->which > ITIMER_PROF) { 1598034Sroot u.u_error = EINVAL; 1608034Sroot return; 1617424Sroot } 16225897Skarels s = splclock(); 1638114Sroot if (uap->which == ITIMER_REAL) { 1648146Sroot /* 1658146Sroot * Convert from absoulte to relative time in .it_value 1668146Sroot * part of real time timer. If time for real time timer 1678146Sroot * has passed return 0, else return difference between 1688146Sroot * current time and time for the timer to go off. 1698146Sroot */ 1708114Sroot aitv = u.u_procp->p_realtimer; 1718114Sroot if (timerisset(&aitv.it_value)) 1728114Sroot if (timercmp(&aitv.it_value, &time, <)) 1738114Sroot timerclear(&aitv.it_value); 1748114Sroot else 1758114Sroot timevalsub(&aitv.it_value, &time); 1768114Sroot } else 1778114Sroot aitv = u.u_timer[uap->which]; 1788114Sroot splx(s); 1799998Ssam u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv, 1809998Ssam sizeof (struct itimerval)); 1817424Sroot } 1827424Sroot 1838034Sroot setitimer() 1847424Sroot { 1857424Sroot register struct a { 1868034Sroot u_int which; 1878103Sroot struct itimerval *itv, *oitv; 1888034Sroot } *uap = (struct a *)u.u_ap; 189*36939Sbostic struct itimerval aitv; 190*36939Sbostic register struct itimerval *itvp; 1918034Sroot int s; 1928114Sroot register struct proc *p = u.u_procp; 1937424Sroot 194*36939Sbostic if (uap->which > ITIMER_PROF) { 1958034Sroot u.u_error = EINVAL; 1968103Sroot return; 1977424Sroot } 198*36939Sbostic itvp = uap->itv; 199*36939Sbostic if (itvp && (u.u_error = copyin((caddr_t)itvp, (caddr_t)&aitv, 200*36939Sbostic sizeof(struct itimerval)))) 201*36939Sbostic return; 202*36939Sbostic if (uap->itv = uap->oitv) { 2038103Sroot getitimer(); 204*36939Sbostic if (u.u_error) 205*36939Sbostic return; 2068103Sroot } 207*36939Sbostic if (itvp == 0) 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