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