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