2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/limits.h>
38 #include <sys/clock.h>
40 #include <sys/mutex.h>
41 #include <sys/sysproto.h>
42 #include <sys/eventhandler.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/kernel.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
51 #include <sys/posix4.h>
53 #include <sys/timers.h>
54 #include <sys/timetc.h>
55 #include <sys/vnode.h>
58 #include <vm/vm_extern.h>
60 #define MAX_CLOCKS (CLOCK_MONOTONIC+1)
62 static struct kclock posix_clocks
[MAX_CLOCKS
];
63 static uma_zone_t itimer_zone
= NULL
;
66 * Time of day and interval timer support.
68 * These routines provide the kernel entry points to get and set
69 * the time-of-day and per-process interval timers. Subroutines
70 * here provide support for adding and subtracting timeval structures
71 * and decrementing interval timers, optionally reloading the interval
72 * timers when they expire.
75 static int settime(struct thread
*, struct timeval
*);
76 static void timevalfix(struct timeval
*);
77 static void no_lease_updatetime(int);
79 static void itimer_start(void);
80 static int itimer_init(void *, int, int);
81 static void itimer_fini(void *, int);
82 static void itimer_enter(struct itimer
*);
83 static void itimer_leave(struct itimer
*);
84 static struct itimer
*itimer_find(struct proc
*, int);
85 static void itimers_alloc(struct proc
*);
86 static void itimers_event_hook_exec(void *arg
, struct proc
*p
, struct image_params
*imgp
);
87 static void itimers_event_hook_exit(void *arg
, struct proc
*p
);
88 static int realtimer_create(struct itimer
*);
89 static int realtimer_gettime(struct itimer
*, struct itimerspec
*);
90 static int realtimer_settime(struct itimer
*, int,
91 struct itimerspec
*, struct itimerspec
*);
92 static int realtimer_delete(struct itimer
*);
93 static void realtimer_clocktime(clockid_t
, struct timespec
*);
94 static void realtimer_expire(void *);
95 static int kern_timer_create(struct thread
*, clockid_t
,
96 struct sigevent
*, int *, int);
97 static int kern_timer_delete(struct thread
*, int);
99 int register_posix_clock(int, struct kclock
*);
100 void itimer_fire(struct itimer
*it
);
101 int itimespecfix(struct timespec
*ts
);
103 #define CLOCK_CALL(clock, call, arglist) \
104 ((*posix_clocks[clock].call) arglist)
106 SYSINIT(posix_timer
, SI_SUB_P1003_1B
, SI_ORDER_FIRST
+4, itimer_start
, NULL
);
110 no_lease_updatetime(deltat
)
115 void (*lease_updatetime
)(int) = no_lease_updatetime
;
118 settime(struct thread
*td
, struct timeval
*tv
)
120 struct timeval delta
, tv1
, tv2
;
121 static struct timeval maxtime
, laststep
;
128 timevalsub(&delta
, &tv1
);
131 * If the system is secure, we do not allow the time to be
132 * set to a value earlier than 1 second less than the highest
133 * time we have yet seen. The worst a miscreant can do in
134 * this circumstance is "freeze" time. He couldn't go
137 * We similarly do not allow the clock to be stepped more
138 * than one second, nor more than once per second. This allows
139 * a miscreant to make the clock march double-time, but no worse.
141 if (securelevel_gt(td
->td_ucred
, 1) != 0) {
142 if (delta
.tv_sec
< 0 || delta
.tv_usec
< 0) {
144 * Update maxtime to latest time we've seen.
146 if (tv1
.tv_sec
> maxtime
.tv_sec
)
149 timevalsub(&tv2
, &maxtime
);
150 if (tv2
.tv_sec
< -1) {
151 tv
->tv_sec
= maxtime
.tv_sec
- 1;
152 printf("Time adjustment clamped to -1 second\n");
155 if (tv1
.tv_sec
== laststep
.tv_sec
) {
159 if (delta
.tv_sec
> 1) {
160 tv
->tv_sec
= tv1
.tv_sec
+ 1;
161 printf("Time adjustment clamped to +1 second\n");
167 ts
.tv_sec
= tv
->tv_sec
;
168 ts
.tv_nsec
= tv
->tv_usec
* 1000;
171 (void) splsoftclock();
172 lease_updatetime(delta
.tv_sec
);
179 #ifndef _SYS_SYSPROTO_H_
180 struct clock_gettime_args
{
187 clock_gettime(struct thread
*td
, struct clock_gettime_args
*uap
)
192 error
= kern_clock_gettime(td
, uap
->clock_id
, &ats
);
194 error
= copyout(&ats
, uap
->tp
, sizeof(ats
));
200 kern_clock_gettime(struct thread
*td
, clockid_t clock_id
, struct timespec
*ats
)
202 struct timeval sys
, user
;
204 uint64_t runtime
, curtime
, switchtime
;
208 case CLOCK_REALTIME
: /* Default to precise. */
209 case CLOCK_REALTIME_PRECISE
:
212 case CLOCK_REALTIME_FAST
:
218 calcru(p
, &user
, &sys
);
221 TIMEVAL_TO_TIMESPEC(&user
, ats
);
226 calcru(p
, &user
, &sys
);
229 timevaladd(&user
, &sys
);
230 TIMEVAL_TO_TIMESPEC(&user
, ats
);
232 case CLOCK_MONOTONIC
: /* Default to precise. */
233 case CLOCK_MONOTONIC_PRECISE
:
235 case CLOCK_UPTIME_PRECISE
:
238 case CLOCK_UPTIME_FAST
:
239 case CLOCK_MONOTONIC_FAST
:
243 ats
->tv_sec
= time_second
;
246 case CLOCK_THREAD_CPUTIME_ID
:
248 switchtime
= PCPU_GET(switchtime
);
249 curtime
= cpu_ticks();
250 runtime
= td
->td_runtime
;
252 runtime
= cputick2usec(runtime
+ curtime
- switchtime
);
253 ats
->tv_sec
= runtime
/ 1000000;
254 ats
->tv_nsec
= runtime
% 1000000 * 1000;
262 #ifndef _SYS_SYSPROTO_H_
263 struct clock_settime_args
{
265 const struct timespec
*tp
;
270 clock_settime(struct thread
*td
, struct clock_settime_args
*uap
)
275 if ((error
= copyin(uap
->tp
, &ats
, sizeof(ats
))) != 0)
277 return (kern_clock_settime(td
, uap
->clock_id
, &ats
));
281 kern_clock_settime(struct thread
*td
, clockid_t clock_id
, struct timespec
*ats
)
286 if ((error
= priv_check(td
, PRIV_CLOCK_SETTIME
)) != 0)
288 if (clock_id
!= CLOCK_REALTIME
)
290 if (ats
->tv_nsec
< 0 || ats
->tv_nsec
>= 1000000000)
292 /* XXX Don't convert nsec->usec and back */
293 TIMESPEC_TO_TIMEVAL(&atv
, ats
);
294 error
= settime(td
, &atv
);
298 #ifndef _SYS_SYSPROTO_H_
299 struct clock_getres_args
{
305 clock_getres(struct thread
*td
, struct clock_getres_args
*uap
)
313 error
= kern_clock_getres(td
, uap
->clock_id
, &ts
);
315 error
= copyout(&ts
, uap
->tp
, sizeof(ts
));
320 kern_clock_getres(struct thread
*td
, clockid_t clock_id
, struct timespec
*ts
)
326 case CLOCK_REALTIME_FAST
:
327 case CLOCK_REALTIME_PRECISE
:
328 case CLOCK_MONOTONIC
:
329 case CLOCK_MONOTONIC_FAST
:
330 case CLOCK_MONOTONIC_PRECISE
:
332 case CLOCK_UPTIME_FAST
:
333 case CLOCK_UPTIME_PRECISE
:
335 * Round up the result of the division cheaply by adding 1.
336 * Rounding up is especially important if rounding down
337 * would give 0. Perfect rounding is unimportant.
339 ts
->tv_nsec
= 1000000000 / tc_getfrequency() + 1;
343 /* Accurately round up here because we can do so cheaply. */
344 ts
->tv_nsec
= (1000000000 + hz
- 1) / hz
;
350 case CLOCK_THREAD_CPUTIME_ID
:
351 /* sync with cputick2usec */
352 ts
->tv_nsec
= 1000000 / cpu_tickrate();
353 if (ts
->tv_nsec
== 0)
365 kern_nanosleep(struct thread
*td
, struct timespec
*rqt
, struct timespec
*rmt
)
367 struct timespec ts
, ts2
, ts3
;
371 if (rqt
->tv_nsec
< 0 || rqt
->tv_nsec
>= 1000000000)
373 if (rqt
->tv_sec
< 0 || (rqt
->tv_sec
== 0 && rqt
->tv_nsec
== 0))
376 timespecadd(&ts
, rqt
);
377 TIMESPEC_TO_TIMEVAL(&tv
, rqt
);
379 error
= tsleep(&nanowait
, PWAIT
| PCATCH
, "nanslp",
382 if (error
!= EWOULDBLOCK
) {
383 if (error
== ERESTART
)
386 timespecsub(&ts
, &ts2
);
393 if (timespeccmp(&ts2
, &ts
, >=))
396 timespecsub(&ts3
, &ts2
);
397 TIMESPEC_TO_TIMEVAL(&tv
, &ts3
);
401 #ifndef _SYS_SYSPROTO_H_
402 struct nanosleep_args
{
403 struct timespec
*rqtp
;
404 struct timespec
*rmtp
;
409 nanosleep(struct thread
*td
, struct nanosleep_args
*uap
)
411 struct timespec rmt
, rqt
;
414 error
= copyin(uap
->rqtp
, &rqt
, sizeof(rqt
));
419 !useracc((caddr_t
)uap
->rmtp
, sizeof(rmt
), VM_PROT_WRITE
))
421 error
= kern_nanosleep(td
, &rqt
, &rmt
);
422 if (error
&& uap
->rmtp
) {
425 error2
= copyout(&rmt
, uap
->rmtp
, sizeof(rmt
));
432 #ifndef _SYS_SYSPROTO_H_
433 struct gettimeofday_args
{
435 struct timezone
*tzp
;
440 gettimeofday(struct thread
*td
, struct gettimeofday_args
*uap
)
448 error
= copyout(&atv
, uap
->tp
, sizeof (atv
));
450 if (error
== 0 && uap
->tzp
!= NULL
) {
451 rtz
.tz_minuteswest
= tz_minuteswest
;
452 rtz
.tz_dsttime
= tz_dsttime
;
453 error
= copyout(&rtz
, uap
->tzp
, sizeof (rtz
));
458 #ifndef _SYS_SYSPROTO_H_
459 struct settimeofday_args
{
461 struct timezone
*tzp
;
466 settimeofday(struct thread
*td
, struct settimeofday_args
*uap
)
468 struct timeval atv
, *tvp
;
469 struct timezone atz
, *tzp
;
473 error
= copyin(uap
->tv
, &atv
, sizeof(atv
));
480 error
= copyin(uap
->tzp
, &atz
, sizeof(atz
));
486 return (kern_settimeofday(td
, tvp
, tzp
));
490 kern_settimeofday(struct thread
*td
, struct timeval
*tv
, struct timezone
*tzp
)
494 error
= priv_check(td
, PRIV_SETTIMEOFDAY
);
497 /* Verify all parameters before changing time. */
499 if (tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
501 error
= settime(td
, tv
);
503 if (tzp
&& error
== 0) {
504 tz_minuteswest
= tzp
->tz_minuteswest
;
505 tz_dsttime
= tzp
->tz_dsttime
;
511 * Get value of an interval timer. The process virtual and profiling virtual
512 * time timers are kept in the p_stats area, since they can be swapped out.
513 * These are kept internally in the way they are specified externally: in
514 * time until they expire.
516 * The real time interval timer is kept in the process table slot for the
517 * process, and its value (it_value) is kept as an absolute time rather than
518 * as a delta, so that it is easy to keep periodic real-time signals from
521 * Virtual time timers are processed in the hardclock() routine of
522 * kern_clock.c. The real time timer is processed by a timeout routine,
523 * called from the softclock() routine. Since a callout may be delayed in
524 * real time due to interrupt processing in the system, it is possible for
525 * the real time timeout routine (realitexpire, given below), to be delayed
526 * in real time past when it is supposed to occur. It does not suffice,
527 * therefore, to reload the real timer .it_value from the real time timers
528 * .it_interval. Rather, we compute the next time in absolute time the timer
531 #ifndef _SYS_SYSPROTO_H_
532 struct getitimer_args
{
534 struct itimerval
*itv
;
538 getitimer(struct thread
*td
, struct getitimer_args
*uap
)
540 struct itimerval aitv
;
543 error
= kern_getitimer(td
, uap
->which
, &aitv
);
546 return (copyout(&aitv
, uap
->itv
, sizeof (struct itimerval
)));
550 kern_getitimer(struct thread
*td
, u_int which
, struct itimerval
*aitv
)
552 struct proc
*p
= td
->td_proc
;
555 if (which
> ITIMER_PROF
)
558 if (which
== ITIMER_REAL
) {
560 * Convert from absolute to relative time in .it_value
561 * part of real time timer. If time for real time timer
562 * has passed return 0, else return difference between
563 * current time and time for the timer to go off.
566 *aitv
= p
->p_realtimer
;
568 if (timevalisset(&aitv
->it_value
)) {
569 getmicrouptime(&ctv
);
570 if (timevalcmp(&aitv
->it_value
, &ctv
, <))
571 timevalclear(&aitv
->it_value
);
573 timevalsub(&aitv
->it_value
, &ctv
);
577 *aitv
= p
->p_stats
->p_timer
[which
];
583 #ifndef _SYS_SYSPROTO_H_
584 struct setitimer_args
{
586 struct itimerval
*itv
, *oitv
;
590 setitimer(struct thread
*td
, struct setitimer_args
*uap
)
592 struct itimerval aitv
, oitv
;
595 if (uap
->itv
== NULL
) {
596 uap
->itv
= uap
->oitv
;
597 return (getitimer(td
, (struct getitimer_args
*)uap
));
600 if ((error
= copyin(uap
->itv
, &aitv
, sizeof(struct itimerval
))))
602 error
= kern_setitimer(td
, uap
->which
, &aitv
, &oitv
);
603 if (error
!= 0 || uap
->oitv
== NULL
)
605 return (copyout(&oitv
, uap
->oitv
, sizeof(struct itimerval
)));
609 kern_setitimer(struct thread
*td
, u_int which
, struct itimerval
*aitv
,
610 struct itimerval
*oitv
)
612 struct proc
*p
= td
->td_proc
;
616 return (kern_getitimer(td
, which
, oitv
));
618 if (which
> ITIMER_PROF
)
620 if (itimerfix(&aitv
->it_value
))
622 if (!timevalisset(&aitv
->it_value
))
623 timevalclear(&aitv
->it_interval
);
624 else if (itimerfix(&aitv
->it_interval
))
627 if (which
== ITIMER_REAL
) {
629 if (timevalisset(&p
->p_realtimer
.it_value
))
630 callout_stop(&p
->p_itcallout
);
631 getmicrouptime(&ctv
);
632 if (timevalisset(&aitv
->it_value
)) {
633 callout_reset(&p
->p_itcallout
, tvtohz(&aitv
->it_value
),
635 timevaladd(&aitv
->it_value
, &ctv
);
637 *oitv
= p
->p_realtimer
;
638 p
->p_realtimer
= *aitv
;
640 if (timevalisset(&oitv
->it_value
)) {
641 if (timevalcmp(&oitv
->it_value
, &ctv
, <))
642 timevalclear(&oitv
->it_value
);
644 timevalsub(&oitv
->it_value
, &ctv
);
648 *oitv
= p
->p_stats
->p_timer
[which
];
649 p
->p_stats
->p_timer
[which
] = *aitv
;
656 * Real interval timer expired:
657 * send process whose timer expired an alarm signal.
658 * If time is not set up to reload, then just return.
659 * Else compute next time timer should go off which is > current time.
660 * This is where delay in processing this timeout causes multiple
661 * SIGALRM calls to be compressed into one.
662 * tvtohz() always adds 1 to allow for the time until the next clock
663 * interrupt being strictly less than 1 clock tick, but we don't want
664 * that here since we want to appear to be in sync with the clock
665 * interrupt even when we're delayed.
668 realitexpire(void *arg
)
671 struct timeval ctv
, ntv
;
673 p
= (struct proc
*)arg
;
676 if (!timevalisset(&p
->p_realtimer
.it_interval
)) {
677 timevalclear(&p
->p_realtimer
.it_value
);
678 if (p
->p_flag
& P_WEXIT
)
679 wakeup(&p
->p_itcallout
);
684 timevaladd(&p
->p_realtimer
.it_value
,
685 &p
->p_realtimer
.it_interval
);
686 getmicrouptime(&ctv
);
687 if (timevalcmp(&p
->p_realtimer
.it_value
, &ctv
, >)) {
688 ntv
= p
->p_realtimer
.it_value
;
689 timevalsub(&ntv
, &ctv
);
690 callout_reset(&p
->p_itcallout
, tvtohz(&ntv
) - 1,
700 * Check that a proposed value to load into the .it_value or
701 * .it_interval part of an interval timer is acceptable, and
702 * fix it to have at least minimal value (i.e. if it is less
703 * than the resolution of the clock, round it up.)
706 itimerfix(struct timeval
*tv
)
709 if (tv
->tv_sec
< 0 || tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
711 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
717 * Decrement an interval timer by a specified number
718 * of microseconds, which must be less than a second,
719 * i.e. < 1000000. If the timer expires, then reload
720 * it. In this case, carry over (usec - old value) to
721 * reduce the value reloaded into the timer so that
722 * the timer does not drift. This routine assumes
723 * that it is called in a context where the timers
724 * on which it is operating cannot change in value.
727 itimerdecr(struct itimerval
*itp
, int usec
)
730 if (itp
->it_value
.tv_usec
< usec
) {
731 if (itp
->it_value
.tv_sec
== 0) {
732 /* expired, and already in next interval */
733 usec
-= itp
->it_value
.tv_usec
;
736 itp
->it_value
.tv_usec
+= 1000000;
737 itp
->it_value
.tv_sec
--;
739 itp
->it_value
.tv_usec
-= usec
;
741 if (timevalisset(&itp
->it_value
))
743 /* expired, exactly at end of interval */
745 if (timevalisset(&itp
->it_interval
)) {
746 itp
->it_value
= itp
->it_interval
;
747 itp
->it_value
.tv_usec
-= usec
;
748 if (itp
->it_value
.tv_usec
< 0) {
749 itp
->it_value
.tv_usec
+= 1000000;
750 itp
->it_value
.tv_sec
--;
753 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
758 * Add and subtract routines for timevals.
759 * N.B.: subtract routine doesn't deal with
760 * results which are before the beginning,
761 * it just gets very confused in this case.
765 timevaladd(struct timeval
*t1
, const struct timeval
*t2
)
768 t1
->tv_sec
+= t2
->tv_sec
;
769 t1
->tv_usec
+= t2
->tv_usec
;
774 timevalsub(struct timeval
*t1
, const struct timeval
*t2
)
777 t1
->tv_sec
-= t2
->tv_sec
;
778 t1
->tv_usec
-= t2
->tv_usec
;
783 timevalfix(struct timeval
*t1
)
786 if (t1
->tv_usec
< 0) {
788 t1
->tv_usec
+= 1000000;
790 if (t1
->tv_usec
>= 1000000) {
792 t1
->tv_usec
-= 1000000;
797 * ratecheck(): simple time-based rate-limit checking.
800 ratecheck(struct timeval
*lasttime
, const struct timeval
*mininterval
)
802 struct timeval tv
, delta
;
805 getmicrouptime(&tv
); /* NB: 10ms precision */
807 timevalsub(&delta
, lasttime
);
810 * check for 0,0 is so that the message will be seen at least once,
811 * even if interval is huge.
813 if (timevalcmp(&delta
, mininterval
, >=) ||
814 (lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0)) {
823 * ppsratecheck(): packets (or events) per second limitation.
825 * Return 0 if the limit is to be enforced (e.g. the caller
826 * should drop a packet because of the rate limitation).
828 * maxpps of 0 always causes zero to be returned. maxpps of -1
829 * always causes 1 to be returned; this effectively defeats rate
832 * Note that we maintain the struct timeval for compatibility
833 * with other bsd systems. We reuse the storage and just monitor
834 * clock ticks for minimal overhead.
837 ppsratecheck(struct timeval
*lasttime
, int *curpps
, int maxpps
)
842 * Reset the last time and counter if this is the first call
843 * or more than a second has passed since the last update of
847 if (lasttime
->tv_sec
== 0 || (u_int
)(now
- lasttime
->tv_sec
) >= hz
) {
848 lasttime
->tv_sec
= now
;
850 return (maxpps
!= 0);
852 (*curpps
)++; /* NB: ignore potential overflow */
853 return (maxpps
< 0 || *curpps
< maxpps
);
860 struct kclock rt_clock
= {
861 .timer_create
= realtimer_create
,
862 .timer_delete
= realtimer_delete
,
863 .timer_settime
= realtimer_settime
,
864 .timer_gettime
= realtimer_gettime
,
868 itimer_zone
= uma_zcreate("itimer", sizeof(struct itimer
),
869 NULL
, NULL
, itimer_init
, itimer_fini
, UMA_ALIGN_PTR
, 0);
870 register_posix_clock(CLOCK_REALTIME
, &rt_clock
);
871 register_posix_clock(CLOCK_MONOTONIC
, &rt_clock
);
872 p31b_setcfg(CTL_P1003_1B_TIMERS
, 200112L);
873 p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX
, INT_MAX
);
874 p31b_setcfg(CTL_P1003_1B_TIMER_MAX
, TIMER_MAX
);
875 EVENTHANDLER_REGISTER(process_exit
, itimers_event_hook_exit
,
876 (void *)ITIMER_EV_EXIT
, EVENTHANDLER_PRI_ANY
);
877 EVENTHANDLER_REGISTER(process_exec
, itimers_event_hook_exec
,
878 (void *)ITIMER_EV_EXEC
, EVENTHANDLER_PRI_ANY
);
882 register_posix_clock(int clockid
, struct kclock
*clk
)
884 if ((unsigned)clockid
>= MAX_CLOCKS
) {
885 printf("%s: invalid clockid\n", __func__
);
888 posix_clocks
[clockid
] = *clk
;
893 itimer_init(void *mem
, int size
, int flags
)
897 it
= (struct itimer
*)mem
;
898 mtx_init(&it
->it_mtx
, "itimer lock", NULL
, MTX_DEF
);
903 itimer_fini(void *mem
, int size
)
907 it
= (struct itimer
*)mem
;
908 mtx_destroy(&it
->it_mtx
);
912 itimer_enter(struct itimer
*it
)
915 mtx_assert(&it
->it_mtx
, MA_OWNED
);
920 itimer_leave(struct itimer
*it
)
923 mtx_assert(&it
->it_mtx
, MA_OWNED
);
924 KASSERT(it
->it_usecount
> 0, ("invalid it_usecount"));
926 if (--it
->it_usecount
== 0 && (it
->it_flags
& ITF_WANTED
) != 0)
930 #ifndef _SYS_SYSPROTO_H_
931 struct ktimer_create_args
{
933 struct sigevent
* evp
;
938 ktimer_create(struct thread
*td
, struct ktimer_create_args
*uap
)
940 struct sigevent
*evp1
, ev
;
944 if (uap
->evp
!= NULL
) {
945 error
= copyin(uap
->evp
, &ev
, sizeof(ev
));
952 error
= kern_timer_create(td
, uap
->clock_id
, evp1
, &id
, -1);
955 error
= copyout(&id
, uap
->timerid
, sizeof(int));
957 kern_timer_delete(td
, id
);
963 kern_timer_create(struct thread
*td
, clockid_t clock_id
,
964 struct sigevent
*evp
, int *timerid
, int preset_id
)
966 struct proc
*p
= td
->td_proc
;
971 if (clock_id
< 0 || clock_id
>= MAX_CLOCKS
)
974 if (posix_clocks
[clock_id
].timer_create
== NULL
)
978 if (evp
->sigev_notify
!= SIGEV_NONE
&&
979 evp
->sigev_notify
!= SIGEV_SIGNAL
&&
980 evp
->sigev_notify
!= SIGEV_THREAD_ID
)
982 if ((evp
->sigev_notify
== SIGEV_SIGNAL
||
983 evp
->sigev_notify
== SIGEV_THREAD_ID
) &&
984 !_SIG_VALID(evp
->sigev_signo
))
988 if (p
->p_itimers
== NULL
)
991 it
= uma_zalloc(itimer_zone
, M_WAITOK
);
995 timespecclear(&it
->it_time
.it_value
);
996 timespecclear(&it
->it_time
.it_interval
);
998 it
->it_overrun_last
= 0;
999 it
->it_clockid
= clock_id
;
1000 it
->it_timerid
= -1;
1002 ksiginfo_init(&it
->it_ksi
);
1003 it
->it_ksi
.ksi_flags
|= KSI_INS
| KSI_EXT
;
1004 error
= CLOCK_CALL(clock_id
, timer_create
, (it
));
1009 if (preset_id
!= -1) {
1010 KASSERT(preset_id
>= 0 && preset_id
< 3, ("invalid preset_id"));
1012 if (p
->p_itimers
->its_timers
[id
] != NULL
) {
1019 * Find a free timer slot, skipping those reserved
1022 for (id
= 3; id
< TIMER_MAX
; id
++)
1023 if (p
->p_itimers
->its_timers
[id
] == NULL
)
1025 if (id
== TIMER_MAX
) {
1031 it
->it_timerid
= id
;
1032 p
->p_itimers
->its_timers
[id
] = it
;
1034 it
->it_sigev
= *evp
;
1036 it
->it_sigev
.sigev_notify
= SIGEV_SIGNAL
;
1039 case CLOCK_REALTIME
:
1040 it
->it_sigev
.sigev_signo
= SIGALRM
;
1043 it
->it_sigev
.sigev_signo
= SIGVTALRM
;
1046 it
->it_sigev
.sigev_signo
= SIGPROF
;
1049 it
->it_sigev
.sigev_value
.sival_int
= id
;
1052 if (it
->it_sigev
.sigev_notify
== SIGEV_SIGNAL
||
1053 it
->it_sigev
.sigev_notify
== SIGEV_THREAD_ID
) {
1054 it
->it_ksi
.ksi_signo
= it
->it_sigev
.sigev_signo
;
1055 it
->it_ksi
.ksi_code
= SI_TIMER
;
1056 it
->it_ksi
.ksi_value
= it
->it_sigev
.sigev_value
;
1057 it
->it_ksi
.ksi_timerid
= id
;
1065 CLOCK_CALL(it
->it_clockid
, timer_delete
, (it
));
1067 uma_zfree(itimer_zone
, it
);
1071 #ifndef _SYS_SYSPROTO_H_
1072 struct ktimer_delete_args
{
1077 ktimer_delete(struct thread
*td
, struct ktimer_delete_args
*uap
)
1079 return (kern_timer_delete(td
, uap
->timerid
));
1082 static struct itimer
*
1083 itimer_find(struct proc
*p
, int timerid
)
1087 PROC_LOCK_ASSERT(p
, MA_OWNED
);
1088 if ((p
->p_itimers
== NULL
) || (timerid
>= TIMER_MAX
) ||
1089 (it
= p
->p_itimers
->its_timers
[timerid
]) == NULL
) {
1093 if ((it
->it_flags
& ITF_DELETING
) != 0) {
1101 kern_timer_delete(struct thread
*td
, int timerid
)
1103 struct proc
*p
= td
->td_proc
;
1107 it
= itimer_find(p
, timerid
);
1114 it
->it_flags
|= ITF_DELETING
;
1115 while (it
->it_usecount
> 0) {
1116 it
->it_flags
|= ITF_WANTED
;
1117 msleep(it
, &it
->it_mtx
, PPAUSE
, "itimer", 0);
1119 it
->it_flags
&= ~ITF_WANTED
;
1120 CLOCK_CALL(it
->it_clockid
, timer_delete
, (it
));
1124 if (KSI_ONQ(&it
->it_ksi
))
1125 sigqueue_take(&it
->it_ksi
);
1126 p
->p_itimers
->its_timers
[timerid
] = NULL
;
1128 uma_zfree(itimer_zone
, it
);
1132 #ifndef _SYS_SYSPROTO_H_
1133 struct ktimer_settime_args
{
1136 const struct itimerspec
* value
;
1137 struct itimerspec
* ovalue
;
1141 ktimer_settime(struct thread
*td
, struct ktimer_settime_args
*uap
)
1143 struct proc
*p
= td
->td_proc
;
1145 struct itimerspec val
, oval
, *ovalp
;
1148 error
= copyin(uap
->value
, &val
, sizeof(val
));
1152 if (uap
->ovalue
!= NULL
)
1158 if (uap
->timerid
< 3 ||
1159 (it
= itimer_find(p
, uap
->timerid
)) == NULL
) {
1165 error
= CLOCK_CALL(it
->it_clockid
, timer_settime
,
1166 (it
, uap
->flags
, &val
, ovalp
));
1170 if (error
== 0 && uap
->ovalue
!= NULL
)
1171 error
= copyout(ovalp
, uap
->ovalue
, sizeof(*ovalp
));
1175 #ifndef _SYS_SYSPROTO_H_
1176 struct ktimer_gettime_args
{
1178 struct itimerspec
* value
;
1182 ktimer_gettime(struct thread
*td
, struct ktimer_gettime_args
*uap
)
1184 struct proc
*p
= td
->td_proc
;
1186 struct itimerspec val
;
1190 if (uap
->timerid
< 3 ||
1191 (it
= itimer_find(p
, uap
->timerid
)) == NULL
) {
1197 error
= CLOCK_CALL(it
->it_clockid
, timer_gettime
,
1203 error
= copyout(&val
, uap
->value
, sizeof(val
));
1207 #ifndef _SYS_SYSPROTO_H_
1208 struct timer_getoverrun_args
{
1213 ktimer_getoverrun(struct thread
*td
, struct ktimer_getoverrun_args
*uap
)
1215 struct proc
*p
= td
->td_proc
;
1220 if (uap
->timerid
< 3 ||
1221 (it
= itimer_find(p
, uap
->timerid
)) == NULL
) {
1225 td
->td_retval
[0] = it
->it_overrun_last
;
1234 realtimer_create(struct itimer
*it
)
1236 callout_init_mtx(&it
->it_callout
, &it
->it_mtx
, 0);
1241 realtimer_delete(struct itimer
*it
)
1243 mtx_assert(&it
->it_mtx
, MA_OWNED
);
1246 callout_drain(&it
->it_callout
);
1252 realtimer_gettime(struct itimer
*it
, struct itimerspec
*ovalue
)
1254 struct timespec cts
;
1256 mtx_assert(&it
->it_mtx
, MA_OWNED
);
1258 realtimer_clocktime(it
->it_clockid
, &cts
);
1259 *ovalue
= it
->it_time
;
1260 if (ovalue
->it_value
.tv_sec
!= 0 || ovalue
->it_value
.tv_nsec
!= 0) {
1261 timespecsub(&ovalue
->it_value
, &cts
);
1262 if (ovalue
->it_value
.tv_sec
< 0 ||
1263 (ovalue
->it_value
.tv_sec
== 0 &&
1264 ovalue
->it_value
.tv_nsec
== 0)) {
1265 ovalue
->it_value
.tv_sec
= 0;
1266 ovalue
->it_value
.tv_nsec
= 1;
1273 realtimer_settime(struct itimer
*it
, int flags
,
1274 struct itimerspec
*value
, struct itimerspec
*ovalue
)
1276 struct timespec cts
, ts
;
1278 struct itimerspec val
;
1280 mtx_assert(&it
->it_mtx
, MA_OWNED
);
1283 if (itimespecfix(&val
.it_value
))
1286 if (timespecisset(&val
.it_value
)) {
1287 if (itimespecfix(&val
.it_interval
))
1290 timespecclear(&val
.it_interval
);
1294 realtimer_gettime(it
, ovalue
);
1297 if (timespecisset(&val
.it_value
)) {
1298 realtimer_clocktime(it
->it_clockid
, &cts
);
1300 if ((flags
& TIMER_ABSTIME
) == 0) {
1301 /* Convert to absolute time. */
1302 timespecadd(&it
->it_time
.it_value
, &cts
);
1304 timespecsub(&ts
, &cts
);
1306 * We don't care if ts is negative, tztohz will
1310 TIMESPEC_TO_TIMEVAL(&tv
, &ts
);
1311 callout_reset(&it
->it_callout
, tvtohz(&tv
),
1312 realtimer_expire
, it
);
1314 callout_stop(&it
->it_callout
);
1321 realtimer_clocktime(clockid_t id
, struct timespec
*ts
)
1323 if (id
== CLOCK_REALTIME
)
1325 else /* CLOCK_MONOTONIC */
1330 itimer_accept(struct proc
*p
, int timerid
, ksiginfo_t
*ksi
)
1334 PROC_LOCK_ASSERT(p
, MA_OWNED
);
1335 it
= itimer_find(p
, timerid
);
1337 ksi
->ksi_overrun
= it
->it_overrun
;
1338 it
->it_overrun_last
= it
->it_overrun
;
1347 itimespecfix(struct timespec
*ts
)
1350 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0 || ts
->tv_nsec
>= 1000000000)
1352 if (ts
->tv_sec
== 0 && ts
->tv_nsec
!= 0 && ts
->tv_nsec
< tick
* 1000)
1353 ts
->tv_nsec
= tick
* 1000;
1357 /* Timeout callback for realtime timer */
1359 realtimer_expire(void *arg
)
1361 struct timespec cts
, ts
;
1366 it
= (struct itimer
*)arg
;
1369 realtimer_clocktime(it
->it_clockid
, &cts
);
1370 /* Only fire if time is reached. */
1371 if (timespeccmp(&cts
, &it
->it_time
.it_value
, >=)) {
1372 if (timespecisset(&it
->it_time
.it_interval
)) {
1373 timespecadd(&it
->it_time
.it_value
,
1374 &it
->it_time
.it_interval
);
1375 while (timespeccmp(&cts
, &it
->it_time
.it_value
, >=)) {
1376 if (it
->it_overrun
< INT_MAX
)
1379 it
->it_ksi
.ksi_errno
= ERANGE
;
1380 timespecadd(&it
->it_time
.it_value
,
1381 &it
->it_time
.it_interval
);
1384 /* single shot timer ? */
1385 timespecclear(&it
->it_time
.it_value
);
1387 if (timespecisset(&it
->it_time
.it_value
)) {
1388 ts
= it
->it_time
.it_value
;
1389 timespecsub(&ts
, &cts
);
1390 TIMESPEC_TO_TIMEVAL(&tv
, &ts
);
1391 callout_reset(&it
->it_callout
, tvtohz(&tv
),
1392 realtimer_expire
, it
);
1397 } else 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
), realtimer_expire
,
1407 itimer_fire(struct itimer
*it
)
1409 struct proc
*p
= it
->it_proc
;
1412 if (it
->it_sigev
.sigev_notify
== SIGEV_SIGNAL
||
1413 it
->it_sigev
.sigev_notify
== SIGEV_THREAD_ID
) {
1415 if (!KSI_ONQ(&it
->it_ksi
)) {
1416 it
->it_ksi
.ksi_errno
= 0;
1417 ret
= psignal_event(p
, &it
->it_sigev
, &it
->it_ksi
);
1418 if (__predict_false(ret
!= 0)) {
1421 * Broken userland code, thread went
1422 * away, disarm the timer.
1426 timespecclear(&it
->it_time
.it_value
);
1427 timespecclear(&it
->it_time
.it_interval
);
1428 callout_stop(&it
->it_callout
);
1433 if (it
->it_overrun
< INT_MAX
)
1436 it
->it_ksi
.ksi_errno
= ERANGE
;
1443 itimers_alloc(struct proc
*p
)
1445 struct itimers
*its
;
1448 its
= malloc(sizeof (struct itimers
), M_SUBPROC
, M_WAITOK
| M_ZERO
);
1449 LIST_INIT(&its
->its_virtual
);
1450 LIST_INIT(&its
->its_prof
);
1451 TAILQ_INIT(&its
->its_worklist
);
1452 for (i
= 0; i
< TIMER_MAX
; i
++)
1453 its
->its_timers
[i
] = NULL
;
1455 if (p
->p_itimers
== NULL
) {
1461 free(its
, M_SUBPROC
);
1466 itimers_event_hook_exec(void *arg
, struct proc
*p
, struct image_params
*imgp __unused
)
1468 itimers_event_hook_exit(arg
, p
);
1471 /* Clean up timers when some process events are being triggered. */
1473 itimers_event_hook_exit(void *arg
, struct proc
*p
)
1475 struct itimers
*its
;
1477 int event
= (int)(intptr_t)arg
;
1480 if (p
->p_itimers
!= NULL
) {
1482 for (i
= 0; i
< MAX_CLOCKS
; ++i
) {
1483 if (posix_clocks
[i
].event_hook
!= NULL
)
1484 CLOCK_CALL(i
, event_hook
, (p
, i
, event
));
1487 * According to susv3, XSI interval timers should be inherited
1490 if (event
== ITIMER_EV_EXEC
)
1492 else if (event
== ITIMER_EV_EXIT
)
1495 panic("unhandled event");
1496 for (; i
< TIMER_MAX
; ++i
) {
1497 if ((it
= its
->its_timers
[i
]) != NULL
)
1498 kern_timer_delete(curthread
, i
);
1500 if (its
->its_timers
[0] == NULL
&&
1501 its
->its_timers
[1] == NULL
&&
1502 its
->its_timers
[2] == NULL
) {
1503 free(its
, M_SUBPROC
);
1504 p
->p_itimers
= NULL
;