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