xref: /csrg-svn/sys/kern/kern_time.c (revision 9998)
1*9998Ssam /*	kern_time.c	5.13	82/12/28	*/
27424Sroot 
39757Ssam #include "../machine/reg.h"
49757Ssam 
57424Sroot #include "../h/param.h"
68034Sroot #include "../h/dir.h"		/* XXX */
77424Sroot #include "../h/user.h"
88034Sroot #include "../h/kernel.h"
97424Sroot #include "../h/inode.h"
107424Sroot #include "../h/proc.h"
117424Sroot 
128103Sroot /*
138103Sroot  * Time of day and interval timer support.
148146Sroot  *
158146Sroot  * These routines provide the kernel entry points to get and set
168146Sroot  * the time-of-day and per-process interval timers.  Subroutines
178146Sroot  * here provide support for adding and subtracting timeval structures
188146Sroot  * and decrementing interval timers, optionally reloading the interval
198146Sroot  * timers when they expire.
208103Sroot  */
218103Sroot 
228034Sroot gettimeofday()
237424Sroot {
248034Sroot 	register struct a {
258034Sroot 		struct	timeval *tp;
268034Sroot 		struct	timezone *tzp;
278034Sroot 	} *uap = (struct a *)u.u_ap;
288034Sroot 	struct timeval atv;
298103Sroot 	int s;
307500Sroot 
318103Sroot 	s = spl7(); atv = time; splx(s);
32*9998Ssam 	u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv));
33*9998Ssam 	if (u.u_error)
348034Sroot 		return;
358034Sroot 	if (uap->tzp == 0)
368034Sroot 		return;
378103Sroot 	/* SHOULD HAVE PER-PROCESS TIMEZONE */
38*9998Ssam 	u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, sizeof (tz));
397500Sroot }
407500Sroot 
418034Sroot settimeofday()
427500Sroot {
438034Sroot 	register struct a {
448103Sroot 		struct	timeval *tv;
458103Sroot 		struct	timezone *tzp;
468034Sroot 	} *uap = (struct a *)u.u_ap;
478034Sroot 	struct timeval atv;
488034Sroot 	struct timezone atz;
497500Sroot 
50*9998Ssam 	u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
51*9998Ssam 		sizeof (struct timeval));
52*9998Ssam 	if (u.u_error)
538034Sroot 		return;
548103Sroot 	setthetime(&atv);
558103Sroot 	if (uap->tzp && suser()) {
56*9998Ssam 		u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
57*9998Ssam 			sizeof (atz));
58*9998Ssam 		if (u.u_error)
598034Sroot 			return;
608034Sroot 	}
617500Sroot }
627500Sroot 
638103Sroot setthetime(tv)
648103Sroot 	struct timeval *tv;
658103Sroot {
668103Sroot 	int s;
678103Sroot 
688103Sroot 	if (!suser())
698103Sroot 		return;
708146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
718103Sroot 	boottime.tv_sec += tv->tv_sec - time.tv_sec;
728103Sroot 	s = spl7(); time = *tv; splx(s);
739007Sroot 	resettodr();
748103Sroot }
758103Sroot 
768146Sroot /*
778146Sroot  * Get value of an interval timer.  The process virtual and
788146Sroot  * profiling virtual time timers are kept in the u. area, since
798146Sroot  * they can be swapped out.  These are kept internally in the
808146Sroot  * way they are specified externally: in time until they expire.
818146Sroot  *
828146Sroot  * The real time interval timer is kept in the process table slot
838146Sroot  * for the process, and its value (it_value) is kept as an
848146Sroot  * absolute time rather than as a delta, so that it is easy to keep
858146Sroot  * periodic real-time signals from drifting.
868146Sroot  *
878146Sroot  * Virtual time timers are processed in the hardclock() routine of
888146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
898146Sroot  * routine, called from the softclock() routine.  Since a callout
908146Sroot  * may be delayed in real time due to interrupt processing in the system,
918146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
928146Sroot  * to be delayed in real time past when it is supposed to occur.  It
938146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
948146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
958146Sroot  * absolute time the timer should go off.
968146Sroot  */
978034Sroot getitimer()
988034Sroot {
997424Sroot 	register struct a {
1008034Sroot 		u_int	which;
1018034Sroot 		struct	itimerval *itv;
1028034Sroot 	} *uap = (struct a *)u.u_ap;
1038114Sroot 	struct itimerval aitv;
1048034Sroot 	int s;
1057424Sroot 
1068034Sroot 	if (uap->which > 2) {
1078034Sroot 		u.u_error = EINVAL;
1088034Sroot 		return;
1097424Sroot 	}
1108034Sroot 	s = spl7();
1118114Sroot 	if (uap->which == ITIMER_REAL) {
1128146Sroot 		/*
1138146Sroot 		 * Convert from absoulte to relative time in .it_value
1148146Sroot 		 * part of real time timer.  If time for real time timer
1158146Sroot 		 * has passed return 0, else return difference between
1168146Sroot 		 * current time and time for the timer to go off.
1178146Sroot 		 */
1188114Sroot 		aitv = u.u_procp->p_realtimer;
1198114Sroot 		if (timerisset(&aitv.it_value))
1208114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1218114Sroot 				timerclear(&aitv.it_value);
1228114Sroot 			else
1238114Sroot 				timevalsub(&aitv.it_value, &time);
1248114Sroot 	} else
1258114Sroot 		aitv = u.u_timer[uap->which];
1268114Sroot 	splx(s);
127*9998Ssam 	u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
128*9998Ssam 	    sizeof (struct itimerval));
1298034Sroot 	splx(s);
1307424Sroot }
1317424Sroot 
1328034Sroot setitimer()
1337424Sroot {
1347424Sroot 	register struct a {
1358034Sroot 		u_int	which;
1368103Sroot 		struct	itimerval *itv, *oitv;
1378034Sroot 	} *uap = (struct a *)u.u_ap;
1388034Sroot 	struct itimerval aitv;
1398034Sroot 	int s;
1408114Sroot 	register struct proc *p = u.u_procp;
1417424Sroot 
1428034Sroot 	if (uap->which > 2) {
1438034Sroot 		u.u_error = EINVAL;
1448103Sroot 		return;
1457424Sroot 	}
146*9998Ssam 	u.u_error = copyin((caddr_t)uap->itv, (caddr_t)&aitv,
147*9998Ssam 	    sizeof (struct itimerval));
148*9998Ssam 	if (u.u_error)
1498103Sroot 		return;
1508103Sroot 	if (uap->oitv) {
1518103Sroot 		uap->itv = uap->oitv;
1528103Sroot 		getitimer();
1538103Sroot 	}
1548103Sroot 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) {
1558103Sroot 		u.u_error = EINVAL;
1568103Sroot 		return;
1578103Sroot 	}
1588103Sroot 	s = spl7();
1598114Sroot 	if (uap->which == ITIMER_REAL) {
1608625Sroot 		untimeout(realitexpire, (caddr_t)p);
1618114Sroot 		if (timerisset(&aitv.it_value)) {
1628114Sroot 			timevaladd(&aitv.it_value, &time);
1638625Sroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
1648114Sroot 		}
1658114Sroot 		p->p_realtimer = aitv;
1668114Sroot 	} else
1678103Sroot 		u.u_timer[uap->which] = aitv;
1688034Sroot 	splx(s);
1697424Sroot }
1707424Sroot 
1718146Sroot /*
1728146Sroot  * Real interval timer expired:
1738146Sroot  * send process whose timer expired an alarm signal.
1748146Sroot  * If time is not set up to reload, then just return.
1758146Sroot  * Else compute next time timer should go off which is > current time.
1768146Sroot  * This is where delay in processing this timeout causes multiple
1778146Sroot  * SIGALRM calls to be compressed into one.
1788146Sroot  */
1798146Sroot realitexpire(p)
1808114Sroot 	register struct proc *p;
1818114Sroot {
1828114Sroot 	int s;
1838114Sroot 
1848114Sroot 	psignal(p, SIGALRM);
1858114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
1868114Sroot 		timerclear(&p->p_realtimer.it_value);
1878114Sroot 		return;
1888114Sroot 	}
1898114Sroot 	for (;;) {
1908114Sroot 		s = spl7();
1918114Sroot 		timevaladd(&p->p_realtimer.it_value,
1928114Sroot 		    &p->p_realtimer.it_interval);
1938114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
1948625Sroot 			timeout(realitexpire, (caddr_t)p,
1958625Sroot 			    hzto(&p->p_realtimer.it_value));
1968114Sroot 			splx(s);
1978114Sroot 			return;
1988114Sroot 		}
1998114Sroot 		splx(s);
2008114Sroot 	}
2018114Sroot }
2028114Sroot 
2038146Sroot /*
2048146Sroot  * Check that a proposed value to load into the .it_value or
2058146Sroot  * .it_interval part of an interval timer is acceptable, and
2068146Sroot  * fix it to have at least minimal value (i.e. if it is less
2078146Sroot  * than the resolution of the clock, round it up.)
2088146Sroot  */
2098103Sroot itimerfix(tv)
2108103Sroot 	struct timeval *tv;
2117424Sroot {
2128034Sroot 
2138114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2148114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2158103Sroot 		return (EINVAL);
2168103Sroot 	if (tv->tv_sec == 0 && tv->tv_usec < tick)
2178103Sroot 		tv->tv_usec = tick;
2188103Sroot 	return (0);
2198034Sroot }
2208034Sroot 
2218146Sroot /*
2228146Sroot  * Decrement an interval timer by a specified number
2238146Sroot  * of microseconds, which must be less than a second,
2248146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2258146Sroot  * it.  In this case, carry over (usec - old value) to
2268146Sroot  * reducint the value reloaded into the timer so that
2278146Sroot  * the timer does not drift.  This routine assumes
2288146Sroot  * that it is called in a context where the timers
2298146Sroot  * on which it is operating cannot change in value.
2308146Sroot  */
2318034Sroot itimerdecr(itp, usec)
2328034Sroot 	register struct itimerval *itp;
2338034Sroot 	int usec;
2348034Sroot {
2358034Sroot 
2368103Sroot 	if (itp->it_value.tv_usec < usec) {
2378103Sroot 		if (itp->it_value.tv_sec == 0) {
2388146Sroot 			/* expired, and already in next interval */
2398103Sroot 			usec -= itp->it_value.tv_usec;
2408034Sroot 			goto expire;
2418103Sroot 		}
2428103Sroot 		itp->it_value.tv_usec += 1000000;
2438103Sroot 		itp->it_value.tv_sec--;
2448034Sroot 	}
2458103Sroot 	itp->it_value.tv_usec -= usec;
2468103Sroot 	usec = 0;
2478103Sroot 	if (timerisset(&itp->it_value))
2488034Sroot 		return (1);
2498146Sroot 	/* expired, exactly at end of interval */
2508034Sroot expire:
2518103Sroot 	if (timerisset(&itp->it_interval)) {
2528103Sroot 		itp->it_value = itp->it_interval;
2538103Sroot 		itp->it_value.tv_usec -= usec;
2548103Sroot 		if (itp->it_value.tv_usec < 0) {
2558103Sroot 			itp->it_value.tv_usec += 1000000;
2568103Sroot 			itp->it_value.tv_sec--;
2578103Sroot 		}
2588103Sroot 	} else
2598146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
2608034Sroot 	return (0);
2618034Sroot }
2628034Sroot 
2638146Sroot /*
2648146Sroot  * Add and subtract routines for timevals.
2658146Sroot  * N.B.: subtract routine doesn't deal with
2668146Sroot  * results which are before the beginning,
2678146Sroot  * it just gets very confused in this case.
2688146Sroot  * Caveat emptor.
2698146Sroot  */
2708146Sroot timevaladd(t1, t2)
2718146Sroot 	struct timeval *t1, *t2;
2728146Sroot {
2738146Sroot 
2748146Sroot 	t1->tv_sec += t2->tv_sec;
2758146Sroot 	t1->tv_usec += t2->tv_usec;
2768146Sroot 	timevalfix(t1);
2778146Sroot }
2788146Sroot 
2798146Sroot timevalsub(t1, t2)
2808146Sroot 	struct timeval *t1, *t2;
2818146Sroot {
2828146Sroot 
2838146Sroot 	t1->tv_sec -= t2->tv_sec;
2848146Sroot 	t1->tv_usec -= t2->tv_usec;
2858146Sroot 	timevalfix(t1);
2868146Sroot }
2878146Sroot 
2888146Sroot timevalfix(t1)
2898146Sroot 	struct timeval *t1;
2908146Sroot {
2918146Sroot 
2928146Sroot 	if (t1->tv_usec < 0) {
2938146Sroot 		t1->tv_sec--;
2948146Sroot 		t1->tv_usec += 1000000;
2958146Sroot 	}
2968146Sroot 	if (t1->tv_usec >= 1000000) {
2978146Sroot 		t1->tv_sec++;
2988146Sroot 		t1->tv_usec -= 1000000;
2998146Sroot 	}
3008146Sroot }
3018146Sroot 
3028034Sroot #ifndef NOCOMPAT
3038034Sroot otime()
3048034Sroot {
3058034Sroot 
3068034Sroot 	u.u_r.r_time = time.tv_sec;
3078034Sroot }
3088034Sroot 
3098103Sroot ostime()
3108103Sroot {
3118103Sroot 	register struct a {
3128103Sroot 		int	time;
3138103Sroot 	} *uap = (struct a *)u.u_ap;
3148103Sroot 	struct timeval tv;
3158103Sroot 
3168103Sroot 	tv.tv_sec = uap->time;
3178103Sroot 	tv.tv_usec = 0;
3188103Sroot 	setthetime(&tv);
3198103Sroot }
3208103Sroot 
3218146Sroot /* from old timeb.h */
3228146Sroot struct timeb {
3238146Sroot 	time_t	time;
3248146Sroot 	u_short	millitm;
3258146Sroot 	short	timezone;
3268146Sroot 	short	dstflag;
3278146Sroot };
3288034Sroot 
3298034Sroot oftime()
3308034Sroot {
3317424Sroot 	register struct a {
3328034Sroot 		struct	timeb	*tp;
3337424Sroot 	} *uap;
3348146Sroot 	struct timeb tb;
3357424Sroot 
3367424Sroot 	uap = (struct a *)u.u_ap;
3378034Sroot 	(void) spl7();
3388146Sroot 	tb.time = time.tv_sec;
3398146Sroot 	tb.millitm = time.tv_usec / 1000;
3408034Sroot 	(void) spl0();
3418146Sroot 	tb.timezone = tz.tz_minuteswest;
3428146Sroot 	tb.dstflag = tz.tz_dsttime;
343*9998Ssam 	u.u_error = copyout((caddr_t)&tb, (caddr_t)uap->tp, sizeof (tb));
3447424Sroot }
3458146Sroot 
3468120Sroot oalarm()
3478120Sroot {
3488120Sroot 	register struct a {
3498120Sroot 		int	deltat;
3508120Sroot 	} *uap = (struct a *)u.u_ap;
3518120Sroot 	register struct proc *p = u.u_procp;
3528120Sroot 	int s = spl7();
3538120Sroot 
3548625Sroot 	untimeout(realitexpire, (caddr_t)p);
3558120Sroot 	timerclear(&p->p_realtimer.it_interval);
3568120Sroot 	u.u_r.r_val1 = 0;
3578120Sroot 	if (timerisset(&p->p_realtimer.it_value) &&
3588120Sroot 	    timercmp(&p->p_realtimer.it_value, &time, >))
3598120Sroot 		u.u_r.r_val1 = p->p_realtimer.it_value.tv_sec - time.tv_sec;
3608120Sroot 	if (uap->deltat == 0) {
3618120Sroot 		splx(s);
3628120Sroot 		return;
3638120Sroot 	}
3648120Sroot 	p->p_realtimer.it_value = time;
3658120Sroot 	p->p_realtimer.it_value.tv_sec += uap->deltat;
3668625Sroot 	timeout(realitexpire, (caddr_t)p, hzto(&p->p_realtimer.it_value));
3678120Sroot 	splx(s);
3688120Sroot }
3698146Sroot #endif
370