kern_time.c revision 1.9 1 /* $NetBSD: kern_time.c,v 1.9 1994/06/29 06:32:48 cgd 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 extern int tickadj; /* "standard" clock skew, us./tick */
129 int tickdelta; /* current clock skew, us. per tick */
130 long timedelta; /* unapplied time correction, us. */
131 long bigadj = 1000000; /* use 10x skew above bigadj us. */
132
133 struct adjtime_args {
134 struct timeval *delta;
135 struct timeval *olddelta;
136 };
137 /* ARGSUSED */
138 int
139 adjtime(p, uap, retval)
140 struct proc *p;
141 register struct adjtime_args *uap;
142 int *retval;
143 {
144 struct timeval atv;
145 register long ndelta, ntickdelta, odelta;
146 int s, error;
147
148 if (error = suser(p->p_ucred, &p->p_acflag))
149 return (error);
150 if (error =
151 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval)))
152 return (error);
153
154 /*
155 * Compute the total correction and the rate at which to apply it.
156 * Round the adjustment down to a whole multiple of the per-tick
157 * delta, so that after some number of incremental changes in
158 * hardclock(), tickdelta will become zero, lest the correction
159 * overshoot and start taking us away from the desired final time.
160 */
161 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
162 if (ndelta > bigadj)
163 ntickdelta = 10 * tickadj;
164 else
165 ntickdelta = tickadj;
166 if (ndelta % ntickdelta)
167 ndelta = ndelta / ntickdelta * ntickdelta;
168
169 /*
170 * To make hardclock()'s job easier, make the per-tick delta negative
171 * if we want time to run slower; then hardclock can simply compute
172 * tick + tickdelta, and subtract tickdelta from timedelta.
173 */
174 if (ndelta < 0)
175 ntickdelta = -ntickdelta;
176 s = splclock();
177 odelta = timedelta;
178 timedelta = ndelta;
179 tickdelta = ntickdelta;
180 splx(s);
181
182 if (uap->olddelta) {
183 atv.tv_sec = odelta / 1000000;
184 atv.tv_usec = odelta % 1000000;
185 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
186 sizeof(struct timeval));
187 }
188 return (0);
189 }
190
191 /*
192 * Get value of an interval timer. The process virtual and
193 * profiling virtual time timers are kept in the p_stats area, since
194 * they can be swapped out. These are kept internally in the
195 * way they are specified externally: in time until they expire.
196 *
197 * The real time interval timer is kept in the process table slot
198 * for the process, and its value (it_value) is kept as an
199 * absolute time rather than as a delta, so that it is easy to keep
200 * periodic real-time signals from drifting.
201 *
202 * Virtual time timers are processed in the hardclock() routine of
203 * kern_clock.c. The real time timer is processed by a timeout
204 * routine, called from the softclock() routine. Since a callout
205 * may be delayed in real time due to interrupt processing in the system,
206 * it is possible for the real time timeout routine (realitexpire, given below),
207 * to be delayed in real time past when it is supposed to occur. It
208 * does not suffice, therefore, to reload the real timer .it_value from the
209 * real time timers .it_interval. Rather, we compute the next time in
210 * absolute time the timer should go off.
211 */
212 struct getitimer_args {
213 u_int which;
214 struct itimerval *itv;
215 };
216 /* ARGSUSED */
217 int
218 getitimer(p, uap, retval)
219 struct proc *p;
220 register struct getitimer_args *uap;
221 int *retval;
222 {
223 struct itimerval aitv;
224 int s;
225
226 if (uap->which > ITIMER_PROF)
227 return (EINVAL);
228 s = splclock();
229 if (uap->which == ITIMER_REAL) {
230 /*
231 * Convert from absoulte to relative time in .it_value
232 * part of real time timer. If time for real time timer
233 * has passed return 0, else return difference between
234 * current time and time for the timer to go off.
235 */
236 aitv = p->p_realtimer;
237 if (timerisset(&aitv.it_value))
238 if (timercmp(&aitv.it_value, &time, <))
239 timerclear(&aitv.it_value);
240 else
241 timevalsub(&aitv.it_value,
242 (struct timeval *)&time);
243 } else
244 aitv = p->p_stats->p_timer[uap->which];
245 splx(s);
246 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
247 sizeof (struct itimerval)));
248 }
249
250 struct setitimer_args {
251 u_int which;
252 struct itimerval *itv, *oitv;
253 };
254 /* ARGSUSED */
255 int
256 setitimer(p, uap, retval)
257 struct proc *p;
258 register struct setitimer_args *uap;
259 int *retval;
260 {
261 struct itimerval aitv;
262 register struct itimerval *itvp;
263 int s, error;
264
265 if (uap->which > ITIMER_PROF)
266 return (EINVAL);
267 itvp = uap->itv;
268 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
269 sizeof(struct itimerval))))
270 return (error);
271 if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
272 return (error);
273 if (itvp == 0)
274 return (0);
275 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
276 return (EINVAL);
277 s = splclock();
278 if (uap->which == ITIMER_REAL) {
279 untimeout(realitexpire, p);
280 if (timerisset(&aitv.it_value)) {
281 timevaladd(&aitv.it_value, (struct timeval *)&time);
282 timeout(realitexpire, p, hzto(&aitv.it_value));
283 }
284 p->p_realtimer = aitv;
285 } else
286 p->p_stats->p_timer[uap->which] = aitv;
287 splx(s);
288 return (0);
289 }
290
291 /*
292 * Real interval timer expired:
293 * send process whose timer expired an alarm signal.
294 * If time is not set up to reload, then just return.
295 * Else compute next time timer should go off which is > current time.
296 * This is where delay in processing this timeout causes multiple
297 * SIGALRM calls to be compressed into one.
298 */
299 void
300 realitexpire(arg)
301 void *arg;
302 {
303 register struct proc *p;
304 int s;
305
306 p = (struct proc *)arg;
307 psignal(p, SIGALRM);
308 if (!timerisset(&p->p_realtimer.it_interval)) {
309 timerclear(&p->p_realtimer.it_value);
310 return;
311 }
312 for (;;) {
313 s = splclock();
314 timevaladd(&p->p_realtimer.it_value,
315 &p->p_realtimer.it_interval);
316 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
317 timeout(realitexpire, p,
318 hzto(&p->p_realtimer.it_value));
319 splx(s);
320 return;
321 }
322 splx(s);
323 }
324 }
325
326 /*
327 * Check that a proposed value to load into the .it_value or
328 * .it_interval part of an interval timer is acceptable, and
329 * fix it to have at least minimal value (i.e. if it is less
330 * than the resolution of the clock, round it up.)
331 */
332 int
333 itimerfix(tv)
334 struct timeval *tv;
335 {
336
337 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
338 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
339 return (EINVAL);
340 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
341 tv->tv_usec = tick;
342 return (0);
343 }
344
345 /*
346 * Decrement an interval timer by a specified number
347 * of microseconds, which must be less than a second,
348 * i.e. < 1000000. If the timer expires, then reload
349 * it. In this case, carry over (usec - old value) to
350 * reduce the value reloaded into the timer so that
351 * the timer does not drift. This routine assumes
352 * that it is called in a context where the timers
353 * on which it is operating cannot change in value.
354 */
355 int
356 itimerdecr(itp, usec)
357 register struct itimerval *itp;
358 int usec;
359 {
360
361 if (itp->it_value.tv_usec < usec) {
362 if (itp->it_value.tv_sec == 0) {
363 /* expired, and already in next interval */
364 usec -= itp->it_value.tv_usec;
365 goto expire;
366 }
367 itp->it_value.tv_usec += 1000000;
368 itp->it_value.tv_sec--;
369 }
370 itp->it_value.tv_usec -= usec;
371 usec = 0;
372 if (timerisset(&itp->it_value))
373 return (1);
374 /* expired, exactly at end of interval */
375 expire:
376 if (timerisset(&itp->it_interval)) {
377 itp->it_value = itp->it_interval;
378 itp->it_value.tv_usec -= usec;
379 if (itp->it_value.tv_usec < 0) {
380 itp->it_value.tv_usec += 1000000;
381 itp->it_value.tv_sec--;
382 }
383 } else
384 itp->it_value.tv_usec = 0; /* sec is already 0 */
385 return (0);
386 }
387
388 /*
389 * Add and subtract routines for timevals.
390 * N.B.: subtract routine doesn't deal with
391 * results which are before the beginning,
392 * it just gets very confused in this case.
393 * Caveat emptor.
394 */
395 void
396 timevaladd(t1, t2)
397 struct timeval *t1, *t2;
398 {
399
400 t1->tv_sec += t2->tv_sec;
401 t1->tv_usec += t2->tv_usec;
402 timevalfix(t1);
403 }
404
405 void
406 timevalsub(t1, t2)
407 struct timeval *t1, *t2;
408 {
409
410 t1->tv_sec -= t2->tv_sec;
411 t1->tv_usec -= t2->tv_usec;
412 timevalfix(t1);
413 }
414
415 void
416 timevalfix(t1)
417 struct timeval *t1;
418 {
419
420 if (t1->tv_usec < 0) {
421 t1->tv_sec--;
422 t1->tv_usec += 1000000;
423 }
424 if (t1->tv_usec >= 1000000) {
425 t1->tv_sec++;
426 t1->tv_usec -= 1000000;
427 }
428 }
429