kern_time.c revision 1.10 1 /* $NetBSD: kern_time.c,v 1.10 1994/09/18 21:49:07 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
36 */
37
38 #include <sys/param.h>
39 #include <sys/resourcevar.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44
45 #include <machine/cpu.h>
46
47 /*
48 * Time of day and interval timer support.
49 *
50 * These routines provide the kernel entry points to get and set
51 * the time-of-day and per-process interval timers. Subroutines
52 * here provide support for adding and subtracting timeval structures
53 * and decrementing interval timers, optionally reloading the interval
54 * timers when they expire.
55 */
56
57 struct gettimeofday_args {
58 struct timeval *tp;
59 struct timezone *tzp;
60 };
61 /* ARGSUSED */
62 int
63 gettimeofday(p, uap, retval)
64 struct proc *p;
65 register struct gettimeofday_args *uap;
66 int *retval;
67 {
68 struct timeval atv;
69 int error = 0;
70
71 if (uap->tp) {
72 microtime(&atv);
73 if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
74 sizeof (atv)))
75 return (error);
76 }
77 if (uap->tzp)
78 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
79 sizeof (tz));
80 return (error);
81 }
82
83 struct settimeofday_args {
84 struct timeval *tv;
85 struct timezone *tzp;
86 };
87 /* ARGSUSED */
88 int
89 settimeofday(p, uap, retval)
90 struct proc *p;
91 struct settimeofday_args *uap;
92 int *retval;
93 {
94 struct timeval atv, delta;
95 struct timezone atz;
96 int error, s;
97
98 if (error = suser(p->p_ucred, &p->p_acflag))
99 return (error);
100 /* Verify all parameters before changing time. */
101 if (uap->tv &&
102 (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv))))
103 return (error);
104 if (uap->tzp &&
105 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
106 return (error);
107 if (uap->tv) {
108 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
109 s = splclock();
110 /* nb. delta.tv_usec may be < 0, but this is OK here */
111 delta.tv_sec = atv.tv_sec - time.tv_sec;
112 delta.tv_usec = atv.tv_usec - time.tv_usec;
113 time = atv;
114 (void) splsoftclock();
115 timevaladd(&boottime, &delta);
116 timevalfix(&boottime);
117 timevaladd(&runtime, &delta);
118 timevalfix(&runtime);
119 LEASE_UPDATETIME(delta.tv_sec);
120 splx(s);
121 resettodr();
122 }
123 if (uap->tzp)
124 tz = atz;
125 return (0);
126 }
127
128 int tickdelta; /* current clock skew, us. per tick */
129 long timedelta; /* unapplied time correction, us. */
130 long bigadj = 1000000; /* use 10x skew above bigadj us. */
131
132 struct adjtime_args {
133 struct timeval *delta;
134 struct timeval *olddelta;
135 };
136 /* ARGSUSED */
137 int
138 adjtime(p, uap, retval)
139 struct proc *p;
140 register struct adjtime_args *uap;
141 int *retval;
142 {
143 struct timeval atv;
144 register long ndelta, ntickdelta, odelta;
145 int s, error;
146
147 if (error = suser(p->p_ucred, &p->p_acflag))
148 return (error);
149 if (error =
150 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval)))
151 return (error);
152
153 /*
154 * Compute the total correction and the rate at which to apply it.
155 * Round the adjustment down to a whole multiple of the per-tick
156 * delta, so that after some number of incremental changes in
157 * hardclock(), tickdelta will become zero, lest the correction
158 * overshoot and start taking us away from the desired final time.
159 */
160 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
161 if (ndelta > bigadj)
162 ntickdelta = 10 * tickadj;
163 else
164 ntickdelta = tickadj;
165 if (ndelta % ntickdelta)
166 ndelta = ndelta / ntickdelta * ntickdelta;
167
168 /*
169 * To make hardclock()'s job easier, make the per-tick delta negative
170 * if we want time to run slower; then hardclock can simply compute
171 * tick + tickdelta, and subtract tickdelta from timedelta.
172 */
173 if (ndelta < 0)
174 ntickdelta = -ntickdelta;
175 s = splclock();
176 odelta = timedelta;
177 timedelta = ndelta;
178 tickdelta = ntickdelta;
179 splx(s);
180
181 if (uap->olddelta) {
182 atv.tv_sec = odelta / 1000000;
183 atv.tv_usec = odelta % 1000000;
184 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
185 sizeof(struct timeval));
186 }
187 return (0);
188 }
189
190 /*
191 * Get value of an interval timer. The process virtual and
192 * profiling virtual time timers are kept in the p_stats area, since
193 * they can be swapped out. These are kept internally in the
194 * way they are specified externally: in time until they expire.
195 *
196 * The real time interval timer is kept in the process table slot
197 * for the process, and its value (it_value) is kept as an
198 * absolute time rather than as a delta, so that it is easy to keep
199 * periodic real-time signals from drifting.
200 *
201 * Virtual time timers are processed in the hardclock() routine of
202 * kern_clock.c. The real time timer is processed by a timeout
203 * routine, called from the softclock() routine. Since a callout
204 * may be delayed in real time due to interrupt processing in the system,
205 * it is possible for the real time timeout routine (realitexpire, given below),
206 * to be delayed in real time past when it is supposed to occur. It
207 * does not suffice, therefore, to reload the real timer .it_value from the
208 * real time timers .it_interval. Rather, we compute the next time in
209 * absolute time the timer should go off.
210 */
211 struct getitimer_args {
212 u_int which;
213 struct itimerval *itv;
214 };
215 /* ARGSUSED */
216 int
217 getitimer(p, uap, retval)
218 struct proc *p;
219 register struct getitimer_args *uap;
220 int *retval;
221 {
222 struct itimerval aitv;
223 int s;
224
225 if (uap->which > ITIMER_PROF)
226 return (EINVAL);
227 s = splclock();
228 if (uap->which == ITIMER_REAL) {
229 /*
230 * Convert from absoulte to relative time in .it_value
231 * part of real time timer. If time for real time timer
232 * has passed return 0, else return difference between
233 * current time and time for the timer to go off.
234 */
235 aitv = p->p_realtimer;
236 if (timerisset(&aitv.it_value))
237 if (timercmp(&aitv.it_value, &time, <))
238 timerclear(&aitv.it_value);
239 else
240 timevalsub(&aitv.it_value,
241 (struct timeval *)&time);
242 } else
243 aitv = p->p_stats->p_timer[uap->which];
244 splx(s);
245 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
246 sizeof (struct itimerval)));
247 }
248
249 struct setitimer_args {
250 u_int which;
251 struct itimerval *itv, *oitv;
252 };
253 /* ARGSUSED */
254 int
255 setitimer(p, uap, retval)
256 struct proc *p;
257 register struct setitimer_args *uap;
258 int *retval;
259 {
260 struct itimerval aitv;
261 register struct itimerval *itvp;
262 int s, error;
263
264 if (uap->which > ITIMER_PROF)
265 return (EINVAL);
266 itvp = uap->itv;
267 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
268 sizeof(struct itimerval))))
269 return (error);
270 if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
271 return (error);
272 if (itvp == 0)
273 return (0);
274 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
275 return (EINVAL);
276 s = splclock();
277 if (uap->which == ITIMER_REAL) {
278 untimeout(realitexpire, p);
279 if (timerisset(&aitv.it_value)) {
280 timevaladd(&aitv.it_value, (struct timeval *)&time);
281 timeout(realitexpire, p, hzto(&aitv.it_value));
282 }
283 p->p_realtimer = aitv;
284 } else
285 p->p_stats->p_timer[uap->which] = aitv;
286 splx(s);
287 return (0);
288 }
289
290 /*
291 * Real interval timer expired:
292 * send process whose timer expired an alarm signal.
293 * If time is not set up to reload, then just return.
294 * Else compute next time timer should go off which is > current time.
295 * This is where delay in processing this timeout causes multiple
296 * SIGALRM calls to be compressed into one.
297 */
298 void
299 realitexpire(arg)
300 void *arg;
301 {
302 register struct proc *p;
303 int s;
304
305 p = (struct proc *)arg;
306 psignal(p, SIGALRM);
307 if (!timerisset(&p->p_realtimer.it_interval)) {
308 timerclear(&p->p_realtimer.it_value);
309 return;
310 }
311 for (;;) {
312 s = splclock();
313 timevaladd(&p->p_realtimer.it_value,
314 &p->p_realtimer.it_interval);
315 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
316 timeout(realitexpire, p,
317 hzto(&p->p_realtimer.it_value));
318 splx(s);
319 return;
320 }
321 splx(s);
322 }
323 }
324
325 /*
326 * Check that a proposed value to load into the .it_value or
327 * .it_interval part of an interval timer is acceptable, and
328 * fix it to have at least minimal value (i.e. if it is less
329 * than the resolution of the clock, round it up.)
330 */
331 int
332 itimerfix(tv)
333 struct timeval *tv;
334 {
335
336 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
337 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
338 return (EINVAL);
339 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
340 tv->tv_usec = tick;
341 return (0);
342 }
343
344 /*
345 * Decrement an interval timer by a specified number
346 * of microseconds, which must be less than a second,
347 * i.e. < 1000000. If the timer expires, then reload
348 * it. In this case, carry over (usec - old value) to
349 * reduce the value reloaded into the timer so that
350 * the timer does not drift. This routine assumes
351 * that it is called in a context where the timers
352 * on which it is operating cannot change in value.
353 */
354 int
355 itimerdecr(itp, usec)
356 register struct itimerval *itp;
357 int usec;
358 {
359
360 if (itp->it_value.tv_usec < usec) {
361 if (itp->it_value.tv_sec == 0) {
362 /* expired, and already in next interval */
363 usec -= itp->it_value.tv_usec;
364 goto expire;
365 }
366 itp->it_value.tv_usec += 1000000;
367 itp->it_value.tv_sec--;
368 }
369 itp->it_value.tv_usec -= usec;
370 usec = 0;
371 if (timerisset(&itp->it_value))
372 return (1);
373 /* expired, exactly at end of interval */
374 expire:
375 if (timerisset(&itp->it_interval)) {
376 itp->it_value = itp->it_interval;
377 itp->it_value.tv_usec -= usec;
378 if (itp->it_value.tv_usec < 0) {
379 itp->it_value.tv_usec += 1000000;
380 itp->it_value.tv_sec--;
381 }
382 } else
383 itp->it_value.tv_usec = 0; /* sec is already 0 */
384 return (0);
385 }
386
387 /*
388 * Add and subtract routines for timevals.
389 * N.B.: subtract routine doesn't deal with
390 * results which are before the beginning,
391 * it just gets very confused in this case.
392 * Caveat emptor.
393 */
394 void
395 timevaladd(t1, t2)
396 struct timeval *t1, *t2;
397 {
398
399 t1->tv_sec += t2->tv_sec;
400 t1->tv_usec += t2->tv_usec;
401 timevalfix(t1);
402 }
403
404 void
405 timevalsub(t1, t2)
406 struct timeval *t1, *t2;
407 {
408
409 t1->tv_sec -= t2->tv_sec;
410 t1->tv_usec -= t2->tv_usec;
411 timevalfix(t1);
412 }
413
414 void
415 timevalfix(t1)
416 struct timeval *t1;
417 {
418
419 if (t1->tv_usec < 0) {
420 t1->tv_sec--;
421 t1->tv_usec += 1000000;
422 }
423 if (t1->tv_usec >= 1000000) {
424 t1->tv_sec++;
425 t1->tv_usec -= 1000000;
426 }
427 }
428