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