xref: /csrg-svn/sys/kern/kern_time.c (revision 9007)
1*9007Sroot /*	kern_time.c	5.11	82/11/02	*/
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.
138146Sroot  *
148146Sroot  * These routines provide the kernel entry points to get and set
158146Sroot  * the time-of-day and per-process interval timers.  Subroutines
168146Sroot  * here provide support for adding and subtracting timeval structures
178146Sroot  * and decrementing interval timers, optionally reloading the interval
188146Sroot  * 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 */
388625Sroot 	if (copyout((caddr_t)&tz, (caddr_t)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 	int s;
708103Sroot 
718103Sroot 	if (!suser())
728103Sroot 		return;
738146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
748103Sroot 	boottime.tv_sec += tv->tv_sec - time.tv_sec;
758103Sroot 	s = spl7(); time = *tv; splx(s);
76*9007Sroot 	resettodr();
778103Sroot }
788103Sroot 
798146Sroot /*
808146Sroot  * Get value of an interval timer.  The process virtual and
818146Sroot  * profiling virtual time timers are kept in the u. area, since
828146Sroot  * they can be swapped out.  These are kept internally in the
838146Sroot  * way they are specified externally: in time until they expire.
848146Sroot  *
858146Sroot  * The real time interval timer is kept in the process table slot
868146Sroot  * for the process, and its value (it_value) is kept as an
878146Sroot  * absolute time rather than as a delta, so that it is easy to keep
888146Sroot  * periodic real-time signals from drifting.
898146Sroot  *
908146Sroot  * Virtual time timers are processed in the hardclock() routine of
918146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
928146Sroot  * routine, called from the softclock() routine.  Since a callout
938146Sroot  * may be delayed in real time due to interrupt processing in the system,
948146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
958146Sroot  * to be delayed in real time past when it is supposed to occur.  It
968146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
978146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
988146Sroot  * absolute time the timer should go off.
998146Sroot  */
1008034Sroot getitimer()
1018034Sroot {
1027424Sroot 	register struct a {
1038034Sroot 		u_int	which;
1048034Sroot 		struct	itimerval *itv;
1058034Sroot 	} *uap = (struct a *)u.u_ap;
1068114Sroot 	struct itimerval aitv;
1078034Sroot 	int s;
1087424Sroot 
1098034Sroot 	if (uap->which > 2) {
1108034Sroot 		u.u_error = EINVAL;
1118034Sroot 		return;
1127424Sroot 	}
1138034Sroot 	s = spl7();
1148114Sroot 	if (uap->which == ITIMER_REAL) {
1158146Sroot 		/*
1168146Sroot 		 * Convert from absoulte to relative time in .it_value
1178146Sroot 		 * part of real time timer.  If time for real time timer
1188146Sroot 		 * has passed return 0, else return difference between
1198146Sroot 		 * current time and time for the timer to go off.
1208146Sroot 		 */
1218114Sroot 		aitv = u.u_procp->p_realtimer;
1228114Sroot 		if (timerisset(&aitv.it_value))
1238114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1248114Sroot 				timerclear(&aitv.it_value);
1258114Sroot 			else
1268114Sroot 				timevalsub(&aitv.it_value, &time);
1278114Sroot 	} else
1288114Sroot 		aitv = u.u_timer[uap->which];
1298114Sroot 	splx(s);
1308625Sroot 	if (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
1318625Sroot 	    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) {
1658625Sroot 		untimeout(realitexpire, (caddr_t)p);
1668114Sroot 		if (timerisset(&aitv.it_value)) {
1678114Sroot 			timevaladd(&aitv.it_value, &time);
1688625Sroot 			timeout(realitexpire, (caddr_t)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 
1768146Sroot /*
1778146Sroot  * Real interval timer expired:
1788146Sroot  * send process whose timer expired an alarm signal.
1798146Sroot  * If time is not set up to reload, then just return.
1808146Sroot  * Else compute next time timer should go off which is > current time.
1818146Sroot  * This is where delay in processing this timeout causes multiple
1828146Sroot  * SIGALRM calls to be compressed into one.
1838146Sroot  */
1848146Sroot 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, >)) {
1998625Sroot 			timeout(realitexpire, (caddr_t)p,
2008625Sroot 			    hzto(&p->p_realtimer.it_value));
2018114Sroot 			splx(s);
2028114Sroot 			return;
2038114Sroot 		}
2048114Sroot 		splx(s);
2058114Sroot 	}
2068114Sroot }
2078114Sroot 
2088146Sroot /*
2098146Sroot  * Check that a proposed value to load into the .it_value or
2108146Sroot  * .it_interval part of an interval timer is acceptable, and
2118146Sroot  * fix it to have at least minimal value (i.e. if it is less
2128146Sroot  * than the resolution of the clock, round it up.)
2138146Sroot  */
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 
2268146Sroot /*
2278146Sroot  * Decrement an interval timer by a specified number
2288146Sroot  * of microseconds, which must be less than a second,
2298146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2308146Sroot  * it.  In this case, carry over (usec - old value) to
2318146Sroot  * reducint the value reloaded into the timer so that
2328146Sroot  * the timer does not drift.  This routine assumes
2338146Sroot  * that it is called in a context where the timers
2348146Sroot  * on which it is operating cannot change in value.
2358146Sroot  */
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) {
2438146Sroot 			/* 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);
2548146Sroot 	/* 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
2648146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
2658034Sroot 	return (0);
2668034Sroot }
2678034Sroot 
2688146Sroot /*
2698146Sroot  * Add and subtract routines for timevals.
2708146Sroot  * N.B.: subtract routine doesn't deal with
2718146Sroot  * results which are before the beginning,
2728146Sroot  * it just gets very confused in this case.
2738146Sroot  * Caveat emptor.
2748146Sroot  */
2758146Sroot timevaladd(t1, t2)
2768146Sroot 	struct timeval *t1, *t2;
2778146Sroot {
2788146Sroot 
2798146Sroot 	t1->tv_sec += t2->tv_sec;
2808146Sroot 	t1->tv_usec += t2->tv_usec;
2818146Sroot 	timevalfix(t1);
2828146Sroot }
2838146Sroot 
2848146Sroot timevalsub(t1, t2)
2858146Sroot 	struct timeval *t1, *t2;
2868146Sroot {
2878146Sroot 
2888146Sroot 	t1->tv_sec -= t2->tv_sec;
2898146Sroot 	t1->tv_usec -= t2->tv_usec;
2908146Sroot 	timevalfix(t1);
2918146Sroot }
2928146Sroot 
2938146Sroot timevalfix(t1)
2948146Sroot 	struct timeval *t1;
2958146Sroot {
2968146Sroot 
2978146Sroot 	if (t1->tv_usec < 0) {
2988146Sroot 		t1->tv_sec--;
2998146Sroot 		t1->tv_usec += 1000000;
3008146Sroot 	}
3018146Sroot 	if (t1->tv_usec >= 1000000) {
3028146Sroot 		t1->tv_sec++;
3038146Sroot 		t1->tv_usec -= 1000000;
3048146Sroot 	}
3058146Sroot }
3068146Sroot 
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 
3268146Sroot /* from old timeb.h */
3278146Sroot struct timeb {
3288146Sroot 	time_t	time;
3298146Sroot 	u_short	millitm;
3308146Sroot 	short	timezone;
3318146Sroot 	short	dstflag;
3328146Sroot };
3338034Sroot 
3348034Sroot oftime()
3358034Sroot {
3367424Sroot 	register struct a {
3378034Sroot 		struct	timeb	*tp;
3387424Sroot 	} *uap;
3398146Sroot 	struct timeb tb;
3407424Sroot 
3417424Sroot 	uap = (struct a *)u.u_ap;
3428034Sroot 	(void) spl7();
3438146Sroot 	tb.time = time.tv_sec;
3448146Sroot 	tb.millitm = time.tv_usec / 1000;
3458034Sroot 	(void) spl0();
3468146Sroot 	tb.timezone = tz.tz_minuteswest;
3478146Sroot 	tb.dstflag = tz.tz_dsttime;
3488446Sroot 	if (copyout((caddr_t)&tb, (caddr_t)uap->tp, sizeof (tb)) < 0)
3497424Sroot 		u.u_error = EFAULT;
3507424Sroot }
3518146Sroot 
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 	int s = spl7();
3598120Sroot 
3608625Sroot 	untimeout(realitexpire, (caddr_t)p);
3618120Sroot 	timerclear(&p->p_realtimer.it_interval);
3628120Sroot 	u.u_r.r_val1 = 0;
3638120Sroot 	if (timerisset(&p->p_realtimer.it_value) &&
3648120Sroot 	    timercmp(&p->p_realtimer.it_value, &time, >))
3658120Sroot 		u.u_r.r_val1 = p->p_realtimer.it_value.tv_sec - time.tv_sec;
3668120Sroot 	if (uap->deltat == 0) {
3678120Sroot 		splx(s);
3688120Sroot 		return;
3698120Sroot 	}
3708120Sroot 	p->p_realtimer.it_value = time;
3718120Sroot 	p->p_realtimer.it_value.tv_sec += uap->deltat;
3728625Sroot 	timeout(realitexpire, (caddr_t)p, hzto(&p->p_realtimer.it_value));
3738120Sroot 	splx(s);
3748120Sroot }
3758146Sroot #endif
376