proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[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 $
41c20dac 35 * $DragonFly: src/sys/kern/kern_time.c,v 1.3 2003/06/23 17:55: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
41c20dac 65static int nanosleep1 __P((struct timespec *rqt,
984263bc
MD
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
41c20dac 149clock_gettime(struct clock_gettime_args *uap)
984263bc
MD
150{
151 struct timespec ats;
152
153 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
154 return (EINVAL);
155 nanotime(&ats);
156 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
157}
158
159#ifndef _SYS_SYSPROTO_H_
160struct clock_settime_args {
161 clockid_t clock_id;
162 const struct timespec *tp;
163};
164#endif
165
166/* ARGSUSED */
167int
41c20dac 168clock_settime(struct clock_settime_args *uap)
984263bc
MD
169{
170 struct timeval atv;
171 struct timespec ats;
172 int error;
173
41c20dac 174 if ((error = suser()) != 0)
984263bc
MD
175 return (error);
176 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
177 return (EINVAL);
178 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
179 return (error);
180 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
181 return (EINVAL);
182 /* XXX Don't convert nsec->usec and back */
183 TIMESPEC_TO_TIMEVAL(&atv, &ats);
184 if ((error = settime(&atv)))
185 return (error);
186 return (0);
187}
188
189#ifndef _SYS_SYSPROTO_H_
190struct clock_getres_args {
191 clockid_t clock_id;
192 struct timespec *tp;
193};
194#endif
195
196int
41c20dac 197clock_getres(struct clock_getres_args *uap)
984263bc
MD
198{
199 struct timespec ts;
200 int error;
201
202 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
203 return (EINVAL);
204 error = 0;
205 if (SCARG(uap, tp)) {
206 ts.tv_sec = 0;
207 /*
208 * Round up the result of the division cheaply by adding 1.
209 * Rounding up is especially important if rounding down
210 * would give 0. Perfect rounding is unimportant.
211 */
212 ts.tv_nsec = 1000000000 / timecounter->tc_frequency + 1;
213 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
214 }
215 return (error);
216}
217
218static int nanowait;
219
220static int
41c20dac 221nanosleep1(struct timespec *rqt, struct timespec *rmt)
984263bc
MD
222{
223 struct timespec ts, ts2, ts3;
224 struct timeval tv;
225 int error;
226
227 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
228 return (EINVAL);
229 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
230 return (0);
231 getnanouptime(&ts);
232 timespecadd(&ts, rqt);
233 TIMESPEC_TO_TIMEVAL(&tv, rqt);
234 for (;;) {
235 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
236 tvtohz(&tv));
237 getnanouptime(&ts2);
238 if (error != EWOULDBLOCK) {
239 if (error == ERESTART)
240 error = EINTR;
241 if (rmt != NULL) {
242 timespecsub(&ts, &ts2);
243 if (ts.tv_sec < 0)
244 timespecclear(&ts);
245 *rmt = ts;
246 }
247 return (error);
248 }
249 if (timespeccmp(&ts2, &ts, >=))
250 return (0);
251 ts3 = ts;
252 timespecsub(&ts3, &ts2);
253 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
254 }
255}
256
257#ifndef _SYS_SYSPROTO_H_
258struct nanosleep_args {
259 struct timespec *rqtp;
260 struct timespec *rmtp;
261};
262#endif
263
264/* ARGSUSED */
265int
41c20dac 266nanosleep(struct nanosleep_args *uap)
984263bc
MD
267{
268 struct timespec rmt, rqt;
269 int error, error2;
270
271 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
272 if (error)
273 return (error);
274 if (SCARG(uap, rmtp))
275 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
276 VM_PROT_WRITE))
277 return (EFAULT);
41c20dac 278 error = nanosleep1(&rqt, &rmt);
984263bc
MD
279 if (error && SCARG(uap, rmtp)) {
280 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
281 if (error2) /* XXX shouldn't happen, did useracc() above */
282 return (error2);
283 }
284 return (error);
285}
286
287#ifndef _SYS_SYSPROTO_H_
288struct gettimeofday_args {
289 struct timeval *tp;
290 struct timezone *tzp;
291};
292#endif
293/* ARGSUSED */
294int
41c20dac 295gettimeofday(struct gettimeofday_args *uap)
984263bc
MD
296{
297 struct timeval atv;
298 int error = 0;
299
300 if (uap->tp) {
301 microtime(&atv);
302 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
303 sizeof (atv))))
304 return (error);
305 }
306 if (uap->tzp)
307 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
308 sizeof (tz));
309 return (error);
310}
311
312#ifndef _SYS_SYSPROTO_H_
313struct settimeofday_args {
314 struct timeval *tv;
315 struct timezone *tzp;
316};
317#endif
318/* ARGSUSED */
319int
41c20dac 320settimeofday(struct settimeofday_args *uap)
984263bc
MD
321{
322 struct timeval atv;
323 struct timezone atz;
324 int error;
325
41c20dac 326 if ((error = suser()))
984263bc
MD
327 return (error);
328 /* Verify all parameters before changing time. */
329 if (uap->tv) {
330 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
331 sizeof(atv))))
332 return (error);
333 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
334 return (EINVAL);
335 }
336 if (uap->tzp &&
337 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
338 return (error);
339 if (uap->tv && (error = settime(&atv)))
340 return (error);
341 if (uap->tzp)
342 tz = atz;
343 return (0);
344}
345
346int tickdelta; /* current clock skew, us. per tick */
347long timedelta; /* unapplied time correction, us. */
348static long bigadj = 1000000; /* use 10x skew above bigadj us. */
349
350#ifndef _SYS_SYSPROTO_H_
351struct adjtime_args {
352 struct timeval *delta;
353 struct timeval *olddelta;
354};
355#endif
356/* ARGSUSED */
357int
41c20dac 358adjtime(struct adjtime_args *uap)
984263bc
MD
359{
360 struct timeval atv;
361 register long ndelta, ntickdelta, odelta;
362 int s, error;
363
41c20dac 364 if ((error = suser()))
984263bc
MD
365 return (error);
366 if ((error =
367 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
368 return (error);
369
370 /*
371 * Compute the total correction and the rate at which to apply it.
372 * Round the adjustment down to a whole multiple of the per-tick
373 * delta, so that after some number of incremental changes in
374 * hardclock(), tickdelta will become zero, lest the correction
375 * overshoot and start taking us away from the desired final time.
376 */
377 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
378 if (ndelta > bigadj || ndelta < -bigadj)
379 ntickdelta = 10 * tickadj;
380 else
381 ntickdelta = tickadj;
382 if (ndelta % ntickdelta)
383 ndelta = ndelta / ntickdelta * ntickdelta;
384
385 /*
386 * To make hardclock()'s job easier, make the per-tick delta negative
387 * if we want time to run slower; then hardclock can simply compute
388 * tick + tickdelta, and subtract tickdelta from timedelta.
389 */
390 if (ndelta < 0)
391 ntickdelta = -ntickdelta;
392 s = splclock();
393 odelta = timedelta;
394 timedelta = ndelta;
395 tickdelta = ntickdelta;
396 splx(s);
397
398 if (uap->olddelta) {
399 atv.tv_sec = odelta / 1000000;
400 atv.tv_usec = odelta % 1000000;
401 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
402 sizeof(struct timeval));
403 }
404 return (0);
405}
406
407/*
408 * Get value of an interval timer. The process virtual and
409 * profiling virtual time timers are kept in the p_stats area, since
410 * they can be swapped out. These are kept internally in the
411 * way they are specified externally: in time until they expire.
412 *
413 * The real time interval timer is kept in the process table slot
414 * for the process, and its value (it_value) is kept as an
415 * absolute time rather than as a delta, so that it is easy to keep
416 * periodic real-time signals from drifting.
417 *
418 * Virtual time timers are processed in the hardclock() routine of
419 * kern_clock.c. The real time timer is processed by a timeout
420 * routine, called from the softclock() routine. Since a callout
421 * may be delayed in real time due to interrupt processing in the system,
422 * it is possible for the real time timeout routine (realitexpire, given below),
423 * to be delayed in real time past when it is supposed to occur. It
424 * does not suffice, therefore, to reload the real timer .it_value from the
425 * real time timers .it_interval. Rather, we compute the next time in
426 * absolute time the timer should go off.
427 */
428#ifndef _SYS_SYSPROTO_H_
429struct getitimer_args {
430 u_int which;
431 struct itimerval *itv;
432};
433#endif
434/* ARGSUSED */
435int
41c20dac 436getitimer(struct getitimer_args *uap)
984263bc 437{
41c20dac 438 struct proc *p = curproc;
984263bc
MD
439 struct timeval ctv;
440 struct itimerval aitv;
441 int s;
442
443 if (uap->which > ITIMER_PROF)
444 return (EINVAL);
445 s = splclock(); /* XXX still needed ? */
446 if (uap->which == ITIMER_REAL) {
447 /*
448 * Convert from absolute to relative time in .it_value
449 * part of real time timer. If time for real time timer
450 * has passed return 0, else return difference between
451 * current time and time for the timer to go off.
452 */
453 aitv = p->p_realtimer;
454 if (timevalisset(&aitv.it_value)) {
455 getmicrouptime(&ctv);
456 if (timevalcmp(&aitv.it_value, &ctv, <))
457 timevalclear(&aitv.it_value);
458 else
459 timevalsub(&aitv.it_value, &ctv);
460 }
461 } else
462 aitv = p->p_stats->p_timer[uap->which];
463 splx(s);
464 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
465 sizeof (struct itimerval)));
466}
467
468#ifndef _SYS_SYSPROTO_H_
469struct setitimer_args {
470 u_int which;
471 struct itimerval *itv, *oitv;
472};
473#endif
474/* ARGSUSED */
475int
41c20dac 476setitimer(struct setitimer_args *uap)
984263bc
MD
477{
478 struct itimerval aitv;
479 struct timeval ctv;
41c20dac
MD
480 struct itimerval *itvp;
481 struct proc *p = curproc;
984263bc
MD
482 int s, error;
483
484 if (uap->which > ITIMER_PROF)
485 return (EINVAL);
486 itvp = uap->itv;
487 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
488 sizeof(struct itimerval))))
489 return (error);
490 if ((uap->itv = uap->oitv) &&
41c20dac 491 (error = getitimer((struct getitimer_args *)uap)))
984263bc
MD
492 return (error);
493 if (itvp == 0)
494 return (0);
495 if (itimerfix(&aitv.it_value))
496 return (EINVAL);
497 if (!timevalisset(&aitv.it_value))
498 timevalclear(&aitv.it_interval);
499 else if (itimerfix(&aitv.it_interval))
500 return (EINVAL);
501 s = splclock(); /* XXX: still needed ? */
502 if (uap->which == ITIMER_REAL) {
503 if (timevalisset(&p->p_realtimer.it_value))
504 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
505 if (timevalisset(&aitv.it_value))
506 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
507 tvtohz(&aitv.it_value));
508 getmicrouptime(&ctv);
509 timevaladd(&aitv.it_value, &ctv);
510 p->p_realtimer = aitv;
511 } else
512 p->p_stats->p_timer[uap->which] = aitv;
513 splx(s);
514 return (0);
515}
516
517/*
518 * Real interval timer expired:
519 * send process whose timer expired an alarm signal.
520 * If time is not set up to reload, then just return.
521 * Else compute next time timer should go off which is > current time.
522 * This is where delay in processing this timeout causes multiple
523 * SIGALRM calls to be compressed into one.
524 * tvtohz() always adds 1 to allow for the time until the next clock
525 * interrupt being strictly less than 1 clock tick, but we don't want
526 * that here since we want to appear to be in sync with the clock
527 * interrupt even when we're delayed.
528 */
529void
530realitexpire(arg)
531 void *arg;
532{
533 register struct proc *p;
534 struct timeval ctv, ntv;
535 int s;
536
537 p = (struct proc *)arg;
538 psignal(p, SIGALRM);
539 if (!timevalisset(&p->p_realtimer.it_interval)) {
540 timevalclear(&p->p_realtimer.it_value);
541 return;
542 }
543 for (;;) {
544 s = splclock(); /* XXX: still neeeded ? */
545 timevaladd(&p->p_realtimer.it_value,
546 &p->p_realtimer.it_interval);
547 getmicrouptime(&ctv);
548 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
549 ntv = p->p_realtimer.it_value;
550 timevalsub(&ntv, &ctv);
551 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
552 tvtohz(&ntv) - 1);
553 splx(s);
554 return;
555 }
556 splx(s);
557 }
558}
559
560/*
561 * Check that a proposed value to load into the .it_value or
562 * .it_interval part of an interval timer is acceptable, and
563 * fix it to have at least minimal value (i.e. if it is less
564 * than the resolution of the clock, round it up.)
565 */
566int
567itimerfix(tv)
568 struct timeval *tv;
569{
570
571 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
572 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
573 return (EINVAL);
574 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
575 tv->tv_usec = tick;
576 return (0);
577}
578
579/*
580 * Decrement an interval timer by a specified number
581 * of microseconds, which must be less than a second,
582 * i.e. < 1000000. If the timer expires, then reload
583 * it. In this case, carry over (usec - old value) to
584 * reduce the value reloaded into the timer so that
585 * the timer does not drift. This routine assumes
586 * that it is called in a context where the timers
587 * on which it is operating cannot change in value.
588 */
589int
590itimerdecr(itp, usec)
591 register struct itimerval *itp;
592 int usec;
593{
594
595 if (itp->it_value.tv_usec < usec) {
596 if (itp->it_value.tv_sec == 0) {
597 /* expired, and already in next interval */
598 usec -= itp->it_value.tv_usec;
599 goto expire;
600 }
601 itp->it_value.tv_usec += 1000000;
602 itp->it_value.tv_sec--;
603 }
604 itp->it_value.tv_usec -= usec;
605 usec = 0;
606 if (timevalisset(&itp->it_value))
607 return (1);
608 /* expired, exactly at end of interval */
609expire:
610 if (timevalisset(&itp->it_interval)) {
611 itp->it_value = itp->it_interval;
612 itp->it_value.tv_usec -= usec;
613 if (itp->it_value.tv_usec < 0) {
614 itp->it_value.tv_usec += 1000000;
615 itp->it_value.tv_sec--;
616 }
617 } else
618 itp->it_value.tv_usec = 0; /* sec is already 0 */
619 return (0);
620}
621
622/*
623 * Add and subtract routines for timevals.
624 * N.B.: subtract routine doesn't deal with
625 * results which are before the beginning,
626 * it just gets very confused in this case.
627 * Caveat emptor.
628 */
629void
630timevaladd(t1, t2)
631 struct timeval *t1, *t2;
632{
633
634 t1->tv_sec += t2->tv_sec;
635 t1->tv_usec += t2->tv_usec;
636 timevalfix(t1);
637}
638
639void
640timevalsub(t1, t2)
641 struct timeval *t1, *t2;
642{
643
644 t1->tv_sec -= t2->tv_sec;
645 t1->tv_usec -= t2->tv_usec;
646 timevalfix(t1);
647}
648
649static void
650timevalfix(t1)
651 struct timeval *t1;
652{
653
654 if (t1->tv_usec < 0) {
655 t1->tv_sec--;
656 t1->tv_usec += 1000000;
657 }
658 if (t1->tv_usec >= 1000000) {
659 t1->tv_sec++;
660 t1->tv_usec -= 1000000;
661 }
662}