kern_time.c revision 1.23 1 /* $NetBSD: kern_time.c,v 1.23 1996/11/15 23:53:32 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 #include <sys/signalvar.h>
45
46 #include <sys/mount.h>
47 #include <sys/syscallargs.h>
48
49 #if defined(NFSCLIENT) || defined(NFSSERVER)
50 #include <nfs/rpcv2.h>
51 #include <nfs/nfsproto.h>
52 #include <nfs/nfs_var.h>
53 #endif
54
55 #include <machine/cpu.h>
56
57 static void settime __P((struct timeval *));
58
59 /*
60 * Time of day and interval timer support.
61 *
62 * These routines provide the kernel entry points to get and set
63 * the time-of-day and per-process interval timers. Subroutines
64 * here provide support for adding and subtracting timeval structures
65 * and decrementing interval timers, optionally reloading the interval
66 * timers when they expire.
67 */
68
69
70 /* This function is used by clock_settime and settimeofday */
71 static void
72 settime(tv)
73 struct timeval *tv;
74 {
75 struct timeval delta;
76 int s;
77
78 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
79 s = splclock();
80 timersub(tv, &time, &delta);
81 time = *tv;
82 (void) splsoftclock();
83 timeradd(&boottime, &delta, &boottime);
84 timeradd(&runtime, &delta, &runtime);
85 # if defined(NFSCLIENT) || defined(NFSSERVER)
86 nqnfs_lease_updatetime(delta.tv_sec);
87 # endif
88 splx(s);
89 resettodr();
90 }
91
92 /* ARGSUSED */
93 int
94 sys_clock_gettime(p, v, retval)
95 struct proc *p;
96 void *v;
97 register_t *retval;
98 {
99 register struct sys_clock_gettime_args /* {
100 syscallarg(clockid_t) clock_id;
101 syscallarg(struct timespec *) tp;
102 } */ *uap = v;
103 clockid_t clock_id;
104 struct timeval atv;
105 struct timespec ats;
106
107 clock_id = SCARG(uap, clock_id);
108 if (clock_id != CLOCK_REALTIME)
109 return (EINVAL);
110
111 microtime(&atv);
112 TIMEVAL_TO_TIMESPEC(&atv,&ats);
113
114 return copyout((caddr_t)&ats, SCARG(uap, tp), sizeof(ats));
115 }
116
117 /* ARGSUSED */
118 int
119 sys_clock_settime(p, v, retval)
120 struct proc *p;
121 void *v;
122 register_t *retval;
123 {
124 register struct sys_clock_settime_args /* {
125 syscallarg(clockid_t) clock_id;
126 syscallarg(const struct timespec *) tp;
127 } */ *uap = v;
128 clockid_t clock_id;
129 struct timeval atv;
130 struct timespec ats;
131 int error;
132
133 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
134 return (error);
135
136 clock_id = SCARG(uap, clock_id);
137 if (clock_id != CLOCK_REALTIME)
138 return (EINVAL);
139
140 if ((error = copyin((const char *)SCARG(uap, tp), (caddr_t)&ats,
141 sizeof(ats))) != 0)
142 return (error);
143
144 TIMESPEC_TO_TIMEVAL(&atv,&ats);
145 settime(&atv);
146
147 return 0;
148 }
149
150 int
151 sys_clock_getres(p, v, retval)
152 struct proc *p;
153 void *v;
154 register_t *retval;
155 {
156 register struct sys_clock_getres_args /* {
157 syscallarg(clockid_t) clock_id;
158 syscallarg(struct timespec *) tp;
159 } */ *uap = v;
160 clockid_t clock_id;
161 struct timespec ts;
162 int error = 0;
163
164 clock_id = SCARG(uap, clock_id);
165 if (clock_id != CLOCK_REALTIME)
166 return (EINVAL);
167
168 if (SCARG(uap, tp)) {
169 ts.tv_sec = 0;
170 ts.tv_nsec = 1000000000 / hz;
171
172 error = copyout((caddr_t)&ts, (caddr_t)SCARG(uap, tp),
173 sizeof (ts));
174 }
175
176 return error;
177 }
178
179
180 /* ARGSUSED */
181 int
182 sys_gettimeofday(p, v, retval)
183 struct proc *p;
184 void *v;
185 register_t *retval;
186 {
187 register struct sys_gettimeofday_args /* {
188 syscallarg(struct timeval *) tp;
189 syscallarg(struct timezone *) tzp;
190 } */ *uap = v;
191 struct timeval atv;
192 int error = 0;
193
194 if (SCARG(uap, tp)) {
195 microtime(&atv);
196 error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
197 sizeof (atv));
198 if (error)
199 return (error);
200 }
201 if (SCARG(uap, tzp))
202 error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
203 sizeof (tz));
204 return (error);
205 }
206
207 /* ARGSUSED */
208 int
209 sys_settimeofday(p, v, retval)
210 struct proc *p;
211 void *v;
212 register_t *retval;
213 {
214 struct sys_settimeofday_args /* {
215 syscallarg(struct timeval *) tv;
216 syscallarg(struct timezone *) tzp;
217 } */ *uap = v;
218 struct timeval atv;
219 struct timezone atz;
220 int error;
221
222 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
223 return (error);
224 /* Verify all parameters before changing time. */
225 if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
226 (caddr_t)&atv, sizeof(atv))))
227 return (error);
228 if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
229 (caddr_t)&atz, sizeof(atz))))
230 return (error);
231 if (SCARG(uap, tv))
232 settime(&atv);
233 if (SCARG(uap, tzp))
234 tz = atz;
235 return (0);
236 }
237
238 int tickdelta; /* current clock skew, us. per tick */
239 long timedelta; /* unapplied time correction, us. */
240 long bigadj = 1000000; /* use 10x skew above bigadj us. */
241
242 /* ARGSUSED */
243 int
244 sys_adjtime(p, v, retval)
245 struct proc *p;
246 void *v;
247 register_t *retval;
248 {
249 register struct sys_adjtime_args /* {
250 syscallarg(struct timeval *) delta;
251 syscallarg(struct timeval *) olddelta;
252 } */ *uap = v;
253 struct timeval atv;
254 register long ndelta, ntickdelta, odelta;
255 int s, error;
256
257 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
258 return (error);
259
260 error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
261 sizeof(struct timeval));
262 if (error)
263 return (error);
264
265 /*
266 * Compute the total correction and the rate at which to apply it.
267 * Round the adjustment down to a whole multiple of the per-tick
268 * delta, so that after some number of incremental changes in
269 * hardclock(), tickdelta will become zero, lest the correction
270 * overshoot and start taking us away from the desired final time.
271 */
272 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
273 if (ndelta > bigadj)
274 ntickdelta = 10 * tickadj;
275 else
276 ntickdelta = tickadj;
277 if (ndelta % ntickdelta)
278 ndelta = ndelta / ntickdelta * ntickdelta;
279
280 /*
281 * To make hardclock()'s job easier, make the per-tick delta negative
282 * if we want time to run slower; then hardclock can simply compute
283 * tick + tickdelta, and subtract tickdelta from timedelta.
284 */
285 if (ndelta < 0)
286 ntickdelta = -ntickdelta;
287 s = splclock();
288 odelta = timedelta;
289 timedelta = ndelta;
290 tickdelta = ntickdelta;
291 splx(s);
292
293 if (SCARG(uap, olddelta)) {
294 atv.tv_sec = odelta / 1000000;
295 atv.tv_usec = odelta % 1000000;
296 (void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
297 sizeof(struct timeval));
298 }
299 return (0);
300 }
301
302 /*
303 * Get value of an interval timer. The process virtual and
304 * profiling virtual time timers are kept in the p_stats area, since
305 * they can be swapped out. These are kept internally in the
306 * way they are specified externally: in time until they expire.
307 *
308 * The real time interval timer is kept in the process table slot
309 * for the process, and its value (it_value) is kept as an
310 * absolute time rather than as a delta, so that it is easy to keep
311 * periodic real-time signals from drifting.
312 *
313 * Virtual time timers are processed in the hardclock() routine of
314 * kern_clock.c. The real time timer is processed by a timeout
315 * routine, called from the softclock() routine. Since a callout
316 * may be delayed in real time due to interrupt processing in the system,
317 * it is possible for the real time timeout routine (realitexpire, given below),
318 * to be delayed in real time past when it is supposed to occur. It
319 * does not suffice, therefore, to reload the real timer .it_value from the
320 * real time timers .it_interval. Rather, we compute the next time in
321 * absolute time the timer should go off.
322 */
323 /* ARGSUSED */
324 int
325 sys_getitimer(p, v, retval)
326 struct proc *p;
327 void *v;
328 register_t *retval;
329 {
330 register struct sys_getitimer_args /* {
331 syscallarg(u_int) which;
332 syscallarg(struct itimerval *) itv;
333 } */ *uap = v;
334 struct itimerval aitv;
335 int s;
336
337 if (SCARG(uap, which) > ITIMER_PROF)
338 return (EINVAL);
339 s = splclock();
340 if (SCARG(uap, which) == ITIMER_REAL) {
341 /*
342 * Convert from absolute to relative time in .it_value
343 * part of real time timer. If time for real time timer
344 * has passed return 0, else return difference between
345 * current time and time for the timer to go off.
346 */
347 aitv = p->p_realtimer;
348 if (timerisset(&aitv.it_value))
349 if (timercmp(&aitv.it_value, &time, <))
350 timerclear(&aitv.it_value);
351 else
352 timersub(&aitv.it_value, &time, &aitv.it_value);
353 } else
354 aitv = p->p_stats->p_timer[SCARG(uap, which)];
355 splx(s);
356 return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
357 sizeof (struct itimerval)));
358 }
359
360 /* ARGSUSED */
361 int
362 sys_setitimer(p, v, retval)
363 struct proc *p;
364 register void *v;
365 register_t *retval;
366 {
367 register struct sys_setitimer_args /* {
368 syscallarg(u_int) which;
369 syscallarg(struct itimerval *) itv;
370 syscallarg(struct itimerval *) oitv;
371 } */ *uap = v;
372 struct sys_getitimer_args getargs;
373 struct itimerval aitv;
374 register struct itimerval *itvp;
375 int s, error;
376
377 if (SCARG(uap, which) > ITIMER_PROF)
378 return (EINVAL);
379 itvp = SCARG(uap, itv);
380 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
381 sizeof(struct itimerval))))
382 return (error);
383 if (SCARG(uap, oitv) != NULL) {
384 SCARG(&getargs, which) = SCARG(uap, which);
385 SCARG(&getargs, itv) = SCARG(uap, oitv);
386 if ((error = sys_getitimer(p, &getargs, retval)) != 0)
387 return (error);
388 }
389 if (itvp == 0)
390 return (0);
391 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
392 return (EINVAL);
393 s = splclock();
394 if (SCARG(uap, which) == ITIMER_REAL) {
395 untimeout(realitexpire, p);
396 if (timerisset(&aitv.it_value)) {
397 timeradd(&aitv.it_value, &time, &aitv.it_value);
398 timeout(realitexpire, p, hzto(&aitv.it_value));
399 }
400 p->p_realtimer = aitv;
401 } else
402 p->p_stats->p_timer[SCARG(uap, which)] = aitv;
403 splx(s);
404 return (0);
405 }
406
407 /*
408 * Real interval timer expired:
409 * send process whose timer expired an alarm signal.
410 * If time is not set up to reload, then just return.
411 * Else compute next time timer should go off which is > current time.
412 * This is where delay in processing this timeout causes multiple
413 * SIGALRM calls to be compressed into one.
414 */
415 void
416 realitexpire(arg)
417 void *arg;
418 {
419 register struct proc *p;
420 int s;
421
422 p = (struct proc *)arg;
423 psignal(p, SIGALRM);
424 if (!timerisset(&p->p_realtimer.it_interval)) {
425 timerclear(&p->p_realtimer.it_value);
426 return;
427 }
428 for (;;) {
429 s = splclock();
430 timeradd(&p->p_realtimer.it_value,
431 &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
432 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
433 timeout(realitexpire, p,
434 hzto(&p->p_realtimer.it_value));
435 splx(s);
436 return;
437 }
438 splx(s);
439 }
440 }
441
442 /*
443 * Check that a proposed value to load into the .it_value or
444 * .it_interval part of an interval timer is acceptable, and
445 * fix it to have at least minimal value (i.e. if it is less
446 * than the resolution of the clock, round it up.)
447 */
448 int
449 itimerfix(tv)
450 struct timeval *tv;
451 {
452
453 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
454 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
455 return (EINVAL);
456 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
457 tv->tv_usec = tick;
458 return (0);
459 }
460
461 /*
462 * Decrement an interval timer by a specified number
463 * of microseconds, which must be less than a second,
464 * i.e. < 1000000. If the timer expires, then reload
465 * it. In this case, carry over (usec - old value) to
466 * reduce the value reloaded into the timer so that
467 * the timer does not drift. This routine assumes
468 * that it is called in a context where the timers
469 * on which it is operating cannot change in value.
470 */
471 int
472 itimerdecr(itp, usec)
473 register struct itimerval *itp;
474 int usec;
475 {
476
477 if (itp->it_value.tv_usec < usec) {
478 if (itp->it_value.tv_sec == 0) {
479 /* expired, and already in next interval */
480 usec -= itp->it_value.tv_usec;
481 goto expire;
482 }
483 itp->it_value.tv_usec += 1000000;
484 itp->it_value.tv_sec--;
485 }
486 itp->it_value.tv_usec -= usec;
487 usec = 0;
488 if (timerisset(&itp->it_value))
489 return (1);
490 /* expired, exactly at end of interval */
491 expire:
492 if (timerisset(&itp->it_interval)) {
493 itp->it_value = itp->it_interval;
494 itp->it_value.tv_usec -= usec;
495 if (itp->it_value.tv_usec < 0) {
496 itp->it_value.tv_usec += 1000000;
497 itp->it_value.tv_sec--;
498 }
499 } else
500 itp->it_value.tv_usec = 0; /* sec is already 0 */
501 return (0);
502 }
503