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