19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)kern_time.c 8.1 (Berkeley) 6/10/93 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 35df8bae1dSRodney W. Grimes #include <sys/param.h> 36e96c1fdcSPeter Wemm #include <sys/systm.h> 37aff5bcb1SDavid Xu #include <sys/limits.h> 38f645b0b5SPoul-Henning Kamp #include <sys/clock.h> 39fb919e4dSMark Murray #include <sys/lock.h> 40fb919e4dSMark Murray #include <sys/mutex.h> 41d2d3e875SBruce Evans #include <sys/sysproto.h> 42d26b1a1fSDavid Xu #include <sys/eventhandler.h> 43df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 44797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 45df8bae1dSRodney W. Grimes #include <sys/kernel.h> 46098176f0SDavide Italiano #include <sys/sleepqueue.h> 47efa42cbcSPaul Saab #include <sys/syscallsubr.h> 4877e718f7SDavid Xu #include <sys/sysctl.h> 4994c8fcd8SPeter Wemm #include <sys/sysent.h> 50acd3428bSRobert Watson #include <sys/priv.h> 51df8bae1dSRodney W. Grimes #include <sys/proc.h> 526aeb05d7STom Rhodes #include <sys/posix4.h> 53708e7684SPeter Wemm #include <sys/time.h> 5486857b36SDavid Xu #include <sys/timers.h> 5591266b96SPoul-Henning Kamp #include <sys/timetc.h> 56df8bae1dSRodney W. Grimes #include <sys/vnode.h> 57fb919e4dSMark Murray 585b870b7bSPeter Wemm #include <vm/vm.h> 595b870b7bSPeter Wemm #include <vm/vm_extern.h> 60df8bae1dSRodney W. Grimes 6186857b36SDavid Xu #define MAX_CLOCKS (CLOCK_MONOTONIC+1) 62d65f1abcSDavid Xu #define CPUCLOCK_BIT 0x80000000 63d65f1abcSDavid Xu #define CPUCLOCK_PROCESS_BIT 0x40000000 64d65f1abcSDavid Xu #define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT)) 65d65f1abcSDavid Xu #define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid)) 66d65f1abcSDavid Xu #define MAKE_PROCESS_CPUCLOCK(pid) \ 67d65f1abcSDavid Xu (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid)) 6886857b36SDavid Xu 6986857b36SDavid Xu static struct kclock posix_clocks[MAX_CLOCKS]; 7086857b36SDavid Xu static uma_zone_t itimer_zone = NULL; 7186857b36SDavid Xu 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Time of day and interval timer support. 74df8bae1dSRodney W. Grimes * 75df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 76df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 77df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 78df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 79df8bae1dSRodney W. Grimes * timers when they expire. 80df8bae1dSRodney W. Grimes */ 81df8bae1dSRodney W. Grimes 827edfb592SJohn Baldwin static int settime(struct thread *, struct timeval *); 834d77a549SAlfred Perlstein static void timevalfix(struct timeval *); 8494c8fcd8SPeter Wemm 8586857b36SDavid Xu static void itimer_start(void); 8686857b36SDavid Xu static int itimer_init(void *, int, int); 8786857b36SDavid Xu static void itimer_fini(void *, int); 8886857b36SDavid Xu static void itimer_enter(struct itimer *); 8986857b36SDavid Xu static void itimer_leave(struct itimer *); 90843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int); 9186857b36SDavid Xu static void itimers_alloc(struct proc *); 92993182e5SAlexander Leidinger static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp); 93993182e5SAlexander Leidinger static void itimers_event_hook_exit(void *arg, struct proc *p); 9486857b36SDavid Xu static int realtimer_create(struct itimer *); 9586857b36SDavid Xu static int realtimer_gettime(struct itimer *, struct itimerspec *); 9686857b36SDavid Xu static int realtimer_settime(struct itimer *, int, 9786857b36SDavid Xu struct itimerspec *, struct itimerspec *); 9886857b36SDavid Xu static int realtimer_delete(struct itimer *); 9956c06c4bSDavid Xu static void realtimer_clocktime(clockid_t, struct timespec *); 10086857b36SDavid Xu static void realtimer_expire(void *); 10186857b36SDavid Xu static int kern_timer_create(struct thread *, clockid_t, 10261d3a4efSDavid Xu struct sigevent *, int *, int); 10361d3a4efSDavid Xu static int kern_timer_delete(struct thread *, int); 10486857b36SDavid Xu 10586857b36SDavid Xu int register_posix_clock(int, struct kclock *); 10686857b36SDavid Xu void itimer_fire(struct itimer *it); 10756c06c4bSDavid Xu int itimespecfix(struct timespec *ts); 10886857b36SDavid Xu 10986857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist) \ 11086857b36SDavid Xu ((*posix_clocks[clock].call) arglist) 11186857b36SDavid Xu 11286857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 11386857b36SDavid Xu 11486857b36SDavid Xu 11594c8fcd8SPeter Wemm static int 11691afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 11794c8fcd8SPeter Wemm { 118fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 119c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 1207ec73f64SPoul-Henning Kamp struct timespec ts; 12194c8fcd8SPeter Wemm int s; 12294c8fcd8SPeter Wemm 123708e7684SPeter Wemm s = splclock(); 1249c8fff87SBruce Evans microtime(&tv1); 12500af9731SPoul-Henning Kamp delta = *tv; 12600af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 12794c8fcd8SPeter Wemm 12894c8fcd8SPeter Wemm /* 1299c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 130fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 131fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 132fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 133fcae3aa6SNick Sayer * back to the past. 134c0bd94a7SNick Sayer * 135c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 136c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 137c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 13894c8fcd8SPeter Wemm */ 1397edfb592SJohn Baldwin if (securelevel_gt(td->td_ucred, 1) != 0) { 140fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1413f92429aSMatt Jacob /* 142c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1433f92429aSMatt Jacob */ 144fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 145fcae3aa6SNick Sayer maxtime = tv1; 146fcae3aa6SNick Sayer tv2 = *tv; 147fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 148fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1493f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 150fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 151fcae3aa6SNick Sayer } 1523f92429aSMatt Jacob } else { 153c0bd94a7SNick Sayer if (tv1.tv_sec == laststep.tv_sec) { 154c0bd94a7SNick Sayer splx(s); 155c0bd94a7SNick Sayer return (EPERM); 156c0bd94a7SNick Sayer } 157c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 158c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 159c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 160c0bd94a7SNick Sayer } 161c0bd94a7SNick Sayer laststep = *tv; 162fcae3aa6SNick Sayer } 1639c8fff87SBruce Evans } 1649c8fff87SBruce Evans 1657ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1667ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 1677edfb592SJohn Baldwin mtx_lock(&Giant); 16891266b96SPoul-Henning Kamp tc_setclock(&ts); 16994c8fcd8SPeter Wemm resettodr(); 1707edfb592SJohn Baldwin mtx_unlock(&Giant); 17194c8fcd8SPeter Wemm return (0); 17294c8fcd8SPeter Wemm } 17394c8fcd8SPeter Wemm 17494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 175d65f1abcSDavid Xu struct clock_getcpuclockid2_args { 176d65f1abcSDavid Xu id_t id; 177d65f1abcSDavid Xu int which, 178d65f1abcSDavid Xu clockid_t *clock_id; 179d65f1abcSDavid Xu }; 180d65f1abcSDavid Xu #endif 181d65f1abcSDavid Xu /* ARGSUSED */ 182d65f1abcSDavid Xu int 183d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap) 184d65f1abcSDavid Xu { 185d65f1abcSDavid Xu clockid_t clk_id; 186*d31e4b3aSKonstantin Belousov int error; 187*d31e4b3aSKonstantin Belousov 188*d31e4b3aSKonstantin Belousov error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id); 189*d31e4b3aSKonstantin Belousov if (error == 0) 190*d31e4b3aSKonstantin Belousov error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 191*d31e4b3aSKonstantin Belousov return (error); 192*d31e4b3aSKonstantin Belousov } 193*d31e4b3aSKonstantin Belousov 194*d31e4b3aSKonstantin Belousov int 195*d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which, 196*d31e4b3aSKonstantin Belousov clockid_t *clk_id) 197*d31e4b3aSKonstantin Belousov { 198d65f1abcSDavid Xu struct proc *p; 199d65f1abcSDavid Xu pid_t pid; 200d65f1abcSDavid Xu lwpid_t tid; 201d65f1abcSDavid Xu int error; 202d65f1abcSDavid Xu 203*d31e4b3aSKonstantin Belousov switch (which) { 204d65f1abcSDavid Xu case CPUCLOCK_WHICH_PID: 205*d31e4b3aSKonstantin Belousov if (id != 0) { 206*d31e4b3aSKonstantin Belousov p = pfind(id); 207d65f1abcSDavid Xu if (p == NULL) 208d65f1abcSDavid Xu return (ESRCH); 209d65f1abcSDavid Xu error = p_cansee(td, p); 210d65f1abcSDavid Xu PROC_UNLOCK(p); 211*d31e4b3aSKonstantin Belousov if (error != 0) 212d65f1abcSDavid Xu return (error); 213*d31e4b3aSKonstantin Belousov pid = id; 214d65f1abcSDavid Xu } else { 215d65f1abcSDavid Xu pid = td->td_proc->p_pid; 216d65f1abcSDavid Xu } 217*d31e4b3aSKonstantin Belousov *clk_id = MAKE_PROCESS_CPUCLOCK(pid); 218*d31e4b3aSKonstantin Belousov return (0); 219d65f1abcSDavid Xu case CPUCLOCK_WHICH_TID: 220*d31e4b3aSKonstantin Belousov tid = id == 0 ? td->td_tid : id; 221*d31e4b3aSKonstantin Belousov *clk_id = MAKE_THREAD_CPUCLOCK(tid); 222*d31e4b3aSKonstantin Belousov return (0); 223d65f1abcSDavid Xu default: 224d65f1abcSDavid Xu return (EINVAL); 225d65f1abcSDavid Xu } 226d65f1abcSDavid Xu } 227d65f1abcSDavid Xu 228d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_ 22994c8fcd8SPeter Wemm struct clock_gettime_args { 23094c8fcd8SPeter Wemm clockid_t clock_id; 23194c8fcd8SPeter Wemm struct timespec *tp; 23294c8fcd8SPeter Wemm }; 23394c8fcd8SPeter Wemm #endif 23494c8fcd8SPeter Wemm /* ARGSUSED */ 23594c8fcd8SPeter Wemm int 2368451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) 23794c8fcd8SPeter Wemm { 23894c8fcd8SPeter Wemm struct timespec ats; 239f0b479cdSPaul Saab int error; 240f0b479cdSPaul Saab 241f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 242f0b479cdSPaul Saab if (error == 0) 243f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 244f0b479cdSPaul Saab 245f0b479cdSPaul Saab return (error); 246f0b479cdSPaul Saab } 247f0b479cdSPaul Saab 248d65f1abcSDavid Xu static inline void 249d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats) 250d65f1abcSDavid Xu { 251d65f1abcSDavid Xu runtime = cputick2usec(runtime); 252d65f1abcSDavid Xu ats->tv_sec = runtime / 1000000; 253d65f1abcSDavid Xu ats->tv_nsec = runtime % 1000000 * 1000; 254d65f1abcSDavid Xu } 255d65f1abcSDavid Xu 256d65f1abcSDavid Xu static void 257d65f1abcSDavid Xu get_thread_cputime(struct thread *targettd, struct timespec *ats) 258d65f1abcSDavid Xu { 259d65f1abcSDavid Xu uint64_t runtime, curtime, switchtime; 260d65f1abcSDavid Xu 261d65f1abcSDavid Xu if (targettd == NULL) { /* current thread */ 262d65f1abcSDavid Xu critical_enter(); 263d65f1abcSDavid Xu switchtime = PCPU_GET(switchtime); 264d65f1abcSDavid Xu curtime = cpu_ticks(); 265d65f1abcSDavid Xu runtime = curthread->td_runtime; 266d65f1abcSDavid Xu critical_exit(); 267d65f1abcSDavid Xu runtime += curtime - switchtime; 268d65f1abcSDavid Xu } else { 269d65f1abcSDavid Xu thread_lock(targettd); 270d65f1abcSDavid Xu runtime = targettd->td_runtime; 271d65f1abcSDavid Xu thread_unlock(targettd); 272d65f1abcSDavid Xu } 273d65f1abcSDavid Xu cputick2timespec(runtime, ats); 274d65f1abcSDavid Xu } 275d65f1abcSDavid Xu 276d65f1abcSDavid Xu static void 277d65f1abcSDavid Xu get_process_cputime(struct proc *targetp, struct timespec *ats) 278d65f1abcSDavid Xu { 279d65f1abcSDavid Xu uint64_t runtime; 280d65f1abcSDavid Xu struct rusage ru; 281d65f1abcSDavid Xu 282d65f1abcSDavid Xu PROC_SLOCK(targetp); 283d65f1abcSDavid Xu rufetch(targetp, &ru); 284d65f1abcSDavid Xu runtime = targetp->p_rux.rux_runtime; 285d65f1abcSDavid Xu PROC_SUNLOCK(targetp); 286d65f1abcSDavid Xu cputick2timespec(runtime, ats); 287d65f1abcSDavid Xu } 288d65f1abcSDavid Xu 289d65f1abcSDavid Xu static int 290d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) 291d65f1abcSDavid Xu { 292d65f1abcSDavid Xu struct proc *p, *p2; 293d65f1abcSDavid Xu struct thread *td2; 294d65f1abcSDavid Xu lwpid_t tid; 295d65f1abcSDavid Xu pid_t pid; 296d65f1abcSDavid Xu int error; 297d65f1abcSDavid Xu 298d65f1abcSDavid Xu p = td->td_proc; 299d65f1abcSDavid Xu if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { 300d65f1abcSDavid Xu tid = clock_id & CPUCLOCK_ID_MASK; 301d65f1abcSDavid Xu td2 = tdfind(tid, p->p_pid); 302d65f1abcSDavid Xu if (td2 == NULL) 303d65f1abcSDavid Xu return (EINVAL); 304d65f1abcSDavid Xu get_thread_cputime(td2, ats); 305d65f1abcSDavid Xu PROC_UNLOCK(td2->td_proc); 306d65f1abcSDavid Xu } else { 307d65f1abcSDavid Xu pid = clock_id & CPUCLOCK_ID_MASK; 308c7c536c7SKonstantin Belousov error = pget(pid, PGET_CANSEE, &p2); 309c7c536c7SKonstantin Belousov if (error != 0) 310d65f1abcSDavid Xu return (EINVAL); 311d65f1abcSDavid Xu get_process_cputime(p2, ats); 312d65f1abcSDavid Xu PROC_UNLOCK(p2); 313d65f1abcSDavid Xu } 314d65f1abcSDavid Xu return (0); 315d65f1abcSDavid Xu } 316d65f1abcSDavid Xu 317f0b479cdSPaul Saab int 318f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 319f0b479cdSPaul Saab { 320de0a9241SKelly Yancey struct timeval sys, user; 32178c85e8dSJohn Baldwin struct proc *p; 32294c8fcd8SPeter Wemm 32378c85e8dSJohn Baldwin p = td->td_proc; 324f0b479cdSPaul Saab switch (clock_id) { 3255e758b95SRobert Watson case CLOCK_REALTIME: /* Default to precise. */ 3265e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 327f0b479cdSPaul Saab nanotime(ats); 328b8817154SKelly Yancey break; 3295e758b95SRobert Watson case CLOCK_REALTIME_FAST: 3305e758b95SRobert Watson getnanotime(ats); 3315e758b95SRobert Watson break; 332b8817154SKelly Yancey case CLOCK_VIRTUAL: 33378c85e8dSJohn Baldwin PROC_LOCK(p); 334a1fe14bcSAttilio Rao PROC_SLOCK(p); 33578c85e8dSJohn Baldwin calcru(p, &user, &sys); 336a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 33778c85e8dSJohn Baldwin PROC_UNLOCK(p); 338f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 339b8817154SKelly Yancey break; 340b8817154SKelly Yancey case CLOCK_PROF: 34178c85e8dSJohn Baldwin PROC_LOCK(p); 342a1fe14bcSAttilio Rao PROC_SLOCK(p); 34378c85e8dSJohn Baldwin calcru(p, &user, &sys); 344a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 34578c85e8dSJohn Baldwin PROC_UNLOCK(p); 346de0a9241SKelly Yancey timevaladd(&user, &sys); 347f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 348de0a9241SKelly Yancey break; 3495e758b95SRobert Watson case CLOCK_MONOTONIC: /* Default to precise. */ 3505e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 3515eefd889SAndre Oppermann case CLOCK_UPTIME: 3525e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 353f0b479cdSPaul Saab nanouptime(ats); 354b8817154SKelly Yancey break; 3555e758b95SRobert Watson case CLOCK_UPTIME_FAST: 3565e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 3575e758b95SRobert Watson getnanouptime(ats); 3585e758b95SRobert Watson break; 3595e758b95SRobert Watson case CLOCK_SECOND: 3605e758b95SRobert Watson ats->tv_sec = time_second; 3615e758b95SRobert Watson ats->tv_nsec = 0; 3625e758b95SRobert Watson break; 36300d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 364d65f1abcSDavid Xu get_thread_cputime(NULL, ats); 365d65f1abcSDavid Xu break; 366d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 367d65f1abcSDavid Xu PROC_LOCK(p); 368d65f1abcSDavid Xu get_process_cputime(p, ats); 369d65f1abcSDavid Xu PROC_UNLOCK(p); 37000d6ac63SDavid Xu break; 371b8817154SKelly Yancey default: 372d65f1abcSDavid Xu if ((int)clock_id >= 0) 3735cb3dc8fSPoul-Henning Kamp return (EINVAL); 374d65f1abcSDavid Xu return (get_cputime(td, clock_id, ats)); 375b8817154SKelly Yancey } 376f0b479cdSPaul Saab return (0); 37794c8fcd8SPeter Wemm } 37894c8fcd8SPeter Wemm 37994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 38094c8fcd8SPeter Wemm struct clock_settime_args { 38194c8fcd8SPeter Wemm clockid_t clock_id; 38294c8fcd8SPeter Wemm const struct timespec *tp; 38394c8fcd8SPeter Wemm }; 38494c8fcd8SPeter Wemm #endif 38594c8fcd8SPeter Wemm /* ARGSUSED */ 38694c8fcd8SPeter Wemm int 3878451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap) 38894c8fcd8SPeter Wemm { 38994c8fcd8SPeter Wemm struct timespec ats; 39094c8fcd8SPeter Wemm int error; 39194c8fcd8SPeter Wemm 392f0b479cdSPaul Saab if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 393f0b479cdSPaul Saab return (error); 394f0b479cdSPaul Saab return (kern_clock_settime(td, uap->clock_id, &ats)); 395f0b479cdSPaul Saab } 396f0b479cdSPaul Saab 397f0b479cdSPaul Saab int 398f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 399f0b479cdSPaul Saab { 400f0b479cdSPaul Saab struct timeval atv; 401f0b479cdSPaul Saab int error; 402f0b479cdSPaul Saab 403acd3428bSRobert Watson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 4047edfb592SJohn Baldwin return (error); 405f0b479cdSPaul Saab if (clock_id != CLOCK_REALTIME) 4067edfb592SJohn Baldwin return (EINVAL); 407f0b479cdSPaul Saab if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 4087edfb592SJohn Baldwin return (EINVAL); 409a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 410f0b479cdSPaul Saab TIMESPEC_TO_TIMEVAL(&atv, ats); 4117edfb592SJohn Baldwin error = settime(td, &atv); 41294c8fcd8SPeter Wemm return (error); 41394c8fcd8SPeter Wemm } 41494c8fcd8SPeter Wemm 41594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 41694c8fcd8SPeter Wemm struct clock_getres_args { 41794c8fcd8SPeter Wemm clockid_t clock_id; 41894c8fcd8SPeter Wemm struct timespec *tp; 41994c8fcd8SPeter Wemm }; 42094c8fcd8SPeter Wemm #endif 42194c8fcd8SPeter Wemm int 4228451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap) 42394c8fcd8SPeter Wemm { 42494c8fcd8SPeter Wemm struct timespec ts; 425f0b479cdSPaul Saab int error; 42694c8fcd8SPeter Wemm 427f0b479cdSPaul Saab if (uap->tp == NULL) 428f0b479cdSPaul Saab return (0); 429f0b479cdSPaul Saab 430f0b479cdSPaul Saab error = kern_clock_getres(td, uap->clock_id, &ts); 431f0b479cdSPaul Saab if (error == 0) 432f0b479cdSPaul Saab error = copyout(&ts, uap->tp, sizeof(ts)); 433f0b479cdSPaul Saab return (error); 434f0b479cdSPaul Saab } 435f0b479cdSPaul Saab 436f0b479cdSPaul Saab int 437f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 438f0b479cdSPaul Saab { 439f0b479cdSPaul Saab 440f0b479cdSPaul Saab ts->tv_sec = 0; 441f0b479cdSPaul Saab switch (clock_id) { 442b8817154SKelly Yancey case CLOCK_REALTIME: 4435e758b95SRobert Watson case CLOCK_REALTIME_FAST: 4445e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 445b8817154SKelly Yancey case CLOCK_MONOTONIC: 4465e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 4475e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 4485eefd889SAndre Oppermann case CLOCK_UPTIME: 4495e758b95SRobert Watson case CLOCK_UPTIME_FAST: 4505e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 451ac0653dcSBruce Evans /* 452ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 453ac0653dcSBruce Evans * Rounding up is especially important if rounding down 454ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 455ac0653dcSBruce Evans */ 456f0b479cdSPaul Saab ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 457b8817154SKelly Yancey break; 458b8817154SKelly Yancey case CLOCK_VIRTUAL: 459b8817154SKelly Yancey case CLOCK_PROF: 460b8817154SKelly Yancey /* Accurately round up here because we can do so cheaply. */ 461f0b479cdSPaul Saab ts->tv_nsec = (1000000000 + hz - 1) / hz; 462b8817154SKelly Yancey break; 4635e758b95SRobert Watson case CLOCK_SECOND: 4645e758b95SRobert Watson ts->tv_sec = 1; 4655e758b95SRobert Watson ts->tv_nsec = 0; 4665e758b95SRobert Watson break; 46700d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 468d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 469d65f1abcSDavid Xu cputime: 47000d6ac63SDavid Xu /* sync with cputick2usec */ 47100d6ac63SDavid Xu ts->tv_nsec = 1000000 / cpu_tickrate(); 47200d6ac63SDavid Xu if (ts->tv_nsec == 0) 47300d6ac63SDavid Xu ts->tv_nsec = 1000; 47400d6ac63SDavid Xu break; 475b8817154SKelly Yancey default: 476d65f1abcSDavid Xu if ((int)clock_id < 0) 477d65f1abcSDavid Xu goto cputime; 478b8817154SKelly Yancey return (EINVAL); 47994c8fcd8SPeter Wemm } 480de0a9241SKelly Yancey return (0); 48194c8fcd8SPeter Wemm } 48294c8fcd8SPeter Wemm 4830dbf17e6SAlexander Motin static uint8_t nanowait[MAXCPU]; 48494c8fcd8SPeter Wemm 4857fdf2c85SPaul Saab int 4867fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 4875b870b7bSPeter Wemm { 488098176f0SDavide Italiano struct timespec ts; 489098176f0SDavide Italiano sbintime_t sbt, sbtt, prec, tmp; 490980c545dSAlexander Motin time_t over; 49133841826SPoul-Henning Kamp int error; 4925b870b7bSPeter Wemm 4937d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 494708e7684SPeter Wemm return (EINVAL); 495d254af07SMatthew Dillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 4967d7fb492SBruce Evans return (0); 497980c545dSAlexander Motin ts = *rqt; 498980c545dSAlexander Motin if (ts.tv_sec > INT32_MAX / 2) { 499980c545dSAlexander Motin over = ts.tv_sec - INT32_MAX / 2; 500980c545dSAlexander Motin ts.tv_sec -= over; 501980c545dSAlexander Motin } else 502980c545dSAlexander Motin over = 0; 503980c545dSAlexander Motin tmp = tstosbt(ts); 504098176f0SDavide Italiano prec = tmp; 505098176f0SDavide Italiano prec >>= tc_precexp; 506098176f0SDavide Italiano if (TIMESEL(&sbt, tmp)) 507098176f0SDavide Italiano sbt += tc_tick_sbt; 508098176f0SDavide Italiano sbt += tmp; 5090dbf17e6SAlexander Motin error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp", 5100dbf17e6SAlexander Motin sbt, prec, C_ABSOLUTE); 51133841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 51233841826SPoul-Henning Kamp if (error == ERESTART) 51394c8fcd8SPeter Wemm error = EINTR; 514098176f0SDavide Italiano TIMESEL(&sbtt, tmp); 51533841826SPoul-Henning Kamp if (rmt != NULL) { 516098176f0SDavide Italiano ts = sbttots(sbt - sbtt); 517980c545dSAlexander Motin ts.tv_sec += over; 51833841826SPoul-Henning Kamp if (ts.tv_sec < 0) 51933841826SPoul-Henning Kamp timespecclear(&ts); 52000af9731SPoul-Henning Kamp *rmt = ts; 52100af9731SPoul-Henning Kamp } 522098176f0SDavide Italiano if (sbtt >= sbt) 523098176f0SDavide Italiano return (0); 52433841826SPoul-Henning Kamp return (error); 52533841826SPoul-Henning Kamp } 52633841826SPoul-Henning Kamp return (0); 5275b870b7bSPeter Wemm } 52894c8fcd8SPeter Wemm 5295b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 5305b870b7bSPeter Wemm struct nanosleep_args { 5315b870b7bSPeter Wemm struct timespec *rqtp; 5325b870b7bSPeter Wemm struct timespec *rmtp; 5335b870b7bSPeter Wemm }; 5345b870b7bSPeter Wemm #endif 5355b870b7bSPeter Wemm /* ARGSUSED */ 5365b870b7bSPeter Wemm int 5378451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap) 5385b870b7bSPeter Wemm { 5395b870b7bSPeter Wemm struct timespec rmt, rqt; 540fb99ab88SMatthew Dillon int error; 5415b870b7bSPeter Wemm 542d1e405c5SAlfred Perlstein error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 5435b870b7bSPeter Wemm if (error) 5445b870b7bSPeter Wemm return (error); 545fb99ab88SMatthew Dillon 54631f3e2adSAlfred Perlstein if (uap->rmtp && 54731f3e2adSAlfred Perlstein !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 54831f3e2adSAlfred Perlstein return (EFAULT); 5497fdf2c85SPaul Saab error = kern_nanosleep(td, &rqt, &rmt); 550d1e405c5SAlfred Perlstein if (error && uap->rmtp) { 551fb99ab88SMatthew Dillon int error2; 552fb99ab88SMatthew Dillon 553d1e405c5SAlfred Perlstein error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 55431f3e2adSAlfred Perlstein if (error2) 555fb99ab88SMatthew Dillon error = error2; 556708e7684SPeter Wemm } 557708e7684SPeter Wemm return (error); 55894c8fcd8SPeter Wemm } 55994c8fcd8SPeter Wemm 5605b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 561df8bae1dSRodney W. Grimes struct gettimeofday_args { 562df8bae1dSRodney W. Grimes struct timeval *tp; 563df8bae1dSRodney W. Grimes struct timezone *tzp; 564df8bae1dSRodney W. Grimes }; 565d2d3e875SBruce Evans #endif 566df8bae1dSRodney W. Grimes /* ARGSUSED */ 56726f9a767SRodney W. Grimes int 5688451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap) 569df8bae1dSRodney W. Grimes { 570df8bae1dSRodney W. Grimes struct timeval atv; 571411c25edSTim J. Robbins struct timezone rtz; 572df8bae1dSRodney W. Grimes int error = 0; 573df8bae1dSRodney W. Grimes 574df8bae1dSRodney W. Grimes if (uap->tp) { 575df8bae1dSRodney W. Grimes microtime(&atv); 57601609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 577df8bae1dSRodney W. Grimes } 57821dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 57991f1c2b3SPoul-Henning Kamp rtz.tz_minuteswest = tz_minuteswest; 58091f1c2b3SPoul-Henning Kamp rtz.tz_dsttime = tz_dsttime; 581411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 58221dcdb38SPoul-Henning Kamp } 583df8bae1dSRodney W. Grimes return (error); 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes 586d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 587df8bae1dSRodney W. Grimes struct settimeofday_args { 588df8bae1dSRodney W. Grimes struct timeval *tv; 589df8bae1dSRodney W. Grimes struct timezone *tzp; 590df8bae1dSRodney W. Grimes }; 591d2d3e875SBruce Evans #endif 592df8bae1dSRodney W. Grimes /* ARGSUSED */ 59326f9a767SRodney W. Grimes int 5948451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap) 595df8bae1dSRodney W. Grimes { 596b88ec951SJohn Baldwin struct timeval atv, *tvp; 597b88ec951SJohn Baldwin struct timezone atz, *tzp; 598b88ec951SJohn Baldwin int error; 599b88ec951SJohn Baldwin 600b88ec951SJohn Baldwin if (uap->tv) { 601b88ec951SJohn Baldwin error = copyin(uap->tv, &atv, sizeof(atv)); 602b88ec951SJohn Baldwin if (error) 603b88ec951SJohn Baldwin return (error); 604b88ec951SJohn Baldwin tvp = &atv; 605b88ec951SJohn Baldwin } else 606b88ec951SJohn Baldwin tvp = NULL; 607b88ec951SJohn Baldwin if (uap->tzp) { 608b88ec951SJohn Baldwin error = copyin(uap->tzp, &atz, sizeof(atz)); 609b88ec951SJohn Baldwin if (error) 610b88ec951SJohn Baldwin return (error); 611b88ec951SJohn Baldwin tzp = &atz; 612b88ec951SJohn Baldwin } else 613b88ec951SJohn Baldwin tzp = NULL; 614b88ec951SJohn Baldwin return (kern_settimeofday(td, tvp, tzp)); 615b88ec951SJohn Baldwin } 616b88ec951SJohn Baldwin 617b88ec951SJohn Baldwin int 618b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 619b88ec951SJohn Baldwin { 620b88ec951SJohn Baldwin int error; 621fb99ab88SMatthew Dillon 622acd3428bSRobert Watson error = priv_check(td, PRIV_SETTIMEOFDAY); 623b88ec951SJohn Baldwin if (error) 6247edfb592SJohn Baldwin return (error); 625df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 626b88ec951SJohn Baldwin if (tv) { 627b88ec951SJohn Baldwin if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 6287edfb592SJohn Baldwin return (EINVAL); 629b88ec951SJohn Baldwin error = settime(td, tv); 630708e7684SPeter Wemm } 631b88ec951SJohn Baldwin if (tzp && error == 0) { 632b88ec951SJohn Baldwin tz_minuteswest = tzp->tz_minuteswest; 633b88ec951SJohn Baldwin tz_dsttime = tzp->tz_dsttime; 634b88ec951SJohn Baldwin } 6357edfb592SJohn Baldwin return (error); 636b88ec951SJohn Baldwin } 6377edfb592SJohn Baldwin 638df8bae1dSRodney W. Grimes /* 639873fbcd7SRobert Watson * Get value of an interval timer. The process virtual and profiling virtual 640873fbcd7SRobert Watson * time timers are kept in the p_stats area, since they can be swapped out. 641873fbcd7SRobert Watson * These are kept internally in the way they are specified externally: in 642873fbcd7SRobert Watson * time until they expire. 643df8bae1dSRodney W. Grimes * 644873fbcd7SRobert Watson * The real time interval timer is kept in the process table slot for the 645873fbcd7SRobert Watson * process, and its value (it_value) is kept as an absolute time rather than 646873fbcd7SRobert Watson * as a delta, so that it is easy to keep periodic real-time signals from 647873fbcd7SRobert Watson * drifting. 648df8bae1dSRodney W. Grimes * 649df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 650873fbcd7SRobert Watson * kern_clock.c. The real time timer is processed by a timeout routine, 651873fbcd7SRobert Watson * called from the softclock() routine. Since a callout may be delayed in 652873fbcd7SRobert Watson * real time due to interrupt processing in the system, it is possible for 653873fbcd7SRobert Watson * the real time timeout routine (realitexpire, given below), to be delayed 654873fbcd7SRobert Watson * in real time past when it is supposed to occur. It does not suffice, 655873fbcd7SRobert Watson * therefore, to reload the real timer .it_value from the real time timers 656873fbcd7SRobert Watson * .it_interval. Rather, we compute the next time in absolute time the timer 657873fbcd7SRobert Watson * should go off. 658df8bae1dSRodney W. Grimes */ 659d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 660df8bae1dSRodney W. Grimes struct getitimer_args { 661df8bae1dSRodney W. Grimes u_int which; 662df8bae1dSRodney W. Grimes struct itimerval *itv; 663df8bae1dSRodney W. Grimes }; 664d2d3e875SBruce Evans #endif 66526f9a767SRodney W. Grimes int 6668451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap) 667df8bae1dSRodney W. Grimes { 668df8bae1dSRodney W. Grimes struct itimerval aitv; 669c90110d6SJohn Baldwin int error; 670df8bae1dSRodney W. Grimes 671cfa0efe7SMaxim Sobolev error = kern_getitimer(td, uap->which, &aitv); 672cfa0efe7SMaxim Sobolev if (error != 0) 673cfa0efe7SMaxim Sobolev return (error); 674cfa0efe7SMaxim Sobolev return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 675cfa0efe7SMaxim Sobolev } 676cfa0efe7SMaxim Sobolev 677cfa0efe7SMaxim Sobolev int 678cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 679cfa0efe7SMaxim Sobolev { 680cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 681cfa0efe7SMaxim Sobolev struct timeval ctv; 682cfa0efe7SMaxim Sobolev 683cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 684df8bae1dSRodney W. Grimes return (EINVAL); 685fb99ab88SMatthew Dillon 686cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 687df8bae1dSRodney W. Grimes /* 688ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 689df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 690df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 691df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 692df8bae1dSRodney W. Grimes */ 69396d7f8efSTim J. Robbins PROC_LOCK(p); 694cfa0efe7SMaxim Sobolev *aitv = p->p_realtimer; 69596d7f8efSTim J. Robbins PROC_UNLOCK(p); 696cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 697b5ea3779SAlexander Motin microuptime(&ctv); 698cfa0efe7SMaxim Sobolev if (timevalcmp(&aitv->it_value, &ctv, <)) 699cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_value); 700df8bae1dSRodney W. Grimes else 701cfa0efe7SMaxim Sobolev timevalsub(&aitv->it_value, &ctv); 702227ee8a1SPoul-Henning Kamp } 703fb99ab88SMatthew Dillon } else { 704982d11f8SJeff Roberson PROC_SLOCK(p); 705cfa0efe7SMaxim Sobolev *aitv = p->p_stats->p_timer[which]; 706982d11f8SJeff Roberson PROC_SUNLOCK(p); 707fb99ab88SMatthew Dillon } 708cfa0efe7SMaxim Sobolev return (0); 709df8bae1dSRodney W. Grimes } 710df8bae1dSRodney W. Grimes 711d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 712df8bae1dSRodney W. Grimes struct setitimer_args { 713df8bae1dSRodney W. Grimes u_int which; 714df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 715df8bae1dSRodney W. Grimes }; 716d2d3e875SBruce Evans #endif 71726f9a767SRodney W. Grimes int 7188451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap) 719df8bae1dSRodney W. Grimes { 720cfa0efe7SMaxim Sobolev struct itimerval aitv, oitv; 721c90110d6SJohn Baldwin int error; 72296d7f8efSTim J. Robbins 72396d7f8efSTim J. Robbins if (uap->itv == NULL) { 72496d7f8efSTim J. Robbins uap->itv = uap->oitv; 7258451d0ddSKip Macy return (sys_getitimer(td, (struct getitimer_args *)uap)); 72696d7f8efSTim J. Robbins } 727df8bae1dSRodney W. Grimes 72896d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 729df8bae1dSRodney W. Grimes return (error); 730cfa0efe7SMaxim Sobolev error = kern_setitimer(td, uap->which, &aitv, &oitv); 731cfa0efe7SMaxim Sobolev if (error != 0 || uap->oitv == NULL) 732cfa0efe7SMaxim Sobolev return (error); 733cfa0efe7SMaxim Sobolev return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 734cfa0efe7SMaxim Sobolev } 735cfa0efe7SMaxim Sobolev 736cfa0efe7SMaxim Sobolev int 737c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 738c90110d6SJohn Baldwin struct itimerval *oitv) 739cfa0efe7SMaxim Sobolev { 740cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 741cfa0efe7SMaxim Sobolev struct timeval ctv; 742b5ea3779SAlexander Motin sbintime_t sbt, pr; 743cfa0efe7SMaxim Sobolev 7445e85ac17SJohn Baldwin if (aitv == NULL) 7455e85ac17SJohn Baldwin return (kern_getitimer(td, which, oitv)); 7465e85ac17SJohn Baldwin 747cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 74896d7f8efSTim J. Robbins return (EINVAL); 749b5ea3779SAlexander Motin if (itimerfix(&aitv->it_value) || 750b5ea3779SAlexander Motin aitv->it_value.tv_sec > INT32_MAX / 2) 751cfa0efe7SMaxim Sobolev return (EINVAL); 752cfa0efe7SMaxim Sobolev if (!timevalisset(&aitv->it_value)) 753cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_interval); 754b5ea3779SAlexander Motin else if (itimerfix(&aitv->it_interval) || 755b5ea3779SAlexander Motin aitv->it_interval.tv_sec > INT32_MAX / 2) 75696d7f8efSTim J. Robbins return (EINVAL); 75796d7f8efSTim J. Robbins 758cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 75996d7f8efSTim J. Robbins PROC_LOCK(p); 7604cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 7614f559836SJake Burkholder callout_stop(&p->p_itcallout); 762b5ea3779SAlexander Motin microuptime(&ctv); 763cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 764b5ea3779SAlexander Motin pr = tvtosbt(aitv->it_value) >> tc_precexp; 765cfa0efe7SMaxim Sobolev timevaladd(&aitv->it_value, &ctv); 766b5ea3779SAlexander Motin sbt = tvtosbt(aitv->it_value); 767b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, sbt, pr, 768b5ea3779SAlexander Motin realitexpire, p, C_ABSOLUTE); 76925b4d3a8SJohn Baldwin } 770cfa0efe7SMaxim Sobolev *oitv = p->p_realtimer; 771cfa0efe7SMaxim Sobolev p->p_realtimer = *aitv; 77296d7f8efSTim J. Robbins PROC_UNLOCK(p); 773cfa0efe7SMaxim Sobolev if (timevalisset(&oitv->it_value)) { 774cfa0efe7SMaxim Sobolev if (timevalcmp(&oitv->it_value, &ctv, <)) 775cfa0efe7SMaxim Sobolev timevalclear(&oitv->it_value); 77696d7f8efSTim J. Robbins else 777cfa0efe7SMaxim Sobolev timevalsub(&oitv->it_value, &ctv); 778fb99ab88SMatthew Dillon } 77996d7f8efSTim J. Robbins } else { 780982d11f8SJeff Roberson PROC_SLOCK(p); 781cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 782cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 783982d11f8SJeff Roberson PROC_SUNLOCK(p); 78496d7f8efSTim J. Robbins } 78596d7f8efSTim J. Robbins return (0); 786df8bae1dSRodney W. Grimes } 787df8bae1dSRodney W. Grimes 788df8bae1dSRodney W. Grimes /* 789df8bae1dSRodney W. Grimes * Real interval timer expired: 790df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 791df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 792df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 793df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 794df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 795c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 7969207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 7979207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 7989207f00aSBruce Evans * interrupt even when we're delayed. 799df8bae1dSRodney W. Grimes */ 800df8bae1dSRodney W. Grimes void 80191afe087SPoul-Henning Kamp realitexpire(void *arg) 802df8bae1dSRodney W. Grimes { 80391afe087SPoul-Henning Kamp struct proc *p; 804b5ea3779SAlexander Motin struct timeval ctv; 805b5ea3779SAlexander Motin sbintime_t isbt; 806df8bae1dSRodney W. Grimes 807df8bae1dSRodney W. Grimes p = (struct proc *)arg; 8088451d0ddSKip Macy kern_psignal(p, SIGALRM); 8094cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 8104cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 8115499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 8125499ea01SJohn Baldwin wakeup(&p->p_itcallout); 813df8bae1dSRodney W. Grimes return; 814df8bae1dSRodney W. Grimes } 815b5ea3779SAlexander Motin isbt = tvtosbt(p->p_realtimer.it_interval); 816b5ea3779SAlexander Motin if (isbt >= sbt_timethreshold) 817b5ea3779SAlexander Motin getmicrouptime(&ctv); 818b5ea3779SAlexander Motin else 819b5ea3779SAlexander Motin microuptime(&ctv); 820b5ea3779SAlexander Motin do { 821df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 822df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 823b5ea3779SAlexander Motin } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=)); 824b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value), 825b5ea3779SAlexander Motin isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE); 826df8bae1dSRodney W. Grimes } 827df8bae1dSRodney W. Grimes 828df8bae1dSRodney W. Grimes /* 829df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 830df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 831df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 832df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 833df8bae1dSRodney W. Grimes */ 83426f9a767SRodney W. Grimes int 83591afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 836df8bae1dSRodney W. Grimes { 837df8bae1dSRodney W. Grimes 83886857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 839df8bae1dSRodney W. Grimes return (EINVAL); 840b5ea3779SAlexander Motin if (tv->tv_sec == 0 && tv->tv_usec != 0 && 841b5ea3779SAlexander Motin tv->tv_usec < (u_int)tick / 16) 842b5ea3779SAlexander Motin tv->tv_usec = (u_int)tick / 16; 843df8bae1dSRodney W. Grimes return (0); 844df8bae1dSRodney W. Grimes } 845df8bae1dSRodney W. Grimes 846df8bae1dSRodney W. Grimes /* 847df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 848df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 849df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 850df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 851df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 852df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 853df8bae1dSRodney W. Grimes * that it is called in a context where the timers 854df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 855df8bae1dSRodney W. Grimes */ 85626f9a767SRodney W. Grimes int 85791afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 858df8bae1dSRodney W. Grimes { 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 861df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 862df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 863df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 864df8bae1dSRodney W. Grimes goto expire; 865df8bae1dSRodney W. Grimes } 866df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 867df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 868df8bae1dSRodney W. Grimes } 869df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 870df8bae1dSRodney W. Grimes usec = 0; 8714cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 872df8bae1dSRodney W. Grimes return (1); 873df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 874df8bae1dSRodney W. Grimes expire: 8754cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 876df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 877df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 878df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 879df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 880df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 881df8bae1dSRodney W. Grimes } 882df8bae1dSRodney W. Grimes } else 883df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 884df8bae1dSRodney W. Grimes return (0); 885df8bae1dSRodney W. Grimes } 886df8bae1dSRodney W. Grimes 887df8bae1dSRodney W. Grimes /* 888df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 889df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 890df8bae1dSRodney W. Grimes * results which are before the beginning, 891df8bae1dSRodney W. Grimes * it just gets very confused in this case. 892df8bae1dSRodney W. Grimes * Caveat emptor. 893df8bae1dSRodney W. Grimes */ 89426f9a767SRodney W. Grimes void 8956ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 896df8bae1dSRodney W. Grimes { 897df8bae1dSRodney W. Grimes 898df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 899df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 900df8bae1dSRodney W. Grimes timevalfix(t1); 901df8bae1dSRodney W. Grimes } 902df8bae1dSRodney W. Grimes 90326f9a767SRodney W. Grimes void 9046ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 905df8bae1dSRodney W. Grimes { 906df8bae1dSRodney W. Grimes 907df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 908df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 909df8bae1dSRodney W. Grimes timevalfix(t1); 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes 91287b6de2bSPoul-Henning Kamp static void 91391afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 914df8bae1dSRodney W. Grimes { 915df8bae1dSRodney W. Grimes 916df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 917df8bae1dSRodney W. Grimes t1->tv_sec--; 918df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 919df8bae1dSRodney W. Grimes } 920df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 921df8bae1dSRodney W. Grimes t1->tv_sec++; 922df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 923df8bae1dSRodney W. Grimes } 924df8bae1dSRodney W. Grimes } 92591974ce1SSam Leffler 92691974ce1SSam Leffler /* 927addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 92891974ce1SSam Leffler */ 92991974ce1SSam Leffler int 93091974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 93191974ce1SSam Leffler { 93291974ce1SSam Leffler struct timeval tv, delta; 93391974ce1SSam Leffler int rv = 0; 93491974ce1SSam Leffler 935addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 936addea9d4SSam Leffler delta = tv; 937addea9d4SSam Leffler timevalsub(&delta, lasttime); 93891974ce1SSam Leffler 93991974ce1SSam Leffler /* 94091974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 94191974ce1SSam Leffler * even if interval is huge. 94291974ce1SSam Leffler */ 94391974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 94491974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 94591974ce1SSam Leffler *lasttime = tv; 94691974ce1SSam Leffler rv = 1; 94791974ce1SSam Leffler } 94891974ce1SSam Leffler 94991974ce1SSam Leffler return (rv); 95091974ce1SSam Leffler } 95191974ce1SSam Leffler 95291974ce1SSam Leffler /* 95391974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 954addea9d4SSam Leffler * 955addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 956addea9d4SSam Leffler * should drop a packet because of the rate limitation). 957addea9d4SSam Leffler * 958893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 959893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 960893bec80SSam Leffler * limiting. 961893bec80SSam Leffler * 962addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 963addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 964addea9d4SSam Leffler * clock ticks for minimal overhead. 96591974ce1SSam Leffler */ 96691974ce1SSam Leffler int 96791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 96891974ce1SSam Leffler { 969addea9d4SSam Leffler int now; 97091974ce1SSam Leffler 97191974ce1SSam Leffler /* 972addea9d4SSam Leffler * Reset the last time and counter if this is the first call 973addea9d4SSam Leffler * or more than a second has passed since the last update of 974addea9d4SSam Leffler * lasttime. 97591974ce1SSam Leffler */ 976addea9d4SSam Leffler now = ticks; 977addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 978addea9d4SSam Leffler lasttime->tv_sec = now; 979addea9d4SSam Leffler *curpps = 1; 980893bec80SSam Leffler return (maxpps != 0); 981addea9d4SSam Leffler } else { 982addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 983addea9d4SSam Leffler return (maxpps < 0 || *curpps < maxpps); 984addea9d4SSam Leffler } 98591974ce1SSam Leffler } 98686857b36SDavid Xu 98786857b36SDavid Xu static void 98886857b36SDavid Xu itimer_start(void) 98986857b36SDavid Xu { 99086857b36SDavid Xu struct kclock rt_clock = { 99186857b36SDavid Xu .timer_create = realtimer_create, 99286857b36SDavid Xu .timer_delete = realtimer_delete, 99386857b36SDavid Xu .timer_settime = realtimer_settime, 99486857b36SDavid Xu .timer_gettime = realtimer_gettime, 995843b99c6SDavid Xu .event_hook = NULL 99686857b36SDavid Xu }; 99786857b36SDavid Xu 99886857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 99986857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 100086857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 100186857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 100277e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 100377e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 100477e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 1005993182e5SAlexander Leidinger EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 1006d26b1a1fSDavid Xu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 1007993182e5SAlexander Leidinger EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 1008d26b1a1fSDavid Xu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 100986857b36SDavid Xu } 101086857b36SDavid Xu 101186857b36SDavid Xu int 101286857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk) 101386857b36SDavid Xu { 101486857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 101586857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 101686857b36SDavid Xu return (0); 101786857b36SDavid Xu } 101886857b36SDavid Xu posix_clocks[clockid] = *clk; 101986857b36SDavid Xu return (1); 102086857b36SDavid Xu } 102186857b36SDavid Xu 102286857b36SDavid Xu static int 102386857b36SDavid Xu itimer_init(void *mem, int size, int flags) 102486857b36SDavid Xu { 102586857b36SDavid Xu struct itimer *it; 102686857b36SDavid Xu 102786857b36SDavid Xu it = (struct itimer *)mem; 102886857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 102986857b36SDavid Xu return (0); 103086857b36SDavid Xu } 103186857b36SDavid Xu 103286857b36SDavid Xu static void 103386857b36SDavid Xu itimer_fini(void *mem, int size) 103486857b36SDavid Xu { 103586857b36SDavid Xu struct itimer *it; 103686857b36SDavid Xu 103786857b36SDavid Xu it = (struct itimer *)mem; 103886857b36SDavid Xu mtx_destroy(&it->it_mtx); 103986857b36SDavid Xu } 104086857b36SDavid Xu 104186857b36SDavid Xu static void 104286857b36SDavid Xu itimer_enter(struct itimer *it) 104386857b36SDavid Xu { 104486857b36SDavid Xu 104586857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 104686857b36SDavid Xu it->it_usecount++; 104786857b36SDavid Xu } 104886857b36SDavid Xu 104986857b36SDavid Xu static void 105086857b36SDavid Xu itimer_leave(struct itimer *it) 105186857b36SDavid Xu { 105286857b36SDavid Xu 105386857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 105486857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 105586857b36SDavid Xu 105686857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 105786857b36SDavid Xu wakeup(it); 105886857b36SDavid Xu } 105986857b36SDavid Xu 106086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 106161d3a4efSDavid Xu struct ktimer_create_args { 106286857b36SDavid Xu clockid_t clock_id; 106386857b36SDavid Xu struct sigevent * evp; 106461d3a4efSDavid Xu int * timerid; 106586857b36SDavid Xu }; 106686857b36SDavid Xu #endif 106786857b36SDavid Xu int 10688451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 106986857b36SDavid Xu { 107086857b36SDavid Xu struct sigevent *evp1, ev; 107161d3a4efSDavid Xu int id; 107286857b36SDavid Xu int error; 107386857b36SDavid Xu 107486857b36SDavid Xu if (uap->evp != NULL) { 107586857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 107686857b36SDavid Xu if (error != 0) 107786857b36SDavid Xu return (error); 107886857b36SDavid Xu evp1 = &ev; 107986857b36SDavid Xu } else 108086857b36SDavid Xu evp1 = NULL; 108186857b36SDavid Xu 108286857b36SDavid Xu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 108386857b36SDavid Xu 108486857b36SDavid Xu if (error == 0) { 108561d3a4efSDavid Xu error = copyout(&id, uap->timerid, sizeof(int)); 108686857b36SDavid Xu if (error != 0) 108786857b36SDavid Xu kern_timer_delete(td, id); 108886857b36SDavid Xu } 108986857b36SDavid Xu return (error); 109086857b36SDavid Xu } 109186857b36SDavid Xu 109286857b36SDavid Xu static int 109386857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id, 109461d3a4efSDavid Xu struct sigevent *evp, int *timerid, int preset_id) 109586857b36SDavid Xu { 109686857b36SDavid Xu struct proc *p = td->td_proc; 109786857b36SDavid Xu struct itimer *it; 109886857b36SDavid Xu int id; 109986857b36SDavid Xu int error; 110086857b36SDavid Xu 110186857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 110286857b36SDavid Xu return (EINVAL); 110386857b36SDavid Xu 110486857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 110586857b36SDavid Xu return (EINVAL); 110686857b36SDavid Xu 110786857b36SDavid Xu if (evp != NULL) { 110886857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 110956c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 111056c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 111186857b36SDavid Xu return (EINVAL); 111256c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 111356c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 111486857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 111586857b36SDavid Xu return (EINVAL); 111686857b36SDavid Xu } 111786857b36SDavid Xu 111860354683SDavid Xu if (p->p_itimers == NULL) 111986857b36SDavid Xu itimers_alloc(p); 112086857b36SDavid Xu 112186857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 112286857b36SDavid Xu it->it_flags = 0; 112386857b36SDavid Xu it->it_usecount = 0; 112486857b36SDavid Xu it->it_active = 0; 112556c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 112656c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 112786857b36SDavid Xu it->it_overrun = 0; 112886857b36SDavid Xu it->it_overrun_last = 0; 112986857b36SDavid Xu it->it_clockid = clock_id; 113086857b36SDavid Xu it->it_timerid = -1; 113186857b36SDavid Xu it->it_proc = p; 113286857b36SDavid Xu ksiginfo_init(&it->it_ksi); 113386857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 113486857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 113586857b36SDavid Xu if (error != 0) 113686857b36SDavid Xu goto out; 113786857b36SDavid Xu 113886857b36SDavid Xu PROC_LOCK(p); 113986857b36SDavid Xu if (preset_id != -1) { 114086857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 114186857b36SDavid Xu id = preset_id; 114260354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 114386857b36SDavid Xu PROC_UNLOCK(p); 114486857b36SDavid Xu error = 0; 114586857b36SDavid Xu goto out; 114686857b36SDavid Xu } 114786857b36SDavid Xu } else { 114886857b36SDavid Xu /* 114986857b36SDavid Xu * Find a free timer slot, skipping those reserved 115086857b36SDavid Xu * for setitimer(). 115186857b36SDavid Xu */ 115286857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 115360354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 115486857b36SDavid Xu break; 115586857b36SDavid Xu if (id == TIMER_MAX) { 115686857b36SDavid Xu PROC_UNLOCK(p); 115786857b36SDavid Xu error = EAGAIN; 115886857b36SDavid Xu goto out; 115986857b36SDavid Xu } 116086857b36SDavid Xu } 116186857b36SDavid Xu it->it_timerid = id; 116260354683SDavid Xu p->p_itimers->its_timers[id] = it; 116386857b36SDavid Xu if (evp != NULL) 116486857b36SDavid Xu it->it_sigev = *evp; 116586857b36SDavid Xu else { 116686857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 116786857b36SDavid Xu switch (clock_id) { 116886857b36SDavid Xu default: 116986857b36SDavid Xu case CLOCK_REALTIME: 117086857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 117186857b36SDavid Xu break; 117286857b36SDavid Xu case CLOCK_VIRTUAL: 117386857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 117486857b36SDavid Xu break; 117586857b36SDavid Xu case CLOCK_PROF: 117686857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 117786857b36SDavid Xu break; 117886857b36SDavid Xu } 11798f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 118086857b36SDavid Xu } 118186857b36SDavid Xu 118256c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 118356c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 118486857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 118586857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 118686857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 118786857b36SDavid Xu it->it_ksi.ksi_timerid = id; 118886857b36SDavid Xu } 118986857b36SDavid Xu PROC_UNLOCK(p); 119086857b36SDavid Xu *timerid = id; 119186857b36SDavid Xu return (0); 119286857b36SDavid Xu 119386857b36SDavid Xu out: 119486857b36SDavid Xu ITIMER_LOCK(it); 119586857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 119686857b36SDavid Xu ITIMER_UNLOCK(it); 119786857b36SDavid Xu uma_zfree(itimer_zone, it); 119886857b36SDavid Xu return (error); 119986857b36SDavid Xu } 120086857b36SDavid Xu 120186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 120261d3a4efSDavid Xu struct ktimer_delete_args { 120361d3a4efSDavid Xu int timerid; 120486857b36SDavid Xu }; 120586857b36SDavid Xu #endif 120686857b36SDavid Xu int 12078451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 120886857b36SDavid Xu { 120986857b36SDavid Xu return (kern_timer_delete(td, uap->timerid)); 121086857b36SDavid Xu } 121186857b36SDavid Xu 121286857b36SDavid Xu static struct itimer * 1213843b99c6SDavid Xu itimer_find(struct proc *p, int timerid) 121486857b36SDavid Xu { 121586857b36SDavid Xu struct itimer *it; 121686857b36SDavid Xu 121786857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 12183f935cf3SColin Percival if ((p->p_itimers == NULL) || 12193f935cf3SColin Percival (timerid < 0) || (timerid >= TIMER_MAX) || 122060354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 122186857b36SDavid Xu return (NULL); 122286857b36SDavid Xu } 122386857b36SDavid Xu ITIMER_LOCK(it); 1224843b99c6SDavid Xu if ((it->it_flags & ITF_DELETING) != 0) { 122586857b36SDavid Xu ITIMER_UNLOCK(it); 122686857b36SDavid Xu it = NULL; 122786857b36SDavid Xu } 122886857b36SDavid Xu return (it); 122986857b36SDavid Xu } 123086857b36SDavid Xu 123186857b36SDavid Xu static int 123261d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid) 123386857b36SDavid Xu { 123486857b36SDavid Xu struct proc *p = td->td_proc; 123586857b36SDavid Xu struct itimer *it; 123686857b36SDavid Xu 123786857b36SDavid Xu PROC_LOCK(p); 1238843b99c6SDavid Xu it = itimer_find(p, timerid); 123986857b36SDavid Xu if (it == NULL) { 124086857b36SDavid Xu PROC_UNLOCK(p); 124186857b36SDavid Xu return (EINVAL); 124286857b36SDavid Xu } 124386857b36SDavid Xu PROC_UNLOCK(p); 124486857b36SDavid Xu 124586857b36SDavid Xu it->it_flags |= ITF_DELETING; 124686857b36SDavid Xu while (it->it_usecount > 0) { 124786857b36SDavid Xu it->it_flags |= ITF_WANTED; 124886857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 124986857b36SDavid Xu } 125086857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 125186857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 125286857b36SDavid Xu ITIMER_UNLOCK(it); 125386857b36SDavid Xu 125486857b36SDavid Xu PROC_LOCK(p); 125586857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 125686857b36SDavid Xu sigqueue_take(&it->it_ksi); 125760354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 125886857b36SDavid Xu PROC_UNLOCK(p); 125986857b36SDavid Xu uma_zfree(itimer_zone, it); 126086857b36SDavid Xu return (0); 126186857b36SDavid Xu } 126286857b36SDavid Xu 126386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 126461d3a4efSDavid Xu struct ktimer_settime_args { 126561d3a4efSDavid Xu int timerid; 126686857b36SDavid Xu int flags; 126786857b36SDavid Xu const struct itimerspec * value; 126886857b36SDavid Xu struct itimerspec * ovalue; 126986857b36SDavid Xu }; 127086857b36SDavid Xu #endif 127186857b36SDavid Xu int 12728451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 127386857b36SDavid Xu { 127486857b36SDavid Xu struct proc *p = td->td_proc; 127586857b36SDavid Xu struct itimer *it; 127686857b36SDavid Xu struct itimerspec val, oval, *ovalp; 127786857b36SDavid Xu int error; 127886857b36SDavid Xu 127986857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 128086857b36SDavid Xu if (error != 0) 128186857b36SDavid Xu return (error); 128286857b36SDavid Xu 128386857b36SDavid Xu if (uap->ovalue != NULL) 128486857b36SDavid Xu ovalp = &oval; 128586857b36SDavid Xu else 128686857b36SDavid Xu ovalp = NULL; 128786857b36SDavid Xu 128886857b36SDavid Xu PROC_LOCK(p); 128986857b36SDavid Xu if (uap->timerid < 3 || 1290843b99c6SDavid Xu (it = itimer_find(p, uap->timerid)) == NULL) { 129186857b36SDavid Xu PROC_UNLOCK(p); 129286857b36SDavid Xu error = EINVAL; 129386857b36SDavid Xu } else { 129486857b36SDavid Xu PROC_UNLOCK(p); 129586857b36SDavid Xu itimer_enter(it); 129686857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_settime, 129786857b36SDavid Xu (it, uap->flags, &val, ovalp)); 129886857b36SDavid Xu itimer_leave(it); 129986857b36SDavid Xu ITIMER_UNLOCK(it); 130086857b36SDavid Xu } 130186857b36SDavid Xu if (error == 0 && uap->ovalue != NULL) 130286857b36SDavid Xu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 130386857b36SDavid Xu return (error); 130486857b36SDavid Xu } 130586857b36SDavid Xu 130686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 130761d3a4efSDavid Xu struct ktimer_gettime_args { 130861d3a4efSDavid Xu int timerid; 130986857b36SDavid Xu struct itimerspec * value; 131086857b36SDavid Xu }; 131186857b36SDavid Xu #endif 131286857b36SDavid Xu int 13138451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 131486857b36SDavid Xu { 131586857b36SDavid Xu struct proc *p = td->td_proc; 131686857b36SDavid Xu struct itimer *it; 131786857b36SDavid Xu struct itimerspec val; 131886857b36SDavid Xu int error; 131986857b36SDavid Xu 132086857b36SDavid Xu PROC_LOCK(p); 132186857b36SDavid Xu if (uap->timerid < 3 || 1322843b99c6SDavid Xu (it = itimer_find(p, uap->timerid)) == NULL) { 132386857b36SDavid Xu PROC_UNLOCK(p); 132486857b36SDavid Xu error = EINVAL; 132586857b36SDavid Xu } else { 132686857b36SDavid Xu PROC_UNLOCK(p); 132786857b36SDavid Xu itimer_enter(it); 132886857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_gettime, 132986857b36SDavid Xu (it, &val)); 133086857b36SDavid Xu itimer_leave(it); 133186857b36SDavid Xu ITIMER_UNLOCK(it); 133286857b36SDavid Xu } 133386857b36SDavid Xu if (error == 0) 133486857b36SDavid Xu error = copyout(&val, uap->value, sizeof(val)); 133586857b36SDavid Xu return (error); 133686857b36SDavid Xu } 133786857b36SDavid Xu 133886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 133986857b36SDavid Xu struct timer_getoverrun_args { 134061d3a4efSDavid Xu int timerid; 134186857b36SDavid Xu }; 134286857b36SDavid Xu #endif 134386857b36SDavid Xu int 13448451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 134586857b36SDavid Xu { 134686857b36SDavid Xu struct proc *p = td->td_proc; 134786857b36SDavid Xu struct itimer *it; 134886857b36SDavid Xu int error ; 134986857b36SDavid Xu 135086857b36SDavid Xu PROC_LOCK(p); 135186857b36SDavid Xu if (uap->timerid < 3 || 1352843b99c6SDavid Xu (it = itimer_find(p, uap->timerid)) == NULL) { 135386857b36SDavid Xu PROC_UNLOCK(p); 135486857b36SDavid Xu error = EINVAL; 135586857b36SDavid Xu } else { 135686857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 135786857b36SDavid Xu ITIMER_UNLOCK(it); 135856c06c4bSDavid Xu PROC_UNLOCK(p); 135986857b36SDavid Xu error = 0; 136086857b36SDavid Xu } 136186857b36SDavid Xu return (error); 136286857b36SDavid Xu } 136386857b36SDavid Xu 136486857b36SDavid Xu static int 136586857b36SDavid Xu realtimer_create(struct itimer *it) 136686857b36SDavid Xu { 136786857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 136886857b36SDavid Xu return (0); 136986857b36SDavid Xu } 137086857b36SDavid Xu 137186857b36SDavid Xu static int 137286857b36SDavid Xu realtimer_delete(struct itimer *it) 137386857b36SDavid Xu { 137486857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 1375843b99c6SDavid Xu 1376d6b6592eSDavid Xu /* 1377d6b6592eSDavid Xu * clear timer's value and interval to tell realtimer_expire 1378d6b6592eSDavid Xu * to not rearm the timer. 1379d6b6592eSDavid Xu */ 1380d6b6592eSDavid Xu timespecclear(&it->it_time.it_value); 1381d6b6592eSDavid Xu timespecclear(&it->it_time.it_interval); 1382843b99c6SDavid Xu ITIMER_UNLOCK(it); 1383843b99c6SDavid Xu callout_drain(&it->it_callout); 1384843b99c6SDavid Xu ITIMER_LOCK(it); 138586857b36SDavid Xu return (0); 138686857b36SDavid Xu } 138786857b36SDavid Xu 138886857b36SDavid Xu static int 138986857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 139086857b36SDavid Xu { 139156c06c4bSDavid Xu struct timespec cts; 139286857b36SDavid Xu 139386857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 139486857b36SDavid Xu 139556c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 139656c06c4bSDavid Xu *ovalue = it->it_time; 139786857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 139856c06c4bSDavid Xu timespecsub(&ovalue->it_value, &cts); 139986857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 140086857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 140186857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 140286857b36SDavid Xu ovalue->it_value.tv_sec = 0; 140386857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 140486857b36SDavid Xu } 140586857b36SDavid Xu } 140686857b36SDavid Xu return (0); 140786857b36SDavid Xu } 140886857b36SDavid Xu 140986857b36SDavid Xu static int 141086857b36SDavid Xu realtimer_settime(struct itimer *it, int flags, 141186857b36SDavid Xu struct itimerspec *value, struct itimerspec *ovalue) 141286857b36SDavid Xu { 141356c06c4bSDavid Xu struct timespec cts, ts; 141456c06c4bSDavid Xu struct timeval tv; 141556c06c4bSDavid Xu struct itimerspec val; 141686857b36SDavid Xu 141786857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 141886857b36SDavid Xu 141956c06c4bSDavid Xu val = *value; 142056c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 142186857b36SDavid Xu return (EINVAL); 142286857b36SDavid Xu 142356c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 142456c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 142586857b36SDavid Xu return (EINVAL); 142686857b36SDavid Xu } else { 142756c06c4bSDavid Xu timespecclear(&val.it_interval); 142886857b36SDavid Xu } 142986857b36SDavid Xu 143086857b36SDavid Xu if (ovalue != NULL) 143186857b36SDavid Xu realtimer_gettime(it, ovalue); 143286857b36SDavid Xu 143386857b36SDavid Xu it->it_time = val; 143456c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 143556c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 143656c06c4bSDavid Xu ts = val.it_value; 143786857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 143886857b36SDavid Xu /* Convert to absolute time. */ 143956c06c4bSDavid Xu timespecadd(&it->it_time.it_value, &cts); 144086857b36SDavid Xu } else { 144156c06c4bSDavid Xu timespecsub(&ts, &cts); 144286857b36SDavid Xu /* 144356c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 144486857b36SDavid Xu * fix it. 144586857b36SDavid Xu */ 144686857b36SDavid Xu } 144756c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 144856c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 144986857b36SDavid Xu realtimer_expire, it); 145086857b36SDavid Xu } else { 145186857b36SDavid Xu callout_stop(&it->it_callout); 145286857b36SDavid Xu } 145386857b36SDavid Xu 145486857b36SDavid Xu return (0); 145586857b36SDavid Xu } 145686857b36SDavid Xu 145786857b36SDavid Xu static void 145856c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 145986857b36SDavid Xu { 146086857b36SDavid Xu if (id == CLOCK_REALTIME) 146156c06c4bSDavid Xu getnanotime(ts); 146286857b36SDavid Xu else /* CLOCK_MONOTONIC */ 146356c06c4bSDavid Xu getnanouptime(ts); 146456c06c4bSDavid Xu } 146556c06c4bSDavid Xu 146656c06c4bSDavid Xu int 146761d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 146856c06c4bSDavid Xu { 146956c06c4bSDavid Xu struct itimer *it; 147056c06c4bSDavid Xu 147156c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 1472843b99c6SDavid Xu it = itimer_find(p, timerid); 147356c06c4bSDavid Xu if (it != NULL) { 147456c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 147556c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 147656c06c4bSDavid Xu it->it_overrun = 0; 147756c06c4bSDavid Xu ITIMER_UNLOCK(it); 147856c06c4bSDavid Xu return (0); 147956c06c4bSDavid Xu } 148056c06c4bSDavid Xu return (EINVAL); 148156c06c4bSDavid Xu } 148256c06c4bSDavid Xu 148356c06c4bSDavid Xu int 148456c06c4bSDavid Xu itimespecfix(struct timespec *ts) 148556c06c4bSDavid Xu { 148656c06c4bSDavid Xu 148756c06c4bSDavid Xu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 148856c06c4bSDavid Xu return (EINVAL); 148956c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 149056c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 149156c06c4bSDavid Xu return (0); 149286857b36SDavid Xu } 149386857b36SDavid Xu 149486857b36SDavid Xu /* Timeout callback for realtime timer */ 149586857b36SDavid Xu static void 149686857b36SDavid Xu realtimer_expire(void *arg) 149786857b36SDavid Xu { 149856c06c4bSDavid Xu struct timespec cts, ts; 149956c06c4bSDavid Xu struct timeval tv; 150086857b36SDavid Xu struct itimer *it; 150186857b36SDavid Xu 150286857b36SDavid Xu it = (struct itimer *)arg; 150386857b36SDavid Xu 150456c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 150586857b36SDavid Xu /* Only fire if time is reached. */ 150656c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 150756c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 150856c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 150986857b36SDavid Xu &it->it_time.it_interval); 151056c06c4bSDavid Xu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 151177e718f7SDavid Xu if (it->it_overrun < INT_MAX) 151286857b36SDavid Xu it->it_overrun++; 151377e718f7SDavid Xu else 151477e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 151556c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 151686857b36SDavid Xu &it->it_time.it_interval); 151786857b36SDavid Xu } 151886857b36SDavid Xu } else { 151986857b36SDavid Xu /* single shot timer ? */ 152056c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 152186857b36SDavid Xu } 152256c06c4bSDavid Xu if (timespecisset(&it->it_time.it_value)) { 152356c06c4bSDavid Xu ts = it->it_time.it_value; 152456c06c4bSDavid Xu timespecsub(&ts, &cts); 152556c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 152656c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 152786857b36SDavid Xu realtimer_expire, it); 152886857b36SDavid Xu } 1529d6b6592eSDavid Xu itimer_enter(it); 153086857b36SDavid Xu ITIMER_UNLOCK(it); 153186857b36SDavid Xu itimer_fire(it); 153286857b36SDavid Xu ITIMER_LOCK(it); 1533d6b6592eSDavid Xu itimer_leave(it); 153456c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 153556c06c4bSDavid Xu ts = it->it_time.it_value; 153656c06c4bSDavid Xu timespecsub(&ts, &cts); 153756c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 153856c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 153986857b36SDavid Xu it); 154086857b36SDavid Xu } 154186857b36SDavid Xu } 154286857b36SDavid Xu 154386857b36SDavid Xu void 154486857b36SDavid Xu itimer_fire(struct itimer *it) 154586857b36SDavid Xu { 154686857b36SDavid Xu struct proc *p = it->it_proc; 1547cf7d9a8cSDavid Xu struct thread *td; 154886857b36SDavid Xu 154956c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 155056c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1551cf7d9a8cSDavid Xu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 155256c06c4bSDavid Xu ITIMER_LOCK(it); 155356c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 155456c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 155556c06c4bSDavid Xu callout_stop(&it->it_callout); 155656c06c4bSDavid Xu ITIMER_UNLOCK(it); 1557cf7d9a8cSDavid Xu return; 15586d7b314bSDavid Xu } 1559cf7d9a8cSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 1560cf7d9a8cSDavid Xu it->it_ksi.ksi_errno = 0; 1561cf7d9a8cSDavid Xu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1562cf7d9a8cSDavid Xu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 156356c06c4bSDavid Xu } else { 156477e718f7SDavid Xu if (it->it_overrun < INT_MAX) 15656d7b314bSDavid Xu it->it_overrun++; 156677e718f7SDavid Xu else 156777e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 156856c06c4bSDavid Xu } 156986857b36SDavid Xu PROC_UNLOCK(p); 157086857b36SDavid Xu } 157186857b36SDavid Xu } 157286857b36SDavid Xu 157386857b36SDavid Xu static void 157486857b36SDavid Xu itimers_alloc(struct proc *p) 157586857b36SDavid Xu { 157660354683SDavid Xu struct itimers *its; 157760354683SDavid Xu int i; 157886857b36SDavid Xu 157960354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 158086857b36SDavid Xu LIST_INIT(&its->its_virtual); 158186857b36SDavid Xu LIST_INIT(&its->its_prof); 158286857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 158360354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 158460354683SDavid Xu its->its_timers[i] = NULL; 158560354683SDavid Xu PROC_LOCK(p); 158660354683SDavid Xu if (p->p_itimers == NULL) { 158760354683SDavid Xu p->p_itimers = its; 158860354683SDavid Xu PROC_UNLOCK(p); 158960354683SDavid Xu } 159060354683SDavid Xu else { 159160354683SDavid Xu PROC_UNLOCK(p); 159260354683SDavid Xu free(its, M_SUBPROC); 159360354683SDavid Xu } 159486857b36SDavid Xu } 159586857b36SDavid Xu 1596993182e5SAlexander Leidinger static void 1597993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1598993182e5SAlexander Leidinger { 1599993182e5SAlexander Leidinger itimers_event_hook_exit(arg, p); 1600993182e5SAlexander Leidinger } 1601993182e5SAlexander Leidinger 160286857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 1603d26b1a1fSDavid Xu static void 1604993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p) 160586857b36SDavid Xu { 160686857b36SDavid Xu struct itimers *its; 160786857b36SDavid Xu struct itimer *it; 16083e70c6f0SDavid Xu int event = (int)(intptr_t)arg; 160986857b36SDavid Xu int i; 161086857b36SDavid Xu 161160354683SDavid Xu if (p->p_itimers != NULL) { 161260354683SDavid Xu its = p->p_itimers; 161386857b36SDavid Xu for (i = 0; i < MAX_CLOCKS; ++i) { 161486857b36SDavid Xu if (posix_clocks[i].event_hook != NULL) 161586857b36SDavid Xu CLOCK_CALL(i, event_hook, (p, i, event)); 161686857b36SDavid Xu } 161786857b36SDavid Xu /* 161886857b36SDavid Xu * According to susv3, XSI interval timers should be inherited 161986857b36SDavid Xu * by new image. 162086857b36SDavid Xu */ 162186857b36SDavid Xu if (event == ITIMER_EV_EXEC) 162286857b36SDavid Xu i = 3; 162386857b36SDavid Xu else if (event == ITIMER_EV_EXIT) 162486857b36SDavid Xu i = 0; 162586857b36SDavid Xu else 162686857b36SDavid Xu panic("unhandled event"); 162786857b36SDavid Xu for (; i < TIMER_MAX; ++i) { 1628843b99c6SDavid Xu if ((it = its->its_timers[i]) != NULL) 1629843b99c6SDavid Xu kern_timer_delete(curthread, i); 163086857b36SDavid Xu } 163186857b36SDavid Xu if (its->its_timers[0] == NULL && 163286857b36SDavid Xu its->its_timers[1] == NULL && 163386857b36SDavid Xu its->its_timers[2] == NULL) { 163460354683SDavid Xu free(its, M_SUBPROC); 163560354683SDavid Xu p->p_itimers = NULL; 163686857b36SDavid Xu } 163786857b36SDavid Xu } 163886857b36SDavid Xu } 1639