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