1*8146Sroot /* kern_time.c 5.7 82/09/11 */ 27424Sroot 37424Sroot #include "../h/param.h" 48034Sroot #include "../h/dir.h" /* XXX */ 57424Sroot #include "../h/user.h" 68034Sroot #include "../h/kernel.h" 77424Sroot #include "../h/reg.h" 87424Sroot #include "../h/inode.h" 97424Sroot #include "../h/proc.h" 107424Sroot 118103Sroot /* 128103Sroot * Time of day and interval timer support. 13*8146Sroot * 14*8146Sroot * These routines provide the kernel entry points to get and set 15*8146Sroot * the time-of-day and per-process interval timers. Subroutines 16*8146Sroot * here provide support for adding and subtracting timeval structures 17*8146Sroot * and decrementing interval timers, optionally reloading the interval 18*8146Sroot * timers when they expire. 198103Sroot */ 208103Sroot 218034Sroot gettimeofday() 227424Sroot { 238034Sroot register struct a { 248034Sroot struct timeval *tp; 258034Sroot struct timezone *tzp; 268034Sroot } *uap = (struct a *)u.u_ap; 278034Sroot struct timeval atv; 288103Sroot int s; 297500Sroot 308103Sroot s = spl7(); atv = time; splx(s); 318034Sroot if (copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv))) { 328034Sroot u.u_error = EFAULT; 338034Sroot return; 348034Sroot } 358034Sroot if (uap->tzp == 0) 368034Sroot return; 378103Sroot /* SHOULD HAVE PER-PROCESS TIMEZONE */ 388034Sroot if (copyout((caddr_t)&tz, uap->tzp, sizeof (tz))) { 398034Sroot u.u_error = EFAULT; 408034Sroot return; 418034Sroot } 427500Sroot } 437500Sroot 448034Sroot settimeofday() 457500Sroot { 468034Sroot register struct a { 478103Sroot struct timeval *tv; 488103Sroot struct timezone *tzp; 498034Sroot } *uap = (struct a *)u.u_ap; 508034Sroot struct timeval atv; 518034Sroot struct timezone atz; 527500Sroot 538034Sroot if (copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof (struct timeval))) { 548034Sroot u.u_error = EFAULT; 558034Sroot return; 568034Sroot } 578103Sroot setthetime(&atv); 588103Sroot if (uap->tzp && suser()) { 598034Sroot if (copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof (atz))) { 608034Sroot u.u_error = EFAULT; 618034Sroot return; 628034Sroot } 638034Sroot } 647500Sroot } 657500Sroot 668103Sroot setthetime(tv) 678103Sroot struct timeval *tv; 688103Sroot { 698103Sroot register int delta; 708103Sroot int s; 718103Sroot 728103Sroot if (!suser()) 738103Sroot return; 74*8146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 758103Sroot boottime.tv_sec += tv->tv_sec - time.tv_sec; 768103Sroot s = spl7(); time = *tv; splx(s); 778103Sroot clockset(); 788103Sroot } 798103Sroot 80*8146Sroot /* 81*8146Sroot * Get value of an interval timer. The process virtual and 82*8146Sroot * profiling virtual time timers are kept in the u. area, since 83*8146Sroot * they can be swapped out. These are kept internally in the 84*8146Sroot * way they are specified externally: in time until they expire. 85*8146Sroot * 86*8146Sroot * The real time interval timer is kept in the process table slot 87*8146Sroot * for the process, and its value (it_value) is kept as an 88*8146Sroot * absolute time rather than as a delta, so that it is easy to keep 89*8146Sroot * periodic real-time signals from drifting. 90*8146Sroot * 91*8146Sroot * Virtual time timers are processed in the hardclock() routine of 92*8146Sroot * kern_clock.c. The real time timer is processed by a timeout 93*8146Sroot * routine, called from the softclock() routine. Since a callout 94*8146Sroot * may be delayed in real time due to interrupt processing in the system, 95*8146Sroot * it is possible for the real time timeout routine (realitexpire, given below), 96*8146Sroot * to be delayed in real time past when it is supposed to occur. It 97*8146Sroot * does not suffice, therefore, to reload the real timer .it_value from the 98*8146Sroot * real time timers .it_interval. Rather, we compute the next time in 99*8146Sroot * absolute time the timer should go off. 100*8146Sroot */ 1018034Sroot getitimer() 1028034Sroot { 1037424Sroot register struct a { 1048034Sroot u_int which; 1058034Sroot struct itimerval *itv; 1068034Sroot } *uap = (struct a *)u.u_ap; 1078114Sroot struct itimerval aitv; 1088034Sroot int s; 1097424Sroot 1108034Sroot if (uap->which > 2) { 1118034Sroot u.u_error = EINVAL; 1128034Sroot return; 1137424Sroot } 1148034Sroot s = spl7(); 1158114Sroot if (uap->which == ITIMER_REAL) { 116*8146Sroot /* 117*8146Sroot * Convert from absoulte to relative time in .it_value 118*8146Sroot * part of real time timer. If time for real time timer 119*8146Sroot * has passed return 0, else return difference between 120*8146Sroot * current time and time for the timer to go off. 121*8146Sroot */ 1228114Sroot aitv = u.u_procp->p_realtimer; 1238114Sroot if (timerisset(&aitv.it_value)) 1248114Sroot if (timercmp(&aitv.it_value, &time, <)) 1258114Sroot timerclear(&aitv.it_value); 1268114Sroot else 1278114Sroot timevalsub(&aitv.it_value, &time); 1288114Sroot } else 1298114Sroot aitv = u.u_timer[uap->which]; 1308114Sroot splx(s); 1318114Sroot if (copyout((caddr_t)&aitv, uap->itv, sizeof (struct itimerval))) 1327424Sroot u.u_error = EFAULT; 1338034Sroot splx(s); 1347424Sroot } 1357424Sroot 1368034Sroot setitimer() 1377424Sroot { 1387424Sroot register struct a { 1398034Sroot u_int which; 1408103Sroot struct itimerval *itv, *oitv; 1418034Sroot } *uap = (struct a *)u.u_ap; 1428034Sroot struct itimerval aitv; 1438034Sroot int s; 1448114Sroot register struct proc *p = u.u_procp; 1457424Sroot 1468034Sroot if (uap->which > 2) { 1478034Sroot u.u_error = EINVAL; 1488103Sroot return; 1497424Sroot } 1508034Sroot if (copyin((caddr_t)uap->itv, (caddr_t)&aitv, 1518034Sroot sizeof (struct itimerval))) { 1528034Sroot u.u_error = EFAULT; 1538103Sroot return; 1548034Sroot } 1558103Sroot if (uap->oitv) { 1568103Sroot uap->itv = uap->oitv; 1578103Sroot getitimer(); 1588103Sroot } 1598103Sroot if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) { 1608103Sroot u.u_error = EINVAL; 1618103Sroot return; 1628103Sroot } 1638103Sroot s = spl7(); 1648114Sroot if (uap->which == ITIMER_REAL) { 165*8146Sroot untimeout(realitexpire, p); 1668114Sroot if (timerisset(&aitv.it_value)) { 1678114Sroot timevaladd(&aitv.it_value, &time); 168*8146Sroot timeout(realitexpire, p, hzto(&aitv.it_value)); 1698114Sroot } 1708114Sroot p->p_realtimer = aitv; 1718114Sroot } else 1728103Sroot u.u_timer[uap->which] = aitv; 1738034Sroot splx(s); 1747424Sroot } 1757424Sroot 176*8146Sroot /* 177*8146Sroot * Real interval timer expired: 178*8146Sroot * send process whose timer expired an alarm signal. 179*8146Sroot * If time is not set up to reload, then just return. 180*8146Sroot * Else compute next time timer should go off which is > current time. 181*8146Sroot * This is where delay in processing this timeout causes multiple 182*8146Sroot * SIGALRM calls to be compressed into one. 183*8146Sroot */ 184*8146Sroot realitexpire(p) 1858114Sroot register struct proc *p; 1868114Sroot { 1878114Sroot int s; 1888114Sroot 1898114Sroot psignal(p, SIGALRM); 1908114Sroot if (!timerisset(&p->p_realtimer.it_interval)) { 1918114Sroot timerclear(&p->p_realtimer.it_value); 1928114Sroot return; 1938114Sroot } 1948114Sroot for (;;) { 1958114Sroot s = spl7(); 1968114Sroot timevaladd(&p->p_realtimer.it_value, 1978114Sroot &p->p_realtimer.it_interval); 1988114Sroot if (timercmp(&p->p_realtimer.it_value, &time, >)) { 199*8146Sroot timeout(realitexpire, 200*8146Sroot p, hzto(&p->p_realtimer.it_value)); 2018114Sroot splx(s); 2028114Sroot return; 2038114Sroot } 2048114Sroot splx(s); 2058114Sroot } 2068114Sroot } 2078114Sroot 208*8146Sroot /* 209*8146Sroot * Check that a proposed value to load into the .it_value or 210*8146Sroot * .it_interval part of an interval timer is acceptable, and 211*8146Sroot * fix it to have at least minimal value (i.e. if it is less 212*8146Sroot * than the resolution of the clock, round it up.) 213*8146Sroot */ 2148103Sroot itimerfix(tv) 2158103Sroot struct timeval *tv; 2167424Sroot { 2178034Sroot 2188114Sroot if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 2198114Sroot tv->tv_usec < 0 || tv->tv_usec >= 1000000) 2208103Sroot return (EINVAL); 2218103Sroot if (tv->tv_sec == 0 && tv->tv_usec < tick) 2228103Sroot tv->tv_usec = tick; 2238103Sroot return (0); 2248034Sroot } 2258034Sroot 226*8146Sroot /* 227*8146Sroot * Decrement an interval timer by a specified number 228*8146Sroot * of microseconds, which must be less than a second, 229*8146Sroot * i.e. < 1000000. If the timer expires, then reload 230*8146Sroot * it. In this case, carry over (usec - old value) to 231*8146Sroot * reducint the value reloaded into the timer so that 232*8146Sroot * the timer does not drift. This routine assumes 233*8146Sroot * that it is called in a context where the timers 234*8146Sroot * on which it is operating cannot change in value. 235*8146Sroot */ 2368034Sroot itimerdecr(itp, usec) 2378034Sroot register struct itimerval *itp; 2388034Sroot int usec; 2398034Sroot { 2408034Sroot 2418103Sroot if (itp->it_value.tv_usec < usec) { 2428103Sroot if (itp->it_value.tv_sec == 0) { 243*8146Sroot /* expired, and already in next interval */ 2448103Sroot usec -= itp->it_value.tv_usec; 2458034Sroot goto expire; 2468103Sroot } 2478103Sroot itp->it_value.tv_usec += 1000000; 2488103Sroot itp->it_value.tv_sec--; 2498034Sroot } 2508103Sroot itp->it_value.tv_usec -= usec; 2518103Sroot usec = 0; 2528103Sroot if (timerisset(&itp->it_value)) 2538034Sroot return (1); 254*8146Sroot /* expired, exactly at end of interval */ 2558034Sroot expire: 2568103Sroot if (timerisset(&itp->it_interval)) { 2578103Sroot itp->it_value = itp->it_interval; 2588103Sroot itp->it_value.tv_usec -= usec; 2598103Sroot if (itp->it_value.tv_usec < 0) { 2608103Sroot itp->it_value.tv_usec += 1000000; 2618103Sroot itp->it_value.tv_sec--; 2628103Sroot } 2638103Sroot } else 264*8146Sroot itp->it_value.tv_usec = 0; /* sec is already 0 */ 2658034Sroot return (0); 2668034Sroot } 2678034Sroot 268*8146Sroot /* 269*8146Sroot * Add and subtract routines for timevals. 270*8146Sroot * N.B.: subtract routine doesn't deal with 271*8146Sroot * results which are before the beginning, 272*8146Sroot * it just gets very confused in this case. 273*8146Sroot * Caveat emptor. 274*8146Sroot */ 275*8146Sroot timevaladd(t1, t2) 276*8146Sroot struct timeval *t1, *t2; 277*8146Sroot { 278*8146Sroot 279*8146Sroot t1->tv_sec += t2->tv_sec; 280*8146Sroot t1->tv_usec += t2->tv_usec; 281*8146Sroot timevalfix(t1); 282*8146Sroot } 283*8146Sroot 284*8146Sroot timevalsub(t1, t2) 285*8146Sroot struct timeval *t1, *t2; 286*8146Sroot { 287*8146Sroot 288*8146Sroot t1->tv_sec -= t2->tv_sec; 289*8146Sroot t1->tv_usec -= t2->tv_usec; 290*8146Sroot timevalfix(t1); 291*8146Sroot } 292*8146Sroot 293*8146Sroot timevalfix(t1) 294*8146Sroot struct timeval *t1; 295*8146Sroot { 296*8146Sroot 297*8146Sroot if (t1->tv_usec < 0) { 298*8146Sroot t1->tv_sec--; 299*8146Sroot t1->tv_usec += 1000000; 300*8146Sroot } 301*8146Sroot if (t1->tv_usec >= 1000000) { 302*8146Sroot t1->tv_sec++; 303*8146Sroot t1->tv_usec -= 1000000; 304*8146Sroot } 305*8146Sroot } 306*8146Sroot 3078034Sroot #ifndef NOCOMPAT 3088034Sroot otime() 3098034Sroot { 3108034Sroot 3118034Sroot u.u_r.r_time = time.tv_sec; 3128034Sroot } 3138034Sroot 3148103Sroot ostime() 3158103Sroot { 3168103Sroot register struct a { 3178103Sroot int time; 3188103Sroot } *uap = (struct a *)u.u_ap; 3198103Sroot struct timeval tv; 3208103Sroot 3218103Sroot tv.tv_sec = uap->time; 3228103Sroot tv.tv_usec = 0; 3238103Sroot setthetime(&tv); 3248103Sroot } 3258103Sroot 326*8146Sroot /* from old timeb.h */ 327*8146Sroot struct timeb { 328*8146Sroot time_t time; 329*8146Sroot u_short millitm; 330*8146Sroot short timezone; 331*8146Sroot short dstflag; 332*8146Sroot }; 3338034Sroot 3348034Sroot oftime() 3358034Sroot { 3367424Sroot register struct a { 3378034Sroot struct timeb *tp; 3387424Sroot } *uap; 339*8146Sroot struct timeb tb; 3407424Sroot 3417424Sroot uap = (struct a *)u.u_ap; 3428034Sroot (void) spl7(); 343*8146Sroot tb.time = time.tv_sec; 344*8146Sroot tb.millitm = time.tv_usec / 1000; 3458034Sroot (void) spl0(); 346*8146Sroot tb.timezone = tz.tz_minuteswest; 347*8146Sroot tb.dstflag = tz.tz_dsttime; 348*8146Sroot if (copyout((caddr_t)&tb, (caddr_t)uap->tp, sizeof(t)) < 0) 3497424Sroot u.u_error = EFAULT; 3507424Sroot } 351*8146Sroot 3528120Sroot oalarm() 3538120Sroot { 3548120Sroot register struct a { 3558120Sroot int deltat; 3568120Sroot } *uap = (struct a *)u.u_ap; 3578120Sroot register struct proc *p = u.u_procp; 3588120Sroot struct timeval atv; 3598120Sroot int s = spl7(); 3608120Sroot 361*8146Sroot untimeout(realitexpire, p); 3628120Sroot timerclear(&p->p_realtimer.it_interval); 3638120Sroot u.u_r.r_val1 = 0; 3648120Sroot if (timerisset(&p->p_realtimer.it_value) && 3658120Sroot timercmp(&p->p_realtimer.it_value, &time, >)) 3668120Sroot u.u_r.r_val1 = p->p_realtimer.it_value.tv_sec - time.tv_sec; 3678120Sroot if (uap->deltat == 0) { 3688120Sroot splx(s); 3698120Sroot return; 3708120Sroot } 3718120Sroot p->p_realtimer.it_value = time; 3728120Sroot p->p_realtimer.it_value.tv_sec += uap->deltat; 373*8146Sroot timeout(realitexpire, p, hzto(&p->p_realtimer.it_value)); 3748120Sroot splx(s); 3758120Sroot } 376*8146Sroot #endif 377