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