xref: /netbsd-src/sys/kern/kern_time.c (revision 9c1da17e908379b8a470f1117a6395bd6a0ca559)
1 /*	$NetBSD: kern_time.c,v 1.92 2005/07/23 18:54:07 cube Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2004, 2005 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1982, 1986, 1989, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.92 2005/07/23 18:54:07 cube Exp $");
72 
73 #include "fs_nfs.h"
74 #include "opt_nfs.h"
75 #include "opt_nfsserver.h"
76 
77 #include <sys/param.h>
78 #include <sys/resourcevar.h>
79 #include <sys/kernel.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
82 #include <sys/proc.h>
83 #include <sys/sa.h>
84 #include <sys/savar.h>
85 #include <sys/vnode.h>
86 #include <sys/signalvar.h>
87 #include <sys/syslog.h>
88 
89 #include <sys/mount.h>
90 #include <sys/syscallargs.h>
91 
92 #include <uvm/uvm_extern.h>
93 
94 #if defined(NFS) || defined(NFSSERVER)
95 #include <nfs/rpcv2.h>
96 #include <nfs/nfsproto.h>
97 #include <nfs/nfs_var.h>
98 #endif
99 
100 #include <machine/cpu.h>
101 
102 static void timerupcall(struct lwp *, void *);
103 
104 
105 /* Time of day and interval timer support.
106  *
107  * These routines provide the kernel entry points to get and set
108  * the time-of-day and per-process interval timers.  Subroutines
109  * here provide support for adding and subtracting timeval structures
110  * and decrementing interval timers, optionally reloading the interval
111  * timers when they expire.
112  */
113 
114 /* This function is used by clock_settime and settimeofday */
115 int
116 settime(struct timeval *tv)
117 {
118 	struct timeval delta;
119 	struct cpu_info *ci;
120 	int s;
121 
122 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
123 	s = splclock();
124 	timersub(tv, &time, &delta);
125 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
126 		splx(s);
127 		return (EPERM);
128 	}
129 #ifdef notyet
130 	if ((delta.tv_sec < 86400) && securelevel > 0) {
131 		splx(s);
132 		return (EPERM);
133 	}
134 #endif
135 	time = *tv;
136 	(void) spllowersoftclock();
137 	timeradd(&boottime, &delta, &boottime);
138 	/*
139 	 * XXXSMP
140 	 * This is wrong.  We should traverse a list of all
141 	 * CPUs and add the delta to the runtime of those
142 	 * CPUs which have a process on them.
143 	 */
144 	ci = curcpu();
145 	timeradd(&ci->ci_schedstate.spc_runtime, &delta,
146 	    &ci->ci_schedstate.spc_runtime);
147 #	if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
148 		nqnfs_lease_updatetime(delta.tv_sec);
149 #	endif
150 	splx(s);
151 	resettodr();
152 	return (0);
153 }
154 
155 /* ARGSUSED */
156 int
157 sys_clock_gettime(struct lwp *l, void *v, register_t *retval)
158 {
159 	struct sys_clock_gettime_args /* {
160 		syscallarg(clockid_t) clock_id;
161 		syscallarg(struct timespec *) tp;
162 	} */ *uap = v;
163 	clockid_t clock_id;
164 	struct timeval atv;
165 	struct timespec ats;
166 	int s;
167 
168 	clock_id = SCARG(uap, clock_id);
169 	switch (clock_id) {
170 	case CLOCK_REALTIME:
171 		microtime(&atv);
172 		TIMEVAL_TO_TIMESPEC(&atv,&ats);
173 		break;
174 	case CLOCK_MONOTONIC:
175 		/* XXX "hz" granularity */
176 		s = splclock();
177 		atv = mono_time;
178 		splx(s);
179 		TIMEVAL_TO_TIMESPEC(&atv,&ats);
180 		break;
181 	default:
182 		return (EINVAL);
183 	}
184 
185 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
186 }
187 
188 /* ARGSUSED */
189 int
190 sys_clock_settime(struct lwp *l, void *v, register_t *retval)
191 {
192 	struct sys_clock_settime_args /* {
193 		syscallarg(clockid_t) clock_id;
194 		syscallarg(const struct timespec *) tp;
195 	} */ *uap = v;
196 	struct proc *p = l->l_proc;
197 	int error;
198 
199 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
200 		return (error);
201 
202 	return (clock_settime1(SCARG(uap, clock_id), SCARG(uap, tp)));
203 }
204 
205 
206 int
207 clock_settime1(clockid_t clock_id, const struct timespec *tp)
208 {
209 	struct timespec ats;
210 	struct timeval atv;
211 	int error;
212 
213 	if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
214 		return (error);
215 
216 	switch (clock_id) {
217 	case CLOCK_REALTIME:
218 		TIMESPEC_TO_TIMEVAL(&atv, &ats);
219 		if ((error = settime(&atv)) != 0)
220 			return (error);
221 		break;
222 	case CLOCK_MONOTONIC:
223 		return (EINVAL);	/* read-only clock */
224 	default:
225 		return (EINVAL);
226 	}
227 
228 	return 0;
229 }
230 
231 int
232 sys_clock_getres(struct lwp *l, void *v, register_t *retval)
233 {
234 	struct sys_clock_getres_args /* {
235 		syscallarg(clockid_t) clock_id;
236 		syscallarg(struct timespec *) tp;
237 	} */ *uap = v;
238 	clockid_t clock_id;
239 	struct timespec ts;
240 	int error = 0;
241 
242 	clock_id = SCARG(uap, clock_id);
243 	switch (clock_id) {
244 	case CLOCK_REALTIME:
245 	case CLOCK_MONOTONIC:
246 		ts.tv_sec = 0;
247 		ts.tv_nsec = 1000000000 / hz;
248 		break;
249 	default:
250 		return (EINVAL);
251 	}
252 
253 	if (SCARG(uap, tp))
254 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
255 
256 	return error;
257 }
258 
259 /* ARGSUSED */
260 int
261 sys_nanosleep(struct lwp *l, void *v, register_t *retval)
262 {
263 	static int nanowait;
264 	struct sys_nanosleep_args/* {
265 		syscallarg(struct timespec *) rqtp;
266 		syscallarg(struct timespec *) rmtp;
267 	} */ *uap = v;
268 	struct timespec rqt;
269 	struct timespec rmt;
270 	struct timeval atv, utv;
271 	int error, s, timo;
272 
273 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
274 	if (error)
275 		return (error);
276 
277 	TIMESPEC_TO_TIMEVAL(&atv,&rqt);
278 	if (itimerfix(&atv))
279 		return (EINVAL);
280 
281 	s = splclock();
282 	timeradd(&atv,&time,&atv);
283 	timo = hzto(&atv);
284 	/*
285 	 * Avoid inadvertantly sleeping forever
286 	 */
287 	if (timo == 0)
288 		timo = 1;
289 	splx(s);
290 
291 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
292 	if (error == ERESTART)
293 		error = EINTR;
294 	if (error == EWOULDBLOCK)
295 		error = 0;
296 
297 	if (SCARG(uap, rmtp)) {
298 		int error1;
299 
300 		s = splclock();
301 		utv = time;
302 		splx(s);
303 
304 		timersub(&atv, &utv, &utv);
305 		if (utv.tv_sec < 0)
306 			timerclear(&utv);
307 
308 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
309 		error1 = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
310 			sizeof(rmt));
311 		if (error1)
312 			return (error1);
313 	}
314 
315 	return error;
316 }
317 
318 /* ARGSUSED */
319 int
320 sys_gettimeofday(struct lwp *l, void *v, register_t *retval)
321 {
322 	struct sys_gettimeofday_args /* {
323 		syscallarg(struct timeval *) tp;
324 		syscallarg(void *) tzp;		really "struct timezone *"
325 	} */ *uap = v;
326 	struct timeval atv;
327 	int error = 0;
328 	struct timezone tzfake;
329 
330 	if (SCARG(uap, tp)) {
331 		microtime(&atv);
332 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
333 		if (error)
334 			return (error);
335 	}
336 	if (SCARG(uap, tzp)) {
337 		/*
338 		 * NetBSD has no kernel notion of time zone, so we just
339 		 * fake up a timezone struct and return it if demanded.
340 		 */
341 		tzfake.tz_minuteswest = 0;
342 		tzfake.tz_dsttime = 0;
343 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
344 	}
345 	return (error);
346 }
347 
348 /* ARGSUSED */
349 int
350 sys_settimeofday(struct lwp *l, void *v, register_t *retval)
351 {
352 	struct sys_settimeofday_args /* {
353 		syscallarg(const struct timeval *) tv;
354 		syscallarg(const void *) tzp;	really "const struct timezone *"
355 	} */ *uap = v;
356 	struct proc *p = l->l_proc;
357 	int error;
358 
359 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
360 		return (error);
361 
362 	return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), p);
363 }
364 
365 int
366 settimeofday1(const struct timeval *utv, const struct timezone *utzp,
367     struct proc *p)
368 {
369 	struct timeval atv;
370 	struct timezone atz;
371 	struct timeval *tv = NULL;
372 	struct timezone *tzp = NULL;
373 	int error;
374 
375 	/* Verify all parameters before changing time. */
376 	if (utv) {
377 		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
378 			return (error);
379 		tv = &atv;
380 	}
381 	/* XXX since we don't use tz, probably no point in doing copyin. */
382 	if (utzp) {
383 		if ((error = copyin(utzp, &atz, sizeof(atz))) != 0)
384 			return (error);
385 		tzp = &atz;
386 	}
387 
388 	if (tv)
389 		if ((error = settime(tv)) != 0)
390 			return (error);
391 	/*
392 	 * NetBSD has no kernel notion of time zone, and only an
393 	 * obsolete program would try to set it, so we log a warning.
394 	 */
395 	if (tzp)
396 		log(LOG_WARNING, "pid %d attempted to set the "
397 		    "(obsolete) kernel time zone\n", p->p_pid);
398 	return (0);
399 }
400 
401 int	tickdelta;			/* current clock skew, us. per tick */
402 long	timedelta;			/* unapplied time correction, us. */
403 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
404 int	time_adjusted;			/* set if an adjustment is made */
405 
406 /* ARGSUSED */
407 int
408 sys_adjtime(struct lwp *l, void *v, register_t *retval)
409 {
410 	struct sys_adjtime_args /* {
411 		syscallarg(const struct timeval *) delta;
412 		syscallarg(struct timeval *) olddelta;
413 	} */ *uap = v;
414 	struct proc *p = l->l_proc;
415 	int error;
416 
417 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
418 		return (error);
419 
420 	return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), p);
421 }
422 
423 int
424 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
425 {
426 	struct timeval atv;
427 	long ndelta, ntickdelta, odelta;
428 	int error;
429 	int s;
430 
431 	error = copyin(delta, &atv, sizeof(struct timeval));
432 	if (error)
433 		return (error);
434 
435 	/*
436 	 * Compute the total correction and the rate at which to apply it.
437 	 * Round the adjustment down to a whole multiple of the per-tick
438 	 * delta, so that after some number of incremental changes in
439 	 * hardclock(), tickdelta will become zero, lest the correction
440 	 * overshoot and start taking us away from the desired final time.
441 	 */
442 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
443 	if (ndelta > bigadj || ndelta < -bigadj)
444 		ntickdelta = 10 * tickadj;
445 	else
446 		ntickdelta = tickadj;
447 	if (ndelta % ntickdelta)
448 		ndelta = ndelta / ntickdelta * ntickdelta;
449 
450 	/*
451 	 * To make hardclock()'s job easier, make the per-tick delta negative
452 	 * if we want time to run slower; then hardclock can simply compute
453 	 * tick + tickdelta, and subtract tickdelta from timedelta.
454 	 */
455 	if (ndelta < 0)
456 		ntickdelta = -ntickdelta;
457 	if (ndelta != 0)
458 		/* We need to save the system clock time during shutdown */
459 		time_adjusted |= 1;
460 	s = splclock();
461 	odelta = timedelta;
462 	timedelta = ndelta;
463 	tickdelta = ntickdelta;
464 	splx(s);
465 
466 	if (olddelta) {
467 		atv.tv_sec = odelta / 1000000;
468 		atv.tv_usec = odelta % 1000000;
469 		error = copyout(&atv, olddelta, sizeof(struct timeval));
470 	}
471 	return error;
472 }
473 
474 /*
475  * Interval timer support. Both the BSD getitimer() family and the POSIX
476  * timer_*() family of routines are supported.
477  *
478  * All timers are kept in an array pointed to by p_timers, which is
479  * allocated on demand - many processes don't use timers at all. The
480  * first three elements in this array are reserved for the BSD timers:
481  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
482  * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
483  * syscall.
484  *
485  * Realtime timers are kept in the ptimer structure as an absolute
486  * time; virtual time timers are kept as a linked list of deltas.
487  * Virtual time timers are processed in the hardclock() routine of
488  * kern_clock.c.  The real time timer is processed by a callout
489  * routine, called from the softclock() routine.  Since a callout may
490  * be delayed in real time due to interrupt processing in the system,
491  * it is possible for the real time timeout routine (realtimeexpire,
492  * given below), to be delayed in real time past when it is supposed
493  * to occur.  It does not suffice, therefore, to reload the real timer
494  * .it_value from the real time timers .it_interval.  Rather, we
495  * compute the next time in absolute time the timer should go off.  */
496 
497 /* Allocate a POSIX realtime timer. */
498 int
499 sys_timer_create(struct lwp *l, void *v, register_t *retval)
500 {
501 	struct sys_timer_create_args /* {
502 		syscallarg(clockid_t) clock_id;
503 		syscallarg(struct sigevent *) evp;
504 		syscallarg(timer_t *) timerid;
505 	} */ *uap = v;
506 
507 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
508 	    SCARG(uap, evp), copyin, l->l_proc);
509 }
510 
511 int
512 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
513     copyinout_t fetch_event, struct proc *p)
514 {
515 	int error;
516 	timer_t timerid;
517 	struct ptimer *pt;
518 
519 	if (id < CLOCK_REALTIME ||
520 	    id > CLOCK_PROF)
521 		return (EINVAL);
522 
523 	if (p->p_timers == NULL)
524 		timers_alloc(p);
525 
526 	/* Find a free timer slot, skipping those reserved for setitimer(). */
527 	for (timerid = 3; timerid < TIMER_MAX; timerid++)
528 		if (p->p_timers->pts_timers[timerid] == NULL)
529 			break;
530 
531 	if (timerid == TIMER_MAX)
532 		return EAGAIN;
533 
534 	pt = pool_get(&ptimer_pool, PR_WAITOK);
535 	if (evp) {
536 		if (((error =
537 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
538 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
539 			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
540 			pool_put(&ptimer_pool, pt);
541 			return (error ? error : EINVAL);
542 		}
543 	} else {
544 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
545 		switch (id) {
546 		case CLOCK_REALTIME:
547 			pt->pt_ev.sigev_signo = SIGALRM;
548 			break;
549 		case CLOCK_VIRTUAL:
550 			pt->pt_ev.sigev_signo = SIGVTALRM;
551 			break;
552 		case CLOCK_PROF:
553 			pt->pt_ev.sigev_signo = SIGPROF;
554 			break;
555 		}
556 		pt->pt_ev.sigev_value.sival_int = timerid;
557 	}
558 	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
559 	pt->pt_info.ksi_errno = 0;
560 	pt->pt_info.ksi_code = 0;
561 	pt->pt_info.ksi_pid = p->p_pid;
562 	pt->pt_info.ksi_uid = p->p_cred->p_ruid;
563 	pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
564 
565 	pt->pt_type = id;
566 	pt->pt_proc = p;
567 	pt->pt_overruns = 0;
568 	pt->pt_poverruns = 0;
569 	pt->pt_entry = timerid;
570 	timerclear(&pt->pt_time.it_value);
571 	if (id == CLOCK_REALTIME)
572 		callout_init(&pt->pt_ch);
573 	else
574 		pt->pt_active = 0;
575 
576 	p->p_timers->pts_timers[timerid] = pt;
577 
578 	return copyout(&timerid, tid, sizeof(timerid));
579 }
580 
581 /* Delete a POSIX realtime timer */
582 int
583 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
584 {
585 	struct sys_timer_delete_args /*  {
586 		syscallarg(timer_t) timerid;
587 	} */ *uap = v;
588 	struct proc *p = l->l_proc;
589 	timer_t timerid;
590 	struct ptimer *pt, *ptn;
591 	int s;
592 
593 	timerid = SCARG(uap, timerid);
594 
595 	if ((p->p_timers == NULL) ||
596 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
597 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
598 		return (EINVAL);
599 
600 	if (pt->pt_type == CLOCK_REALTIME)
601 		callout_stop(&pt->pt_ch);
602 	else if (pt->pt_active) {
603 		s = splclock();
604 		ptn = LIST_NEXT(pt, pt_list);
605 		LIST_REMOVE(pt, pt_list);
606 		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
607 			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
608 			    &ptn->pt_time.it_value);
609 		splx(s);
610 	}
611 
612 	p->p_timers->pts_timers[timerid] = NULL;
613 	pool_put(&ptimer_pool, pt);
614 
615 	return (0);
616 }
617 
618 /*
619  * Set up the given timer. The value in pt->pt_time.it_value is taken
620  * to be an absolute time for CLOCK_REALTIME timers and a relative
621  * time for virtual timers.
622  * Must be called at splclock().
623  */
624 void
625 timer_settime(struct ptimer *pt)
626 {
627 	struct ptimer *ptn, *pptn;
628 	struct ptlist *ptl;
629 
630 	if (pt->pt_type == CLOCK_REALTIME) {
631 		callout_stop(&pt->pt_ch);
632 		if (timerisset(&pt->pt_time.it_value)) {
633 			/*
634 			 * Don't need to check hzto() return value, here.
635 			 * callout_reset() does it for us.
636 			 */
637 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
638 			    realtimerexpire, pt);
639 		}
640 	} else {
641 		if (pt->pt_active) {
642 			ptn = LIST_NEXT(pt, pt_list);
643 			LIST_REMOVE(pt, pt_list);
644 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
645 				timeradd(&pt->pt_time.it_value,
646 				    &ptn->pt_time.it_value,
647 				    &ptn->pt_time.it_value);
648 		}
649 		if (timerisset(&pt->pt_time.it_value)) {
650 			if (pt->pt_type == CLOCK_VIRTUAL)
651 				ptl = &pt->pt_proc->p_timers->pts_virtual;
652 			else
653 				ptl = &pt->pt_proc->p_timers->pts_prof;
654 
655 			for (ptn = LIST_FIRST(ptl), pptn = NULL;
656 			     ptn && timercmp(&pt->pt_time.it_value,
657 				 &ptn->pt_time.it_value, >);
658 			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
659 				timersub(&pt->pt_time.it_value,
660 				    &ptn->pt_time.it_value,
661 				    &pt->pt_time.it_value);
662 
663 			if (pptn)
664 				LIST_INSERT_AFTER(pptn, pt, pt_list);
665 			else
666 				LIST_INSERT_HEAD(ptl, pt, pt_list);
667 
668 			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
669 				timersub(&ptn->pt_time.it_value,
670 				    &pt->pt_time.it_value,
671 				    &ptn->pt_time.it_value);
672 
673 			pt->pt_active = 1;
674 		} else
675 			pt->pt_active = 0;
676 	}
677 }
678 
679 void
680 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
681 {
682 	struct ptimer *ptn;
683 
684 	*aitv = pt->pt_time;
685 	if (pt->pt_type == CLOCK_REALTIME) {
686 		/*
687 		 * Convert from absolute to relative time in .it_value
688 		 * part of real time timer.  If time for real time
689 		 * timer has passed return 0, else return difference
690 		 * between current time and time for the timer to go
691 		 * off.
692 		 */
693 		if (timerisset(&aitv->it_value)) {
694 			if (timercmp(&aitv->it_value, &time, <))
695 				timerclear(&aitv->it_value);
696 			else
697 				timersub(&aitv->it_value, &time,
698 				    &aitv->it_value);
699 		}
700 	} else if (pt->pt_active) {
701 		if (pt->pt_type == CLOCK_VIRTUAL)
702 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
703 		else
704 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
705 		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
706 			timeradd(&aitv->it_value,
707 			    &ptn->pt_time.it_value, &aitv->it_value);
708 		KASSERT(ptn != NULL); /* pt should be findable on the list */
709 	} else
710 		timerclear(&aitv->it_value);
711 }
712 
713 
714 
715 /* Set and arm a POSIX realtime timer */
716 int
717 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
718 {
719 	struct sys_timer_settime_args /* {
720 		syscallarg(timer_t) timerid;
721 		syscallarg(int) flags;
722 		syscallarg(const struct itimerspec *) value;
723 		syscallarg(struct itimerspec *) ovalue;
724 	} */ *uap = v;
725 	int error;
726 	struct itimerspec value, ovalue, *ovp = NULL;
727 
728 	if ((error = copyin(SCARG(uap, value), &value,
729 	    sizeof(struct itimerspec))) != 0)
730 		return (error);
731 
732 	if (SCARG(uap, ovalue))
733 		ovp = &ovalue;
734 
735 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
736 	    SCARG(uap, flags), l->l_proc)) != 0)
737 		return error;
738 
739 	if (ovp)
740 		return copyout(&ovalue, SCARG(uap, ovalue),
741 		    sizeof(struct itimerspec));
742 	return 0;
743 }
744 
745 int
746 dotimer_settime(int timerid, struct itimerspec *value,
747     struct itimerspec *ovalue, int flags, struct proc *p)
748 {
749 	int s;
750 	struct itimerval val, oval;
751 	struct ptimer *pt;
752 
753 	if ((p->p_timers == NULL) ||
754 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
755 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
756 		return (EINVAL);
757 
758 	TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
759 	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
760 	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
761 		return (EINVAL);
762 
763 	oval = pt->pt_time;
764 	pt->pt_time = val;
765 
766 	s = splclock();
767 	/*
768 	 * If we've been passed a relative time for a realtime timer,
769 	 * convert it to absolute; if an absolute time for a virtual
770 	 * timer, convert it to relative and make sure we don't set it
771 	 * to zero, which would cancel the timer, or let it go
772 	 * negative, which would confuse the comparison tests.
773 	 */
774 	if (timerisset(&pt->pt_time.it_value)) {
775 		if (pt->pt_type == CLOCK_REALTIME) {
776 			if ((flags & TIMER_ABSTIME) == 0)
777 				timeradd(&pt->pt_time.it_value, &time,
778 				    &pt->pt_time.it_value);
779 		} else {
780 			if ((flags & TIMER_ABSTIME) != 0) {
781 				timersub(&pt->pt_time.it_value, &time,
782 				    &pt->pt_time.it_value);
783 				if (!timerisset(&pt->pt_time.it_value) ||
784 				    pt->pt_time.it_value.tv_sec < 0) {
785 					pt->pt_time.it_value.tv_sec = 0;
786 					pt->pt_time.it_value.tv_usec = 1;
787 				}
788 			}
789 		}
790 	}
791 
792 	timer_settime(pt);
793 	splx(s);
794 
795 	if (ovalue) {
796 		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
797 		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
798 	}
799 
800 	return (0);
801 }
802 
803 /* Return the time remaining until a POSIX timer fires. */
804 int
805 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
806 {
807 	struct sys_timer_gettime_args /* {
808 		syscallarg(timer_t) timerid;
809 		syscallarg(struct itimerspec *) value;
810 	} */ *uap = v;
811 	struct itimerspec its;
812 	int error;
813 
814 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
815 	    &its)) != 0)
816 		return error;
817 
818 	return copyout(&its, SCARG(uap, value), sizeof(its));
819 }
820 
821 int
822 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
823 {
824 	int s;
825 	struct ptimer *pt;
826 	struct itimerval aitv;
827 
828 	if ((p->p_timers == NULL) ||
829 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
830 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
831 		return (EINVAL);
832 
833 	s = splclock();
834 	timer_gettime(pt, &aitv);
835 	splx(s);
836 
837 	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
838 	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
839 
840 	return 0;
841 }
842 
843 /*
844  * Return the count of the number of times a periodic timer expired
845  * while a notification was already pending. The counter is reset when
846  * a timer expires and a notification can be posted.
847  */
848 int
849 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
850 {
851 	struct sys_timer_getoverrun_args /* {
852 		syscallarg(timer_t) timerid;
853 	} */ *uap = v;
854 	struct proc *p = l->l_proc;
855 	int timerid;
856 	struct ptimer *pt;
857 
858 	timerid = SCARG(uap, timerid);
859 
860 	if ((p->p_timers == NULL) ||
861 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
862 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
863 		return (EINVAL);
864 
865 	*retval = pt->pt_poverruns;
866 
867 	return (0);
868 }
869 
870 /* Glue function that triggers an upcall; called from userret(). */
871 static void
872 timerupcall(struct lwp *l, void *arg)
873 {
874 	struct ptimers *pt = (struct ptimers *)arg;
875 	unsigned int i, fired, done;
876 	extern struct pool siginfo_pool;	/* XXX Ew. */
877 
878 	KDASSERT(l->l_proc->p_sa);
879 	/* Bail out if we do not own the virtual processor */
880 	if (l->l_savp->savp_lwp != l)
881 		return ;
882 
883 	KERNEL_PROC_LOCK(l);
884 
885 	fired = pt->pts_fired;
886 	done = 0;
887 	while ((i = ffs(fired)) != 0) {
888 		siginfo_t *si;
889 		int mask = 1 << --i;
890 		int f;
891 
892 		f = l->l_flag & L_SA;
893 		l->l_flag &= ~L_SA;
894 		si = pool_get(&siginfo_pool, PR_WAITOK);
895 		si->_info = pt->pts_timers[i]->pt_info.ksi_info;
896 		if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
897 		    sizeof(*si), si) != 0) {
898 			pool_put(&siginfo_pool, si);
899 			/* XXX What do we do here?? */
900 		} else
901 			done |= mask;
902 		fired &= ~mask;
903 		l->l_flag |= f;
904 	}
905 	pt->pts_fired &= ~done;
906 	if (pt->pts_fired == 0)
907 		l->l_proc->p_userret = NULL;
908 
909 	KERNEL_PROC_UNLOCK(l);
910 }
911 
912 
913 /*
914  * Real interval timer expired:
915  * send process whose timer expired an alarm signal.
916  * If time is not set up to reload, then just return.
917  * Else compute next time timer should go off which is > current time.
918  * This is where delay in processing this timeout causes multiple
919  * SIGALRM calls to be compressed into one.
920  */
921 void
922 realtimerexpire(void *arg)
923 {
924 	struct ptimer *pt;
925 	int s;
926 
927 	pt = (struct ptimer *)arg;
928 
929 	itimerfire(pt);
930 
931 	if (!timerisset(&pt->pt_time.it_interval)) {
932 		timerclear(&pt->pt_time.it_value);
933 		return;
934 	}
935 	for (;;) {
936 		s = splclock();
937 		timeradd(&pt->pt_time.it_value,
938 		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
939 		if (timercmp(&pt->pt_time.it_value, &time, >)) {
940 			/*
941 			 * Don't need to check hzto() return value, here.
942 			 * callout_reset() does it for us.
943 			 */
944 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
945 			    realtimerexpire, pt);
946 			splx(s);
947 			return;
948 		}
949 		splx(s);
950 		pt->pt_overruns++;
951 	}
952 }
953 
954 /* BSD routine to get the value of an interval timer. */
955 /* ARGSUSED */
956 int
957 sys_getitimer(struct lwp *l, void *v, register_t *retval)
958 {
959 	struct sys_getitimer_args /* {
960 		syscallarg(int) which;
961 		syscallarg(struct itimerval *) itv;
962 	} */ *uap = v;
963 	struct proc *p = l->l_proc;
964 	struct itimerval aitv;
965 	int error;
966 
967 	error = dogetitimer(p, SCARG(uap, which), &aitv);
968 	if (error)
969 		return error;
970 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
971 }
972 
973 int
974 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
975 {
976 	int s;
977 
978 	if ((u_int)which > ITIMER_PROF)
979 		return (EINVAL);
980 
981 	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
982 		timerclear(&itvp->it_value);
983 		timerclear(&itvp->it_interval);
984 	} else {
985 		s = splclock();
986 		timer_gettime(p->p_timers->pts_timers[which], itvp);
987 		splx(s);
988 	}
989 
990 	return 0;
991 }
992 
993 /* BSD routine to set/arm an interval timer. */
994 /* ARGSUSED */
995 int
996 sys_setitimer(struct lwp *l, void *v, register_t *retval)
997 {
998 	struct sys_setitimer_args /* {
999 		syscallarg(int) which;
1000 		syscallarg(const struct itimerval *) itv;
1001 		syscallarg(struct itimerval *) oitv;
1002 	} */ *uap = v;
1003 	struct proc *p = l->l_proc;
1004 	int which = SCARG(uap, which);
1005 	struct sys_getitimer_args getargs;
1006 	const struct itimerval *itvp;
1007 	struct itimerval aitv;
1008 	int error;
1009 
1010 	if ((u_int)which > ITIMER_PROF)
1011 		return (EINVAL);
1012 	itvp = SCARG(uap, itv);
1013 	if (itvp &&
1014 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1015 		return (error);
1016 	if (SCARG(uap, oitv) != NULL) {
1017 		SCARG(&getargs, which) = which;
1018 		SCARG(&getargs, itv) = SCARG(uap, oitv);
1019 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1020 			return (error);
1021 	}
1022 	if (itvp == 0)
1023 		return (0);
1024 
1025 	return dosetitimer(p, which, &aitv);
1026 }
1027 
1028 int
1029 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1030 {
1031 	struct ptimer *pt;
1032 	int s;
1033 
1034 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1035 		return (EINVAL);
1036 
1037 	/*
1038 	 * Don't bother allocating data structures if the process just
1039 	 * wants to clear the timer.
1040 	 */
1041 	if (!timerisset(&itvp->it_value) &&
1042 	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1043 		return (0);
1044 
1045 	if (p->p_timers == NULL)
1046 		timers_alloc(p);
1047 	if (p->p_timers->pts_timers[which] == NULL) {
1048 		pt = pool_get(&ptimer_pool, PR_WAITOK);
1049 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1050 		pt->pt_ev.sigev_value.sival_int = which;
1051 		pt->pt_overruns = 0;
1052 		pt->pt_proc = p;
1053 		pt->pt_type = which;
1054 		pt->pt_entry = which;
1055 		switch (which) {
1056 		case ITIMER_REAL:
1057 			callout_init(&pt->pt_ch);
1058 			pt->pt_ev.sigev_signo = SIGALRM;
1059 			break;
1060 		case ITIMER_VIRTUAL:
1061 			pt->pt_active = 0;
1062 			pt->pt_ev.sigev_signo = SIGVTALRM;
1063 			break;
1064 		case ITIMER_PROF:
1065 			pt->pt_active = 0;
1066 			pt->pt_ev.sigev_signo = SIGPROF;
1067 			break;
1068 		}
1069 	} else
1070 		pt = p->p_timers->pts_timers[which];
1071 
1072 	pt->pt_time = *itvp;
1073 	p->p_timers->pts_timers[which] = pt;
1074 
1075 	s = splclock();
1076 	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1077 		/* Convert to absolute time */
1078 		timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
1079 	}
1080 	timer_settime(pt);
1081 	splx(s);
1082 
1083 	return (0);
1084 }
1085 
1086 /* Utility routines to manage the array of pointers to timers. */
1087 void
1088 timers_alloc(struct proc *p)
1089 {
1090 	int i;
1091 	struct ptimers *pts;
1092 
1093 	pts = malloc(sizeof (struct ptimers), M_SUBPROC, 0);
1094 	LIST_INIT(&pts->pts_virtual);
1095 	LIST_INIT(&pts->pts_prof);
1096 	for (i = 0; i < TIMER_MAX; i++)
1097 		pts->pts_timers[i] = NULL;
1098 	pts->pts_fired = 0;
1099 	p->p_timers = pts;
1100 }
1101 
1102 /*
1103  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1104  * then clean up all timers and free all the data structures. If
1105  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1106  * by timer_create(), not the BSD setitimer() timers, and only free the
1107  * structure if none of those remain.
1108  */
1109 void
1110 timers_free(struct proc *p, int which)
1111 {
1112 	int i, s;
1113 	struct ptimers *pts;
1114 	struct ptimer *pt, *ptn;
1115 	struct timeval tv;
1116 
1117 	if (p->p_timers) {
1118 		pts = p->p_timers;
1119 		if (which == TIMERS_ALL)
1120 			i = 0;
1121 		else {
1122 			s = splclock();
1123 			timerclear(&tv);
1124 			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1125 			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1126 			     ptn = LIST_NEXT(ptn, pt_list))
1127 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1128 			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1129 			if (ptn) {
1130 				timeradd(&tv, &ptn->pt_time.it_value,
1131 				    &ptn->pt_time.it_value);
1132 				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1133 				    ptn, pt_list);
1134 			}
1135 
1136 			timerclear(&tv);
1137 			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1138 			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
1139 			     ptn = LIST_NEXT(ptn, pt_list))
1140 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1141 			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1142 			if (ptn) {
1143 				timeradd(&tv, &ptn->pt_time.it_value,
1144 				    &ptn->pt_time.it_value);
1145 				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1146 				    pt_list);
1147 			}
1148 			splx(s);
1149 			i = 3;
1150 		}
1151 		for ( ; i < TIMER_MAX; i++)
1152 			if ((pt = pts->pts_timers[i]) != NULL) {
1153 				if (pt->pt_type == CLOCK_REALTIME)
1154 					callout_stop(&pt->pt_ch);
1155 				pts->pts_timers[i] = NULL;
1156 				pool_put(&ptimer_pool, pt);
1157 			}
1158 		if ((pts->pts_timers[0] == NULL) &&
1159 		    (pts->pts_timers[1] == NULL) &&
1160 		    (pts->pts_timers[2] == NULL)) {
1161 			p->p_timers = NULL;
1162 			free(pts, M_SUBPROC);
1163 		}
1164 	}
1165 }
1166 
1167 /*
1168  * Check that a proposed value to load into the .it_value or
1169  * .it_interval part of an interval timer is acceptable, and
1170  * fix it to have at least minimal value (i.e. if it is less
1171  * than the resolution of the clock, round it up.)
1172  */
1173 int
1174 itimerfix(struct timeval *tv)
1175 {
1176 
1177 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1178 		return (EINVAL);
1179 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1180 		tv->tv_usec = tick;
1181 	return (0);
1182 }
1183 
1184 /*
1185  * Decrement an interval timer by a specified number
1186  * of microseconds, which must be less than a second,
1187  * i.e. < 1000000.  If the timer expires, then reload
1188  * it.  In this case, carry over (usec - old value) to
1189  * reduce the value reloaded into the timer so that
1190  * the timer does not drift.  This routine assumes
1191  * that it is called in a context where the timers
1192  * on which it is operating cannot change in value.
1193  */
1194 int
1195 itimerdecr(struct ptimer *pt, int usec)
1196 {
1197 	struct itimerval *itp;
1198 
1199 	itp = &pt->pt_time;
1200 	if (itp->it_value.tv_usec < usec) {
1201 		if (itp->it_value.tv_sec == 0) {
1202 			/* expired, and already in next interval */
1203 			usec -= itp->it_value.tv_usec;
1204 			goto expire;
1205 		}
1206 		itp->it_value.tv_usec += 1000000;
1207 		itp->it_value.tv_sec--;
1208 	}
1209 	itp->it_value.tv_usec -= usec;
1210 	usec = 0;
1211 	if (timerisset(&itp->it_value))
1212 		return (1);
1213 	/* expired, exactly at end of interval */
1214 expire:
1215 	if (timerisset(&itp->it_interval)) {
1216 		itp->it_value = itp->it_interval;
1217 		itp->it_value.tv_usec -= usec;
1218 		if (itp->it_value.tv_usec < 0) {
1219 			itp->it_value.tv_usec += 1000000;
1220 			itp->it_value.tv_sec--;
1221 		}
1222 		timer_settime(pt);
1223 	} else
1224 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1225 	return (0);
1226 }
1227 
1228 void
1229 itimerfire(struct ptimer *pt)
1230 {
1231 	struct proc *p = pt->pt_proc;
1232 	struct sadata_vp *vp;
1233 	int s;
1234 	unsigned int i;
1235 
1236 	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1237 		/*
1238 		 * No RT signal infrastructure exists at this time;
1239 		 * just post the signal number and throw away the
1240 		 * value.
1241 		 */
1242 		if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
1243 			pt->pt_overruns++;
1244 		else {
1245 			ksiginfo_t ksi;
1246 			(void)memset(&ksi, 0, sizeof(ksi));
1247 			ksi.ksi_signo = pt->pt_ev.sigev_signo;
1248 			ksi.ksi_code = SI_TIMER;
1249 			ksi.ksi_sigval = pt->pt_ev.sigev_value;
1250 			pt->pt_poverruns = pt->pt_overruns;
1251 			pt->pt_overruns = 0;
1252 			kpsignal(p, &ksi, NULL);
1253 		}
1254 	} else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
1255 		/* Cause the process to generate an upcall when it returns. */
1256 
1257 		if (p->p_userret == NULL) {
1258 			/*
1259 			 * XXX stop signals can be processed inside tsleep,
1260 			 * which can be inside sa_yield's inner loop, which
1261 			 * makes testing for sa_idle alone insuffucent to
1262 			 * determine if we really should call setrunnable.
1263 			 */
1264 			pt->pt_poverruns = pt->pt_overruns;
1265 			pt->pt_overruns = 0;
1266 			i = 1 << pt->pt_entry;
1267 			p->p_timers->pts_fired = i;
1268 			p->p_userret = timerupcall;
1269 			p->p_userret_arg = p->p_timers;
1270 
1271 			SCHED_LOCK(s);
1272 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1273 				if (vp->savp_lwp->l_flag & L_SA_IDLE) {
1274 					vp->savp_lwp->l_flag &= ~L_SA_IDLE;
1275 					sched_wakeup(vp->savp_lwp);
1276 					break;
1277 				}
1278 			}
1279 			SCHED_UNLOCK(s);
1280 		} else if (p->p_userret == timerupcall) {
1281 			i = 1 << pt->pt_entry;
1282 			if ((p->p_timers->pts_fired & i) == 0) {
1283 				pt->pt_poverruns = pt->pt_overruns;
1284 				pt->pt_overruns = 0;
1285 				p->p_timers->pts_fired |= i;
1286 			} else
1287 				pt->pt_overruns++;
1288 		} else {
1289 			pt->pt_overruns++;
1290 			if ((p->p_flag & P_WEXIT) == 0)
1291 				printf("itimerfire(%d): overrun %d on timer %x (userret is %p)\n",
1292 				    p->p_pid, pt->pt_overruns,
1293 				    pt->pt_ev.sigev_value.sival_int,
1294 				    p->p_userret);
1295 		}
1296 	}
1297 
1298 }
1299 
1300 /*
1301  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
1302  * for usage and rationale.
1303  */
1304 int
1305 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1306 {
1307 	struct timeval tv, delta;
1308 	int s, rv = 0;
1309 
1310 	s = splclock();
1311 	tv = mono_time;
1312 	splx(s);
1313 
1314 	timersub(&tv, lasttime, &delta);
1315 
1316 	/*
1317 	 * check for 0,0 is so that the message will be seen at least once,
1318 	 * even if interval is huge.
1319 	 */
1320 	if (timercmp(&delta, mininterval, >=) ||
1321 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1322 		*lasttime = tv;
1323 		rv = 1;
1324 	}
1325 
1326 	return (rv);
1327 }
1328 
1329 /*
1330  * ppsratecheck(): packets (or events) per second limitation.
1331  */
1332 int
1333 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1334 {
1335 	struct timeval tv, delta;
1336 	int s, rv;
1337 
1338 	s = splclock();
1339 	tv = mono_time;
1340 	splx(s);
1341 
1342 	timersub(&tv, lasttime, &delta);
1343 
1344 	/*
1345 	 * check for 0,0 is so that the message will be seen at least once.
1346 	 * if more than one second have passed since the last update of
1347 	 * lasttime, reset the counter.
1348 	 *
1349 	 * we do increment *curpps even in *curpps < maxpps case, as some may
1350 	 * try to use *curpps for stat purposes as well.
1351 	 */
1352 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1353 	    delta.tv_sec >= 1) {
1354 		*lasttime = tv;
1355 		*curpps = 0;
1356 	}
1357 	if (maxpps < 0)
1358 		rv = 1;
1359 	else if (*curpps < maxpps)
1360 		rv = 1;
1361 	else
1362 		rv = 0;
1363 
1364 #if 1 /*DIAGNOSTIC?*/
1365 	/* be careful about wrap-around */
1366 	if (*curpps + 1 > *curpps)
1367 		*curpps = *curpps + 1;
1368 #else
1369 	/*
1370 	 * assume that there's not too many calls to this function.
1371 	 * not sure if the assumption holds, as it depends on *caller's*
1372 	 * behavior, not the behavior of this function.
1373 	 * IMHO it is wrong to make assumption on the caller's behavior,
1374 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1375 	 */
1376 	*curpps = *curpps + 1;
1377 #endif
1378 
1379 	return (rv);
1380 }
1381