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