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