xref: /freebsd/sys/kern/kern_time.c (revision 5b20c5e1f8d59f14901f0c0e4b6b577eaa3f9569)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_ktrace.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/limits.h>
42 #include <sys/clock.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/sysproto.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/kernel.h>
49 #include <sys/sleepqueue.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/posix4.h>
56 #include <sys/time.h>
57 #include <sys/timers.h>
58 #include <sys/timetc.h>
59 #include <sys/vnode.h>
60 #ifdef KTRACE
61 #include <sys/ktrace.h>
62 #endif
63 
64 #include <vm/vm.h>
65 #include <vm/vm_extern.h>
66 
67 #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
68 #define CPUCLOCK_BIT		0x80000000
69 #define CPUCLOCK_PROCESS_BIT	0x40000000
70 #define CPUCLOCK_ID_MASK	(~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
71 #define MAKE_THREAD_CPUCLOCK(tid)	(CPUCLOCK_BIT|(tid))
72 #define MAKE_PROCESS_CPUCLOCK(pid)	\
73 	(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
74 
75 static struct kclock	posix_clocks[MAX_CLOCKS];
76 static uma_zone_t	itimer_zone = NULL;
77 
78 /*
79  * Time of day and interval timer support.
80  *
81  * These routines provide the kernel entry points to get and set
82  * the time-of-day and per-process interval timers.  Subroutines
83  * here provide support for adding and subtracting timeval structures
84  * and decrementing interval timers, optionally reloading the interval
85  * timers when they expire.
86  */
87 
88 static int	settime(struct thread *, struct timeval *);
89 static void	timevalfix(struct timeval *);
90 static int	user_clock_nanosleep(struct thread *td, clockid_t clock_id,
91 		    int flags, const struct timespec *ua_rqtp,
92 		    struct timespec *ua_rmtp);
93 
94 static void	itimer_start(void);
95 static int	itimer_init(void *, int, int);
96 static void	itimer_fini(void *, int);
97 static void	itimer_enter(struct itimer *);
98 static void	itimer_leave(struct itimer *);
99 static struct itimer *itimer_find(struct proc *, int);
100 static void	itimers_alloc(struct proc *);
101 static int	realtimer_create(struct itimer *);
102 static int	realtimer_gettime(struct itimer *, struct itimerspec *);
103 static int	realtimer_settime(struct itimer *, int,
104 			struct itimerspec *, struct itimerspec *);
105 static int	realtimer_delete(struct itimer *);
106 static void	realtimer_clocktime(clockid_t, struct timespec *);
107 static void	realtimer_expire(void *);
108 
109 static int	register_posix_clock(int, const struct kclock *);
110 static void	itimer_fire(struct itimer *it);
111 static int	itimespecfix(struct timespec *ts);
112 
113 #define CLOCK_CALL(clock, call, arglist)		\
114 	((*posix_clocks[clock].call) arglist)
115 
116 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
117 
118 static int
119 settime(struct thread *td, struct timeval *tv)
120 {
121 	struct timeval delta, tv1, tv2;
122 	static struct timeval maxtime, laststep;
123 	struct timespec ts;
124 
125 	microtime(&tv1);
126 	delta = *tv;
127 	timevalsub(&delta, &tv1);
128 
129 	/*
130 	 * If the system is secure, we do not allow the time to be
131 	 * set to a value earlier than 1 second less than the highest
132 	 * time we have yet seen. The worst a miscreant can do in
133 	 * this circumstance is "freeze" time. He couldn't go
134 	 * back to the past.
135 	 *
136 	 * We similarly do not allow the clock to be stepped more
137 	 * than one second, nor more than once per second. This allows
138 	 * a miscreant to make the clock march double-time, but no worse.
139 	 */
140 	if (securelevel_gt(td->td_ucred, 1) != 0) {
141 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
142 			/*
143 			 * Update maxtime to latest time we've seen.
144 			 */
145 			if (tv1.tv_sec > maxtime.tv_sec)
146 				maxtime = tv1;
147 			tv2 = *tv;
148 			timevalsub(&tv2, &maxtime);
149 			if (tv2.tv_sec < -1) {
150 				tv->tv_sec = maxtime.tv_sec - 1;
151 				printf("Time adjustment clamped to -1 second\n");
152 			}
153 		} else {
154 			if (tv1.tv_sec == laststep.tv_sec)
155 				return (EPERM);
156 			if (delta.tv_sec > 1) {
157 				tv->tv_sec = tv1.tv_sec + 1;
158 				printf("Time adjustment clamped to +1 second\n");
159 			}
160 			laststep = *tv;
161 		}
162 	}
163 
164 	ts.tv_sec = tv->tv_sec;
165 	ts.tv_nsec = tv->tv_usec * 1000;
166 	tc_setclock(&ts);
167 	resettodr();
168 	return (0);
169 }
170 
171 #ifndef _SYS_SYSPROTO_H_
172 struct clock_getcpuclockid2_args {
173 	id_t id;
174 	int which,
175 	clockid_t *clock_id;
176 };
177 #endif
178 /* ARGSUSED */
179 int
180 sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
181 {
182 	clockid_t clk_id;
183 	int error;
184 
185 	error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
186 	if (error == 0)
187 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
188 	return (error);
189 }
190 
191 int
192 kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
193     clockid_t *clk_id)
194 {
195 	struct proc *p;
196 	pid_t pid;
197 	lwpid_t tid;
198 	int error;
199 
200 	switch (which) {
201 	case CPUCLOCK_WHICH_PID:
202 		if (id != 0) {
203 			error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
204 			if (error != 0)
205 				return (error);
206 			PROC_UNLOCK(p);
207 			pid = id;
208 		} else {
209 			pid = td->td_proc->p_pid;
210 		}
211 		*clk_id = MAKE_PROCESS_CPUCLOCK(pid);
212 		return (0);
213 	case CPUCLOCK_WHICH_TID:
214 		tid = id == 0 ? td->td_tid : id;
215 		*clk_id = MAKE_THREAD_CPUCLOCK(tid);
216 		return (0);
217 	default:
218 		return (EINVAL);
219 	}
220 }
221 
222 #ifndef _SYS_SYSPROTO_H_
223 struct clock_gettime_args {
224 	clockid_t clock_id;
225 	struct	timespec *tp;
226 };
227 #endif
228 /* ARGSUSED */
229 int
230 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
231 {
232 	struct timespec ats;
233 	int error;
234 
235 	error = kern_clock_gettime(td, uap->clock_id, &ats);
236 	if (error == 0)
237 		error = copyout(&ats, uap->tp, sizeof(ats));
238 
239 	return (error);
240 }
241 
242 static inline void
243 cputick2timespec(uint64_t runtime, struct timespec *ats)
244 {
245 	runtime = cputick2usec(runtime);
246 	ats->tv_sec = runtime / 1000000;
247 	ats->tv_nsec = runtime % 1000000 * 1000;
248 }
249 
250 void
251 kern_thread_cputime(struct thread *targettd, struct timespec *ats)
252 {
253 	uint64_t runtime, curtime, switchtime;
254 
255 	if (targettd == NULL) { /* current thread */
256 		critical_enter();
257 		switchtime = PCPU_GET(switchtime);
258 		curtime = cpu_ticks();
259 		runtime = curthread->td_runtime;
260 		critical_exit();
261 		runtime += curtime - switchtime;
262 	} else {
263 		PROC_LOCK_ASSERT(targettd->td_proc, MA_OWNED);
264 		thread_lock(targettd);
265 		runtime = targettd->td_runtime;
266 		thread_unlock(targettd);
267 	}
268 	cputick2timespec(runtime, ats);
269 }
270 
271 void
272 kern_process_cputime(struct proc *targetp, struct timespec *ats)
273 {
274 	uint64_t runtime;
275 	struct rusage ru;
276 
277 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
278 	PROC_STATLOCK(targetp);
279 	rufetch(targetp, &ru);
280 	runtime = targetp->p_rux.rux_runtime;
281 	if (curthread->td_proc == targetp)
282 		runtime += cpu_ticks() - PCPU_GET(switchtime);
283 	PROC_STATUNLOCK(targetp);
284 	cputick2timespec(runtime, ats);
285 }
286 
287 static int
288 get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
289 {
290 	struct proc *p, *p2;
291 	struct thread *td2;
292 	lwpid_t tid;
293 	pid_t pid;
294 	int error;
295 
296 	p = td->td_proc;
297 	if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
298 		tid = clock_id & CPUCLOCK_ID_MASK;
299 		td2 = tdfind(tid, p->p_pid);
300 		if (td2 == NULL)
301 			return (EINVAL);
302 		kern_thread_cputime(td2, ats);
303 		PROC_UNLOCK(td2->td_proc);
304 	} else {
305 		pid = clock_id & CPUCLOCK_ID_MASK;
306 		error = pget(pid, PGET_CANSEE, &p2);
307 		if (error != 0)
308 			return (EINVAL);
309 		kern_process_cputime(p2, ats);
310 		PROC_UNLOCK(p2);
311 	}
312 	return (0);
313 }
314 
315 int
316 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
317 {
318 	struct timeval sys, user;
319 	struct proc *p;
320 
321 	p = td->td_proc;
322 	switch (clock_id) {
323 	case CLOCK_REALTIME:		/* Default to precise. */
324 	case CLOCK_REALTIME_PRECISE:
325 		nanotime(ats);
326 		break;
327 	case CLOCK_REALTIME_FAST:
328 		getnanotime(ats);
329 		break;
330 	case CLOCK_VIRTUAL:
331 		PROC_LOCK(p);
332 		PROC_STATLOCK(p);
333 		calcru(p, &user, &sys);
334 		PROC_STATUNLOCK(p);
335 		PROC_UNLOCK(p);
336 		TIMEVAL_TO_TIMESPEC(&user, ats);
337 		break;
338 	case CLOCK_PROF:
339 		PROC_LOCK(p);
340 		PROC_STATLOCK(p);
341 		calcru(p, &user, &sys);
342 		PROC_STATUNLOCK(p);
343 		PROC_UNLOCK(p);
344 		timevaladd(&user, &sys);
345 		TIMEVAL_TO_TIMESPEC(&user, ats);
346 		break;
347 	case CLOCK_MONOTONIC:		/* Default to precise. */
348 	case CLOCK_MONOTONIC_PRECISE:
349 	case CLOCK_UPTIME:
350 	case CLOCK_UPTIME_PRECISE:
351 		nanouptime(ats);
352 		break;
353 	case CLOCK_UPTIME_FAST:
354 	case CLOCK_MONOTONIC_FAST:
355 		getnanouptime(ats);
356 		break;
357 	case CLOCK_SECOND:
358 		ats->tv_sec = time_second;
359 		ats->tv_nsec = 0;
360 		break;
361 	case CLOCK_THREAD_CPUTIME_ID:
362 		kern_thread_cputime(NULL, ats);
363 		break;
364 	case CLOCK_PROCESS_CPUTIME_ID:
365 		PROC_LOCK(p);
366 		kern_process_cputime(p, ats);
367 		PROC_UNLOCK(p);
368 		break;
369 	default:
370 		if ((int)clock_id >= 0)
371 			return (EINVAL);
372 		return (get_cputime(td, clock_id, ats));
373 	}
374 	return (0);
375 }
376 
377 #ifndef _SYS_SYSPROTO_H_
378 struct clock_settime_args {
379 	clockid_t clock_id;
380 	const struct	timespec *tp;
381 };
382 #endif
383 /* ARGSUSED */
384 int
385 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
386 {
387 	struct timespec ats;
388 	int error;
389 
390 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
391 		return (error);
392 	return (kern_clock_settime(td, uap->clock_id, &ats));
393 }
394 
395 static int allow_insane_settime = 0;
396 SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN,
397     &allow_insane_settime, 0,
398     "do not perform possibly restrictive checks on settime(2) args");
399 
400 int
401 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
402 {
403 	struct timeval atv;
404 	int error;
405 
406 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
407 		return (error);
408 	if (clock_id != CLOCK_REALTIME)
409 		return (EINVAL);
410 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000 ||
411 	    ats->tv_sec < 0)
412 		return (EINVAL);
413 	if (!allow_insane_settime &&
414 	    (ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60 ||
415 	    ats->tv_sec < utc_offset()))
416 		return (EINVAL);
417 	/* XXX Don't convert nsec->usec and back */
418 	TIMESPEC_TO_TIMEVAL(&atv, ats);
419 	error = settime(td, &atv);
420 	return (error);
421 }
422 
423 #ifndef _SYS_SYSPROTO_H_
424 struct clock_getres_args {
425 	clockid_t clock_id;
426 	struct	timespec *tp;
427 };
428 #endif
429 int
430 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
431 {
432 	struct timespec ts;
433 	int error;
434 
435 	if (uap->tp == NULL)
436 		return (0);
437 
438 	error = kern_clock_getres(td, uap->clock_id, &ts);
439 	if (error == 0)
440 		error = copyout(&ts, uap->tp, sizeof(ts));
441 	return (error);
442 }
443 
444 int
445 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
446 {
447 
448 	ts->tv_sec = 0;
449 	switch (clock_id) {
450 	case CLOCK_REALTIME:
451 	case CLOCK_REALTIME_FAST:
452 	case CLOCK_REALTIME_PRECISE:
453 	case CLOCK_MONOTONIC:
454 	case CLOCK_MONOTONIC_FAST:
455 	case CLOCK_MONOTONIC_PRECISE:
456 	case CLOCK_UPTIME:
457 	case CLOCK_UPTIME_FAST:
458 	case CLOCK_UPTIME_PRECISE:
459 		/*
460 		 * Round up the result of the division cheaply by adding 1.
461 		 * Rounding up is especially important if rounding down
462 		 * would give 0.  Perfect rounding is unimportant.
463 		 */
464 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
465 		break;
466 	case CLOCK_VIRTUAL:
467 	case CLOCK_PROF:
468 		/* Accurately round up here because we can do so cheaply. */
469 		ts->tv_nsec = howmany(1000000000, hz);
470 		break;
471 	case CLOCK_SECOND:
472 		ts->tv_sec = 1;
473 		ts->tv_nsec = 0;
474 		break;
475 	case CLOCK_THREAD_CPUTIME_ID:
476 	case CLOCK_PROCESS_CPUTIME_ID:
477 	cputime:
478 		/* sync with cputick2usec */
479 		ts->tv_nsec = 1000000 / cpu_tickrate();
480 		if (ts->tv_nsec == 0)
481 			ts->tv_nsec = 1000;
482 		break;
483 	default:
484 		if ((int)clock_id < 0)
485 			goto cputime;
486 		return (EINVAL);
487 	}
488 	return (0);
489 }
490 
491 int
492 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
493 {
494 
495 	return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt,
496 	    rmt));
497 }
498 
499 static uint8_t nanowait[MAXCPU];
500 
501 int
502 kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
503     const struct timespec *rqt, struct timespec *rmt)
504 {
505 	struct timespec ts, now;
506 	sbintime_t sbt, sbtt, prec, tmp;
507 	time_t over;
508 	int error;
509 	bool is_abs_real;
510 
511 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
512 		return (EINVAL);
513 	if ((flags & ~TIMER_ABSTIME) != 0)
514 		return (EINVAL);
515 	switch (clock_id) {
516 	case CLOCK_REALTIME:
517 	case CLOCK_REALTIME_PRECISE:
518 	case CLOCK_REALTIME_FAST:
519 	case CLOCK_SECOND:
520 		is_abs_real = (flags & TIMER_ABSTIME) != 0;
521 		break;
522 	case CLOCK_MONOTONIC:
523 	case CLOCK_MONOTONIC_PRECISE:
524 	case CLOCK_MONOTONIC_FAST:
525 	case CLOCK_UPTIME:
526 	case CLOCK_UPTIME_PRECISE:
527 	case CLOCK_UPTIME_FAST:
528 		is_abs_real = false;
529 		break;
530 	case CLOCK_VIRTUAL:
531 	case CLOCK_PROF:
532 	case CLOCK_PROCESS_CPUTIME_ID:
533 		return (ENOTSUP);
534 	case CLOCK_THREAD_CPUTIME_ID:
535 	default:
536 		return (EINVAL);
537 	}
538 	do {
539 		ts = *rqt;
540 		if ((flags & TIMER_ABSTIME) != 0) {
541 			if (is_abs_real)
542 				td->td_rtcgen =
543 				    atomic_load_acq_int(&rtc_generation);
544 			error = kern_clock_gettime(td, clock_id, &now);
545 			KASSERT(error == 0, ("kern_clock_gettime: %d", error));
546 			timespecsub(&ts, &now, &ts);
547 		}
548 		if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
549 			error = EWOULDBLOCK;
550 			break;
551 		}
552 		if (ts.tv_sec > INT32_MAX / 2) {
553 			over = ts.tv_sec - INT32_MAX / 2;
554 			ts.tv_sec -= over;
555 		} else
556 			over = 0;
557 		tmp = tstosbt(ts);
558 		prec = tmp;
559 		prec >>= tc_precexp;
560 		if (TIMESEL(&sbt, tmp))
561 			sbt += tc_tick_sbt;
562 		sbt += tmp;
563 		error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
564 		    sbt, prec, C_ABSOLUTE);
565 	} while (error == 0 && is_abs_real && td->td_rtcgen == 0);
566 	td->td_rtcgen = 0;
567 	if (error != EWOULDBLOCK) {
568 		if (TIMESEL(&sbtt, tmp))
569 			sbtt += tc_tick_sbt;
570 		if (sbtt >= sbt)
571 			return (0);
572 		if (error == ERESTART)
573 			error = EINTR;
574 		if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) {
575 			ts = sbttots(sbt - sbtt);
576 			ts.tv_sec += over;
577 			if (ts.tv_sec < 0)
578 				timespecclear(&ts);
579 			*rmt = ts;
580 		}
581 		return (error);
582 	}
583 	return (0);
584 }
585 
586 #ifndef _SYS_SYSPROTO_H_
587 struct nanosleep_args {
588 	struct	timespec *rqtp;
589 	struct	timespec *rmtp;
590 };
591 #endif
592 /* ARGSUSED */
593 int
594 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
595 {
596 
597 	return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME,
598 	    uap->rqtp, uap->rmtp));
599 }
600 
601 #ifndef _SYS_SYSPROTO_H_
602 struct clock_nanosleep_args {
603 	clockid_t clock_id;
604 	int 	  flags;
605 	struct	timespec *rqtp;
606 	struct	timespec *rmtp;
607 };
608 #endif
609 /* ARGSUSED */
610 int
611 sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap)
612 {
613 	int error;
614 
615 	error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp,
616 	    uap->rmtp);
617 	return (kern_posix_error(td, error));
618 }
619 
620 static int
621 user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
622     const struct timespec *ua_rqtp, struct timespec *ua_rmtp)
623 {
624 	struct timespec rmt, rqt;
625 	int error, error2;
626 
627 	error = copyin(ua_rqtp, &rqt, sizeof(rqt));
628 	if (error)
629 		return (error);
630 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
631 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
632 		error2 = copyout(&rmt, ua_rmtp, sizeof(rmt));
633 		if (error2 != 0)
634 			error = error2;
635 	}
636 	return (error);
637 }
638 
639 #ifndef _SYS_SYSPROTO_H_
640 struct gettimeofday_args {
641 	struct	timeval *tp;
642 	struct	timezone *tzp;
643 };
644 #endif
645 /* ARGSUSED */
646 int
647 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
648 {
649 	struct timeval atv;
650 	struct timezone rtz;
651 	int error = 0;
652 
653 	if (uap->tp) {
654 		microtime(&atv);
655 		error = copyout(&atv, uap->tp, sizeof (atv));
656 	}
657 	if (error == 0 && uap->tzp != NULL) {
658 		rtz.tz_minuteswest = 0;
659 		rtz.tz_dsttime = 0;
660 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
661 	}
662 	return (error);
663 }
664 
665 #ifndef _SYS_SYSPROTO_H_
666 struct settimeofday_args {
667 	struct	timeval *tv;
668 	struct	timezone *tzp;
669 };
670 #endif
671 /* ARGSUSED */
672 int
673 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
674 {
675 	struct timeval atv, *tvp;
676 	struct timezone atz, *tzp;
677 	int error;
678 
679 	if (uap->tv) {
680 		error = copyin(uap->tv, &atv, sizeof(atv));
681 		if (error)
682 			return (error);
683 		tvp = &atv;
684 	} else
685 		tvp = NULL;
686 	if (uap->tzp) {
687 		error = copyin(uap->tzp, &atz, sizeof(atz));
688 		if (error)
689 			return (error);
690 		tzp = &atz;
691 	} else
692 		tzp = NULL;
693 	return (kern_settimeofday(td, tvp, tzp));
694 }
695 
696 int
697 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
698 {
699 	int error;
700 
701 	error = priv_check(td, PRIV_SETTIMEOFDAY);
702 	if (error)
703 		return (error);
704 	/* Verify all parameters before changing time. */
705 	if (tv) {
706 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
707 		    tv->tv_sec < 0)
708 			return (EINVAL);
709 		error = settime(td, tv);
710 	}
711 	return (error);
712 }
713 
714 /*
715  * Get value of an interval timer.  The process virtual and profiling virtual
716  * time timers are kept in the p_stats area, since they can be swapped out.
717  * These are kept internally in the way they are specified externally: in
718  * time until they expire.
719  *
720  * The real time interval timer is kept in the process table slot for the
721  * process, and its value (it_value) is kept as an absolute time rather than
722  * as a delta, so that it is easy to keep periodic real-time signals from
723  * drifting.
724  *
725  * Virtual time timers are processed in the hardclock() routine of
726  * kern_clock.c.  The real time timer is processed by a timeout routine,
727  * called from the softclock() routine.  Since a callout may be delayed in
728  * real time due to interrupt processing in the system, it is possible for
729  * the real time timeout routine (realitexpire, given below), to be delayed
730  * in real time past when it is supposed to occur.  It does not suffice,
731  * therefore, to reload the real timer .it_value from the real time timers
732  * .it_interval.  Rather, we compute the next time in absolute time the timer
733  * should go off.
734  */
735 #ifndef _SYS_SYSPROTO_H_
736 struct getitimer_args {
737 	u_int	which;
738 	struct	itimerval *itv;
739 };
740 #endif
741 int
742 sys_getitimer(struct thread *td, struct getitimer_args *uap)
743 {
744 	struct itimerval aitv;
745 	int error;
746 
747 	error = kern_getitimer(td, uap->which, &aitv);
748 	if (error != 0)
749 		return (error);
750 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
751 }
752 
753 int
754 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
755 {
756 	struct proc *p = td->td_proc;
757 	struct timeval ctv;
758 
759 	if (which > ITIMER_PROF)
760 		return (EINVAL);
761 
762 	if (which == ITIMER_REAL) {
763 		/*
764 		 * Convert from absolute to relative time in .it_value
765 		 * part of real time timer.  If time for real time timer
766 		 * has passed return 0, else return difference between
767 		 * current time and time for the timer to go off.
768 		 */
769 		PROC_LOCK(p);
770 		*aitv = p->p_realtimer;
771 		PROC_UNLOCK(p);
772 		if (timevalisset(&aitv->it_value)) {
773 			microuptime(&ctv);
774 			if (timevalcmp(&aitv->it_value, &ctv, <))
775 				timevalclear(&aitv->it_value);
776 			else
777 				timevalsub(&aitv->it_value, &ctv);
778 		}
779 	} else {
780 		PROC_ITIMLOCK(p);
781 		*aitv = p->p_stats->p_timer[which];
782 		PROC_ITIMUNLOCK(p);
783 	}
784 #ifdef KTRACE
785 	if (KTRPOINT(td, KTR_STRUCT))
786 		ktritimerval(aitv);
787 #endif
788 	return (0);
789 }
790 
791 #ifndef _SYS_SYSPROTO_H_
792 struct setitimer_args {
793 	u_int	which;
794 	struct	itimerval *itv, *oitv;
795 };
796 #endif
797 int
798 sys_setitimer(struct thread *td, struct setitimer_args *uap)
799 {
800 	struct itimerval aitv, oitv;
801 	int error;
802 
803 	if (uap->itv == NULL) {
804 		uap->itv = uap->oitv;
805 		return (sys_getitimer(td, (struct getitimer_args *)uap));
806 	}
807 
808 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
809 		return (error);
810 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
811 	if (error != 0 || uap->oitv == NULL)
812 		return (error);
813 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
814 }
815 
816 int
817 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
818     struct itimerval *oitv)
819 {
820 	struct proc *p = td->td_proc;
821 	struct timeval ctv;
822 	sbintime_t sbt, pr;
823 
824 	if (aitv == NULL)
825 		return (kern_getitimer(td, which, oitv));
826 
827 	if (which > ITIMER_PROF)
828 		return (EINVAL);
829 #ifdef KTRACE
830 	if (KTRPOINT(td, KTR_STRUCT))
831 		ktritimerval(aitv);
832 #endif
833 	if (itimerfix(&aitv->it_value) ||
834 	    aitv->it_value.tv_sec > INT32_MAX / 2)
835 		return (EINVAL);
836 	if (!timevalisset(&aitv->it_value))
837 		timevalclear(&aitv->it_interval);
838 	else if (itimerfix(&aitv->it_interval) ||
839 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
840 		return (EINVAL);
841 
842 	if (which == ITIMER_REAL) {
843 		PROC_LOCK(p);
844 		if (timevalisset(&p->p_realtimer.it_value))
845 			callout_stop(&p->p_itcallout);
846 		microuptime(&ctv);
847 		if (timevalisset(&aitv->it_value)) {
848 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
849 			timevaladd(&aitv->it_value, &ctv);
850 			sbt = tvtosbt(aitv->it_value);
851 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
852 			    realitexpire, p, C_ABSOLUTE);
853 		}
854 		*oitv = p->p_realtimer;
855 		p->p_realtimer = *aitv;
856 		PROC_UNLOCK(p);
857 		if (timevalisset(&oitv->it_value)) {
858 			if (timevalcmp(&oitv->it_value, &ctv, <))
859 				timevalclear(&oitv->it_value);
860 			else
861 				timevalsub(&oitv->it_value, &ctv);
862 		}
863 	} else {
864 		if (aitv->it_interval.tv_sec == 0 &&
865 		    aitv->it_interval.tv_usec != 0 &&
866 		    aitv->it_interval.tv_usec < tick)
867 			aitv->it_interval.tv_usec = tick;
868 		if (aitv->it_value.tv_sec == 0 &&
869 		    aitv->it_value.tv_usec != 0 &&
870 		    aitv->it_value.tv_usec < tick)
871 			aitv->it_value.tv_usec = tick;
872 		PROC_ITIMLOCK(p);
873 		*oitv = p->p_stats->p_timer[which];
874 		p->p_stats->p_timer[which] = *aitv;
875 		PROC_ITIMUNLOCK(p);
876 	}
877 #ifdef KTRACE
878 	if (KTRPOINT(td, KTR_STRUCT))
879 		ktritimerval(oitv);
880 #endif
881 	return (0);
882 }
883 
884 static void
885 realitexpire_reset_callout(struct proc *p, sbintime_t *isbtp)
886 {
887 	sbintime_t prec;
888 
889 	prec = isbtp == NULL ? tvtosbt(p->p_realtimer.it_interval) : *isbtp;
890 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
891 	    prec >> tc_precexp, realitexpire, p, C_ABSOLUTE);
892 }
893 
894 void
895 itimer_proc_continue(struct proc *p)
896 {
897 	struct timeval ctv;
898 	struct itimer *it;
899 	int id;
900 
901 	PROC_LOCK_ASSERT(p, MA_OWNED);
902 
903 	if ((p->p_flag2 & P2_ITSTOPPED) != 0) {
904 		p->p_flag2 &= ~P2_ITSTOPPED;
905 		microuptime(&ctv);
906 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >=))
907 			realitexpire(p);
908 		else
909 			realitexpire_reset_callout(p, NULL);
910 	}
911 
912 	if (p->p_itimers != NULL) {
913 		for (id = 3; id < TIMER_MAX; id++) {
914 			it = p->p_itimers->its_timers[id];
915 			if (it == NULL)
916 				continue;
917 			if ((it->it_flags & ITF_PSTOPPED) != 0) {
918 				ITIMER_LOCK(it);
919 				if ((it->it_flags & ITF_PSTOPPED) != 0) {
920 					it->it_flags &= ~ITF_PSTOPPED;
921 					if ((it->it_flags & ITF_DELETING) == 0)
922 						realtimer_expire(it);
923 				}
924 				ITIMER_UNLOCK(it);
925 			}
926 		}
927 	}
928 }
929 
930 /*
931  * Real interval timer expired:
932  * send process whose timer expired an alarm signal.
933  * If time is not set up to reload, then just return.
934  * Else compute next time timer should go off which is > current time.
935  * This is where delay in processing this timeout causes multiple
936  * SIGALRM calls to be compressed into one.
937  * tvtohz() always adds 1 to allow for the time until the next clock
938  * interrupt being strictly less than 1 clock tick, but we don't want
939  * that here since we want to appear to be in sync with the clock
940  * interrupt even when we're delayed.
941  */
942 void
943 realitexpire(void *arg)
944 {
945 	struct proc *p;
946 	struct timeval ctv;
947 	sbintime_t isbt;
948 
949 	p = (struct proc *)arg;
950 	kern_psignal(p, SIGALRM);
951 	if (!timevalisset(&p->p_realtimer.it_interval)) {
952 		timevalclear(&p->p_realtimer.it_value);
953 		if (p->p_flag & P_WEXIT)
954 			wakeup(&p->p_itcallout);
955 		return;
956 	}
957 
958 	isbt = tvtosbt(p->p_realtimer.it_interval);
959 	if (isbt >= sbt_timethreshold)
960 		getmicrouptime(&ctv);
961 	else
962 		microuptime(&ctv);
963 	do {
964 		timevaladd(&p->p_realtimer.it_value,
965 		    &p->p_realtimer.it_interval);
966 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
967 
968 	if (P_SHOULDSTOP(p) || P_KILLED(p)) {
969 		p->p_flag2 |= P2_ITSTOPPED;
970 		return;
971 	}
972 
973 	p->p_flag2 &= ~P2_ITSTOPPED;
974 	realitexpire_reset_callout(p, &isbt);
975 }
976 
977 /*
978  * Check that a proposed value to load into the .it_value or
979  * .it_interval part of an interval timer is acceptable, and
980  * fix it to have at least minimal value (i.e. if it is less
981  * than the resolution of the clock, round it up.)
982  */
983 int
984 itimerfix(struct timeval *tv)
985 {
986 
987 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
988 		return (EINVAL);
989 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
990 	    tv->tv_usec < (u_int)tick / 16)
991 		tv->tv_usec = (u_int)tick / 16;
992 	return (0);
993 }
994 
995 /*
996  * Decrement an interval timer by a specified number
997  * of microseconds, which must be less than a second,
998  * i.e. < 1000000.  If the timer expires, then reload
999  * it.  In this case, carry over (usec - old value) to
1000  * reduce the value reloaded into the timer so that
1001  * the timer does not drift.  This routine assumes
1002  * that it is called in a context where the timers
1003  * on which it is operating cannot change in value.
1004  */
1005 int
1006 itimerdecr(struct itimerval *itp, int usec)
1007 {
1008 
1009 	if (itp->it_value.tv_usec < usec) {
1010 		if (itp->it_value.tv_sec == 0) {
1011 			/* expired, and already in next interval */
1012 			usec -= itp->it_value.tv_usec;
1013 			goto expire;
1014 		}
1015 		itp->it_value.tv_usec += 1000000;
1016 		itp->it_value.tv_sec--;
1017 	}
1018 	itp->it_value.tv_usec -= usec;
1019 	usec = 0;
1020 	if (timevalisset(&itp->it_value))
1021 		return (1);
1022 	/* expired, exactly at end of interval */
1023 expire:
1024 	if (timevalisset(&itp->it_interval)) {
1025 		itp->it_value = itp->it_interval;
1026 		itp->it_value.tv_usec -= usec;
1027 		if (itp->it_value.tv_usec < 0) {
1028 			itp->it_value.tv_usec += 1000000;
1029 			itp->it_value.tv_sec--;
1030 		}
1031 	} else
1032 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1033 	return (0);
1034 }
1035 
1036 /*
1037  * Add and subtract routines for timevals.
1038  * N.B.: subtract routine doesn't deal with
1039  * results which are before the beginning,
1040  * it just gets very confused in this case.
1041  * Caveat emptor.
1042  */
1043 void
1044 timevaladd(struct timeval *t1, const struct timeval *t2)
1045 {
1046 
1047 	t1->tv_sec += t2->tv_sec;
1048 	t1->tv_usec += t2->tv_usec;
1049 	timevalfix(t1);
1050 }
1051 
1052 void
1053 timevalsub(struct timeval *t1, const struct timeval *t2)
1054 {
1055 
1056 	t1->tv_sec -= t2->tv_sec;
1057 	t1->tv_usec -= t2->tv_usec;
1058 	timevalfix(t1);
1059 }
1060 
1061 static void
1062 timevalfix(struct timeval *t1)
1063 {
1064 
1065 	if (t1->tv_usec < 0) {
1066 		t1->tv_sec--;
1067 		t1->tv_usec += 1000000;
1068 	}
1069 	if (t1->tv_usec >= 1000000) {
1070 		t1->tv_sec++;
1071 		t1->tv_usec -= 1000000;
1072 	}
1073 }
1074 
1075 /*
1076  * ratecheck(): simple time-based rate-limit checking.
1077  */
1078 int
1079 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1080 {
1081 	struct timeval tv, delta;
1082 	int rv = 0;
1083 
1084 	getmicrouptime(&tv);		/* NB: 10ms precision */
1085 	delta = tv;
1086 	timevalsub(&delta, lasttime);
1087 
1088 	/*
1089 	 * check for 0,0 is so that the message will be seen at least once,
1090 	 * even if interval is huge.
1091 	 */
1092 	if (timevalcmp(&delta, mininterval, >=) ||
1093 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1094 		*lasttime = tv;
1095 		rv = 1;
1096 	}
1097 
1098 	return (rv);
1099 }
1100 
1101 /*
1102  * ppsratecheck(): packets (or events) per second limitation.
1103  *
1104  * Return 0 if the limit is to be enforced (e.g. the caller
1105  * should drop a packet because of the rate limitation).
1106  *
1107  * maxpps of 0 always causes zero to be returned.  maxpps of -1
1108  * always causes 1 to be returned; this effectively defeats rate
1109  * limiting.
1110  *
1111  * Note that we maintain the struct timeval for compatibility
1112  * with other bsd systems.  We reuse the storage and just monitor
1113  * clock ticks for minimal overhead.
1114  */
1115 int
1116 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1117 {
1118 	int now;
1119 
1120 	/*
1121 	 * Reset the last time and counter if this is the first call
1122 	 * or more than a second has passed since the last update of
1123 	 * lasttime.
1124 	 */
1125 	now = ticks;
1126 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1127 		lasttime->tv_sec = now;
1128 		*curpps = 1;
1129 		return (maxpps != 0);
1130 	} else {
1131 		(*curpps)++;		/* NB: ignore potential overflow */
1132 		return (maxpps < 0 || *curpps <= maxpps);
1133 	}
1134 }
1135 
1136 static void
1137 itimer_start(void)
1138 {
1139 	static const struct kclock rt_clock = {
1140 		.timer_create  = realtimer_create,
1141 		.timer_delete  = realtimer_delete,
1142 		.timer_settime = realtimer_settime,
1143 		.timer_gettime = realtimer_gettime,
1144 	};
1145 
1146 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
1147 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
1148 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
1149 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
1150 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
1151 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
1152 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1153 }
1154 
1155 static int
1156 register_posix_clock(int clockid, const struct kclock *clk)
1157 {
1158 	if ((unsigned)clockid >= MAX_CLOCKS) {
1159 		printf("%s: invalid clockid\n", __func__);
1160 		return (0);
1161 	}
1162 	posix_clocks[clockid] = *clk;
1163 	return (1);
1164 }
1165 
1166 static int
1167 itimer_init(void *mem, int size, int flags)
1168 {
1169 	struct itimer *it;
1170 
1171 	it = (struct itimer *)mem;
1172 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1173 	return (0);
1174 }
1175 
1176 static void
1177 itimer_fini(void *mem, int size)
1178 {
1179 	struct itimer *it;
1180 
1181 	it = (struct itimer *)mem;
1182 	mtx_destroy(&it->it_mtx);
1183 }
1184 
1185 static void
1186 itimer_enter(struct itimer *it)
1187 {
1188 
1189 	mtx_assert(&it->it_mtx, MA_OWNED);
1190 	it->it_usecount++;
1191 }
1192 
1193 static void
1194 itimer_leave(struct itimer *it)
1195 {
1196 
1197 	mtx_assert(&it->it_mtx, MA_OWNED);
1198 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1199 
1200 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1201 		wakeup(it);
1202 }
1203 
1204 #ifndef _SYS_SYSPROTO_H_
1205 struct ktimer_create_args {
1206 	clockid_t clock_id;
1207 	struct sigevent * evp;
1208 	int * timerid;
1209 };
1210 #endif
1211 int
1212 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1213 {
1214 	struct sigevent *evp, ev;
1215 	int id;
1216 	int error;
1217 
1218 	if (uap->evp == NULL) {
1219 		evp = NULL;
1220 	} else {
1221 		error = copyin(uap->evp, &ev, sizeof(ev));
1222 		if (error != 0)
1223 			return (error);
1224 		evp = &ev;
1225 	}
1226 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1227 	if (error == 0) {
1228 		error = copyout(&id, uap->timerid, sizeof(int));
1229 		if (error != 0)
1230 			kern_ktimer_delete(td, id);
1231 	}
1232 	return (error);
1233 }
1234 
1235 int
1236 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1237     int *timerid, int preset_id)
1238 {
1239 	struct proc *p = td->td_proc;
1240 	struct itimer *it;
1241 	int id;
1242 	int error;
1243 
1244 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1245 		return (EINVAL);
1246 
1247 	if (posix_clocks[clock_id].timer_create == NULL)
1248 		return (EINVAL);
1249 
1250 	if (evp != NULL) {
1251 		if (evp->sigev_notify != SIGEV_NONE &&
1252 		    evp->sigev_notify != SIGEV_SIGNAL &&
1253 		    evp->sigev_notify != SIGEV_THREAD_ID)
1254 			return (EINVAL);
1255 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
1256 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
1257 			!_SIG_VALID(evp->sigev_signo))
1258 			return (EINVAL);
1259 	}
1260 
1261 	if (p->p_itimers == NULL)
1262 		itimers_alloc(p);
1263 
1264 	it = uma_zalloc(itimer_zone, M_WAITOK);
1265 	it->it_flags = 0;
1266 	it->it_usecount = 0;
1267 	it->it_active = 0;
1268 	timespecclear(&it->it_time.it_value);
1269 	timespecclear(&it->it_time.it_interval);
1270 	it->it_overrun = 0;
1271 	it->it_overrun_last = 0;
1272 	it->it_clockid = clock_id;
1273 	it->it_timerid = -1;
1274 	it->it_proc = p;
1275 	ksiginfo_init(&it->it_ksi);
1276 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1277 	error = CLOCK_CALL(clock_id, timer_create, (it));
1278 	if (error != 0)
1279 		goto out;
1280 
1281 	PROC_LOCK(p);
1282 	if (preset_id != -1) {
1283 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1284 		id = preset_id;
1285 		if (p->p_itimers->its_timers[id] != NULL) {
1286 			PROC_UNLOCK(p);
1287 			error = 0;
1288 			goto out;
1289 		}
1290 	} else {
1291 		/*
1292 		 * Find a free timer slot, skipping those reserved
1293 		 * for setitimer().
1294 		 */
1295 		for (id = 3; id < TIMER_MAX; id++)
1296 			if (p->p_itimers->its_timers[id] == NULL)
1297 				break;
1298 		if (id == TIMER_MAX) {
1299 			PROC_UNLOCK(p);
1300 			error = EAGAIN;
1301 			goto out;
1302 		}
1303 	}
1304 	it->it_timerid = id;
1305 	p->p_itimers->its_timers[id] = it;
1306 	if (evp != NULL)
1307 		it->it_sigev = *evp;
1308 	else {
1309 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1310 		switch (clock_id) {
1311 		default:
1312 		case CLOCK_REALTIME:
1313 			it->it_sigev.sigev_signo = SIGALRM;
1314 			break;
1315 		case CLOCK_VIRTUAL:
1316  			it->it_sigev.sigev_signo = SIGVTALRM;
1317 			break;
1318 		case CLOCK_PROF:
1319 			it->it_sigev.sigev_signo = SIGPROF;
1320 			break;
1321 		}
1322 		it->it_sigev.sigev_value.sival_int = id;
1323 	}
1324 
1325 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1326 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1327 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1328 		it->it_ksi.ksi_code = SI_TIMER;
1329 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1330 		it->it_ksi.ksi_timerid = id;
1331 	}
1332 	PROC_UNLOCK(p);
1333 	*timerid = id;
1334 	return (0);
1335 
1336 out:
1337 	ITIMER_LOCK(it);
1338 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
1339 	ITIMER_UNLOCK(it);
1340 	uma_zfree(itimer_zone, it);
1341 	return (error);
1342 }
1343 
1344 #ifndef _SYS_SYSPROTO_H_
1345 struct ktimer_delete_args {
1346 	int timerid;
1347 };
1348 #endif
1349 int
1350 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1351 {
1352 
1353 	return (kern_ktimer_delete(td, uap->timerid));
1354 }
1355 
1356 static struct itimer *
1357 itimer_find(struct proc *p, int timerid)
1358 {
1359 	struct itimer *it;
1360 
1361 	PROC_LOCK_ASSERT(p, MA_OWNED);
1362 	if ((p->p_itimers == NULL) ||
1363 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
1364 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
1365 		return (NULL);
1366 	}
1367 	ITIMER_LOCK(it);
1368 	if ((it->it_flags & ITF_DELETING) != 0) {
1369 		ITIMER_UNLOCK(it);
1370 		it = NULL;
1371 	}
1372 	return (it);
1373 }
1374 
1375 int
1376 kern_ktimer_delete(struct thread *td, int timerid)
1377 {
1378 	struct proc *p = td->td_proc;
1379 	struct itimer *it;
1380 
1381 	PROC_LOCK(p);
1382 	it = itimer_find(p, timerid);
1383 	if (it == NULL) {
1384 		PROC_UNLOCK(p);
1385 		return (EINVAL);
1386 	}
1387 	PROC_UNLOCK(p);
1388 
1389 	it->it_flags |= ITF_DELETING;
1390 	while (it->it_usecount > 0) {
1391 		it->it_flags |= ITF_WANTED;
1392 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1393 	}
1394 	it->it_flags &= ~ITF_WANTED;
1395 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
1396 	ITIMER_UNLOCK(it);
1397 
1398 	PROC_LOCK(p);
1399 	if (KSI_ONQ(&it->it_ksi))
1400 		sigqueue_take(&it->it_ksi);
1401 	p->p_itimers->its_timers[timerid] = NULL;
1402 	PROC_UNLOCK(p);
1403 	uma_zfree(itimer_zone, it);
1404 	return (0);
1405 }
1406 
1407 #ifndef _SYS_SYSPROTO_H_
1408 struct ktimer_settime_args {
1409 	int timerid;
1410 	int flags;
1411 	const struct itimerspec * value;
1412 	struct itimerspec * ovalue;
1413 };
1414 #endif
1415 int
1416 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1417 {
1418 	struct itimerspec val, oval, *ovalp;
1419 	int error;
1420 
1421 	error = copyin(uap->value, &val, sizeof(val));
1422 	if (error != 0)
1423 		return (error);
1424 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1425 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1426 	if (error == 0 && uap->ovalue != NULL)
1427 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1428 	return (error);
1429 }
1430 
1431 int
1432 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1433     struct itimerspec *val, struct itimerspec *oval)
1434 {
1435 	struct proc *p;
1436 	struct itimer *it;
1437 	int error;
1438 
1439 	p = td->td_proc;
1440 	PROC_LOCK(p);
1441 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1442 		PROC_UNLOCK(p);
1443 		error = EINVAL;
1444 	} else {
1445 		PROC_UNLOCK(p);
1446 		itimer_enter(it);
1447 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1448 		    flags, val, oval));
1449 		itimer_leave(it);
1450 		ITIMER_UNLOCK(it);
1451 	}
1452 	return (error);
1453 }
1454 
1455 #ifndef _SYS_SYSPROTO_H_
1456 struct ktimer_gettime_args {
1457 	int timerid;
1458 	struct itimerspec * value;
1459 };
1460 #endif
1461 int
1462 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1463 {
1464 	struct itimerspec val;
1465 	int error;
1466 
1467 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1468 	if (error == 0)
1469 		error = copyout(&val, uap->value, sizeof(val));
1470 	return (error);
1471 }
1472 
1473 int
1474 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1475 {
1476 	struct proc *p;
1477 	struct itimer *it;
1478 	int error;
1479 
1480 	p = td->td_proc;
1481 	PROC_LOCK(p);
1482 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1483 		PROC_UNLOCK(p);
1484 		error = EINVAL;
1485 	} else {
1486 		PROC_UNLOCK(p);
1487 		itimer_enter(it);
1488 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1489 		itimer_leave(it);
1490 		ITIMER_UNLOCK(it);
1491 	}
1492 	return (error);
1493 }
1494 
1495 #ifndef _SYS_SYSPROTO_H_
1496 struct timer_getoverrun_args {
1497 	int timerid;
1498 };
1499 #endif
1500 int
1501 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1502 {
1503 
1504 	return (kern_ktimer_getoverrun(td, uap->timerid));
1505 }
1506 
1507 int
1508 kern_ktimer_getoverrun(struct thread *td, int timer_id)
1509 {
1510 	struct proc *p = td->td_proc;
1511 	struct itimer *it;
1512 	int error ;
1513 
1514 	PROC_LOCK(p);
1515 	if (timer_id < 3 ||
1516 	    (it = itimer_find(p, timer_id)) == NULL) {
1517 		PROC_UNLOCK(p);
1518 		error = EINVAL;
1519 	} else {
1520 		td->td_retval[0] = it->it_overrun_last;
1521 		ITIMER_UNLOCK(it);
1522 		PROC_UNLOCK(p);
1523 		error = 0;
1524 	}
1525 	return (error);
1526 }
1527 
1528 static int
1529 realtimer_create(struct itimer *it)
1530 {
1531 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1532 	return (0);
1533 }
1534 
1535 static int
1536 realtimer_delete(struct itimer *it)
1537 {
1538 	mtx_assert(&it->it_mtx, MA_OWNED);
1539 
1540 	/*
1541 	 * clear timer's value and interval to tell realtimer_expire
1542 	 * to not rearm the timer.
1543 	 */
1544 	timespecclear(&it->it_time.it_value);
1545 	timespecclear(&it->it_time.it_interval);
1546 	ITIMER_UNLOCK(it);
1547 	callout_drain(&it->it_callout);
1548 	ITIMER_LOCK(it);
1549 	return (0);
1550 }
1551 
1552 static int
1553 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1554 {
1555 	struct timespec cts;
1556 
1557 	mtx_assert(&it->it_mtx, MA_OWNED);
1558 
1559 	realtimer_clocktime(it->it_clockid, &cts);
1560 	*ovalue = it->it_time;
1561 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1562 		timespecsub(&ovalue->it_value, &cts, &ovalue->it_value);
1563 		if (ovalue->it_value.tv_sec < 0 ||
1564 		    (ovalue->it_value.tv_sec == 0 &&
1565 		     ovalue->it_value.tv_nsec == 0)) {
1566 			ovalue->it_value.tv_sec  = 0;
1567 			ovalue->it_value.tv_nsec = 1;
1568 		}
1569 	}
1570 	return (0);
1571 }
1572 
1573 static int
1574 realtimer_settime(struct itimer *it, int flags, struct itimerspec *value,
1575     struct itimerspec *ovalue)
1576 {
1577 	struct timespec cts, ts;
1578 	struct timeval tv;
1579 	struct itimerspec val;
1580 
1581 	mtx_assert(&it->it_mtx, MA_OWNED);
1582 
1583 	val = *value;
1584 	if (itimespecfix(&val.it_value))
1585 		return (EINVAL);
1586 
1587 	if (timespecisset(&val.it_value)) {
1588 		if (itimespecfix(&val.it_interval))
1589 			return (EINVAL);
1590 	} else {
1591 		timespecclear(&val.it_interval);
1592 	}
1593 
1594 	if (ovalue != NULL)
1595 		realtimer_gettime(it, ovalue);
1596 
1597 	it->it_time = val;
1598 	if (timespecisset(&val.it_value)) {
1599 		realtimer_clocktime(it->it_clockid, &cts);
1600 		ts = val.it_value;
1601 		if ((flags & TIMER_ABSTIME) == 0) {
1602 			/* Convert to absolute time. */
1603 			timespecadd(&it->it_time.it_value, &cts,
1604 			    &it->it_time.it_value);
1605 		} else {
1606 			timespecsub(&ts, &cts, &ts);
1607 			/*
1608 			 * We don't care if ts is negative, tztohz will
1609 			 * fix it.
1610 			 */
1611 		}
1612 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
1613 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1614 		    it);
1615 	} else {
1616 		callout_stop(&it->it_callout);
1617 	}
1618 
1619 	return (0);
1620 }
1621 
1622 static void
1623 realtimer_clocktime(clockid_t id, struct timespec *ts)
1624 {
1625 	if (id == CLOCK_REALTIME)
1626 		getnanotime(ts);
1627 	else	/* CLOCK_MONOTONIC */
1628 		getnanouptime(ts);
1629 }
1630 
1631 int
1632 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1633 {
1634 	struct itimer *it;
1635 
1636 	PROC_LOCK_ASSERT(p, MA_OWNED);
1637 	it = itimer_find(p, timerid);
1638 	if (it != NULL) {
1639 		ksi->ksi_overrun = it->it_overrun;
1640 		it->it_overrun_last = it->it_overrun;
1641 		it->it_overrun = 0;
1642 		ITIMER_UNLOCK(it);
1643 		return (0);
1644 	}
1645 	return (EINVAL);
1646 }
1647 
1648 static int
1649 itimespecfix(struct timespec *ts)
1650 {
1651 
1652 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1653 		return (EINVAL);
1654 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1655 		ts->tv_nsec = tick * 1000;
1656 	return (0);
1657 }
1658 
1659 #define	timespectons(tsp)			\
1660 	((uint64_t)(tsp)->tv_sec * 1000000000 + (tsp)->tv_nsec)
1661 #define	timespecfromns(ns) (struct timespec){	\
1662 	.tv_sec = (ns) / 1000000000,		\
1663 	.tv_nsec = (ns) % 1000000000		\
1664 }
1665 
1666 /* Timeout callback for realtime timer */
1667 static void
1668 realtimer_expire(void *arg)
1669 {
1670 	struct timespec cts, ts;
1671 	struct timeval tv;
1672 	struct itimer *it;
1673 	struct proc *p;
1674 	uint64_t interval, now, overruns, value;
1675 
1676 	it = (struct itimer *)arg;
1677 
1678 	realtimer_clocktime(it->it_clockid, &cts);
1679 	/* Only fire if time is reached. */
1680 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1681 		if (timespecisset(&it->it_time.it_interval)) {
1682 			timespecadd(&it->it_time.it_value,
1683 			    &it->it_time.it_interval,
1684 			    &it->it_time.it_value);
1685 
1686 			interval = timespectons(&it->it_time.it_interval);
1687 			value = timespectons(&it->it_time.it_value);
1688 			now = timespectons(&cts);
1689 
1690 			if (now >= value) {
1691 				/*
1692 				 * We missed at least one period.
1693 				 */
1694 				overruns = howmany(now - value + 1, interval);
1695 				if (it->it_overrun + overruns >=
1696 				    it->it_overrun &&
1697 				    it->it_overrun + overruns <= INT_MAX) {
1698 					it->it_overrun += (int)overruns;
1699 				} else {
1700 					it->it_overrun = INT_MAX;
1701 					it->it_ksi.ksi_errno = ERANGE;
1702 				}
1703 				value =
1704 				    now + interval - (now - value) % interval;
1705 				it->it_time.it_value = timespecfromns(value);
1706 			}
1707 		} else {
1708 			/* single shot timer ? */
1709 			timespecclear(&it->it_time.it_value);
1710 		}
1711 		if (timespecisset(&it->it_time.it_value)) {
1712 			p = it->it_proc;
1713 			if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1714 				it->it_flags |= ITF_PSTOPPED;
1715 			} else {
1716 				timespecsub(&it->it_time.it_value, &cts, &ts);
1717 				TIMESPEC_TO_TIMEVAL(&tv, &ts);
1718 				callout_reset(&it->it_callout, tvtohz(&tv),
1719 				    realtimer_expire, it);
1720 			}
1721 		}
1722 		itimer_enter(it);
1723 		ITIMER_UNLOCK(it);
1724 		itimer_fire(it);
1725 		ITIMER_LOCK(it);
1726 		itimer_leave(it);
1727 	} else if (timespecisset(&it->it_time.it_value)) {
1728 		p = it->it_proc;
1729 		if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1730 			it->it_flags |= ITF_PSTOPPED;
1731 		} else {
1732 			ts = it->it_time.it_value;
1733 			timespecsub(&ts, &cts, &ts);
1734 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1735 			callout_reset(&it->it_callout, tvtohz(&tv),
1736 			    realtimer_expire, it);
1737 		}
1738 	}
1739 }
1740 
1741 static void
1742 itimer_fire(struct itimer *it)
1743 {
1744 	struct proc *p = it->it_proc;
1745 	struct thread *td;
1746 
1747 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1748 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1749 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1750 			ITIMER_LOCK(it);
1751 			timespecclear(&it->it_time.it_value);
1752 			timespecclear(&it->it_time.it_interval);
1753 			callout_stop(&it->it_callout);
1754 			ITIMER_UNLOCK(it);
1755 			return;
1756 		}
1757 		if (!KSI_ONQ(&it->it_ksi)) {
1758 			it->it_ksi.ksi_errno = 0;
1759 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1760 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1761 		} else {
1762 			if (it->it_overrun < INT_MAX)
1763 				it->it_overrun++;
1764 			else
1765 				it->it_ksi.ksi_errno = ERANGE;
1766 		}
1767 		PROC_UNLOCK(p);
1768 	}
1769 }
1770 
1771 static void
1772 itimers_alloc(struct proc *p)
1773 {
1774 	struct itimers *its;
1775 	int i;
1776 
1777 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1778 	LIST_INIT(&its->its_virtual);
1779 	LIST_INIT(&its->its_prof);
1780 	TAILQ_INIT(&its->its_worklist);
1781 	for (i = 0; i < TIMER_MAX; i++)
1782 		its->its_timers[i] = NULL;
1783 	PROC_LOCK(p);
1784 	if (p->p_itimers == NULL) {
1785 		p->p_itimers = its;
1786 		PROC_UNLOCK(p);
1787 	}
1788 	else {
1789 		PROC_UNLOCK(p);
1790 		free(its, M_SUBPROC);
1791 	}
1792 }
1793 
1794 /* Clean up timers when some process events are being triggered. */
1795 static void
1796 itimers_event_exit_exec(int start_idx, struct proc *p)
1797 {
1798 	struct itimers *its;
1799 	struct itimer *it;
1800 	int i;
1801 
1802 	its = p->p_itimers;
1803 	if (its == NULL)
1804 		return;
1805 
1806 	for (i = start_idx; i < TIMER_MAX; ++i) {
1807 		if ((it = its->its_timers[i]) != NULL)
1808 			kern_ktimer_delete(curthread, i);
1809 	}
1810 	if (its->its_timers[0] == NULL && its->its_timers[1] == NULL &&
1811 	    its->its_timers[2] == NULL) {
1812 		free(its, M_SUBPROC);
1813 		p->p_itimers = NULL;
1814 	}
1815 }
1816 
1817 void
1818 itimers_exec(struct proc *p)
1819 {
1820 	/*
1821 	 * According to susv3, XSI interval timers should be inherited
1822 	 * by new image.
1823 	 */
1824 	itimers_event_exit_exec(3, p);
1825 }
1826 
1827 void
1828 itimers_exit(struct proc *p)
1829 {
1830 	itimers_event_exit_exec(0, p);
1831 }
1832