source: rtems-libbsd/freebsd/kern/kern_time.c @ d4515a8

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since d4515a8 was d4515a8, checked in by Joel Sherrill <joel.sherrill@…>, on 10/23/12 at 21:49:40

kern_time: Disable some routines RTEMS has

  • Property mode set to 100644
File size: 35.6 KB
Line 
1#include <freebsd/machine/rtems-bsd-config.h>
2
3/*-
4 * Copyright (c) 1982, 1986, 1989, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *      @(#)kern_time.c 8.1 (Berkeley) 6/10/93
32 */
33
34#include <freebsd/sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include <freebsd/sys/param.h>
38#include <freebsd/sys/systm.h>
39#include <freebsd/sys/limits.h>
40#ifndef __rtems__
41#include <freebsd/sys/clock.h>
42#endif /* __rtems__ */
43#include <freebsd/sys/lock.h>
44#include <freebsd/sys/mutex.h>
45#include <freebsd/sys/sysproto.h>
46#include <freebsd/sys/eventhandler.h>
47#include <freebsd/sys/resourcevar.h>
48#include <freebsd/sys/signalvar.h>
49#include <freebsd/sys/kernel.h>
50#include <freebsd/sys/syscallsubr.h>
51#include <freebsd/sys/sysctl.h>
52#include <freebsd/sys/sysent.h>
53#include <freebsd/sys/priv.h>
54#include <freebsd/sys/proc.h>
55#ifndef __rtems__
56#include <freebsd/sys/posix4.h>
57#include <freebsd/sys/time.h>
58#include <freebsd/sys/timers.h>
59#include <freebsd/sys/timetc.h>
60#include <freebsd/sys/vnode.h>
61
62#include <freebsd/vm/vm.h>
63#include <freebsd/vm/vm_extern.h>
64
65#define MAX_CLOCKS      (CLOCK_MONOTONIC+1)
66
67static struct kclock    posix_clocks[MAX_CLOCKS];
68static uma_zone_t       itimer_zone = NULL;
69
70/*
71 * Time of day and interval timer support.
72 *
73 * These routines provide the kernel entry points to get and set
74 * the time-of-day and per-process interval timers.  Subroutines
75 * here provide support for adding and subtracting timeval structures
76 * and decrementing interval timers, optionally reloading the interval
77 * timers when they expire.
78 */
79
80static int      settime(struct thread *, struct timeval *);
81#endif /* __rtems__ */
82static void     timevalfix(struct timeval *);
83
84#ifndef __rtems__
85static void     itimer_start(void);
86static int      itimer_init(void *, int, int);
87static void     itimer_fini(void *, int);
88static void     itimer_enter(struct itimer *);
89static void     itimer_leave(struct itimer *);
90static struct itimer *itimer_find(struct proc *, int);
91static void     itimers_alloc(struct proc *);
92static void     itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
93static void     itimers_event_hook_exit(void *arg, struct proc *p);
94static int      realtimer_create(struct itimer *);
95static int      realtimer_gettime(struct itimer *, struct itimerspec *);
96static int      realtimer_settime(struct itimer *, int,
97                        struct itimerspec *, struct itimerspec *);
98static int      realtimer_delete(struct itimer *);
99static void     realtimer_clocktime(clockid_t, struct timespec *);
100static void     realtimer_expire(void *);
101static int      kern_timer_create(struct thread *, clockid_t,
102                        struct sigevent *, int *, int);
103static int      kern_timer_delete(struct thread *, int);
104
105int             register_posix_clock(int, struct kclock *);
106void            itimer_fire(struct itimer *it);
107int             itimespecfix(struct timespec *ts);
108
109#define CLOCK_CALL(clock, call, arglist)                \
110        ((*posix_clocks[clock].call) arglist)
111
112SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
113
114
115static int
116settime(struct thread *td, struct timeval *tv)
117{
118        struct timeval delta, tv1, tv2;
119        static struct timeval maxtime, laststep;
120        struct timespec ts;
121        int s;
122
123        s = splclock();
124        microtime(&tv1);
125        delta = *tv;
126        timevalsub(&delta, &tv1);
127
128        /*
129         * If the system is secure, we do not allow the time to be
130         * set to a value earlier than 1 second less than the highest
131         * time we have yet seen. The worst a miscreant can do in
132         * this circumstance is "freeze" time. He couldn't go
133         * back to the past.
134         *
135         * We similarly do not allow the clock to be stepped more
136         * than one second, nor more than once per second. This allows
137         * a miscreant to make the clock march double-time, but no worse.
138         */
139        if (securelevel_gt(td->td_ucred, 1) != 0) {
140                if (delta.tv_sec < 0 || delta.tv_usec < 0) {
141                        /*
142                         * Update maxtime to latest time we've seen.
143                         */
144                        if (tv1.tv_sec > maxtime.tv_sec)
145                                maxtime = tv1;
146                        tv2 = *tv;
147                        timevalsub(&tv2, &maxtime);
148                        if (tv2.tv_sec < -1) {
149                                tv->tv_sec = maxtime.tv_sec - 1;
150                                printf("Time adjustment clamped to -1 second\n");
151                        }
152                } else {
153                        if (tv1.tv_sec == laststep.tv_sec) {
154                                splx(s);
155                                return (EPERM);
156                        }
157                        if (delta.tv_sec > 1) {
158                                tv->tv_sec = tv1.tv_sec + 1;
159                                printf("Time adjustment clamped to +1 second\n");
160                        }
161                        laststep = *tv;
162                }
163        }
164
165        ts.tv_sec = tv->tv_sec;
166        ts.tv_nsec = tv->tv_usec * 1000;
167        mtx_lock(&Giant);
168        tc_setclock(&ts);
169        resettodr();
170        mtx_unlock(&Giant);
171        return (0);
172}
173
174#ifndef _SYS_SYSPROTO_HH_
175struct clock_gettime_args {
176        clockid_t clock_id;
177        struct  timespec *tp;
178};
179#endif
180#ifndef __rtems__
181/* ARGSUSED */
182int
183clock_gettime(struct thread *td, struct clock_gettime_args *uap)
184{
185        struct timespec ats;
186        int error;
187
188        error = kern_clock_gettime(td, uap->clock_id, &ats);
189        if (error == 0)
190                error = copyout(&ats, uap->tp, sizeof(ats));
191
192        return (error);
193}
194#endif
195
196#ifndef __rtems__
197int
198kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
199{
200        struct timeval sys, user;
201        struct proc *p;
202        uint64_t runtime, curtime, switchtime;
203
204        p = td->td_proc;
205        switch (clock_id) {
206        case CLOCK_REALTIME:            /* Default to precise. */
207        case CLOCK_REALTIME_PRECISE:
208                nanotime(ats);
209                break;
210        case CLOCK_REALTIME_FAST:
211                getnanotime(ats);
212                break;
213        case CLOCK_VIRTUAL:
214                PROC_LOCK(p);
215                PROC_SLOCK(p);
216                calcru(p, &user, &sys);
217                PROC_SUNLOCK(p);
218                PROC_UNLOCK(p);
219                TIMEVAL_TO_TIMESPEC(&user, ats);
220                break;
221        case CLOCK_PROF:
222                PROC_LOCK(p);
223                PROC_SLOCK(p);
224                calcru(p, &user, &sys);
225                PROC_SUNLOCK(p);
226                PROC_UNLOCK(p);
227                timevaladd(&user, &sys);
228                TIMEVAL_TO_TIMESPEC(&user, ats);
229                break;
230        case CLOCK_MONOTONIC:           /* Default to precise. */
231        case CLOCK_MONOTONIC_PRECISE:
232        case CLOCK_UPTIME:
233        case CLOCK_UPTIME_PRECISE:
234                nanouptime(ats);
235                break;
236        case CLOCK_UPTIME_FAST:
237        case CLOCK_MONOTONIC_FAST:
238                getnanouptime(ats);
239                break;
240        case CLOCK_SECOND:
241                ats->tv_sec = time_second;
242                ats->tv_nsec = 0;
243                break;
244        case CLOCK_THREAD_CPUTIME_ID:
245                critical_enter();
246                switchtime = PCPU_GET(switchtime);
247                curtime = cpu_ticks();
248                runtime = td->td_runtime;
249                critical_exit();
250                runtime = cputick2usec(runtime + curtime - switchtime);
251                ats->tv_sec = runtime / 1000000;
252                ats->tv_nsec = runtime % 1000000 * 1000;
253                break;
254        default:
255                return (EINVAL);
256        }
257        return (0);
258}
259#endif
260
261#ifndef _SYS_SYSPROTO_HH_
262struct clock_settime_args {
263        clockid_t clock_id;
264        const struct    timespec *tp;
265};
266#endif
267#ifndef __rtems__
268/* ARGSUSED */
269int
270clock_settime(struct thread *td, struct clock_settime_args *uap)
271{
272        struct timespec ats;
273        int error;
274
275        if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
276                return (error);
277        return (kern_clock_settime(td, uap->clock_id, &ats));
278}
279
280int
281kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
282{
283        struct timeval atv;
284        int error;
285
286        if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
287                return (error);
288        if (clock_id != CLOCK_REALTIME)
289                return (EINVAL);
290        if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
291                return (EINVAL);
292        /* XXX Don't convert nsec->usec and back */
293        TIMESPEC_TO_TIMEVAL(&atv, ats);
294        error = settime(td, &atv);
295        return (error);
296}
297#endif
298
299#ifndef _SYS_SYSPROTO_HH_
300struct clock_getres_args {
301        clockid_t clock_id;
302        struct  timespec *tp;
303};
304#endif
305#ifndef __rtems__
306int
307clock_getres(struct thread *td, struct clock_getres_args *uap)
308{
309        struct timespec ts;
310        int error;
311
312        if (uap->tp == NULL)
313                return (0);
314
315        error = kern_clock_getres(td, uap->clock_id, &ts);
316        if (error == 0)
317                error = copyout(&ts, uap->tp, sizeof(ts));
318        return (error);
319}
320
321int
322kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
323{
324
325        ts->tv_sec = 0;
326        switch (clock_id) {
327        case CLOCK_REALTIME:
328        case CLOCK_REALTIME_FAST:
329        case CLOCK_REALTIME_PRECISE:
330        case CLOCK_MONOTONIC:
331        case CLOCK_MONOTONIC_FAST:
332        case CLOCK_MONOTONIC_PRECISE:
333        case CLOCK_UPTIME:
334        case CLOCK_UPTIME_FAST:
335        case CLOCK_UPTIME_PRECISE:
336                /*
337                 * Round up the result of the division cheaply by adding 1.
338                 * Rounding up is especially important if rounding down
339                 * would give 0.  Perfect rounding is unimportant.
340                 */
341                ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
342                break;
343        case CLOCK_VIRTUAL:
344        case CLOCK_PROF:
345                /* Accurately round up here because we can do so cheaply. */
346                ts->tv_nsec = (1000000000 + hz - 1) / hz;
347                break;
348        case CLOCK_SECOND:
349                ts->tv_sec = 1;
350                ts->tv_nsec = 0;
351                break;
352        case CLOCK_THREAD_CPUTIME_ID:
353                /* sync with cputick2usec */
354                ts->tv_nsec = 1000000 / cpu_tickrate();
355                if (ts->tv_nsec == 0)
356                        ts->tv_nsec = 1000;
357                break;
358        default:
359                return (EINVAL);
360        }
361        return (0);
362}
363#endif
364
365static int nanowait;
366
367int
368kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
369{
370        struct timespec ts, ts2, ts3;
371        struct timeval tv;
372        int error;
373
374        if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
375                return (EINVAL);
376        if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
377                return (0);
378        getnanouptime(&ts);
379        timespecadd(&ts, rqt);
380        TIMESPEC_TO_TIMEVAL(&tv, rqt);
381        for (;;) {
382                error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
383                    tvtohz(&tv));
384                getnanouptime(&ts2);
385                if (error != EWOULDBLOCK) {
386                        if (error == ERESTART)
387                                error = EINTR;
388                        if (rmt != NULL) {
389                                timespecsub(&ts, &ts2);
390                                if (ts.tv_sec < 0)
391                                        timespecclear(&ts);
392                                *rmt = ts;
393                        }
394                        return (error);
395                }
396                if (timespeccmp(&ts2, &ts, >=))
397                        return (0);
398                ts3 = ts;
399                timespecsub(&ts3, &ts2);
400                TIMESPEC_TO_TIMEVAL(&tv, &ts3);
401        }
402}
403
404#ifndef _SYS_SYSPROTO_HH_
405struct nanosleep_args {
406        struct  timespec *rqtp;
407        struct  timespec *rmtp;
408};
409#endif
410/* ARGSUSED */
411int
412nanosleep(struct thread *td, struct nanosleep_args *uap)
413{
414        struct timespec rmt, rqt;
415        int error;
416
417        error = copyin(uap->rqtp, &rqt, sizeof(rqt));
418        if (error)
419                return (error);
420
421        if (uap->rmtp &&
422            !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
423                        return (EFAULT);
424        error = kern_nanosleep(td, &rqt, &rmt);
425        if (error && uap->rmtp) {
426                int error2;
427
428                error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
429                if (error2)
430                        error = error2;
431        }
432        return (error);
433}
434
435#ifndef _SYS_SYSPROTO_HH_
436struct gettimeofday_args {
437        struct  timeval *tp;
438        struct  timezone *tzp;
439};
440#endif
441/* ARGSUSED */
442int
443gettimeofday(struct thread *td, struct gettimeofday_args *uap)
444{
445        struct timeval atv;
446        struct timezone rtz;
447        int error = 0;
448
449        if (uap->tp) {
450                microtime(&atv);
451                error = copyout(&atv, uap->tp, sizeof (atv));
452        }
453        if (error == 0 && uap->tzp != NULL) {
454                rtz.tz_minuteswest = tz_minuteswest;
455                rtz.tz_dsttime = tz_dsttime;
456                error = copyout(&rtz, uap->tzp, sizeof (rtz));
457        }
458        return (error);
459}
460
461#ifndef _SYS_SYSPROTO_HH_
462struct settimeofday_args {
463        struct  timeval *tv;
464        struct  timezone *tzp;
465};
466#endif
467/* ARGSUSED */
468int
469settimeofday(struct thread *td, struct settimeofday_args *uap)
470{
471        struct timeval atv, *tvp;
472        struct timezone atz, *tzp;
473        int error;
474
475        if (uap->tv) {
476                error = copyin(uap->tv, &atv, sizeof(atv));
477                if (error)
478                        return (error);
479                tvp = &atv;
480        } else
481                tvp = NULL;
482        if (uap->tzp) {
483                error = copyin(uap->tzp, &atz, sizeof(atz));
484                if (error)
485                        return (error);
486                tzp = &atz;
487        } else
488                tzp = NULL;
489        return (kern_settimeofday(td, tvp, tzp));
490}
491
492int
493kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
494{
495        int error;
496
497        error = priv_check(td, PRIV_SETTIMEOFDAY);
498        if (error)
499                return (error);
500        /* Verify all parameters before changing time. */
501        if (tv) {
502                if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
503                        return (EINVAL);
504                error = settime(td, tv);
505        }
506        if (tzp && error == 0) {
507                tz_minuteswest = tzp->tz_minuteswest;
508                tz_dsttime = tzp->tz_dsttime;
509        }
510        return (error);
511}
512
513/*
514 * Get value of an interval timer.  The process virtual and profiling virtual
515 * time timers are kept in the p_stats area, since they can be swapped out.
516 * These are kept internally in the way they are specified externally: in
517 * time until they expire.
518 *
519 * The real time interval timer is kept in the process table slot for the
520 * process, and its value (it_value) is kept as an absolute time rather than
521 * as a delta, so that it is easy to keep periodic real-time signals from
522 * drifting.
523 *
524 * Virtual time timers are processed in the hardclock() routine of
525 * kern_clock.c.  The real time timer is processed by a timeout routine,
526 * called from the softclock() routine.  Since a callout may be delayed in
527 * real time due to interrupt processing in the system, it is possible for
528 * the real time timeout routine (realitexpire, given below), to be delayed
529 * in real time past when it is supposed to occur.  It does not suffice,
530 * therefore, to reload the real timer .it_value from the real time timers
531 * .it_interval.  Rather, we compute the next time in absolute time the timer
532 * should go off.
533 */
534#ifndef _SYS_SYSPROTO_HH_
535struct getitimer_args {
536        u_int   which;
537        struct  itimerval *itv;
538};
539#endif
540int
541getitimer(struct thread *td, struct getitimer_args *uap)
542{
543        struct itimerval aitv;
544        int error;
545
546        error = kern_getitimer(td, uap->which, &aitv);
547        if (error != 0)
548                return (error);
549        return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
550}
551
552int
553kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
554{
555        struct proc *p = td->td_proc;
556        struct timeval ctv;
557
558        if (which > ITIMER_PROF)
559                return (EINVAL);
560
561        if (which == ITIMER_REAL) {
562                /*
563                 * Convert from absolute to relative time in .it_value
564                 * part of real time timer.  If time for real time timer
565                 * has passed return 0, else return difference between
566                 * current time and time for the timer to go off.
567                 */
568                PROC_LOCK(p);
569                *aitv = p->p_realtimer;
570                PROC_UNLOCK(p);
571                if (timevalisset(&aitv->it_value)) {
572                        getmicrouptime(&ctv);
573                        if (timevalcmp(&aitv->it_value, &ctv, <))
574                                timevalclear(&aitv->it_value);
575                        else
576                                timevalsub(&aitv->it_value, &ctv);
577                }
578        } else {
579                PROC_SLOCK(p);
580                *aitv = p->p_stats->p_timer[which];
581                PROC_SUNLOCK(p);
582        }
583        return (0);
584}
585
586#ifndef _SYS_SYSPROTO_HH_
587struct setitimer_args {
588        u_int   which;
589        struct  itimerval *itv, *oitv;
590};
591#endif
592int
593setitimer(struct thread *td, struct setitimer_args *uap)
594{
595        struct itimerval aitv, oitv;
596        int error;
597
598        if (uap->itv == NULL) {
599                uap->itv = uap->oitv;
600                return (getitimer(td, (struct getitimer_args *)uap));
601        }
602
603        if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
604                return (error);
605        error = kern_setitimer(td, uap->which, &aitv, &oitv);
606        if (error != 0 || uap->oitv == NULL)
607                return (error);
608        return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
609}
610
611int
612kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
613    struct itimerval *oitv)
614{
615        struct proc *p = td->td_proc;
616        struct timeval ctv;
617
618        if (aitv == NULL)
619                return (kern_getitimer(td, which, oitv));
620
621        if (which > ITIMER_PROF)
622                return (EINVAL);
623        if (itimerfix(&aitv->it_value))
624                return (EINVAL);
625        if (!timevalisset(&aitv->it_value))
626                timevalclear(&aitv->it_interval);
627        else if (itimerfix(&aitv->it_interval))
628                return (EINVAL);
629
630        if (which == ITIMER_REAL) {
631                PROC_LOCK(p);
632                if (timevalisset(&p->p_realtimer.it_value))
633                        callout_stop(&p->p_itcallout);
634                getmicrouptime(&ctv);
635                if (timevalisset(&aitv->it_value)) {
636                        callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
637                            realitexpire, p);
638                        timevaladd(&aitv->it_value, &ctv);
639                }
640                *oitv = p->p_realtimer;
641                p->p_realtimer = *aitv;
642                PROC_UNLOCK(p);
643                if (timevalisset(&oitv->it_value)) {
644                        if (timevalcmp(&oitv->it_value, &ctv, <))
645                                timevalclear(&oitv->it_value);
646                        else
647                                timevalsub(&oitv->it_value, &ctv);
648                }
649        } else {
650                PROC_SLOCK(p);
651                *oitv = p->p_stats->p_timer[which];
652                p->p_stats->p_timer[which] = *aitv;
653                PROC_SUNLOCK(p);
654        }
655        return (0);
656}
657
658/*
659 * Real interval timer expired:
660 * send process whose timer expired an alarm signal.
661 * If time is not set up to reload, then just return.
662 * Else compute next time timer should go off which is > current time.
663 * This is where delay in processing this timeout causes multiple
664 * SIGALRM calls to be compressed into one.
665 * tvtohz() always adds 1 to allow for the time until the next clock
666 * interrupt being strictly less than 1 clock tick, but we don't want
667 * that here since we want to appear to be in sync with the clock
668 * interrupt even when we're delayed.
669 */
670void
671realitexpire(void *arg)
672{
673        struct proc *p;
674        struct timeval ctv, ntv;
675
676        p = (struct proc *)arg;
677        PROC_LOCK(p);
678        psignal(p, SIGALRM);
679        if (!timevalisset(&p->p_realtimer.it_interval)) {
680                timevalclear(&p->p_realtimer.it_value);
681                if (p->p_flag & P_WEXIT)
682                        wakeup(&p->p_itcallout);
683                PROC_UNLOCK(p);
684                return;
685        }
686        for (;;) {
687                timevaladd(&p->p_realtimer.it_value,
688                    &p->p_realtimer.it_interval);
689                getmicrouptime(&ctv);
690                if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
691                        ntv = p->p_realtimer.it_value;
692                        timevalsub(&ntv, &ctv);
693                        callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
694                            realitexpire, p);
695                        PROC_UNLOCK(p);
696                        return;
697                }
698        }
699        /*NOTREACHED*/
700}
701
702/*
703 * Check that a proposed value to load into the .it_value or
704 * .it_interval part of an interval timer is acceptable, and
705 * fix it to have at least minimal value (i.e. if it is less
706 * than the resolution of the clock, round it up.)
707 */
708int
709itimerfix(struct timeval *tv)
710{
711
712        if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
713                return (EINVAL);
714        if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
715                tv->tv_usec = tick;
716        return (0);
717}
718
719/*
720 * Decrement an interval timer by a specified number
721 * of microseconds, which must be less than a second,
722 * i.e. < 1000000.  If the timer expires, then reload
723 * it.  In this case, carry over (usec - old value) to
724 * reduce the value reloaded into the timer so that
725 * the timer does not drift.  This routine assumes
726 * that it is called in a context where the timers
727 * on which it is operating cannot change in value.
728 */
729int
730itimerdecr(struct itimerval *itp, int usec)
731{
732
733        if (itp->it_value.tv_usec < usec) {
734                if (itp->it_value.tv_sec == 0) {
735                        /* expired, and already in next interval */
736                        usec -= itp->it_value.tv_usec;
737                        goto expire;
738                }
739                itp->it_value.tv_usec += 1000000;
740                itp->it_value.tv_sec--;
741        }
742        itp->it_value.tv_usec -= usec;
743        usec = 0;
744        if (timevalisset(&itp->it_value))
745                return (1);
746        /* expired, exactly at end of interval */
747expire:
748        if (timevalisset(&itp->it_interval)) {
749                itp->it_value = itp->it_interval;
750                itp->it_value.tv_usec -= usec;
751                if (itp->it_value.tv_usec < 0) {
752                        itp->it_value.tv_usec += 1000000;
753                        itp->it_value.tv_sec--;
754                }
755        } else
756                itp->it_value.tv_usec = 0;              /* sec is already 0 */
757        return (0);
758}
759
760/*
761 * Add and subtract routines for timevals.
762 * N.B.: subtract routine doesn't deal with
763 * results which are before the beginning,
764 * it just gets very confused in this case.
765 * Caveat emptor.
766 */
767void
768timevaladd(struct timeval *t1, const struct timeval *t2)
769{
770
771        t1->tv_sec += t2->tv_sec;
772        t1->tv_usec += t2->tv_usec;
773        timevalfix(t1);
774}
775#endif /* __rtems__ */
776
777void
778timevalsub(struct timeval *t1, const struct timeval *t2)
779{
780
781        t1->tv_sec -= t2->tv_sec;
782        t1->tv_usec -= t2->tv_usec;
783        timevalfix(t1);
784}
785
786static void
787timevalfix(struct timeval *t1)
788{
789
790        if (t1->tv_usec < 0) {
791                t1->tv_sec--;
792                t1->tv_usec += 1000000;
793        }
794        if (t1->tv_usec >= 1000000) {
795                t1->tv_sec++;
796                t1->tv_usec -= 1000000;
797        }
798}
799
800/*
801 * ratecheck(): simple time-based rate-limit checking.
802 */
803int
804ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
805{
806        struct timeval tv, delta;
807        int rv = 0;
808
809        getmicrouptime(&tv);            /* NB: 10ms precision */
810        delta = tv;
811        timevalsub(&delta, lasttime);
812
813        /*
814         * check for 0,0 is so that the message will be seen at least once,
815         * even if interval is huge.
816         */
817        if (timevalcmp(&delta, mininterval, >=) ||
818            (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
819                *lasttime = tv;
820                rv = 1;
821        }
822
823        return (rv);
824}
825
826/*
827 * ppsratecheck(): packets (or events) per second limitation.
828 *
829 * Return 0 if the limit is to be enforced (e.g. the caller
830 * should drop a packet because of the rate limitation).
831 *
832 * maxpps of 0 always causes zero to be returned.  maxpps of -1
833 * always causes 1 to be returned; this effectively defeats rate
834 * limiting.
835 *
836 * Note that we maintain the struct timeval for compatibility
837 * with other bsd systems.  We reuse the storage and just monitor
838 * clock ticks for minimal overhead. 
839 */
840int
841ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
842{
843        int now;
844
845        /*
846         * Reset the last time and counter if this is the first call
847         * or more than a second has passed since the last update of
848         * lasttime.
849         */
850        now = ticks;
851        if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
852                lasttime->tv_sec = now;
853                *curpps = 1;
854                return (maxpps != 0);
855        } else {
856                (*curpps)++;            /* NB: ignore potential overflow */
857                return (maxpps < 0 || *curpps < maxpps);
858        }
859}
860
861#ifndef __rtems__
862static void
863itimer_start(void)
864{
865        struct kclock rt_clock = {
866                .timer_create  = realtimer_create,
867                .timer_delete  = realtimer_delete,
868                .timer_settime = realtimer_settime,
869                .timer_gettime = realtimer_gettime,
870                .event_hook    = NULL
871        };
872
873        itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
874                NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
875        register_posix_clock(CLOCK_REALTIME,  &rt_clock);
876        register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
877        p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
878        p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
879        p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
880        EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
881                (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
882        EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
883                (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
884}
885
886int
887register_posix_clock(int clockid, struct kclock *clk)
888{
889        if ((unsigned)clockid >= MAX_CLOCKS) {
890                printf("%s: invalid clockid\n", __func__);
891                return (0);
892        }
893        posix_clocks[clockid] = *clk;
894        return (1);
895}
896
897static int
898itimer_init(void *mem, int size, int flags)
899{
900        struct itimer *it;
901
902        it = (struct itimer *)mem;
903        mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
904        return (0);
905}
906
907static void
908itimer_fini(void *mem, int size)
909{
910        struct itimer *it;
911
912        it = (struct itimer *)mem;
913        mtx_destroy(&it->it_mtx);
914}
915
916static void
917itimer_enter(struct itimer *it)
918{
919
920        mtx_assert(&it->it_mtx, MA_OWNED);
921        it->it_usecount++;
922}
923
924static void
925itimer_leave(struct itimer *it)
926{
927
928        mtx_assert(&it->it_mtx, MA_OWNED);
929        KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
930
931        if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
932                wakeup(it);
933}
934
935#ifndef _SYS_SYSPROTO_HH_
936struct ktimer_create_args {
937        clockid_t clock_id;
938        struct sigevent * evp;
939        int * timerid;
940};
941#endif
942int
943ktimer_create(struct thread *td, struct ktimer_create_args *uap)
944{
945        struct sigevent *evp1, ev;
946        int id;
947        int error;
948
949        if (uap->evp != NULL) {
950                error = copyin(uap->evp, &ev, sizeof(ev));
951                if (error != 0)
952                        return (error);
953                evp1 = &ev;
954        } else
955                evp1 = NULL;
956
957        error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
958
959        if (error == 0) {
960                error = copyout(&id, uap->timerid, sizeof(int));
961                if (error != 0)
962                        kern_timer_delete(td, id);
963        }
964        return (error);
965}
966
967static int
968kern_timer_create(struct thread *td, clockid_t clock_id,
969        struct sigevent *evp, int *timerid, int preset_id)
970{
971        struct proc *p = td->td_proc;
972        struct itimer *it;
973        int id;
974        int error;
975
976        if (clock_id < 0 || clock_id >= MAX_CLOCKS)
977                return (EINVAL);
978
979        if (posix_clocks[clock_id].timer_create == NULL)
980                return (EINVAL);
981
982        if (evp != NULL) {
983                if (evp->sigev_notify != SIGEV_NONE &&
984                    evp->sigev_notify != SIGEV_SIGNAL &&
985                    evp->sigev_notify != SIGEV_THREAD_ID)
986                        return (EINVAL);
987                if ((evp->sigev_notify == SIGEV_SIGNAL ||
988                     evp->sigev_notify == SIGEV_THREAD_ID) &&
989                        !_SIG_VALID(evp->sigev_signo))
990                        return (EINVAL);
991        }
992       
993        if (p->p_itimers == NULL)
994                itimers_alloc(p);
995       
996        it = uma_zalloc(itimer_zone, M_WAITOK);
997        it->it_flags = 0;
998        it->it_usecount = 0;
999        it->it_active = 0;
1000        timespecclear(&it->it_time.it_value);
1001        timespecclear(&it->it_time.it_interval);
1002        it->it_overrun = 0;
1003        it->it_overrun_last = 0;
1004        it->it_clockid = clock_id;
1005        it->it_timerid = -1;
1006        it->it_proc = p;
1007        ksiginfo_init(&it->it_ksi);
1008        it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1009        error = CLOCK_CALL(clock_id, timer_create, (it));
1010        if (error != 0)
1011                goto out;
1012
1013        PROC_LOCK(p);
1014        if (preset_id != -1) {
1015                KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1016                id = preset_id;
1017                if (p->p_itimers->its_timers[id] != NULL) {
1018                        PROC_UNLOCK(p);
1019                        error = 0;
1020                        goto out;
1021                }
1022        } else {
1023                /*
1024                 * Find a free timer slot, skipping those reserved
1025                 * for setitimer().
1026                 */
1027                for (id = 3; id < TIMER_MAX; id++)
1028                        if (p->p_itimers->its_timers[id] == NULL)
1029                                break;
1030                if (id == TIMER_MAX) {
1031                        PROC_UNLOCK(p);
1032                        error = EAGAIN;
1033                        goto out;
1034                }
1035        }
1036        it->it_timerid = id;
1037        p->p_itimers->its_timers[id] = it;
1038        if (evp != NULL)
1039                it->it_sigev = *evp;
1040        else {
1041                it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1042                switch (clock_id) {
1043                default:
1044                case CLOCK_REALTIME:
1045                        it->it_sigev.sigev_signo = SIGALRM;
1046                        break;
1047                case CLOCK_VIRTUAL:
1048                        it->it_sigev.sigev_signo = SIGVTALRM;
1049                        break;
1050                case CLOCK_PROF:
1051                        it->it_sigev.sigev_signo = SIGPROF;
1052                        break;
1053                }
1054                it->it_sigev.sigev_value.sival_int = id;
1055        }
1056
1057        if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1058            it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1059                it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1060                it->it_ksi.ksi_code = SI_TIMER;
1061                it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1062                it->it_ksi.ksi_timerid = id;
1063        }
1064        PROC_UNLOCK(p);
1065        *timerid = id;
1066        return (0);
1067
1068out:
1069        ITIMER_LOCK(it);
1070        CLOCK_CALL(it->it_clockid, timer_delete, (it));
1071        ITIMER_UNLOCK(it);
1072        uma_zfree(itimer_zone, it);
1073        return (error);
1074}
1075
1076#ifndef _SYS_SYSPROTO_HH_
1077struct ktimer_delete_args {
1078        int timerid;
1079};
1080#endif
1081int
1082ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1083{
1084        return (kern_timer_delete(td, uap->timerid));
1085}
1086
1087static struct itimer *
1088itimer_find(struct proc *p, int timerid)
1089{
1090        struct itimer *it;
1091
1092        PROC_LOCK_ASSERT(p, MA_OWNED);
1093        if ((p->p_itimers == NULL) ||
1094            (timerid < 0) || (timerid >= TIMER_MAX) ||
1095            (it = p->p_itimers->its_timers[timerid]) == NULL) {
1096                return (NULL);
1097        }
1098        ITIMER_LOCK(it);
1099        if ((it->it_flags & ITF_DELETING) != 0) {
1100                ITIMER_UNLOCK(it);
1101                it = NULL;
1102        }
1103        return (it);
1104}
1105
1106static int
1107kern_timer_delete(struct thread *td, int timerid)
1108{
1109        struct proc *p = td->td_proc;
1110        struct itimer *it;
1111
1112        PROC_LOCK(p);
1113        it = itimer_find(p, timerid);
1114        if (it == NULL) {
1115                PROC_UNLOCK(p);
1116                return (EINVAL);
1117        }
1118        PROC_UNLOCK(p);
1119
1120        it->it_flags |= ITF_DELETING;
1121        while (it->it_usecount > 0) {
1122                it->it_flags |= ITF_WANTED;
1123                msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1124        }
1125        it->it_flags &= ~ITF_WANTED;
1126        CLOCK_CALL(it->it_clockid, timer_delete, (it));
1127        ITIMER_UNLOCK(it);
1128
1129        PROC_LOCK(p);
1130        if (KSI_ONQ(&it->it_ksi))
1131                sigqueue_take(&it->it_ksi);
1132        p->p_itimers->its_timers[timerid] = NULL;
1133        PROC_UNLOCK(p);
1134        uma_zfree(itimer_zone, it);
1135        return (0);
1136}
1137
1138#ifndef _SYS_SYSPROTO_HH_
1139struct ktimer_settime_args {
1140        int timerid;
1141        int flags;
1142        const struct itimerspec * value;
1143        struct itimerspec * ovalue;
1144};
1145#endif
1146int
1147ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1148{
1149        struct proc *p = td->td_proc;
1150        struct itimer *it;
1151        struct itimerspec val, oval, *ovalp;
1152        int error;
1153
1154        error = copyin(uap->value, &val, sizeof(val));
1155        if (error != 0)
1156                return (error);
1157       
1158        if (uap->ovalue != NULL)
1159                ovalp = &oval;
1160        else
1161                ovalp = NULL;
1162
1163        PROC_LOCK(p);
1164        if (uap->timerid < 3 ||
1165            (it = itimer_find(p, uap->timerid)) == NULL) {
1166                PROC_UNLOCK(p);
1167                error = EINVAL;
1168        } else {
1169                PROC_UNLOCK(p);
1170                itimer_enter(it);
1171                error = CLOCK_CALL(it->it_clockid, timer_settime,
1172                                (it, uap->flags, &val, ovalp));
1173                itimer_leave(it);
1174                ITIMER_UNLOCK(it);
1175        }
1176        if (error == 0 && uap->ovalue != NULL)
1177                error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1178        return (error);
1179}
1180
1181#ifndef _SYS_SYSPROTO_HH_
1182struct ktimer_gettime_args {
1183        int timerid;
1184        struct itimerspec * value;
1185};
1186#endif
1187int
1188ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1189{
1190        struct proc *p = td->td_proc;
1191        struct itimer *it;
1192        struct itimerspec val;
1193        int error;
1194
1195        PROC_LOCK(p);
1196        if (uap->timerid < 3 ||
1197           (it = itimer_find(p, uap->timerid)) == NULL) {
1198                PROC_UNLOCK(p);
1199                error = EINVAL;
1200        } else {
1201                PROC_UNLOCK(p);
1202                itimer_enter(it);
1203                error = CLOCK_CALL(it->it_clockid, timer_gettime,
1204                                (it, &val));
1205                itimer_leave(it);
1206                ITIMER_UNLOCK(it);
1207        }
1208        if (error == 0)
1209                error = copyout(&val, uap->value, sizeof(val));
1210        return (error);
1211}
1212
1213#ifndef _SYS_SYSPROTO_HH_
1214struct timer_getoverrun_args {
1215        int timerid;
1216};
1217#endif
1218int
1219ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1220{
1221        struct proc *p = td->td_proc;
1222        struct itimer *it;
1223        int error ;
1224
1225        PROC_LOCK(p);
1226        if (uap->timerid < 3 ||
1227            (it = itimer_find(p, uap->timerid)) == NULL) {
1228                PROC_UNLOCK(p);
1229                error = EINVAL;
1230        } else {
1231                td->td_retval[0] = it->it_overrun_last;
1232                ITIMER_UNLOCK(it);
1233                PROC_UNLOCK(p);
1234                error = 0;
1235        }
1236        return (error);
1237}
1238
1239static int
1240realtimer_create(struct itimer *it)
1241{
1242        callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1243        return (0);
1244}
1245
1246static int
1247realtimer_delete(struct itimer *it)
1248{
1249        mtx_assert(&it->it_mtx, MA_OWNED);
1250       
1251        /*
1252         * clear timer's value and interval to tell realtimer_expire
1253         * to not rearm the timer.
1254         */
1255        timespecclear(&it->it_time.it_value);
1256        timespecclear(&it->it_time.it_interval);
1257        ITIMER_UNLOCK(it);
1258        callout_drain(&it->it_callout);
1259        ITIMER_LOCK(it);
1260        return (0);
1261}
1262
1263static int
1264realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1265{
1266        struct timespec cts;
1267
1268        mtx_assert(&it->it_mtx, MA_OWNED);
1269
1270        realtimer_clocktime(it->it_clockid, &cts);
1271        *ovalue = it->it_time;
1272        if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1273                timespecsub(&ovalue->it_value, &cts);
1274                if (ovalue->it_value.tv_sec < 0 ||
1275                    (ovalue->it_value.tv_sec == 0 &&
1276                     ovalue->it_value.tv_nsec == 0)) {
1277                        ovalue->it_value.tv_sec  = 0;
1278                        ovalue->it_value.tv_nsec = 1;
1279                }
1280        }
1281        return (0);
1282}
1283
1284static int
1285realtimer_settime(struct itimer *it, int flags,
1286        struct itimerspec *value, struct itimerspec *ovalue)
1287{
1288        struct timespec cts, ts;
1289        struct timeval tv;
1290        struct itimerspec val;
1291
1292        mtx_assert(&it->it_mtx, MA_OWNED);
1293
1294        val = *value;
1295        if (itimespecfix(&val.it_value))
1296                return (EINVAL);
1297
1298        if (timespecisset(&val.it_value)) {
1299                if (itimespecfix(&val.it_interval))
1300                        return (EINVAL);
1301        } else {
1302                timespecclear(&val.it_interval);
1303        }
1304       
1305        if (ovalue != NULL)
1306                realtimer_gettime(it, ovalue);
1307
1308        it->it_time = val;
1309        if (timespecisset(&val.it_value)) {
1310                realtimer_clocktime(it->it_clockid, &cts);
1311                ts = val.it_value;
1312                if ((flags & TIMER_ABSTIME) == 0) {
1313                        /* Convert to absolute time. */
1314                        timespecadd(&it->it_time.it_value, &cts);
1315                } else {
1316                        timespecsub(&ts, &cts);
1317                        /*
1318                         * We don't care if ts is negative, tztohz will
1319                         * fix it.
1320                         */
1321                }
1322                TIMESPEC_TO_TIMEVAL(&tv, &ts);
1323                callout_reset(&it->it_callout, tvtohz(&tv),
1324                        realtimer_expire, it);
1325        } else {
1326                callout_stop(&it->it_callout);
1327        }
1328
1329        return (0);
1330}
1331
1332static void
1333realtimer_clocktime(clockid_t id, struct timespec *ts)
1334{
1335        if (id == CLOCK_REALTIME)
1336                getnanotime(ts);
1337        else    /* CLOCK_MONOTONIC */
1338                getnanouptime(ts);
1339}
1340
1341int
1342itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1343{
1344        struct itimer *it;
1345
1346        PROC_LOCK_ASSERT(p, MA_OWNED);
1347        it = itimer_find(p, timerid);
1348        if (it != NULL) {
1349                ksi->ksi_overrun = it->it_overrun;
1350                it->it_overrun_last = it->it_overrun;
1351                it->it_overrun = 0;
1352                ITIMER_UNLOCK(it);
1353                return (0);
1354        }
1355        return (EINVAL);
1356}
1357
1358int
1359itimespecfix(struct timespec *ts)
1360{
1361
1362        if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1363                return (EINVAL);
1364        if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1365                ts->tv_nsec = tick * 1000;
1366        return (0);
1367}
1368
1369/* Timeout callback for realtime timer */
1370static void
1371realtimer_expire(void *arg)
1372{
1373        struct timespec cts, ts;
1374        struct timeval tv;
1375        struct itimer *it;
1376
1377        it = (struct itimer *)arg;
1378
1379        realtimer_clocktime(it->it_clockid, &cts);
1380        /* Only fire if time is reached. */
1381        if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1382                if (timespecisset(&it->it_time.it_interval)) {
1383                        timespecadd(&it->it_time.it_value,
1384                                    &it->it_time.it_interval);
1385                        while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1386                                if (it->it_overrun < INT_MAX)
1387                                        it->it_overrun++;
1388                                else
1389                                        it->it_ksi.ksi_errno = ERANGE;
1390                                timespecadd(&it->it_time.it_value,
1391                                            &it->it_time.it_interval);
1392                        }
1393                } else {
1394                        /* single shot timer ? */
1395                        timespecclear(&it->it_time.it_value);
1396                }
1397                if (timespecisset(&it->it_time.it_value)) {
1398                        ts = it->it_time.it_value;
1399                        timespecsub(&ts, &cts);
1400                        TIMESPEC_TO_TIMEVAL(&tv, &ts);
1401                        callout_reset(&it->it_callout, tvtohz(&tv),
1402                                 realtimer_expire, it);
1403                }
1404                itimer_enter(it);
1405                ITIMER_UNLOCK(it);
1406                itimer_fire(it);
1407                ITIMER_LOCK(it);
1408                itimer_leave(it);
1409        } else if (timespecisset(&it->it_time.it_value)) {
1410                ts = it->it_time.it_value;
1411                timespecsub(&ts, &cts);
1412                TIMESPEC_TO_TIMEVAL(&tv, &ts);
1413                callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1414                        it);
1415        }
1416}
1417
1418void
1419itimer_fire(struct itimer *it)
1420{
1421        struct proc *p = it->it_proc;
1422        int ret;
1423
1424        if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1425            it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1426                PROC_LOCK(p);
1427                if (!KSI_ONQ(&it->it_ksi)) {
1428                        it->it_ksi.ksi_errno = 0;
1429                        ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
1430                        if (__predict_false(ret != 0)) {
1431                                it->it_overrun++;
1432                                /*
1433                                 * Broken userland code, thread went
1434                                 * away, disarm the timer.
1435                                 */
1436                                if (ret == ESRCH) {
1437                                        ITIMER_LOCK(it);
1438                                        timespecclear(&it->it_time.it_value);
1439                                        timespecclear(&it->it_time.it_interval);
1440                                        callout_stop(&it->it_callout);
1441                                        ITIMER_UNLOCK(it);
1442                                }
1443                        }
1444                } else {
1445                        if (it->it_overrun < INT_MAX)
1446                                it->it_overrun++;
1447                        else
1448                                it->it_ksi.ksi_errno = ERANGE;
1449                }
1450                PROC_UNLOCK(p);
1451        }
1452}
1453
1454static void
1455itimers_alloc(struct proc *p)
1456{
1457        struct itimers *its;
1458        int i;
1459
1460        its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1461        LIST_INIT(&its->its_virtual);
1462        LIST_INIT(&its->its_prof);
1463        TAILQ_INIT(&its->its_worklist);
1464        for (i = 0; i < TIMER_MAX; i++)
1465                its->its_timers[i] = NULL;
1466        PROC_LOCK(p);
1467        if (p->p_itimers == NULL) {
1468                p->p_itimers = its;
1469                PROC_UNLOCK(p);
1470        }
1471        else {
1472                PROC_UNLOCK(p);
1473                free(its, M_SUBPROC);
1474        }
1475}
1476
1477static void
1478itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1479{
1480        itimers_event_hook_exit(arg, p);
1481}
1482
1483/* Clean up timers when some process events are being triggered. */
1484static void
1485itimers_event_hook_exit(void *arg, struct proc *p)
1486{
1487        struct itimers *its;
1488        struct itimer *it;
1489        int event = (int)(intptr_t)arg;
1490        int i;
1491
1492        if (p->p_itimers != NULL) {
1493                its = p->p_itimers;
1494                for (i = 0; i < MAX_CLOCKS; ++i) {
1495                        if (posix_clocks[i].event_hook != NULL)
1496                                CLOCK_CALL(i, event_hook, (p, i, event));
1497                }
1498                /*
1499                 * According to susv3, XSI interval timers should be inherited
1500                 * by new image.
1501                 */
1502                if (event == ITIMER_EV_EXEC)
1503                        i = 3;
1504                else if (event == ITIMER_EV_EXIT)
1505                        i = 0;
1506                else
1507                        panic("unhandled event");
1508                for (; i < TIMER_MAX; ++i) {
1509                        if ((it = its->its_timers[i]) != NULL)
1510                                kern_timer_delete(curthread, i);
1511                }
1512                if (its->its_timers[0] == NULL &&
1513                    its->its_timers[1] == NULL &&
1514                    its->its_timers[2] == NULL) {
1515                        free(its, M_SUBPROC);
1516                        p->p_itimers = NULL;
1517                }
1518        }
1519}
1520#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.