xref: /freebsd/sys/kern/kern_time.c (revision 83158c68934b4fae6897bbe1694cf9a7f4b9c0b1)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1569a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
37de56aee0SKonstantin Belousov #include "opt_ktrace.h"
38de56aee0SKonstantin Belousov 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40e96c1fdcSPeter Wemm #include <sys/systm.h>
41aff5bcb1SDavid Xu #include <sys/limits.h>
42f645b0b5SPoul-Henning Kamp #include <sys/clock.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44fb919e4dSMark Murray #include <sys/mutex.h>
45d2d3e875SBruce Evans #include <sys/sysproto.h>
46df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
47797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
49098176f0SDavide Italiano #include <sys/sleepqueue.h>
50efa42cbcSPaul Saab #include <sys/syscallsubr.h>
5177e718f7SDavid Xu #include <sys/sysctl.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
546aeb05d7STom Rhodes #include <sys/posix4.h>
55708e7684SPeter Wemm #include <sys/time.h>
5686857b36SDavid Xu #include <sys/timers.h>
5791266b96SPoul-Henning Kamp #include <sys/timetc.h>
58df8bae1dSRodney W. Grimes #include <sys/vnode.h>
59de56aee0SKonstantin Belousov #ifdef KTRACE
60de56aee0SKonstantin Belousov #include <sys/ktrace.h>
61de56aee0SKonstantin Belousov #endif
62fb919e4dSMark Murray 
635b870b7bSPeter Wemm #include <vm/vm.h>
645b870b7bSPeter Wemm #include <vm/vm_extern.h>
65df8bae1dSRodney W. Grimes 
6686857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
67d65f1abcSDavid Xu #define CPUCLOCK_BIT		0x80000000
68d65f1abcSDavid Xu #define CPUCLOCK_PROCESS_BIT	0x40000000
69d65f1abcSDavid Xu #define CPUCLOCK_ID_MASK	(~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
70d65f1abcSDavid Xu #define MAKE_THREAD_CPUCLOCK(tid)	(CPUCLOCK_BIT|(tid))
71d65f1abcSDavid Xu #define MAKE_PROCESS_CPUCLOCK(pid)	\
72d65f1abcSDavid Xu 	(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
7386857b36SDavid Xu 
748b3c4231SMark Johnston #define NS_PER_SEC	1000000000
758b3c4231SMark Johnston 
7686857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
7786857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
7886857b36SDavid Xu 
79df8bae1dSRodney W. Grimes /*
80df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
81df8bae1dSRodney W. Grimes  *
82df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
83df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
84df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
85df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
86df8bae1dSRodney W. Grimes  * timers when they expire.
87df8bae1dSRodney W. Grimes  */
88df8bae1dSRodney W. Grimes 
897edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
904d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
913f8455b0SEric van Gyzen static int	user_clock_nanosleep(struct thread *td, clockid_t clock_id,
923f8455b0SEric van Gyzen 		    int flags, const struct timespec *ua_rqtp,
933f8455b0SEric van Gyzen 		    struct timespec *ua_rmtp);
9494c8fcd8SPeter Wemm 
9586857b36SDavid Xu static void	itimer_start(void);
9686857b36SDavid Xu static int	itimer_init(void *, int, int);
9786857b36SDavid Xu static void	itimer_fini(void *, int);
9886857b36SDavid Xu static void	itimer_enter(struct itimer *);
9986857b36SDavid Xu static void	itimer_leave(struct itimer *);
100843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int);
10186857b36SDavid Xu static void	itimers_alloc(struct proc *);
10286857b36SDavid Xu static int	realtimer_create(struct itimer *);
10386857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
10486857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
10586857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
10686857b36SDavid Xu static int	realtimer_delete(struct itimer *);
10756c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
10886857b36SDavid Xu static void	realtimer_expire(void *);
1095cc1d199SKonstantin Belousov static void	realtimer_expire_l(struct itimer *it, bool proc_locked);
11086857b36SDavid Xu 
111ef221ff6SMark Johnston static void	realitexpire(void *arg);
112ef221ff6SMark Johnston 
113e68c6191SKonstantin Belousov static int	register_posix_clock(int, const struct kclock *);
1148ff2b41cSMark Johnston static void	itimer_fire(struct itimer *it);
1158ff2b41cSMark Johnston static int	itimespecfix(struct timespec *ts);
11686857b36SDavid Xu 
11786857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
11886857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
11986857b36SDavid Xu 
12086857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
12186857b36SDavid Xu 
12294c8fcd8SPeter Wemm static int
12391afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
12494c8fcd8SPeter Wemm {
125fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
126c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1277ec73f64SPoul-Henning Kamp 	struct timespec ts;
12894c8fcd8SPeter Wemm 
1299c8fff87SBruce Evans 	microtime(&tv1);
13000af9731SPoul-Henning Kamp 	delta = *tv;
13100af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
13294c8fcd8SPeter Wemm 
13394c8fcd8SPeter Wemm 	/*
1349c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
135fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
136fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
137fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
138fcae3aa6SNick Sayer 	 * back to the past.
139c0bd94a7SNick Sayer 	 *
140c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
141c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
142c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14394c8fcd8SPeter Wemm 	 */
1447edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
145fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1463f92429aSMatt Jacob 			/*
147c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1483f92429aSMatt Jacob 			 */
149fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
150fcae3aa6SNick Sayer 				maxtime = tv1;
151fcae3aa6SNick Sayer 			tv2 = *tv;
152fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
153fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1543f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
155fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
156fcae3aa6SNick Sayer 			}
1573f92429aSMatt Jacob 		} else {
1581822421cSKonstantin Belousov 			if (tv1.tv_sec == laststep.tv_sec)
159c0bd94a7SNick Sayer 				return (EPERM);
160c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
161c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
162c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
163c0bd94a7SNick Sayer 			}
164c0bd94a7SNick Sayer 			laststep = *tv;
165fcae3aa6SNick Sayer 		}
1669c8fff87SBruce Evans 	}
1679c8fff87SBruce Evans 
1687ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1697ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
17091266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17194c8fcd8SPeter Wemm 	resettodr();
17294c8fcd8SPeter Wemm 	return (0);
17394c8fcd8SPeter Wemm }
17494c8fcd8SPeter Wemm 
17594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
176d65f1abcSDavid Xu struct clock_getcpuclockid2_args {
177d65f1abcSDavid Xu 	id_t id;
178d65f1abcSDavid Xu 	int which,
179d65f1abcSDavid Xu 	clockid_t *clock_id;
180d65f1abcSDavid Xu };
181d65f1abcSDavid Xu #endif
182d65f1abcSDavid Xu /* ARGSUSED */
183d65f1abcSDavid Xu int
184d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
185d65f1abcSDavid Xu {
186d65f1abcSDavid Xu 	clockid_t clk_id;
187d31e4b3aSKonstantin Belousov 	int error;
188d31e4b3aSKonstantin Belousov 
189d31e4b3aSKonstantin Belousov 	error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
190d31e4b3aSKonstantin Belousov 	if (error == 0)
191d31e4b3aSKonstantin Belousov 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
192d31e4b3aSKonstantin Belousov 	return (error);
193d31e4b3aSKonstantin Belousov }
194d31e4b3aSKonstantin Belousov 
195d31e4b3aSKonstantin Belousov int
196d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
197d31e4b3aSKonstantin Belousov     clockid_t *clk_id)
198d31e4b3aSKonstantin Belousov {
199d65f1abcSDavid Xu 	struct proc *p;
200d65f1abcSDavid Xu 	pid_t pid;
201d65f1abcSDavid Xu 	lwpid_t tid;
202d65f1abcSDavid Xu 	int error;
203d65f1abcSDavid Xu 
204d31e4b3aSKonstantin Belousov 	switch (which) {
205d65f1abcSDavid Xu 	case CPUCLOCK_WHICH_PID:
206d31e4b3aSKonstantin Belousov 		if (id != 0) {
20747ce3e53SDmitry Chagin 			error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
208d31e4b3aSKonstantin Belousov 			if (error != 0)
209d65f1abcSDavid Xu 				return (error);
21047ce3e53SDmitry Chagin 			PROC_UNLOCK(p);
211d31e4b3aSKonstantin Belousov 			pid = id;
212d65f1abcSDavid Xu 		} else {
213d65f1abcSDavid Xu 			pid = td->td_proc->p_pid;
214d65f1abcSDavid Xu 		}
215d31e4b3aSKonstantin Belousov 		*clk_id = MAKE_PROCESS_CPUCLOCK(pid);
216d31e4b3aSKonstantin Belousov 		return (0);
217d65f1abcSDavid Xu 	case CPUCLOCK_WHICH_TID:
218d31e4b3aSKonstantin Belousov 		tid = id == 0 ? td->td_tid : id;
219d31e4b3aSKonstantin Belousov 		*clk_id = MAKE_THREAD_CPUCLOCK(tid);
220d31e4b3aSKonstantin Belousov 		return (0);
221d65f1abcSDavid Xu 	default:
222d65f1abcSDavid Xu 		return (EINVAL);
223d65f1abcSDavid Xu 	}
224d65f1abcSDavid Xu }
225d65f1abcSDavid Xu 
226d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_
22794c8fcd8SPeter Wemm struct clock_gettime_args {
22894c8fcd8SPeter Wemm 	clockid_t clock_id;
22994c8fcd8SPeter Wemm 	struct	timespec *tp;
23094c8fcd8SPeter Wemm };
23194c8fcd8SPeter Wemm #endif
23294c8fcd8SPeter Wemm /* ARGSUSED */
23394c8fcd8SPeter Wemm int
2348451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
23594c8fcd8SPeter Wemm {
23694c8fcd8SPeter Wemm 	struct timespec ats;
237f0b479cdSPaul Saab 	int error;
238f0b479cdSPaul Saab 
239f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
240f0b479cdSPaul Saab 	if (error == 0)
241f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
242f0b479cdSPaul Saab 
243f0b479cdSPaul Saab 	return (error);
244f0b479cdSPaul Saab }
245f0b479cdSPaul Saab 
246d65f1abcSDavid Xu static inline void
247d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats)
248d65f1abcSDavid Xu {
249bb53dd56Sfirk 	uint64_t tr;
250bb53dd56Sfirk 	tr = cpu_tickrate();
251bb53dd56Sfirk 	ats->tv_sec = runtime / tr;
252bb53dd56Sfirk 	ats->tv_nsec = ((runtime % tr) * 1000000000ULL) / tr;
253d65f1abcSDavid Xu }
254d65f1abcSDavid Xu 
255cbc10891SDmitry Chagin void
256cbc10891SDmitry Chagin kern_thread_cputime(struct thread *targettd, struct timespec *ats)
257d65f1abcSDavid Xu {
258d65f1abcSDavid Xu 	uint64_t runtime, curtime, switchtime;
259d65f1abcSDavid Xu 
260d65f1abcSDavid Xu 	if (targettd == NULL) { /* current thread */
26128d08dc7Sfirk 		spinlock_enter();
262d65f1abcSDavid Xu 		switchtime = PCPU_GET(switchtime);
263d65f1abcSDavid Xu 		curtime = cpu_ticks();
264d65f1abcSDavid Xu 		runtime = curthread->td_runtime;
26528d08dc7Sfirk 		spinlock_exit();
266d65f1abcSDavid Xu 		runtime += curtime - switchtime;
267d65f1abcSDavid Xu 	} else {
2680783b709SKonstantin Belousov 		PROC_LOCK_ASSERT(targettd->td_proc, MA_OWNED);
269d65f1abcSDavid Xu 		thread_lock(targettd);
270d65f1abcSDavid Xu 		runtime = targettd->td_runtime;
271d65f1abcSDavid Xu 		thread_unlock(targettd);
272d65f1abcSDavid Xu 	}
273d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
274d65f1abcSDavid Xu }
275d65f1abcSDavid Xu 
276cbc10891SDmitry Chagin void
277cbc10891SDmitry Chagin kern_process_cputime(struct proc *targetp, struct timespec *ats)
278d65f1abcSDavid Xu {
279d65f1abcSDavid Xu 	uint64_t runtime;
280d65f1abcSDavid Xu 	struct rusage ru;
281d65f1abcSDavid Xu 
282cbc10891SDmitry Chagin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
2835c7bebf9SKonstantin Belousov 	PROC_STATLOCK(targetp);
284d65f1abcSDavid Xu 	rufetch(targetp, &ru);
285d65f1abcSDavid Xu 	runtime = targetp->p_rux.rux_runtime;
2867e8db781SColin Percival 	if (curthread->td_proc == targetp)
2877e8db781SColin Percival 		runtime += cpu_ticks() - PCPU_GET(switchtime);
2885c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(targetp);
289d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
290d65f1abcSDavid Xu }
291d65f1abcSDavid Xu 
292d65f1abcSDavid Xu static int
293d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
294d65f1abcSDavid Xu {
295d65f1abcSDavid Xu 	struct proc *p, *p2;
296d65f1abcSDavid Xu 	struct thread *td2;
297d65f1abcSDavid Xu 	lwpid_t tid;
298d65f1abcSDavid Xu 	pid_t pid;
299d65f1abcSDavid Xu 	int error;
300d65f1abcSDavid Xu 
301d65f1abcSDavid Xu 	p = td->td_proc;
302d65f1abcSDavid Xu 	if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
303d65f1abcSDavid Xu 		tid = clock_id & CPUCLOCK_ID_MASK;
304d65f1abcSDavid Xu 		td2 = tdfind(tid, p->p_pid);
305d65f1abcSDavid Xu 		if (td2 == NULL)
306d65f1abcSDavid Xu 			return (EINVAL);
307cbc10891SDmitry Chagin 		kern_thread_cputime(td2, ats);
308d65f1abcSDavid Xu 		PROC_UNLOCK(td2->td_proc);
309d65f1abcSDavid Xu 	} else {
310d65f1abcSDavid Xu 		pid = clock_id & CPUCLOCK_ID_MASK;
311c7c536c7SKonstantin Belousov 		error = pget(pid, PGET_CANSEE, &p2);
312c7c536c7SKonstantin Belousov 		if (error != 0)
313d65f1abcSDavid Xu 			return (EINVAL);
314cbc10891SDmitry Chagin 		kern_process_cputime(p2, ats);
315d65f1abcSDavid Xu 		PROC_UNLOCK(p2);
316d65f1abcSDavid Xu 	}
317d65f1abcSDavid Xu 	return (0);
318d65f1abcSDavid Xu }
319d65f1abcSDavid Xu 
320f0b479cdSPaul Saab int
321f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
322f0b479cdSPaul Saab {
323de0a9241SKelly Yancey 	struct timeval sys, user;
32478c85e8dSJohn Baldwin 	struct proc *p;
32594c8fcd8SPeter Wemm 
32678c85e8dSJohn Baldwin 	p = td->td_proc;
327f0b479cdSPaul Saab 	switch (clock_id) {
3285e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
3295e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
330f0b479cdSPaul Saab 		nanotime(ats);
331b8817154SKelly Yancey 		break;
3325e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3335e758b95SRobert Watson 		getnanotime(ats);
3345e758b95SRobert Watson 		break;
335b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
33678c85e8dSJohn Baldwin 		PROC_LOCK(p);
3375c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
33878c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3395c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
34078c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
341f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
342b8817154SKelly Yancey 		break;
343b8817154SKelly Yancey 	case CLOCK_PROF:
34478c85e8dSJohn Baldwin 		PROC_LOCK(p);
3455c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
34678c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3475c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
34878c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
349de0a9241SKelly Yancey 		timevaladd(&user, &sys);
350f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
351de0a9241SKelly Yancey 		break;
3525e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
3535e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3545eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3555e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
356f0b479cdSPaul Saab 		nanouptime(ats);
357b8817154SKelly Yancey 		break;
3585e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3595e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3605e758b95SRobert Watson 		getnanouptime(ats);
3615e758b95SRobert Watson 		break;
3625e758b95SRobert Watson 	case CLOCK_SECOND:
3635e758b95SRobert Watson 		ats->tv_sec = time_second;
3645e758b95SRobert Watson 		ats->tv_nsec = 0;
3655e758b95SRobert Watson 		break;
36600d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
367cbc10891SDmitry Chagin 		kern_thread_cputime(NULL, ats);
368d65f1abcSDavid Xu 		break;
369d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
370d65f1abcSDavid Xu 		PROC_LOCK(p);
371cbc10891SDmitry Chagin 		kern_process_cputime(p, ats);
372d65f1abcSDavid Xu 		PROC_UNLOCK(p);
37300d6ac63SDavid Xu 		break;
374b8817154SKelly Yancey 	default:
375d65f1abcSDavid Xu 		if ((int)clock_id >= 0)
3765cb3dc8fSPoul-Henning Kamp 			return (EINVAL);
377d65f1abcSDavid Xu 		return (get_cputime(td, clock_id, ats));
378b8817154SKelly Yancey 	}
379f0b479cdSPaul Saab 	return (0);
38094c8fcd8SPeter Wemm }
38194c8fcd8SPeter Wemm 
38294c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
38394c8fcd8SPeter Wemm struct clock_settime_args {
38494c8fcd8SPeter Wemm 	clockid_t clock_id;
38594c8fcd8SPeter Wemm 	const struct	timespec *tp;
38694c8fcd8SPeter Wemm };
38794c8fcd8SPeter Wemm #endif
38894c8fcd8SPeter Wemm /* ARGSUSED */
38994c8fcd8SPeter Wemm int
3908451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
39194c8fcd8SPeter Wemm {
39294c8fcd8SPeter Wemm 	struct timespec ats;
39394c8fcd8SPeter Wemm 	int error;
39494c8fcd8SPeter Wemm 
395f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
396f0b479cdSPaul Saab 		return (error);
397f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
398f0b479cdSPaul Saab }
399f0b479cdSPaul Saab 
40090a79ac5SConrad Meyer static int allow_insane_settime = 0;
40190a79ac5SConrad Meyer SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN,
40290a79ac5SConrad Meyer     &allow_insane_settime, 0,
40390a79ac5SConrad Meyer     "do not perform possibly restrictive checks on settime(2) args");
40490a79ac5SConrad Meyer 
405f0b479cdSPaul Saab int
406f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
407f0b479cdSPaul Saab {
408f0b479cdSPaul Saab 	struct timeval atv;
409f0b479cdSPaul Saab 	int error;
410f0b479cdSPaul Saab 
411acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
4127edfb592SJohn Baldwin 		return (error);
413f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
4147edfb592SJohn Baldwin 		return (EINVAL);
41591e7bdcdSDmitry Chagin 	if (!timespecvalid_interval(ats))
4167edfb592SJohn Baldwin 		return (EINVAL);
417bc79b41cSMark Johnston 	if (!allow_insane_settime &&
418bc79b41cSMark Johnston 	    (ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60 ||
419bc79b41cSMark Johnston 	    ats->tv_sec < utc_offset()))
42090a79ac5SConrad Meyer 		return (EINVAL);
421a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
422f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
4237edfb592SJohn Baldwin 	error = settime(td, &atv);
42494c8fcd8SPeter Wemm 	return (error);
42594c8fcd8SPeter Wemm }
42694c8fcd8SPeter Wemm 
42794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
42894c8fcd8SPeter Wemm struct clock_getres_args {
42994c8fcd8SPeter Wemm 	clockid_t clock_id;
43094c8fcd8SPeter Wemm 	struct	timespec *tp;
43194c8fcd8SPeter Wemm };
43294c8fcd8SPeter Wemm #endif
43394c8fcd8SPeter Wemm int
4348451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
43594c8fcd8SPeter Wemm {
43694c8fcd8SPeter Wemm 	struct timespec ts;
437f0b479cdSPaul Saab 	int error;
43894c8fcd8SPeter Wemm 
439f0b479cdSPaul Saab 	if (uap->tp == NULL)
440f0b479cdSPaul Saab 		return (0);
441f0b479cdSPaul Saab 
442f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
443f0b479cdSPaul Saab 	if (error == 0)
444f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
445f0b479cdSPaul Saab 	return (error);
446f0b479cdSPaul Saab }
447f0b479cdSPaul Saab 
448f0b479cdSPaul Saab int
449f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
450f0b479cdSPaul Saab {
451f0b479cdSPaul Saab 
452f0b479cdSPaul Saab 	ts->tv_sec = 0;
453f0b479cdSPaul Saab 	switch (clock_id) {
454b8817154SKelly Yancey 	case CLOCK_REALTIME:
4555e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
4565e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
457b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
4585e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
4595e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
4605eefd889SAndre Oppermann 	case CLOCK_UPTIME:
4615e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
4625e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
463ac0653dcSBruce Evans 		/*
464ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
465ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
466ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
467ac0653dcSBruce Evans 		 */
4688b3c4231SMark Johnston 		ts->tv_nsec = NS_PER_SEC / tc_getfrequency() + 1;
469b8817154SKelly Yancey 		break;
470b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
471b8817154SKelly Yancey 	case CLOCK_PROF:
472b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
4738b3c4231SMark Johnston 		ts->tv_nsec = howmany(NS_PER_SEC, hz);
474b8817154SKelly Yancey 		break;
4755e758b95SRobert Watson 	case CLOCK_SECOND:
4765e758b95SRobert Watson 		ts->tv_sec = 1;
4775e758b95SRobert Watson 		ts->tv_nsec = 0;
4785e758b95SRobert Watson 		break;
47900d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
480d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
481d65f1abcSDavid Xu 	cputime:
482bb53dd56Sfirk 		ts->tv_nsec = 1000000000 / cpu_tickrate() + 1;
48300d6ac63SDavid Xu 		break;
484b8817154SKelly Yancey 	default:
485d65f1abcSDavid Xu 		if ((int)clock_id < 0)
486d65f1abcSDavid Xu 			goto cputime;
487b8817154SKelly Yancey 		return (EINVAL);
48894c8fcd8SPeter Wemm 	}
489de0a9241SKelly Yancey 	return (0);
49094c8fcd8SPeter Wemm }
49194c8fcd8SPeter Wemm 
4927fdf2c85SPaul Saab int
4937fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
4945b870b7bSPeter Wemm {
4953f8455b0SEric van Gyzen 
4963f8455b0SEric van Gyzen 	return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt,
4973f8455b0SEric van Gyzen 	    rmt));
4983f8455b0SEric van Gyzen }
4993f8455b0SEric van Gyzen 
5003f8455b0SEric van Gyzen static uint8_t nanowait[MAXCPU];
5013f8455b0SEric van Gyzen 
5023f8455b0SEric van Gyzen int
5033f8455b0SEric van Gyzen kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
5043f8455b0SEric van Gyzen     const struct timespec *rqt, struct timespec *rmt)
5053f8455b0SEric van Gyzen {
5063f8455b0SEric van Gyzen 	struct timespec ts, now;
507098176f0SDavide Italiano 	sbintime_t sbt, sbtt, prec, tmp;
508980c545dSAlexander Motin 	time_t over;
50933841826SPoul-Henning Kamp 	int error;
5103f8455b0SEric van Gyzen 	bool is_abs_real;
5115b870b7bSPeter Wemm 
5128b3c4231SMark Johnston 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= NS_PER_SEC)
513708e7684SPeter Wemm 		return (EINVAL);
5143f8455b0SEric van Gyzen 	if ((flags & ~TIMER_ABSTIME) != 0)
5153f8455b0SEric van Gyzen 		return (EINVAL);
5163f8455b0SEric van Gyzen 	switch (clock_id) {
5173f8455b0SEric van Gyzen 	case CLOCK_REALTIME:
5183f8455b0SEric van Gyzen 	case CLOCK_REALTIME_PRECISE:
5193f8455b0SEric van Gyzen 	case CLOCK_REALTIME_FAST:
5203f8455b0SEric van Gyzen 	case CLOCK_SECOND:
5213f8455b0SEric van Gyzen 		is_abs_real = (flags & TIMER_ABSTIME) != 0;
5223f8455b0SEric van Gyzen 		break;
5233f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC:
5243f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC_PRECISE:
5253f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC_FAST:
5263f8455b0SEric van Gyzen 	case CLOCK_UPTIME:
5273f8455b0SEric van Gyzen 	case CLOCK_UPTIME_PRECISE:
5283f8455b0SEric van Gyzen 	case CLOCK_UPTIME_FAST:
5293f8455b0SEric van Gyzen 		is_abs_real = false;
5303f8455b0SEric van Gyzen 		break;
5313f8455b0SEric van Gyzen 	case CLOCK_VIRTUAL:
5323f8455b0SEric van Gyzen 	case CLOCK_PROF:
5333f8455b0SEric van Gyzen 	case CLOCK_PROCESS_CPUTIME_ID:
5343f8455b0SEric van Gyzen 		return (ENOTSUP);
5353f8455b0SEric van Gyzen 	case CLOCK_THREAD_CPUTIME_ID:
5363f8455b0SEric van Gyzen 	default:
5373f8455b0SEric van Gyzen 		return (EINVAL);
5383f8455b0SEric van Gyzen 	}
5393f8455b0SEric van Gyzen 	do {
540980c545dSAlexander Motin 		ts = *rqt;
5413f8455b0SEric van Gyzen 		if ((flags & TIMER_ABSTIME) != 0) {
5423f8455b0SEric van Gyzen 			if (is_abs_real)
5433f8455b0SEric van Gyzen 				td->td_rtcgen =
5443f8455b0SEric van Gyzen 				    atomic_load_acq_int(&rtc_generation);
5453f8455b0SEric van Gyzen 			error = kern_clock_gettime(td, clock_id, &now);
5463f8455b0SEric van Gyzen 			KASSERT(error == 0, ("kern_clock_gettime: %d", error));
5476040822cSAlan Somers 			timespecsub(&ts, &now, &ts);
5483f8455b0SEric van Gyzen 		}
5493f8455b0SEric van Gyzen 		if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
5503f8455b0SEric van Gyzen 			error = EWOULDBLOCK;
5513f8455b0SEric van Gyzen 			break;
5523f8455b0SEric van Gyzen 		}
553980c545dSAlexander Motin 		if (ts.tv_sec > INT32_MAX / 2) {
554980c545dSAlexander Motin 			over = ts.tv_sec - INT32_MAX / 2;
555980c545dSAlexander Motin 			ts.tv_sec -= over;
556980c545dSAlexander Motin 		} else
557980c545dSAlexander Motin 			over = 0;
558980c545dSAlexander Motin 		tmp = tstosbt(ts);
559098176f0SDavide Italiano 		prec = tmp;
560098176f0SDavide Italiano 		prec >>= tc_precexp;
561098176f0SDavide Italiano 		if (TIMESEL(&sbt, tmp))
562098176f0SDavide Italiano 			sbt += tc_tick_sbt;
563098176f0SDavide Italiano 		sbt += tmp;
5640dbf17e6SAlexander Motin 		error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
5650dbf17e6SAlexander Motin 		    sbt, prec, C_ABSOLUTE);
5663f8455b0SEric van Gyzen 	} while (error == 0 && is_abs_real && td->td_rtcgen == 0);
5673f8455b0SEric van Gyzen 	td->td_rtcgen = 0;
56833841826SPoul-Henning Kamp 	if (error != EWOULDBLOCK) {
56970c144dcSBryan Drewery 		if (TIMESEL(&sbtt, tmp))
57070c144dcSBryan Drewery 			sbtt += tc_tick_sbt;
5713f8455b0SEric van Gyzen 		if (sbtt >= sbt)
5723f8455b0SEric van Gyzen 			return (0);
57333841826SPoul-Henning Kamp 		if (error == ERESTART)
57494c8fcd8SPeter Wemm 			error = EINTR;
5753f8455b0SEric van Gyzen 		if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) {
576098176f0SDavide Italiano 			ts = sbttots(sbt - sbtt);
577980c545dSAlexander Motin 			ts.tv_sec += over;
57833841826SPoul-Henning Kamp 			if (ts.tv_sec < 0)
57933841826SPoul-Henning Kamp 				timespecclear(&ts);
58000af9731SPoul-Henning Kamp 			*rmt = ts;
58100af9731SPoul-Henning Kamp 		}
58233841826SPoul-Henning Kamp 		return (error);
58333841826SPoul-Henning Kamp 	}
58433841826SPoul-Henning Kamp 	return (0);
5855b870b7bSPeter Wemm }
58694c8fcd8SPeter Wemm 
5875b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
5885b870b7bSPeter Wemm struct nanosleep_args {
5895b870b7bSPeter Wemm 	struct	timespec *rqtp;
5905b870b7bSPeter Wemm 	struct	timespec *rmtp;
5915b870b7bSPeter Wemm };
5925b870b7bSPeter Wemm #endif
5935b870b7bSPeter Wemm /* ARGSUSED */
5945b870b7bSPeter Wemm int
5958451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
5965b870b7bSPeter Wemm {
5973f8455b0SEric van Gyzen 
5983f8455b0SEric van Gyzen 	return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME,
5993f8455b0SEric van Gyzen 	    uap->rqtp, uap->rmtp));
6003f8455b0SEric van Gyzen }
6013f8455b0SEric van Gyzen 
6023f8455b0SEric van Gyzen #ifndef _SYS_SYSPROTO_H_
6033f8455b0SEric van Gyzen struct clock_nanosleep_args {
6043f8455b0SEric van Gyzen 	clockid_t clock_id;
6053f8455b0SEric van Gyzen 	int 	  flags;
6063f8455b0SEric van Gyzen 	struct	timespec *rqtp;
6073f8455b0SEric van Gyzen 	struct	timespec *rmtp;
6083f8455b0SEric van Gyzen };
6093f8455b0SEric van Gyzen #endif
6103f8455b0SEric van Gyzen /* ARGSUSED */
6113f8455b0SEric van Gyzen int
6123f8455b0SEric van Gyzen sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap)
6133f8455b0SEric van Gyzen {
6143f8455b0SEric van Gyzen 	int error;
6153f8455b0SEric van Gyzen 
6163f8455b0SEric van Gyzen 	error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp,
6173f8455b0SEric van Gyzen 	    uap->rmtp);
6183f8455b0SEric van Gyzen 	return (kern_posix_error(td, error));
6193f8455b0SEric van Gyzen }
6203f8455b0SEric van Gyzen 
6213f8455b0SEric van Gyzen static int
6223f8455b0SEric van Gyzen user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
6233f8455b0SEric van Gyzen     const struct timespec *ua_rqtp, struct timespec *ua_rmtp)
6243f8455b0SEric van Gyzen {
6255b870b7bSPeter Wemm 	struct timespec rmt, rqt;
626618a20d4SBrooks Davis 	int error, error2;
6275b870b7bSPeter Wemm 
6283f8455b0SEric van Gyzen 	error = copyin(ua_rqtp, &rqt, sizeof(rqt));
6295b870b7bSPeter Wemm 	if (error)
6305b870b7bSPeter Wemm 		return (error);
6313f8455b0SEric van Gyzen 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
6323f8455b0SEric van Gyzen 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
6333f8455b0SEric van Gyzen 		error2 = copyout(&rmt, ua_rmtp, sizeof(rmt));
634618a20d4SBrooks Davis 		if (error2 != 0)
635fb99ab88SMatthew Dillon 			error = error2;
636708e7684SPeter Wemm 	}
637708e7684SPeter Wemm 	return (error);
63894c8fcd8SPeter Wemm }
63994c8fcd8SPeter Wemm 
6405b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
641df8bae1dSRodney W. Grimes struct gettimeofday_args {
642df8bae1dSRodney W. Grimes 	struct	timeval *tp;
643df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
644df8bae1dSRodney W. Grimes };
645d2d3e875SBruce Evans #endif
646df8bae1dSRodney W. Grimes /* ARGSUSED */
64726f9a767SRodney W. Grimes int
6488451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
649df8bae1dSRodney W. Grimes {
650df8bae1dSRodney W. Grimes 	struct timeval atv;
651411c25edSTim J. Robbins 	struct timezone rtz;
652df8bae1dSRodney W. Grimes 	int error = 0;
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes 	if (uap->tp) {
655df8bae1dSRodney W. Grimes 		microtime(&atv);
65601609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
657df8bae1dSRodney W. Grimes 	}
65821dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
659329f0aa9SWarner Losh 		rtz.tz_minuteswest = 0;
660329f0aa9SWarner Losh 		rtz.tz_dsttime = 0;
661411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
66221dcdb38SPoul-Henning Kamp 	}
663df8bae1dSRodney W. Grimes 	return (error);
664df8bae1dSRodney W. Grimes }
665df8bae1dSRodney W. Grimes 
666d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
667df8bae1dSRodney W. Grimes struct settimeofday_args {
668df8bae1dSRodney W. Grimes 	struct	timeval *tv;
669df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
670df8bae1dSRodney W. Grimes };
671d2d3e875SBruce Evans #endif
672df8bae1dSRodney W. Grimes /* ARGSUSED */
67326f9a767SRodney W. Grimes int
6748451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
675df8bae1dSRodney W. Grimes {
676b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
677b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
678b88ec951SJohn Baldwin 	int error;
679b88ec951SJohn Baldwin 
680b88ec951SJohn Baldwin 	if (uap->tv) {
681b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
682b88ec951SJohn Baldwin 		if (error)
683b88ec951SJohn Baldwin 			return (error);
684b88ec951SJohn Baldwin 		tvp = &atv;
685b88ec951SJohn Baldwin 	} else
686b88ec951SJohn Baldwin 		tvp = NULL;
687b88ec951SJohn Baldwin 	if (uap->tzp) {
688b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
689b88ec951SJohn Baldwin 		if (error)
690b88ec951SJohn Baldwin 			return (error);
691b88ec951SJohn Baldwin 		tzp = &atz;
692b88ec951SJohn Baldwin 	} else
693b88ec951SJohn Baldwin 		tzp = NULL;
694b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
695b88ec951SJohn Baldwin }
696b88ec951SJohn Baldwin 
697b88ec951SJohn Baldwin int
698b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
699b88ec951SJohn Baldwin {
700b88ec951SJohn Baldwin 	int error;
701fb99ab88SMatthew Dillon 
702acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
703b88ec951SJohn Baldwin 	if (error)
7047edfb592SJohn Baldwin 		return (error);
705df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
706b88ec951SJohn Baldwin 	if (tv) {
7073e18d701SDmitry Chagin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
7083e18d701SDmitry Chagin 		    tv->tv_sec < 0)
7097edfb592SJohn Baldwin 			return (EINVAL);
710b88ec951SJohn Baldwin 		error = settime(td, tv);
711708e7684SPeter Wemm 	}
7127edfb592SJohn Baldwin 	return (error);
713b88ec951SJohn Baldwin }
7147edfb592SJohn Baldwin 
715df8bae1dSRodney W. Grimes /*
716873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
717873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
718873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
719873fbcd7SRobert Watson  * time until they expire.
720df8bae1dSRodney W. Grimes  *
721873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
722873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
723873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
724873fbcd7SRobert Watson  * drifting.
725df8bae1dSRodney W. Grimes  *
726df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
727873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
728873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
729873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
730873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
731873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
732873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
733873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
734873fbcd7SRobert Watson  * should go off.
735df8bae1dSRodney W. Grimes  */
736d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
737df8bae1dSRodney W. Grimes struct getitimer_args {
738df8bae1dSRodney W. Grimes 	u_int	which;
739df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
740df8bae1dSRodney W. Grimes };
741d2d3e875SBruce Evans #endif
74226f9a767SRodney W. Grimes int
7438451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap)
744df8bae1dSRodney W. Grimes {
745df8bae1dSRodney W. Grimes 	struct itimerval aitv;
746c90110d6SJohn Baldwin 	int error;
747df8bae1dSRodney W. Grimes 
748cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
749cfa0efe7SMaxim Sobolev 	if (error != 0)
750cfa0efe7SMaxim Sobolev 		return (error);
751cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
752cfa0efe7SMaxim Sobolev }
753cfa0efe7SMaxim Sobolev 
754cfa0efe7SMaxim Sobolev int
755cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
756cfa0efe7SMaxim Sobolev {
757cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
758cfa0efe7SMaxim Sobolev 	struct timeval ctv;
759cfa0efe7SMaxim Sobolev 
760cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
761df8bae1dSRodney W. Grimes 		return (EINVAL);
762fb99ab88SMatthew Dillon 
763cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
764df8bae1dSRodney W. Grimes 		/*
765ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
766df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
767df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
768df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
769df8bae1dSRodney W. Grimes 		 */
77096d7f8efSTim J. Robbins 		PROC_LOCK(p);
771cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
77296d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
773cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
774b5ea3779SAlexander Motin 			microuptime(&ctv);
775cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
776cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
777df8bae1dSRodney W. Grimes 			else
778cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
779227ee8a1SPoul-Henning Kamp 		}
780fb99ab88SMatthew Dillon 	} else {
7815c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
782cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
7835c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
784fb99ab88SMatthew Dillon 	}
785de56aee0SKonstantin Belousov #ifdef KTRACE
786de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
787de56aee0SKonstantin Belousov 		ktritimerval(aitv);
788de56aee0SKonstantin Belousov #endif
789cfa0efe7SMaxim Sobolev 	return (0);
790df8bae1dSRodney W. Grimes }
791df8bae1dSRodney W. Grimes 
792d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
793df8bae1dSRodney W. Grimes struct setitimer_args {
794df8bae1dSRodney W. Grimes 	u_int	which;
795df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
796df8bae1dSRodney W. Grimes };
797d2d3e875SBruce Evans #endif
79826f9a767SRodney W. Grimes int
7998451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap)
800df8bae1dSRodney W. Grimes {
801cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
802c90110d6SJohn Baldwin 	int error;
80396d7f8efSTim J. Robbins 
80496d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
80596d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
8068451d0ddSKip Macy 		return (sys_getitimer(td, (struct getitimer_args *)uap));
80796d7f8efSTim J. Robbins 	}
808df8bae1dSRodney W. Grimes 
80996d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
810df8bae1dSRodney W. Grimes 		return (error);
811cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
812cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
813cfa0efe7SMaxim Sobolev 		return (error);
814cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
815cfa0efe7SMaxim Sobolev }
816cfa0efe7SMaxim Sobolev 
817cfa0efe7SMaxim Sobolev int
818c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
819c90110d6SJohn Baldwin     struct itimerval *oitv)
820cfa0efe7SMaxim Sobolev {
821cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
822cfa0efe7SMaxim Sobolev 	struct timeval ctv;
823b5ea3779SAlexander Motin 	sbintime_t sbt, pr;
824cfa0efe7SMaxim Sobolev 
8255e85ac17SJohn Baldwin 	if (aitv == NULL)
8265e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
8275e85ac17SJohn Baldwin 
828cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
82996d7f8efSTim J. Robbins 		return (EINVAL);
830de56aee0SKonstantin Belousov #ifdef KTRACE
831de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
832de56aee0SKonstantin Belousov 		ktritimerval(aitv);
833de56aee0SKonstantin Belousov #endif
834b5ea3779SAlexander Motin 	if (itimerfix(&aitv->it_value) ||
835b5ea3779SAlexander Motin 	    aitv->it_value.tv_sec > INT32_MAX / 2)
836cfa0efe7SMaxim Sobolev 		return (EINVAL);
837cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
838cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
839b5ea3779SAlexander Motin 	else if (itimerfix(&aitv->it_interval) ||
840b5ea3779SAlexander Motin 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
84196d7f8efSTim J. Robbins 		return (EINVAL);
84296d7f8efSTim J. Robbins 
843cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
84496d7f8efSTim J. Robbins 		PROC_LOCK(p);
8454cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
8464f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
847b5ea3779SAlexander Motin 		microuptime(&ctv);
848cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
849b5ea3779SAlexander Motin 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
850cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
851b5ea3779SAlexander Motin 			sbt = tvtosbt(aitv->it_value);
852b5ea3779SAlexander Motin 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
853b5ea3779SAlexander Motin 			    realitexpire, p, C_ABSOLUTE);
85425b4d3a8SJohn Baldwin 		}
855cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
856cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
85796d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
858cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
859cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
860cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
86196d7f8efSTim J. Robbins 			else
862cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
863fb99ab88SMatthew Dillon 		}
86496d7f8efSTim J. Robbins 	} else {
865d10a1df8SAlexander Motin 		if (aitv->it_interval.tv_sec == 0 &&
866d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec != 0 &&
867d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec < tick)
868d10a1df8SAlexander Motin 			aitv->it_interval.tv_usec = tick;
869d10a1df8SAlexander Motin 		if (aitv->it_value.tv_sec == 0 &&
870d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec != 0 &&
871d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec < tick)
872d10a1df8SAlexander Motin 			aitv->it_value.tv_usec = tick;
8735c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
874cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
875cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
8765c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
87796d7f8efSTim J. Robbins 	}
878de56aee0SKonstantin Belousov #ifdef KTRACE
879de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
880de56aee0SKonstantin Belousov 		ktritimerval(oitv);
881de56aee0SKonstantin Belousov #endif
88296d7f8efSTim J. Robbins 	return (0);
883df8bae1dSRodney W. Grimes }
884df8bae1dSRodney W. Grimes 
885dc47fdf1SKonstantin Belousov static void
886dc47fdf1SKonstantin Belousov realitexpire_reset_callout(struct proc *p, sbintime_t *isbtp)
887dc47fdf1SKonstantin Belousov {
888dc47fdf1SKonstantin Belousov 	sbintime_t prec;
889dc47fdf1SKonstantin Belousov 
890dc47fdf1SKonstantin Belousov 	prec = isbtp == NULL ? tvtosbt(p->p_realtimer.it_interval) : *isbtp;
891dc47fdf1SKonstantin Belousov 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
892dc47fdf1SKonstantin Belousov 	    prec >> tc_precexp, realitexpire, p, C_ABSOLUTE);
893dc47fdf1SKonstantin Belousov }
894dc47fdf1SKonstantin Belousov 
895dc47fdf1SKonstantin Belousov void
896dc47fdf1SKonstantin Belousov itimer_proc_continue(struct proc *p)
897dc47fdf1SKonstantin Belousov {
898dc47fdf1SKonstantin Belousov 	struct timeval ctv;
8994d27d8d2SKonstantin Belousov 	struct itimer *it;
9004d27d8d2SKonstantin Belousov 	int id;
901dc47fdf1SKonstantin Belousov 
902dc47fdf1SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
903dc47fdf1SKonstantin Belousov 
904dc47fdf1SKonstantin Belousov 	if ((p->p_flag2 & P2_ITSTOPPED) != 0) {
905dc47fdf1SKonstantin Belousov 		p->p_flag2 &= ~P2_ITSTOPPED;
906dc47fdf1SKonstantin Belousov 		microuptime(&ctv);
907dc47fdf1SKonstantin Belousov 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >=))
908dc47fdf1SKonstantin Belousov 			realitexpire(p);
909dc47fdf1SKonstantin Belousov 		else
910dc47fdf1SKonstantin Belousov 			realitexpire_reset_callout(p, NULL);
911dc47fdf1SKonstantin Belousov 	}
9124d27d8d2SKonstantin Belousov 
9134d27d8d2SKonstantin Belousov 	if (p->p_itimers != NULL) {
9144d27d8d2SKonstantin Belousov 		for (id = 3; id < TIMER_MAX; id++) {
9154d27d8d2SKonstantin Belousov 			it = p->p_itimers->its_timers[id];
9164d27d8d2SKonstantin Belousov 			if (it == NULL)
9174d27d8d2SKonstantin Belousov 				continue;
9184d27d8d2SKonstantin Belousov 			if ((it->it_flags & ITF_PSTOPPED) != 0) {
9194d27d8d2SKonstantin Belousov 				ITIMER_LOCK(it);
9204d27d8d2SKonstantin Belousov 				if ((it->it_flags & ITF_PSTOPPED) != 0) {
9214d27d8d2SKonstantin Belousov 					it->it_flags &= ~ITF_PSTOPPED;
9224d27d8d2SKonstantin Belousov 					if ((it->it_flags & ITF_DELETING) == 0)
9235cc1d199SKonstantin Belousov 						realtimer_expire_l(it, true);
9244d27d8d2SKonstantin Belousov 				}
9254d27d8d2SKonstantin Belousov 				ITIMER_UNLOCK(it);
9264d27d8d2SKonstantin Belousov 			}
9274d27d8d2SKonstantin Belousov 		}
9284d27d8d2SKonstantin Belousov 	}
929dc47fdf1SKonstantin Belousov }
930dc47fdf1SKonstantin Belousov 
931df8bae1dSRodney W. Grimes /*
932df8bae1dSRodney W. Grimes  * Real interval timer expired:
933df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
934df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
935df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
936df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
937df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
938c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
9399207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
9409207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
9419207f00aSBruce Evans  * interrupt even when we're delayed.
942df8bae1dSRodney W. Grimes  */
943ef221ff6SMark Johnston static void
94491afe087SPoul-Henning Kamp realitexpire(void *arg)
945df8bae1dSRodney W. Grimes {
94691afe087SPoul-Henning Kamp 	struct proc *p;
947b5ea3779SAlexander Motin 	struct timeval ctv;
948b5ea3779SAlexander Motin 	sbintime_t isbt;
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
9518451d0ddSKip Macy 	kern_psignal(p, SIGALRM);
9524cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
9534cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
954df8bae1dSRodney W. Grimes 		return;
955df8bae1dSRodney W. Grimes 	}
956dc47fdf1SKonstantin Belousov 
957b5ea3779SAlexander Motin 	isbt = tvtosbt(p->p_realtimer.it_interval);
958b5ea3779SAlexander Motin 	if (isbt >= sbt_timethreshold)
959b5ea3779SAlexander Motin 		getmicrouptime(&ctv);
960b5ea3779SAlexander Motin 	else
961b5ea3779SAlexander Motin 		microuptime(&ctv);
962b5ea3779SAlexander Motin 	do {
963df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
964df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
965b5ea3779SAlexander Motin 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
966dc47fdf1SKonstantin Belousov 
967dc47fdf1SKonstantin Belousov 	if (P_SHOULDSTOP(p) || P_KILLED(p)) {
968dc47fdf1SKonstantin Belousov 		p->p_flag2 |= P2_ITSTOPPED;
969dc47fdf1SKonstantin Belousov 		return;
970dc47fdf1SKonstantin Belousov 	}
971dc47fdf1SKonstantin Belousov 
972dc47fdf1SKonstantin Belousov 	p->p_flag2 &= ~P2_ITSTOPPED;
973dc47fdf1SKonstantin Belousov 	realitexpire_reset_callout(p, &isbt);
974df8bae1dSRodney W. Grimes }
975df8bae1dSRodney W. Grimes 
976df8bae1dSRodney W. Grimes /*
977df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
978df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
979df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
980df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
981df8bae1dSRodney W. Grimes  */
98226f9a767SRodney W. Grimes int
98391afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
984df8bae1dSRodney W. Grimes {
985df8bae1dSRodney W. Grimes 
98686857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
987df8bae1dSRodney W. Grimes 		return (EINVAL);
988b5ea3779SAlexander Motin 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
989b5ea3779SAlexander Motin 	    tv->tv_usec < (u_int)tick / 16)
990b5ea3779SAlexander Motin 		tv->tv_usec = (u_int)tick / 16;
991df8bae1dSRodney W. Grimes 	return (0);
992df8bae1dSRodney W. Grimes }
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes /*
995df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
996df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
997df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
998df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
999df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
1000df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
1001df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
1002df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
1003df8bae1dSRodney W. Grimes  */
100426f9a767SRodney W. Grimes int
100591afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
1006df8bae1dSRodney W. Grimes {
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
1009df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
1010df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
1011df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
1012df8bae1dSRodney W. Grimes 			goto expire;
1013df8bae1dSRodney W. Grimes 		}
1014df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
1015df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
1016df8bae1dSRodney W. Grimes 	}
1017df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
1018df8bae1dSRodney W. Grimes 	usec = 0;
10194cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
1020df8bae1dSRodney W. Grimes 		return (1);
1021df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
1022df8bae1dSRodney W. Grimes expire:
10234cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
1024df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
1025df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
1026df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
1027df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
1028df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
1029df8bae1dSRodney W. Grimes 		}
1030df8bae1dSRodney W. Grimes 	} else
1031df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1032df8bae1dSRodney W. Grimes 	return (0);
1033df8bae1dSRodney W. Grimes }
1034df8bae1dSRodney W. Grimes 
1035df8bae1dSRodney W. Grimes /*
1036df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
1037df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
1038df8bae1dSRodney W. Grimes  * results which are before the beginning,
1039df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
1040df8bae1dSRodney W. Grimes  * Caveat emptor.
1041df8bae1dSRodney W. Grimes  */
104226f9a767SRodney W. Grimes void
10436ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
1044df8bae1dSRodney W. Grimes {
1045df8bae1dSRodney W. Grimes 
1046df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
1047df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
1048df8bae1dSRodney W. Grimes 	timevalfix(t1);
1049df8bae1dSRodney W. Grimes }
1050df8bae1dSRodney W. Grimes 
105126f9a767SRodney W. Grimes void
10526ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
1053df8bae1dSRodney W. Grimes {
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
1056df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
1057df8bae1dSRodney W. Grimes 	timevalfix(t1);
1058df8bae1dSRodney W. Grimes }
1059df8bae1dSRodney W. Grimes 
106087b6de2bSPoul-Henning Kamp static void
106191afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
1062df8bae1dSRodney W. Grimes {
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
1065df8bae1dSRodney W. Grimes 		t1->tv_sec--;
1066df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
1067df8bae1dSRodney W. Grimes 	}
1068df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
1069df8bae1dSRodney W. Grimes 		t1->tv_sec++;
1070df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
1071df8bae1dSRodney W. Grimes 	}
1072df8bae1dSRodney W. Grimes }
107391974ce1SSam Leffler 
107491974ce1SSam Leffler /*
1075addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
107691974ce1SSam Leffler  */
107791974ce1SSam Leffler int
107891974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
107991974ce1SSam Leffler {
108091974ce1SSam Leffler 	struct timeval tv, delta;
108191974ce1SSam Leffler 	int rv = 0;
108291974ce1SSam Leffler 
1083addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
1084addea9d4SSam Leffler 	delta = tv;
1085addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
108691974ce1SSam Leffler 
108791974ce1SSam Leffler 	/*
108891974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
108991974ce1SSam Leffler 	 * even if interval is huge.
109091974ce1SSam Leffler 	 */
109191974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
109291974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
109391974ce1SSam Leffler 		*lasttime = tv;
109491974ce1SSam Leffler 		rv = 1;
109591974ce1SSam Leffler 	}
109691974ce1SSam Leffler 
109791974ce1SSam Leffler 	return (rv);
109891974ce1SSam Leffler }
109991974ce1SSam Leffler 
110091974ce1SSam Leffler /*
1101*83158c68SMateusz Guzik  * eventratecheck(): events per second limitation.
1102addea9d4SSam Leffler  *
1103addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
1104*83158c68SMateusz Guzik  * should ignore the event because of the rate limitation).
1105addea9d4SSam Leffler  *
1106*83158c68SMateusz Guzik  * maxeps of 0 always causes zero to be returned.  maxeps of -1
1107893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
1108893bec80SSam Leffler  * limiting.
1109893bec80SSam Leffler  *
1110addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
1111addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
1112addea9d4SSam Leffler  * clock ticks for minimal overhead.
111391974ce1SSam Leffler  */
111491974ce1SSam Leffler int
1115*83158c68SMateusz Guzik eventratecheck(struct timeval *lasttime, int *cureps, int maxeps)
111691974ce1SSam Leffler {
1117addea9d4SSam Leffler 	int now;
111891974ce1SSam Leffler 
111991974ce1SSam Leffler 	/*
1120addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
1121addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
1122addea9d4SSam Leffler 	 * lasttime.
112391974ce1SSam Leffler 	 */
1124addea9d4SSam Leffler 	now = ticks;
1125addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1126addea9d4SSam Leffler 		lasttime->tv_sec = now;
1127*83158c68SMateusz Guzik 		*cureps = 1;
1128*83158c68SMateusz Guzik 		return (maxeps != 0);
1129addea9d4SSam Leffler 	} else {
1130*83158c68SMateusz Guzik 		(*cureps)++;		/* NB: ignore potential overflow */
1131*83158c68SMateusz Guzik 		return (maxeps < 0 || *cureps <= maxeps);
1132addea9d4SSam Leffler 	}
113391974ce1SSam Leffler }
113486857b36SDavid Xu 
113586857b36SDavid Xu static void
113686857b36SDavid Xu itimer_start(void)
113786857b36SDavid Xu {
1138e68c6191SKonstantin Belousov 	static const struct kclock rt_clock = {
113986857b36SDavid Xu 		.timer_create  = realtimer_create,
114086857b36SDavid Xu 		.timer_delete  = realtimer_delete,
114186857b36SDavid Xu 		.timer_settime = realtimer_settime,
114286857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
114386857b36SDavid Xu 	};
114486857b36SDavid Xu 
114586857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
114686857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
114786857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
114886857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
114977e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
115077e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
115177e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
115286857b36SDavid Xu }
115386857b36SDavid Xu 
1154e68c6191SKonstantin Belousov static int
1155e68c6191SKonstantin Belousov register_posix_clock(int clockid, const struct kclock *clk)
115686857b36SDavid Xu {
115786857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
115886857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
115986857b36SDavid Xu 		return (0);
116086857b36SDavid Xu 	}
116186857b36SDavid Xu 	posix_clocks[clockid] = *clk;
116286857b36SDavid Xu 	return (1);
116386857b36SDavid Xu }
116486857b36SDavid Xu 
116586857b36SDavid Xu static int
116686857b36SDavid Xu itimer_init(void *mem, int size, int flags)
116786857b36SDavid Xu {
116886857b36SDavid Xu 	struct itimer *it;
116986857b36SDavid Xu 
117086857b36SDavid Xu 	it = (struct itimer *)mem;
117186857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
117286857b36SDavid Xu 	return (0);
117386857b36SDavid Xu }
117486857b36SDavid Xu 
117586857b36SDavid Xu static void
117686857b36SDavid Xu itimer_fini(void *mem, int size)
117786857b36SDavid Xu {
117886857b36SDavid Xu 	struct itimer *it;
117986857b36SDavid Xu 
118086857b36SDavid Xu 	it = (struct itimer *)mem;
118186857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
118286857b36SDavid Xu }
118386857b36SDavid Xu 
118486857b36SDavid Xu static void
118586857b36SDavid Xu itimer_enter(struct itimer *it)
118686857b36SDavid Xu {
118786857b36SDavid Xu 
118886857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
118986857b36SDavid Xu 	it->it_usecount++;
119086857b36SDavid Xu }
119186857b36SDavid Xu 
119286857b36SDavid Xu static void
119386857b36SDavid Xu itimer_leave(struct itimer *it)
119486857b36SDavid Xu {
119586857b36SDavid Xu 
119686857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
119786857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
119886857b36SDavid Xu 
119986857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
120086857b36SDavid Xu 		wakeup(it);
120186857b36SDavid Xu }
120286857b36SDavid Xu 
120386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
120461d3a4efSDavid Xu struct ktimer_create_args {
120586857b36SDavid Xu 	clockid_t clock_id;
120686857b36SDavid Xu 	struct sigevent * evp;
120761d3a4efSDavid Xu 	int * timerid;
120886857b36SDavid Xu };
120986857b36SDavid Xu #endif
121086857b36SDavid Xu int
12118451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
121286857b36SDavid Xu {
1213643ee871SKonstantin Belousov 	struct sigevent *evp, ev;
121461d3a4efSDavid Xu 	int id;
121586857b36SDavid Xu 	int error;
121686857b36SDavid Xu 
1217643ee871SKonstantin Belousov 	if (uap->evp == NULL) {
1218643ee871SKonstantin Belousov 		evp = NULL;
1219643ee871SKonstantin Belousov 	} else {
122086857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
122186857b36SDavid Xu 		if (error != 0)
122286857b36SDavid Xu 			return (error);
1223643ee871SKonstantin Belousov 		evp = &ev;
1224643ee871SKonstantin Belousov 	}
1225643ee871SKonstantin Belousov 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
122686857b36SDavid Xu 	if (error == 0) {
122761d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
122886857b36SDavid Xu 		if (error != 0)
1229643ee871SKonstantin Belousov 			kern_ktimer_delete(td, id);
123086857b36SDavid Xu 	}
123186857b36SDavid Xu 	return (error);
123286857b36SDavid Xu }
123386857b36SDavid Xu 
1234643ee871SKonstantin Belousov int
1235643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1236643ee871SKonstantin Belousov     int *timerid, int preset_id)
123786857b36SDavid Xu {
123886857b36SDavid Xu 	struct proc *p = td->td_proc;
123986857b36SDavid Xu 	struct itimer *it;
124086857b36SDavid Xu 	int id;
124186857b36SDavid Xu 	int error;
124286857b36SDavid Xu 
124386857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
124486857b36SDavid Xu 		return (EINVAL);
124586857b36SDavid Xu 
124686857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
124786857b36SDavid Xu 		return (EINVAL);
124886857b36SDavid Xu 
124986857b36SDavid Xu 	if (evp != NULL) {
125086857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
125156c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
125256c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
125386857b36SDavid Xu 			return (EINVAL);
125456c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
125556c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
125686857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
125786857b36SDavid Xu 			return (EINVAL);
125886857b36SDavid Xu 	}
125986857b36SDavid Xu 
126060354683SDavid Xu 	if (p->p_itimers == NULL)
126186857b36SDavid Xu 		itimers_alloc(p);
126286857b36SDavid Xu 
126386857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
126486857b36SDavid Xu 	it->it_flags = 0;
126586857b36SDavid Xu 	it->it_usecount = 0;
126656c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
126756c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
126886857b36SDavid Xu 	it->it_overrun = 0;
126986857b36SDavid Xu 	it->it_overrun_last = 0;
127086857b36SDavid Xu 	it->it_clockid = clock_id;
127186857b36SDavid Xu 	it->it_proc = p;
127286857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
127386857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
127486857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
127586857b36SDavid Xu 	if (error != 0)
127686857b36SDavid Xu 		goto out;
127786857b36SDavid Xu 
127886857b36SDavid Xu 	PROC_LOCK(p);
127986857b36SDavid Xu 	if (preset_id != -1) {
128086857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
128186857b36SDavid Xu 		id = preset_id;
128260354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
128386857b36SDavid Xu 			PROC_UNLOCK(p);
128486857b36SDavid Xu 			error = 0;
128586857b36SDavid Xu 			goto out;
128686857b36SDavid Xu 		}
128786857b36SDavid Xu 	} else {
128886857b36SDavid Xu 		/*
128986857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
129086857b36SDavid Xu 		 * for setitimer().
129186857b36SDavid Xu 		 */
129286857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
129360354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
129486857b36SDavid Xu 				break;
129586857b36SDavid Xu 		if (id == TIMER_MAX) {
129686857b36SDavid Xu 			PROC_UNLOCK(p);
129786857b36SDavid Xu 			error = EAGAIN;
129886857b36SDavid Xu 			goto out;
129986857b36SDavid Xu 		}
130086857b36SDavid Xu 	}
130160354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
130286857b36SDavid Xu 	if (evp != NULL)
130386857b36SDavid Xu 		it->it_sigev = *evp;
130486857b36SDavid Xu 	else {
130586857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
130686857b36SDavid Xu 		switch (clock_id) {
130786857b36SDavid Xu 		default:
130886857b36SDavid Xu 		case CLOCK_REALTIME:
130986857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
131086857b36SDavid Xu 			break;
131186857b36SDavid Xu 		case CLOCK_VIRTUAL:
131286857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
131386857b36SDavid Xu 			break;
131486857b36SDavid Xu 		case CLOCK_PROF:
131586857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
131686857b36SDavid Xu 			break;
131786857b36SDavid Xu 		}
13188f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
131986857b36SDavid Xu 	}
132086857b36SDavid Xu 
132156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
132256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
132386857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
132486857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
132586857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
132686857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
132786857b36SDavid Xu 	}
132886857b36SDavid Xu 	PROC_UNLOCK(p);
132986857b36SDavid Xu 	*timerid = id;
133086857b36SDavid Xu 	return (0);
133186857b36SDavid Xu 
133286857b36SDavid Xu out:
133386857b36SDavid Xu 	ITIMER_LOCK(it);
133486857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
133586857b36SDavid Xu 	ITIMER_UNLOCK(it);
133686857b36SDavid Xu 	uma_zfree(itimer_zone, it);
133786857b36SDavid Xu 	return (error);
133886857b36SDavid Xu }
133986857b36SDavid Xu 
134086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
134161d3a4efSDavid Xu struct ktimer_delete_args {
134261d3a4efSDavid Xu 	int timerid;
134386857b36SDavid Xu };
134486857b36SDavid Xu #endif
134586857b36SDavid Xu int
13468451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
134786857b36SDavid Xu {
1348643ee871SKonstantin Belousov 
1349643ee871SKonstantin Belousov 	return (kern_ktimer_delete(td, uap->timerid));
135086857b36SDavid Xu }
135186857b36SDavid Xu 
135286857b36SDavid Xu static struct itimer *
1353843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
135486857b36SDavid Xu {
135586857b36SDavid Xu 	struct itimer *it;
135686857b36SDavid Xu 
135786857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
13583f935cf3SColin Percival 	if ((p->p_itimers == NULL) ||
13593f935cf3SColin Percival 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
136060354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
136186857b36SDavid Xu 		return (NULL);
136286857b36SDavid Xu 	}
136386857b36SDavid Xu 	ITIMER_LOCK(it);
1364843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
136586857b36SDavid Xu 		ITIMER_UNLOCK(it);
136686857b36SDavid Xu 		it = NULL;
136786857b36SDavid Xu 	}
136886857b36SDavid Xu 	return (it);
136986857b36SDavid Xu }
137086857b36SDavid Xu 
1371643ee871SKonstantin Belousov int
1372643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid)
137386857b36SDavid Xu {
137486857b36SDavid Xu 	struct proc *p = td->td_proc;
137586857b36SDavid Xu 	struct itimer *it;
137686857b36SDavid Xu 
137786857b36SDavid Xu 	PROC_LOCK(p);
1378843b99c6SDavid Xu 	it = itimer_find(p, timerid);
137986857b36SDavid Xu 	if (it == NULL) {
138086857b36SDavid Xu 		PROC_UNLOCK(p);
138186857b36SDavid Xu 		return (EINVAL);
138286857b36SDavid Xu 	}
138386857b36SDavid Xu 	PROC_UNLOCK(p);
138486857b36SDavid Xu 
138586857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
138686857b36SDavid Xu 	while (it->it_usecount > 0) {
138786857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
138886857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
138986857b36SDavid Xu 	}
139086857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
139186857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
139286857b36SDavid Xu 	ITIMER_UNLOCK(it);
139386857b36SDavid Xu 
139486857b36SDavid Xu 	PROC_LOCK(p);
139586857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
139686857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
139760354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
139886857b36SDavid Xu 	PROC_UNLOCK(p);
139986857b36SDavid Xu 	uma_zfree(itimer_zone, it);
140086857b36SDavid Xu 	return (0);
140186857b36SDavid Xu }
140286857b36SDavid Xu 
140386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
140461d3a4efSDavid Xu struct ktimer_settime_args {
140561d3a4efSDavid Xu 	int timerid;
140686857b36SDavid Xu 	int flags;
140786857b36SDavid Xu 	const struct itimerspec * value;
140886857b36SDavid Xu 	struct itimerspec * ovalue;
140986857b36SDavid Xu };
141086857b36SDavid Xu #endif
141186857b36SDavid Xu int
14128451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
141386857b36SDavid Xu {
141486857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
141586857b36SDavid Xu 	int error;
141686857b36SDavid Xu 
141786857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
141886857b36SDavid Xu 	if (error != 0)
141986857b36SDavid Xu 		return (error);
1420643ee871SKonstantin Belousov 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1421643ee871SKonstantin Belousov 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1422643ee871SKonstantin Belousov 	if (error == 0 && uap->ovalue != NULL)
1423643ee871SKonstantin Belousov 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1424643ee871SKonstantin Belousov 	return (error);
1425643ee871SKonstantin Belousov }
142686857b36SDavid Xu 
1427643ee871SKonstantin Belousov int
1428643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1429643ee871SKonstantin Belousov     struct itimerspec *val, struct itimerspec *oval)
1430643ee871SKonstantin Belousov {
1431643ee871SKonstantin Belousov 	struct proc *p;
1432643ee871SKonstantin Belousov 	struct itimer *it;
1433643ee871SKonstantin Belousov 	int error;
143486857b36SDavid Xu 
1435643ee871SKonstantin Belousov 	p = td->td_proc;
143686857b36SDavid Xu 	PROC_LOCK(p);
1437643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
143886857b36SDavid Xu 		PROC_UNLOCK(p);
143986857b36SDavid Xu 		error = EINVAL;
144086857b36SDavid Xu 	} else {
144186857b36SDavid Xu 		PROC_UNLOCK(p);
144286857b36SDavid Xu 		itimer_enter(it);
1443643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1444643ee871SKonstantin Belousov 		    flags, val, oval));
144586857b36SDavid Xu 		itimer_leave(it);
144686857b36SDavid Xu 		ITIMER_UNLOCK(it);
144786857b36SDavid Xu 	}
144886857b36SDavid Xu 	return (error);
144986857b36SDavid Xu }
145086857b36SDavid Xu 
145186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
145261d3a4efSDavid Xu struct ktimer_gettime_args {
145361d3a4efSDavid Xu 	int timerid;
145486857b36SDavid Xu 	struct itimerspec * value;
145586857b36SDavid Xu };
145686857b36SDavid Xu #endif
145786857b36SDavid Xu int
14588451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
145986857b36SDavid Xu {
146086857b36SDavid Xu 	struct itimerspec val;
146186857b36SDavid Xu 	int error;
146286857b36SDavid Xu 
1463643ee871SKonstantin Belousov 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1464643ee871SKonstantin Belousov 	if (error == 0)
1465643ee871SKonstantin Belousov 		error = copyout(&val, uap->value, sizeof(val));
1466643ee871SKonstantin Belousov 	return (error);
1467643ee871SKonstantin Belousov }
1468643ee871SKonstantin Belousov 
1469643ee871SKonstantin Belousov int
1470643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1471643ee871SKonstantin Belousov {
1472643ee871SKonstantin Belousov 	struct proc *p;
1473643ee871SKonstantin Belousov 	struct itimer *it;
1474643ee871SKonstantin Belousov 	int error;
1475643ee871SKonstantin Belousov 
1476643ee871SKonstantin Belousov 	p = td->td_proc;
147786857b36SDavid Xu 	PROC_LOCK(p);
1478643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
147986857b36SDavid Xu 		PROC_UNLOCK(p);
148086857b36SDavid Xu 		error = EINVAL;
148186857b36SDavid Xu 	} else {
148286857b36SDavid Xu 		PROC_UNLOCK(p);
148386857b36SDavid Xu 		itimer_enter(it);
1484643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
148586857b36SDavid Xu 		itimer_leave(it);
148686857b36SDavid Xu 		ITIMER_UNLOCK(it);
148786857b36SDavid Xu 	}
148886857b36SDavid Xu 	return (error);
148986857b36SDavid Xu }
149086857b36SDavid Xu 
149186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
149286857b36SDavid Xu struct timer_getoverrun_args {
149361d3a4efSDavid Xu 	int timerid;
149486857b36SDavid Xu };
149586857b36SDavid Xu #endif
149686857b36SDavid Xu int
14978451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
149886857b36SDavid Xu {
1499e346f8c4SBjoern A. Zeeb 
1500e346f8c4SBjoern A. Zeeb 	return (kern_ktimer_getoverrun(td, uap->timerid));
1501e346f8c4SBjoern A. Zeeb }
1502e346f8c4SBjoern A. Zeeb 
1503e346f8c4SBjoern A. Zeeb int
1504e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id)
1505e346f8c4SBjoern A. Zeeb {
150686857b36SDavid Xu 	struct proc *p = td->td_proc;
150786857b36SDavid Xu 	struct itimer *it;
150886857b36SDavid Xu 	int error ;
150986857b36SDavid Xu 
151086857b36SDavid Xu 	PROC_LOCK(p);
1511e346f8c4SBjoern A. Zeeb 	if (timer_id < 3 ||
1512e346f8c4SBjoern A. Zeeb 	    (it = itimer_find(p, timer_id)) == NULL) {
151386857b36SDavid Xu 		PROC_UNLOCK(p);
151486857b36SDavid Xu 		error = EINVAL;
151586857b36SDavid Xu 	} else {
151686857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
151786857b36SDavid Xu 		ITIMER_UNLOCK(it);
151856c06c4bSDavid Xu 		PROC_UNLOCK(p);
151986857b36SDavid Xu 		error = 0;
152086857b36SDavid Xu 	}
152186857b36SDavid Xu 	return (error);
152286857b36SDavid Xu }
152386857b36SDavid Xu 
152486857b36SDavid Xu static int
152586857b36SDavid Xu realtimer_create(struct itimer *it)
152686857b36SDavid Xu {
152786857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
152886857b36SDavid Xu 	return (0);
152986857b36SDavid Xu }
153086857b36SDavid Xu 
153186857b36SDavid Xu static int
153286857b36SDavid Xu realtimer_delete(struct itimer *it)
153386857b36SDavid Xu {
153486857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1535843b99c6SDavid Xu 
1536d6b6592eSDavid Xu 	/*
1537d6b6592eSDavid Xu 	 * clear timer's value and interval to tell realtimer_expire
1538d6b6592eSDavid Xu 	 * to not rearm the timer.
1539d6b6592eSDavid Xu 	 */
1540d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_value);
1541d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_interval);
1542843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1543843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1544843b99c6SDavid Xu 	ITIMER_LOCK(it);
154586857b36SDavid Xu 	return (0);
154686857b36SDavid Xu }
154786857b36SDavid Xu 
154886857b36SDavid Xu static int
154986857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
155086857b36SDavid Xu {
155156c06c4bSDavid Xu 	struct timespec cts;
155286857b36SDavid Xu 
155386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
155486857b36SDavid Xu 
155556c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
155656c06c4bSDavid Xu 	*ovalue = it->it_time;
155786857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
15586040822cSAlan Somers 		timespecsub(&ovalue->it_value, &cts, &ovalue->it_value);
155986857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
156086857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
156186857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
156286857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
156386857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
156486857b36SDavid Xu 		}
156586857b36SDavid Xu 	}
156686857b36SDavid Xu 	return (0);
156786857b36SDavid Xu }
156886857b36SDavid Xu 
156986857b36SDavid Xu static int
157060d12ef9SMark Johnston realtimer_settime(struct itimer *it, int flags, struct itimerspec *value,
157160d12ef9SMark Johnston     struct itimerspec *ovalue)
157286857b36SDavid Xu {
157356c06c4bSDavid Xu 	struct timespec cts, ts;
157456c06c4bSDavid Xu 	struct timeval tv;
157556c06c4bSDavid Xu 	struct itimerspec val;
157686857b36SDavid Xu 
157786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
157886857b36SDavid Xu 
157956c06c4bSDavid Xu 	val = *value;
158056c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
158186857b36SDavid Xu 		return (EINVAL);
158286857b36SDavid Xu 
158356c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
158456c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
158586857b36SDavid Xu 			return (EINVAL);
158686857b36SDavid Xu 	} else {
158756c06c4bSDavid Xu 		timespecclear(&val.it_interval);
158886857b36SDavid Xu 	}
158986857b36SDavid Xu 
159086857b36SDavid Xu 	if (ovalue != NULL)
159186857b36SDavid Xu 		realtimer_gettime(it, ovalue);
159286857b36SDavid Xu 
159386857b36SDavid Xu 	it->it_time = val;
159456c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
159556c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
159656c06c4bSDavid Xu 		ts = val.it_value;
159786857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
159886857b36SDavid Xu 			/* Convert to absolute time. */
15996040822cSAlan Somers 			timespecadd(&it->it_time.it_value, &cts,
16006040822cSAlan Somers 			    &it->it_time.it_value);
160186857b36SDavid Xu 		} else {
16026040822cSAlan Somers 			timespecsub(&ts, &cts, &ts);
160386857b36SDavid Xu 			/*
160456c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
160586857b36SDavid Xu 			 * fix it.
160686857b36SDavid Xu 			 */
160786857b36SDavid Xu 		}
160856c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
160960d12ef9SMark Johnston 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
161060d12ef9SMark Johnston 		    it);
161186857b36SDavid Xu 	} else {
161286857b36SDavid Xu 		callout_stop(&it->it_callout);
161386857b36SDavid Xu 	}
161486857b36SDavid Xu 
161586857b36SDavid Xu 	return (0);
161686857b36SDavid Xu }
161786857b36SDavid Xu 
161886857b36SDavid Xu static void
161956c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
162086857b36SDavid Xu {
162186857b36SDavid Xu 	if (id == CLOCK_REALTIME)
162256c06c4bSDavid Xu 		getnanotime(ts);
162386857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
162456c06c4bSDavid Xu 		getnanouptime(ts);
162556c06c4bSDavid Xu }
162656c06c4bSDavid Xu 
162756c06c4bSDavid Xu int
162861d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
162956c06c4bSDavid Xu {
163056c06c4bSDavid Xu 	struct itimer *it;
163156c06c4bSDavid Xu 
163256c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1633843b99c6SDavid Xu 	it = itimer_find(p, timerid);
163456c06c4bSDavid Xu 	if (it != NULL) {
163556c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
163656c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
163756c06c4bSDavid Xu 		it->it_overrun = 0;
163856c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
163956c06c4bSDavid Xu 		return (0);
164056c06c4bSDavid Xu 	}
164156c06c4bSDavid Xu 	return (EINVAL);
164256c06c4bSDavid Xu }
164356c06c4bSDavid Xu 
16448ff2b41cSMark Johnston static int
164556c06c4bSDavid Xu itimespecfix(struct timespec *ts)
164656c06c4bSDavid Xu {
164756c06c4bSDavid Xu 
164891e7bdcdSDmitry Chagin 	if (!timespecvalid_interval(ts))
16498b3c4231SMark Johnston 		return (EINVAL);
16508b3c4231SMark Johnston 	if ((UINT64_MAX - ts->tv_nsec) / NS_PER_SEC < ts->tv_sec)
165156c06c4bSDavid Xu 		return (EINVAL);
165256c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
165356c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
165456c06c4bSDavid Xu 	return (0);
165586857b36SDavid Xu }
165686857b36SDavid Xu 
16577995dae9SMark Johnston #define	timespectons(tsp)			\
16588b3c4231SMark Johnston 	((uint64_t)(tsp)->tv_sec * NS_PER_SEC + (tsp)->tv_nsec)
16597995dae9SMark Johnston #define	timespecfromns(ns) (struct timespec){	\
16608b3c4231SMark Johnston 	.tv_sec = (ns) / NS_PER_SEC,		\
16618b3c4231SMark Johnston 	.tv_nsec = (ns) % NS_PER_SEC		\
16627995dae9SMark Johnston }
16637995dae9SMark Johnston 
166486857b36SDavid Xu static void
16655cc1d199SKonstantin Belousov realtimer_expire_l(struct itimer *it, bool proc_locked)
166686857b36SDavid Xu {
166756c06c4bSDavid Xu 	struct timespec cts, ts;
166856c06c4bSDavid Xu 	struct timeval tv;
16694d27d8d2SKonstantin Belousov 	struct proc *p;
16707995dae9SMark Johnston 	uint64_t interval, now, overruns, value;
167186857b36SDavid Xu 
167256c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
167386857b36SDavid Xu 	/* Only fire if time is reached. */
167456c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
167556c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
167656c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
16776040822cSAlan Somers 			    &it->it_time.it_interval,
16786040822cSAlan Somers 			    &it->it_time.it_value);
16797995dae9SMark Johnston 
16807995dae9SMark Johnston 			interval = timespectons(&it->it_time.it_interval);
16817995dae9SMark Johnston 			value = timespectons(&it->it_time.it_value);
16827995dae9SMark Johnston 			now = timespectons(&cts);
16837995dae9SMark Johnston 
16847995dae9SMark Johnston 			if (now >= value) {
16857995dae9SMark Johnston 				/*
16867995dae9SMark Johnston 				 * We missed at least one period.
16877995dae9SMark Johnston 				 */
16887995dae9SMark Johnston 				overruns = howmany(now - value + 1, interval);
16897995dae9SMark Johnston 				if (it->it_overrun + overruns >=
16907995dae9SMark Johnston 				    it->it_overrun &&
16917995dae9SMark Johnston 				    it->it_overrun + overruns <= INT_MAX) {
16927995dae9SMark Johnston 					it->it_overrun += (int)overruns;
16937995dae9SMark Johnston 				} else {
16947995dae9SMark Johnston 					it->it_overrun = INT_MAX;
169577e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
16967995dae9SMark Johnston 				}
16977995dae9SMark Johnston 				value =
16987995dae9SMark Johnston 				    now + interval - (now - value) % interval;
16997995dae9SMark Johnston 				it->it_time.it_value = timespecfromns(value);
170086857b36SDavid Xu 			}
170186857b36SDavid Xu 		} else {
170286857b36SDavid Xu 			/* single shot timer ? */
170356c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
170486857b36SDavid Xu 		}
17055cc1d199SKonstantin Belousov 
17064d27d8d2SKonstantin Belousov 		p = it->it_proc;
17075cc1d199SKonstantin Belousov 		if (timespecisset(&it->it_time.it_value)) {
17084d27d8d2SKonstantin Belousov 			if (P_SHOULDSTOP(p) || P_KILLED(p)) {
17094d27d8d2SKonstantin Belousov 				it->it_flags |= ITF_PSTOPPED;
17104d27d8d2SKonstantin Belousov 			} else {
17116040822cSAlan Somers 				timespecsub(&it->it_time.it_value, &cts, &ts);
171256c06c4bSDavid Xu 				TIMESPEC_TO_TIMEVAL(&tv, &ts);
171356c06c4bSDavid Xu 				callout_reset(&it->it_callout, tvtohz(&tv),
171486857b36SDavid Xu 				    realtimer_expire, it);
171586857b36SDavid Xu 			}
17164d27d8d2SKonstantin Belousov 		}
17175cc1d199SKonstantin Belousov 
1718d6b6592eSDavid Xu 		itimer_enter(it);
171986857b36SDavid Xu 		ITIMER_UNLOCK(it);
17205cc1d199SKonstantin Belousov 		if (proc_locked)
17215cc1d199SKonstantin Belousov 			PROC_UNLOCK(p);
172286857b36SDavid Xu 		itimer_fire(it);
17235cc1d199SKonstantin Belousov 		if (proc_locked)
17245cc1d199SKonstantin Belousov 			PROC_LOCK(p);
172586857b36SDavid Xu 		ITIMER_LOCK(it);
1726d6b6592eSDavid Xu 		itimer_leave(it);
172756c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
17284d27d8d2SKonstantin Belousov 		p = it->it_proc;
17294d27d8d2SKonstantin Belousov 		if (P_SHOULDSTOP(p) || P_KILLED(p)) {
17304d27d8d2SKonstantin Belousov 			it->it_flags |= ITF_PSTOPPED;
17314d27d8d2SKonstantin Belousov 		} else {
173256c06c4bSDavid Xu 			ts = it->it_time.it_value;
17336040822cSAlan Somers 			timespecsub(&ts, &cts, &ts);
173456c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
17354d27d8d2SKonstantin Belousov 			callout_reset(&it->it_callout, tvtohz(&tv),
17364d27d8d2SKonstantin Belousov 			    realtimer_expire, it);
17374d27d8d2SKonstantin Belousov 		}
173886857b36SDavid Xu 	}
173986857b36SDavid Xu }
174086857b36SDavid Xu 
17415cc1d199SKonstantin Belousov /* Timeout callback for realtime timer */
17425cc1d199SKonstantin Belousov static void
17435cc1d199SKonstantin Belousov realtimer_expire(void *arg)
17445cc1d199SKonstantin Belousov {
17455cc1d199SKonstantin Belousov 	realtimer_expire_l(arg, false);
17465cc1d199SKonstantin Belousov }
17475cc1d199SKonstantin Belousov 
17488ff2b41cSMark Johnston static void
174986857b36SDavid Xu itimer_fire(struct itimer *it)
175086857b36SDavid Xu {
175186857b36SDavid Xu 	struct proc *p = it->it_proc;
1752cf7d9a8cSDavid Xu 	struct thread *td;
175386857b36SDavid Xu 
175456c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
175556c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1756cf7d9a8cSDavid Xu 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
175756c06c4bSDavid Xu 			ITIMER_LOCK(it);
175856c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
175956c06c4bSDavid Xu 			timespecclear(&it->it_time.it_interval);
176056c06c4bSDavid Xu 			callout_stop(&it->it_callout);
176156c06c4bSDavid Xu 			ITIMER_UNLOCK(it);
1762cf7d9a8cSDavid Xu 			return;
17636d7b314bSDavid Xu 		}
1764cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
1765cf7d9a8cSDavid Xu 			it->it_ksi.ksi_errno = 0;
1766cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1767cf7d9a8cSDavid Xu 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
176856c06c4bSDavid Xu 		} else {
176977e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
17706d7b314bSDavid Xu 				it->it_overrun++;
177177e718f7SDavid Xu 			else
177277e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
177356c06c4bSDavid Xu 		}
177486857b36SDavid Xu 		PROC_UNLOCK(p);
177586857b36SDavid Xu 	}
177686857b36SDavid Xu }
177786857b36SDavid Xu 
177886857b36SDavid Xu static void
177986857b36SDavid Xu itimers_alloc(struct proc *p)
178086857b36SDavid Xu {
178160354683SDavid Xu 	struct itimers *its;
178286857b36SDavid Xu 
178360354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
178460354683SDavid Xu 	PROC_LOCK(p);
178560354683SDavid Xu 	if (p->p_itimers == NULL) {
178660354683SDavid Xu 		p->p_itimers = its;
178760354683SDavid Xu 		PROC_UNLOCK(p);
178860354683SDavid Xu 	}
178960354683SDavid Xu 	else {
179060354683SDavid Xu 		PROC_UNLOCK(p);
179160354683SDavid Xu 		free(its, M_SUBPROC);
179260354683SDavid Xu 	}
179386857b36SDavid Xu }
179486857b36SDavid Xu 
179586857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1796d26b1a1fSDavid Xu static void
1797e68c6191SKonstantin Belousov itimers_event_exit_exec(int start_idx, struct proc *p)
179886857b36SDavid Xu {
179986857b36SDavid Xu 	struct itimers *its;
180086857b36SDavid Xu 	struct itimer *it;
180186857b36SDavid Xu 	int i;
180286857b36SDavid Xu 
180360354683SDavid Xu 	its = p->p_itimers;
1804e68c6191SKonstantin Belousov 	if (its == NULL)
1805e68c6191SKonstantin Belousov 		return;
1806e68c6191SKonstantin Belousov 
1807e68c6191SKonstantin Belousov 	for (i = start_idx; i < TIMER_MAX; ++i) {
1808843b99c6SDavid Xu 		if ((it = its->its_timers[i]) != NULL)
1809643ee871SKonstantin Belousov 			kern_ktimer_delete(curthread, i);
181086857b36SDavid Xu 	}
1811e68c6191SKonstantin Belousov 	if (its->its_timers[0] == NULL && its->its_timers[1] == NULL &&
181286857b36SDavid Xu 	    its->its_timers[2] == NULL) {
18133138392aSMark Johnston 		/* Synchronize with itimer_proc_continue(). */
18143138392aSMark Johnston 		PROC_LOCK(p);
181560354683SDavid Xu 		p->p_itimers = NULL;
18163138392aSMark Johnston 		PROC_UNLOCK(p);
18173138392aSMark Johnston 		free(its, M_SUBPROC);
181886857b36SDavid Xu 	}
181986857b36SDavid Xu }
1820e68c6191SKonstantin Belousov 
1821e68c6191SKonstantin Belousov void
1822e68c6191SKonstantin Belousov itimers_exec(struct proc *p)
1823e68c6191SKonstantin Belousov {
1824e68c6191SKonstantin Belousov 	/*
1825e68c6191SKonstantin Belousov 	 * According to susv3, XSI interval timers should be inherited
1826e68c6191SKonstantin Belousov 	 * by new image.
1827e68c6191SKonstantin Belousov 	 */
1828e68c6191SKonstantin Belousov 	itimers_event_exit_exec(3, p);
1829e68c6191SKonstantin Belousov }
1830e68c6191SKonstantin Belousov 
1831e68c6191SKonstantin Belousov void
1832e68c6191SKonstantin Belousov itimers_exit(struct proc *p)
1833e68c6191SKonstantin Belousov {
1834e68c6191SKonstantin Belousov 	itimers_event_exit_exec(0, p);
183586857b36SDavid Xu }
1836