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; 186d65f1abcSDavid Xu struct proc *p; 187d65f1abcSDavid Xu pid_t pid; 188d65f1abcSDavid Xu lwpid_t tid; 189d65f1abcSDavid Xu int error; 190d65f1abcSDavid Xu 191d65f1abcSDavid Xu switch(uap->which) { 192d65f1abcSDavid Xu case CPUCLOCK_WHICH_PID: 193d65f1abcSDavid Xu if (uap->id != 0) { 194d65f1abcSDavid Xu p = pfind(uap->id); 195d65f1abcSDavid Xu if (p == NULL) 196d65f1abcSDavid Xu return (ESRCH); 197d65f1abcSDavid Xu error = p_cansee(td, p); 198d65f1abcSDavid Xu PROC_UNLOCK(p); 199d65f1abcSDavid Xu if (error) 200d65f1abcSDavid Xu return (error); 201d65f1abcSDavid Xu pid = uap->id; 202d65f1abcSDavid Xu } else { 203d65f1abcSDavid Xu pid = td->td_proc->p_pid; 204d65f1abcSDavid Xu } 205d65f1abcSDavid Xu clk_id = MAKE_PROCESS_CPUCLOCK(pid); 206d65f1abcSDavid Xu break; 207d65f1abcSDavid Xu case CPUCLOCK_WHICH_TID: 208d65f1abcSDavid Xu if (uap->id == 0) 209d65f1abcSDavid Xu tid = td->td_tid; 210d65f1abcSDavid Xu else 211d65f1abcSDavid Xu tid = uap->id; 212d65f1abcSDavid Xu clk_id = MAKE_THREAD_CPUCLOCK(tid); 213d65f1abcSDavid Xu break; 214d65f1abcSDavid Xu default: 215d65f1abcSDavid Xu return (EINVAL); 216d65f1abcSDavid Xu } 217d65f1abcSDavid Xu return (copyout(&clk_id, uap->clock_id, sizeof(clockid_t))); 218d65f1abcSDavid Xu } 219d65f1abcSDavid Xu 220d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_ 22194c8fcd8SPeter Wemm struct clock_gettime_args { 22294c8fcd8SPeter Wemm clockid_t clock_id; 22394c8fcd8SPeter Wemm struct timespec *tp; 22494c8fcd8SPeter Wemm }; 22594c8fcd8SPeter Wemm #endif 22694c8fcd8SPeter Wemm /* ARGSUSED */ 22794c8fcd8SPeter Wemm int 2288451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) 22994c8fcd8SPeter Wemm { 23094c8fcd8SPeter Wemm struct timespec ats; 231f0b479cdSPaul Saab int error; 232f0b479cdSPaul Saab 233f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 234f0b479cdSPaul Saab if (error == 0) 235f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 236f0b479cdSPaul Saab 237f0b479cdSPaul Saab return (error); 238f0b479cdSPaul Saab } 239f0b479cdSPaul Saab 240d65f1abcSDavid Xu static inline void 241d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats) 242d65f1abcSDavid Xu { 243d65f1abcSDavid Xu runtime = cputick2usec(runtime); 244d65f1abcSDavid Xu ats->tv_sec = runtime / 1000000; 245d65f1abcSDavid Xu ats->tv_nsec = runtime % 1000000 * 1000; 246d65f1abcSDavid Xu } 247d65f1abcSDavid Xu 248d65f1abcSDavid Xu static void 249d65f1abcSDavid Xu get_thread_cputime(struct thread *targettd, struct timespec *ats) 250d65f1abcSDavid Xu { 251d65f1abcSDavid Xu uint64_t runtime, curtime, switchtime; 252d65f1abcSDavid Xu 253d65f1abcSDavid Xu if (targettd == NULL) { /* current thread */ 254d65f1abcSDavid Xu critical_enter(); 255d65f1abcSDavid Xu switchtime = PCPU_GET(switchtime); 256d65f1abcSDavid Xu curtime = cpu_ticks(); 257d65f1abcSDavid Xu runtime = curthread->td_runtime; 258d65f1abcSDavid Xu critical_exit(); 259d65f1abcSDavid Xu runtime += curtime - switchtime; 260d65f1abcSDavid Xu } else { 261d65f1abcSDavid Xu thread_lock(targettd); 262d65f1abcSDavid Xu runtime = targettd->td_runtime; 263d65f1abcSDavid Xu thread_unlock(targettd); 264d65f1abcSDavid Xu } 265d65f1abcSDavid Xu cputick2timespec(runtime, ats); 266d65f1abcSDavid Xu } 267d65f1abcSDavid Xu 268d65f1abcSDavid Xu static void 269d65f1abcSDavid Xu get_process_cputime(struct proc *targetp, struct timespec *ats) 270d65f1abcSDavid Xu { 271d65f1abcSDavid Xu uint64_t runtime; 272d65f1abcSDavid Xu struct rusage ru; 273d65f1abcSDavid Xu 274d65f1abcSDavid Xu PROC_SLOCK(targetp); 275d65f1abcSDavid Xu rufetch(targetp, &ru); 276d65f1abcSDavid Xu runtime = targetp->p_rux.rux_runtime; 277d65f1abcSDavid Xu PROC_SUNLOCK(targetp); 278d65f1abcSDavid Xu cputick2timespec(runtime, ats); 279d65f1abcSDavid Xu } 280d65f1abcSDavid Xu 281d65f1abcSDavid Xu static int 282d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) 283d65f1abcSDavid Xu { 284d65f1abcSDavid Xu struct proc *p, *p2; 285d65f1abcSDavid Xu struct thread *td2; 286d65f1abcSDavid Xu lwpid_t tid; 287d65f1abcSDavid Xu pid_t pid; 288d65f1abcSDavid Xu int error; 289d65f1abcSDavid Xu 290d65f1abcSDavid Xu p = td->td_proc; 291d65f1abcSDavid Xu if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { 292d65f1abcSDavid Xu tid = clock_id & CPUCLOCK_ID_MASK; 293d65f1abcSDavid Xu td2 = tdfind(tid, p->p_pid); 294d65f1abcSDavid Xu if (td2 == NULL) 295d65f1abcSDavid Xu return (EINVAL); 296d65f1abcSDavid Xu get_thread_cputime(td2, ats); 297d65f1abcSDavid Xu PROC_UNLOCK(td2->td_proc); 298d65f1abcSDavid Xu } else { 299d65f1abcSDavid Xu pid = clock_id & CPUCLOCK_ID_MASK; 300d65f1abcSDavid Xu p2 = pfind(pid); 301d65f1abcSDavid Xu if (p2 == NULL) 302d65f1abcSDavid Xu return (EINVAL); 303d65f1abcSDavid Xu error = p_cansee(td, p2); 304d65f1abcSDavid Xu if (error) { 305d65f1abcSDavid Xu PROC_UNLOCK(p2); 306d65f1abcSDavid Xu return (EINVAL); 307d65f1abcSDavid Xu } 308d65f1abcSDavid Xu get_process_cputime(p2, ats); 309d65f1abcSDavid Xu PROC_UNLOCK(p2); 310d65f1abcSDavid Xu } 311d65f1abcSDavid Xu return (0); 312d65f1abcSDavid Xu } 313d65f1abcSDavid Xu 314f0b479cdSPaul Saab int 315f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 316f0b479cdSPaul Saab { 317de0a9241SKelly Yancey struct timeval sys, user; 31878c85e8dSJohn Baldwin struct proc *p; 31994c8fcd8SPeter Wemm 32078c85e8dSJohn Baldwin p = td->td_proc; 321f0b479cdSPaul Saab switch (clock_id) { 3225e758b95SRobert Watson case CLOCK_REALTIME: /* Default to precise. */ 3235e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 324f0b479cdSPaul Saab nanotime(ats); 325b8817154SKelly Yancey break; 3265e758b95SRobert Watson case CLOCK_REALTIME_FAST: 3275e758b95SRobert Watson getnanotime(ats); 3285e758b95SRobert Watson break; 329b8817154SKelly Yancey case CLOCK_VIRTUAL: 33078c85e8dSJohn Baldwin PROC_LOCK(p); 331a1fe14bcSAttilio Rao PROC_SLOCK(p); 33278c85e8dSJohn Baldwin calcru(p, &user, &sys); 333a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 33478c85e8dSJohn Baldwin PROC_UNLOCK(p); 335f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 336b8817154SKelly Yancey break; 337b8817154SKelly Yancey case CLOCK_PROF: 33878c85e8dSJohn Baldwin PROC_LOCK(p); 339a1fe14bcSAttilio Rao PROC_SLOCK(p); 34078c85e8dSJohn Baldwin calcru(p, &user, &sys); 341a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 34278c85e8dSJohn Baldwin PROC_UNLOCK(p); 343de0a9241SKelly Yancey timevaladd(&user, &sys); 344f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 345de0a9241SKelly Yancey break; 3465e758b95SRobert Watson case CLOCK_MONOTONIC: /* Default to precise. */ 3475e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 3485eefd889SAndre Oppermann case CLOCK_UPTIME: 3495e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 350f0b479cdSPaul Saab nanouptime(ats); 351b8817154SKelly Yancey break; 3525e758b95SRobert Watson case CLOCK_UPTIME_FAST: 3535e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 3545e758b95SRobert Watson getnanouptime(ats); 3555e758b95SRobert Watson break; 3565e758b95SRobert Watson case CLOCK_SECOND: 3575e758b95SRobert Watson ats->tv_sec = time_second; 3585e758b95SRobert Watson ats->tv_nsec = 0; 3595e758b95SRobert Watson break; 36000d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 361d65f1abcSDavid Xu get_thread_cputime(NULL, ats); 362d65f1abcSDavid Xu break; 363d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 364d65f1abcSDavid Xu PROC_LOCK(p); 365d65f1abcSDavid Xu get_process_cputime(p, ats); 366d65f1abcSDavid Xu PROC_UNLOCK(p); 36700d6ac63SDavid Xu break; 368b8817154SKelly Yancey default: 369d65f1abcSDavid Xu if ((int)clock_id >= 0) 3705cb3dc8fSPoul-Henning Kamp return (EINVAL); 371d65f1abcSDavid Xu return (get_cputime(td, clock_id, ats)); 372b8817154SKelly Yancey } 373f0b479cdSPaul Saab return (0); 37494c8fcd8SPeter Wemm } 37594c8fcd8SPeter Wemm 37694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 37794c8fcd8SPeter Wemm struct clock_settime_args { 37894c8fcd8SPeter Wemm clockid_t clock_id; 37994c8fcd8SPeter Wemm const struct timespec *tp; 38094c8fcd8SPeter Wemm }; 38194c8fcd8SPeter Wemm #endif 38294c8fcd8SPeter Wemm /* ARGSUSED */ 38394c8fcd8SPeter Wemm int 3848451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap) 38594c8fcd8SPeter Wemm { 38694c8fcd8SPeter Wemm struct timespec ats; 38794c8fcd8SPeter Wemm int error; 38894c8fcd8SPeter Wemm 389f0b479cdSPaul Saab if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 390f0b479cdSPaul Saab return (error); 391f0b479cdSPaul Saab return (kern_clock_settime(td, uap->clock_id, &ats)); 392f0b479cdSPaul Saab } 393f0b479cdSPaul Saab 394f0b479cdSPaul Saab int 395f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 396f0b479cdSPaul Saab { 397f0b479cdSPaul Saab struct timeval atv; 398f0b479cdSPaul Saab int error; 399f0b479cdSPaul Saab 400acd3428bSRobert Watson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 4017edfb592SJohn Baldwin return (error); 402f0b479cdSPaul Saab if (clock_id != CLOCK_REALTIME) 4037edfb592SJohn Baldwin return (EINVAL); 404f0b479cdSPaul Saab if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 4057edfb592SJohn Baldwin return (EINVAL); 406a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 407f0b479cdSPaul Saab TIMESPEC_TO_TIMEVAL(&atv, ats); 4087edfb592SJohn Baldwin error = settime(td, &atv); 40994c8fcd8SPeter Wemm return (error); 41094c8fcd8SPeter Wemm } 41194c8fcd8SPeter Wemm 41294c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 41394c8fcd8SPeter Wemm struct clock_getres_args { 41494c8fcd8SPeter Wemm clockid_t clock_id; 41594c8fcd8SPeter Wemm struct timespec *tp; 41694c8fcd8SPeter Wemm }; 41794c8fcd8SPeter Wemm #endif 41894c8fcd8SPeter Wemm int 4198451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap) 42094c8fcd8SPeter Wemm { 42194c8fcd8SPeter Wemm struct timespec ts; 422f0b479cdSPaul Saab int error; 42394c8fcd8SPeter Wemm 424f0b479cdSPaul Saab if (uap->tp == NULL) 425f0b479cdSPaul Saab return (0); 426f0b479cdSPaul Saab 427f0b479cdSPaul Saab error = kern_clock_getres(td, uap->clock_id, &ts); 428f0b479cdSPaul Saab if (error == 0) 429f0b479cdSPaul Saab error = copyout(&ts, uap->tp, sizeof(ts)); 430f0b479cdSPaul Saab return (error); 431f0b479cdSPaul Saab } 432f0b479cdSPaul Saab 433f0b479cdSPaul Saab int 434f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 435f0b479cdSPaul Saab { 436f0b479cdSPaul Saab 437f0b479cdSPaul Saab ts->tv_sec = 0; 438f0b479cdSPaul Saab switch (clock_id) { 439b8817154SKelly Yancey case CLOCK_REALTIME: 4405e758b95SRobert Watson case CLOCK_REALTIME_FAST: 4415e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 442b8817154SKelly Yancey case CLOCK_MONOTONIC: 4435e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 4445e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 4455eefd889SAndre Oppermann case CLOCK_UPTIME: 4465e758b95SRobert Watson case CLOCK_UPTIME_FAST: 4475e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 448ac0653dcSBruce Evans /* 449ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 450ac0653dcSBruce Evans * Rounding up is especially important if rounding down 451ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 452ac0653dcSBruce Evans */ 453f0b479cdSPaul Saab ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 454b8817154SKelly Yancey break; 455b8817154SKelly Yancey case CLOCK_VIRTUAL: 456b8817154SKelly Yancey case CLOCK_PROF: 457b8817154SKelly Yancey /* Accurately round up here because we can do so cheaply. */ 458f0b479cdSPaul Saab ts->tv_nsec = (1000000000 + hz - 1) / hz; 459b8817154SKelly Yancey break; 4605e758b95SRobert Watson case CLOCK_SECOND: 4615e758b95SRobert Watson ts->tv_sec = 1; 4625e758b95SRobert Watson ts->tv_nsec = 0; 4635e758b95SRobert Watson break; 46400d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 465d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 466d65f1abcSDavid Xu cputime: 46700d6ac63SDavid Xu /* sync with cputick2usec */ 46800d6ac63SDavid Xu ts->tv_nsec = 1000000 / cpu_tickrate(); 46900d6ac63SDavid Xu if (ts->tv_nsec == 0) 47000d6ac63SDavid Xu ts->tv_nsec = 1000; 47100d6ac63SDavid Xu break; 472b8817154SKelly Yancey default: 473d65f1abcSDavid Xu if ((int)clock_id < 0) 474d65f1abcSDavid Xu goto cputime; 475b8817154SKelly Yancey return (EINVAL); 47694c8fcd8SPeter Wemm } 477de0a9241SKelly Yancey return (0); 47894c8fcd8SPeter Wemm } 47994c8fcd8SPeter Wemm 480*0dbf17e6SAlexander Motin static uint8_t nanowait[MAXCPU]; 48194c8fcd8SPeter Wemm 4827fdf2c85SPaul Saab int 4837fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 4845b870b7bSPeter Wemm { 485098176f0SDavide Italiano struct timespec ts; 486098176f0SDavide Italiano sbintime_t sbt, sbtt, prec, tmp; 487980c545dSAlexander Motin time_t over; 48833841826SPoul-Henning Kamp int error; 4895b870b7bSPeter Wemm 4907d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 491708e7684SPeter Wemm return (EINVAL); 492d254af07SMatthew Dillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 4937d7fb492SBruce Evans return (0); 494980c545dSAlexander Motin ts = *rqt; 495980c545dSAlexander Motin if (ts.tv_sec > INT32_MAX / 2) { 496980c545dSAlexander Motin over = ts.tv_sec - INT32_MAX / 2; 497980c545dSAlexander Motin ts.tv_sec -= over; 498980c545dSAlexander Motin } else 499980c545dSAlexander Motin over = 0; 500980c545dSAlexander Motin tmp = tstosbt(ts); 501098176f0SDavide Italiano prec = tmp; 502098176f0SDavide Italiano prec >>= tc_precexp; 503098176f0SDavide Italiano if (TIMESEL(&sbt, tmp)) 504098176f0SDavide Italiano sbt += tc_tick_sbt; 505098176f0SDavide Italiano sbt += tmp; 506*0dbf17e6SAlexander Motin error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp", 507*0dbf17e6SAlexander Motin sbt, prec, C_ABSOLUTE); 50833841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 50933841826SPoul-Henning Kamp if (error == ERESTART) 51094c8fcd8SPeter Wemm error = EINTR; 511098176f0SDavide Italiano TIMESEL(&sbtt, tmp); 51233841826SPoul-Henning Kamp if (rmt != NULL) { 513098176f0SDavide Italiano ts = sbttots(sbt - sbtt); 514980c545dSAlexander Motin ts.tv_sec += over; 51533841826SPoul-Henning Kamp if (ts.tv_sec < 0) 51633841826SPoul-Henning Kamp timespecclear(&ts); 51700af9731SPoul-Henning Kamp *rmt = ts; 51800af9731SPoul-Henning Kamp } 519098176f0SDavide Italiano if (sbtt >= sbt) 520098176f0SDavide Italiano return (0); 52133841826SPoul-Henning Kamp return (error); 52233841826SPoul-Henning Kamp } 52333841826SPoul-Henning Kamp return (0); 5245b870b7bSPeter Wemm } 52594c8fcd8SPeter Wemm 5265b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 5275b870b7bSPeter Wemm struct nanosleep_args { 5285b870b7bSPeter Wemm struct timespec *rqtp; 5295b870b7bSPeter Wemm struct timespec *rmtp; 5305b870b7bSPeter Wemm }; 5315b870b7bSPeter Wemm #endif 5325b870b7bSPeter Wemm /* ARGSUSED */ 5335b870b7bSPeter Wemm int 5348451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap) 5355b870b7bSPeter Wemm { 5365b870b7bSPeter Wemm struct timespec rmt, rqt; 537fb99ab88SMatthew Dillon int error; 5385b870b7bSPeter Wemm 539d1e405c5SAlfred Perlstein error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 5405b870b7bSPeter Wemm if (error) 5415b870b7bSPeter Wemm return (error); 542fb99ab88SMatthew Dillon 54331f3e2adSAlfred Perlstein if (uap->rmtp && 54431f3e2adSAlfred Perlstein !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 54531f3e2adSAlfred Perlstein return (EFAULT); 5467fdf2c85SPaul Saab error = kern_nanosleep(td, &rqt, &rmt); 547d1e405c5SAlfred Perlstein if (error && uap->rmtp) { 548fb99ab88SMatthew Dillon int error2; 549fb99ab88SMatthew Dillon 550d1e405c5SAlfred Perlstein error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 55131f3e2adSAlfred Perlstein if (error2) 552fb99ab88SMatthew Dillon error = error2; 553708e7684SPeter Wemm } 554708e7684SPeter Wemm return (error); 55594c8fcd8SPeter Wemm } 55694c8fcd8SPeter Wemm 5575b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 558df8bae1dSRodney W. Grimes struct gettimeofday_args { 559df8bae1dSRodney W. Grimes struct timeval *tp; 560df8bae1dSRodney W. Grimes struct timezone *tzp; 561df8bae1dSRodney W. Grimes }; 562d2d3e875SBruce Evans #endif 563df8bae1dSRodney W. Grimes /* ARGSUSED */ 56426f9a767SRodney W. Grimes int 5658451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap) 566df8bae1dSRodney W. Grimes { 567df8bae1dSRodney W. Grimes struct timeval atv; 568411c25edSTim J. Robbins struct timezone rtz; 569df8bae1dSRodney W. Grimes int error = 0; 570df8bae1dSRodney W. Grimes 571df8bae1dSRodney W. Grimes if (uap->tp) { 572df8bae1dSRodney W. Grimes microtime(&atv); 57301609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 574df8bae1dSRodney W. Grimes } 57521dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 57691f1c2b3SPoul-Henning Kamp rtz.tz_minuteswest = tz_minuteswest; 57791f1c2b3SPoul-Henning Kamp rtz.tz_dsttime = tz_dsttime; 578411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 57921dcdb38SPoul-Henning Kamp } 580df8bae1dSRodney W. Grimes return (error); 581df8bae1dSRodney W. Grimes } 582df8bae1dSRodney W. Grimes 583d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 584df8bae1dSRodney W. Grimes struct settimeofday_args { 585df8bae1dSRodney W. Grimes struct timeval *tv; 586df8bae1dSRodney W. Grimes struct timezone *tzp; 587df8bae1dSRodney W. Grimes }; 588d2d3e875SBruce Evans #endif 589df8bae1dSRodney W. Grimes /* ARGSUSED */ 59026f9a767SRodney W. Grimes int 5918451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap) 592df8bae1dSRodney W. Grimes { 593b88ec951SJohn Baldwin struct timeval atv, *tvp; 594b88ec951SJohn Baldwin struct timezone atz, *tzp; 595b88ec951SJohn Baldwin int error; 596b88ec951SJohn Baldwin 597b88ec951SJohn Baldwin if (uap->tv) { 598b88ec951SJohn Baldwin error = copyin(uap->tv, &atv, sizeof(atv)); 599b88ec951SJohn Baldwin if (error) 600b88ec951SJohn Baldwin return (error); 601b88ec951SJohn Baldwin tvp = &atv; 602b88ec951SJohn Baldwin } else 603b88ec951SJohn Baldwin tvp = NULL; 604b88ec951SJohn Baldwin if (uap->tzp) { 605b88ec951SJohn Baldwin error = copyin(uap->tzp, &atz, sizeof(atz)); 606b88ec951SJohn Baldwin if (error) 607b88ec951SJohn Baldwin return (error); 608b88ec951SJohn Baldwin tzp = &atz; 609b88ec951SJohn Baldwin } else 610b88ec951SJohn Baldwin tzp = NULL; 611b88ec951SJohn Baldwin return (kern_settimeofday(td, tvp, tzp)); 612b88ec951SJohn Baldwin } 613b88ec951SJohn Baldwin 614b88ec951SJohn Baldwin int 615b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 616b88ec951SJohn Baldwin { 617b88ec951SJohn Baldwin int error; 618fb99ab88SMatthew Dillon 619acd3428bSRobert Watson error = priv_check(td, PRIV_SETTIMEOFDAY); 620b88ec951SJohn Baldwin if (error) 6217edfb592SJohn Baldwin return (error); 622df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 623b88ec951SJohn Baldwin if (tv) { 624b88ec951SJohn Baldwin if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 6257edfb592SJohn Baldwin return (EINVAL); 626b88ec951SJohn Baldwin error = settime(td, tv); 627708e7684SPeter Wemm } 628b88ec951SJohn Baldwin if (tzp && error == 0) { 629b88ec951SJohn Baldwin tz_minuteswest = tzp->tz_minuteswest; 630b88ec951SJohn Baldwin tz_dsttime = tzp->tz_dsttime; 631b88ec951SJohn Baldwin } 6327edfb592SJohn Baldwin return (error); 633b88ec951SJohn Baldwin } 6347edfb592SJohn Baldwin 635df8bae1dSRodney W. Grimes /* 636873fbcd7SRobert Watson * Get value of an interval timer. The process virtual and profiling virtual 637873fbcd7SRobert Watson * time timers are kept in the p_stats area, since they can be swapped out. 638873fbcd7SRobert Watson * These are kept internally in the way they are specified externally: in 639873fbcd7SRobert Watson * time until they expire. 640df8bae1dSRodney W. Grimes * 641873fbcd7SRobert Watson * The real time interval timer is kept in the process table slot for the 642873fbcd7SRobert Watson * process, and its value (it_value) is kept as an absolute time rather than 643873fbcd7SRobert Watson * as a delta, so that it is easy to keep periodic real-time signals from 644873fbcd7SRobert Watson * drifting. 645df8bae1dSRodney W. Grimes * 646df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 647873fbcd7SRobert Watson * kern_clock.c. The real time timer is processed by a timeout routine, 648873fbcd7SRobert Watson * called from the softclock() routine. Since a callout may be delayed in 649873fbcd7SRobert Watson * real time due to interrupt processing in the system, it is possible for 650873fbcd7SRobert Watson * the real time timeout routine (realitexpire, given below), to be delayed 651873fbcd7SRobert Watson * in real time past when it is supposed to occur. It does not suffice, 652873fbcd7SRobert Watson * therefore, to reload the real timer .it_value from the real time timers 653873fbcd7SRobert Watson * .it_interval. Rather, we compute the next time in absolute time the timer 654873fbcd7SRobert Watson * should go off. 655df8bae1dSRodney W. Grimes */ 656d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 657df8bae1dSRodney W. Grimes struct getitimer_args { 658df8bae1dSRodney W. Grimes u_int which; 659df8bae1dSRodney W. Grimes struct itimerval *itv; 660df8bae1dSRodney W. Grimes }; 661d2d3e875SBruce Evans #endif 66226f9a767SRodney W. Grimes int 6638451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap) 664df8bae1dSRodney W. Grimes { 665df8bae1dSRodney W. Grimes struct itimerval aitv; 666c90110d6SJohn Baldwin int error; 667df8bae1dSRodney W. Grimes 668cfa0efe7SMaxim Sobolev error = kern_getitimer(td, uap->which, &aitv); 669cfa0efe7SMaxim Sobolev if (error != 0) 670cfa0efe7SMaxim Sobolev return (error); 671cfa0efe7SMaxim Sobolev return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 672cfa0efe7SMaxim Sobolev } 673cfa0efe7SMaxim Sobolev 674cfa0efe7SMaxim Sobolev int 675cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 676cfa0efe7SMaxim Sobolev { 677cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 678cfa0efe7SMaxim Sobolev struct timeval ctv; 679cfa0efe7SMaxim Sobolev 680cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 681df8bae1dSRodney W. Grimes return (EINVAL); 682fb99ab88SMatthew Dillon 683cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 684df8bae1dSRodney W. Grimes /* 685ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 686df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 687df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 688df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 689df8bae1dSRodney W. Grimes */ 69096d7f8efSTim J. Robbins PROC_LOCK(p); 691cfa0efe7SMaxim Sobolev *aitv = p->p_realtimer; 69296d7f8efSTim J. Robbins PROC_UNLOCK(p); 693cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 694b5ea3779SAlexander Motin microuptime(&ctv); 695cfa0efe7SMaxim Sobolev if (timevalcmp(&aitv->it_value, &ctv, <)) 696cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_value); 697df8bae1dSRodney W. Grimes else 698cfa0efe7SMaxim Sobolev timevalsub(&aitv->it_value, &ctv); 699227ee8a1SPoul-Henning Kamp } 700fb99ab88SMatthew Dillon } else { 701982d11f8SJeff Roberson PROC_SLOCK(p); 702cfa0efe7SMaxim Sobolev *aitv = p->p_stats->p_timer[which]; 703982d11f8SJeff Roberson PROC_SUNLOCK(p); 704fb99ab88SMatthew Dillon } 705cfa0efe7SMaxim Sobolev return (0); 706df8bae1dSRodney W. Grimes } 707df8bae1dSRodney W. Grimes 708d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 709df8bae1dSRodney W. Grimes struct setitimer_args { 710df8bae1dSRodney W. Grimes u_int which; 711df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 712df8bae1dSRodney W. Grimes }; 713d2d3e875SBruce Evans #endif 71426f9a767SRodney W. Grimes int 7158451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap) 716df8bae1dSRodney W. Grimes { 717cfa0efe7SMaxim Sobolev struct itimerval aitv, oitv; 718c90110d6SJohn Baldwin int error; 71996d7f8efSTim J. Robbins 72096d7f8efSTim J. Robbins if (uap->itv == NULL) { 72196d7f8efSTim J. Robbins uap->itv = uap->oitv; 7228451d0ddSKip Macy return (sys_getitimer(td, (struct getitimer_args *)uap)); 72396d7f8efSTim J. Robbins } 724df8bae1dSRodney W. Grimes 72596d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 726df8bae1dSRodney W. Grimes return (error); 727cfa0efe7SMaxim Sobolev error = kern_setitimer(td, uap->which, &aitv, &oitv); 728cfa0efe7SMaxim Sobolev if (error != 0 || uap->oitv == NULL) 729cfa0efe7SMaxim Sobolev return (error); 730cfa0efe7SMaxim Sobolev return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 731cfa0efe7SMaxim Sobolev } 732cfa0efe7SMaxim Sobolev 733cfa0efe7SMaxim Sobolev int 734c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 735c90110d6SJohn Baldwin struct itimerval *oitv) 736cfa0efe7SMaxim Sobolev { 737cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 738cfa0efe7SMaxim Sobolev struct timeval ctv; 739b5ea3779SAlexander Motin sbintime_t sbt, pr; 740cfa0efe7SMaxim Sobolev 7415e85ac17SJohn Baldwin if (aitv == NULL) 7425e85ac17SJohn Baldwin return (kern_getitimer(td, which, oitv)); 7435e85ac17SJohn Baldwin 744cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 74596d7f8efSTim J. Robbins return (EINVAL); 746b5ea3779SAlexander Motin if (itimerfix(&aitv->it_value) || 747b5ea3779SAlexander Motin aitv->it_value.tv_sec > INT32_MAX / 2) 748cfa0efe7SMaxim Sobolev return (EINVAL); 749cfa0efe7SMaxim Sobolev if (!timevalisset(&aitv->it_value)) 750cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_interval); 751b5ea3779SAlexander Motin else if (itimerfix(&aitv->it_interval) || 752b5ea3779SAlexander Motin aitv->it_interval.tv_sec > INT32_MAX / 2) 75396d7f8efSTim J. Robbins return (EINVAL); 75496d7f8efSTim J. Robbins 755cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 75696d7f8efSTim J. Robbins PROC_LOCK(p); 7574cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 7584f559836SJake Burkholder callout_stop(&p->p_itcallout); 759b5ea3779SAlexander Motin microuptime(&ctv); 760cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 761b5ea3779SAlexander Motin pr = tvtosbt(aitv->it_value) >> tc_precexp; 762cfa0efe7SMaxim Sobolev timevaladd(&aitv->it_value, &ctv); 763b5ea3779SAlexander Motin sbt = tvtosbt(aitv->it_value); 764b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, sbt, pr, 765b5ea3779SAlexander Motin realitexpire, p, C_ABSOLUTE); 76625b4d3a8SJohn Baldwin } 767cfa0efe7SMaxim Sobolev *oitv = p->p_realtimer; 768cfa0efe7SMaxim Sobolev p->p_realtimer = *aitv; 76996d7f8efSTim J. Robbins PROC_UNLOCK(p); 770cfa0efe7SMaxim Sobolev if (timevalisset(&oitv->it_value)) { 771cfa0efe7SMaxim Sobolev if (timevalcmp(&oitv->it_value, &ctv, <)) 772cfa0efe7SMaxim Sobolev timevalclear(&oitv->it_value); 77396d7f8efSTim J. Robbins else 774cfa0efe7SMaxim Sobolev timevalsub(&oitv->it_value, &ctv); 775fb99ab88SMatthew Dillon } 77696d7f8efSTim J. Robbins } else { 777982d11f8SJeff Roberson PROC_SLOCK(p); 778cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 779cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 780982d11f8SJeff Roberson PROC_SUNLOCK(p); 78196d7f8efSTim J. Robbins } 78296d7f8efSTim J. Robbins return (0); 783df8bae1dSRodney W. Grimes } 784df8bae1dSRodney W. Grimes 785df8bae1dSRodney W. Grimes /* 786df8bae1dSRodney W. Grimes * Real interval timer expired: 787df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 788df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 789df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 790df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 791df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 792c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 7939207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 7949207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 7959207f00aSBruce Evans * interrupt even when we're delayed. 796df8bae1dSRodney W. Grimes */ 797df8bae1dSRodney W. Grimes void 79891afe087SPoul-Henning Kamp realitexpire(void *arg) 799df8bae1dSRodney W. Grimes { 80091afe087SPoul-Henning Kamp struct proc *p; 801b5ea3779SAlexander Motin struct timeval ctv; 802b5ea3779SAlexander Motin sbintime_t isbt; 803df8bae1dSRodney W. Grimes 804df8bae1dSRodney W. Grimes p = (struct proc *)arg; 8058451d0ddSKip Macy kern_psignal(p, SIGALRM); 8064cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 8074cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 8085499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 8095499ea01SJohn Baldwin wakeup(&p->p_itcallout); 810df8bae1dSRodney W. Grimes return; 811df8bae1dSRodney W. Grimes } 812b5ea3779SAlexander Motin isbt = tvtosbt(p->p_realtimer.it_interval); 813b5ea3779SAlexander Motin if (isbt >= sbt_timethreshold) 814b5ea3779SAlexander Motin getmicrouptime(&ctv); 815b5ea3779SAlexander Motin else 816b5ea3779SAlexander Motin microuptime(&ctv); 817b5ea3779SAlexander Motin do { 818df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 819df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 820b5ea3779SAlexander Motin } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=)); 821b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value), 822b5ea3779SAlexander Motin isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE); 823df8bae1dSRodney W. Grimes } 824df8bae1dSRodney W. Grimes 825df8bae1dSRodney W. Grimes /* 826df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 827df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 828df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 829df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 830df8bae1dSRodney W. Grimes */ 83126f9a767SRodney W. Grimes int 83291afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 833df8bae1dSRodney W. Grimes { 834df8bae1dSRodney W. Grimes 83586857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 836df8bae1dSRodney W. Grimes return (EINVAL); 837b5ea3779SAlexander Motin if (tv->tv_sec == 0 && tv->tv_usec != 0 && 838b5ea3779SAlexander Motin tv->tv_usec < (u_int)tick / 16) 839b5ea3779SAlexander Motin tv->tv_usec = (u_int)tick / 16; 840df8bae1dSRodney W. Grimes return (0); 841df8bae1dSRodney W. Grimes } 842df8bae1dSRodney W. Grimes 843df8bae1dSRodney W. Grimes /* 844df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 845df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 846df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 847df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 848df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 849df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 850df8bae1dSRodney W. Grimes * that it is called in a context where the timers 851df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 852df8bae1dSRodney W. Grimes */ 85326f9a767SRodney W. Grimes int 85491afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 855df8bae1dSRodney W. Grimes { 856df8bae1dSRodney W. Grimes 857df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 858df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 859df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 860df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 861df8bae1dSRodney W. Grimes goto expire; 862df8bae1dSRodney W. Grimes } 863df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 864df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 865df8bae1dSRodney W. Grimes } 866df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 867df8bae1dSRodney W. Grimes usec = 0; 8684cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 869df8bae1dSRodney W. Grimes return (1); 870df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 871df8bae1dSRodney W. Grimes expire: 8724cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 873df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 874df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 875df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 876df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 877df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 878df8bae1dSRodney W. Grimes } 879df8bae1dSRodney W. Grimes } else 880df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 881df8bae1dSRodney W. Grimes return (0); 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes 884df8bae1dSRodney W. Grimes /* 885df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 886df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 887df8bae1dSRodney W. Grimes * results which are before the beginning, 888df8bae1dSRodney W. Grimes * it just gets very confused in this case. 889df8bae1dSRodney W. Grimes * Caveat emptor. 890df8bae1dSRodney W. Grimes */ 89126f9a767SRodney W. Grimes void 8926ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 893df8bae1dSRodney W. Grimes { 894df8bae1dSRodney W. Grimes 895df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 896df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 897df8bae1dSRodney W. Grimes timevalfix(t1); 898df8bae1dSRodney W. Grimes } 899df8bae1dSRodney W. Grimes 90026f9a767SRodney W. Grimes void 9016ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 902df8bae1dSRodney W. Grimes { 903df8bae1dSRodney W. Grimes 904df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 905df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 906df8bae1dSRodney W. Grimes timevalfix(t1); 907df8bae1dSRodney W. Grimes } 908df8bae1dSRodney W. Grimes 90987b6de2bSPoul-Henning Kamp static void 91091afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 911df8bae1dSRodney W. Grimes { 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 914df8bae1dSRodney W. Grimes t1->tv_sec--; 915df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 918df8bae1dSRodney W. Grimes t1->tv_sec++; 919df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 920df8bae1dSRodney W. Grimes } 921df8bae1dSRodney W. Grimes } 92291974ce1SSam Leffler 92391974ce1SSam Leffler /* 924addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 92591974ce1SSam Leffler */ 92691974ce1SSam Leffler int 92791974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 92891974ce1SSam Leffler { 92991974ce1SSam Leffler struct timeval tv, delta; 93091974ce1SSam Leffler int rv = 0; 93191974ce1SSam Leffler 932addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 933addea9d4SSam Leffler delta = tv; 934addea9d4SSam Leffler timevalsub(&delta, lasttime); 93591974ce1SSam Leffler 93691974ce1SSam Leffler /* 93791974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 93891974ce1SSam Leffler * even if interval is huge. 93991974ce1SSam Leffler */ 94091974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 94191974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 94291974ce1SSam Leffler *lasttime = tv; 94391974ce1SSam Leffler rv = 1; 94491974ce1SSam Leffler } 94591974ce1SSam Leffler 94691974ce1SSam Leffler return (rv); 94791974ce1SSam Leffler } 94891974ce1SSam Leffler 94991974ce1SSam Leffler /* 95091974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 951addea9d4SSam Leffler * 952addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 953addea9d4SSam Leffler * should drop a packet because of the rate limitation). 954addea9d4SSam Leffler * 955893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 956893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 957893bec80SSam Leffler * limiting. 958893bec80SSam Leffler * 959addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 960addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 961addea9d4SSam Leffler * clock ticks for minimal overhead. 96291974ce1SSam Leffler */ 96391974ce1SSam Leffler int 96491974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 96591974ce1SSam Leffler { 966addea9d4SSam Leffler int now; 96791974ce1SSam Leffler 96891974ce1SSam Leffler /* 969addea9d4SSam Leffler * Reset the last time and counter if this is the first call 970addea9d4SSam Leffler * or more than a second has passed since the last update of 971addea9d4SSam Leffler * lasttime. 97291974ce1SSam Leffler */ 973addea9d4SSam Leffler now = ticks; 974addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 975addea9d4SSam Leffler lasttime->tv_sec = now; 976addea9d4SSam Leffler *curpps = 1; 977893bec80SSam Leffler return (maxpps != 0); 978addea9d4SSam Leffler } else { 979addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 980addea9d4SSam Leffler return (maxpps < 0 || *curpps < maxpps); 981addea9d4SSam Leffler } 98291974ce1SSam Leffler } 98386857b36SDavid Xu 98486857b36SDavid Xu static void 98586857b36SDavid Xu itimer_start(void) 98686857b36SDavid Xu { 98786857b36SDavid Xu struct kclock rt_clock = { 98886857b36SDavid Xu .timer_create = realtimer_create, 98986857b36SDavid Xu .timer_delete = realtimer_delete, 99086857b36SDavid Xu .timer_settime = realtimer_settime, 99186857b36SDavid Xu .timer_gettime = realtimer_gettime, 992843b99c6SDavid Xu .event_hook = NULL 99386857b36SDavid Xu }; 99486857b36SDavid Xu 99586857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 99686857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 99786857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 99886857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 99977e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 100077e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 100177e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 1002993182e5SAlexander Leidinger EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 1003d26b1a1fSDavid Xu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 1004993182e5SAlexander Leidinger EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 1005d26b1a1fSDavid Xu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 100686857b36SDavid Xu } 100786857b36SDavid Xu 100886857b36SDavid Xu int 100986857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk) 101086857b36SDavid Xu { 101186857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 101286857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 101386857b36SDavid Xu return (0); 101486857b36SDavid Xu } 101586857b36SDavid Xu posix_clocks[clockid] = *clk; 101686857b36SDavid Xu return (1); 101786857b36SDavid Xu } 101886857b36SDavid Xu 101986857b36SDavid Xu static int 102086857b36SDavid Xu itimer_init(void *mem, int size, int flags) 102186857b36SDavid Xu { 102286857b36SDavid Xu struct itimer *it; 102386857b36SDavid Xu 102486857b36SDavid Xu it = (struct itimer *)mem; 102586857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 102686857b36SDavid Xu return (0); 102786857b36SDavid Xu } 102886857b36SDavid Xu 102986857b36SDavid Xu static void 103086857b36SDavid Xu itimer_fini(void *mem, int size) 103186857b36SDavid Xu { 103286857b36SDavid Xu struct itimer *it; 103386857b36SDavid Xu 103486857b36SDavid Xu it = (struct itimer *)mem; 103586857b36SDavid Xu mtx_destroy(&it->it_mtx); 103686857b36SDavid Xu } 103786857b36SDavid Xu 103886857b36SDavid Xu static void 103986857b36SDavid Xu itimer_enter(struct itimer *it) 104086857b36SDavid Xu { 104186857b36SDavid Xu 104286857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 104386857b36SDavid Xu it->it_usecount++; 104486857b36SDavid Xu } 104586857b36SDavid Xu 104686857b36SDavid Xu static void 104786857b36SDavid Xu itimer_leave(struct itimer *it) 104886857b36SDavid Xu { 104986857b36SDavid Xu 105086857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 105186857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 105286857b36SDavid Xu 105386857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 105486857b36SDavid Xu wakeup(it); 105586857b36SDavid Xu } 105686857b36SDavid Xu 105786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 105861d3a4efSDavid Xu struct ktimer_create_args { 105986857b36SDavid Xu clockid_t clock_id; 106086857b36SDavid Xu struct sigevent * evp; 106161d3a4efSDavid Xu int * timerid; 106286857b36SDavid Xu }; 106386857b36SDavid Xu #endif 106486857b36SDavid Xu int 10658451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 106686857b36SDavid Xu { 106786857b36SDavid Xu struct sigevent *evp1, ev; 106861d3a4efSDavid Xu int id; 106986857b36SDavid Xu int error; 107086857b36SDavid Xu 107186857b36SDavid Xu if (uap->evp != NULL) { 107286857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 107386857b36SDavid Xu if (error != 0) 107486857b36SDavid Xu return (error); 107586857b36SDavid Xu evp1 = &ev; 107686857b36SDavid Xu } else 107786857b36SDavid Xu evp1 = NULL; 107886857b36SDavid Xu 107986857b36SDavid Xu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 108086857b36SDavid Xu 108186857b36SDavid Xu if (error == 0) { 108261d3a4efSDavid Xu error = copyout(&id, uap->timerid, sizeof(int)); 108386857b36SDavid Xu if (error != 0) 108486857b36SDavid Xu kern_timer_delete(td, id); 108586857b36SDavid Xu } 108686857b36SDavid Xu return (error); 108786857b36SDavid Xu } 108886857b36SDavid Xu 108986857b36SDavid Xu static int 109086857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id, 109161d3a4efSDavid Xu struct sigevent *evp, int *timerid, int preset_id) 109286857b36SDavid Xu { 109386857b36SDavid Xu struct proc *p = td->td_proc; 109486857b36SDavid Xu struct itimer *it; 109586857b36SDavid Xu int id; 109686857b36SDavid Xu int error; 109786857b36SDavid Xu 109886857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 109986857b36SDavid Xu return (EINVAL); 110086857b36SDavid Xu 110186857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 110286857b36SDavid Xu return (EINVAL); 110386857b36SDavid Xu 110486857b36SDavid Xu if (evp != NULL) { 110586857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 110656c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 110756c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 110886857b36SDavid Xu return (EINVAL); 110956c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 111056c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 111186857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 111286857b36SDavid Xu return (EINVAL); 111386857b36SDavid Xu } 111486857b36SDavid Xu 111560354683SDavid Xu if (p->p_itimers == NULL) 111686857b36SDavid Xu itimers_alloc(p); 111786857b36SDavid Xu 111886857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 111986857b36SDavid Xu it->it_flags = 0; 112086857b36SDavid Xu it->it_usecount = 0; 112186857b36SDavid Xu it->it_active = 0; 112256c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 112356c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 112486857b36SDavid Xu it->it_overrun = 0; 112586857b36SDavid Xu it->it_overrun_last = 0; 112686857b36SDavid Xu it->it_clockid = clock_id; 112786857b36SDavid Xu it->it_timerid = -1; 112886857b36SDavid Xu it->it_proc = p; 112986857b36SDavid Xu ksiginfo_init(&it->it_ksi); 113086857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 113186857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 113286857b36SDavid Xu if (error != 0) 113386857b36SDavid Xu goto out; 113486857b36SDavid Xu 113586857b36SDavid Xu PROC_LOCK(p); 113686857b36SDavid Xu if (preset_id != -1) { 113786857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 113886857b36SDavid Xu id = preset_id; 113960354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 114086857b36SDavid Xu PROC_UNLOCK(p); 114186857b36SDavid Xu error = 0; 114286857b36SDavid Xu goto out; 114386857b36SDavid Xu } 114486857b36SDavid Xu } else { 114586857b36SDavid Xu /* 114686857b36SDavid Xu * Find a free timer slot, skipping those reserved 114786857b36SDavid Xu * for setitimer(). 114886857b36SDavid Xu */ 114986857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 115060354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 115186857b36SDavid Xu break; 115286857b36SDavid Xu if (id == TIMER_MAX) { 115386857b36SDavid Xu PROC_UNLOCK(p); 115486857b36SDavid Xu error = EAGAIN; 115586857b36SDavid Xu goto out; 115686857b36SDavid Xu } 115786857b36SDavid Xu } 115886857b36SDavid Xu it->it_timerid = id; 115960354683SDavid Xu p->p_itimers->its_timers[id] = it; 116086857b36SDavid Xu if (evp != NULL) 116186857b36SDavid Xu it->it_sigev = *evp; 116286857b36SDavid Xu else { 116386857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 116486857b36SDavid Xu switch (clock_id) { 116586857b36SDavid Xu default: 116686857b36SDavid Xu case CLOCK_REALTIME: 116786857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 116886857b36SDavid Xu break; 116986857b36SDavid Xu case CLOCK_VIRTUAL: 117086857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 117186857b36SDavid Xu break; 117286857b36SDavid Xu case CLOCK_PROF: 117386857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 117486857b36SDavid Xu break; 117586857b36SDavid Xu } 11768f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 117786857b36SDavid Xu } 117886857b36SDavid Xu 117956c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 118056c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 118186857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 118286857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 118386857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 118486857b36SDavid Xu it->it_ksi.ksi_timerid = id; 118586857b36SDavid Xu } 118686857b36SDavid Xu PROC_UNLOCK(p); 118786857b36SDavid Xu *timerid = id; 118886857b36SDavid Xu return (0); 118986857b36SDavid Xu 119086857b36SDavid Xu out: 119186857b36SDavid Xu ITIMER_LOCK(it); 119286857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 119386857b36SDavid Xu ITIMER_UNLOCK(it); 119486857b36SDavid Xu uma_zfree(itimer_zone, it); 119586857b36SDavid Xu return (error); 119686857b36SDavid Xu } 119786857b36SDavid Xu 119886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 119961d3a4efSDavid Xu struct ktimer_delete_args { 120061d3a4efSDavid Xu int timerid; 120186857b36SDavid Xu }; 120286857b36SDavid Xu #endif 120386857b36SDavid Xu int 12048451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 120586857b36SDavid Xu { 120686857b36SDavid Xu return (kern_timer_delete(td, uap->timerid)); 120786857b36SDavid Xu } 120886857b36SDavid Xu 120986857b36SDavid Xu static struct itimer * 1210843b99c6SDavid Xu itimer_find(struct proc *p, int timerid) 121186857b36SDavid Xu { 121286857b36SDavid Xu struct itimer *it; 121386857b36SDavid Xu 121486857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 12153f935cf3SColin Percival if ((p->p_itimers == NULL) || 12163f935cf3SColin Percival (timerid < 0) || (timerid >= TIMER_MAX) || 121760354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 121886857b36SDavid Xu return (NULL); 121986857b36SDavid Xu } 122086857b36SDavid Xu ITIMER_LOCK(it); 1221843b99c6SDavid Xu if ((it->it_flags & ITF_DELETING) != 0) { 122286857b36SDavid Xu ITIMER_UNLOCK(it); 122386857b36SDavid Xu it = NULL; 122486857b36SDavid Xu } 122586857b36SDavid Xu return (it); 122686857b36SDavid Xu } 122786857b36SDavid Xu 122886857b36SDavid Xu static int 122961d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid) 123086857b36SDavid Xu { 123186857b36SDavid Xu struct proc *p = td->td_proc; 123286857b36SDavid Xu struct itimer *it; 123386857b36SDavid Xu 123486857b36SDavid Xu PROC_LOCK(p); 1235843b99c6SDavid Xu it = itimer_find(p, timerid); 123686857b36SDavid Xu if (it == NULL) { 123786857b36SDavid Xu PROC_UNLOCK(p); 123886857b36SDavid Xu return (EINVAL); 123986857b36SDavid Xu } 124086857b36SDavid Xu PROC_UNLOCK(p); 124186857b36SDavid Xu 124286857b36SDavid Xu it->it_flags |= ITF_DELETING; 124386857b36SDavid Xu while (it->it_usecount > 0) { 124486857b36SDavid Xu it->it_flags |= ITF_WANTED; 124586857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 124686857b36SDavid Xu } 124786857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 124886857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 124986857b36SDavid Xu ITIMER_UNLOCK(it); 125086857b36SDavid Xu 125186857b36SDavid Xu PROC_LOCK(p); 125286857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 125386857b36SDavid Xu sigqueue_take(&it->it_ksi); 125460354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 125586857b36SDavid Xu PROC_UNLOCK(p); 125686857b36SDavid Xu uma_zfree(itimer_zone, it); 125786857b36SDavid Xu return (0); 125886857b36SDavid Xu } 125986857b36SDavid Xu 126086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 126161d3a4efSDavid Xu struct ktimer_settime_args { 126261d3a4efSDavid Xu int timerid; 126386857b36SDavid Xu int flags; 126486857b36SDavid Xu const struct itimerspec * value; 126586857b36SDavid Xu struct itimerspec * ovalue; 126686857b36SDavid Xu }; 126786857b36SDavid Xu #endif 126886857b36SDavid Xu int 12698451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 127086857b36SDavid Xu { 127186857b36SDavid Xu struct proc *p = td->td_proc; 127286857b36SDavid Xu struct itimer *it; 127386857b36SDavid Xu struct itimerspec val, oval, *ovalp; 127486857b36SDavid Xu int error; 127586857b36SDavid Xu 127686857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 127786857b36SDavid Xu if (error != 0) 127886857b36SDavid Xu return (error); 127986857b36SDavid Xu 128086857b36SDavid Xu if (uap->ovalue != NULL) 128186857b36SDavid Xu ovalp = &oval; 128286857b36SDavid Xu else 128386857b36SDavid Xu ovalp = NULL; 128486857b36SDavid Xu 128586857b36SDavid Xu PROC_LOCK(p); 128686857b36SDavid Xu if (uap->timerid < 3 || 1287843b99c6SDavid Xu (it = itimer_find(p, uap->timerid)) == NULL) { 128886857b36SDavid Xu PROC_UNLOCK(p); 128986857b36SDavid Xu error = EINVAL; 129086857b36SDavid Xu } else { 129186857b36SDavid Xu PROC_UNLOCK(p); 129286857b36SDavid Xu itimer_enter(it); 129386857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_settime, 129486857b36SDavid Xu (it, uap->flags, &val, ovalp)); 129586857b36SDavid Xu itimer_leave(it); 129686857b36SDavid Xu ITIMER_UNLOCK(it); 129786857b36SDavid Xu } 129886857b36SDavid Xu if (error == 0 && uap->ovalue != NULL) 129986857b36SDavid Xu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 130086857b36SDavid Xu return (error); 130186857b36SDavid Xu } 130286857b36SDavid Xu 130386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 130461d3a4efSDavid Xu struct ktimer_gettime_args { 130561d3a4efSDavid Xu int timerid; 130686857b36SDavid Xu struct itimerspec * value; 130786857b36SDavid Xu }; 130886857b36SDavid Xu #endif 130986857b36SDavid Xu int 13108451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 131186857b36SDavid Xu { 131286857b36SDavid Xu struct proc *p = td->td_proc; 131386857b36SDavid Xu struct itimer *it; 131486857b36SDavid Xu struct itimerspec val; 131586857b36SDavid Xu int error; 131686857b36SDavid Xu 131786857b36SDavid Xu PROC_LOCK(p); 131886857b36SDavid Xu if (uap->timerid < 3 || 1319843b99c6SDavid Xu (it = itimer_find(p, uap->timerid)) == NULL) { 132086857b36SDavid Xu PROC_UNLOCK(p); 132186857b36SDavid Xu error = EINVAL; 132286857b36SDavid Xu } else { 132386857b36SDavid Xu PROC_UNLOCK(p); 132486857b36SDavid Xu itimer_enter(it); 132586857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_gettime, 132686857b36SDavid Xu (it, &val)); 132786857b36SDavid Xu itimer_leave(it); 132886857b36SDavid Xu ITIMER_UNLOCK(it); 132986857b36SDavid Xu } 133086857b36SDavid Xu if (error == 0) 133186857b36SDavid Xu error = copyout(&val, uap->value, sizeof(val)); 133286857b36SDavid Xu return (error); 133386857b36SDavid Xu } 133486857b36SDavid Xu 133586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 133686857b36SDavid Xu struct timer_getoverrun_args { 133761d3a4efSDavid Xu int timerid; 133886857b36SDavid Xu }; 133986857b36SDavid Xu #endif 134086857b36SDavid Xu int 13418451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 134286857b36SDavid Xu { 134386857b36SDavid Xu struct proc *p = td->td_proc; 134486857b36SDavid Xu struct itimer *it; 134586857b36SDavid Xu int error ; 134686857b36SDavid Xu 134786857b36SDavid Xu PROC_LOCK(p); 134886857b36SDavid Xu if (uap->timerid < 3 || 1349843b99c6SDavid Xu (it = itimer_find(p, uap->timerid)) == NULL) { 135086857b36SDavid Xu PROC_UNLOCK(p); 135186857b36SDavid Xu error = EINVAL; 135286857b36SDavid Xu } else { 135386857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 135486857b36SDavid Xu ITIMER_UNLOCK(it); 135556c06c4bSDavid Xu PROC_UNLOCK(p); 135686857b36SDavid Xu error = 0; 135786857b36SDavid Xu } 135886857b36SDavid Xu return (error); 135986857b36SDavid Xu } 136086857b36SDavid Xu 136186857b36SDavid Xu static int 136286857b36SDavid Xu realtimer_create(struct itimer *it) 136386857b36SDavid Xu { 136486857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 136586857b36SDavid Xu return (0); 136686857b36SDavid Xu } 136786857b36SDavid Xu 136886857b36SDavid Xu static int 136986857b36SDavid Xu realtimer_delete(struct itimer *it) 137086857b36SDavid Xu { 137186857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 1372843b99c6SDavid Xu 1373d6b6592eSDavid Xu /* 1374d6b6592eSDavid Xu * clear timer's value and interval to tell realtimer_expire 1375d6b6592eSDavid Xu * to not rearm the timer. 1376d6b6592eSDavid Xu */ 1377d6b6592eSDavid Xu timespecclear(&it->it_time.it_value); 1378d6b6592eSDavid Xu timespecclear(&it->it_time.it_interval); 1379843b99c6SDavid Xu ITIMER_UNLOCK(it); 1380843b99c6SDavid Xu callout_drain(&it->it_callout); 1381843b99c6SDavid Xu ITIMER_LOCK(it); 138286857b36SDavid Xu return (0); 138386857b36SDavid Xu } 138486857b36SDavid Xu 138586857b36SDavid Xu static int 138686857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 138786857b36SDavid Xu { 138856c06c4bSDavid Xu struct timespec cts; 138986857b36SDavid Xu 139086857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 139186857b36SDavid Xu 139256c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 139356c06c4bSDavid Xu *ovalue = it->it_time; 139486857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 139556c06c4bSDavid Xu timespecsub(&ovalue->it_value, &cts); 139686857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 139786857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 139886857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 139986857b36SDavid Xu ovalue->it_value.tv_sec = 0; 140086857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 140186857b36SDavid Xu } 140286857b36SDavid Xu } 140386857b36SDavid Xu return (0); 140486857b36SDavid Xu } 140586857b36SDavid Xu 140686857b36SDavid Xu static int 140786857b36SDavid Xu realtimer_settime(struct itimer *it, int flags, 140886857b36SDavid Xu struct itimerspec *value, struct itimerspec *ovalue) 140986857b36SDavid Xu { 141056c06c4bSDavid Xu struct timespec cts, ts; 141156c06c4bSDavid Xu struct timeval tv; 141256c06c4bSDavid Xu struct itimerspec val; 141386857b36SDavid Xu 141486857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 141586857b36SDavid Xu 141656c06c4bSDavid Xu val = *value; 141756c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 141886857b36SDavid Xu return (EINVAL); 141986857b36SDavid Xu 142056c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 142156c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 142286857b36SDavid Xu return (EINVAL); 142386857b36SDavid Xu } else { 142456c06c4bSDavid Xu timespecclear(&val.it_interval); 142586857b36SDavid Xu } 142686857b36SDavid Xu 142786857b36SDavid Xu if (ovalue != NULL) 142886857b36SDavid Xu realtimer_gettime(it, ovalue); 142986857b36SDavid Xu 143086857b36SDavid Xu it->it_time = val; 143156c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 143256c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 143356c06c4bSDavid Xu ts = val.it_value; 143486857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 143586857b36SDavid Xu /* Convert to absolute time. */ 143656c06c4bSDavid Xu timespecadd(&it->it_time.it_value, &cts); 143786857b36SDavid Xu } else { 143856c06c4bSDavid Xu timespecsub(&ts, &cts); 143986857b36SDavid Xu /* 144056c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 144186857b36SDavid Xu * fix it. 144286857b36SDavid Xu */ 144386857b36SDavid Xu } 144456c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 144556c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 144686857b36SDavid Xu realtimer_expire, it); 144786857b36SDavid Xu } else { 144886857b36SDavid Xu callout_stop(&it->it_callout); 144986857b36SDavid Xu } 145086857b36SDavid Xu 145186857b36SDavid Xu return (0); 145286857b36SDavid Xu } 145386857b36SDavid Xu 145486857b36SDavid Xu static void 145556c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 145686857b36SDavid Xu { 145786857b36SDavid Xu if (id == CLOCK_REALTIME) 145856c06c4bSDavid Xu getnanotime(ts); 145986857b36SDavid Xu else /* CLOCK_MONOTONIC */ 146056c06c4bSDavid Xu getnanouptime(ts); 146156c06c4bSDavid Xu } 146256c06c4bSDavid Xu 146356c06c4bSDavid Xu int 146461d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 146556c06c4bSDavid Xu { 146656c06c4bSDavid Xu struct itimer *it; 146756c06c4bSDavid Xu 146856c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 1469843b99c6SDavid Xu it = itimer_find(p, timerid); 147056c06c4bSDavid Xu if (it != NULL) { 147156c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 147256c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 147356c06c4bSDavid Xu it->it_overrun = 0; 147456c06c4bSDavid Xu ITIMER_UNLOCK(it); 147556c06c4bSDavid Xu return (0); 147656c06c4bSDavid Xu } 147756c06c4bSDavid Xu return (EINVAL); 147856c06c4bSDavid Xu } 147956c06c4bSDavid Xu 148056c06c4bSDavid Xu int 148156c06c4bSDavid Xu itimespecfix(struct timespec *ts) 148256c06c4bSDavid Xu { 148356c06c4bSDavid Xu 148456c06c4bSDavid Xu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 148556c06c4bSDavid Xu return (EINVAL); 148656c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 148756c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 148856c06c4bSDavid Xu return (0); 148986857b36SDavid Xu } 149086857b36SDavid Xu 149186857b36SDavid Xu /* Timeout callback for realtime timer */ 149286857b36SDavid Xu static void 149386857b36SDavid Xu realtimer_expire(void *arg) 149486857b36SDavid Xu { 149556c06c4bSDavid Xu struct timespec cts, ts; 149656c06c4bSDavid Xu struct timeval tv; 149786857b36SDavid Xu struct itimer *it; 149886857b36SDavid Xu 149986857b36SDavid Xu it = (struct itimer *)arg; 150086857b36SDavid Xu 150156c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 150286857b36SDavid Xu /* Only fire if time is reached. */ 150356c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 150456c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 150556c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 150686857b36SDavid Xu &it->it_time.it_interval); 150756c06c4bSDavid Xu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 150877e718f7SDavid Xu if (it->it_overrun < INT_MAX) 150986857b36SDavid Xu it->it_overrun++; 151077e718f7SDavid Xu else 151177e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 151256c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 151386857b36SDavid Xu &it->it_time.it_interval); 151486857b36SDavid Xu } 151586857b36SDavid Xu } else { 151686857b36SDavid Xu /* single shot timer ? */ 151756c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 151886857b36SDavid Xu } 151956c06c4bSDavid Xu if (timespecisset(&it->it_time.it_value)) { 152056c06c4bSDavid Xu ts = it->it_time.it_value; 152156c06c4bSDavid Xu timespecsub(&ts, &cts); 152256c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 152356c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 152486857b36SDavid Xu realtimer_expire, it); 152586857b36SDavid Xu } 1526d6b6592eSDavid Xu itimer_enter(it); 152786857b36SDavid Xu ITIMER_UNLOCK(it); 152886857b36SDavid Xu itimer_fire(it); 152986857b36SDavid Xu ITIMER_LOCK(it); 1530d6b6592eSDavid Xu itimer_leave(it); 153156c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 153256c06c4bSDavid Xu ts = it->it_time.it_value; 153356c06c4bSDavid Xu timespecsub(&ts, &cts); 153456c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 153556c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 153686857b36SDavid Xu it); 153786857b36SDavid Xu } 153886857b36SDavid Xu } 153986857b36SDavid Xu 154086857b36SDavid Xu void 154186857b36SDavid Xu itimer_fire(struct itimer *it) 154286857b36SDavid Xu { 154386857b36SDavid Xu struct proc *p = it->it_proc; 1544cf7d9a8cSDavid Xu struct thread *td; 154586857b36SDavid Xu 154656c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 154756c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1548cf7d9a8cSDavid Xu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 154956c06c4bSDavid Xu ITIMER_LOCK(it); 155056c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 155156c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 155256c06c4bSDavid Xu callout_stop(&it->it_callout); 155356c06c4bSDavid Xu ITIMER_UNLOCK(it); 1554cf7d9a8cSDavid Xu return; 15556d7b314bSDavid Xu } 1556cf7d9a8cSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 1557cf7d9a8cSDavid Xu it->it_ksi.ksi_errno = 0; 1558cf7d9a8cSDavid Xu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1559cf7d9a8cSDavid Xu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 156056c06c4bSDavid Xu } else { 156177e718f7SDavid Xu if (it->it_overrun < INT_MAX) 15626d7b314bSDavid Xu it->it_overrun++; 156377e718f7SDavid Xu else 156477e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 156556c06c4bSDavid Xu } 156686857b36SDavid Xu PROC_UNLOCK(p); 156786857b36SDavid Xu } 156886857b36SDavid Xu } 156986857b36SDavid Xu 157086857b36SDavid Xu static void 157186857b36SDavid Xu itimers_alloc(struct proc *p) 157286857b36SDavid Xu { 157360354683SDavid Xu struct itimers *its; 157460354683SDavid Xu int i; 157586857b36SDavid Xu 157660354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 157786857b36SDavid Xu LIST_INIT(&its->its_virtual); 157886857b36SDavid Xu LIST_INIT(&its->its_prof); 157986857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 158060354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 158160354683SDavid Xu its->its_timers[i] = NULL; 158260354683SDavid Xu PROC_LOCK(p); 158360354683SDavid Xu if (p->p_itimers == NULL) { 158460354683SDavid Xu p->p_itimers = its; 158560354683SDavid Xu PROC_UNLOCK(p); 158660354683SDavid Xu } 158760354683SDavid Xu else { 158860354683SDavid Xu PROC_UNLOCK(p); 158960354683SDavid Xu free(its, M_SUBPROC); 159060354683SDavid Xu } 159186857b36SDavid Xu } 159286857b36SDavid Xu 1593993182e5SAlexander Leidinger static void 1594993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1595993182e5SAlexander Leidinger { 1596993182e5SAlexander Leidinger itimers_event_hook_exit(arg, p); 1597993182e5SAlexander Leidinger } 1598993182e5SAlexander Leidinger 159986857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 1600d26b1a1fSDavid Xu static void 1601993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p) 160286857b36SDavid Xu { 160386857b36SDavid Xu struct itimers *its; 160486857b36SDavid Xu struct itimer *it; 16053e70c6f0SDavid Xu int event = (int)(intptr_t)arg; 160686857b36SDavid Xu int i; 160786857b36SDavid Xu 160860354683SDavid Xu if (p->p_itimers != NULL) { 160960354683SDavid Xu its = p->p_itimers; 161086857b36SDavid Xu for (i = 0; i < MAX_CLOCKS; ++i) { 161186857b36SDavid Xu if (posix_clocks[i].event_hook != NULL) 161286857b36SDavid Xu CLOCK_CALL(i, event_hook, (p, i, event)); 161386857b36SDavid Xu } 161486857b36SDavid Xu /* 161586857b36SDavid Xu * According to susv3, XSI interval timers should be inherited 161686857b36SDavid Xu * by new image. 161786857b36SDavid Xu */ 161886857b36SDavid Xu if (event == ITIMER_EV_EXEC) 161986857b36SDavid Xu i = 3; 162086857b36SDavid Xu else if (event == ITIMER_EV_EXIT) 162186857b36SDavid Xu i = 0; 162286857b36SDavid Xu else 162386857b36SDavid Xu panic("unhandled event"); 162486857b36SDavid Xu for (; i < TIMER_MAX; ++i) { 1625843b99c6SDavid Xu if ((it = its->its_timers[i]) != NULL) 1626843b99c6SDavid Xu kern_timer_delete(curthread, i); 162786857b36SDavid Xu } 162886857b36SDavid Xu if (its->its_timers[0] == NULL && 162986857b36SDavid Xu its->its_timers[1] == NULL && 163086857b36SDavid Xu its->its_timers[2] == NULL) { 163160354683SDavid Xu free(its, M_SUBPROC); 163260354683SDavid Xu p->p_itimers = NULL; 163386857b36SDavid Xu } 163486857b36SDavid Xu } 163586857b36SDavid Xu } 1636