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> 42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 44df8bae1dSRodney W. Grimes #include <sys/kernel.h> 454b8d5f2dSRobert Watson #include <sys/mac.h> 46efa42cbcSPaul Saab #include <sys/syscallsubr.h> 4794c8fcd8SPeter Wemm #include <sys/sysent.h> 48df8bae1dSRodney W. Grimes #include <sys/proc.h> 49708e7684SPeter Wemm #include <sys/time.h> 5086857b36SDavid Xu #include <sys/timers.h> 5191266b96SPoul-Henning Kamp #include <sys/timetc.h> 52df8bae1dSRodney W. Grimes #include <sys/vnode.h> 53fb919e4dSMark Murray 545b870b7bSPeter Wemm #include <vm/vm.h> 555b870b7bSPeter Wemm #include <vm/vm_extern.h> 56df8bae1dSRodney W. Grimes 5786857b36SDavid Xu #define MAX_CLOCKS (CLOCK_MONOTONIC+1) 5886857b36SDavid Xu 5991f1c2b3SPoul-Henning Kamp int tz_minuteswest; 6091f1c2b3SPoul-Henning Kamp int tz_dsttime; 61ac7e6123SDavid Greenman 6286857b36SDavid Xu static struct kclock posix_clocks[MAX_CLOCKS]; 6386857b36SDavid Xu static uma_zone_t itimer_zone = NULL; 6486857b36SDavid Xu 65df8bae1dSRodney W. Grimes /* 66df8bae1dSRodney W. Grimes * Time of day and interval timer support. 67df8bae1dSRodney W. Grimes * 68df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 69df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 70df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 71df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 72df8bae1dSRodney W. Grimes * timers when they expire. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 757edfb592SJohn Baldwin static int settime(struct thread *, struct timeval *); 764d77a549SAlfred Perlstein static void timevalfix(struct timeval *); 774d77a549SAlfred Perlstein static void no_lease_updatetime(int); 7894c8fcd8SPeter Wemm 7986857b36SDavid Xu static void itimer_start(void); 8086857b36SDavid Xu static int itimer_init(void *, int, int); 8186857b36SDavid Xu static void itimer_fini(void *, int); 8286857b36SDavid Xu static void itimer_enter(struct itimer *); 8386857b36SDavid Xu static void itimer_leave(struct itimer *); 8486857b36SDavid Xu static struct itimer *itimer_find(struct proc *, timer_t, int); 8586857b36SDavid Xu static void itimers_alloc(struct proc *); 8686857b36SDavid Xu static int realtimer_create(struct itimer *); 8786857b36SDavid Xu static int realtimer_gettime(struct itimer *, struct itimerspec *); 8886857b36SDavid Xu static int realtimer_settime(struct itimer *, int, 8986857b36SDavid Xu struct itimerspec *, struct itimerspec *); 9086857b36SDavid Xu static int realtimer_delete(struct itimer *); 9156c06c4bSDavid Xu static void realtimer_clocktime(clockid_t, struct timespec *); 9286857b36SDavid Xu static void realtimer_expire(void *); 9386857b36SDavid Xu static void realtimer_event_hook(struct proc *, clockid_t, int event); 9486857b36SDavid Xu static int kern_timer_create(struct thread *, clockid_t, 9586857b36SDavid Xu struct sigevent *, timer_t *, timer_t); 9686857b36SDavid Xu static int kern_timer_delete(struct thread *, timer_t); 9786857b36SDavid Xu 9886857b36SDavid Xu int register_posix_clock(int, struct kclock *); 9986857b36SDavid Xu void itimer_fire(struct itimer *it); 10056c06c4bSDavid Xu int itimespecfix(struct timespec *ts); 10186857b36SDavid Xu 10286857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist) \ 10386857b36SDavid Xu ((*posix_clocks[clock].call) arglist) 10486857b36SDavid Xu 10586857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 10686857b36SDavid Xu 10786857b36SDavid Xu 1081b09ae77SPoul-Henning Kamp static void 1091b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat) 1101b09ae77SPoul-Henning Kamp int deltat; 1111b09ae77SPoul-Henning Kamp { 1121b09ae77SPoul-Henning Kamp } 1131b09ae77SPoul-Henning Kamp 1144d77a549SAlfred Perlstein void (*lease_updatetime)(int) = no_lease_updatetime; 1151b09ae77SPoul-Henning Kamp 11694c8fcd8SPeter Wemm static int 11791afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 11894c8fcd8SPeter Wemm { 119fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 120c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 1217ec73f64SPoul-Henning Kamp struct timespec ts; 12294c8fcd8SPeter Wemm int s; 12394c8fcd8SPeter Wemm 124708e7684SPeter Wemm s = splclock(); 1259c8fff87SBruce Evans microtime(&tv1); 12600af9731SPoul-Henning Kamp delta = *tv; 12700af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 12894c8fcd8SPeter Wemm 12994c8fcd8SPeter Wemm /* 1309c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 131fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 132fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 133fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 134fcae3aa6SNick Sayer * back to the past. 135c0bd94a7SNick Sayer * 136c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 137c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 138c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 13994c8fcd8SPeter Wemm */ 1407edfb592SJohn Baldwin if (securelevel_gt(td->td_ucred, 1) != 0) { 141fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1423f92429aSMatt Jacob /* 143c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1443f92429aSMatt Jacob */ 145fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 146fcae3aa6SNick Sayer maxtime = tv1; 147fcae3aa6SNick Sayer tv2 = *tv; 148fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 149fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1503f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 151fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 152fcae3aa6SNick Sayer } 1533f92429aSMatt Jacob } else { 154c0bd94a7SNick Sayer if (tv1.tv_sec == laststep.tv_sec) { 155c0bd94a7SNick Sayer splx(s); 156c0bd94a7SNick Sayer return (EPERM); 157c0bd94a7SNick Sayer } 158c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 159c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 160c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 161c0bd94a7SNick Sayer } 162c0bd94a7SNick Sayer laststep = *tv; 163fcae3aa6SNick Sayer } 1649c8fff87SBruce Evans } 1659c8fff87SBruce Evans 1667ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1677ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 1687edfb592SJohn Baldwin mtx_lock(&Giant); 16991266b96SPoul-Henning Kamp tc_setclock(&ts); 17094c8fcd8SPeter Wemm (void) splsoftclock(); 17194c8fcd8SPeter Wemm lease_updatetime(delta.tv_sec); 17294c8fcd8SPeter Wemm splx(s); 17394c8fcd8SPeter Wemm resettodr(); 1747edfb592SJohn Baldwin mtx_unlock(&Giant); 17594c8fcd8SPeter Wemm return (0); 17694c8fcd8SPeter Wemm } 17794c8fcd8SPeter Wemm 17894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 17994c8fcd8SPeter Wemm struct clock_gettime_args { 18094c8fcd8SPeter Wemm clockid_t clock_id; 18194c8fcd8SPeter Wemm struct timespec *tp; 18294c8fcd8SPeter Wemm }; 18394c8fcd8SPeter Wemm #endif 184708e7684SPeter Wemm 185fb99ab88SMatthew Dillon /* 186fb99ab88SMatthew Dillon * MPSAFE 187fb99ab88SMatthew Dillon */ 18894c8fcd8SPeter Wemm /* ARGSUSED */ 18994c8fcd8SPeter Wemm int 19091afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap) 19194c8fcd8SPeter Wemm { 19294c8fcd8SPeter Wemm struct timespec ats; 193f0b479cdSPaul Saab int error; 194f0b479cdSPaul Saab 195f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 196f0b479cdSPaul Saab if (error == 0) 197f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 198f0b479cdSPaul Saab 199f0b479cdSPaul Saab return (error); 200f0b479cdSPaul Saab } 201f0b479cdSPaul Saab 202f0b479cdSPaul Saab int 203f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 204f0b479cdSPaul Saab { 205de0a9241SKelly Yancey struct timeval sys, user; 20678c85e8dSJohn Baldwin struct proc *p; 20794c8fcd8SPeter Wemm 20878c85e8dSJohn Baldwin p = td->td_proc; 209f0b479cdSPaul Saab switch (clock_id) { 210b8817154SKelly Yancey case CLOCK_REALTIME: 211f0b479cdSPaul Saab nanotime(ats); 212b8817154SKelly Yancey break; 213b8817154SKelly Yancey case CLOCK_VIRTUAL: 21478c85e8dSJohn Baldwin PROC_LOCK(p); 21578c85e8dSJohn Baldwin calcru(p, &user, &sys); 21678c85e8dSJohn Baldwin PROC_UNLOCK(p); 217f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 218b8817154SKelly Yancey break; 219b8817154SKelly Yancey case CLOCK_PROF: 22078c85e8dSJohn Baldwin PROC_LOCK(p); 22178c85e8dSJohn Baldwin calcru(p, &user, &sys); 22278c85e8dSJohn Baldwin PROC_UNLOCK(p); 223de0a9241SKelly Yancey timevaladd(&user, &sys); 224f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 225de0a9241SKelly Yancey break; 226de0a9241SKelly Yancey case CLOCK_MONOTONIC: 2275eefd889SAndre Oppermann case CLOCK_UPTIME: 228f0b479cdSPaul Saab nanouptime(ats); 229b8817154SKelly Yancey break; 230b8817154SKelly Yancey default: 2315cb3dc8fSPoul-Henning Kamp return (EINVAL); 232b8817154SKelly Yancey } 233f0b479cdSPaul Saab return (0); 23494c8fcd8SPeter Wemm } 23594c8fcd8SPeter Wemm 23694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 23794c8fcd8SPeter Wemm struct clock_settime_args { 23894c8fcd8SPeter Wemm clockid_t clock_id; 23994c8fcd8SPeter Wemm const struct timespec *tp; 24094c8fcd8SPeter Wemm }; 24194c8fcd8SPeter Wemm #endif 242708e7684SPeter Wemm 243fb99ab88SMatthew Dillon /* 244fb99ab88SMatthew Dillon * MPSAFE 245fb99ab88SMatthew Dillon */ 24694c8fcd8SPeter Wemm /* ARGSUSED */ 24794c8fcd8SPeter Wemm int 24891afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap) 24994c8fcd8SPeter Wemm { 25094c8fcd8SPeter Wemm struct timespec ats; 25194c8fcd8SPeter Wemm int error; 25294c8fcd8SPeter Wemm 253f0b479cdSPaul Saab if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 254f0b479cdSPaul Saab return (error); 255f0b479cdSPaul Saab return (kern_clock_settime(td, uap->clock_id, &ats)); 256f0b479cdSPaul Saab } 257f0b479cdSPaul Saab 258f0b479cdSPaul Saab int 259f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 260f0b479cdSPaul Saab { 261f0b479cdSPaul Saab struct timeval atv; 262f0b479cdSPaul Saab int error; 263f0b479cdSPaul Saab 2644b8d5f2dSRobert Watson #ifdef MAC 2654b8d5f2dSRobert Watson error = mac_check_system_settime(td->td_ucred); 2664b8d5f2dSRobert Watson if (error) 2674b8d5f2dSRobert Watson return (error); 2684b8d5f2dSRobert Watson #endif 26944731cabSJohn Baldwin if ((error = suser(td)) != 0) 2707edfb592SJohn Baldwin return (error); 271f0b479cdSPaul Saab if (clock_id != CLOCK_REALTIME) 2727edfb592SJohn Baldwin return (EINVAL); 273f0b479cdSPaul Saab if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 2747edfb592SJohn Baldwin return (EINVAL); 275a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 276f0b479cdSPaul Saab TIMESPEC_TO_TIMEVAL(&atv, ats); 2777edfb592SJohn Baldwin error = settime(td, &atv); 27894c8fcd8SPeter Wemm return (error); 27994c8fcd8SPeter Wemm } 28094c8fcd8SPeter Wemm 28194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 28294c8fcd8SPeter Wemm struct clock_getres_args { 28394c8fcd8SPeter Wemm clockid_t clock_id; 28494c8fcd8SPeter Wemm struct timespec *tp; 28594c8fcd8SPeter Wemm }; 28694c8fcd8SPeter Wemm #endif 287708e7684SPeter Wemm 28894c8fcd8SPeter Wemm int 28991afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap) 29094c8fcd8SPeter Wemm { 29194c8fcd8SPeter Wemm struct timespec ts; 292f0b479cdSPaul Saab int error; 29394c8fcd8SPeter Wemm 294f0b479cdSPaul Saab if (uap->tp == NULL) 295f0b479cdSPaul Saab return (0); 296f0b479cdSPaul Saab 297f0b479cdSPaul Saab error = kern_clock_getres(td, uap->clock_id, &ts); 298f0b479cdSPaul Saab if (error == 0) 299f0b479cdSPaul Saab error = copyout(&ts, uap->tp, sizeof(ts)); 300f0b479cdSPaul Saab return (error); 301f0b479cdSPaul Saab } 302f0b479cdSPaul Saab 303f0b479cdSPaul Saab int 304f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 305f0b479cdSPaul Saab { 306f0b479cdSPaul Saab 307f0b479cdSPaul Saab ts->tv_sec = 0; 308f0b479cdSPaul Saab switch (clock_id) { 309b8817154SKelly Yancey case CLOCK_REALTIME: 310b8817154SKelly Yancey case CLOCK_MONOTONIC: 3115eefd889SAndre Oppermann case CLOCK_UPTIME: 312ac0653dcSBruce Evans /* 313ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 314ac0653dcSBruce Evans * Rounding up is especially important if rounding down 315ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 316ac0653dcSBruce Evans */ 317f0b479cdSPaul Saab ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 318b8817154SKelly Yancey break; 319b8817154SKelly Yancey case CLOCK_VIRTUAL: 320b8817154SKelly Yancey case CLOCK_PROF: 321b8817154SKelly Yancey /* Accurately round up here because we can do so cheaply. */ 322f0b479cdSPaul Saab ts->tv_nsec = (1000000000 + hz - 1) / hz; 323b8817154SKelly Yancey break; 324b8817154SKelly Yancey default: 325b8817154SKelly Yancey return (EINVAL); 32694c8fcd8SPeter Wemm } 327de0a9241SKelly Yancey return (0); 32894c8fcd8SPeter Wemm } 32994c8fcd8SPeter Wemm 33094c8fcd8SPeter Wemm static int nanowait; 33194c8fcd8SPeter Wemm 3327fdf2c85SPaul Saab int 3337fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 3345b870b7bSPeter Wemm { 3355704ba6aSPoul-Henning Kamp struct timespec ts, ts2, ts3; 33633841826SPoul-Henning Kamp struct timeval tv; 33733841826SPoul-Henning Kamp int error; 3385b870b7bSPeter Wemm 3397d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 340708e7684SPeter Wemm return (EINVAL); 341d254af07SMatthew Dillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 3427d7fb492SBruce Evans return (0); 343c21410e1SPoul-Henning Kamp getnanouptime(&ts); 34400af9731SPoul-Henning Kamp timespecadd(&ts, rqt); 34533841826SPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, rqt); 34633841826SPoul-Henning Kamp for (;;) { 34733841826SPoul-Henning Kamp error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 34833841826SPoul-Henning Kamp tvtohz(&tv)); 349c21410e1SPoul-Henning Kamp getnanouptime(&ts2); 35033841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 35133841826SPoul-Henning Kamp if (error == ERESTART) 35294c8fcd8SPeter Wemm error = EINTR; 35333841826SPoul-Henning Kamp if (rmt != NULL) { 35433841826SPoul-Henning Kamp timespecsub(&ts, &ts2); 35533841826SPoul-Henning Kamp if (ts.tv_sec < 0) 35633841826SPoul-Henning Kamp timespecclear(&ts); 35700af9731SPoul-Henning Kamp *rmt = ts; 35800af9731SPoul-Henning Kamp } 35933841826SPoul-Henning Kamp return (error); 36033841826SPoul-Henning Kamp } 36133841826SPoul-Henning Kamp if (timespeccmp(&ts2, &ts, >=)) 36233841826SPoul-Henning Kamp return (0); 3635704ba6aSPoul-Henning Kamp ts3 = ts; 3645704ba6aSPoul-Henning Kamp timespecsub(&ts3, &ts2); 3655704ba6aSPoul-Henning Kamp TIMESPEC_TO_TIMEVAL(&tv, &ts3); 36633841826SPoul-Henning Kamp } 3675b870b7bSPeter Wemm } 36894c8fcd8SPeter Wemm 3695b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 3705b870b7bSPeter Wemm struct nanosleep_args { 3715b870b7bSPeter Wemm struct timespec *rqtp; 3725b870b7bSPeter Wemm struct timespec *rmtp; 3735b870b7bSPeter Wemm }; 3745b870b7bSPeter Wemm #endif 3755b870b7bSPeter Wemm 376fb99ab88SMatthew Dillon /* 377fb99ab88SMatthew Dillon * MPSAFE 378fb99ab88SMatthew Dillon */ 3795b870b7bSPeter Wemm /* ARGSUSED */ 3805b870b7bSPeter Wemm int 38191afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap) 3825b870b7bSPeter Wemm { 3835b870b7bSPeter Wemm struct timespec rmt, rqt; 384fb99ab88SMatthew Dillon int error; 3855b870b7bSPeter Wemm 386d1e405c5SAlfred Perlstein error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 3875b870b7bSPeter Wemm if (error) 3885b870b7bSPeter Wemm return (error); 389fb99ab88SMatthew Dillon 39031f3e2adSAlfred Perlstein if (uap->rmtp && 39131f3e2adSAlfred Perlstein !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 39231f3e2adSAlfred Perlstein return (EFAULT); 3937fdf2c85SPaul Saab error = kern_nanosleep(td, &rqt, &rmt); 394d1e405c5SAlfred Perlstein if (error && uap->rmtp) { 395fb99ab88SMatthew Dillon int error2; 396fb99ab88SMatthew Dillon 397d1e405c5SAlfred Perlstein error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 39831f3e2adSAlfred Perlstein if (error2) 399fb99ab88SMatthew Dillon error = error2; 400708e7684SPeter Wemm } 401708e7684SPeter Wemm return (error); 40294c8fcd8SPeter Wemm } 40394c8fcd8SPeter Wemm 4045b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 405df8bae1dSRodney W. Grimes struct gettimeofday_args { 406df8bae1dSRodney W. Grimes struct timeval *tp; 407df8bae1dSRodney W. Grimes struct timezone *tzp; 408df8bae1dSRodney W. Grimes }; 409d2d3e875SBruce Evans #endif 410fb99ab88SMatthew Dillon /* 411fb99ab88SMatthew Dillon * MPSAFE 412fb99ab88SMatthew Dillon */ 413df8bae1dSRodney W. Grimes /* ARGSUSED */ 41426f9a767SRodney W. Grimes int 41591afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap) 416df8bae1dSRodney W. Grimes { 417df8bae1dSRodney W. Grimes struct timeval atv; 418411c25edSTim J. Robbins struct timezone rtz; 419df8bae1dSRodney W. Grimes int error = 0; 420df8bae1dSRodney W. Grimes 421df8bae1dSRodney W. Grimes if (uap->tp) { 422df8bae1dSRodney W. Grimes microtime(&atv); 42301609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 424df8bae1dSRodney W. Grimes } 42521dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 42691f1c2b3SPoul-Henning Kamp rtz.tz_minuteswest = tz_minuteswest; 42791f1c2b3SPoul-Henning Kamp rtz.tz_dsttime = tz_dsttime; 428411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 42921dcdb38SPoul-Henning Kamp } 430df8bae1dSRodney W. Grimes return (error); 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes 433d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 434df8bae1dSRodney W. Grimes struct settimeofday_args { 435df8bae1dSRodney W. Grimes struct timeval *tv; 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 settimeofday(struct thread *td, struct settimeofday_args *uap) 445df8bae1dSRodney W. Grimes { 446b88ec951SJohn Baldwin struct timeval atv, *tvp; 447b88ec951SJohn Baldwin struct timezone atz, *tzp; 448b88ec951SJohn Baldwin int error; 449b88ec951SJohn Baldwin 450b88ec951SJohn Baldwin if (uap->tv) { 451b88ec951SJohn Baldwin error = copyin(uap->tv, &atv, sizeof(atv)); 452b88ec951SJohn Baldwin if (error) 453b88ec951SJohn Baldwin return (error); 454b88ec951SJohn Baldwin tvp = &atv; 455b88ec951SJohn Baldwin } else 456b88ec951SJohn Baldwin tvp = NULL; 457b88ec951SJohn Baldwin if (uap->tzp) { 458b88ec951SJohn Baldwin error = copyin(uap->tzp, &atz, sizeof(atz)); 459b88ec951SJohn Baldwin if (error) 460b88ec951SJohn Baldwin return (error); 461b88ec951SJohn Baldwin tzp = &atz; 462b88ec951SJohn Baldwin } else 463b88ec951SJohn Baldwin tzp = NULL; 464b88ec951SJohn Baldwin return (kern_settimeofday(td, tvp, tzp)); 465b88ec951SJohn Baldwin } 466b88ec951SJohn Baldwin 467b88ec951SJohn Baldwin int 468b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 469b88ec951SJohn Baldwin { 470b88ec951SJohn Baldwin int error; 471fb99ab88SMatthew Dillon 4724b8d5f2dSRobert Watson #ifdef MAC 4734b8d5f2dSRobert Watson error = mac_check_system_settime(td->td_ucred); 4744b8d5f2dSRobert Watson if (error) 4754b8d5f2dSRobert Watson return (error); 4764b8d5f2dSRobert Watson #endif 477b88ec951SJohn Baldwin error = suser(td); 478b88ec951SJohn Baldwin if (error) 4797edfb592SJohn Baldwin return (error); 480df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 481b88ec951SJohn Baldwin if (tv) { 482b88ec951SJohn Baldwin if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 4837edfb592SJohn Baldwin return (EINVAL); 484b88ec951SJohn Baldwin error = settime(td, tv); 485708e7684SPeter Wemm } 486b88ec951SJohn Baldwin if (tzp && error == 0) { 487b88ec951SJohn Baldwin tz_minuteswest = tzp->tz_minuteswest; 488b88ec951SJohn Baldwin tz_dsttime = tzp->tz_dsttime; 489b88ec951SJohn Baldwin } 4907edfb592SJohn Baldwin return (error); 491b88ec951SJohn Baldwin } 4927edfb592SJohn Baldwin 493df8bae1dSRodney W. Grimes /* 494df8bae1dSRodney W. Grimes * Get value of an interval timer. The process virtual and 495df8bae1dSRodney W. Grimes * profiling virtual time timers are kept in the p_stats area, since 496df8bae1dSRodney W. Grimes * they can be swapped out. These are kept internally in the 497df8bae1dSRodney W. Grimes * way they are specified externally: in time until they expire. 498df8bae1dSRodney W. Grimes * 499df8bae1dSRodney W. Grimes * The real time interval timer is kept in the process table slot 500df8bae1dSRodney W. Grimes * for the process, and its value (it_value) is kept as an 501df8bae1dSRodney W. Grimes * absolute time rather than as a delta, so that it is easy to keep 502df8bae1dSRodney W. Grimes * periodic real-time signals from drifting. 503df8bae1dSRodney W. Grimes * 504df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 505df8bae1dSRodney W. Grimes * kern_clock.c. The real time timer is processed by a timeout 506df8bae1dSRodney W. Grimes * routine, called from the softclock() routine. Since a callout 507df8bae1dSRodney W. Grimes * may be delayed in real time due to interrupt processing in the system, 508df8bae1dSRodney W. Grimes * it is possible for the real time timeout routine (realitexpire, given below), 509df8bae1dSRodney W. Grimes * to be delayed in real time past when it is supposed to occur. It 510df8bae1dSRodney W. Grimes * does not suffice, therefore, to reload the real timer .it_value from the 511df8bae1dSRodney W. Grimes * real time timers .it_interval. Rather, we compute the next time in 512df8bae1dSRodney W. Grimes * absolute time the timer should go off. 513df8bae1dSRodney W. Grimes */ 514d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 515df8bae1dSRodney W. Grimes struct getitimer_args { 516df8bae1dSRodney W. Grimes u_int which; 517df8bae1dSRodney W. Grimes struct itimerval *itv; 518df8bae1dSRodney W. Grimes }; 519d2d3e875SBruce Evans #endif 520fb99ab88SMatthew Dillon /* 521fb99ab88SMatthew Dillon * MPSAFE 522fb99ab88SMatthew Dillon */ 52326f9a767SRodney W. Grimes int 52491afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap) 525df8bae1dSRodney W. Grimes { 526df8bae1dSRodney W. Grimes struct itimerval aitv; 527c90110d6SJohn Baldwin int error; 528df8bae1dSRodney W. Grimes 529cfa0efe7SMaxim Sobolev error = kern_getitimer(td, uap->which, &aitv); 530cfa0efe7SMaxim Sobolev if (error != 0) 531cfa0efe7SMaxim Sobolev return (error); 532cfa0efe7SMaxim Sobolev return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 533cfa0efe7SMaxim Sobolev } 534cfa0efe7SMaxim Sobolev 535cfa0efe7SMaxim Sobolev int 536cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 537cfa0efe7SMaxim Sobolev { 538cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 539cfa0efe7SMaxim Sobolev struct timeval ctv; 540cfa0efe7SMaxim Sobolev 541cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 542df8bae1dSRodney W. Grimes return (EINVAL); 543fb99ab88SMatthew Dillon 544cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 545df8bae1dSRodney W. Grimes /* 546ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 547df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 548df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 549df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 550df8bae1dSRodney W. Grimes */ 55196d7f8efSTim J. Robbins PROC_LOCK(p); 552cfa0efe7SMaxim Sobolev *aitv = p->p_realtimer; 55396d7f8efSTim J. Robbins PROC_UNLOCK(p); 554cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 555c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 556cfa0efe7SMaxim Sobolev if (timevalcmp(&aitv->it_value, &ctv, <)) 557cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_value); 558df8bae1dSRodney W. Grimes else 559cfa0efe7SMaxim Sobolev timevalsub(&aitv->it_value, &ctv); 560227ee8a1SPoul-Henning Kamp } 561fb99ab88SMatthew Dillon } else { 56296d7f8efSTim J. Robbins mtx_lock_spin(&sched_lock); 563cfa0efe7SMaxim Sobolev *aitv = p->p_stats->p_timer[which]; 56496d7f8efSTim J. Robbins mtx_unlock_spin(&sched_lock); 565fb99ab88SMatthew Dillon } 566cfa0efe7SMaxim Sobolev return (0); 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes 569d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 570df8bae1dSRodney W. Grimes struct setitimer_args { 571df8bae1dSRodney W. Grimes u_int which; 572df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 573df8bae1dSRodney W. Grimes }; 574d2d3e875SBruce Evans #endif 575cfa0efe7SMaxim Sobolev 576fb99ab88SMatthew Dillon /* 577fb99ab88SMatthew Dillon * MPSAFE 578fb99ab88SMatthew Dillon */ 57926f9a767SRodney W. Grimes int 58091afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap) 581df8bae1dSRodney W. Grimes { 582cfa0efe7SMaxim Sobolev struct itimerval aitv, oitv; 583c90110d6SJohn Baldwin int error; 58496d7f8efSTim J. Robbins 58596d7f8efSTim J. Robbins if (uap->itv == NULL) { 58696d7f8efSTim J. Robbins uap->itv = uap->oitv; 58796d7f8efSTim J. Robbins return (getitimer(td, (struct getitimer_args *)uap)); 58896d7f8efSTim J. Robbins } 589df8bae1dSRodney W. Grimes 59096d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 591df8bae1dSRodney W. Grimes return (error); 592cfa0efe7SMaxim Sobolev error = kern_setitimer(td, uap->which, &aitv, &oitv); 593cfa0efe7SMaxim Sobolev if (error != 0 || uap->oitv == NULL) 594cfa0efe7SMaxim Sobolev return (error); 595cfa0efe7SMaxim Sobolev return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 596cfa0efe7SMaxim Sobolev } 597cfa0efe7SMaxim Sobolev 598cfa0efe7SMaxim Sobolev int 599c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 600c90110d6SJohn Baldwin struct itimerval *oitv) 601cfa0efe7SMaxim Sobolev { 602cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 603cfa0efe7SMaxim Sobolev struct timeval ctv; 604cfa0efe7SMaxim Sobolev 6055e85ac17SJohn Baldwin if (aitv == NULL) 6065e85ac17SJohn Baldwin return (kern_getitimer(td, which, oitv)); 6075e85ac17SJohn Baldwin 608cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 60996d7f8efSTim J. Robbins return (EINVAL); 610cfa0efe7SMaxim Sobolev if (itimerfix(&aitv->it_value)) 611cfa0efe7SMaxim Sobolev return (EINVAL); 612cfa0efe7SMaxim Sobolev if (!timevalisset(&aitv->it_value)) 613cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_interval); 614cfa0efe7SMaxim Sobolev else if (itimerfix(&aitv->it_interval)) 61596d7f8efSTim J. Robbins return (EINVAL); 61696d7f8efSTim J. Robbins 617cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 61896d7f8efSTim J. Robbins PROC_LOCK(p); 6194cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 6204f559836SJake Burkholder callout_stop(&p->p_itcallout); 62125b4d3a8SJohn Baldwin getmicrouptime(&ctv); 622cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 623cfa0efe7SMaxim Sobolev callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 6244f559836SJake Burkholder realitexpire, p); 625cfa0efe7SMaxim Sobolev timevaladd(&aitv->it_value, &ctv); 62625b4d3a8SJohn Baldwin } 627cfa0efe7SMaxim Sobolev *oitv = p->p_realtimer; 628cfa0efe7SMaxim Sobolev p->p_realtimer = *aitv; 62996d7f8efSTim J. Robbins PROC_UNLOCK(p); 630cfa0efe7SMaxim Sobolev if (timevalisset(&oitv->it_value)) { 631cfa0efe7SMaxim Sobolev if (timevalcmp(&oitv->it_value, &ctv, <)) 632cfa0efe7SMaxim Sobolev timevalclear(&oitv->it_value); 63396d7f8efSTim J. Robbins else 634cfa0efe7SMaxim Sobolev timevalsub(&oitv->it_value, &ctv); 635fb99ab88SMatthew Dillon } 63696d7f8efSTim J. Robbins } else { 63796d7f8efSTim J. Robbins mtx_lock_spin(&sched_lock); 638cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 639cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 64096d7f8efSTim J. Robbins mtx_unlock_spin(&sched_lock); 64196d7f8efSTim J. Robbins } 64296d7f8efSTim J. Robbins return (0); 643df8bae1dSRodney W. Grimes } 644df8bae1dSRodney W. Grimes 645df8bae1dSRodney W. Grimes /* 646df8bae1dSRodney W. Grimes * Real interval timer expired: 647df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 648df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 649df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 650df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 651df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 652c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 6539207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 6549207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 6559207f00aSBruce Evans * interrupt even when we're delayed. 656df8bae1dSRodney W. Grimes */ 657df8bae1dSRodney W. Grimes void 65891afe087SPoul-Henning Kamp realitexpire(void *arg) 659df8bae1dSRodney W. Grimes { 66091afe087SPoul-Henning Kamp struct proc *p; 661bfe6c9faSPoul-Henning Kamp struct timeval ctv, ntv; 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes p = (struct proc *)arg; 66437824023SJohn Baldwin PROC_LOCK(p); 665df8bae1dSRodney W. Grimes psignal(p, SIGALRM); 6664cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 6674cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 6685499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 6695499ea01SJohn Baldwin wakeup(&p->p_itcallout); 67037824023SJohn Baldwin PROC_UNLOCK(p); 671df8bae1dSRodney W. Grimes return; 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes for (;;) { 674df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 675df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 676c21410e1SPoul-Henning Kamp getmicrouptime(&ctv); 6774cf41af3SPoul-Henning Kamp if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 678bfe6c9faSPoul-Henning Kamp ntv = p->p_realtimer.it_value; 679bfe6c9faSPoul-Henning Kamp timevalsub(&ntv, &ctv); 6804f559836SJake Burkholder callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 6814f559836SJake Burkholder realitexpire, p); 68237824023SJohn Baldwin PROC_UNLOCK(p); 683df8bae1dSRodney W. Grimes return; 684df8bae1dSRodney W. Grimes } 685df8bae1dSRodney W. Grimes } 68637824023SJohn Baldwin /*NOTREACHED*/ 687df8bae1dSRodney W. Grimes } 688df8bae1dSRodney W. Grimes 689df8bae1dSRodney W. Grimes /* 690df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 691df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 692df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 693df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 694df8bae1dSRodney W. Grimes */ 69526f9a767SRodney W. Grimes int 69691afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 697df8bae1dSRodney W. Grimes { 698df8bae1dSRodney W. Grimes 69986857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 700df8bae1dSRodney W. Grimes return (EINVAL); 701df8bae1dSRodney W. Grimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 702df8bae1dSRodney W. Grimes tv->tv_usec = tick; 703df8bae1dSRodney W. Grimes return (0); 704df8bae1dSRodney W. Grimes } 705df8bae1dSRodney W. Grimes 706df8bae1dSRodney W. Grimes /* 707df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 708df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 709df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 710df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 711df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 712df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 713df8bae1dSRodney W. Grimes * that it is called in a context where the timers 714df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 715df8bae1dSRodney W. Grimes */ 71626f9a767SRodney W. Grimes int 71791afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 718df8bae1dSRodney W. Grimes { 719df8bae1dSRodney W. Grimes 720df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 721df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 722df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 723df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 724df8bae1dSRodney W. Grimes goto expire; 725df8bae1dSRodney W. Grimes } 726df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 727df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 728df8bae1dSRodney W. Grimes } 729df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 730df8bae1dSRodney W. Grimes usec = 0; 7314cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 732df8bae1dSRodney W. Grimes return (1); 733df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 734df8bae1dSRodney W. Grimes expire: 7354cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 736df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 737df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 738df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 739df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 740df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 741df8bae1dSRodney W. Grimes } 742df8bae1dSRodney W. Grimes } else 743df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 744df8bae1dSRodney W. Grimes return (0); 745df8bae1dSRodney W. Grimes } 746df8bae1dSRodney W. Grimes 747df8bae1dSRodney W. Grimes /* 748df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 749df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 750df8bae1dSRodney W. Grimes * results which are before the beginning, 751df8bae1dSRodney W. Grimes * it just gets very confused in this case. 752df8bae1dSRodney W. Grimes * Caveat emptor. 753df8bae1dSRodney W. Grimes */ 75426f9a767SRodney W. Grimes void 7556ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 756df8bae1dSRodney W. Grimes { 757df8bae1dSRodney W. Grimes 758df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 759df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 760df8bae1dSRodney W. Grimes timevalfix(t1); 761df8bae1dSRodney W. Grimes } 762df8bae1dSRodney W. Grimes 76326f9a767SRodney W. Grimes void 7646ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 765df8bae1dSRodney W. Grimes { 766df8bae1dSRodney W. Grimes 767df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 768df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 769df8bae1dSRodney W. Grimes timevalfix(t1); 770df8bae1dSRodney W. Grimes } 771df8bae1dSRodney W. Grimes 77287b6de2bSPoul-Henning Kamp static void 77391afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 774df8bae1dSRodney W. Grimes { 775df8bae1dSRodney W. Grimes 776df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 777df8bae1dSRodney W. Grimes t1->tv_sec--; 778df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 779df8bae1dSRodney W. Grimes } 780df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 781df8bae1dSRodney W. Grimes t1->tv_sec++; 782df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 783df8bae1dSRodney W. Grimes } 784df8bae1dSRodney W. Grimes } 78591974ce1SSam Leffler 78691974ce1SSam Leffler /* 787addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 78891974ce1SSam Leffler */ 78991974ce1SSam Leffler int 79091974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 79191974ce1SSam Leffler { 79291974ce1SSam Leffler struct timeval tv, delta; 79391974ce1SSam Leffler int rv = 0; 79491974ce1SSam Leffler 795addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 796addea9d4SSam Leffler delta = tv; 797addea9d4SSam Leffler timevalsub(&delta, lasttime); 79891974ce1SSam Leffler 79991974ce1SSam Leffler /* 80091974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 80191974ce1SSam Leffler * even if interval is huge. 80291974ce1SSam Leffler */ 80391974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 80491974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 80591974ce1SSam Leffler *lasttime = tv; 80691974ce1SSam Leffler rv = 1; 80791974ce1SSam Leffler } 80891974ce1SSam Leffler 80991974ce1SSam Leffler return (rv); 81091974ce1SSam Leffler } 81191974ce1SSam Leffler 81291974ce1SSam Leffler /* 81391974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 814addea9d4SSam Leffler * 815addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 816addea9d4SSam Leffler * should drop a packet because of the rate limitation). 817addea9d4SSam Leffler * 818893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 819893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 820893bec80SSam Leffler * limiting. 821893bec80SSam Leffler * 822addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 823addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 824addea9d4SSam Leffler * clock ticks for minimal overhead. 82591974ce1SSam Leffler */ 82691974ce1SSam Leffler int 82791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 82891974ce1SSam Leffler { 829addea9d4SSam Leffler int now; 83091974ce1SSam Leffler 83191974ce1SSam Leffler /* 832addea9d4SSam Leffler * Reset the last time and counter if this is the first call 833addea9d4SSam Leffler * or more than a second has passed since the last update of 834addea9d4SSam Leffler * lasttime. 83591974ce1SSam Leffler */ 836addea9d4SSam Leffler now = ticks; 837addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 838addea9d4SSam Leffler lasttime->tv_sec = now; 839addea9d4SSam Leffler *curpps = 1; 840893bec80SSam Leffler return (maxpps != 0); 841addea9d4SSam Leffler } else { 842addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 843addea9d4SSam Leffler return (maxpps < 0 || *curpps < maxpps); 844addea9d4SSam Leffler } 84591974ce1SSam Leffler } 84686857b36SDavid Xu 84786857b36SDavid Xu static void 84886857b36SDavid Xu itimer_start(void) 84986857b36SDavid Xu { 85086857b36SDavid Xu struct kclock rt_clock = { 85186857b36SDavid Xu .timer_create = realtimer_create, 85286857b36SDavid Xu .timer_delete = realtimer_delete, 85386857b36SDavid Xu .timer_settime = realtimer_settime, 85486857b36SDavid Xu .timer_gettime = realtimer_gettime, 85586857b36SDavid Xu .event_hook = realtimer_event_hook 85686857b36SDavid Xu }; 85786857b36SDavid Xu 85886857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 85986857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 86086857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 86186857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 86286857b36SDavid Xu } 86386857b36SDavid Xu 86486857b36SDavid Xu int 86586857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk) 86686857b36SDavid Xu { 86786857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 86886857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 86986857b36SDavid Xu return (0); 87086857b36SDavid Xu } 87186857b36SDavid Xu posix_clocks[clockid] = *clk; 87286857b36SDavid Xu return (1); 87386857b36SDavid Xu } 87486857b36SDavid Xu 87586857b36SDavid Xu static int 87686857b36SDavid Xu itimer_init(void *mem, int size, int flags) 87786857b36SDavid Xu { 87886857b36SDavid Xu struct itimer *it; 87986857b36SDavid Xu 88086857b36SDavid Xu it = (struct itimer *)mem; 88186857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 88286857b36SDavid Xu return (0); 88386857b36SDavid Xu } 88486857b36SDavid Xu 88586857b36SDavid Xu static void 88686857b36SDavid Xu itimer_fini(void *mem, int size) 88786857b36SDavid Xu { 88886857b36SDavid Xu struct itimer *it; 88986857b36SDavid Xu 89086857b36SDavid Xu it = (struct itimer *)mem; 89186857b36SDavid Xu mtx_destroy(&it->it_mtx); 89286857b36SDavid Xu } 89386857b36SDavid Xu 89486857b36SDavid Xu static void 89586857b36SDavid Xu itimer_enter(struct itimer *it) 89686857b36SDavid Xu { 89786857b36SDavid Xu 89886857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 89986857b36SDavid Xu it->it_usecount++; 90086857b36SDavid Xu } 90186857b36SDavid Xu 90286857b36SDavid Xu static void 90386857b36SDavid Xu itimer_leave(struct itimer *it) 90486857b36SDavid Xu { 90586857b36SDavid Xu 90686857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 90786857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 90886857b36SDavid Xu 90986857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 91086857b36SDavid Xu wakeup(it); 91186857b36SDavid Xu } 91286857b36SDavid Xu 91386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 91486857b36SDavid Xu struct timer_create_args { 91586857b36SDavid Xu clockid_t clock_id; 91686857b36SDavid Xu struct sigevent * evp; 91786857b36SDavid Xu timer_t * timerid; 91886857b36SDavid Xu }; 91986857b36SDavid Xu #endif 92086857b36SDavid Xu 92186857b36SDavid Xu int 92286857b36SDavid Xu timer_create(struct thread *td, struct timer_create_args *uap) 92386857b36SDavid Xu { 92486857b36SDavid Xu struct sigevent *evp1, ev; 92586857b36SDavid Xu timer_t id; 92686857b36SDavid Xu int error; 92786857b36SDavid Xu 92886857b36SDavid Xu if (uap->evp != NULL) { 92986857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 93086857b36SDavid Xu if (error != 0) 93186857b36SDavid Xu return (error); 93286857b36SDavid Xu evp1 = &ev; 93386857b36SDavid Xu } else 93486857b36SDavid Xu evp1 = NULL; 93586857b36SDavid Xu 93686857b36SDavid Xu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 93786857b36SDavid Xu 93886857b36SDavid Xu if (error == 0) { 93986857b36SDavid Xu error = copyout(&id, uap->timerid, sizeof(timer_t)); 94086857b36SDavid Xu if (error != 0) 94186857b36SDavid Xu kern_timer_delete(td, id); 94286857b36SDavid Xu } 94386857b36SDavid Xu return (error); 94486857b36SDavid Xu } 94586857b36SDavid Xu 94686857b36SDavid Xu static int 94786857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id, 94886857b36SDavid Xu struct sigevent *evp, timer_t *timerid, timer_t preset_id) 94986857b36SDavid Xu { 95086857b36SDavid Xu struct proc *p = td->td_proc; 95186857b36SDavid Xu struct itimer *it; 95286857b36SDavid Xu int id; 95386857b36SDavid Xu int error; 95486857b36SDavid Xu 95586857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 95686857b36SDavid Xu return (EINVAL); 95786857b36SDavid Xu 95886857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 95986857b36SDavid Xu return (EINVAL); 96086857b36SDavid Xu 96186857b36SDavid Xu if (evp != NULL) { 96286857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 96356c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 96456c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 96586857b36SDavid Xu return (EINVAL); 96656c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 96756c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 96886857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 96986857b36SDavid Xu return (EINVAL); 97086857b36SDavid Xu } 97186857b36SDavid Xu 97260354683SDavid Xu if (p->p_itimers == NULL) 97386857b36SDavid Xu itimers_alloc(p); 97486857b36SDavid Xu 97586857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 97686857b36SDavid Xu it->it_flags = 0; 97786857b36SDavid Xu it->it_usecount = 0; 97886857b36SDavid Xu it->it_active = 0; 97956c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 98056c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 98186857b36SDavid Xu it->it_overrun = 0; 98286857b36SDavid Xu it->it_overrun_last = 0; 98386857b36SDavid Xu it->it_clockid = clock_id; 98486857b36SDavid Xu it->it_timerid = -1; 98586857b36SDavid Xu it->it_proc = p; 98686857b36SDavid Xu ksiginfo_init(&it->it_ksi); 98786857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 98886857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 98986857b36SDavid Xu if (error != 0) 99086857b36SDavid Xu goto out; 99186857b36SDavid Xu 99286857b36SDavid Xu PROC_LOCK(p); 99386857b36SDavid Xu if (preset_id != -1) { 99486857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 99586857b36SDavid Xu id = preset_id; 99660354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 99786857b36SDavid Xu PROC_UNLOCK(p); 99886857b36SDavid Xu error = 0; 99986857b36SDavid Xu goto out; 100086857b36SDavid Xu } 100186857b36SDavid Xu } else { 100286857b36SDavid Xu /* 100386857b36SDavid Xu * Find a free timer slot, skipping those reserved 100486857b36SDavid Xu * for setitimer(). 100586857b36SDavid Xu */ 100686857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 100760354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 100886857b36SDavid Xu break; 100986857b36SDavid Xu if (id == TIMER_MAX) { 101086857b36SDavid Xu PROC_UNLOCK(p); 101186857b36SDavid Xu error = EAGAIN; 101286857b36SDavid Xu goto out; 101386857b36SDavid Xu } 101486857b36SDavid Xu } 101586857b36SDavid Xu it->it_timerid = id; 101660354683SDavid Xu p->p_itimers->its_timers[id] = it; 101786857b36SDavid Xu if (evp != NULL) 101886857b36SDavid Xu it->it_sigev = *evp; 101986857b36SDavid Xu else { 102086857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 102186857b36SDavid Xu switch (clock_id) { 102286857b36SDavid Xu default: 102386857b36SDavid Xu case CLOCK_REALTIME: 102486857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 102586857b36SDavid Xu break; 102686857b36SDavid Xu case CLOCK_VIRTUAL: 102786857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 102886857b36SDavid Xu break; 102986857b36SDavid Xu case CLOCK_PROF: 103086857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 103186857b36SDavid Xu break; 103286857b36SDavid Xu } 10338f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 103486857b36SDavid Xu } 103586857b36SDavid Xu 103656c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 103756c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 103886857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 103986857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 104086857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 104186857b36SDavid Xu it->it_ksi.ksi_timerid = id; 104286857b36SDavid Xu } 104386857b36SDavid Xu PROC_UNLOCK(p); 104486857b36SDavid Xu *timerid = id; 104586857b36SDavid Xu return (0); 104686857b36SDavid Xu 104786857b36SDavid Xu out: 104886857b36SDavid Xu ITIMER_LOCK(it); 104986857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 105086857b36SDavid Xu ITIMER_UNLOCK(it); 105186857b36SDavid Xu uma_zfree(itimer_zone, it); 105286857b36SDavid Xu return (error); 105386857b36SDavid Xu } 105486857b36SDavid Xu 105586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 105686857b36SDavid Xu struct timer_delete_args { 105786857b36SDavid Xu timer_t timerid; 105886857b36SDavid Xu }; 105986857b36SDavid Xu #endif 106086857b36SDavid Xu 106186857b36SDavid Xu int 106286857b36SDavid Xu timer_delete(struct thread *td, struct timer_delete_args *uap) 106386857b36SDavid Xu { 106486857b36SDavid Xu return (kern_timer_delete(td, uap->timerid)); 106586857b36SDavid Xu } 106686857b36SDavid Xu 106786857b36SDavid Xu static struct itimer * 106886857b36SDavid Xu itimer_find(struct proc *p, timer_t timerid, int include_deleting) 106986857b36SDavid Xu { 107086857b36SDavid Xu struct itimer *it; 107186857b36SDavid Xu 107286857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 107360354683SDavid Xu if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) || 107460354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 107586857b36SDavid Xu return (NULL); 107686857b36SDavid Xu } 107786857b36SDavid Xu ITIMER_LOCK(it); 107886857b36SDavid Xu if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) { 107986857b36SDavid Xu ITIMER_UNLOCK(it); 108086857b36SDavid Xu it = NULL; 108186857b36SDavid Xu } 108286857b36SDavid Xu return (it); 108386857b36SDavid Xu } 108486857b36SDavid Xu 108586857b36SDavid Xu static int 108686857b36SDavid Xu kern_timer_delete(struct thread *td, timer_t timerid) 108786857b36SDavid Xu { 108886857b36SDavid Xu struct proc *p = td->td_proc; 108986857b36SDavid Xu struct itimer *it; 109086857b36SDavid Xu 109186857b36SDavid Xu PROC_LOCK(p); 109286857b36SDavid Xu it = itimer_find(p, timerid, 0); 109386857b36SDavid Xu if (it == NULL) { 109486857b36SDavid Xu PROC_UNLOCK(p); 109586857b36SDavid Xu return (EINVAL); 109686857b36SDavid Xu } 109786857b36SDavid Xu PROC_UNLOCK(p); 109886857b36SDavid Xu 109986857b36SDavid Xu it->it_flags |= ITF_DELETING; 110086857b36SDavid Xu while (it->it_usecount > 0) { 110186857b36SDavid Xu it->it_flags |= ITF_WANTED; 110286857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 110386857b36SDavid Xu } 110486857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 110586857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 110686857b36SDavid Xu ITIMER_UNLOCK(it); 110786857b36SDavid Xu 110886857b36SDavid Xu PROC_LOCK(p); 110986857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 111086857b36SDavid Xu sigqueue_take(&it->it_ksi); 111160354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 111286857b36SDavid Xu PROC_UNLOCK(p); 111386857b36SDavid Xu uma_zfree(itimer_zone, it); 111486857b36SDavid Xu return (0); 111586857b36SDavid Xu } 111686857b36SDavid Xu 111786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 111886857b36SDavid Xu struct timer_settime_args { 111986857b36SDavid Xu timer_t timerid; 112086857b36SDavid Xu int flags; 112186857b36SDavid Xu const struct itimerspec * value; 112286857b36SDavid Xu struct itimerspec * ovalue; 112386857b36SDavid Xu }; 112486857b36SDavid Xu #endif 112586857b36SDavid Xu 112686857b36SDavid Xu int 112786857b36SDavid Xu timer_settime(struct thread *td, struct timer_settime_args *uap) 112886857b36SDavid Xu { 112986857b36SDavid Xu struct proc *p = td->td_proc; 113086857b36SDavid Xu struct itimer *it; 113186857b36SDavid Xu struct itimerspec val, oval, *ovalp; 113286857b36SDavid Xu int error; 113386857b36SDavid Xu 113486857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 113586857b36SDavid Xu if (error != 0) 113686857b36SDavid Xu return (error); 113786857b36SDavid Xu 113886857b36SDavid Xu if (uap->ovalue != NULL) 113986857b36SDavid Xu ovalp = &oval; 114086857b36SDavid Xu else 114186857b36SDavid Xu ovalp = NULL; 114286857b36SDavid Xu 114386857b36SDavid Xu PROC_LOCK(p); 114486857b36SDavid Xu if (uap->timerid < 3 || 114586857b36SDavid Xu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 114686857b36SDavid Xu PROC_UNLOCK(p); 114786857b36SDavid Xu error = EINVAL; 114886857b36SDavid Xu } else { 114986857b36SDavid Xu PROC_UNLOCK(p); 115086857b36SDavid Xu itimer_enter(it); 115186857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_settime, 115286857b36SDavid Xu (it, uap->flags, &val, ovalp)); 115386857b36SDavid Xu itimer_leave(it); 115486857b36SDavid Xu ITIMER_UNLOCK(it); 115586857b36SDavid Xu } 115686857b36SDavid Xu if (error == 0 && uap->ovalue != NULL) 115786857b36SDavid Xu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 115886857b36SDavid Xu return (error); 115986857b36SDavid Xu } 116086857b36SDavid Xu 116186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 116286857b36SDavid Xu struct timer_gettime_args { 116386857b36SDavid Xu timer_t timerid; 116486857b36SDavid Xu struct itimerspec * value; 116586857b36SDavid Xu }; 116686857b36SDavid Xu #endif 116786857b36SDavid Xu 116886857b36SDavid Xu int 116986857b36SDavid Xu timer_gettime(struct thread *td, struct timer_gettime_args *uap) 117086857b36SDavid Xu { 117186857b36SDavid Xu struct proc *p = td->td_proc; 117286857b36SDavid Xu struct itimer *it; 117386857b36SDavid Xu struct itimerspec val; 117486857b36SDavid Xu int error; 117586857b36SDavid Xu 117686857b36SDavid Xu PROC_LOCK(p); 117786857b36SDavid Xu if (uap->timerid < 3 || 117886857b36SDavid Xu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 117986857b36SDavid Xu PROC_UNLOCK(p); 118086857b36SDavid Xu error = EINVAL; 118186857b36SDavid Xu } else { 118286857b36SDavid Xu PROC_UNLOCK(p); 118386857b36SDavid Xu itimer_enter(it); 118486857b36SDavid Xu error = CLOCK_CALL(it->it_clockid, timer_gettime, 118586857b36SDavid Xu (it, &val)); 118686857b36SDavid Xu itimer_leave(it); 118786857b36SDavid Xu ITIMER_UNLOCK(it); 118886857b36SDavid Xu } 118986857b36SDavid Xu if (error == 0) 119086857b36SDavid Xu error = copyout(&val, uap->value, sizeof(val)); 119186857b36SDavid Xu return (error); 119286857b36SDavid Xu } 119386857b36SDavid Xu 119486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 119586857b36SDavid Xu struct timer_getoverrun_args { 119686857b36SDavid Xu timer_t timerid; 119786857b36SDavid Xu }; 119886857b36SDavid Xu #endif 119986857b36SDavid Xu 120086857b36SDavid Xu int 120186857b36SDavid Xu timer_getoverrun(struct thread *td, struct timer_getoverrun_args *uap) 120286857b36SDavid Xu { 120386857b36SDavid Xu struct proc *p = td->td_proc; 120486857b36SDavid Xu struct itimer *it; 120586857b36SDavid Xu int error ; 120686857b36SDavid Xu 120786857b36SDavid Xu PROC_LOCK(p); 120886857b36SDavid Xu if (uap->timerid < 3 || 120986857b36SDavid Xu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 121086857b36SDavid Xu PROC_UNLOCK(p); 121186857b36SDavid Xu error = EINVAL; 121286857b36SDavid Xu } else { 121386857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 121486857b36SDavid Xu ITIMER_UNLOCK(it); 121556c06c4bSDavid Xu PROC_UNLOCK(p); 121686857b36SDavid Xu error = 0; 121786857b36SDavid Xu } 121886857b36SDavid Xu return (error); 121986857b36SDavid Xu } 122086857b36SDavid Xu 122186857b36SDavid Xu static int 122286857b36SDavid Xu realtimer_create(struct itimer *it) 122386857b36SDavid Xu { 122486857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 122586857b36SDavid Xu return (0); 122686857b36SDavid Xu } 122786857b36SDavid Xu 122886857b36SDavid Xu static int 122986857b36SDavid Xu realtimer_delete(struct itimer *it) 123086857b36SDavid Xu { 123186857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 123286857b36SDavid Xu callout_stop(&it->it_callout); 123386857b36SDavid Xu return (0); 123486857b36SDavid Xu } 123586857b36SDavid Xu 123686857b36SDavid Xu static int 123786857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 123886857b36SDavid Xu { 123956c06c4bSDavid Xu struct timespec cts; 124086857b36SDavid Xu 124186857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 124286857b36SDavid Xu 124356c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 124456c06c4bSDavid Xu *ovalue = it->it_time; 124586857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 124656c06c4bSDavid Xu timespecsub(&ovalue->it_value, &cts); 124786857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 124886857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 124986857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 125086857b36SDavid Xu ovalue->it_value.tv_sec = 0; 125186857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 125286857b36SDavid Xu } 125386857b36SDavid Xu } 125486857b36SDavid Xu return (0); 125586857b36SDavid Xu } 125686857b36SDavid Xu 125786857b36SDavid Xu static int 125886857b36SDavid Xu realtimer_settime(struct itimer *it, int flags, 125986857b36SDavid Xu struct itimerspec *value, struct itimerspec *ovalue) 126086857b36SDavid Xu { 126156c06c4bSDavid Xu struct timespec cts, ts; 126256c06c4bSDavid Xu struct timeval tv; 126356c06c4bSDavid Xu struct itimerspec val; 126486857b36SDavid Xu 126586857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 126686857b36SDavid Xu 126756c06c4bSDavid Xu val = *value; 126856c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 126986857b36SDavid Xu return (EINVAL); 127086857b36SDavid Xu 127156c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 127256c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 127386857b36SDavid Xu return (EINVAL); 127486857b36SDavid Xu } else { 127556c06c4bSDavid Xu timespecclear(&val.it_interval); 127686857b36SDavid Xu } 127786857b36SDavid Xu 127886857b36SDavid Xu if (ovalue != NULL) 127986857b36SDavid Xu realtimer_gettime(it, ovalue); 128086857b36SDavid Xu 128186857b36SDavid Xu it->it_time = val; 128256c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 128356c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 128456c06c4bSDavid Xu ts = val.it_value; 128586857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 128686857b36SDavid Xu /* Convert to absolute time. */ 128756c06c4bSDavid Xu timespecadd(&it->it_time.it_value, &cts); 128886857b36SDavid Xu } else { 128956c06c4bSDavid Xu timespecsub(&ts, &cts); 129086857b36SDavid Xu /* 129156c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 129286857b36SDavid Xu * fix it. 129386857b36SDavid Xu */ 129486857b36SDavid Xu } 129556c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 129656c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 129786857b36SDavid Xu realtimer_expire, it); 129886857b36SDavid Xu } else { 129986857b36SDavid Xu callout_stop(&it->it_callout); 130086857b36SDavid Xu } 130186857b36SDavid Xu 130286857b36SDavid Xu return (0); 130386857b36SDavid Xu } 130486857b36SDavid Xu 130586857b36SDavid Xu static void 130656c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 130786857b36SDavid Xu { 130886857b36SDavid Xu if (id == CLOCK_REALTIME) 130956c06c4bSDavid Xu getnanotime(ts); 131086857b36SDavid Xu else /* CLOCK_MONOTONIC */ 131156c06c4bSDavid Xu getnanouptime(ts); 131256c06c4bSDavid Xu } 131356c06c4bSDavid Xu 131456c06c4bSDavid Xu int 131556c06c4bSDavid Xu itimer_accept(struct proc *p, timer_t timerid, ksiginfo_t *ksi) 131656c06c4bSDavid Xu { 131756c06c4bSDavid Xu struct itimer *it; 131856c06c4bSDavid Xu 131956c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 132056c06c4bSDavid Xu it = itimer_find(p, timerid, 0); 132156c06c4bSDavid Xu if (it != NULL) { 132256c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 132356c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 132456c06c4bSDavid Xu it->it_overrun = 0; 132556c06c4bSDavid Xu ITIMER_UNLOCK(it); 132656c06c4bSDavid Xu return (0); 132756c06c4bSDavid Xu } 132856c06c4bSDavid Xu return (EINVAL); 132956c06c4bSDavid Xu } 133056c06c4bSDavid Xu 133156c06c4bSDavid Xu int 133256c06c4bSDavid Xu itimespecfix(struct timespec *ts) 133356c06c4bSDavid Xu { 133456c06c4bSDavid Xu 133556c06c4bSDavid Xu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 133656c06c4bSDavid Xu return (EINVAL); 133756c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 133856c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 133956c06c4bSDavid Xu return (0); 134086857b36SDavid Xu } 134186857b36SDavid Xu 134286857b36SDavid Xu static void 134386857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event) 134486857b36SDavid Xu { 134586857b36SDavid Xu struct itimers *its; 134686857b36SDavid Xu struct itimer *it; 134786857b36SDavid Xu int i; 134886857b36SDavid Xu 134986857b36SDavid Xu /* 135086857b36SDavid Xu * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX 135186857b36SDavid Xu * specification, it should be inherited by new process image. 135286857b36SDavid Xu */ 135386857b36SDavid Xu if (event == ITIMER_EV_EXEC) 135486857b36SDavid Xu i = 1; 135586857b36SDavid Xu else 135686857b36SDavid Xu i = 0; 135760354683SDavid Xu its = p->p_itimers; 135886857b36SDavid Xu for (; i < TIMER_MAX; i++) { 135986857b36SDavid Xu if ((it = its->its_timers[i]) != NULL && 136086857b36SDavid Xu it->it_clockid == clock_id) { 136186857b36SDavid Xu ITIMER_LOCK(it); 136286857b36SDavid Xu callout_stop(&it->it_callout); 136386857b36SDavid Xu ITIMER_UNLOCK(it); 136486857b36SDavid Xu } 136586857b36SDavid Xu } 136686857b36SDavid Xu } 136786857b36SDavid Xu 136886857b36SDavid Xu /* Timeout callback for realtime timer */ 136986857b36SDavid Xu static void 137086857b36SDavid Xu realtimer_expire(void *arg) 137186857b36SDavid Xu { 137256c06c4bSDavid Xu struct timespec cts, ts; 137356c06c4bSDavid Xu struct timeval tv; 137486857b36SDavid Xu struct itimer *it; 137586857b36SDavid Xu struct proc *p; 137686857b36SDavid Xu 137786857b36SDavid Xu it = (struct itimer *)arg; 137886857b36SDavid Xu p = it->it_proc; 137986857b36SDavid Xu 138056c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 138186857b36SDavid Xu /* Only fire if time is reached. */ 138256c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 138356c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 138456c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 138586857b36SDavid Xu &it->it_time.it_interval); 138656c06c4bSDavid Xu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 138786857b36SDavid Xu it->it_overrun++; 138856c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 138986857b36SDavid Xu &it->it_time.it_interval); 139086857b36SDavid Xu } 139186857b36SDavid Xu } else { 139286857b36SDavid Xu /* single shot timer ? */ 139356c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 139486857b36SDavid Xu } 139556c06c4bSDavid Xu if (timespecisset(&it->it_time.it_value)) { 139656c06c4bSDavid Xu ts = it->it_time.it_value; 139756c06c4bSDavid Xu timespecsub(&ts, &cts); 139856c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 139956c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 140086857b36SDavid Xu realtimer_expire, it); 140186857b36SDavid Xu } 140286857b36SDavid Xu ITIMER_UNLOCK(it); 140386857b36SDavid Xu itimer_fire(it); 140486857b36SDavid Xu ITIMER_LOCK(it); 140556c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 140656c06c4bSDavid Xu ts = it->it_time.it_value; 140756c06c4bSDavid Xu timespecsub(&ts, &cts); 140856c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 140956c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 141086857b36SDavid Xu it); 141186857b36SDavid Xu } 141286857b36SDavid Xu } 141386857b36SDavid Xu 141486857b36SDavid Xu void 141586857b36SDavid Xu itimer_fire(struct itimer *it) 141686857b36SDavid Xu { 141786857b36SDavid Xu struct proc *p = it->it_proc; 14186d7b314bSDavid Xu int ret; 141986857b36SDavid Xu 142056c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 142156c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 142286857b36SDavid Xu PROC_LOCK(p); 14236d7b314bSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 14246d7b314bSDavid Xu ret = psignal_event(p, &it->it_sigev, &it->it_ksi); 14256d7b314bSDavid Xu if (__predict_false(ret != 0)) { 142686857b36SDavid Xu it->it_overrun++; 142756c06c4bSDavid Xu /* 142856c06c4bSDavid Xu * Broken userland code, thread went 142956c06c4bSDavid Xu * away, disarm the timer. 143056c06c4bSDavid Xu */ 14316d7b314bSDavid Xu if (ret == ESRCH) { 143256c06c4bSDavid Xu ITIMER_LOCK(it); 143356c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 143456c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 143556c06c4bSDavid Xu callout_stop(&it->it_callout); 143656c06c4bSDavid Xu ITIMER_UNLOCK(it); 14376d7b314bSDavid Xu } 143856c06c4bSDavid Xu } 143956c06c4bSDavid Xu } else { 14406d7b314bSDavid Xu it->it_overrun++; 144156c06c4bSDavid Xu } 144286857b36SDavid Xu PROC_UNLOCK(p); 144386857b36SDavid Xu } 144486857b36SDavid Xu } 144586857b36SDavid Xu 144686857b36SDavid Xu static void 144786857b36SDavid Xu itimers_alloc(struct proc *p) 144886857b36SDavid Xu { 144960354683SDavid Xu struct itimers *its; 145060354683SDavid Xu int i; 145186857b36SDavid Xu 145260354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 145386857b36SDavid Xu LIST_INIT(&its->its_virtual); 145486857b36SDavid Xu LIST_INIT(&its->its_prof); 145586857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 145660354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 145760354683SDavid Xu its->its_timers[i] = NULL; 145860354683SDavid Xu PROC_LOCK(p); 145960354683SDavid Xu if (p->p_itimers == NULL) { 146060354683SDavid Xu p->p_itimers = its; 146160354683SDavid Xu PROC_UNLOCK(p); 146260354683SDavid Xu } 146360354683SDavid Xu else { 146460354683SDavid Xu PROC_UNLOCK(p); 146560354683SDavid Xu free(its, M_SUBPROC); 146660354683SDavid Xu } 146786857b36SDavid Xu } 146886857b36SDavid Xu 146986857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 147086857b36SDavid Xu void 147186857b36SDavid Xu itimers_event_hook(struct proc *p, int event) 147286857b36SDavid Xu { 147386857b36SDavid Xu struct itimers *its; 147486857b36SDavid Xu struct itimer *it; 147586857b36SDavid Xu int i; 147686857b36SDavid Xu 147760354683SDavid Xu if (p->p_itimers != NULL) { 147860354683SDavid Xu its = p->p_itimers; 147986857b36SDavid Xu for (i = 0; i < MAX_CLOCKS; ++i) { 148086857b36SDavid Xu if (posix_clocks[i].event_hook != NULL) 148186857b36SDavid Xu CLOCK_CALL(i, event_hook, (p, i, event)); 148286857b36SDavid Xu } 148386857b36SDavid Xu /* 148486857b36SDavid Xu * According to susv3, XSI interval timers should be inherited 148586857b36SDavid Xu * by new image. 148686857b36SDavid Xu */ 148786857b36SDavid Xu if (event == ITIMER_EV_EXEC) 148886857b36SDavid Xu i = 3; 148986857b36SDavid Xu else if (event == ITIMER_EV_EXIT) 149086857b36SDavid Xu i = 0; 149186857b36SDavid Xu else 149286857b36SDavid Xu panic("unhandled event"); 149386857b36SDavid Xu for (; i < TIMER_MAX; ++i) { 149486857b36SDavid Xu if ((it = its->its_timers[i]) != NULL) { 149586857b36SDavid Xu PROC_LOCK(p); 149686857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 149786857b36SDavid Xu sigqueue_take(&it->it_ksi); 149886857b36SDavid Xu PROC_UNLOCK(p); 149986857b36SDavid Xu uma_zfree(itimer_zone, its->its_timers[i]); 150086857b36SDavid Xu its->its_timers[i] = NULL; 150186857b36SDavid Xu } 150286857b36SDavid Xu } 150386857b36SDavid Xu if (its->its_timers[0] == NULL && 150486857b36SDavid Xu its->its_timers[1] == NULL && 150586857b36SDavid Xu its->its_timers[2] == NULL) { 150660354683SDavid Xu free(its, M_SUBPROC); 150760354683SDavid Xu p->p_itimers = NULL; 150886857b36SDavid Xu } 150986857b36SDavid Xu } 151086857b36SDavid Xu } 1511