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