xref: /freebsd/sys/kern/kern_time.c (revision 3e18d701dea5599d1d4a2c4c9c481c52853cf644)
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) {
20347ce3e53SDmitry Chagin 			error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
204d31e4b3aSKonstantin Belousov 			if (error != 0)
205d65f1abcSDavid Xu 				return (error);
20647ce3e53SDmitry 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);
401*3e18d701SDmitry Chagin 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000 ||
402*3e18d701SDmitry Chagin 	    ats->tv_sec < 0)
4037edfb592SJohn Baldwin 		return (EINVAL);
404a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
405f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
4067edfb592SJohn Baldwin 	error = settime(td, &atv);
40794c8fcd8SPeter Wemm 	return (error);
40894c8fcd8SPeter Wemm }
40994c8fcd8SPeter Wemm 
41094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
41194c8fcd8SPeter Wemm struct clock_getres_args {
41294c8fcd8SPeter Wemm 	clockid_t clock_id;
41394c8fcd8SPeter Wemm 	struct	timespec *tp;
41494c8fcd8SPeter Wemm };
41594c8fcd8SPeter Wemm #endif
41694c8fcd8SPeter Wemm int
4178451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
41894c8fcd8SPeter Wemm {
41994c8fcd8SPeter Wemm 	struct timespec ts;
420f0b479cdSPaul Saab 	int error;
42194c8fcd8SPeter Wemm 
422f0b479cdSPaul Saab 	if (uap->tp == NULL)
423f0b479cdSPaul Saab 		return (0);
424f0b479cdSPaul Saab 
425f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
426f0b479cdSPaul Saab 	if (error == 0)
427f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
428f0b479cdSPaul Saab 	return (error);
429f0b479cdSPaul Saab }
430f0b479cdSPaul Saab 
431f0b479cdSPaul Saab int
432f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
433f0b479cdSPaul Saab {
434f0b479cdSPaul Saab 
435f0b479cdSPaul Saab 	ts->tv_sec = 0;
436f0b479cdSPaul Saab 	switch (clock_id) {
437b8817154SKelly Yancey 	case CLOCK_REALTIME:
4385e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
4395e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
440b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
4415e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
4425e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
4435eefd889SAndre Oppermann 	case CLOCK_UPTIME:
4445e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
4455e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
446ac0653dcSBruce Evans 		/*
447ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
448ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
449ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
450ac0653dcSBruce Evans 		 */
451f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
452b8817154SKelly Yancey 		break;
453b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
454b8817154SKelly Yancey 	case CLOCK_PROF:
455b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
456f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
457b8817154SKelly Yancey 		break;
4585e758b95SRobert Watson 	case CLOCK_SECOND:
4595e758b95SRobert Watson 		ts->tv_sec = 1;
4605e758b95SRobert Watson 		ts->tv_nsec = 0;
4615e758b95SRobert Watson 		break;
46200d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
463d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
464d65f1abcSDavid Xu 	cputime:
46500d6ac63SDavid Xu 		/* sync with cputick2usec */
46600d6ac63SDavid Xu 		ts->tv_nsec = 1000000 / cpu_tickrate();
46700d6ac63SDavid Xu 		if (ts->tv_nsec == 0)
46800d6ac63SDavid Xu 			ts->tv_nsec = 1000;
46900d6ac63SDavid Xu 		break;
470b8817154SKelly Yancey 	default:
471d65f1abcSDavid Xu 		if ((int)clock_id < 0)
472d65f1abcSDavid Xu 			goto cputime;
473b8817154SKelly Yancey 		return (EINVAL);
47494c8fcd8SPeter Wemm 	}
475de0a9241SKelly Yancey 	return (0);
47694c8fcd8SPeter Wemm }
47794c8fcd8SPeter Wemm 
4780dbf17e6SAlexander Motin static uint8_t nanowait[MAXCPU];
47994c8fcd8SPeter Wemm 
4807fdf2c85SPaul Saab int
4817fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
4825b870b7bSPeter Wemm {
483098176f0SDavide Italiano 	struct timespec ts;
484098176f0SDavide Italiano 	sbintime_t sbt, sbtt, prec, tmp;
485980c545dSAlexander Motin 	time_t over;
48633841826SPoul-Henning Kamp 	int error;
4875b870b7bSPeter Wemm 
4887d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
489708e7684SPeter Wemm 		return (EINVAL);
490d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
4917d7fb492SBruce Evans 		return (0);
492980c545dSAlexander Motin 	ts = *rqt;
493980c545dSAlexander Motin 	if (ts.tv_sec > INT32_MAX / 2) {
494980c545dSAlexander Motin 		over = ts.tv_sec - INT32_MAX / 2;
495980c545dSAlexander Motin 		ts.tv_sec -= over;
496980c545dSAlexander Motin 	} else
497980c545dSAlexander Motin 		over = 0;
498980c545dSAlexander Motin 	tmp = tstosbt(ts);
499098176f0SDavide Italiano 	prec = tmp;
500098176f0SDavide Italiano 	prec >>= tc_precexp;
501098176f0SDavide Italiano 	if (TIMESEL(&sbt, tmp))
502098176f0SDavide Italiano 		sbt += tc_tick_sbt;
503098176f0SDavide Italiano 	sbt += tmp;
5040dbf17e6SAlexander Motin 	error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
5050dbf17e6SAlexander Motin 	    sbt, prec, C_ABSOLUTE);
50633841826SPoul-Henning Kamp 	if (error != EWOULDBLOCK) {
50733841826SPoul-Henning Kamp 		if (error == ERESTART)
50894c8fcd8SPeter Wemm 			error = EINTR;
509098176f0SDavide Italiano 		TIMESEL(&sbtt, tmp);
51033841826SPoul-Henning Kamp 		if (rmt != NULL) {
511098176f0SDavide Italiano 			ts = sbttots(sbt - sbtt);
512980c545dSAlexander Motin 			ts.tv_sec += over;
51333841826SPoul-Henning Kamp 			if (ts.tv_sec < 0)
51433841826SPoul-Henning Kamp 				timespecclear(&ts);
51500af9731SPoul-Henning Kamp 			*rmt = ts;
51600af9731SPoul-Henning Kamp 		}
517098176f0SDavide Italiano 		if (sbtt >= sbt)
518098176f0SDavide Italiano 			return (0);
51933841826SPoul-Henning Kamp 		return (error);
52033841826SPoul-Henning Kamp 	}
52133841826SPoul-Henning Kamp 	return (0);
5225b870b7bSPeter Wemm }
52394c8fcd8SPeter Wemm 
5245b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
5255b870b7bSPeter Wemm struct nanosleep_args {
5265b870b7bSPeter Wemm 	struct	timespec *rqtp;
5275b870b7bSPeter Wemm 	struct	timespec *rmtp;
5285b870b7bSPeter Wemm };
5295b870b7bSPeter Wemm #endif
5305b870b7bSPeter Wemm /* ARGSUSED */
5315b870b7bSPeter Wemm int
5328451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
5335b870b7bSPeter Wemm {
5345b870b7bSPeter Wemm 	struct timespec rmt, rqt;
535fb99ab88SMatthew Dillon 	int error;
5365b870b7bSPeter Wemm 
537d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
5385b870b7bSPeter Wemm 	if (error)
5395b870b7bSPeter Wemm 		return (error);
540fb99ab88SMatthew Dillon 
54131f3e2adSAlfred Perlstein 	if (uap->rmtp &&
54231f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
54331f3e2adSAlfred Perlstein 			return (EFAULT);
5447fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
545d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
546fb99ab88SMatthew Dillon 		int error2;
547fb99ab88SMatthew Dillon 
548d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
54931f3e2adSAlfred Perlstein 		if (error2)
550fb99ab88SMatthew Dillon 			error = error2;
551708e7684SPeter Wemm 	}
552708e7684SPeter Wemm 	return (error);
55394c8fcd8SPeter Wemm }
55494c8fcd8SPeter Wemm 
5555b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
556df8bae1dSRodney W. Grimes struct gettimeofday_args {
557df8bae1dSRodney W. Grimes 	struct	timeval *tp;
558df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
559df8bae1dSRodney W. Grimes };
560d2d3e875SBruce Evans #endif
561df8bae1dSRodney W. Grimes /* ARGSUSED */
56226f9a767SRodney W. Grimes int
5638451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
564df8bae1dSRodney W. Grimes {
565df8bae1dSRodney W. Grimes 	struct timeval atv;
566411c25edSTim J. Robbins 	struct timezone rtz;
567df8bae1dSRodney W. Grimes 	int error = 0;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	if (uap->tp) {
570df8bae1dSRodney W. Grimes 		microtime(&atv);
57101609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
572df8bae1dSRodney W. Grimes 	}
57321dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
57491f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
57591f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
576411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
57721dcdb38SPoul-Henning Kamp 	}
578df8bae1dSRodney W. Grimes 	return (error);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
582df8bae1dSRodney W. Grimes struct settimeofday_args {
583df8bae1dSRodney W. Grimes 	struct	timeval *tv;
584df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
585df8bae1dSRodney W. Grimes };
586d2d3e875SBruce Evans #endif
587df8bae1dSRodney W. Grimes /* ARGSUSED */
58826f9a767SRodney W. Grimes int
5898451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
590df8bae1dSRodney W. Grimes {
591b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
592b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
593b88ec951SJohn Baldwin 	int error;
594b88ec951SJohn Baldwin 
595b88ec951SJohn Baldwin 	if (uap->tv) {
596b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
597b88ec951SJohn Baldwin 		if (error)
598b88ec951SJohn Baldwin 			return (error);
599b88ec951SJohn Baldwin 		tvp = &atv;
600b88ec951SJohn Baldwin 	} else
601b88ec951SJohn Baldwin 		tvp = NULL;
602b88ec951SJohn Baldwin 	if (uap->tzp) {
603b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
604b88ec951SJohn Baldwin 		if (error)
605b88ec951SJohn Baldwin 			return (error);
606b88ec951SJohn Baldwin 		tzp = &atz;
607b88ec951SJohn Baldwin 	} else
608b88ec951SJohn Baldwin 		tzp = NULL;
609b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
610b88ec951SJohn Baldwin }
611b88ec951SJohn Baldwin 
612b88ec951SJohn Baldwin int
613b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
614b88ec951SJohn Baldwin {
615b88ec951SJohn Baldwin 	int error;
616fb99ab88SMatthew Dillon 
617acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
618b88ec951SJohn Baldwin 	if (error)
6197edfb592SJohn Baldwin 		return (error);
620df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
621b88ec951SJohn Baldwin 	if (tv) {
622*3e18d701SDmitry Chagin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
623*3e18d701SDmitry Chagin 		    tv->tv_sec < 0)
6247edfb592SJohn Baldwin 			return (EINVAL);
625b88ec951SJohn Baldwin 		error = settime(td, tv);
626708e7684SPeter Wemm 	}
627b88ec951SJohn Baldwin 	if (tzp && error == 0) {
628b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
629b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
630b88ec951SJohn Baldwin 	}
6317edfb592SJohn Baldwin 	return (error);
632b88ec951SJohn Baldwin }
6337edfb592SJohn Baldwin 
634df8bae1dSRodney W. Grimes /*
635873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
636873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
637873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
638873fbcd7SRobert Watson  * time until they expire.
639df8bae1dSRodney W. Grimes  *
640873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
641873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
642873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
643873fbcd7SRobert Watson  * drifting.
644df8bae1dSRodney W. Grimes  *
645df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
646873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
647873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
648873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
649873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
650873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
651873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
652873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
653873fbcd7SRobert Watson  * should go off.
654df8bae1dSRodney W. Grimes  */
655d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
656df8bae1dSRodney W. Grimes struct getitimer_args {
657df8bae1dSRodney W. Grimes 	u_int	which;
658df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
659df8bae1dSRodney W. Grimes };
660d2d3e875SBruce Evans #endif
66126f9a767SRodney W. Grimes int
6628451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap)
663df8bae1dSRodney W. Grimes {
664df8bae1dSRodney W. Grimes 	struct itimerval aitv;
665c90110d6SJohn Baldwin 	int error;
666df8bae1dSRodney W. Grimes 
667cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
668cfa0efe7SMaxim Sobolev 	if (error != 0)
669cfa0efe7SMaxim Sobolev 		return (error);
670cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
671cfa0efe7SMaxim Sobolev }
672cfa0efe7SMaxim Sobolev 
673cfa0efe7SMaxim Sobolev int
674cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
675cfa0efe7SMaxim Sobolev {
676cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
677cfa0efe7SMaxim Sobolev 	struct timeval ctv;
678cfa0efe7SMaxim Sobolev 
679cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
680df8bae1dSRodney W. Grimes 		return (EINVAL);
681fb99ab88SMatthew Dillon 
682cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
683df8bae1dSRodney W. Grimes 		/*
684ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
685df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
686df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
687df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
688df8bae1dSRodney W. Grimes 		 */
68996d7f8efSTim J. Robbins 		PROC_LOCK(p);
690cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
69196d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
692cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
693b5ea3779SAlexander Motin 			microuptime(&ctv);
694cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
695cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
696df8bae1dSRodney W. Grimes 			else
697cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
698227ee8a1SPoul-Henning Kamp 		}
699fb99ab88SMatthew Dillon 	} else {
7005c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
701cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
7025c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
703fb99ab88SMatthew Dillon 	}
704cfa0efe7SMaxim Sobolev 	return (0);
705df8bae1dSRodney W. Grimes }
706df8bae1dSRodney W. Grimes 
707d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
708df8bae1dSRodney W. Grimes struct setitimer_args {
709df8bae1dSRodney W. Grimes 	u_int	which;
710df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
711df8bae1dSRodney W. Grimes };
712d2d3e875SBruce Evans #endif
71326f9a767SRodney W. Grimes int
7148451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap)
715df8bae1dSRodney W. Grimes {
716cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
717c90110d6SJohn Baldwin 	int error;
71896d7f8efSTim J. Robbins 
71996d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
72096d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
7218451d0ddSKip Macy 		return (sys_getitimer(td, (struct getitimer_args *)uap));
72296d7f8efSTim J. Robbins 	}
723df8bae1dSRodney W. Grimes 
72496d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
725df8bae1dSRodney W. Grimes 		return (error);
726cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
727cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
728cfa0efe7SMaxim Sobolev 		return (error);
729cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
730cfa0efe7SMaxim Sobolev }
731cfa0efe7SMaxim Sobolev 
732cfa0efe7SMaxim Sobolev int
733c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
734c90110d6SJohn Baldwin     struct itimerval *oitv)
735cfa0efe7SMaxim Sobolev {
736cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
737cfa0efe7SMaxim Sobolev 	struct timeval ctv;
738b5ea3779SAlexander Motin 	sbintime_t sbt, pr;
739cfa0efe7SMaxim Sobolev 
7405e85ac17SJohn Baldwin 	if (aitv == NULL)
7415e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
7425e85ac17SJohn Baldwin 
743cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
74496d7f8efSTim J. Robbins 		return (EINVAL);
745b5ea3779SAlexander Motin 	if (itimerfix(&aitv->it_value) ||
746b5ea3779SAlexander Motin 	    aitv->it_value.tv_sec > INT32_MAX / 2)
747cfa0efe7SMaxim Sobolev 		return (EINVAL);
748cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
749cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
750b5ea3779SAlexander Motin 	else if (itimerfix(&aitv->it_interval) ||
751b5ea3779SAlexander Motin 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
75296d7f8efSTim J. Robbins 		return (EINVAL);
75396d7f8efSTim J. Robbins 
754cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
75596d7f8efSTim J. Robbins 		PROC_LOCK(p);
7564cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
7574f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
758b5ea3779SAlexander Motin 		microuptime(&ctv);
759cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
760b5ea3779SAlexander Motin 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
761cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
762b5ea3779SAlexander Motin 			sbt = tvtosbt(aitv->it_value);
763b5ea3779SAlexander Motin 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
764b5ea3779SAlexander Motin 			    realitexpire, p, C_ABSOLUTE);
76525b4d3a8SJohn Baldwin 		}
766cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
767cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
76896d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
769cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
770cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
771cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
77296d7f8efSTim J. Robbins 			else
773cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
774fb99ab88SMatthew Dillon 		}
77596d7f8efSTim J. Robbins 	} else {
776d10a1df8SAlexander Motin 		if (aitv->it_interval.tv_sec == 0 &&
777d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec != 0 &&
778d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec < tick)
779d10a1df8SAlexander Motin 			aitv->it_interval.tv_usec = tick;
780d10a1df8SAlexander Motin 		if (aitv->it_value.tv_sec == 0 &&
781d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec != 0 &&
782d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec < tick)
783d10a1df8SAlexander Motin 			aitv->it_value.tv_usec = tick;
7845c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
785cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
786cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
7875c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
78896d7f8efSTim J. Robbins 	}
78996d7f8efSTim J. Robbins 	return (0);
790df8bae1dSRodney W. Grimes }
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes /*
793df8bae1dSRodney W. Grimes  * Real interval timer expired:
794df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
795df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
796df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
797df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
798df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
799c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
8009207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
8019207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
8029207f00aSBruce Evans  * interrupt even when we're delayed.
803df8bae1dSRodney W. Grimes  */
804df8bae1dSRodney W. Grimes void
80591afe087SPoul-Henning Kamp realitexpire(void *arg)
806df8bae1dSRodney W. Grimes {
80791afe087SPoul-Henning Kamp 	struct proc *p;
808b5ea3779SAlexander Motin 	struct timeval ctv;
809b5ea3779SAlexander Motin 	sbintime_t isbt;
810df8bae1dSRodney W. Grimes 
811df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
8128451d0ddSKip Macy 	kern_psignal(p, SIGALRM);
8134cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
8144cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
8155499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
8165499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
817df8bae1dSRodney W. Grimes 		return;
818df8bae1dSRodney W. Grimes 	}
819b5ea3779SAlexander Motin 	isbt = tvtosbt(p->p_realtimer.it_interval);
820b5ea3779SAlexander Motin 	if (isbt >= sbt_timethreshold)
821b5ea3779SAlexander Motin 		getmicrouptime(&ctv);
822b5ea3779SAlexander Motin 	else
823b5ea3779SAlexander Motin 		microuptime(&ctv);
824b5ea3779SAlexander Motin 	do {
825df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
826df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
827b5ea3779SAlexander Motin 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
828b5ea3779SAlexander Motin 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
829b5ea3779SAlexander Motin 	    isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
830df8bae1dSRodney W. Grimes }
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes /*
833df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
834df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
835df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
836df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
837df8bae1dSRodney W. Grimes  */
83826f9a767SRodney W. Grimes int
83991afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
840df8bae1dSRodney W. Grimes {
841df8bae1dSRodney W. Grimes 
84286857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
843df8bae1dSRodney W. Grimes 		return (EINVAL);
844b5ea3779SAlexander Motin 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
845b5ea3779SAlexander Motin 	    tv->tv_usec < (u_int)tick / 16)
846b5ea3779SAlexander Motin 		tv->tv_usec = (u_int)tick / 16;
847df8bae1dSRodney W. Grimes 	return (0);
848df8bae1dSRodney W. Grimes }
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes /*
851df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
852df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
853df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
854df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
855df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
856df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
857df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
858df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
859df8bae1dSRodney W. Grimes  */
86026f9a767SRodney W. Grimes int
86191afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
862df8bae1dSRodney W. Grimes {
863df8bae1dSRodney W. Grimes 
864df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
865df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
866df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
867df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
868df8bae1dSRodney W. Grimes 			goto expire;
869df8bae1dSRodney W. Grimes 		}
870df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
871df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
872df8bae1dSRodney W. Grimes 	}
873df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
874df8bae1dSRodney W. Grimes 	usec = 0;
8754cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
876df8bae1dSRodney W. Grimes 		return (1);
877df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
878df8bae1dSRodney W. Grimes expire:
8794cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
880df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
881df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
882df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
883df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
884df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
885df8bae1dSRodney W. Grimes 		}
886df8bae1dSRodney W. Grimes 	} else
887df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
888df8bae1dSRodney W. Grimes 	return (0);
889df8bae1dSRodney W. Grimes }
890df8bae1dSRodney W. Grimes 
891df8bae1dSRodney W. Grimes /*
892df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
893df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
894df8bae1dSRodney W. Grimes  * results which are before the beginning,
895df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
896df8bae1dSRodney W. Grimes  * Caveat emptor.
897df8bae1dSRodney W. Grimes  */
89826f9a767SRodney W. Grimes void
8996ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
900df8bae1dSRodney W. Grimes {
901df8bae1dSRodney W. Grimes 
902df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
903df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
904df8bae1dSRodney W. Grimes 	timevalfix(t1);
905df8bae1dSRodney W. Grimes }
906df8bae1dSRodney W. Grimes 
90726f9a767SRodney W. Grimes void
9086ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
909df8bae1dSRodney W. Grimes {
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
912df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
913df8bae1dSRodney W. Grimes 	timevalfix(t1);
914df8bae1dSRodney W. Grimes }
915df8bae1dSRodney W. Grimes 
91687b6de2bSPoul-Henning Kamp static void
91791afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
918df8bae1dSRodney W. Grimes {
919df8bae1dSRodney W. Grimes 
920df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
921df8bae1dSRodney W. Grimes 		t1->tv_sec--;
922df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
923df8bae1dSRodney W. Grimes 	}
924df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
925df8bae1dSRodney W. Grimes 		t1->tv_sec++;
926df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
927df8bae1dSRodney W. Grimes 	}
928df8bae1dSRodney W. Grimes }
92991974ce1SSam Leffler 
93091974ce1SSam Leffler /*
931addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
93291974ce1SSam Leffler  */
93391974ce1SSam Leffler int
93491974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
93591974ce1SSam Leffler {
93691974ce1SSam Leffler 	struct timeval tv, delta;
93791974ce1SSam Leffler 	int rv = 0;
93891974ce1SSam Leffler 
939addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
940addea9d4SSam Leffler 	delta = tv;
941addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
94291974ce1SSam Leffler 
94391974ce1SSam Leffler 	/*
94491974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
94591974ce1SSam Leffler 	 * even if interval is huge.
94691974ce1SSam Leffler 	 */
94791974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
94891974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
94991974ce1SSam Leffler 		*lasttime = tv;
95091974ce1SSam Leffler 		rv = 1;
95191974ce1SSam Leffler 	}
95291974ce1SSam Leffler 
95391974ce1SSam Leffler 	return (rv);
95491974ce1SSam Leffler }
95591974ce1SSam Leffler 
95691974ce1SSam Leffler /*
95791974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
958addea9d4SSam Leffler  *
959addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
960addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
961addea9d4SSam Leffler  *
962893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
963893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
964893bec80SSam Leffler  * limiting.
965893bec80SSam Leffler  *
966addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
967addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
968addea9d4SSam Leffler  * clock ticks for minimal overhead.
96991974ce1SSam Leffler  */
97091974ce1SSam Leffler int
97191974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
97291974ce1SSam Leffler {
973addea9d4SSam Leffler 	int now;
97491974ce1SSam Leffler 
97591974ce1SSam Leffler 	/*
976addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
977addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
978addea9d4SSam Leffler 	 * lasttime.
97991974ce1SSam Leffler 	 */
980addea9d4SSam Leffler 	now = ticks;
981addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
982addea9d4SSam Leffler 		lasttime->tv_sec = now;
983addea9d4SSam Leffler 		*curpps = 1;
984893bec80SSam Leffler 		return (maxpps != 0);
985addea9d4SSam Leffler 	} else {
986addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
987f8f02928SIan Lepore 		return (maxpps < 0 || *curpps <= maxpps);
988addea9d4SSam Leffler 	}
98991974ce1SSam Leffler }
99086857b36SDavid Xu 
99186857b36SDavid Xu static void
99286857b36SDavid Xu itimer_start(void)
99386857b36SDavid Xu {
99486857b36SDavid Xu 	struct kclock rt_clock = {
99586857b36SDavid Xu 		.timer_create  = realtimer_create,
99686857b36SDavid Xu 		.timer_delete  = realtimer_delete,
99786857b36SDavid Xu 		.timer_settime = realtimer_settime,
99886857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
999843b99c6SDavid Xu 		.event_hook    = NULL
100086857b36SDavid Xu 	};
100186857b36SDavid Xu 
100286857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
100386857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
100486857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
100586857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
100677e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
100777e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
100877e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1009993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1010d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1011993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1012d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
101386857b36SDavid Xu }
101486857b36SDavid Xu 
101586857b36SDavid Xu int
101686857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
101786857b36SDavid Xu {
101886857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
101986857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
102086857b36SDavid Xu 		return (0);
102186857b36SDavid Xu 	}
102286857b36SDavid Xu 	posix_clocks[clockid] = *clk;
102386857b36SDavid Xu 	return (1);
102486857b36SDavid Xu }
102586857b36SDavid Xu 
102686857b36SDavid Xu static int
102786857b36SDavid Xu itimer_init(void *mem, int size, int flags)
102886857b36SDavid Xu {
102986857b36SDavid Xu 	struct itimer *it;
103086857b36SDavid Xu 
103186857b36SDavid Xu 	it = (struct itimer *)mem;
103286857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
103386857b36SDavid Xu 	return (0);
103486857b36SDavid Xu }
103586857b36SDavid Xu 
103686857b36SDavid Xu static void
103786857b36SDavid Xu itimer_fini(void *mem, int size)
103886857b36SDavid Xu {
103986857b36SDavid Xu 	struct itimer *it;
104086857b36SDavid Xu 
104186857b36SDavid Xu 	it = (struct itimer *)mem;
104286857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
104386857b36SDavid Xu }
104486857b36SDavid Xu 
104586857b36SDavid Xu static void
104686857b36SDavid Xu itimer_enter(struct itimer *it)
104786857b36SDavid Xu {
104886857b36SDavid Xu 
104986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
105086857b36SDavid Xu 	it->it_usecount++;
105186857b36SDavid Xu }
105286857b36SDavid Xu 
105386857b36SDavid Xu static void
105486857b36SDavid Xu itimer_leave(struct itimer *it)
105586857b36SDavid Xu {
105686857b36SDavid Xu 
105786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
105886857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
105986857b36SDavid Xu 
106086857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
106186857b36SDavid Xu 		wakeup(it);
106286857b36SDavid Xu }
106386857b36SDavid Xu 
106486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
106561d3a4efSDavid Xu struct ktimer_create_args {
106686857b36SDavid Xu 	clockid_t clock_id;
106786857b36SDavid Xu 	struct sigevent * evp;
106861d3a4efSDavid Xu 	int * timerid;
106986857b36SDavid Xu };
107086857b36SDavid Xu #endif
107186857b36SDavid Xu int
10728451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
107386857b36SDavid Xu {
1074643ee871SKonstantin Belousov 	struct sigevent *evp, ev;
107561d3a4efSDavid Xu 	int id;
107686857b36SDavid Xu 	int error;
107786857b36SDavid Xu 
1078643ee871SKonstantin Belousov 	if (uap->evp == NULL) {
1079643ee871SKonstantin Belousov 		evp = NULL;
1080643ee871SKonstantin Belousov 	} else {
108186857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
108286857b36SDavid Xu 		if (error != 0)
108386857b36SDavid Xu 			return (error);
1084643ee871SKonstantin Belousov 		evp = &ev;
1085643ee871SKonstantin Belousov 	}
1086643ee871SKonstantin Belousov 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
108786857b36SDavid Xu 	if (error == 0) {
108861d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
108986857b36SDavid Xu 		if (error != 0)
1090643ee871SKonstantin Belousov 			kern_ktimer_delete(td, id);
109186857b36SDavid Xu 	}
109286857b36SDavid Xu 	return (error);
109386857b36SDavid Xu }
109486857b36SDavid Xu 
1095643ee871SKonstantin Belousov int
1096643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1097643ee871SKonstantin Belousov     int *timerid, int preset_id)
109886857b36SDavid Xu {
109986857b36SDavid Xu 	struct proc *p = td->td_proc;
110086857b36SDavid Xu 	struct itimer *it;
110186857b36SDavid Xu 	int id;
110286857b36SDavid Xu 	int error;
110386857b36SDavid Xu 
110486857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
110586857b36SDavid Xu 		return (EINVAL);
110686857b36SDavid Xu 
110786857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
110886857b36SDavid Xu 		return (EINVAL);
110986857b36SDavid Xu 
111086857b36SDavid Xu 	if (evp != NULL) {
111186857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
111256c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
111356c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
111486857b36SDavid Xu 			return (EINVAL);
111556c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
111656c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
111786857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
111886857b36SDavid Xu 			return (EINVAL);
111986857b36SDavid Xu 	}
112086857b36SDavid Xu 
112160354683SDavid Xu 	if (p->p_itimers == NULL)
112286857b36SDavid Xu 		itimers_alloc(p);
112386857b36SDavid Xu 
112486857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
112586857b36SDavid Xu 	it->it_flags = 0;
112686857b36SDavid Xu 	it->it_usecount = 0;
112786857b36SDavid Xu 	it->it_active = 0;
112856c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
112956c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
113086857b36SDavid Xu 	it->it_overrun = 0;
113186857b36SDavid Xu 	it->it_overrun_last = 0;
113286857b36SDavid Xu 	it->it_clockid = clock_id;
113386857b36SDavid Xu 	it->it_timerid = -1;
113486857b36SDavid Xu 	it->it_proc = p;
113586857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
113686857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
113786857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
113886857b36SDavid Xu 	if (error != 0)
113986857b36SDavid Xu 		goto out;
114086857b36SDavid Xu 
114186857b36SDavid Xu 	PROC_LOCK(p);
114286857b36SDavid Xu 	if (preset_id != -1) {
114386857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
114486857b36SDavid Xu 		id = preset_id;
114560354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
114686857b36SDavid Xu 			PROC_UNLOCK(p);
114786857b36SDavid Xu 			error = 0;
114886857b36SDavid Xu 			goto out;
114986857b36SDavid Xu 		}
115086857b36SDavid Xu 	} else {
115186857b36SDavid Xu 		/*
115286857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
115386857b36SDavid Xu 		 * for setitimer().
115486857b36SDavid Xu 		 */
115586857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
115660354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
115786857b36SDavid Xu 				break;
115886857b36SDavid Xu 		if (id == TIMER_MAX) {
115986857b36SDavid Xu 			PROC_UNLOCK(p);
116086857b36SDavid Xu 			error = EAGAIN;
116186857b36SDavid Xu 			goto out;
116286857b36SDavid Xu 		}
116386857b36SDavid Xu 	}
116486857b36SDavid Xu 	it->it_timerid = id;
116560354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
116686857b36SDavid Xu 	if (evp != NULL)
116786857b36SDavid Xu 		it->it_sigev = *evp;
116886857b36SDavid Xu 	else {
116986857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
117086857b36SDavid Xu 		switch (clock_id) {
117186857b36SDavid Xu 		default:
117286857b36SDavid Xu 		case CLOCK_REALTIME:
117386857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
117486857b36SDavid Xu 			break;
117586857b36SDavid Xu 		case CLOCK_VIRTUAL:
117686857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
117786857b36SDavid Xu 			break;
117886857b36SDavid Xu 		case CLOCK_PROF:
117986857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
118086857b36SDavid Xu 			break;
118186857b36SDavid Xu 		}
11828f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
118386857b36SDavid Xu 	}
118486857b36SDavid Xu 
118556c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
118656c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
118786857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
118886857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
118986857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
119086857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
119186857b36SDavid Xu 	}
119286857b36SDavid Xu 	PROC_UNLOCK(p);
119386857b36SDavid Xu 	*timerid = id;
119486857b36SDavid Xu 	return (0);
119586857b36SDavid Xu 
119686857b36SDavid Xu out:
119786857b36SDavid Xu 	ITIMER_LOCK(it);
119886857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
119986857b36SDavid Xu 	ITIMER_UNLOCK(it);
120086857b36SDavid Xu 	uma_zfree(itimer_zone, it);
120186857b36SDavid Xu 	return (error);
120286857b36SDavid Xu }
120386857b36SDavid Xu 
120486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
120561d3a4efSDavid Xu struct ktimer_delete_args {
120661d3a4efSDavid Xu 	int timerid;
120786857b36SDavid Xu };
120886857b36SDavid Xu #endif
120986857b36SDavid Xu int
12108451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
121186857b36SDavid Xu {
1212643ee871SKonstantin Belousov 
1213643ee871SKonstantin Belousov 	return (kern_ktimer_delete(td, uap->timerid));
121486857b36SDavid Xu }
121586857b36SDavid Xu 
121686857b36SDavid Xu static struct itimer *
1217843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
121886857b36SDavid Xu {
121986857b36SDavid Xu 	struct itimer *it;
122086857b36SDavid Xu 
122186857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
12223f935cf3SColin Percival 	if ((p->p_itimers == NULL) ||
12233f935cf3SColin Percival 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
122460354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
122586857b36SDavid Xu 		return (NULL);
122686857b36SDavid Xu 	}
122786857b36SDavid Xu 	ITIMER_LOCK(it);
1228843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
122986857b36SDavid Xu 		ITIMER_UNLOCK(it);
123086857b36SDavid Xu 		it = NULL;
123186857b36SDavid Xu 	}
123286857b36SDavid Xu 	return (it);
123386857b36SDavid Xu }
123486857b36SDavid Xu 
1235643ee871SKonstantin Belousov int
1236643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid)
123786857b36SDavid Xu {
123886857b36SDavid Xu 	struct proc *p = td->td_proc;
123986857b36SDavid Xu 	struct itimer *it;
124086857b36SDavid Xu 
124186857b36SDavid Xu 	PROC_LOCK(p);
1242843b99c6SDavid Xu 	it = itimer_find(p, timerid);
124386857b36SDavid Xu 	if (it == NULL) {
124486857b36SDavid Xu 		PROC_UNLOCK(p);
124586857b36SDavid Xu 		return (EINVAL);
124686857b36SDavid Xu 	}
124786857b36SDavid Xu 	PROC_UNLOCK(p);
124886857b36SDavid Xu 
124986857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
125086857b36SDavid Xu 	while (it->it_usecount > 0) {
125186857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
125286857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
125386857b36SDavid Xu 	}
125486857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
125586857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
125686857b36SDavid Xu 	ITIMER_UNLOCK(it);
125786857b36SDavid Xu 
125886857b36SDavid Xu 	PROC_LOCK(p);
125986857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
126086857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
126160354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
126286857b36SDavid Xu 	PROC_UNLOCK(p);
126386857b36SDavid Xu 	uma_zfree(itimer_zone, it);
126486857b36SDavid Xu 	return (0);
126586857b36SDavid Xu }
126686857b36SDavid Xu 
126786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
126861d3a4efSDavid Xu struct ktimer_settime_args {
126961d3a4efSDavid Xu 	int timerid;
127086857b36SDavid Xu 	int flags;
127186857b36SDavid Xu 	const struct itimerspec * value;
127286857b36SDavid Xu 	struct itimerspec * ovalue;
127386857b36SDavid Xu };
127486857b36SDavid Xu #endif
127586857b36SDavid Xu int
12768451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
127786857b36SDavid Xu {
127886857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
127986857b36SDavid Xu 	int error;
128086857b36SDavid Xu 
128186857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
128286857b36SDavid Xu 	if (error != 0)
128386857b36SDavid Xu 		return (error);
1284643ee871SKonstantin Belousov 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1285643ee871SKonstantin Belousov 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1286643ee871SKonstantin Belousov 	if (error == 0 && uap->ovalue != NULL)
1287643ee871SKonstantin Belousov 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1288643ee871SKonstantin Belousov 	return (error);
1289643ee871SKonstantin Belousov }
129086857b36SDavid Xu 
1291643ee871SKonstantin Belousov int
1292643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1293643ee871SKonstantin Belousov     struct itimerspec *val, struct itimerspec *oval)
1294643ee871SKonstantin Belousov {
1295643ee871SKonstantin Belousov 	struct proc *p;
1296643ee871SKonstantin Belousov 	struct itimer *it;
1297643ee871SKonstantin Belousov 	int error;
129886857b36SDavid Xu 
1299643ee871SKonstantin Belousov 	p = td->td_proc;
130086857b36SDavid Xu 	PROC_LOCK(p);
1301643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
130286857b36SDavid Xu 		PROC_UNLOCK(p);
130386857b36SDavid Xu 		error = EINVAL;
130486857b36SDavid Xu 	} else {
130586857b36SDavid Xu 		PROC_UNLOCK(p);
130686857b36SDavid Xu 		itimer_enter(it);
1307643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1308643ee871SKonstantin Belousov 		    flags, val, oval));
130986857b36SDavid Xu 		itimer_leave(it);
131086857b36SDavid Xu 		ITIMER_UNLOCK(it);
131186857b36SDavid Xu 	}
131286857b36SDavid Xu 	return (error);
131386857b36SDavid Xu }
131486857b36SDavid Xu 
131586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
131661d3a4efSDavid Xu struct ktimer_gettime_args {
131761d3a4efSDavid Xu 	int timerid;
131886857b36SDavid Xu 	struct itimerspec * value;
131986857b36SDavid Xu };
132086857b36SDavid Xu #endif
132186857b36SDavid Xu int
13228451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
132386857b36SDavid Xu {
132486857b36SDavid Xu 	struct itimerspec val;
132586857b36SDavid Xu 	int error;
132686857b36SDavid Xu 
1327643ee871SKonstantin Belousov 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1328643ee871SKonstantin Belousov 	if (error == 0)
1329643ee871SKonstantin Belousov 		error = copyout(&val, uap->value, sizeof(val));
1330643ee871SKonstantin Belousov 	return (error);
1331643ee871SKonstantin Belousov }
1332643ee871SKonstantin Belousov 
1333643ee871SKonstantin Belousov int
1334643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1335643ee871SKonstantin Belousov {
1336643ee871SKonstantin Belousov 	struct proc *p;
1337643ee871SKonstantin Belousov 	struct itimer *it;
1338643ee871SKonstantin Belousov 	int error;
1339643ee871SKonstantin Belousov 
1340643ee871SKonstantin Belousov 	p = td->td_proc;
134186857b36SDavid Xu 	PROC_LOCK(p);
1342643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
134386857b36SDavid Xu 		PROC_UNLOCK(p);
134486857b36SDavid Xu 		error = EINVAL;
134586857b36SDavid Xu 	} else {
134686857b36SDavid Xu 		PROC_UNLOCK(p);
134786857b36SDavid Xu 		itimer_enter(it);
1348643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
134986857b36SDavid Xu 		itimer_leave(it);
135086857b36SDavid Xu 		ITIMER_UNLOCK(it);
135186857b36SDavid Xu 	}
135286857b36SDavid Xu 	return (error);
135386857b36SDavid Xu }
135486857b36SDavid Xu 
135586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
135686857b36SDavid Xu struct timer_getoverrun_args {
135761d3a4efSDavid Xu 	int timerid;
135886857b36SDavid Xu };
135986857b36SDavid Xu #endif
136086857b36SDavid Xu int
13618451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
136286857b36SDavid Xu {
1363e346f8c4SBjoern A. Zeeb 
1364e346f8c4SBjoern A. Zeeb 	return (kern_ktimer_getoverrun(td, uap->timerid));
1365e346f8c4SBjoern A. Zeeb }
1366e346f8c4SBjoern A. Zeeb 
1367e346f8c4SBjoern A. Zeeb int
1368e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id)
1369e346f8c4SBjoern A. Zeeb {
137086857b36SDavid Xu 	struct proc *p = td->td_proc;
137186857b36SDavid Xu 	struct itimer *it;
137286857b36SDavid Xu 	int error ;
137386857b36SDavid Xu 
137486857b36SDavid Xu 	PROC_LOCK(p);
1375e346f8c4SBjoern A. Zeeb 	if (timer_id < 3 ||
1376e346f8c4SBjoern A. Zeeb 	    (it = itimer_find(p, timer_id)) == NULL) {
137786857b36SDavid Xu 		PROC_UNLOCK(p);
137886857b36SDavid Xu 		error = EINVAL;
137986857b36SDavid Xu 	} else {
138086857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
138186857b36SDavid Xu 		ITIMER_UNLOCK(it);
138256c06c4bSDavid Xu 		PROC_UNLOCK(p);
138386857b36SDavid Xu 		error = 0;
138486857b36SDavid Xu 	}
138586857b36SDavid Xu 	return (error);
138686857b36SDavid Xu }
138786857b36SDavid Xu 
138886857b36SDavid Xu static int
138986857b36SDavid Xu realtimer_create(struct itimer *it)
139086857b36SDavid Xu {
139186857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
139286857b36SDavid Xu 	return (0);
139386857b36SDavid Xu }
139486857b36SDavid Xu 
139586857b36SDavid Xu static int
139686857b36SDavid Xu realtimer_delete(struct itimer *it)
139786857b36SDavid Xu {
139886857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1399843b99c6SDavid Xu 
1400d6b6592eSDavid Xu 	/*
1401d6b6592eSDavid Xu 	 * clear timer's value and interval to tell realtimer_expire
1402d6b6592eSDavid Xu 	 * to not rearm the timer.
1403d6b6592eSDavid Xu 	 */
1404d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_value);
1405d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_interval);
1406843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1407843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1408843b99c6SDavid Xu 	ITIMER_LOCK(it);
140986857b36SDavid Xu 	return (0);
141086857b36SDavid Xu }
141186857b36SDavid Xu 
141286857b36SDavid Xu static int
141386857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
141486857b36SDavid Xu {
141556c06c4bSDavid Xu 	struct timespec cts;
141686857b36SDavid Xu 
141786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
141886857b36SDavid Xu 
141956c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
142056c06c4bSDavid Xu 	*ovalue = it->it_time;
142186857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
142256c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
142386857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
142486857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
142586857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
142686857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
142786857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
142886857b36SDavid Xu 		}
142986857b36SDavid Xu 	}
143086857b36SDavid Xu 	return (0);
143186857b36SDavid Xu }
143286857b36SDavid Xu 
143386857b36SDavid Xu static int
143486857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
143586857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
143686857b36SDavid Xu {
143756c06c4bSDavid Xu 	struct timespec cts, ts;
143856c06c4bSDavid Xu 	struct timeval tv;
143956c06c4bSDavid Xu 	struct itimerspec val;
144086857b36SDavid Xu 
144186857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
144286857b36SDavid Xu 
144356c06c4bSDavid Xu 	val = *value;
144456c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
144586857b36SDavid Xu 		return (EINVAL);
144686857b36SDavid Xu 
144756c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
144856c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
144986857b36SDavid Xu 			return (EINVAL);
145086857b36SDavid Xu 	} else {
145156c06c4bSDavid Xu 		timespecclear(&val.it_interval);
145286857b36SDavid Xu 	}
145386857b36SDavid Xu 
145486857b36SDavid Xu 	if (ovalue != NULL)
145586857b36SDavid Xu 		realtimer_gettime(it, ovalue);
145686857b36SDavid Xu 
145786857b36SDavid Xu 	it->it_time = val;
145856c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
145956c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
146056c06c4bSDavid Xu 		ts = val.it_value;
146186857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
146286857b36SDavid Xu 			/* Convert to absolute time. */
146356c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
146486857b36SDavid Xu 		} else {
146556c06c4bSDavid Xu 			timespecsub(&ts, &cts);
146686857b36SDavid Xu 			/*
146756c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
146886857b36SDavid Xu 			 * fix it.
146986857b36SDavid Xu 			 */
147086857b36SDavid Xu 		}
147156c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
147256c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
147386857b36SDavid Xu 			realtimer_expire, it);
147486857b36SDavid Xu 	} else {
147586857b36SDavid Xu 		callout_stop(&it->it_callout);
147686857b36SDavid Xu 	}
147786857b36SDavid Xu 
147886857b36SDavid Xu 	return (0);
147986857b36SDavid Xu }
148086857b36SDavid Xu 
148186857b36SDavid Xu static void
148256c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
148386857b36SDavid Xu {
148486857b36SDavid Xu 	if (id == CLOCK_REALTIME)
148556c06c4bSDavid Xu 		getnanotime(ts);
148686857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
148756c06c4bSDavid Xu 		getnanouptime(ts);
148856c06c4bSDavid Xu }
148956c06c4bSDavid Xu 
149056c06c4bSDavid Xu int
149161d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
149256c06c4bSDavid Xu {
149356c06c4bSDavid Xu 	struct itimer *it;
149456c06c4bSDavid Xu 
149556c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1496843b99c6SDavid Xu 	it = itimer_find(p, timerid);
149756c06c4bSDavid Xu 	if (it != NULL) {
149856c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
149956c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
150056c06c4bSDavid Xu 		it->it_overrun = 0;
150156c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
150256c06c4bSDavid Xu 		return (0);
150356c06c4bSDavid Xu 	}
150456c06c4bSDavid Xu 	return (EINVAL);
150556c06c4bSDavid Xu }
150656c06c4bSDavid Xu 
150756c06c4bSDavid Xu int
150856c06c4bSDavid Xu itimespecfix(struct timespec *ts)
150956c06c4bSDavid Xu {
151056c06c4bSDavid Xu 
151156c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
151256c06c4bSDavid Xu 		return (EINVAL);
151356c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
151456c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
151556c06c4bSDavid Xu 	return (0);
151686857b36SDavid Xu }
151786857b36SDavid Xu 
151886857b36SDavid Xu /* Timeout callback for realtime timer */
151986857b36SDavid Xu static void
152086857b36SDavid Xu realtimer_expire(void *arg)
152186857b36SDavid Xu {
152256c06c4bSDavid Xu 	struct timespec cts, ts;
152356c06c4bSDavid Xu 	struct timeval tv;
152486857b36SDavid Xu 	struct itimer *it;
152586857b36SDavid Xu 
152686857b36SDavid Xu 	it = (struct itimer *)arg;
152786857b36SDavid Xu 
152856c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
152986857b36SDavid Xu 	/* Only fire if time is reached. */
153056c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
153156c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
153256c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
153386857b36SDavid Xu 				    &it->it_time.it_interval);
153456c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
153577e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
153686857b36SDavid Xu 					it->it_overrun++;
153777e718f7SDavid Xu 				else
153877e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
153956c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
154086857b36SDavid Xu 					    &it->it_time.it_interval);
154186857b36SDavid Xu 			}
154286857b36SDavid Xu 		} else {
154386857b36SDavid Xu 			/* single shot timer ? */
154456c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
154586857b36SDavid Xu 		}
154656c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
154756c06c4bSDavid Xu 			ts = it->it_time.it_value;
154856c06c4bSDavid Xu 			timespecsub(&ts, &cts);
154956c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
155056c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
155186857b36SDavid Xu 				 realtimer_expire, it);
155286857b36SDavid Xu 		}
1553d6b6592eSDavid Xu 		itimer_enter(it);
155486857b36SDavid Xu 		ITIMER_UNLOCK(it);
155586857b36SDavid Xu 		itimer_fire(it);
155686857b36SDavid Xu 		ITIMER_LOCK(it);
1557d6b6592eSDavid Xu 		itimer_leave(it);
155856c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
155956c06c4bSDavid Xu 		ts = it->it_time.it_value;
156056c06c4bSDavid Xu 		timespecsub(&ts, &cts);
156156c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
156256c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
156386857b36SDavid Xu  			it);
156486857b36SDavid Xu 	}
156586857b36SDavid Xu }
156686857b36SDavid Xu 
156786857b36SDavid Xu void
156886857b36SDavid Xu itimer_fire(struct itimer *it)
156986857b36SDavid Xu {
157086857b36SDavid Xu 	struct proc *p = it->it_proc;
1571cf7d9a8cSDavid Xu 	struct thread *td;
157286857b36SDavid Xu 
157356c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
157456c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1575cf7d9a8cSDavid Xu 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
157656c06c4bSDavid Xu 			ITIMER_LOCK(it);
157756c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
157856c06c4bSDavid Xu 			timespecclear(&it->it_time.it_interval);
157956c06c4bSDavid Xu 			callout_stop(&it->it_callout);
158056c06c4bSDavid Xu 			ITIMER_UNLOCK(it);
1581cf7d9a8cSDavid Xu 			return;
15826d7b314bSDavid Xu 		}
1583cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
1584cf7d9a8cSDavid Xu 			it->it_ksi.ksi_errno = 0;
1585cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1586cf7d9a8cSDavid Xu 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
158756c06c4bSDavid Xu 		} else {
158877e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
15896d7b314bSDavid Xu 				it->it_overrun++;
159077e718f7SDavid Xu 			else
159177e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
159256c06c4bSDavid Xu 		}
159386857b36SDavid Xu 		PROC_UNLOCK(p);
159486857b36SDavid Xu 	}
159586857b36SDavid Xu }
159686857b36SDavid Xu 
159786857b36SDavid Xu static void
159886857b36SDavid Xu itimers_alloc(struct proc *p)
159986857b36SDavid Xu {
160060354683SDavid Xu 	struct itimers *its;
160160354683SDavid Xu 	int i;
160286857b36SDavid Xu 
160360354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
160486857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
160586857b36SDavid Xu 	LIST_INIT(&its->its_prof);
160686857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
160760354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
160860354683SDavid Xu 		its->its_timers[i] = NULL;
160960354683SDavid Xu 	PROC_LOCK(p);
161060354683SDavid Xu 	if (p->p_itimers == NULL) {
161160354683SDavid Xu 		p->p_itimers = its;
161260354683SDavid Xu 		PROC_UNLOCK(p);
161360354683SDavid Xu 	}
161460354683SDavid Xu 	else {
161560354683SDavid Xu 		PROC_UNLOCK(p);
161660354683SDavid Xu 		free(its, M_SUBPROC);
161760354683SDavid Xu 	}
161886857b36SDavid Xu }
161986857b36SDavid Xu 
1620993182e5SAlexander Leidinger static void
1621993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1622993182e5SAlexander Leidinger {
1623993182e5SAlexander Leidinger 	itimers_event_hook_exit(arg, p);
1624993182e5SAlexander Leidinger }
1625993182e5SAlexander Leidinger 
162686857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1627d26b1a1fSDavid Xu static void
1628993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
162986857b36SDavid Xu {
163086857b36SDavid Xu 	struct itimers *its;
163186857b36SDavid Xu 	struct itimer *it;
16323e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
163386857b36SDavid Xu 	int i;
163486857b36SDavid Xu 
163560354683SDavid Xu 	if (p->p_itimers != NULL) {
163660354683SDavid Xu 		its = p->p_itimers;
163786857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
163886857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
163986857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
164086857b36SDavid Xu 		}
164186857b36SDavid Xu 		/*
164286857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
164386857b36SDavid Xu 		 * by new image.
164486857b36SDavid Xu 		 */
164586857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
164686857b36SDavid Xu 			i = 3;
164786857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
164886857b36SDavid Xu 			i = 0;
164986857b36SDavid Xu 		else
165086857b36SDavid Xu 			panic("unhandled event");
165186857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
1652843b99c6SDavid Xu 			if ((it = its->its_timers[i]) != NULL)
1653643ee871SKonstantin Belousov 				kern_ktimer_delete(curthread, i);
165486857b36SDavid Xu 		}
165586857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
165686857b36SDavid Xu 		    its->its_timers[1] == NULL &&
165786857b36SDavid Xu 		    its->its_timers[2] == NULL) {
165860354683SDavid Xu 			free(its, M_SUBPROC);
165960354683SDavid Xu 			p->p_itimers = NULL;
166086857b36SDavid Xu 		}
166186857b36SDavid Xu 	}
166286857b36SDavid Xu }
1663