xref: /freebsd/sys/kern/kern_time.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
19454b2d8SWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro 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>
46d26b1a1fSDavid Xu #include <sys/eventhandler.h>
47df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
48797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/kernel.h>
50098176f0SDavide Italiano #include <sys/sleepqueue.h>
51efa42cbcSPaul Saab #include <sys/syscallsubr.h>
5277e718f7SDavid Xu #include <sys/sysctl.h>
5394c8fcd8SPeter Wemm #include <sys/sysent.h>
54acd3428bSRobert Watson #include <sys/priv.h>
55df8bae1dSRodney W. Grimes #include <sys/proc.h>
566aeb05d7STom Rhodes #include <sys/posix4.h>
57708e7684SPeter Wemm #include <sys/time.h>
5886857b36SDavid Xu #include <sys/timers.h>
5991266b96SPoul-Henning Kamp #include <sys/timetc.h>
60df8bae1dSRodney W. Grimes #include <sys/vnode.h>
61de56aee0SKonstantin Belousov #ifdef KTRACE
62de56aee0SKonstantin Belousov #include <sys/ktrace.h>
63de56aee0SKonstantin Belousov #endif
64fb919e4dSMark Murray 
655b870b7bSPeter Wemm #include <vm/vm.h>
665b870b7bSPeter Wemm #include <vm/vm_extern.h>
67df8bae1dSRodney W. Grimes 
6886857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
69d65f1abcSDavid Xu #define CPUCLOCK_BIT		0x80000000
70d65f1abcSDavid Xu #define CPUCLOCK_PROCESS_BIT	0x40000000
71d65f1abcSDavid Xu #define CPUCLOCK_ID_MASK	(~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
72d65f1abcSDavid Xu #define MAKE_THREAD_CPUCLOCK(tid)	(CPUCLOCK_BIT|(tid))
73d65f1abcSDavid Xu #define MAKE_PROCESS_CPUCLOCK(pid)	\
74d65f1abcSDavid Xu 	(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
7586857b36SDavid Xu 
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 *);
102993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
103993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
10486857b36SDavid Xu static int	realtimer_create(struct itimer *);
10586857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
10686857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
10786857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
10886857b36SDavid Xu static int	realtimer_delete(struct itimer *);
10956c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
11086857b36SDavid Xu static void	realtimer_expire(void *);
11186857b36SDavid Xu 
11286857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
11386857b36SDavid Xu void		itimer_fire(struct itimer *it);
11456c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
11586857b36SDavid Xu 
11686857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
11786857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
11886857b36SDavid Xu 
11986857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
12086857b36SDavid Xu 
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 {
249d65f1abcSDavid Xu 	runtime = cputick2usec(runtime);
250d65f1abcSDavid Xu 	ats->tv_sec = runtime / 1000000;
251d65f1abcSDavid Xu 	ats->tv_nsec = runtime % 1000000 * 1000;
252d65f1abcSDavid Xu }
253d65f1abcSDavid Xu 
254d65f1abcSDavid Xu static void
255d65f1abcSDavid Xu get_thread_cputime(struct thread *targettd, struct timespec *ats)
256d65f1abcSDavid Xu {
257d65f1abcSDavid Xu 	uint64_t runtime, curtime, switchtime;
258d65f1abcSDavid Xu 
259d65f1abcSDavid Xu 	if (targettd == NULL) { /* current thread */
260d65f1abcSDavid Xu 		critical_enter();
261d65f1abcSDavid Xu 		switchtime = PCPU_GET(switchtime);
262d65f1abcSDavid Xu 		curtime = cpu_ticks();
263d65f1abcSDavid Xu 		runtime = curthread->td_runtime;
264d65f1abcSDavid Xu 		critical_exit();
265d65f1abcSDavid Xu 		runtime += curtime - switchtime;
266d65f1abcSDavid Xu 	} else {
267d65f1abcSDavid Xu 		thread_lock(targettd);
268d65f1abcSDavid Xu 		runtime = targettd->td_runtime;
269d65f1abcSDavid Xu 		thread_unlock(targettd);
270d65f1abcSDavid Xu 	}
271d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
272d65f1abcSDavid Xu }
273d65f1abcSDavid Xu 
274d65f1abcSDavid Xu static void
275d65f1abcSDavid Xu get_process_cputime(struct proc *targetp, struct timespec *ats)
276d65f1abcSDavid Xu {
277d65f1abcSDavid Xu 	uint64_t runtime;
278d65f1abcSDavid Xu 	struct rusage ru;
279d65f1abcSDavid Xu 
2805c7bebf9SKonstantin Belousov 	PROC_STATLOCK(targetp);
281d65f1abcSDavid Xu 	rufetch(targetp, &ru);
282d65f1abcSDavid Xu 	runtime = targetp->p_rux.rux_runtime;
2835c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(targetp);
284d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
285d65f1abcSDavid Xu }
286d65f1abcSDavid Xu 
287d65f1abcSDavid Xu static int
288d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
289d65f1abcSDavid Xu {
290d65f1abcSDavid Xu 	struct proc *p, *p2;
291d65f1abcSDavid Xu 	struct thread *td2;
292d65f1abcSDavid Xu 	lwpid_t tid;
293d65f1abcSDavid Xu 	pid_t pid;
294d65f1abcSDavid Xu 	int error;
295d65f1abcSDavid Xu 
296d65f1abcSDavid Xu 	p = td->td_proc;
297d65f1abcSDavid Xu 	if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
298d65f1abcSDavid Xu 		tid = clock_id & CPUCLOCK_ID_MASK;
299d65f1abcSDavid Xu 		td2 = tdfind(tid, p->p_pid);
300d65f1abcSDavid Xu 		if (td2 == NULL)
301d65f1abcSDavid Xu 			return (EINVAL);
302d65f1abcSDavid Xu 		get_thread_cputime(td2, ats);
303d65f1abcSDavid Xu 		PROC_UNLOCK(td2->td_proc);
304d65f1abcSDavid Xu 	} else {
305d65f1abcSDavid Xu 		pid = clock_id & CPUCLOCK_ID_MASK;
306c7c536c7SKonstantin Belousov 		error = pget(pid, PGET_CANSEE, &p2);
307c7c536c7SKonstantin Belousov 		if (error != 0)
308d65f1abcSDavid Xu 			return (EINVAL);
309d65f1abcSDavid Xu 		get_process_cputime(p2, ats);
310d65f1abcSDavid Xu 		PROC_UNLOCK(p2);
311d65f1abcSDavid Xu 	}
312d65f1abcSDavid Xu 	return (0);
313d65f1abcSDavid Xu }
314d65f1abcSDavid Xu 
315f0b479cdSPaul Saab int
316f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
317f0b479cdSPaul Saab {
318de0a9241SKelly Yancey 	struct timeval sys, user;
31978c85e8dSJohn Baldwin 	struct proc *p;
32094c8fcd8SPeter Wemm 
32178c85e8dSJohn Baldwin 	p = td->td_proc;
322f0b479cdSPaul Saab 	switch (clock_id) {
3235e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
3245e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
325f0b479cdSPaul Saab 		nanotime(ats);
326b8817154SKelly Yancey 		break;
3275e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3285e758b95SRobert Watson 		getnanotime(ats);
3295e758b95SRobert Watson 		break;
330b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
33178c85e8dSJohn Baldwin 		PROC_LOCK(p);
3325c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
33378c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3345c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
33578c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
336f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
337b8817154SKelly Yancey 		break;
338b8817154SKelly Yancey 	case CLOCK_PROF:
33978c85e8dSJohn Baldwin 		PROC_LOCK(p);
3405c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
34178c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3425c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
34378c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
344de0a9241SKelly Yancey 		timevaladd(&user, &sys);
345f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
346de0a9241SKelly Yancey 		break;
3475e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
3485e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3495eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3505e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
351f0b479cdSPaul Saab 		nanouptime(ats);
352b8817154SKelly Yancey 		break;
3535e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3545e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3555e758b95SRobert Watson 		getnanouptime(ats);
3565e758b95SRobert Watson 		break;
3575e758b95SRobert Watson 	case CLOCK_SECOND:
3585e758b95SRobert Watson 		ats->tv_sec = time_second;
3595e758b95SRobert Watson 		ats->tv_nsec = 0;
3605e758b95SRobert Watson 		break;
36100d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
362d65f1abcSDavid Xu 		get_thread_cputime(NULL, ats);
363d65f1abcSDavid Xu 		break;
364d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
365d65f1abcSDavid Xu 		PROC_LOCK(p);
366d65f1abcSDavid Xu 		get_process_cputime(p, ats);
367d65f1abcSDavid Xu 		PROC_UNLOCK(p);
36800d6ac63SDavid Xu 		break;
369b8817154SKelly Yancey 	default:
370d65f1abcSDavid Xu 		if ((int)clock_id >= 0)
3715cb3dc8fSPoul-Henning Kamp 			return (EINVAL);
372d65f1abcSDavid Xu 		return (get_cputime(td, clock_id, ats));
373b8817154SKelly Yancey 	}
374f0b479cdSPaul Saab 	return (0);
37594c8fcd8SPeter Wemm }
37694c8fcd8SPeter Wemm 
37794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
37894c8fcd8SPeter Wemm struct clock_settime_args {
37994c8fcd8SPeter Wemm 	clockid_t clock_id;
38094c8fcd8SPeter Wemm 	const struct	timespec *tp;
38194c8fcd8SPeter Wemm };
38294c8fcd8SPeter Wemm #endif
38394c8fcd8SPeter Wemm /* ARGSUSED */
38494c8fcd8SPeter Wemm int
3858451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
38694c8fcd8SPeter Wemm {
38794c8fcd8SPeter Wemm 	struct timespec ats;
38894c8fcd8SPeter Wemm 	int error;
38994c8fcd8SPeter Wemm 
390f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
391f0b479cdSPaul Saab 		return (error);
392f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
393f0b479cdSPaul Saab }
394f0b479cdSPaul Saab 
39590a79ac5SConrad Meyer static int allow_insane_settime = 0;
39690a79ac5SConrad Meyer SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN,
39790a79ac5SConrad Meyer     &allow_insane_settime, 0,
39890a79ac5SConrad Meyer     "do not perform possibly restrictive checks on settime(2) args");
39990a79ac5SConrad Meyer 
400f0b479cdSPaul Saab int
401f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
402f0b479cdSPaul Saab {
403f0b479cdSPaul Saab 	struct timeval atv;
404f0b479cdSPaul Saab 	int error;
405f0b479cdSPaul Saab 
406acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
4077edfb592SJohn Baldwin 		return (error);
408f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
4097edfb592SJohn Baldwin 		return (EINVAL);
4103e18d701SDmitry Chagin 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000 ||
4113e18d701SDmitry Chagin 	    ats->tv_sec < 0)
4127edfb592SJohn Baldwin 		return (EINVAL);
41381d606f5SEd Maste 	if (!allow_insane_settime && ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60)
41490a79ac5SConrad Meyer 		return (EINVAL);
415a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
416f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
4177edfb592SJohn Baldwin 	error = settime(td, &atv);
41894c8fcd8SPeter Wemm 	return (error);
41994c8fcd8SPeter Wemm }
42094c8fcd8SPeter Wemm 
42194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
42294c8fcd8SPeter Wemm struct clock_getres_args {
42394c8fcd8SPeter Wemm 	clockid_t clock_id;
42494c8fcd8SPeter Wemm 	struct	timespec *tp;
42594c8fcd8SPeter Wemm };
42694c8fcd8SPeter Wemm #endif
42794c8fcd8SPeter Wemm int
4288451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
42994c8fcd8SPeter Wemm {
43094c8fcd8SPeter Wemm 	struct timespec ts;
431f0b479cdSPaul Saab 	int error;
43294c8fcd8SPeter Wemm 
433f0b479cdSPaul Saab 	if (uap->tp == NULL)
434f0b479cdSPaul Saab 		return (0);
435f0b479cdSPaul Saab 
436f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
437f0b479cdSPaul Saab 	if (error == 0)
438f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
439f0b479cdSPaul Saab 	return (error);
440f0b479cdSPaul Saab }
441f0b479cdSPaul Saab 
442f0b479cdSPaul Saab int
443f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
444f0b479cdSPaul Saab {
445f0b479cdSPaul Saab 
446f0b479cdSPaul Saab 	ts->tv_sec = 0;
447f0b479cdSPaul Saab 	switch (clock_id) {
448b8817154SKelly Yancey 	case CLOCK_REALTIME:
4495e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
4505e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
451b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
4525e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
4535e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
4545eefd889SAndre Oppermann 	case CLOCK_UPTIME:
4555e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
4565e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
457ac0653dcSBruce Evans 		/*
458ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
459ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
460ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
461ac0653dcSBruce Evans 		 */
462f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
463b8817154SKelly Yancey 		break;
464b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
465b8817154SKelly Yancey 	case CLOCK_PROF:
466b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
46755e0987aSPedro F. Giffuni 		ts->tv_nsec = howmany(1000000000, hz);
468b8817154SKelly Yancey 		break;
4695e758b95SRobert Watson 	case CLOCK_SECOND:
4705e758b95SRobert Watson 		ts->tv_sec = 1;
4715e758b95SRobert Watson 		ts->tv_nsec = 0;
4725e758b95SRobert Watson 		break;
47300d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
474d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
475d65f1abcSDavid Xu 	cputime:
47600d6ac63SDavid Xu 		/* sync with cputick2usec */
47700d6ac63SDavid Xu 		ts->tv_nsec = 1000000 / cpu_tickrate();
47800d6ac63SDavid Xu 		if (ts->tv_nsec == 0)
47900d6ac63SDavid Xu 			ts->tv_nsec = 1000;
48000d6ac63SDavid Xu 		break;
481b8817154SKelly Yancey 	default:
482d65f1abcSDavid Xu 		if ((int)clock_id < 0)
483d65f1abcSDavid Xu 			goto cputime;
484b8817154SKelly Yancey 		return (EINVAL);
48594c8fcd8SPeter Wemm 	}
486de0a9241SKelly Yancey 	return (0);
48794c8fcd8SPeter Wemm }
48894c8fcd8SPeter Wemm 
4897fdf2c85SPaul Saab int
4907fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
4915b870b7bSPeter Wemm {
4923f8455b0SEric van Gyzen 
4933f8455b0SEric van Gyzen 	return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt,
4943f8455b0SEric van Gyzen 	    rmt));
4953f8455b0SEric van Gyzen }
4963f8455b0SEric van Gyzen 
4973f8455b0SEric van Gyzen static uint8_t nanowait[MAXCPU];
4983f8455b0SEric van Gyzen 
4993f8455b0SEric van Gyzen int
5003f8455b0SEric van Gyzen kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
5013f8455b0SEric van Gyzen     const struct timespec *rqt, struct timespec *rmt)
5023f8455b0SEric van Gyzen {
5033f8455b0SEric van Gyzen 	struct timespec ts, now;
504098176f0SDavide Italiano 	sbintime_t sbt, sbtt, prec, tmp;
505980c545dSAlexander Motin 	time_t over;
50633841826SPoul-Henning Kamp 	int error;
5073f8455b0SEric van Gyzen 	bool is_abs_real;
5085b870b7bSPeter Wemm 
5097d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
510708e7684SPeter Wemm 		return (EINVAL);
5113f8455b0SEric van Gyzen 	if ((flags & ~TIMER_ABSTIME) != 0)
5123f8455b0SEric van Gyzen 		return (EINVAL);
5133f8455b0SEric van Gyzen 	switch (clock_id) {
5143f8455b0SEric van Gyzen 	case CLOCK_REALTIME:
5153f8455b0SEric van Gyzen 	case CLOCK_REALTIME_PRECISE:
5163f8455b0SEric van Gyzen 	case CLOCK_REALTIME_FAST:
5173f8455b0SEric van Gyzen 	case CLOCK_SECOND:
5183f8455b0SEric van Gyzen 		is_abs_real = (flags & TIMER_ABSTIME) != 0;
5193f8455b0SEric van Gyzen 		break;
5203f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC:
5213f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC_PRECISE:
5223f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC_FAST:
5233f8455b0SEric van Gyzen 	case CLOCK_UPTIME:
5243f8455b0SEric van Gyzen 	case CLOCK_UPTIME_PRECISE:
5253f8455b0SEric van Gyzen 	case CLOCK_UPTIME_FAST:
5263f8455b0SEric van Gyzen 		is_abs_real = false;
5273f8455b0SEric van Gyzen 		break;
5283f8455b0SEric van Gyzen 	case CLOCK_VIRTUAL:
5293f8455b0SEric van Gyzen 	case CLOCK_PROF:
5303f8455b0SEric van Gyzen 	case CLOCK_PROCESS_CPUTIME_ID:
5313f8455b0SEric van Gyzen 		return (ENOTSUP);
5323f8455b0SEric van Gyzen 	case CLOCK_THREAD_CPUTIME_ID:
5333f8455b0SEric van Gyzen 	default:
5343f8455b0SEric van Gyzen 		return (EINVAL);
5353f8455b0SEric van Gyzen 	}
5363f8455b0SEric van Gyzen 	do {
537980c545dSAlexander Motin 		ts = *rqt;
5383f8455b0SEric van Gyzen 		if ((flags & TIMER_ABSTIME) != 0) {
5393f8455b0SEric van Gyzen 			if (is_abs_real)
5403f8455b0SEric van Gyzen 				td->td_rtcgen =
5413f8455b0SEric van Gyzen 				    atomic_load_acq_int(&rtc_generation);
5423f8455b0SEric van Gyzen 			error = kern_clock_gettime(td, clock_id, &now);
5433f8455b0SEric van Gyzen 			KASSERT(error == 0, ("kern_clock_gettime: %d", error));
5443f8455b0SEric van Gyzen 			timespecsub(&ts, &now);
5453f8455b0SEric van Gyzen 		}
5463f8455b0SEric van Gyzen 		if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
5473f8455b0SEric van Gyzen 			error = EWOULDBLOCK;
5483f8455b0SEric van Gyzen 			break;
5493f8455b0SEric van Gyzen 		}
550980c545dSAlexander Motin 		if (ts.tv_sec > INT32_MAX / 2) {
551980c545dSAlexander Motin 			over = ts.tv_sec - INT32_MAX / 2;
552980c545dSAlexander Motin 			ts.tv_sec -= over;
553980c545dSAlexander Motin 		} else
554980c545dSAlexander Motin 			over = 0;
555980c545dSAlexander Motin 		tmp = tstosbt(ts);
556098176f0SDavide Italiano 		prec = tmp;
557098176f0SDavide Italiano 		prec >>= tc_precexp;
558098176f0SDavide Italiano 		if (TIMESEL(&sbt, tmp))
559098176f0SDavide Italiano 			sbt += tc_tick_sbt;
560098176f0SDavide Italiano 		sbt += tmp;
5610dbf17e6SAlexander Motin 		error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
5620dbf17e6SAlexander Motin 		    sbt, prec, C_ABSOLUTE);
5633f8455b0SEric van Gyzen 	} while (error == 0 && is_abs_real && td->td_rtcgen == 0);
5643f8455b0SEric van Gyzen 	td->td_rtcgen = 0;
56533841826SPoul-Henning Kamp 	if (error != EWOULDBLOCK) {
5663f8455b0SEric van Gyzen 		TIMESEL(&sbtt, tmp);
5673f8455b0SEric van Gyzen 		if (sbtt >= sbt)
5683f8455b0SEric van Gyzen 			return (0);
56933841826SPoul-Henning Kamp 		if (error == ERESTART)
57094c8fcd8SPeter Wemm 			error = EINTR;
5713f8455b0SEric van Gyzen 		if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) {
572098176f0SDavide Italiano 			ts = sbttots(sbt - sbtt);
573980c545dSAlexander Motin 			ts.tv_sec += over;
57433841826SPoul-Henning Kamp 			if (ts.tv_sec < 0)
57533841826SPoul-Henning Kamp 				timespecclear(&ts);
57600af9731SPoul-Henning Kamp 			*rmt = ts;
57700af9731SPoul-Henning Kamp 		}
57833841826SPoul-Henning Kamp 		return (error);
57933841826SPoul-Henning Kamp 	}
58033841826SPoul-Henning Kamp 	return (0);
5815b870b7bSPeter Wemm }
58294c8fcd8SPeter Wemm 
5835b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
5845b870b7bSPeter Wemm struct nanosleep_args {
5855b870b7bSPeter Wemm 	struct	timespec *rqtp;
5865b870b7bSPeter Wemm 	struct	timespec *rmtp;
5875b870b7bSPeter Wemm };
5885b870b7bSPeter Wemm #endif
5895b870b7bSPeter Wemm /* ARGSUSED */
5905b870b7bSPeter Wemm int
5918451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
5925b870b7bSPeter Wemm {
5933f8455b0SEric van Gyzen 
5943f8455b0SEric van Gyzen 	return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME,
5953f8455b0SEric van Gyzen 	    uap->rqtp, uap->rmtp));
5963f8455b0SEric van Gyzen }
5973f8455b0SEric van Gyzen 
5983f8455b0SEric van Gyzen #ifndef _SYS_SYSPROTO_H_
5993f8455b0SEric van Gyzen struct clock_nanosleep_args {
6003f8455b0SEric van Gyzen 	clockid_t clock_id;
6013f8455b0SEric van Gyzen 	int 	  flags;
6023f8455b0SEric van Gyzen 	struct	timespec *rqtp;
6033f8455b0SEric van Gyzen 	struct	timespec *rmtp;
6043f8455b0SEric van Gyzen };
6053f8455b0SEric van Gyzen #endif
6063f8455b0SEric van Gyzen /* ARGSUSED */
6073f8455b0SEric van Gyzen int
6083f8455b0SEric van Gyzen sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap)
6093f8455b0SEric van Gyzen {
6103f8455b0SEric van Gyzen 	int error;
6113f8455b0SEric van Gyzen 
6123f8455b0SEric van Gyzen 	error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp,
6133f8455b0SEric van Gyzen 	    uap->rmtp);
6143f8455b0SEric van Gyzen 	return (kern_posix_error(td, error));
6153f8455b0SEric van Gyzen }
6163f8455b0SEric van Gyzen 
6173f8455b0SEric van Gyzen static int
6183f8455b0SEric van Gyzen user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
6193f8455b0SEric van Gyzen     const struct timespec *ua_rqtp, struct timespec *ua_rmtp)
6203f8455b0SEric van Gyzen {
6215b870b7bSPeter Wemm 	struct timespec rmt, rqt;
622fb99ab88SMatthew Dillon 	int error;
6235b870b7bSPeter Wemm 
6243f8455b0SEric van Gyzen 	error = copyin(ua_rqtp, &rqt, sizeof(rqt));
6255b870b7bSPeter Wemm 	if (error)
6265b870b7bSPeter Wemm 		return (error);
6273f8455b0SEric van Gyzen 	if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
6283f8455b0SEric van Gyzen 	    !useracc(ua_rmtp, sizeof(rmt), VM_PROT_WRITE))
62931f3e2adSAlfred Perlstein 		return (EFAULT);
6303f8455b0SEric van Gyzen 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
6313f8455b0SEric van Gyzen 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
632fb99ab88SMatthew Dillon 		int error2;
633fb99ab88SMatthew Dillon 
6343f8455b0SEric van Gyzen 		error2 = copyout(&rmt, ua_rmtp, sizeof(rmt));
63531f3e2adSAlfred Perlstein 		if (error2)
636fb99ab88SMatthew Dillon 			error = error2;
637708e7684SPeter Wemm 	}
638708e7684SPeter Wemm 	return (error);
63994c8fcd8SPeter Wemm }
64094c8fcd8SPeter Wemm 
6415b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
642df8bae1dSRodney W. Grimes struct gettimeofday_args {
643df8bae1dSRodney W. Grimes 	struct	timeval *tp;
644df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
645df8bae1dSRodney W. Grimes };
646d2d3e875SBruce Evans #endif
647df8bae1dSRodney W. Grimes /* ARGSUSED */
64826f9a767SRodney W. Grimes int
6498451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
650df8bae1dSRodney W. Grimes {
651df8bae1dSRodney W. Grimes 	struct timeval atv;
652411c25edSTim J. Robbins 	struct timezone rtz;
653df8bae1dSRodney W. Grimes 	int error = 0;
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes 	if (uap->tp) {
656df8bae1dSRodney W. Grimes 		microtime(&atv);
65701609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
658df8bae1dSRodney W. Grimes 	}
65921dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
66091f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
66191f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
662411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
66321dcdb38SPoul-Henning Kamp 	}
664df8bae1dSRodney W. Grimes 	return (error);
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
667d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
668df8bae1dSRodney W. Grimes struct settimeofday_args {
669df8bae1dSRodney W. Grimes 	struct	timeval *tv;
670df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
671df8bae1dSRodney W. Grimes };
672d2d3e875SBruce Evans #endif
673df8bae1dSRodney W. Grimes /* ARGSUSED */
67426f9a767SRodney W. Grimes int
6758451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
676df8bae1dSRodney W. Grimes {
677b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
678b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
679b88ec951SJohn Baldwin 	int error;
680b88ec951SJohn Baldwin 
681b88ec951SJohn Baldwin 	if (uap->tv) {
682b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
683b88ec951SJohn Baldwin 		if (error)
684b88ec951SJohn Baldwin 			return (error);
685b88ec951SJohn Baldwin 		tvp = &atv;
686b88ec951SJohn Baldwin 	} else
687b88ec951SJohn Baldwin 		tvp = NULL;
688b88ec951SJohn Baldwin 	if (uap->tzp) {
689b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
690b88ec951SJohn Baldwin 		if (error)
691b88ec951SJohn Baldwin 			return (error);
692b88ec951SJohn Baldwin 		tzp = &atz;
693b88ec951SJohn Baldwin 	} else
694b88ec951SJohn Baldwin 		tzp = NULL;
695b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
696b88ec951SJohn Baldwin }
697b88ec951SJohn Baldwin 
698b88ec951SJohn Baldwin int
699b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
700b88ec951SJohn Baldwin {
701b88ec951SJohn Baldwin 	int error;
702fb99ab88SMatthew Dillon 
703acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
704b88ec951SJohn Baldwin 	if (error)
7057edfb592SJohn Baldwin 		return (error);
706df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
707b88ec951SJohn Baldwin 	if (tv) {
7083e18d701SDmitry Chagin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
7093e18d701SDmitry Chagin 		    tv->tv_sec < 0)
7107edfb592SJohn Baldwin 			return (EINVAL);
711b88ec951SJohn Baldwin 		error = settime(td, tv);
712708e7684SPeter Wemm 	}
713b88ec951SJohn Baldwin 	if (tzp && error == 0) {
714b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
715b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
716b88ec951SJohn Baldwin 	}
7177edfb592SJohn Baldwin 	return (error);
718b88ec951SJohn Baldwin }
7197edfb592SJohn Baldwin 
720df8bae1dSRodney W. Grimes /*
721873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
722873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
723873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
724873fbcd7SRobert Watson  * time until they expire.
725df8bae1dSRodney W. Grimes  *
726873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
727873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
728873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
729873fbcd7SRobert Watson  * drifting.
730df8bae1dSRodney W. Grimes  *
731df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
732873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
733873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
734873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
735873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
736873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
737873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
738873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
739873fbcd7SRobert Watson  * should go off.
740df8bae1dSRodney W. Grimes  */
741d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
742df8bae1dSRodney W. Grimes struct getitimer_args {
743df8bae1dSRodney W. Grimes 	u_int	which;
744df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
745df8bae1dSRodney W. Grimes };
746d2d3e875SBruce Evans #endif
74726f9a767SRodney W. Grimes int
7488451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap)
749df8bae1dSRodney W. Grimes {
750df8bae1dSRodney W. Grimes 	struct itimerval aitv;
751c90110d6SJohn Baldwin 	int error;
752df8bae1dSRodney W. Grimes 
753cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
754cfa0efe7SMaxim Sobolev 	if (error != 0)
755cfa0efe7SMaxim Sobolev 		return (error);
756cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
757cfa0efe7SMaxim Sobolev }
758cfa0efe7SMaxim Sobolev 
759cfa0efe7SMaxim Sobolev int
760cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
761cfa0efe7SMaxim Sobolev {
762cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
763cfa0efe7SMaxim Sobolev 	struct timeval ctv;
764cfa0efe7SMaxim Sobolev 
765cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
766df8bae1dSRodney W. Grimes 		return (EINVAL);
767fb99ab88SMatthew Dillon 
768cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
769df8bae1dSRodney W. Grimes 		/*
770ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
771df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
772df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
773df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
774df8bae1dSRodney W. Grimes 		 */
77596d7f8efSTim J. Robbins 		PROC_LOCK(p);
776cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
77796d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
778cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
779b5ea3779SAlexander Motin 			microuptime(&ctv);
780cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
781cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
782df8bae1dSRodney W. Grimes 			else
783cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
784227ee8a1SPoul-Henning Kamp 		}
785fb99ab88SMatthew Dillon 	} else {
7865c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
787cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
7885c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
789fb99ab88SMatthew Dillon 	}
790de56aee0SKonstantin Belousov #ifdef KTRACE
791de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
792de56aee0SKonstantin Belousov 		ktritimerval(aitv);
793de56aee0SKonstantin Belousov #endif
794cfa0efe7SMaxim Sobolev 	return (0);
795df8bae1dSRodney W. Grimes }
796df8bae1dSRodney W. Grimes 
797d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
798df8bae1dSRodney W. Grimes struct setitimer_args {
799df8bae1dSRodney W. Grimes 	u_int	which;
800df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
801df8bae1dSRodney W. Grimes };
802d2d3e875SBruce Evans #endif
80326f9a767SRodney W. Grimes int
8048451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap)
805df8bae1dSRodney W. Grimes {
806cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
807c90110d6SJohn Baldwin 	int error;
80896d7f8efSTim J. Robbins 
80996d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
81096d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
8118451d0ddSKip Macy 		return (sys_getitimer(td, (struct getitimer_args *)uap));
81296d7f8efSTim J. Robbins 	}
813df8bae1dSRodney W. Grimes 
81496d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
815df8bae1dSRodney W. Grimes 		return (error);
816cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
817cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
818cfa0efe7SMaxim Sobolev 		return (error);
819cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
820cfa0efe7SMaxim Sobolev }
821cfa0efe7SMaxim Sobolev 
822cfa0efe7SMaxim Sobolev int
823c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
824c90110d6SJohn Baldwin     struct itimerval *oitv)
825cfa0efe7SMaxim Sobolev {
826cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
827cfa0efe7SMaxim Sobolev 	struct timeval ctv;
828b5ea3779SAlexander Motin 	sbintime_t sbt, pr;
829cfa0efe7SMaxim Sobolev 
8305e85ac17SJohn Baldwin 	if (aitv == NULL)
8315e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
8325e85ac17SJohn Baldwin 
833cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
83496d7f8efSTim J. Robbins 		return (EINVAL);
835de56aee0SKonstantin Belousov #ifdef KTRACE
836de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
837de56aee0SKonstantin Belousov 		ktritimerval(aitv);
838de56aee0SKonstantin Belousov #endif
839b5ea3779SAlexander Motin 	if (itimerfix(&aitv->it_value) ||
840b5ea3779SAlexander Motin 	    aitv->it_value.tv_sec > INT32_MAX / 2)
841cfa0efe7SMaxim Sobolev 		return (EINVAL);
842cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
843cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
844b5ea3779SAlexander Motin 	else if (itimerfix(&aitv->it_interval) ||
845b5ea3779SAlexander Motin 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
84696d7f8efSTim J. Robbins 		return (EINVAL);
84796d7f8efSTim J. Robbins 
848cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
84996d7f8efSTim J. Robbins 		PROC_LOCK(p);
8504cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
8514f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
852b5ea3779SAlexander Motin 		microuptime(&ctv);
853cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
854b5ea3779SAlexander Motin 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
855cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
856b5ea3779SAlexander Motin 			sbt = tvtosbt(aitv->it_value);
857b5ea3779SAlexander Motin 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
858b5ea3779SAlexander Motin 			    realitexpire, p, C_ABSOLUTE);
85925b4d3a8SJohn Baldwin 		}
860cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
861cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
86296d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
863cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
864cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
865cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
86696d7f8efSTim J. Robbins 			else
867cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
868fb99ab88SMatthew Dillon 		}
86996d7f8efSTim J. Robbins 	} else {
870d10a1df8SAlexander Motin 		if (aitv->it_interval.tv_sec == 0 &&
871d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec != 0 &&
872d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec < tick)
873d10a1df8SAlexander Motin 			aitv->it_interval.tv_usec = tick;
874d10a1df8SAlexander Motin 		if (aitv->it_value.tv_sec == 0 &&
875d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec != 0 &&
876d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec < tick)
877d10a1df8SAlexander Motin 			aitv->it_value.tv_usec = tick;
8785c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
879cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
880cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
8815c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
88296d7f8efSTim J. Robbins 	}
883de56aee0SKonstantin Belousov #ifdef KTRACE
884de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
885de56aee0SKonstantin Belousov 		ktritimerval(oitv);
886de56aee0SKonstantin Belousov #endif
88796d7f8efSTim J. Robbins 	return (0);
888df8bae1dSRodney W. Grimes }
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes /*
891df8bae1dSRodney W. Grimes  * Real interval timer expired:
892df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
893df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
894df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
895df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
896df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
897c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
8989207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
8999207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
9009207f00aSBruce Evans  * interrupt even when we're delayed.
901df8bae1dSRodney W. Grimes  */
902df8bae1dSRodney W. Grimes void
90391afe087SPoul-Henning Kamp realitexpire(void *arg)
904df8bae1dSRodney W. Grimes {
90591afe087SPoul-Henning Kamp 	struct proc *p;
906b5ea3779SAlexander Motin 	struct timeval ctv;
907b5ea3779SAlexander Motin 	sbintime_t isbt;
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
9108451d0ddSKip Macy 	kern_psignal(p, SIGALRM);
9114cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
9124cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
9135499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
9145499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
915df8bae1dSRodney W. Grimes 		return;
916df8bae1dSRodney W. Grimes 	}
917b5ea3779SAlexander Motin 	isbt = tvtosbt(p->p_realtimer.it_interval);
918b5ea3779SAlexander Motin 	if (isbt >= sbt_timethreshold)
919b5ea3779SAlexander Motin 		getmicrouptime(&ctv);
920b5ea3779SAlexander Motin 	else
921b5ea3779SAlexander Motin 		microuptime(&ctv);
922b5ea3779SAlexander Motin 	do {
923df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
924df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
925b5ea3779SAlexander Motin 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
926b5ea3779SAlexander Motin 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
927b5ea3779SAlexander Motin 	    isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes /*
931df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
932df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
933df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
934df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
935df8bae1dSRodney W. Grimes  */
93626f9a767SRodney W. Grimes int
93791afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
938df8bae1dSRodney W. Grimes {
939df8bae1dSRodney W. Grimes 
94086857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
941df8bae1dSRodney W. Grimes 		return (EINVAL);
942b5ea3779SAlexander Motin 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
943b5ea3779SAlexander Motin 	    tv->tv_usec < (u_int)tick / 16)
944b5ea3779SAlexander Motin 		tv->tv_usec = (u_int)tick / 16;
945df8bae1dSRodney W. Grimes 	return (0);
946df8bae1dSRodney W. Grimes }
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes /*
949df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
950df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
951df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
952df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
953df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
954df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
955df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
956df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
957df8bae1dSRodney W. Grimes  */
95826f9a767SRodney W. Grimes int
95991afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
960df8bae1dSRodney W. Grimes {
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
963df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
964df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
965df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
966df8bae1dSRodney W. Grimes 			goto expire;
967df8bae1dSRodney W. Grimes 		}
968df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
969df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
970df8bae1dSRodney W. Grimes 	}
971df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
972df8bae1dSRodney W. Grimes 	usec = 0;
9734cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
974df8bae1dSRodney W. Grimes 		return (1);
975df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
976df8bae1dSRodney W. Grimes expire:
9774cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
978df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
979df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
980df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
981df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
982df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
983df8bae1dSRodney W. Grimes 		}
984df8bae1dSRodney W. Grimes 	} else
985df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
986df8bae1dSRodney W. Grimes 	return (0);
987df8bae1dSRodney W. Grimes }
988df8bae1dSRodney W. Grimes 
989df8bae1dSRodney W. Grimes /*
990df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
991df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
992df8bae1dSRodney W. Grimes  * results which are before the beginning,
993df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
994df8bae1dSRodney W. Grimes  * Caveat emptor.
995df8bae1dSRodney W. Grimes  */
99626f9a767SRodney W. Grimes void
9976ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
998df8bae1dSRodney W. Grimes {
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
1001df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
1002df8bae1dSRodney W. Grimes 	timevalfix(t1);
1003df8bae1dSRodney W. Grimes }
1004df8bae1dSRodney W. Grimes 
100526f9a767SRodney W. Grimes void
10066ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
1007df8bae1dSRodney W. Grimes {
1008df8bae1dSRodney W. Grimes 
1009df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
1010df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
1011df8bae1dSRodney W. Grimes 	timevalfix(t1);
1012df8bae1dSRodney W. Grimes }
1013df8bae1dSRodney W. Grimes 
101487b6de2bSPoul-Henning Kamp static void
101591afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
1016df8bae1dSRodney W. Grimes {
1017df8bae1dSRodney W. Grimes 
1018df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
1019df8bae1dSRodney W. Grimes 		t1->tv_sec--;
1020df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
1021df8bae1dSRodney W. Grimes 	}
1022df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
1023df8bae1dSRodney W. Grimes 		t1->tv_sec++;
1024df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
1025df8bae1dSRodney W. Grimes 	}
1026df8bae1dSRodney W. Grimes }
102791974ce1SSam Leffler 
102891974ce1SSam Leffler /*
1029addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
103091974ce1SSam Leffler  */
103191974ce1SSam Leffler int
103291974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
103391974ce1SSam Leffler {
103491974ce1SSam Leffler 	struct timeval tv, delta;
103591974ce1SSam Leffler 	int rv = 0;
103691974ce1SSam Leffler 
1037addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
1038addea9d4SSam Leffler 	delta = tv;
1039addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
104091974ce1SSam Leffler 
104191974ce1SSam Leffler 	/*
104291974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
104391974ce1SSam Leffler 	 * even if interval is huge.
104491974ce1SSam Leffler 	 */
104591974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
104691974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
104791974ce1SSam Leffler 		*lasttime = tv;
104891974ce1SSam Leffler 		rv = 1;
104991974ce1SSam Leffler 	}
105091974ce1SSam Leffler 
105191974ce1SSam Leffler 	return (rv);
105291974ce1SSam Leffler }
105391974ce1SSam Leffler 
105491974ce1SSam Leffler /*
105591974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
1056addea9d4SSam Leffler  *
1057addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
1058addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
1059addea9d4SSam Leffler  *
1060893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
1061893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
1062893bec80SSam Leffler  * limiting.
1063893bec80SSam Leffler  *
1064addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
1065addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
1066addea9d4SSam Leffler  * clock ticks for minimal overhead.
106791974ce1SSam Leffler  */
106891974ce1SSam Leffler int
106991974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
107091974ce1SSam Leffler {
1071addea9d4SSam Leffler 	int now;
107291974ce1SSam Leffler 
107391974ce1SSam Leffler 	/*
1074addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
1075addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
1076addea9d4SSam Leffler 	 * lasttime.
107791974ce1SSam Leffler 	 */
1078addea9d4SSam Leffler 	now = ticks;
1079addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1080addea9d4SSam Leffler 		lasttime->tv_sec = now;
1081addea9d4SSam Leffler 		*curpps = 1;
1082893bec80SSam Leffler 		return (maxpps != 0);
1083addea9d4SSam Leffler 	} else {
1084addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
1085f8f02928SIan Lepore 		return (maxpps < 0 || *curpps <= maxpps);
1086addea9d4SSam Leffler 	}
108791974ce1SSam Leffler }
108886857b36SDavid Xu 
108986857b36SDavid Xu static void
109086857b36SDavid Xu itimer_start(void)
109186857b36SDavid Xu {
109286857b36SDavid Xu 	struct kclock rt_clock = {
109386857b36SDavid Xu 		.timer_create  = realtimer_create,
109486857b36SDavid Xu 		.timer_delete  = realtimer_delete,
109586857b36SDavid Xu 		.timer_settime = realtimer_settime,
109686857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
1097843b99c6SDavid Xu 		.event_hook    = NULL
109886857b36SDavid Xu 	};
109986857b36SDavid Xu 
110086857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
110186857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
110286857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
110386857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
110477e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
110577e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
110677e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1107993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1108d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1109993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1110d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
111186857b36SDavid Xu }
111286857b36SDavid Xu 
111386857b36SDavid Xu int
111486857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
111586857b36SDavid Xu {
111686857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
111786857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
111886857b36SDavid Xu 		return (0);
111986857b36SDavid Xu 	}
112086857b36SDavid Xu 	posix_clocks[clockid] = *clk;
112186857b36SDavid Xu 	return (1);
112286857b36SDavid Xu }
112386857b36SDavid Xu 
112486857b36SDavid Xu static int
112586857b36SDavid Xu itimer_init(void *mem, int size, int flags)
112686857b36SDavid Xu {
112786857b36SDavid Xu 	struct itimer *it;
112886857b36SDavid Xu 
112986857b36SDavid Xu 	it = (struct itimer *)mem;
113086857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
113186857b36SDavid Xu 	return (0);
113286857b36SDavid Xu }
113386857b36SDavid Xu 
113486857b36SDavid Xu static void
113586857b36SDavid Xu itimer_fini(void *mem, int size)
113686857b36SDavid Xu {
113786857b36SDavid Xu 	struct itimer *it;
113886857b36SDavid Xu 
113986857b36SDavid Xu 	it = (struct itimer *)mem;
114086857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
114186857b36SDavid Xu }
114286857b36SDavid Xu 
114386857b36SDavid Xu static void
114486857b36SDavid Xu itimer_enter(struct itimer *it)
114586857b36SDavid Xu {
114686857b36SDavid Xu 
114786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
114886857b36SDavid Xu 	it->it_usecount++;
114986857b36SDavid Xu }
115086857b36SDavid Xu 
115186857b36SDavid Xu static void
115286857b36SDavid Xu itimer_leave(struct itimer *it)
115386857b36SDavid Xu {
115486857b36SDavid Xu 
115586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
115686857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
115786857b36SDavid Xu 
115886857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
115986857b36SDavid Xu 		wakeup(it);
116086857b36SDavid Xu }
116186857b36SDavid Xu 
116286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
116361d3a4efSDavid Xu struct ktimer_create_args {
116486857b36SDavid Xu 	clockid_t clock_id;
116586857b36SDavid Xu 	struct sigevent * evp;
116661d3a4efSDavid Xu 	int * timerid;
116786857b36SDavid Xu };
116886857b36SDavid Xu #endif
116986857b36SDavid Xu int
11708451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
117186857b36SDavid Xu {
1172643ee871SKonstantin Belousov 	struct sigevent *evp, ev;
117361d3a4efSDavid Xu 	int id;
117486857b36SDavid Xu 	int error;
117586857b36SDavid Xu 
1176643ee871SKonstantin Belousov 	if (uap->evp == NULL) {
1177643ee871SKonstantin Belousov 		evp = NULL;
1178643ee871SKonstantin Belousov 	} else {
117986857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
118086857b36SDavid Xu 		if (error != 0)
118186857b36SDavid Xu 			return (error);
1182643ee871SKonstantin Belousov 		evp = &ev;
1183643ee871SKonstantin Belousov 	}
1184643ee871SKonstantin Belousov 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
118586857b36SDavid Xu 	if (error == 0) {
118661d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
118786857b36SDavid Xu 		if (error != 0)
1188643ee871SKonstantin Belousov 			kern_ktimer_delete(td, id);
118986857b36SDavid Xu 	}
119086857b36SDavid Xu 	return (error);
119186857b36SDavid Xu }
119286857b36SDavid Xu 
1193643ee871SKonstantin Belousov int
1194643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1195643ee871SKonstantin Belousov     int *timerid, int preset_id)
119686857b36SDavid Xu {
119786857b36SDavid Xu 	struct proc *p = td->td_proc;
119886857b36SDavid Xu 	struct itimer *it;
119986857b36SDavid Xu 	int id;
120086857b36SDavid Xu 	int error;
120186857b36SDavid Xu 
120286857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
120386857b36SDavid Xu 		return (EINVAL);
120486857b36SDavid Xu 
120586857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
120686857b36SDavid Xu 		return (EINVAL);
120786857b36SDavid Xu 
120886857b36SDavid Xu 	if (evp != NULL) {
120986857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
121056c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
121156c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
121286857b36SDavid Xu 			return (EINVAL);
121356c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
121456c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
121586857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
121686857b36SDavid Xu 			return (EINVAL);
121786857b36SDavid Xu 	}
121886857b36SDavid Xu 
121960354683SDavid Xu 	if (p->p_itimers == NULL)
122086857b36SDavid Xu 		itimers_alloc(p);
122186857b36SDavid Xu 
122286857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
122386857b36SDavid Xu 	it->it_flags = 0;
122486857b36SDavid Xu 	it->it_usecount = 0;
122586857b36SDavid Xu 	it->it_active = 0;
122656c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
122756c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
122886857b36SDavid Xu 	it->it_overrun = 0;
122986857b36SDavid Xu 	it->it_overrun_last = 0;
123086857b36SDavid Xu 	it->it_clockid = clock_id;
123186857b36SDavid Xu 	it->it_timerid = -1;
123286857b36SDavid Xu 	it->it_proc = p;
123386857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
123486857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
123586857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
123686857b36SDavid Xu 	if (error != 0)
123786857b36SDavid Xu 		goto out;
123886857b36SDavid Xu 
123986857b36SDavid Xu 	PROC_LOCK(p);
124086857b36SDavid Xu 	if (preset_id != -1) {
124186857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
124286857b36SDavid Xu 		id = preset_id;
124360354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
124486857b36SDavid Xu 			PROC_UNLOCK(p);
124586857b36SDavid Xu 			error = 0;
124686857b36SDavid Xu 			goto out;
124786857b36SDavid Xu 		}
124886857b36SDavid Xu 	} else {
124986857b36SDavid Xu 		/*
125086857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
125186857b36SDavid Xu 		 * for setitimer().
125286857b36SDavid Xu 		 */
125386857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
125460354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
125586857b36SDavid Xu 				break;
125686857b36SDavid Xu 		if (id == TIMER_MAX) {
125786857b36SDavid Xu 			PROC_UNLOCK(p);
125886857b36SDavid Xu 			error = EAGAIN;
125986857b36SDavid Xu 			goto out;
126086857b36SDavid Xu 		}
126186857b36SDavid Xu 	}
126286857b36SDavid Xu 	it->it_timerid = id;
126360354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
126486857b36SDavid Xu 	if (evp != NULL)
126586857b36SDavid Xu 		it->it_sigev = *evp;
126686857b36SDavid Xu 	else {
126786857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
126886857b36SDavid Xu 		switch (clock_id) {
126986857b36SDavid Xu 		default:
127086857b36SDavid Xu 		case CLOCK_REALTIME:
127186857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
127286857b36SDavid Xu 			break;
127386857b36SDavid Xu 		case CLOCK_VIRTUAL:
127486857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
127586857b36SDavid Xu 			break;
127686857b36SDavid Xu 		case CLOCK_PROF:
127786857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
127886857b36SDavid Xu 			break;
127986857b36SDavid Xu 		}
12808f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
128186857b36SDavid Xu 	}
128286857b36SDavid Xu 
128356c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
128456c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
128586857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
128686857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
128786857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
128886857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
128986857b36SDavid Xu 	}
129086857b36SDavid Xu 	PROC_UNLOCK(p);
129186857b36SDavid Xu 	*timerid = id;
129286857b36SDavid Xu 	return (0);
129386857b36SDavid Xu 
129486857b36SDavid Xu out:
129586857b36SDavid Xu 	ITIMER_LOCK(it);
129686857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
129786857b36SDavid Xu 	ITIMER_UNLOCK(it);
129886857b36SDavid Xu 	uma_zfree(itimer_zone, it);
129986857b36SDavid Xu 	return (error);
130086857b36SDavid Xu }
130186857b36SDavid Xu 
130286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
130361d3a4efSDavid Xu struct ktimer_delete_args {
130461d3a4efSDavid Xu 	int timerid;
130586857b36SDavid Xu };
130686857b36SDavid Xu #endif
130786857b36SDavid Xu int
13088451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
130986857b36SDavid Xu {
1310643ee871SKonstantin Belousov 
1311643ee871SKonstantin Belousov 	return (kern_ktimer_delete(td, uap->timerid));
131286857b36SDavid Xu }
131386857b36SDavid Xu 
131486857b36SDavid Xu static struct itimer *
1315843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
131686857b36SDavid Xu {
131786857b36SDavid Xu 	struct itimer *it;
131886857b36SDavid Xu 
131986857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
13203f935cf3SColin Percival 	if ((p->p_itimers == NULL) ||
13213f935cf3SColin Percival 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
132260354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
132386857b36SDavid Xu 		return (NULL);
132486857b36SDavid Xu 	}
132586857b36SDavid Xu 	ITIMER_LOCK(it);
1326843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
132786857b36SDavid Xu 		ITIMER_UNLOCK(it);
132886857b36SDavid Xu 		it = NULL;
132986857b36SDavid Xu 	}
133086857b36SDavid Xu 	return (it);
133186857b36SDavid Xu }
133286857b36SDavid Xu 
1333643ee871SKonstantin Belousov int
1334643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid)
133586857b36SDavid Xu {
133686857b36SDavid Xu 	struct proc *p = td->td_proc;
133786857b36SDavid Xu 	struct itimer *it;
133886857b36SDavid Xu 
133986857b36SDavid Xu 	PROC_LOCK(p);
1340843b99c6SDavid Xu 	it = itimer_find(p, timerid);
134186857b36SDavid Xu 	if (it == NULL) {
134286857b36SDavid Xu 		PROC_UNLOCK(p);
134386857b36SDavid Xu 		return (EINVAL);
134486857b36SDavid Xu 	}
134586857b36SDavid Xu 	PROC_UNLOCK(p);
134686857b36SDavid Xu 
134786857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
134886857b36SDavid Xu 	while (it->it_usecount > 0) {
134986857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
135086857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
135186857b36SDavid Xu 	}
135286857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
135386857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
135486857b36SDavid Xu 	ITIMER_UNLOCK(it);
135586857b36SDavid Xu 
135686857b36SDavid Xu 	PROC_LOCK(p);
135786857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
135886857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
135960354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
136086857b36SDavid Xu 	PROC_UNLOCK(p);
136186857b36SDavid Xu 	uma_zfree(itimer_zone, it);
136286857b36SDavid Xu 	return (0);
136386857b36SDavid Xu }
136486857b36SDavid Xu 
136586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
136661d3a4efSDavid Xu struct ktimer_settime_args {
136761d3a4efSDavid Xu 	int timerid;
136886857b36SDavid Xu 	int flags;
136986857b36SDavid Xu 	const struct itimerspec * value;
137086857b36SDavid Xu 	struct itimerspec * ovalue;
137186857b36SDavid Xu };
137286857b36SDavid Xu #endif
137386857b36SDavid Xu int
13748451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
137586857b36SDavid Xu {
137686857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
137786857b36SDavid Xu 	int error;
137886857b36SDavid Xu 
137986857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
138086857b36SDavid Xu 	if (error != 0)
138186857b36SDavid Xu 		return (error);
1382643ee871SKonstantin Belousov 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1383643ee871SKonstantin Belousov 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1384643ee871SKonstantin Belousov 	if (error == 0 && uap->ovalue != NULL)
1385643ee871SKonstantin Belousov 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1386643ee871SKonstantin Belousov 	return (error);
1387643ee871SKonstantin Belousov }
138886857b36SDavid Xu 
1389643ee871SKonstantin Belousov int
1390643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1391643ee871SKonstantin Belousov     struct itimerspec *val, struct itimerspec *oval)
1392643ee871SKonstantin Belousov {
1393643ee871SKonstantin Belousov 	struct proc *p;
1394643ee871SKonstantin Belousov 	struct itimer *it;
1395643ee871SKonstantin Belousov 	int error;
139686857b36SDavid Xu 
1397643ee871SKonstantin Belousov 	p = td->td_proc;
139886857b36SDavid Xu 	PROC_LOCK(p);
1399643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
140086857b36SDavid Xu 		PROC_UNLOCK(p);
140186857b36SDavid Xu 		error = EINVAL;
140286857b36SDavid Xu 	} else {
140386857b36SDavid Xu 		PROC_UNLOCK(p);
140486857b36SDavid Xu 		itimer_enter(it);
1405643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1406643ee871SKonstantin Belousov 		    flags, val, oval));
140786857b36SDavid Xu 		itimer_leave(it);
140886857b36SDavid Xu 		ITIMER_UNLOCK(it);
140986857b36SDavid Xu 	}
141086857b36SDavid Xu 	return (error);
141186857b36SDavid Xu }
141286857b36SDavid Xu 
141386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
141461d3a4efSDavid Xu struct ktimer_gettime_args {
141561d3a4efSDavid Xu 	int timerid;
141686857b36SDavid Xu 	struct itimerspec * value;
141786857b36SDavid Xu };
141886857b36SDavid Xu #endif
141986857b36SDavid Xu int
14208451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
142186857b36SDavid Xu {
142286857b36SDavid Xu 	struct itimerspec val;
142386857b36SDavid Xu 	int error;
142486857b36SDavid Xu 
1425643ee871SKonstantin Belousov 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1426643ee871SKonstantin Belousov 	if (error == 0)
1427643ee871SKonstantin Belousov 		error = copyout(&val, uap->value, sizeof(val));
1428643ee871SKonstantin Belousov 	return (error);
1429643ee871SKonstantin Belousov }
1430643ee871SKonstantin Belousov 
1431643ee871SKonstantin Belousov int
1432643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1433643ee871SKonstantin Belousov {
1434643ee871SKonstantin Belousov 	struct proc *p;
1435643ee871SKonstantin Belousov 	struct itimer *it;
1436643ee871SKonstantin Belousov 	int error;
1437643ee871SKonstantin Belousov 
1438643ee871SKonstantin Belousov 	p = td->td_proc;
143986857b36SDavid Xu 	PROC_LOCK(p);
1440643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
144186857b36SDavid Xu 		PROC_UNLOCK(p);
144286857b36SDavid Xu 		error = EINVAL;
144386857b36SDavid Xu 	} else {
144486857b36SDavid Xu 		PROC_UNLOCK(p);
144586857b36SDavid Xu 		itimer_enter(it);
1446643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
144786857b36SDavid Xu 		itimer_leave(it);
144886857b36SDavid Xu 		ITIMER_UNLOCK(it);
144986857b36SDavid Xu 	}
145086857b36SDavid Xu 	return (error);
145186857b36SDavid Xu }
145286857b36SDavid Xu 
145386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
145486857b36SDavid Xu struct timer_getoverrun_args {
145561d3a4efSDavid Xu 	int timerid;
145686857b36SDavid Xu };
145786857b36SDavid Xu #endif
145886857b36SDavid Xu int
14598451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
146086857b36SDavid Xu {
1461e346f8c4SBjoern A. Zeeb 
1462e346f8c4SBjoern A. Zeeb 	return (kern_ktimer_getoverrun(td, uap->timerid));
1463e346f8c4SBjoern A. Zeeb }
1464e346f8c4SBjoern A. Zeeb 
1465e346f8c4SBjoern A. Zeeb int
1466e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id)
1467e346f8c4SBjoern A. Zeeb {
146886857b36SDavid Xu 	struct proc *p = td->td_proc;
146986857b36SDavid Xu 	struct itimer *it;
147086857b36SDavid Xu 	int error ;
147186857b36SDavid Xu 
147286857b36SDavid Xu 	PROC_LOCK(p);
1473e346f8c4SBjoern A. Zeeb 	if (timer_id < 3 ||
1474e346f8c4SBjoern A. Zeeb 	    (it = itimer_find(p, timer_id)) == NULL) {
147586857b36SDavid Xu 		PROC_UNLOCK(p);
147686857b36SDavid Xu 		error = EINVAL;
147786857b36SDavid Xu 	} else {
147886857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
147986857b36SDavid Xu 		ITIMER_UNLOCK(it);
148056c06c4bSDavid Xu 		PROC_UNLOCK(p);
148186857b36SDavid Xu 		error = 0;
148286857b36SDavid Xu 	}
148386857b36SDavid Xu 	return (error);
148486857b36SDavid Xu }
148586857b36SDavid Xu 
148686857b36SDavid Xu static int
148786857b36SDavid Xu realtimer_create(struct itimer *it)
148886857b36SDavid Xu {
148986857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
149086857b36SDavid Xu 	return (0);
149186857b36SDavid Xu }
149286857b36SDavid Xu 
149386857b36SDavid Xu static int
149486857b36SDavid Xu realtimer_delete(struct itimer *it)
149586857b36SDavid Xu {
149686857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1497843b99c6SDavid Xu 
1498d6b6592eSDavid Xu 	/*
1499d6b6592eSDavid Xu 	 * clear timer's value and interval to tell realtimer_expire
1500d6b6592eSDavid Xu 	 * to not rearm the timer.
1501d6b6592eSDavid Xu 	 */
1502d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_value);
1503d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_interval);
1504843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1505843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1506843b99c6SDavid Xu 	ITIMER_LOCK(it);
150786857b36SDavid Xu 	return (0);
150886857b36SDavid Xu }
150986857b36SDavid Xu 
151086857b36SDavid Xu static int
151186857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
151286857b36SDavid Xu {
151356c06c4bSDavid Xu 	struct timespec cts;
151486857b36SDavid Xu 
151586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
151686857b36SDavid Xu 
151756c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
151856c06c4bSDavid Xu 	*ovalue = it->it_time;
151986857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
152056c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
152186857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
152286857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
152386857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
152486857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
152586857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
152686857b36SDavid Xu 		}
152786857b36SDavid Xu 	}
152886857b36SDavid Xu 	return (0);
152986857b36SDavid Xu }
153086857b36SDavid Xu 
153186857b36SDavid Xu static int
153286857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
153386857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
153486857b36SDavid Xu {
153556c06c4bSDavid Xu 	struct timespec cts, ts;
153656c06c4bSDavid Xu 	struct timeval tv;
153756c06c4bSDavid Xu 	struct itimerspec val;
153886857b36SDavid Xu 
153986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
154086857b36SDavid Xu 
154156c06c4bSDavid Xu 	val = *value;
154256c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
154386857b36SDavid Xu 		return (EINVAL);
154486857b36SDavid Xu 
154556c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
154656c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
154786857b36SDavid Xu 			return (EINVAL);
154886857b36SDavid Xu 	} else {
154956c06c4bSDavid Xu 		timespecclear(&val.it_interval);
155086857b36SDavid Xu 	}
155186857b36SDavid Xu 
155286857b36SDavid Xu 	if (ovalue != NULL)
155386857b36SDavid Xu 		realtimer_gettime(it, ovalue);
155486857b36SDavid Xu 
155586857b36SDavid Xu 	it->it_time = val;
155656c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
155756c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
155856c06c4bSDavid Xu 		ts = val.it_value;
155986857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
156086857b36SDavid Xu 			/* Convert to absolute time. */
156156c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
156286857b36SDavid Xu 		} else {
156356c06c4bSDavid Xu 			timespecsub(&ts, &cts);
156486857b36SDavid Xu 			/*
156556c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
156686857b36SDavid Xu 			 * fix it.
156786857b36SDavid Xu 			 */
156886857b36SDavid Xu 		}
156956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
157056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
157186857b36SDavid Xu 			realtimer_expire, it);
157286857b36SDavid Xu 	} else {
157386857b36SDavid Xu 		callout_stop(&it->it_callout);
157486857b36SDavid Xu 	}
157586857b36SDavid Xu 
157686857b36SDavid Xu 	return (0);
157786857b36SDavid Xu }
157886857b36SDavid Xu 
157986857b36SDavid Xu static void
158056c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
158186857b36SDavid Xu {
158286857b36SDavid Xu 	if (id == CLOCK_REALTIME)
158356c06c4bSDavid Xu 		getnanotime(ts);
158486857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
158556c06c4bSDavid Xu 		getnanouptime(ts);
158656c06c4bSDavid Xu }
158756c06c4bSDavid Xu 
158856c06c4bSDavid Xu int
158961d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
159056c06c4bSDavid Xu {
159156c06c4bSDavid Xu 	struct itimer *it;
159256c06c4bSDavid Xu 
159356c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1594843b99c6SDavid Xu 	it = itimer_find(p, timerid);
159556c06c4bSDavid Xu 	if (it != NULL) {
159656c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
159756c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
159856c06c4bSDavid Xu 		it->it_overrun = 0;
159956c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
160056c06c4bSDavid Xu 		return (0);
160156c06c4bSDavid Xu 	}
160256c06c4bSDavid Xu 	return (EINVAL);
160356c06c4bSDavid Xu }
160456c06c4bSDavid Xu 
160556c06c4bSDavid Xu int
160656c06c4bSDavid Xu itimespecfix(struct timespec *ts)
160756c06c4bSDavid Xu {
160856c06c4bSDavid Xu 
160956c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
161056c06c4bSDavid Xu 		return (EINVAL);
161156c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
161256c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
161356c06c4bSDavid Xu 	return (0);
161486857b36SDavid Xu }
161586857b36SDavid Xu 
161686857b36SDavid Xu /* Timeout callback for realtime timer */
161786857b36SDavid Xu static void
161886857b36SDavid Xu realtimer_expire(void *arg)
161986857b36SDavid Xu {
162056c06c4bSDavid Xu 	struct timespec cts, ts;
162156c06c4bSDavid Xu 	struct timeval tv;
162286857b36SDavid Xu 	struct itimer *it;
162386857b36SDavid Xu 
162486857b36SDavid Xu 	it = (struct itimer *)arg;
162586857b36SDavid Xu 
162656c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
162786857b36SDavid Xu 	/* Only fire if time is reached. */
162856c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
162956c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
163056c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
163186857b36SDavid Xu 				    &it->it_time.it_interval);
163256c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
163377e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
163486857b36SDavid Xu 					it->it_overrun++;
163577e718f7SDavid Xu 				else
163677e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
163756c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
163886857b36SDavid Xu 					    &it->it_time.it_interval);
163986857b36SDavid Xu 			}
164086857b36SDavid Xu 		} else {
164186857b36SDavid Xu 			/* single shot timer ? */
164256c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
164386857b36SDavid Xu 		}
164456c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
164556c06c4bSDavid Xu 			ts = it->it_time.it_value;
164656c06c4bSDavid Xu 			timespecsub(&ts, &cts);
164756c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
164856c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
164986857b36SDavid Xu 				 realtimer_expire, it);
165086857b36SDavid Xu 		}
1651d6b6592eSDavid Xu 		itimer_enter(it);
165286857b36SDavid Xu 		ITIMER_UNLOCK(it);
165386857b36SDavid Xu 		itimer_fire(it);
165486857b36SDavid Xu 		ITIMER_LOCK(it);
1655d6b6592eSDavid Xu 		itimer_leave(it);
165656c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
165756c06c4bSDavid Xu 		ts = it->it_time.it_value;
165856c06c4bSDavid Xu 		timespecsub(&ts, &cts);
165956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
166056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
166186857b36SDavid Xu  			it);
166286857b36SDavid Xu 	}
166386857b36SDavid Xu }
166486857b36SDavid Xu 
166586857b36SDavid Xu void
166686857b36SDavid Xu itimer_fire(struct itimer *it)
166786857b36SDavid Xu {
166886857b36SDavid Xu 	struct proc *p = it->it_proc;
1669cf7d9a8cSDavid Xu 	struct thread *td;
167086857b36SDavid Xu 
167156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
167256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1673cf7d9a8cSDavid Xu 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
167456c06c4bSDavid Xu 			ITIMER_LOCK(it);
167556c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
167656c06c4bSDavid Xu 			timespecclear(&it->it_time.it_interval);
167756c06c4bSDavid Xu 			callout_stop(&it->it_callout);
167856c06c4bSDavid Xu 			ITIMER_UNLOCK(it);
1679cf7d9a8cSDavid Xu 			return;
16806d7b314bSDavid Xu 		}
1681cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
1682cf7d9a8cSDavid Xu 			it->it_ksi.ksi_errno = 0;
1683cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1684cf7d9a8cSDavid Xu 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
168556c06c4bSDavid Xu 		} else {
168677e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
16876d7b314bSDavid Xu 				it->it_overrun++;
168877e718f7SDavid Xu 			else
168977e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
169056c06c4bSDavid Xu 		}
169186857b36SDavid Xu 		PROC_UNLOCK(p);
169286857b36SDavid Xu 	}
169386857b36SDavid Xu }
169486857b36SDavid Xu 
169586857b36SDavid Xu static void
169686857b36SDavid Xu itimers_alloc(struct proc *p)
169786857b36SDavid Xu {
169860354683SDavid Xu 	struct itimers *its;
169960354683SDavid Xu 	int i;
170086857b36SDavid Xu 
170160354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
170286857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
170386857b36SDavid Xu 	LIST_INIT(&its->its_prof);
170486857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
170560354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
170660354683SDavid Xu 		its->its_timers[i] = NULL;
170760354683SDavid Xu 	PROC_LOCK(p);
170860354683SDavid Xu 	if (p->p_itimers == NULL) {
170960354683SDavid Xu 		p->p_itimers = its;
171060354683SDavid Xu 		PROC_UNLOCK(p);
171160354683SDavid Xu 	}
171260354683SDavid Xu 	else {
171360354683SDavid Xu 		PROC_UNLOCK(p);
171460354683SDavid Xu 		free(its, M_SUBPROC);
171560354683SDavid Xu 	}
171686857b36SDavid Xu }
171786857b36SDavid Xu 
1718993182e5SAlexander Leidinger static void
1719993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1720993182e5SAlexander Leidinger {
1721993182e5SAlexander Leidinger 	itimers_event_hook_exit(arg, p);
1722993182e5SAlexander Leidinger }
1723993182e5SAlexander Leidinger 
172486857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1725d26b1a1fSDavid Xu static void
1726993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
172786857b36SDavid Xu {
172886857b36SDavid Xu 	struct itimers *its;
172986857b36SDavid Xu 	struct itimer *it;
17303e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
173186857b36SDavid Xu 	int i;
173286857b36SDavid Xu 
173360354683SDavid Xu 	if (p->p_itimers != NULL) {
173460354683SDavid Xu 		its = p->p_itimers;
173586857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
173686857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
173786857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
173886857b36SDavid Xu 		}
173986857b36SDavid Xu 		/*
174086857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
174186857b36SDavid Xu 		 * by new image.
174286857b36SDavid Xu 		 */
174386857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
174486857b36SDavid Xu 			i = 3;
174586857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
174686857b36SDavid Xu 			i = 0;
174786857b36SDavid Xu 		else
174886857b36SDavid Xu 			panic("unhandled event");
174986857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
1750843b99c6SDavid Xu 			if ((it = its->its_timers[i]) != NULL)
1751643ee871SKonstantin Belousov 				kern_ktimer_delete(curthread, i);
175286857b36SDavid Xu 		}
175386857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
175486857b36SDavid Xu 		    its->its_timers[1] == NULL &&
175586857b36SDavid Xu 		    its->its_timers[2] == NULL) {
175660354683SDavid Xu 			free(its, M_SUBPROC);
175760354683SDavid Xu 			p->p_itimers = NULL;
175886857b36SDavid Xu 		}
175986857b36SDavid Xu 	}
176086857b36SDavid Xu }
1761