kern_time.c revision 1.146.2.4 1 /* $NetBSD: kern_time.c,v 1.146.2.4 2008/06/23 04:31:51 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.4 2008/06/23 04:31:51 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_sa->sa_mutex);
931 mutex_enter(p->p_lock);
932
933 fired = pt->pts_fired;
934 done = 0;
935 while ((i = ffs(fired)) != 0) {
936 siginfo_t *si;
937 int mask = 1 << --i;
938 int f;
939
940 lwp_lock(l);
941 f = l->l_flag & LW_SA;
942 l->l_flag &= ~LW_SA;
943 lwp_unlock(l);
944 si = siginfo_alloc(PR_WAITOK);
945 si->_info = pt->pts_timers[i]->pt_info.ksi_info;
946 if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
947 sizeof(*si), si, siginfo_free) != 0) {
948 siginfo_free(si);
949 /* XXX What do we do here?? */
950 } else
951 done |= mask;
952 fired &= ~mask;
953 lwp_lock(l);
954 l->l_flag |= f;
955 lwp_unlock(l);
956 }
957 pt->pts_fired &= ~done;
958 if (pt->pts_fired == 0)
959 l->l_proc->p_timerpend = 0;
960
961 mutex_exit(p->p_lock);
962 mutex_exit(&p->p_sa->sa_mutex);
963 }
964
965 /*
966 * Real interval timer expired:
967 * send process whose timer expired an alarm signal.
968 * If time is not set up to reload, then just return.
969 * Else compute next time timer should go off which is > current time.
970 * This is where delay in processing this timeout causes multiple
971 * SIGALRM calls to be compressed into one.
972 */
973 void
974 realtimerexpire(void *arg)
975 {
976 uint64_t last_val, next_val, interval, now_ms;
977 struct timeval now, next;
978 struct ptimer *pt;
979 int backwards;
980
981 pt = arg;
982
983 mutex_spin_enter(&timer_lock);
984 itimerfire(pt);
985
986 if (!timerisset(&pt->pt_time.it_interval)) {
987 timerclear(&pt->pt_time.it_value);
988 mutex_spin_exit(&timer_lock);
989 return;
990 }
991
992 getmicrotime(&now);
993 backwards = (timercmp(&pt->pt_time.it_value, &now, >));
994 timeradd(&pt->pt_time.it_value, &pt->pt_time.it_interval, &next);
995 /* Handle the easy case of non-overflown timers first. */
996 if (!backwards && timercmp(&next, &now, >)) {
997 pt->pt_time.it_value = next;
998 } else {
999 #define TV2MS(x) (((uint64_t)(x)->tv_sec) * 1000000 + (x)->tv_usec)
1000 now_ms = TV2MS(&now);
1001 last_val = TV2MS(&pt->pt_time.it_value);
1002 interval = TV2MS(&pt->pt_time.it_interval);
1003 #undef TV2MS
1004
1005 next_val = now_ms +
1006 (now_ms - last_val + interval - 1) % interval;
1007
1008 if (backwards)
1009 next_val += interval;
1010 else
1011 pt->pt_overruns += (now_ms - last_val) / interval;
1012
1013 pt->pt_time.it_value.tv_sec = next_val / 1000000;
1014 pt->pt_time.it_value.tv_usec = next_val % 1000000;
1015 }
1016
1017 /*
1018 * Don't need to check hzto() return value, here.
1019 * callout_reset() does it for us.
1020 */
1021 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
1022 realtimerexpire, pt);
1023 mutex_spin_exit(&timer_lock);
1024 }
1025
1026 /* BSD routine to get the value of an interval timer. */
1027 /* ARGSUSED */
1028 int
1029 sys_getitimer(struct lwp *l, const struct sys_getitimer_args *uap,
1030 register_t *retval)
1031 {
1032 /* {
1033 syscallarg(int) which;
1034 syscallarg(struct itimerval *) itv;
1035 } */
1036 struct proc *p = l->l_proc;
1037 struct itimerval aitv;
1038 int error;
1039
1040 error = dogetitimer(p, SCARG(uap, which), &aitv);
1041 if (error)
1042 return error;
1043 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
1044 }
1045
1046 int
1047 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
1048 {
1049 struct ptimers *pts;
1050 struct ptimer *pt;
1051
1052 if ((u_int)which > ITIMER_PROF)
1053 return (EINVAL);
1054
1055 mutex_spin_enter(&timer_lock);
1056 pts = p->p_timers;
1057 if (pts == NULL || (pt = pts->pts_timers[which]) == NULL) {
1058 timerclear(&itvp->it_value);
1059 timerclear(&itvp->it_interval);
1060 } else
1061 timer_gettime(pt, itvp);
1062 mutex_spin_exit(&timer_lock);
1063
1064 return 0;
1065 }
1066
1067 /* BSD routine to set/arm an interval timer. */
1068 /* ARGSUSED */
1069 int
1070 sys_setitimer(struct lwp *l, const struct sys_setitimer_args *uap,
1071 register_t *retval)
1072 {
1073 /* {
1074 syscallarg(int) which;
1075 syscallarg(const struct itimerval *) itv;
1076 syscallarg(struct itimerval *) oitv;
1077 } */
1078 struct proc *p = l->l_proc;
1079 int which = SCARG(uap, which);
1080 struct sys_getitimer_args getargs;
1081 const struct itimerval *itvp;
1082 struct itimerval aitv;
1083 int error;
1084
1085 if ((u_int)which > ITIMER_PROF)
1086 return (EINVAL);
1087 itvp = SCARG(uap, itv);
1088 if (itvp &&
1089 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1090 return (error);
1091 if (SCARG(uap, oitv) != NULL) {
1092 SCARG(&getargs, which) = which;
1093 SCARG(&getargs, itv) = SCARG(uap, oitv);
1094 if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1095 return (error);
1096 }
1097 if (itvp == 0)
1098 return (0);
1099
1100 return dosetitimer(p, which, &aitv);
1101 }
1102
1103 int
1104 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1105 {
1106 struct timeval now;
1107 struct ptimers *pts;
1108 struct ptimer *pt, *spare;
1109
1110 if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1111 return (EINVAL);
1112
1113 /*
1114 * Don't bother allocating data structures if the process just
1115 * wants to clear the timer.
1116 */
1117 spare = NULL;
1118 pts = p->p_timers;
1119 retry:
1120 if (!timerisset(&itvp->it_value) && (pts == NULL ||
1121 pts->pts_timers[which] == NULL))
1122 return (0);
1123 if (pts == NULL)
1124 pts = timers_alloc(p);
1125 mutex_spin_enter(&timer_lock);
1126 pt = pts->pts_timers[which];
1127 if (pt == NULL) {
1128 if (spare == NULL) {
1129 mutex_spin_exit(&timer_lock);
1130 spare = pool_get(&ptimer_pool, PR_WAITOK);
1131 goto retry;
1132 }
1133 pt = spare;
1134 spare = NULL;
1135 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1136 pt->pt_ev.sigev_value.sival_int = which;
1137 pt->pt_overruns = 0;
1138 pt->pt_proc = p;
1139 pt->pt_type = which;
1140 pt->pt_entry = which;
1141 pt->pt_active = 0;
1142 pt->pt_queued = false;
1143 callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
1144 switch (which) {
1145 case ITIMER_REAL:
1146 pt->pt_ev.sigev_signo = SIGALRM;
1147 break;
1148 case ITIMER_VIRTUAL:
1149 pt->pt_ev.sigev_signo = SIGVTALRM;
1150 break;
1151 case ITIMER_PROF:
1152 pt->pt_ev.sigev_signo = SIGPROF;
1153 break;
1154 }
1155 pts->pts_timers[which] = pt;
1156 }
1157 pt->pt_time = *itvp;
1158
1159 if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1160 /* Convert to absolute time */
1161 /* XXX need to wrap in splclock for timecounters case? */
1162 getmicrotime(&now);
1163 timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
1164 }
1165 timer_settime(pt);
1166 mutex_spin_exit(&timer_lock);
1167 if (spare != NULL)
1168 pool_put(&ptimer_pool, spare);
1169
1170 return (0);
1171 }
1172
1173 /* Utility routines to manage the array of pointers to timers. */
1174 struct ptimers *
1175 timers_alloc(struct proc *p)
1176 {
1177 struct ptimers *pts;
1178 int i;
1179
1180 pts = pool_get(&ptimers_pool, PR_WAITOK);
1181 LIST_INIT(&pts->pts_virtual);
1182 LIST_INIT(&pts->pts_prof);
1183 for (i = 0; i < TIMER_MAX; i++)
1184 pts->pts_timers[i] = NULL;
1185 pts->pts_fired = 0;
1186 mutex_spin_enter(&timer_lock);
1187 if (p->p_timers == NULL) {
1188 p->p_timers = pts;
1189 mutex_spin_exit(&timer_lock);
1190 return pts;
1191 }
1192 mutex_spin_exit(&timer_lock);
1193 pool_put(&ptimers_pool, pts);
1194 return p->p_timers;
1195 }
1196
1197 /*
1198 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1199 * then clean up all timers and free all the data structures. If
1200 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1201 * by timer_create(), not the BSD setitimer() timers, and only free the
1202 * structure if none of those remain.
1203 */
1204 void
1205 timers_free(struct proc *p, int which)
1206 {
1207 struct ptimers *pts;
1208 struct ptimer *ptn;
1209 struct timeval tv;
1210 int i;
1211
1212 if (p->p_timers == NULL)
1213 return;
1214
1215 pts = p->p_timers;
1216 mutex_spin_enter(&timer_lock);
1217 if (which == TIMERS_ALL) {
1218 p->p_timers = NULL;
1219 i = 0;
1220 } else {
1221 timerclear(&tv);
1222 for (ptn = LIST_FIRST(&pts->pts_virtual);
1223 ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1224 ptn = LIST_NEXT(ptn, pt_list))
1225 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1226 LIST_FIRST(&pts->pts_virtual) = NULL;
1227 if (ptn) {
1228 timeradd(&tv, &ptn->pt_time.it_value,
1229 &ptn->pt_time.it_value);
1230 LIST_INSERT_HEAD(&pts->pts_virtual, ptn, pt_list);
1231 }
1232 timerclear(&tv);
1233 for (ptn = LIST_FIRST(&pts->pts_prof);
1234 ptn && ptn != pts->pts_timers[ITIMER_PROF];
1235 ptn = LIST_NEXT(ptn, pt_list))
1236 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1237 LIST_FIRST(&pts->pts_prof) = NULL;
1238 if (ptn) {
1239 timeradd(&tv, &ptn->pt_time.it_value,
1240 &ptn->pt_time.it_value);
1241 LIST_INSERT_HEAD(&pts->pts_prof, ptn, pt_list);
1242 }
1243 i = 3;
1244 }
1245 for ( ; i < TIMER_MAX; i++) {
1246 if (pts->pts_timers[i] != NULL) {
1247 itimerfree(pts, i);
1248 mutex_spin_enter(&timer_lock);
1249 }
1250 }
1251 if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
1252 pts->pts_timers[2] == NULL) {
1253 p->p_timers = NULL;
1254 mutex_spin_exit(&timer_lock);
1255 pool_put(&ptimers_pool, pts);
1256 } else
1257 mutex_spin_exit(&timer_lock);
1258 }
1259
1260 static void
1261 itimerfree(struct ptimers *pts, int index)
1262 {
1263 struct ptimer *pt;
1264
1265 KASSERT(mutex_owned(&timer_lock));
1266
1267 pt = pts->pts_timers[index];
1268 pts->pts_timers[index] = NULL;
1269 if (pt->pt_type == CLOCK_REALTIME)
1270 callout_halt(&pt->pt_ch, &timer_lock);
1271 else if (pt->pt_queued)
1272 TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1273 mutex_spin_exit(&timer_lock);
1274 callout_destroy(&pt->pt_ch);
1275 pool_put(&ptimer_pool, pt);
1276 }
1277
1278 /*
1279 * Decrement an interval timer by a specified number
1280 * of microseconds, which must be less than a second,
1281 * i.e. < 1000000. If the timer expires, then reload
1282 * it. In this case, carry over (usec - old value) to
1283 * reduce the value reloaded into the timer so that
1284 * the timer does not drift. This routine assumes
1285 * that it is called in a context where the timers
1286 * on which it is operating cannot change in value.
1287 */
1288 static int
1289 itimerdecr(struct ptimer *pt, int usec)
1290 {
1291 struct itimerval *itp;
1292
1293 KASSERT(mutex_owned(&timer_lock));
1294
1295 itp = &pt->pt_time;
1296 if (itp->it_value.tv_usec < usec) {
1297 if (itp->it_value.tv_sec == 0) {
1298 /* expired, and already in next interval */
1299 usec -= itp->it_value.tv_usec;
1300 goto expire;
1301 }
1302 itp->it_value.tv_usec += 1000000;
1303 itp->it_value.tv_sec--;
1304 }
1305 itp->it_value.tv_usec -= usec;
1306 usec = 0;
1307 if (timerisset(&itp->it_value))
1308 return (1);
1309 /* expired, exactly at end of interval */
1310 expire:
1311 if (timerisset(&itp->it_interval)) {
1312 itp->it_value = itp->it_interval;
1313 itp->it_value.tv_usec -= usec;
1314 if (itp->it_value.tv_usec < 0) {
1315 itp->it_value.tv_usec += 1000000;
1316 itp->it_value.tv_sec--;
1317 }
1318 timer_settime(pt);
1319 } else
1320 itp->it_value.tv_usec = 0; /* sec is already 0 */
1321 return (0);
1322 }
1323
1324 static void
1325 itimerfire(struct ptimer *pt)
1326 {
1327
1328 KASSERT(mutex_owned(&timer_lock));
1329
1330 /*
1331 * XXX Can overrun, but we don't do signal queueing yet, anyway.
1332 * XXX Relying on the clock interrupt is stupid.
1333 */
1334 if ((pt->pt_ev.sigev_notify == SIGEV_SA && pt->pt_proc->p_sa == NULL) ||
1335 (pt->pt_ev.sigev_notify != SIGEV_SIGNAL &&
1336 pt->pt_ev.sigev_notify != SIGEV_SA) || pt->pt_queued)
1337 return;
1338 TAILQ_INSERT_TAIL(&timer_queue, pt, pt_chain);
1339 pt->pt_queued = true;
1340 softint_schedule(timer_sih);
1341 }
1342
1343 void
1344 timer_tick(lwp_t *l, bool user)
1345 {
1346 struct ptimers *pts;
1347 struct ptimer *pt;
1348 proc_t *p;
1349
1350 p = l->l_proc;
1351 if (p->p_timers == NULL)
1352 return;
1353
1354 mutex_spin_enter(&timer_lock);
1355 if ((pts = l->l_proc->p_timers) != NULL) {
1356 /*
1357 * Run current process's virtual and profile time, as needed.
1358 */
1359 if (user && (pt = LIST_FIRST(&pts->pts_virtual)) != NULL)
1360 if (itimerdecr(pt, tick) == 0)
1361 itimerfire(pt);
1362 if ((pt = LIST_FIRST(&pts->pts_prof)) != NULL)
1363 if (itimerdecr(pt, tick) == 0)
1364 itimerfire(pt);
1365 }
1366 mutex_spin_exit(&timer_lock);
1367 }
1368
1369 /*
1370 * timer_sa_intr:
1371 *
1372 * SIGEV_SA handling for timer_intr(). We are called (and return)
1373 * with the timer lock held. We know that the process had SA enabled
1374 * when this timer was enqueued. As timer_intr() is a soft interrupt
1375 * handler, SA should still be enabled by the time we get here.
1376 *
1377 * XXX Is it legit to lock p_lock and the lwp at this time?
1378 */
1379 static void
1380 timer_sa_intr(struct ptimer *pt, proc_t *p)
1381 {
1382 unsigned int i;
1383 struct sadata_vp *vp;
1384
1385 /* Cause the process to generate an upcall when it returns. */
1386 if (!p->p_timerpend) {
1387 /*
1388 * XXX stop signals can be processed inside tsleep,
1389 * which can be inside sa_yield's inner loop, which
1390 * makes testing for sa_idle alone insuffucent to
1391 * determine if we really should call setrunnable.
1392 */
1393 pt->pt_poverruns = pt->pt_overruns;
1394 pt->pt_overruns = 0;
1395 i = 1 << pt->pt_entry;
1396 p->p_timers->pts_fired = i;
1397 p->p_timerpend = 1;
1398
1399 mutex_enter(p->p_lock);
1400 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1401 lwp_need_userret(vp->savp_lwp);
1402 lwp_lock(vp->savp_lwp);
1403 if (vp->savp_lwp->l_flag & LW_SA_IDLE) {
1404 vp->savp_lwp->l_flag &= ~LW_SA_IDLE;
1405 lwp_unlock(vp->savp_lwp);
1406 wakeup(vp->savp_lwp);
1407 break;
1408 }
1409 lwp_unlock(vp->savp_lwp);
1410 }
1411 mutex_exit(p->p_lock);
1412 } else {
1413 i = 1 << pt->pt_entry;
1414 if ((p->p_timers->pts_fired & i) == 0) {
1415 pt->pt_poverruns = pt->pt_overruns;
1416 pt->pt_overruns = 0;
1417 p->p_timers->pts_fired |= i;
1418 } else
1419 pt->pt_overruns++;
1420 }
1421 }
1422
1423 static void
1424 timer_intr(void *cookie)
1425 {
1426 ksiginfo_t ksi;
1427 struct ptimer *pt;
1428 proc_t *p;
1429
1430 mutex_spin_enter(&timer_lock);
1431 while ((pt = TAILQ_FIRST(&timer_queue)) != NULL) {
1432 TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1433 KASSERT(pt->pt_queued);
1434 pt->pt_queued = false;
1435
1436 if (pt->pt_proc->p_timers == NULL) {
1437 /* Process is dying. */
1438 continue;
1439 }
1440 p = pt->pt_proc;
1441 if (pt->pt_ev.sigev_notify == SIGEV_SA) {
1442 timer_sa_intr(pt, p);
1443 continue;
1444 }
1445 if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL)
1446 continue;
1447 if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
1448 pt->pt_overruns++;
1449 continue;
1450 }
1451
1452 KSI_INIT(&ksi);
1453 ksi.ksi_signo = pt->pt_ev.sigev_signo;
1454 ksi.ksi_code = SI_TIMER;
1455 ksi.ksi_value = pt->pt_ev.sigev_value;
1456 pt->pt_poverruns = pt->pt_overruns;
1457 pt->pt_overruns = 0;
1458 mutex_spin_exit(&timer_lock);
1459
1460 mutex_enter(proc_lock);
1461 kpsignal(p, &ksi, NULL);
1462 mutex_exit(proc_lock);
1463
1464 mutex_spin_enter(&timer_lock);
1465 }
1466 mutex_spin_exit(&timer_lock);
1467 }
1468
1469 /*
1470 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1471 * for usage and rationale.
1472 */
1473 int
1474 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1475 {
1476 struct timeval tv, delta;
1477 int rv = 0;
1478
1479 getmicrouptime(&tv);
1480 timersub(&tv, lasttime, &delta);
1481
1482 /*
1483 * check for 0,0 is so that the message will be seen at least once,
1484 * even if interval is huge.
1485 */
1486 if (timercmp(&delta, mininterval, >=) ||
1487 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1488 *lasttime = tv;
1489 rv = 1;
1490 }
1491
1492 return (rv);
1493 }
1494
1495 /*
1496 * ppsratecheck(): packets (or events) per second limitation.
1497 */
1498 int
1499 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1500 {
1501 struct timeval tv, delta;
1502 int rv;
1503
1504 getmicrouptime(&tv);
1505 timersub(&tv, lasttime, &delta);
1506
1507 /*
1508 * check for 0,0 is so that the message will be seen at least once.
1509 * if more than one second have passed since the last update of
1510 * lasttime, reset the counter.
1511 *
1512 * we do increment *curpps even in *curpps < maxpps case, as some may
1513 * try to use *curpps for stat purposes as well.
1514 */
1515 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1516 delta.tv_sec >= 1) {
1517 *lasttime = tv;
1518 *curpps = 0;
1519 }
1520 if (maxpps < 0)
1521 rv = 1;
1522 else if (*curpps < maxpps)
1523 rv = 1;
1524 else
1525 rv = 0;
1526
1527 #if 1 /*DIAGNOSTIC?*/
1528 /* be careful about wrap-around */
1529 if (*curpps + 1 > *curpps)
1530 *curpps = *curpps + 1;
1531 #else
1532 /*
1533 * assume that there's not too many calls to this function.
1534 * not sure if the assumption holds, as it depends on *caller's*
1535 * behavior, not the behavior of this function.
1536 * IMHO it is wrong to make assumption on the caller's behavior,
1537 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1538 */
1539 *curpps = *curpps + 1;
1540 #endif
1541
1542 return (rv);
1543 }
1544