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