kern_time.c revision 1.194
1/*	$NetBSD: kern_time.c,v 1.194 2019/01/31 20:09:05 maxv Exp $	*/
2
3/*-
4 * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christopher G. Demetriou, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1982, 1986, 1989, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
61 */
62
63#include <sys/cdefs.h>
64__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.194 2019/01/31 20:09:05 maxv Exp $");
65
66#include <sys/param.h>
67#include <sys/resourcevar.h>
68#include <sys/kernel.h>
69#include <sys/systm.h>
70#include <sys/proc.h>
71#include <sys/vnode.h>
72#include <sys/signalvar.h>
73#include <sys/syslog.h>
74#include <sys/timetc.h>
75#include <sys/timex.h>
76#include <sys/kauth.h>
77#include <sys/mount.h>
78#include <sys/syscallargs.h>
79#include <sys/cpu.h>
80
81static void	timer_intr(void *);
82static void	itimerfire(struct ptimer *);
83static void	itimerfree(struct ptimers *, int);
84
85kmutex_t	timer_lock;
86
87static void	*timer_sih;
88static TAILQ_HEAD(, ptimer) timer_queue;
89
90struct pool ptimer_pool, ptimers_pool;
91
92#define	CLOCK_VIRTUAL_P(clockid)	\
93	((clockid) == CLOCK_VIRTUAL || (clockid) == CLOCK_PROF)
94
95CTASSERT(ITIMER_REAL == CLOCK_REALTIME);
96CTASSERT(ITIMER_VIRTUAL == CLOCK_VIRTUAL);
97CTASSERT(ITIMER_PROF == CLOCK_PROF);
98CTASSERT(ITIMER_MONOTONIC == CLOCK_MONOTONIC);
99
100#define	DELAYTIMER_MAX	32
101
102/*
103 * Initialize timekeeping.
104 */
105void
106time_init(void)
107{
108
109	pool_init(&ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
110	    &pool_allocator_nointr, IPL_NONE);
111	pool_init(&ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
112	    &pool_allocator_nointr, IPL_NONE);
113}
114
115void
116time_init2(void)
117{
118
119	TAILQ_INIT(&timer_queue);
120	mutex_init(&timer_lock, MUTEX_DEFAULT, IPL_SCHED);
121	timer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
122	    timer_intr, NULL);
123}
124
125/* Time of day and interval timer support.
126 *
127 * These routines provide the kernel entry points to get and set
128 * the time-of-day and per-process interval timers.  Subroutines
129 * here provide support for adding and subtracting timeval structures
130 * and decrementing interval timers, optionally reloading the interval
131 * timers when they expire.
132 */
133
134/* This function is used by clock_settime and settimeofday */
135static int
136settime1(struct proc *p, const struct timespec *ts, bool check_kauth)
137{
138	struct timespec delta, now;
139	int s;
140
141	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
142	s = splclock();
143	nanotime(&now);
144	timespecsub(ts, &now, &delta);
145
146	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
147	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
148	    &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
149		splx(s);
150		return (EPERM);
151	}
152
153#ifdef notyet
154	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
155		splx(s);
156		return (EPERM);
157	}
158#endif
159
160	tc_setclock(ts);
161
162	timespecadd(&boottime, &delta, &boottime);
163
164	resettodr();
165	splx(s);
166
167	return (0);
168}
169
170int
171settime(struct proc *p, struct timespec *ts)
172{
173	return (settime1(p, ts, true));
174}
175
176/* ARGSUSED */
177int
178sys___clock_gettime50(struct lwp *l,
179    const struct sys___clock_gettime50_args *uap, register_t *retval)
180{
181	/* {
182		syscallarg(clockid_t) clock_id;
183		syscallarg(struct timespec *) tp;
184	} */
185	int error;
186	struct timespec ats;
187
188	error = clock_gettime1(SCARG(uap, clock_id), &ats);
189	if (error != 0)
190		return error;
191
192	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
193}
194
195/* ARGSUSED */
196int
197sys___clock_settime50(struct lwp *l,
198    const struct sys___clock_settime50_args *uap, register_t *retval)
199{
200	/* {
201		syscallarg(clockid_t) clock_id;
202		syscallarg(const struct timespec *) tp;
203	} */
204	int error;
205	struct timespec ats;
206
207	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
208		return error;
209
210	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
211}
212
213
214int
215clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
216    bool check_kauth)
217{
218	int error;
219
220	switch (clock_id) {
221	case CLOCK_REALTIME:
222		if ((error = settime1(p, tp, check_kauth)) != 0)
223			return (error);
224		break;
225	case CLOCK_MONOTONIC:
226		return (EINVAL);	/* read-only clock */
227	default:
228		return (EINVAL);
229	}
230
231	return 0;
232}
233
234int
235sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
236    register_t *retval)
237{
238	/* {
239		syscallarg(clockid_t) clock_id;
240		syscallarg(struct timespec *) tp;
241	} */
242	struct timespec ts;
243	int error;
244
245	if ((error = clock_getres1(SCARG(uap, clock_id), &ts)) != 0)
246		return error;
247
248	if (SCARG(uap, tp))
249		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
250
251	return error;
252}
253
254int
255clock_getres1(clockid_t clock_id, struct timespec *ts)
256{
257
258	switch (clock_id) {
259	case CLOCK_REALTIME:
260	case CLOCK_MONOTONIC:
261		ts->tv_sec = 0;
262		if (tc_getfrequency() > 1000000000)
263			ts->tv_nsec = 1;
264		else
265			ts->tv_nsec = 1000000000 / tc_getfrequency();
266		break;
267	default:
268		return EINVAL;
269	}
270
271	return 0;
272}
273
274/* ARGSUSED */
275int
276sys___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
277    register_t *retval)
278{
279	/* {
280		syscallarg(struct timespec *) rqtp;
281		syscallarg(struct timespec *) rmtp;
282	} */
283	struct timespec rmt, rqt;
284	int error, error1;
285
286	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
287	if (error)
288		return (error);
289
290	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
291	    SCARG(uap, rmtp) ? &rmt : NULL);
292	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
293		return error;
294
295	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
296	return error1 ? error1 : error;
297}
298
299/* ARGSUSED */
300int
301sys_clock_nanosleep(struct lwp *l, const struct sys_clock_nanosleep_args *uap,
302    register_t *retval)
303{
304	/* {
305		syscallarg(clockid_t) clock_id;
306		syscallarg(int) flags;
307		syscallarg(struct timespec *) rqtp;
308		syscallarg(struct timespec *) rmtp;
309	} */
310	struct timespec rmt, rqt;
311	int error, error1;
312
313	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
314	if (error)
315		goto out;
316
317	error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), &rqt,
318	    SCARG(uap, rmtp) ? &rmt : NULL);
319	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
320		goto out;
321
322	if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 &&
323	    (error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt))) != 0)
324		error = error1;
325out:
326	*retval = error;
327	return 0;
328}
329
330int
331nanosleep1(struct lwp *l, clockid_t clock_id, int flags, struct timespec *rqt,
332    struct timespec *rmt)
333{
334	struct timespec rmtstart;
335	int error, timo;
336
337	if ((error = ts2timo(clock_id, flags, rqt, &timo, &rmtstart)) != 0) {
338		if (error == ETIMEDOUT) {
339			error = 0;
340			if (rmt != NULL)
341				rmt->tv_sec = rmt->tv_nsec = 0;
342		}
343		return error;
344	}
345
346	/*
347	 * Avoid inadvertently sleeping forever
348	 */
349	if (timo == 0)
350		timo = 1;
351again:
352	error = kpause("nanoslp", true, timo, NULL);
353	if (rmt != NULL || error == 0) {
354		struct timespec rmtend;
355		struct timespec t0;
356		struct timespec *t;
357
358		(void)clock_gettime1(clock_id, &rmtend);
359		t = (rmt != NULL) ? rmt : &t0;
360		if (flags & TIMER_ABSTIME) {
361			timespecsub(rqt, &rmtend, t);
362		} else {
363			timespecsub(&rmtend, &rmtstart, t);
364			timespecsub(rqt, t, t);
365		}
366		if (t->tv_sec < 0)
367			timespecclear(t);
368		if (error == 0) {
369			timo = tstohz(t);
370			if (timo > 0)
371				goto again;
372		}
373	}
374
375	if (error == ERESTART)
376		error = EINTR;
377	if (error == EWOULDBLOCK)
378		error = 0;
379
380	return error;
381}
382
383int
384sys_clock_getcpuclockid2(struct lwp *l,
385    const struct sys_clock_getcpuclockid2_args *uap,
386    register_t *retval)
387{
388	/* {
389		syscallarg(idtype_t idtype;
390		syscallarg(id_t id);
391		syscallarg(clockid_t *)clock_id;
392	} */
393	pid_t pid;
394	lwpid_t lid;
395	clockid_t clock_id;
396	id_t id = SCARG(uap, id);
397
398	switch (SCARG(uap, idtype)) {
399	case P_PID:
400		pid = id == 0 ? l->l_proc->p_pid : id;
401		clock_id = CLOCK_PROCESS_CPUTIME_ID | pid;
402		break;
403	case P_LWPID:
404		lid = id == 0 ? l->l_lid : id;
405		clock_id = CLOCK_THREAD_CPUTIME_ID | lid;
406		break;
407	default:
408		return EINVAL;
409	}
410	return copyout(&clock_id, SCARG(uap, clock_id), sizeof(clock_id));
411}
412
413/* ARGSUSED */
414int
415sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
416    register_t *retval)
417{
418	/* {
419		syscallarg(struct timeval *) tp;
420		syscallarg(void *) tzp;		really "struct timezone *";
421	} */
422	struct timeval atv;
423	int error = 0;
424	struct timezone tzfake;
425
426	if (SCARG(uap, tp)) {
427		memset(&atv, 0, sizeof(atv));
428		microtime(&atv);
429		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
430		if (error)
431			return (error);
432	}
433	if (SCARG(uap, tzp)) {
434		/*
435		 * NetBSD has no kernel notion of time zone, so we just
436		 * fake up a timezone struct and return it if demanded.
437		 */
438		tzfake.tz_minuteswest = 0;
439		tzfake.tz_dsttime = 0;
440		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
441	}
442	return (error);
443}
444
445/* ARGSUSED */
446int
447sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
448    register_t *retval)
449{
450	/* {
451		syscallarg(const struct timeval *) tv;
452		syscallarg(const void *) tzp; really "const struct timezone *";
453	} */
454
455	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
456}
457
458int
459settimeofday1(const struct timeval *utv, bool userspace,
460    const void *utzp, struct lwp *l, bool check_kauth)
461{
462	struct timeval atv;
463	struct timespec ts;
464	int error;
465
466	/* Verify all parameters before changing time. */
467
468	/*
469	 * NetBSD has no kernel notion of time zone, and only an
470	 * obsolete program would try to set it, so we log a warning.
471	 */
472	if (utzp)
473		log(LOG_WARNING, "pid %d attempted to set the "
474		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
475
476	if (utv == NULL)
477		return 0;
478
479	if (userspace) {
480		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
481			return error;
482		utv = &atv;
483	}
484
485	TIMEVAL_TO_TIMESPEC(utv, &ts);
486	return settime1(l->l_proc, &ts, check_kauth);
487}
488
489int	time_adjusted;			/* set if an adjustment is made */
490
491/* ARGSUSED */
492int
493sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
494    register_t *retval)
495{
496	/* {
497		syscallarg(const struct timeval *) delta;
498		syscallarg(struct timeval *) olddelta;
499	} */
500	int error;
501	struct timeval atv, oldatv;
502
503	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
504	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
505		return error;
506
507	if (SCARG(uap, delta)) {
508		error = copyin(SCARG(uap, delta), &atv,
509		    sizeof(*SCARG(uap, delta)));
510		if (error)
511			return (error);
512	}
513	adjtime1(SCARG(uap, delta) ? &atv : NULL,
514	    SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
515	if (SCARG(uap, olddelta))
516		error = copyout(&oldatv, SCARG(uap, olddelta),
517		    sizeof(*SCARG(uap, olddelta)));
518	return error;
519}
520
521void
522adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
523{
524	extern int64_t time_adjtime;  /* in kern_ntptime.c */
525
526	if (olddelta) {
527		memset(olddelta, 0, sizeof(*olddelta));
528		mutex_spin_enter(&timecounter_lock);
529		olddelta->tv_sec = time_adjtime / 1000000;
530		olddelta->tv_usec = time_adjtime % 1000000;
531		if (olddelta->tv_usec < 0) {
532			olddelta->tv_usec += 1000000;
533			olddelta->tv_sec--;
534		}
535		mutex_spin_exit(&timecounter_lock);
536	}
537
538	if (delta) {
539		mutex_spin_enter(&timecounter_lock);
540		time_adjtime = delta->tv_sec * 1000000 + delta->tv_usec;
541
542		if (time_adjtime) {
543			/* We need to save the system time during shutdown */
544			time_adjusted |= 1;
545		}
546		mutex_spin_exit(&timecounter_lock);
547	}
548}
549
550/*
551 * Interval timer support. Both the BSD getitimer() family and the POSIX
552 * timer_*() family of routines are supported.
553 *
554 * All timers are kept in an array pointed to by p_timers, which is
555 * allocated on demand - many processes don't use timers at all. The
556 * first four elements in this array are reserved for the BSD timers:
557 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, element
558 * 2 is ITIMER_PROF, and element 3 is ITIMER_MONOTONIC. The rest may be
559 * allocated by the timer_create() syscall.
560 *
561 * Realtime timers are kept in the ptimer structure as an absolute
562 * time; virtual time timers are kept as a linked list of deltas.
563 * Virtual time timers are processed in the hardclock() routine of
564 * kern_clock.c.  The real time timer is processed by a callout
565 * routine, called from the softclock() routine.  Since a callout may
566 * be delayed in real time due to interrupt processing in the system,
567 * it is possible for the real time timeout routine (realtimeexpire,
568 * given below), to be delayed in real time past when it is supposed
569 * to occur.  It does not suffice, therefore, to reload the real timer
570 * .it_value from the real time timers .it_interval.  Rather, we
571 * compute the next time in absolute time the timer should go off.  */
572
573/* Allocate a POSIX realtime timer. */
574int
575sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
576    register_t *retval)
577{
578	/* {
579		syscallarg(clockid_t) clock_id;
580		syscallarg(struct sigevent *) evp;
581		syscallarg(timer_t *) timerid;
582	} */
583
584	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
585	    SCARG(uap, evp), copyin, l);
586}
587
588int
589timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
590    copyin_t fetch_event, struct lwp *l)
591{
592	int error;
593	timer_t timerid;
594	struct ptimers *pts;
595	struct ptimer *pt;
596	struct proc *p;
597
598	p = l->l_proc;
599
600	if ((u_int)id > CLOCK_MONOTONIC)
601		return (EINVAL);
602
603	if ((pts = p->p_timers) == NULL)
604		pts = timers_alloc(p);
605
606	pt = pool_get(&ptimer_pool, PR_WAITOK);
607	memset(pt, 0, sizeof(*pt));
608	if (evp != NULL) {
609		if (((error =
610		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
611		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
612			(pt->pt_ev.sigev_notify > SIGEV_SA)) ||
613			(pt->pt_ev.sigev_notify == SIGEV_SIGNAL &&
614			 (pt->pt_ev.sigev_signo <= 0 ||
615			  pt->pt_ev.sigev_signo >= NSIG))) {
616			pool_put(&ptimer_pool, pt);
617			return (error ? error : EINVAL);
618		}
619	}
620
621	/* Find a free timer slot, skipping those reserved for setitimer(). */
622	mutex_spin_enter(&timer_lock);
623	for (timerid = TIMER_MIN; timerid < TIMER_MAX; timerid++)
624		if (pts->pts_timers[timerid] == NULL)
625			break;
626	if (timerid == TIMER_MAX) {
627		mutex_spin_exit(&timer_lock);
628		pool_put(&ptimer_pool, pt);
629		return EAGAIN;
630	}
631	if (evp == NULL) {
632		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
633		switch (id) {
634		case CLOCK_REALTIME:
635		case CLOCK_MONOTONIC:
636			pt->pt_ev.sigev_signo = SIGALRM;
637			break;
638		case CLOCK_VIRTUAL:
639			pt->pt_ev.sigev_signo = SIGVTALRM;
640			break;
641		case CLOCK_PROF:
642			pt->pt_ev.sigev_signo = SIGPROF;
643			break;
644		}
645		pt->pt_ev.sigev_value.sival_int = timerid;
646	}
647	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
648	pt->pt_info.ksi_errno = 0;
649	pt->pt_info.ksi_code = 0;
650	pt->pt_info.ksi_pid = p->p_pid;
651	pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
652	pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
653	pt->pt_type = id;
654	pt->pt_proc = p;
655	pt->pt_overruns = 0;
656	pt->pt_poverruns = 0;
657	pt->pt_entry = timerid;
658	pt->pt_queued = false;
659	timespecclear(&pt->pt_time.it_value);
660	if (!CLOCK_VIRTUAL_P(id))
661		callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
662	else
663		pt->pt_active = 0;
664
665	pts->pts_timers[timerid] = pt;
666	mutex_spin_exit(&timer_lock);
667
668	return copyout(&timerid, tid, sizeof(timerid));
669}
670
671/* Delete a POSIX realtime timer */
672int
673sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
674    register_t *retval)
675{
676	/* {
677		syscallarg(timer_t) timerid;
678	} */
679	struct proc *p = l->l_proc;
680	timer_t timerid;
681	struct ptimers *pts;
682	struct ptimer *pt, *ptn;
683
684	timerid = SCARG(uap, timerid);
685	pts = p->p_timers;
686
687	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
688		return (EINVAL);
689
690	mutex_spin_enter(&timer_lock);
691	if ((pt = pts->pts_timers[timerid]) == NULL) {
692		mutex_spin_exit(&timer_lock);
693		return (EINVAL);
694	}
695	if (CLOCK_VIRTUAL_P(pt->pt_type)) {
696		if (pt->pt_active) {
697			ptn = LIST_NEXT(pt, pt_list);
698			LIST_REMOVE(pt, pt_list);
699			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
700				timespecadd(&pt->pt_time.it_value,
701				    &ptn->pt_time.it_value,
702				    &ptn->pt_time.it_value);
703			pt->pt_active = 0;
704		}
705	}
706	itimerfree(pts, timerid);
707
708	return (0);
709}
710
711/*
712 * Set up the given timer. The value in pt->pt_time.it_value is taken
713 * to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC timers and
714 * a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers.
715 */
716void
717timer_settime(struct ptimer *pt)
718{
719	struct ptimer *ptn, *pptn;
720	struct ptlist *ptl;
721
722	KASSERT(mutex_owned(&timer_lock));
723
724	if (!CLOCK_VIRTUAL_P(pt->pt_type)) {
725		callout_halt(&pt->pt_ch, &timer_lock);
726		if (timespecisset(&pt->pt_time.it_value)) {
727			/*
728			 * Don't need to check tshzto() return value, here.
729			 * callout_reset() does it for us.
730			 */
731			callout_reset(&pt->pt_ch,
732			    pt->pt_type == CLOCK_MONOTONIC ?
733			    tshztoup(&pt->pt_time.it_value) :
734			    tshzto(&pt->pt_time.it_value),
735			    realtimerexpire, pt);
736		}
737	} else {
738		if (pt->pt_active) {
739			ptn = LIST_NEXT(pt, pt_list);
740			LIST_REMOVE(pt, pt_list);
741			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
742				timespecadd(&pt->pt_time.it_value,
743				    &ptn->pt_time.it_value,
744				    &ptn->pt_time.it_value);
745		}
746		if (timespecisset(&pt->pt_time.it_value)) {
747			if (pt->pt_type == CLOCK_VIRTUAL)
748				ptl = &pt->pt_proc->p_timers->pts_virtual;
749			else
750				ptl = &pt->pt_proc->p_timers->pts_prof;
751
752			for (ptn = LIST_FIRST(ptl), pptn = NULL;
753			     ptn && timespeccmp(&pt->pt_time.it_value,
754				 &ptn->pt_time.it_value, >);
755			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
756				timespecsub(&pt->pt_time.it_value,
757				    &ptn->pt_time.it_value,
758				    &pt->pt_time.it_value);
759
760			if (pptn)
761				LIST_INSERT_AFTER(pptn, pt, pt_list);
762			else
763				LIST_INSERT_HEAD(ptl, pt, pt_list);
764
765			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
766				timespecsub(&ptn->pt_time.it_value,
767				    &pt->pt_time.it_value,
768				    &ptn->pt_time.it_value);
769
770			pt->pt_active = 1;
771		} else
772			pt->pt_active = 0;
773	}
774}
775
776void
777timer_gettime(struct ptimer *pt, struct itimerspec *aits)
778{
779	struct timespec now;
780	struct ptimer *ptn;
781
782	KASSERT(mutex_owned(&timer_lock));
783
784	*aits = pt->pt_time;
785	if (!CLOCK_VIRTUAL_P(pt->pt_type)) {
786		/*
787		 * Convert from absolute to relative time in .it_value
788		 * part of real time timer.  If time for real time
789		 * timer has passed return 0, else return difference
790		 * between current time and time for the timer to go
791		 * off.
792		 */
793		if (timespecisset(&aits->it_value)) {
794			if (pt->pt_type == CLOCK_REALTIME) {
795				getnanotime(&now);
796			} else { /* CLOCK_MONOTONIC */
797				getnanouptime(&now);
798			}
799			if (timespeccmp(&aits->it_value, &now, <))
800				timespecclear(&aits->it_value);
801			else
802				timespecsub(&aits->it_value, &now,
803				    &aits->it_value);
804		}
805	} else if (pt->pt_active) {
806		if (pt->pt_type == CLOCK_VIRTUAL)
807			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
808		else
809			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
810		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
811			timespecadd(&aits->it_value,
812			    &ptn->pt_time.it_value, &aits->it_value);
813		KASSERT(ptn != NULL); /* pt should be findable on the list */
814	} else
815		timespecclear(&aits->it_value);
816}
817
818
819
820/* Set and arm a POSIX realtime timer */
821int
822sys___timer_settime50(struct lwp *l,
823    const struct sys___timer_settime50_args *uap,
824    register_t *retval)
825{
826	/* {
827		syscallarg(timer_t) timerid;
828		syscallarg(int) flags;
829		syscallarg(const struct itimerspec *) value;
830		syscallarg(struct itimerspec *) ovalue;
831	} */
832	int error;
833	struct itimerspec value, ovalue, *ovp = NULL;
834
835	if ((error = copyin(SCARG(uap, value), &value,
836	    sizeof(struct itimerspec))) != 0)
837		return (error);
838
839	if (SCARG(uap, ovalue))
840		ovp = &ovalue;
841
842	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
843	    SCARG(uap, flags), l->l_proc)) != 0)
844		return error;
845
846	if (ovp)
847		return copyout(&ovalue, SCARG(uap, ovalue),
848		    sizeof(struct itimerspec));
849	return 0;
850}
851
852int
853dotimer_settime(int timerid, struct itimerspec *value,
854    struct itimerspec *ovalue, int flags, struct proc *p)
855{
856	struct timespec now;
857	struct itimerspec val, oval;
858	struct ptimers *pts;
859	struct ptimer *pt;
860	int error;
861
862	pts = p->p_timers;
863
864	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
865		return EINVAL;
866	val = *value;
867	if ((error = itimespecfix(&val.it_value)) != 0 ||
868	    (error = itimespecfix(&val.it_interval)) != 0)
869		return error;
870
871	mutex_spin_enter(&timer_lock);
872	if ((pt = pts->pts_timers[timerid]) == NULL) {
873		mutex_spin_exit(&timer_lock);
874		return EINVAL;
875	}
876
877	oval = pt->pt_time;
878	pt->pt_time = val;
879
880	/*
881	 * If we've been passed a relative time for a realtime timer,
882	 * convert it to absolute; if an absolute time for a virtual
883	 * timer, convert it to relative and make sure we don't set it
884	 * to zero, which would cancel the timer, or let it go
885	 * negative, which would confuse the comparison tests.
886	 */
887	if (timespecisset(&pt->pt_time.it_value)) {
888		if (!CLOCK_VIRTUAL_P(pt->pt_type)) {
889			if ((flags & TIMER_ABSTIME) == 0) {
890				if (pt->pt_type == CLOCK_REALTIME) {
891					getnanotime(&now);
892				} else { /* CLOCK_MONOTONIC */
893					getnanouptime(&now);
894				}
895				timespecadd(&pt->pt_time.it_value, &now,
896				    &pt->pt_time.it_value);
897			}
898		} else {
899			if ((flags & TIMER_ABSTIME) != 0) {
900				getnanotime(&now);
901				timespecsub(&pt->pt_time.it_value, &now,
902				    &pt->pt_time.it_value);
903				if (!timespecisset(&pt->pt_time.it_value) ||
904				    pt->pt_time.it_value.tv_sec < 0) {
905					pt->pt_time.it_value.tv_sec = 0;
906					pt->pt_time.it_value.tv_nsec = 1;
907				}
908			}
909		}
910	}
911
912	timer_settime(pt);
913	mutex_spin_exit(&timer_lock);
914
915	if (ovalue)
916		*ovalue = oval;
917
918	return (0);
919}
920
921/* Return the time remaining until a POSIX timer fires. */
922int
923sys___timer_gettime50(struct lwp *l,
924    const struct sys___timer_gettime50_args *uap, register_t *retval)
925{
926	/* {
927		syscallarg(timer_t) timerid;
928		syscallarg(struct itimerspec *) value;
929	} */
930	struct itimerspec its;
931	int error;
932
933	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
934	    &its)) != 0)
935		return error;
936
937	return copyout(&its, SCARG(uap, value), sizeof(its));
938}
939
940int
941dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
942{
943	struct ptimer *pt;
944	struct ptimers *pts;
945
946	pts = p->p_timers;
947	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
948		return (EINVAL);
949	mutex_spin_enter(&timer_lock);
950	if ((pt = pts->pts_timers[timerid]) == NULL) {
951		mutex_spin_exit(&timer_lock);
952		return (EINVAL);
953	}
954	timer_gettime(pt, its);
955	mutex_spin_exit(&timer_lock);
956
957	return 0;
958}
959
960/*
961 * Return the count of the number of times a periodic timer expired
962 * while a notification was already pending. The counter is reset when
963 * a timer expires and a notification can be posted.
964 */
965int
966sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
967    register_t *retval)
968{
969	/* {
970		syscallarg(timer_t) timerid;
971	} */
972	struct proc *p = l->l_proc;
973	struct ptimers *pts;
974	int timerid;
975	struct ptimer *pt;
976
977	timerid = SCARG(uap, timerid);
978
979	pts = p->p_timers;
980	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
981		return (EINVAL);
982	mutex_spin_enter(&timer_lock);
983	if ((pt = pts->pts_timers[timerid]) == NULL) {
984		mutex_spin_exit(&timer_lock);
985		return (EINVAL);
986	}
987	*retval = pt->pt_poverruns;
988	if (*retval >= DELAYTIMER_MAX)
989		*retval = DELAYTIMER_MAX;
990	mutex_spin_exit(&timer_lock);
991
992	return (0);
993}
994
995/*
996 * Real interval timer expired:
997 * send process whose timer expired an alarm signal.
998 * If time is not set up to reload, then just return.
999 * Else compute next time timer should go off which is > current time.
1000 * This is where delay in processing this timeout causes multiple
1001 * SIGALRM calls to be compressed into one.
1002 */
1003void
1004realtimerexpire(void *arg)
1005{
1006	uint64_t last_val, next_val, interval, now_ns;
1007	struct timespec now, next;
1008	struct ptimer *pt;
1009	int backwards;
1010
1011	pt = arg;
1012
1013	mutex_spin_enter(&timer_lock);
1014	itimerfire(pt);
1015
1016	if (!timespecisset(&pt->pt_time.it_interval)) {
1017		timespecclear(&pt->pt_time.it_value);
1018		mutex_spin_exit(&timer_lock);
1019		return;
1020	}
1021
1022	if (pt->pt_type == CLOCK_MONOTONIC) {
1023		getnanouptime(&now);
1024	} else {
1025		getnanotime(&now);
1026	}
1027	backwards = (timespeccmp(&pt->pt_time.it_value, &now, >));
1028	timespecadd(&pt->pt_time.it_value, &pt->pt_time.it_interval, &next);
1029	/* Handle the easy case of non-overflown timers first. */
1030	if (!backwards && timespeccmp(&next, &now, >)) {
1031		pt->pt_time.it_value = next;
1032	} else {
1033		now_ns = timespec2ns(&now);
1034		last_val = timespec2ns(&pt->pt_time.it_value);
1035		interval = timespec2ns(&pt->pt_time.it_interval);
1036
1037		next_val = now_ns +
1038		    (now_ns - last_val + interval - 1) % interval;
1039
1040		if (backwards)
1041			next_val += interval;
1042		else
1043			pt->pt_overruns += (now_ns - last_val) / interval;
1044
1045		pt->pt_time.it_value.tv_sec = next_val / 1000000000;
1046		pt->pt_time.it_value.tv_nsec = next_val % 1000000000;
1047	}
1048
1049	/*
1050	 * Don't need to check tshzto() return value, here.
1051	 * callout_reset() does it for us.
1052	 */
1053	callout_reset(&pt->pt_ch, pt->pt_type == CLOCK_MONOTONIC ?
1054	    tshztoup(&pt->pt_time.it_value) : tshzto(&pt->pt_time.it_value),
1055	    realtimerexpire, pt);
1056	mutex_spin_exit(&timer_lock);
1057}
1058
1059/* BSD routine to get the value of an interval timer. */
1060/* ARGSUSED */
1061int
1062sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
1063    register_t *retval)
1064{
1065	/* {
1066		syscallarg(int) which;
1067		syscallarg(struct itimerval *) itv;
1068	} */
1069	struct proc *p = l->l_proc;
1070	struct itimerval aitv;
1071	int error;
1072
1073	memset(&aitv, 0, sizeof(aitv));
1074	error = dogetitimer(p, SCARG(uap, which), &aitv);
1075	if (error)
1076		return error;
1077	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
1078}
1079
1080int
1081dogetitimer(struct proc *p, int which, struct itimerval *itvp)
1082{
1083	struct ptimers *pts;
1084	struct ptimer *pt;
1085	struct itimerspec its;
1086
1087	if ((u_int)which > ITIMER_MONOTONIC)
1088		return (EINVAL);
1089
1090	mutex_spin_enter(&timer_lock);
1091	pts = p->p_timers;
1092	if (pts == NULL || (pt = pts->pts_timers[which]) == NULL) {
1093		timerclear(&itvp->it_value);
1094		timerclear(&itvp->it_interval);
1095	} else {
1096		timer_gettime(pt, &its);
1097		TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
1098		TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
1099	}
1100	mutex_spin_exit(&timer_lock);
1101
1102	return 0;
1103}
1104
1105/* BSD routine to set/arm an interval timer. */
1106/* ARGSUSED */
1107int
1108sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
1109    register_t *retval)
1110{
1111	/* {
1112		syscallarg(int) which;
1113		syscallarg(const struct itimerval *) itv;
1114		syscallarg(struct itimerval *) oitv;
1115	} */
1116	struct proc *p = l->l_proc;
1117	int which = SCARG(uap, which);
1118	struct sys___getitimer50_args getargs;
1119	const struct itimerval *itvp;
1120	struct itimerval aitv;
1121	int error;
1122
1123	if ((u_int)which > ITIMER_MONOTONIC)
1124		return (EINVAL);
1125	itvp = SCARG(uap, itv);
1126	if (itvp &&
1127	    (error = copyin(itvp, &aitv, sizeof(struct itimerval))) != 0)
1128		return (error);
1129	if (SCARG(uap, oitv) != NULL) {
1130		SCARG(&getargs, which) = which;
1131		SCARG(&getargs, itv) = SCARG(uap, oitv);
1132		if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
1133			return (error);
1134	}
1135	if (itvp == 0)
1136		return (0);
1137
1138	return dosetitimer(p, which, &aitv);
1139}
1140
1141int
1142dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1143{
1144	struct timespec now;
1145	struct ptimers *pts;
1146	struct ptimer *pt, *spare;
1147
1148	KASSERT((u_int)which <= CLOCK_MONOTONIC);
1149	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1150		return (EINVAL);
1151
1152	/*
1153	 * Don't bother allocating data structures if the process just
1154	 * wants to clear the timer.
1155	 */
1156	spare = NULL;
1157	pts = p->p_timers;
1158 retry:
1159	if (!timerisset(&itvp->it_value) && (pts == NULL ||
1160	    pts->pts_timers[which] == NULL))
1161		return (0);
1162	if (pts == NULL)
1163		pts = timers_alloc(p);
1164	mutex_spin_enter(&timer_lock);
1165	pt = pts->pts_timers[which];
1166	if (pt == NULL) {
1167		if (spare == NULL) {
1168			mutex_spin_exit(&timer_lock);
1169			spare = pool_get(&ptimer_pool, PR_WAITOK);
1170			memset(spare, 0, sizeof(*spare));
1171			goto retry;
1172		}
1173		pt = spare;
1174		spare = NULL;
1175		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1176		pt->pt_ev.sigev_value.sival_int = which;
1177		pt->pt_overruns = 0;
1178		pt->pt_proc = p;
1179		pt->pt_type = which;
1180		pt->pt_entry = which;
1181		pt->pt_queued = false;
1182		if (pt->pt_type == CLOCK_REALTIME)
1183			callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
1184		else
1185			pt->pt_active = 0;
1186
1187		switch (which) {
1188		case ITIMER_REAL:
1189		case ITIMER_MONOTONIC:
1190			pt->pt_ev.sigev_signo = SIGALRM;
1191			break;
1192		case ITIMER_VIRTUAL:
1193			pt->pt_ev.sigev_signo = SIGVTALRM;
1194			break;
1195		case ITIMER_PROF:
1196			pt->pt_ev.sigev_signo = SIGPROF;
1197			break;
1198		}
1199		pts->pts_timers[which] = pt;
1200	}
1201
1202	TIMEVAL_TO_TIMESPEC(&itvp->it_value, &pt->pt_time.it_value);
1203	TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &pt->pt_time.it_interval);
1204
1205	if (timespecisset(&pt->pt_time.it_value)) {
1206		/* Convert to absolute time */
1207		/* XXX need to wrap in splclock for timecounters case? */
1208		switch (which) {
1209		case ITIMER_REAL:
1210			getnanotime(&now);
1211			timespecadd(&pt->pt_time.it_value, &now,
1212			    &pt->pt_time.it_value);
1213			break;
1214		case ITIMER_MONOTONIC:
1215			getnanouptime(&now);
1216			timespecadd(&pt->pt_time.it_value, &now,
1217			    &pt->pt_time.it_value);
1218			break;
1219		default:
1220			break;
1221		}
1222	}
1223	timer_settime(pt);
1224	mutex_spin_exit(&timer_lock);
1225	if (spare != NULL)
1226		pool_put(&ptimer_pool, spare);
1227
1228	return (0);
1229}
1230
1231/* Utility routines to manage the array of pointers to timers. */
1232struct ptimers *
1233timers_alloc(struct proc *p)
1234{
1235	struct ptimers *pts;
1236	int i;
1237
1238	pts = pool_get(&ptimers_pool, PR_WAITOK);
1239	LIST_INIT(&pts->pts_virtual);
1240	LIST_INIT(&pts->pts_prof);
1241	for (i = 0; i < TIMER_MAX; i++)
1242		pts->pts_timers[i] = NULL;
1243	mutex_spin_enter(&timer_lock);
1244	if (p->p_timers == NULL) {
1245		p->p_timers = pts;
1246		mutex_spin_exit(&timer_lock);
1247		return pts;
1248	}
1249	mutex_spin_exit(&timer_lock);
1250	pool_put(&ptimers_pool, pts);
1251	return p->p_timers;
1252}
1253
1254/*
1255 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1256 * then clean up all timers and free all the data structures. If
1257 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1258 * by timer_create(), not the BSD setitimer() timers, and only free the
1259 * structure if none of those remain.
1260 */
1261void
1262timers_free(struct proc *p, int which)
1263{
1264	struct ptimers *pts;
1265	struct ptimer *ptn;
1266	struct timespec ts;
1267	int i;
1268
1269	if (p->p_timers == NULL)
1270		return;
1271
1272	pts = p->p_timers;
1273	mutex_spin_enter(&timer_lock);
1274	if (which == TIMERS_ALL) {
1275		p->p_timers = NULL;
1276		i = 0;
1277	} else {
1278		timespecclear(&ts);
1279		for (ptn = LIST_FIRST(&pts->pts_virtual);
1280		     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1281		     ptn = LIST_NEXT(ptn, pt_list)) {
1282			KASSERT(ptn->pt_type == CLOCK_VIRTUAL);
1283			timespecadd(&ts, &ptn->pt_time.it_value, &ts);
1284		}
1285		LIST_FIRST(&pts->pts_virtual) = NULL;
1286		if (ptn) {
1287			KASSERT(ptn->pt_type == CLOCK_VIRTUAL);
1288			timespecadd(&ts, &ptn->pt_time.it_value,
1289			    &ptn->pt_time.it_value);
1290			LIST_INSERT_HEAD(&pts->pts_virtual, ptn, pt_list);
1291		}
1292		timespecclear(&ts);
1293		for (ptn = LIST_FIRST(&pts->pts_prof);
1294		     ptn && ptn != pts->pts_timers[ITIMER_PROF];
1295		     ptn = LIST_NEXT(ptn, pt_list)) {
1296			KASSERT(ptn->pt_type == CLOCK_PROF);
1297			timespecadd(&ts, &ptn->pt_time.it_value, &ts);
1298		}
1299		LIST_FIRST(&pts->pts_prof) = NULL;
1300		if (ptn) {
1301			KASSERT(ptn->pt_type == CLOCK_PROF);
1302			timespecadd(&ts, &ptn->pt_time.it_value,
1303			    &ptn->pt_time.it_value);
1304			LIST_INSERT_HEAD(&pts->pts_prof, ptn, pt_list);
1305		}
1306		i = TIMER_MIN;
1307	}
1308	for ( ; i < TIMER_MAX; i++) {
1309		if (pts->pts_timers[i] != NULL) {
1310			itimerfree(pts, i);
1311			mutex_spin_enter(&timer_lock);
1312		}
1313	}
1314	if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
1315	    pts->pts_timers[2] == NULL && pts->pts_timers[3] == NULL) {
1316		p->p_timers = NULL;
1317		mutex_spin_exit(&timer_lock);
1318		pool_put(&ptimers_pool, pts);
1319	} else
1320		mutex_spin_exit(&timer_lock);
1321}
1322
1323static void
1324itimerfree(struct ptimers *pts, int index)
1325{
1326	struct ptimer *pt;
1327
1328	KASSERT(mutex_owned(&timer_lock));
1329
1330	pt = pts->pts_timers[index];
1331	pts->pts_timers[index] = NULL;
1332	if (!CLOCK_VIRTUAL_P(pt->pt_type))
1333		callout_halt(&pt->pt_ch, &timer_lock);
1334	if (pt->pt_queued)
1335		TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1336	mutex_spin_exit(&timer_lock);
1337	if (!CLOCK_VIRTUAL_P(pt->pt_type))
1338		callout_destroy(&pt->pt_ch);
1339	pool_put(&ptimer_pool, pt);
1340}
1341
1342/*
1343 * Decrement an interval timer by a specified number
1344 * of nanoseconds, which must be less than a second,
1345 * i.e. < 1000000000.  If the timer expires, then reload
1346 * it.  In this case, carry over (nsec - old value) to
1347 * reduce the value reloaded into the timer so that
1348 * the timer does not drift.  This routine assumes
1349 * that it is called in a context where the timers
1350 * on which it is operating cannot change in value.
1351 */
1352static int
1353itimerdecr(struct ptimer *pt, int nsec)
1354{
1355	struct itimerspec *itp;
1356
1357	KASSERT(mutex_owned(&timer_lock));
1358	KASSERT(CLOCK_VIRTUAL_P(pt->pt_type));
1359
1360	itp = &pt->pt_time;
1361	if (itp->it_value.tv_nsec < nsec) {
1362		if (itp->it_value.tv_sec == 0) {
1363			/* expired, and already in next interval */
1364			nsec -= itp->it_value.tv_nsec;
1365			goto expire;
1366		}
1367		itp->it_value.tv_nsec += 1000000000;
1368		itp->it_value.tv_sec--;
1369	}
1370	itp->it_value.tv_nsec -= nsec;
1371	nsec = 0;
1372	if (timespecisset(&itp->it_value))
1373		return (1);
1374	/* expired, exactly at end of interval */
1375expire:
1376	if (timespecisset(&itp->it_interval)) {
1377		itp->it_value = itp->it_interval;
1378		itp->it_value.tv_nsec -= nsec;
1379		if (itp->it_value.tv_nsec < 0) {
1380			itp->it_value.tv_nsec += 1000000000;
1381			itp->it_value.tv_sec--;
1382		}
1383		timer_settime(pt);
1384	} else
1385		itp->it_value.tv_nsec = 0;		/* sec is already 0 */
1386	return (0);
1387}
1388
1389static void
1390itimerfire(struct ptimer *pt)
1391{
1392
1393	KASSERT(mutex_owned(&timer_lock));
1394
1395	/*
1396	 * XXX Can overrun, but we don't do signal queueing yet, anyway.
1397	 * XXX Relying on the clock interrupt is stupid.
1398	 */
1399	if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL || pt->pt_queued) {
1400		return;
1401	}
1402	TAILQ_INSERT_TAIL(&timer_queue, pt, pt_chain);
1403	pt->pt_queued = true;
1404	softint_schedule(timer_sih);
1405}
1406
1407void
1408timer_tick(lwp_t *l, bool user)
1409{
1410	struct ptimers *pts;
1411	struct ptimer *pt;
1412	proc_t *p;
1413
1414	p = l->l_proc;
1415	if (p->p_timers == NULL)
1416		return;
1417
1418	mutex_spin_enter(&timer_lock);
1419	if ((pts = l->l_proc->p_timers) != NULL) {
1420		/*
1421		 * Run current process's virtual and profile time, as needed.
1422		 */
1423		if (user && (pt = LIST_FIRST(&pts->pts_virtual)) != NULL)
1424			if (itimerdecr(pt, tick * 1000) == 0)
1425				itimerfire(pt);
1426		if ((pt = LIST_FIRST(&pts->pts_prof)) != NULL)
1427			if (itimerdecr(pt, tick * 1000) == 0)
1428				itimerfire(pt);
1429	}
1430	mutex_spin_exit(&timer_lock);
1431}
1432
1433static void
1434timer_intr(void *cookie)
1435{
1436	ksiginfo_t ksi;
1437	struct ptimer *pt;
1438	proc_t *p;
1439
1440	mutex_enter(proc_lock);
1441	mutex_spin_enter(&timer_lock);
1442	while ((pt = TAILQ_FIRST(&timer_queue)) != NULL) {
1443		TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1444		KASSERT(pt->pt_queued);
1445		pt->pt_queued = false;
1446
1447		if (pt->pt_proc->p_timers == NULL) {
1448			/* Process is dying. */
1449			continue;
1450		}
1451		p = pt->pt_proc;
1452		if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
1453			continue;
1454		}
1455		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
1456			pt->pt_overruns++;
1457			continue;
1458		}
1459
1460		KSI_INIT(&ksi);
1461		ksi.ksi_signo = pt->pt_ev.sigev_signo;
1462		ksi.ksi_code = SI_TIMER;
1463		ksi.ksi_value = pt->pt_ev.sigev_value;
1464		pt->pt_poverruns = pt->pt_overruns;
1465		pt->pt_overruns = 0;
1466		mutex_spin_exit(&timer_lock);
1467		kpsignal(p, &ksi, NULL);
1468		mutex_spin_enter(&timer_lock);
1469	}
1470	mutex_spin_exit(&timer_lock);
1471	mutex_exit(proc_lock);
1472}
1473
1474/*
1475 * Check if the time will wrap if set to ts.
1476 *
1477 * ts - timespec describing the new time
1478 * delta - the delta between the current time and ts
1479 */
1480bool
1481time_wraps(struct timespec *ts, struct timespec *delta)
1482{
1483
1484	/*
1485	 * Don't allow the time to be set forward so far it
1486	 * will wrap and become negative, thus allowing an
1487	 * attacker to bypass the next check below.  The
1488	 * cutoff is 1 year before rollover occurs, so even
1489	 * if the attacker uses adjtime(2) to move the time
1490	 * past the cutoff, it will take a very long time
1491	 * to get to the wrap point.
1492	 */
1493	if ((ts->tv_sec > LLONG_MAX - 365*24*60*60) ||
1494	    (delta->tv_sec < 0 || delta->tv_nsec < 0))
1495		return true;
1496
1497	return false;
1498}
1499