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