kern_time.c revision 1.36.6.3 1 /* $NetBSD: kern_time.c,v 1.36.6.3 2000/02/18 20:25:34 he Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christopher G. Demetriou.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1982, 1986, 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
72 */
73
74 #include "fs_nfs.h"
75 #include "opt_nfsserver.h"
76
77 #include <sys/param.h>
78 #include <sys/resourcevar.h>
79 #include <sys/kernel.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/vnode.h>
83 #include <sys/signalvar.h>
84 #include <sys/syslog.h>
85
86 #include <sys/mount.h>
87 #include <sys/syscallargs.h>
88
89 #include <vm/vm.h>
90 #include <uvm/uvm_extern.h>
91
92 #if defined(NFS) || defined(NFSSERVER)
93 #include <nfs/rpcv2.h>
94 #include <nfs/nfsproto.h>
95 #include <nfs/nfs_var.h>
96 #endif
97
98 #include <machine/cpu.h>
99
100 static int settime __P((struct timeval *));
101
102 /*
103 * Time of day and interval timer support.
104 *
105 * These routines provide the kernel entry points to get and set
106 * the time-of-day and per-process interval timers. Subroutines
107 * here provide support for adding and subtracting timeval structures
108 * and decrementing interval timers, optionally reloading the interval
109 * timers when they expire.
110 */
111
112 /* This function is used by clock_settime and settimeofday */
113 static int
114 settime(tv)
115 struct timeval *tv;
116 {
117 struct timeval delta;
118 int s;
119
120 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
121 s = splclock();
122 timersub(tv, &time, &delta);
123 if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1)
124 return (EPERM);
125 #ifdef notyet
126 if ((delta.tv_sec < 86400) && securelevel > 0)
127 return (EPERM);
128 #endif
129 time = *tv;
130 (void) splsoftclock();
131 timeradd(&boottime, &delta, &boottime);
132 timeradd(&runtime, &delta, &runtime);
133 # if defined(NFS) || defined(NFSSERVER)
134 nqnfs_lease_updatetime(delta.tv_sec);
135 # endif
136 splx(s);
137 resettodr();
138 return (0);
139 }
140
141 /* ARGSUSED */
142 int
143 sys_clock_gettime(p, v, retval)
144 struct proc *p;
145 void *v;
146 register_t *retval;
147 {
148 register struct sys_clock_gettime_args /* {
149 syscallarg(clockid_t) clock_id;
150 syscallarg(struct timespec *) tp;
151 } */ *uap = v;
152 clockid_t clock_id;
153 struct timeval atv;
154 struct timespec ats;
155
156 clock_id = SCARG(uap, clock_id);
157 if (clock_id != CLOCK_REALTIME)
158 return (EINVAL);
159
160 microtime(&atv);
161 TIMEVAL_TO_TIMESPEC(&atv,&ats);
162
163 return copyout(&ats, SCARG(uap, tp), sizeof(ats));
164 }
165
166 /* ARGSUSED */
167 int
168 sys_clock_settime(p, v, retval)
169 struct proc *p;
170 void *v;
171 register_t *retval;
172 {
173 register struct sys_clock_settime_args /* {
174 syscallarg(clockid_t) clock_id;
175 syscallarg(const struct timespec *) tp;
176 } */ *uap = v;
177 clockid_t clock_id;
178 struct timeval atv;
179 struct timespec ats;
180 int error;
181
182 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
183 return (error);
184
185 clock_id = SCARG(uap, clock_id);
186 if (clock_id != CLOCK_REALTIME)
187 return (EINVAL);
188
189 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
190 return (error);
191
192 TIMESPEC_TO_TIMEVAL(&atv,&ats);
193 if ((error = settime(&atv)))
194 return (error);
195
196 return 0;
197 }
198
199 int
200 sys_clock_getres(p, v, retval)
201 struct proc *p;
202 void *v;
203 register_t *retval;
204 {
205 register struct sys_clock_getres_args /* {
206 syscallarg(clockid_t) clock_id;
207 syscallarg(struct timespec *) tp;
208 } */ *uap = v;
209 clockid_t clock_id;
210 struct timespec ts;
211 int error = 0;
212
213 clock_id = SCARG(uap, clock_id);
214 if (clock_id != CLOCK_REALTIME)
215 return (EINVAL);
216
217 if (SCARG(uap, tp)) {
218 ts.tv_sec = 0;
219 ts.tv_nsec = 1000000000 / hz;
220
221 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
222 }
223
224 return error;
225 }
226
227 /* ARGSUSED */
228 int
229 sys_nanosleep(p, v, retval)
230 struct proc *p;
231 void *v;
232 register_t *retval;
233 {
234 static int nanowait;
235 register struct sys_nanosleep_args/* {
236 syscallarg(struct timespec *) rqtp;
237 syscallarg(struct timespec *) rmtp;
238 } */ *uap = v;
239 struct timespec rqt;
240 struct timespec rmt;
241 struct timeval atv, utv;
242 int error, s, timo;
243
244 error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
245 sizeof(struct timespec));
246 if (error)
247 return (error);
248
249 TIMESPEC_TO_TIMEVAL(&atv,&rqt)
250 if (itimerfix(&atv))
251 return (EINVAL);
252
253 s = splclock();
254 timeradd(&atv,&time,&atv);
255 timo = hzto(&atv);
256 /*
257 * Avoid inadvertantly sleeping forever
258 */
259 if (timo == 0)
260 timo = 1;
261 splx(s);
262
263 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
264 if (error == ERESTART)
265 error = EINTR;
266 if (error == EWOULDBLOCK)
267 error = 0;
268
269 if (SCARG(uap, rmtp)) {
270 int error;
271
272 s = splclock();
273 utv = time;
274 splx(s);
275
276 timersub(&atv, &utv, &utv);
277 if (utv.tv_sec < 0)
278 timerclear(&utv);
279
280 TIMEVAL_TO_TIMESPEC(&utv,&rmt);
281 error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
282 sizeof(rmt));
283 if (error)
284 return (error);
285 }
286
287 return error;
288 }
289
290 /* ARGSUSED */
291 int
292 sys_gettimeofday(p, v, retval)
293 struct proc *p;
294 void *v;
295 register_t *retval;
296 {
297 register struct sys_gettimeofday_args /* {
298 syscallarg(struct timeval *) tp;
299 syscallarg(struct timezone *) tzp;
300 } */ *uap = v;
301 struct timeval atv;
302 int error = 0;
303 struct timezone tzfake;
304
305 if (SCARG(uap, tp)) {
306 microtime(&atv);
307 error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
308 if (error)
309 return (error);
310 }
311 if (SCARG(uap, tzp)) {
312 /*
313 * NetBSD has no kernel notion of time zone, so we just
314 * fake up a timezone struct and return it if demanded.
315 */
316 tzfake.tz_minuteswest = 0;
317 tzfake.tz_dsttime = 0;
318 error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
319 }
320 return (error);
321 }
322
323 /* ARGSUSED */
324 int
325 sys_settimeofday(p, v, retval)
326 struct proc *p;
327 void *v;
328 register_t *retval;
329 {
330 struct sys_settimeofday_args /* {
331 syscallarg(const struct timeval *) tv;
332 syscallarg(const struct timezone *) tzp;
333 } */ *uap = v;
334 struct timeval atv;
335 struct timezone atz;
336 int error;
337
338 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
339 return (error);
340 /* Verify all parameters before changing time. */
341 if (SCARG(uap, tv) && (error = copyin(SCARG(uap, tv),
342 &atv, sizeof(atv))))
343 return (error);
344 /* XXX since we don't use tz, probably no point in doing copyin. */
345 if (SCARG(uap, tzp) && (error = copyin(SCARG(uap, tzp),
346 &atz, sizeof(atz))))
347 return (error);
348 if (SCARG(uap, tv))
349 if ((error = settime(&atv)))
350 return (error);
351 /*
352 * NetBSD has no kernel notion of time zone, and only an
353 * obsolete program would try to set it, so we log a warning.
354 */
355 if (SCARG(uap, tzp))
356 log(LOG_WARNING, "pid %d attempted to set the "
357 "(obsolete) kernel time zone\n", p->p_pid);
358 return (0);
359 }
360
361 int tickdelta; /* current clock skew, us. per tick */
362 long timedelta; /* unapplied time correction, us. */
363 long bigadj = 1000000; /* use 10x skew above bigadj us. */
364
365 /* ARGSUSED */
366 int
367 sys_adjtime(p, v, retval)
368 struct proc *p;
369 void *v;
370 register_t *retval;
371 {
372 register struct sys_adjtime_args /* {
373 syscallarg(const struct timeval *) delta;
374 syscallarg(struct timeval *) olddelta;
375 } */ *uap = v;
376 struct timeval atv;
377 register long ndelta, ntickdelta, odelta;
378 int s, error;
379
380 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
381 return (error);
382
383 error = copyin(SCARG(uap, delta), &atv, sizeof(struct timeval));
384 if (error)
385 return (error);
386 if (SCARG(uap, olddelta) != NULL &&
387 uvm_useracc((caddr_t)SCARG(uap, olddelta), sizeof(struct timeval),
388 B_WRITE) == FALSE)
389 return (EFAULT);
390
391 /*
392 * Compute the total correction and the rate at which to apply it.
393 * Round the adjustment down to a whole multiple of the per-tick
394 * delta, so that after some number of incremental changes in
395 * hardclock(), tickdelta will become zero, lest the correction
396 * overshoot and start taking us away from the desired final time.
397 */
398 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
399 if (ndelta > bigadj || ndelta < -bigadj)
400 ntickdelta = 10 * tickadj;
401 else
402 ntickdelta = tickadj;
403 if (ndelta % ntickdelta)
404 ndelta = ndelta / ntickdelta * ntickdelta;
405
406 /*
407 * To make hardclock()'s job easier, make the per-tick delta negative
408 * if we want time to run slower; then hardclock can simply compute
409 * tick + tickdelta, and subtract tickdelta from timedelta.
410 */
411 if (ndelta < 0)
412 ntickdelta = -ntickdelta;
413 s = splclock();
414 odelta = timedelta;
415 timedelta = ndelta;
416 tickdelta = ntickdelta;
417 splx(s);
418
419 if (SCARG(uap, olddelta)) {
420 atv.tv_sec = odelta / 1000000;
421 atv.tv_usec = odelta % 1000000;
422 (void) copyout(&atv, SCARG(uap, olddelta),
423 sizeof(struct timeval));
424 }
425 return (0);
426 }
427
428 /*
429 * Get value of an interval timer. The process virtual and
430 * profiling virtual time timers are kept in the p_stats area, since
431 * they can be swapped out. These are kept internally in the
432 * way they are specified externally: in time until they expire.
433 *
434 * The real time interval timer is kept in the process table slot
435 * for the process, and its value (it_value) is kept as an
436 * absolute time rather than as a delta, so that it is easy to keep
437 * periodic real-time signals from drifting.
438 *
439 * Virtual time timers are processed in the hardclock() routine of
440 * kern_clock.c. The real time timer is processed by a timeout
441 * routine, called from the softclock() routine. Since a callout
442 * may be delayed in real time due to interrupt processing in the system,
443 * it is possible for the real time timeout routine (realitexpire, given below),
444 * to be delayed in real time past when it is supposed to occur. It
445 * does not suffice, therefore, to reload the real timer .it_value from the
446 * real time timers .it_interval. Rather, we compute the next time in
447 * absolute time the timer should go off.
448 */
449 /* ARGSUSED */
450 int
451 sys_getitimer(p, v, retval)
452 struct proc *p;
453 void *v;
454 register_t *retval;
455 {
456 register struct sys_getitimer_args /* {
457 syscallarg(int) which;
458 syscallarg(struct itimerval *) itv;
459 } */ *uap = v;
460 int which = SCARG(uap, which);
461 struct itimerval aitv;
462 int s;
463
464 if ((u_int)which > ITIMER_PROF)
465 return (EINVAL);
466 s = splclock();
467 if (which == ITIMER_REAL) {
468 /*
469 * Convert from absolute to relative time in .it_value
470 * part of real time timer. If time for real time timer
471 * has passed return 0, else return difference between
472 * current time and time for the timer to go off.
473 */
474 aitv = p->p_realtimer;
475 if (timerisset(&aitv.it_value)) {
476 if (timercmp(&aitv.it_value, &time, <))
477 timerclear(&aitv.it_value);
478 else
479 timersub(&aitv.it_value, &time, &aitv.it_value);
480 }
481 } else
482 aitv = p->p_stats->p_timer[which];
483 splx(s);
484 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
485 }
486
487 /* ARGSUSED */
488 int
489 sys_setitimer(p, v, retval)
490 struct proc *p;
491 register void *v;
492 register_t *retval;
493 {
494 register struct sys_setitimer_args /* {
495 syscallarg(int) which;
496 syscallarg(const struct itimerval *) itv;
497 syscallarg(struct itimerval *) oitv;
498 } */ *uap = v;
499 int which = SCARG(uap, which);
500 struct sys_getitimer_args getargs;
501 struct itimerval aitv;
502 register const struct itimerval *itvp;
503 int s, error;
504
505 if ((u_int)which > ITIMER_PROF)
506 return (EINVAL);
507 itvp = SCARG(uap, itv);
508 if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
509 return (error);
510 if (SCARG(uap, oitv) != NULL) {
511 SCARG(&getargs, which) = which;
512 SCARG(&getargs, itv) = SCARG(uap, oitv);
513 if ((error = sys_getitimer(p, &getargs, retval)) != 0)
514 return (error);
515 }
516 if (itvp == 0)
517 return (0);
518 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
519 return (EINVAL);
520 s = splclock();
521 if (which == ITIMER_REAL) {
522 untimeout(realitexpire, p);
523 if (timerisset(&aitv.it_value)) {
524 timeradd(&aitv.it_value, &time, &aitv.it_value);
525 timeout(realitexpire, p, hzto(&aitv.it_value));
526 }
527 p->p_realtimer = aitv;
528 } else
529 p->p_stats->p_timer[which] = aitv;
530 splx(s);
531 return (0);
532 }
533
534 /*
535 * Real interval timer expired:
536 * send process whose timer expired an alarm signal.
537 * If time is not set up to reload, then just return.
538 * Else compute next time timer should go off which is > current time.
539 * This is where delay in processing this timeout causes multiple
540 * SIGALRM calls to be compressed into one.
541 */
542 void
543 realitexpire(arg)
544 void *arg;
545 {
546 register struct proc *p;
547 int s;
548
549 p = (struct proc *)arg;
550 psignal(p, SIGALRM);
551 if (!timerisset(&p->p_realtimer.it_interval)) {
552 timerclear(&p->p_realtimer.it_value);
553 return;
554 }
555 for (;;) {
556 s = splclock();
557 timeradd(&p->p_realtimer.it_value,
558 &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
559 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
560 timeout(realitexpire, p,
561 hzto(&p->p_realtimer.it_value));
562 splx(s);
563 return;
564 }
565 splx(s);
566 }
567 }
568
569 /*
570 * Check that a proposed value to load into the .it_value or
571 * .it_interval part of an interval timer is acceptable, and
572 * fix it to have at least minimal value (i.e. if it is less
573 * than the resolution of the clock, round it up.)
574 */
575 int
576 itimerfix(tv)
577 struct timeval *tv;
578 {
579
580 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
581 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
582 return (EINVAL);
583 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
584 tv->tv_usec = tick;
585 return (0);
586 }
587
588 /*
589 * Decrement an interval timer by a specified number
590 * of microseconds, which must be less than a second,
591 * i.e. < 1000000. If the timer expires, then reload
592 * it. In this case, carry over (usec - old value) to
593 * reduce the value reloaded into the timer so that
594 * the timer does not drift. This routine assumes
595 * that it is called in a context where the timers
596 * on which it is operating cannot change in value.
597 */
598 int
599 itimerdecr(itp, usec)
600 register struct itimerval *itp;
601 int usec;
602 {
603
604 if (itp->it_value.tv_usec < usec) {
605 if (itp->it_value.tv_sec == 0) {
606 /* expired, and already in next interval */
607 usec -= itp->it_value.tv_usec;
608 goto expire;
609 }
610 itp->it_value.tv_usec += 1000000;
611 itp->it_value.tv_sec--;
612 }
613 itp->it_value.tv_usec -= usec;
614 usec = 0;
615 if (timerisset(&itp->it_value))
616 return (1);
617 /* expired, exactly at end of interval */
618 expire:
619 if (timerisset(&itp->it_interval)) {
620 itp->it_value = itp->it_interval;
621 itp->it_value.tv_usec -= usec;
622 if (itp->it_value.tv_usec < 0) {
623 itp->it_value.tv_usec += 1000000;
624 itp->it_value.tv_sec--;
625 }
626 } else
627 itp->it_value.tv_usec = 0; /* sec is already 0 */
628 return (0);
629 }
630
631 /*
632 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
633 * for usage and rationale.
634 */
635 int
636 ratecheck(lasttime, mininterval)
637 struct timeval *lasttime;
638 const struct timeval *mininterval;
639 {
640 struct timeval delta;
641 int s, rv = 0;
642
643 s = splclock();
644 timersub(&mono_time, lasttime, &delta);
645
646 /*
647 * check for 0,0 is so that the message will be seen at least once,
648 * even if interval is huge.
649 */
650 if (timercmp(&delta, mininterval, >=) ||
651 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
652 *lasttime = mono_time;
653 rv = 1;
654 }
655 splx(s);
656
657 return (rv);
658 }
659