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 758b3c4231SMark Johnston #define NS_PER_SEC 1000000000 768b3c4231SMark Johnston 7786857b36SDavid Xu static struct kclock posix_clocks[MAX_CLOCKS]; 7886857b36SDavid Xu static uma_zone_t itimer_zone = NULL; 7986857b36SDavid Xu 80df8bae1dSRodney W. Grimes /* 81df8bae1dSRodney W. Grimes * Time of day and interval timer support. 82df8bae1dSRodney W. Grimes * 83df8bae1dSRodney W. Grimes * These routines provide the kernel entry points to get and set 84df8bae1dSRodney W. Grimes * the time-of-day and per-process interval timers. Subroutines 85df8bae1dSRodney W. Grimes * here provide support for adding and subtracting timeval structures 86df8bae1dSRodney W. Grimes * and decrementing interval timers, optionally reloading the interval 87df8bae1dSRodney W. Grimes * timers when they expire. 88df8bae1dSRodney W. Grimes */ 89df8bae1dSRodney W. Grimes 907edfb592SJohn Baldwin static int settime(struct thread *, struct timeval *); 914d77a549SAlfred Perlstein static void timevalfix(struct timeval *); 923f8455b0SEric van Gyzen static int user_clock_nanosleep(struct thread *td, clockid_t clock_id, 933f8455b0SEric van Gyzen int flags, const struct timespec *ua_rqtp, 943f8455b0SEric van Gyzen struct timespec *ua_rmtp); 9594c8fcd8SPeter Wemm 9686857b36SDavid Xu static void itimer_start(void); 9786857b36SDavid Xu static int itimer_init(void *, int, int); 9886857b36SDavid Xu static void itimer_fini(void *, int); 9986857b36SDavid Xu static void itimer_enter(struct itimer *); 10086857b36SDavid Xu static void itimer_leave(struct itimer *); 101843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int); 10286857b36SDavid Xu static void itimers_alloc(struct proc *); 10386857b36SDavid Xu static int realtimer_create(struct itimer *); 10486857b36SDavid Xu static int realtimer_gettime(struct itimer *, struct itimerspec *); 10586857b36SDavid Xu static int realtimer_settime(struct itimer *, int, 10686857b36SDavid Xu struct itimerspec *, struct itimerspec *); 10786857b36SDavid Xu static int realtimer_delete(struct itimer *); 10856c06c4bSDavid Xu static void realtimer_clocktime(clockid_t, struct timespec *); 10986857b36SDavid Xu static void realtimer_expire(void *); 1105cc1d199SKonstantin Belousov static void realtimer_expire_l(struct itimer *it, bool proc_locked); 11186857b36SDavid Xu 112e68c6191SKonstantin Belousov static int register_posix_clock(int, const struct kclock *); 1138ff2b41cSMark Johnston static void itimer_fire(struct itimer *it); 1148ff2b41cSMark Johnston static int itimespecfix(struct timespec *ts); 11586857b36SDavid Xu 11686857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist) \ 11786857b36SDavid Xu ((*posix_clocks[clock].call) arglist) 11886857b36SDavid Xu 11986857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 12086857b36SDavid Xu 12194c8fcd8SPeter Wemm static int 12291afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv) 12394c8fcd8SPeter Wemm { 124fcae3aa6SNick Sayer struct timeval delta, tv1, tv2; 125c0bd94a7SNick Sayer static struct timeval maxtime, laststep; 1267ec73f64SPoul-Henning Kamp struct timespec ts; 12794c8fcd8SPeter Wemm 1289c8fff87SBruce Evans microtime(&tv1); 12900af9731SPoul-Henning Kamp delta = *tv; 13000af9731SPoul-Henning Kamp timevalsub(&delta, &tv1); 13194c8fcd8SPeter Wemm 13294c8fcd8SPeter Wemm /* 1339c8fff87SBruce Evans * If the system is secure, we do not allow the time to be 134fcae3aa6SNick Sayer * set to a value earlier than 1 second less than the highest 135fcae3aa6SNick Sayer * time we have yet seen. The worst a miscreant can do in 136fcae3aa6SNick Sayer * this circumstance is "freeze" time. He couldn't go 137fcae3aa6SNick Sayer * back to the past. 138c0bd94a7SNick Sayer * 139c0bd94a7SNick Sayer * We similarly do not allow the clock to be stepped more 140c0bd94a7SNick Sayer * than one second, nor more than once per second. This allows 141c0bd94a7SNick Sayer * a miscreant to make the clock march double-time, but no worse. 14294c8fcd8SPeter Wemm */ 1437edfb592SJohn Baldwin if (securelevel_gt(td->td_ucred, 1) != 0) { 144fcae3aa6SNick Sayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 1453f92429aSMatt Jacob /* 146c0bd94a7SNick Sayer * Update maxtime to latest time we've seen. 1473f92429aSMatt Jacob */ 148fcae3aa6SNick Sayer if (tv1.tv_sec > maxtime.tv_sec) 149fcae3aa6SNick Sayer maxtime = tv1; 150fcae3aa6SNick Sayer tv2 = *tv; 151fcae3aa6SNick Sayer timevalsub(&tv2, &maxtime); 152fcae3aa6SNick Sayer if (tv2.tv_sec < -1) { 1533f92429aSMatt Jacob tv->tv_sec = maxtime.tv_sec - 1; 154fcae3aa6SNick Sayer printf("Time adjustment clamped to -1 second\n"); 155fcae3aa6SNick Sayer } 1563f92429aSMatt Jacob } else { 1571822421cSKonstantin Belousov if (tv1.tv_sec == laststep.tv_sec) 158c0bd94a7SNick Sayer return (EPERM); 159c0bd94a7SNick Sayer if (delta.tv_sec > 1) { 160c0bd94a7SNick Sayer tv->tv_sec = tv1.tv_sec + 1; 161c0bd94a7SNick Sayer printf("Time adjustment clamped to +1 second\n"); 162c0bd94a7SNick Sayer } 163c0bd94a7SNick Sayer laststep = *tv; 164fcae3aa6SNick Sayer } 1659c8fff87SBruce Evans } 1669c8fff87SBruce Evans 1677ec73f64SPoul-Henning Kamp ts.tv_sec = tv->tv_sec; 1687ec73f64SPoul-Henning Kamp ts.tv_nsec = tv->tv_usec * 1000; 16991266b96SPoul-Henning Kamp tc_setclock(&ts); 17094c8fcd8SPeter Wemm resettodr(); 17194c8fcd8SPeter Wemm return (0); 17294c8fcd8SPeter Wemm } 17394c8fcd8SPeter Wemm 17494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 175d65f1abcSDavid Xu struct clock_getcpuclockid2_args { 176d65f1abcSDavid Xu id_t id; 177d65f1abcSDavid Xu int which, 178d65f1abcSDavid Xu clockid_t *clock_id; 179d65f1abcSDavid Xu }; 180d65f1abcSDavid Xu #endif 181d65f1abcSDavid Xu /* ARGSUSED */ 182d65f1abcSDavid Xu int 183d65f1abcSDavid Xu sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap) 184d65f1abcSDavid Xu { 185d65f1abcSDavid Xu clockid_t clk_id; 186d31e4b3aSKonstantin Belousov int error; 187d31e4b3aSKonstantin Belousov 188d31e4b3aSKonstantin Belousov error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id); 189d31e4b3aSKonstantin Belousov if (error == 0) 190d31e4b3aSKonstantin Belousov error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 191d31e4b3aSKonstantin Belousov return (error); 192d31e4b3aSKonstantin Belousov } 193d31e4b3aSKonstantin Belousov 194d31e4b3aSKonstantin Belousov int 195d31e4b3aSKonstantin Belousov kern_clock_getcpuclockid2(struct thread *td, id_t id, int which, 196d31e4b3aSKonstantin Belousov clockid_t *clk_id) 197d31e4b3aSKonstantin Belousov { 198d65f1abcSDavid Xu struct proc *p; 199d65f1abcSDavid Xu pid_t pid; 200d65f1abcSDavid Xu lwpid_t tid; 201d65f1abcSDavid Xu int error; 202d65f1abcSDavid Xu 203d31e4b3aSKonstantin Belousov switch (which) { 204d65f1abcSDavid Xu case CPUCLOCK_WHICH_PID: 205d31e4b3aSKonstantin Belousov if (id != 0) { 20647ce3e53SDmitry Chagin error = pget(id, PGET_CANSEE | PGET_NOTID, &p); 207d31e4b3aSKonstantin Belousov if (error != 0) 208d65f1abcSDavid Xu return (error); 20947ce3e53SDmitry Chagin PROC_UNLOCK(p); 210d31e4b3aSKonstantin Belousov pid = id; 211d65f1abcSDavid Xu } else { 212d65f1abcSDavid Xu pid = td->td_proc->p_pid; 213d65f1abcSDavid Xu } 214d31e4b3aSKonstantin Belousov *clk_id = MAKE_PROCESS_CPUCLOCK(pid); 215d31e4b3aSKonstantin Belousov return (0); 216d65f1abcSDavid Xu case CPUCLOCK_WHICH_TID: 217d31e4b3aSKonstantin Belousov tid = id == 0 ? td->td_tid : id; 218d31e4b3aSKonstantin Belousov *clk_id = MAKE_THREAD_CPUCLOCK(tid); 219d31e4b3aSKonstantin Belousov return (0); 220d65f1abcSDavid Xu default: 221d65f1abcSDavid Xu return (EINVAL); 222d65f1abcSDavid Xu } 223d65f1abcSDavid Xu } 224d65f1abcSDavid Xu 225d65f1abcSDavid Xu #ifndef _SYS_SYSPROTO_H_ 22694c8fcd8SPeter Wemm struct clock_gettime_args { 22794c8fcd8SPeter Wemm clockid_t clock_id; 22894c8fcd8SPeter Wemm struct timespec *tp; 22994c8fcd8SPeter Wemm }; 23094c8fcd8SPeter Wemm #endif 23194c8fcd8SPeter Wemm /* ARGSUSED */ 23294c8fcd8SPeter Wemm int 2338451d0ddSKip Macy sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) 23494c8fcd8SPeter Wemm { 23594c8fcd8SPeter Wemm struct timespec ats; 236f0b479cdSPaul Saab int error; 237f0b479cdSPaul Saab 238f0b479cdSPaul Saab error = kern_clock_gettime(td, uap->clock_id, &ats); 239f0b479cdSPaul Saab if (error == 0) 240f0b479cdSPaul Saab error = copyout(&ats, uap->tp, sizeof(ats)); 241f0b479cdSPaul Saab 242f0b479cdSPaul Saab return (error); 243f0b479cdSPaul Saab } 244f0b479cdSPaul Saab 245d65f1abcSDavid Xu static inline void 246d65f1abcSDavid Xu cputick2timespec(uint64_t runtime, struct timespec *ats) 247d65f1abcSDavid Xu { 248d65f1abcSDavid Xu runtime = cputick2usec(runtime); 249d65f1abcSDavid Xu ats->tv_sec = runtime / 1000000; 250d65f1abcSDavid Xu ats->tv_nsec = runtime % 1000000 * 1000; 251d65f1abcSDavid Xu } 252d65f1abcSDavid Xu 253cbc10891SDmitry Chagin void 254cbc10891SDmitry Chagin kern_thread_cputime(struct thread *targettd, struct timespec *ats) 255d65f1abcSDavid Xu { 256d65f1abcSDavid Xu uint64_t runtime, curtime, switchtime; 257d65f1abcSDavid Xu 258d65f1abcSDavid Xu if (targettd == NULL) { /* current thread */ 259d65f1abcSDavid Xu critical_enter(); 260d65f1abcSDavid Xu switchtime = PCPU_GET(switchtime); 261d65f1abcSDavid Xu curtime = cpu_ticks(); 262d65f1abcSDavid Xu runtime = curthread->td_runtime; 263d65f1abcSDavid Xu critical_exit(); 264d65f1abcSDavid Xu runtime += curtime - switchtime; 265d65f1abcSDavid Xu } else { 2660783b709SKonstantin Belousov PROC_LOCK_ASSERT(targettd->td_proc, MA_OWNED); 267d65f1abcSDavid Xu thread_lock(targettd); 268d65f1abcSDavid Xu runtime = targettd->td_runtime; 269d65f1abcSDavid Xu thread_unlock(targettd); 270d65f1abcSDavid Xu } 271d65f1abcSDavid Xu cputick2timespec(runtime, ats); 272d65f1abcSDavid Xu } 273d65f1abcSDavid Xu 274cbc10891SDmitry Chagin void 275cbc10891SDmitry Chagin kern_process_cputime(struct proc *targetp, struct timespec *ats) 276d65f1abcSDavid Xu { 277d65f1abcSDavid Xu uint64_t runtime; 278d65f1abcSDavid Xu struct rusage ru; 279d65f1abcSDavid Xu 280cbc10891SDmitry Chagin PROC_LOCK_ASSERT(targetp, MA_OWNED); 2815c7bebf9SKonstantin Belousov PROC_STATLOCK(targetp); 282d65f1abcSDavid Xu rufetch(targetp, &ru); 283d65f1abcSDavid Xu runtime = targetp->p_rux.rux_runtime; 2847e8db781SColin Percival if (curthread->td_proc == targetp) 2857e8db781SColin Percival runtime += cpu_ticks() - PCPU_GET(switchtime); 2865c7bebf9SKonstantin Belousov PROC_STATUNLOCK(targetp); 287d65f1abcSDavid Xu cputick2timespec(runtime, ats); 288d65f1abcSDavid Xu } 289d65f1abcSDavid Xu 290d65f1abcSDavid Xu static int 291d65f1abcSDavid Xu get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) 292d65f1abcSDavid Xu { 293d65f1abcSDavid Xu struct proc *p, *p2; 294d65f1abcSDavid Xu struct thread *td2; 295d65f1abcSDavid Xu lwpid_t tid; 296d65f1abcSDavid Xu pid_t pid; 297d65f1abcSDavid Xu int error; 298d65f1abcSDavid Xu 299d65f1abcSDavid Xu p = td->td_proc; 300d65f1abcSDavid Xu if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { 301d65f1abcSDavid Xu tid = clock_id & CPUCLOCK_ID_MASK; 302d65f1abcSDavid Xu td2 = tdfind(tid, p->p_pid); 303d65f1abcSDavid Xu if (td2 == NULL) 304d65f1abcSDavid Xu return (EINVAL); 305cbc10891SDmitry Chagin kern_thread_cputime(td2, ats); 306d65f1abcSDavid Xu PROC_UNLOCK(td2->td_proc); 307d65f1abcSDavid Xu } else { 308d65f1abcSDavid Xu pid = clock_id & CPUCLOCK_ID_MASK; 309c7c536c7SKonstantin Belousov error = pget(pid, PGET_CANSEE, &p2); 310c7c536c7SKonstantin Belousov if (error != 0) 311d65f1abcSDavid Xu return (EINVAL); 312cbc10891SDmitry Chagin kern_process_cputime(p2, ats); 313d65f1abcSDavid Xu PROC_UNLOCK(p2); 314d65f1abcSDavid Xu } 315d65f1abcSDavid Xu return (0); 316d65f1abcSDavid Xu } 317d65f1abcSDavid Xu 318f0b479cdSPaul Saab int 319f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 320f0b479cdSPaul Saab { 321de0a9241SKelly Yancey struct timeval sys, user; 32278c85e8dSJohn Baldwin struct proc *p; 32394c8fcd8SPeter Wemm 32478c85e8dSJohn Baldwin p = td->td_proc; 325f0b479cdSPaul Saab switch (clock_id) { 3265e758b95SRobert Watson case CLOCK_REALTIME: /* Default to precise. */ 3275e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 328f0b479cdSPaul Saab nanotime(ats); 329b8817154SKelly Yancey break; 3305e758b95SRobert Watson case CLOCK_REALTIME_FAST: 3315e758b95SRobert Watson getnanotime(ats); 3325e758b95SRobert Watson break; 333b8817154SKelly Yancey case CLOCK_VIRTUAL: 33478c85e8dSJohn Baldwin PROC_LOCK(p); 3355c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 33678c85e8dSJohn Baldwin calcru(p, &user, &sys); 3375c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 33878c85e8dSJohn Baldwin PROC_UNLOCK(p); 339f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 340b8817154SKelly Yancey break; 341b8817154SKelly Yancey case CLOCK_PROF: 34278c85e8dSJohn Baldwin PROC_LOCK(p); 3435c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 34478c85e8dSJohn Baldwin calcru(p, &user, &sys); 3455c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 34678c85e8dSJohn Baldwin PROC_UNLOCK(p); 347de0a9241SKelly Yancey timevaladd(&user, &sys); 348f0b479cdSPaul Saab TIMEVAL_TO_TIMESPEC(&user, ats); 349de0a9241SKelly Yancey break; 3505e758b95SRobert Watson case CLOCK_MONOTONIC: /* Default to precise. */ 3515e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 3525eefd889SAndre Oppermann case CLOCK_UPTIME: 3535e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 354f0b479cdSPaul Saab nanouptime(ats); 355b8817154SKelly Yancey break; 3565e758b95SRobert Watson case CLOCK_UPTIME_FAST: 3575e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 3585e758b95SRobert Watson getnanouptime(ats); 3595e758b95SRobert Watson break; 3605e758b95SRobert Watson case CLOCK_SECOND: 3615e758b95SRobert Watson ats->tv_sec = time_second; 3625e758b95SRobert Watson ats->tv_nsec = 0; 3635e758b95SRobert Watson break; 36400d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 365cbc10891SDmitry Chagin kern_thread_cputime(NULL, ats); 366d65f1abcSDavid Xu break; 367d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 368d65f1abcSDavid Xu PROC_LOCK(p); 369cbc10891SDmitry Chagin kern_process_cputime(p, ats); 370d65f1abcSDavid Xu PROC_UNLOCK(p); 37100d6ac63SDavid Xu break; 372b8817154SKelly Yancey default: 373d65f1abcSDavid Xu if ((int)clock_id >= 0) 3745cb3dc8fSPoul-Henning Kamp return (EINVAL); 375d65f1abcSDavid Xu return (get_cputime(td, clock_id, ats)); 376b8817154SKelly Yancey } 377f0b479cdSPaul Saab return (0); 37894c8fcd8SPeter Wemm } 37994c8fcd8SPeter Wemm 38094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 38194c8fcd8SPeter Wemm struct clock_settime_args { 38294c8fcd8SPeter Wemm clockid_t clock_id; 38394c8fcd8SPeter Wemm const struct timespec *tp; 38494c8fcd8SPeter Wemm }; 38594c8fcd8SPeter Wemm #endif 38694c8fcd8SPeter Wemm /* ARGSUSED */ 38794c8fcd8SPeter Wemm int 3888451d0ddSKip Macy sys_clock_settime(struct thread *td, struct clock_settime_args *uap) 38994c8fcd8SPeter Wemm { 39094c8fcd8SPeter Wemm struct timespec ats; 39194c8fcd8SPeter Wemm int error; 39294c8fcd8SPeter Wemm 393f0b479cdSPaul Saab if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 394f0b479cdSPaul Saab return (error); 395f0b479cdSPaul Saab return (kern_clock_settime(td, uap->clock_id, &ats)); 396f0b479cdSPaul Saab } 397f0b479cdSPaul Saab 39890a79ac5SConrad Meyer static int allow_insane_settime = 0; 39990a79ac5SConrad Meyer SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN, 40090a79ac5SConrad Meyer &allow_insane_settime, 0, 40190a79ac5SConrad Meyer "do not perform possibly restrictive checks on settime(2) args"); 40290a79ac5SConrad Meyer 403f0b479cdSPaul Saab int 404f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 405f0b479cdSPaul Saab { 406f0b479cdSPaul Saab struct timeval atv; 407f0b479cdSPaul Saab int error; 408f0b479cdSPaul Saab 409acd3428bSRobert Watson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 4107edfb592SJohn Baldwin return (error); 411f0b479cdSPaul Saab if (clock_id != CLOCK_REALTIME) 4127edfb592SJohn Baldwin return (EINVAL); 4138b3c4231SMark Johnston if (ats->tv_nsec < 0 || ats->tv_nsec >= NS_PER_SEC || ats->tv_sec < 0) 4147edfb592SJohn Baldwin return (EINVAL); 415bc79b41cSMark Johnston if (!allow_insane_settime && 416bc79b41cSMark Johnston (ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60 || 417bc79b41cSMark Johnston ats->tv_sec < utc_offset())) 41890a79ac5SConrad Meyer return (EINVAL); 419a0502b19SPoul-Henning Kamp /* XXX Don't convert nsec->usec and back */ 420f0b479cdSPaul Saab TIMESPEC_TO_TIMEVAL(&atv, ats); 4217edfb592SJohn Baldwin error = settime(td, &atv); 42294c8fcd8SPeter Wemm return (error); 42394c8fcd8SPeter Wemm } 42494c8fcd8SPeter Wemm 42594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 42694c8fcd8SPeter Wemm struct clock_getres_args { 42794c8fcd8SPeter Wemm clockid_t clock_id; 42894c8fcd8SPeter Wemm struct timespec *tp; 42994c8fcd8SPeter Wemm }; 43094c8fcd8SPeter Wemm #endif 43194c8fcd8SPeter Wemm int 4328451d0ddSKip Macy sys_clock_getres(struct thread *td, struct clock_getres_args *uap) 43394c8fcd8SPeter Wemm { 43494c8fcd8SPeter Wemm struct timespec ts; 435f0b479cdSPaul Saab int error; 43694c8fcd8SPeter Wemm 437f0b479cdSPaul Saab if (uap->tp == NULL) 438f0b479cdSPaul Saab return (0); 439f0b479cdSPaul Saab 440f0b479cdSPaul Saab error = kern_clock_getres(td, uap->clock_id, &ts); 441f0b479cdSPaul Saab if (error == 0) 442f0b479cdSPaul Saab error = copyout(&ts, uap->tp, sizeof(ts)); 443f0b479cdSPaul Saab return (error); 444f0b479cdSPaul Saab } 445f0b479cdSPaul Saab 446f0b479cdSPaul Saab int 447f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 448f0b479cdSPaul Saab { 449f0b479cdSPaul Saab 450f0b479cdSPaul Saab ts->tv_sec = 0; 451f0b479cdSPaul Saab switch (clock_id) { 452b8817154SKelly Yancey case CLOCK_REALTIME: 4535e758b95SRobert Watson case CLOCK_REALTIME_FAST: 4545e758b95SRobert Watson case CLOCK_REALTIME_PRECISE: 455b8817154SKelly Yancey case CLOCK_MONOTONIC: 4565e758b95SRobert Watson case CLOCK_MONOTONIC_FAST: 4575e758b95SRobert Watson case CLOCK_MONOTONIC_PRECISE: 4585eefd889SAndre Oppermann case CLOCK_UPTIME: 4595e758b95SRobert Watson case CLOCK_UPTIME_FAST: 4605e758b95SRobert Watson case CLOCK_UPTIME_PRECISE: 461ac0653dcSBruce Evans /* 462ac0653dcSBruce Evans * Round up the result of the division cheaply by adding 1. 463ac0653dcSBruce Evans * Rounding up is especially important if rounding down 464ac0653dcSBruce Evans * would give 0. Perfect rounding is unimportant. 465ac0653dcSBruce Evans */ 4668b3c4231SMark Johnston ts->tv_nsec = NS_PER_SEC / tc_getfrequency() + 1; 467b8817154SKelly Yancey break; 468b8817154SKelly Yancey case CLOCK_VIRTUAL: 469b8817154SKelly Yancey case CLOCK_PROF: 470b8817154SKelly Yancey /* Accurately round up here because we can do so cheaply. */ 4718b3c4231SMark Johnston ts->tv_nsec = howmany(NS_PER_SEC, hz); 472b8817154SKelly Yancey break; 4735e758b95SRobert Watson case CLOCK_SECOND: 4745e758b95SRobert Watson ts->tv_sec = 1; 4755e758b95SRobert Watson ts->tv_nsec = 0; 4765e758b95SRobert Watson break; 47700d6ac63SDavid Xu case CLOCK_THREAD_CPUTIME_ID: 478d65f1abcSDavid Xu case CLOCK_PROCESS_CPUTIME_ID: 479d65f1abcSDavid Xu cputime: 48000d6ac63SDavid Xu /* sync with cputick2usec */ 48100d6ac63SDavid Xu ts->tv_nsec = 1000000 / cpu_tickrate(); 48200d6ac63SDavid Xu if (ts->tv_nsec == 0) 48300d6ac63SDavid Xu ts->tv_nsec = 1000; 48400d6ac63SDavid Xu break; 485b8817154SKelly Yancey default: 486d65f1abcSDavid Xu if ((int)clock_id < 0) 487d65f1abcSDavid Xu goto cputime; 488b8817154SKelly Yancey return (EINVAL); 48994c8fcd8SPeter Wemm } 490de0a9241SKelly Yancey return (0); 49194c8fcd8SPeter Wemm } 49294c8fcd8SPeter Wemm 4937fdf2c85SPaul Saab int 4947fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 4955b870b7bSPeter Wemm { 4963f8455b0SEric van Gyzen 4973f8455b0SEric van Gyzen return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt, 4983f8455b0SEric van Gyzen rmt)); 4993f8455b0SEric van Gyzen } 5003f8455b0SEric van Gyzen 5013f8455b0SEric van Gyzen static uint8_t nanowait[MAXCPU]; 5023f8455b0SEric van Gyzen 5033f8455b0SEric van Gyzen int 5043f8455b0SEric van Gyzen kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags, 5053f8455b0SEric van Gyzen const struct timespec *rqt, struct timespec *rmt) 5063f8455b0SEric van Gyzen { 5073f8455b0SEric van Gyzen struct timespec ts, now; 508098176f0SDavide Italiano sbintime_t sbt, sbtt, prec, tmp; 509980c545dSAlexander Motin time_t over; 51033841826SPoul-Henning Kamp int error; 5113f8455b0SEric van Gyzen bool is_abs_real; 5125b870b7bSPeter Wemm 5138b3c4231SMark Johnston if (rqt->tv_nsec < 0 || rqt->tv_nsec >= NS_PER_SEC) 514708e7684SPeter Wemm return (EINVAL); 5153f8455b0SEric van Gyzen if ((flags & ~TIMER_ABSTIME) != 0) 5163f8455b0SEric van Gyzen return (EINVAL); 5173f8455b0SEric van Gyzen switch (clock_id) { 5183f8455b0SEric van Gyzen case CLOCK_REALTIME: 5193f8455b0SEric van Gyzen case CLOCK_REALTIME_PRECISE: 5203f8455b0SEric van Gyzen case CLOCK_REALTIME_FAST: 5213f8455b0SEric van Gyzen case CLOCK_SECOND: 5223f8455b0SEric van Gyzen is_abs_real = (flags & TIMER_ABSTIME) != 0; 5233f8455b0SEric van Gyzen break; 5243f8455b0SEric van Gyzen case CLOCK_MONOTONIC: 5253f8455b0SEric van Gyzen case CLOCK_MONOTONIC_PRECISE: 5263f8455b0SEric van Gyzen case CLOCK_MONOTONIC_FAST: 5273f8455b0SEric van Gyzen case CLOCK_UPTIME: 5283f8455b0SEric van Gyzen case CLOCK_UPTIME_PRECISE: 5293f8455b0SEric van Gyzen case CLOCK_UPTIME_FAST: 5303f8455b0SEric van Gyzen is_abs_real = false; 5313f8455b0SEric van Gyzen break; 5323f8455b0SEric van Gyzen case CLOCK_VIRTUAL: 5333f8455b0SEric van Gyzen case CLOCK_PROF: 5343f8455b0SEric van Gyzen case CLOCK_PROCESS_CPUTIME_ID: 5353f8455b0SEric van Gyzen return (ENOTSUP); 5363f8455b0SEric van Gyzen case CLOCK_THREAD_CPUTIME_ID: 5373f8455b0SEric van Gyzen default: 5383f8455b0SEric van Gyzen return (EINVAL); 5393f8455b0SEric van Gyzen } 5403f8455b0SEric van Gyzen do { 541980c545dSAlexander Motin ts = *rqt; 5423f8455b0SEric van Gyzen if ((flags & TIMER_ABSTIME) != 0) { 5433f8455b0SEric van Gyzen if (is_abs_real) 5443f8455b0SEric van Gyzen td->td_rtcgen = 5453f8455b0SEric van Gyzen atomic_load_acq_int(&rtc_generation); 5463f8455b0SEric van Gyzen error = kern_clock_gettime(td, clock_id, &now); 5473f8455b0SEric van Gyzen KASSERT(error == 0, ("kern_clock_gettime: %d", error)); 5486040822cSAlan Somers timespecsub(&ts, &now, &ts); 5493f8455b0SEric van Gyzen } 5503f8455b0SEric van Gyzen if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) { 5513f8455b0SEric van Gyzen error = EWOULDBLOCK; 5523f8455b0SEric van Gyzen break; 5533f8455b0SEric van Gyzen } 554980c545dSAlexander Motin if (ts.tv_sec > INT32_MAX / 2) { 555980c545dSAlexander Motin over = ts.tv_sec - INT32_MAX / 2; 556980c545dSAlexander Motin ts.tv_sec -= over; 557980c545dSAlexander Motin } else 558980c545dSAlexander Motin over = 0; 559980c545dSAlexander Motin tmp = tstosbt(ts); 560098176f0SDavide Italiano prec = tmp; 561098176f0SDavide Italiano prec >>= tc_precexp; 562098176f0SDavide Italiano if (TIMESEL(&sbt, tmp)) 563098176f0SDavide Italiano sbt += tc_tick_sbt; 564098176f0SDavide Italiano sbt += tmp; 5650dbf17e6SAlexander Motin error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp", 5660dbf17e6SAlexander Motin sbt, prec, C_ABSOLUTE); 5673f8455b0SEric van Gyzen } while (error == 0 && is_abs_real && td->td_rtcgen == 0); 5683f8455b0SEric van Gyzen td->td_rtcgen = 0; 56933841826SPoul-Henning Kamp if (error != EWOULDBLOCK) { 57070c144dcSBryan Drewery if (TIMESEL(&sbtt, tmp)) 57170c144dcSBryan Drewery sbtt += tc_tick_sbt; 5723f8455b0SEric van Gyzen if (sbtt >= sbt) 5733f8455b0SEric van Gyzen return (0); 57433841826SPoul-Henning Kamp if (error == ERESTART) 57594c8fcd8SPeter Wemm error = EINTR; 5763f8455b0SEric van Gyzen if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) { 577098176f0SDavide Italiano ts = sbttots(sbt - sbtt); 578980c545dSAlexander Motin ts.tv_sec += over; 57933841826SPoul-Henning Kamp if (ts.tv_sec < 0) 58033841826SPoul-Henning Kamp timespecclear(&ts); 58100af9731SPoul-Henning Kamp *rmt = ts; 58200af9731SPoul-Henning Kamp } 58333841826SPoul-Henning Kamp return (error); 58433841826SPoul-Henning Kamp } 58533841826SPoul-Henning Kamp return (0); 5865b870b7bSPeter Wemm } 58794c8fcd8SPeter Wemm 5885b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 5895b870b7bSPeter Wemm struct nanosleep_args { 5905b870b7bSPeter Wemm struct timespec *rqtp; 5915b870b7bSPeter Wemm struct timespec *rmtp; 5925b870b7bSPeter Wemm }; 5935b870b7bSPeter Wemm #endif 5945b870b7bSPeter Wemm /* ARGSUSED */ 5955b870b7bSPeter Wemm int 5968451d0ddSKip Macy sys_nanosleep(struct thread *td, struct nanosleep_args *uap) 5975b870b7bSPeter Wemm { 5983f8455b0SEric van Gyzen 5993f8455b0SEric van Gyzen return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, 6003f8455b0SEric van Gyzen uap->rqtp, uap->rmtp)); 6013f8455b0SEric van Gyzen } 6023f8455b0SEric van Gyzen 6033f8455b0SEric van Gyzen #ifndef _SYS_SYSPROTO_H_ 6043f8455b0SEric van Gyzen struct clock_nanosleep_args { 6053f8455b0SEric van Gyzen clockid_t clock_id; 6063f8455b0SEric van Gyzen int flags; 6073f8455b0SEric van Gyzen struct timespec *rqtp; 6083f8455b0SEric van Gyzen struct timespec *rmtp; 6093f8455b0SEric van Gyzen }; 6103f8455b0SEric van Gyzen #endif 6113f8455b0SEric van Gyzen /* ARGSUSED */ 6123f8455b0SEric van Gyzen int 6133f8455b0SEric van Gyzen sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap) 6143f8455b0SEric van Gyzen { 6153f8455b0SEric van Gyzen int error; 6163f8455b0SEric van Gyzen 6173f8455b0SEric van Gyzen error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp, 6183f8455b0SEric van Gyzen uap->rmtp); 6193f8455b0SEric van Gyzen return (kern_posix_error(td, error)); 6203f8455b0SEric van Gyzen } 6213f8455b0SEric van Gyzen 6223f8455b0SEric van Gyzen static int 6233f8455b0SEric van Gyzen user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags, 6243f8455b0SEric van Gyzen const struct timespec *ua_rqtp, struct timespec *ua_rmtp) 6253f8455b0SEric van Gyzen { 6265b870b7bSPeter Wemm struct timespec rmt, rqt; 627618a20d4SBrooks Davis int error, error2; 6285b870b7bSPeter Wemm 6293f8455b0SEric van Gyzen error = copyin(ua_rqtp, &rqt, sizeof(rqt)); 6305b870b7bSPeter Wemm if (error) 6315b870b7bSPeter Wemm return (error); 6323f8455b0SEric van Gyzen error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt); 6333f8455b0SEric van Gyzen if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) { 6343f8455b0SEric van Gyzen error2 = copyout(&rmt, ua_rmtp, sizeof(rmt)); 635618a20d4SBrooks Davis if (error2 != 0) 636fb99ab88SMatthew Dillon error = error2; 637708e7684SPeter Wemm } 638708e7684SPeter Wemm return (error); 63994c8fcd8SPeter Wemm } 64094c8fcd8SPeter Wemm 6415b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_ 642df8bae1dSRodney W. Grimes struct gettimeofday_args { 643df8bae1dSRodney W. Grimes struct timeval *tp; 644df8bae1dSRodney W. Grimes struct timezone *tzp; 645df8bae1dSRodney W. Grimes }; 646d2d3e875SBruce Evans #endif 647df8bae1dSRodney W. Grimes /* ARGSUSED */ 64826f9a767SRodney W. Grimes int 6498451d0ddSKip Macy sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap) 650df8bae1dSRodney W. Grimes { 651df8bae1dSRodney W. Grimes struct timeval atv; 652411c25edSTim J. Robbins struct timezone rtz; 653df8bae1dSRodney W. Grimes int error = 0; 654df8bae1dSRodney W. Grimes 655df8bae1dSRodney W. Grimes if (uap->tp) { 656df8bae1dSRodney W. Grimes microtime(&atv); 65701609114SAlfred Perlstein error = copyout(&atv, uap->tp, sizeof (atv)); 658df8bae1dSRodney W. Grimes } 65921dcdb38SPoul-Henning Kamp if (error == 0 && uap->tzp != NULL) { 660329f0aa9SWarner Losh rtz.tz_minuteswest = 0; 661329f0aa9SWarner Losh rtz.tz_dsttime = 0; 662411c25edSTim J. Robbins error = copyout(&rtz, uap->tzp, sizeof (rtz)); 66321dcdb38SPoul-Henning Kamp } 664df8bae1dSRodney W. Grimes return (error); 665df8bae1dSRodney W. Grimes } 666df8bae1dSRodney W. Grimes 667d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 668df8bae1dSRodney W. Grimes struct settimeofday_args { 669df8bae1dSRodney W. Grimes struct timeval *tv; 670df8bae1dSRodney W. Grimes struct timezone *tzp; 671df8bae1dSRodney W. Grimes }; 672d2d3e875SBruce Evans #endif 673df8bae1dSRodney W. Grimes /* ARGSUSED */ 67426f9a767SRodney W. Grimes int 6758451d0ddSKip Macy sys_settimeofday(struct thread *td, struct settimeofday_args *uap) 676df8bae1dSRodney W. Grimes { 677b88ec951SJohn Baldwin struct timeval atv, *tvp; 678b88ec951SJohn Baldwin struct timezone atz, *tzp; 679b88ec951SJohn Baldwin int error; 680b88ec951SJohn Baldwin 681b88ec951SJohn Baldwin if (uap->tv) { 682b88ec951SJohn Baldwin error = copyin(uap->tv, &atv, sizeof(atv)); 683b88ec951SJohn Baldwin if (error) 684b88ec951SJohn Baldwin return (error); 685b88ec951SJohn Baldwin tvp = &atv; 686b88ec951SJohn Baldwin } else 687b88ec951SJohn Baldwin tvp = NULL; 688b88ec951SJohn Baldwin if (uap->tzp) { 689b88ec951SJohn Baldwin error = copyin(uap->tzp, &atz, sizeof(atz)); 690b88ec951SJohn Baldwin if (error) 691b88ec951SJohn Baldwin return (error); 692b88ec951SJohn Baldwin tzp = &atz; 693b88ec951SJohn Baldwin } else 694b88ec951SJohn Baldwin tzp = NULL; 695b88ec951SJohn Baldwin return (kern_settimeofday(td, tvp, tzp)); 696b88ec951SJohn Baldwin } 697b88ec951SJohn Baldwin 698b88ec951SJohn Baldwin int 699b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 700b88ec951SJohn Baldwin { 701b88ec951SJohn Baldwin int error; 702fb99ab88SMatthew Dillon 703acd3428bSRobert Watson error = priv_check(td, PRIV_SETTIMEOFDAY); 704b88ec951SJohn Baldwin if (error) 7057edfb592SJohn Baldwin return (error); 706df8bae1dSRodney W. Grimes /* Verify all parameters before changing time. */ 707b88ec951SJohn Baldwin if (tv) { 7083e18d701SDmitry Chagin if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 || 7093e18d701SDmitry Chagin tv->tv_sec < 0) 7107edfb592SJohn Baldwin return (EINVAL); 711b88ec951SJohn Baldwin error = settime(td, tv); 712708e7684SPeter Wemm } 7137edfb592SJohn Baldwin return (error); 714b88ec951SJohn Baldwin } 7157edfb592SJohn Baldwin 716df8bae1dSRodney W. Grimes /* 717873fbcd7SRobert Watson * Get value of an interval timer. The process virtual and profiling virtual 718873fbcd7SRobert Watson * time timers are kept in the p_stats area, since they can be swapped out. 719873fbcd7SRobert Watson * These are kept internally in the way they are specified externally: in 720873fbcd7SRobert Watson * time until they expire. 721df8bae1dSRodney W. Grimes * 722873fbcd7SRobert Watson * The real time interval timer is kept in the process table slot for the 723873fbcd7SRobert Watson * process, and its value (it_value) is kept as an absolute time rather than 724873fbcd7SRobert Watson * as a delta, so that it is easy to keep periodic real-time signals from 725873fbcd7SRobert Watson * drifting. 726df8bae1dSRodney W. Grimes * 727df8bae1dSRodney W. Grimes * Virtual time timers are processed in the hardclock() routine of 728873fbcd7SRobert Watson * kern_clock.c. The real time timer is processed by a timeout routine, 729873fbcd7SRobert Watson * called from the softclock() routine. Since a callout may be delayed in 730873fbcd7SRobert Watson * real time due to interrupt processing in the system, it is possible for 731873fbcd7SRobert Watson * the real time timeout routine (realitexpire, given below), to be delayed 732873fbcd7SRobert Watson * in real time past when it is supposed to occur. It does not suffice, 733873fbcd7SRobert Watson * therefore, to reload the real timer .it_value from the real time timers 734873fbcd7SRobert Watson * .it_interval. Rather, we compute the next time in absolute time the timer 735873fbcd7SRobert Watson * should go off. 736df8bae1dSRodney W. Grimes */ 737d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 738df8bae1dSRodney W. Grimes struct getitimer_args { 739df8bae1dSRodney W. Grimes u_int which; 740df8bae1dSRodney W. Grimes struct itimerval *itv; 741df8bae1dSRodney W. Grimes }; 742d2d3e875SBruce Evans #endif 74326f9a767SRodney W. Grimes int 7448451d0ddSKip Macy sys_getitimer(struct thread *td, struct getitimer_args *uap) 745df8bae1dSRodney W. Grimes { 746df8bae1dSRodney W. Grimes struct itimerval aitv; 747c90110d6SJohn Baldwin int error; 748df8bae1dSRodney W. Grimes 749cfa0efe7SMaxim Sobolev error = kern_getitimer(td, uap->which, &aitv); 750cfa0efe7SMaxim Sobolev if (error != 0) 751cfa0efe7SMaxim Sobolev return (error); 752cfa0efe7SMaxim Sobolev return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 753cfa0efe7SMaxim Sobolev } 754cfa0efe7SMaxim Sobolev 755cfa0efe7SMaxim Sobolev int 756cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 757cfa0efe7SMaxim Sobolev { 758cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 759cfa0efe7SMaxim Sobolev struct timeval ctv; 760cfa0efe7SMaxim Sobolev 761cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 762df8bae1dSRodney W. Grimes return (EINVAL); 763fb99ab88SMatthew Dillon 764cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 765df8bae1dSRodney W. Grimes /* 766ee002b68SBruce Evans * Convert from absolute to relative time in .it_value 767df8bae1dSRodney W. Grimes * part of real time timer. If time for real time timer 768df8bae1dSRodney W. Grimes * has passed return 0, else return difference between 769df8bae1dSRodney W. Grimes * current time and time for the timer to go off. 770df8bae1dSRodney W. Grimes */ 77196d7f8efSTim J. Robbins PROC_LOCK(p); 772cfa0efe7SMaxim Sobolev *aitv = p->p_realtimer; 77396d7f8efSTim J. Robbins PROC_UNLOCK(p); 774cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 775b5ea3779SAlexander Motin microuptime(&ctv); 776cfa0efe7SMaxim Sobolev if (timevalcmp(&aitv->it_value, &ctv, <)) 777cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_value); 778df8bae1dSRodney W. Grimes else 779cfa0efe7SMaxim Sobolev timevalsub(&aitv->it_value, &ctv); 780227ee8a1SPoul-Henning Kamp } 781fb99ab88SMatthew Dillon } else { 7825c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 783cfa0efe7SMaxim Sobolev *aitv = p->p_stats->p_timer[which]; 7845c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 785fb99ab88SMatthew Dillon } 786de56aee0SKonstantin Belousov #ifdef KTRACE 787de56aee0SKonstantin Belousov if (KTRPOINT(td, KTR_STRUCT)) 788de56aee0SKonstantin Belousov ktritimerval(aitv); 789de56aee0SKonstantin Belousov #endif 790cfa0efe7SMaxim Sobolev return (0); 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes 793d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 794df8bae1dSRodney W. Grimes struct setitimer_args { 795df8bae1dSRodney W. Grimes u_int which; 796df8bae1dSRodney W. Grimes struct itimerval *itv, *oitv; 797df8bae1dSRodney W. Grimes }; 798d2d3e875SBruce Evans #endif 79926f9a767SRodney W. Grimes int 8008451d0ddSKip Macy sys_setitimer(struct thread *td, struct setitimer_args *uap) 801df8bae1dSRodney W. Grimes { 802cfa0efe7SMaxim Sobolev struct itimerval aitv, oitv; 803c90110d6SJohn Baldwin int error; 80496d7f8efSTim J. Robbins 80596d7f8efSTim J. Robbins if (uap->itv == NULL) { 80696d7f8efSTim J. Robbins uap->itv = uap->oitv; 8078451d0ddSKip Macy return (sys_getitimer(td, (struct getitimer_args *)uap)); 80896d7f8efSTim J. Robbins } 809df8bae1dSRodney W. Grimes 81096d7f8efSTim J. Robbins if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 811df8bae1dSRodney W. Grimes return (error); 812cfa0efe7SMaxim Sobolev error = kern_setitimer(td, uap->which, &aitv, &oitv); 813cfa0efe7SMaxim Sobolev if (error != 0 || uap->oitv == NULL) 814cfa0efe7SMaxim Sobolev return (error); 815cfa0efe7SMaxim Sobolev return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 816cfa0efe7SMaxim Sobolev } 817cfa0efe7SMaxim Sobolev 818cfa0efe7SMaxim Sobolev int 819c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 820c90110d6SJohn Baldwin struct itimerval *oitv) 821cfa0efe7SMaxim Sobolev { 822cfa0efe7SMaxim Sobolev struct proc *p = td->td_proc; 823cfa0efe7SMaxim Sobolev struct timeval ctv; 824b5ea3779SAlexander Motin sbintime_t sbt, pr; 825cfa0efe7SMaxim Sobolev 8265e85ac17SJohn Baldwin if (aitv == NULL) 8275e85ac17SJohn Baldwin return (kern_getitimer(td, which, oitv)); 8285e85ac17SJohn Baldwin 829cfa0efe7SMaxim Sobolev if (which > ITIMER_PROF) 83096d7f8efSTim J. Robbins return (EINVAL); 831de56aee0SKonstantin Belousov #ifdef KTRACE 832de56aee0SKonstantin Belousov if (KTRPOINT(td, KTR_STRUCT)) 833de56aee0SKonstantin Belousov ktritimerval(aitv); 834de56aee0SKonstantin Belousov #endif 835b5ea3779SAlexander Motin if (itimerfix(&aitv->it_value) || 836b5ea3779SAlexander Motin aitv->it_value.tv_sec > INT32_MAX / 2) 837cfa0efe7SMaxim Sobolev return (EINVAL); 838cfa0efe7SMaxim Sobolev if (!timevalisset(&aitv->it_value)) 839cfa0efe7SMaxim Sobolev timevalclear(&aitv->it_interval); 840b5ea3779SAlexander Motin else if (itimerfix(&aitv->it_interval) || 841b5ea3779SAlexander Motin aitv->it_interval.tv_sec > INT32_MAX / 2) 84296d7f8efSTim J. Robbins return (EINVAL); 84396d7f8efSTim J. Robbins 844cfa0efe7SMaxim Sobolev if (which == ITIMER_REAL) { 84596d7f8efSTim J. Robbins PROC_LOCK(p); 8464cf41af3SPoul-Henning Kamp if (timevalisset(&p->p_realtimer.it_value)) 8474f559836SJake Burkholder callout_stop(&p->p_itcallout); 848b5ea3779SAlexander Motin microuptime(&ctv); 849cfa0efe7SMaxim Sobolev if (timevalisset(&aitv->it_value)) { 850b5ea3779SAlexander Motin pr = tvtosbt(aitv->it_value) >> tc_precexp; 851cfa0efe7SMaxim Sobolev timevaladd(&aitv->it_value, &ctv); 852b5ea3779SAlexander Motin sbt = tvtosbt(aitv->it_value); 853b5ea3779SAlexander Motin callout_reset_sbt(&p->p_itcallout, sbt, pr, 854b5ea3779SAlexander Motin realitexpire, p, C_ABSOLUTE); 85525b4d3a8SJohn Baldwin } 856cfa0efe7SMaxim Sobolev *oitv = p->p_realtimer; 857cfa0efe7SMaxim Sobolev p->p_realtimer = *aitv; 85896d7f8efSTim J. Robbins PROC_UNLOCK(p); 859cfa0efe7SMaxim Sobolev if (timevalisset(&oitv->it_value)) { 860cfa0efe7SMaxim Sobolev if (timevalcmp(&oitv->it_value, &ctv, <)) 861cfa0efe7SMaxim Sobolev timevalclear(&oitv->it_value); 86296d7f8efSTim J. Robbins else 863cfa0efe7SMaxim Sobolev timevalsub(&oitv->it_value, &ctv); 864fb99ab88SMatthew Dillon } 86596d7f8efSTim J. Robbins } else { 866d10a1df8SAlexander Motin if (aitv->it_interval.tv_sec == 0 && 867d10a1df8SAlexander Motin aitv->it_interval.tv_usec != 0 && 868d10a1df8SAlexander Motin aitv->it_interval.tv_usec < tick) 869d10a1df8SAlexander Motin aitv->it_interval.tv_usec = tick; 870d10a1df8SAlexander Motin if (aitv->it_value.tv_sec == 0 && 871d10a1df8SAlexander Motin aitv->it_value.tv_usec != 0 && 872d10a1df8SAlexander Motin aitv->it_value.tv_usec < tick) 873d10a1df8SAlexander Motin aitv->it_value.tv_usec = tick; 8745c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 875cfa0efe7SMaxim Sobolev *oitv = p->p_stats->p_timer[which]; 876cfa0efe7SMaxim Sobolev p->p_stats->p_timer[which] = *aitv; 8775c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 87896d7f8efSTim J. Robbins } 879de56aee0SKonstantin Belousov #ifdef KTRACE 880de56aee0SKonstantin Belousov if (KTRPOINT(td, KTR_STRUCT)) 881de56aee0SKonstantin Belousov ktritimerval(oitv); 882de56aee0SKonstantin Belousov #endif 88396d7f8efSTim J. Robbins return (0); 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes 886dc47fdf1SKonstantin Belousov static void 887dc47fdf1SKonstantin Belousov realitexpire_reset_callout(struct proc *p, sbintime_t *isbtp) 888dc47fdf1SKonstantin Belousov { 889dc47fdf1SKonstantin Belousov sbintime_t prec; 890dc47fdf1SKonstantin Belousov 891dc47fdf1SKonstantin Belousov prec = isbtp == NULL ? tvtosbt(p->p_realtimer.it_interval) : *isbtp; 892dc47fdf1SKonstantin Belousov callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value), 893dc47fdf1SKonstantin Belousov prec >> tc_precexp, realitexpire, p, C_ABSOLUTE); 894dc47fdf1SKonstantin Belousov } 895dc47fdf1SKonstantin Belousov 896dc47fdf1SKonstantin Belousov void 897dc47fdf1SKonstantin Belousov itimer_proc_continue(struct proc *p) 898dc47fdf1SKonstantin Belousov { 899dc47fdf1SKonstantin Belousov struct timeval ctv; 9004d27d8d2SKonstantin Belousov struct itimer *it; 9014d27d8d2SKonstantin Belousov int id; 902dc47fdf1SKonstantin Belousov 903dc47fdf1SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 904dc47fdf1SKonstantin Belousov 905dc47fdf1SKonstantin Belousov if ((p->p_flag2 & P2_ITSTOPPED) != 0) { 906dc47fdf1SKonstantin Belousov p->p_flag2 &= ~P2_ITSTOPPED; 907dc47fdf1SKonstantin Belousov microuptime(&ctv); 908dc47fdf1SKonstantin Belousov if (timevalcmp(&p->p_realtimer.it_value, &ctv, >=)) 909dc47fdf1SKonstantin Belousov realitexpire(p); 910dc47fdf1SKonstantin Belousov else 911dc47fdf1SKonstantin Belousov realitexpire_reset_callout(p, NULL); 912dc47fdf1SKonstantin Belousov } 9134d27d8d2SKonstantin Belousov 9144d27d8d2SKonstantin Belousov if (p->p_itimers != NULL) { 9154d27d8d2SKonstantin Belousov for (id = 3; id < TIMER_MAX; id++) { 9164d27d8d2SKonstantin Belousov it = p->p_itimers->its_timers[id]; 9174d27d8d2SKonstantin Belousov if (it == NULL) 9184d27d8d2SKonstantin Belousov continue; 9194d27d8d2SKonstantin Belousov if ((it->it_flags & ITF_PSTOPPED) != 0) { 9204d27d8d2SKonstantin Belousov ITIMER_LOCK(it); 9214d27d8d2SKonstantin Belousov if ((it->it_flags & ITF_PSTOPPED) != 0) { 9224d27d8d2SKonstantin Belousov it->it_flags &= ~ITF_PSTOPPED; 9234d27d8d2SKonstantin Belousov if ((it->it_flags & ITF_DELETING) == 0) 9245cc1d199SKonstantin Belousov realtimer_expire_l(it, true); 9254d27d8d2SKonstantin Belousov } 9264d27d8d2SKonstantin Belousov ITIMER_UNLOCK(it); 9274d27d8d2SKonstantin Belousov } 9284d27d8d2SKonstantin Belousov } 9294d27d8d2SKonstantin Belousov } 930dc47fdf1SKonstantin Belousov } 931dc47fdf1SKonstantin Belousov 932df8bae1dSRodney W. Grimes /* 933df8bae1dSRodney W. Grimes * Real interval timer expired: 934df8bae1dSRodney W. Grimes * send process whose timer expired an alarm signal. 935df8bae1dSRodney W. Grimes * If time is not set up to reload, then just return. 936df8bae1dSRodney W. Grimes * Else compute next time timer should go off which is > current time. 937df8bae1dSRodney W. Grimes * This is where delay in processing this timeout causes multiple 938df8bae1dSRodney W. Grimes * SIGALRM calls to be compressed into one. 939c8b47828SBruce Evans * tvtohz() always adds 1 to allow for the time until the next clock 9409207f00aSBruce Evans * interrupt being strictly less than 1 clock tick, but we don't want 9419207f00aSBruce Evans * that here since we want to appear to be in sync with the clock 9429207f00aSBruce Evans * interrupt even when we're delayed. 943df8bae1dSRodney W. Grimes */ 944df8bae1dSRodney W. Grimes void 94591afe087SPoul-Henning Kamp realitexpire(void *arg) 946df8bae1dSRodney W. Grimes { 94791afe087SPoul-Henning Kamp struct proc *p; 948b5ea3779SAlexander Motin struct timeval ctv; 949b5ea3779SAlexander Motin sbintime_t isbt; 950df8bae1dSRodney W. Grimes 951df8bae1dSRodney W. Grimes p = (struct proc *)arg; 9528451d0ddSKip Macy kern_psignal(p, SIGALRM); 9534cf41af3SPoul-Henning Kamp if (!timevalisset(&p->p_realtimer.it_interval)) { 9544cf41af3SPoul-Henning Kamp timevalclear(&p->p_realtimer.it_value); 9555499ea01SJohn Baldwin if (p->p_flag & P_WEXIT) 9565499ea01SJohn Baldwin wakeup(&p->p_itcallout); 957df8bae1dSRodney W. Grimes return; 958df8bae1dSRodney W. Grimes } 959dc47fdf1SKonstantin Belousov 960b5ea3779SAlexander Motin isbt = tvtosbt(p->p_realtimer.it_interval); 961b5ea3779SAlexander Motin if (isbt >= sbt_timethreshold) 962b5ea3779SAlexander Motin getmicrouptime(&ctv); 963b5ea3779SAlexander Motin else 964b5ea3779SAlexander Motin microuptime(&ctv); 965b5ea3779SAlexander Motin do { 966df8bae1dSRodney W. Grimes timevaladd(&p->p_realtimer.it_value, 967df8bae1dSRodney W. Grimes &p->p_realtimer.it_interval); 968b5ea3779SAlexander Motin } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=)); 969dc47fdf1SKonstantin Belousov 970dc47fdf1SKonstantin Belousov if (P_SHOULDSTOP(p) || P_KILLED(p)) { 971dc47fdf1SKonstantin Belousov p->p_flag2 |= P2_ITSTOPPED; 972dc47fdf1SKonstantin Belousov return; 973dc47fdf1SKonstantin Belousov } 974dc47fdf1SKonstantin Belousov 975dc47fdf1SKonstantin Belousov p->p_flag2 &= ~P2_ITSTOPPED; 976dc47fdf1SKonstantin Belousov realitexpire_reset_callout(p, &isbt); 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes 979df8bae1dSRodney W. Grimes /* 980df8bae1dSRodney W. Grimes * Check that a proposed value to load into the .it_value or 981df8bae1dSRodney W. Grimes * .it_interval part of an interval timer is acceptable, and 982df8bae1dSRodney W. Grimes * fix it to have at least minimal value (i.e. if it is less 983df8bae1dSRodney W. Grimes * than the resolution of the clock, round it up.) 984df8bae1dSRodney W. Grimes */ 98526f9a767SRodney W. Grimes int 98691afe087SPoul-Henning Kamp itimerfix(struct timeval *tv) 987df8bae1dSRodney W. Grimes { 988df8bae1dSRodney W. Grimes 98986857b36SDavid Xu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 990df8bae1dSRodney W. Grimes return (EINVAL); 991b5ea3779SAlexander Motin if (tv->tv_sec == 0 && tv->tv_usec != 0 && 992b5ea3779SAlexander Motin tv->tv_usec < (u_int)tick / 16) 993b5ea3779SAlexander Motin tv->tv_usec = (u_int)tick / 16; 994df8bae1dSRodney W. Grimes return (0); 995df8bae1dSRodney W. Grimes } 996df8bae1dSRodney W. Grimes 997df8bae1dSRodney W. Grimes /* 998df8bae1dSRodney W. Grimes * Decrement an interval timer by a specified number 999df8bae1dSRodney W. Grimes * of microseconds, which must be less than a second, 1000df8bae1dSRodney W. Grimes * i.e. < 1000000. If the timer expires, then reload 1001df8bae1dSRodney W. Grimes * it. In this case, carry over (usec - old value) to 1002df8bae1dSRodney W. Grimes * reduce the value reloaded into the timer so that 1003df8bae1dSRodney W. Grimes * the timer does not drift. This routine assumes 1004df8bae1dSRodney W. Grimes * that it is called in a context where the timers 1005df8bae1dSRodney W. Grimes * on which it is operating cannot change in value. 1006df8bae1dSRodney W. Grimes */ 100726f9a767SRodney W. Grimes int 100891afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec) 1009df8bae1dSRodney W. Grimes { 1010df8bae1dSRodney W. Grimes 1011df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < usec) { 1012df8bae1dSRodney W. Grimes if (itp->it_value.tv_sec == 0) { 1013df8bae1dSRodney W. Grimes /* expired, and already in next interval */ 1014df8bae1dSRodney W. Grimes usec -= itp->it_value.tv_usec; 1015df8bae1dSRodney W. Grimes goto expire; 1016df8bae1dSRodney W. Grimes } 1017df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 1018df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 1019df8bae1dSRodney W. Grimes } 1020df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 1021df8bae1dSRodney W. Grimes usec = 0; 10224cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_value)) 1023df8bae1dSRodney W. Grimes return (1); 1024df8bae1dSRodney W. Grimes /* expired, exactly at end of interval */ 1025df8bae1dSRodney W. Grimes expire: 10264cf41af3SPoul-Henning Kamp if (timevalisset(&itp->it_interval)) { 1027df8bae1dSRodney W. Grimes itp->it_value = itp->it_interval; 1028df8bae1dSRodney W. Grimes itp->it_value.tv_usec -= usec; 1029df8bae1dSRodney W. Grimes if (itp->it_value.tv_usec < 0) { 1030df8bae1dSRodney W. Grimes itp->it_value.tv_usec += 1000000; 1031df8bae1dSRodney W. Grimes itp->it_value.tv_sec--; 1032df8bae1dSRodney W. Grimes } 1033df8bae1dSRodney W. Grimes } else 1034df8bae1dSRodney W. Grimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 1035df8bae1dSRodney W. Grimes return (0); 1036df8bae1dSRodney W. Grimes } 1037df8bae1dSRodney W. Grimes 1038df8bae1dSRodney W. Grimes /* 1039df8bae1dSRodney W. Grimes * Add and subtract routines for timevals. 1040df8bae1dSRodney W. Grimes * N.B.: subtract routine doesn't deal with 1041df8bae1dSRodney W. Grimes * results which are before the beginning, 1042df8bae1dSRodney W. Grimes * it just gets very confused in this case. 1043df8bae1dSRodney W. Grimes * Caveat emptor. 1044df8bae1dSRodney W. Grimes */ 104526f9a767SRodney W. Grimes void 10466ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2) 1047df8bae1dSRodney W. Grimes { 1048df8bae1dSRodney W. Grimes 1049df8bae1dSRodney W. Grimes t1->tv_sec += t2->tv_sec; 1050df8bae1dSRodney W. Grimes t1->tv_usec += t2->tv_usec; 1051df8bae1dSRodney W. Grimes timevalfix(t1); 1052df8bae1dSRodney W. Grimes } 1053df8bae1dSRodney W. Grimes 105426f9a767SRodney W. Grimes void 10556ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2) 1056df8bae1dSRodney W. Grimes { 1057df8bae1dSRodney W. Grimes 1058df8bae1dSRodney W. Grimes t1->tv_sec -= t2->tv_sec; 1059df8bae1dSRodney W. Grimes t1->tv_usec -= t2->tv_usec; 1060df8bae1dSRodney W. Grimes timevalfix(t1); 1061df8bae1dSRodney W. Grimes } 1062df8bae1dSRodney W. Grimes 106387b6de2bSPoul-Henning Kamp static void 106491afe087SPoul-Henning Kamp timevalfix(struct timeval *t1) 1065df8bae1dSRodney W. Grimes { 1066df8bae1dSRodney W. Grimes 1067df8bae1dSRodney W. Grimes if (t1->tv_usec < 0) { 1068df8bae1dSRodney W. Grimes t1->tv_sec--; 1069df8bae1dSRodney W. Grimes t1->tv_usec += 1000000; 1070df8bae1dSRodney W. Grimes } 1071df8bae1dSRodney W. Grimes if (t1->tv_usec >= 1000000) { 1072df8bae1dSRodney W. Grimes t1->tv_sec++; 1073df8bae1dSRodney W. Grimes t1->tv_usec -= 1000000; 1074df8bae1dSRodney W. Grimes } 1075df8bae1dSRodney W. Grimes } 107691974ce1SSam Leffler 107791974ce1SSam Leffler /* 1078addea9d4SSam Leffler * ratecheck(): simple time-based rate-limit checking. 107991974ce1SSam Leffler */ 108091974ce1SSam Leffler int 108191974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 108291974ce1SSam Leffler { 108391974ce1SSam Leffler struct timeval tv, delta; 108491974ce1SSam Leffler int rv = 0; 108591974ce1SSam Leffler 1086addea9d4SSam Leffler getmicrouptime(&tv); /* NB: 10ms precision */ 1087addea9d4SSam Leffler delta = tv; 1088addea9d4SSam Leffler timevalsub(&delta, lasttime); 108991974ce1SSam Leffler 109091974ce1SSam Leffler /* 109191974ce1SSam Leffler * check for 0,0 is so that the message will be seen at least once, 109291974ce1SSam Leffler * even if interval is huge. 109391974ce1SSam Leffler */ 109491974ce1SSam Leffler if (timevalcmp(&delta, mininterval, >=) || 109591974ce1SSam Leffler (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 109691974ce1SSam Leffler *lasttime = tv; 109791974ce1SSam Leffler rv = 1; 109891974ce1SSam Leffler } 109991974ce1SSam Leffler 110091974ce1SSam Leffler return (rv); 110191974ce1SSam Leffler } 110291974ce1SSam Leffler 110391974ce1SSam Leffler /* 110491974ce1SSam Leffler * ppsratecheck(): packets (or events) per second limitation. 1105addea9d4SSam Leffler * 1106addea9d4SSam Leffler * Return 0 if the limit is to be enforced (e.g. the caller 1107addea9d4SSam Leffler * should drop a packet because of the rate limitation). 1108addea9d4SSam Leffler * 1109893bec80SSam Leffler * maxpps of 0 always causes zero to be returned. maxpps of -1 1110893bec80SSam Leffler * always causes 1 to be returned; this effectively defeats rate 1111893bec80SSam Leffler * limiting. 1112893bec80SSam Leffler * 1113addea9d4SSam Leffler * Note that we maintain the struct timeval for compatibility 1114addea9d4SSam Leffler * with other bsd systems. We reuse the storage and just monitor 1115addea9d4SSam Leffler * clock ticks for minimal overhead. 111691974ce1SSam Leffler */ 111791974ce1SSam Leffler int 111891974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 111991974ce1SSam Leffler { 1120addea9d4SSam Leffler int now; 112191974ce1SSam Leffler 112291974ce1SSam Leffler /* 1123addea9d4SSam Leffler * Reset the last time and counter if this is the first call 1124addea9d4SSam Leffler * or more than a second has passed since the last update of 1125addea9d4SSam Leffler * lasttime. 112691974ce1SSam Leffler */ 1127addea9d4SSam Leffler now = ticks; 1128addea9d4SSam Leffler if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 1129addea9d4SSam Leffler lasttime->tv_sec = now; 1130addea9d4SSam Leffler *curpps = 1; 1131893bec80SSam Leffler return (maxpps != 0); 1132addea9d4SSam Leffler } else { 1133addea9d4SSam Leffler (*curpps)++; /* NB: ignore potential overflow */ 1134f8f02928SIan Lepore return (maxpps < 0 || *curpps <= maxpps); 1135addea9d4SSam Leffler } 113691974ce1SSam Leffler } 113786857b36SDavid Xu 113886857b36SDavid Xu static void 113986857b36SDavid Xu itimer_start(void) 114086857b36SDavid Xu { 1141e68c6191SKonstantin Belousov static const struct kclock rt_clock = { 114286857b36SDavid Xu .timer_create = realtimer_create, 114386857b36SDavid Xu .timer_delete = realtimer_delete, 114486857b36SDavid Xu .timer_settime = realtimer_settime, 114586857b36SDavid Xu .timer_gettime = realtimer_gettime, 114686857b36SDavid Xu }; 114786857b36SDavid Xu 114886857b36SDavid Xu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 114986857b36SDavid Xu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 115086857b36SDavid Xu register_posix_clock(CLOCK_REALTIME, &rt_clock); 115186857b36SDavid Xu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 115277e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 115377e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 115477e718f7SDavid Xu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 115586857b36SDavid Xu } 115686857b36SDavid Xu 1157e68c6191SKonstantin Belousov static int 1158e68c6191SKonstantin Belousov register_posix_clock(int clockid, const struct kclock *clk) 115986857b36SDavid Xu { 116086857b36SDavid Xu if ((unsigned)clockid >= MAX_CLOCKS) { 116186857b36SDavid Xu printf("%s: invalid clockid\n", __func__); 116286857b36SDavid Xu return (0); 116386857b36SDavid Xu } 116486857b36SDavid Xu posix_clocks[clockid] = *clk; 116586857b36SDavid Xu return (1); 116686857b36SDavid Xu } 116786857b36SDavid Xu 116886857b36SDavid Xu static int 116986857b36SDavid Xu itimer_init(void *mem, int size, int flags) 117086857b36SDavid Xu { 117186857b36SDavid Xu struct itimer *it; 117286857b36SDavid Xu 117386857b36SDavid Xu it = (struct itimer *)mem; 117486857b36SDavid Xu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 117586857b36SDavid Xu return (0); 117686857b36SDavid Xu } 117786857b36SDavid Xu 117886857b36SDavid Xu static void 117986857b36SDavid Xu itimer_fini(void *mem, int size) 118086857b36SDavid Xu { 118186857b36SDavid Xu struct itimer *it; 118286857b36SDavid Xu 118386857b36SDavid Xu it = (struct itimer *)mem; 118486857b36SDavid Xu mtx_destroy(&it->it_mtx); 118586857b36SDavid Xu } 118686857b36SDavid Xu 118786857b36SDavid Xu static void 118886857b36SDavid Xu itimer_enter(struct itimer *it) 118986857b36SDavid Xu { 119086857b36SDavid Xu 119186857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 119286857b36SDavid Xu it->it_usecount++; 119386857b36SDavid Xu } 119486857b36SDavid Xu 119586857b36SDavid Xu static void 119686857b36SDavid Xu itimer_leave(struct itimer *it) 119786857b36SDavid Xu { 119886857b36SDavid Xu 119986857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 120086857b36SDavid Xu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 120186857b36SDavid Xu 120286857b36SDavid Xu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 120386857b36SDavid Xu wakeup(it); 120486857b36SDavid Xu } 120586857b36SDavid Xu 120686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 120761d3a4efSDavid Xu struct ktimer_create_args { 120886857b36SDavid Xu clockid_t clock_id; 120986857b36SDavid Xu struct sigevent * evp; 121061d3a4efSDavid Xu int * timerid; 121186857b36SDavid Xu }; 121286857b36SDavid Xu #endif 121386857b36SDavid Xu int 12148451d0ddSKip Macy sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 121586857b36SDavid Xu { 1216643ee871SKonstantin Belousov struct sigevent *evp, ev; 121761d3a4efSDavid Xu int id; 121886857b36SDavid Xu int error; 121986857b36SDavid Xu 1220643ee871SKonstantin Belousov if (uap->evp == NULL) { 1221643ee871SKonstantin Belousov evp = NULL; 1222643ee871SKonstantin Belousov } else { 122386857b36SDavid Xu error = copyin(uap->evp, &ev, sizeof(ev)); 122486857b36SDavid Xu if (error != 0) 122586857b36SDavid Xu return (error); 1226643ee871SKonstantin Belousov evp = &ev; 1227643ee871SKonstantin Belousov } 1228643ee871SKonstantin Belousov error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1); 122986857b36SDavid Xu if (error == 0) { 123061d3a4efSDavid Xu error = copyout(&id, uap->timerid, sizeof(int)); 123186857b36SDavid Xu if (error != 0) 1232643ee871SKonstantin Belousov kern_ktimer_delete(td, id); 123386857b36SDavid Xu } 123486857b36SDavid Xu return (error); 123586857b36SDavid Xu } 123686857b36SDavid Xu 1237643ee871SKonstantin Belousov int 1238643ee871SKonstantin Belousov kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp, 1239643ee871SKonstantin Belousov int *timerid, int preset_id) 124086857b36SDavid Xu { 124186857b36SDavid Xu struct proc *p = td->td_proc; 124286857b36SDavid Xu struct itimer *it; 124386857b36SDavid Xu int id; 124486857b36SDavid Xu int error; 124586857b36SDavid Xu 124686857b36SDavid Xu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 124786857b36SDavid Xu return (EINVAL); 124886857b36SDavid Xu 124986857b36SDavid Xu if (posix_clocks[clock_id].timer_create == NULL) 125086857b36SDavid Xu return (EINVAL); 125186857b36SDavid Xu 125286857b36SDavid Xu if (evp != NULL) { 125386857b36SDavid Xu if (evp->sigev_notify != SIGEV_NONE && 125456c06c4bSDavid Xu evp->sigev_notify != SIGEV_SIGNAL && 125556c06c4bSDavid Xu evp->sigev_notify != SIGEV_THREAD_ID) 125686857b36SDavid Xu return (EINVAL); 125756c06c4bSDavid Xu if ((evp->sigev_notify == SIGEV_SIGNAL || 125856c06c4bSDavid Xu evp->sigev_notify == SIGEV_THREAD_ID) && 125986857b36SDavid Xu !_SIG_VALID(evp->sigev_signo)) 126086857b36SDavid Xu return (EINVAL); 126186857b36SDavid Xu } 126286857b36SDavid Xu 126360354683SDavid Xu if (p->p_itimers == NULL) 126486857b36SDavid Xu itimers_alloc(p); 126586857b36SDavid Xu 126686857b36SDavid Xu it = uma_zalloc(itimer_zone, M_WAITOK); 126786857b36SDavid Xu it->it_flags = 0; 126886857b36SDavid Xu it->it_usecount = 0; 126986857b36SDavid Xu it->it_active = 0; 127056c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 127156c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 127286857b36SDavid Xu it->it_overrun = 0; 127386857b36SDavid Xu it->it_overrun_last = 0; 127486857b36SDavid Xu it->it_clockid = clock_id; 127586857b36SDavid Xu it->it_timerid = -1; 127686857b36SDavid Xu it->it_proc = p; 127786857b36SDavid Xu ksiginfo_init(&it->it_ksi); 127886857b36SDavid Xu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 127986857b36SDavid Xu error = CLOCK_CALL(clock_id, timer_create, (it)); 128086857b36SDavid Xu if (error != 0) 128186857b36SDavid Xu goto out; 128286857b36SDavid Xu 128386857b36SDavid Xu PROC_LOCK(p); 128486857b36SDavid Xu if (preset_id != -1) { 128586857b36SDavid Xu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 128686857b36SDavid Xu id = preset_id; 128760354683SDavid Xu if (p->p_itimers->its_timers[id] != NULL) { 128886857b36SDavid Xu PROC_UNLOCK(p); 128986857b36SDavid Xu error = 0; 129086857b36SDavid Xu goto out; 129186857b36SDavid Xu } 129286857b36SDavid Xu } else { 129386857b36SDavid Xu /* 129486857b36SDavid Xu * Find a free timer slot, skipping those reserved 129586857b36SDavid Xu * for setitimer(). 129686857b36SDavid Xu */ 129786857b36SDavid Xu for (id = 3; id < TIMER_MAX; id++) 129860354683SDavid Xu if (p->p_itimers->its_timers[id] == NULL) 129986857b36SDavid Xu break; 130086857b36SDavid Xu if (id == TIMER_MAX) { 130186857b36SDavid Xu PROC_UNLOCK(p); 130286857b36SDavid Xu error = EAGAIN; 130386857b36SDavid Xu goto out; 130486857b36SDavid Xu } 130586857b36SDavid Xu } 130686857b36SDavid Xu it->it_timerid = id; 130760354683SDavid Xu p->p_itimers->its_timers[id] = it; 130886857b36SDavid Xu if (evp != NULL) 130986857b36SDavid Xu it->it_sigev = *evp; 131086857b36SDavid Xu else { 131186857b36SDavid Xu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 131286857b36SDavid Xu switch (clock_id) { 131386857b36SDavid Xu default: 131486857b36SDavid Xu case CLOCK_REALTIME: 131586857b36SDavid Xu it->it_sigev.sigev_signo = SIGALRM; 131686857b36SDavid Xu break; 131786857b36SDavid Xu case CLOCK_VIRTUAL: 131886857b36SDavid Xu it->it_sigev.sigev_signo = SIGVTALRM; 131986857b36SDavid Xu break; 132086857b36SDavid Xu case CLOCK_PROF: 132186857b36SDavid Xu it->it_sigev.sigev_signo = SIGPROF; 132286857b36SDavid Xu break; 132386857b36SDavid Xu } 13248f0371f1SDavid Xu it->it_sigev.sigev_value.sival_int = id; 132586857b36SDavid Xu } 132686857b36SDavid Xu 132756c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 132856c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 132986857b36SDavid Xu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 133086857b36SDavid Xu it->it_ksi.ksi_code = SI_TIMER; 133186857b36SDavid Xu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 133286857b36SDavid Xu it->it_ksi.ksi_timerid = id; 133386857b36SDavid Xu } 133486857b36SDavid Xu PROC_UNLOCK(p); 133586857b36SDavid Xu *timerid = id; 133686857b36SDavid Xu return (0); 133786857b36SDavid Xu 133886857b36SDavid Xu out: 133986857b36SDavid Xu ITIMER_LOCK(it); 134086857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 134186857b36SDavid Xu ITIMER_UNLOCK(it); 134286857b36SDavid Xu uma_zfree(itimer_zone, it); 134386857b36SDavid Xu return (error); 134486857b36SDavid Xu } 134586857b36SDavid Xu 134686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 134761d3a4efSDavid Xu struct ktimer_delete_args { 134861d3a4efSDavid Xu int timerid; 134986857b36SDavid Xu }; 135086857b36SDavid Xu #endif 135186857b36SDavid Xu int 13528451d0ddSKip Macy sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 135386857b36SDavid Xu { 1354643ee871SKonstantin Belousov 1355643ee871SKonstantin Belousov return (kern_ktimer_delete(td, uap->timerid)); 135686857b36SDavid Xu } 135786857b36SDavid Xu 135886857b36SDavid Xu static struct itimer * 1359843b99c6SDavid Xu itimer_find(struct proc *p, int timerid) 136086857b36SDavid Xu { 136186857b36SDavid Xu struct itimer *it; 136286857b36SDavid Xu 136386857b36SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 13643f935cf3SColin Percival if ((p->p_itimers == NULL) || 13653f935cf3SColin Percival (timerid < 0) || (timerid >= TIMER_MAX) || 136660354683SDavid Xu (it = p->p_itimers->its_timers[timerid]) == NULL) { 136786857b36SDavid Xu return (NULL); 136886857b36SDavid Xu } 136986857b36SDavid Xu ITIMER_LOCK(it); 1370843b99c6SDavid Xu if ((it->it_flags & ITF_DELETING) != 0) { 137186857b36SDavid Xu ITIMER_UNLOCK(it); 137286857b36SDavid Xu it = NULL; 137386857b36SDavid Xu } 137486857b36SDavid Xu return (it); 137586857b36SDavid Xu } 137686857b36SDavid Xu 1377643ee871SKonstantin Belousov int 1378643ee871SKonstantin Belousov kern_ktimer_delete(struct thread *td, int timerid) 137986857b36SDavid Xu { 138086857b36SDavid Xu struct proc *p = td->td_proc; 138186857b36SDavid Xu struct itimer *it; 138286857b36SDavid Xu 138386857b36SDavid Xu PROC_LOCK(p); 1384843b99c6SDavid Xu it = itimer_find(p, timerid); 138586857b36SDavid Xu if (it == NULL) { 138686857b36SDavid Xu PROC_UNLOCK(p); 138786857b36SDavid Xu return (EINVAL); 138886857b36SDavid Xu } 138986857b36SDavid Xu PROC_UNLOCK(p); 139086857b36SDavid Xu 139186857b36SDavid Xu it->it_flags |= ITF_DELETING; 139286857b36SDavid Xu while (it->it_usecount > 0) { 139386857b36SDavid Xu it->it_flags |= ITF_WANTED; 139486857b36SDavid Xu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 139586857b36SDavid Xu } 139686857b36SDavid Xu it->it_flags &= ~ITF_WANTED; 139786857b36SDavid Xu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 139886857b36SDavid Xu ITIMER_UNLOCK(it); 139986857b36SDavid Xu 140086857b36SDavid Xu PROC_LOCK(p); 140186857b36SDavid Xu if (KSI_ONQ(&it->it_ksi)) 140286857b36SDavid Xu sigqueue_take(&it->it_ksi); 140360354683SDavid Xu p->p_itimers->its_timers[timerid] = NULL; 140486857b36SDavid Xu PROC_UNLOCK(p); 140586857b36SDavid Xu uma_zfree(itimer_zone, it); 140686857b36SDavid Xu return (0); 140786857b36SDavid Xu } 140886857b36SDavid Xu 140986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 141061d3a4efSDavid Xu struct ktimer_settime_args { 141161d3a4efSDavid Xu int timerid; 141286857b36SDavid Xu int flags; 141386857b36SDavid Xu const struct itimerspec * value; 141486857b36SDavid Xu struct itimerspec * ovalue; 141586857b36SDavid Xu }; 141686857b36SDavid Xu #endif 141786857b36SDavid Xu int 14188451d0ddSKip Macy sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 141986857b36SDavid Xu { 142086857b36SDavid Xu struct itimerspec val, oval, *ovalp; 142186857b36SDavid Xu int error; 142286857b36SDavid Xu 142386857b36SDavid Xu error = copyin(uap->value, &val, sizeof(val)); 142486857b36SDavid Xu if (error != 0) 142586857b36SDavid Xu return (error); 1426643ee871SKonstantin Belousov ovalp = uap->ovalue != NULL ? &oval : NULL; 1427643ee871SKonstantin Belousov error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp); 1428643ee871SKonstantin Belousov if (error == 0 && uap->ovalue != NULL) 1429643ee871SKonstantin Belousov error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1430643ee871SKonstantin Belousov return (error); 1431643ee871SKonstantin Belousov } 143286857b36SDavid Xu 1433643ee871SKonstantin Belousov int 1434643ee871SKonstantin Belousov kern_ktimer_settime(struct thread *td, int timer_id, int flags, 1435643ee871SKonstantin Belousov struct itimerspec *val, struct itimerspec *oval) 1436643ee871SKonstantin Belousov { 1437643ee871SKonstantin Belousov struct proc *p; 1438643ee871SKonstantin Belousov struct itimer *it; 1439643ee871SKonstantin Belousov int error; 144086857b36SDavid Xu 1441643ee871SKonstantin Belousov p = td->td_proc; 144286857b36SDavid Xu PROC_LOCK(p); 1443643ee871SKonstantin Belousov if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) { 144486857b36SDavid Xu PROC_UNLOCK(p); 144586857b36SDavid Xu error = EINVAL; 144686857b36SDavid Xu } else { 144786857b36SDavid Xu PROC_UNLOCK(p); 144886857b36SDavid Xu itimer_enter(it); 1449643ee871SKonstantin Belousov error = CLOCK_CALL(it->it_clockid, timer_settime, (it, 1450643ee871SKonstantin Belousov flags, val, oval)); 145186857b36SDavid Xu itimer_leave(it); 145286857b36SDavid Xu ITIMER_UNLOCK(it); 145386857b36SDavid Xu } 145486857b36SDavid Xu return (error); 145586857b36SDavid Xu } 145686857b36SDavid Xu 145786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 145861d3a4efSDavid Xu struct ktimer_gettime_args { 145961d3a4efSDavid Xu int timerid; 146086857b36SDavid Xu struct itimerspec * value; 146186857b36SDavid Xu }; 146286857b36SDavid Xu #endif 146386857b36SDavid Xu int 14648451d0ddSKip Macy sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 146586857b36SDavid Xu { 146686857b36SDavid Xu struct itimerspec val; 146786857b36SDavid Xu int error; 146886857b36SDavid Xu 1469643ee871SKonstantin Belousov error = kern_ktimer_gettime(td, uap->timerid, &val); 1470643ee871SKonstantin Belousov if (error == 0) 1471643ee871SKonstantin Belousov error = copyout(&val, uap->value, sizeof(val)); 1472643ee871SKonstantin Belousov return (error); 1473643ee871SKonstantin Belousov } 1474643ee871SKonstantin Belousov 1475643ee871SKonstantin Belousov int 1476643ee871SKonstantin Belousov kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val) 1477643ee871SKonstantin Belousov { 1478643ee871SKonstantin Belousov struct proc *p; 1479643ee871SKonstantin Belousov struct itimer *it; 1480643ee871SKonstantin Belousov int error; 1481643ee871SKonstantin Belousov 1482643ee871SKonstantin Belousov p = td->td_proc; 148386857b36SDavid Xu PROC_LOCK(p); 1484643ee871SKonstantin Belousov if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) { 148586857b36SDavid Xu PROC_UNLOCK(p); 148686857b36SDavid Xu error = EINVAL; 148786857b36SDavid Xu } else { 148886857b36SDavid Xu PROC_UNLOCK(p); 148986857b36SDavid Xu itimer_enter(it); 1490643ee871SKonstantin Belousov error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val)); 149186857b36SDavid Xu itimer_leave(it); 149286857b36SDavid Xu ITIMER_UNLOCK(it); 149386857b36SDavid Xu } 149486857b36SDavid Xu return (error); 149586857b36SDavid Xu } 149686857b36SDavid Xu 149786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_ 149886857b36SDavid Xu struct timer_getoverrun_args { 149961d3a4efSDavid Xu int timerid; 150086857b36SDavid Xu }; 150186857b36SDavid Xu #endif 150286857b36SDavid Xu int 15038451d0ddSKip Macy sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 150486857b36SDavid Xu { 1505e346f8c4SBjoern A. Zeeb 1506e346f8c4SBjoern A. Zeeb return (kern_ktimer_getoverrun(td, uap->timerid)); 1507e346f8c4SBjoern A. Zeeb } 1508e346f8c4SBjoern A. Zeeb 1509e346f8c4SBjoern A. Zeeb int 1510e346f8c4SBjoern A. Zeeb kern_ktimer_getoverrun(struct thread *td, int timer_id) 1511e346f8c4SBjoern A. Zeeb { 151286857b36SDavid Xu struct proc *p = td->td_proc; 151386857b36SDavid Xu struct itimer *it; 151486857b36SDavid Xu int error ; 151586857b36SDavid Xu 151686857b36SDavid Xu PROC_LOCK(p); 1517e346f8c4SBjoern A. Zeeb if (timer_id < 3 || 1518e346f8c4SBjoern A. Zeeb (it = itimer_find(p, timer_id)) == NULL) { 151986857b36SDavid Xu PROC_UNLOCK(p); 152086857b36SDavid Xu error = EINVAL; 152186857b36SDavid Xu } else { 152286857b36SDavid Xu td->td_retval[0] = it->it_overrun_last; 152386857b36SDavid Xu ITIMER_UNLOCK(it); 152456c06c4bSDavid Xu PROC_UNLOCK(p); 152586857b36SDavid Xu error = 0; 152686857b36SDavid Xu } 152786857b36SDavid Xu return (error); 152886857b36SDavid Xu } 152986857b36SDavid Xu 153086857b36SDavid Xu static int 153186857b36SDavid Xu realtimer_create(struct itimer *it) 153286857b36SDavid Xu { 153386857b36SDavid Xu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 153486857b36SDavid Xu return (0); 153586857b36SDavid Xu } 153686857b36SDavid Xu 153786857b36SDavid Xu static int 153886857b36SDavid Xu realtimer_delete(struct itimer *it) 153986857b36SDavid Xu { 154086857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 1541843b99c6SDavid Xu 1542d6b6592eSDavid Xu /* 1543d6b6592eSDavid Xu * clear timer's value and interval to tell realtimer_expire 1544d6b6592eSDavid Xu * to not rearm the timer. 1545d6b6592eSDavid Xu */ 1546d6b6592eSDavid Xu timespecclear(&it->it_time.it_value); 1547d6b6592eSDavid Xu timespecclear(&it->it_time.it_interval); 1548843b99c6SDavid Xu ITIMER_UNLOCK(it); 1549843b99c6SDavid Xu callout_drain(&it->it_callout); 1550843b99c6SDavid Xu ITIMER_LOCK(it); 155186857b36SDavid Xu return (0); 155286857b36SDavid Xu } 155386857b36SDavid Xu 155486857b36SDavid Xu static int 155586857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 155686857b36SDavid Xu { 155756c06c4bSDavid Xu struct timespec cts; 155886857b36SDavid Xu 155986857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 156086857b36SDavid Xu 156156c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 156256c06c4bSDavid Xu *ovalue = it->it_time; 156386857b36SDavid Xu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 15646040822cSAlan Somers timespecsub(&ovalue->it_value, &cts, &ovalue->it_value); 156586857b36SDavid Xu if (ovalue->it_value.tv_sec < 0 || 156686857b36SDavid Xu (ovalue->it_value.tv_sec == 0 && 156786857b36SDavid Xu ovalue->it_value.tv_nsec == 0)) { 156886857b36SDavid Xu ovalue->it_value.tv_sec = 0; 156986857b36SDavid Xu ovalue->it_value.tv_nsec = 1; 157086857b36SDavid Xu } 157186857b36SDavid Xu } 157286857b36SDavid Xu return (0); 157386857b36SDavid Xu } 157486857b36SDavid Xu 157586857b36SDavid Xu static int 157660d12ef9SMark Johnston realtimer_settime(struct itimer *it, int flags, struct itimerspec *value, 157760d12ef9SMark Johnston struct itimerspec *ovalue) 157886857b36SDavid Xu { 157956c06c4bSDavid Xu struct timespec cts, ts; 158056c06c4bSDavid Xu struct timeval tv; 158156c06c4bSDavid Xu struct itimerspec val; 158286857b36SDavid Xu 158386857b36SDavid Xu mtx_assert(&it->it_mtx, MA_OWNED); 158486857b36SDavid Xu 158556c06c4bSDavid Xu val = *value; 158656c06c4bSDavid Xu if (itimespecfix(&val.it_value)) 158786857b36SDavid Xu return (EINVAL); 158886857b36SDavid Xu 158956c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 159056c06c4bSDavid Xu if (itimespecfix(&val.it_interval)) 159186857b36SDavid Xu return (EINVAL); 159286857b36SDavid Xu } else { 159356c06c4bSDavid Xu timespecclear(&val.it_interval); 159486857b36SDavid Xu } 159586857b36SDavid Xu 159686857b36SDavid Xu if (ovalue != NULL) 159786857b36SDavid Xu realtimer_gettime(it, ovalue); 159886857b36SDavid Xu 159986857b36SDavid Xu it->it_time = val; 160056c06c4bSDavid Xu if (timespecisset(&val.it_value)) { 160156c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 160256c06c4bSDavid Xu ts = val.it_value; 160386857b36SDavid Xu if ((flags & TIMER_ABSTIME) == 0) { 160486857b36SDavid Xu /* Convert to absolute time. */ 16056040822cSAlan Somers timespecadd(&it->it_time.it_value, &cts, 16066040822cSAlan Somers &it->it_time.it_value); 160786857b36SDavid Xu } else { 16086040822cSAlan Somers timespecsub(&ts, &cts, &ts); 160986857b36SDavid Xu /* 161056c06c4bSDavid Xu * We don't care if ts is negative, tztohz will 161186857b36SDavid Xu * fix it. 161286857b36SDavid Xu */ 161386857b36SDavid Xu } 161456c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 161560d12ef9SMark Johnston callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 161660d12ef9SMark Johnston it); 161786857b36SDavid Xu } else { 161886857b36SDavid Xu callout_stop(&it->it_callout); 161986857b36SDavid Xu } 162086857b36SDavid Xu 162186857b36SDavid Xu return (0); 162286857b36SDavid Xu } 162386857b36SDavid Xu 162486857b36SDavid Xu static void 162556c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts) 162686857b36SDavid Xu { 162786857b36SDavid Xu if (id == CLOCK_REALTIME) 162856c06c4bSDavid Xu getnanotime(ts); 162986857b36SDavid Xu else /* CLOCK_MONOTONIC */ 163056c06c4bSDavid Xu getnanouptime(ts); 163156c06c4bSDavid Xu } 163256c06c4bSDavid Xu 163356c06c4bSDavid Xu int 163461d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 163556c06c4bSDavid Xu { 163656c06c4bSDavid Xu struct itimer *it; 163756c06c4bSDavid Xu 163856c06c4bSDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 1639843b99c6SDavid Xu it = itimer_find(p, timerid); 164056c06c4bSDavid Xu if (it != NULL) { 164156c06c4bSDavid Xu ksi->ksi_overrun = it->it_overrun; 164256c06c4bSDavid Xu it->it_overrun_last = it->it_overrun; 164356c06c4bSDavid Xu it->it_overrun = 0; 164456c06c4bSDavid Xu ITIMER_UNLOCK(it); 164556c06c4bSDavid Xu return (0); 164656c06c4bSDavid Xu } 164756c06c4bSDavid Xu return (EINVAL); 164856c06c4bSDavid Xu } 164956c06c4bSDavid Xu 16508ff2b41cSMark Johnston static int 165156c06c4bSDavid Xu itimespecfix(struct timespec *ts) 165256c06c4bSDavid Xu { 165356c06c4bSDavid Xu 16548b3c4231SMark Johnston if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= NS_PER_SEC) 16558b3c4231SMark Johnston return (EINVAL); 16568b3c4231SMark Johnston if ((UINT64_MAX - ts->tv_nsec) / NS_PER_SEC < ts->tv_sec) 165756c06c4bSDavid Xu return (EINVAL); 165856c06c4bSDavid Xu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 165956c06c4bSDavid Xu ts->tv_nsec = tick * 1000; 166056c06c4bSDavid Xu return (0); 166186857b36SDavid Xu } 166286857b36SDavid Xu 16637995dae9SMark Johnston #define timespectons(tsp) \ 16648b3c4231SMark Johnston ((uint64_t)(tsp)->tv_sec * NS_PER_SEC + (tsp)->tv_nsec) 16657995dae9SMark Johnston #define timespecfromns(ns) (struct timespec){ \ 16668b3c4231SMark Johnston .tv_sec = (ns) / NS_PER_SEC, \ 16678b3c4231SMark Johnston .tv_nsec = (ns) % NS_PER_SEC \ 16687995dae9SMark Johnston } 16697995dae9SMark Johnston 167086857b36SDavid Xu static void 16715cc1d199SKonstantin Belousov realtimer_expire_l(struct itimer *it, bool proc_locked) 167286857b36SDavid Xu { 167356c06c4bSDavid Xu struct timespec cts, ts; 167456c06c4bSDavid Xu struct timeval tv; 16754d27d8d2SKonstantin Belousov struct proc *p; 16767995dae9SMark Johnston uint64_t interval, now, overruns, value; 167786857b36SDavid Xu 167856c06c4bSDavid Xu realtimer_clocktime(it->it_clockid, &cts); 167986857b36SDavid Xu /* Only fire if time is reached. */ 168056c06c4bSDavid Xu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 168156c06c4bSDavid Xu if (timespecisset(&it->it_time.it_interval)) { 168256c06c4bSDavid Xu timespecadd(&it->it_time.it_value, 16836040822cSAlan Somers &it->it_time.it_interval, 16846040822cSAlan Somers &it->it_time.it_value); 16857995dae9SMark Johnston 16867995dae9SMark Johnston interval = timespectons(&it->it_time.it_interval); 16877995dae9SMark Johnston value = timespectons(&it->it_time.it_value); 16887995dae9SMark Johnston now = timespectons(&cts); 16897995dae9SMark Johnston 16907995dae9SMark Johnston if (now >= value) { 16917995dae9SMark Johnston /* 16927995dae9SMark Johnston * We missed at least one period. 16937995dae9SMark Johnston */ 16947995dae9SMark Johnston overruns = howmany(now - value + 1, interval); 16957995dae9SMark Johnston if (it->it_overrun + overruns >= 16967995dae9SMark Johnston it->it_overrun && 16977995dae9SMark Johnston it->it_overrun + overruns <= INT_MAX) { 16987995dae9SMark Johnston it->it_overrun += (int)overruns; 16997995dae9SMark Johnston } else { 17007995dae9SMark Johnston it->it_overrun = INT_MAX; 170177e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 17027995dae9SMark Johnston } 17037995dae9SMark Johnston value = 17047995dae9SMark Johnston now + interval - (now - value) % interval; 17057995dae9SMark Johnston it->it_time.it_value = timespecfromns(value); 170686857b36SDavid Xu } 170786857b36SDavid Xu } else { 170886857b36SDavid Xu /* single shot timer ? */ 170956c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 171086857b36SDavid Xu } 17115cc1d199SKonstantin Belousov 17124d27d8d2SKonstantin Belousov p = it->it_proc; 17135cc1d199SKonstantin Belousov if (timespecisset(&it->it_time.it_value)) { 17144d27d8d2SKonstantin Belousov if (P_SHOULDSTOP(p) || P_KILLED(p)) { 17154d27d8d2SKonstantin Belousov it->it_flags |= ITF_PSTOPPED; 17164d27d8d2SKonstantin Belousov } else { 17176040822cSAlan Somers timespecsub(&it->it_time.it_value, &cts, &ts); 171856c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 171956c06c4bSDavid Xu callout_reset(&it->it_callout, tvtohz(&tv), 172086857b36SDavid Xu realtimer_expire, it); 172186857b36SDavid Xu } 17224d27d8d2SKonstantin Belousov } 17235cc1d199SKonstantin Belousov 1724d6b6592eSDavid Xu itimer_enter(it); 172586857b36SDavid Xu ITIMER_UNLOCK(it); 17265cc1d199SKonstantin Belousov if (proc_locked) 17275cc1d199SKonstantin Belousov PROC_UNLOCK(p); 172886857b36SDavid Xu itimer_fire(it); 17295cc1d199SKonstantin Belousov if (proc_locked) 17305cc1d199SKonstantin Belousov PROC_LOCK(p); 173186857b36SDavid Xu ITIMER_LOCK(it); 1732d6b6592eSDavid Xu itimer_leave(it); 173356c06c4bSDavid Xu } else if (timespecisset(&it->it_time.it_value)) { 17344d27d8d2SKonstantin Belousov p = it->it_proc; 17354d27d8d2SKonstantin Belousov if (P_SHOULDSTOP(p) || P_KILLED(p)) { 17364d27d8d2SKonstantin Belousov it->it_flags |= ITF_PSTOPPED; 17374d27d8d2SKonstantin Belousov } else { 173856c06c4bSDavid Xu ts = it->it_time.it_value; 17396040822cSAlan Somers timespecsub(&ts, &cts, &ts); 174056c06c4bSDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts); 17414d27d8d2SKonstantin Belousov callout_reset(&it->it_callout, tvtohz(&tv), 17424d27d8d2SKonstantin Belousov realtimer_expire, it); 17434d27d8d2SKonstantin Belousov } 174486857b36SDavid Xu } 174586857b36SDavid Xu } 174686857b36SDavid Xu 17475cc1d199SKonstantin Belousov /* Timeout callback for realtime timer */ 17485cc1d199SKonstantin Belousov static void 17495cc1d199SKonstantin Belousov realtimer_expire(void *arg) 17505cc1d199SKonstantin Belousov { 17515cc1d199SKonstantin Belousov realtimer_expire_l(arg, false); 17525cc1d199SKonstantin Belousov } 17535cc1d199SKonstantin Belousov 17548ff2b41cSMark Johnston static void 175586857b36SDavid Xu itimer_fire(struct itimer *it) 175686857b36SDavid Xu { 175786857b36SDavid Xu struct proc *p = it->it_proc; 1758cf7d9a8cSDavid Xu struct thread *td; 175986857b36SDavid Xu 176056c06c4bSDavid Xu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 176156c06c4bSDavid Xu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1762cf7d9a8cSDavid Xu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 176356c06c4bSDavid Xu ITIMER_LOCK(it); 176456c06c4bSDavid Xu timespecclear(&it->it_time.it_value); 176556c06c4bSDavid Xu timespecclear(&it->it_time.it_interval); 176656c06c4bSDavid Xu callout_stop(&it->it_callout); 176756c06c4bSDavid Xu ITIMER_UNLOCK(it); 1768cf7d9a8cSDavid Xu return; 17696d7b314bSDavid Xu } 1770cf7d9a8cSDavid Xu if (!KSI_ONQ(&it->it_ksi)) { 1771cf7d9a8cSDavid Xu it->it_ksi.ksi_errno = 0; 1772cf7d9a8cSDavid Xu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1773cf7d9a8cSDavid Xu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 177456c06c4bSDavid Xu } else { 177577e718f7SDavid Xu if (it->it_overrun < INT_MAX) 17766d7b314bSDavid Xu it->it_overrun++; 177777e718f7SDavid Xu else 177877e718f7SDavid Xu it->it_ksi.ksi_errno = ERANGE; 177956c06c4bSDavid Xu } 178086857b36SDavid Xu PROC_UNLOCK(p); 178186857b36SDavid Xu } 178286857b36SDavid Xu } 178386857b36SDavid Xu 178486857b36SDavid Xu static void 178586857b36SDavid Xu itimers_alloc(struct proc *p) 178686857b36SDavid Xu { 178760354683SDavid Xu struct itimers *its; 178860354683SDavid Xu int i; 178986857b36SDavid Xu 179060354683SDavid Xu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 179186857b36SDavid Xu LIST_INIT(&its->its_virtual); 179286857b36SDavid Xu LIST_INIT(&its->its_prof); 179386857b36SDavid Xu TAILQ_INIT(&its->its_worklist); 179460354683SDavid Xu for (i = 0; i < TIMER_MAX; i++) 179560354683SDavid Xu its->its_timers[i] = NULL; 179660354683SDavid Xu PROC_LOCK(p); 179760354683SDavid Xu if (p->p_itimers == NULL) { 179860354683SDavid Xu p->p_itimers = its; 179960354683SDavid Xu PROC_UNLOCK(p); 180060354683SDavid Xu } 180160354683SDavid Xu else { 180260354683SDavid Xu PROC_UNLOCK(p); 180360354683SDavid Xu free(its, M_SUBPROC); 180460354683SDavid Xu } 180586857b36SDavid Xu } 180686857b36SDavid Xu 180786857b36SDavid Xu /* Clean up timers when some process events are being triggered. */ 1808d26b1a1fSDavid Xu static void 1809e68c6191SKonstantin Belousov itimers_event_exit_exec(int start_idx, struct proc *p) 181086857b36SDavid Xu { 181186857b36SDavid Xu struct itimers *its; 181286857b36SDavid Xu struct itimer *it; 181386857b36SDavid Xu int i; 181486857b36SDavid Xu 181560354683SDavid Xu its = p->p_itimers; 1816e68c6191SKonstantin Belousov if (its == NULL) 1817e68c6191SKonstantin Belousov return; 1818e68c6191SKonstantin Belousov 1819e68c6191SKonstantin Belousov for (i = start_idx; i < TIMER_MAX; ++i) { 1820843b99c6SDavid Xu if ((it = its->its_timers[i]) != NULL) 1821643ee871SKonstantin Belousov kern_ktimer_delete(curthread, i); 182286857b36SDavid Xu } 1823e68c6191SKonstantin Belousov if (its->its_timers[0] == NULL && its->its_timers[1] == NULL && 182486857b36SDavid Xu its->its_timers[2] == NULL) { 1825*3138392aSMark Johnston /* Synchronize with itimer_proc_continue(). */ 1826*3138392aSMark Johnston PROC_LOCK(p); 182760354683SDavid Xu p->p_itimers = NULL; 1828*3138392aSMark Johnston PROC_UNLOCK(p); 1829*3138392aSMark Johnston free(its, M_SUBPROC); 183086857b36SDavid Xu } 183186857b36SDavid Xu } 1832e68c6191SKonstantin Belousov 1833e68c6191SKonstantin Belousov void 1834e68c6191SKonstantin Belousov itimers_exec(struct proc *p) 1835e68c6191SKonstantin Belousov { 1836e68c6191SKonstantin Belousov /* 1837e68c6191SKonstantin Belousov * According to susv3, XSI interval timers should be inherited 1838e68c6191SKonstantin Belousov * by new image. 1839e68c6191SKonstantin Belousov */ 1840e68c6191SKonstantin Belousov itimers_event_exit_exec(3, p); 1841e68c6191SKonstantin Belousov } 1842e68c6191SKonstantin Belousov 1843e68c6191SKonstantin Belousov void 1844e68c6191SKonstantin Belousov itimers_exit(struct proc *p) 1845e68c6191SKonstantin Belousov { 1846e68c6191SKonstantin Belousov itimers_event_exit_exec(0, p); 184786857b36SDavid Xu } 1848