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