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