kernel - Add kern.gettimeofday_quick sysctl
[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 $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/sysproto.h>
41#include <sys/resourcevar.h>
42#include <sys/signalvar.h>
43#include <sys/kernel.h>
984263bc 44#include <sys/sysent.h>
df2244e3 45#include <sys/sysunion.h>
984263bc 46#include <sys/proc.h>
895c1f85 47#include <sys/priv.h>
984263bc
MD
48#include <sys/time.h>
49#include <sys/vnode.h>
a94976ad 50#include <sys/sysctl.h>
b3ce8a64 51#include <sys/kern_syscall.h>
984263bc
MD
52#include <vm/vm.h>
53#include <vm/vm_extern.h>
684a93c4 54
245e4f17 55#include <sys/msgport2.h>
88c4d2f6 56#include <sys/thread2.h>
684a93c4 57#include <sys/mplock2.h>
984263bc
MD
58
59struct timezone tz;
60
61/*
62 * Time of day and interval timer support.
63 *
64 * These routines provide the kernel entry points to get and set
65 * the time-of-day and per-process interval timers. Subroutines
66 * here provide support for adding and subtracting timeval structures
67 * and decrementing interval timers, optionally reloading the interval
68 * timers when they expire.
69 */
70
b3ce8a64
MD
71static int settime(struct timeval *);
72static void timevalfix(struct timeval *);
984263bc 73
3b58baa0
MD
74/*
75 * Nanosleep tries very hard to sleep for a precisely requested time
76 * interval, down to 1uS. The administrator can impose a minimum delay
77 * and a delay below which we hard-loop instead of initiate a timer
78 * interrupt and sleep.
79 *
80 * For machines under high loads it might be beneficial to increase min_us
81 * to e.g. 1000uS (1ms) so spining processes sleep meaningfully.
82 */
83static int nanosleep_min_us = 10;
84static int nanosleep_hard_us = 100;
d11c94c4 85static int gettimeofday_quick = 0;
3b58baa0
MD
86SYSCTL_INT(_kern, OID_AUTO, nanosleep_min_us, CTLFLAG_RW,
87 &nanosleep_min_us, 0, "")
88SYSCTL_INT(_kern, OID_AUTO, nanosleep_hard_us, CTLFLAG_RW,
89 &nanosleep_hard_us, 0, "")
d11c94c4
MD
90SYSCTL_INT(_kern, OID_AUTO, gettimeofday_quick, CTLFLAG_RW,
91 &gettimeofday_quick, 0, "")
a94976ad 92
984263bc 93static int
c972a82f 94settime(struct timeval *tv)
984263bc
MD
95{
96 struct timeval delta, tv1, tv2;
97 static struct timeval maxtime, laststep;
98 struct timespec ts;
33924148
JS
99 int origcpu;
100
157202af 101 if ((origcpu = mycpu->gd_cpuid) != 0)
33924148 102 lwkt_setcpu_self(globaldata_find(0));
984263bc 103
88c4d2f6 104 crit_enter();
984263bc
MD
105 microtime(&tv1);
106 delta = *tv;
107 timevalsub(&delta, &tv1);
108
109 /*
110 * If the system is secure, we do not allow the time to be
111 * set to a value earlier than 1 second less than the highest
112 * time we have yet seen. The worst a miscreant can do in
113 * this circumstance is "freeze" time. He couldn't go
114 * back to the past.
115 *
116 * We similarly do not allow the clock to be stepped more
117 * than one second, nor more than once per second. This allows
118 * a miscreant to make the clock march double-time, but no worse.
119 */
120 if (securelevel > 1) {
121 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
122 /*
123 * Update maxtime to latest time we've seen.
124 */
125 if (tv1.tv_sec > maxtime.tv_sec)
126 maxtime = tv1;
127 tv2 = *tv;
128 timevalsub(&tv2, &maxtime);
129 if (tv2.tv_sec < -1) {
130 tv->tv_sec = maxtime.tv_sec - 1;
6ea70f76 131 kprintf("Time adjustment clamped to -1 second\n");
984263bc
MD
132 }
133 } else {
134 if (tv1.tv_sec == laststep.tv_sec) {
88c4d2f6 135 crit_exit();
984263bc
MD
136 return (EPERM);
137 }
138 if (delta.tv_sec > 1) {
139 tv->tv_sec = tv1.tv_sec + 1;
6ea70f76 140 kprintf("Time adjustment clamped to +1 second\n");
984263bc
MD
141 }
142 laststep = *tv;
143 }
144 }
145
146 ts.tv_sec = tv->tv_sec;
147 ts.tv_nsec = tv->tv_usec * 1000;
88c4d2f6 148 set_timeofday(&ts);
88c4d2f6 149 crit_exit();
33924148 150
157202af 151 if (origcpu != 0)
33924148 152 lwkt_setcpu_self(globaldata_find(origcpu));
33924148 153
984263bc
MD
154 resettodr();
155 return (0);
156}
157
3919ced0
MD
158/*
159 * MPSAFE
160 */
984263bc 161int
b3ce8a64 162kern_clock_gettime(clockid_t clock_id, struct timespec *ats)
984263bc 163{
b3ce8a64 164 int error = 0;
91810a6f 165 struct proc *p;
984263bc 166
b3ce8a64 167 switch(clock_id) {
26be1876 168 case CLOCK_REALTIME:
91810a6f 169 case CLOCK_REALTIME_PRECISE:
b3ce8a64
MD
170 nanotime(ats);
171 break;
91810a6f
MD
172 case CLOCK_REALTIME_FAST:
173 getnanotime(ats);
174 break;
26be1876 175 case CLOCK_MONOTONIC:
91810a6f
MD
176 case CLOCK_MONOTONIC_PRECISE:
177 case CLOCK_UPTIME:
178 case CLOCK_UPTIME_PRECISE:
b3ce8a64
MD
179 nanouptime(ats);
180 break;
91810a6f
MD
181 case CLOCK_MONOTONIC_FAST:
182 case CLOCK_UPTIME_FAST:
183 getnanouptime(ats);
184 break;
185 case CLOCK_VIRTUAL:
186 p = curproc;
187 ats->tv_sec = p->p_timer[ITIMER_VIRTUAL].it_value.tv_sec;
188 ats->tv_nsec = p->p_timer[ITIMER_VIRTUAL].it_value.tv_usec *
189 1000;
190 break;
191 case CLOCK_PROF:
192 p = curproc;
193 ats->tv_sec = p->p_timer[ITIMER_PROF].it_value.tv_sec;
194 ats->tv_nsec = p->p_timer[ITIMER_PROF].it_value.tv_usec *
195 1000;
196 break;
197 case CLOCK_SECOND:
198 ats->tv_sec = time_second;
199 ats->tv_nsec = 0;
200 break;
26be1876 201 default:
b3ce8a64
MD
202 error = EINVAL;
203 break;
26be1876 204 }
b3ce8a64 205 return (error);
984263bc
MD
206}
207
3919ced0
MD
208/*
209 * MPSAFE
210 */
984263bc 211int
b3ce8a64
MD
212sys_clock_gettime(struct clock_gettime_args *uap)
213{
214 struct timespec ats;
215 int error;
216
217 error = kern_clock_gettime(uap->clock_id, &ats);
218 if (error == 0)
219 error = copyout(&ats, uap->tp, sizeof(ats));
220
221 return (error);
222}
223
224int
225kern_clock_settime(clockid_t clock_id, struct timespec *ats)
984263bc 226{
dadab5e9 227 struct thread *td = curthread;
984263bc 228 struct timeval atv;
984263bc
MD
229 int error;
230
cc125f38 231 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
984263bc 232 return (error);
b3ce8a64 233 if (clock_id != CLOCK_REALTIME)
984263bc 234 return (EINVAL);
b3ce8a64
MD
235 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
236 return (EINVAL);
237
238 TIMESPEC_TO_TIMEVAL(&atv, ats);
239 error = settime(&atv);
240 return (error);
984263bc
MD
241}
242
3919ced0
MD
243/*
244 * MPALMOSTSAFE
245 */
984263bc 246int
b3ce8a64 247sys_clock_settime(struct clock_settime_args *uap)
984263bc 248{
b3ce8a64
MD
249 struct timespec ats;
250 int error;
251
252 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
253 return (error);
254
3919ced0
MD
255 get_mplock();
256 error = kern_clock_settime(uap->clock_id, &ats);
257 rel_mplock();
258 return (error);
b3ce8a64
MD
259}
260
3919ced0
MD
261/*
262 * MPSAFE
263 */
b3ce8a64
MD
264int
265kern_clock_getres(clockid_t clock_id, struct timespec *ts)
266{
267 int error;
984263bc 268
b3ce8a64 269 switch(clock_id) {
26be1876 270 case CLOCK_REALTIME:
91810a6f
MD
271 case CLOCK_REALTIME_FAST:
272 case CLOCK_REALTIME_PRECISE:
26be1876 273 case CLOCK_MONOTONIC:
91810a6f
MD
274 case CLOCK_MONOTONIC_FAST:
275 case CLOCK_MONOTONIC_PRECISE:
276 case CLOCK_UPTIME:
277 case CLOCK_UPTIME_FAST:
278 case CLOCK_UPTIME_PRECISE:
984263bc 279 /*
26be1876
MD
280 * Round up the result of the division cheaply
281 * by adding 1. Rounding up is especially important
282 * if rounding down would give 0. Perfect rounding
283 * is unimportant.
984263bc 284 */
b3ce8a64
MD
285 ts->tv_sec = 0;
286 ts->tv_nsec = 1000000000 / sys_cputimer->freq + 1;
287 error = 0;
288 break;
91810a6f
MD
289 case CLOCK_VIRTUAL:
290 case CLOCK_PROF:
291 /* Accurately round up here because we can do so cheaply. */
292 ts->tv_sec = 0;
293 ts->tv_nsec = (1000000000 + hz - 1) / hz;
294 error = 0;
295 break;
296 case CLOCK_SECOND:
297 ts->tv_sec = 1;
298 ts->tv_nsec = 0;
299 error = 0;
300 break;
26be1876 301 default:
b3ce8a64
MD
302 error = EINVAL;
303 break;
984263bc 304 }
b3ce8a64
MD
305
306 return(error);
307}
308
3919ced0
MD
309/*
310 * MPSAFE
311 */
b3ce8a64
MD
312int
313sys_clock_getres(struct clock_getres_args *uap)
314{
315 int error;
316 struct timespec ts;
317
318 error = kern_clock_getres(uap->clock_id, &ts);
319 if (error == 0)
320 error = copyout(&ts, uap->tp, sizeof(ts));
321
322 return (error);
984263bc
MD
323}
324
88c4d2f6
MD
325/*
326 * nanosleep1()
327 *
328 * This is a general helper function for nanosleep() (aka sleep() aka
329 * usleep()).
330 *
331 * If there is less then one tick's worth of time left and
332 * we haven't done a yield, or the remaining microseconds is
333 * ridiculously low, do a yield. This avoids having
334 * to deal with systimer overheads when the system is under
335 * heavy loads. If we have done a yield already then use
336 * a systimer and an uninterruptable thread wait.
337 *
338 * If there is more then a tick's worth of time left,
339 * calculate the baseline ticks and use an interruptable
340 * tsleep, then handle the fine-grained delay on the next
341 * loop. This usually results in two sleeps occuring, a long one
342 * and a short one.
3919ced0
MD
343 *
344 * MPSAFE
88c4d2f6
MD
345 */
346static void
96d52ac8
SZ
347ns1_systimer(systimer_t info, int in_ipi __unused,
348 struct intrframe *frame __unused)
88c4d2f6
MD
349{
350 lwkt_schedule(info->data);
351}
984263bc 352
8ba5f7ef 353int
41c20dac 354nanosleep1(struct timespec *rqt, struct timespec *rmt)
984263bc 355{
88c4d2f6 356 static int nanowait;
984263bc
MD
357 struct timespec ts, ts2, ts3;
358 struct timeval tv;
359 int error;
360
361 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
362 return (EINVAL);
8ba5f7ef 363 /* XXX: imho this should return EINVAL at least for tv_sec < 0 */
984263bc
MD
364 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
365 return (0);
a94976ad
MD
366 nanouptime(&ts);
367 timespecadd(&ts, rqt); /* ts = target timestamp compare */
368 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
88c4d2f6 369
984263bc 370 for (;;) {
88c4d2f6
MD
371 int ticks;
372 struct systimer info;
373
a591f597 374 ticks = tv.tv_usec / ustick; /* approximate */
a94976ad 375
88c4d2f6 376 if (tv.tv_sec == 0 && ticks == 0) {
37af14fe 377 thread_t td = curthread;
3b58baa0
MD
378 if (tv.tv_usec > 0 && tv.tv_usec < nanosleep_min_us)
379 tv.tv_usec = nanosleep_min_us;
380 if (tv.tv_usec < nanosleep_hard_us) {
f9235b6d 381 lwkt_user_yield();
3b58baa0 382 cpu_pause();
a94976ad 383 } else {
37af14fe 384 crit_enter_quick(td);
88c4d2f6 385 systimer_init_oneshot(&info, ns1_systimer,
37af14fe
MD
386 td, tv.tv_usec);
387 lwkt_deschedule_self(td);
388 crit_exit_quick(td);
88c4d2f6
MD
389 lwkt_switch();
390 systimer_del(&info); /* make sure it's gone */
a94976ad 391 }
08f2f1bb 392 error = iscaught(td->td_lwp);
88c4d2f6
MD
393 } else if (tv.tv_sec == 0) {
394 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
a94976ad 395 } else {
88c4d2f6
MD
396 ticks = tvtohz_low(&tv); /* also handles overflow */
397 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
a94976ad
MD
398 }
399 nanouptime(&ts2);
88c4d2f6 400 if (error && error != EWOULDBLOCK) {
984263bc
MD
401 if (error == ERESTART)
402 error = EINTR;
403 if (rmt != NULL) {
404 timespecsub(&ts, &ts2);
405 if (ts.tv_sec < 0)
406 timespecclear(&ts);
407 *rmt = ts;
408 }
409 return (error);
410 }
411 if (timespeccmp(&ts2, &ts, >=))
412 return (0);
413 ts3 = ts;
414 timespecsub(&ts3, &ts2);
415 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
416 }
417}
418
3919ced0
MD
419/*
420 * MPSAFE
421 */
984263bc 422int
753fd850 423sys_nanosleep(struct nanosleep_args *uap)
984263bc 424{
245e4f17 425 int error;
f9a13fc4
MD
426 struct timespec rqt;
427 struct timespec rmt;
984263bc 428
f9a13fc4 429 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
984263bc
MD
430 if (error)
431 return (error);
f9a13fc4
MD
432
433 error = nanosleep1(&rqt, &rmt);
434
245e4f17 435 /*
f9a13fc4 436 * copyout the residual if nanosleep was interrupted.
245e4f17 437 */
55d25c87
SS
438 if (error && uap->rmtp) {
439 int error2;
440
441 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
442 if (error2)
443 error = error2;
444 }
984263bc
MD
445 return (error);
446}
447
3919ced0 448/*
d11c94c4
MD
449 * The gettimeofday() system call is supposed to return a fine-grained
450 * realtime stamp. However, acquiring a fine-grained stamp can create a
451 * bottleneck when multiple cpu cores are trying to accessing e.g. the
452 * HPET hardware timer all at the same time, so we have a sysctl that
453 * allows its behavior to be changed to a more coarse-grained timestamp
454 * which does not have to access a hardware timer.
3919ced0 455 */
984263bc 456int
753fd850 457sys_gettimeofday(struct gettimeofday_args *uap)
984263bc
MD
458{
459 struct timeval atv;
460 int error = 0;
461
462 if (uap->tp) {
d11c94c4
MD
463 if (gettimeofday_quick)
464 getmicrotime(&atv);
465 else
466 microtime(&atv);
984263bc
MD
467 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
468 sizeof (atv))))
469 return (error);
470 }
471 if (uap->tzp)
472 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
473 sizeof (tz));
474 return (error);
475}
476
3919ced0
MD
477/*
478 * MPALMOSTSAFE
479 */
984263bc 480int
753fd850 481sys_settimeofday(struct settimeofday_args *uap)
984263bc 482{
dadab5e9 483 struct thread *td = curthread;
984263bc
MD
484 struct timeval atv;
485 struct timezone atz;
486 int error;
487
cc125f38 488 if ((error = priv_check(td, PRIV_SETTIMEOFDAY)))
984263bc 489 return (error);
f59ccd43
MD
490 /*
491 * Verify all parameters before changing time.
492 *
493 * NOTE: We do not allow the time to be set to 0.0, which also by
494 * happy coincidence works around a pkgsrc bulk build bug.
495 */
984263bc
MD
496 if (uap->tv) {
497 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
498 sizeof(atv))))
499 return (error);
500 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
501 return (EINVAL);
f59ccd43
MD
502 if (atv.tv_sec == 0 && atv.tv_usec == 0)
503 return (EINVAL);
984263bc
MD
504 }
505 if (uap->tzp &&
506 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
507 return (error);
3919ced0
MD
508
509 get_mplock();
510 if (uap->tv && (error = settime(&atv))) {
511 rel_mplock();
984263bc 512 return (error);
3919ced0
MD
513 }
514 rel_mplock();
984263bc
MD
515 if (uap->tzp)
516 tz = atz;
517 return (0);
518}
519
4026c000
JS
520static void
521kern_adjtime_common(void)
522{
523 if ((ntp_delta >= 0 && ntp_delta < ntp_default_tick_delta) ||
7df7080b 524 (ntp_delta < 0 && ntp_delta > -ntp_default_tick_delta))
4026c000
JS
525 ntp_tick_delta = ntp_delta;
526 else if (ntp_delta > ntp_big_delta)
527 ntp_tick_delta = 10 * ntp_default_tick_delta;
528 else if (ntp_delta < -ntp_big_delta)
529 ntp_tick_delta = -10 * ntp_default_tick_delta;
530 else if (ntp_delta > 0)
531 ntp_tick_delta = ntp_default_tick_delta;
532 else
533 ntp_tick_delta = -ntp_default_tick_delta;
534}
535
536void
537kern_adjtime(int64_t delta, int64_t *odelta)
538{
539 int origcpu;
540
157202af 541 if ((origcpu = mycpu->gd_cpuid) != 0)
4026c000 542 lwkt_setcpu_self(globaldata_find(0));
4026c000
JS
543
544 crit_enter();
545 *odelta = ntp_delta;
08f95c49 546 ntp_delta = delta;
4026c000
JS
547 kern_adjtime_common();
548 crit_exit();
549
157202af 550 if (origcpu != 0)
4026c000 551 lwkt_setcpu_self(globaldata_find(origcpu));
4026c000
JS
552}
553
b6da4cbb
JS
554static void
555kern_get_ntp_delta(int64_t *delta)
556{
557 int origcpu;
558
559 if ((origcpu = mycpu->gd_cpuid) != 0)
560 lwkt_setcpu_self(globaldata_find(0));
561
562 crit_enter();
563 *delta = ntp_delta;
564 crit_exit();
565
566 if (origcpu != 0)
567 lwkt_setcpu_self(globaldata_find(origcpu));
568}
569
4026c000
JS
570void
571kern_reladjtime(int64_t delta)
572{
573 int origcpu;
574
157202af 575 if ((origcpu = mycpu->gd_cpuid) != 0)
4026c000 576 lwkt_setcpu_self(globaldata_find(0));
4026c000
JS
577
578 crit_enter();
579 ntp_delta += delta;
580 kern_adjtime_common();
581 crit_exit();
582
157202af 583 if (origcpu != 0)
4026c000 584 lwkt_setcpu_self(globaldata_find(origcpu));
4026c000 585}
984263bc 586
0143455b
JS
587static void
588kern_adjfreq(int64_t rate)
589{
590 int origcpu;
591
157202af 592 if ((origcpu = mycpu->gd_cpuid) != 0)
0143455b 593 lwkt_setcpu_self(globaldata_find(0));
0143455b
JS
594
595 crit_enter();
596 ntp_tick_permanent = rate;
597 crit_exit();
598
157202af 599 if (origcpu != 0)
0143455b 600 lwkt_setcpu_self(globaldata_find(origcpu));
0143455b
JS
601}
602
3919ced0
MD
603/*
604 * MPALMOSTSAFE
605 */
984263bc 606int
753fd850 607sys_adjtime(struct adjtime_args *uap)
984263bc 608{
dadab5e9 609 struct thread *td = curthread;
984263bc 610 struct timeval atv;
4026c000 611 int64_t ndelta, odelta;
88c4d2f6 612 int error;
984263bc 613
cc125f38 614 if ((error = priv_check(td, PRIV_ADJTIME)))
984263bc 615 return (error);
3919ced0
MD
616 error = copyin(uap->delta, &atv, sizeof(struct timeval));
617 if (error)
984263bc
MD
618 return (error);
619
620 /*
621 * Compute the total correction and the rate at which to apply it.
622 * Round the adjustment down to a whole multiple of the per-tick
623 * delta, so that after some number of incremental changes in
624 * hardclock(), tickdelta will become zero, lest the correction
625 * overshoot and start taking us away from the desired final time.
626 */
08f95c49 627 ndelta = (int64_t)atv.tv_sec * 1000000000 + atv.tv_usec * 1000;
3919ced0 628 get_mplock();
4026c000 629 kern_adjtime(ndelta, &odelta);
3919ced0 630 rel_mplock();
984263bc
MD
631
632 if (uap->olddelta) {
4026c000 633 atv.tv_sec = odelta / 1000000000;
a821f7fc 634 atv.tv_usec = odelta % 1000000000 / 1000;
3919ced0 635 copyout(&atv, uap->olddelta, sizeof(struct timeval));
984263bc
MD
636 }
637 return (0);
638}
639
4026c000
JS
640static int
641sysctl_adjtime(SYSCTL_HANDLER_ARGS)
642{
643 int64_t delta;
644 int error;
645
4026c000 646 if (req->newptr != NULL) {
895c1f85 647 if (priv_check(curthread, PRIV_ROOT))
4026c000
JS
648 return (EPERM);
649 error = SYSCTL_IN(req, &delta, sizeof(delta));
650 if (error)
651 return (error);
652 kern_reladjtime(delta);
653 }
5eb5a6bc
MD
654
655 if (req->oldptr)
656 kern_get_ntp_delta(&delta);
657 error = SYSCTL_OUT(req, &delta, sizeof(delta));
658 return (error);
4026c000
JS
659}
660
08f95c49
MD
661/*
662 * delta is in nanoseconds.
663 */
0143455b 664static int
b6da4cbb
JS
665sysctl_delta(SYSCTL_HANDLER_ARGS)
666{
667 int64_t delta, old_delta;
668 int error;
669
670 if (req->newptr != NULL) {
895c1f85 671 if (priv_check(curthread, PRIV_ROOT))
b6da4cbb
JS
672 return (EPERM);
673 error = SYSCTL_IN(req, &delta, sizeof(delta));
674 if (error)
675 return (error);
676 kern_adjtime(delta, &old_delta);
b6da4cbb
JS
677 }
678
5eb5a6bc
MD
679 if (req->oldptr != NULL)
680 kern_get_ntp_delta(&old_delta);
cebaad99 681 error = SYSCTL_OUT(req, &old_delta, sizeof(old_delta));
5eb5a6bc 682 return (error);
b6da4cbb
JS
683}
684
08f95c49
MD
685/*
686 * frequency is in nanoseconds per second shifted left 32.
687 * kern_adjfreq() needs it in nanoseconds per tick shifted left 32.
688 */
b6da4cbb 689static int
0143455b
JS
690sysctl_adjfreq(SYSCTL_HANDLER_ARGS)
691{
692 int64_t freqdelta;
693 int error;
694
0143455b 695 if (req->newptr != NULL) {
895c1f85 696 if (priv_check(curthread, PRIV_ROOT))
0143455b
JS
697 return (EPERM);
698 error = SYSCTL_IN(req, &freqdelta, sizeof(freqdelta));
699 if (error)
700 return (error);
701
702 freqdelta /= hz;
703 kern_adjfreq(freqdelta);
704 }
5eb5a6bc
MD
705
706 if (req->oldptr != NULL)
707 freqdelta = ntp_tick_permanent * hz;
708 error = SYSCTL_OUT(req, &freqdelta, sizeof(freqdelta));
709 if (error)
710 return (error);
711
0143455b
JS
712 return (0);
713}
714
4026c000 715SYSCTL_NODE(_kern, OID_AUTO, ntp, CTLFLAG_RW, 0, "NTP related controls");
5eb5a6bc
MD
716SYSCTL_PROC(_kern_ntp, OID_AUTO, permanent,
717 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
cebaad99 718 sysctl_adjfreq, "Q", "permanent correction per second");
b6da4cbb 719SYSCTL_PROC(_kern_ntp, OID_AUTO, delta,
5eb5a6bc 720 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
cebaad99 721 sysctl_delta, "Q", "one-time delta");
5eb5a6bc
MD
722SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, big_delta, CTLFLAG_RD,
723 &ntp_big_delta, sizeof(ntp_big_delta), "Q",
724 "threshold for fast adjustment");
725SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, tick_delta, CTLFLAG_RD,
726 &ntp_tick_delta, sizeof(ntp_tick_delta), "LU",
727 "per-tick adjustment");
728SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, default_tick_delta, CTLFLAG_RD,
729 &ntp_default_tick_delta, sizeof(ntp_default_tick_delta), "LU",
730 "default per-tick adjustment");
48590578 731SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, next_leap_second, CTLFLAG_RW,
5eb5a6bc
MD
732 &ntp_leap_second, sizeof(ntp_leap_second), "LU",
733 "next leap second");
48590578
JS
734SYSCTL_INT(_kern_ntp, OID_AUTO, insert_leap_second, CTLFLAG_RW,
735 &ntp_leap_insert, 0, "insert or remove leap second");
4026c000 736SYSCTL_PROC(_kern_ntp, OID_AUTO, adjust,
cebaad99
JS
737 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
738 sysctl_adjtime, "Q", "relative adjust for delta");
4026c000 739
984263bc
MD
740/*
741 * Get value of an interval timer. The process virtual and
742 * profiling virtual time timers are kept in the p_stats area, since
743 * they can be swapped out. These are kept internally in the
744 * way they are specified externally: in time until they expire.
745 *
746 * The real time interval timer is kept in the process table slot
747 * for the process, and its value (it_value) is kept as an
748 * absolute time rather than as a delta, so that it is easy to keep
749 * periodic real-time signals from drifting.
750 *
751 * Virtual time timers are processed in the hardclock() routine of
752 * kern_clock.c. The real time timer is processed by a timeout
753 * routine, called from the softclock() routine. Since a callout
754 * may be delayed in real time due to interrupt processing in the system,
755 * it is possible for the real time timeout routine (realitexpire, given below),
756 * to be delayed in real time past when it is supposed to occur. It
757 * does not suffice, therefore, to reload the real timer .it_value from the
758 * real time timers .it_interval. Rather, we compute the next time in
759 * absolute time the timer should go off.
3919ced0
MD
760 *
761 * MPALMOSTSAFE
984263bc 762 */
984263bc 763int
753fd850 764sys_getitimer(struct getitimer_args *uap)
984263bc 765{
41c20dac 766 struct proc *p = curproc;
984263bc
MD
767 struct timeval ctv;
768 struct itimerval aitv;
984263bc
MD
769
770 if (uap->which > ITIMER_PROF)
771 return (EINVAL);
d7f4c458 772 lwkt_gettoken(&p->p_token);
984263bc
MD
773 if (uap->which == ITIMER_REAL) {
774 /*
775 * Convert from absolute to relative time in .it_value
776 * part of real time timer. If time for real time timer
777 * has passed return 0, else return difference between
778 * current time and time for the timer to go off.
779 */
780 aitv = p->p_realtimer;
781 if (timevalisset(&aitv.it_value)) {
782 getmicrouptime(&ctv);
783 if (timevalcmp(&aitv.it_value, &ctv, <))
784 timevalclear(&aitv.it_value);
785 else
786 timevalsub(&aitv.it_value, &ctv);
787 }
88c4d2f6 788 } else {
93328593 789 aitv = p->p_timer[uap->which];
88c4d2f6 790 }
d7f4c458 791 lwkt_reltoken(&p->p_token);
3919ced0 792 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
984263bc
MD
793}
794
3919ced0
MD
795/*
796 * MPALMOSTSAFE
797 */
984263bc 798int
753fd850 799sys_setitimer(struct setitimer_args *uap)
984263bc
MD
800{
801 struct itimerval aitv;
802 struct timeval ctv;
41c20dac
MD
803 struct itimerval *itvp;
804 struct proc *p = curproc;
88c4d2f6 805 int error;
984263bc
MD
806
807 if (uap->which > ITIMER_PROF)
808 return (EINVAL);
809 itvp = uap->itv;
810 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
811 sizeof(struct itimerval))))
812 return (error);
813 if ((uap->itv = uap->oitv) &&
753fd850 814 (error = sys_getitimer((struct getitimer_args *)uap)))
984263bc 815 return (error);
4090d6ff 816 if (itvp == NULL)
984263bc
MD
817 return (0);
818 if (itimerfix(&aitv.it_value))
819 return (EINVAL);
820 if (!timevalisset(&aitv.it_value))
821 timevalclear(&aitv.it_interval);
822 else if (itimerfix(&aitv.it_interval))
823 return (EINVAL);
d7f4c458 824 lwkt_gettoken(&p->p_token);
984263bc
MD
825 if (uap->which == ITIMER_REAL) {
826 if (timevalisset(&p->p_realtimer.it_value))
a471eac5 827 callout_stop_sync(&p->p_ithandle);
984263bc 828 if (timevalisset(&aitv.it_value))
8fbf9130
JS
829 callout_reset(&p->p_ithandle,
830 tvtohz_high(&aitv.it_value), realitexpire, p);
984263bc
MD
831 getmicrouptime(&ctv);
832 timevaladd(&aitv.it_value, &ctv);
833 p->p_realtimer = aitv;
88c4d2f6 834 } else {
93328593 835 p->p_timer[uap->which] = aitv;
898e34b3
MD
836 switch(uap->which) {
837 case ITIMER_VIRTUAL:
4643740a 838 p->p_flags &= ~P_SIGVTALRM;
898e34b3
MD
839 break;
840 case ITIMER_PROF:
4643740a 841 p->p_flags &= ~P_SIGPROF;
898e34b3
MD
842 break;
843 }
88c4d2f6 844 }
d7f4c458 845 lwkt_reltoken(&p->p_token);
984263bc
MD
846 return (0);
847}
848
849/*
850 * Real interval timer expired:
851 * send process whose timer expired an alarm signal.
852 * If time is not set up to reload, then just return.
853 * Else compute next time timer should go off which is > current time.
854 * This is where delay in processing this timeout causes multiple
855 * SIGALRM calls to be compressed into one.
a94976ad 856 * tvtohz_high() always adds 1 to allow for the time until the next clock
984263bc
MD
857 * interrupt being strictly less than 1 clock tick, but we don't want
858 * that here since we want to appear to be in sync with the clock
859 * interrupt even when we're delayed.
860 */
861void
c972a82f 862realitexpire(void *arg)
984263bc 863{
1fd87d54 864 struct proc *p;
984263bc 865 struct timeval ctv, ntv;
984263bc
MD
866
867 p = (struct proc *)arg;
a471eac5 868 PHOLD(p);
d7f4c458 869 lwkt_gettoken(&p->p_token);
84204577 870 ksignal(p, SIGALRM);
984263bc
MD
871 if (!timevalisset(&p->p_realtimer.it_interval)) {
872 timevalclear(&p->p_realtimer.it_value);
a471eac5 873 goto done;
984263bc
MD
874 }
875 for (;;) {
984263bc 876 timevaladd(&p->p_realtimer.it_value,
d7f4c458 877 &p->p_realtimer.it_interval);
984263bc
MD
878 getmicrouptime(&ctv);
879 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
880 ntv = p->p_realtimer.it_value;
881 timevalsub(&ntv, &ctv);
8fbf9130
JS
882 callout_reset(&p->p_ithandle, tvtohz_low(&ntv),
883 realitexpire, p);
a471eac5 884 goto done;
984263bc 885 }
984263bc 886 }
a471eac5 887done:
d7f4c458 888 lwkt_reltoken(&p->p_token);
a471eac5 889 PRELE(p);
984263bc
MD
890}
891
892/*
893 * Check that a proposed value to load into the .it_value or
894 * .it_interval part of an interval timer is acceptable, and
895 * fix it to have at least minimal value (i.e. if it is less
896 * than the resolution of the clock, round it up.)
3919ced0
MD
897 *
898 * MPSAFE
984263bc
MD
899 */
900int
c972a82f 901itimerfix(struct timeval *tv)
984263bc
MD
902{
903
904 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
905 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
906 return (EINVAL);
a591f597
MD
907 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < ustick)
908 tv->tv_usec = ustick;
984263bc
MD
909 return (0);
910}
911
912/*
913 * Decrement an interval timer by a specified number
914 * of microseconds, which must be less than a second,
915 * i.e. < 1000000. If the timer expires, then reload
916 * it. In this case, carry over (usec - old value) to
917 * reduce the value reloaded into the timer so that
918 * the timer does not drift. This routine assumes
919 * that it is called in a context where the timers
920 * on which it is operating cannot change in value.
921 */
922int
c972a82f 923itimerdecr(struct itimerval *itp, int usec)
984263bc
MD
924{
925
926 if (itp->it_value.tv_usec < usec) {
927 if (itp->it_value.tv_sec == 0) {
928 /* expired, and already in next interval */
929 usec -= itp->it_value.tv_usec;
930 goto expire;
931 }
932 itp->it_value.tv_usec += 1000000;
933 itp->it_value.tv_sec--;
934 }
935 itp->it_value.tv_usec -= usec;
936 usec = 0;
937 if (timevalisset(&itp->it_value))
938 return (1);
939 /* expired, exactly at end of interval */
940expire:
941 if (timevalisset(&itp->it_interval)) {
942 itp->it_value = itp->it_interval;
943 itp->it_value.tv_usec -= usec;
944 if (itp->it_value.tv_usec < 0) {
945 itp->it_value.tv_usec += 1000000;
946 itp->it_value.tv_sec--;
947 }
948 } else
949 itp->it_value.tv_usec = 0; /* sec is already 0 */
950 return (0);
951}
952
953/*
954 * Add and subtract routines for timevals.
955 * N.B.: subtract routine doesn't deal with
956 * results which are before the beginning,
957 * it just gets very confused in this case.
958 * Caveat emptor.
959 */
960void
9deadd02 961timevaladd(struct timeval *t1, const struct timeval *t2)
984263bc
MD
962{
963
964 t1->tv_sec += t2->tv_sec;
965 t1->tv_usec += t2->tv_usec;
966 timevalfix(t1);
967}
968
969void
9deadd02 970timevalsub(struct timeval *t1, const struct timeval *t2)
984263bc
MD
971{
972
973 t1->tv_sec -= t2->tv_sec;
974 t1->tv_usec -= t2->tv_usec;
975 timevalfix(t1);
976}
977
978static void
c972a82f 979timevalfix(struct timeval *t1)
984263bc
MD
980{
981
982 if (t1->tv_usec < 0) {
983 t1->tv_sec--;
984 t1->tv_usec += 1000000;
985 }
986 if (t1->tv_usec >= 1000000) {
987 t1->tv_sec++;
988 t1->tv_usec -= 1000000;
989 }
990}
cea4446f
HP
991
992/*
993 * ratecheck(): simple time-based rate-limit checking.
994 */
995int
996ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
997{
998 struct timeval tv, delta;
999 int rv = 0;
1000
1001 getmicrouptime(&tv); /* NB: 10ms precision */
1002 delta = tv;
1003 timevalsub(&delta, lasttime);
1004
1005 /*
1006 * check for 0,0 is so that the message will be seen at least once,
1007 * even if interval is huge.
1008 */
1009 if (timevalcmp(&delta, mininterval, >=) ||
1010 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1011 *lasttime = tv;
1012 rv = 1;
1013 }
1014
1015 return (rv);
1016}
1017
1018/*
1019 * ppsratecheck(): packets (or events) per second limitation.
1020 *
1021 * Return 0 if the limit is to be enforced (e.g. the caller
1022 * should drop a packet because of the rate limitation).
1023 *
1024 * maxpps of 0 always causes zero to be returned. maxpps of -1
1025 * always causes 1 to be returned; this effectively defeats rate
1026 * limiting.
1027 *
1028 * Note that we maintain the struct timeval for compatibility
1029 * with other bsd systems. We reuse the storage and just monitor
1030 * clock ticks for minimal overhead.
1031 */
1032int
1033ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1034{
1035 int now;
1036
1037 /*
1038 * Reset the last time and counter if this is the first call
1039 * or more than a second has passed since the last update of
1040 * lasttime.
1041 */
1042 now = ticks;
1043 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1044 lasttime->tv_sec = now;
1045 *curpps = 1;
1046 return (maxpps != 0);
1047 } else {
1048 (*curpps)++; /* NB: ignore potential overflow */
1049 return (maxpps < 0 || *curpps < maxpps);
1050 }
1051}
1052