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