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