Always use SYSCTL_OUT, sysctl doesn't print the value otherwise.
[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 $
cebaad99 35 * $DragonFly: src/sys/kern/kern_time.c,v 1.26 2005/04/23 19:59:05 joerg 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>
df2244e3 47#include <sys/sysunion.h>
984263bc
MD
48#include <sys/proc.h>
49#include <sys/time.h>
50#include <sys/vnode.h>
a94976ad 51#include <sys/sysctl.h>
984263bc
MD
52#include <vm/vm.h>
53#include <vm/vm_extern.h>
245e4f17 54#include <sys/msgport2.h>
88c4d2f6 55#include <sys/thread2.h>
984263bc
MD
56
57struct timezone tz;
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
402ed7e1
RG
69static int nanosleep1 (struct timespec *rqt,
70 struct timespec *rmt);
71static int settime (struct timeval *);
72static void timevalfix (struct timeval *);
73static void no_lease_updatetime (int);
984263bc 74
88c4d2f6
MD
75static int sleep_hard_us = 100;
76SYSCTL_INT(_kern, OID_AUTO, sleep_hard_us, CTLFLAG_RW, &sleep_hard_us, 0, "")
a94976ad 77
984263bc
MD
78static void
79no_lease_updatetime(deltat)
80 int deltat;
81{
82}
83
402ed7e1 84void (*lease_updatetime) (int) = no_lease_updatetime;
984263bc
MD
85
86static int
87settime(tv)
88 struct timeval *tv;
89{
90 struct timeval delta, tv1, tv2;
91 static struct timeval maxtime, laststep;
92 struct timespec ts;
33924148
JS
93 int origcpu;
94
157202af 95 if ((origcpu = mycpu->gd_cpuid) != 0)
33924148 96 lwkt_setcpu_self(globaldata_find(0));
984263bc 97
88c4d2f6 98 crit_enter();
984263bc
MD
99 microtime(&tv1);
100 delta = *tv;
101 timevalsub(&delta, &tv1);
102
103 /*
104 * If the system is secure, we do not allow the time to be
105 * set to a value earlier than 1 second less than the highest
106 * time we have yet seen. The worst a miscreant can do in
107 * this circumstance is "freeze" time. He couldn't go
108 * back to the past.
109 *
110 * We similarly do not allow the clock to be stepped more
111 * than one second, nor more than once per second. This allows
112 * a miscreant to make the clock march double-time, but no worse.
113 */
114 if (securelevel > 1) {
115 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
116 /*
117 * Update maxtime to latest time we've seen.
118 */
119 if (tv1.tv_sec > maxtime.tv_sec)
120 maxtime = tv1;
121 tv2 = *tv;
122 timevalsub(&tv2, &maxtime);
123 if (tv2.tv_sec < -1) {
124 tv->tv_sec = maxtime.tv_sec - 1;
125 printf("Time adjustment clamped to -1 second\n");
126 }
127 } else {
128 if (tv1.tv_sec == laststep.tv_sec) {
88c4d2f6 129 crit_exit();
984263bc
MD
130 return (EPERM);
131 }
132 if (delta.tv_sec > 1) {
133 tv->tv_sec = tv1.tv_sec + 1;
134 printf("Time adjustment clamped to +1 second\n");
135 }
136 laststep = *tv;
137 }
138 }
139
140 ts.tv_sec = tv->tv_sec;
141 ts.tv_nsec = tv->tv_usec * 1000;
88c4d2f6 142 set_timeofday(&ts);
984263bc 143 lease_updatetime(delta.tv_sec);
88c4d2f6 144 crit_exit();
33924148 145
157202af 146 if (origcpu != 0)
33924148 147 lwkt_setcpu_self(globaldata_find(origcpu));
33924148 148
984263bc
MD
149 resettodr();
150 return (0);
151}
152
984263bc
MD
153/* ARGSUSED */
154int
41c20dac 155clock_gettime(struct clock_gettime_args *uap)
984263bc
MD
156{
157 struct timespec ats;
158
26be1876
MD
159 switch(uap->clock_id) {
160 case CLOCK_REALTIME:
161 nanotime(&ats);
162 return (copyout(&ats, uap->tp, sizeof(ats)));
163 case CLOCK_MONOTONIC:
164 nanouptime(&ats);
165 return (copyout(&ats, uap->tp, sizeof(ats)));
166 default:
984263bc 167 return (EINVAL);
26be1876 168 }
984263bc
MD
169}
170
984263bc
MD
171/* ARGSUSED */
172int
41c20dac 173clock_settime(struct clock_settime_args *uap)
984263bc 174{
dadab5e9 175 struct thread *td = curthread;
984263bc
MD
176 struct timeval atv;
177 struct timespec ats;
178 int error;
179
dadab5e9 180 if ((error = suser(td)) != 0)
984263bc 181 return (error);
26be1876
MD
182 switch(uap->clock_id) {
183 case CLOCK_REALTIME:
184 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
185 return (error);
186 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
187 return (EINVAL);
188 /* XXX Don't convert nsec->usec and back */
189 TIMESPEC_TO_TIMEVAL(&atv, &ats);
190 error = settime(&atv);
984263bc 191 return (error);
26be1876 192 default:
984263bc 193 return (EINVAL);
26be1876 194 }
984263bc
MD
195}
196
984263bc 197int
41c20dac 198clock_getres(struct clock_getres_args *uap)
984263bc
MD
199{
200 struct timespec ts;
984263bc 201
26be1876
MD
202 switch(uap->clock_id) {
203 case CLOCK_REALTIME:
204 case CLOCK_MONOTONIC:
984263bc 205 /*
26be1876
MD
206 * Round up the result of the division cheaply
207 * by adding 1. Rounding up is especially important
208 * if rounding down would give 0. Perfect rounding
209 * is unimportant.
984263bc 210 */
26be1876 211 ts.tv_sec = 0;
88c4d2f6 212 ts.tv_nsec = 1000000000 / cputimer_freq + 1;
26be1876
MD
213 return(copyout(&ts, uap->tp, sizeof(ts)));
214 default:
215 return(EINVAL);
984263bc 216 }
984263bc
MD
217}
218
88c4d2f6
MD
219/*
220 * nanosleep1()
221 *
222 * This is a general helper function for nanosleep() (aka sleep() aka
223 * usleep()).
224 *
225 * If there is less then one tick's worth of time left and
226 * we haven't done a yield, or the remaining microseconds is
227 * ridiculously low, do a yield. This avoids having
228 * to deal with systimer overheads when the system is under
229 * heavy loads. If we have done a yield already then use
230 * a systimer and an uninterruptable thread wait.
231 *
232 * If there is more then a tick's worth of time left,
233 * calculate the baseline ticks and use an interruptable
234 * tsleep, then handle the fine-grained delay on the next
235 * loop. This usually results in two sleeps occuring, a long one
236 * and a short one.
237 */
238static void
239ns1_systimer(systimer_t info)
240{
241 lwkt_schedule(info->data);
242}
984263bc
MD
243
244static int
41c20dac 245nanosleep1(struct timespec *rqt, struct timespec *rmt)
984263bc 246{
88c4d2f6 247 static int nanowait;
984263bc
MD
248 struct timespec ts, ts2, ts3;
249 struct timeval tv;
250 int error;
88c4d2f6 251 int tried_yield;
984263bc
MD
252
253 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
254 return (EINVAL);
255 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
256 return (0);
a94976ad
MD
257 nanouptime(&ts);
258 timespecadd(&ts, rqt); /* ts = target timestamp compare */
259 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
88c4d2f6
MD
260 tried_yield = 0;
261
984263bc 262 for (;;) {
88c4d2f6
MD
263 int ticks;
264 struct systimer info;
265
266 ticks = tv.tv_usec / tick; /* approximate */
a94976ad 267
88c4d2f6 268 if (tv.tv_sec == 0 && ticks == 0) {
37af14fe 269 thread_t td = curthread;
88c4d2f6
MD
270 if (tried_yield || tv.tv_usec < sleep_hard_us) {
271 tried_yield = 0;
a94976ad 272 uio_yield();
a94976ad 273 } else {
37af14fe 274 crit_enter_quick(td);
88c4d2f6 275 systimer_init_oneshot(&info, ns1_systimer,
37af14fe
MD
276 td, tv.tv_usec);
277 lwkt_deschedule_self(td);
278 crit_exit_quick(td);
88c4d2f6
MD
279 lwkt_switch();
280 systimer_del(&info); /* make sure it's gone */
a94976ad 281 }
37af14fe 282 error = iscaught(td->td_proc);
88c4d2f6
MD
283 } else if (tv.tv_sec == 0) {
284 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
a94976ad 285 } else {
88c4d2f6
MD
286 ticks = tvtohz_low(&tv); /* also handles overflow */
287 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
a94976ad
MD
288 }
289 nanouptime(&ts2);
88c4d2f6 290 if (error && error != EWOULDBLOCK) {
984263bc
MD
291 if (error == ERESTART)
292 error = EINTR;
293 if (rmt != NULL) {
294 timespecsub(&ts, &ts2);
295 if (ts.tv_sec < 0)
296 timespecclear(&ts);
297 *rmt = ts;
298 }
299 return (error);
300 }
301 if (timespeccmp(&ts2, &ts, >=))
302 return (0);
303 ts3 = ts;
304 timespecsub(&ts3, &ts2);
305 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
306 }
307}
308
245e4f17 309static void nanosleep_done(void *arg);
df2244e3 310static void nanosleep_copyout(union sysunion *sysun);
245e4f17 311
984263bc
MD
312/* ARGSUSED */
313int
41c20dac 314nanosleep(struct nanosleep_args *uap)
984263bc 315{
245e4f17 316 int error;
df2244e3 317 struct sysmsg_sleep *smsleep = &uap->sysmsg.sm.sleep;
984263bc 318
df2244e3 319 error = copyin(uap->rqtp, &smsleep->rqt, sizeof(smsleep->rqt));
984263bc
MD
320 if (error)
321 return (error);
245e4f17
MD
322 /*
323 * YYY clean this up to always use the callout, note that an abort
324 * implementation should record the residual in the async case.
325 */
df2244e3 326 if (uap->sysmsg.lmsg.ms_flags & MSGF_ASYNC) {
245e4f17
MD
327 quad_t ticks;
328
df2244e3
MD
329 ticks = (quad_t)smsleep->rqt.tv_nsec * hz / 1000000000LL;
330 if (smsleep->rqt.tv_sec)
331 ticks += (quad_t)smsleep->rqt.tv_sec * hz;
245e4f17
MD
332 if (ticks <= 0) {
333 if (ticks == 0)
334 error = 0;
335 else
336 error = EINVAL;
337 } else {
df2244e3 338 uap->sysmsg.copyout = nanosleep_copyout;
1fa2b4b4 339 uap->sysmsg.lmsg.ms_flags &= ~MSGF_DONE;
df2244e3
MD
340 callout_init(&smsleep->timer);
341 callout_reset(&smsleep->timer, ticks, nanosleep_done, uap);
245e4f17
MD
342 error = EASYNC;
343 }
344 } else {
345 /*
346 * Old synchronous sleep code, copyout the residual if
347 * nanosleep was interrupted.
348 */
df2244e3 349 error = nanosleep1(&smsleep->rqt, &smsleep->rmt);
ab2eb4eb
DR
350 if (error && uap->rmtp)
351 error = copyout(&smsleep->rmt, uap->rmtp, sizeof(smsleep->rmt));
984263bc
MD
352 }
353 return (error);
354}
355
245e4f17
MD
356/*
357 * Asynch completion for the nanosleep() syscall. This function may be
358 * called from any context and cannot legally access the originating
359 * thread, proc, or its user space.
360 *
361 * YYY change the callout interface API so we can simply assign the replymsg
362 * function to it directly.
363 */
364static void
365nanosleep_done(void *arg)
366{
367 struct nanosleep_args *uap = arg;
1fa2b4b4 368 lwkt_msg_t msg = &uap->sysmsg.lmsg;
245e4f17 369
1fa2b4b4 370 lwkt_replymsg(msg, 0);
245e4f17
MD
371}
372
373/*
374 * Asynch return for the nanosleep() syscall, called in the context of the
375 * originating thread when it pulls the message off the reply port. This
376 * function is responsible for any copyouts to userland. Kernel threads
377 * which do their own internal system calls will not usually call the return
378 * function.
379 */
380static void
df2244e3 381nanosleep_copyout(union sysunion *sysun)
245e4f17 382{
df2244e3
MD
383 struct nanosleep_args *uap = &sysun->nanosleep;
384 struct sysmsg_sleep *smsleep = &uap->sysmsg.sm.sleep;
245e4f17 385
df2244e3
MD
386 if (sysun->lmsg.ms_error && uap->rmtp) {
387 sysun->lmsg.ms_error =
388 copyout(&smsleep->rmt, uap->rmtp, sizeof(smsleep->rmt));
245e4f17
MD
389 }
390}
391
984263bc
MD
392/* ARGSUSED */
393int
41c20dac 394gettimeofday(struct gettimeofday_args *uap)
984263bc
MD
395{
396 struct timeval atv;
397 int error = 0;
398
399 if (uap->tp) {
400 microtime(&atv);
401 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
402 sizeof (atv))))
403 return (error);
404 }
405 if (uap->tzp)
406 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
407 sizeof (tz));
408 return (error);
409}
410
984263bc
MD
411/* ARGSUSED */
412int
41c20dac 413settimeofday(struct settimeofday_args *uap)
984263bc 414{
dadab5e9 415 struct thread *td = curthread;
984263bc
MD
416 struct timeval atv;
417 struct timezone atz;
418 int error;
419
dadab5e9 420 if ((error = suser(td)))
984263bc
MD
421 return (error);
422 /* Verify all parameters before changing time. */
423 if (uap->tv) {
424 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
425 sizeof(atv))))
426 return (error);
427 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
428 return (EINVAL);
429 }
430 if (uap->tzp &&
431 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
432 return (error);
433 if (uap->tv && (error = settime(&atv)))
434 return (error);
435 if (uap->tzp)
436 tz = atz;
437 return (0);
438}
439
4026c000
JS
440static void
441kern_adjtime_common(void)
442{
443 if ((ntp_delta >= 0 && ntp_delta < ntp_default_tick_delta) ||
444 (ntp_delta < 0 && ntp_delta > ntp_default_tick_delta))
445 ntp_tick_delta = ntp_delta;
446 else if (ntp_delta > ntp_big_delta)
447 ntp_tick_delta = 10 * ntp_default_tick_delta;
448 else if (ntp_delta < -ntp_big_delta)
449 ntp_tick_delta = -10 * ntp_default_tick_delta;
450 else if (ntp_delta > 0)
451 ntp_tick_delta = ntp_default_tick_delta;
452 else
453 ntp_tick_delta = -ntp_default_tick_delta;
454}
455
456void
457kern_adjtime(int64_t delta, int64_t *odelta)
458{
459 int origcpu;
460
157202af 461 if ((origcpu = mycpu->gd_cpuid) != 0)
4026c000 462 lwkt_setcpu_self(globaldata_find(0));
4026c000
JS
463
464 crit_enter();
465 *odelta = ntp_delta;
466 ntp_delta += delta;
467 kern_adjtime_common();
468 crit_exit();
469
157202af 470 if (origcpu != 0)
4026c000 471 lwkt_setcpu_self(globaldata_find(origcpu));
4026c000
JS
472}
473
b6da4cbb
JS
474static void
475kern_get_ntp_delta(int64_t *delta)
476{
477 int origcpu;
478
479 if ((origcpu = mycpu->gd_cpuid) != 0)
480 lwkt_setcpu_self(globaldata_find(0));
481
482 crit_enter();
483 *delta = ntp_delta;
484 crit_exit();
485
486 if (origcpu != 0)
487 lwkt_setcpu_self(globaldata_find(origcpu));
488}
489
4026c000
JS
490void
491kern_reladjtime(int64_t delta)
492{
493 int origcpu;
494
157202af 495 if ((origcpu = mycpu->gd_cpuid) != 0)
4026c000 496 lwkt_setcpu_self(globaldata_find(0));
4026c000
JS
497
498 crit_enter();
499 ntp_delta += delta;
500 kern_adjtime_common();
501 crit_exit();
502
157202af 503 if (origcpu != 0)
4026c000 504 lwkt_setcpu_self(globaldata_find(origcpu));
4026c000 505}
984263bc 506
0143455b
JS
507static void
508kern_adjfreq(int64_t rate)
509{
510 int origcpu;
511
157202af 512 if ((origcpu = mycpu->gd_cpuid) != 0)
0143455b 513 lwkt_setcpu_self(globaldata_find(0));
0143455b
JS
514
515 crit_enter();
516 ntp_tick_permanent = rate;
517 crit_exit();
518
157202af 519 if (origcpu != 0)
0143455b 520 lwkt_setcpu_self(globaldata_find(origcpu));
0143455b
JS
521}
522
984263bc
MD
523/* ARGSUSED */
524int
41c20dac 525adjtime(struct adjtime_args *uap)
984263bc 526{
dadab5e9 527 struct thread *td = curthread;
984263bc 528 struct timeval atv;
4026c000 529 int64_t ndelta, odelta;
88c4d2f6 530 int error;
984263bc 531
dadab5e9 532 if ((error = suser(td)))
984263bc
MD
533 return (error);
534 if ((error =
535 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
536 return (error);
537
538 /*
539 * Compute the total correction and the rate at which to apply it.
540 * Round the adjustment down to a whole multiple of the per-tick
541 * delta, so that after some number of incremental changes in
542 * hardclock(), tickdelta will become zero, lest the correction
543 * overshoot and start taking us away from the desired final time.
544 */
4026c000
JS
545 ndelta = atv.tv_sec * 1000000000 + atv.tv_usec * 1000;
546 kern_adjtime(ndelta, &odelta);
984263bc
MD
547
548 if (uap->olddelta) {
4026c000
JS
549 atv.tv_sec = odelta / 1000000000;
550 atv.tv_usec = odelta % 1000000 / 1000;
984263bc
MD
551 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
552 sizeof(struct timeval));
553 }
554 return (0);
555}
556
4026c000
JS
557static int
558sysctl_adjtime(SYSCTL_HANDLER_ARGS)
559{
560 int64_t delta;
561 int error;
562
cebaad99
JS
563 delta = 0;
564 error = SYSCTL_OUT(req, &delta, sizeof(delta));
565 if (error)
566 return (error);
4026c000
JS
567 if (req->newptr != NULL) {
568 if (suser(curthread))
569 return (EPERM);
570 error = SYSCTL_IN(req, &delta, sizeof(delta));
571 if (error)
572 return (error);
573 kern_reladjtime(delta);
574 }
575 return (0);
576}
577
b6da4cbb
JS
578static int
579sysctl_delta(SYSCTL_HANDLER_ARGS)
580{
581 int64_t delta, old_delta;
582 int error;
583
584 if (req->newptr != NULL) {
585 if (suser(curthread))
586 return (EPERM);
587 error = SYSCTL_IN(req, &delta, sizeof(delta));
588 if (error)
589 return (error);
590 kern_adjtime(delta, &old_delta);
591 /* Fall through for writing old_delta */
592 } else if (req->oldptr != NULL) {
593 kern_get_ntp_delta(&old_delta);
594 }
595
cebaad99
JS
596 error = SYSCTL_OUT(req, &old_delta, sizeof(old_delta));
597 if (error)
598 return (error);
b6da4cbb
JS
599
600 return (0);
601}
602
0143455b
JS
603static int
604sysctl_adjfreq(SYSCTL_HANDLER_ARGS)
605{
606 int64_t freqdelta;
607 int error;
608
cebaad99
JS
609 freqdelta = ntp_tick_permanent * hz;
610 error = SYSCTL_OUT(req, &freqdelta, sizeof(freqdelta));
611 if (error)
612 return (error);
0143455b
JS
613 if (req->newptr != NULL) {
614 if (suser(curthread))
615 return (EPERM);
616 error = SYSCTL_IN(req, &freqdelta, sizeof(freqdelta));
617 if (error)
618 return (error);
619
620 freqdelta /= hz;
621 kern_adjfreq(freqdelta);
622 }
623 return (0);
624}
625
4026c000 626SYSCTL_NODE(_kern, OID_AUTO, ntp, CTLFLAG_RW, 0, "NTP related controls");
cebaad99
JS
627SYSCTL_PROC(_kern_ntp, OID_AUTO, permanent, CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
628 sysctl_adjfreq, "Q", "permanent correction per second");
b6da4cbb 629SYSCTL_PROC(_kern_ntp, OID_AUTO, delta,
cebaad99
JS
630 CTLTYPE_QUAD | CTLFLAG_RW, 0, 0,
631 sysctl_delta, "Q", "one-time delta");
632SYSCTL_QUAD(_kern_ntp, OID_AUTO, big_delta, CTLFLAG_RD,
633 &ntp_big_delta, 0, "threshold for fast adjustment");
634SYSCTL_QUAD(_kern_ntp, OID_AUTO, tick_delta, CTLFLAG_RD,
635 &ntp_tick_delta, 0, "per-tick adjustment");
636SYSCTL_QUAD(_kern_ntp, OID_AUTO, default_tick_delta, CTLFLAG_RD,
637 &ntp_default_tick_delta, 0, "default per-tick adjustment");
48590578 638SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, next_leap_second, CTLFLAG_RW,
cebaad99 639 &ntp_leap_second, sizeof(time_t), "T,time_t", "next leap second");
48590578
JS
640SYSCTL_INT(_kern_ntp, OID_AUTO, insert_leap_second, CTLFLAG_RW,
641 &ntp_leap_insert, 0, "insert or remove leap second");
4026c000 642SYSCTL_PROC(_kern_ntp, OID_AUTO, adjust,
cebaad99
JS
643 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
644 sysctl_adjtime, "Q", "relative adjust for delta");
4026c000 645
984263bc
MD
646/*
647 * Get value of an interval timer. The process virtual and
648 * profiling virtual time timers are kept in the p_stats area, since
649 * they can be swapped out. These are kept internally in the
650 * way they are specified externally: in time until they expire.
651 *
652 * The real time interval timer is kept in the process table slot
653 * for the process, and its value (it_value) is kept as an
654 * absolute time rather than as a delta, so that it is easy to keep
655 * periodic real-time signals from drifting.
656 *
657 * Virtual time timers are processed in the hardclock() routine of
658 * kern_clock.c. The real time timer is processed by a timeout
659 * routine, called from the softclock() routine. Since a callout
660 * may be delayed in real time due to interrupt processing in the system,
661 * it is possible for the real time timeout routine (realitexpire, given below),
662 * to be delayed in real time past when it is supposed to occur. It
663 * does not suffice, therefore, to reload the real timer .it_value from the
664 * real time timers .it_interval. Rather, we compute the next time in
665 * absolute time the timer should go off.
666 */
984263bc
MD
667/* ARGSUSED */
668int
41c20dac 669getitimer(struct getitimer_args *uap)
984263bc 670{
41c20dac 671 struct proc *p = curproc;
984263bc
MD
672 struct timeval ctv;
673 struct itimerval aitv;
984263bc
MD
674
675 if (uap->which > ITIMER_PROF)
676 return (EINVAL);
88c4d2f6 677 crit_enter();
984263bc
MD
678 if (uap->which == ITIMER_REAL) {
679 /*
680 * Convert from absolute to relative time in .it_value
681 * part of real time timer. If time for real time timer
682 * has passed return 0, else return difference between
683 * current time and time for the timer to go off.
684 */
685 aitv = p->p_realtimer;
686 if (timevalisset(&aitv.it_value)) {
687 getmicrouptime(&ctv);
688 if (timevalcmp(&aitv.it_value, &ctv, <))
689 timevalclear(&aitv.it_value);
690 else
691 timevalsub(&aitv.it_value, &ctv);
692 }
88c4d2f6 693 } else {
984263bc 694 aitv = p->p_stats->p_timer[uap->which];
88c4d2f6
MD
695 }
696 crit_exit();
984263bc
MD
697 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
698 sizeof (struct itimerval)));
699}
700
984263bc
MD
701/* ARGSUSED */
702int
41c20dac 703setitimer(struct setitimer_args *uap)
984263bc
MD
704{
705 struct itimerval aitv;
706 struct timeval ctv;
41c20dac
MD
707 struct itimerval *itvp;
708 struct proc *p = curproc;
88c4d2f6 709 int error;
984263bc
MD
710
711 if (uap->which > ITIMER_PROF)
712 return (EINVAL);
713 itvp = uap->itv;
714 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
715 sizeof(struct itimerval))))
716 return (error);
717 if ((uap->itv = uap->oitv) &&
41c20dac 718 (error = getitimer((struct getitimer_args *)uap)))
984263bc
MD
719 return (error);
720 if (itvp == 0)
721 return (0);
722 if (itimerfix(&aitv.it_value))
723 return (EINVAL);
724 if (!timevalisset(&aitv.it_value))
725 timevalclear(&aitv.it_interval);
726 else if (itimerfix(&aitv.it_interval))
727 return (EINVAL);
88c4d2f6 728 crit_enter();
984263bc
MD
729 if (uap->which == ITIMER_REAL) {
730 if (timevalisset(&p->p_realtimer.it_value))
8fbf9130 731 callout_stop(&p->p_ithandle);
984263bc 732 if (timevalisset(&aitv.it_value))
8fbf9130
JS
733 callout_reset(&p->p_ithandle,
734 tvtohz_high(&aitv.it_value), realitexpire, p);
984263bc
MD
735 getmicrouptime(&ctv);
736 timevaladd(&aitv.it_value, &ctv);
737 p->p_realtimer = aitv;
88c4d2f6 738 } else {
984263bc 739 p->p_stats->p_timer[uap->which] = aitv;
88c4d2f6
MD
740 }
741 crit_exit();
984263bc
MD
742 return (0);
743}
744
745/*
746 * Real interval timer expired:
747 * send process whose timer expired an alarm signal.
748 * If time is not set up to reload, then just return.
749 * Else compute next time timer should go off which is > current time.
750 * This is where delay in processing this timeout causes multiple
751 * SIGALRM calls to be compressed into one.
a94976ad 752 * tvtohz_high() always adds 1 to allow for the time until the next clock
984263bc
MD
753 * interrupt being strictly less than 1 clock tick, but we don't want
754 * that here since we want to appear to be in sync with the clock
755 * interrupt even when we're delayed.
756 */
757void
758realitexpire(arg)
759 void *arg;
760{
1fd87d54 761 struct proc *p;
984263bc 762 struct timeval ctv, ntv;
984263bc
MD
763
764 p = (struct proc *)arg;
765 psignal(p, SIGALRM);
766 if (!timevalisset(&p->p_realtimer.it_interval)) {
767 timevalclear(&p->p_realtimer.it_value);
768 return;
769 }
770 for (;;) {
88c4d2f6 771 crit_enter();
984263bc
MD
772 timevaladd(&p->p_realtimer.it_value,
773 &p->p_realtimer.it_interval);
774 getmicrouptime(&ctv);
775 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
776 ntv = p->p_realtimer.it_value;
777 timevalsub(&ntv, &ctv);
8fbf9130
JS
778 callout_reset(&p->p_ithandle, tvtohz_low(&ntv),
779 realitexpire, p);
88c4d2f6 780 crit_exit();
984263bc
MD
781 return;
782 }
88c4d2f6 783 crit_exit();
984263bc
MD
784 }
785}
786
787/*
788 * Check that a proposed value to load into the .it_value or
789 * .it_interval part of an interval timer is acceptable, and
790 * fix it to have at least minimal value (i.e. if it is less
791 * than the resolution of the clock, round it up.)
792 */
793int
794itimerfix(tv)
795 struct timeval *tv;
796{
797
798 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
799 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
800 return (EINVAL);
801 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
802 tv->tv_usec = tick;
803 return (0);
804}
805
806/*
807 * Decrement an interval timer by a specified number
808 * of microseconds, which must be less than a second,
809 * i.e. < 1000000. If the timer expires, then reload
810 * it. In this case, carry over (usec - old value) to
811 * reduce the value reloaded into the timer so that
812 * the timer does not drift. This routine assumes
813 * that it is called in a context where the timers
814 * on which it is operating cannot change in value.
815 */
816int
817itimerdecr(itp, usec)
1fd87d54 818 struct itimerval *itp;
984263bc
MD
819 int usec;
820{
821
822 if (itp->it_value.tv_usec < usec) {
823 if (itp->it_value.tv_sec == 0) {
824 /* expired, and already in next interval */
825 usec -= itp->it_value.tv_usec;
826 goto expire;
827 }
828 itp->it_value.tv_usec += 1000000;
829 itp->it_value.tv_sec--;
830 }
831 itp->it_value.tv_usec -= usec;
832 usec = 0;
833 if (timevalisset(&itp->it_value))
834 return (1);
835 /* expired, exactly at end of interval */
836expire:
837 if (timevalisset(&itp->it_interval)) {
838 itp->it_value = itp->it_interval;
839 itp->it_value.tv_usec -= usec;
840 if (itp->it_value.tv_usec < 0) {
841 itp->it_value.tv_usec += 1000000;
842 itp->it_value.tv_sec--;
843 }
844 } else
845 itp->it_value.tv_usec = 0; /* sec is already 0 */
846 return (0);
847}
848
849/*
850 * Add and subtract routines for timevals.
851 * N.B.: subtract routine doesn't deal with
852 * results which are before the beginning,
853 * it just gets very confused in this case.
854 * Caveat emptor.
855 */
856void
857timevaladd(t1, t2)
858 struct timeval *t1, *t2;
859{
860
861 t1->tv_sec += t2->tv_sec;
862 t1->tv_usec += t2->tv_usec;
863 timevalfix(t1);
864}
865
866void
867timevalsub(t1, t2)
868 struct timeval *t1, *t2;
869{
870
871 t1->tv_sec -= t2->tv_sec;
872 t1->tv_usec -= t2->tv_usec;
873 timevalfix(t1);
874}
875
876static void
877timevalfix(t1)
878 struct timeval *t1;
879{
880
881 if (t1->tv_usec < 0) {
882 t1->tv_sec--;
883 t1->tv_usec += 1000000;
884 }
885 if (t1->tv_usec >= 1000000) {
886 t1->tv_sec++;
887 t1->tv_usec -= 1000000;
888 }
889}
cea4446f
HP
890
891/*
892 * ratecheck(): simple time-based rate-limit checking.
893 */
894int
895ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
896{
897 struct timeval tv, delta;
898 int rv = 0;
899
900 getmicrouptime(&tv); /* NB: 10ms precision */
901 delta = tv;
902 timevalsub(&delta, lasttime);
903
904 /*
905 * check for 0,0 is so that the message will be seen at least once,
906 * even if interval is huge.
907 */
908 if (timevalcmp(&delta, mininterval, >=) ||
909 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
910 *lasttime = tv;
911 rv = 1;
912 }
913
914 return (rv);
915}
916
917/*
918 * ppsratecheck(): packets (or events) per second limitation.
919 *
920 * Return 0 if the limit is to be enforced (e.g. the caller
921 * should drop a packet because of the rate limitation).
922 *
923 * maxpps of 0 always causes zero to be returned. maxpps of -1
924 * always causes 1 to be returned; this effectively defeats rate
925 * limiting.
926 *
927 * Note that we maintain the struct timeval for compatibility
928 * with other bsd systems. We reuse the storage and just monitor
929 * clock ticks for minimal overhead.
930 */
931int
932ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
933{
934 int now;
935
936 /*
937 * Reset the last time and counter if this is the first call
938 * or more than a second has passed since the last update of
939 * lasttime.
940 */
941 now = ticks;
942 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
943 lasttime->tv_sec = now;
944 *curpps = 1;
945 return (maxpps != 0);
946 } else {
947 (*curpps)++; /* NB: ignore potential overflow */
948 return (maxpps < 0 || *curpps < maxpps);
949 }
950}
951