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