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 354b8d5f2dSRobert Watson #include "opt_mac.h" 364b8d5f2dSRobert Watson 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38e96c1fdcSPeter Wemm #include <sys/systm.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> 464b8d5f2dSRobert Watson #include <sys/mac.h> 47efa42cbcSPaul Saab #include <sys/syscallsubr.h> 4877e718f7SDavid Xu #include <sys/sysctl.h> 4994c8fcd8SPeter Wemm #include <sys/sysent.h> 50df8bae1dSRodney W. Grimes #include <sys/proc.h> 51708e7684SPeter Wemm #include <sys/time.h> 5286857b36SDavid Xu #include <sys/timers.h> 5391266b96SPoul-Henning Kamp #include <sys/timetc.h> 54df8bae1dSRodney W. Grimes #include <sys/vnode.h> 55fb919e4dSMark Murray 5677e718f7SDavid Xu #include <posix4/posix4.h> 5777e718f7SDavid Xu 585b870b7bSPeter Wemm #include <vm/vm.h> 595b870b7bSPeter Wemm #include <vm/vm_extern.h> 60df8bae1dSRodney W. Grimes 6186857b36SDavid Xu #define MAX_CLOCKS (CLOCK_MONOTONIC+1) 6286857b36SDavid Xu 6391f1c2b3SPoul-Henning Kamp int tz_minuteswest; 6491f1c2b3SPoul-Henning Kamp int tz_dsttime; 65ac7e6123SDavid Greenman 6686857b36SDavid Xu static struct kclock posix_clocks[MAX_CLOCKS]; 6786857b36SDavid Xu static uma_zone_t itimer_zone = NULL; 6886857b36SDavid Xu 69df8bae1dSRodney W. Grimes /* 70df8bae1dSRodney W. Grimes * Time of day and interval timer support. 71df8bae1dSRodney W. Grimes * 72df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 73df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 74df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 75df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 76df8bae1dSRodney W. Grimes * timers when they expire. 77df8bae1dSRodney W. Grimes */ 78df8bae1dSRodney W. Grimes 797edfb592SJohn Baldwin static int settime(struct thread *, struct timeval *); 804d77a549SAlfred Perlstein static void timevalfix(struct timeval *); 814d77a549SAlfred Perlstein static void no_lease_updatetime(int); 8294c8fcd8SPeter Wemm 8386857b36SDavid Xu static void itimer_start(void); 8486857b36SDavid Xu static int itimer_init(void *, int, int); 8586857b36SDavid Xu static void itimer_fini(void *, int); 8686857b36SDavid Xu static void itimer_enter(struct itimer *); 8786857b36SDavid Xu static void itimer_leave(struct itimer *); 8886857b36SDavid Xu static struct itimer *itimer_find(struct proc *, timer_t, int); 8986857b36SDavid Xu static void itimers_alloc(struct proc *); 90d26b1a1fSDavid Xu static void itimers_event_hook(void *arg, struct proc *p); 9186857b36SDavid Xu static int realtimer_create(struct itimer *); 9286857b36SDavid Xu static int realtimer_gettime(struct itimer *, struct itimerspec *); 9386857b36SDavid Xu static int realtimer_settime(struct itimer *, int, 9486857b36SDavid Xu struct itimerspec *, struct itimerspec *); 9586857b36SDavid Xu static int realtimer_delete(struct itimer *); 9656c06c4bSDavid Xu static void realtimer_clocktime(clockid_t, struct timespec *); 9786857b36SDavid Xu static void realtimer_expire(void *); 9886857b36SDavid Xu static void realtimer_event_hook(struct proc *, clockid_t, int event); 9986857b36SDavid Xu static int kern_timer_create(struct thread *, clockid_t, 10086857b36SDavid Xu struct sigevent *, timer_t *, timer_t); 10186857b36SDavid Xu static int kern_timer_delete(struct thread *, timer_t); 10286857b36SDavid Xu 10386857b36SDavid Xu int register_posix_clock(int, struct kclock *); 10486857b36SDavid Xu void itimer_fire(struct itimer *it); 10556c06c4bSDavid Xu int itimespecfix(struct timespec *ts); 10686857b36SDavid Xu 10786857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist) \ 10886857b36SDavid Xu ((*posix_clocks[clock].call) arglist) 10986857b36SDavid Xu 11086857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 11186857b36SDavid Xu 11286857b36SDavid Xu 1131b09ae77SPoul-Henning Kamp static void 1141b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat) 1151b09ae77SPoul-Henning Kamp int deltat; 1161b09ae77SPoul-Henning Kamp { 1171b09ae77SPoul-Henning Kamp } 1181b09ae77SPoul-Henning Kamp 1194d77a549SAlfred Perlstein void (*lease_updatetime)(int) = no_lease_updatetime; 1201b09ae77SPoul-Henning Kamp 12194c8fcd8SPeter Wemm static int 12291afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 12394c8fcd8SPeter Wemm { 124fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 125c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 1267ec73f64SPoul-Henning Kamp struct timespec ts; 12794c8fcd8SPeter Wemm int s; 12894c8fcd8SPeter Wemm 129708e7684SPeter Wemm s = splclock(); 1309c8fff87SBruce Evans microtime(&tv1); 13100af9731SPoul-Henning Kamp delta = *tv; 13200af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 13394c8fcd8SPeter Wemm 13494c8fcd8SPeter Wemm /* 1359c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 136fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 137fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 138fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 139fcae3aa6SNick Sayer * back to the past. 140c0bd94a7SNick Sayer * 141c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 142c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 143c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 14494c8fcd8SPeter Wemm */ 1457edfb592SJohn Baldwin if (securelevel_gt(td->td_ucred, 1) != 0) { 146fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1473f92429aSMatt Jacob /* 148c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1493f92429aSMatt Jacob */ 150fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 151fcae3aa6SNick Sayer maxtime = tv1; 152fcae3aa6SNick Sayer tv2 = *tv; 153fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 154fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1553f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 156fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 157fcae3aa6SNick Sayer } 1583f92429aSMatt Jacob } else { 159c0bd94a7SNick Sayer if (tv1.tv_sec == laststep.tv_sec) { 160c0bd94a7SNick Sayer splx(s); 161c0bd94a7SNick Sayer return (EPERM); 162c0bd94a7SNick Sayer } 163c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 164c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 165c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 166c0bd94a7SNick Sayer } 167c0bd94a7SNick Sayer laststep = *tv; 168fcae3aa6SNick Sayer } 1699c8fff87SBruce Evans } 1709c8fff87SBruce Evans 1717ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1727ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 1737edfb592SJohn Baldwin mtx_lock(&Giant); 17491266b96SPoul-Henning Kamp tc_setclock(&ts); 17594c8fcd8SPeter Wemm (void) splsoftclock(); 17694c8fcd8SPeter Wemm lease_updatetime(delta.tv_sec); 17794c8fcd8SPeter Wemm splx(s); 17894c8fcd8SPeter Wemm resettodr(); 1797edfb592SJohn Baldwin mtx_unlock(&Giant); 18094c8fcd8SPeter Wemm return (0); 18194c8fcd8SPeter Wemm } 18294c8fcd8SPeter Wemm 18394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 18494c8fcd8SPeter Wemm struct clock_gettime_args { 18594c8fcd8SPeter Wemm clockid_t clock_id; 18694c8fcd8SPeter Wemm struct timespec *tp; 18794c8fcd8SPeter Wemm }; 18894c8fcd8SPeter Wemm #endif 189708e7684SPeter Wemm 190fb99ab88SMatthew Dillon /* 191fb99ab88SMatthew Dillon * MPSAFE 192fb99ab88SMatthew Dillon */ 19394c8fcd8SPeter Wemm /* ARGSUSED */ 19494c8fcd8SPeter Wemm int 19591afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap) 19694c8fcd8SPeter Wemm { 19794c8fcd8SPeter Wemm struct timespec ats; 198f0b479cdSPaul Saab int error; 199f0b479cdSPaul Saab 200f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 201f0b479cdSPaul Saab if (error == 0) 202f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 203f0b479cdSPaul Saab 204f0b479cdSPaul Saab return (error); 205f0b479cdSPaul Saab } 206f0b479cdSPaul Saab 207f0b479cdSPaul Saab int 208f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 209f0b479cdSPaul Saab { 210de0a9241SKelly Yancey struct timeval sys, user; 21178c85e8dSJohn Baldwin struct proc *p; 21294c8fcd8SPeter Wemm 21378c85e8dSJohn Baldwin p = td->td_proc; 214f0b479cdSPaul Saab switch (clock_id) { 2155e758b95SRobert Watson case CLOCK_REALTIME: /* Default to precise. */ 2165e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 217f0b479cdSPaul Saab nanotime(ats); 218b8817154SKelly Yancey break; 2195e758b95SRobert Watson case CLOCK_REALTIME_FAST: 2205e758b95SRobert Watson getnanotime(ats); 2215e758b95SRobert Watson break; 222b8817154SKelly Yancey case CLOCK_VIRTUAL: 22378c85e8dSJohn Baldwin PROC_LOCK(p); 22478c85e8dSJohn Baldwin calcru(p, &user, &sys); 22578c85e8dSJohn Baldwin PROC_UNLOCK(p); 226f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 227b8817154SKelly Yancey break; 228b8817154SKelly Yancey case CLOCK_PROF: 22978c85e8dSJohn Baldwin PROC_LOCK(p); 23078c85e8dSJohn Baldwin calcru(p, &user, &sys); 23178c85e8dSJohn Baldwin PROC_UNLOCK(p); 232de0a9241SKelly Yancey timevaladd(&user, &sys); 233f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 234de0a9241SKelly Yancey break; 2355e758b95SRobert Watson case CLOCK_MONOTONIC: /* Default to precise. */ 2365e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 2375eefd889SAndre Oppermann case CLOCK_UPTIME: 2385e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 239f0b479cdSPaul Saab nanouptime(ats); 240b8817154SKelly Yancey break; 2415e758b95SRobert Watson case CLOCK_UPTIME_FAST: 2425e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 2435e758b95SRobert Watson getnanouptime(ats); 2445e758b95SRobert Watson break; 2455e758b95SRobert Watson case CLOCK_SECOND: 2465e758b95SRobert Watson ats->tv_sec = time_second; 2475e758b95SRobert Watson ats->tv_nsec = 0; 2485e758b95SRobert Watson break; 249b8817154SKelly Yancey default: 2505cb3dc8fSPoul-Henning Kamp return (EINVAL); 251b8817154SKelly Yancey } 252f0b479cdSPaul Saab return (0); 25394c8fcd8SPeter Wemm } 25494c8fcd8SPeter Wemm 25594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 25694c8fcd8SPeter Wemm struct clock_settime_args { 25794c8fcd8SPeter Wemm clockid_t clock_id; 25894c8fcd8SPeter Wemm const struct timespec *tp; 25994c8fcd8SPeter Wemm }; 26094c8fcd8SPeter Wemm #endif 261708e7684SPeter Wemm 262fb99ab88SMatthew Dillon /* 263fb99ab88SMatthew Dillon * MPSAFE 264fb99ab88SMatthew Dillon */ 26594c8fcd8SPeter Wemm /* ARGSUSED */ 26694c8fcd8SPeter Wemm int 26791afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap) 26894c8fcd8SPeter Wemm { 26994c8fcd8SPeter Wemm struct timespec ats; 27094c8fcd8SPeter Wemm int error; 27194c8fcd8SPeter Wemm 272f0b479cdSPaul Saab if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 273f0b479cdSPaul Saab return (error); 274f0b479cdSPaul Saab return (kern_clock_settime(td, uap->clock_id, &ats)); 275f0b479cdSPaul Saab } 276f0b479cdSPaul Saab 277f0b479cdSPaul Saab int 278f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 279f0b479cdSPaul Saab { 280f0b479cdSPaul Saab struct timeval atv; 281f0b479cdSPaul Saab int error; 282f0b479cdSPaul Saab 2834b8d5f2dSRobert Watson #ifdef MAC 2844b8d5f2dSRobert Watson error = mac_check_system_settime(td->td_ucred); 2854b8d5f2dSRobert Watson if (error) 2864b8d5f2dSRobert Watson return (error); 2874b8d5f2dSRobert Watson #endif 28844731cabSJohn Baldwin if ((error = suser(td)) != 0) 2897edfb592SJohn Baldwin return (error); 290f0b479cdSPaul Saab if (clock_id != CLOCK_REALTIME) 2917edfb592SJohn Baldwin return (EINVAL); 292f0b479cdSPaul Saab if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 2937edfb592SJohn Baldwin return (EINVAL); 294a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 295f0b479cdSPaul Saab TIMESPEC_TO_TIMEVAL(&atv, ats); 2967edfb592SJohn Baldwin error = settime(td, &atv); 29794c8fcd8SPeter Wemm return (error); 29894c8fcd8SPeter Wemm } 29994c8fcd8SPeter Wemm 30094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 30194c8fcd8SPeter Wemm struct clock_getres_args { 30294c8fcd8SPeter Wemm clockid_t clock_id; 30394c8fcd8SPeter Wemm struct timespec *tp; 30494c8fcd8SPeter Wemm }; 30594c8fcd8SPeter Wemm #endif 306708e7684SPeter Wemm 30794c8fcd8SPeter Wemm int 30891afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap) 30994c8fcd8SPeter Wemm { 31094c8fcd8SPeter Wemm struct timespec ts; 311f0b479cdSPaul Saab int error; 31294c8fcd8SPeter Wemm 313f0b479cdSPaul Saab if (uap->tp == NULL) 314f0b479cdSPaul Saab return (0); 315f0b479cdSPaul Saab 316f0b479cdSPaul Saab error = kern_clock_getres(td, uap->clock_id, &ts); 317f0b479cdSPaul Saab if (error == 0) 318f0b479cdSPaul Saab error = copyout(&ts, uap->tp, sizeof(ts)); 319f0b479cdSPaul Saab return (error); 320f0b479cdSPaul Saab } 321f0b479cdSPaul Saab 322f0b479cdSPaul Saab int 323f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 324f0b479cdSPaul Saab { 325f0b479cdSPaul Saab 326f0b479cdSPaul Saab ts->tv_sec = 0; 327f0b479cdSPaul Saab switch (clock_id) { 328b8817154SKelly Yancey case CLOCK_REALTIME: 3295e758b95SRobert Watson case CLOCK_REALTIME_FAST: 3305e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 331b8817154SKelly Yancey case CLOCK_MONOTONIC: 3325e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 3335e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 3345eefd889SAndre Oppermann case CLOCK_UPTIME: 3355e758b95SRobert Watson case CLOCK_UPTIME_FAST: 3365e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 337ac0653dcSBruce Evans /* 338ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 339ac0653dcSBruce Evans * Rounding up is especially important if rounding down 340ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 341ac0653dcSBruce Evans */ 342f0b479cdSPaul Saab ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 343b8817154SKelly Yancey break; 344b8817154SKelly Yancey case CLOCK_VIRTUAL: 345b8817154SKelly Yancey case CLOCK_PROF: 346b8817154SKelly Yancey /* Accurately round up here because we can do so cheaply. */ 347f0b479cdSPaul Saab ts->tv_nsec = (1000000000 + hz - 1) / hz; 348b8817154SKelly Yancey break; 3495e758b95SRobert Watson case CLOCK_SECOND: 3505e758b95SRobert Watson ts->tv_sec = 1; 3515e758b95SRobert Watson ts->tv_nsec = 0; 3525e758b95SRobert Watson break; 353b8817154SKelly Yancey default: 354b8817154SKelly Yancey return (EINVAL); 35594c8fcd8SPeter Wemm } 356de0a9241SKelly Yancey return (0); 35794c8fcd8SPeter Wemm } 35894c8fcd8SPeter Wemm 35994c8fcd8SPeter Wemm static int nanowait; 36094c8fcd8SPeter Wemm 3617fdf2c85SPaul Saab int 3627fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 3635b870b7bSPeter Wemm { 3645704ba6aSPoul-Henning Kamp struct timespec ts, ts2, ts3; 36533841826SPoul-Henning Kamp struct timeval tv; 36633841826SPoul-Henning Kamp int error; 3675b870b7bSPeter Wemm 3687d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 369708e7684SPeter Wemm return (EINVAL); 370d254af07SMatthew Dillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 3717d7fb492SBruce Evans return (0); 372c21410e1SPoul-Henning Kamp getnanouptime(&ts); 37300af9731SPoul-Henning Kamp timespecadd(&ts, rqt); 37433841826SPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, rqt); 37533841826SPoul-Henning Kamp for (;;) { 37633841826SPoul-Henning Kamp error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 37733841826SPoul-Henning Kamp tvtohz(&tv)); 378c21410e1SPoul-Henning Kamp getnanouptime(&ts2); 37933841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 38033841826SPoul-Henning Kamp if (error == ERESTART) 38194c8fcd8SPeter Wemm error = EINTR; 38233841826SPoul-Henning Kamp if (rmt != NULL) { 38333841826SPoul-Henning Kamp timespecsub(&ts, &ts2); 38433841826SPoul-Henning Kamp if (ts.tv_sec < 0) 38533841826SPoul-Henning Kamp timespecclear(&ts); 38600af9731SPoul-Henning Kamp *rmt = ts; 38700af9731SPoul-Henning Kamp } 38833841826SPoul-Henning Kamp return (error); 38933841826SPoul-Henning Kamp } 39033841826SPoul-Henning Kamp if (timespeccmp(&ts2, &ts, >=)) 39133841826SPoul-Henning Kamp return (0); 3925704ba6aSPoul-Henning Kamp ts3 = ts; 3935704ba6aSPoul-Henning Kamp timespecsub(&ts3, &ts2); 3945704ba6aSPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, &ts3); 39533841826SPoul-Henning Kamp } 3965b870b7bSPeter Wemm } 39794c8fcd8SPeter Wemm 3985b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 3995b870b7bSPeter Wemm struct nanosleep_args { 4005b870b7bSPeter Wemm struct timespec *rqtp; 4015b870b7bSPeter Wemm struct timespec *rmtp; 4025b870b7bSPeter Wemm }; 4035b870b7bSPeter Wemm #endif 4045b870b7bSPeter Wemm 405fb99ab88SMatthew Dillon /* 406fb99ab88SMatthew Dillon * MPSAFE 407fb99ab88SMatthew Dillon */ 4085b870b7bSPeter Wemm /* ARGSUSED */ 4095b870b7bSPeter Wemm int 41091afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap) 4115b870b7bSPeter Wemm { 4125b870b7bSPeter Wemm struct timespec rmt, rqt; 413fb99ab88SMatthew Dillon int error; 4145b870b7bSPeter Wemm 415d1e405c5SAlfred Perlstein error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 4165b870b7bSPeter Wemm if (error) 4175b870b7bSPeter Wemm return (error); 418fb99ab88SMatthew Dillon 41931f3e2adSAlfred Perlstein if (uap->rmtp && 42031f3e2adSAlfred Perlstein !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 42131f3e2adSAlfred Perlstein return (EFAULT); 4227fdf2c85SPaul Saab error = kern_nanosleep(td, &rqt, &rmt); 423d1e405c5SAlfred Perlstein if (error && uap->rmtp) { 424fb99ab88SMatthew Dillon int error2; 425fb99ab88SMatthew Dillon 426d1e405c5SAlfred Perlstein error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 42731f3e2adSAlfred Perlstein if (error2) 428fb99ab88SMatthew Dillon error = error2; 429708e7684SPeter Wemm } 430708e7684SPeter Wemm return (error); 43194c8fcd8SPeter Wemm } 43294c8fcd8SPeter Wemm 4335b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 434df8bae1dSRodney W. Grimes struct gettimeofday_args { 435df8bae1dSRodney W. Grimes struct timeval *tp; 436df8bae1dSRodney W. Grimes struct timezone *tzp; 437df8bae1dSRodney W. Grimes }; 438d2d3e875SBruce Evans #endif 439fb99ab88SMatthew Dillon /* 440fb99ab88SMatthew Dillon * MPSAFE 441fb99ab88SMatthew Dillon */ 442df8bae1dSRodney W. Grimes /* ARGSUSED */ 44326f9a767SRodney W. Grimes int 44491afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap) 445df8bae1dSRodney W. Grimes { 446df8bae1dSRodney W. Grimes struct timeval atv; 447411c25edSTim J. Robbins struct timezone rtz; 448df8bae1dSRodney W. Grimes int error = 0; 449df8bae1dSRodney W. Grimes 450df8bae1dSRodney W. Grimes if (uap->tp) { 451df8bae1dSRodney W. Grimes microtime(&atv); 45201609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 453df8bae1dSRodney W. Grimes } 45421dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 45591f1c2b3SPoul-Henning Kamp rtz.tz_minuteswest = tz_minuteswest; 45691f1c2b3SPoul-Henning Kamp rtz.tz_dsttime = tz_dsttime; 457411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 45821dcdb38SPoul-Henning Kamp } 459df8bae1dSRodney W. Grimes return (error); 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes 462d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 463df8bae1dSRodney W. Grimes struct settimeofday_args { 464df8bae1dSRodney W. Grimes struct timeval *tv; 465df8bae1dSRodney W. Grimes struct timezone *tzp; 466df8bae1dSRodney W. Grimes }; 467d2d3e875SBruce Evans #endif 468fb99ab88SMatthew Dillon /* 469fb99ab88SMatthew Dillon * MPSAFE 470fb99ab88SMatthew Dillon */ 471df8bae1dSRodney W. Grimes /* ARGSUSED */ 47226f9a767SRodney W. Grimes int 47391afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap) 474df8bae1dSRodney W. Grimes { 475b88ec951SJohn Baldwin struct timeval atv, *tvp; 476b88ec951SJohn Baldwin struct timezone atz, *tzp; 477b88ec951SJohn Baldwin int error; 478b88ec951SJohn Baldwin 479b88ec951SJohn Baldwin if (uap->tv) { 480b88ec951SJohn Baldwin error = copyin(uap->tv, &atv, sizeof(atv)); 481b88ec951SJohn Baldwin if (error) 482b88ec951SJohn Baldwin return (error); 483b88ec951SJohn Baldwin tvp = &atv; 484b88ec951SJohn Baldwin } else 485b88ec951SJohn Baldwin tvp = NULL; 486b88ec951SJohn Baldwin if (uap->tzp) { 487b88ec951SJohn Baldwin error = copyin(uap->tzp, &atz, sizeof(atz)); 488b88ec951SJohn Baldwin if (error) 489b88ec951SJohn Baldwin return (error); 490b88ec951SJohn Baldwin tzp = &atz; 491b88ec951SJohn Baldwin } else 492b88ec951SJohn Baldwin tzp = NULL; 493b88ec951SJohn Baldwin return (kern_settimeofday(td, tvp, tzp)); 494b88ec951SJohn Baldwin } 495b88ec951SJohn Baldwin 496b88ec951SJohn Baldwin int 497b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 498b88ec951SJohn Baldwin { 499b88ec951SJohn Baldwin int error; 500fb99ab88SMatthew Dillon 5014b8d5f2dSRobert Watson #ifdef MAC 5024b8d5f2dSRobert Watson error = mac_check_system_settime(td->td_ucred); 5034b8d5f2dSRobert Watson if (error) 5044b8d5f2dSRobert Watson return (error); 5054b8d5f2dSRobert Watson #endif 506b88ec951SJohn Baldwin error = suser(td); 507b88ec951SJohn Baldwin if (error) 5087edfb592SJohn Baldwin return (error); 509df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 510b88ec951SJohn Baldwin if (tv) { 511b88ec951SJohn Baldwin if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 5127edfb592SJohn Baldwin return (EINVAL); 513b88ec951SJohn Baldwin error = settime(td, tv); 514708e7684SPeter Wemm } 515b88ec951SJohn Baldwin if (tzp && error == 0) { 516b88ec951SJohn Baldwin tz_minuteswest = tzp->tz_minuteswest; 517b88ec951SJohn Baldwin tz_dsttime = tzp->tz_dsttime; 518b88ec951SJohn Baldwin } 5197edfb592SJohn Baldwin return (error); 520b88ec951SJohn Baldwin } 5217edfb592SJohn Baldwin 522df8bae1dSRodney W. Grimes /* 523df8bae1dSRodney W. Grimes * Get value of an interval timer. The process virtual and 524df8bae1dSRodney W. Grimes * profiling virtual time timers are kept in the p_stats area, since 525df8bae1dSRodney W. Grimes * they can be swapped out. These are kept internally in the 526df8bae1dSRodney W. Grimes * way they are specified externally: in time until they expire. 527df8bae1dSRodney W. Grimes * 528df8bae1dSRodney W. Grimes * The real time interval timer is kept in the process table slot 529df8bae1dSRodney W. Grimes * for the process, and its value (it_value) is kept as an 530df8bae1dSRodney W. Grimes * absolute time rather than as a delta, so that it is easy to keep 531df8bae1dSRodney W. Grimes * periodic real-time signals from drifting. 532df8bae1dSRodney W. Grimes * 533df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 534df8bae1dSRodney W. Grimes * kern_clock.c. The real time timer is processed by a timeout 535df8bae1dSRodney W. Grimes * routine, called from the softclock() routine. Since a callout 536df8bae1dSRodney W. Grimes * may be delayed in real time due to interrupt processing in the system, 537df8bae1dSRodney W. Grimes * it is possible for the real time timeout routine (realitexpire, given below), 538df8bae1dSRodney W. Grimes * to be delayed in real time past when it is supposed to occur. It 539df8bae1dSRodney W. Grimes * does not suffice, therefore, to reload the real timer .it_value from the 540df8bae1dSRodney W. Grimes * real time timers .it_interval. Rather, we compute the next time in 541df8bae1dSRodney W. Grimes * absolute time the timer should go off. 542df8bae1dSRodney W. Grimes */ 543d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 544df8bae1dSRodney W. Grimes struct getitimer_args { 545df8bae1dSRodney W. Grimes u_int which; 546df8bae1dSRodney W. Grimes struct itimerval *itv; 547df8bae1dSRodney W. Grimes }; 548d2d3e875SBruce Evans #endif 549fb99ab88SMatthew Dillon /* 550fb99ab88SMatthew Dillon * MPSAFE 551fb99ab88SMatthew Dillon */ 55226f9a767SRodney W. Grimes int 55391afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap) 554df8bae1dSRodney W. Grimes { 555df8bae1dSRodney W. Grimes struct itimerval aitv; 556c90110d6SJohn Baldwin int error; 557df8bae1dSRodney W. Grimes 558cfa0efe7SMaxim Sobolev error = kern_getitimer(td, uap->which, &aitv); 559cfa0efe7SMaxim Sobolev if (error != 0) 560cfa0efe7SMaxim Sobolev return (error); 561cfa0efe7SMaxim Sobolev return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 562cfa0efe7SMaxim Sobolev } 563cfa0efe7SMaxim Sobolev 564cfa0efe7SMaxim Sobolev int 565cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 566cfa0efe7SMaxim Sobolev { 567cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 568cfa0efe7SMaxim Sobolev struct timeval ctv; 569cfa0efe7SMaxim Sobolev 570cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 571df8bae1dSRodney W. Grimes return (EINVAL); 572fb99ab88SMatthew Dillon 573cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 574df8bae1dSRodney W. Grimes /* 575ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 576df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 577df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 578df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 579df8bae1dSRodney W. Grimes */ 58096d7f8efSTim J. Robbins PROC_LOCK(p); 581cfa0efe7SMaxim Sobolev *aitv = p->p_realtimer; 58296d7f8efSTim J. Robbins PROC_UNLOCK(p); 583cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 584c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 585cfa0efe7SMaxim Sobolev if (timevalcmp(&aitv->it_value, &ctv, <)) 586cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_value); 587df8bae1dSRodney W. Grimes else 588cfa0efe7SMaxim Sobolev timevalsub(&aitv->it_value, &ctv); 589227ee8a1SPoul-Henning Kamp } 590fb99ab88SMatthew Dillon } else { 59196d7f8efSTim J. Robbins mtx_lock_spin(&sched_lock); 592cfa0efe7SMaxim Sobolev *aitv = p->p_stats->p_timer[which]; 59396d7f8efSTim J. Robbins mtx_unlock_spin(&sched_lock); 594fb99ab88SMatthew Dillon } 595cfa0efe7SMaxim Sobolev return (0); 596df8bae1dSRodney W. Grimes } 597df8bae1dSRodney W. Grimes 598d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 599df8bae1dSRodney W. Grimes struct setitimer_args { 600df8bae1dSRodney W. Grimes u_int which; 601df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 602df8bae1dSRodney W. Grimes }; 603d2d3e875SBruce Evans #endif 604cfa0efe7SMaxim Sobolev 605fb99ab88SMatthew Dillon /* 606fb99ab88SMatthew Dillon * MPSAFE 607fb99ab88SMatthew Dillon */ 60826f9a767SRodney W. Grimes int 60991afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap) 610df8bae1dSRodney W. Grimes { 611cfa0efe7SMaxim Sobolev struct itimerval aitv, oitv; 612c90110d6SJohn Baldwin int error; 61396d7f8efSTim J. Robbins 61496d7f8efSTim J. Robbins if (uap->itv == NULL) { 61596d7f8efSTim J. Robbins uap->itv = uap->oitv; 61696d7f8efSTim J. Robbins return (getitimer(td, (struct getitimer_args *)uap)); 61796d7f8efSTim J. Robbins } 618df8bae1dSRodney W. Grimes 61996d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 620df8bae1dSRodney W. Grimes return (error); 621cfa0efe7SMaxim Sobolev error = kern_setitimer(td, uap->which, &aitv, &oitv); 622cfa0efe7SMaxim Sobolev if (error != 0 || uap->oitv == NULL) 623cfa0efe7SMaxim Sobolev return (error); 624cfa0efe7SMaxim Sobolev return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 625cfa0efe7SMaxim Sobolev } 626cfa0efe7SMaxim Sobolev 627cfa0efe7SMaxim Sobolev int 628c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 629c90110d6SJohn Baldwin struct itimerval *oitv) 630cfa0efe7SMaxim Sobolev { 631cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 632cfa0efe7SMaxim Sobolev struct timeval ctv; 633cfa0efe7SMaxim Sobolev 6345e85ac17SJohn Baldwin if (aitv == NULL) 6355e85ac17SJohn Baldwin return (kern_getitimer(td, which, oitv)); 6365e85ac17SJohn Baldwin 637cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 63896d7f8efSTim J. Robbins return (EINVAL); 639cfa0efe7SMaxim Sobolev if (itimerfix(&aitv->it_value)) 640cfa0efe7SMaxim Sobolev return (EINVAL); 641cfa0efe7SMaxim Sobolev if (!timevalisset(&aitv->it_value)) 642cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_interval); 643cfa0efe7SMaxim Sobolev else if (itimerfix(&aitv->it_interval)) 64496d7f8efSTim J. Robbins return (EINVAL); 64596d7f8efSTim J. Robbins 646cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 64796d7f8efSTim J. Robbins PROC_LOCK(p); 6484cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 6494f559836SJake Burkholder callout_stop(&p->p_itcallout); 65025b4d3a8SJohn Baldwin getmicrouptime(&ctv); 651cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 652cfa0efe7SMaxim Sobolev callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 6534f559836SJake Burkholder realitexpire, p); 654cfa0efe7SMaxim Sobolev timevaladd(&aitv->it_value, &ctv); 65525b4d3a8SJohn Baldwin } 656cfa0efe7SMaxim Sobolev *oitv = p->p_realtimer; 657cfa0efe7SMaxim Sobolev p->p_realtimer = *aitv; 65896d7f8efSTim J. Robbins PROC_UNLOCK(p); 659cfa0efe7SMaxim Sobolev if (timevalisset(&oitv->it_value)) { 660cfa0efe7SMaxim Sobolev if (timevalcmp(&oitv->it_value, &ctv, <)) 661cfa0efe7SMaxim Sobolev timevalclear(&oitv->it_value); 66296d7f8efSTim J. Robbins else 663cfa0efe7SMaxim Sobolev timevalsub(&oitv->it_value, &ctv); 664fb99ab88SMatthew Dillon } 66596d7f8efSTim J. Robbins } else { 66696d7f8efSTim J. Robbins mtx_lock_spin(&sched_lock); 667cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 668cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 66996d7f8efSTim J. Robbins mtx_unlock_spin(&sched_lock); 67096d7f8efSTim J. Robbins } 67196d7f8efSTim J. Robbins return (0); 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes /* 675df8bae1dSRodney W. Grimes * Real interval timer expired: 676df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 677df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 678df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 679df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 680df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 681c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 6829207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 6839207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 6849207f00aSBruce Evans * interrupt even when we're delayed. 685df8bae1dSRodney W. Grimes */ 686df8bae1dSRodney W. Grimes void 68791afe087SPoul-Henning Kamp realitexpire(void *arg) 688df8bae1dSRodney W. Grimes { 68991afe087SPoul-Henning Kamp struct proc *p; 690bfe6c9faSPoul-Henning Kamp struct timeval ctv, ntv; 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes p = (struct proc *)arg; 69337824023SJohn Baldwin PROC_LOCK(p); 694df8bae1dSRodney W. Grimes psignal(p, SIGALRM); 6954cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 6964cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 6975499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 6985499ea01SJohn Baldwin wakeup(&p->p_itcallout); 69937824023SJohn Baldwin PROC_UNLOCK(p); 700df8bae1dSRodney W. Grimes return; 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes for (;;) { 703df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 704df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 705c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 7064cf41af3SPoul-Henning Kamp if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 707bfe6c9faSPoul-Henning Kamp ntv = p->p_realtimer.it_value; 708bfe6c9faSPoul-Henning Kamp timevalsub(&ntv, &ctv); 7094f559836SJake Burkholder callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 7104f559836SJake Burkholder realitexpire, p); 71137824023SJohn Baldwin PROC_UNLOCK(p); 712df8bae1dSRodney W. Grimes return; 713df8bae1dSRodney W. Grimes } 714df8bae1dSRodney W. Grimes } 71537824023SJohn Baldwin /*NOTREACHED*/ 716df8bae1dSRodney W. Grimes } 717df8bae1dSRodney W. Grimes 718df8bae1dSRodney W. Grimes /* 719df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 720df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 721df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 722df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 723df8bae1dSRodney W. Grimes */ 72426f9a767SRodney W. Grimes int 72591afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 726df8bae1dSRodney W. Grimes { 727df8bae1dSRodney W. Grimes 72886857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 729df8bae1dSRodney W. Grimes return (EINVAL); 730df8bae1dSRodney W. Grimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 731df8bae1dSRodney W. Grimes tv->tv_usec = tick; 732df8bae1dSRodney W. Grimes return (0); 733df8bae1dSRodney W. Grimes } 734df8bae1dSRodney W. Grimes 735df8bae1dSRodney W. Grimes /* 736df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 737df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 738df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 739df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 740df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 741df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 742df8bae1dSRodney W. Grimes * that it is called in a context where the timers 743df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 744df8bae1dSRodney W. Grimes */ 74526f9a767SRodney W. Grimes int 74691afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 747df8bae1dSRodney W. Grimes { 748df8bae1dSRodney W. Grimes 749df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 750df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 751df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 752df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 753df8bae1dSRodney W. Grimes goto expire; 754df8bae1dSRodney W. Grimes } 755df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 756df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 759df8bae1dSRodney W. Grimes usec = 0; 7604cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 761df8bae1dSRodney W. Grimes return (1); 762df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 763df8bae1dSRodney W. Grimes expire: 7644cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 765df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 766df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 767df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 768df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 769df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 770df8bae1dSRodney W. Grimes } 771df8bae1dSRodney W. Grimes } else 772df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 773df8bae1dSRodney W. Grimes return (0); 774df8bae1dSRodney W. Grimes } 775df8bae1dSRodney W. Grimes 776df8bae1dSRodney W. Grimes /* 777df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 778df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 779df8bae1dSRodney W. Grimes * results which are before the beginning, 780df8bae1dSRodney W. Grimes * it just gets very confused in this case. 781df8bae1dSRodney W. Grimes * Caveat emptor. 782df8bae1dSRodney W. Grimes */ 78326f9a767SRodney W. Grimes void 7846ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 785df8bae1dSRodney W. Grimes { 786df8bae1dSRodney W. Grimes 787df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 788df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 789df8bae1dSRodney W. Grimes timevalfix(t1); 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes 79226f9a767SRodney W. Grimes void 7936ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 794df8bae1dSRodney W. Grimes { 795df8bae1dSRodney W. Grimes 796df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 797df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 798df8bae1dSRodney W. Grimes timevalfix(t1); 799df8bae1dSRodney W. Grimes } 800df8bae1dSRodney W. Grimes 80187b6de2bSPoul-Henning Kamp static void 80291afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 803df8bae1dSRodney W. Grimes { 804df8bae1dSRodney W. Grimes 805df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 806df8bae1dSRodney W. Grimes t1->tv_sec--; 807df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 808df8bae1dSRodney W. Grimes } 809df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 810df8bae1dSRodney W. Grimes t1->tv_sec++; 811df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 812df8bae1dSRodney W. Grimes } 813df8bae1dSRodney W. Grimes } 81491974ce1SSam Leffler 81591974ce1SSam Leffler /* 816addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 81791974ce1SSam Leffler */ 81891974ce1SSam Leffler int 81991974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 82091974ce1SSam Leffler { 82191974ce1SSam Leffler struct timeval tv, delta; 82291974ce1SSam Leffler int rv = 0; 82391974ce1SSam Leffler 824addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 825addea9d4SSam Leffler delta = tv; 826addea9d4SSam Leffler timevalsub(&delta, lasttime); 82791974ce1SSam Leffler 82891974ce1SSam Leffler /* 82991974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 83091974ce1SSam Leffler * even if interval is huge. 83191974ce1SSam Leffler */ 83291974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 83391974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 83491974ce1SSam Leffler *lasttime = tv; 83591974ce1SSam Leffler rv = 1; 83691974ce1SSam Leffler } 83791974ce1SSam Leffler 83891974ce1SSam Leffler return (rv); 83991974ce1SSam Leffler } 84091974ce1SSam Leffler 84191974ce1SSam Leffler /* 84291974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 843addea9d4SSam Leffler * 844addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 845addea9d4SSam Leffler * should drop a packet because of the rate limitation). 846addea9d4SSam Leffler * 847893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 848893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 849893bec80SSam Leffler * limiting. 850893bec80SSam Leffler * 851addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 852addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 853addea9d4SSam Leffler * clock ticks for minimal overhead. 85491974ce1SSam Leffler */ 85591974ce1SSam Leffler int 85691974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 85791974ce1SSam Leffler { 858addea9d4SSam Leffler int now; 85991974ce1SSam Leffler 86091974ce1SSam Leffler /* 861addea9d4SSam Leffler * Reset the last time and counter if this is the first call 862addea9d4SSam Leffler * or more than a second has passed since the last update of 863addea9d4SSam Leffler * lasttime. 86491974ce1SSam Leffler */ 865addea9d4SSam Leffler now = ticks; 866addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 867addea9d4SSam Leffler lasttime->tv_sec = now; 868addea9d4SSam Leffler *curpps = 1; 869893bec80SSam Leffler return (maxpps != 0); 870addea9d4SSam Leffler } else { 871addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 872addea9d4SSam Leffler return (maxpps < 0 || *curpps < maxpps); 873addea9d4SSam Leffler } 87491974ce1SSam Leffler } 87586857b36SDavid Xu 87686857b36SDavid Xu static void 87786857b36SDavid Xu itimer_start(void) 87886857b36SDavid Xu { 87986857b36SDavid Xu struct kclock rt_clock = { 88086857b36SDavid Xu .timer_create = realtimer_create, 88186857b36SDavid Xu .timer_delete = realtimer_delete, 88286857b36SDavid Xu .timer_settime = realtimer_settime, 88386857b36SDavid Xu .timer_gettime = realtimer_gettime, 88486857b36SDavid Xu .event_hook = realtimer_event_hook 88586857b36SDavid Xu }; 88686857b36SDavid Xu 88786857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 88886857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 88986857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 89086857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 89177e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 89277e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 89377e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 894d26b1a1fSDavid Xu EVENTHANDLER_REGISTER(process_exit, itimers_event_hook, 895d26b1a1fSDavid Xu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 896d26b1a1fSDavid Xu EVENTHANDLER_REGISTER(process_exec, itimers_event_hook, 897d26b1a1fSDavid Xu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 89886857b36SDavid Xu } 89986857b36SDavid Xu 90086857b36SDavid Xu int 90186857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk) 90286857b36SDavid Xu { 90386857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 90486857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 90586857b36SDavid Xu return (0); 90686857b36SDavid Xu } 90786857b36SDavid Xu posix_clocks[clockid] = *clk; 90886857b36SDavid Xu return (1); 90986857b36SDavid Xu } 91086857b36SDavid Xu 91186857b36SDavid Xu static int 91286857b36SDavid Xu itimer_init(void *mem, int size, int flags) 91386857b36SDavid Xu { 91486857b36SDavid Xu struct itimer *it; 91586857b36SDavid Xu 91686857b36SDavid Xu it = (struct itimer *)mem; 91786857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 91886857b36SDavid Xu return (0); 91986857b36SDavid Xu } 92086857b36SDavid Xu 92186857b36SDavid Xu static void 92286857b36SDavid Xu itimer_fini(void *mem, int size) 92386857b36SDavid Xu { 92486857b36SDavid Xu struct itimer *it; 92586857b36SDavid Xu 92686857b36SDavid Xu it = (struct itimer *)mem; 92786857b36SDavid Xu mtx_destroy(&it->it_mtx); 92886857b36SDavid Xu } 92986857b36SDavid Xu 93086857b36SDavid Xu static void 93186857b36SDavid Xu itimer_enter(struct itimer *it) 93286857b36SDavid Xu { 93386857b36SDavid Xu 93486857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 93586857b36SDavid Xu it->it_usecount++; 93686857b36SDavid Xu } 93786857b36SDavid Xu 93886857b36SDavid Xu static void 93986857b36SDavid Xu itimer_leave(struct itimer *it) 94086857b36SDavid Xu { 94186857b36SDavid Xu 94286857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 94386857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 94486857b36SDavid Xu 94586857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 94686857b36SDavid Xu wakeup(it); 94786857b36SDavid Xu } 94886857b36SDavid Xu 94986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 95086857b36SDavid Xu struct timer_create_args { 95186857b36SDavid Xu clockid_t clock_id; 95286857b36SDavid Xu struct sigevent * evp; 95386857b36SDavid Xu timer_t * timerid; 95486857b36SDavid Xu }; 95586857b36SDavid Xu #endif 95686857b36SDavid Xu 95786857b36SDavid Xu int 95886857b36SDavid Xu timer_create(struct thread *td, struct timer_create_args *uap) 95986857b36SDavid Xu { 96086857b36SDavid Xu struct sigevent *evp1, ev; 96186857b36SDavid Xu timer_t id; 96286857b36SDavid Xu int error; 96386857b36SDavid Xu 96486857b36SDavid Xu if (uap->evp != NULL) { 96586857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 96686857b36SDavid Xu if (error != 0) 96786857b36SDavid Xu return (error); 96886857b36SDavid Xu evp1 = &ev; 96986857b36SDavid Xu } else 97086857b36SDavid Xu evp1 = NULL; 97186857b36SDavid Xu 97286857b36SDavid Xu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 97386857b36SDavid Xu 97486857b36SDavid Xu if (error == 0) { 97586857b36SDavid Xu error = copyout(&id, uap->timerid, sizeof(timer_t)); 97686857b36SDavid Xu if (error != 0) 97786857b36SDavid Xu kern_timer_delete(td, id); 97886857b36SDavid Xu } 97986857b36SDavid Xu return (error); 98086857b36SDavid Xu } 98186857b36SDavid Xu 98286857b36SDavid Xu static int 98386857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id, 98486857b36SDavid Xu struct sigevent *evp, timer_t *timerid, timer_t preset_id) 98586857b36SDavid Xu { 98686857b36SDavid Xu struct proc *p = td->td_proc; 98786857b36SDavid Xu struct itimer *it; 98886857b36SDavid Xu int id; 98986857b36SDavid Xu int error; 99086857b36SDavid Xu 99186857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 99286857b36SDavid Xu return (EINVAL); 99386857b36SDavid Xu 99486857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 99586857b36SDavid Xu return (EINVAL); 99686857b36SDavid Xu 99786857b36SDavid Xu if (evp != NULL) { 99886857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 99956c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 100056c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 100186857b36SDavid Xu return (EINVAL); 100256c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 100356c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 100486857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 100586857b36SDavid Xu return (EINVAL); 100686857b36SDavid Xu } 100786857b36SDavid Xu 100860354683SDavid Xu if (p->p_itimers == NULL) 100986857b36SDavid Xu itimers_alloc(p); 101086857b36SDavid Xu 101186857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 101286857b36SDavid Xu it->it_flags = 0; 101386857b36SDavid Xu it->it_usecount = 0; 101486857b36SDavid Xu it->it_active = 0; 101556c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 101656c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 101786857b36SDavid Xu it->it_overrun = 0; 101886857b36SDavid Xu it->it_overrun_last = 0; 101986857b36SDavid Xu it->it_clockid = clock_id; 102086857b36SDavid Xu it->it_timerid = -1; 102186857b36SDavid Xu it->it_proc = p; 102286857b36SDavid Xu ksiginfo_init(&it->it_ksi); 102386857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 102486857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 102586857b36SDavid Xu if (error != 0) 102686857b36SDavid Xu goto out; 102786857b36SDavid Xu 102886857b36SDavid Xu PROC_LOCK(p); 102986857b36SDavid Xu if (preset_id != -1) { 103086857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 103186857b36SDavid Xu id = preset_id; 103260354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 103386857b36SDavid Xu PROC_UNLOCK(p); 103486857b36SDavid Xu error = 0; 103586857b36SDavid Xu goto out; 103686857b36SDavid Xu } 103786857b36SDavid Xu } else { 103886857b36SDavid Xu /* 103986857b36SDavid Xu * Find a free timer slot, skipping those reserved 104086857b36SDavid Xu * for setitimer(). 104186857b36SDavid Xu */ 104286857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 104360354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 104486857b36SDavid Xu break; 104586857b36SDavid Xu if (id == TIMER_MAX) { 104686857b36SDavid Xu PROC_UNLOCK(p); 104786857b36SDavid Xu error = EAGAIN; 104886857b36SDavid Xu goto out; 104986857b36SDavid Xu } 105086857b36SDavid Xu } 105186857b36SDavid Xu it->it_timerid = id; 105260354683SDavid Xu p->p_itimers->its_timers[id] = it; 105386857b36SDavid Xu if (evp != NULL) 105486857b36SDavid Xu it->it_sigev = *evp; 105586857b36SDavid Xu else { 105686857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 105786857b36SDavid Xu switch (clock_id) { 105886857b36SDavid Xu default: 105986857b36SDavid Xu case CLOCK_REALTIME: 106086857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 106186857b36SDavid Xu break; 106286857b36SDavid Xu case CLOCK_VIRTUAL: 106386857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 106486857b36SDavid Xu break; 106586857b36SDavid Xu case CLOCK_PROF: 106686857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 106786857b36SDavid Xu break; 106886857b36SDavid Xu } 10698f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 107086857b36SDavid Xu } 107186857b36SDavid Xu 107256c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 107356c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 107486857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 107586857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 107686857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 107786857b36SDavid Xu it->it_ksi.ksi_timerid = id; 107886857b36SDavid Xu } 107986857b36SDavid Xu PROC_UNLOCK(p); 108086857b36SDavid Xu *timerid = id; 108186857b36SDavid Xu return (0); 108286857b36SDavid Xu 108386857b36SDavid Xu out: 108486857b36SDavid Xu ITIMER_LOCK(it); 108586857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 108686857b36SDavid Xu ITIMER_UNLOCK(it); 108786857b36SDavid Xu uma_zfree(itimer_zone, it); 108886857b36SDavid Xu return (error); 108986857b36SDavid Xu } 109086857b36SDavid Xu 109186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 109286857b36SDavid Xu struct timer_delete_args { 109386857b36SDavid Xu timer_t timerid; 109486857b36SDavid Xu }; 109586857b36SDavid Xu #endif 109686857b36SDavid Xu 109786857b36SDavid Xu int 109886857b36SDavid Xu timer_delete(struct thread *td, struct timer_delete_args *uap) 109986857b36SDavid Xu { 110086857b36SDavid Xu return (kern_timer_delete(td, uap->timerid)); 110186857b36SDavid Xu } 110286857b36SDavid Xu 110386857b36SDavid Xu static struct itimer * 110486857b36SDavid Xu itimer_find(struct proc *p, timer_t timerid, int include_deleting) 110586857b36SDavid Xu { 110686857b36SDavid Xu struct itimer *it; 110786857b36SDavid Xu 110886857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 110960354683SDavid Xu if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) || 111060354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 111186857b36SDavid Xu return (NULL); 111286857b36SDavid Xu } 111386857b36SDavid Xu ITIMER_LOCK(it); 111486857b36SDavid Xu if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) { 111586857b36SDavid Xu ITIMER_UNLOCK(it); 111686857b36SDavid Xu it = NULL; 111786857b36SDavid Xu } 111886857b36SDavid Xu return (it); 111986857b36SDavid Xu } 112086857b36SDavid Xu 112186857b36SDavid Xu static int 112286857b36SDavid Xu kern_timer_delete(struct thread *td, timer_t timerid) 112386857b36SDavid Xu { 112486857b36SDavid Xu struct proc *p = td->td_proc; 112586857b36SDavid Xu struct itimer *it; 112686857b36SDavid Xu 112786857b36SDavid Xu PROC_LOCK(p); 112886857b36SDavid Xu it = itimer_find(p, timerid, 0); 112986857b36SDavid Xu if (it == NULL) { 113086857b36SDavid Xu PROC_UNLOCK(p); 113186857b36SDavid Xu return (EINVAL); 113286857b36SDavid Xu } 113386857b36SDavid Xu PROC_UNLOCK(p); 113486857b36SDavid Xu 113586857b36SDavid Xu it->it_flags |= ITF_DELETING; 113686857b36SDavid Xu while (it->it_usecount > 0) { 113786857b36SDavid Xu it->it_flags |= ITF_WANTED; 113886857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 113986857b36SDavid Xu } 114086857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 114186857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 114286857b36SDavid Xu ITIMER_UNLOCK(it); 114386857b36SDavid Xu 114486857b36SDavid Xu PROC_LOCK(p); 114586857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 114686857b36SDavid Xu sigqueue_take(&it->it_ksi); 114760354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 114886857b36SDavid Xu PROC_UNLOCK(p); 114986857b36SDavid Xu uma_zfree(itimer_zone, it); 115086857b36SDavid Xu return (0); 115186857b36SDavid Xu } 115286857b36SDavid Xu 115386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 115486857b36SDavid Xu struct timer_settime_args { 115586857b36SDavid Xu timer_t timerid; 115686857b36SDavid Xu int flags; 115786857b36SDavid Xu const struct itimerspec * value; 115886857b36SDavid Xu struct itimerspec * ovalue; 115986857b36SDavid Xu }; 116086857b36SDavid Xu #endif 116186857b36SDavid Xu 116286857b36SDavid Xu int 116386857b36SDavid Xu timer_settime(struct thread *td, struct timer_settime_args *uap) 116486857b36SDavid Xu { 116586857b36SDavid Xu struct proc *p = td->td_proc; 116686857b36SDavid Xu struct itimer *it; 116786857b36SDavid Xu struct itimerspec val, oval, *ovalp; 116886857b36SDavid Xu int error; 116986857b36SDavid Xu 117086857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 117186857b36SDavid Xu if (error != 0) 117286857b36SDavid Xu return (error); 117386857b36SDavid Xu 117486857b36SDavid Xu if (uap->ovalue != NULL) 117586857b36SDavid Xu ovalp = &oval; 117686857b36SDavid Xu else 117786857b36SDavid Xu ovalp = NULL; 117886857b36SDavid Xu 117986857b36SDavid Xu PROC_LOCK(p); 118086857b36SDavid Xu if (uap->timerid < 3 || 118186857b36SDavid Xu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 118286857b36SDavid Xu PROC_UNLOCK(p); 118386857b36SDavid Xu error = EINVAL; 118486857b36SDavid Xu } else { 118586857b36SDavid Xu PROC_UNLOCK(p); 118686857b36SDavid Xu itimer_enter(it); 118786857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_settime, 118886857b36SDavid Xu (it, uap->flags, &val, ovalp)); 118986857b36SDavid Xu itimer_leave(it); 119086857b36SDavid Xu ITIMER_UNLOCK(it); 119186857b36SDavid Xu } 119286857b36SDavid Xu if (error == 0 && uap->ovalue != NULL) 119386857b36SDavid Xu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 119486857b36SDavid Xu return (error); 119586857b36SDavid Xu } 119686857b36SDavid Xu 119786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 119886857b36SDavid Xu struct timer_gettime_args { 119986857b36SDavid Xu timer_t timerid; 120086857b36SDavid Xu struct itimerspec * value; 120186857b36SDavid Xu }; 120286857b36SDavid Xu #endif 120386857b36SDavid Xu 120486857b36SDavid Xu int 120586857b36SDavid Xu timer_gettime(struct thread *td, struct timer_gettime_args *uap) 120686857b36SDavid Xu { 120786857b36SDavid Xu struct proc *p = td->td_proc; 120886857b36SDavid Xu struct itimer *it; 120986857b36SDavid Xu struct itimerspec val; 121086857b36SDavid Xu int error; 121186857b36SDavid Xu 121286857b36SDavid Xu PROC_LOCK(p); 121386857b36SDavid Xu if (uap->timerid < 3 || 121486857b36SDavid Xu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 121586857b36SDavid Xu PROC_UNLOCK(p); 121686857b36SDavid Xu error = EINVAL; 121786857b36SDavid Xu } else { 121886857b36SDavid Xu PROC_UNLOCK(p); 121986857b36SDavid Xu itimer_enter(it); 122086857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_gettime, 122186857b36SDavid Xu (it, &val)); 122286857b36SDavid Xu itimer_leave(it); 122386857b36SDavid Xu ITIMER_UNLOCK(it); 122486857b36SDavid Xu } 122586857b36SDavid Xu if (error == 0) 122686857b36SDavid Xu error = copyout(&val, uap->value, sizeof(val)); 122786857b36SDavid Xu return (error); 122886857b36SDavid Xu } 122986857b36SDavid Xu 123086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 123186857b36SDavid Xu struct timer_getoverrun_args { 123286857b36SDavid Xu timer_t timerid; 123386857b36SDavid Xu }; 123486857b36SDavid Xu #endif 123586857b36SDavid Xu 123686857b36SDavid Xu int 123786857b36SDavid Xu timer_getoverrun(struct thread *td, struct timer_getoverrun_args *uap) 123886857b36SDavid Xu { 123986857b36SDavid Xu struct proc *p = td->td_proc; 124086857b36SDavid Xu struct itimer *it; 124186857b36SDavid Xu int error ; 124286857b36SDavid Xu 124386857b36SDavid Xu PROC_LOCK(p); 124486857b36SDavid Xu if (uap->timerid < 3 || 124586857b36SDavid Xu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 124686857b36SDavid Xu PROC_UNLOCK(p); 124786857b36SDavid Xu error = EINVAL; 124886857b36SDavid Xu } else { 124986857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 125086857b36SDavid Xu ITIMER_UNLOCK(it); 125156c06c4bSDavid Xu PROC_UNLOCK(p); 125286857b36SDavid Xu error = 0; 125386857b36SDavid Xu } 125486857b36SDavid Xu return (error); 125586857b36SDavid Xu } 125686857b36SDavid Xu 125786857b36SDavid Xu static int 125886857b36SDavid Xu realtimer_create(struct itimer *it) 125986857b36SDavid Xu { 126086857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 126186857b36SDavid Xu return (0); 126286857b36SDavid Xu } 126386857b36SDavid Xu 126486857b36SDavid Xu static int 126586857b36SDavid Xu realtimer_delete(struct itimer *it) 126686857b36SDavid Xu { 126786857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 126886857b36SDavid Xu callout_stop(&it->it_callout); 126986857b36SDavid Xu return (0); 127086857b36SDavid Xu } 127186857b36SDavid Xu 127286857b36SDavid Xu static int 127386857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 127486857b36SDavid Xu { 127556c06c4bSDavid Xu struct timespec cts; 127686857b36SDavid Xu 127786857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 127886857b36SDavid Xu 127956c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 128056c06c4bSDavid Xu *ovalue = it->it_time; 128186857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 128256c06c4bSDavid Xu timespecsub(&ovalue->it_value, &cts); 128386857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 128486857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 128586857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 128686857b36SDavid Xu ovalue->it_value.tv_sec = 0; 128786857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 128886857b36SDavid Xu } 128986857b36SDavid Xu } 129086857b36SDavid Xu return (0); 129186857b36SDavid Xu } 129286857b36SDavid Xu 129386857b36SDavid Xu static int 129486857b36SDavid Xu realtimer_settime(struct itimer *it, int flags, 129586857b36SDavid Xu struct itimerspec *value, struct itimerspec *ovalue) 129686857b36SDavid Xu { 129756c06c4bSDavid Xu struct timespec cts, ts; 129856c06c4bSDavid Xu struct timeval tv; 129956c06c4bSDavid Xu struct itimerspec val; 130086857b36SDavid Xu 130186857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 130286857b36SDavid Xu 130356c06c4bSDavid Xu val = *value; 130456c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 130586857b36SDavid Xu return (EINVAL); 130686857b36SDavid Xu 130756c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 130856c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 130986857b36SDavid Xu return (EINVAL); 131086857b36SDavid Xu } else { 131156c06c4bSDavid Xu timespecclear(&val.it_interval); 131286857b36SDavid Xu } 131386857b36SDavid Xu 131486857b36SDavid Xu if (ovalue != NULL) 131586857b36SDavid Xu realtimer_gettime(it, ovalue); 131686857b36SDavid Xu 131786857b36SDavid Xu it->it_time = val; 131856c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 131956c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 132056c06c4bSDavid Xu ts = val.it_value; 132186857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 132286857b36SDavid Xu /* Convert to absolute time. */ 132356c06c4bSDavid Xu timespecadd(&it->it_time.it_value, &cts); 132486857b36SDavid Xu } else { 132556c06c4bSDavid Xu timespecsub(&ts, &cts); 132686857b36SDavid Xu /* 132756c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 132886857b36SDavid Xu * fix it. 132986857b36SDavid Xu */ 133086857b36SDavid Xu } 133156c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 133256c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 133386857b36SDavid Xu realtimer_expire, it); 133486857b36SDavid Xu } else { 133586857b36SDavid Xu callout_stop(&it->it_callout); 133686857b36SDavid Xu } 133786857b36SDavid Xu 133886857b36SDavid Xu return (0); 133986857b36SDavid Xu } 134086857b36SDavid Xu 134186857b36SDavid Xu static void 134256c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 134386857b36SDavid Xu { 134486857b36SDavid Xu if (id == CLOCK_REALTIME) 134556c06c4bSDavid Xu getnanotime(ts); 134686857b36SDavid Xu else /* CLOCK_MONOTONIC */ 134756c06c4bSDavid Xu getnanouptime(ts); 134856c06c4bSDavid Xu } 134956c06c4bSDavid Xu 135056c06c4bSDavid Xu int 135156c06c4bSDavid Xu itimer_accept(struct proc *p, timer_t timerid, ksiginfo_t *ksi) 135256c06c4bSDavid Xu { 135356c06c4bSDavid Xu struct itimer *it; 135456c06c4bSDavid Xu 135556c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 135656c06c4bSDavid Xu it = itimer_find(p, timerid, 0); 135756c06c4bSDavid Xu if (it != NULL) { 135856c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 135956c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 136056c06c4bSDavid Xu it->it_overrun = 0; 136156c06c4bSDavid Xu ITIMER_UNLOCK(it); 136256c06c4bSDavid Xu return (0); 136356c06c4bSDavid Xu } 136456c06c4bSDavid Xu return (EINVAL); 136556c06c4bSDavid Xu } 136656c06c4bSDavid Xu 136756c06c4bSDavid Xu int 136856c06c4bSDavid Xu itimespecfix(struct timespec *ts) 136956c06c4bSDavid Xu { 137056c06c4bSDavid Xu 137156c06c4bSDavid Xu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 137256c06c4bSDavid Xu return (EINVAL); 137356c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 137456c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 137556c06c4bSDavid Xu return (0); 137686857b36SDavid Xu } 137786857b36SDavid Xu 137886857b36SDavid Xu static void 137986857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event) 138086857b36SDavid Xu { 138186857b36SDavid Xu struct itimers *its; 138286857b36SDavid Xu struct itimer *it; 138386857b36SDavid Xu int i; 138486857b36SDavid Xu 138586857b36SDavid Xu /* 138686857b36SDavid Xu * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX 138786857b36SDavid Xu * specification, it should be inherited by new process image. 138886857b36SDavid Xu */ 138986857b36SDavid Xu if (event == ITIMER_EV_EXEC) 139086857b36SDavid Xu i = 1; 139186857b36SDavid Xu else 139286857b36SDavid Xu i = 0; 139360354683SDavid Xu its = p->p_itimers; 139486857b36SDavid Xu for (; i < TIMER_MAX; i++) { 139586857b36SDavid Xu if ((it = its->its_timers[i]) != NULL && 139686857b36SDavid Xu it->it_clockid == clock_id) { 139786857b36SDavid Xu ITIMER_LOCK(it); 139886857b36SDavid Xu callout_stop(&it->it_callout); 139986857b36SDavid Xu ITIMER_UNLOCK(it); 140086857b36SDavid Xu } 140186857b36SDavid Xu } 140286857b36SDavid Xu } 140386857b36SDavid Xu 140486857b36SDavid Xu /* Timeout callback for realtime timer */ 140586857b36SDavid Xu static void 140686857b36SDavid Xu realtimer_expire(void *arg) 140786857b36SDavid Xu { 140856c06c4bSDavid Xu struct timespec cts, ts; 140956c06c4bSDavid Xu struct timeval tv; 141086857b36SDavid Xu struct itimer *it; 141186857b36SDavid Xu struct proc *p; 141286857b36SDavid Xu 141386857b36SDavid Xu it = (struct itimer *)arg; 141486857b36SDavid Xu p = it->it_proc; 141586857b36SDavid Xu 141656c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 141786857b36SDavid Xu /* Only fire if time is reached. */ 141856c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 141956c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 142056c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 142186857b36SDavid Xu &it->it_time.it_interval); 142256c06c4bSDavid Xu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 142377e718f7SDavid Xu if (it->it_overrun < INT_MAX) 142486857b36SDavid Xu it->it_overrun++; 142577e718f7SDavid Xu else 142677e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 142756c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 142886857b36SDavid Xu &it->it_time.it_interval); 142986857b36SDavid Xu } 143086857b36SDavid Xu } else { 143186857b36SDavid Xu /* single shot timer ? */ 143256c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 143386857b36SDavid Xu } 143456c06c4bSDavid Xu if (timespecisset(&it->it_time.it_value)) { 143556c06c4bSDavid Xu ts = it->it_time.it_value; 143656c06c4bSDavid Xu timespecsub(&ts, &cts); 143756c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 143856c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 143986857b36SDavid Xu realtimer_expire, it); 144086857b36SDavid Xu } 144186857b36SDavid Xu ITIMER_UNLOCK(it); 144286857b36SDavid Xu itimer_fire(it); 144386857b36SDavid Xu ITIMER_LOCK(it); 144456c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 144556c06c4bSDavid Xu ts = it->it_time.it_value; 144656c06c4bSDavid Xu timespecsub(&ts, &cts); 144756c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 144856c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 144986857b36SDavid Xu it); 145086857b36SDavid Xu } 145186857b36SDavid Xu } 145286857b36SDavid Xu 145386857b36SDavid Xu void 145486857b36SDavid Xu itimer_fire(struct itimer *it) 145586857b36SDavid Xu { 145686857b36SDavid Xu struct proc *p = it->it_proc; 14576d7b314bSDavid Xu int ret; 145886857b36SDavid Xu 145956c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 146056c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 146186857b36SDavid Xu PROC_LOCK(p); 14626d7b314bSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 146377e718f7SDavid Xu it->it_ksi.ksi_errno = 0; 14646d7b314bSDavid Xu ret = psignal_event(p, &it->it_sigev, &it->it_ksi); 14656d7b314bSDavid Xu if (__predict_false(ret != 0)) { 146686857b36SDavid Xu it->it_overrun++; 146756c06c4bSDavid Xu /* 146856c06c4bSDavid Xu * Broken userland code, thread went 146956c06c4bSDavid Xu * away, disarm the timer. 147056c06c4bSDavid Xu */ 14716d7b314bSDavid Xu if (ret == ESRCH) { 147256c06c4bSDavid Xu ITIMER_LOCK(it); 147356c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 147456c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 147556c06c4bSDavid Xu callout_stop(&it->it_callout); 147656c06c4bSDavid Xu ITIMER_UNLOCK(it); 14776d7b314bSDavid Xu } 147856c06c4bSDavid Xu } 147956c06c4bSDavid Xu } else { 148077e718f7SDavid Xu if (it->it_overrun < INT_MAX) 14816d7b314bSDavid Xu it->it_overrun++; 148277e718f7SDavid Xu else 148377e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 148456c06c4bSDavid Xu } 148586857b36SDavid Xu PROC_UNLOCK(p); 148686857b36SDavid Xu } 148786857b36SDavid Xu } 148886857b36SDavid Xu 148986857b36SDavid Xu static void 149086857b36SDavid Xu itimers_alloc(struct proc *p) 149186857b36SDavid Xu { 149260354683SDavid Xu struct itimers *its; 149360354683SDavid Xu int i; 149486857b36SDavid Xu 149560354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 149686857b36SDavid Xu LIST_INIT(&its->its_virtual); 149786857b36SDavid Xu LIST_INIT(&its->its_prof); 149886857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 149960354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 150060354683SDavid Xu its->its_timers[i] = NULL; 150160354683SDavid Xu PROC_LOCK(p); 150260354683SDavid Xu if (p->p_itimers == NULL) { 150360354683SDavid Xu p->p_itimers = its; 150460354683SDavid Xu PROC_UNLOCK(p); 150560354683SDavid Xu } 150660354683SDavid Xu else { 150760354683SDavid Xu PROC_UNLOCK(p); 150860354683SDavid Xu free(its, M_SUBPROC); 150960354683SDavid Xu } 151086857b36SDavid Xu } 151186857b36SDavid Xu 151286857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 1513d26b1a1fSDavid Xu static void 1514d26b1a1fSDavid Xu itimers_event_hook(void *arg, struct proc *p) 151586857b36SDavid Xu { 151686857b36SDavid Xu struct itimers *its; 151786857b36SDavid Xu struct itimer *it; 1518d26b1a1fSDavid Xu int event = (int)arg; 151986857b36SDavid Xu int i; 152086857b36SDavid Xu 152160354683SDavid Xu if (p->p_itimers != NULL) { 152260354683SDavid Xu its = p->p_itimers; 152386857b36SDavid Xu for (i = 0; i < MAX_CLOCKS; ++i) { 152486857b36SDavid Xu if (posix_clocks[i].event_hook != NULL) 152586857b36SDavid Xu CLOCK_CALL(i, event_hook, (p, i, event)); 152686857b36SDavid Xu } 152786857b36SDavid Xu /* 152886857b36SDavid Xu * According to susv3, XSI interval timers should be inherited 152986857b36SDavid Xu * by new image. 153086857b36SDavid Xu */ 153186857b36SDavid Xu if (event == ITIMER_EV_EXEC) 153286857b36SDavid Xu i = 3; 153386857b36SDavid Xu else if (event == ITIMER_EV_EXIT) 153486857b36SDavid Xu i = 0; 153586857b36SDavid Xu else 153686857b36SDavid Xu panic("unhandled event"); 153786857b36SDavid Xu for (; i < TIMER_MAX; ++i) { 153886857b36SDavid Xu if ((it = its->its_timers[i]) != NULL) { 153986857b36SDavid Xu PROC_LOCK(p); 154086857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 154186857b36SDavid Xu sigqueue_take(&it->it_ksi); 154286857b36SDavid Xu PROC_UNLOCK(p); 154386857b36SDavid Xu uma_zfree(itimer_zone, its->its_timers[i]); 154486857b36SDavid Xu its->its_timers[i] = NULL; 154586857b36SDavid Xu } 154686857b36SDavid Xu } 154786857b36SDavid Xu if (its->its_timers[0] == NULL && 154886857b36SDavid Xu its->its_timers[1] == NULL && 154986857b36SDavid Xu its->its_timers[2] == NULL) { 155060354683SDavid Xu free(its, M_SUBPROC); 155160354683SDavid Xu p->p_itimers = NULL; 155286857b36SDavid Xu } 155386857b36SDavid Xu } 155486857b36SDavid Xu } 1555