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