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