xref: /csrg-svn/sys/kern/kern_time.c (revision 54927)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_time.c	7.18 (Berkeley) 07/10/92
8  */
9 
10 #include "param.h"
11 #include "resourcevar.h"
12 #include "kernel.h"
13 #include "systm.h"
14 #include "proc.h"
15 #include "vnode.h"
16 
17 #include "machine/cpu.h"
18 
19 /*
20  * Time of day and interval timer support.
21  *
22  * These routines provide the kernel entry points to get and set
23  * the time-of-day and per-process interval timers.  Subroutines
24  * here provide support for adding and subtracting timeval structures
25  * and decrementing interval timers, optionally reloading the interval
26  * timers when they expire.
27  */
28 
29 struct gettimeofday_args {
30 	struct	timeval *tp;
31 	struct	timezone *tzp;
32 };
33 /* ARGSUSED */
34 gettimeofday(p, uap, retval)
35 	struct proc *p;
36 	register struct gettimeofday_args *uap;
37 	int *retval;
38 {
39 	struct timeval atv;
40 	int error = 0;
41 
42 	if (uap->tp) {
43 		microtime(&atv);
44 		if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
45 		    sizeof (atv)))
46 			return (error);
47 	}
48 	if (uap->tzp)
49 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
50 		    sizeof (tz));
51 	return (error);
52 }
53 
54 struct settimeofday_args {
55 	struct	timeval *tv;
56 	struct	timezone *tzp;
57 };
58 /* ARGSUSED */
59 settimeofday(p, uap, retval)
60 	struct proc *p;
61 	struct settimeofday_args *uap;
62 	int *retval;
63 {
64 	struct timeval atv, delta;
65 	struct timezone atz;
66 	int error, s;
67 
68 	if (error = suser(p->p_ucred, &p->p_acflag))
69 		return (error);
70 	/* Verify all parameters before changing time. */
71 	if (uap->tv &&
72 	    (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv))))
73 		return (error);
74 	if (uap->tzp &&
75 	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
76 		return (error);
77 	if (uap->tv) {
78 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
79 		s = splclock();
80 		/* nb. delta.tv_usec may be < 0, but this is OK here */
81 		delta.tv_sec = atv.tv_sec - time.tv_sec;
82 		delta.tv_usec = atv.tv_usec - time.tv_usec;
83 		time = atv;
84 		(void) splsoftclock();
85 		timevaladd(&boottime, &delta);
86 		timevalfix(&boottime);
87 		timevaladd(&runtime, &delta);
88 		timevalfix(&runtime);
89 		LEASE_UPDATETIME(delta.tv_sec);
90 		splx(s);
91 		resettodr();
92 	}
93 	if (uap->tzp)
94 		tz = atz;
95 	return (0);
96 }
97 
98 extern	int tickadj;			/* "standard" clock skew, us./tick */
99 int	tickdelta;			/* current clock skew, us. per tick */
100 long	timedelta;			/* unapplied time correction, us. */
101 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
102 
103 struct adjtime_args {
104 	struct timeval *delta;
105 	struct timeval *olddelta;
106 };
107 /* ARGSUSED */
108 adjtime(p, uap, retval)
109 	struct proc *p;
110 	register struct adjtime_args *uap;
111 	int *retval;
112 {
113 	struct timeval atv, oatv;
114 	register long ndelta;
115 	int s, error;
116 
117 	if (error = suser(p->p_ucred, &p->p_acflag))
118 		return (error);
119 	if (error =
120 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
121 		return (error);
122 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
123 	if (timedelta == 0)
124 		if (ndelta > bigadj)
125 			tickdelta = 10 * tickadj;
126 		else
127 			tickdelta = tickadj;
128 	if (ndelta % tickdelta)
129 		ndelta = ndelta / tickadj * tickadj;
130 
131 	s = splclock();
132 	if (uap->olddelta) {
133 		oatv.tv_sec = timedelta / 1000000;
134 		oatv.tv_usec = timedelta % 1000000;
135 	}
136 	timedelta = ndelta;
137 	splx(s);
138 
139 	if (uap->olddelta)
140 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
141 			sizeof (struct timeval));
142 	return (0);
143 }
144 
145 /*
146  * Get value of an interval timer.  The process virtual and
147  * profiling virtual time timers are kept in the p_stats area, since
148  * they can be swapped out.  These are kept internally in the
149  * way they are specified externally: in time until they expire.
150  *
151  * The real time interval timer is kept in the process table slot
152  * for the process, and its value (it_value) is kept as an
153  * absolute time rather than as a delta, so that it is easy to keep
154  * periodic real-time signals from drifting.
155  *
156  * Virtual time timers are processed in the hardclock() routine of
157  * kern_clock.c.  The real time timer is processed by a timeout
158  * routine, called from the softclock() routine.  Since a callout
159  * may be delayed in real time due to interrupt processing in the system,
160  * it is possible for the real time timeout routine (realitexpire, given below),
161  * to be delayed in real time past when it is supposed to occur.  It
162  * does not suffice, therefore, to reload the real timer .it_value from the
163  * real time timers .it_interval.  Rather, we compute the next time in
164  * absolute time the timer should go off.
165  */
166 struct getitimer_args {
167 	u_int	which;
168 	struct	itimerval *itv;
169 };
170 /* ARGSUSED */
171 getitimer(p, uap, retval)
172 	struct proc *p;
173 	register struct getitimer_args *uap;
174 	int *retval;
175 {
176 	struct itimerval aitv;
177 	int s;
178 
179 	if (uap->which > ITIMER_PROF)
180 		return (EINVAL);
181 	s = splclock();
182 	if (uap->which == ITIMER_REAL) {
183 		/*
184 		 * Convert from absoulte to relative time in .it_value
185 		 * part of real time timer.  If time for real time timer
186 		 * has passed return 0, else return difference between
187 		 * current time and time for the timer to go off.
188 		 */
189 		aitv = p->p_realtimer;
190 		if (timerisset(&aitv.it_value))
191 			if (timercmp(&aitv.it_value, &time, <))
192 				timerclear(&aitv.it_value);
193 			else
194 				timevalsub(&aitv.it_value,
195 				    (struct timeval *)&time);
196 	} else
197 		aitv = p->p_stats->p_timer[uap->which];
198 	splx(s);
199 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
200 	    sizeof (struct itimerval)));
201 }
202 
203 struct setitimer_args {
204 	u_int	which;
205 	struct	itimerval *itv, *oitv;
206 };
207 /* ARGSUSED */
208 setitimer(p, uap, retval)
209 	struct proc *p;
210 	register struct setitimer_args *uap;
211 	int *retval;
212 {
213 	struct itimerval aitv;
214 	register struct itimerval *itvp;
215 	int s, error;
216 
217 	if (uap->which > ITIMER_PROF)
218 		return (EINVAL);
219 	itvp = uap->itv;
220 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
221 	    sizeof(struct itimerval))))
222 		return (error);
223 	if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
224 		return (error);
225 	if (itvp == 0)
226 		return (0);
227 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
228 		return (EINVAL);
229 	s = splclock();
230 	if (uap->which == ITIMER_REAL) {
231 		untimeout(realitexpire, (caddr_t)p);
232 		if (timerisset(&aitv.it_value)) {
233 			timevaladd(&aitv.it_value, (struct timeval *)&time);
234 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
235 		}
236 		p->p_realtimer = aitv;
237 	} else
238 		p->p_stats->p_timer[uap->which] = aitv;
239 	splx(s);
240 	return (0);
241 }
242 
243 /*
244  * Real interval timer expired:
245  * send process whose timer expired an alarm signal.
246  * If time is not set up to reload, then just return.
247  * Else compute next time timer should go off which is > current time.
248  * This is where delay in processing this timeout causes multiple
249  * SIGALRM calls to be compressed into one.
250  */
251 void
252 realitexpire(arg)
253 	void *arg;
254 {
255 	register struct proc *p;
256 	int s;
257 
258 	p = (struct proc *)arg;
259 	psignal(p, SIGALRM);
260 	if (!timerisset(&p->p_realtimer.it_interval)) {
261 		timerclear(&p->p_realtimer.it_value);
262 		return;
263 	}
264 	for (;;) {
265 		s = splclock();
266 		timevaladd(&p->p_realtimer.it_value,
267 		    &p->p_realtimer.it_interval);
268 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
269 			timeout(realitexpire, (caddr_t)p,
270 			    hzto(&p->p_realtimer.it_value));
271 			splx(s);
272 			return;
273 		}
274 		splx(s);
275 	}
276 }
277 
278 /*
279  * Check that a proposed value to load into the .it_value or
280  * .it_interval part of an interval timer is acceptable, and
281  * fix it to have at least minimal value (i.e. if it is less
282  * than the resolution of the clock, round it up.)
283  */
284 itimerfix(tv)
285 	struct timeval *tv;
286 {
287 
288 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
289 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
290 		return (EINVAL);
291 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
292 		tv->tv_usec = tick;
293 	return (0);
294 }
295 
296 /*
297  * Decrement an interval timer by a specified number
298  * of microseconds, which must be less than a second,
299  * i.e. < 1000000.  If the timer expires, then reload
300  * it.  In this case, carry over (usec - old value) to
301  * reduce the value reloaded into the timer so that
302  * the timer does not drift.  This routine assumes
303  * that it is called in a context where the timers
304  * on which it is operating cannot change in value.
305  */
306 itimerdecr(itp, usec)
307 	register struct itimerval *itp;
308 	int usec;
309 {
310 
311 	if (itp->it_value.tv_usec < usec) {
312 		if (itp->it_value.tv_sec == 0) {
313 			/* expired, and already in next interval */
314 			usec -= itp->it_value.tv_usec;
315 			goto expire;
316 		}
317 		itp->it_value.tv_usec += 1000000;
318 		itp->it_value.tv_sec--;
319 	}
320 	itp->it_value.tv_usec -= usec;
321 	usec = 0;
322 	if (timerisset(&itp->it_value))
323 		return (1);
324 	/* expired, exactly at end of interval */
325 expire:
326 	if (timerisset(&itp->it_interval)) {
327 		itp->it_value = itp->it_interval;
328 		itp->it_value.tv_usec -= usec;
329 		if (itp->it_value.tv_usec < 0) {
330 			itp->it_value.tv_usec += 1000000;
331 			itp->it_value.tv_sec--;
332 		}
333 	} else
334 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
335 	return (0);
336 }
337 
338 /*
339  * Add and subtract routines for timevals.
340  * N.B.: subtract routine doesn't deal with
341  * results which are before the beginning,
342  * it just gets very confused in this case.
343  * Caveat emptor.
344  */
345 timevaladd(t1, t2)
346 	struct timeval *t1, *t2;
347 {
348 
349 	t1->tv_sec += t2->tv_sec;
350 	t1->tv_usec += t2->tv_usec;
351 	timevalfix(t1);
352 }
353 
354 timevalsub(t1, t2)
355 	struct timeval *t1, *t2;
356 {
357 
358 	t1->tv_sec -= t2->tv_sec;
359 	t1->tv_usec -= t2->tv_usec;
360 	timevalfix(t1);
361 }
362 
363 timevalfix(t1)
364 	struct timeval *t1;
365 {
366 
367 	if (t1->tv_usec < 0) {
368 		t1->tv_sec--;
369 		t1->tv_usec += 1000000;
370 	}
371 	if (t1->tv_usec >= 1000000) {
372 		t1->tv_sec++;
373 		t1->tv_usec -= 1000000;
374 	}
375 }
376