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