xref: /freebsd/sys/kern/kern_time.c (revision 47ce3e5326118e6d72d425dd59262dc4c0f25db3)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35df8bae1dSRodney W. Grimes #include <sys/param.h>
36e96c1fdcSPeter Wemm #include <sys/systm.h>
37aff5bcb1SDavid Xu #include <sys/limits.h>
38f645b0b5SPoul-Henning Kamp #include <sys/clock.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42d26b1a1fSDavid Xu #include <sys/eventhandler.h>
43df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
44797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46098176f0SDavide Italiano #include <sys/sleepqueue.h>
47efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4877e718f7SDavid Xu #include <sys/sysctl.h>
4994c8fcd8SPeter Wemm #include <sys/sysent.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
526aeb05d7STom Rhodes #include <sys/posix4.h>
53708e7684SPeter Wemm #include <sys/time.h>
5486857b36SDavid Xu #include <sys/timers.h>
5591266b96SPoul-Henning Kamp #include <sys/timetc.h>
56df8bae1dSRodney W. Grimes #include <sys/vnode.h>
57fb919e4dSMark Murray 
585b870b7bSPeter Wemm #include <vm/vm.h>
595b870b7bSPeter Wemm #include <vm/vm_extern.h>
60df8bae1dSRodney W. Grimes 
6186857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
62d65f1abcSDavid Xu #define CPUCLOCK_BIT		0x80000000
63d65f1abcSDavid Xu #define CPUCLOCK_PROCESS_BIT	0x40000000
64d65f1abcSDavid Xu #define CPUCLOCK_ID_MASK	(~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
65d65f1abcSDavid Xu #define MAKE_THREAD_CPUCLOCK(tid)	(CPUCLOCK_BIT|(tid))
66d65f1abcSDavid Xu #define MAKE_PROCESS_CPUCLOCK(pid)	\
67d65f1abcSDavid Xu 	(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
6886857b36SDavid Xu 
6986857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
7086857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
7186857b36SDavid Xu 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
74df8bae1dSRodney W. Grimes  *
75df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
76df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
77df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
78df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
79df8bae1dSRodney W. Grimes  * timers when they expire.
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes 
827edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
834d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
8494c8fcd8SPeter Wemm 
8586857b36SDavid Xu static void	itimer_start(void);
8686857b36SDavid Xu static int	itimer_init(void *, int, int);
8786857b36SDavid Xu static void	itimer_fini(void *, int);
8886857b36SDavid Xu static void	itimer_enter(struct itimer *);
8986857b36SDavid Xu static void	itimer_leave(struct itimer *);
90843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int);
9186857b36SDavid Xu static void	itimers_alloc(struct proc *);
92993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
93993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
9486857b36SDavid Xu static int	realtimer_create(struct itimer *);
9586857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
9686857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9786857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9886857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9956c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
10086857b36SDavid Xu static void	realtimer_expire(void *);
10186857b36SDavid Xu 
10286857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
10386857b36SDavid Xu void		itimer_fire(struct itimer *it);
10456c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10586857b36SDavid Xu 
10686857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
10786857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
10886857b36SDavid Xu 
10986857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
11086857b36SDavid Xu 
11186857b36SDavid Xu 
11294c8fcd8SPeter Wemm static int
11391afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
11494c8fcd8SPeter Wemm {
115fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
116c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1177ec73f64SPoul-Henning Kamp 	struct timespec ts;
11894c8fcd8SPeter Wemm 	int s;
11994c8fcd8SPeter Wemm 
120708e7684SPeter Wemm 	s = splclock();
1219c8fff87SBruce Evans 	microtime(&tv1);
12200af9731SPoul-Henning Kamp 	delta = *tv;
12300af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
12494c8fcd8SPeter Wemm 
12594c8fcd8SPeter Wemm 	/*
1269c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
127fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
128fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
129fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
130fcae3aa6SNick Sayer 	 * back to the past.
131c0bd94a7SNick Sayer 	 *
132c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
133c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
134c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
13594c8fcd8SPeter Wemm 	 */
1367edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
137fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1383f92429aSMatt Jacob 			/*
139c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1403f92429aSMatt Jacob 			 */
141fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
142fcae3aa6SNick Sayer 				maxtime = tv1;
143fcae3aa6SNick Sayer 			tv2 = *tv;
144fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
145fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1463f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
147fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
148fcae3aa6SNick Sayer 			}
1493f92429aSMatt Jacob 		} else {
150c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
151c0bd94a7SNick Sayer 				splx(s);
152c0bd94a7SNick Sayer 				return (EPERM);
153c0bd94a7SNick Sayer 			}
154c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
155c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
156c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
157c0bd94a7SNick Sayer 			}
158c0bd94a7SNick Sayer 			laststep = *tv;
159fcae3aa6SNick Sayer 		}
1609c8fff87SBruce Evans 	}
1619c8fff87SBruce Evans 
1627ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1637ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1647edfb592SJohn Baldwin 	mtx_lock(&Giant);
16591266b96SPoul-Henning Kamp 	tc_setclock(&ts);
16694c8fcd8SPeter Wemm 	resettodr();
1677edfb592SJohn Baldwin 	mtx_unlock(&Giant);
16894c8fcd8SPeter Wemm 	return (0);
16994c8fcd8SPeter Wemm }
17094c8fcd8SPeter Wemm 
17194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
172d65f1abcSDavid Xu struct clock_getcpuclockid2_args {
173d65f1abcSDavid Xu 	id_t id;
174d65f1abcSDavid Xu 	int which,
175d65f1abcSDavid Xu 	clockid_t *clock_id;
176d65f1abcSDavid Xu };
177d65f1abcSDavid Xu #endif
178d65f1abcSDavid Xu /* ARGSUSED */
179d65f1abcSDavid Xu int
180d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
181d65f1abcSDavid Xu {
182d65f1abcSDavid Xu 	clockid_t clk_id;
183d31e4b3aSKonstantin Belousov 	int error;
184d31e4b3aSKonstantin Belousov 
185d31e4b3aSKonstantin Belousov 	error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
186d31e4b3aSKonstantin Belousov 	if (error == 0)
187d31e4b3aSKonstantin Belousov 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
188d31e4b3aSKonstantin Belousov 	return (error);
189d31e4b3aSKonstantin Belousov }
190d31e4b3aSKonstantin Belousov 
191d31e4b3aSKonstantin Belousov int
192d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
193d31e4b3aSKonstantin Belousov     clockid_t *clk_id)
194d31e4b3aSKonstantin Belousov {
195d65f1abcSDavid Xu 	struct proc *p;
196d65f1abcSDavid Xu 	pid_t pid;
197d65f1abcSDavid Xu 	lwpid_t tid;
198d65f1abcSDavid Xu 	int error;
199d65f1abcSDavid Xu 
200d31e4b3aSKonstantin Belousov 	switch (which) {
201d65f1abcSDavid Xu 	case CPUCLOCK_WHICH_PID:
202d31e4b3aSKonstantin Belousov 		if (id != 0) {
203*47ce3e53SDmitry Chagin 			error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
204d31e4b3aSKonstantin Belousov 			if (error != 0)
205d65f1abcSDavid Xu 				return (error);
206*47ce3e53SDmitry Chagin 			PROC_UNLOCK(p);
207d31e4b3aSKonstantin Belousov 			pid = id;
208d65f1abcSDavid Xu 		} else {
209d65f1abcSDavid Xu 			pid = td->td_proc->p_pid;
210d65f1abcSDavid Xu 		}
211d31e4b3aSKonstantin Belousov 		*clk_id = MAKE_PROCESS_CPUCLOCK(pid);
212d31e4b3aSKonstantin Belousov 		return (0);
213d65f1abcSDavid Xu 	case CPUCLOCK_WHICH_TID:
214d31e4b3aSKonstantin Belousov 		tid = id == 0 ? td->td_tid : id;
215d31e4b3aSKonstantin Belousov 		*clk_id = MAKE_THREAD_CPUCLOCK(tid);
216d31e4b3aSKonstantin Belousov 		return (0);
217d65f1abcSDavid Xu 	default:
218d65f1abcSDavid Xu 		return (EINVAL);
219d65f1abcSDavid Xu 	}
220d65f1abcSDavid Xu }
221d65f1abcSDavid Xu 
222d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_
22394c8fcd8SPeter Wemm struct clock_gettime_args {
22494c8fcd8SPeter Wemm 	clockid_t clock_id;
22594c8fcd8SPeter Wemm 	struct	timespec *tp;
22694c8fcd8SPeter Wemm };
22794c8fcd8SPeter Wemm #endif
22894c8fcd8SPeter Wemm /* ARGSUSED */
22994c8fcd8SPeter Wemm int
2308451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
23194c8fcd8SPeter Wemm {
23294c8fcd8SPeter Wemm 	struct timespec ats;
233f0b479cdSPaul Saab 	int error;
234f0b479cdSPaul Saab 
235f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
236f0b479cdSPaul Saab 	if (error == 0)
237f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
238f0b479cdSPaul Saab 
239f0b479cdSPaul Saab 	return (error);
240f0b479cdSPaul Saab }
241f0b479cdSPaul Saab 
242d65f1abcSDavid Xu static inline void
243d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats)
244d65f1abcSDavid Xu {
245d65f1abcSDavid Xu 	runtime = cputick2usec(runtime);
246d65f1abcSDavid Xu 	ats->tv_sec = runtime / 1000000;
247d65f1abcSDavid Xu 	ats->tv_nsec = runtime % 1000000 * 1000;
248d65f1abcSDavid Xu }
249d65f1abcSDavid Xu 
250d65f1abcSDavid Xu static void
251d65f1abcSDavid Xu get_thread_cputime(struct thread *targettd, struct timespec *ats)
252d65f1abcSDavid Xu {
253d65f1abcSDavid Xu 	uint64_t runtime, curtime, switchtime;
254d65f1abcSDavid Xu 
255d65f1abcSDavid Xu 	if (targettd == NULL) { /* current thread */
256d65f1abcSDavid Xu 		critical_enter();
257d65f1abcSDavid Xu 		switchtime = PCPU_GET(switchtime);
258d65f1abcSDavid Xu 		curtime = cpu_ticks();
259d65f1abcSDavid Xu 		runtime = curthread->td_runtime;
260d65f1abcSDavid Xu 		critical_exit();
261d65f1abcSDavid Xu 		runtime += curtime - switchtime;
262d65f1abcSDavid Xu 	} else {
263d65f1abcSDavid Xu 		thread_lock(targettd);
264d65f1abcSDavid Xu 		runtime = targettd->td_runtime;
265d65f1abcSDavid Xu 		thread_unlock(targettd);
266d65f1abcSDavid Xu 	}
267d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
268d65f1abcSDavid Xu }
269d65f1abcSDavid Xu 
270d65f1abcSDavid Xu static void
271d65f1abcSDavid Xu get_process_cputime(struct proc *targetp, struct timespec *ats)
272d65f1abcSDavid Xu {
273d65f1abcSDavid Xu 	uint64_t runtime;
274d65f1abcSDavid Xu 	struct rusage ru;
275d65f1abcSDavid Xu 
2765c7bebf9SKonstantin Belousov 	PROC_STATLOCK(targetp);
277d65f1abcSDavid Xu 	rufetch(targetp, &ru);
278d65f1abcSDavid Xu 	runtime = targetp->p_rux.rux_runtime;
2795c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(targetp);
280d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
281d65f1abcSDavid Xu }
282d65f1abcSDavid Xu 
283d65f1abcSDavid Xu static int
284d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
285d65f1abcSDavid Xu {
286d65f1abcSDavid Xu 	struct proc *p, *p2;
287d65f1abcSDavid Xu 	struct thread *td2;
288d65f1abcSDavid Xu 	lwpid_t tid;
289d65f1abcSDavid Xu 	pid_t pid;
290d65f1abcSDavid Xu 	int error;
291d65f1abcSDavid Xu 
292d65f1abcSDavid Xu 	p = td->td_proc;
293d65f1abcSDavid Xu 	if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
294d65f1abcSDavid Xu 		tid = clock_id & CPUCLOCK_ID_MASK;
295d65f1abcSDavid Xu 		td2 = tdfind(tid, p->p_pid);
296d65f1abcSDavid Xu 		if (td2 == NULL)
297d65f1abcSDavid Xu 			return (EINVAL);
298d65f1abcSDavid Xu 		get_thread_cputime(td2, ats);
299d65f1abcSDavid Xu 		PROC_UNLOCK(td2->td_proc);
300d65f1abcSDavid Xu 	} else {
301d65f1abcSDavid Xu 		pid = clock_id & CPUCLOCK_ID_MASK;
302c7c536c7SKonstantin Belousov 		error = pget(pid, PGET_CANSEE, &p2);
303c7c536c7SKonstantin Belousov 		if (error != 0)
304d65f1abcSDavid Xu 			return (EINVAL);
305d65f1abcSDavid Xu 		get_process_cputime(p2, ats);
306d65f1abcSDavid Xu 		PROC_UNLOCK(p2);
307d65f1abcSDavid Xu 	}
308d65f1abcSDavid Xu 	return (0);
309d65f1abcSDavid Xu }
310d65f1abcSDavid Xu 
311f0b479cdSPaul Saab int
312f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
313f0b479cdSPaul Saab {
314de0a9241SKelly Yancey 	struct timeval sys, user;
31578c85e8dSJohn Baldwin 	struct proc *p;
31694c8fcd8SPeter Wemm 
31778c85e8dSJohn Baldwin 	p = td->td_proc;
318f0b479cdSPaul Saab 	switch (clock_id) {
3195e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
3205e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
321f0b479cdSPaul Saab 		nanotime(ats);
322b8817154SKelly Yancey 		break;
3235e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3245e758b95SRobert Watson 		getnanotime(ats);
3255e758b95SRobert Watson 		break;
326b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
32778c85e8dSJohn Baldwin 		PROC_LOCK(p);
3285c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
32978c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3305c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
33178c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
332f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
333b8817154SKelly Yancey 		break;
334b8817154SKelly Yancey 	case CLOCK_PROF:
33578c85e8dSJohn Baldwin 		PROC_LOCK(p);
3365c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
33778c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3385c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
33978c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
340de0a9241SKelly Yancey 		timevaladd(&user, &sys);
341f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
342de0a9241SKelly Yancey 		break;
3435e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
3445e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3455eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3465e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
347f0b479cdSPaul Saab 		nanouptime(ats);
348b8817154SKelly Yancey 		break;
3495e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3505e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3515e758b95SRobert Watson 		getnanouptime(ats);
3525e758b95SRobert Watson 		break;
3535e758b95SRobert Watson 	case CLOCK_SECOND:
3545e758b95SRobert Watson 		ats->tv_sec = time_second;
3555e758b95SRobert Watson 		ats->tv_nsec = 0;
3565e758b95SRobert Watson 		break;
35700d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
358d65f1abcSDavid Xu 		get_thread_cputime(NULL, ats);
359d65f1abcSDavid Xu 		break;
360d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
361d65f1abcSDavid Xu 		PROC_LOCK(p);
362d65f1abcSDavid Xu 		get_process_cputime(p, ats);
363d65f1abcSDavid Xu 		PROC_UNLOCK(p);
36400d6ac63SDavid Xu 		break;
365b8817154SKelly Yancey 	default:
366d65f1abcSDavid Xu 		if ((int)clock_id >= 0)
3675cb3dc8fSPoul-Henning Kamp 			return (EINVAL);
368d65f1abcSDavid Xu 		return (get_cputime(td, clock_id, ats));
369b8817154SKelly Yancey 	}
370f0b479cdSPaul Saab 	return (0);
37194c8fcd8SPeter Wemm }
37294c8fcd8SPeter Wemm 
37394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
37494c8fcd8SPeter Wemm struct clock_settime_args {
37594c8fcd8SPeter Wemm 	clockid_t clock_id;
37694c8fcd8SPeter Wemm 	const struct	timespec *tp;
37794c8fcd8SPeter Wemm };
37894c8fcd8SPeter Wemm #endif
37994c8fcd8SPeter Wemm /* ARGSUSED */
38094c8fcd8SPeter Wemm int
3818451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
38294c8fcd8SPeter Wemm {
38394c8fcd8SPeter Wemm 	struct timespec ats;
38494c8fcd8SPeter Wemm 	int error;
38594c8fcd8SPeter Wemm 
386f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
387f0b479cdSPaul Saab 		return (error);
388f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
389f0b479cdSPaul Saab }
390f0b479cdSPaul Saab 
391f0b479cdSPaul Saab int
392f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
393f0b479cdSPaul Saab {
394f0b479cdSPaul Saab 	struct timeval atv;
395f0b479cdSPaul Saab 	int error;
396f0b479cdSPaul Saab 
397acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
3987edfb592SJohn Baldwin 		return (error);
399f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
4007edfb592SJohn Baldwin 		return (EINVAL);
401f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
4027edfb592SJohn Baldwin 		return (EINVAL);
403a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
404f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
4057edfb592SJohn Baldwin 	error = settime(td, &atv);
40694c8fcd8SPeter Wemm 	return (error);
40794c8fcd8SPeter Wemm }
40894c8fcd8SPeter Wemm 
40994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
41094c8fcd8SPeter Wemm struct clock_getres_args {
41194c8fcd8SPeter Wemm 	clockid_t clock_id;
41294c8fcd8SPeter Wemm 	struct	timespec *tp;
41394c8fcd8SPeter Wemm };
41494c8fcd8SPeter Wemm #endif
41594c8fcd8SPeter Wemm int
4168451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
41794c8fcd8SPeter Wemm {
41894c8fcd8SPeter Wemm 	struct timespec ts;
419f0b479cdSPaul Saab 	int error;
42094c8fcd8SPeter Wemm 
421f0b479cdSPaul Saab 	if (uap->tp == NULL)
422f0b479cdSPaul Saab 		return (0);
423f0b479cdSPaul Saab 
424f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
425f0b479cdSPaul Saab 	if (error == 0)
426f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
427f0b479cdSPaul Saab 	return (error);
428f0b479cdSPaul Saab }
429f0b479cdSPaul Saab 
430f0b479cdSPaul Saab int
431f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
432f0b479cdSPaul Saab {
433f0b479cdSPaul Saab 
434f0b479cdSPaul Saab 	ts->tv_sec = 0;
435f0b479cdSPaul Saab 	switch (clock_id) {
436b8817154SKelly Yancey 	case CLOCK_REALTIME:
4375e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
4385e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
439b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
4405e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
4415e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
4425eefd889SAndre Oppermann 	case CLOCK_UPTIME:
4435e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
4445e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
445ac0653dcSBruce Evans 		/*
446ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
447ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
448ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
449ac0653dcSBruce Evans 		 */
450f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
451b8817154SKelly Yancey 		break;
452b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
453b8817154SKelly Yancey 	case CLOCK_PROF:
454b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
455f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
456b8817154SKelly Yancey 		break;
4575e758b95SRobert Watson 	case CLOCK_SECOND:
4585e758b95SRobert Watson 		ts->tv_sec = 1;
4595e758b95SRobert Watson 		ts->tv_nsec = 0;
4605e758b95SRobert Watson 		break;
46100d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
462d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
463d65f1abcSDavid Xu 	cputime:
46400d6ac63SDavid Xu 		/* sync with cputick2usec */
46500d6ac63SDavid Xu 		ts->tv_nsec = 1000000 / cpu_tickrate();
46600d6ac63SDavid Xu 		if (ts->tv_nsec == 0)
46700d6ac63SDavid Xu 			ts->tv_nsec = 1000;
46800d6ac63SDavid Xu 		break;
469b8817154SKelly Yancey 	default:
470d65f1abcSDavid Xu 		if ((int)clock_id < 0)
471d65f1abcSDavid Xu 			goto cputime;
472b8817154SKelly Yancey 		return (EINVAL);
47394c8fcd8SPeter Wemm 	}
474de0a9241SKelly Yancey 	return (0);
47594c8fcd8SPeter Wemm }
47694c8fcd8SPeter Wemm 
4770dbf17e6SAlexander Motin static uint8_t nanowait[MAXCPU];
47894c8fcd8SPeter Wemm 
4797fdf2c85SPaul Saab int
4807fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
4815b870b7bSPeter Wemm {
482098176f0SDavide Italiano 	struct timespec ts;
483098176f0SDavide Italiano 	sbintime_t sbt, sbtt, prec, tmp;
484980c545dSAlexander Motin 	time_t over;
48533841826SPoul-Henning Kamp 	int error;
4865b870b7bSPeter Wemm 
4877d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
488708e7684SPeter Wemm 		return (EINVAL);
489d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
4907d7fb492SBruce Evans 		return (0);
491980c545dSAlexander Motin 	ts = *rqt;
492980c545dSAlexander Motin 	if (ts.tv_sec > INT32_MAX / 2) {
493980c545dSAlexander Motin 		over = ts.tv_sec - INT32_MAX / 2;
494980c545dSAlexander Motin 		ts.tv_sec -= over;
495980c545dSAlexander Motin 	} else
496980c545dSAlexander Motin 		over = 0;
497980c545dSAlexander Motin 	tmp = tstosbt(ts);
498098176f0SDavide Italiano 	prec = tmp;
499098176f0SDavide Italiano 	prec >>= tc_precexp;
500098176f0SDavide Italiano 	if (TIMESEL(&sbt, tmp))
501098176f0SDavide Italiano 		sbt += tc_tick_sbt;
502098176f0SDavide Italiano 	sbt += tmp;
5030dbf17e6SAlexander Motin 	error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
5040dbf17e6SAlexander Motin 	    sbt, prec, C_ABSOLUTE);
50533841826SPoul-Henning Kamp 	if (error != EWOULDBLOCK) {
50633841826SPoul-Henning Kamp 		if (error == ERESTART)
50794c8fcd8SPeter Wemm 			error = EINTR;
508098176f0SDavide Italiano 		TIMESEL(&sbtt, tmp);
50933841826SPoul-Henning Kamp 		if (rmt != NULL) {
510098176f0SDavide Italiano 			ts = sbttots(sbt - sbtt);
511980c545dSAlexander Motin 			ts.tv_sec += over;
51233841826SPoul-Henning Kamp 			if (ts.tv_sec < 0)
51333841826SPoul-Henning Kamp 				timespecclear(&ts);
51400af9731SPoul-Henning Kamp 			*rmt = ts;
51500af9731SPoul-Henning Kamp 		}
516098176f0SDavide Italiano 		if (sbtt >= sbt)
517098176f0SDavide Italiano 			return (0);
51833841826SPoul-Henning Kamp 		return (error);
51933841826SPoul-Henning Kamp 	}
52033841826SPoul-Henning Kamp 	return (0);
5215b870b7bSPeter Wemm }
52294c8fcd8SPeter Wemm 
5235b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
5245b870b7bSPeter Wemm struct nanosleep_args {
5255b870b7bSPeter Wemm 	struct	timespec *rqtp;
5265b870b7bSPeter Wemm 	struct	timespec *rmtp;
5275b870b7bSPeter Wemm };
5285b870b7bSPeter Wemm #endif
5295b870b7bSPeter Wemm /* ARGSUSED */
5305b870b7bSPeter Wemm int
5318451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
5325b870b7bSPeter Wemm {
5335b870b7bSPeter Wemm 	struct timespec rmt, rqt;
534fb99ab88SMatthew Dillon 	int error;
5355b870b7bSPeter Wemm 
536d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
5375b870b7bSPeter Wemm 	if (error)
5385b870b7bSPeter Wemm 		return (error);
539fb99ab88SMatthew Dillon 
54031f3e2adSAlfred Perlstein 	if (uap->rmtp &&
54131f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
54231f3e2adSAlfred Perlstein 			return (EFAULT);
5437fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
544d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
545fb99ab88SMatthew Dillon 		int error2;
546fb99ab88SMatthew Dillon 
547d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
54831f3e2adSAlfred Perlstein 		if (error2)
549fb99ab88SMatthew Dillon 			error = error2;
550708e7684SPeter Wemm 	}
551708e7684SPeter Wemm 	return (error);
55294c8fcd8SPeter Wemm }
55394c8fcd8SPeter Wemm 
5545b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
555df8bae1dSRodney W. Grimes struct gettimeofday_args {
556df8bae1dSRodney W. Grimes 	struct	timeval *tp;
557df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
558df8bae1dSRodney W. Grimes };
559d2d3e875SBruce Evans #endif
560df8bae1dSRodney W. Grimes /* ARGSUSED */
56126f9a767SRodney W. Grimes int
5628451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
563df8bae1dSRodney W. Grimes {
564df8bae1dSRodney W. Grimes 	struct timeval atv;
565411c25edSTim J. Robbins 	struct timezone rtz;
566df8bae1dSRodney W. Grimes 	int error = 0;
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 	if (uap->tp) {
569df8bae1dSRodney W. Grimes 		microtime(&atv);
57001609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
571df8bae1dSRodney W. Grimes 	}
57221dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
57391f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
57491f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
575411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
57621dcdb38SPoul-Henning Kamp 	}
577df8bae1dSRodney W. Grimes 	return (error);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
580d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
581df8bae1dSRodney W. Grimes struct settimeofday_args {
582df8bae1dSRodney W. Grimes 	struct	timeval *tv;
583df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
584df8bae1dSRodney W. Grimes };
585d2d3e875SBruce Evans #endif
586df8bae1dSRodney W. Grimes /* ARGSUSED */
58726f9a767SRodney W. Grimes int
5888451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
589df8bae1dSRodney W. Grimes {
590b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
591b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
592b88ec951SJohn Baldwin 	int error;
593b88ec951SJohn Baldwin 
594b88ec951SJohn Baldwin 	if (uap->tv) {
595b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
596b88ec951SJohn Baldwin 		if (error)
597b88ec951SJohn Baldwin 			return (error);
598b88ec951SJohn Baldwin 		tvp = &atv;
599b88ec951SJohn Baldwin 	} else
600b88ec951SJohn Baldwin 		tvp = NULL;
601b88ec951SJohn Baldwin 	if (uap->tzp) {
602b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
603b88ec951SJohn Baldwin 		if (error)
604b88ec951SJohn Baldwin 			return (error);
605b88ec951SJohn Baldwin 		tzp = &atz;
606b88ec951SJohn Baldwin 	} else
607b88ec951SJohn Baldwin 		tzp = NULL;
608b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
609b88ec951SJohn Baldwin }
610b88ec951SJohn Baldwin 
611b88ec951SJohn Baldwin int
612b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
613b88ec951SJohn Baldwin {
614b88ec951SJohn Baldwin 	int error;
615fb99ab88SMatthew Dillon 
616acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
617b88ec951SJohn Baldwin 	if (error)
6187edfb592SJohn Baldwin 		return (error);
619df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
620b88ec951SJohn Baldwin 	if (tv) {
621b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
6227edfb592SJohn Baldwin 			return (EINVAL);
623b88ec951SJohn Baldwin 		error = settime(td, tv);
624708e7684SPeter Wemm 	}
625b88ec951SJohn Baldwin 	if (tzp && error == 0) {
626b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
627b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
628b88ec951SJohn Baldwin 	}
6297edfb592SJohn Baldwin 	return (error);
630b88ec951SJohn Baldwin }
6317edfb592SJohn Baldwin 
632df8bae1dSRodney W. Grimes /*
633873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
634873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
635873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
636873fbcd7SRobert Watson  * time until they expire.
637df8bae1dSRodney W. Grimes  *
638873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
639873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
640873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
641873fbcd7SRobert Watson  * drifting.
642df8bae1dSRodney W. Grimes  *
643df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
644873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
645873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
646873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
647873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
648873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
649873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
650873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
651873fbcd7SRobert Watson  * should go off.
652df8bae1dSRodney W. Grimes  */
653d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
654df8bae1dSRodney W. Grimes struct getitimer_args {
655df8bae1dSRodney W. Grimes 	u_int	which;
656df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
657df8bae1dSRodney W. Grimes };
658d2d3e875SBruce Evans #endif
65926f9a767SRodney W. Grimes int
6608451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap)
661df8bae1dSRodney W. Grimes {
662df8bae1dSRodney W. Grimes 	struct itimerval aitv;
663c90110d6SJohn Baldwin 	int error;
664df8bae1dSRodney W. Grimes 
665cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
666cfa0efe7SMaxim Sobolev 	if (error != 0)
667cfa0efe7SMaxim Sobolev 		return (error);
668cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
669cfa0efe7SMaxim Sobolev }
670cfa0efe7SMaxim Sobolev 
671cfa0efe7SMaxim Sobolev int
672cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
673cfa0efe7SMaxim Sobolev {
674cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
675cfa0efe7SMaxim Sobolev 	struct timeval ctv;
676cfa0efe7SMaxim Sobolev 
677cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
678df8bae1dSRodney W. Grimes 		return (EINVAL);
679fb99ab88SMatthew Dillon 
680cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
681df8bae1dSRodney W. Grimes 		/*
682ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
683df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
684df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
685df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
686df8bae1dSRodney W. Grimes 		 */
68796d7f8efSTim J. Robbins 		PROC_LOCK(p);
688cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
68996d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
690cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
691b5ea3779SAlexander Motin 			microuptime(&ctv);
692cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
693cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
694df8bae1dSRodney W. Grimes 			else
695cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
696227ee8a1SPoul-Henning Kamp 		}
697fb99ab88SMatthew Dillon 	} else {
6985c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
699cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
7005c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
701fb99ab88SMatthew Dillon 	}
702cfa0efe7SMaxim Sobolev 	return (0);
703df8bae1dSRodney W. Grimes }
704df8bae1dSRodney W. Grimes 
705d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
706df8bae1dSRodney W. Grimes struct setitimer_args {
707df8bae1dSRodney W. Grimes 	u_int	which;
708df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
709df8bae1dSRodney W. Grimes };
710d2d3e875SBruce Evans #endif
71126f9a767SRodney W. Grimes int
7128451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap)
713df8bae1dSRodney W. Grimes {
714cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
715c90110d6SJohn Baldwin 	int error;
71696d7f8efSTim J. Robbins 
71796d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
71896d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
7198451d0ddSKip Macy 		return (sys_getitimer(td, (struct getitimer_args *)uap));
72096d7f8efSTim J. Robbins 	}
721df8bae1dSRodney W. Grimes 
72296d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
723df8bae1dSRodney W. Grimes 		return (error);
724cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
725cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
726cfa0efe7SMaxim Sobolev 		return (error);
727cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
728cfa0efe7SMaxim Sobolev }
729cfa0efe7SMaxim Sobolev 
730cfa0efe7SMaxim Sobolev int
731c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
732c90110d6SJohn Baldwin     struct itimerval *oitv)
733cfa0efe7SMaxim Sobolev {
734cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
735cfa0efe7SMaxim Sobolev 	struct timeval ctv;
736b5ea3779SAlexander Motin 	sbintime_t sbt, pr;
737cfa0efe7SMaxim Sobolev 
7385e85ac17SJohn Baldwin 	if (aitv == NULL)
7395e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
7405e85ac17SJohn Baldwin 
741cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
74296d7f8efSTim J. Robbins 		return (EINVAL);
743b5ea3779SAlexander Motin 	if (itimerfix(&aitv->it_value) ||
744b5ea3779SAlexander Motin 	    aitv->it_value.tv_sec > INT32_MAX / 2)
745cfa0efe7SMaxim Sobolev 		return (EINVAL);
746cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
747cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
748b5ea3779SAlexander Motin 	else if (itimerfix(&aitv->it_interval) ||
749b5ea3779SAlexander Motin 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
75096d7f8efSTim J. Robbins 		return (EINVAL);
75196d7f8efSTim J. Robbins 
752cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
75396d7f8efSTim J. Robbins 		PROC_LOCK(p);
7544cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
7554f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
756b5ea3779SAlexander Motin 		microuptime(&ctv);
757cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
758b5ea3779SAlexander Motin 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
759cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
760b5ea3779SAlexander Motin 			sbt = tvtosbt(aitv->it_value);
761b5ea3779SAlexander Motin 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
762b5ea3779SAlexander Motin 			    realitexpire, p, C_ABSOLUTE);
76325b4d3a8SJohn Baldwin 		}
764cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
765cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
76696d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
767cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
768cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
769cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
77096d7f8efSTim J. Robbins 			else
771cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
772fb99ab88SMatthew Dillon 		}
77396d7f8efSTim J. Robbins 	} else {
774d10a1df8SAlexander Motin 		if (aitv->it_interval.tv_sec == 0 &&
775d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec != 0 &&
776d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec < tick)
777d10a1df8SAlexander Motin 			aitv->it_interval.tv_usec = tick;
778d10a1df8SAlexander Motin 		if (aitv->it_value.tv_sec == 0 &&
779d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec != 0 &&
780d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec < tick)
781d10a1df8SAlexander Motin 			aitv->it_value.tv_usec = tick;
7825c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
783cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
784cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
7855c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
78696d7f8efSTim J. Robbins 	}
78796d7f8efSTim J. Robbins 	return (0);
788df8bae1dSRodney W. Grimes }
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes /*
791df8bae1dSRodney W. Grimes  * Real interval timer expired:
792df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
793df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
794df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
795df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
796df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
797c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
7989207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
7999207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
8009207f00aSBruce Evans  * interrupt even when we're delayed.
801df8bae1dSRodney W. Grimes  */
802df8bae1dSRodney W. Grimes void
80391afe087SPoul-Henning Kamp realitexpire(void *arg)
804df8bae1dSRodney W. Grimes {
80591afe087SPoul-Henning Kamp 	struct proc *p;
806b5ea3779SAlexander Motin 	struct timeval ctv;
807b5ea3779SAlexander Motin 	sbintime_t isbt;
808df8bae1dSRodney W. Grimes 
809df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
8108451d0ddSKip Macy 	kern_psignal(p, SIGALRM);
8114cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
8124cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
8135499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
8145499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
815df8bae1dSRodney W. Grimes 		return;
816df8bae1dSRodney W. Grimes 	}
817b5ea3779SAlexander Motin 	isbt = tvtosbt(p->p_realtimer.it_interval);
818b5ea3779SAlexander Motin 	if (isbt >= sbt_timethreshold)
819b5ea3779SAlexander Motin 		getmicrouptime(&ctv);
820b5ea3779SAlexander Motin 	else
821b5ea3779SAlexander Motin 		microuptime(&ctv);
822b5ea3779SAlexander Motin 	do {
823df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
824df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
825b5ea3779SAlexander Motin 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
826b5ea3779SAlexander Motin 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
827b5ea3779SAlexander Motin 	    isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
828df8bae1dSRodney W. Grimes }
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes /*
831df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
832df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
833df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
834df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
835df8bae1dSRodney W. Grimes  */
83626f9a767SRodney W. Grimes int
83791afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
838df8bae1dSRodney W. Grimes {
839df8bae1dSRodney W. Grimes 
84086857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
841df8bae1dSRodney W. Grimes 		return (EINVAL);
842b5ea3779SAlexander Motin 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
843b5ea3779SAlexander Motin 	    tv->tv_usec < (u_int)tick / 16)
844b5ea3779SAlexander Motin 		tv->tv_usec = (u_int)tick / 16;
845df8bae1dSRodney W. Grimes 	return (0);
846df8bae1dSRodney W. Grimes }
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes /*
849df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
850df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
851df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
852df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
853df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
854df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
855df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
856df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
857df8bae1dSRodney W. Grimes  */
85826f9a767SRodney W. Grimes int
85991afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
860df8bae1dSRodney W. Grimes {
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
863df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
864df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
865df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
866df8bae1dSRodney W. Grimes 			goto expire;
867df8bae1dSRodney W. Grimes 		}
868df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
869df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
870df8bae1dSRodney W. Grimes 	}
871df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
872df8bae1dSRodney W. Grimes 	usec = 0;
8734cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
874df8bae1dSRodney W. Grimes 		return (1);
875df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
876df8bae1dSRodney W. Grimes expire:
8774cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
878df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
879df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
880df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
881df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
882df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
883df8bae1dSRodney W. Grimes 		}
884df8bae1dSRodney W. Grimes 	} else
885df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
886df8bae1dSRodney W. Grimes 	return (0);
887df8bae1dSRodney W. Grimes }
888df8bae1dSRodney W. Grimes 
889df8bae1dSRodney W. Grimes /*
890df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
891df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
892df8bae1dSRodney W. Grimes  * results which are before the beginning,
893df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
894df8bae1dSRodney W. Grimes  * Caveat emptor.
895df8bae1dSRodney W. Grimes  */
89626f9a767SRodney W. Grimes void
8976ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
898df8bae1dSRodney W. Grimes {
899df8bae1dSRodney W. Grimes 
900df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
901df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
902df8bae1dSRodney W. Grimes 	timevalfix(t1);
903df8bae1dSRodney W. Grimes }
904df8bae1dSRodney W. Grimes 
90526f9a767SRodney W. Grimes void
9066ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
907df8bae1dSRodney W. Grimes {
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
910df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
911df8bae1dSRodney W. Grimes 	timevalfix(t1);
912df8bae1dSRodney W. Grimes }
913df8bae1dSRodney W. Grimes 
91487b6de2bSPoul-Henning Kamp static void
91591afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
916df8bae1dSRodney W. Grimes {
917df8bae1dSRodney W. Grimes 
918df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
919df8bae1dSRodney W. Grimes 		t1->tv_sec--;
920df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
921df8bae1dSRodney W. Grimes 	}
922df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
923df8bae1dSRodney W. Grimes 		t1->tv_sec++;
924df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
925df8bae1dSRodney W. Grimes 	}
926df8bae1dSRodney W. Grimes }
92791974ce1SSam Leffler 
92891974ce1SSam Leffler /*
929addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
93091974ce1SSam Leffler  */
93191974ce1SSam Leffler int
93291974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
93391974ce1SSam Leffler {
93491974ce1SSam Leffler 	struct timeval tv, delta;
93591974ce1SSam Leffler 	int rv = 0;
93691974ce1SSam Leffler 
937addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
938addea9d4SSam Leffler 	delta = tv;
939addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
94091974ce1SSam Leffler 
94191974ce1SSam Leffler 	/*
94291974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
94391974ce1SSam Leffler 	 * even if interval is huge.
94491974ce1SSam Leffler 	 */
94591974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
94691974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
94791974ce1SSam Leffler 		*lasttime = tv;
94891974ce1SSam Leffler 		rv = 1;
94991974ce1SSam Leffler 	}
95091974ce1SSam Leffler 
95191974ce1SSam Leffler 	return (rv);
95291974ce1SSam Leffler }
95391974ce1SSam Leffler 
95491974ce1SSam Leffler /*
95591974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
956addea9d4SSam Leffler  *
957addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
958addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
959addea9d4SSam Leffler  *
960893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
961893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
962893bec80SSam Leffler  * limiting.
963893bec80SSam Leffler  *
964addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
965addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
966addea9d4SSam Leffler  * clock ticks for minimal overhead.
96791974ce1SSam Leffler  */
96891974ce1SSam Leffler int
96991974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
97091974ce1SSam Leffler {
971addea9d4SSam Leffler 	int now;
97291974ce1SSam Leffler 
97391974ce1SSam Leffler 	/*
974addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
975addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
976addea9d4SSam Leffler 	 * lasttime.
97791974ce1SSam Leffler 	 */
978addea9d4SSam Leffler 	now = ticks;
979addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
980addea9d4SSam Leffler 		lasttime->tv_sec = now;
981addea9d4SSam Leffler 		*curpps = 1;
982893bec80SSam Leffler 		return (maxpps != 0);
983addea9d4SSam Leffler 	} else {
984addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
985addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
986addea9d4SSam Leffler 	}
98791974ce1SSam Leffler }
98886857b36SDavid Xu 
98986857b36SDavid Xu static void
99086857b36SDavid Xu itimer_start(void)
99186857b36SDavid Xu {
99286857b36SDavid Xu 	struct kclock rt_clock = {
99386857b36SDavid Xu 		.timer_create  = realtimer_create,
99486857b36SDavid Xu 		.timer_delete  = realtimer_delete,
99586857b36SDavid Xu 		.timer_settime = realtimer_settime,
99686857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
997843b99c6SDavid Xu 		.event_hook    = NULL
99886857b36SDavid Xu 	};
99986857b36SDavid Xu 
100086857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
100186857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
100286857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
100386857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
100477e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
100577e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
100677e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1007993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1008d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1009993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1010d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
101186857b36SDavid Xu }
101286857b36SDavid Xu 
101386857b36SDavid Xu int
101486857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
101586857b36SDavid Xu {
101686857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
101786857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
101886857b36SDavid Xu 		return (0);
101986857b36SDavid Xu 	}
102086857b36SDavid Xu 	posix_clocks[clockid] = *clk;
102186857b36SDavid Xu 	return (1);
102286857b36SDavid Xu }
102386857b36SDavid Xu 
102486857b36SDavid Xu static int
102586857b36SDavid Xu itimer_init(void *mem, int size, int flags)
102686857b36SDavid Xu {
102786857b36SDavid Xu 	struct itimer *it;
102886857b36SDavid Xu 
102986857b36SDavid Xu 	it = (struct itimer *)mem;
103086857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
103186857b36SDavid Xu 	return (0);
103286857b36SDavid Xu }
103386857b36SDavid Xu 
103486857b36SDavid Xu static void
103586857b36SDavid Xu itimer_fini(void *mem, int size)
103686857b36SDavid Xu {
103786857b36SDavid Xu 	struct itimer *it;
103886857b36SDavid Xu 
103986857b36SDavid Xu 	it = (struct itimer *)mem;
104086857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
104186857b36SDavid Xu }
104286857b36SDavid Xu 
104386857b36SDavid Xu static void
104486857b36SDavid Xu itimer_enter(struct itimer *it)
104586857b36SDavid Xu {
104686857b36SDavid Xu 
104786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
104886857b36SDavid Xu 	it->it_usecount++;
104986857b36SDavid Xu }
105086857b36SDavid Xu 
105186857b36SDavid Xu static void
105286857b36SDavid Xu itimer_leave(struct itimer *it)
105386857b36SDavid Xu {
105486857b36SDavid Xu 
105586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
105686857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
105786857b36SDavid Xu 
105886857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
105986857b36SDavid Xu 		wakeup(it);
106086857b36SDavid Xu }
106186857b36SDavid Xu 
106286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
106361d3a4efSDavid Xu struct ktimer_create_args {
106486857b36SDavid Xu 	clockid_t clock_id;
106586857b36SDavid Xu 	struct sigevent * evp;
106661d3a4efSDavid Xu 	int * timerid;
106786857b36SDavid Xu };
106886857b36SDavid Xu #endif
106986857b36SDavid Xu int
10708451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
107186857b36SDavid Xu {
1072643ee871SKonstantin Belousov 	struct sigevent *evp, ev;
107361d3a4efSDavid Xu 	int id;
107486857b36SDavid Xu 	int error;
107586857b36SDavid Xu 
1076643ee871SKonstantin Belousov 	if (uap->evp == NULL) {
1077643ee871SKonstantin Belousov 		evp = NULL;
1078643ee871SKonstantin Belousov 	} else {
107986857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
108086857b36SDavid Xu 		if (error != 0)
108186857b36SDavid Xu 			return (error);
1082643ee871SKonstantin Belousov 		evp = &ev;
1083643ee871SKonstantin Belousov 	}
1084643ee871SKonstantin Belousov 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
108586857b36SDavid Xu 	if (error == 0) {
108661d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
108786857b36SDavid Xu 		if (error != 0)
1088643ee871SKonstantin Belousov 			kern_ktimer_delete(td, id);
108986857b36SDavid Xu 	}
109086857b36SDavid Xu 	return (error);
109186857b36SDavid Xu }
109286857b36SDavid Xu 
1093643ee871SKonstantin Belousov int
1094643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1095643ee871SKonstantin Belousov     int *timerid, int preset_id)
109686857b36SDavid Xu {
109786857b36SDavid Xu 	struct proc *p = td->td_proc;
109886857b36SDavid Xu 	struct itimer *it;
109986857b36SDavid Xu 	int id;
110086857b36SDavid Xu 	int error;
110186857b36SDavid Xu 
110286857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
110386857b36SDavid Xu 		return (EINVAL);
110486857b36SDavid Xu 
110586857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
110686857b36SDavid Xu 		return (EINVAL);
110786857b36SDavid Xu 
110886857b36SDavid Xu 	if (evp != NULL) {
110986857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
111056c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
111156c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
111286857b36SDavid Xu 			return (EINVAL);
111356c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
111456c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
111586857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
111686857b36SDavid Xu 			return (EINVAL);
111786857b36SDavid Xu 	}
111886857b36SDavid Xu 
111960354683SDavid Xu 	if (p->p_itimers == NULL)
112086857b36SDavid Xu 		itimers_alloc(p);
112186857b36SDavid Xu 
112286857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
112386857b36SDavid Xu 	it->it_flags = 0;
112486857b36SDavid Xu 	it->it_usecount = 0;
112586857b36SDavid Xu 	it->it_active = 0;
112656c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
112756c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
112886857b36SDavid Xu 	it->it_overrun = 0;
112986857b36SDavid Xu 	it->it_overrun_last = 0;
113086857b36SDavid Xu 	it->it_clockid = clock_id;
113186857b36SDavid Xu 	it->it_timerid = -1;
113286857b36SDavid Xu 	it->it_proc = p;
113386857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
113486857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
113586857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
113686857b36SDavid Xu 	if (error != 0)
113786857b36SDavid Xu 		goto out;
113886857b36SDavid Xu 
113986857b36SDavid Xu 	PROC_LOCK(p);
114086857b36SDavid Xu 	if (preset_id != -1) {
114186857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
114286857b36SDavid Xu 		id = preset_id;
114360354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
114486857b36SDavid Xu 			PROC_UNLOCK(p);
114586857b36SDavid Xu 			error = 0;
114686857b36SDavid Xu 			goto out;
114786857b36SDavid Xu 		}
114886857b36SDavid Xu 	} else {
114986857b36SDavid Xu 		/*
115086857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
115186857b36SDavid Xu 		 * for setitimer().
115286857b36SDavid Xu 		 */
115386857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
115460354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
115586857b36SDavid Xu 				break;
115686857b36SDavid Xu 		if (id == TIMER_MAX) {
115786857b36SDavid Xu 			PROC_UNLOCK(p);
115886857b36SDavid Xu 			error = EAGAIN;
115986857b36SDavid Xu 			goto out;
116086857b36SDavid Xu 		}
116186857b36SDavid Xu 	}
116286857b36SDavid Xu 	it->it_timerid = id;
116360354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
116486857b36SDavid Xu 	if (evp != NULL)
116586857b36SDavid Xu 		it->it_sigev = *evp;
116686857b36SDavid Xu 	else {
116786857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
116886857b36SDavid Xu 		switch (clock_id) {
116986857b36SDavid Xu 		default:
117086857b36SDavid Xu 		case CLOCK_REALTIME:
117186857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
117286857b36SDavid Xu 			break;
117386857b36SDavid Xu 		case CLOCK_VIRTUAL:
117486857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
117586857b36SDavid Xu 			break;
117686857b36SDavid Xu 		case CLOCK_PROF:
117786857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
117886857b36SDavid Xu 			break;
117986857b36SDavid Xu 		}
11808f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
118186857b36SDavid Xu 	}
118286857b36SDavid Xu 
118356c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
118456c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
118586857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
118686857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
118786857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
118886857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
118986857b36SDavid Xu 	}
119086857b36SDavid Xu 	PROC_UNLOCK(p);
119186857b36SDavid Xu 	*timerid = id;
119286857b36SDavid Xu 	return (0);
119386857b36SDavid Xu 
119486857b36SDavid Xu out:
119586857b36SDavid Xu 	ITIMER_LOCK(it);
119686857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
119786857b36SDavid Xu 	ITIMER_UNLOCK(it);
119886857b36SDavid Xu 	uma_zfree(itimer_zone, it);
119986857b36SDavid Xu 	return (error);
120086857b36SDavid Xu }
120186857b36SDavid Xu 
120286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
120361d3a4efSDavid Xu struct ktimer_delete_args {
120461d3a4efSDavid Xu 	int timerid;
120586857b36SDavid Xu };
120686857b36SDavid Xu #endif
120786857b36SDavid Xu int
12088451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
120986857b36SDavid Xu {
1210643ee871SKonstantin Belousov 
1211643ee871SKonstantin Belousov 	return (kern_ktimer_delete(td, uap->timerid));
121286857b36SDavid Xu }
121386857b36SDavid Xu 
121486857b36SDavid Xu static struct itimer *
1215843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
121686857b36SDavid Xu {
121786857b36SDavid Xu 	struct itimer *it;
121886857b36SDavid Xu 
121986857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
12203f935cf3SColin Percival 	if ((p->p_itimers == NULL) ||
12213f935cf3SColin Percival 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
122260354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
122386857b36SDavid Xu 		return (NULL);
122486857b36SDavid Xu 	}
122586857b36SDavid Xu 	ITIMER_LOCK(it);
1226843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
122786857b36SDavid Xu 		ITIMER_UNLOCK(it);
122886857b36SDavid Xu 		it = NULL;
122986857b36SDavid Xu 	}
123086857b36SDavid Xu 	return (it);
123186857b36SDavid Xu }
123286857b36SDavid Xu 
1233643ee871SKonstantin Belousov int
1234643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid)
123586857b36SDavid Xu {
123686857b36SDavid Xu 	struct proc *p = td->td_proc;
123786857b36SDavid Xu 	struct itimer *it;
123886857b36SDavid Xu 
123986857b36SDavid Xu 	PROC_LOCK(p);
1240843b99c6SDavid Xu 	it = itimer_find(p, timerid);
124186857b36SDavid Xu 	if (it == NULL) {
124286857b36SDavid Xu 		PROC_UNLOCK(p);
124386857b36SDavid Xu 		return (EINVAL);
124486857b36SDavid Xu 	}
124586857b36SDavid Xu 	PROC_UNLOCK(p);
124686857b36SDavid Xu 
124786857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
124886857b36SDavid Xu 	while (it->it_usecount > 0) {
124986857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
125086857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
125186857b36SDavid Xu 	}
125286857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
125386857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
125486857b36SDavid Xu 	ITIMER_UNLOCK(it);
125586857b36SDavid Xu 
125686857b36SDavid Xu 	PROC_LOCK(p);
125786857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
125886857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
125960354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
126086857b36SDavid Xu 	PROC_UNLOCK(p);
126186857b36SDavid Xu 	uma_zfree(itimer_zone, it);
126286857b36SDavid Xu 	return (0);
126386857b36SDavid Xu }
126486857b36SDavid Xu 
126586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
126661d3a4efSDavid Xu struct ktimer_settime_args {
126761d3a4efSDavid Xu 	int timerid;
126886857b36SDavid Xu 	int flags;
126986857b36SDavid Xu 	const struct itimerspec * value;
127086857b36SDavid Xu 	struct itimerspec * ovalue;
127186857b36SDavid Xu };
127286857b36SDavid Xu #endif
127386857b36SDavid Xu int
12748451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
127586857b36SDavid Xu {
127686857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
127786857b36SDavid Xu 	int error;
127886857b36SDavid Xu 
127986857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
128086857b36SDavid Xu 	if (error != 0)
128186857b36SDavid Xu 		return (error);
1282643ee871SKonstantin Belousov 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1283643ee871SKonstantin Belousov 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1284643ee871SKonstantin Belousov 	if (error == 0 && uap->ovalue != NULL)
1285643ee871SKonstantin Belousov 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1286643ee871SKonstantin Belousov 	return (error);
1287643ee871SKonstantin Belousov }
128886857b36SDavid Xu 
1289643ee871SKonstantin Belousov int
1290643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1291643ee871SKonstantin Belousov     struct itimerspec *val, struct itimerspec *oval)
1292643ee871SKonstantin Belousov {
1293643ee871SKonstantin Belousov 	struct proc *p;
1294643ee871SKonstantin Belousov 	struct itimer *it;
1295643ee871SKonstantin Belousov 	int error;
129686857b36SDavid Xu 
1297643ee871SKonstantin Belousov 	p = td->td_proc;
129886857b36SDavid Xu 	PROC_LOCK(p);
1299643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
130086857b36SDavid Xu 		PROC_UNLOCK(p);
130186857b36SDavid Xu 		error = EINVAL;
130286857b36SDavid Xu 	} else {
130386857b36SDavid Xu 		PROC_UNLOCK(p);
130486857b36SDavid Xu 		itimer_enter(it);
1305643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1306643ee871SKonstantin Belousov 		    flags, val, oval));
130786857b36SDavid Xu 		itimer_leave(it);
130886857b36SDavid Xu 		ITIMER_UNLOCK(it);
130986857b36SDavid Xu 	}
131086857b36SDavid Xu 	return (error);
131186857b36SDavid Xu }
131286857b36SDavid Xu 
131386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
131461d3a4efSDavid Xu struct ktimer_gettime_args {
131561d3a4efSDavid Xu 	int timerid;
131686857b36SDavid Xu 	struct itimerspec * value;
131786857b36SDavid Xu };
131886857b36SDavid Xu #endif
131986857b36SDavid Xu int
13208451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
132186857b36SDavid Xu {
132286857b36SDavid Xu 	struct itimerspec val;
132386857b36SDavid Xu 	int error;
132486857b36SDavid Xu 
1325643ee871SKonstantin Belousov 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1326643ee871SKonstantin Belousov 	if (error == 0)
1327643ee871SKonstantin Belousov 		error = copyout(&val, uap->value, sizeof(val));
1328643ee871SKonstantin Belousov 	return (error);
1329643ee871SKonstantin Belousov }
1330643ee871SKonstantin Belousov 
1331643ee871SKonstantin Belousov int
1332643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1333643ee871SKonstantin Belousov {
1334643ee871SKonstantin Belousov 	struct proc *p;
1335643ee871SKonstantin Belousov 	struct itimer *it;
1336643ee871SKonstantin Belousov 	int error;
1337643ee871SKonstantin Belousov 
1338643ee871SKonstantin Belousov 	p = td->td_proc;
133986857b36SDavid Xu 	PROC_LOCK(p);
1340643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
134186857b36SDavid Xu 		PROC_UNLOCK(p);
134286857b36SDavid Xu 		error = EINVAL;
134386857b36SDavid Xu 	} else {
134486857b36SDavid Xu 		PROC_UNLOCK(p);
134586857b36SDavid Xu 		itimer_enter(it);
1346643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
134786857b36SDavid Xu 		itimer_leave(it);
134886857b36SDavid Xu 		ITIMER_UNLOCK(it);
134986857b36SDavid Xu 	}
135086857b36SDavid Xu 	return (error);
135186857b36SDavid Xu }
135286857b36SDavid Xu 
135386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
135486857b36SDavid Xu struct timer_getoverrun_args {
135561d3a4efSDavid Xu 	int timerid;
135686857b36SDavid Xu };
135786857b36SDavid Xu #endif
135886857b36SDavid Xu int
13598451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
136086857b36SDavid Xu {
1361e346f8c4SBjoern A. Zeeb 
1362e346f8c4SBjoern A. Zeeb 	return (kern_ktimer_getoverrun(td, uap->timerid));
1363e346f8c4SBjoern A. Zeeb }
1364e346f8c4SBjoern A. Zeeb 
1365e346f8c4SBjoern A. Zeeb int
1366e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id)
1367e346f8c4SBjoern A. Zeeb {
136886857b36SDavid Xu 	struct proc *p = td->td_proc;
136986857b36SDavid Xu 	struct itimer *it;
137086857b36SDavid Xu 	int error ;
137186857b36SDavid Xu 
137286857b36SDavid Xu 	PROC_LOCK(p);
1373e346f8c4SBjoern A. Zeeb 	if (timer_id < 3 ||
1374e346f8c4SBjoern A. Zeeb 	    (it = itimer_find(p, timer_id)) == NULL) {
137586857b36SDavid Xu 		PROC_UNLOCK(p);
137686857b36SDavid Xu 		error = EINVAL;
137786857b36SDavid Xu 	} else {
137886857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
137986857b36SDavid Xu 		ITIMER_UNLOCK(it);
138056c06c4bSDavid Xu 		PROC_UNLOCK(p);
138186857b36SDavid Xu 		error = 0;
138286857b36SDavid Xu 	}
138386857b36SDavid Xu 	return (error);
138486857b36SDavid Xu }
138586857b36SDavid Xu 
138686857b36SDavid Xu static int
138786857b36SDavid Xu realtimer_create(struct itimer *it)
138886857b36SDavid Xu {
138986857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
139086857b36SDavid Xu 	return (0);
139186857b36SDavid Xu }
139286857b36SDavid Xu 
139386857b36SDavid Xu static int
139486857b36SDavid Xu realtimer_delete(struct itimer *it)
139586857b36SDavid Xu {
139686857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1397843b99c6SDavid Xu 
1398d6b6592eSDavid Xu 	/*
1399d6b6592eSDavid Xu 	 * clear timer's value and interval to tell realtimer_expire
1400d6b6592eSDavid Xu 	 * to not rearm the timer.
1401d6b6592eSDavid Xu 	 */
1402d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_value);
1403d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_interval);
1404843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1405843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1406843b99c6SDavid Xu 	ITIMER_LOCK(it);
140786857b36SDavid Xu 	return (0);
140886857b36SDavid Xu }
140986857b36SDavid Xu 
141086857b36SDavid Xu static int
141186857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
141286857b36SDavid Xu {
141356c06c4bSDavid Xu 	struct timespec cts;
141486857b36SDavid Xu 
141586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
141686857b36SDavid Xu 
141756c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
141856c06c4bSDavid Xu 	*ovalue = it->it_time;
141986857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
142056c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
142186857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
142286857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
142386857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
142486857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
142586857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
142686857b36SDavid Xu 		}
142786857b36SDavid Xu 	}
142886857b36SDavid Xu 	return (0);
142986857b36SDavid Xu }
143086857b36SDavid Xu 
143186857b36SDavid Xu static int
143286857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
143386857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
143486857b36SDavid Xu {
143556c06c4bSDavid Xu 	struct timespec cts, ts;
143656c06c4bSDavid Xu 	struct timeval tv;
143756c06c4bSDavid Xu 	struct itimerspec val;
143886857b36SDavid Xu 
143986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
144086857b36SDavid Xu 
144156c06c4bSDavid Xu 	val = *value;
144256c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
144386857b36SDavid Xu 		return (EINVAL);
144486857b36SDavid Xu 
144556c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
144656c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
144786857b36SDavid Xu 			return (EINVAL);
144886857b36SDavid Xu 	} else {
144956c06c4bSDavid Xu 		timespecclear(&val.it_interval);
145086857b36SDavid Xu 	}
145186857b36SDavid Xu 
145286857b36SDavid Xu 	if (ovalue != NULL)
145386857b36SDavid Xu 		realtimer_gettime(it, ovalue);
145486857b36SDavid Xu 
145586857b36SDavid Xu 	it->it_time = val;
145656c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
145756c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
145856c06c4bSDavid Xu 		ts = val.it_value;
145986857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
146086857b36SDavid Xu 			/* Convert to absolute time. */
146156c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
146286857b36SDavid Xu 		} else {
146356c06c4bSDavid Xu 			timespecsub(&ts, &cts);
146486857b36SDavid Xu 			/*
146556c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
146686857b36SDavid Xu 			 * fix it.
146786857b36SDavid Xu 			 */
146886857b36SDavid Xu 		}
146956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
147056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
147186857b36SDavid Xu 			realtimer_expire, it);
147286857b36SDavid Xu 	} else {
147386857b36SDavid Xu 		callout_stop(&it->it_callout);
147486857b36SDavid Xu 	}
147586857b36SDavid Xu 
147686857b36SDavid Xu 	return (0);
147786857b36SDavid Xu }
147886857b36SDavid Xu 
147986857b36SDavid Xu static void
148056c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
148186857b36SDavid Xu {
148286857b36SDavid Xu 	if (id == CLOCK_REALTIME)
148356c06c4bSDavid Xu 		getnanotime(ts);
148486857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
148556c06c4bSDavid Xu 		getnanouptime(ts);
148656c06c4bSDavid Xu }
148756c06c4bSDavid Xu 
148856c06c4bSDavid Xu int
148961d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
149056c06c4bSDavid Xu {
149156c06c4bSDavid Xu 	struct itimer *it;
149256c06c4bSDavid Xu 
149356c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1494843b99c6SDavid Xu 	it = itimer_find(p, timerid);
149556c06c4bSDavid Xu 	if (it != NULL) {
149656c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
149756c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
149856c06c4bSDavid Xu 		it->it_overrun = 0;
149956c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
150056c06c4bSDavid Xu 		return (0);
150156c06c4bSDavid Xu 	}
150256c06c4bSDavid Xu 	return (EINVAL);
150356c06c4bSDavid Xu }
150456c06c4bSDavid Xu 
150556c06c4bSDavid Xu int
150656c06c4bSDavid Xu itimespecfix(struct timespec *ts)
150756c06c4bSDavid Xu {
150856c06c4bSDavid Xu 
150956c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
151056c06c4bSDavid Xu 		return (EINVAL);
151156c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
151256c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
151356c06c4bSDavid Xu 	return (0);
151486857b36SDavid Xu }
151586857b36SDavid Xu 
151686857b36SDavid Xu /* Timeout callback for realtime timer */
151786857b36SDavid Xu static void
151886857b36SDavid Xu realtimer_expire(void *arg)
151986857b36SDavid Xu {
152056c06c4bSDavid Xu 	struct timespec cts, ts;
152156c06c4bSDavid Xu 	struct timeval tv;
152286857b36SDavid Xu 	struct itimer *it;
152386857b36SDavid Xu 
152486857b36SDavid Xu 	it = (struct itimer *)arg;
152586857b36SDavid Xu 
152656c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
152786857b36SDavid Xu 	/* Only fire if time is reached. */
152856c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
152956c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
153056c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
153186857b36SDavid Xu 				    &it->it_time.it_interval);
153256c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
153377e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
153486857b36SDavid Xu 					it->it_overrun++;
153577e718f7SDavid Xu 				else
153677e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
153756c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
153886857b36SDavid Xu 					    &it->it_time.it_interval);
153986857b36SDavid Xu 			}
154086857b36SDavid Xu 		} else {
154186857b36SDavid Xu 			/* single shot timer ? */
154256c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
154386857b36SDavid Xu 		}
154456c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
154556c06c4bSDavid Xu 			ts = it->it_time.it_value;
154656c06c4bSDavid Xu 			timespecsub(&ts, &cts);
154756c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
154856c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
154986857b36SDavid Xu 				 realtimer_expire, it);
155086857b36SDavid Xu 		}
1551d6b6592eSDavid Xu 		itimer_enter(it);
155286857b36SDavid Xu 		ITIMER_UNLOCK(it);
155386857b36SDavid Xu 		itimer_fire(it);
155486857b36SDavid Xu 		ITIMER_LOCK(it);
1555d6b6592eSDavid Xu 		itimer_leave(it);
155656c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
155756c06c4bSDavid Xu 		ts = it->it_time.it_value;
155856c06c4bSDavid Xu 		timespecsub(&ts, &cts);
155956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
156056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
156186857b36SDavid Xu  			it);
156286857b36SDavid Xu 	}
156386857b36SDavid Xu }
156486857b36SDavid Xu 
156586857b36SDavid Xu void
156686857b36SDavid Xu itimer_fire(struct itimer *it)
156786857b36SDavid Xu {
156886857b36SDavid Xu 	struct proc *p = it->it_proc;
1569cf7d9a8cSDavid Xu 	struct thread *td;
157086857b36SDavid Xu 
157156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
157256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1573cf7d9a8cSDavid Xu 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
157456c06c4bSDavid Xu 			ITIMER_LOCK(it);
157556c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
157656c06c4bSDavid Xu 			timespecclear(&it->it_time.it_interval);
157756c06c4bSDavid Xu 			callout_stop(&it->it_callout);
157856c06c4bSDavid Xu 			ITIMER_UNLOCK(it);
1579cf7d9a8cSDavid Xu 			return;
15806d7b314bSDavid Xu 		}
1581cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
1582cf7d9a8cSDavid Xu 			it->it_ksi.ksi_errno = 0;
1583cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1584cf7d9a8cSDavid Xu 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
158556c06c4bSDavid Xu 		} else {
158677e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
15876d7b314bSDavid Xu 				it->it_overrun++;
158877e718f7SDavid Xu 			else
158977e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
159056c06c4bSDavid Xu 		}
159186857b36SDavid Xu 		PROC_UNLOCK(p);
159286857b36SDavid Xu 	}
159386857b36SDavid Xu }
159486857b36SDavid Xu 
159586857b36SDavid Xu static void
159686857b36SDavid Xu itimers_alloc(struct proc *p)
159786857b36SDavid Xu {
159860354683SDavid Xu 	struct itimers *its;
159960354683SDavid Xu 	int i;
160086857b36SDavid Xu 
160160354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
160286857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
160386857b36SDavid Xu 	LIST_INIT(&its->its_prof);
160486857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
160560354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
160660354683SDavid Xu 		its->its_timers[i] = NULL;
160760354683SDavid Xu 	PROC_LOCK(p);
160860354683SDavid Xu 	if (p->p_itimers == NULL) {
160960354683SDavid Xu 		p->p_itimers = its;
161060354683SDavid Xu 		PROC_UNLOCK(p);
161160354683SDavid Xu 	}
161260354683SDavid Xu 	else {
161360354683SDavid Xu 		PROC_UNLOCK(p);
161460354683SDavid Xu 		free(its, M_SUBPROC);
161560354683SDavid Xu 	}
161686857b36SDavid Xu }
161786857b36SDavid Xu 
1618993182e5SAlexander Leidinger static void
1619993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1620993182e5SAlexander Leidinger {
1621993182e5SAlexander Leidinger 	itimers_event_hook_exit(arg, p);
1622993182e5SAlexander Leidinger }
1623993182e5SAlexander Leidinger 
162486857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1625d26b1a1fSDavid Xu static void
1626993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
162786857b36SDavid Xu {
162886857b36SDavid Xu 	struct itimers *its;
162986857b36SDavid Xu 	struct itimer *it;
16303e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
163186857b36SDavid Xu 	int i;
163286857b36SDavid Xu 
163360354683SDavid Xu 	if (p->p_itimers != NULL) {
163460354683SDavid Xu 		its = p->p_itimers;
163586857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
163686857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
163786857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
163886857b36SDavid Xu 		}
163986857b36SDavid Xu 		/*
164086857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
164186857b36SDavid Xu 		 * by new image.
164286857b36SDavid Xu 		 */
164386857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
164486857b36SDavid Xu 			i = 3;
164586857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
164686857b36SDavid Xu 			i = 0;
164786857b36SDavid Xu 		else
164886857b36SDavid Xu 			panic("unhandled event");
164986857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
1650843b99c6SDavid Xu 			if ((it = its->its_timers[i]) != NULL)
1651643ee871SKonstantin Belousov 				kern_ktimer_delete(curthread, i);
165286857b36SDavid Xu 		}
165386857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
165486857b36SDavid Xu 		    its->its_timers[1] == NULL &&
165586857b36SDavid Xu 		    its->its_timers[2] == NULL) {
165660354683SDavid Xu 			free(its, M_SUBPROC);
165760354683SDavid Xu 			p->p_itimers = NULL;
165886857b36SDavid Xu 		}
165986857b36SDavid Xu 	}
166086857b36SDavid Xu }
1661