xref: /freebsd/sys/kern/kern_time.c (revision 3f8455b0905a05552b1155e09165787e1d098060)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1369a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35de56aee0SKonstantin Belousov #include "opt_ktrace.h"
36de56aee0SKonstantin Belousov 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39aff5bcb1SDavid Xu #include <sys/limits.h>
40f645b0b5SPoul-Henning Kamp #include <sys/clock.h>
41fb919e4dSMark Murray #include <sys/lock.h>
42fb919e4dSMark Murray #include <sys/mutex.h>
43d2d3e875SBruce Evans #include <sys/sysproto.h>
44d26b1a1fSDavid Xu #include <sys/eventhandler.h>
45df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
46797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
48098176f0SDavide Italiano #include <sys/sleepqueue.h>
49efa42cbcSPaul Saab #include <sys/syscallsubr.h>
5077e718f7SDavid Xu #include <sys/sysctl.h>
5194c8fcd8SPeter Wemm #include <sys/sysent.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 
7486857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
7586857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
7686857b36SDavid Xu 
77df8bae1dSRodney W. Grimes /*
78df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
79df8bae1dSRodney W. Grimes  *
80df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
81df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
82df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
83df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
84df8bae1dSRodney W. Grimes  * timers when they expire.
85df8bae1dSRodney W. Grimes  */
86df8bae1dSRodney W. Grimes 
877edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
884d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
89*3f8455b0SEric van Gyzen static int	user_clock_nanosleep(struct thread *td, clockid_t clock_id,
90*3f8455b0SEric van Gyzen 		    int flags, const struct timespec *ua_rqtp,
91*3f8455b0SEric van Gyzen 		    struct timespec *ua_rmtp);
9294c8fcd8SPeter Wemm 
9386857b36SDavid Xu static void	itimer_start(void);
9486857b36SDavid Xu static int	itimer_init(void *, int, int);
9586857b36SDavid Xu static void	itimer_fini(void *, int);
9686857b36SDavid Xu static void	itimer_enter(struct itimer *);
9786857b36SDavid Xu static void	itimer_leave(struct itimer *);
98843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int);
9986857b36SDavid Xu static void	itimers_alloc(struct proc *);
100993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
101993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
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 *);
10986857b36SDavid Xu 
11086857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
11186857b36SDavid Xu void		itimer_fire(struct itimer *it);
11256c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
11386857b36SDavid Xu 
11486857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
11586857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
11686857b36SDavid Xu 
11786857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
11886857b36SDavid Xu 
11986857b36SDavid Xu 
12094c8fcd8SPeter Wemm static int
12191afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
12294c8fcd8SPeter Wemm {
123fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
124c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1257ec73f64SPoul-Henning Kamp 	struct timespec ts;
12694c8fcd8SPeter Wemm 
1279c8fff87SBruce Evans 	microtime(&tv1);
12800af9731SPoul-Henning Kamp 	delta = *tv;
12900af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
13094c8fcd8SPeter Wemm 
13194c8fcd8SPeter Wemm 	/*
1329c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
133fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
134fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
135fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
136fcae3aa6SNick Sayer 	 * back to the past.
137c0bd94a7SNick Sayer 	 *
138c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
139c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
140c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14194c8fcd8SPeter Wemm 	 */
1427edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
143fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1443f92429aSMatt Jacob 			/*
145c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1463f92429aSMatt Jacob 			 */
147fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
148fcae3aa6SNick Sayer 				maxtime = tv1;
149fcae3aa6SNick Sayer 			tv2 = *tv;
150fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
151fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1523f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
153fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
154fcae3aa6SNick Sayer 			}
1553f92429aSMatt Jacob 		} else {
1561822421cSKonstantin Belousov 			if (tv1.tv_sec == laststep.tv_sec)
157c0bd94a7SNick Sayer 				return (EPERM);
158c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
159c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
160c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
161c0bd94a7SNick Sayer 			}
162c0bd94a7SNick Sayer 			laststep = *tv;
163fcae3aa6SNick Sayer 		}
1649c8fff87SBruce Evans 	}
1659c8fff87SBruce Evans 
1667ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1677ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
16891266b96SPoul-Henning Kamp 	tc_setclock(&ts);
16994c8fcd8SPeter Wemm 	resettodr();
17094c8fcd8SPeter Wemm 	return (0);
17194c8fcd8SPeter Wemm }
17294c8fcd8SPeter Wemm 
17394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
174d65f1abcSDavid Xu struct clock_getcpuclockid2_args {
175d65f1abcSDavid Xu 	id_t id;
176d65f1abcSDavid Xu 	int which,
177d65f1abcSDavid Xu 	clockid_t *clock_id;
178d65f1abcSDavid Xu };
179d65f1abcSDavid Xu #endif
180d65f1abcSDavid Xu /* ARGSUSED */
181d65f1abcSDavid Xu int
182d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
183d65f1abcSDavid Xu {
184d65f1abcSDavid Xu 	clockid_t clk_id;
185d31e4b3aSKonstantin Belousov 	int error;
186d31e4b3aSKonstantin Belousov 
187d31e4b3aSKonstantin Belousov 	error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
188d31e4b3aSKonstantin Belousov 	if (error == 0)
189d31e4b3aSKonstantin Belousov 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
190d31e4b3aSKonstantin Belousov 	return (error);
191d31e4b3aSKonstantin Belousov }
192d31e4b3aSKonstantin Belousov 
193d31e4b3aSKonstantin Belousov int
194d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
195d31e4b3aSKonstantin Belousov     clockid_t *clk_id)
196d31e4b3aSKonstantin Belousov {
197d65f1abcSDavid Xu 	struct proc *p;
198d65f1abcSDavid Xu 	pid_t pid;
199d65f1abcSDavid Xu 	lwpid_t tid;
200d65f1abcSDavid Xu 	int error;
201d65f1abcSDavid Xu 
202d31e4b3aSKonstantin Belousov 	switch (which) {
203d65f1abcSDavid Xu 	case CPUCLOCK_WHICH_PID:
204d31e4b3aSKonstantin Belousov 		if (id != 0) {
20547ce3e53SDmitry Chagin 			error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
206d31e4b3aSKonstantin Belousov 			if (error != 0)
207d65f1abcSDavid Xu 				return (error);
20847ce3e53SDmitry Chagin 			PROC_UNLOCK(p);
209d31e4b3aSKonstantin Belousov 			pid = id;
210d65f1abcSDavid Xu 		} else {
211d65f1abcSDavid Xu 			pid = td->td_proc->p_pid;
212d65f1abcSDavid Xu 		}
213d31e4b3aSKonstantin Belousov 		*clk_id = MAKE_PROCESS_CPUCLOCK(pid);
214d31e4b3aSKonstantin Belousov 		return (0);
215d65f1abcSDavid Xu 	case CPUCLOCK_WHICH_TID:
216d31e4b3aSKonstantin Belousov 		tid = id == 0 ? td->td_tid : id;
217d31e4b3aSKonstantin Belousov 		*clk_id = MAKE_THREAD_CPUCLOCK(tid);
218d31e4b3aSKonstantin Belousov 		return (0);
219d65f1abcSDavid Xu 	default:
220d65f1abcSDavid Xu 		return (EINVAL);
221d65f1abcSDavid Xu 	}
222d65f1abcSDavid Xu }
223d65f1abcSDavid Xu 
224d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_
22594c8fcd8SPeter Wemm struct clock_gettime_args {
22694c8fcd8SPeter Wemm 	clockid_t clock_id;
22794c8fcd8SPeter Wemm 	struct	timespec *tp;
22894c8fcd8SPeter Wemm };
22994c8fcd8SPeter Wemm #endif
23094c8fcd8SPeter Wemm /* ARGSUSED */
23194c8fcd8SPeter Wemm int
2328451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
23394c8fcd8SPeter Wemm {
23494c8fcd8SPeter Wemm 	struct timespec ats;
235f0b479cdSPaul Saab 	int error;
236f0b479cdSPaul Saab 
237f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
238f0b479cdSPaul Saab 	if (error == 0)
239f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
240f0b479cdSPaul Saab 
241f0b479cdSPaul Saab 	return (error);
242f0b479cdSPaul Saab }
243f0b479cdSPaul Saab 
244d65f1abcSDavid Xu static inline void
245d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats)
246d65f1abcSDavid Xu {
247d65f1abcSDavid Xu 	runtime = cputick2usec(runtime);
248d65f1abcSDavid Xu 	ats->tv_sec = runtime / 1000000;
249d65f1abcSDavid Xu 	ats->tv_nsec = runtime % 1000000 * 1000;
250d65f1abcSDavid Xu }
251d65f1abcSDavid Xu 
252d65f1abcSDavid Xu static void
253d65f1abcSDavid Xu get_thread_cputime(struct thread *targettd, struct timespec *ats)
254d65f1abcSDavid Xu {
255d65f1abcSDavid Xu 	uint64_t runtime, curtime, switchtime;
256d65f1abcSDavid Xu 
257d65f1abcSDavid Xu 	if (targettd == NULL) { /* current thread */
258d65f1abcSDavid Xu 		critical_enter();
259d65f1abcSDavid Xu 		switchtime = PCPU_GET(switchtime);
260d65f1abcSDavid Xu 		curtime = cpu_ticks();
261d65f1abcSDavid Xu 		runtime = curthread->td_runtime;
262d65f1abcSDavid Xu 		critical_exit();
263d65f1abcSDavid Xu 		runtime += curtime - switchtime;
264d65f1abcSDavid Xu 	} else {
265d65f1abcSDavid Xu 		thread_lock(targettd);
266d65f1abcSDavid Xu 		runtime = targettd->td_runtime;
267d65f1abcSDavid Xu 		thread_unlock(targettd);
268d65f1abcSDavid Xu 	}
269d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
270d65f1abcSDavid Xu }
271d65f1abcSDavid Xu 
272d65f1abcSDavid Xu static void
273d65f1abcSDavid Xu get_process_cputime(struct proc *targetp, struct timespec *ats)
274d65f1abcSDavid Xu {
275d65f1abcSDavid Xu 	uint64_t runtime;
276d65f1abcSDavid Xu 	struct rusage ru;
277d65f1abcSDavid Xu 
2785c7bebf9SKonstantin Belousov 	PROC_STATLOCK(targetp);
279d65f1abcSDavid Xu 	rufetch(targetp, &ru);
280d65f1abcSDavid Xu 	runtime = targetp->p_rux.rux_runtime;
2815c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(targetp);
282d65f1abcSDavid Xu 	cputick2timespec(runtime, ats);
283d65f1abcSDavid Xu }
284d65f1abcSDavid Xu 
285d65f1abcSDavid Xu static int
286d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
287d65f1abcSDavid Xu {
288d65f1abcSDavid Xu 	struct proc *p, *p2;
289d65f1abcSDavid Xu 	struct thread *td2;
290d65f1abcSDavid Xu 	lwpid_t tid;
291d65f1abcSDavid Xu 	pid_t pid;
292d65f1abcSDavid Xu 	int error;
293d65f1abcSDavid Xu 
294d65f1abcSDavid Xu 	p = td->td_proc;
295d65f1abcSDavid Xu 	if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
296d65f1abcSDavid Xu 		tid = clock_id & CPUCLOCK_ID_MASK;
297d65f1abcSDavid Xu 		td2 = tdfind(tid, p->p_pid);
298d65f1abcSDavid Xu 		if (td2 == NULL)
299d65f1abcSDavid Xu 			return (EINVAL);
300d65f1abcSDavid Xu 		get_thread_cputime(td2, ats);
301d65f1abcSDavid Xu 		PROC_UNLOCK(td2->td_proc);
302d65f1abcSDavid Xu 	} else {
303d65f1abcSDavid Xu 		pid = clock_id & CPUCLOCK_ID_MASK;
304c7c536c7SKonstantin Belousov 		error = pget(pid, PGET_CANSEE, &p2);
305c7c536c7SKonstantin Belousov 		if (error != 0)
306d65f1abcSDavid Xu 			return (EINVAL);
307d65f1abcSDavid Xu 		get_process_cputime(p2, ats);
308d65f1abcSDavid Xu 		PROC_UNLOCK(p2);
309d65f1abcSDavid Xu 	}
310d65f1abcSDavid Xu 	return (0);
311d65f1abcSDavid Xu }
312d65f1abcSDavid Xu 
313f0b479cdSPaul Saab int
314f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
315f0b479cdSPaul Saab {
316de0a9241SKelly Yancey 	struct timeval sys, user;
31778c85e8dSJohn Baldwin 	struct proc *p;
31894c8fcd8SPeter Wemm 
31978c85e8dSJohn Baldwin 	p = td->td_proc;
320f0b479cdSPaul Saab 	switch (clock_id) {
3215e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
3225e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
323f0b479cdSPaul Saab 		nanotime(ats);
324b8817154SKelly Yancey 		break;
3255e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3265e758b95SRobert Watson 		getnanotime(ats);
3275e758b95SRobert Watson 		break;
328b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
32978c85e8dSJohn Baldwin 		PROC_LOCK(p);
3305c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
33178c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3325c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
33378c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
334f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
335b8817154SKelly Yancey 		break;
336b8817154SKelly Yancey 	case CLOCK_PROF:
33778c85e8dSJohn Baldwin 		PROC_LOCK(p);
3385c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
33978c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
3405c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
34178c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
342de0a9241SKelly Yancey 		timevaladd(&user, &sys);
343f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
344de0a9241SKelly Yancey 		break;
3455e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
3465e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3475eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3485e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
349f0b479cdSPaul Saab 		nanouptime(ats);
350b8817154SKelly Yancey 		break;
3515e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3525e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3535e758b95SRobert Watson 		getnanouptime(ats);
3545e758b95SRobert Watson 		break;
3555e758b95SRobert Watson 	case CLOCK_SECOND:
3565e758b95SRobert Watson 		ats->tv_sec = time_second;
3575e758b95SRobert Watson 		ats->tv_nsec = 0;
3585e758b95SRobert Watson 		break;
35900d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
360d65f1abcSDavid Xu 		get_thread_cputime(NULL, ats);
361d65f1abcSDavid Xu 		break;
362d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
363d65f1abcSDavid Xu 		PROC_LOCK(p);
364d65f1abcSDavid Xu 		get_process_cputime(p, ats);
365d65f1abcSDavid Xu 		PROC_UNLOCK(p);
36600d6ac63SDavid Xu 		break;
367b8817154SKelly Yancey 	default:
368d65f1abcSDavid Xu 		if ((int)clock_id >= 0)
3695cb3dc8fSPoul-Henning Kamp 			return (EINVAL);
370d65f1abcSDavid Xu 		return (get_cputime(td, clock_id, ats));
371b8817154SKelly Yancey 	}
372f0b479cdSPaul Saab 	return (0);
37394c8fcd8SPeter Wemm }
37494c8fcd8SPeter Wemm 
37594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
37694c8fcd8SPeter Wemm struct clock_settime_args {
37794c8fcd8SPeter Wemm 	clockid_t clock_id;
37894c8fcd8SPeter Wemm 	const struct	timespec *tp;
37994c8fcd8SPeter Wemm };
38094c8fcd8SPeter Wemm #endif
38194c8fcd8SPeter Wemm /* ARGSUSED */
38294c8fcd8SPeter Wemm int
3838451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
38494c8fcd8SPeter Wemm {
38594c8fcd8SPeter Wemm 	struct timespec ats;
38694c8fcd8SPeter Wemm 	int error;
38794c8fcd8SPeter Wemm 
388f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
389f0b479cdSPaul Saab 		return (error);
390f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
391f0b479cdSPaul Saab }
392f0b479cdSPaul Saab 
39390a79ac5SConrad Meyer static int allow_insane_settime = 0;
39490a79ac5SConrad Meyer SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN,
39590a79ac5SConrad Meyer     &allow_insane_settime, 0,
39690a79ac5SConrad Meyer     "do not perform possibly restrictive checks on settime(2) args");
39790a79ac5SConrad Meyer 
398f0b479cdSPaul Saab int
399f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
400f0b479cdSPaul Saab {
401f0b479cdSPaul Saab 	struct timeval atv;
402f0b479cdSPaul Saab 	int error;
403f0b479cdSPaul Saab 
404acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
4057edfb592SJohn Baldwin 		return (error);
406f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
4077edfb592SJohn Baldwin 		return (EINVAL);
4083e18d701SDmitry Chagin 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000 ||
4093e18d701SDmitry Chagin 	    ats->tv_sec < 0)
4107edfb592SJohn Baldwin 		return (EINVAL);
41190a79ac5SConrad Meyer 	if (!allow_insane_settime && ats->tv_sec > 9999ULL * 366 * 24 * 60 * 60)
41290a79ac5SConrad Meyer 		return (EINVAL);
413a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
414f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
4157edfb592SJohn Baldwin 	error = settime(td, &atv);
41694c8fcd8SPeter Wemm 	return (error);
41794c8fcd8SPeter Wemm }
41894c8fcd8SPeter Wemm 
41994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
42094c8fcd8SPeter Wemm struct clock_getres_args {
42194c8fcd8SPeter Wemm 	clockid_t clock_id;
42294c8fcd8SPeter Wemm 	struct	timespec *tp;
42394c8fcd8SPeter Wemm };
42494c8fcd8SPeter Wemm #endif
42594c8fcd8SPeter Wemm int
4268451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
42794c8fcd8SPeter Wemm {
42894c8fcd8SPeter Wemm 	struct timespec ts;
429f0b479cdSPaul Saab 	int error;
43094c8fcd8SPeter Wemm 
431f0b479cdSPaul Saab 	if (uap->tp == NULL)
432f0b479cdSPaul Saab 		return (0);
433f0b479cdSPaul Saab 
434f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
435f0b479cdSPaul Saab 	if (error == 0)
436f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
437f0b479cdSPaul Saab 	return (error);
438f0b479cdSPaul Saab }
439f0b479cdSPaul Saab 
440f0b479cdSPaul Saab int
441f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
442f0b479cdSPaul Saab {
443f0b479cdSPaul Saab 
444f0b479cdSPaul Saab 	ts->tv_sec = 0;
445f0b479cdSPaul Saab 	switch (clock_id) {
446b8817154SKelly Yancey 	case CLOCK_REALTIME:
4475e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
4485e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
449b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
4505e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
4515e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
4525eefd889SAndre Oppermann 	case CLOCK_UPTIME:
4535e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
4545e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
455ac0653dcSBruce Evans 		/*
456ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
457ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
458ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
459ac0653dcSBruce Evans 		 */
460f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
461b8817154SKelly Yancey 		break;
462b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
463b8817154SKelly Yancey 	case CLOCK_PROF:
464b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
46555e0987aSPedro F. Giffuni 		ts->tv_nsec = howmany(1000000000, hz);
466b8817154SKelly Yancey 		break;
4675e758b95SRobert Watson 	case CLOCK_SECOND:
4685e758b95SRobert Watson 		ts->tv_sec = 1;
4695e758b95SRobert Watson 		ts->tv_nsec = 0;
4705e758b95SRobert Watson 		break;
47100d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
472d65f1abcSDavid Xu 	case CLOCK_PROCESS_CPUTIME_ID:
473d65f1abcSDavid Xu 	cputime:
47400d6ac63SDavid Xu 		/* sync with cputick2usec */
47500d6ac63SDavid Xu 		ts->tv_nsec = 1000000 / cpu_tickrate();
47600d6ac63SDavid Xu 		if (ts->tv_nsec == 0)
47700d6ac63SDavid Xu 			ts->tv_nsec = 1000;
47800d6ac63SDavid Xu 		break;
479b8817154SKelly Yancey 	default:
480d65f1abcSDavid Xu 		if ((int)clock_id < 0)
481d65f1abcSDavid Xu 			goto cputime;
482b8817154SKelly Yancey 		return (EINVAL);
48394c8fcd8SPeter Wemm 	}
484de0a9241SKelly Yancey 	return (0);
48594c8fcd8SPeter Wemm }
48694c8fcd8SPeter Wemm 
4877fdf2c85SPaul Saab int
4887fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
4895b870b7bSPeter Wemm {
490*3f8455b0SEric van Gyzen 
491*3f8455b0SEric van Gyzen 	return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt,
492*3f8455b0SEric van Gyzen 	    rmt));
493*3f8455b0SEric van Gyzen }
494*3f8455b0SEric van Gyzen 
495*3f8455b0SEric van Gyzen static uint8_t nanowait[MAXCPU];
496*3f8455b0SEric van Gyzen 
497*3f8455b0SEric van Gyzen int
498*3f8455b0SEric van Gyzen kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
499*3f8455b0SEric van Gyzen     const struct timespec *rqt, struct timespec *rmt)
500*3f8455b0SEric van Gyzen {
501*3f8455b0SEric van Gyzen 	struct timespec ts, now;
502098176f0SDavide Italiano 	sbintime_t sbt, sbtt, prec, tmp;
503980c545dSAlexander Motin 	time_t over;
50433841826SPoul-Henning Kamp 	int error;
505*3f8455b0SEric van Gyzen 	bool is_abs_real;
5065b870b7bSPeter Wemm 
5077d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
508708e7684SPeter Wemm 		return (EINVAL);
509*3f8455b0SEric van Gyzen 	if ((flags & ~TIMER_ABSTIME) != 0)
510*3f8455b0SEric van Gyzen 		return (EINVAL);
511*3f8455b0SEric van Gyzen 	switch (clock_id) {
512*3f8455b0SEric van Gyzen 	case CLOCK_REALTIME:
513*3f8455b0SEric van Gyzen 	case CLOCK_REALTIME_PRECISE:
514*3f8455b0SEric van Gyzen 	case CLOCK_REALTIME_FAST:
515*3f8455b0SEric van Gyzen 	case CLOCK_SECOND:
516*3f8455b0SEric van Gyzen 		is_abs_real = (flags & TIMER_ABSTIME) != 0;
517*3f8455b0SEric van Gyzen 		break;
518*3f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC:
519*3f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC_PRECISE:
520*3f8455b0SEric van Gyzen 	case CLOCK_MONOTONIC_FAST:
521*3f8455b0SEric van Gyzen 	case CLOCK_UPTIME:
522*3f8455b0SEric van Gyzen 	case CLOCK_UPTIME_PRECISE:
523*3f8455b0SEric van Gyzen 	case CLOCK_UPTIME_FAST:
524*3f8455b0SEric van Gyzen 		is_abs_real = false;
525*3f8455b0SEric van Gyzen 		break;
526*3f8455b0SEric van Gyzen 	case CLOCK_VIRTUAL:
527*3f8455b0SEric van Gyzen 	case CLOCK_PROF:
528*3f8455b0SEric van Gyzen 	case CLOCK_PROCESS_CPUTIME_ID:
529*3f8455b0SEric van Gyzen 		return (ENOTSUP);
530*3f8455b0SEric van Gyzen 	case CLOCK_THREAD_CPUTIME_ID:
531*3f8455b0SEric van Gyzen 	default:
532*3f8455b0SEric van Gyzen 		return (EINVAL);
533*3f8455b0SEric van Gyzen 	}
534*3f8455b0SEric van Gyzen 	do {
535980c545dSAlexander Motin 		ts = *rqt;
536*3f8455b0SEric van Gyzen 		if ((flags & TIMER_ABSTIME) != 0) {
537*3f8455b0SEric van Gyzen 			if (is_abs_real)
538*3f8455b0SEric van Gyzen 				td->td_rtcgen =
539*3f8455b0SEric van Gyzen 				    atomic_load_acq_int(&rtc_generation);
540*3f8455b0SEric van Gyzen 			error = kern_clock_gettime(td, clock_id, &now);
541*3f8455b0SEric van Gyzen 			KASSERT(error == 0, ("kern_clock_gettime: %d", error));
542*3f8455b0SEric van Gyzen 			timespecsub(&ts, &now);
543*3f8455b0SEric van Gyzen 		}
544*3f8455b0SEric van Gyzen 		if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
545*3f8455b0SEric van Gyzen 			error = EWOULDBLOCK;
546*3f8455b0SEric van Gyzen 			break;
547*3f8455b0SEric van Gyzen 		}
548980c545dSAlexander Motin 		if (ts.tv_sec > INT32_MAX / 2) {
549980c545dSAlexander Motin 			over = ts.tv_sec - INT32_MAX / 2;
550980c545dSAlexander Motin 			ts.tv_sec -= over;
551980c545dSAlexander Motin 		} else
552980c545dSAlexander Motin 			over = 0;
553980c545dSAlexander Motin 		tmp = tstosbt(ts);
554098176f0SDavide Italiano 		prec = tmp;
555098176f0SDavide Italiano 		prec >>= tc_precexp;
556098176f0SDavide Italiano 		if (TIMESEL(&sbt, tmp))
557098176f0SDavide Italiano 			sbt += tc_tick_sbt;
558098176f0SDavide Italiano 		sbt += tmp;
5590dbf17e6SAlexander Motin 		error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
5600dbf17e6SAlexander Motin 		    sbt, prec, C_ABSOLUTE);
561*3f8455b0SEric van Gyzen 	} while (error == 0 && is_abs_real && td->td_rtcgen == 0);
562*3f8455b0SEric van Gyzen 	td->td_rtcgen = 0;
56333841826SPoul-Henning Kamp 	if (error != EWOULDBLOCK) {
564*3f8455b0SEric van Gyzen 		TIMESEL(&sbtt, tmp);
565*3f8455b0SEric van Gyzen 		if (sbtt >= sbt)
566*3f8455b0SEric van Gyzen 			return (0);
56733841826SPoul-Henning Kamp 		if (error == ERESTART)
56894c8fcd8SPeter Wemm 			error = EINTR;
569*3f8455b0SEric van Gyzen 		if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) {
570098176f0SDavide Italiano 			ts = sbttots(sbt - sbtt);
571980c545dSAlexander Motin 			ts.tv_sec += over;
57233841826SPoul-Henning Kamp 			if (ts.tv_sec < 0)
57333841826SPoul-Henning Kamp 				timespecclear(&ts);
57400af9731SPoul-Henning Kamp 			*rmt = ts;
57500af9731SPoul-Henning Kamp 		}
57633841826SPoul-Henning Kamp 		return (error);
57733841826SPoul-Henning Kamp 	}
57833841826SPoul-Henning Kamp 	return (0);
5795b870b7bSPeter Wemm }
58094c8fcd8SPeter Wemm 
5815b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
5825b870b7bSPeter Wemm struct nanosleep_args {
5835b870b7bSPeter Wemm 	struct	timespec *rqtp;
5845b870b7bSPeter Wemm 	struct	timespec *rmtp;
5855b870b7bSPeter Wemm };
5865b870b7bSPeter Wemm #endif
5875b870b7bSPeter Wemm /* ARGSUSED */
5885b870b7bSPeter Wemm int
5898451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
5905b870b7bSPeter Wemm {
591*3f8455b0SEric van Gyzen 
592*3f8455b0SEric van Gyzen 	return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME,
593*3f8455b0SEric van Gyzen 	    uap->rqtp, uap->rmtp));
594*3f8455b0SEric van Gyzen }
595*3f8455b0SEric van Gyzen 
596*3f8455b0SEric van Gyzen #ifndef _SYS_SYSPROTO_H_
597*3f8455b0SEric van Gyzen struct clock_nanosleep_args {
598*3f8455b0SEric van Gyzen 	clockid_t clock_id;
599*3f8455b0SEric van Gyzen 	int 	  flags;
600*3f8455b0SEric van Gyzen 	struct	timespec *rqtp;
601*3f8455b0SEric van Gyzen 	struct	timespec *rmtp;
602*3f8455b0SEric van Gyzen };
603*3f8455b0SEric van Gyzen #endif
604*3f8455b0SEric van Gyzen /* ARGSUSED */
605*3f8455b0SEric van Gyzen int
606*3f8455b0SEric van Gyzen sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap)
607*3f8455b0SEric van Gyzen {
608*3f8455b0SEric van Gyzen 	int error;
609*3f8455b0SEric van Gyzen 
610*3f8455b0SEric van Gyzen 	error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp,
611*3f8455b0SEric van Gyzen 	    uap->rmtp);
612*3f8455b0SEric van Gyzen 	return (kern_posix_error(td, error));
613*3f8455b0SEric van Gyzen }
614*3f8455b0SEric van Gyzen 
615*3f8455b0SEric van Gyzen static int
616*3f8455b0SEric van Gyzen user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
617*3f8455b0SEric van Gyzen     const struct timespec *ua_rqtp, struct timespec *ua_rmtp)
618*3f8455b0SEric van Gyzen {
6195b870b7bSPeter Wemm 	struct timespec rmt, rqt;
620fb99ab88SMatthew Dillon 	int error;
6215b870b7bSPeter Wemm 
622*3f8455b0SEric van Gyzen 	error = copyin(ua_rqtp, &rqt, sizeof(rqt));
6235b870b7bSPeter Wemm 	if (error)
6245b870b7bSPeter Wemm 		return (error);
625*3f8455b0SEric van Gyzen 	if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
626*3f8455b0SEric van Gyzen 	    !useracc(ua_rmtp, sizeof(rmt), VM_PROT_WRITE))
62731f3e2adSAlfred Perlstein 		return (EFAULT);
628*3f8455b0SEric van Gyzen 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
629*3f8455b0SEric van Gyzen 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
630fb99ab88SMatthew Dillon 		int error2;
631fb99ab88SMatthew Dillon 
632*3f8455b0SEric van Gyzen 		error2 = copyout(&rmt, ua_rmtp, sizeof(rmt));
63331f3e2adSAlfred Perlstein 		if (error2)
634fb99ab88SMatthew Dillon 			error = error2;
635708e7684SPeter Wemm 	}
636708e7684SPeter Wemm 	return (error);
63794c8fcd8SPeter Wemm }
63894c8fcd8SPeter Wemm 
6395b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
640df8bae1dSRodney W. Grimes struct gettimeofday_args {
641df8bae1dSRodney W. Grimes 	struct	timeval *tp;
642df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
643df8bae1dSRodney W. Grimes };
644d2d3e875SBruce Evans #endif
645df8bae1dSRodney W. Grimes /* ARGSUSED */
64626f9a767SRodney W. Grimes int
6478451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
648df8bae1dSRodney W. Grimes {
649df8bae1dSRodney W. Grimes 	struct timeval atv;
650411c25edSTim J. Robbins 	struct timezone rtz;
651df8bae1dSRodney W. Grimes 	int error = 0;
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 	if (uap->tp) {
654df8bae1dSRodney W. Grimes 		microtime(&atv);
65501609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
656df8bae1dSRodney W. Grimes 	}
65721dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
65891f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
65991f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
660411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
66121dcdb38SPoul-Henning Kamp 	}
662df8bae1dSRodney W. Grimes 	return (error);
663df8bae1dSRodney W. Grimes }
664df8bae1dSRodney W. Grimes 
665d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
666df8bae1dSRodney W. Grimes struct settimeofday_args {
667df8bae1dSRodney W. Grimes 	struct	timeval *tv;
668df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
669df8bae1dSRodney W. Grimes };
670d2d3e875SBruce Evans #endif
671df8bae1dSRodney W. Grimes /* ARGSUSED */
67226f9a767SRodney W. Grimes int
6738451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
674df8bae1dSRodney W. Grimes {
675b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
676b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
677b88ec951SJohn Baldwin 	int error;
678b88ec951SJohn Baldwin 
679b88ec951SJohn Baldwin 	if (uap->tv) {
680b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
681b88ec951SJohn Baldwin 		if (error)
682b88ec951SJohn Baldwin 			return (error);
683b88ec951SJohn Baldwin 		tvp = &atv;
684b88ec951SJohn Baldwin 	} else
685b88ec951SJohn Baldwin 		tvp = NULL;
686b88ec951SJohn Baldwin 	if (uap->tzp) {
687b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
688b88ec951SJohn Baldwin 		if (error)
689b88ec951SJohn Baldwin 			return (error);
690b88ec951SJohn Baldwin 		tzp = &atz;
691b88ec951SJohn Baldwin 	} else
692b88ec951SJohn Baldwin 		tzp = NULL;
693b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
694b88ec951SJohn Baldwin }
695b88ec951SJohn Baldwin 
696b88ec951SJohn Baldwin int
697b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
698b88ec951SJohn Baldwin {
699b88ec951SJohn Baldwin 	int error;
700fb99ab88SMatthew Dillon 
701acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
702b88ec951SJohn Baldwin 	if (error)
7037edfb592SJohn Baldwin 		return (error);
704df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
705b88ec951SJohn Baldwin 	if (tv) {
7063e18d701SDmitry Chagin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
7073e18d701SDmitry Chagin 		    tv->tv_sec < 0)
7087edfb592SJohn Baldwin 			return (EINVAL);
709b88ec951SJohn Baldwin 		error = settime(td, tv);
710708e7684SPeter Wemm 	}
711b88ec951SJohn Baldwin 	if (tzp && error == 0) {
712b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
713b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
714b88ec951SJohn Baldwin 	}
7157edfb592SJohn Baldwin 	return (error);
716b88ec951SJohn Baldwin }
7177edfb592SJohn Baldwin 
718df8bae1dSRodney W. Grimes /*
719873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
720873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
721873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
722873fbcd7SRobert Watson  * time until they expire.
723df8bae1dSRodney W. Grimes  *
724873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
725873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
726873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
727873fbcd7SRobert Watson  * drifting.
728df8bae1dSRodney W. Grimes  *
729df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
730873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
731873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
732873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
733873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
734873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
735873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
736873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
737873fbcd7SRobert Watson  * should go off.
738df8bae1dSRodney W. Grimes  */
739d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
740df8bae1dSRodney W. Grimes struct getitimer_args {
741df8bae1dSRodney W. Grimes 	u_int	which;
742df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
743df8bae1dSRodney W. Grimes };
744d2d3e875SBruce Evans #endif
74526f9a767SRodney W. Grimes int
7468451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap)
747df8bae1dSRodney W. Grimes {
748df8bae1dSRodney W. Grimes 	struct itimerval aitv;
749c90110d6SJohn Baldwin 	int error;
750df8bae1dSRodney W. Grimes 
751cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
752cfa0efe7SMaxim Sobolev 	if (error != 0)
753cfa0efe7SMaxim Sobolev 		return (error);
754cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
755cfa0efe7SMaxim Sobolev }
756cfa0efe7SMaxim Sobolev 
757cfa0efe7SMaxim Sobolev int
758cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
759cfa0efe7SMaxim Sobolev {
760cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
761cfa0efe7SMaxim Sobolev 	struct timeval ctv;
762cfa0efe7SMaxim Sobolev 
763cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
764df8bae1dSRodney W. Grimes 		return (EINVAL);
765fb99ab88SMatthew Dillon 
766cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
767df8bae1dSRodney W. Grimes 		/*
768ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
769df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
770df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
771df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
772df8bae1dSRodney W. Grimes 		 */
77396d7f8efSTim J. Robbins 		PROC_LOCK(p);
774cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
77596d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
776cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
777b5ea3779SAlexander Motin 			microuptime(&ctv);
778cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
779cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
780df8bae1dSRodney W. Grimes 			else
781cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
782227ee8a1SPoul-Henning Kamp 		}
783fb99ab88SMatthew Dillon 	} else {
7845c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
785cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
7865c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
787fb99ab88SMatthew Dillon 	}
788de56aee0SKonstantin Belousov #ifdef KTRACE
789de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
790de56aee0SKonstantin Belousov 		ktritimerval(aitv);
791de56aee0SKonstantin Belousov #endif
792cfa0efe7SMaxim Sobolev 	return (0);
793df8bae1dSRodney W. Grimes }
794df8bae1dSRodney W. Grimes 
795d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
796df8bae1dSRodney W. Grimes struct setitimer_args {
797df8bae1dSRodney W. Grimes 	u_int	which;
798df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
799df8bae1dSRodney W. Grimes };
800d2d3e875SBruce Evans #endif
80126f9a767SRodney W. Grimes int
8028451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap)
803df8bae1dSRodney W. Grimes {
804cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
805c90110d6SJohn Baldwin 	int error;
80696d7f8efSTim J. Robbins 
80796d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
80896d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
8098451d0ddSKip Macy 		return (sys_getitimer(td, (struct getitimer_args *)uap));
81096d7f8efSTim J. Robbins 	}
811df8bae1dSRodney W. Grimes 
81296d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
813df8bae1dSRodney W. Grimes 		return (error);
814cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
815cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
816cfa0efe7SMaxim Sobolev 		return (error);
817cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
818cfa0efe7SMaxim Sobolev }
819cfa0efe7SMaxim Sobolev 
820cfa0efe7SMaxim Sobolev int
821c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
822c90110d6SJohn Baldwin     struct itimerval *oitv)
823cfa0efe7SMaxim Sobolev {
824cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
825cfa0efe7SMaxim Sobolev 	struct timeval ctv;
826b5ea3779SAlexander Motin 	sbintime_t sbt, pr;
827cfa0efe7SMaxim Sobolev 
8285e85ac17SJohn Baldwin 	if (aitv == NULL)
8295e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
8305e85ac17SJohn Baldwin 
831cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
83296d7f8efSTim J. Robbins 		return (EINVAL);
833de56aee0SKonstantin Belousov #ifdef KTRACE
834de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
835de56aee0SKonstantin Belousov 		ktritimerval(aitv);
836de56aee0SKonstantin Belousov #endif
837b5ea3779SAlexander Motin 	if (itimerfix(&aitv->it_value) ||
838b5ea3779SAlexander Motin 	    aitv->it_value.tv_sec > INT32_MAX / 2)
839cfa0efe7SMaxim Sobolev 		return (EINVAL);
840cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
841cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
842b5ea3779SAlexander Motin 	else if (itimerfix(&aitv->it_interval) ||
843b5ea3779SAlexander Motin 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
84496d7f8efSTim J. Robbins 		return (EINVAL);
84596d7f8efSTim J. Robbins 
846cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
84796d7f8efSTim J. Robbins 		PROC_LOCK(p);
8484cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
8494f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
850b5ea3779SAlexander Motin 		microuptime(&ctv);
851cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
852b5ea3779SAlexander Motin 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
853cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
854b5ea3779SAlexander Motin 			sbt = tvtosbt(aitv->it_value);
855b5ea3779SAlexander Motin 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
856b5ea3779SAlexander Motin 			    realitexpire, p, C_ABSOLUTE);
85725b4d3a8SJohn Baldwin 		}
858cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
859cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
86096d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
861cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
862cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
863cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
86496d7f8efSTim J. Robbins 			else
865cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
866fb99ab88SMatthew Dillon 		}
86796d7f8efSTim J. Robbins 	} else {
868d10a1df8SAlexander Motin 		if (aitv->it_interval.tv_sec == 0 &&
869d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec != 0 &&
870d10a1df8SAlexander Motin 		    aitv->it_interval.tv_usec < tick)
871d10a1df8SAlexander Motin 			aitv->it_interval.tv_usec = tick;
872d10a1df8SAlexander Motin 		if (aitv->it_value.tv_sec == 0 &&
873d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec != 0 &&
874d10a1df8SAlexander Motin 		    aitv->it_value.tv_usec < tick)
875d10a1df8SAlexander Motin 			aitv->it_value.tv_usec = tick;
8765c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
877cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
878cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
8795c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
88096d7f8efSTim J. Robbins 	}
881de56aee0SKonstantin Belousov #ifdef KTRACE
882de56aee0SKonstantin Belousov 	if (KTRPOINT(td, KTR_STRUCT))
883de56aee0SKonstantin Belousov 		ktritimerval(oitv);
884de56aee0SKonstantin Belousov #endif
88596d7f8efSTim J. Robbins 	return (0);
886df8bae1dSRodney W. Grimes }
887df8bae1dSRodney W. Grimes 
888df8bae1dSRodney W. Grimes /*
889df8bae1dSRodney W. Grimes  * Real interval timer expired:
890df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
891df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
892df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
893df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
894df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
895c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
8969207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
8979207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
8989207f00aSBruce Evans  * interrupt even when we're delayed.
899df8bae1dSRodney W. Grimes  */
900df8bae1dSRodney W. Grimes void
90191afe087SPoul-Henning Kamp realitexpire(void *arg)
902df8bae1dSRodney W. Grimes {
90391afe087SPoul-Henning Kamp 	struct proc *p;
904b5ea3779SAlexander Motin 	struct timeval ctv;
905b5ea3779SAlexander Motin 	sbintime_t isbt;
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
9088451d0ddSKip Macy 	kern_psignal(p, SIGALRM);
9094cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
9104cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
9115499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
9125499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
913df8bae1dSRodney W. Grimes 		return;
914df8bae1dSRodney W. Grimes 	}
915b5ea3779SAlexander Motin 	isbt = tvtosbt(p->p_realtimer.it_interval);
916b5ea3779SAlexander Motin 	if (isbt >= sbt_timethreshold)
917b5ea3779SAlexander Motin 		getmicrouptime(&ctv);
918b5ea3779SAlexander Motin 	else
919b5ea3779SAlexander Motin 		microuptime(&ctv);
920b5ea3779SAlexander Motin 	do {
921df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
922df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
923b5ea3779SAlexander Motin 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
924b5ea3779SAlexander Motin 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
925b5ea3779SAlexander Motin 	    isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE);
926df8bae1dSRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes /*
929df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
930df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
931df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
932df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
933df8bae1dSRodney W. Grimes  */
93426f9a767SRodney W. Grimes int
93591afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
936df8bae1dSRodney W. Grimes {
937df8bae1dSRodney W. Grimes 
93886857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
939df8bae1dSRodney W. Grimes 		return (EINVAL);
940b5ea3779SAlexander Motin 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
941b5ea3779SAlexander Motin 	    tv->tv_usec < (u_int)tick / 16)
942b5ea3779SAlexander Motin 		tv->tv_usec = (u_int)tick / 16;
943df8bae1dSRodney W. Grimes 	return (0);
944df8bae1dSRodney W. Grimes }
945df8bae1dSRodney W. Grimes 
946df8bae1dSRodney W. Grimes /*
947df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
948df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
949df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
950df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
951df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
952df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
953df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
954df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
955df8bae1dSRodney W. Grimes  */
95626f9a767SRodney W. Grimes int
95791afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
958df8bae1dSRodney W. Grimes {
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
961df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
962df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
963df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
964df8bae1dSRodney W. Grimes 			goto expire;
965df8bae1dSRodney W. Grimes 		}
966df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
967df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
968df8bae1dSRodney W. Grimes 	}
969df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
970df8bae1dSRodney W. Grimes 	usec = 0;
9714cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
972df8bae1dSRodney W. Grimes 		return (1);
973df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
974df8bae1dSRodney W. Grimes expire:
9754cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
976df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
977df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
978df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
979df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
980df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
981df8bae1dSRodney W. Grimes 		}
982df8bae1dSRodney W. Grimes 	} else
983df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
984df8bae1dSRodney W. Grimes 	return (0);
985df8bae1dSRodney W. Grimes }
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes /*
988df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
989df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
990df8bae1dSRodney W. Grimes  * results which are before the beginning,
991df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
992df8bae1dSRodney W. Grimes  * Caveat emptor.
993df8bae1dSRodney W. Grimes  */
99426f9a767SRodney W. Grimes void
9956ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
996df8bae1dSRodney W. Grimes {
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
999df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
1000df8bae1dSRodney W. Grimes 	timevalfix(t1);
1001df8bae1dSRodney W. Grimes }
1002df8bae1dSRodney W. Grimes 
100326f9a767SRodney W. Grimes void
10046ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
1005df8bae1dSRodney W. Grimes {
1006df8bae1dSRodney W. Grimes 
1007df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
1008df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
1009df8bae1dSRodney W. Grimes 	timevalfix(t1);
1010df8bae1dSRodney W. Grimes }
1011df8bae1dSRodney W. Grimes 
101287b6de2bSPoul-Henning Kamp static void
101391afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
1014df8bae1dSRodney W. Grimes {
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
1017df8bae1dSRodney W. Grimes 		t1->tv_sec--;
1018df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
1019df8bae1dSRodney W. Grimes 	}
1020df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
1021df8bae1dSRodney W. Grimes 		t1->tv_sec++;
1022df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
1023df8bae1dSRodney W. Grimes 	}
1024df8bae1dSRodney W. Grimes }
102591974ce1SSam Leffler 
102691974ce1SSam Leffler /*
1027addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
102891974ce1SSam Leffler  */
102991974ce1SSam Leffler int
103091974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
103191974ce1SSam Leffler {
103291974ce1SSam Leffler 	struct timeval tv, delta;
103391974ce1SSam Leffler 	int rv = 0;
103491974ce1SSam Leffler 
1035addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
1036addea9d4SSam Leffler 	delta = tv;
1037addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
103891974ce1SSam Leffler 
103991974ce1SSam Leffler 	/*
104091974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
104191974ce1SSam Leffler 	 * even if interval is huge.
104291974ce1SSam Leffler 	 */
104391974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
104491974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
104591974ce1SSam Leffler 		*lasttime = tv;
104691974ce1SSam Leffler 		rv = 1;
104791974ce1SSam Leffler 	}
104891974ce1SSam Leffler 
104991974ce1SSam Leffler 	return (rv);
105091974ce1SSam Leffler }
105191974ce1SSam Leffler 
105291974ce1SSam Leffler /*
105391974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
1054addea9d4SSam Leffler  *
1055addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
1056addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
1057addea9d4SSam Leffler  *
1058893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
1059893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
1060893bec80SSam Leffler  * limiting.
1061893bec80SSam Leffler  *
1062addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
1063addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
1064addea9d4SSam Leffler  * clock ticks for minimal overhead.
106591974ce1SSam Leffler  */
106691974ce1SSam Leffler int
106791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
106891974ce1SSam Leffler {
1069addea9d4SSam Leffler 	int now;
107091974ce1SSam Leffler 
107191974ce1SSam Leffler 	/*
1072addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
1073addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
1074addea9d4SSam Leffler 	 * lasttime.
107591974ce1SSam Leffler 	 */
1076addea9d4SSam Leffler 	now = ticks;
1077addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1078addea9d4SSam Leffler 		lasttime->tv_sec = now;
1079addea9d4SSam Leffler 		*curpps = 1;
1080893bec80SSam Leffler 		return (maxpps != 0);
1081addea9d4SSam Leffler 	} else {
1082addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
1083f8f02928SIan Lepore 		return (maxpps < 0 || *curpps <= maxpps);
1084addea9d4SSam Leffler 	}
108591974ce1SSam Leffler }
108686857b36SDavid Xu 
108786857b36SDavid Xu static void
108886857b36SDavid Xu itimer_start(void)
108986857b36SDavid Xu {
109086857b36SDavid Xu 	struct kclock rt_clock = {
109186857b36SDavid Xu 		.timer_create  = realtimer_create,
109286857b36SDavid Xu 		.timer_delete  = realtimer_delete,
109386857b36SDavid Xu 		.timer_settime = realtimer_settime,
109486857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
1095843b99c6SDavid Xu 		.event_hook    = NULL
109686857b36SDavid Xu 	};
109786857b36SDavid Xu 
109886857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
109986857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
110086857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
110186857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
110277e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
110377e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
110477e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1105993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
1106d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
1107993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
1108d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
110986857b36SDavid Xu }
111086857b36SDavid Xu 
111186857b36SDavid Xu int
111286857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
111386857b36SDavid Xu {
111486857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
111586857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
111686857b36SDavid Xu 		return (0);
111786857b36SDavid Xu 	}
111886857b36SDavid Xu 	posix_clocks[clockid] = *clk;
111986857b36SDavid Xu 	return (1);
112086857b36SDavid Xu }
112186857b36SDavid Xu 
112286857b36SDavid Xu static int
112386857b36SDavid Xu itimer_init(void *mem, int size, int flags)
112486857b36SDavid Xu {
112586857b36SDavid Xu 	struct itimer *it;
112686857b36SDavid Xu 
112786857b36SDavid Xu 	it = (struct itimer *)mem;
112886857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
112986857b36SDavid Xu 	return (0);
113086857b36SDavid Xu }
113186857b36SDavid Xu 
113286857b36SDavid Xu static void
113386857b36SDavid Xu itimer_fini(void *mem, int size)
113486857b36SDavid Xu {
113586857b36SDavid Xu 	struct itimer *it;
113686857b36SDavid Xu 
113786857b36SDavid Xu 	it = (struct itimer *)mem;
113886857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
113986857b36SDavid Xu }
114086857b36SDavid Xu 
114186857b36SDavid Xu static void
114286857b36SDavid Xu itimer_enter(struct itimer *it)
114386857b36SDavid Xu {
114486857b36SDavid Xu 
114586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
114686857b36SDavid Xu 	it->it_usecount++;
114786857b36SDavid Xu }
114886857b36SDavid Xu 
114986857b36SDavid Xu static void
115086857b36SDavid Xu itimer_leave(struct itimer *it)
115186857b36SDavid Xu {
115286857b36SDavid Xu 
115386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
115486857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
115586857b36SDavid Xu 
115686857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
115786857b36SDavid Xu 		wakeup(it);
115886857b36SDavid Xu }
115986857b36SDavid Xu 
116086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
116161d3a4efSDavid Xu struct ktimer_create_args {
116286857b36SDavid Xu 	clockid_t clock_id;
116386857b36SDavid Xu 	struct sigevent * evp;
116461d3a4efSDavid Xu 	int * timerid;
116586857b36SDavid Xu };
116686857b36SDavid Xu #endif
116786857b36SDavid Xu int
11688451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
116986857b36SDavid Xu {
1170643ee871SKonstantin Belousov 	struct sigevent *evp, ev;
117161d3a4efSDavid Xu 	int id;
117286857b36SDavid Xu 	int error;
117386857b36SDavid Xu 
1174643ee871SKonstantin Belousov 	if (uap->evp == NULL) {
1175643ee871SKonstantin Belousov 		evp = NULL;
1176643ee871SKonstantin Belousov 	} else {
117786857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
117886857b36SDavid Xu 		if (error != 0)
117986857b36SDavid Xu 			return (error);
1180643ee871SKonstantin Belousov 		evp = &ev;
1181643ee871SKonstantin Belousov 	}
1182643ee871SKonstantin Belousov 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
118386857b36SDavid Xu 	if (error == 0) {
118461d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
118586857b36SDavid Xu 		if (error != 0)
1186643ee871SKonstantin Belousov 			kern_ktimer_delete(td, id);
118786857b36SDavid Xu 	}
118886857b36SDavid Xu 	return (error);
118986857b36SDavid Xu }
119086857b36SDavid Xu 
1191643ee871SKonstantin Belousov int
1192643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1193643ee871SKonstantin Belousov     int *timerid, int preset_id)
119486857b36SDavid Xu {
119586857b36SDavid Xu 	struct proc *p = td->td_proc;
119686857b36SDavid Xu 	struct itimer *it;
119786857b36SDavid Xu 	int id;
119886857b36SDavid Xu 	int error;
119986857b36SDavid Xu 
120086857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
120186857b36SDavid Xu 		return (EINVAL);
120286857b36SDavid Xu 
120386857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
120486857b36SDavid Xu 		return (EINVAL);
120586857b36SDavid Xu 
120686857b36SDavid Xu 	if (evp != NULL) {
120786857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
120856c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
120956c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
121086857b36SDavid Xu 			return (EINVAL);
121156c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
121256c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
121386857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
121486857b36SDavid Xu 			return (EINVAL);
121586857b36SDavid Xu 	}
121686857b36SDavid Xu 
121760354683SDavid Xu 	if (p->p_itimers == NULL)
121886857b36SDavid Xu 		itimers_alloc(p);
121986857b36SDavid Xu 
122086857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
122186857b36SDavid Xu 	it->it_flags = 0;
122286857b36SDavid Xu 	it->it_usecount = 0;
122386857b36SDavid Xu 	it->it_active = 0;
122456c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
122556c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
122686857b36SDavid Xu 	it->it_overrun = 0;
122786857b36SDavid Xu 	it->it_overrun_last = 0;
122886857b36SDavid Xu 	it->it_clockid = clock_id;
122986857b36SDavid Xu 	it->it_timerid = -1;
123086857b36SDavid Xu 	it->it_proc = p;
123186857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
123286857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
123386857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
123486857b36SDavid Xu 	if (error != 0)
123586857b36SDavid Xu 		goto out;
123686857b36SDavid Xu 
123786857b36SDavid Xu 	PROC_LOCK(p);
123886857b36SDavid Xu 	if (preset_id != -1) {
123986857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
124086857b36SDavid Xu 		id = preset_id;
124160354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
124286857b36SDavid Xu 			PROC_UNLOCK(p);
124386857b36SDavid Xu 			error = 0;
124486857b36SDavid Xu 			goto out;
124586857b36SDavid Xu 		}
124686857b36SDavid Xu 	} else {
124786857b36SDavid Xu 		/*
124886857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
124986857b36SDavid Xu 		 * for setitimer().
125086857b36SDavid Xu 		 */
125186857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
125260354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
125386857b36SDavid Xu 				break;
125486857b36SDavid Xu 		if (id == TIMER_MAX) {
125586857b36SDavid Xu 			PROC_UNLOCK(p);
125686857b36SDavid Xu 			error = EAGAIN;
125786857b36SDavid Xu 			goto out;
125886857b36SDavid Xu 		}
125986857b36SDavid Xu 	}
126086857b36SDavid Xu 	it->it_timerid = id;
126160354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
126286857b36SDavid Xu 	if (evp != NULL)
126386857b36SDavid Xu 		it->it_sigev = *evp;
126486857b36SDavid Xu 	else {
126586857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
126686857b36SDavid Xu 		switch (clock_id) {
126786857b36SDavid Xu 		default:
126886857b36SDavid Xu 		case CLOCK_REALTIME:
126986857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
127086857b36SDavid Xu 			break;
127186857b36SDavid Xu 		case CLOCK_VIRTUAL:
127286857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
127386857b36SDavid Xu 			break;
127486857b36SDavid Xu 		case CLOCK_PROF:
127586857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
127686857b36SDavid Xu 			break;
127786857b36SDavid Xu 		}
12788f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
127986857b36SDavid Xu 	}
128086857b36SDavid Xu 
128156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
128256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
128386857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
128486857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
128586857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
128686857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
128786857b36SDavid Xu 	}
128886857b36SDavid Xu 	PROC_UNLOCK(p);
128986857b36SDavid Xu 	*timerid = id;
129086857b36SDavid Xu 	return (0);
129186857b36SDavid Xu 
129286857b36SDavid Xu out:
129386857b36SDavid Xu 	ITIMER_LOCK(it);
129486857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
129586857b36SDavid Xu 	ITIMER_UNLOCK(it);
129686857b36SDavid Xu 	uma_zfree(itimer_zone, it);
129786857b36SDavid Xu 	return (error);
129886857b36SDavid Xu }
129986857b36SDavid Xu 
130086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
130161d3a4efSDavid Xu struct ktimer_delete_args {
130261d3a4efSDavid Xu 	int timerid;
130386857b36SDavid Xu };
130486857b36SDavid Xu #endif
130586857b36SDavid Xu int
13068451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
130786857b36SDavid Xu {
1308643ee871SKonstantin Belousov 
1309643ee871SKonstantin Belousov 	return (kern_ktimer_delete(td, uap->timerid));
131086857b36SDavid Xu }
131186857b36SDavid Xu 
131286857b36SDavid Xu static struct itimer *
1313843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
131486857b36SDavid Xu {
131586857b36SDavid Xu 	struct itimer *it;
131686857b36SDavid Xu 
131786857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
13183f935cf3SColin Percival 	if ((p->p_itimers == NULL) ||
13193f935cf3SColin Percival 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
132060354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
132186857b36SDavid Xu 		return (NULL);
132286857b36SDavid Xu 	}
132386857b36SDavid Xu 	ITIMER_LOCK(it);
1324843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
132586857b36SDavid Xu 		ITIMER_UNLOCK(it);
132686857b36SDavid Xu 		it = NULL;
132786857b36SDavid Xu 	}
132886857b36SDavid Xu 	return (it);
132986857b36SDavid Xu }
133086857b36SDavid Xu 
1331643ee871SKonstantin Belousov int
1332643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid)
133386857b36SDavid Xu {
133486857b36SDavid Xu 	struct proc *p = td->td_proc;
133586857b36SDavid Xu 	struct itimer *it;
133686857b36SDavid Xu 
133786857b36SDavid Xu 	PROC_LOCK(p);
1338843b99c6SDavid Xu 	it = itimer_find(p, timerid);
133986857b36SDavid Xu 	if (it == NULL) {
134086857b36SDavid Xu 		PROC_UNLOCK(p);
134186857b36SDavid Xu 		return (EINVAL);
134286857b36SDavid Xu 	}
134386857b36SDavid Xu 	PROC_UNLOCK(p);
134486857b36SDavid Xu 
134586857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
134686857b36SDavid Xu 	while (it->it_usecount > 0) {
134786857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
134886857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
134986857b36SDavid Xu 	}
135086857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
135186857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
135286857b36SDavid Xu 	ITIMER_UNLOCK(it);
135386857b36SDavid Xu 
135486857b36SDavid Xu 	PROC_LOCK(p);
135586857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
135686857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
135760354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
135886857b36SDavid Xu 	PROC_UNLOCK(p);
135986857b36SDavid Xu 	uma_zfree(itimer_zone, it);
136086857b36SDavid Xu 	return (0);
136186857b36SDavid Xu }
136286857b36SDavid Xu 
136386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
136461d3a4efSDavid Xu struct ktimer_settime_args {
136561d3a4efSDavid Xu 	int timerid;
136686857b36SDavid Xu 	int flags;
136786857b36SDavid Xu 	const struct itimerspec * value;
136886857b36SDavid Xu 	struct itimerspec * ovalue;
136986857b36SDavid Xu };
137086857b36SDavid Xu #endif
137186857b36SDavid Xu int
13728451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
137386857b36SDavid Xu {
137486857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
137586857b36SDavid Xu 	int error;
137686857b36SDavid Xu 
137786857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
137886857b36SDavid Xu 	if (error != 0)
137986857b36SDavid Xu 		return (error);
1380643ee871SKonstantin Belousov 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1381643ee871SKonstantin Belousov 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1382643ee871SKonstantin Belousov 	if (error == 0 && uap->ovalue != NULL)
1383643ee871SKonstantin Belousov 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1384643ee871SKonstantin Belousov 	return (error);
1385643ee871SKonstantin Belousov }
138686857b36SDavid Xu 
1387643ee871SKonstantin Belousov int
1388643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1389643ee871SKonstantin Belousov     struct itimerspec *val, struct itimerspec *oval)
1390643ee871SKonstantin Belousov {
1391643ee871SKonstantin Belousov 	struct proc *p;
1392643ee871SKonstantin Belousov 	struct itimer *it;
1393643ee871SKonstantin Belousov 	int error;
139486857b36SDavid Xu 
1395643ee871SKonstantin Belousov 	p = td->td_proc;
139686857b36SDavid Xu 	PROC_LOCK(p);
1397643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
139886857b36SDavid Xu 		PROC_UNLOCK(p);
139986857b36SDavid Xu 		error = EINVAL;
140086857b36SDavid Xu 	} else {
140186857b36SDavid Xu 		PROC_UNLOCK(p);
140286857b36SDavid Xu 		itimer_enter(it);
1403643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1404643ee871SKonstantin Belousov 		    flags, val, oval));
140586857b36SDavid Xu 		itimer_leave(it);
140686857b36SDavid Xu 		ITIMER_UNLOCK(it);
140786857b36SDavid Xu 	}
140886857b36SDavid Xu 	return (error);
140986857b36SDavid Xu }
141086857b36SDavid Xu 
141186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
141261d3a4efSDavid Xu struct ktimer_gettime_args {
141361d3a4efSDavid Xu 	int timerid;
141486857b36SDavid Xu 	struct itimerspec * value;
141586857b36SDavid Xu };
141686857b36SDavid Xu #endif
141786857b36SDavid Xu int
14188451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
141986857b36SDavid Xu {
142086857b36SDavid Xu 	struct itimerspec val;
142186857b36SDavid Xu 	int error;
142286857b36SDavid Xu 
1423643ee871SKonstantin Belousov 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1424643ee871SKonstantin Belousov 	if (error == 0)
1425643ee871SKonstantin Belousov 		error = copyout(&val, uap->value, sizeof(val));
1426643ee871SKonstantin Belousov 	return (error);
1427643ee871SKonstantin Belousov }
1428643ee871SKonstantin Belousov 
1429643ee871SKonstantin Belousov int
1430643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1431643ee871SKonstantin Belousov {
1432643ee871SKonstantin Belousov 	struct proc *p;
1433643ee871SKonstantin Belousov 	struct itimer *it;
1434643ee871SKonstantin Belousov 	int error;
1435643ee871SKonstantin Belousov 
1436643ee871SKonstantin Belousov 	p = td->td_proc;
143786857b36SDavid Xu 	PROC_LOCK(p);
1438643ee871SKonstantin Belousov 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
143986857b36SDavid Xu 		PROC_UNLOCK(p);
144086857b36SDavid Xu 		error = EINVAL;
144186857b36SDavid Xu 	} else {
144286857b36SDavid Xu 		PROC_UNLOCK(p);
144386857b36SDavid Xu 		itimer_enter(it);
1444643ee871SKonstantin Belousov 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
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_
145286857b36SDavid Xu struct timer_getoverrun_args {
145361d3a4efSDavid Xu 	int timerid;
145486857b36SDavid Xu };
145586857b36SDavid Xu #endif
145686857b36SDavid Xu int
14578451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
145886857b36SDavid Xu {
1459e346f8c4SBjoern A. Zeeb 
1460e346f8c4SBjoern A. Zeeb 	return (kern_ktimer_getoverrun(td, uap->timerid));
1461e346f8c4SBjoern A. Zeeb }
1462e346f8c4SBjoern A. Zeeb 
1463e346f8c4SBjoern A. Zeeb int
1464e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id)
1465e346f8c4SBjoern A. Zeeb {
146686857b36SDavid Xu 	struct proc *p = td->td_proc;
146786857b36SDavid Xu 	struct itimer *it;
146886857b36SDavid Xu 	int error ;
146986857b36SDavid Xu 
147086857b36SDavid Xu 	PROC_LOCK(p);
1471e346f8c4SBjoern A. Zeeb 	if (timer_id < 3 ||
1472e346f8c4SBjoern A. Zeeb 	    (it = itimer_find(p, timer_id)) == NULL) {
147386857b36SDavid Xu 		PROC_UNLOCK(p);
147486857b36SDavid Xu 		error = EINVAL;
147586857b36SDavid Xu 	} else {
147686857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
147786857b36SDavid Xu 		ITIMER_UNLOCK(it);
147856c06c4bSDavid Xu 		PROC_UNLOCK(p);
147986857b36SDavid Xu 		error = 0;
148086857b36SDavid Xu 	}
148186857b36SDavid Xu 	return (error);
148286857b36SDavid Xu }
148386857b36SDavid Xu 
148486857b36SDavid Xu static int
148586857b36SDavid Xu realtimer_create(struct itimer *it)
148686857b36SDavid Xu {
148786857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
148886857b36SDavid Xu 	return (0);
148986857b36SDavid Xu }
149086857b36SDavid Xu 
149186857b36SDavid Xu static int
149286857b36SDavid Xu realtimer_delete(struct itimer *it)
149386857b36SDavid Xu {
149486857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1495843b99c6SDavid Xu 
1496d6b6592eSDavid Xu 	/*
1497d6b6592eSDavid Xu 	 * clear timer's value and interval to tell realtimer_expire
1498d6b6592eSDavid Xu 	 * to not rearm the timer.
1499d6b6592eSDavid Xu 	 */
1500d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_value);
1501d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_interval);
1502843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1503843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1504843b99c6SDavid Xu 	ITIMER_LOCK(it);
150586857b36SDavid Xu 	return (0);
150686857b36SDavid Xu }
150786857b36SDavid Xu 
150886857b36SDavid Xu static int
150986857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
151086857b36SDavid Xu {
151156c06c4bSDavid Xu 	struct timespec cts;
151286857b36SDavid Xu 
151386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
151486857b36SDavid Xu 
151556c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
151656c06c4bSDavid Xu 	*ovalue = it->it_time;
151786857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
151856c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
151986857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
152086857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
152186857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
152286857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
152386857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
152486857b36SDavid Xu 		}
152586857b36SDavid Xu 	}
152686857b36SDavid Xu 	return (0);
152786857b36SDavid Xu }
152886857b36SDavid Xu 
152986857b36SDavid Xu static int
153086857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
153186857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
153286857b36SDavid Xu {
153356c06c4bSDavid Xu 	struct timespec cts, ts;
153456c06c4bSDavid Xu 	struct timeval tv;
153556c06c4bSDavid Xu 	struct itimerspec val;
153686857b36SDavid Xu 
153786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
153886857b36SDavid Xu 
153956c06c4bSDavid Xu 	val = *value;
154056c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
154186857b36SDavid Xu 		return (EINVAL);
154286857b36SDavid Xu 
154356c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
154456c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
154586857b36SDavid Xu 			return (EINVAL);
154686857b36SDavid Xu 	} else {
154756c06c4bSDavid Xu 		timespecclear(&val.it_interval);
154886857b36SDavid Xu 	}
154986857b36SDavid Xu 
155086857b36SDavid Xu 	if (ovalue != NULL)
155186857b36SDavid Xu 		realtimer_gettime(it, ovalue);
155286857b36SDavid Xu 
155386857b36SDavid Xu 	it->it_time = val;
155456c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
155556c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
155656c06c4bSDavid Xu 		ts = val.it_value;
155786857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
155886857b36SDavid Xu 			/* Convert to absolute time. */
155956c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
156086857b36SDavid Xu 		} else {
156156c06c4bSDavid Xu 			timespecsub(&ts, &cts);
156286857b36SDavid Xu 			/*
156356c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
156486857b36SDavid Xu 			 * fix it.
156586857b36SDavid Xu 			 */
156686857b36SDavid Xu 		}
156756c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
156856c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
156986857b36SDavid Xu 			realtimer_expire, it);
157086857b36SDavid Xu 	} else {
157186857b36SDavid Xu 		callout_stop(&it->it_callout);
157286857b36SDavid Xu 	}
157386857b36SDavid Xu 
157486857b36SDavid Xu 	return (0);
157586857b36SDavid Xu }
157686857b36SDavid Xu 
157786857b36SDavid Xu static void
157856c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
157986857b36SDavid Xu {
158086857b36SDavid Xu 	if (id == CLOCK_REALTIME)
158156c06c4bSDavid Xu 		getnanotime(ts);
158286857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
158356c06c4bSDavid Xu 		getnanouptime(ts);
158456c06c4bSDavid Xu }
158556c06c4bSDavid Xu 
158656c06c4bSDavid Xu int
158761d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
158856c06c4bSDavid Xu {
158956c06c4bSDavid Xu 	struct itimer *it;
159056c06c4bSDavid Xu 
159156c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1592843b99c6SDavid Xu 	it = itimer_find(p, timerid);
159356c06c4bSDavid Xu 	if (it != NULL) {
159456c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
159556c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
159656c06c4bSDavid Xu 		it->it_overrun = 0;
159756c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
159856c06c4bSDavid Xu 		return (0);
159956c06c4bSDavid Xu 	}
160056c06c4bSDavid Xu 	return (EINVAL);
160156c06c4bSDavid Xu }
160256c06c4bSDavid Xu 
160356c06c4bSDavid Xu int
160456c06c4bSDavid Xu itimespecfix(struct timespec *ts)
160556c06c4bSDavid Xu {
160656c06c4bSDavid Xu 
160756c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
160856c06c4bSDavid Xu 		return (EINVAL);
160956c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
161056c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
161156c06c4bSDavid Xu 	return (0);
161286857b36SDavid Xu }
161386857b36SDavid Xu 
161486857b36SDavid Xu /* Timeout callback for realtime timer */
161586857b36SDavid Xu static void
161686857b36SDavid Xu realtimer_expire(void *arg)
161786857b36SDavid Xu {
161856c06c4bSDavid Xu 	struct timespec cts, ts;
161956c06c4bSDavid Xu 	struct timeval tv;
162086857b36SDavid Xu 	struct itimer *it;
162186857b36SDavid Xu 
162286857b36SDavid Xu 	it = (struct itimer *)arg;
162386857b36SDavid Xu 
162456c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
162586857b36SDavid Xu 	/* Only fire if time is reached. */
162656c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
162756c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
162856c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
162986857b36SDavid Xu 				    &it->it_time.it_interval);
163056c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
163177e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
163286857b36SDavid Xu 					it->it_overrun++;
163377e718f7SDavid Xu 				else
163477e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
163556c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
163686857b36SDavid Xu 					    &it->it_time.it_interval);
163786857b36SDavid Xu 			}
163886857b36SDavid Xu 		} else {
163986857b36SDavid Xu 			/* single shot timer ? */
164056c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
164186857b36SDavid Xu 		}
164256c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
164356c06c4bSDavid Xu 			ts = it->it_time.it_value;
164456c06c4bSDavid Xu 			timespecsub(&ts, &cts);
164556c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
164656c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
164786857b36SDavid Xu 				 realtimer_expire, it);
164886857b36SDavid Xu 		}
1649d6b6592eSDavid Xu 		itimer_enter(it);
165086857b36SDavid Xu 		ITIMER_UNLOCK(it);
165186857b36SDavid Xu 		itimer_fire(it);
165286857b36SDavid Xu 		ITIMER_LOCK(it);
1653d6b6592eSDavid Xu 		itimer_leave(it);
165456c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
165556c06c4bSDavid Xu 		ts = it->it_time.it_value;
165656c06c4bSDavid Xu 		timespecsub(&ts, &cts);
165756c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
165856c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
165986857b36SDavid Xu  			it);
166086857b36SDavid Xu 	}
166186857b36SDavid Xu }
166286857b36SDavid Xu 
166386857b36SDavid Xu void
166486857b36SDavid Xu itimer_fire(struct itimer *it)
166586857b36SDavid Xu {
166686857b36SDavid Xu 	struct proc *p = it->it_proc;
1667cf7d9a8cSDavid Xu 	struct thread *td;
166886857b36SDavid Xu 
166956c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
167056c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1671cf7d9a8cSDavid Xu 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
167256c06c4bSDavid Xu 			ITIMER_LOCK(it);
167356c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
167456c06c4bSDavid Xu 			timespecclear(&it->it_time.it_interval);
167556c06c4bSDavid Xu 			callout_stop(&it->it_callout);
167656c06c4bSDavid Xu 			ITIMER_UNLOCK(it);
1677cf7d9a8cSDavid Xu 			return;
16786d7b314bSDavid Xu 		}
1679cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
1680cf7d9a8cSDavid Xu 			it->it_ksi.ksi_errno = 0;
1681cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1682cf7d9a8cSDavid Xu 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
168356c06c4bSDavid Xu 		} else {
168477e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
16856d7b314bSDavid Xu 				it->it_overrun++;
168677e718f7SDavid Xu 			else
168777e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
168856c06c4bSDavid Xu 		}
168986857b36SDavid Xu 		PROC_UNLOCK(p);
169086857b36SDavid Xu 	}
169186857b36SDavid Xu }
169286857b36SDavid Xu 
169386857b36SDavid Xu static void
169486857b36SDavid Xu itimers_alloc(struct proc *p)
169586857b36SDavid Xu {
169660354683SDavid Xu 	struct itimers *its;
169760354683SDavid Xu 	int i;
169886857b36SDavid Xu 
169960354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
170086857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
170186857b36SDavid Xu 	LIST_INIT(&its->its_prof);
170286857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
170360354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
170460354683SDavid Xu 		its->its_timers[i] = NULL;
170560354683SDavid Xu 	PROC_LOCK(p);
170660354683SDavid Xu 	if (p->p_itimers == NULL) {
170760354683SDavid Xu 		p->p_itimers = its;
170860354683SDavid Xu 		PROC_UNLOCK(p);
170960354683SDavid Xu 	}
171060354683SDavid Xu 	else {
171160354683SDavid Xu 		PROC_UNLOCK(p);
171260354683SDavid Xu 		free(its, M_SUBPROC);
171360354683SDavid Xu 	}
171486857b36SDavid Xu }
171586857b36SDavid Xu 
1716993182e5SAlexander Leidinger static void
1717993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1718993182e5SAlexander Leidinger {
1719993182e5SAlexander Leidinger 	itimers_event_hook_exit(arg, p);
1720993182e5SAlexander Leidinger }
1721993182e5SAlexander Leidinger 
172286857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1723d26b1a1fSDavid Xu static void
1724993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
172586857b36SDavid Xu {
172686857b36SDavid Xu 	struct itimers *its;
172786857b36SDavid Xu 	struct itimer *it;
17283e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
172986857b36SDavid Xu 	int i;
173086857b36SDavid Xu 
173160354683SDavid Xu 	if (p->p_itimers != NULL) {
173260354683SDavid Xu 		its = p->p_itimers;
173386857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
173486857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
173586857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
173686857b36SDavid Xu 		}
173786857b36SDavid Xu 		/*
173886857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
173986857b36SDavid Xu 		 * by new image.
174086857b36SDavid Xu 		 */
174186857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
174286857b36SDavid Xu 			i = 3;
174386857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
174486857b36SDavid Xu 			i = 0;
174586857b36SDavid Xu 		else
174686857b36SDavid Xu 			panic("unhandled event");
174786857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
1748843b99c6SDavid Xu 			if ((it = its->its_timers[i]) != NULL)
1749643ee871SKonstantin Belousov 				kern_ktimer_delete(curthread, i);
175086857b36SDavid Xu 		}
175186857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
175286857b36SDavid Xu 		    its->its_timers[1] == NULL &&
175386857b36SDavid Xu 		    its->its_timers[2] == NULL) {
175460354683SDavid Xu 			free(its, M_SUBPROC);
175560354683SDavid Xu 			p->p_itimers = NULL;
175686857b36SDavid Xu 		}
175786857b36SDavid Xu 	}
175886857b36SDavid Xu }
1759