xref: /netbsd-src/sys/kern/kern_time.c (revision df0caa2637da0538ecdf6b878c4d08e684b43d8f)
1 /*	$NetBSD: kern_time.c,v 1.90 2005/06/23 23:15:12 thorpej 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.90 2005/06/23 23:15:12 thorpej 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 	struct proc *p = l->l_proc;
507 	clockid_t id;
508 	struct sigevent *evp;
509 	struct ptimer *pt;
510 	timer_t timerid;
511 	int error;
512 
513 	id = SCARG(uap, clock_id);
514 	if (id < CLOCK_REALTIME ||
515 	    id > CLOCK_PROF)
516 		return (EINVAL);
517 
518 	if (p->p_timers == NULL)
519 		timers_alloc(p);
520 
521 	/* Find a free timer slot, skipping those reserved for setitimer(). */
522 	for (timerid = 3; timerid < TIMER_MAX; timerid++)
523 		if (p->p_timers->pts_timers[timerid] == NULL)
524 			break;
525 
526 	if (timerid == TIMER_MAX)
527 		return EAGAIN;
528 
529 	pt = pool_get(&ptimer_pool, PR_WAITOK);
530 	evp = SCARG(uap, evp);
531 	if (evp) {
532 		if (((error =
533 		    copyin(evp, &pt->pt_ev, sizeof (pt->pt_ev))) != 0) ||
534 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
535 			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
536 			pool_put(&ptimer_pool, pt);
537 			return (error ? error : EINVAL);
538 		}
539 	} else {
540 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
541 		switch (id) {
542 		case CLOCK_REALTIME:
543 			pt->pt_ev.sigev_signo = SIGALRM;
544 			break;
545 		case CLOCK_VIRTUAL:
546 			pt->pt_ev.sigev_signo = SIGVTALRM;
547 			break;
548 		case CLOCK_PROF:
549 			pt->pt_ev.sigev_signo = SIGPROF;
550 			break;
551 		}
552 		pt->pt_ev.sigev_value.sival_int = timerid;
553 	}
554 	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
555 	pt->pt_info.ksi_errno = 0;
556 	pt->pt_info.ksi_code = 0;
557 	pt->pt_info.ksi_pid = p->p_pid;
558 	pt->pt_info.ksi_uid = p->p_cred->p_ruid;
559 	pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
560 
561 	pt->pt_type = id;
562 	pt->pt_proc = p;
563 	pt->pt_overruns = 0;
564 	pt->pt_poverruns = 0;
565 	pt->pt_entry = timerid;
566 	timerclear(&pt->pt_time.it_value);
567 	if (id == CLOCK_REALTIME)
568 		callout_init(&pt->pt_ch);
569 	else
570 		pt->pt_active = 0;
571 
572 	p->p_timers->pts_timers[timerid] = pt;
573 
574 	return copyout(&timerid, SCARG(uap, timerid), sizeof(timerid));
575 }
576 
577 
578 /* Delete a POSIX realtime timer */
579 int
580 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
581 {
582 	struct sys_timer_delete_args /*  {
583 		syscallarg(timer_t) timerid;
584 	} */ *uap = v;
585 	struct proc *p = l->l_proc;
586 	timer_t timerid;
587 	struct ptimer *pt, *ptn;
588 	int s;
589 
590 	timerid = SCARG(uap, timerid);
591 
592 	if ((p->p_timers == NULL) ||
593 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
594 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
595 		return (EINVAL);
596 
597 	if (pt->pt_type == CLOCK_REALTIME)
598 		callout_stop(&pt->pt_ch);
599 	else if (pt->pt_active) {
600 		s = splclock();
601 		ptn = LIST_NEXT(pt, pt_list);
602 		LIST_REMOVE(pt, pt_list);
603 		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
604 			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
605 			    &ptn->pt_time.it_value);
606 		splx(s);
607 	}
608 
609 	p->p_timers->pts_timers[timerid] = NULL;
610 	pool_put(&ptimer_pool, pt);
611 
612 	return (0);
613 }
614 
615 /*
616  * Set up the given timer. The value in pt->pt_time.it_value is taken
617  * to be an absolute time for CLOCK_REALTIME timers and a relative
618  * time for virtual timers.
619  * Must be called at splclock().
620  */
621 void
622 timer_settime(struct ptimer *pt)
623 {
624 	struct ptimer *ptn, *pptn;
625 	struct ptlist *ptl;
626 
627 	if (pt->pt_type == CLOCK_REALTIME) {
628 		callout_stop(&pt->pt_ch);
629 		if (timerisset(&pt->pt_time.it_value)) {
630 			/*
631 			 * Don't need to check hzto() return value, here.
632 			 * callout_reset() does it for us.
633 			 */
634 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
635 			    realtimerexpire, pt);
636 		}
637 	} else {
638 		if (pt->pt_active) {
639 			ptn = LIST_NEXT(pt, pt_list);
640 			LIST_REMOVE(pt, pt_list);
641 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
642 				timeradd(&pt->pt_time.it_value,
643 				    &ptn->pt_time.it_value,
644 				    &ptn->pt_time.it_value);
645 		}
646 		if (timerisset(&pt->pt_time.it_value)) {
647 			if (pt->pt_type == CLOCK_VIRTUAL)
648 				ptl = &pt->pt_proc->p_timers->pts_virtual;
649 			else
650 				ptl = &pt->pt_proc->p_timers->pts_prof;
651 
652 			for (ptn = LIST_FIRST(ptl), pptn = NULL;
653 			     ptn && timercmp(&pt->pt_time.it_value,
654 				 &ptn->pt_time.it_value, >);
655 			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
656 				timersub(&pt->pt_time.it_value,
657 				    &ptn->pt_time.it_value,
658 				    &pt->pt_time.it_value);
659 
660 			if (pptn)
661 				LIST_INSERT_AFTER(pptn, pt, pt_list);
662 			else
663 				LIST_INSERT_HEAD(ptl, pt, pt_list);
664 
665 			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
666 				timersub(&ptn->pt_time.it_value,
667 				    &pt->pt_time.it_value,
668 				    &ptn->pt_time.it_value);
669 
670 			pt->pt_active = 1;
671 		} else
672 			pt->pt_active = 0;
673 	}
674 }
675 
676 void
677 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
678 {
679 	struct ptimer *ptn;
680 
681 	*aitv = pt->pt_time;
682 	if (pt->pt_type == CLOCK_REALTIME) {
683 		/*
684 		 * Convert from absolute to relative time in .it_value
685 		 * part of real time timer.  If time for real time
686 		 * timer has passed return 0, else return difference
687 		 * between current time and time for the timer to go
688 		 * off.
689 		 */
690 		if (timerisset(&aitv->it_value)) {
691 			if (timercmp(&aitv->it_value, &time, <))
692 				timerclear(&aitv->it_value);
693 			else
694 				timersub(&aitv->it_value, &time,
695 				    &aitv->it_value);
696 		}
697 	} else if (pt->pt_active) {
698 		if (pt->pt_type == CLOCK_VIRTUAL)
699 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
700 		else
701 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
702 		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
703 			timeradd(&aitv->it_value,
704 			    &ptn->pt_time.it_value, &aitv->it_value);
705 		KASSERT(ptn != NULL); /* pt should be findable on the list */
706 	} else
707 		timerclear(&aitv->it_value);
708 }
709 
710 
711 
712 /* Set and arm a POSIX realtime timer */
713 int
714 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
715 {
716 	struct sys_timer_settime_args /* {
717 		syscallarg(timer_t) timerid;
718 		syscallarg(int) flags;
719 		syscallarg(const struct itimerspec *) value;
720 		syscallarg(struct itimerspec *) ovalue;
721 	} */ *uap = v;
722 	struct proc *p = l->l_proc;
723 	int error, s, timerid;
724 	struct itimerval val, oval;
725 	struct itimerspec value, ovalue;
726 	struct ptimer *pt;
727 
728 	timerid = SCARG(uap, timerid);
729 
730 	if ((p->p_timers == NULL) ||
731 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
732 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
733 		return (EINVAL);
734 
735 	if ((error = copyin(SCARG(uap, value), &value,
736 	    sizeof(struct itimerspec))) != 0)
737 		return (error);
738 
739 	TIMESPEC_TO_TIMEVAL(&val.it_value, &value.it_value);
740 	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value.it_interval);
741 	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
742 		return (EINVAL);
743 
744 	oval = pt->pt_time;
745 	pt->pt_time = val;
746 
747 	s = splclock();
748 	/*
749 	 * If we've been passed a relative time for a realtime timer,
750 	 * convert it to absolute; if an absolute time for a virtual
751 	 * timer, convert it to relative and make sure we don't set it
752 	 * to zero, which would cancel the timer, or let it go
753 	 * negative, which would confuse the comparison tests.
754 	 */
755 	if (timerisset(&pt->pt_time.it_value)) {
756 		if (pt->pt_type == CLOCK_REALTIME) {
757 			if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0)
758 				timeradd(&pt->pt_time.it_value, &time,
759 				    &pt->pt_time.it_value);
760 		} else {
761 			if ((SCARG(uap, flags) & TIMER_ABSTIME) != 0) {
762 				timersub(&pt->pt_time.it_value, &time,
763 				    &pt->pt_time.it_value);
764 				if (!timerisset(&pt->pt_time.it_value) ||
765 				    pt->pt_time.it_value.tv_sec < 0) {
766 					pt->pt_time.it_value.tv_sec = 0;
767 					pt->pt_time.it_value.tv_usec = 1;
768 				}
769 			}
770 		}
771 	}
772 
773 	timer_settime(pt);
774 	splx(s);
775 
776 	if (SCARG(uap, ovalue)) {
777 		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue.it_value);
778 		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue.it_interval);
779 		return copyout(&ovalue, SCARG(uap, ovalue),
780 		    sizeof(struct itimerspec));
781 	}
782 
783 	return (0);
784 }
785 
786 /* Return the time remaining until a POSIX timer fires. */
787 int
788 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
789 {
790 	struct sys_timer_gettime_args /* {
791 		syscallarg(timer_t) timerid;
792 		syscallarg(struct itimerspec *) value;
793 	} */ *uap = v;
794 	struct itimerval aitv;
795 	struct itimerspec its;
796 	struct proc *p = l->l_proc;
797 	int s, timerid;
798 	struct ptimer *pt;
799 
800 	timerid = SCARG(uap, timerid);
801 
802 	if ((p->p_timers == NULL) ||
803 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
804 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
805 		return (EINVAL);
806 
807 	s = splclock();
808 	timer_gettime(pt, &aitv);
809 	splx(s);
810 
811 	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its.it_interval);
812 	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its.it_value);
813 
814 	return copyout(&its, SCARG(uap, value), sizeof(its));
815 }
816 
817 /*
818  * Return the count of the number of times a periodic timer expired
819  * while a notification was already pending. The counter is reset when
820  * a timer expires and a notification can be posted.
821  */
822 int
823 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
824 {
825 	struct sys_timer_getoverrun_args /* {
826 		syscallarg(timer_t) timerid;
827 	} */ *uap = v;
828 	struct proc *p = l->l_proc;
829 	int timerid;
830 	struct ptimer *pt;
831 
832 	timerid = SCARG(uap, timerid);
833 
834 	if ((p->p_timers == NULL) ||
835 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
836 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
837 		return (EINVAL);
838 
839 	*retval = pt->pt_poverruns;
840 
841 	return (0);
842 }
843 
844 /* Glue function that triggers an upcall; called from userret(). */
845 static void
846 timerupcall(struct lwp *l, void *arg)
847 {
848 	struct ptimers *pt = (struct ptimers *)arg;
849 	unsigned int i, fired, done;
850 	extern struct pool siginfo_pool;	/* XXX Ew. */
851 
852 	KDASSERT(l->l_proc->p_sa);
853 	/* Bail out if we do not own the virtual processor */
854 	if (l->l_savp->savp_lwp != l)
855 		return ;
856 
857 	KERNEL_PROC_LOCK(l);
858 
859 	fired = pt->pts_fired;
860 	done = 0;
861 	while ((i = ffs(fired)) != 0) {
862 		siginfo_t *si;
863 		int mask = 1 << --i;
864 		int f;
865 
866 		f = l->l_flag & L_SA;
867 		l->l_flag &= ~L_SA;
868 		si = pool_get(&siginfo_pool, PR_WAITOK);
869 		si->_info = pt->pts_timers[i]->pt_info.ksi_info;
870 		if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
871 		    sizeof(*si), si) != 0) {
872 			pool_put(&siginfo_pool, si);
873 			/* XXX What do we do here?? */
874 		} else
875 			done |= mask;
876 		fired &= ~mask;
877 		l->l_flag |= f;
878 	}
879 	pt->pts_fired &= ~done;
880 	if (pt->pts_fired == 0)
881 		l->l_proc->p_userret = NULL;
882 
883 	KERNEL_PROC_UNLOCK(l);
884 }
885 
886 
887 /*
888  * Real interval timer expired:
889  * send process whose timer expired an alarm signal.
890  * If time is not set up to reload, then just return.
891  * Else compute next time timer should go off which is > current time.
892  * This is where delay in processing this timeout causes multiple
893  * SIGALRM calls to be compressed into one.
894  */
895 void
896 realtimerexpire(void *arg)
897 {
898 	struct ptimer *pt;
899 	int s;
900 
901 	pt = (struct ptimer *)arg;
902 
903 	itimerfire(pt);
904 
905 	if (!timerisset(&pt->pt_time.it_interval)) {
906 		timerclear(&pt->pt_time.it_value);
907 		return;
908 	}
909 	for (;;) {
910 		s = splclock();
911 		timeradd(&pt->pt_time.it_value,
912 		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
913 		if (timercmp(&pt->pt_time.it_value, &time, >)) {
914 			/*
915 			 * Don't need to check hzto() return value, here.
916 			 * callout_reset() does it for us.
917 			 */
918 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
919 			    realtimerexpire, pt);
920 			splx(s);
921 			return;
922 		}
923 		splx(s);
924 		pt->pt_overruns++;
925 	}
926 }
927 
928 /* BSD routine to get the value of an interval timer. */
929 /* ARGSUSED */
930 int
931 sys_getitimer(struct lwp *l, void *v, register_t *retval)
932 {
933 	struct sys_getitimer_args /* {
934 		syscallarg(int) which;
935 		syscallarg(struct itimerval *) itv;
936 	} */ *uap = v;
937 	struct proc *p = l->l_proc;
938 	struct itimerval aitv;
939 	int s, which;
940 
941 	which = SCARG(uap, which);
942 
943 	if ((u_int)which > ITIMER_PROF)
944 		return (EINVAL);
945 
946 	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
947 		timerclear(&aitv.it_value);
948 		timerclear(&aitv.it_interval);
949 	} else {
950 		s = splclock();
951 		timer_gettime(p->p_timers->pts_timers[which], &aitv);
952 		splx(s);
953 	}
954 
955 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
956 
957 }
958 
959 /* BSD routine to set/arm an interval timer. */
960 /* ARGSUSED */
961 int
962 sys_setitimer(struct lwp *l, void *v, register_t *retval)
963 {
964 	struct sys_setitimer_args /* {
965 		syscallarg(int) which;
966 		syscallarg(const struct itimerval *) itv;
967 		syscallarg(struct itimerval *) oitv;
968 	} */ *uap = v;
969 	struct proc *p = l->l_proc;
970 	int which = SCARG(uap, which);
971 	struct sys_getitimer_args getargs;
972 	struct itimerval aitv;
973 	const struct itimerval *itvp;
974 	struct ptimer *pt;
975 	int s, error;
976 
977 	if ((u_int)which > ITIMER_PROF)
978 		return (EINVAL);
979 	itvp = SCARG(uap, itv);
980 	if (itvp &&
981 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
982 		return (error);
983 	if (SCARG(uap, oitv) != NULL) {
984 		SCARG(&getargs, which) = which;
985 		SCARG(&getargs, itv) = SCARG(uap, oitv);
986 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
987 			return (error);
988 	}
989 	if (itvp == 0)
990 		return (0);
991 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
992 		return (EINVAL);
993 
994 	/*
995 	 * Don't bother allocating data structures if the process just
996 	 * wants to clear the timer.
997 	 */
998 	if (!timerisset(&aitv.it_value) &&
999 	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1000 		return (0);
1001 
1002 	if (p->p_timers == NULL)
1003 		timers_alloc(p);
1004 	if (p->p_timers->pts_timers[which] == NULL) {
1005 		pt = pool_get(&ptimer_pool, PR_WAITOK);
1006 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1007 		pt->pt_ev.sigev_value.sival_int = which;
1008 		pt->pt_overruns = 0;
1009 		pt->pt_proc = p;
1010 		pt->pt_type = which;
1011 		pt->pt_entry = which;
1012 		switch (which) {
1013 		case ITIMER_REAL:
1014 			callout_init(&pt->pt_ch);
1015 			pt->pt_ev.sigev_signo = SIGALRM;
1016 			break;
1017 		case ITIMER_VIRTUAL:
1018 			pt->pt_active = 0;
1019 			pt->pt_ev.sigev_signo = SIGVTALRM;
1020 			break;
1021 		case ITIMER_PROF:
1022 			pt->pt_active = 0;
1023 			pt->pt_ev.sigev_signo = SIGPROF;
1024 			break;
1025 		}
1026 	} else
1027 		pt = p->p_timers->pts_timers[which];
1028 
1029 	pt->pt_time = aitv;
1030 	p->p_timers->pts_timers[which] = pt;
1031 
1032 	s = splclock();
1033 	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1034 		/* Convert to absolute time */
1035 		timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
1036 	}
1037 	timer_settime(pt);
1038 	splx(s);
1039 
1040 	return (0);
1041 }
1042 
1043 /* Utility routines to manage the array of pointers to timers. */
1044 void
1045 timers_alloc(struct proc *p)
1046 {
1047 	int i;
1048 	struct ptimers *pts;
1049 
1050 	pts = malloc(sizeof (struct ptimers), M_SUBPROC, 0);
1051 	LIST_INIT(&pts->pts_virtual);
1052 	LIST_INIT(&pts->pts_prof);
1053 	for (i = 0; i < TIMER_MAX; i++)
1054 		pts->pts_timers[i] = NULL;
1055 	pts->pts_fired = 0;
1056 	p->p_timers = pts;
1057 }
1058 
1059 /*
1060  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1061  * then clean up all timers and free all the data structures. If
1062  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1063  * by timer_create(), not the BSD setitimer() timers, and only free the
1064  * structure if none of those remain.
1065  */
1066 void
1067 timers_free(struct proc *p, int which)
1068 {
1069 	int i, s;
1070 	struct ptimers *pts;
1071 	struct ptimer *pt, *ptn;
1072 	struct timeval tv;
1073 
1074 	if (p->p_timers) {
1075 		pts = p->p_timers;
1076 		if (which == TIMERS_ALL)
1077 			i = 0;
1078 		else {
1079 			s = splclock();
1080 			timerclear(&tv);
1081 			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1082 			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1083 			     ptn = LIST_NEXT(ptn, pt_list))
1084 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1085 			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1086 			if (ptn) {
1087 				timeradd(&tv, &ptn->pt_time.it_value,
1088 				    &ptn->pt_time.it_value);
1089 				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1090 				    ptn, pt_list);
1091 			}
1092 
1093 			timerclear(&tv);
1094 			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1095 			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
1096 			     ptn = LIST_NEXT(ptn, pt_list))
1097 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1098 			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1099 			if (ptn) {
1100 				timeradd(&tv, &ptn->pt_time.it_value,
1101 				    &ptn->pt_time.it_value);
1102 				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1103 				    pt_list);
1104 			}
1105 			splx(s);
1106 			i = 3;
1107 		}
1108 		for ( ; i < TIMER_MAX; i++)
1109 			if ((pt = pts->pts_timers[i]) != NULL) {
1110 				if (pt->pt_type == CLOCK_REALTIME)
1111 					callout_stop(&pt->pt_ch);
1112 				pts->pts_timers[i] = NULL;
1113 				pool_put(&ptimer_pool, pt);
1114 			}
1115 		if ((pts->pts_timers[0] == NULL) &&
1116 		    (pts->pts_timers[1] == NULL) &&
1117 		    (pts->pts_timers[2] == NULL)) {
1118 			p->p_timers = NULL;
1119 			free(pts, M_SUBPROC);
1120 		}
1121 	}
1122 }
1123 
1124 /*
1125  * Check that a proposed value to load into the .it_value or
1126  * .it_interval part of an interval timer is acceptable, and
1127  * fix it to have at least minimal value (i.e. if it is less
1128  * than the resolution of the clock, round it up.)
1129  */
1130 int
1131 itimerfix(struct timeval *tv)
1132 {
1133 
1134 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1135 		return (EINVAL);
1136 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1137 		tv->tv_usec = tick;
1138 	return (0);
1139 }
1140 
1141 /*
1142  * Decrement an interval timer by a specified number
1143  * of microseconds, which must be less than a second,
1144  * i.e. < 1000000.  If the timer expires, then reload
1145  * it.  In this case, carry over (usec - old value) to
1146  * reduce the value reloaded into the timer so that
1147  * the timer does not drift.  This routine assumes
1148  * that it is called in a context where the timers
1149  * on which it is operating cannot change in value.
1150  */
1151 int
1152 itimerdecr(struct ptimer *pt, int usec)
1153 {
1154 	struct itimerval *itp;
1155 
1156 	itp = &pt->pt_time;
1157 	if (itp->it_value.tv_usec < usec) {
1158 		if (itp->it_value.tv_sec == 0) {
1159 			/* expired, and already in next interval */
1160 			usec -= itp->it_value.tv_usec;
1161 			goto expire;
1162 		}
1163 		itp->it_value.tv_usec += 1000000;
1164 		itp->it_value.tv_sec--;
1165 	}
1166 	itp->it_value.tv_usec -= usec;
1167 	usec = 0;
1168 	if (timerisset(&itp->it_value))
1169 		return (1);
1170 	/* expired, exactly at end of interval */
1171 expire:
1172 	if (timerisset(&itp->it_interval)) {
1173 		itp->it_value = itp->it_interval;
1174 		itp->it_value.tv_usec -= usec;
1175 		if (itp->it_value.tv_usec < 0) {
1176 			itp->it_value.tv_usec += 1000000;
1177 			itp->it_value.tv_sec--;
1178 		}
1179 		timer_settime(pt);
1180 	} else
1181 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1182 	return (0);
1183 }
1184 
1185 void
1186 itimerfire(struct ptimer *pt)
1187 {
1188 	struct proc *p = pt->pt_proc;
1189 	struct sadata_vp *vp;
1190 	int s;
1191 	unsigned int i;
1192 
1193 	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1194 		/*
1195 		 * No RT signal infrastructure exists at this time;
1196 		 * just post the signal number and throw away the
1197 		 * value.
1198 		 */
1199 		if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
1200 			pt->pt_overruns++;
1201 		else {
1202 			ksiginfo_t ksi;
1203 			(void)memset(&ksi, 0, sizeof(ksi));
1204 			ksi.ksi_signo = pt->pt_ev.sigev_signo;
1205 			ksi.ksi_code = SI_TIMER;
1206 			ksi.ksi_sigval = pt->pt_ev.sigev_value;
1207 			pt->pt_poverruns = pt->pt_overruns;
1208 			pt->pt_overruns = 0;
1209 			kpsignal(p, &ksi, NULL);
1210 		}
1211 	} else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
1212 		/* Cause the process to generate an upcall when it returns. */
1213 
1214 		if (p->p_userret == NULL) {
1215 			/*
1216 			 * XXX stop signals can be processed inside tsleep,
1217 			 * which can be inside sa_yield's inner loop, which
1218 			 * makes testing for sa_idle alone insuffucent to
1219 			 * determine if we really should call setrunnable.
1220 			 */
1221 			pt->pt_poverruns = pt->pt_overruns;
1222 			pt->pt_overruns = 0;
1223 			i = 1 << pt->pt_entry;
1224 			p->p_timers->pts_fired = i;
1225 			p->p_userret = timerupcall;
1226 			p->p_userret_arg = p->p_timers;
1227 
1228 			SCHED_LOCK(s);
1229 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1230 				if (vp->savp_lwp->l_flag & L_SA_IDLE) {
1231 					vp->savp_lwp->l_flag &= ~L_SA_IDLE;
1232 					sched_wakeup(vp->savp_lwp);
1233 					break;
1234 				}
1235 			}
1236 			SCHED_UNLOCK(s);
1237 		} else if (p->p_userret == timerupcall) {
1238 			i = 1 << pt->pt_entry;
1239 			if ((p->p_timers->pts_fired & i) == 0) {
1240 				pt->pt_poverruns = pt->pt_overruns;
1241 				pt->pt_overruns = 0;
1242 				p->p_timers->pts_fired |= i;
1243 			} else
1244 				pt->pt_overruns++;
1245 		} else {
1246 			pt->pt_overruns++;
1247 			if ((p->p_flag & P_WEXIT) == 0)
1248 				printf("itimerfire(%d): overrun %d on timer %x (userret is %p)\n",
1249 				    p->p_pid, pt->pt_overruns,
1250 				    pt->pt_ev.sigev_value.sival_int,
1251 				    p->p_userret);
1252 		}
1253 	}
1254 
1255 }
1256 
1257 /*
1258  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
1259  * for usage and rationale.
1260  */
1261 int
1262 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1263 {
1264 	struct timeval tv, delta;
1265 	int s, rv = 0;
1266 
1267 	s = splclock();
1268 	tv = mono_time;
1269 	splx(s);
1270 
1271 	timersub(&tv, lasttime, &delta);
1272 
1273 	/*
1274 	 * check for 0,0 is so that the message will be seen at least once,
1275 	 * even if interval is huge.
1276 	 */
1277 	if (timercmp(&delta, mininterval, >=) ||
1278 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1279 		*lasttime = tv;
1280 		rv = 1;
1281 	}
1282 
1283 	return (rv);
1284 }
1285 
1286 /*
1287  * ppsratecheck(): packets (or events) per second limitation.
1288  */
1289 int
1290 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1291 {
1292 	struct timeval tv, delta;
1293 	int s, rv;
1294 
1295 	s = splclock();
1296 	tv = mono_time;
1297 	splx(s);
1298 
1299 	timersub(&tv, lasttime, &delta);
1300 
1301 	/*
1302 	 * check for 0,0 is so that the message will be seen at least once.
1303 	 * if more than one second have passed since the last update of
1304 	 * lasttime, reset the counter.
1305 	 *
1306 	 * we do increment *curpps even in *curpps < maxpps case, as some may
1307 	 * try to use *curpps for stat purposes as well.
1308 	 */
1309 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1310 	    delta.tv_sec >= 1) {
1311 		*lasttime = tv;
1312 		*curpps = 0;
1313 	}
1314 	if (maxpps < 0)
1315 		rv = 1;
1316 	else if (*curpps < maxpps)
1317 		rv = 1;
1318 	else
1319 		rv = 0;
1320 
1321 #if 1 /*DIAGNOSTIC?*/
1322 	/* be careful about wrap-around */
1323 	if (*curpps + 1 > *curpps)
1324 		*curpps = *curpps + 1;
1325 #else
1326 	/*
1327 	 * assume that there's not too many calls to this function.
1328 	 * not sure if the assumption holds, as it depends on *caller's*
1329 	 * behavior, not the behavior of this function.
1330 	 * IMHO it is wrong to make assumption on the caller's behavior,
1331 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1332 	 */
1333 	*curpps = *curpps + 1;
1334 #endif
1335 
1336 	return (rv);
1337 }
1338