Optimize lwkt_rwlock.c a bit
[dragonfly.git] / sys / kern / kern_time.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/kern_time.c,v 1.68.2.1 2002/10/01 08:00:41 bde Exp $
1de703da 35 * $DragonFly: src/sys/kern/kern_time.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/buf.h>
41#include <sys/sysproto.h>
42#include <sys/resourcevar.h>
43#include <sys/signalvar.h>
44#include <sys/kernel.h>
45#include <sys/systm.h>
46#include <sys/sysent.h>
47#include <sys/proc.h>
48#include <sys/time.h>
49#include <sys/vnode.h>
50#include <vm/vm.h>
51#include <vm/vm_extern.h>
52
53struct timezone tz;
54
55/*
56 * Time of day and interval timer support.
57 *
58 * These routines provide the kernel entry points to get and set
59 * the time-of-day and per-process interval timers. Subroutines
60 * here provide support for adding and subtracting timeval structures
61 * and decrementing interval timers, optionally reloading the interval
62 * timers when they expire.
63 */
64
65static int nanosleep1 __P((struct proc *p, struct timespec *rqt,
66 struct timespec *rmt));
67static int settime __P((struct timeval *));
68static void timevalfix __P((struct timeval *));
69static void no_lease_updatetime __P((int));
70
71static void
72no_lease_updatetime(deltat)
73 int deltat;
74{
75}
76
77void (*lease_updatetime) __P((int)) = no_lease_updatetime;
78
79static int
80settime(tv)
81 struct timeval *tv;
82{
83 struct timeval delta, tv1, tv2;
84 static struct timeval maxtime, laststep;
85 struct timespec ts;
86 int s;
87
88 s = splclock();
89 microtime(&tv1);
90 delta = *tv;
91 timevalsub(&delta, &tv1);
92
93 /*
94 * If the system is secure, we do not allow the time to be
95 * set to a value earlier than 1 second less than the highest
96 * time we have yet seen. The worst a miscreant can do in
97 * this circumstance is "freeze" time. He couldn't go
98 * back to the past.
99 *
100 * We similarly do not allow the clock to be stepped more
101 * than one second, nor more than once per second. This allows
102 * a miscreant to make the clock march double-time, but no worse.
103 */
104 if (securelevel > 1) {
105 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
106 /*
107 * Update maxtime to latest time we've seen.
108 */
109 if (tv1.tv_sec > maxtime.tv_sec)
110 maxtime = tv1;
111 tv2 = *tv;
112 timevalsub(&tv2, &maxtime);
113 if (tv2.tv_sec < -1) {
114 tv->tv_sec = maxtime.tv_sec - 1;
115 printf("Time adjustment clamped to -1 second\n");
116 }
117 } else {
118 if (tv1.tv_sec == laststep.tv_sec) {
119 splx(s);
120 return (EPERM);
121 }
122 if (delta.tv_sec > 1) {
123 tv->tv_sec = tv1.tv_sec + 1;
124 printf("Time adjustment clamped to +1 second\n");
125 }
126 laststep = *tv;
127 }
128 }
129
130 ts.tv_sec = tv->tv_sec;
131 ts.tv_nsec = tv->tv_usec * 1000;
132 set_timecounter(&ts);
133 (void) splsoftclock();
134 lease_updatetime(delta.tv_sec);
135 splx(s);
136 resettodr();
137 return (0);
138}
139
140#ifndef _SYS_SYSPROTO_H_
141struct clock_gettime_args {
142 clockid_t clock_id;
143 struct timespec *tp;
144};
145#endif
146
147/* ARGSUSED */
148int
149clock_gettime(p, uap)
150 struct proc *p;
151 struct clock_gettime_args *uap;
152{
153 struct timespec ats;
154
155 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
156 return (EINVAL);
157 nanotime(&ats);
158 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
159}
160
161#ifndef _SYS_SYSPROTO_H_
162struct clock_settime_args {
163 clockid_t clock_id;
164 const struct timespec *tp;
165};
166#endif
167
168/* ARGSUSED */
169int
170clock_settime(p, uap)
171 struct proc *p;
172 struct clock_settime_args *uap;
173{
174 struct timeval atv;
175 struct timespec ats;
176 int error;
177
178 if ((error = suser(p)) != 0)
179 return (error);
180 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
181 return (EINVAL);
182 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
183 return (error);
184 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
185 return (EINVAL);
186 /* XXX Don't convert nsec->usec and back */
187 TIMESPEC_TO_TIMEVAL(&atv, &ats);
188 if ((error = settime(&atv)))
189 return (error);
190 return (0);
191}
192
193#ifndef _SYS_SYSPROTO_H_
194struct clock_getres_args {
195 clockid_t clock_id;
196 struct timespec *tp;
197};
198#endif
199
200int
201clock_getres(p, uap)
202 struct proc *p;
203 struct clock_getres_args *uap;
204{
205 struct timespec ts;
206 int error;
207
208 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
209 return (EINVAL);
210 error = 0;
211 if (SCARG(uap, tp)) {
212 ts.tv_sec = 0;
213 /*
214 * Round up the result of the division cheaply by adding 1.
215 * Rounding up is especially important if rounding down
216 * would give 0. Perfect rounding is unimportant.
217 */
218 ts.tv_nsec = 1000000000 / timecounter->tc_frequency + 1;
219 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
220 }
221 return (error);
222}
223
224static int nanowait;
225
226static int
227nanosleep1(p, rqt, rmt)
228 struct proc *p;
229 struct timespec *rqt, *rmt;
230{
231 struct timespec ts, ts2, ts3;
232 struct timeval tv;
233 int error;
234
235 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
236 return (EINVAL);
237 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
238 return (0);
239 getnanouptime(&ts);
240 timespecadd(&ts, rqt);
241 TIMESPEC_TO_TIMEVAL(&tv, rqt);
242 for (;;) {
243 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
244 tvtohz(&tv));
245 getnanouptime(&ts2);
246 if (error != EWOULDBLOCK) {
247 if (error == ERESTART)
248 error = EINTR;
249 if (rmt != NULL) {
250 timespecsub(&ts, &ts2);
251 if (ts.tv_sec < 0)
252 timespecclear(&ts);
253 *rmt = ts;
254 }
255 return (error);
256 }
257 if (timespeccmp(&ts2, &ts, >=))
258 return (0);
259 ts3 = ts;
260 timespecsub(&ts3, &ts2);
261 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
262 }
263}
264
265#ifndef _SYS_SYSPROTO_H_
266struct nanosleep_args {
267 struct timespec *rqtp;
268 struct timespec *rmtp;
269};
270#endif
271
272/* ARGSUSED */
273int
274nanosleep(p, uap)
275 struct proc *p;
276 struct nanosleep_args *uap;
277{
278 struct timespec rmt, rqt;
279 int error, error2;
280
281 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
282 if (error)
283 return (error);
284 if (SCARG(uap, rmtp))
285 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
286 VM_PROT_WRITE))
287 return (EFAULT);
288 error = nanosleep1(p, &rqt, &rmt);
289 if (error && SCARG(uap, rmtp)) {
290 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
291 if (error2) /* XXX shouldn't happen, did useracc() above */
292 return (error2);
293 }
294 return (error);
295}
296
297#ifndef _SYS_SYSPROTO_H_
298struct gettimeofday_args {
299 struct timeval *tp;
300 struct timezone *tzp;
301};
302#endif
303/* ARGSUSED */
304int
305gettimeofday(p, uap)
306 struct proc *p;
307 register struct gettimeofday_args *uap;
308{
309 struct timeval atv;
310 int error = 0;
311
312 if (uap->tp) {
313 microtime(&atv);
314 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
315 sizeof (atv))))
316 return (error);
317 }
318 if (uap->tzp)
319 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
320 sizeof (tz));
321 return (error);
322}
323
324#ifndef _SYS_SYSPROTO_H_
325struct settimeofday_args {
326 struct timeval *tv;
327 struct timezone *tzp;
328};
329#endif
330/* ARGSUSED */
331int
332settimeofday(p, uap)
333 struct proc *p;
334 struct settimeofday_args *uap;
335{
336 struct timeval atv;
337 struct timezone atz;
338 int error;
339
340 if ((error = suser(p)))
341 return (error);
342 /* Verify all parameters before changing time. */
343 if (uap->tv) {
344 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
345 sizeof(atv))))
346 return (error);
347 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
348 return (EINVAL);
349 }
350 if (uap->tzp &&
351 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
352 return (error);
353 if (uap->tv && (error = settime(&atv)))
354 return (error);
355 if (uap->tzp)
356 tz = atz;
357 return (0);
358}
359
360int tickdelta; /* current clock skew, us. per tick */
361long timedelta; /* unapplied time correction, us. */
362static long bigadj = 1000000; /* use 10x skew above bigadj us. */
363
364#ifndef _SYS_SYSPROTO_H_
365struct adjtime_args {
366 struct timeval *delta;
367 struct timeval *olddelta;
368};
369#endif
370/* ARGSUSED */
371int
372adjtime(p, uap)
373 struct proc *p;
374 register struct adjtime_args *uap;
375{
376 struct timeval atv;
377 register long ndelta, ntickdelta, odelta;
378 int s, error;
379
380 if ((error = suser(p)))
381 return (error);
382 if ((error =
383 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
384 return (error);
385
386 /*
387 * Compute the total correction and the rate at which to apply it.
388 * Round the adjustment down to a whole multiple of the per-tick
389 * delta, so that after some number of incremental changes in
390 * hardclock(), tickdelta will become zero, lest the correction
391 * overshoot and start taking us away from the desired final time.
392 */
393 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
394 if (ndelta > bigadj || ndelta < -bigadj)
395 ntickdelta = 10 * tickadj;
396 else
397 ntickdelta = tickadj;
398 if (ndelta % ntickdelta)
399 ndelta = ndelta / ntickdelta * ntickdelta;
400
401 /*
402 * To make hardclock()'s job easier, make the per-tick delta negative
403 * if we want time to run slower; then hardclock can simply compute
404 * tick + tickdelta, and subtract tickdelta from timedelta.
405 */
406 if (ndelta < 0)
407 ntickdelta = -ntickdelta;
408 s = splclock();
409 odelta = timedelta;
410 timedelta = ndelta;
411 tickdelta = ntickdelta;
412 splx(s);
413
414 if (uap->olddelta) {
415 atv.tv_sec = odelta / 1000000;
416 atv.tv_usec = odelta % 1000000;
417 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
418 sizeof(struct timeval));
419 }
420 return (0);
421}
422
423/*
424 * Get value of an interval timer. The process virtual and
425 * profiling virtual time timers are kept in the p_stats area, since
426 * they can be swapped out. These are kept internally in the
427 * way they are specified externally: in time until they expire.
428 *
429 * The real time interval timer is kept in the process table slot
430 * for the process, and its value (it_value) is kept as an
431 * absolute time rather than as a delta, so that it is easy to keep
432 * periodic real-time signals from drifting.
433 *
434 * Virtual time timers are processed in the hardclock() routine of
435 * kern_clock.c. The real time timer is processed by a timeout
436 * routine, called from the softclock() routine. Since a callout
437 * may be delayed in real time due to interrupt processing in the system,
438 * it is possible for the real time timeout routine (realitexpire, given below),
439 * to be delayed in real time past when it is supposed to occur. It
440 * does not suffice, therefore, to reload the real timer .it_value from the
441 * real time timers .it_interval. Rather, we compute the next time in
442 * absolute time the timer should go off.
443 */
444#ifndef _SYS_SYSPROTO_H_
445struct getitimer_args {
446 u_int which;
447 struct itimerval *itv;
448};
449#endif
450/* ARGSUSED */
451int
452getitimer(p, uap)
453 struct proc *p;
454 register struct getitimer_args *uap;
455{
456 struct timeval ctv;
457 struct itimerval aitv;
458 int s;
459
460 if (uap->which > ITIMER_PROF)
461 return (EINVAL);
462 s = splclock(); /* XXX still needed ? */
463 if (uap->which == ITIMER_REAL) {
464 /*
465 * Convert from absolute to relative time in .it_value
466 * part of real time timer. If time for real time timer
467 * has passed return 0, else return difference between
468 * current time and time for the timer to go off.
469 */
470 aitv = p->p_realtimer;
471 if (timevalisset(&aitv.it_value)) {
472 getmicrouptime(&ctv);
473 if (timevalcmp(&aitv.it_value, &ctv, <))
474 timevalclear(&aitv.it_value);
475 else
476 timevalsub(&aitv.it_value, &ctv);
477 }
478 } else
479 aitv = p->p_stats->p_timer[uap->which];
480 splx(s);
481 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
482 sizeof (struct itimerval)));
483}
484
485#ifndef _SYS_SYSPROTO_H_
486struct setitimer_args {
487 u_int which;
488 struct itimerval *itv, *oitv;
489};
490#endif
491/* ARGSUSED */
492int
493setitimer(p, uap)
494 struct proc *p;
495 register struct setitimer_args *uap;
496{
497 struct itimerval aitv;
498 struct timeval ctv;
499 register struct itimerval *itvp;
500 int s, error;
501
502 if (uap->which > ITIMER_PROF)
503 return (EINVAL);
504 itvp = uap->itv;
505 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
506 sizeof(struct itimerval))))
507 return (error);
508 if ((uap->itv = uap->oitv) &&
509 (error = getitimer(p, (struct getitimer_args *)uap)))
510 return (error);
511 if (itvp == 0)
512 return (0);
513 if (itimerfix(&aitv.it_value))
514 return (EINVAL);
515 if (!timevalisset(&aitv.it_value))
516 timevalclear(&aitv.it_interval);
517 else if (itimerfix(&aitv.it_interval))
518 return (EINVAL);
519 s = splclock(); /* XXX: still needed ? */
520 if (uap->which == ITIMER_REAL) {
521 if (timevalisset(&p->p_realtimer.it_value))
522 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
523 if (timevalisset(&aitv.it_value))
524 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
525 tvtohz(&aitv.it_value));
526 getmicrouptime(&ctv);
527 timevaladd(&aitv.it_value, &ctv);
528 p->p_realtimer = aitv;
529 } else
530 p->p_stats->p_timer[uap->which] = aitv;
531 splx(s);
532 return (0);
533}
534
535/*
536 * Real interval timer expired:
537 * send process whose timer expired an alarm signal.
538 * If time is not set up to reload, then just return.
539 * Else compute next time timer should go off which is > current time.
540 * This is where delay in processing this timeout causes multiple
541 * SIGALRM calls to be compressed into one.
542 * tvtohz() always adds 1 to allow for the time until the next clock
543 * interrupt being strictly less than 1 clock tick, but we don't want
544 * that here since we want to appear to be in sync with the clock
545 * interrupt even when we're delayed.
546 */
547void
548realitexpire(arg)
549 void *arg;
550{
551 register struct proc *p;
552 struct timeval ctv, ntv;
553 int s;
554
555 p = (struct proc *)arg;
556 psignal(p, SIGALRM);
557 if (!timevalisset(&p->p_realtimer.it_interval)) {
558 timevalclear(&p->p_realtimer.it_value);
559 return;
560 }
561 for (;;) {
562 s = splclock(); /* XXX: still neeeded ? */
563 timevaladd(&p->p_realtimer.it_value,
564 &p->p_realtimer.it_interval);
565 getmicrouptime(&ctv);
566 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
567 ntv = p->p_realtimer.it_value;
568 timevalsub(&ntv, &ctv);
569 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
570 tvtohz(&ntv) - 1);
571 splx(s);
572 return;
573 }
574 splx(s);
575 }
576}
577
578/*
579 * Check that a proposed value to load into the .it_value or
580 * .it_interval part of an interval timer is acceptable, and
581 * fix it to have at least minimal value (i.e. if it is less
582 * than the resolution of the clock, round it up.)
583 */
584int
585itimerfix(tv)
586 struct timeval *tv;
587{
588
589 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
590 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
591 return (EINVAL);
592 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
593 tv->tv_usec = tick;
594 return (0);
595}
596
597/*
598 * Decrement an interval timer by a specified number
599 * of microseconds, which must be less than a second,
600 * i.e. < 1000000. If the timer expires, then reload
601 * it. In this case, carry over (usec - old value) to
602 * reduce the value reloaded into the timer so that
603 * the timer does not drift. This routine assumes
604 * that it is called in a context where the timers
605 * on which it is operating cannot change in value.
606 */
607int
608itimerdecr(itp, usec)
609 register struct itimerval *itp;
610 int usec;
611{
612
613 if (itp->it_value.tv_usec < usec) {
614 if (itp->it_value.tv_sec == 0) {
615 /* expired, and already in next interval */
616 usec -= itp->it_value.tv_usec;
617 goto expire;
618 }
619 itp->it_value.tv_usec += 1000000;
620 itp->it_value.tv_sec--;
621 }
622 itp->it_value.tv_usec -= usec;
623 usec = 0;
624 if (timevalisset(&itp->it_value))
625 return (1);
626 /* expired, exactly at end of interval */
627expire:
628 if (timevalisset(&itp->it_interval)) {
629 itp->it_value = itp->it_interval;
630 itp->it_value.tv_usec -= usec;
631 if (itp->it_value.tv_usec < 0) {
632 itp->it_value.tv_usec += 1000000;
633 itp->it_value.tv_sec--;
634 }
635 } else
636 itp->it_value.tv_usec = 0; /* sec is already 0 */
637 return (0);
638}
639
640/*
641 * Add and subtract routines for timevals.
642 * N.B.: subtract routine doesn't deal with
643 * results which are before the beginning,
644 * it just gets very confused in this case.
645 * Caveat emptor.
646 */
647void
648timevaladd(t1, t2)
649 struct timeval *t1, *t2;
650{
651
652 t1->tv_sec += t2->tv_sec;
653 t1->tv_usec += t2->tv_usec;
654 timevalfix(t1);
655}
656
657void
658timevalsub(t1, t2)
659 struct timeval *t1, *t2;
660{
661
662 t1->tv_sec -= t2->tv_sec;
663 t1->tv_usec -= t2->tv_usec;
664 timevalfix(t1);
665}
666
667static void
668timevalfix(t1)
669 struct timeval *t1;
670{
671
672 if (t1->tv_usec < 0) {
673 t1->tv_sec--;
674 t1->tv_usec += 1000000;
675 }
676 if (t1->tv_usec >= 1000000) {
677 t1->tv_sec++;
678 t1->tv_usec -= 1000000;
679 }
680}