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