19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1569a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)kern_time.c 8.1 (Berkeley) 6/10/93 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 37de56aee0SKonstantin Belousov #include "opt_ktrace.h" 38de56aee0SKonstantin Belousov 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40e96c1fdcSPeter Wemm #include <sys/systm.h> 41aff5bcb1SDavid Xu #include <sys/limits.h> 42f645b0b5SPoul-Henning Kamp #include <sys/clock.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44fb919e4dSMark Murray #include <sys/mutex.h> 45d2d3e875SBruce Evans #include <sys/sysproto.h> 46df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 47797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 48df8bae1dSRodney W. Grimes #include <sys/kernel.h> 49098176f0SDavide Italiano #include <sys/sleepqueue.h> 50efa42cbcSPaul Saab #include <sys/syscallsubr.h> 5177e718f7SDavid Xu #include <sys/sysctl.h> 5294c8fcd8SPeter Wemm #include <sys/sysent.h> 53acd3428bSRobert Watson #include <sys/priv.h> 54df8bae1dSRodney W. Grimes #include <sys/proc.h> 556aeb05d7STom Rhodes #include <sys/posix4.h> 56708e7684SPeter Wemm #include <sys/time.h> 5786857b36SDavid Xu #include <sys/timers.h> 5891266b96SPoul-Henning Kamp #include <sys/timetc.h> 59df8bae1dSRodney W. Grimes #include <sys/vnode.h> 60de56aee0SKonstantin Belousov #ifdef KTRACE 61de56aee0SKonstantin Belousov #include <sys/ktrace.h> 62de56aee0SKonstantin Belousov #endif 63fb919e4dSMark Murray 645b870b7bSPeter Wemm #include <vm/vm.h> 655b870b7bSPeter Wemm #include <vm/vm_extern.h> 66df8bae1dSRodney W. Grimes 6786857b36SDavid Xu #define MAX_CLOCKS (CLOCK_MONOTONIC+1) 68d65f1abcSDavid Xu #define CPUCLOCK_BIT 0x80000000 69d65f1abcSDavid Xu #define CPUCLOCK_PROCESS_BIT 0x40000000 70d65f1abcSDavid Xu #define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT)) 71d65f1abcSDavid Xu #define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid)) 72d65f1abcSDavid Xu #define MAKE_PROCESS_CPUCLOCK(pid) \ 73d65f1abcSDavid Xu (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid)) 7486857b36SDavid Xu 7586857b36SDavid Xu static struct kclock posix_clocks[MAX_CLOCKS]; 7686857b36SDavid Xu static uma_zone_t itimer_zone = NULL; 7786857b36SDavid Xu 78df8bae1dSRodney W. Grimes /* 79df8bae1dSRodney W. Grimes * Time of day and interval timer support. 80df8bae1dSRodney W. Grimes * 81df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 82df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 83df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 84df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 85df8bae1dSRodney W. Grimes * timers when they expire. 86df8bae1dSRodney W. Grimes */ 87df8bae1dSRodney W. Grimes 887edfb592SJohn Baldwin static int settime(struct thread *, struct timeval *); 894d77a549SAlfred Perlstein static void timevalfix(struct timeval *); 903f8455b0SEric van Gyzen static int user_clock_nanosleep(struct thread *td, clockid_t clock_id, 913f8455b0SEric van Gyzen int flags, const struct timespec *ua_rqtp, 923f8455b0SEric van Gyzen struct timespec *ua_rmtp); 9394c8fcd8SPeter Wemm 9486857b36SDavid Xu static void itimer_start(void); 9586857b36SDavid Xu static int itimer_init(void *, int, int); 9686857b36SDavid Xu static void itimer_fini(void *, int); 9786857b36SDavid Xu static void itimer_enter(struct itimer *); 9886857b36SDavid Xu static void itimer_leave(struct itimer *); 99843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int); 10086857b36SDavid Xu static void itimers_alloc(struct proc *); 10186857b36SDavid Xu static int realtimer_create(struct itimer *); 10286857b36SDavid Xu static int realtimer_gettime(struct itimer *, struct itimerspec *); 10386857b36SDavid Xu static int realtimer_settime(struct itimer *, int, 10486857b36SDavid Xu struct itimerspec *, struct itimerspec *); 10586857b36SDavid Xu static int realtimer_delete(struct itimer *); 10656c06c4bSDavid Xu static void realtimer_clocktime(clockid_t, struct timespec *); 10786857b36SDavid Xu static void realtimer_expire(void *); 10886857b36SDavid Xu 109e68c6191SKonstantin Belousov static int register_posix_clock(int, const struct kclock *); 1108ff2b41cSMark Johnston static void itimer_fire(struct itimer *it); 1118ff2b41cSMark Johnston static int itimespecfix(struct timespec *ts); 11286857b36SDavid Xu 11386857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist) \ 11486857b36SDavid Xu ((*posix_clocks[clock].call) arglist) 11586857b36SDavid Xu 11686857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 11786857b36SDavid Xu 11894c8fcd8SPeter Wemm static int 11991afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 12094c8fcd8SPeter Wemm { 121fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 122c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 1237ec73f64SPoul-Henning Kamp struct timespec ts; 12494c8fcd8SPeter Wemm 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 { 1541822421cSKonstantin Belousov if (tv1.tv_sec == laststep.tv_sec) 155c0bd94a7SNick Sayer return (EPERM); 156c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 157c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 158c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 159c0bd94a7SNick Sayer } 160c0bd94a7SNick Sayer laststep = *tv; 161fcae3aa6SNick Sayer } 1629c8fff87SBruce Evans } 1639c8fff87SBruce Evans 1647ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1657ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 16691266b96SPoul-Henning Kamp tc_setclock(&ts); 16794c8fcd8SPeter Wemm resettodr(); 16894c8fcd8SPeter Wemm return (0); 16994c8fcd8SPeter Wemm } 17094c8fcd8SPeter Wemm 17194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 172d65f1abcSDavid Xu struct clock_getcpuclockid2_args { 173d65f1abcSDavid Xu id_t id; 174d65f1abcSDavid Xu int which, 175d65f1abcSDavid Xu clockid_t *clock_id; 176d65f1abcSDavid Xu }; 177d65f1abcSDavid Xu #endif 178d65f1abcSDavid Xu /* ARGSUSED */ 179d65f1abcSDavid Xu int 180d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap) 181d65f1abcSDavid Xu { 182d65f1abcSDavid Xu clockid_t clk_id; 183d31e4b3aSKonstantin Belousov int error; 184d31e4b3aSKonstantin Belousov 185d31e4b3aSKonstantin Belousov error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id); 186d31e4b3aSKonstantin Belousov if (error == 0) 187d31e4b3aSKonstantin Belousov error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 188d31e4b3aSKonstantin Belousov return (error); 189d31e4b3aSKonstantin Belousov } 190d31e4b3aSKonstantin Belousov 191d31e4b3aSKonstantin Belousov int 192d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which, 193d31e4b3aSKonstantin Belousov clockid_t *clk_id) 194d31e4b3aSKonstantin Belousov { 195d65f1abcSDavid Xu struct proc *p; 196d65f1abcSDavid Xu pid_t pid; 197d65f1abcSDavid Xu lwpid_t tid; 198d65f1abcSDavid Xu int error; 199d65f1abcSDavid Xu 200d31e4b3aSKonstantin Belousov switch (which) { 201d65f1abcSDavid Xu case CPUCLOCK_WHICH_PID: 202d31e4b3aSKonstantin Belousov if (id != 0) { 20347ce3e53SDmitry Chagin error = pget(id, PGET_CANSEE | PGET_NOTID, &p); 204d31e4b3aSKonstantin Belousov if (error != 0) 205d65f1abcSDavid Xu return (error); 20647ce3e53SDmitry Chagin PROC_UNLOCK(p); 207d31e4b3aSKonstantin Belousov pid = id; 208d65f1abcSDavid Xu } else { 209d65f1abcSDavid Xu pid = td->td_proc->p_pid; 210d65f1abcSDavid Xu } 211d31e4b3aSKonstantin Belousov *clk_id = MAKE_PROCESS_CPUCLOCK(pid); 212d31e4b3aSKonstantin Belousov return (0); 213d65f1abcSDavid Xu case CPUCLOCK_WHICH_TID: 214d31e4b3aSKonstantin Belousov tid = id == 0 ? td->td_tid : id; 215d31e4b3aSKonstantin Belousov *clk_id = MAKE_THREAD_CPUCLOCK(tid); 216d31e4b3aSKonstantin Belousov return (0); 217d65f1abcSDavid Xu default: 218d65f1abcSDavid Xu return (EINVAL); 219d65f1abcSDavid Xu } 220d65f1abcSDavid Xu } 221d65f1abcSDavid Xu 222d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_ 22394c8fcd8SPeter Wemm struct clock_gettime_args { 22494c8fcd8SPeter Wemm clockid_t clock_id; 22594c8fcd8SPeter Wemm struct timespec *tp; 22694c8fcd8SPeter Wemm }; 22794c8fcd8SPeter Wemm #endif 22894c8fcd8SPeter Wemm /* ARGSUSED */ 22994c8fcd8SPeter Wemm int 2308451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) 23194c8fcd8SPeter Wemm { 23294c8fcd8SPeter Wemm struct timespec ats; 233f0b479cdSPaul Saab int error; 234f0b479cdSPaul Saab 235f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 236f0b479cdSPaul Saab if (error == 0) 237f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 238f0b479cdSPaul Saab 239f0b479cdSPaul Saab return (error); 240f0b479cdSPaul Saab } 241f0b479cdSPaul Saab 242d65f1abcSDavid Xu static inline void 243d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats) 244d65f1abcSDavid Xu { 245d65f1abcSDavid Xu runtime = cputick2usec(runtime); 246d65f1abcSDavid Xu ats->tv_sec = runtime / 1000000; 247d65f1abcSDavid Xu ats->tv_nsec = runtime % 1000000 * 1000; 248d65f1abcSDavid Xu } 249d65f1abcSDavid Xu 250cbc10891SDmitry Chagin void 251cbc10891SDmitry Chagin kern_thread_cputime(struct thread *targettd, struct timespec *ats) 252d65f1abcSDavid Xu { 253d65f1abcSDavid Xu uint64_t runtime, curtime, switchtime; 254d65f1abcSDavid Xu 255d65f1abcSDavid Xu if (targettd == NULL) { /* current thread */ 256d65f1abcSDavid Xu critical_enter(); 257d65f1abcSDavid Xu switchtime = PCPU_GET(switchtime); 258d65f1abcSDavid Xu curtime = cpu_ticks(); 259d65f1abcSDavid Xu runtime = curthread->td_runtime; 260d65f1abcSDavid Xu critical_exit(); 261d65f1abcSDavid Xu runtime += curtime - switchtime; 262d65f1abcSDavid Xu } else { 2630783b709SKonstantin Belousov PROC_LOCK_ASSERT(targettd->td_proc, MA_OWNED); 264d65f1abcSDavid Xu thread_lock(targettd); 265d65f1abcSDavid Xu runtime = targettd->td_runtime; 266d65f1abcSDavid Xu thread_unlock(targettd); 267d65f1abcSDavid Xu } 268d65f1abcSDavid Xu cputick2timespec(runtime, ats); 269d65f1abcSDavid Xu } 270d65f1abcSDavid Xu 271cbc10891SDmitry Chagin void 272cbc10891SDmitry Chagin kern_process_cputime(struct proc *targetp, struct timespec *ats) 273d65f1abcSDavid Xu { 274d65f1abcSDavid Xu uint64_t runtime; 275d65f1abcSDavid Xu struct rusage ru; 276d65f1abcSDavid Xu 277cbc10891SDmitry Chagin PROC_LOCK_ASSERT(targetp, MA_OWNED); 2785c7bebf9SKonstantin Belousov PROC_STATLOCK(targetp); 279d65f1abcSDavid Xu rufetch(targetp, &ru); 280d65f1abcSDavid Xu runtime = targetp->p_rux.rux_runtime; 2817e8db781SColin Percival if (curthread->td_proc == targetp) 2827e8db781SColin Percival runtime += cpu_ticks() - PCPU_GET(switchtime); 2835c7bebf9SKonstantin Belousov PROC_STATUNLOCK(targetp); 284d65f1abcSDavid Xu cputick2timespec(runtime, ats); 285d65f1abcSDavid Xu } 286d65f1abcSDavid Xu 287d65f1abcSDavid Xu static int 288d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) 289d65f1abcSDavid Xu { 290d65f1abcSDavid Xu struct proc *p, *p2; 291d65f1abcSDavid Xu struct thread *td2; 292d65f1abcSDavid Xu lwpid_t tid; 293d65f1abcSDavid Xu pid_t pid; 294d65f1abcSDavid Xu int error; 295d65f1abcSDavid Xu 296d65f1abcSDavid Xu p = td->td_proc; 297d65f1abcSDavid Xu if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { 298d65f1abcSDavid Xu tid = clock_id & CPUCLOCK_ID_MASK; 299d65f1abcSDavid Xu td2 = tdfind(tid, p->p_pid); 300d65f1abcSDavid Xu if (td2 == NULL) 301d65f1abcSDavid Xu return (EINVAL); 302cbc10891SDmitry Chagin kern_thread_cputime(td2, ats); 303d65f1abcSDavid Xu PROC_UNLOCK(td2->td_proc); 304d65f1abcSDavid Xu } else { 305d65f1abcSDavid Xu pid = clock_id & CPUCLOCK_ID_MASK; 306c7c536c7SKonstantin Belousov error = pget(pid, PGET_CANSEE, &p2); 307c7c536c7SKonstantin Belousov if (error != 0) 308d65f1abcSDavid Xu return (EINVAL); 309cbc10891SDmitry Chagin kern_process_cputime(p2, ats); 310d65f1abcSDavid Xu PROC_UNLOCK(p2); 311d65f1abcSDavid Xu } 312d65f1abcSDavid Xu return (0); 313d65f1abcSDavid Xu } 314d65f1abcSDavid Xu 315f0b479cdSPaul Saab int 316f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 317f0b479cdSPaul Saab { 318de0a9241SKelly Yancey struct timeval sys, user; 31978c85e8dSJohn Baldwin struct proc *p; 32094c8fcd8SPeter Wemm 32178c85e8dSJohn Baldwin p = td->td_proc; 322f0b479cdSPaul Saab switch (clock_id) { 3235e758b95SRobert Watson case CLOCK_REALTIME: /* Default to precise. */ 3245e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 325f0b479cdSPaul Saab nanotime(ats); 326b8817154SKelly Yancey break; 3275e758b95SRobert Watson case CLOCK_REALTIME_FAST: 3285e758b95SRobert Watson getnanotime(ats); 3295e758b95SRobert Watson break; 330b8817154SKelly Yancey case CLOCK_VIRTUAL: 33178c85e8dSJohn Baldwin PROC_LOCK(p); 3325c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 33378c85e8dSJohn Baldwin calcru(p, &user, &sys); 3345c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 33578c85e8dSJohn Baldwin PROC_UNLOCK(p); 336f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 337b8817154SKelly Yancey break; 338b8817154SKelly Yancey case CLOCK_PROF: 33978c85e8dSJohn Baldwin PROC_LOCK(p); 3405c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 34178c85e8dSJohn Baldwin calcru(p, &user, &sys); 3425c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 34378c85e8dSJohn Baldwin PROC_UNLOCK(p); 344de0a9241SKelly Yancey timevaladd(&user, &sys); 345f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 346de0a9241SKelly Yancey break; 3475e758b95SRobert Watson case CLOCK_MONOTONIC: /* Default to precise. */ 3485e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 3495eefd889SAndre Oppermann case CLOCK_UPTIME: 3505e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 351f0b479cdSPaul Saab nanouptime(ats); 352b8817154SKelly Yancey break; 3535e758b95SRobert Watson case CLOCK_UPTIME_FAST: 3545e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 3555e758b95SRobert Watson getnanouptime(ats); 3565e758b95SRobert Watson break; 3575e758b95SRobert Watson case CLOCK_SECOND: 3585e758b95SRobert Watson ats->tv_sec = time_second; 3595e758b95SRobert Watson ats->tv_nsec = 0; 3605e758b95SRobert Watson break; 36100d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 362cbc10891SDmitry Chagin kern_thread_cputime(NULL, ats); 363d65f1abcSDavid Xu break; 364d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 365d65f1abcSDavid Xu PROC_LOCK(p); 366cbc10891SDmitry Chagin kern_process_cputime(p, ats); 367d65f1abcSDavid Xu PROC_UNLOCK(p); 36800d6ac63SDavid Xu break; 369b8817154SKelly Yancey default: 370d65f1abcSDavid Xu if ((int)clock_id >= 0) 3715cb3dc8fSPoul-Henning Kamp return (EINVAL); 372d65f1abcSDavid Xu return (get_cputime(td, clock_id, ats)); 373b8817154SKelly Yancey } 374f0b479cdSPaul Saab return (0); 37594c8fcd8SPeter Wemm } 37694c8fcd8SPeter Wemm 37794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 37894c8fcd8SPeter Wemm struct clock_settime_args { 37994c8fcd8SPeter Wemm clockid_t clock_id; 38094c8fcd8SPeter Wemm const struct timespec *tp; 38194c8fcd8SPeter Wemm }; 38294c8fcd8SPeter Wemm #endif 38394c8fcd8SPeter Wemm /* ARGSUSED */ 38494c8fcd8SPeter Wemm int 3858451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap) 38694c8fcd8SPeter Wemm { 38794c8fcd8SPeter Wemm struct timespec ats; 38894c8fcd8SPeter Wemm int error; 38994c8fcd8SPeter Wemm 390f0b479cdSPaul Saab if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 391f0b479cdSPaul Saab return (error); 392f0b479cdSPaul Saab return (kern_clock_settime(td, uap->clock_id, &ats)); 393f0b479cdSPaul Saab } 394f0b479cdSPaul Saab 39590a79ac5SConrad Meyer static int allow_insane_settime = 0; 39690a79ac5SConrad Meyer SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN, 39790a79ac5SConrad Meyer &allow_insane_settime, 0, 39890a79ac5SConrad Meyer "do not perform possibly restrictive checks on settime(2) args"); 39990a79ac5SConrad Meyer 400f0b479cdSPaul Saab int 401f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 402f0b479cdSPaul Saab { 403f0b479cdSPaul Saab struct timeval atv; 404f0b479cdSPaul Saab int error; 405f0b479cdSPaul Saab 406acd3428bSRobert Watson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 4077edfb592SJohn Baldwin return (error); 408f0b479cdSPaul Saab if (clock_id != CLOCK_REALTIME) 4097edfb592SJohn Baldwin return (EINVAL); 4103e18d701SDmitry Chagin if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000 || 4113e18d701SDmitry Chagin ats->tv_sec < 0) 4127edfb592SJohn Baldwin return (EINVAL); 413bc79b41cSMark Johnston if (!allow_insane_settime && 414bc79b41cSMark Johnston (ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60 || 415bc79b41cSMark Johnston ats->tv_sec < utc_offset())) 41690a79ac5SConrad Meyer return (EINVAL); 417a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 418f0b479cdSPaul Saab TIMESPEC_TO_TIMEVAL(&atv, ats); 4197edfb592SJohn Baldwin error = settime(td, &atv); 42094c8fcd8SPeter Wemm return (error); 42194c8fcd8SPeter Wemm } 42294c8fcd8SPeter Wemm 42394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 42494c8fcd8SPeter Wemm struct clock_getres_args { 42594c8fcd8SPeter Wemm clockid_t clock_id; 42694c8fcd8SPeter Wemm struct timespec *tp; 42794c8fcd8SPeter Wemm }; 42894c8fcd8SPeter Wemm #endif 42994c8fcd8SPeter Wemm int 4308451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap) 43194c8fcd8SPeter Wemm { 43294c8fcd8SPeter Wemm struct timespec ts; 433f0b479cdSPaul Saab int error; 43494c8fcd8SPeter Wemm 435f0b479cdSPaul Saab if (uap->tp == NULL) 436f0b479cdSPaul Saab return (0); 437f0b479cdSPaul Saab 438f0b479cdSPaul Saab error = kern_clock_getres(td, uap->clock_id, &ts); 439f0b479cdSPaul Saab if (error == 0) 440f0b479cdSPaul Saab error = copyout(&ts, uap->tp, sizeof(ts)); 441f0b479cdSPaul Saab return (error); 442f0b479cdSPaul Saab } 443f0b479cdSPaul Saab 444f0b479cdSPaul Saab int 445f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 446f0b479cdSPaul Saab { 447f0b479cdSPaul Saab 448f0b479cdSPaul Saab ts->tv_sec = 0; 449f0b479cdSPaul Saab switch (clock_id) { 450b8817154SKelly Yancey case CLOCK_REALTIME: 4515e758b95SRobert Watson case CLOCK_REALTIME_FAST: 4525e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 453b8817154SKelly Yancey case CLOCK_MONOTONIC: 4545e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 4555e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 4565eefd889SAndre Oppermann case CLOCK_UPTIME: 4575e758b95SRobert Watson case CLOCK_UPTIME_FAST: 4585e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 459ac0653dcSBruce Evans /* 460ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 461ac0653dcSBruce Evans * Rounding up is especially important if rounding down 462ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 463ac0653dcSBruce Evans */ 464f0b479cdSPaul Saab ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 465b8817154SKelly Yancey break; 466b8817154SKelly Yancey case CLOCK_VIRTUAL: 467b8817154SKelly Yancey case CLOCK_PROF: 468b8817154SKelly Yancey /* Accurately round up here because we can do so cheaply. */ 46955e0987aSPedro F. Giffuni ts->tv_nsec = howmany(1000000000, hz); 470b8817154SKelly Yancey break; 4715e758b95SRobert Watson case CLOCK_SECOND: 4725e758b95SRobert Watson ts->tv_sec = 1; 4735e758b95SRobert Watson ts->tv_nsec = 0; 4745e758b95SRobert Watson break; 47500d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 476d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 477d65f1abcSDavid Xu cputime: 47800d6ac63SDavid Xu /* sync with cputick2usec */ 47900d6ac63SDavid Xu ts->tv_nsec = 1000000 / cpu_tickrate(); 48000d6ac63SDavid Xu if (ts->tv_nsec == 0) 48100d6ac63SDavid Xu ts->tv_nsec = 1000; 48200d6ac63SDavid Xu break; 483b8817154SKelly Yancey default: 484d65f1abcSDavid Xu if ((int)clock_id < 0) 485d65f1abcSDavid Xu goto cputime; 486b8817154SKelly Yancey return (EINVAL); 48794c8fcd8SPeter Wemm } 488de0a9241SKelly Yancey return (0); 48994c8fcd8SPeter Wemm } 49094c8fcd8SPeter Wemm 4917fdf2c85SPaul Saab int 4927fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 4935b870b7bSPeter Wemm { 4943f8455b0SEric van Gyzen 4953f8455b0SEric van Gyzen return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt, 4963f8455b0SEric van Gyzen rmt)); 4973f8455b0SEric van Gyzen } 4983f8455b0SEric van Gyzen 4993f8455b0SEric van Gyzen static uint8_t nanowait[MAXCPU]; 5003f8455b0SEric van Gyzen 5013f8455b0SEric van Gyzen int 5023f8455b0SEric van Gyzen kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags, 5033f8455b0SEric van Gyzen const struct timespec *rqt, struct timespec *rmt) 5043f8455b0SEric van Gyzen { 5053f8455b0SEric van Gyzen struct timespec ts, now; 506098176f0SDavide Italiano sbintime_t sbt, sbtt, prec, tmp; 507980c545dSAlexander Motin time_t over; 50833841826SPoul-Henning Kamp int error; 5093f8455b0SEric van Gyzen bool is_abs_real; 5105b870b7bSPeter Wemm 5117d7fb492SBruce Evans if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 512708e7684SPeter Wemm return (EINVAL); 5133f8455b0SEric van Gyzen if ((flags & ~TIMER_ABSTIME) != 0) 5143f8455b0SEric van Gyzen return (EINVAL); 5153f8455b0SEric van Gyzen switch (clock_id) { 5163f8455b0SEric van Gyzen case CLOCK_REALTIME: 5173f8455b0SEric van Gyzen case CLOCK_REALTIME_PRECISE: 5183f8455b0SEric van Gyzen case CLOCK_REALTIME_FAST: 5193f8455b0SEric van Gyzen case CLOCK_SECOND: 5203f8455b0SEric van Gyzen is_abs_real = (flags & TIMER_ABSTIME) != 0; 5213f8455b0SEric van Gyzen break; 5223f8455b0SEric van Gyzen case CLOCK_MONOTONIC: 5233f8455b0SEric van Gyzen case CLOCK_MONOTONIC_PRECISE: 5243f8455b0SEric van Gyzen case CLOCK_MONOTONIC_FAST: 5253f8455b0SEric van Gyzen case CLOCK_UPTIME: 5263f8455b0SEric van Gyzen case CLOCK_UPTIME_PRECISE: 5273f8455b0SEric van Gyzen case CLOCK_UPTIME_FAST: 5283f8455b0SEric van Gyzen is_abs_real = false; 5293f8455b0SEric van Gyzen break; 5303f8455b0SEric van Gyzen case CLOCK_VIRTUAL: 5313f8455b0SEric van Gyzen case CLOCK_PROF: 5323f8455b0SEric van Gyzen case CLOCK_PROCESS_CPUTIME_ID: 5333f8455b0SEric van Gyzen return (ENOTSUP); 5343f8455b0SEric van Gyzen case CLOCK_THREAD_CPUTIME_ID: 5353f8455b0SEric van Gyzen default: 5363f8455b0SEric van Gyzen return (EINVAL); 5373f8455b0SEric van Gyzen } 5383f8455b0SEric van Gyzen do { 539980c545dSAlexander Motin ts = *rqt; 5403f8455b0SEric van Gyzen if ((flags & TIMER_ABSTIME) != 0) { 5413f8455b0SEric van Gyzen if (is_abs_real) 5423f8455b0SEric van Gyzen td->td_rtcgen = 5433f8455b0SEric van Gyzen atomic_load_acq_int(&rtc_generation); 5443f8455b0SEric van Gyzen error = kern_clock_gettime(td, clock_id, &now); 5453f8455b0SEric van Gyzen KASSERT(error == 0, ("kern_clock_gettime: %d", error)); 5466040822cSAlan Somers timespecsub(&ts, &now, &ts); 5473f8455b0SEric van Gyzen } 5483f8455b0SEric van Gyzen if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) { 5493f8455b0SEric van Gyzen error = EWOULDBLOCK; 5503f8455b0SEric van Gyzen break; 5513f8455b0SEric van Gyzen } 552980c545dSAlexander Motin if (ts.tv_sec > INT32_MAX / 2) { 553980c545dSAlexander Motin over = ts.tv_sec - INT32_MAX / 2; 554980c545dSAlexander Motin ts.tv_sec -= over; 555980c545dSAlexander Motin } else 556980c545dSAlexander Motin over = 0; 557980c545dSAlexander Motin tmp = tstosbt(ts); 558098176f0SDavide Italiano prec = tmp; 559098176f0SDavide Italiano prec >>= tc_precexp; 560098176f0SDavide Italiano if (TIMESEL(&sbt, tmp)) 561098176f0SDavide Italiano sbt += tc_tick_sbt; 562098176f0SDavide Italiano sbt += tmp; 5630dbf17e6SAlexander Motin error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp", 5640dbf17e6SAlexander Motin sbt, prec, C_ABSOLUTE); 5653f8455b0SEric van Gyzen } while (error == 0 && is_abs_real && td->td_rtcgen == 0); 5663f8455b0SEric van Gyzen td->td_rtcgen = 0; 56733841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 56870c144dcSBryan Drewery if (TIMESEL(&sbtt, tmp)) 56970c144dcSBryan Drewery sbtt += tc_tick_sbt; 5703f8455b0SEric van Gyzen if (sbtt >= sbt) 5713f8455b0SEric van Gyzen return (0); 57233841826SPoul-Henning Kamp if (error == ERESTART) 57394c8fcd8SPeter Wemm error = EINTR; 5743f8455b0SEric van Gyzen if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) { 575098176f0SDavide Italiano ts = sbttots(sbt - sbtt); 576980c545dSAlexander Motin ts.tv_sec += over; 57733841826SPoul-Henning Kamp if (ts.tv_sec < 0) 57833841826SPoul-Henning Kamp timespecclear(&ts); 57900af9731SPoul-Henning Kamp *rmt = ts; 58000af9731SPoul-Henning Kamp } 58133841826SPoul-Henning Kamp return (error); 58233841826SPoul-Henning Kamp } 58333841826SPoul-Henning Kamp return (0); 5845b870b7bSPeter Wemm } 58594c8fcd8SPeter Wemm 5865b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 5875b870b7bSPeter Wemm struct nanosleep_args { 5885b870b7bSPeter Wemm struct timespec *rqtp; 5895b870b7bSPeter Wemm struct timespec *rmtp; 5905b870b7bSPeter Wemm }; 5915b870b7bSPeter Wemm #endif 5925b870b7bSPeter Wemm /* ARGSUSED */ 5935b870b7bSPeter Wemm int 5948451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap) 5955b870b7bSPeter Wemm { 5963f8455b0SEric van Gyzen 5973f8455b0SEric van Gyzen return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, 5983f8455b0SEric van Gyzen uap->rqtp, uap->rmtp)); 5993f8455b0SEric van Gyzen } 6003f8455b0SEric van Gyzen 6013f8455b0SEric van Gyzen #ifndef _SYS_SYSPROTO_H_ 6023f8455b0SEric van Gyzen struct clock_nanosleep_args { 6033f8455b0SEric van Gyzen clockid_t clock_id; 6043f8455b0SEric van Gyzen int flags; 6053f8455b0SEric van Gyzen struct timespec *rqtp; 6063f8455b0SEric van Gyzen struct timespec *rmtp; 6073f8455b0SEric van Gyzen }; 6083f8455b0SEric van Gyzen #endif 6093f8455b0SEric van Gyzen /* ARGSUSED */ 6103f8455b0SEric van Gyzen int 6113f8455b0SEric van Gyzen sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap) 6123f8455b0SEric van Gyzen { 6133f8455b0SEric van Gyzen int error; 6143f8455b0SEric van Gyzen 6153f8455b0SEric van Gyzen error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp, 6163f8455b0SEric van Gyzen uap->rmtp); 6173f8455b0SEric van Gyzen return (kern_posix_error(td, error)); 6183f8455b0SEric van Gyzen } 6193f8455b0SEric van Gyzen 6203f8455b0SEric van Gyzen static int 6213f8455b0SEric van Gyzen user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags, 6223f8455b0SEric van Gyzen const struct timespec *ua_rqtp, struct timespec *ua_rmtp) 6233f8455b0SEric van Gyzen { 6245b870b7bSPeter Wemm struct timespec rmt, rqt; 625618a20d4SBrooks Davis int error, error2; 6265b870b7bSPeter Wemm 6273f8455b0SEric van Gyzen error = copyin(ua_rqtp, &rqt, sizeof(rqt)); 6285b870b7bSPeter Wemm if (error) 6295b870b7bSPeter Wemm return (error); 6303f8455b0SEric van Gyzen error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt); 6313f8455b0SEric van Gyzen if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) { 6323f8455b0SEric van Gyzen error2 = copyout(&rmt, ua_rmtp, sizeof(rmt)); 633618a20d4SBrooks Davis if (error2 != 0) 634fb99ab88SMatthew Dillon error = error2; 635708e7684SPeter Wemm } 636708e7684SPeter Wemm return (error); 63794c8fcd8SPeter Wemm } 63894c8fcd8SPeter Wemm 6395b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 640df8bae1dSRodney W. Grimes struct gettimeofday_args { 641df8bae1dSRodney W. Grimes struct timeval *tp; 642df8bae1dSRodney W. Grimes struct timezone *tzp; 643df8bae1dSRodney W. Grimes }; 644d2d3e875SBruce Evans #endif 645df8bae1dSRodney W. Grimes /* ARGSUSED */ 64626f9a767SRodney W. Grimes int 6478451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap) 648df8bae1dSRodney W. Grimes { 649df8bae1dSRodney W. Grimes struct timeval atv; 650411c25edSTim J. Robbins struct timezone rtz; 651df8bae1dSRodney W. Grimes int error = 0; 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes if (uap->tp) { 654df8bae1dSRodney W. Grimes microtime(&atv); 65501609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 656df8bae1dSRodney W. Grimes } 65721dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 658329f0aa9SWarner Losh rtz.tz_minuteswest = 0; 659329f0aa9SWarner Losh rtz.tz_dsttime = 0; 660411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 66121dcdb38SPoul-Henning Kamp } 662df8bae1dSRodney W. Grimes return (error); 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes 665d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 666df8bae1dSRodney W. Grimes struct settimeofday_args { 667df8bae1dSRodney W. Grimes struct timeval *tv; 668df8bae1dSRodney W. Grimes struct timezone *tzp; 669df8bae1dSRodney W. Grimes }; 670d2d3e875SBruce Evans #endif 671df8bae1dSRodney W. Grimes /* ARGSUSED */ 67226f9a767SRodney W. Grimes int 6738451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap) 674df8bae1dSRodney W. Grimes { 675b88ec951SJohn Baldwin struct timeval atv, *tvp; 676b88ec951SJohn Baldwin struct timezone atz, *tzp; 677b88ec951SJohn Baldwin int error; 678b88ec951SJohn Baldwin 679b88ec951SJohn Baldwin if (uap->tv) { 680b88ec951SJohn Baldwin error = copyin(uap->tv, &atv, sizeof(atv)); 681b88ec951SJohn Baldwin if (error) 682b88ec951SJohn Baldwin return (error); 683b88ec951SJohn Baldwin tvp = &atv; 684b88ec951SJohn Baldwin } else 685b88ec951SJohn Baldwin tvp = NULL; 686b88ec951SJohn Baldwin if (uap->tzp) { 687b88ec951SJohn Baldwin error = copyin(uap->tzp, &atz, sizeof(atz)); 688b88ec951SJohn Baldwin if (error) 689b88ec951SJohn Baldwin return (error); 690b88ec951SJohn Baldwin tzp = &atz; 691b88ec951SJohn Baldwin } else 692b88ec951SJohn Baldwin tzp = NULL; 693b88ec951SJohn Baldwin return (kern_settimeofday(td, tvp, tzp)); 694b88ec951SJohn Baldwin } 695b88ec951SJohn Baldwin 696b88ec951SJohn Baldwin int 697b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 698b88ec951SJohn Baldwin { 699b88ec951SJohn Baldwin int error; 700fb99ab88SMatthew Dillon 701acd3428bSRobert Watson error = priv_check(td, PRIV_SETTIMEOFDAY); 702b88ec951SJohn Baldwin if (error) 7037edfb592SJohn Baldwin return (error); 704df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 705b88ec951SJohn Baldwin if (tv) { 7063e18d701SDmitry Chagin if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 || 7073e18d701SDmitry Chagin tv->tv_sec < 0) 7087edfb592SJohn Baldwin return (EINVAL); 709b88ec951SJohn Baldwin error = settime(td, tv); 710708e7684SPeter Wemm } 7117edfb592SJohn Baldwin return (error); 712b88ec951SJohn Baldwin } 7137edfb592SJohn Baldwin 714df8bae1dSRodney W. Grimes /* 715873fbcd7SRobert Watson * Get value of an interval timer. The process virtual and profiling virtual 716873fbcd7SRobert Watson * time timers are kept in the p_stats area, since they can be swapped out. 717873fbcd7SRobert Watson * These are kept internally in the way they are specified externally: in 718873fbcd7SRobert Watson * time until they expire. 719df8bae1dSRodney W. Grimes * 720873fbcd7SRobert Watson * The real time interval timer is kept in the process table slot for the 721873fbcd7SRobert Watson * process, and its value (it_value) is kept as an absolute time rather than 722873fbcd7SRobert Watson * as a delta, so that it is easy to keep periodic real-time signals from 723873fbcd7SRobert Watson * drifting. 724df8bae1dSRodney W. Grimes * 725df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 726873fbcd7SRobert Watson * kern_clock.c. The real time timer is processed by a timeout routine, 727873fbcd7SRobert Watson * called from the softclock() routine. Since a callout may be delayed in 728873fbcd7SRobert Watson * real time due to interrupt processing in the system, it is possible for 729873fbcd7SRobert Watson * the real time timeout routine (realitexpire, given below), to be delayed 730873fbcd7SRobert Watson * in real time past when it is supposed to occur. It does not suffice, 731873fbcd7SRobert Watson * therefore, to reload the real timer .it_value from the real time timers 732873fbcd7SRobert Watson * .it_interval. Rather, we compute the next time in absolute time the timer 733873fbcd7SRobert Watson * should go off. 734df8bae1dSRodney W. Grimes */ 735d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 736df8bae1dSRodney W. Grimes struct getitimer_args { 737df8bae1dSRodney W. Grimes u_int which; 738df8bae1dSRodney W. Grimes struct itimerval *itv; 739df8bae1dSRodney W. Grimes }; 740d2d3e875SBruce Evans #endif 74126f9a767SRodney W. Grimes int 7428451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap) 743df8bae1dSRodney W. Grimes { 744df8bae1dSRodney W. Grimes struct itimerval aitv; 745c90110d6SJohn Baldwin int error; 746df8bae1dSRodney W. Grimes 747cfa0efe7SMaxim Sobolev error = kern_getitimer(td, uap->which, &aitv); 748cfa0efe7SMaxim Sobolev if (error != 0) 749cfa0efe7SMaxim Sobolev return (error); 750cfa0efe7SMaxim Sobolev return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 751cfa0efe7SMaxim Sobolev } 752cfa0efe7SMaxim Sobolev 753cfa0efe7SMaxim Sobolev int 754cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 755cfa0efe7SMaxim Sobolev { 756cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 757cfa0efe7SMaxim Sobolev struct timeval ctv; 758cfa0efe7SMaxim Sobolev 759cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 760df8bae1dSRodney W. Grimes return (EINVAL); 761fb99ab88SMatthew Dillon 762cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 763df8bae1dSRodney W. Grimes /* 764ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 765df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 766df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 767df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 768df8bae1dSRodney W. Grimes */ 76996d7f8efSTim J. Robbins PROC_LOCK(p); 770cfa0efe7SMaxim Sobolev *aitv = p->p_realtimer; 77196d7f8efSTim J. Robbins PROC_UNLOCK(p); 772cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 773b5ea3779SAlexander Motin microuptime(&ctv); 774cfa0efe7SMaxim Sobolev if (timevalcmp(&aitv->it_value, &ctv, <)) 775cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_value); 776df8bae1dSRodney W. Grimes else 777cfa0efe7SMaxim Sobolev timevalsub(&aitv->it_value, &ctv); 778227ee8a1SPoul-Henning Kamp } 779fb99ab88SMatthew Dillon } else { 7805c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 781cfa0efe7SMaxim Sobolev *aitv = p->p_stats->p_timer[which]; 7825c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 783fb99ab88SMatthew Dillon } 784de56aee0SKonstantin Belousov #ifdef KTRACE 785de56aee0SKonstantin Belousov if (KTRPOINT(td, KTR_STRUCT)) 786de56aee0SKonstantin Belousov ktritimerval(aitv); 787de56aee0SKonstantin Belousov #endif 788cfa0efe7SMaxim Sobolev return (0); 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes 791d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 792df8bae1dSRodney W. Grimes struct setitimer_args { 793df8bae1dSRodney W. Grimes u_int which; 794df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 795df8bae1dSRodney W. Grimes }; 796d2d3e875SBruce Evans #endif 79726f9a767SRodney W. Grimes int 7988451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap) 799df8bae1dSRodney W. Grimes { 800cfa0efe7SMaxim Sobolev struct itimerval aitv, oitv; 801c90110d6SJohn Baldwin int error; 80296d7f8efSTim J. Robbins 80396d7f8efSTim J. Robbins if (uap->itv == NULL) { 80496d7f8efSTim J. Robbins uap->itv = uap->oitv; 8058451d0ddSKip Macy return (sys_getitimer(td, (struct getitimer_args *)uap)); 80696d7f8efSTim J. Robbins } 807df8bae1dSRodney W. Grimes 80896d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 809df8bae1dSRodney W. Grimes return (error); 810cfa0efe7SMaxim Sobolev error = kern_setitimer(td, uap->which, &aitv, &oitv); 811cfa0efe7SMaxim Sobolev if (error != 0 || uap->oitv == NULL) 812cfa0efe7SMaxim Sobolev return (error); 813cfa0efe7SMaxim Sobolev return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 814cfa0efe7SMaxim Sobolev } 815cfa0efe7SMaxim Sobolev 816cfa0efe7SMaxim Sobolev int 817c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 818c90110d6SJohn Baldwin struct itimerval *oitv) 819cfa0efe7SMaxim Sobolev { 820cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 821cfa0efe7SMaxim Sobolev struct timeval ctv; 822b5ea3779SAlexander Motin sbintime_t sbt, pr; 823cfa0efe7SMaxim Sobolev 8245e85ac17SJohn Baldwin if (aitv == NULL) 8255e85ac17SJohn Baldwin return (kern_getitimer(td, which, oitv)); 8265e85ac17SJohn Baldwin 827cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 82896d7f8efSTim J. Robbins return (EINVAL); 829de56aee0SKonstantin Belousov #ifdef KTRACE 830de56aee0SKonstantin Belousov if (KTRPOINT(td, KTR_STRUCT)) 831de56aee0SKonstantin Belousov ktritimerval(aitv); 832de56aee0SKonstantin Belousov #endif 833b5ea3779SAlexander Motin if (itimerfix(&aitv->it_value) || 834b5ea3779SAlexander Motin aitv->it_value.tv_sec > INT32_MAX / 2) 835cfa0efe7SMaxim Sobolev return (EINVAL); 836cfa0efe7SMaxim Sobolev if (!timevalisset(&aitv->it_value)) 837cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_interval); 838b5ea3779SAlexander Motin else if (itimerfix(&aitv->it_interval) || 839b5ea3779SAlexander Motin aitv->it_interval.tv_sec > INT32_MAX / 2) 84096d7f8efSTim J. Robbins return (EINVAL); 84196d7f8efSTim J. Robbins 842cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 84396d7f8efSTim J. Robbins PROC_LOCK(p); 8444cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 8454f559836SJake Burkholder callout_stop(&p->p_itcallout); 846b5ea3779SAlexander Motin microuptime(&ctv); 847cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 848b5ea3779SAlexander Motin pr = tvtosbt(aitv->it_value) >> tc_precexp; 849cfa0efe7SMaxim Sobolev timevaladd(&aitv->it_value, &ctv); 850b5ea3779SAlexander Motin sbt = tvtosbt(aitv->it_value); 851b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, sbt, pr, 852b5ea3779SAlexander Motin realitexpire, p, C_ABSOLUTE); 85325b4d3a8SJohn Baldwin } 854cfa0efe7SMaxim Sobolev *oitv = p->p_realtimer; 855cfa0efe7SMaxim Sobolev p->p_realtimer = *aitv; 85696d7f8efSTim J. Robbins PROC_UNLOCK(p); 857cfa0efe7SMaxim Sobolev if (timevalisset(&oitv->it_value)) { 858cfa0efe7SMaxim Sobolev if (timevalcmp(&oitv->it_value, &ctv, <)) 859cfa0efe7SMaxim Sobolev timevalclear(&oitv->it_value); 86096d7f8efSTim J. Robbins else 861cfa0efe7SMaxim Sobolev timevalsub(&oitv->it_value, &ctv); 862fb99ab88SMatthew Dillon } 86396d7f8efSTim J. Robbins } else { 864d10a1df8SAlexander Motin if (aitv->it_interval.tv_sec == 0 && 865d10a1df8SAlexander Motin aitv->it_interval.tv_usec != 0 && 866d10a1df8SAlexander Motin aitv->it_interval.tv_usec < tick) 867d10a1df8SAlexander Motin aitv->it_interval.tv_usec = tick; 868d10a1df8SAlexander Motin if (aitv->it_value.tv_sec == 0 && 869d10a1df8SAlexander Motin aitv->it_value.tv_usec != 0 && 870d10a1df8SAlexander Motin aitv->it_value.tv_usec < tick) 871d10a1df8SAlexander Motin aitv->it_value.tv_usec = tick; 8725c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 873cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 874cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 8755c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 87696d7f8efSTim J. Robbins } 877de56aee0SKonstantin Belousov #ifdef KTRACE 878de56aee0SKonstantin Belousov if (KTRPOINT(td, KTR_STRUCT)) 879de56aee0SKonstantin Belousov ktritimerval(oitv); 880de56aee0SKonstantin Belousov #endif 88196d7f8efSTim J. Robbins return (0); 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes 884*dc47fdf1SKonstantin Belousov static void 885*dc47fdf1SKonstantin Belousov realitexpire_reset_callout(struct proc *p, sbintime_t *isbtp) 886*dc47fdf1SKonstantin Belousov { 887*dc47fdf1SKonstantin Belousov sbintime_t prec; 888*dc47fdf1SKonstantin Belousov 889*dc47fdf1SKonstantin Belousov prec = isbtp == NULL ? tvtosbt(p->p_realtimer.it_interval) : *isbtp; 890*dc47fdf1SKonstantin Belousov callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value), 891*dc47fdf1SKonstantin Belousov prec >> tc_precexp, realitexpire, p, C_ABSOLUTE); 892*dc47fdf1SKonstantin Belousov } 893*dc47fdf1SKonstantin Belousov 894*dc47fdf1SKonstantin Belousov void 895*dc47fdf1SKonstantin Belousov itimer_proc_continue(struct proc *p) 896*dc47fdf1SKonstantin Belousov { 897*dc47fdf1SKonstantin Belousov struct timeval ctv; 898*dc47fdf1SKonstantin Belousov 899*dc47fdf1SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 900*dc47fdf1SKonstantin Belousov 901*dc47fdf1SKonstantin Belousov if ((p->p_flag2 & P2_ITSTOPPED) != 0) { 902*dc47fdf1SKonstantin Belousov p->p_flag2 &= ~P2_ITSTOPPED; 903*dc47fdf1SKonstantin Belousov microuptime(&ctv); 904*dc47fdf1SKonstantin Belousov if (timevalcmp(&p->p_realtimer.it_value, &ctv, >=)) 905*dc47fdf1SKonstantin Belousov realitexpire(p); 906*dc47fdf1SKonstantin Belousov else 907*dc47fdf1SKonstantin Belousov realitexpire_reset_callout(p, NULL); 908*dc47fdf1SKonstantin Belousov } 909*dc47fdf1SKonstantin Belousov } 910*dc47fdf1SKonstantin Belousov 911df8bae1dSRodney W. Grimes /* 912df8bae1dSRodney W. Grimes * Real interval timer expired: 913df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 914df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 915df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 916df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 917df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 918c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 9199207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 9209207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 9219207f00aSBruce Evans * interrupt even when we're delayed. 922df8bae1dSRodney W. Grimes */ 923df8bae1dSRodney W. Grimes void 92491afe087SPoul-Henning Kamp realitexpire(void *arg) 925df8bae1dSRodney W. Grimes { 92691afe087SPoul-Henning Kamp struct proc *p; 927b5ea3779SAlexander Motin struct timeval ctv; 928b5ea3779SAlexander Motin sbintime_t isbt; 929df8bae1dSRodney W. Grimes 930df8bae1dSRodney W. Grimes p = (struct proc *)arg; 9318451d0ddSKip Macy kern_psignal(p, SIGALRM); 9324cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 9334cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 9345499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 9355499ea01SJohn Baldwin wakeup(&p->p_itcallout); 936df8bae1dSRodney W. Grimes return; 937df8bae1dSRodney W. Grimes } 938*dc47fdf1SKonstantin Belousov 939b5ea3779SAlexander Motin isbt = tvtosbt(p->p_realtimer.it_interval); 940b5ea3779SAlexander Motin if (isbt >= sbt_timethreshold) 941b5ea3779SAlexander Motin getmicrouptime(&ctv); 942b5ea3779SAlexander Motin else 943b5ea3779SAlexander Motin microuptime(&ctv); 944b5ea3779SAlexander Motin do { 945df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 946df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 947b5ea3779SAlexander Motin } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=)); 948*dc47fdf1SKonstantin Belousov 949*dc47fdf1SKonstantin Belousov if (P_SHOULDSTOP(p) || P_KILLED(p)) { 950*dc47fdf1SKonstantin Belousov p->p_flag2 |= P2_ITSTOPPED; 951*dc47fdf1SKonstantin Belousov return; 952*dc47fdf1SKonstantin Belousov } 953*dc47fdf1SKonstantin Belousov 954*dc47fdf1SKonstantin Belousov p->p_flag2 &= ~P2_ITSTOPPED; 955*dc47fdf1SKonstantin Belousov realitexpire_reset_callout(p, &isbt); 956df8bae1dSRodney W. Grimes } 957df8bae1dSRodney W. Grimes 958df8bae1dSRodney W. Grimes /* 959df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 960df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 961df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 962df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 963df8bae1dSRodney W. Grimes */ 96426f9a767SRodney W. Grimes int 96591afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 966df8bae1dSRodney W. Grimes { 967df8bae1dSRodney W. Grimes 96886857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 969df8bae1dSRodney W. Grimes return (EINVAL); 970b5ea3779SAlexander Motin if (tv->tv_sec == 0 && tv->tv_usec != 0 && 971b5ea3779SAlexander Motin tv->tv_usec < (u_int)tick / 16) 972b5ea3779SAlexander Motin tv->tv_usec = (u_int)tick / 16; 973df8bae1dSRodney W. Grimes return (0); 974df8bae1dSRodney W. Grimes } 975df8bae1dSRodney W. Grimes 976df8bae1dSRodney W. Grimes /* 977df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 978df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 979df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 980df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 981df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 982df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 983df8bae1dSRodney W. Grimes * that it is called in a context where the timers 984df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 985df8bae1dSRodney W. Grimes */ 98626f9a767SRodney W. Grimes int 98791afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 988df8bae1dSRodney W. Grimes { 989df8bae1dSRodney W. Grimes 990df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 991df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 992df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 993df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 994df8bae1dSRodney W. Grimes goto expire; 995df8bae1dSRodney W. Grimes } 996df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 997df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 998df8bae1dSRodney W. Grimes } 999df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 1000df8bae1dSRodney W. Grimes usec = 0; 10014cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 1002df8bae1dSRodney W. Grimes return (1); 1003df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 1004df8bae1dSRodney W. Grimes expire: 10054cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 1006df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 1007df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 1008df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 1009df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 1010df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 1011df8bae1dSRodney W. Grimes } 1012df8bae1dSRodney W. Grimes } else 1013df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 1014df8bae1dSRodney W. Grimes return (0); 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 1017df8bae1dSRodney W. Grimes /* 1018df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 1019df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 1020df8bae1dSRodney W. Grimes * results which are before the beginning, 1021df8bae1dSRodney W. Grimes * it just gets very confused in this case. 1022df8bae1dSRodney W. Grimes * Caveat emptor. 1023df8bae1dSRodney W. Grimes */ 102426f9a767SRodney W. Grimes void 10256ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 1026df8bae1dSRodney W. Grimes { 1027df8bae1dSRodney W. Grimes 1028df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 1029df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 1030df8bae1dSRodney W. Grimes timevalfix(t1); 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes 103326f9a767SRodney W. Grimes void 10346ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 1035df8bae1dSRodney W. Grimes { 1036df8bae1dSRodney W. Grimes 1037df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 1038df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 1039df8bae1dSRodney W. Grimes timevalfix(t1); 1040df8bae1dSRodney W. Grimes } 1041df8bae1dSRodney W. Grimes 104287b6de2bSPoul-Henning Kamp static void 104391afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 1044df8bae1dSRodney W. Grimes { 1045df8bae1dSRodney W. Grimes 1046df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 1047df8bae1dSRodney W. Grimes t1->tv_sec--; 1048df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 1049df8bae1dSRodney W. Grimes } 1050df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 1051df8bae1dSRodney W. Grimes t1->tv_sec++; 1052df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 1053df8bae1dSRodney W. Grimes } 1054df8bae1dSRodney W. Grimes } 105591974ce1SSam Leffler 105691974ce1SSam Leffler /* 1057addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 105891974ce1SSam Leffler */ 105991974ce1SSam Leffler int 106091974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 106191974ce1SSam Leffler { 106291974ce1SSam Leffler struct timeval tv, delta; 106391974ce1SSam Leffler int rv = 0; 106491974ce1SSam Leffler 1065addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 1066addea9d4SSam Leffler delta = tv; 1067addea9d4SSam Leffler timevalsub(&delta, lasttime); 106891974ce1SSam Leffler 106991974ce1SSam Leffler /* 107091974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 107191974ce1SSam Leffler * even if interval is huge. 107291974ce1SSam Leffler */ 107391974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 107491974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 107591974ce1SSam Leffler *lasttime = tv; 107691974ce1SSam Leffler rv = 1; 107791974ce1SSam Leffler } 107891974ce1SSam Leffler 107991974ce1SSam Leffler return (rv); 108091974ce1SSam Leffler } 108191974ce1SSam Leffler 108291974ce1SSam Leffler /* 108391974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 1084addea9d4SSam Leffler * 1085addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 1086addea9d4SSam Leffler * should drop a packet because of the rate limitation). 1087addea9d4SSam Leffler * 1088893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 1089893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 1090893bec80SSam Leffler * limiting. 1091893bec80SSam Leffler * 1092addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 1093addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 1094addea9d4SSam Leffler * clock ticks for minimal overhead. 109591974ce1SSam Leffler */ 109691974ce1SSam Leffler int 109791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 109891974ce1SSam Leffler { 1099addea9d4SSam Leffler int now; 110091974ce1SSam Leffler 110191974ce1SSam Leffler /* 1102addea9d4SSam Leffler * Reset the last time and counter if this is the first call 1103addea9d4SSam Leffler * or more than a second has passed since the last update of 1104addea9d4SSam Leffler * lasttime. 110591974ce1SSam Leffler */ 1106addea9d4SSam Leffler now = ticks; 1107addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 1108addea9d4SSam Leffler lasttime->tv_sec = now; 1109addea9d4SSam Leffler *curpps = 1; 1110893bec80SSam Leffler return (maxpps != 0); 1111addea9d4SSam Leffler } else { 1112addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 1113f8f02928SIan Lepore return (maxpps < 0 || *curpps <= maxpps); 1114addea9d4SSam Leffler } 111591974ce1SSam Leffler } 111686857b36SDavid Xu 111786857b36SDavid Xu static void 111886857b36SDavid Xu itimer_start(void) 111986857b36SDavid Xu { 1120e68c6191SKonstantin Belousov static const struct kclock rt_clock = { 112186857b36SDavid Xu .timer_create = realtimer_create, 112286857b36SDavid Xu .timer_delete = realtimer_delete, 112386857b36SDavid Xu .timer_settime = realtimer_settime, 112486857b36SDavid Xu .timer_gettime = realtimer_gettime, 112586857b36SDavid Xu }; 112686857b36SDavid Xu 112786857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 112886857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 112986857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 113086857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 113177e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 113277e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 113377e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 113486857b36SDavid Xu } 113586857b36SDavid Xu 1136e68c6191SKonstantin Belousov static int 1137e68c6191SKonstantin Belousov register_posix_clock(int clockid, const struct kclock *clk) 113886857b36SDavid Xu { 113986857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 114086857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 114186857b36SDavid Xu return (0); 114286857b36SDavid Xu } 114386857b36SDavid Xu posix_clocks[clockid] = *clk; 114486857b36SDavid Xu return (1); 114586857b36SDavid Xu } 114686857b36SDavid Xu 114786857b36SDavid Xu static int 114886857b36SDavid Xu itimer_init(void *mem, int size, int flags) 114986857b36SDavid Xu { 115086857b36SDavid Xu struct itimer *it; 115186857b36SDavid Xu 115286857b36SDavid Xu it = (struct itimer *)mem; 115386857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 115486857b36SDavid Xu return (0); 115586857b36SDavid Xu } 115686857b36SDavid Xu 115786857b36SDavid Xu static void 115886857b36SDavid Xu itimer_fini(void *mem, int size) 115986857b36SDavid Xu { 116086857b36SDavid Xu struct itimer *it; 116186857b36SDavid Xu 116286857b36SDavid Xu it = (struct itimer *)mem; 116386857b36SDavid Xu mtx_destroy(&it->it_mtx); 116486857b36SDavid Xu } 116586857b36SDavid Xu 116686857b36SDavid Xu static void 116786857b36SDavid Xu itimer_enter(struct itimer *it) 116886857b36SDavid Xu { 116986857b36SDavid Xu 117086857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 117186857b36SDavid Xu it->it_usecount++; 117286857b36SDavid Xu } 117386857b36SDavid Xu 117486857b36SDavid Xu static void 117586857b36SDavid Xu itimer_leave(struct itimer *it) 117686857b36SDavid Xu { 117786857b36SDavid Xu 117886857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 117986857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 118086857b36SDavid Xu 118186857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 118286857b36SDavid Xu wakeup(it); 118386857b36SDavid Xu } 118486857b36SDavid Xu 118586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 118661d3a4efSDavid Xu struct ktimer_create_args { 118786857b36SDavid Xu clockid_t clock_id; 118886857b36SDavid Xu struct sigevent * evp; 118961d3a4efSDavid Xu int * timerid; 119086857b36SDavid Xu }; 119186857b36SDavid Xu #endif 119286857b36SDavid Xu int 11938451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 119486857b36SDavid Xu { 1195643ee871SKonstantin Belousov struct sigevent *evp, ev; 119661d3a4efSDavid Xu int id; 119786857b36SDavid Xu int error; 119886857b36SDavid Xu 1199643ee871SKonstantin Belousov if (uap->evp == NULL) { 1200643ee871SKonstantin Belousov evp = NULL; 1201643ee871SKonstantin Belousov } else { 120286857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 120386857b36SDavid Xu if (error != 0) 120486857b36SDavid Xu return (error); 1205643ee871SKonstantin Belousov evp = &ev; 1206643ee871SKonstantin Belousov } 1207643ee871SKonstantin Belousov error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1); 120886857b36SDavid Xu if (error == 0) { 120961d3a4efSDavid Xu error = copyout(&id, uap->timerid, sizeof(int)); 121086857b36SDavid Xu if (error != 0) 1211643ee871SKonstantin Belousov kern_ktimer_delete(td, id); 121286857b36SDavid Xu } 121386857b36SDavid Xu return (error); 121486857b36SDavid Xu } 121586857b36SDavid Xu 1216643ee871SKonstantin Belousov int 1217643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp, 1218643ee871SKonstantin Belousov int *timerid, int preset_id) 121986857b36SDavid Xu { 122086857b36SDavid Xu struct proc *p = td->td_proc; 122186857b36SDavid Xu struct itimer *it; 122286857b36SDavid Xu int id; 122386857b36SDavid Xu int error; 122486857b36SDavid Xu 122586857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 122686857b36SDavid Xu return (EINVAL); 122786857b36SDavid Xu 122886857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 122986857b36SDavid Xu return (EINVAL); 123086857b36SDavid Xu 123186857b36SDavid Xu if (evp != NULL) { 123286857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 123356c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 123456c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 123586857b36SDavid Xu return (EINVAL); 123656c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 123756c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 123886857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 123986857b36SDavid Xu return (EINVAL); 124086857b36SDavid Xu } 124186857b36SDavid Xu 124260354683SDavid Xu if (p->p_itimers == NULL) 124386857b36SDavid Xu itimers_alloc(p); 124486857b36SDavid Xu 124586857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 124686857b36SDavid Xu it->it_flags = 0; 124786857b36SDavid Xu it->it_usecount = 0; 124886857b36SDavid Xu it->it_active = 0; 124956c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 125056c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 125186857b36SDavid Xu it->it_overrun = 0; 125286857b36SDavid Xu it->it_overrun_last = 0; 125386857b36SDavid Xu it->it_clockid = clock_id; 125486857b36SDavid Xu it->it_timerid = -1; 125586857b36SDavid Xu it->it_proc = p; 125686857b36SDavid Xu ksiginfo_init(&it->it_ksi); 125786857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 125886857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 125986857b36SDavid Xu if (error != 0) 126086857b36SDavid Xu goto out; 126186857b36SDavid Xu 126286857b36SDavid Xu PROC_LOCK(p); 126386857b36SDavid Xu if (preset_id != -1) { 126486857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 126586857b36SDavid Xu id = preset_id; 126660354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 126786857b36SDavid Xu PROC_UNLOCK(p); 126886857b36SDavid Xu error = 0; 126986857b36SDavid Xu goto out; 127086857b36SDavid Xu } 127186857b36SDavid Xu } else { 127286857b36SDavid Xu /* 127386857b36SDavid Xu * Find a free timer slot, skipping those reserved 127486857b36SDavid Xu * for setitimer(). 127586857b36SDavid Xu */ 127686857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 127760354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 127886857b36SDavid Xu break; 127986857b36SDavid Xu if (id == TIMER_MAX) { 128086857b36SDavid Xu PROC_UNLOCK(p); 128186857b36SDavid Xu error = EAGAIN; 128286857b36SDavid Xu goto out; 128386857b36SDavid Xu } 128486857b36SDavid Xu } 128586857b36SDavid Xu it->it_timerid = id; 128660354683SDavid Xu p->p_itimers->its_timers[id] = it; 128786857b36SDavid Xu if (evp != NULL) 128886857b36SDavid Xu it->it_sigev = *evp; 128986857b36SDavid Xu else { 129086857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 129186857b36SDavid Xu switch (clock_id) { 129286857b36SDavid Xu default: 129386857b36SDavid Xu case CLOCK_REALTIME: 129486857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 129586857b36SDavid Xu break; 129686857b36SDavid Xu case CLOCK_VIRTUAL: 129786857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 129886857b36SDavid Xu break; 129986857b36SDavid Xu case CLOCK_PROF: 130086857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 130186857b36SDavid Xu break; 130286857b36SDavid Xu } 13038f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 130486857b36SDavid Xu } 130586857b36SDavid Xu 130656c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 130756c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 130886857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 130986857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 131086857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 131186857b36SDavid Xu it->it_ksi.ksi_timerid = id; 131286857b36SDavid Xu } 131386857b36SDavid Xu PROC_UNLOCK(p); 131486857b36SDavid Xu *timerid = id; 131586857b36SDavid Xu return (0); 131686857b36SDavid Xu 131786857b36SDavid Xu out: 131886857b36SDavid Xu ITIMER_LOCK(it); 131986857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 132086857b36SDavid Xu ITIMER_UNLOCK(it); 132186857b36SDavid Xu uma_zfree(itimer_zone, it); 132286857b36SDavid Xu return (error); 132386857b36SDavid Xu } 132486857b36SDavid Xu 132586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 132661d3a4efSDavid Xu struct ktimer_delete_args { 132761d3a4efSDavid Xu int timerid; 132886857b36SDavid Xu }; 132986857b36SDavid Xu #endif 133086857b36SDavid Xu int 13318451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 133286857b36SDavid Xu { 1333643ee871SKonstantin Belousov 1334643ee871SKonstantin Belousov return (kern_ktimer_delete(td, uap->timerid)); 133586857b36SDavid Xu } 133686857b36SDavid Xu 133786857b36SDavid Xu static struct itimer * 1338843b99c6SDavid Xu itimer_find(struct proc *p, int timerid) 133986857b36SDavid Xu { 134086857b36SDavid Xu struct itimer *it; 134186857b36SDavid Xu 134286857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 13433f935cf3SColin Percival if ((p->p_itimers == NULL) || 13443f935cf3SColin Percival (timerid < 0) || (timerid >= TIMER_MAX) || 134560354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 134686857b36SDavid Xu return (NULL); 134786857b36SDavid Xu } 134886857b36SDavid Xu ITIMER_LOCK(it); 1349843b99c6SDavid Xu if ((it->it_flags & ITF_DELETING) != 0) { 135086857b36SDavid Xu ITIMER_UNLOCK(it); 135186857b36SDavid Xu it = NULL; 135286857b36SDavid Xu } 135386857b36SDavid Xu return (it); 135486857b36SDavid Xu } 135586857b36SDavid Xu 1356643ee871SKonstantin Belousov int 1357643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid) 135886857b36SDavid Xu { 135986857b36SDavid Xu struct proc *p = td->td_proc; 136086857b36SDavid Xu struct itimer *it; 136186857b36SDavid Xu 136286857b36SDavid Xu PROC_LOCK(p); 1363843b99c6SDavid Xu it = itimer_find(p, timerid); 136486857b36SDavid Xu if (it == NULL) { 136586857b36SDavid Xu PROC_UNLOCK(p); 136686857b36SDavid Xu return (EINVAL); 136786857b36SDavid Xu } 136886857b36SDavid Xu PROC_UNLOCK(p); 136986857b36SDavid Xu 137086857b36SDavid Xu it->it_flags |= ITF_DELETING; 137186857b36SDavid Xu while (it->it_usecount > 0) { 137286857b36SDavid Xu it->it_flags |= ITF_WANTED; 137386857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 137486857b36SDavid Xu } 137586857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 137686857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 137786857b36SDavid Xu ITIMER_UNLOCK(it); 137886857b36SDavid Xu 137986857b36SDavid Xu PROC_LOCK(p); 138086857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 138186857b36SDavid Xu sigqueue_take(&it->it_ksi); 138260354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 138386857b36SDavid Xu PROC_UNLOCK(p); 138486857b36SDavid Xu uma_zfree(itimer_zone, it); 138586857b36SDavid Xu return (0); 138686857b36SDavid Xu } 138786857b36SDavid Xu 138886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 138961d3a4efSDavid Xu struct ktimer_settime_args { 139061d3a4efSDavid Xu int timerid; 139186857b36SDavid Xu int flags; 139286857b36SDavid Xu const struct itimerspec * value; 139386857b36SDavid Xu struct itimerspec * ovalue; 139486857b36SDavid Xu }; 139586857b36SDavid Xu #endif 139686857b36SDavid Xu int 13978451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 139886857b36SDavid Xu { 139986857b36SDavid Xu struct itimerspec val, oval, *ovalp; 140086857b36SDavid Xu int error; 140186857b36SDavid Xu 140286857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 140386857b36SDavid Xu if (error != 0) 140486857b36SDavid Xu return (error); 1405643ee871SKonstantin Belousov ovalp = uap->ovalue != NULL ? &oval : NULL; 1406643ee871SKonstantin Belousov error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp); 1407643ee871SKonstantin Belousov if (error == 0 && uap->ovalue != NULL) 1408643ee871SKonstantin Belousov error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1409643ee871SKonstantin Belousov return (error); 1410643ee871SKonstantin Belousov } 141186857b36SDavid Xu 1412643ee871SKonstantin Belousov int 1413643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags, 1414643ee871SKonstantin Belousov struct itimerspec *val, struct itimerspec *oval) 1415643ee871SKonstantin Belousov { 1416643ee871SKonstantin Belousov struct proc *p; 1417643ee871SKonstantin Belousov struct itimer *it; 1418643ee871SKonstantin Belousov int error; 141986857b36SDavid Xu 1420643ee871SKonstantin Belousov p = td->td_proc; 142186857b36SDavid Xu PROC_LOCK(p); 1422643ee871SKonstantin Belousov if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) { 142386857b36SDavid Xu PROC_UNLOCK(p); 142486857b36SDavid Xu error = EINVAL; 142586857b36SDavid Xu } else { 142686857b36SDavid Xu PROC_UNLOCK(p); 142786857b36SDavid Xu itimer_enter(it); 1428643ee871SKonstantin Belousov error = CLOCK_CALL(it->it_clockid, timer_settime, (it, 1429643ee871SKonstantin Belousov flags, val, oval)); 143086857b36SDavid Xu itimer_leave(it); 143186857b36SDavid Xu ITIMER_UNLOCK(it); 143286857b36SDavid Xu } 143386857b36SDavid Xu return (error); 143486857b36SDavid Xu } 143586857b36SDavid Xu 143686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 143761d3a4efSDavid Xu struct ktimer_gettime_args { 143861d3a4efSDavid Xu int timerid; 143986857b36SDavid Xu struct itimerspec * value; 144086857b36SDavid Xu }; 144186857b36SDavid Xu #endif 144286857b36SDavid Xu int 14438451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 144486857b36SDavid Xu { 144586857b36SDavid Xu struct itimerspec val; 144686857b36SDavid Xu int error; 144786857b36SDavid Xu 1448643ee871SKonstantin Belousov error = kern_ktimer_gettime(td, uap->timerid, &val); 1449643ee871SKonstantin Belousov if (error == 0) 1450643ee871SKonstantin Belousov error = copyout(&val, uap->value, sizeof(val)); 1451643ee871SKonstantin Belousov return (error); 1452643ee871SKonstantin Belousov } 1453643ee871SKonstantin Belousov 1454643ee871SKonstantin Belousov int 1455643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val) 1456643ee871SKonstantin Belousov { 1457643ee871SKonstantin Belousov struct proc *p; 1458643ee871SKonstantin Belousov struct itimer *it; 1459643ee871SKonstantin Belousov int error; 1460643ee871SKonstantin Belousov 1461643ee871SKonstantin Belousov p = td->td_proc; 146286857b36SDavid Xu PROC_LOCK(p); 1463643ee871SKonstantin Belousov if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) { 146486857b36SDavid Xu PROC_UNLOCK(p); 146586857b36SDavid Xu error = EINVAL; 146686857b36SDavid Xu } else { 146786857b36SDavid Xu PROC_UNLOCK(p); 146886857b36SDavid Xu itimer_enter(it); 1469643ee871SKonstantin Belousov error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val)); 147086857b36SDavid Xu itimer_leave(it); 147186857b36SDavid Xu ITIMER_UNLOCK(it); 147286857b36SDavid Xu } 147386857b36SDavid Xu return (error); 147486857b36SDavid Xu } 147586857b36SDavid Xu 147686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 147786857b36SDavid Xu struct timer_getoverrun_args { 147861d3a4efSDavid Xu int timerid; 147986857b36SDavid Xu }; 148086857b36SDavid Xu #endif 148186857b36SDavid Xu int 14828451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 148386857b36SDavid Xu { 1484e346f8c4SBjoern A. Zeeb 1485e346f8c4SBjoern A. Zeeb return (kern_ktimer_getoverrun(td, uap->timerid)); 1486e346f8c4SBjoern A. Zeeb } 1487e346f8c4SBjoern A. Zeeb 1488e346f8c4SBjoern A. Zeeb int 1489e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id) 1490e346f8c4SBjoern A. Zeeb { 149186857b36SDavid Xu struct proc *p = td->td_proc; 149286857b36SDavid Xu struct itimer *it; 149386857b36SDavid Xu int error ; 149486857b36SDavid Xu 149586857b36SDavid Xu PROC_LOCK(p); 1496e346f8c4SBjoern A. Zeeb if (timer_id < 3 || 1497e346f8c4SBjoern A. Zeeb (it = itimer_find(p, timer_id)) == NULL) { 149886857b36SDavid Xu PROC_UNLOCK(p); 149986857b36SDavid Xu error = EINVAL; 150086857b36SDavid Xu } else { 150186857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 150286857b36SDavid Xu ITIMER_UNLOCK(it); 150356c06c4bSDavid Xu PROC_UNLOCK(p); 150486857b36SDavid Xu error = 0; 150586857b36SDavid Xu } 150686857b36SDavid Xu return (error); 150786857b36SDavid Xu } 150886857b36SDavid Xu 150986857b36SDavid Xu static int 151086857b36SDavid Xu realtimer_create(struct itimer *it) 151186857b36SDavid Xu { 151286857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 151386857b36SDavid Xu return (0); 151486857b36SDavid Xu } 151586857b36SDavid Xu 151686857b36SDavid Xu static int 151786857b36SDavid Xu realtimer_delete(struct itimer *it) 151886857b36SDavid Xu { 151986857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 1520843b99c6SDavid Xu 1521d6b6592eSDavid Xu /* 1522d6b6592eSDavid Xu * clear timer's value and interval to tell realtimer_expire 1523d6b6592eSDavid Xu * to not rearm the timer. 1524d6b6592eSDavid Xu */ 1525d6b6592eSDavid Xu timespecclear(&it->it_time.it_value); 1526d6b6592eSDavid Xu timespecclear(&it->it_time.it_interval); 1527843b99c6SDavid Xu ITIMER_UNLOCK(it); 1528843b99c6SDavid Xu callout_drain(&it->it_callout); 1529843b99c6SDavid Xu ITIMER_LOCK(it); 153086857b36SDavid Xu return (0); 153186857b36SDavid Xu } 153286857b36SDavid Xu 153386857b36SDavid Xu static int 153486857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 153586857b36SDavid Xu { 153656c06c4bSDavid Xu struct timespec cts; 153786857b36SDavid Xu 153886857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 153986857b36SDavid Xu 154056c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 154156c06c4bSDavid Xu *ovalue = it->it_time; 154286857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 15436040822cSAlan Somers timespecsub(&ovalue->it_value, &cts, &ovalue->it_value); 154486857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 154586857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 154686857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 154786857b36SDavid Xu ovalue->it_value.tv_sec = 0; 154886857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 154986857b36SDavid Xu } 155086857b36SDavid Xu } 155186857b36SDavid Xu return (0); 155286857b36SDavid Xu } 155386857b36SDavid Xu 155486857b36SDavid Xu static int 155560d12ef9SMark Johnston realtimer_settime(struct itimer *it, int flags, struct itimerspec *value, 155660d12ef9SMark Johnston struct itimerspec *ovalue) 155786857b36SDavid Xu { 155856c06c4bSDavid Xu struct timespec cts, ts; 155956c06c4bSDavid Xu struct timeval tv; 156056c06c4bSDavid Xu struct itimerspec val; 156186857b36SDavid Xu 156286857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 156386857b36SDavid Xu 156456c06c4bSDavid Xu val = *value; 156556c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 156686857b36SDavid Xu return (EINVAL); 156786857b36SDavid Xu 156856c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 156956c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 157086857b36SDavid Xu return (EINVAL); 157186857b36SDavid Xu } else { 157256c06c4bSDavid Xu timespecclear(&val.it_interval); 157386857b36SDavid Xu } 157486857b36SDavid Xu 157586857b36SDavid Xu if (ovalue != NULL) 157686857b36SDavid Xu realtimer_gettime(it, ovalue); 157786857b36SDavid Xu 157886857b36SDavid Xu it->it_time = val; 157956c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 158056c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 158156c06c4bSDavid Xu ts = val.it_value; 158286857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 158386857b36SDavid Xu /* Convert to absolute time. */ 15846040822cSAlan Somers timespecadd(&it->it_time.it_value, &cts, 15856040822cSAlan Somers &it->it_time.it_value); 158686857b36SDavid Xu } else { 15876040822cSAlan Somers timespecsub(&ts, &cts, &ts); 158886857b36SDavid Xu /* 158956c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 159086857b36SDavid Xu * fix it. 159186857b36SDavid Xu */ 159286857b36SDavid Xu } 159356c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 159460d12ef9SMark Johnston callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 159560d12ef9SMark Johnston it); 159686857b36SDavid Xu } else { 159786857b36SDavid Xu callout_stop(&it->it_callout); 159886857b36SDavid Xu } 159986857b36SDavid Xu 160086857b36SDavid Xu return (0); 160186857b36SDavid Xu } 160286857b36SDavid Xu 160386857b36SDavid Xu static void 160456c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 160586857b36SDavid Xu { 160686857b36SDavid Xu if (id == CLOCK_REALTIME) 160756c06c4bSDavid Xu getnanotime(ts); 160886857b36SDavid Xu else /* CLOCK_MONOTONIC */ 160956c06c4bSDavid Xu getnanouptime(ts); 161056c06c4bSDavid Xu } 161156c06c4bSDavid Xu 161256c06c4bSDavid Xu int 161361d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 161456c06c4bSDavid Xu { 161556c06c4bSDavid Xu struct itimer *it; 161656c06c4bSDavid Xu 161756c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 1618843b99c6SDavid Xu it = itimer_find(p, timerid); 161956c06c4bSDavid Xu if (it != NULL) { 162056c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 162156c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 162256c06c4bSDavid Xu it->it_overrun = 0; 162356c06c4bSDavid Xu ITIMER_UNLOCK(it); 162456c06c4bSDavid Xu return (0); 162556c06c4bSDavid Xu } 162656c06c4bSDavid Xu return (EINVAL); 162756c06c4bSDavid Xu } 162856c06c4bSDavid Xu 16298ff2b41cSMark Johnston static int 163056c06c4bSDavid Xu itimespecfix(struct timespec *ts) 163156c06c4bSDavid Xu { 163256c06c4bSDavid Xu 163356c06c4bSDavid Xu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 163456c06c4bSDavid Xu return (EINVAL); 163556c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 163656c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 163756c06c4bSDavid Xu return (0); 163886857b36SDavid Xu } 163986857b36SDavid Xu 16407995dae9SMark Johnston #define timespectons(tsp) \ 16417995dae9SMark Johnston ((uint64_t)(tsp)->tv_sec * 1000000000 + (tsp)->tv_nsec) 16427995dae9SMark Johnston #define timespecfromns(ns) (struct timespec){ \ 16437995dae9SMark Johnston .tv_sec = (ns) / 1000000000, \ 16447995dae9SMark Johnston .tv_nsec = (ns) % 1000000000 \ 16457995dae9SMark Johnston } 16467995dae9SMark Johnston 164786857b36SDavid Xu /* Timeout callback for realtime timer */ 164886857b36SDavid Xu static void 164986857b36SDavid Xu realtimer_expire(void *arg) 165086857b36SDavid Xu { 165156c06c4bSDavid Xu struct timespec cts, ts; 165256c06c4bSDavid Xu struct timeval tv; 165386857b36SDavid Xu struct itimer *it; 16547995dae9SMark Johnston uint64_t interval, now, overruns, value; 165586857b36SDavid Xu 165686857b36SDavid Xu it = (struct itimer *)arg; 165786857b36SDavid Xu 165856c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 165986857b36SDavid Xu /* Only fire if time is reached. */ 166056c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 166156c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 166256c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 16636040822cSAlan Somers &it->it_time.it_interval, 16646040822cSAlan Somers &it->it_time.it_value); 16657995dae9SMark Johnston 16667995dae9SMark Johnston interval = timespectons(&it->it_time.it_interval); 16677995dae9SMark Johnston value = timespectons(&it->it_time.it_value); 16687995dae9SMark Johnston now = timespectons(&cts); 16697995dae9SMark Johnston 16707995dae9SMark Johnston if (now >= value) { 16717995dae9SMark Johnston /* 16727995dae9SMark Johnston * We missed at least one period. 16737995dae9SMark Johnston */ 16747995dae9SMark Johnston overruns = howmany(now - value + 1, interval); 16757995dae9SMark Johnston if (it->it_overrun + overruns >= 16767995dae9SMark Johnston it->it_overrun && 16777995dae9SMark Johnston it->it_overrun + overruns <= INT_MAX) { 16787995dae9SMark Johnston it->it_overrun += (int)overruns; 16797995dae9SMark Johnston } else { 16807995dae9SMark Johnston it->it_overrun = INT_MAX; 168177e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 16827995dae9SMark Johnston } 16837995dae9SMark Johnston value = 16847995dae9SMark Johnston now + interval - (now - value) % interval; 16857995dae9SMark Johnston it->it_time.it_value = timespecfromns(value); 168686857b36SDavid Xu } 168786857b36SDavid Xu } else { 168886857b36SDavid Xu /* single shot timer ? */ 168956c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 169086857b36SDavid Xu } 169156c06c4bSDavid Xu if (timespecisset(&it->it_time.it_value)) { 16926040822cSAlan Somers timespecsub(&it->it_time.it_value, &cts, &ts); 169356c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 169456c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 169586857b36SDavid Xu realtimer_expire, it); 169686857b36SDavid Xu } 1697d6b6592eSDavid Xu itimer_enter(it); 169886857b36SDavid Xu ITIMER_UNLOCK(it); 169986857b36SDavid Xu itimer_fire(it); 170086857b36SDavid Xu ITIMER_LOCK(it); 1701d6b6592eSDavid Xu itimer_leave(it); 170256c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 170356c06c4bSDavid Xu ts = it->it_time.it_value; 17046040822cSAlan Somers timespecsub(&ts, &cts, &ts); 170556c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 170656c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 170786857b36SDavid Xu it); 170886857b36SDavid Xu } 170986857b36SDavid Xu } 171086857b36SDavid Xu 17118ff2b41cSMark Johnston static void 171286857b36SDavid Xu itimer_fire(struct itimer *it) 171386857b36SDavid Xu { 171486857b36SDavid Xu struct proc *p = it->it_proc; 1715cf7d9a8cSDavid Xu struct thread *td; 171686857b36SDavid Xu 171756c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 171856c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1719cf7d9a8cSDavid Xu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 172056c06c4bSDavid Xu ITIMER_LOCK(it); 172156c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 172256c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 172356c06c4bSDavid Xu callout_stop(&it->it_callout); 172456c06c4bSDavid Xu ITIMER_UNLOCK(it); 1725cf7d9a8cSDavid Xu return; 17266d7b314bSDavid Xu } 1727cf7d9a8cSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 1728cf7d9a8cSDavid Xu it->it_ksi.ksi_errno = 0; 1729cf7d9a8cSDavid Xu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1730cf7d9a8cSDavid Xu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 173156c06c4bSDavid Xu } else { 173277e718f7SDavid Xu if (it->it_overrun < INT_MAX) 17336d7b314bSDavid Xu it->it_overrun++; 173477e718f7SDavid Xu else 173577e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 173656c06c4bSDavid Xu } 173786857b36SDavid Xu PROC_UNLOCK(p); 173886857b36SDavid Xu } 173986857b36SDavid Xu } 174086857b36SDavid Xu 174186857b36SDavid Xu static void 174286857b36SDavid Xu itimers_alloc(struct proc *p) 174386857b36SDavid Xu { 174460354683SDavid Xu struct itimers *its; 174560354683SDavid Xu int i; 174686857b36SDavid Xu 174760354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 174886857b36SDavid Xu LIST_INIT(&its->its_virtual); 174986857b36SDavid Xu LIST_INIT(&its->its_prof); 175086857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 175160354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 175260354683SDavid Xu its->its_timers[i] = NULL; 175360354683SDavid Xu PROC_LOCK(p); 175460354683SDavid Xu if (p->p_itimers == NULL) { 175560354683SDavid Xu p->p_itimers = its; 175660354683SDavid Xu PROC_UNLOCK(p); 175760354683SDavid Xu } 175860354683SDavid Xu else { 175960354683SDavid Xu PROC_UNLOCK(p); 176060354683SDavid Xu free(its, M_SUBPROC); 176160354683SDavid Xu } 176286857b36SDavid Xu } 176386857b36SDavid Xu 176486857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 1765d26b1a1fSDavid Xu static void 1766e68c6191SKonstantin Belousov itimers_event_exit_exec(int start_idx, struct proc *p) 176786857b36SDavid Xu { 176886857b36SDavid Xu struct itimers *its; 176986857b36SDavid Xu struct itimer *it; 177086857b36SDavid Xu int i; 177186857b36SDavid Xu 177260354683SDavid Xu its = p->p_itimers; 1773e68c6191SKonstantin Belousov if (its == NULL) 1774e68c6191SKonstantin Belousov return; 1775e68c6191SKonstantin Belousov 1776e68c6191SKonstantin Belousov for (i = start_idx; i < TIMER_MAX; ++i) { 1777843b99c6SDavid Xu if ((it = its->its_timers[i]) != NULL) 1778643ee871SKonstantin Belousov kern_ktimer_delete(curthread, i); 177986857b36SDavid Xu } 1780e68c6191SKonstantin Belousov if (its->its_timers[0] == NULL && its->its_timers[1] == NULL && 178186857b36SDavid Xu its->its_timers[2] == NULL) { 178260354683SDavid Xu free(its, M_SUBPROC); 178360354683SDavid Xu p->p_itimers = NULL; 178486857b36SDavid Xu } 178586857b36SDavid Xu } 1786e68c6191SKonstantin Belousov 1787e68c6191SKonstantin Belousov void 1788e68c6191SKonstantin Belousov itimers_exec(struct proc *p) 1789e68c6191SKonstantin Belousov { 1790e68c6191SKonstantin Belousov /* 1791e68c6191SKonstantin Belousov * According to susv3, XSI interval timers should be inherited 1792e68c6191SKonstantin Belousov * by new image. 1793e68c6191SKonstantin Belousov */ 1794e68c6191SKonstantin Belousov itimers_event_exit_exec(3, p); 1795e68c6191SKonstantin Belousov } 1796e68c6191SKonstantin Belousov 1797e68c6191SKonstantin Belousov void 1798e68c6191SKonstantin Belousov itimers_exit(struct proc *p) 1799e68c6191SKonstantin Belousov { 1800e68c6191SKonstantin Belousov itimers_event_exit_exec(0, p); 180186857b36SDavid Xu } 1802