kern_time.c revision 33690
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
34 * $Id: kern_time.c,v 1.40 1997/11/07 08:52:58 phk Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/sysproto.h>
39#include <sys/resourcevar.h>
40#include <sys/signalvar.h>
41#include <sys/kernel.h>
42#include <sys/systm.h>
43#include <sys/sysent.h>
44#include <sys/proc.h>
45#include <sys/time.h>
46#include <sys/vnode.h>
47#include <vm/vm.h>
48#include <vm/vm_extern.h>
49
50struct timezone tz;
51
52/*
53 * Time of day and interval timer support.
54 *
55 * These routines provide the kernel entry points to get and set
56 * the time-of-day and per-process interval timers.  Subroutines
57 * here provide support for adding and subtracting timeval structures
58 * and decrementing interval timers, optionally reloading the interval
59 * timers when they expire.
60 */
61
62static int	nanosleep1 __P((struct proc *p, struct timespec *rqt,
63		    struct timespec *rmt));
64static int	settime __P((struct timeval *));
65static void	timevalfix __P((struct timeval *));
66static void	no_lease_updatetime __P((int));
67
68static void
69no_lease_updatetime(deltat)
70	int deltat;
71{
72}
73
74void (*lease_updatetime) __P((int))  = no_lease_updatetime;
75
76static int
77settime(tv)
78	struct timeval *tv;
79{
80	struct timeval delta;
81	struct timespec ts;
82	struct proc *p;
83	int s;
84
85	/*
86	 * Must not set clock backwards in highly secure mode.
87	 */
88	s = splclock();
89	delta.tv_sec = tv->tv_sec - time.tv_sec;
90	delta.tv_usec = tv->tv_usec - time.tv_usec;
91	splx(s);
92	timevalfix(&delta);
93	if (delta.tv_sec < 0 && securelevel > 1)
94		return (EPERM);
95
96	s = splclock();
97	/*
98	 * Recalculate delta directly to minimize clock interrupt
99	 * latency.  Fix it after the ipl has been lowered.
100	 */
101	delta.tv_sec = tv->tv_sec - time.tv_sec;
102	delta.tv_usec = tv->tv_usec - time.tv_usec;
103	ts.tv_sec = tv->tv_sec;
104	ts.tv_nsec = tv->tv_usec * 1000;
105	set_timecounter(&ts);
106	/*
107	 * XXX should arrange for microtime() to agree with *tv if
108	 * it is called now.  As it is, it may add up to about
109	 * `tick' unwanted usec.
110	 * Another problem is that clock interrupts may occur at
111	 * other than multiples of `tick'.  It's not worth fixing
112	 * this here, since the problem is also caused by tick
113	 * adjustments.
114	 */
115	(void) splsoftclock();
116	timevalfix(&delta);
117	timevaladd(&boottime, &delta);
118	timevaladd(&runtime, &delta);
119	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
120		if (timerisset(&p->p_realtimer.it_value))
121			timevaladd(&p->p_realtimer.it_value, &delta);
122		if (p->p_sleepend)
123			timevaladd(p->p_sleepend, &delta);
124	}
125	lease_updatetime(delta.tv_sec);
126	splx(s);
127	resettodr();
128	return (0);
129}
130
131#ifndef _SYS_SYSPROTO_H_
132struct clock_gettime_args {
133	clockid_t clock_id;
134	struct	timespec *tp;
135};
136#endif
137
138/* ARGSUSED */
139int
140clock_gettime(p, uap)
141	struct proc *p;
142	struct clock_gettime_args *uap;
143{
144	struct timespec ats;
145
146	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
147		return (EINVAL);
148	nanotime(&ats);
149	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
150}
151
152#ifndef _SYS_SYSPROTO_H_
153struct clock_settime_args {
154	clockid_t clock_id;
155	const struct	timespec *tp;
156};
157#endif
158
159/* ARGSUSED */
160int
161clock_settime(p, uap)
162	struct proc *p;
163	struct clock_settime_args *uap;
164{
165	struct timeval atv;
166	struct timespec ats;
167	int error;
168
169	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
170		return (error);
171	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
172		return (EINVAL);
173	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
174		return (error);
175	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
176		return (EINVAL);
177	TIMESPEC_TO_TIMEVAL(&atv, &ats);
178	if ((error = settime(&atv)))
179		return (error);
180	return (0);
181}
182
183#ifndef _SYS_SYSPROTO_H_
184struct clock_getres_args {
185	clockid_t clock_id;
186	struct	timespec *tp;
187};
188#endif
189
190int
191clock_getres(p, uap)
192	struct proc *p;
193	struct clock_getres_args *uap;
194{
195	struct timespec ts;
196	int error;
197
198	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
199		return (EINVAL);
200	error = 0;
201	if (SCARG(uap, tp)) {
202		ts.tv_sec = 0;
203		ts.tv_nsec = 1000000000 / timecounter->frequency;
204		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
205	}
206	return (error);
207}
208
209static int nanowait;
210
211static int
212nanosleep1(p, rqt, rmt)
213	struct proc *p;
214	struct timespec *rqt, *rmt;
215{
216	struct timeval atv, utv, rtv;
217	int error, s, timo, i, n;
218
219	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
220		return (EINVAL);
221	if (rqt->tv_sec < 0 || rqt->tv_sec == 0 && rqt->tv_nsec == 0)
222		return (0);
223	TIMESPEC_TO_TIMEVAL(&atv, rqt)
224
225	if (itimerfix(&atv)) {
226		n = atv.tv_sec / 100000000;
227		rtv = atv;
228		rtv.tv_sec %= 100000000;
229		(void)itimerfix(&rtv);
230	} else
231		n = 0;
232
233	for (i = 0, error = EWOULDBLOCK; i <= n && error == EWOULDBLOCK; i++) {
234		if (n > 0) {
235			if (i == n)
236				atv = rtv;
237			else {
238				atv.tv_sec = 100000000;
239				atv.tv_usec = 0;
240			}
241		}
242		/*
243		 * XXX this is not as careful as settimeofday() about minimising
244		 * interrupt latency.  The hzto() interface is inconvenient as usual.
245		 */
246		s = splclock();
247		timevaladd(&atv, &time);
248		timo = hzto(&atv);
249		splx(s);
250
251		p->p_sleepend = &atv;
252		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", timo);
253		p->p_sleepend = NULL;
254		if (error == ERESTART)
255			error = EINTR;
256		if (rmt != NULL && (i == n || error != EWOULDBLOCK)) {
257			/*-
258			 * XXX this is unnecessary and possibly wrong if the timeout
259			 * expired.  Then the remaining time should be zero.  If the
260			 * calculation gives a nonzero value, then we have a bug.
261			 * (1) if settimeofday() was called, then the calculation is
262			 *     probably wrong, since `time' has probably become
263			 *     inconsistent with the ending time `atv'.
264			 *     XXX (1) should be fixed now with p->p_sleepend;
265			 * (2) otherwise, our calculation of `timo' was wrong, perhaps
266			 *     due to `tick' being wrong when hzto() was called or
267			 *     changing afterwards (it can be wrong or change due to
268			 *     hzto() not knowing about adjtime(2) or tickadj(8)).
269			 *     Then we should be sleeping again instead instead of
270			 *     returning.  Rounding up in hzto() probably fixes this
271			 *     problem for small timeouts, but the absolute error may
272			 *     be large for large timeouts.
273			 */
274			s = splclock();
275			utv = time;
276			splx(s);
277			if (i != n) {
278				atv.tv_sec += (n - i - 1) * 100000000;
279				timevaladd(&atv, &rtv);
280			}
281			timevalsub(&atv, &utv);
282			if (atv.tv_sec < 0)
283				timerclear(&atv);
284			TIMEVAL_TO_TIMESPEC(&atv, rmt);
285		}
286	}
287	return (error == EWOULDBLOCK ? 0 : error);
288}
289
290#ifndef _SYS_SYSPROTO_H_
291struct nanosleep_args {
292	struct	timespec *rqtp;
293	struct	timespec *rmtp;
294};
295#endif
296
297/* ARGSUSED */
298int
299nanosleep(p, uap)
300	struct proc *p;
301	struct nanosleep_args *uap;
302{
303	struct timespec rmt, rqt;
304	int error, error2;
305
306	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
307	if (error)
308		return (error);
309	if (SCARG(uap, rmtp))
310		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
311			return (EFAULT);
312	error = nanosleep1(p, &rqt, &rmt);
313	if (SCARG(uap, rmtp)) {
314		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
315		if (error2)	/* XXX shouldn't happen, did useracc() above */
316			return (error2);
317	}
318	return (error);
319}
320
321#ifndef _SYS_SYSPROTO_H_
322struct signanosleep_args {
323	struct	timespec *rqtp;
324	struct	timespec *rmtp;
325	sigset_t *mask;
326};
327#endif
328
329/* ARGSUSED */
330int
331signanosleep(p, uap)
332	struct proc *p;
333	struct signanosleep_args *uap;
334{
335	struct timespec rmt, rqt;
336	int error, error2;
337	sigset_t mask;
338
339	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
340	if (error)
341		return (error);
342	if (SCARG(uap, rmtp))
343		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
344			return (EFAULT);
345	error = copyin(SCARG(uap, mask), &mask, sizeof(mask));
346	if (error)
347		return (error);
348
349	/* change mask for sleep */
350	p->p_sigmask = mask &~ sigcantmask;
351
352	error = nanosleep1(p, &rqt, &rmt);
353
354	if (SCARG(uap, rmtp)) {
355		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
356		if (error2)	/* XXX shouldn't happen, did useracc() above */
357			return (error2);
358	}
359	return (error);
360}
361
362#ifndef _SYS_SYSPROTO_H_
363struct gettimeofday_args {
364	struct	timeval *tp;
365	struct	timezone *tzp;
366};
367#endif
368/* ARGSUSED */
369int
370gettimeofday(p, uap)
371	struct proc *p;
372	register struct gettimeofday_args *uap;
373{
374	struct timeval atv;
375	int error = 0;
376
377	if (uap->tp) {
378		microtime(&atv);
379		if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
380		    sizeof (atv))))
381			return (error);
382	}
383	if (uap->tzp)
384		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
385		    sizeof (tz));
386	return (error);
387}
388
389#ifndef _SYS_SYSPROTO_H_
390struct settimeofday_args {
391	struct	timeval *tv;
392	struct	timezone *tzp;
393};
394#endif
395/* ARGSUSED */
396int
397settimeofday(p, uap)
398	struct proc *p;
399	struct settimeofday_args *uap;
400{
401	struct timeval atv;
402	struct timezone atz;
403	int error;
404
405	if ((error = suser(p->p_ucred, &p->p_acflag)))
406		return (error);
407	/* Verify all parameters before changing time. */
408	if (uap->tv) {
409		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
410		    sizeof(atv))))
411			return (error);
412		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
413			return (EINVAL);
414	}
415	if (uap->tzp &&
416	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
417		return (error);
418	if (uap->tv && (error = settime(&atv)))
419		return (error);
420	if (uap->tzp)
421		tz = atz;
422	return (0);
423}
424
425int	tickdelta;			/* current clock skew, us. per tick */
426long	timedelta;			/* unapplied time correction, us. */
427static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
428
429#ifndef _SYS_SYSPROTO_H_
430struct adjtime_args {
431	struct timeval *delta;
432	struct timeval *olddelta;
433};
434#endif
435/* ARGSUSED */
436int
437adjtime(p, uap)
438	struct proc *p;
439	register struct adjtime_args *uap;
440{
441	struct timeval atv;
442	register long ndelta, ntickdelta, odelta;
443	int s, error;
444
445	if ((error = suser(p->p_ucred, &p->p_acflag)))
446		return (error);
447	if ((error =
448	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
449		return (error);
450
451	/*
452	 * Compute the total correction and the rate at which to apply it.
453	 * Round the adjustment down to a whole multiple of the per-tick
454	 * delta, so that after some number of incremental changes in
455	 * hardclock(), tickdelta will become zero, lest the correction
456	 * overshoot and start taking us away from the desired final time.
457	 */
458	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
459	if (ndelta > bigadj || ndelta < -bigadj)
460		ntickdelta = 10 * tickadj;
461	else
462		ntickdelta = tickadj;
463	if (ndelta % ntickdelta)
464		ndelta = ndelta / ntickdelta * ntickdelta;
465
466	/*
467	 * To make hardclock()'s job easier, make the per-tick delta negative
468	 * if we want time to run slower; then hardclock can simply compute
469	 * tick + tickdelta, and subtract tickdelta from timedelta.
470	 */
471	if (ndelta < 0)
472		ntickdelta = -ntickdelta;
473	s = splclock();
474	odelta = timedelta;
475	timedelta = ndelta;
476	tickdelta = ntickdelta;
477	splx(s);
478
479	if (uap->olddelta) {
480		atv.tv_sec = odelta / 1000000;
481		atv.tv_usec = odelta % 1000000;
482		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
483		    sizeof(struct timeval));
484	}
485	return (0);
486}
487
488/*
489 * Get value of an interval timer.  The process virtual and
490 * profiling virtual time timers are kept in the p_stats area, since
491 * they can be swapped out.  These are kept internally in the
492 * way they are specified externally: in time until they expire.
493 *
494 * The real time interval timer is kept in the process table slot
495 * for the process, and its value (it_value) is kept as an
496 * absolute time rather than as a delta, so that it is easy to keep
497 * periodic real-time signals from drifting.
498 *
499 * Virtual time timers are processed in the hardclock() routine of
500 * kern_clock.c.  The real time timer is processed by a timeout
501 * routine, called from the softclock() routine.  Since a callout
502 * may be delayed in real time due to interrupt processing in the system,
503 * it is possible for the real time timeout routine (realitexpire, given below),
504 * to be delayed in real time past when it is supposed to occur.  It
505 * does not suffice, therefore, to reload the real timer .it_value from the
506 * real time timers .it_interval.  Rather, we compute the next time in
507 * absolute time the timer should go off.
508 */
509#ifndef _SYS_SYSPROTO_H_
510struct getitimer_args {
511	u_int	which;
512	struct	itimerval *itv;
513};
514#endif
515/* ARGSUSED */
516int
517getitimer(p, uap)
518	struct proc *p;
519	register struct getitimer_args *uap;
520{
521	struct itimerval aitv;
522	int s;
523
524	if (uap->which > ITIMER_PROF)
525		return (EINVAL);
526	s = splclock();
527	if (uap->which == ITIMER_REAL) {
528		/*
529		 * Convert from absoulte to relative time in .it_value
530		 * part of real time timer.  If time for real time timer
531		 * has passed return 0, else return difference between
532		 * current time and time for the timer to go off.
533		 */
534		aitv = p->p_realtimer;
535		if (timerisset(&aitv.it_value))
536			if (timercmp(&aitv.it_value, &time, <))
537				timerclear(&aitv.it_value);
538			else
539				timevalsub(&aitv.it_value, &time);
540	} else
541		aitv = p->p_stats->p_timer[uap->which];
542	splx(s);
543	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
544	    sizeof (struct itimerval)));
545}
546
547#ifndef _SYS_SYSPROTO_H_
548struct setitimer_args {
549	u_int	which;
550	struct	itimerval *itv, *oitv;
551};
552#endif
553/* ARGSUSED */
554int
555setitimer(p, uap)
556	struct proc *p;
557	register struct setitimer_args *uap;
558{
559	struct itimerval aitv;
560	register struct itimerval *itvp;
561	int s, error;
562
563	if (uap->which > ITIMER_PROF)
564		return (EINVAL);
565	itvp = uap->itv;
566	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
567	    sizeof(struct itimerval))))
568		return (error);
569	if ((uap->itv = uap->oitv) &&
570	    (error = getitimer(p, (struct getitimer_args *)uap)))
571		return (error);
572	if (itvp == 0)
573		return (0);
574	if (itimerfix(&aitv.it_value))
575		return (EINVAL);
576	if (!timerisset(&aitv.it_value))
577		timerclear(&aitv.it_interval);
578	else if (itimerfix(&aitv.it_interval))
579		return (EINVAL);
580	s = splclock();
581	if (uap->which == ITIMER_REAL) {
582		if (timerisset(&p->p_realtimer.it_value))
583			untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
584		if (timerisset(&aitv.it_value)) {
585			timevaladd(&aitv.it_value, &time);
586			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
587						hzto(&aitv.it_value));
588		}
589		p->p_realtimer = aitv;
590	} else
591		p->p_stats->p_timer[uap->which] = aitv;
592	splx(s);
593	return (0);
594}
595
596/*
597 * Real interval timer expired:
598 * send process whose timer expired an alarm signal.
599 * If time is not set up to reload, then just return.
600 * Else compute next time timer should go off which is > current time.
601 * This is where delay in processing this timeout causes multiple
602 * SIGALRM calls to be compressed into one.
603 * hzto() always adds 1 to allow for the time until the next clock
604 * interrupt being strictly less than 1 clock tick, but we don't want
605 * that here since we want to appear to be in sync with the clock
606 * interrupt even when we're delayed.
607 */
608void
609realitexpire(arg)
610	void *arg;
611{
612	register struct proc *p;
613	int s;
614
615	p = (struct proc *)arg;
616	psignal(p, SIGALRM);
617	if (!timerisset(&p->p_realtimer.it_interval)) {
618		timerclear(&p->p_realtimer.it_value);
619		return;
620	}
621	for (;;) {
622		s = splclock();
623		timevaladd(&p->p_realtimer.it_value,
624		    &p->p_realtimer.it_interval);
625		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
626			p->p_ithandle =
627			    timeout(realitexpire, (caddr_t)p,
628				    hzto(&p->p_realtimer.it_value) - 1);
629			splx(s);
630			return;
631		}
632		splx(s);
633	}
634}
635
636/*
637 * Check that a proposed value to load into the .it_value or
638 * .it_interval part of an interval timer is acceptable, and
639 * fix it to have at least minimal value (i.e. if it is less
640 * than the resolution of the clock, round it up.)
641 */
642int
643itimerfix(tv)
644	struct timeval *tv;
645{
646
647	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
648	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
649		return (EINVAL);
650	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
651		tv->tv_usec = tick;
652	return (0);
653}
654
655/*
656 * Decrement an interval timer by a specified number
657 * of microseconds, which must be less than a second,
658 * i.e. < 1000000.  If the timer expires, then reload
659 * it.  In this case, carry over (usec - old value) to
660 * reduce the value reloaded into the timer so that
661 * the timer does not drift.  This routine assumes
662 * that it is called in a context where the timers
663 * on which it is operating cannot change in value.
664 */
665int
666itimerdecr(itp, usec)
667	register struct itimerval *itp;
668	int usec;
669{
670
671	if (itp->it_value.tv_usec < usec) {
672		if (itp->it_value.tv_sec == 0) {
673			/* expired, and already in next interval */
674			usec -= itp->it_value.tv_usec;
675			goto expire;
676		}
677		itp->it_value.tv_usec += 1000000;
678		itp->it_value.tv_sec--;
679	}
680	itp->it_value.tv_usec -= usec;
681	usec = 0;
682	if (timerisset(&itp->it_value))
683		return (1);
684	/* expired, exactly at end of interval */
685expire:
686	if (timerisset(&itp->it_interval)) {
687		itp->it_value = itp->it_interval;
688		itp->it_value.tv_usec -= usec;
689		if (itp->it_value.tv_usec < 0) {
690			itp->it_value.tv_usec += 1000000;
691			itp->it_value.tv_sec--;
692		}
693	} else
694		itp->it_value.tv_usec = 0;		/* sec is already 0 */
695	return (0);
696}
697
698/*
699 * Add and subtract routines for timevals.
700 * N.B.: subtract routine doesn't deal with
701 * results which are before the beginning,
702 * it just gets very confused in this case.
703 * Caveat emptor.
704 */
705void
706timevaladd(t1, t2)
707	struct timeval *t1, *t2;
708{
709
710	t1->tv_sec += t2->tv_sec;
711	t1->tv_usec += t2->tv_usec;
712	timevalfix(t1);
713}
714
715void
716timevalsub(t1, t2)
717	struct timeval *t1, *t2;
718{
719
720	t1->tv_sec -= t2->tv_sec;
721	t1->tv_usec -= t2->tv_usec;
722	timevalfix(t1);
723}
724
725static void
726timevalfix(t1)
727	struct timeval *t1;
728{
729
730	if (t1->tv_usec < 0) {
731		t1->tv_sec--;
732		t1->tv_usec += 1000000;
733	}
734	if (t1->tv_usec >= 1000000) {
735		t1->tv_sec++;
736		t1->tv_usec -= 1000000;
737	}
738}
739