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 10286857b36SDavid Xu int register_posix_clock(int, struct kclock *); 10386857b36SDavid Xu void itimer_fire(struct itimer *it); 10456c06c4bSDavid Xu int itimespecfix(struct timespec *ts); 10586857b36SDavid Xu 10686857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist) \ 10786857b36SDavid Xu ((*posix_clocks[clock].call) arglist) 10886857b36SDavid Xu 10986857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 11086857b36SDavid Xu 11186857b36SDavid Xu 11294c8fcd8SPeter Wemm static int 11391afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 11494c8fcd8SPeter Wemm { 115fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 116c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 1177ec73f64SPoul-Henning Kamp struct timespec ts; 11894c8fcd8SPeter Wemm int s; 11994c8fcd8SPeter Wemm 120708e7684SPeter Wemm s = splclock(); 1219c8fff87SBruce Evans microtime(&tv1); 12200af9731SPoul-Henning Kamp delta = *tv; 12300af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 12494c8fcd8SPeter Wemm 12594c8fcd8SPeter Wemm /* 1269c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 127fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 128fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 129fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 130fcae3aa6SNick Sayer * back to the past. 131c0bd94a7SNick Sayer * 132c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 133c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 134c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 13594c8fcd8SPeter Wemm */ 1367edfb592SJohn Baldwin if (securelevel_gt(td->td_ucred, 1) != 0) { 137fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1383f92429aSMatt Jacob /* 139c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1403f92429aSMatt Jacob */ 141fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 142fcae3aa6SNick Sayer maxtime = tv1; 143fcae3aa6SNick Sayer tv2 = *tv; 144fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 145fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1463f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 147fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 148fcae3aa6SNick Sayer } 1493f92429aSMatt Jacob } else { 150c0bd94a7SNick Sayer if (tv1.tv_sec == laststep.tv_sec) { 151c0bd94a7SNick Sayer splx(s); 152c0bd94a7SNick Sayer return (EPERM); 153c0bd94a7SNick Sayer } 154c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 155c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 156c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 157c0bd94a7SNick Sayer } 158c0bd94a7SNick Sayer laststep = *tv; 159fcae3aa6SNick Sayer } 1609c8fff87SBruce Evans } 1619c8fff87SBruce Evans 1627ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1637ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 1647edfb592SJohn Baldwin mtx_lock(&Giant); 16591266b96SPoul-Henning Kamp tc_setclock(&ts); 16694c8fcd8SPeter Wemm resettodr(); 1677edfb592SJohn Baldwin mtx_unlock(&Giant); 16894c8fcd8SPeter Wemm return (0); 16994c8fcd8SPeter Wemm } 17094c8fcd8SPeter Wemm 17194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 172d65f1abcSDavid Xu struct clock_getcpuclockid2_args { 173d65f1abcSDavid Xu id_t id; 174d65f1abcSDavid Xu int which, 175d65f1abcSDavid Xu clockid_t *clock_id; 176d65f1abcSDavid Xu }; 177d65f1abcSDavid Xu #endif 178d65f1abcSDavid Xu /* ARGSUSED */ 179d65f1abcSDavid Xu int 180d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap) 181d65f1abcSDavid Xu { 182d65f1abcSDavid Xu clockid_t clk_id; 183d31e4b3aSKonstantin Belousov int error; 184d31e4b3aSKonstantin Belousov 185d31e4b3aSKonstantin Belousov error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id); 186d31e4b3aSKonstantin Belousov if (error == 0) 187d31e4b3aSKonstantin Belousov error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 188d31e4b3aSKonstantin Belousov return (error); 189d31e4b3aSKonstantin Belousov } 190d31e4b3aSKonstantin Belousov 191d31e4b3aSKonstantin Belousov int 192d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which, 193d31e4b3aSKonstantin Belousov clockid_t *clk_id) 194d31e4b3aSKonstantin Belousov { 195d65f1abcSDavid Xu struct proc *p; 196d65f1abcSDavid Xu pid_t pid; 197d65f1abcSDavid Xu lwpid_t tid; 198d65f1abcSDavid Xu int error; 199d65f1abcSDavid Xu 200d31e4b3aSKonstantin Belousov switch (which) { 201d65f1abcSDavid Xu case CPUCLOCK_WHICH_PID: 202d31e4b3aSKonstantin Belousov if (id != 0) { 203d31e4b3aSKonstantin Belousov p = pfind(id); 204d65f1abcSDavid Xu if (p == NULL) 205d65f1abcSDavid Xu return (ESRCH); 206d65f1abcSDavid Xu error = p_cansee(td, p); 207d65f1abcSDavid Xu PROC_UNLOCK(p); 208d31e4b3aSKonstantin Belousov if (error != 0) 209d65f1abcSDavid Xu return (error); 210d31e4b3aSKonstantin Belousov pid = id; 211d65f1abcSDavid Xu } else { 212d65f1abcSDavid Xu pid = td->td_proc->p_pid; 213d65f1abcSDavid Xu } 214d31e4b3aSKonstantin Belousov *clk_id = MAKE_PROCESS_CPUCLOCK(pid); 215d31e4b3aSKonstantin Belousov return (0); 216d65f1abcSDavid Xu case CPUCLOCK_WHICH_TID: 217d31e4b3aSKonstantin Belousov tid = id == 0 ? td->td_tid : id; 218d31e4b3aSKonstantin Belousov *clk_id = MAKE_THREAD_CPUCLOCK(tid); 219d31e4b3aSKonstantin Belousov return (0); 220d65f1abcSDavid Xu default: 221d65f1abcSDavid Xu return (EINVAL); 222d65f1abcSDavid Xu } 223d65f1abcSDavid Xu } 224d65f1abcSDavid Xu 225d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_ 22694c8fcd8SPeter Wemm struct clock_gettime_args { 22794c8fcd8SPeter Wemm clockid_t clock_id; 22894c8fcd8SPeter Wemm struct timespec *tp; 22994c8fcd8SPeter Wemm }; 23094c8fcd8SPeter Wemm #endif 23194c8fcd8SPeter Wemm /* ARGSUSED */ 23294c8fcd8SPeter Wemm int 2338451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) 23494c8fcd8SPeter Wemm { 23594c8fcd8SPeter Wemm struct timespec ats; 236f0b479cdSPaul Saab int error; 237f0b479cdSPaul Saab 238f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 239f0b479cdSPaul Saab if (error == 0) 240f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 241f0b479cdSPaul Saab 242f0b479cdSPaul Saab return (error); 243f0b479cdSPaul Saab } 244f0b479cdSPaul Saab 245d65f1abcSDavid Xu static inline void 246d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats) 247d65f1abcSDavid Xu { 248d65f1abcSDavid Xu runtime = cputick2usec(runtime); 249d65f1abcSDavid Xu ats->tv_sec = runtime / 1000000; 250d65f1abcSDavid Xu ats->tv_nsec = runtime % 1000000 * 1000; 251d65f1abcSDavid Xu } 252d65f1abcSDavid Xu 253d65f1abcSDavid Xu static void 254d65f1abcSDavid Xu get_thread_cputime(struct thread *targettd, struct timespec *ats) 255d65f1abcSDavid Xu { 256d65f1abcSDavid Xu uint64_t runtime, curtime, switchtime; 257d65f1abcSDavid Xu 258d65f1abcSDavid Xu if (targettd == NULL) { /* current thread */ 259d65f1abcSDavid Xu critical_enter(); 260d65f1abcSDavid Xu switchtime = PCPU_GET(switchtime); 261d65f1abcSDavid Xu curtime = cpu_ticks(); 262d65f1abcSDavid Xu runtime = curthread->td_runtime; 263d65f1abcSDavid Xu critical_exit(); 264d65f1abcSDavid Xu runtime += curtime - switchtime; 265d65f1abcSDavid Xu } else { 266d65f1abcSDavid Xu thread_lock(targettd); 267d65f1abcSDavid Xu runtime = targettd->td_runtime; 268d65f1abcSDavid Xu thread_unlock(targettd); 269d65f1abcSDavid Xu } 270d65f1abcSDavid Xu cputick2timespec(runtime, ats); 271d65f1abcSDavid Xu } 272d65f1abcSDavid Xu 273d65f1abcSDavid Xu static void 274d65f1abcSDavid Xu get_process_cputime(struct proc *targetp, struct timespec *ats) 275d65f1abcSDavid Xu { 276d65f1abcSDavid Xu uint64_t runtime; 277d65f1abcSDavid Xu struct rusage ru; 278d65f1abcSDavid Xu 279d65f1abcSDavid Xu PROC_SLOCK(targetp); 280d65f1abcSDavid Xu rufetch(targetp, &ru); 281d65f1abcSDavid Xu runtime = targetp->p_rux.rux_runtime; 282d65f1abcSDavid Xu PROC_SUNLOCK(targetp); 283d65f1abcSDavid Xu cputick2timespec(runtime, ats); 284d65f1abcSDavid Xu } 285d65f1abcSDavid Xu 286d65f1abcSDavid Xu static int 287d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) 288d65f1abcSDavid Xu { 289d65f1abcSDavid Xu struct proc *p, *p2; 290d65f1abcSDavid Xu struct thread *td2; 291d65f1abcSDavid Xu lwpid_t tid; 292d65f1abcSDavid Xu pid_t pid; 293d65f1abcSDavid Xu int error; 294d65f1abcSDavid Xu 295d65f1abcSDavid Xu p = td->td_proc; 296d65f1abcSDavid Xu if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { 297d65f1abcSDavid Xu tid = clock_id & CPUCLOCK_ID_MASK; 298d65f1abcSDavid Xu td2 = tdfind(tid, p->p_pid); 299d65f1abcSDavid Xu if (td2 == NULL) 300d65f1abcSDavid Xu return (EINVAL); 301d65f1abcSDavid Xu get_thread_cputime(td2, ats); 302d65f1abcSDavid Xu PROC_UNLOCK(td2->td_proc); 303d65f1abcSDavid Xu } else { 304d65f1abcSDavid Xu pid = clock_id & CPUCLOCK_ID_MASK; 305c7c536c7SKonstantin Belousov error = pget(pid, PGET_CANSEE, &p2); 306c7c536c7SKonstantin Belousov if (error != 0) 307d65f1abcSDavid Xu return (EINVAL); 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 4800dbf17e6SAlexander 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; 5060dbf17e6SAlexander Motin error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp", 5070dbf17e6SAlexander 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 { 777d10a1df8SAlexander Motin if (aitv->it_interval.tv_sec == 0 && 778d10a1df8SAlexander Motin aitv->it_interval.tv_usec != 0 && 779d10a1df8SAlexander Motin aitv->it_interval.tv_usec < tick) 780d10a1df8SAlexander Motin aitv->it_interval.tv_usec = tick; 781d10a1df8SAlexander Motin if (aitv->it_value.tv_sec == 0 && 782d10a1df8SAlexander Motin aitv->it_value.tv_usec != 0 && 783d10a1df8SAlexander Motin aitv->it_value.tv_usec < tick) 784d10a1df8SAlexander Motin aitv->it_value.tv_usec = tick; 785982d11f8SJeff Roberson PROC_SLOCK(p); 786cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 787cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 788982d11f8SJeff Roberson PROC_SUNLOCK(p); 78996d7f8efSTim J. Robbins } 79096d7f8efSTim J. Robbins return (0); 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes 793df8bae1dSRodney W. Grimes /* 794df8bae1dSRodney W. Grimes * Real interval timer expired: 795df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 796df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 797df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 798df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 799df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 800c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 8019207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 8029207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 8039207f00aSBruce Evans * interrupt even when we're delayed. 804df8bae1dSRodney W. Grimes */ 805df8bae1dSRodney W. Grimes void 80691afe087SPoul-Henning Kamp realitexpire(void *arg) 807df8bae1dSRodney W. Grimes { 80891afe087SPoul-Henning Kamp struct proc *p; 809b5ea3779SAlexander Motin struct timeval ctv; 810b5ea3779SAlexander Motin sbintime_t isbt; 811df8bae1dSRodney W. Grimes 812df8bae1dSRodney W. Grimes p = (struct proc *)arg; 8138451d0ddSKip Macy kern_psignal(p, SIGALRM); 8144cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 8154cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 8165499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 8175499ea01SJohn Baldwin wakeup(&p->p_itcallout); 818df8bae1dSRodney W. Grimes return; 819df8bae1dSRodney W. Grimes } 820b5ea3779SAlexander Motin isbt = tvtosbt(p->p_realtimer.it_interval); 821b5ea3779SAlexander Motin if (isbt >= sbt_timethreshold) 822b5ea3779SAlexander Motin getmicrouptime(&ctv); 823b5ea3779SAlexander Motin else 824b5ea3779SAlexander Motin microuptime(&ctv); 825b5ea3779SAlexander Motin do { 826df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 827df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 828b5ea3779SAlexander Motin } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=)); 829b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value), 830b5ea3779SAlexander Motin isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE); 831df8bae1dSRodney W. Grimes } 832df8bae1dSRodney W. Grimes 833df8bae1dSRodney W. Grimes /* 834df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 835df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 836df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 837df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 838df8bae1dSRodney W. Grimes */ 83926f9a767SRodney W. Grimes int 84091afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 841df8bae1dSRodney W. Grimes { 842df8bae1dSRodney W. Grimes 84386857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 844df8bae1dSRodney W. Grimes return (EINVAL); 845b5ea3779SAlexander Motin if (tv->tv_sec == 0 && tv->tv_usec != 0 && 846b5ea3779SAlexander Motin tv->tv_usec < (u_int)tick / 16) 847b5ea3779SAlexander Motin tv->tv_usec = (u_int)tick / 16; 848df8bae1dSRodney W. Grimes return (0); 849df8bae1dSRodney W. Grimes } 850df8bae1dSRodney W. Grimes 851df8bae1dSRodney W. Grimes /* 852df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 853df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 854df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 855df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 856df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 857df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 858df8bae1dSRodney W. Grimes * that it is called in a context where the timers 859df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 860df8bae1dSRodney W. Grimes */ 86126f9a767SRodney W. Grimes int 86291afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 863df8bae1dSRodney W. Grimes { 864df8bae1dSRodney W. Grimes 865df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 866df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 867df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 868df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 869df8bae1dSRodney W. Grimes goto expire; 870df8bae1dSRodney W. Grimes } 871df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 872df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 873df8bae1dSRodney W. Grimes } 874df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 875df8bae1dSRodney W. Grimes usec = 0; 8764cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 877df8bae1dSRodney W. Grimes return (1); 878df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 879df8bae1dSRodney W. Grimes expire: 8804cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 881df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 882df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 883df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 884df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 885df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 886df8bae1dSRodney W. Grimes } 887df8bae1dSRodney W. Grimes } else 888df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 889df8bae1dSRodney W. Grimes return (0); 890df8bae1dSRodney W. Grimes } 891df8bae1dSRodney W. Grimes 892df8bae1dSRodney W. Grimes /* 893df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 894df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 895df8bae1dSRodney W. Grimes * results which are before the beginning, 896df8bae1dSRodney W. Grimes * it just gets very confused in this case. 897df8bae1dSRodney W. Grimes * Caveat emptor. 898df8bae1dSRodney W. Grimes */ 89926f9a767SRodney W. Grimes void 9006ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 901df8bae1dSRodney W. Grimes { 902df8bae1dSRodney W. Grimes 903df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 904df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 905df8bae1dSRodney W. Grimes timevalfix(t1); 906df8bae1dSRodney W. Grimes } 907df8bae1dSRodney W. Grimes 90826f9a767SRodney W. Grimes void 9096ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 910df8bae1dSRodney W. Grimes { 911df8bae1dSRodney W. Grimes 912df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 913df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 914df8bae1dSRodney W. Grimes timevalfix(t1); 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes 91787b6de2bSPoul-Henning Kamp static void 91891afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 919df8bae1dSRodney W. Grimes { 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 922df8bae1dSRodney W. Grimes t1->tv_sec--; 923df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 924df8bae1dSRodney W. Grimes } 925df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 926df8bae1dSRodney W. Grimes t1->tv_sec++; 927df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 928df8bae1dSRodney W. Grimes } 929df8bae1dSRodney W. Grimes } 93091974ce1SSam Leffler 93191974ce1SSam Leffler /* 932addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 93391974ce1SSam Leffler */ 93491974ce1SSam Leffler int 93591974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 93691974ce1SSam Leffler { 93791974ce1SSam Leffler struct timeval tv, delta; 93891974ce1SSam Leffler int rv = 0; 93991974ce1SSam Leffler 940addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 941addea9d4SSam Leffler delta = tv; 942addea9d4SSam Leffler timevalsub(&delta, lasttime); 94391974ce1SSam Leffler 94491974ce1SSam Leffler /* 94591974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 94691974ce1SSam Leffler * even if interval is huge. 94791974ce1SSam Leffler */ 94891974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 94991974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 95091974ce1SSam Leffler *lasttime = tv; 95191974ce1SSam Leffler rv = 1; 95291974ce1SSam Leffler } 95391974ce1SSam Leffler 95491974ce1SSam Leffler return (rv); 95591974ce1SSam Leffler } 95691974ce1SSam Leffler 95791974ce1SSam Leffler /* 95891974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 959addea9d4SSam Leffler * 960addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 961addea9d4SSam Leffler * should drop a packet because of the rate limitation). 962addea9d4SSam Leffler * 963893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 964893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 965893bec80SSam Leffler * limiting. 966893bec80SSam Leffler * 967addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 968addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 969addea9d4SSam Leffler * clock ticks for minimal overhead. 97091974ce1SSam Leffler */ 97191974ce1SSam Leffler int 97291974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 97391974ce1SSam Leffler { 974addea9d4SSam Leffler int now; 97591974ce1SSam Leffler 97691974ce1SSam Leffler /* 977addea9d4SSam Leffler * Reset the last time and counter if this is the first call 978addea9d4SSam Leffler * or more than a second has passed since the last update of 979addea9d4SSam Leffler * lasttime. 98091974ce1SSam Leffler */ 981addea9d4SSam Leffler now = ticks; 982addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 983addea9d4SSam Leffler lasttime->tv_sec = now; 984addea9d4SSam Leffler *curpps = 1; 985893bec80SSam Leffler return (maxpps != 0); 986addea9d4SSam Leffler } else { 987addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 988addea9d4SSam Leffler return (maxpps < 0 || *curpps < maxpps); 989addea9d4SSam Leffler } 99091974ce1SSam Leffler } 99186857b36SDavid Xu 99286857b36SDavid Xu static void 99386857b36SDavid Xu itimer_start(void) 99486857b36SDavid Xu { 99586857b36SDavid Xu struct kclock rt_clock = { 99686857b36SDavid Xu .timer_create = realtimer_create, 99786857b36SDavid Xu .timer_delete = realtimer_delete, 99886857b36SDavid Xu .timer_settime = realtimer_settime, 99986857b36SDavid Xu .timer_gettime = realtimer_gettime, 1000843b99c6SDavid Xu .event_hook = NULL 100186857b36SDavid Xu }; 100286857b36SDavid Xu 100386857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 100486857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 100586857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 100686857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 100777e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 100877e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 100977e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 1010993182e5SAlexander Leidinger EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 1011d26b1a1fSDavid Xu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 1012993182e5SAlexander Leidinger EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 1013d26b1a1fSDavid Xu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 101486857b36SDavid Xu } 101586857b36SDavid Xu 101686857b36SDavid Xu int 101786857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk) 101886857b36SDavid Xu { 101986857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 102086857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 102186857b36SDavid Xu return (0); 102286857b36SDavid Xu } 102386857b36SDavid Xu posix_clocks[clockid] = *clk; 102486857b36SDavid Xu return (1); 102586857b36SDavid Xu } 102686857b36SDavid Xu 102786857b36SDavid Xu static int 102886857b36SDavid Xu itimer_init(void *mem, int size, int flags) 102986857b36SDavid Xu { 103086857b36SDavid Xu struct itimer *it; 103186857b36SDavid Xu 103286857b36SDavid Xu it = (struct itimer *)mem; 103386857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 103486857b36SDavid Xu return (0); 103586857b36SDavid Xu } 103686857b36SDavid Xu 103786857b36SDavid Xu static void 103886857b36SDavid Xu itimer_fini(void *mem, int size) 103986857b36SDavid Xu { 104086857b36SDavid Xu struct itimer *it; 104186857b36SDavid Xu 104286857b36SDavid Xu it = (struct itimer *)mem; 104386857b36SDavid Xu mtx_destroy(&it->it_mtx); 104486857b36SDavid Xu } 104586857b36SDavid Xu 104686857b36SDavid Xu static void 104786857b36SDavid Xu itimer_enter(struct itimer *it) 104886857b36SDavid Xu { 104986857b36SDavid Xu 105086857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 105186857b36SDavid Xu it->it_usecount++; 105286857b36SDavid Xu } 105386857b36SDavid Xu 105486857b36SDavid Xu static void 105586857b36SDavid Xu itimer_leave(struct itimer *it) 105686857b36SDavid Xu { 105786857b36SDavid Xu 105886857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 105986857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 106086857b36SDavid Xu 106186857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 106286857b36SDavid Xu wakeup(it); 106386857b36SDavid Xu } 106486857b36SDavid Xu 106586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 106661d3a4efSDavid Xu struct ktimer_create_args { 106786857b36SDavid Xu clockid_t clock_id; 106886857b36SDavid Xu struct sigevent * evp; 106961d3a4efSDavid Xu int * timerid; 107086857b36SDavid Xu }; 107186857b36SDavid Xu #endif 107286857b36SDavid Xu int 10738451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 107486857b36SDavid Xu { 1075643ee871SKonstantin Belousov struct sigevent *evp, ev; 107661d3a4efSDavid Xu int id; 107786857b36SDavid Xu int error; 107886857b36SDavid Xu 1079643ee871SKonstantin Belousov if (uap->evp == NULL) { 1080643ee871SKonstantin Belousov evp = NULL; 1081643ee871SKonstantin Belousov } else { 108286857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 108386857b36SDavid Xu if (error != 0) 108486857b36SDavid Xu return (error); 1085643ee871SKonstantin Belousov evp = &ev; 1086643ee871SKonstantin Belousov } 1087643ee871SKonstantin Belousov error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1); 108886857b36SDavid Xu if (error == 0) { 108961d3a4efSDavid Xu error = copyout(&id, uap->timerid, sizeof(int)); 109086857b36SDavid Xu if (error != 0) 1091643ee871SKonstantin Belousov kern_ktimer_delete(td, id); 109286857b36SDavid Xu } 109386857b36SDavid Xu return (error); 109486857b36SDavid Xu } 109586857b36SDavid Xu 1096643ee871SKonstantin Belousov int 1097643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp, 1098643ee871SKonstantin Belousov int *timerid, int preset_id) 109986857b36SDavid Xu { 110086857b36SDavid Xu struct proc *p = td->td_proc; 110186857b36SDavid Xu struct itimer *it; 110286857b36SDavid Xu int id; 110386857b36SDavid Xu int error; 110486857b36SDavid Xu 110586857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 110686857b36SDavid Xu return (EINVAL); 110786857b36SDavid Xu 110886857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 110986857b36SDavid Xu return (EINVAL); 111086857b36SDavid Xu 111186857b36SDavid Xu if (evp != NULL) { 111286857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 111356c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 111456c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 111586857b36SDavid Xu return (EINVAL); 111656c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 111756c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 111886857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 111986857b36SDavid Xu return (EINVAL); 112086857b36SDavid Xu } 112186857b36SDavid Xu 112260354683SDavid Xu if (p->p_itimers == NULL) 112386857b36SDavid Xu itimers_alloc(p); 112486857b36SDavid Xu 112586857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 112686857b36SDavid Xu it->it_flags = 0; 112786857b36SDavid Xu it->it_usecount = 0; 112886857b36SDavid Xu it->it_active = 0; 112956c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 113056c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 113186857b36SDavid Xu it->it_overrun = 0; 113286857b36SDavid Xu it->it_overrun_last = 0; 113386857b36SDavid Xu it->it_clockid = clock_id; 113486857b36SDavid Xu it->it_timerid = -1; 113586857b36SDavid Xu it->it_proc = p; 113686857b36SDavid Xu ksiginfo_init(&it->it_ksi); 113786857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 113886857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 113986857b36SDavid Xu if (error != 0) 114086857b36SDavid Xu goto out; 114186857b36SDavid Xu 114286857b36SDavid Xu PROC_LOCK(p); 114386857b36SDavid Xu if (preset_id != -1) { 114486857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 114586857b36SDavid Xu id = preset_id; 114660354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 114786857b36SDavid Xu PROC_UNLOCK(p); 114886857b36SDavid Xu error = 0; 114986857b36SDavid Xu goto out; 115086857b36SDavid Xu } 115186857b36SDavid Xu } else { 115286857b36SDavid Xu /* 115386857b36SDavid Xu * Find a free timer slot, skipping those reserved 115486857b36SDavid Xu * for setitimer(). 115586857b36SDavid Xu */ 115686857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 115760354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 115886857b36SDavid Xu break; 115986857b36SDavid Xu if (id == TIMER_MAX) { 116086857b36SDavid Xu PROC_UNLOCK(p); 116186857b36SDavid Xu error = EAGAIN; 116286857b36SDavid Xu goto out; 116386857b36SDavid Xu } 116486857b36SDavid Xu } 116586857b36SDavid Xu it->it_timerid = id; 116660354683SDavid Xu p->p_itimers->its_timers[id] = it; 116786857b36SDavid Xu if (evp != NULL) 116886857b36SDavid Xu it->it_sigev = *evp; 116986857b36SDavid Xu else { 117086857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 117186857b36SDavid Xu switch (clock_id) { 117286857b36SDavid Xu default: 117386857b36SDavid Xu case CLOCK_REALTIME: 117486857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 117586857b36SDavid Xu break; 117686857b36SDavid Xu case CLOCK_VIRTUAL: 117786857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 117886857b36SDavid Xu break; 117986857b36SDavid Xu case CLOCK_PROF: 118086857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 118186857b36SDavid Xu break; 118286857b36SDavid Xu } 11838f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 118486857b36SDavid Xu } 118586857b36SDavid Xu 118656c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 118756c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 118886857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 118986857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 119086857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 119186857b36SDavid Xu it->it_ksi.ksi_timerid = id; 119286857b36SDavid Xu } 119386857b36SDavid Xu PROC_UNLOCK(p); 119486857b36SDavid Xu *timerid = id; 119586857b36SDavid Xu return (0); 119686857b36SDavid Xu 119786857b36SDavid Xu out: 119886857b36SDavid Xu ITIMER_LOCK(it); 119986857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 120086857b36SDavid Xu ITIMER_UNLOCK(it); 120186857b36SDavid Xu uma_zfree(itimer_zone, it); 120286857b36SDavid Xu return (error); 120386857b36SDavid Xu } 120486857b36SDavid Xu 120586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 120661d3a4efSDavid Xu struct ktimer_delete_args { 120761d3a4efSDavid Xu int timerid; 120886857b36SDavid Xu }; 120986857b36SDavid Xu #endif 121086857b36SDavid Xu int 12118451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 121286857b36SDavid Xu { 1213643ee871SKonstantin Belousov 1214643ee871SKonstantin Belousov return (kern_ktimer_delete(td, uap->timerid)); 121586857b36SDavid Xu } 121686857b36SDavid Xu 121786857b36SDavid Xu static struct itimer * 1218843b99c6SDavid Xu itimer_find(struct proc *p, int timerid) 121986857b36SDavid Xu { 122086857b36SDavid Xu struct itimer *it; 122186857b36SDavid Xu 122286857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 12233f935cf3SColin Percival if ((p->p_itimers == NULL) || 12243f935cf3SColin Percival (timerid < 0) || (timerid >= TIMER_MAX) || 122560354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 122686857b36SDavid Xu return (NULL); 122786857b36SDavid Xu } 122886857b36SDavid Xu ITIMER_LOCK(it); 1229843b99c6SDavid Xu if ((it->it_flags & ITF_DELETING) != 0) { 123086857b36SDavid Xu ITIMER_UNLOCK(it); 123186857b36SDavid Xu it = NULL; 123286857b36SDavid Xu } 123386857b36SDavid Xu return (it); 123486857b36SDavid Xu } 123586857b36SDavid Xu 1236643ee871SKonstantin Belousov int 1237643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid) 123886857b36SDavid Xu { 123986857b36SDavid Xu struct proc *p = td->td_proc; 124086857b36SDavid Xu struct itimer *it; 124186857b36SDavid Xu 124286857b36SDavid Xu PROC_LOCK(p); 1243843b99c6SDavid Xu it = itimer_find(p, timerid); 124486857b36SDavid Xu if (it == NULL) { 124586857b36SDavid Xu PROC_UNLOCK(p); 124686857b36SDavid Xu return (EINVAL); 124786857b36SDavid Xu } 124886857b36SDavid Xu PROC_UNLOCK(p); 124986857b36SDavid Xu 125086857b36SDavid Xu it->it_flags |= ITF_DELETING; 125186857b36SDavid Xu while (it->it_usecount > 0) { 125286857b36SDavid Xu it->it_flags |= ITF_WANTED; 125386857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 125486857b36SDavid Xu } 125586857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 125686857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 125786857b36SDavid Xu ITIMER_UNLOCK(it); 125886857b36SDavid Xu 125986857b36SDavid Xu PROC_LOCK(p); 126086857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 126186857b36SDavid Xu sigqueue_take(&it->it_ksi); 126260354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 126386857b36SDavid Xu PROC_UNLOCK(p); 126486857b36SDavid Xu uma_zfree(itimer_zone, it); 126586857b36SDavid Xu return (0); 126686857b36SDavid Xu } 126786857b36SDavid Xu 126886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 126961d3a4efSDavid Xu struct ktimer_settime_args { 127061d3a4efSDavid Xu int timerid; 127186857b36SDavid Xu int flags; 127286857b36SDavid Xu const struct itimerspec * value; 127386857b36SDavid Xu struct itimerspec * ovalue; 127486857b36SDavid Xu }; 127586857b36SDavid Xu #endif 127686857b36SDavid Xu int 12778451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 127886857b36SDavid Xu { 127986857b36SDavid Xu struct itimerspec val, oval, *ovalp; 128086857b36SDavid Xu int error; 128186857b36SDavid Xu 128286857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 128386857b36SDavid Xu if (error != 0) 128486857b36SDavid Xu return (error); 1285643ee871SKonstantin Belousov ovalp = uap->ovalue != NULL ? &oval : NULL; 1286643ee871SKonstantin Belousov error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp); 1287643ee871SKonstantin Belousov if (error == 0 && uap->ovalue != NULL) 1288643ee871SKonstantin Belousov error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1289643ee871SKonstantin Belousov return (error); 1290643ee871SKonstantin Belousov } 129186857b36SDavid Xu 1292643ee871SKonstantin Belousov int 1293643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags, 1294643ee871SKonstantin Belousov struct itimerspec *val, struct itimerspec *oval) 1295643ee871SKonstantin Belousov { 1296643ee871SKonstantin Belousov struct proc *p; 1297643ee871SKonstantin Belousov struct itimer *it; 1298643ee871SKonstantin Belousov int error; 129986857b36SDavid Xu 1300643ee871SKonstantin Belousov p = td->td_proc; 130186857b36SDavid Xu PROC_LOCK(p); 1302643ee871SKonstantin Belousov if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) { 130386857b36SDavid Xu PROC_UNLOCK(p); 130486857b36SDavid Xu error = EINVAL; 130586857b36SDavid Xu } else { 130686857b36SDavid Xu PROC_UNLOCK(p); 130786857b36SDavid Xu itimer_enter(it); 1308643ee871SKonstantin Belousov error = CLOCK_CALL(it->it_clockid, timer_settime, (it, 1309643ee871SKonstantin Belousov flags, val, oval)); 131086857b36SDavid Xu itimer_leave(it); 131186857b36SDavid Xu ITIMER_UNLOCK(it); 131286857b36SDavid Xu } 131386857b36SDavid Xu return (error); 131486857b36SDavid Xu } 131586857b36SDavid Xu 131686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 131761d3a4efSDavid Xu struct ktimer_gettime_args { 131861d3a4efSDavid Xu int timerid; 131986857b36SDavid Xu struct itimerspec * value; 132086857b36SDavid Xu }; 132186857b36SDavid Xu #endif 132286857b36SDavid Xu int 13238451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 132486857b36SDavid Xu { 132586857b36SDavid Xu struct itimerspec val; 132686857b36SDavid Xu int error; 132786857b36SDavid Xu 1328643ee871SKonstantin Belousov error = kern_ktimer_gettime(td, uap->timerid, &val); 1329643ee871SKonstantin Belousov if (error == 0) 1330643ee871SKonstantin Belousov error = copyout(&val, uap->value, sizeof(val)); 1331643ee871SKonstantin Belousov return (error); 1332643ee871SKonstantin Belousov } 1333643ee871SKonstantin Belousov 1334643ee871SKonstantin Belousov int 1335643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val) 1336643ee871SKonstantin Belousov { 1337643ee871SKonstantin Belousov struct proc *p; 1338643ee871SKonstantin Belousov struct itimer *it; 1339643ee871SKonstantin Belousov int error; 1340643ee871SKonstantin Belousov 1341643ee871SKonstantin Belousov p = td->td_proc; 134286857b36SDavid Xu PROC_LOCK(p); 1343643ee871SKonstantin Belousov if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) { 134486857b36SDavid Xu PROC_UNLOCK(p); 134586857b36SDavid Xu error = EINVAL; 134686857b36SDavid Xu } else { 134786857b36SDavid Xu PROC_UNLOCK(p); 134886857b36SDavid Xu itimer_enter(it); 1349643ee871SKonstantin Belousov error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val)); 135086857b36SDavid Xu itimer_leave(it); 135186857b36SDavid Xu ITIMER_UNLOCK(it); 135286857b36SDavid Xu } 135386857b36SDavid Xu return (error); 135486857b36SDavid Xu } 135586857b36SDavid Xu 135686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 135786857b36SDavid Xu struct timer_getoverrun_args { 135861d3a4efSDavid Xu int timerid; 135986857b36SDavid Xu }; 136086857b36SDavid Xu #endif 136186857b36SDavid Xu int 13628451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 136386857b36SDavid Xu { 1364*e346f8c4SBjoern A. Zeeb 1365*e346f8c4SBjoern A. Zeeb return (kern_ktimer_getoverrun(td, uap->timerid)); 1366*e346f8c4SBjoern A. Zeeb } 1367*e346f8c4SBjoern A. Zeeb 1368*e346f8c4SBjoern A. Zeeb int 1369*e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id) 1370*e346f8c4SBjoern A. Zeeb { 137186857b36SDavid Xu struct proc *p = td->td_proc; 137286857b36SDavid Xu struct itimer *it; 137386857b36SDavid Xu int error ; 137486857b36SDavid Xu 137586857b36SDavid Xu PROC_LOCK(p); 1376*e346f8c4SBjoern A. Zeeb if (timer_id < 3 || 1377*e346f8c4SBjoern A. Zeeb (it = itimer_find(p, timer_id)) == NULL) { 137886857b36SDavid Xu PROC_UNLOCK(p); 137986857b36SDavid Xu error = EINVAL; 138086857b36SDavid Xu } else { 138186857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 138286857b36SDavid Xu ITIMER_UNLOCK(it); 138356c06c4bSDavid Xu PROC_UNLOCK(p); 138486857b36SDavid Xu error = 0; 138586857b36SDavid Xu } 138686857b36SDavid Xu return (error); 138786857b36SDavid Xu } 138886857b36SDavid Xu 138986857b36SDavid Xu static int 139086857b36SDavid Xu realtimer_create(struct itimer *it) 139186857b36SDavid Xu { 139286857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 139386857b36SDavid Xu return (0); 139486857b36SDavid Xu } 139586857b36SDavid Xu 139686857b36SDavid Xu static int 139786857b36SDavid Xu realtimer_delete(struct itimer *it) 139886857b36SDavid Xu { 139986857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 1400843b99c6SDavid Xu 1401d6b6592eSDavid Xu /* 1402d6b6592eSDavid Xu * clear timer's value and interval to tell realtimer_expire 1403d6b6592eSDavid Xu * to not rearm the timer. 1404d6b6592eSDavid Xu */ 1405d6b6592eSDavid Xu timespecclear(&it->it_time.it_value); 1406d6b6592eSDavid Xu timespecclear(&it->it_time.it_interval); 1407843b99c6SDavid Xu ITIMER_UNLOCK(it); 1408843b99c6SDavid Xu callout_drain(&it->it_callout); 1409843b99c6SDavid Xu ITIMER_LOCK(it); 141086857b36SDavid Xu return (0); 141186857b36SDavid Xu } 141286857b36SDavid Xu 141386857b36SDavid Xu static int 141486857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 141586857b36SDavid Xu { 141656c06c4bSDavid Xu struct timespec cts; 141786857b36SDavid Xu 141886857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 141986857b36SDavid Xu 142056c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 142156c06c4bSDavid Xu *ovalue = it->it_time; 142286857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 142356c06c4bSDavid Xu timespecsub(&ovalue->it_value, &cts); 142486857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 142586857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 142686857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 142786857b36SDavid Xu ovalue->it_value.tv_sec = 0; 142886857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 142986857b36SDavid Xu } 143086857b36SDavid Xu } 143186857b36SDavid Xu return (0); 143286857b36SDavid Xu } 143386857b36SDavid Xu 143486857b36SDavid Xu static int 143586857b36SDavid Xu realtimer_settime(struct itimer *it, int flags, 143686857b36SDavid Xu struct itimerspec *value, struct itimerspec *ovalue) 143786857b36SDavid Xu { 143856c06c4bSDavid Xu struct timespec cts, ts; 143956c06c4bSDavid Xu struct timeval tv; 144056c06c4bSDavid Xu struct itimerspec val; 144186857b36SDavid Xu 144286857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 144386857b36SDavid Xu 144456c06c4bSDavid Xu val = *value; 144556c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 144686857b36SDavid Xu return (EINVAL); 144786857b36SDavid Xu 144856c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 144956c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 145086857b36SDavid Xu return (EINVAL); 145186857b36SDavid Xu } else { 145256c06c4bSDavid Xu timespecclear(&val.it_interval); 145386857b36SDavid Xu } 145486857b36SDavid Xu 145586857b36SDavid Xu if (ovalue != NULL) 145686857b36SDavid Xu realtimer_gettime(it, ovalue); 145786857b36SDavid Xu 145886857b36SDavid Xu it->it_time = val; 145956c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 146056c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 146156c06c4bSDavid Xu ts = val.it_value; 146286857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 146386857b36SDavid Xu /* Convert to absolute time. */ 146456c06c4bSDavid Xu timespecadd(&it->it_time.it_value, &cts); 146586857b36SDavid Xu } else { 146656c06c4bSDavid Xu timespecsub(&ts, &cts); 146786857b36SDavid Xu /* 146856c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 146986857b36SDavid Xu * fix it. 147086857b36SDavid Xu */ 147186857b36SDavid Xu } 147256c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 147356c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 147486857b36SDavid Xu realtimer_expire, it); 147586857b36SDavid Xu } else { 147686857b36SDavid Xu callout_stop(&it->it_callout); 147786857b36SDavid Xu } 147886857b36SDavid Xu 147986857b36SDavid Xu return (0); 148086857b36SDavid Xu } 148186857b36SDavid Xu 148286857b36SDavid Xu static void 148356c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 148486857b36SDavid Xu { 148586857b36SDavid Xu if (id == CLOCK_REALTIME) 148656c06c4bSDavid Xu getnanotime(ts); 148786857b36SDavid Xu else /* CLOCK_MONOTONIC */ 148856c06c4bSDavid Xu getnanouptime(ts); 148956c06c4bSDavid Xu } 149056c06c4bSDavid Xu 149156c06c4bSDavid Xu int 149261d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 149356c06c4bSDavid Xu { 149456c06c4bSDavid Xu struct itimer *it; 149556c06c4bSDavid Xu 149656c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 1497843b99c6SDavid Xu it = itimer_find(p, timerid); 149856c06c4bSDavid Xu if (it != NULL) { 149956c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 150056c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 150156c06c4bSDavid Xu it->it_overrun = 0; 150256c06c4bSDavid Xu ITIMER_UNLOCK(it); 150356c06c4bSDavid Xu return (0); 150456c06c4bSDavid Xu } 150556c06c4bSDavid Xu return (EINVAL); 150656c06c4bSDavid Xu } 150756c06c4bSDavid Xu 150856c06c4bSDavid Xu int 150956c06c4bSDavid Xu itimespecfix(struct timespec *ts) 151056c06c4bSDavid Xu { 151156c06c4bSDavid Xu 151256c06c4bSDavid Xu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 151356c06c4bSDavid Xu return (EINVAL); 151456c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 151556c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 151656c06c4bSDavid Xu return (0); 151786857b36SDavid Xu } 151886857b36SDavid Xu 151986857b36SDavid Xu /* Timeout callback for realtime timer */ 152086857b36SDavid Xu static void 152186857b36SDavid Xu realtimer_expire(void *arg) 152286857b36SDavid Xu { 152356c06c4bSDavid Xu struct timespec cts, ts; 152456c06c4bSDavid Xu struct timeval tv; 152586857b36SDavid Xu struct itimer *it; 152686857b36SDavid Xu 152786857b36SDavid Xu it = (struct itimer *)arg; 152886857b36SDavid Xu 152956c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 153086857b36SDavid Xu /* Only fire if time is reached. */ 153156c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 153256c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 153356c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 153486857b36SDavid Xu &it->it_time.it_interval); 153556c06c4bSDavid Xu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 153677e718f7SDavid Xu if (it->it_overrun < INT_MAX) 153786857b36SDavid Xu it->it_overrun++; 153877e718f7SDavid Xu else 153977e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 154056c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 154186857b36SDavid Xu &it->it_time.it_interval); 154286857b36SDavid Xu } 154386857b36SDavid Xu } else { 154486857b36SDavid Xu /* single shot timer ? */ 154556c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 154686857b36SDavid Xu } 154756c06c4bSDavid Xu if (timespecisset(&it->it_time.it_value)) { 154856c06c4bSDavid Xu ts = it->it_time.it_value; 154956c06c4bSDavid Xu timespecsub(&ts, &cts); 155056c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 155156c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 155286857b36SDavid Xu realtimer_expire, it); 155386857b36SDavid Xu } 1554d6b6592eSDavid Xu itimer_enter(it); 155586857b36SDavid Xu ITIMER_UNLOCK(it); 155686857b36SDavid Xu itimer_fire(it); 155786857b36SDavid Xu ITIMER_LOCK(it); 1558d6b6592eSDavid Xu itimer_leave(it); 155956c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 156056c06c4bSDavid Xu ts = it->it_time.it_value; 156156c06c4bSDavid Xu timespecsub(&ts, &cts); 156256c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 156356c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 156486857b36SDavid Xu it); 156586857b36SDavid Xu } 156686857b36SDavid Xu } 156786857b36SDavid Xu 156886857b36SDavid Xu void 156986857b36SDavid Xu itimer_fire(struct itimer *it) 157086857b36SDavid Xu { 157186857b36SDavid Xu struct proc *p = it->it_proc; 1572cf7d9a8cSDavid Xu struct thread *td; 157386857b36SDavid Xu 157456c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 157556c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1576cf7d9a8cSDavid Xu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 157756c06c4bSDavid Xu ITIMER_LOCK(it); 157856c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 157956c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 158056c06c4bSDavid Xu callout_stop(&it->it_callout); 158156c06c4bSDavid Xu ITIMER_UNLOCK(it); 1582cf7d9a8cSDavid Xu return; 15836d7b314bSDavid Xu } 1584cf7d9a8cSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 1585cf7d9a8cSDavid Xu it->it_ksi.ksi_errno = 0; 1586cf7d9a8cSDavid Xu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1587cf7d9a8cSDavid Xu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 158856c06c4bSDavid Xu } else { 158977e718f7SDavid Xu if (it->it_overrun < INT_MAX) 15906d7b314bSDavid Xu it->it_overrun++; 159177e718f7SDavid Xu else 159277e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 159356c06c4bSDavid Xu } 159486857b36SDavid Xu PROC_UNLOCK(p); 159586857b36SDavid Xu } 159686857b36SDavid Xu } 159786857b36SDavid Xu 159886857b36SDavid Xu static void 159986857b36SDavid Xu itimers_alloc(struct proc *p) 160086857b36SDavid Xu { 160160354683SDavid Xu struct itimers *its; 160260354683SDavid Xu int i; 160386857b36SDavid Xu 160460354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 160586857b36SDavid Xu LIST_INIT(&its->its_virtual); 160686857b36SDavid Xu LIST_INIT(&its->its_prof); 160786857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 160860354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 160960354683SDavid Xu its->its_timers[i] = NULL; 161060354683SDavid Xu PROC_LOCK(p); 161160354683SDavid Xu if (p->p_itimers == NULL) { 161260354683SDavid Xu p->p_itimers = its; 161360354683SDavid Xu PROC_UNLOCK(p); 161460354683SDavid Xu } 161560354683SDavid Xu else { 161660354683SDavid Xu PROC_UNLOCK(p); 161760354683SDavid Xu free(its, M_SUBPROC); 161860354683SDavid Xu } 161986857b36SDavid Xu } 162086857b36SDavid Xu 1621993182e5SAlexander Leidinger static void 1622993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1623993182e5SAlexander Leidinger { 1624993182e5SAlexander Leidinger itimers_event_hook_exit(arg, p); 1625993182e5SAlexander Leidinger } 1626993182e5SAlexander Leidinger 162786857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 1628d26b1a1fSDavid Xu static void 1629993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p) 163086857b36SDavid Xu { 163186857b36SDavid Xu struct itimers *its; 163286857b36SDavid Xu struct itimer *it; 16333e70c6f0SDavid Xu int event = (int)(intptr_t)arg; 163486857b36SDavid Xu int i; 163586857b36SDavid Xu 163660354683SDavid Xu if (p->p_itimers != NULL) { 163760354683SDavid Xu its = p->p_itimers; 163886857b36SDavid Xu for (i = 0; i < MAX_CLOCKS; ++i) { 163986857b36SDavid Xu if (posix_clocks[i].event_hook != NULL) 164086857b36SDavid Xu CLOCK_CALL(i, event_hook, (p, i, event)); 164186857b36SDavid Xu } 164286857b36SDavid Xu /* 164386857b36SDavid Xu * According to susv3, XSI interval timers should be inherited 164486857b36SDavid Xu * by new image. 164586857b36SDavid Xu */ 164686857b36SDavid Xu if (event == ITIMER_EV_EXEC) 164786857b36SDavid Xu i = 3; 164886857b36SDavid Xu else if (event == ITIMER_EV_EXIT) 164986857b36SDavid Xu i = 0; 165086857b36SDavid Xu else 165186857b36SDavid Xu panic("unhandled event"); 165286857b36SDavid Xu for (; i < TIMER_MAX; ++i) { 1653843b99c6SDavid Xu if ((it = its->its_timers[i]) != NULL) 1654643ee871SKonstantin Belousov kern_ktimer_delete(curthread, i); 165586857b36SDavid Xu } 165686857b36SDavid Xu if (its->its_timers[0] == NULL && 165786857b36SDavid Xu its->its_timers[1] == NULL && 165886857b36SDavid Xu its->its_timers[2] == NULL) { 165960354683SDavid Xu free(its, M_SUBPROC); 166060354683SDavid Xu p->p_itimers = NULL; 166186857b36SDavid Xu } 166286857b36SDavid Xu } 166386857b36SDavid Xu } 1664