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