kern_time.c revision 151585
1139804Simp/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 291541Srgrimes * @(#)kern_time.c 8.1 (Berkeley) 6/10/93 301541Srgrimes */ 311541Srgrimes 32116182Sobrien#include <sys/cdefs.h> 33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_time.c 151585 2005-10-23 12:19:08Z davidxu $"); 34116182Sobrien 35106369Srwatson#include "opt_mac.h" 36106369Srwatson 371541Srgrimes#include <sys/param.h> 3848274Speter#include <sys/systm.h> 3976166Smarkm#include <sys/lock.h> 4076166Smarkm#include <sys/mutex.h> 4112221Sbde#include <sys/sysproto.h> 421541Srgrimes#include <sys/resourcevar.h> 433308Sphk#include <sys/signalvar.h> 441541Srgrimes#include <sys/kernel.h> 45106369Srwatson#include <sys/mac.h> 46140483Sps#include <sys/syscallsubr.h> 4725583Speter#include <sys/sysent.h> 481541Srgrimes#include <sys/proc.h> 4925656Speter#include <sys/time.h> 50151576Sdavidxu#include <sys/timers.h> 5158377Sphk#include <sys/timetc.h> 521541Srgrimes#include <sys/vnode.h> 5376166Smarkm 5426335Speter#include <vm/vm.h> 5526335Speter#include <vm/vm_extern.h> 561541Srgrimes 57151576Sdavidxu#define MAX_CLOCKS (CLOCK_MONOTONIC+1) 58151576Sdavidxu 59110299Sphkint tz_minuteswest; 60110299Sphkint tz_dsttime; 619369Sdg 62151576Sdavidxustatic struct kclock posix_clocks[MAX_CLOCKS]; 63151576Sdavidxustatic uma_zone_t itimer_zone = NULL; 64151576Sdavidxu 658876Srgrimes/* 661541Srgrimes * Time of day and interval timer support. 671541Srgrimes * 681541Srgrimes * These routines provide the kernel entry points to get and set 691541Srgrimes * the time-of-day and per-process interval timers. Subroutines 701541Srgrimes * here provide support for adding and subtracting timeval structures 711541Srgrimes * and decrementing interval timers, optionally reloading the interval 721541Srgrimes * timers when they expire. 731541Srgrimes */ 741541Srgrimes 7594343Sjhbstatic int settime(struct thread *, struct timeval *); 7692723Salfredstatic void timevalfix(struct timeval *); 7792723Salfredstatic void no_lease_updatetime(int); 7813016Sbde 79151576Sdavidxustatic void itimer_start(void); 80151576Sdavidxustatic int itimer_init(void *, int, int); 81151576Sdavidxustatic void itimer_fini(void *, int); 82151576Sdavidxustatic void itimer_enter(struct itimer *); 83151576Sdavidxustatic void itimer_leave(struct itimer *); 84151576Sdavidxustatic struct itimer *itimer_find(struct proc *, timer_t, int); 85151576Sdavidxustatic void itimers_alloc(struct proc *); 86151576Sdavidxustatic int realtimer_create(struct itimer *); 87151576Sdavidxustatic int realtimer_gettime(struct itimer *, struct itimerspec *); 88151576Sdavidxustatic int realtimer_settime(struct itimer *, int, 89151576Sdavidxu struct itimerspec *, struct itimerspec *); 90151576Sdavidxustatic int realtimer_delete(struct itimer *); 91151576Sdavidxustatic void realtimer_clocktime(clockid_t, struct timeval *); 92151576Sdavidxustatic void realtimer_expire(void *); 93151576Sdavidxustatic void realtimer_event_hook(struct proc *, clockid_t, int event); 94151576Sdavidxustatic int kern_timer_create(struct thread *, clockid_t, 95151576Sdavidxu struct sigevent *, timer_t *, timer_t); 96151576Sdavidxustatic int kern_timer_delete(struct thread *, timer_t); 97151576Sdavidxu 98151576Sdavidxuint register_posix_clock(int, struct kclock *); 99151576Sdavidxuvoid itimer_fire(struct itimer *it); 100151576Sdavidxu 101151576Sdavidxu#define CLOCK_CALL(clock, call, arglist) \ 102151576Sdavidxu ((*posix_clocks[clock].call) arglist) 103151576Sdavidxu 104151576SdavidxuSYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 105151576Sdavidxu 106151576Sdavidxu 10730739Sphkstatic void 10830739Sphkno_lease_updatetime(deltat) 10930739Sphk int deltat; 11030739Sphk{ 11130739Sphk} 11230739Sphk 11392723Salfredvoid (*lease_updatetime)(int) = no_lease_updatetime; 11430739Sphk 11525583Speterstatic int 116102074Sphksettime(struct thread *td, struct timeval *tv) 11725583Speter{ 11845433Snsayer struct timeval delta, tv1, tv2; 11945438Snsayer static struct timeval maxtime, laststep; 12033690Sphk struct timespec ts; 12125583Speter int s; 12225583Speter 12325656Speter s = splclock(); 12433818Sbde microtime(&tv1); 12535029Sphk delta = *tv; 12635029Sphk timevalsub(&delta, &tv1); 12725583Speter 12825583Speter /* 12933818Sbde * If the system is secure, we do not allow the time to be 13045433Snsayer * set to a value earlier than 1 second less than the highest 13145433Snsayer * time we have yet seen. The worst a miscreant can do in 13245433Snsayer * this circumstance is "freeze" time. He couldn't go 13345433Snsayer * back to the past. 13445438Snsayer * 13545438Snsayer * We similarly do not allow the clock to be stepped more 13645438Snsayer * than one second, nor more than once per second. This allows 13745438Snsayer * a miscreant to make the clock march double-time, but no worse. 13825583Speter */ 13994343Sjhb if (securelevel_gt(td->td_ucred, 1) != 0) { 14045433Snsayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 14145437Smjacob /* 14245438Snsayer * Update maxtime to latest time we've seen. 14345437Smjacob */ 14445437Smjacob if (tv1.tv_sec > maxtime.tv_sec) 14545437Smjacob maxtime = tv1; 14645437Smjacob tv2 = *tv; 14745437Smjacob timevalsub(&tv2, &maxtime); 14845437Smjacob if (tv2.tv_sec < -1) { 14945437Smjacob tv->tv_sec = maxtime.tv_sec - 1; 15045433Snsayer printf("Time adjustment clamped to -1 second\n"); 15145433Snsayer } 15245437Smjacob } else { 15345438Snsayer if (tv1.tv_sec == laststep.tv_sec) { 15445438Snsayer splx(s); 15545438Snsayer return (EPERM); 15645438Snsayer } 15745438Snsayer if (delta.tv_sec > 1) { 15845438Snsayer tv->tv_sec = tv1.tv_sec + 1; 15945438Snsayer printf("Time adjustment clamped to +1 second\n"); 16045438Snsayer } 16145438Snsayer laststep = *tv; 16245433Snsayer } 16333818Sbde } 16433818Sbde 16533690Sphk ts.tv_sec = tv->tv_sec; 16633690Sphk ts.tv_nsec = tv->tv_usec * 1000; 16794343Sjhb mtx_lock(&Giant); 16858377Sphk tc_setclock(&ts); 16925583Speter (void) splsoftclock(); 17025583Speter lease_updatetime(delta.tv_sec); 17125583Speter splx(s); 17225583Speter resettodr(); 17394343Sjhb mtx_unlock(&Giant); 17425583Speter return (0); 17525583Speter} 17625583Speter 17712221Sbde#ifndef _SYS_SYSPROTO_H_ 17825583Speterstruct clock_gettime_args { 17925583Speter clockid_t clock_id; 18025583Speter struct timespec *tp; 18125583Speter}; 18225583Speter#endif 18325656Speter 18482746Sdillon/* 18582746Sdillon * MPSAFE 18682746Sdillon */ 18725583Speter/* ARGSUSED */ 18825583Speterint 189102074Sphkclock_gettime(struct thread *td, struct clock_gettime_args *uap) 19025583Speter{ 19125583Speter struct timespec ats; 192151357Sps int error; 193151357Sps 194151357Sps error = kern_clock_gettime(td, uap->clock_id, &ats); 195151357Sps if (error == 0) 196151357Sps error = copyout(&ats, uap->tp, sizeof(ats)); 197151357Sps 198151357Sps return (error); 199151357Sps} 200151357Sps 201151357Spsint 202151357Spskern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 203151357Sps{ 204130884Skbyanc struct timeval sys, user; 205136152Sjhb struct proc *p; 20625583Speter 207136152Sjhb p = td->td_proc; 208151357Sps switch (clock_id) { 209130654Skbyanc case CLOCK_REALTIME: 210151357Sps nanotime(ats); 211130654Skbyanc break; 212130654Skbyanc case CLOCK_VIRTUAL: 213136152Sjhb PROC_LOCK(p); 214136152Sjhb calcru(p, &user, &sys); 215136152Sjhb PROC_UNLOCK(p); 216151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 217130654Skbyanc break; 218130654Skbyanc case CLOCK_PROF: 219136152Sjhb PROC_LOCK(p); 220136152Sjhb calcru(p, &user, &sys); 221136152Sjhb PROC_UNLOCK(p); 222130884Skbyanc timevaladd(&user, &sys); 223151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 224130654Skbyanc break; 225130884Skbyanc case CLOCK_MONOTONIC: 226151357Sps nanouptime(ats); 227130884Skbyanc break; 228130654Skbyanc default: 229111315Sphk return (EINVAL); 230130654Skbyanc } 231151357Sps return (0); 23225583Speter} 23325583Speter 23425583Speter#ifndef _SYS_SYSPROTO_H_ 23525583Speterstruct clock_settime_args { 23625583Speter clockid_t clock_id; 23725583Speter const struct timespec *tp; 23825583Speter}; 23925583Speter#endif 24025656Speter 24182746Sdillon/* 24282746Sdillon * MPSAFE 24382746Sdillon */ 24425583Speter/* ARGSUSED */ 24525583Speterint 246102074Sphkclock_settime(struct thread *td, struct clock_settime_args *uap) 24725583Speter{ 24825583Speter struct timespec ats; 24925583Speter int error; 25025583Speter 251151357Sps if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 252151357Sps return (error); 253151357Sps return (kern_clock_settime(td, uap->clock_id, &ats)); 254151357Sps} 255151357Sps 256151357Spsint 257151357Spskern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 258151357Sps{ 259151357Sps struct timeval atv; 260151357Sps int error; 261151357Sps 262106369Srwatson#ifdef MAC 263106369Srwatson error = mac_check_system_settime(td->td_ucred); 264106369Srwatson if (error) 265106369Srwatson return (error); 266106369Srwatson#endif 26793593Sjhb if ((error = suser(td)) != 0) 26894343Sjhb return (error); 269151357Sps if (clock_id != CLOCK_REALTIME) 27094343Sjhb return (EINVAL); 271151357Sps if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 27294343Sjhb return (EINVAL); 27334901Sphk /* XXX Don't convert nsec->usec and back */ 274151357Sps TIMESPEC_TO_TIMEVAL(&atv, ats); 27594343Sjhb error = settime(td, &atv); 27682746Sdillon return (error); 27725583Speter} 27825583Speter 27925583Speter#ifndef _SYS_SYSPROTO_H_ 28025583Speterstruct clock_getres_args { 28125583Speter clockid_t clock_id; 28225583Speter struct timespec *tp; 28325583Speter}; 28425583Speter#endif 28525656Speter 28625583Speterint 287102074Sphkclock_getres(struct thread *td, struct clock_getres_args *uap) 28825583Speter{ 28925583Speter struct timespec ts; 290151357Sps int error; 29125583Speter 292151357Sps if (uap->tp == NULL) 293151357Sps return (0); 294151357Sps 295151357Sps error = kern_clock_getres(td, uap->clock_id, &ts); 296151357Sps if (error == 0) 297151357Sps error = copyout(&ts, uap->tp, sizeof(ts)); 298151357Sps return (error); 299151357Sps} 300151357Sps 301151357Spsint 302151357Spskern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 303151357Sps{ 304151357Sps 305151357Sps ts->tv_sec = 0; 306151357Sps switch (clock_id) { 307130654Skbyanc case CLOCK_REALTIME: 308130654Skbyanc case CLOCK_MONOTONIC: 309103964Sbde /* 310103964Sbde * Round up the result of the division cheaply by adding 1. 311103964Sbde * Rounding up is especially important if rounding down 312103964Sbde * would give 0. Perfect rounding is unimportant. 313103964Sbde */ 314151357Sps ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 315130654Skbyanc break; 316130654Skbyanc case CLOCK_VIRTUAL: 317130654Skbyanc case CLOCK_PROF: 318130654Skbyanc /* Accurately round up here because we can do so cheaply. */ 319151357Sps ts->tv_nsec = (1000000000 + hz - 1) / hz; 320130654Skbyanc break; 321130654Skbyanc default: 322130654Skbyanc return (EINVAL); 323130654Skbyanc } 324151357Sps return (0); 32525583Speter} 32625583Speter 32726335Speterstatic int nanowait; 32825656Speter 329140481Spsint 330140481Spskern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 33125583Speter{ 33235045Sphk struct timespec ts, ts2, ts3; 33335042Sphk struct timeval tv; 33435042Sphk int error; 33525583Speter 33628773Sbde if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 33725656Speter return (EINVAL); 33843301Sdillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 33928773Sbde return (0); 34036119Sphk getnanouptime(&ts); 34135029Sphk timespecadd(&ts, rqt); 34235042Sphk TIMESPEC_TO_TIMEVAL(&tv, rqt); 34335042Sphk for (;;) { 34435042Sphk error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 34535042Sphk tvtohz(&tv)); 34636119Sphk getnanouptime(&ts2); 34735042Sphk if (error != EWOULDBLOCK) { 34835042Sphk if (error == ERESTART) 34935042Sphk error = EINTR; 35035042Sphk if (rmt != NULL) { 35135042Sphk timespecsub(&ts, &ts2); 35235042Sphk if (ts.tv_sec < 0) 35335042Sphk timespecclear(&ts); 35435042Sphk *rmt = ts; 35535042Sphk } 35635042Sphk return (error); 35735042Sphk } 35835029Sphk if (timespeccmp(&ts2, &ts, >=)) 35935042Sphk return (0); 36035045Sphk ts3 = ts; 36135045Sphk timespecsub(&ts3, &ts2); 36235045Sphk TIMESPEC_TO_TIMEVAL(&tv, &ts3); 36326335Speter } 36426335Speter} 36525583Speter 36626335Speter#ifndef _SYS_SYSPROTO_H_ 36726335Speterstruct nanosleep_args { 36826335Speter struct timespec *rqtp; 36926335Speter struct timespec *rmtp; 37026335Speter}; 37126335Speter#endif 37226335Speter 37382746Sdillon/* 37482746Sdillon * MPSAFE 37582746Sdillon */ 37626335Speter/* ARGSUSED */ 37726335Speterint 378102074Sphknanosleep(struct thread *td, struct nanosleep_args *uap) 37926335Speter{ 38026335Speter struct timespec rmt, rqt; 38182746Sdillon int error; 38226335Speter 383107849Salfred error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 38426335Speter if (error) 38526335Speter return (error); 38682746Sdillon 387109521Salfred if (uap->rmtp && 388109521Salfred !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 389109521Salfred return (EFAULT); 390140481Sps error = kern_nanosleep(td, &rqt, &rmt); 391107849Salfred if (error && uap->rmtp) { 39282746Sdillon int error2; 39382746Sdillon 394107849Salfred error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 395109521Salfred if (error2) 39682746Sdillon error = error2; 39725583Speter } 39825656Speter return (error); 39925583Speter} 40025583Speter 40126335Speter#ifndef _SYS_SYSPROTO_H_ 4021541Srgrimesstruct gettimeofday_args { 4031541Srgrimes struct timeval *tp; 4041541Srgrimes struct timezone *tzp; 4051541Srgrimes}; 40612221Sbde#endif 40782746Sdillon/* 40882746Sdillon * MPSAFE 40982746Sdillon */ 4101541Srgrimes/* ARGSUSED */ 4111549Srgrimesint 412102074Sphkgettimeofday(struct thread *td, struct gettimeofday_args *uap) 4131541Srgrimes{ 4141541Srgrimes struct timeval atv; 415110286Stjr struct timezone rtz; 4161541Srgrimes int error = 0; 4171541Srgrimes 4181541Srgrimes if (uap->tp) { 4191541Srgrimes microtime(&atv); 42099012Salfred error = copyout(&atv, uap->tp, sizeof (atv)); 4211541Srgrimes } 42290836Sphk if (error == 0 && uap->tzp != NULL) { 423110299Sphk rtz.tz_minuteswest = tz_minuteswest; 424110299Sphk rtz.tz_dsttime = tz_dsttime; 425110286Stjr error = copyout(&rtz, uap->tzp, sizeof (rtz)); 42682746Sdillon } 4271541Srgrimes return (error); 4281541Srgrimes} 4291541Srgrimes 43012221Sbde#ifndef _SYS_SYSPROTO_H_ 4311541Srgrimesstruct settimeofday_args { 4321541Srgrimes struct timeval *tv; 4331541Srgrimes struct timezone *tzp; 4341541Srgrimes}; 43512221Sbde#endif 43682746Sdillon/* 43782746Sdillon * MPSAFE 43882746Sdillon */ 4391541Srgrimes/* ARGSUSED */ 4401549Srgrimesint 441102074Sphksettimeofday(struct thread *td, struct settimeofday_args *uap) 4421541Srgrimes{ 443144445Sjhb struct timeval atv, *tvp; 444144445Sjhb struct timezone atz, *tzp; 445144445Sjhb int error; 4461541Srgrimes 447144445Sjhb if (uap->tv) { 448144445Sjhb error = copyin(uap->tv, &atv, sizeof(atv)); 449144445Sjhb if (error) 450144445Sjhb return (error); 451144445Sjhb tvp = &atv; 452144445Sjhb } else 453144445Sjhb tvp = NULL; 454144445Sjhb if (uap->tzp) { 455144445Sjhb error = copyin(uap->tzp, &atz, sizeof(atz)); 456144445Sjhb if (error) 457144445Sjhb return (error); 458144445Sjhb tzp = &atz; 459144445Sjhb } else 460144445Sjhb tzp = NULL; 461144445Sjhb return (kern_settimeofday(td, tvp, tzp)); 462144445Sjhb} 463144445Sjhb 464144445Sjhbint 465144445Sjhbkern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 466144445Sjhb{ 467144445Sjhb int error; 468144445Sjhb 469106369Srwatson#ifdef MAC 470106369Srwatson error = mac_check_system_settime(td->td_ucred); 471106369Srwatson if (error) 472106369Srwatson return (error); 473106369Srwatson#endif 474144445Sjhb error = suser(td); 475144445Sjhb if (error) 47694343Sjhb return (error); 4771541Srgrimes /* Verify all parameters before changing time. */ 478144445Sjhb if (tv) { 479144445Sjhb if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 48094343Sjhb return (EINVAL); 481144445Sjhb error = settime(td, tv); 48225656Speter } 483144445Sjhb if (tzp && error == 0) { 484144445Sjhb tz_minuteswest = tzp->tz_minuteswest; 485144445Sjhb tz_dsttime = tzp->tz_dsttime; 48682746Sdillon } 48782746Sdillon return (error); 4881541Srgrimes} 489144445Sjhb 49082746Sdillon/* 4911541Srgrimes * Get value of an interval timer. The process virtual and 4921541Srgrimes * profiling virtual time timers are kept in the p_stats area, since 4931541Srgrimes * they can be swapped out. These are kept internally in the 4941541Srgrimes * way they are specified externally: in time until they expire. 4951541Srgrimes * 4961541Srgrimes * The real time interval timer is kept in the process table slot 4971541Srgrimes * for the process, and its value (it_value) is kept as an 4981541Srgrimes * absolute time rather than as a delta, so that it is easy to keep 4991541Srgrimes * periodic real-time signals from drifting. 5001541Srgrimes * 5011541Srgrimes * Virtual time timers are processed in the hardclock() routine of 5021541Srgrimes * kern_clock.c. The real time timer is processed by a timeout 5031541Srgrimes * routine, called from the softclock() routine. Since a callout 5041541Srgrimes * may be delayed in real time due to interrupt processing in the system, 5051541Srgrimes * it is possible for the real time timeout routine (realitexpire, given below), 5061541Srgrimes * to be delayed in real time past when it is supposed to occur. It 5071541Srgrimes * does not suffice, therefore, to reload the real timer .it_value from the 5081541Srgrimes * real time timers .it_interval. Rather, we compute the next time in 5091541Srgrimes * absolute time the timer should go off. 5101541Srgrimes */ 51112221Sbde#ifndef _SYS_SYSPROTO_H_ 5121541Srgrimesstruct getitimer_args { 5131541Srgrimes u_int which; 5141541Srgrimes struct itimerval *itv; 5151541Srgrimes}; 51612221Sbde#endif 51782746Sdillon/* 51882746Sdillon * MPSAFE 51982746Sdillon */ 5201549Srgrimesint 521102074Sphkgetitimer(struct thread *td, struct getitimer_args *uap) 5221541Srgrimes{ 523141470Sjhb struct itimerval aitv; 524140832Ssobomax int error; 525140832Ssobomax 526140832Ssobomax error = kern_getitimer(td, uap->which, &aitv); 527140832Ssobomax if (error != 0) 528140832Ssobomax return (error); 529140832Ssobomax return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 530140832Ssobomax} 531140832Ssobomax 532140832Ssobomaxint 533140832Ssobomaxkern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 534140832Ssobomax{ 53583366Sjulian struct proc *p = td->td_proc; 53634961Sphk struct timeval ctv; 5371541Srgrimes 538140832Ssobomax if (which > ITIMER_PROF) 5391541Srgrimes return (EINVAL); 54082746Sdillon 541140832Ssobomax if (which == ITIMER_REAL) { 5421541Srgrimes /* 54336128Sbde * Convert from absolute to relative time in .it_value 5441541Srgrimes * part of real time timer. If time for real time timer 5451541Srgrimes * has passed return 0, else return difference between 5461541Srgrimes * current time and time for the timer to go off. 5471541Srgrimes */ 548111034Stjr PROC_LOCK(p); 549140832Ssobomax *aitv = p->p_realtimer; 550111034Stjr PROC_UNLOCK(p); 551140832Ssobomax if (timevalisset(&aitv->it_value)) { 55236119Sphk getmicrouptime(&ctv); 553140832Ssobomax if (timevalcmp(&aitv->it_value, &ctv, <)) 554140832Ssobomax timevalclear(&aitv->it_value); 5551541Srgrimes else 556140832Ssobomax timevalsub(&aitv->it_value, &ctv); 55734961Sphk } 55882746Sdillon } else { 559111034Stjr mtx_lock_spin(&sched_lock); 560140832Ssobomax *aitv = p->p_stats->p_timer[which]; 561111034Stjr mtx_unlock_spin(&sched_lock); 56282746Sdillon } 563140832Ssobomax return (0); 5641541Srgrimes} 5651541Srgrimes 56612221Sbde#ifndef _SYS_SYSPROTO_H_ 5671541Srgrimesstruct setitimer_args { 5681541Srgrimes u_int which; 5691541Srgrimes struct itimerval *itv, *oitv; 5701541Srgrimes}; 57112221Sbde#endif 572140832Ssobomax 57382746Sdillon/* 57482746Sdillon * MPSAFE 57582746Sdillon */ 5761549Srgrimesint 577102074Sphksetitimer(struct thread *td, struct setitimer_args *uap) 5781541Srgrimes{ 579141470Sjhb struct itimerval aitv, oitv; 580140832Ssobomax int error; 5811541Srgrimes 582111034Stjr if (uap->itv == NULL) { 583111034Stjr uap->itv = uap->oitv; 584111034Stjr return (getitimer(td, (struct getitimer_args *)uap)); 585111034Stjr } 586111034Stjr 587111034Stjr if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 5881541Srgrimes return (error); 589140832Ssobomax error = kern_setitimer(td, uap->which, &aitv, &oitv); 590140832Ssobomax if (error != 0 || uap->oitv == NULL) 591140832Ssobomax return (error); 592140832Ssobomax return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 593140832Ssobomax} 594140832Ssobomax 595140832Ssobomaxint 596141470Sjhbkern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 597141470Sjhb struct itimerval *oitv) 598140832Ssobomax{ 599140832Ssobomax struct proc *p = td->td_proc; 600140832Ssobomax struct timeval ctv; 601140832Ssobomax 602141483Sjhb if (aitv == NULL) 603141483Sjhb return (kern_getitimer(td, which, oitv)); 604141483Sjhb 605140832Ssobomax if (which > ITIMER_PROF) 606111034Stjr return (EINVAL); 607140832Ssobomax if (itimerfix(&aitv->it_value)) 608111034Stjr return (EINVAL); 609140832Ssobomax if (!timevalisset(&aitv->it_value)) 610140832Ssobomax timevalclear(&aitv->it_interval); 611140832Ssobomax else if (itimerfix(&aitv->it_interval)) 612140832Ssobomax return (EINVAL); 61382746Sdillon 614140832Ssobomax if (which == ITIMER_REAL) { 615111034Stjr PROC_LOCK(p); 61635058Sphk if (timevalisset(&p->p_realtimer.it_value)) 61769286Sjake callout_stop(&p->p_itcallout); 618114980Sjhb getmicrouptime(&ctv); 619140832Ssobomax if (timevalisset(&aitv->it_value)) { 620140832Ssobomax callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 62169286Sjake realitexpire, p); 622140832Ssobomax timevaladd(&aitv->it_value, &ctv); 623114980Sjhb } 624140832Ssobomax *oitv = p->p_realtimer; 625140832Ssobomax p->p_realtimer = *aitv; 626111034Stjr PROC_UNLOCK(p); 627140832Ssobomax if (timevalisset(&oitv->it_value)) { 628140832Ssobomax if (timevalcmp(&oitv->it_value, &ctv, <)) 629140832Ssobomax timevalclear(&oitv->it_value); 630111034Stjr else 631140832Ssobomax timevalsub(&oitv->it_value, &ctv); 632111034Stjr } 63382746Sdillon } else { 634111034Stjr mtx_lock_spin(&sched_lock); 635140832Ssobomax *oitv = p->p_stats->p_timer[which]; 636140832Ssobomax p->p_stats->p_timer[which] = *aitv; 637111034Stjr mtx_unlock_spin(&sched_lock); 63882746Sdillon } 639140832Ssobomax return (0); 6401541Srgrimes} 6411541Srgrimes 6421541Srgrimes/* 6431541Srgrimes * Real interval timer expired: 6441541Srgrimes * send process whose timer expired an alarm signal. 6451541Srgrimes * If time is not set up to reload, then just return. 6461541Srgrimes * Else compute next time timer should go off which is > current time. 6471541Srgrimes * This is where delay in processing this timeout causes multiple 6481541Srgrimes * SIGALRM calls to be compressed into one. 64936127Sbde * tvtohz() always adds 1 to allow for the time until the next clock 6509327Sbde * interrupt being strictly less than 1 clock tick, but we don't want 6519327Sbde * that here since we want to appear to be in sync with the clock 6529327Sbde * interrupt even when we're delayed. 6531541Srgrimes */ 6541541Srgrimesvoid 655102074Sphkrealitexpire(void *arg) 6561541Srgrimes{ 657102074Sphk struct proc *p; 65835044Sphk struct timeval ctv, ntv; 6591541Srgrimes 6601541Srgrimes p = (struct proc *)arg; 66173916Sjhb PROC_LOCK(p); 6621541Srgrimes psignal(p, SIGALRM); 66335058Sphk if (!timevalisset(&p->p_realtimer.it_interval)) { 66435058Sphk timevalclear(&p->p_realtimer.it_value); 665116123Sjhb if (p->p_flag & P_WEXIT) 666116123Sjhb wakeup(&p->p_itcallout); 66773916Sjhb PROC_UNLOCK(p); 6681541Srgrimes return; 6691541Srgrimes } 6701541Srgrimes for (;;) { 6711541Srgrimes timevaladd(&p->p_realtimer.it_value, 6721541Srgrimes &p->p_realtimer.it_interval); 67336119Sphk getmicrouptime(&ctv); 67435058Sphk if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 67535044Sphk ntv = p->p_realtimer.it_value; 67635044Sphk timevalsub(&ntv, &ctv); 67769286Sjake callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 67869286Sjake realitexpire, p); 67973916Sjhb PROC_UNLOCK(p); 6801541Srgrimes return; 6811541Srgrimes } 6821541Srgrimes } 68373916Sjhb /*NOTREACHED*/ 6841541Srgrimes} 6851541Srgrimes 6861541Srgrimes/* 6871541Srgrimes * Check that a proposed value to load into the .it_value or 6881541Srgrimes * .it_interval part of an interval timer is acceptable, and 6891541Srgrimes * fix it to have at least minimal value (i.e. if it is less 6901541Srgrimes * than the resolution of the clock, round it up.) 6911541Srgrimes */ 6921549Srgrimesint 693102074Sphkitimerfix(struct timeval *tv) 6941541Srgrimes{ 6951541Srgrimes 696151576Sdavidxu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 6971541Srgrimes return (EINVAL); 6981541Srgrimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 6991541Srgrimes tv->tv_usec = tick; 7001541Srgrimes return (0); 7011541Srgrimes} 7021541Srgrimes 7031541Srgrimes/* 7041541Srgrimes * Decrement an interval timer by a specified number 7051541Srgrimes * of microseconds, which must be less than a second, 7061541Srgrimes * i.e. < 1000000. If the timer expires, then reload 7071541Srgrimes * it. In this case, carry over (usec - old value) to 7081541Srgrimes * reduce the value reloaded into the timer so that 7091541Srgrimes * the timer does not drift. This routine assumes 7101541Srgrimes * that it is called in a context where the timers 7111541Srgrimes * on which it is operating cannot change in value. 7121541Srgrimes */ 7131549Srgrimesint 714102074Sphkitimerdecr(struct itimerval *itp, int usec) 7151541Srgrimes{ 7161541Srgrimes 7171541Srgrimes if (itp->it_value.tv_usec < usec) { 7181541Srgrimes if (itp->it_value.tv_sec == 0) { 7191541Srgrimes /* expired, and already in next interval */ 7201541Srgrimes usec -= itp->it_value.tv_usec; 7211541Srgrimes goto expire; 7221541Srgrimes } 7231541Srgrimes itp->it_value.tv_usec += 1000000; 7241541Srgrimes itp->it_value.tv_sec--; 7251541Srgrimes } 7261541Srgrimes itp->it_value.tv_usec -= usec; 7271541Srgrimes usec = 0; 72835058Sphk if (timevalisset(&itp->it_value)) 7291541Srgrimes return (1); 7301541Srgrimes /* expired, exactly at end of interval */ 7311541Srgrimesexpire: 73235058Sphk if (timevalisset(&itp->it_interval)) { 7331541Srgrimes itp->it_value = itp->it_interval; 7341541Srgrimes itp->it_value.tv_usec -= usec; 7351541Srgrimes if (itp->it_value.tv_usec < 0) { 7361541Srgrimes itp->it_value.tv_usec += 1000000; 7371541Srgrimes itp->it_value.tv_sec--; 7381541Srgrimes } 7391541Srgrimes } else 7401541Srgrimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 7411541Srgrimes return (0); 7421541Srgrimes} 7431541Srgrimes 7441541Srgrimes/* 7451541Srgrimes * Add and subtract routines for timevals. 7461541Srgrimes * N.B.: subtract routine doesn't deal with 7471541Srgrimes * results which are before the beginning, 7481541Srgrimes * it just gets very confused in this case. 7491541Srgrimes * Caveat emptor. 7501541Srgrimes */ 7511549Srgrimesvoid 752121523Salfredtimevaladd(struct timeval *t1, const struct timeval *t2) 7531541Srgrimes{ 7541541Srgrimes 7551541Srgrimes t1->tv_sec += t2->tv_sec; 7561541Srgrimes t1->tv_usec += t2->tv_usec; 7571541Srgrimes timevalfix(t1); 7581541Srgrimes} 7591541Srgrimes 7601549Srgrimesvoid 761121523Salfredtimevalsub(struct timeval *t1, const struct timeval *t2) 7621541Srgrimes{ 7631541Srgrimes 7641541Srgrimes t1->tv_sec -= t2->tv_sec; 7651541Srgrimes t1->tv_usec -= t2->tv_usec; 7661541Srgrimes timevalfix(t1); 7671541Srgrimes} 7681541Srgrimes 76912819Sphkstatic void 770102074Sphktimevalfix(struct timeval *t1) 7711541Srgrimes{ 7721541Srgrimes 7731541Srgrimes if (t1->tv_usec < 0) { 7741541Srgrimes t1->tv_sec--; 7751541Srgrimes t1->tv_usec += 1000000; 7761541Srgrimes } 7771541Srgrimes if (t1->tv_usec >= 1000000) { 7781541Srgrimes t1->tv_sec++; 7791541Srgrimes t1->tv_usec -= 1000000; 7801541Srgrimes } 7811541Srgrimes} 782108142Ssam 783108142Ssam/* 784108511Ssam * ratecheck(): simple time-based rate-limit checking. 785108142Ssam */ 786108142Ssamint 787108142Ssamratecheck(struct timeval *lasttime, const struct timeval *mininterval) 788108142Ssam{ 789108142Ssam struct timeval tv, delta; 790108142Ssam int rv = 0; 791108142Ssam 792108511Ssam getmicrouptime(&tv); /* NB: 10ms precision */ 793108511Ssam delta = tv; 794108511Ssam timevalsub(&delta, lasttime); 795108142Ssam 796108142Ssam /* 797108142Ssam * check for 0,0 is so that the message will be seen at least once, 798108142Ssam * even if interval is huge. 799108142Ssam */ 800108142Ssam if (timevalcmp(&delta, mininterval, >=) || 801108142Ssam (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 802108142Ssam *lasttime = tv; 803108142Ssam rv = 1; 804108142Ssam } 805108142Ssam 806108142Ssam return (rv); 807108142Ssam} 808108142Ssam 809108142Ssam/* 810108142Ssam * ppsratecheck(): packets (or events) per second limitation. 811108511Ssam * 812108511Ssam * Return 0 if the limit is to be enforced (e.g. the caller 813108511Ssam * should drop a packet because of the rate limitation). 814108511Ssam * 815111558Ssam * maxpps of 0 always causes zero to be returned. maxpps of -1 816111558Ssam * always causes 1 to be returned; this effectively defeats rate 817111558Ssam * limiting. 818111558Ssam * 819108511Ssam * Note that we maintain the struct timeval for compatibility 820108511Ssam * with other bsd systems. We reuse the storage and just monitor 821108511Ssam * clock ticks for minimal overhead. 822108142Ssam */ 823108142Ssamint 824108142Ssamppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 825108142Ssam{ 826108511Ssam int now; 827108142Ssam 828108142Ssam /* 829108511Ssam * Reset the last time and counter if this is the first call 830108511Ssam * or more than a second has passed since the last update of 831108511Ssam * lasttime. 832108142Ssam */ 833108511Ssam now = ticks; 834108511Ssam if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 835108511Ssam lasttime->tv_sec = now; 836108511Ssam *curpps = 1; 837111558Ssam return (maxpps != 0); 838108511Ssam } else { 839108511Ssam (*curpps)++; /* NB: ignore potential overflow */ 840108511Ssam return (maxpps < 0 || *curpps < maxpps); 841108511Ssam } 842108142Ssam} 843151576Sdavidxu 844151576Sdavidxustatic void 845151576Sdavidxuitimer_start(void) 846151576Sdavidxu{ 847151576Sdavidxu struct kclock rt_clock = { 848151576Sdavidxu .timer_create = realtimer_create, 849151576Sdavidxu .timer_delete = realtimer_delete, 850151576Sdavidxu .timer_settime = realtimer_settime, 851151576Sdavidxu .timer_gettime = realtimer_gettime, 852151576Sdavidxu .event_hook = realtimer_event_hook 853151576Sdavidxu }; 854151576Sdavidxu 855151576Sdavidxu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 856151576Sdavidxu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 857151576Sdavidxu register_posix_clock(CLOCK_REALTIME, &rt_clock); 858151576Sdavidxu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 859151576Sdavidxu} 860151576Sdavidxu 861151576Sdavidxuint 862151576Sdavidxuregister_posix_clock(int clockid, struct kclock *clk) 863151576Sdavidxu{ 864151576Sdavidxu if ((unsigned)clockid >= MAX_CLOCKS) { 865151576Sdavidxu printf("%s: invalid clockid\n", __func__); 866151576Sdavidxu return (0); 867151576Sdavidxu } 868151576Sdavidxu posix_clocks[clockid] = *clk; 869151576Sdavidxu return (1); 870151576Sdavidxu} 871151576Sdavidxu 872151576Sdavidxustatic int 873151576Sdavidxuitimer_init(void *mem, int size, int flags) 874151576Sdavidxu{ 875151576Sdavidxu struct itimer *it; 876151576Sdavidxu 877151576Sdavidxu it = (struct itimer *)mem; 878151576Sdavidxu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 879151576Sdavidxu return (0); 880151576Sdavidxu} 881151576Sdavidxu 882151576Sdavidxustatic void 883151576Sdavidxuitimer_fini(void *mem, int size) 884151576Sdavidxu{ 885151576Sdavidxu struct itimer *it; 886151576Sdavidxu 887151576Sdavidxu it = (struct itimer *)mem; 888151576Sdavidxu mtx_destroy(&it->it_mtx); 889151576Sdavidxu} 890151576Sdavidxu 891151576Sdavidxustatic void 892151576Sdavidxuitimer_enter(struct itimer *it) 893151576Sdavidxu{ 894151576Sdavidxu 895151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 896151576Sdavidxu it->it_usecount++; 897151576Sdavidxu} 898151576Sdavidxu 899151576Sdavidxustatic void 900151576Sdavidxuitimer_leave(struct itimer *it) 901151576Sdavidxu{ 902151576Sdavidxu 903151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 904151576Sdavidxu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 905151576Sdavidxu 906151576Sdavidxu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 907151576Sdavidxu wakeup(it); 908151576Sdavidxu} 909151576Sdavidxu 910151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 911151576Sdavidxustruct timer_create_args { 912151576Sdavidxu clockid_t clock_id; 913151576Sdavidxu struct sigevent * evp; 914151576Sdavidxu timer_t * timerid; 915151576Sdavidxu}; 916151576Sdavidxu#endif 917151576Sdavidxu 918151576Sdavidxuint 919151576Sdavidxutimer_create(struct thread *td, struct timer_create_args *uap) 920151576Sdavidxu{ 921151576Sdavidxu struct sigevent *evp1, ev; 922151576Sdavidxu timer_t id; 923151576Sdavidxu int error; 924151576Sdavidxu 925151576Sdavidxu if (uap->evp != NULL) { 926151576Sdavidxu error = copyin(uap->evp, &ev, sizeof(ev)); 927151576Sdavidxu if (error != 0) 928151576Sdavidxu return (error); 929151576Sdavidxu evp1 = &ev; 930151576Sdavidxu } else 931151576Sdavidxu evp1 = NULL; 932151576Sdavidxu 933151576Sdavidxu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 934151576Sdavidxu 935151576Sdavidxu if (error == 0) { 936151576Sdavidxu error = copyout(&id, uap->timerid, sizeof(timer_t)); 937151576Sdavidxu if (error != 0) 938151576Sdavidxu kern_timer_delete(td, id); 939151576Sdavidxu } 940151576Sdavidxu return (error); 941151576Sdavidxu} 942151576Sdavidxu 943151576Sdavidxustatic int 944151576Sdavidxukern_timer_create(struct thread *td, clockid_t clock_id, 945151576Sdavidxu struct sigevent *evp, timer_t *timerid, timer_t preset_id) 946151576Sdavidxu{ 947151576Sdavidxu struct proc *p = td->td_proc; 948151576Sdavidxu struct itimer *it; 949151576Sdavidxu int id; 950151576Sdavidxu int error; 951151576Sdavidxu 952151576Sdavidxu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 953151576Sdavidxu return (EINVAL); 954151576Sdavidxu 955151576Sdavidxu if (posix_clocks[clock_id].timer_create == NULL) 956151576Sdavidxu return (EINVAL); 957151576Sdavidxu 958151576Sdavidxu if (evp != NULL) { 959151576Sdavidxu if (evp->sigev_notify != SIGEV_NONE && 960151576Sdavidxu evp->sigev_notify != SIGEV_SIGNAL) 961151576Sdavidxu return (EINVAL); 962151576Sdavidxu if (evp->sigev_notify == SIGEV_SIGNAL && 963151576Sdavidxu !_SIG_VALID(evp->sigev_signo)) 964151576Sdavidxu return (EINVAL); 965151576Sdavidxu } 966151576Sdavidxu 967151585Sdavidxu if (p->p_itimers == NULL) 968151576Sdavidxu itimers_alloc(p); 969151576Sdavidxu 970151576Sdavidxu it = uma_zalloc(itimer_zone, M_WAITOK); 971151576Sdavidxu it->it_flags = 0; 972151576Sdavidxu it->it_usecount = 0; 973151576Sdavidxu it->it_active = 0; 974151576Sdavidxu timevalclear(&it->it_time.it_value); 975151576Sdavidxu timevalclear(&it->it_time.it_interval); 976151576Sdavidxu it->it_overrun = 0; 977151576Sdavidxu it->it_overrun_last = 0; 978151576Sdavidxu it->it_clockid = clock_id; 979151576Sdavidxu it->it_timerid = -1; 980151576Sdavidxu it->it_proc = p; 981151576Sdavidxu ksiginfo_init(&it->it_ksi); 982151576Sdavidxu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 983151576Sdavidxu error = CLOCK_CALL(clock_id, timer_create, (it)); 984151576Sdavidxu if (error != 0) 985151576Sdavidxu goto out; 986151576Sdavidxu 987151576Sdavidxu PROC_LOCK(p); 988151576Sdavidxu if (preset_id != -1) { 989151576Sdavidxu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 990151576Sdavidxu id = preset_id; 991151585Sdavidxu if (p->p_itimers->its_timers[id] != NULL) { 992151576Sdavidxu PROC_UNLOCK(p); 993151576Sdavidxu error = 0; 994151576Sdavidxu goto out; 995151576Sdavidxu } 996151576Sdavidxu } else { 997151576Sdavidxu /* 998151576Sdavidxu * Find a free timer slot, skipping those reserved 999151576Sdavidxu * for setitimer(). 1000151576Sdavidxu */ 1001151576Sdavidxu for (id = 3; id < TIMER_MAX; id++) 1002151585Sdavidxu if (p->p_itimers->its_timers[id] == NULL) 1003151576Sdavidxu break; 1004151576Sdavidxu if (id == TIMER_MAX) { 1005151576Sdavidxu PROC_UNLOCK(p); 1006151576Sdavidxu error = EAGAIN; 1007151576Sdavidxu goto out; 1008151576Sdavidxu } 1009151576Sdavidxu } 1010151576Sdavidxu it->it_timerid = id; 1011151585Sdavidxu p->p_itimers->its_timers[id] = it; 1012151576Sdavidxu if (evp != NULL) 1013151576Sdavidxu it->it_sigev = *evp; 1014151576Sdavidxu else { 1015151576Sdavidxu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 1016151576Sdavidxu switch (clock_id) { 1017151576Sdavidxu default: 1018151576Sdavidxu case CLOCK_REALTIME: 1019151576Sdavidxu it->it_sigev.sigev_signo = SIGALRM; 1020151576Sdavidxu break; 1021151576Sdavidxu case CLOCK_VIRTUAL: 1022151576Sdavidxu it->it_sigev.sigev_signo = SIGVTALRM; 1023151576Sdavidxu break; 1024151576Sdavidxu case CLOCK_PROF: 1025151576Sdavidxu it->it_sigev.sigev_signo = SIGPROF; 1026151576Sdavidxu break; 1027151576Sdavidxu } 1028151576Sdavidxu it->it_sigev.sigev_value.sigval_int = id; 1029151576Sdavidxu } 1030151576Sdavidxu 1031151576Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL) { 1032151576Sdavidxu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 1033151576Sdavidxu it->it_ksi.ksi_code = SI_TIMER; 1034151576Sdavidxu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 1035151576Sdavidxu it->it_ksi.ksi_timerid = id; 1036151576Sdavidxu } 1037151576Sdavidxu PROC_UNLOCK(p); 1038151576Sdavidxu *timerid = id; 1039151576Sdavidxu return (0); 1040151576Sdavidxu 1041151576Sdavidxuout: 1042151576Sdavidxu ITIMER_LOCK(it); 1043151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1044151576Sdavidxu ITIMER_UNLOCK(it); 1045151576Sdavidxu uma_zfree(itimer_zone, it); 1046151576Sdavidxu return (error); 1047151576Sdavidxu} 1048151576Sdavidxu 1049151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1050151576Sdavidxustruct timer_delete_args { 1051151576Sdavidxu timer_t timerid; 1052151576Sdavidxu}; 1053151576Sdavidxu#endif 1054151576Sdavidxu 1055151576Sdavidxuint 1056151576Sdavidxutimer_delete(struct thread *td, struct timer_delete_args *uap) 1057151576Sdavidxu{ 1058151576Sdavidxu return (kern_timer_delete(td, uap->timerid)); 1059151576Sdavidxu} 1060151576Sdavidxu 1061151576Sdavidxustatic struct itimer * 1062151576Sdavidxuitimer_find(struct proc *p, timer_t timerid, int include_deleting) 1063151576Sdavidxu{ 1064151576Sdavidxu struct itimer *it; 1065151576Sdavidxu 1066151576Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1067151585Sdavidxu if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) || 1068151585Sdavidxu (it = p->p_itimers->its_timers[timerid]) == NULL) { 1069151576Sdavidxu return (NULL); 1070151576Sdavidxu } 1071151576Sdavidxu ITIMER_LOCK(it); 1072151576Sdavidxu if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) { 1073151576Sdavidxu ITIMER_UNLOCK(it); 1074151576Sdavidxu it = NULL; 1075151576Sdavidxu } 1076151576Sdavidxu return (it); 1077151576Sdavidxu} 1078151576Sdavidxu 1079151576Sdavidxustatic int 1080151576Sdavidxukern_timer_delete(struct thread *td, timer_t timerid) 1081151576Sdavidxu{ 1082151576Sdavidxu struct proc *p = td->td_proc; 1083151576Sdavidxu struct itimer *it; 1084151576Sdavidxu 1085151576Sdavidxu PROC_LOCK(p); 1086151576Sdavidxu it = itimer_find(p, timerid, 0); 1087151576Sdavidxu if (it == NULL) { 1088151576Sdavidxu PROC_UNLOCK(p); 1089151576Sdavidxu return (EINVAL); 1090151576Sdavidxu } 1091151576Sdavidxu PROC_UNLOCK(p); 1092151576Sdavidxu 1093151576Sdavidxu it->it_flags |= ITF_DELETING; 1094151576Sdavidxu while (it->it_usecount > 0) { 1095151576Sdavidxu it->it_flags |= ITF_WANTED; 1096151576Sdavidxu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 1097151576Sdavidxu } 1098151576Sdavidxu it->it_flags &= ~ITF_WANTED; 1099151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1100151576Sdavidxu ITIMER_UNLOCK(it); 1101151576Sdavidxu 1102151576Sdavidxu PROC_LOCK(p); 1103151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) 1104151576Sdavidxu sigqueue_take(&it->it_ksi); 1105151585Sdavidxu p->p_itimers->its_timers[timerid] = NULL; 1106151576Sdavidxu PROC_UNLOCK(p); 1107151576Sdavidxu uma_zfree(itimer_zone, it); 1108151576Sdavidxu return (0); 1109151576Sdavidxu} 1110151576Sdavidxu 1111151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1112151576Sdavidxustruct timer_settime_args { 1113151576Sdavidxu timer_t timerid; 1114151576Sdavidxu int flags; 1115151576Sdavidxu const struct itimerspec * value; 1116151576Sdavidxu struct itimerspec * ovalue; 1117151576Sdavidxu}; 1118151576Sdavidxu#endif 1119151576Sdavidxu 1120151576Sdavidxuint 1121151576Sdavidxutimer_settime(struct thread *td, struct timer_settime_args *uap) 1122151576Sdavidxu{ 1123151576Sdavidxu struct proc *p = td->td_proc; 1124151576Sdavidxu struct itimer *it; 1125151576Sdavidxu struct itimerspec val, oval, *ovalp; 1126151576Sdavidxu int error; 1127151576Sdavidxu 1128151576Sdavidxu error = copyin(uap->value, &val, sizeof(val)); 1129151576Sdavidxu if (error != 0) 1130151576Sdavidxu return (error); 1131151576Sdavidxu 1132151576Sdavidxu if (uap->ovalue != NULL) 1133151576Sdavidxu ovalp = &oval; 1134151576Sdavidxu else 1135151576Sdavidxu ovalp = NULL; 1136151576Sdavidxu 1137151576Sdavidxu PROC_LOCK(p); 1138151576Sdavidxu if (uap->timerid < 3 || 1139151576Sdavidxu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 1140151576Sdavidxu PROC_UNLOCK(p); 1141151576Sdavidxu error = EINVAL; 1142151576Sdavidxu } else { 1143151576Sdavidxu PROC_UNLOCK(p); 1144151576Sdavidxu itimer_enter(it); 1145151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_settime, 1146151576Sdavidxu (it, uap->flags, &val, ovalp)); 1147151576Sdavidxu itimer_leave(it); 1148151576Sdavidxu ITIMER_UNLOCK(it); 1149151576Sdavidxu } 1150151576Sdavidxu if (error == 0 && uap->ovalue != NULL) 1151151576Sdavidxu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1152151576Sdavidxu return (error); 1153151576Sdavidxu} 1154151576Sdavidxu 1155151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1156151576Sdavidxustruct timer_gettime_args { 1157151576Sdavidxu timer_t timerid; 1158151576Sdavidxu struct itimerspec * value; 1159151576Sdavidxu}; 1160151576Sdavidxu#endif 1161151576Sdavidxu 1162151576Sdavidxuint 1163151576Sdavidxutimer_gettime(struct thread *td, struct timer_gettime_args *uap) 1164151576Sdavidxu{ 1165151576Sdavidxu struct proc *p = td->td_proc; 1166151576Sdavidxu struct itimer *it; 1167151576Sdavidxu struct itimerspec val; 1168151576Sdavidxu int error; 1169151576Sdavidxu 1170151576Sdavidxu PROC_LOCK(p); 1171151576Sdavidxu if (uap->timerid < 3 || 1172151576Sdavidxu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 1173151576Sdavidxu PROC_UNLOCK(p); 1174151576Sdavidxu error = EINVAL; 1175151576Sdavidxu } else { 1176151576Sdavidxu PROC_UNLOCK(p); 1177151576Sdavidxu itimer_enter(it); 1178151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_gettime, 1179151576Sdavidxu (it, &val)); 1180151576Sdavidxu itimer_leave(it); 1181151576Sdavidxu ITIMER_UNLOCK(it); 1182151576Sdavidxu } 1183151576Sdavidxu if (error == 0) 1184151576Sdavidxu error = copyout(&val, uap->value, sizeof(val)); 1185151576Sdavidxu return (error); 1186151576Sdavidxu} 1187151576Sdavidxu 1188151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1189151576Sdavidxustruct timer_getoverrun_args { 1190151576Sdavidxu timer_t timerid; 1191151576Sdavidxu}; 1192151576Sdavidxu#endif 1193151576Sdavidxu 1194151576Sdavidxuint 1195151576Sdavidxutimer_getoverrun(struct thread *td, struct timer_getoverrun_args *uap) 1196151576Sdavidxu{ 1197151576Sdavidxu struct proc *p = td->td_proc; 1198151576Sdavidxu struct itimer *it; 1199151576Sdavidxu int error ; 1200151576Sdavidxu 1201151576Sdavidxu PROC_LOCK(p); 1202151576Sdavidxu if (uap->timerid < 3 || 1203151576Sdavidxu (it = itimer_find(p, uap->timerid, 0)) == NULL) { 1204151576Sdavidxu PROC_UNLOCK(p); 1205151576Sdavidxu error = EINVAL; 1206151576Sdavidxu } else { 1207151576Sdavidxu PROC_UNLOCK(p); 1208151576Sdavidxu td->td_retval[0] = it->it_overrun_last; 1209151576Sdavidxu ITIMER_UNLOCK(it); 1210151576Sdavidxu error = 0; 1211151576Sdavidxu } 1212151576Sdavidxu return (error); 1213151576Sdavidxu} 1214151576Sdavidxu 1215151576Sdavidxustatic int 1216151576Sdavidxurealtimer_create(struct itimer *it) 1217151576Sdavidxu{ 1218151576Sdavidxu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 1219151576Sdavidxu return (0); 1220151576Sdavidxu} 1221151576Sdavidxu 1222151576Sdavidxustatic int 1223151576Sdavidxurealtimer_delete(struct itimer *it) 1224151576Sdavidxu{ 1225151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1226151576Sdavidxu callout_stop(&it->it_callout); 1227151576Sdavidxu return (0); 1228151576Sdavidxu} 1229151576Sdavidxu 1230151576Sdavidxustatic int 1231151576Sdavidxurealtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 1232151576Sdavidxu{ 1233151576Sdavidxu struct timespec ts; 1234151576Sdavidxu 1235151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1236151576Sdavidxu 1237151576Sdavidxu TIMEVAL_TO_TIMESPEC(&it->it_time.it_value, &ovalue->it_value); 1238151576Sdavidxu TIMEVAL_TO_TIMESPEC(&it->it_time.it_interval, &ovalue->it_interval); 1239151576Sdavidxu if (it->it_clockid == CLOCK_REALTIME) 1240151576Sdavidxu getnanotime(&ts); 1241151576Sdavidxu else /* CLOCK_MONOTONIC */ 1242151576Sdavidxu getnanouptime(&ts); 1243151576Sdavidxu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 1244151576Sdavidxu timespecsub(&ovalue->it_value, &ts); 1245151576Sdavidxu if (ovalue->it_value.tv_sec < 0 || 1246151576Sdavidxu (ovalue->it_value.tv_sec == 0 && 1247151576Sdavidxu ovalue->it_value.tv_nsec == 0)) { 1248151576Sdavidxu ovalue->it_value.tv_sec = 0; 1249151576Sdavidxu ovalue->it_value.tv_nsec = 1; 1250151576Sdavidxu } 1251151576Sdavidxu } 1252151576Sdavidxu return (0); 1253151576Sdavidxu} 1254151576Sdavidxu 1255151576Sdavidxustatic int 1256151576Sdavidxurealtimer_settime(struct itimer *it, int flags, 1257151576Sdavidxu struct itimerspec *value, struct itimerspec *ovalue) 1258151576Sdavidxu{ 1259151576Sdavidxu struct timeval tv, tv2; 1260151576Sdavidxu struct itimerval val; 1261151576Sdavidxu 1262151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1263151576Sdavidxu 1264151576Sdavidxu TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value); 1265151576Sdavidxu TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval); 1266151576Sdavidxu 1267151576Sdavidxu if (itimerfix(&val.it_value)) 1268151576Sdavidxu return (EINVAL); 1269151576Sdavidxu 1270151576Sdavidxu if (timevalisset(&val.it_value)) { 1271151576Sdavidxu if (itimerfix(&val.it_interval)) 1272151576Sdavidxu return (EINVAL); 1273151576Sdavidxu } else { 1274151576Sdavidxu timevalclear(&val.it_interval); 1275151576Sdavidxu } 1276151576Sdavidxu 1277151576Sdavidxu if (ovalue != NULL) 1278151576Sdavidxu realtimer_gettime(it, ovalue); 1279151576Sdavidxu 1280151576Sdavidxu it->it_time = val; 1281151576Sdavidxu if (timevalisset(&val.it_value)) { 1282151576Sdavidxu realtimer_clocktime(it->it_clockid, &tv); 1283151576Sdavidxu tv2 = val.it_value; 1284151576Sdavidxu if ((flags & TIMER_ABSTIME) == 0) { 1285151576Sdavidxu /* Convert to absolute time. */ 1286151576Sdavidxu timevaladd(&it->it_time.it_value, &tv); 1287151576Sdavidxu } else { 1288151576Sdavidxu timevalsub(&tv2, &tv); 1289151576Sdavidxu /* 1290151576Sdavidxu * We don't care if tv2 is negative, tztohz will 1291151576Sdavidxu * fix it. 1292151576Sdavidxu */ 1293151576Sdavidxu } 1294151576Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv2), 1295151576Sdavidxu realtimer_expire, it); 1296151576Sdavidxu } else { 1297151576Sdavidxu callout_stop(&it->it_callout); 1298151576Sdavidxu } 1299151576Sdavidxu 1300151576Sdavidxu return (0); 1301151576Sdavidxu} 1302151576Sdavidxu 1303151576Sdavidxustatic void 1304151576Sdavidxurealtimer_clocktime(clockid_t id, struct timeval *tv) 1305151576Sdavidxu{ 1306151576Sdavidxu if (id == CLOCK_REALTIME) 1307151576Sdavidxu getmicrotime(tv); 1308151576Sdavidxu else /* CLOCK_MONOTONIC */ 1309151576Sdavidxu getmicrouptime(tv); 1310151576Sdavidxu} 1311151576Sdavidxu 1312151576Sdavidxustatic void 1313151576Sdavidxurealtimer_event_hook(struct proc *p, clockid_t clock_id, int event) 1314151576Sdavidxu{ 1315151576Sdavidxu struct itimers *its; 1316151576Sdavidxu struct itimer *it; 1317151576Sdavidxu int i; 1318151576Sdavidxu 1319151576Sdavidxu /* 1320151576Sdavidxu * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX 1321151576Sdavidxu * specification, it should be inherited by new process image. 1322151576Sdavidxu */ 1323151576Sdavidxu if (event == ITIMER_EV_EXEC) 1324151576Sdavidxu i = 1; 1325151576Sdavidxu else 1326151576Sdavidxu i = 0; 1327151585Sdavidxu its = p->p_itimers; 1328151576Sdavidxu for (; i < TIMER_MAX; i++) { 1329151576Sdavidxu if ((it = its->its_timers[i]) != NULL && 1330151576Sdavidxu it->it_clockid == clock_id) { 1331151576Sdavidxu ITIMER_LOCK(it); 1332151576Sdavidxu callout_stop(&it->it_callout); 1333151576Sdavidxu ITIMER_UNLOCK(it); 1334151576Sdavidxu } 1335151576Sdavidxu } 1336151576Sdavidxu} 1337151576Sdavidxu 1338151576Sdavidxu/* Timeout callback for realtime timer */ 1339151576Sdavidxustatic void 1340151576Sdavidxurealtimer_expire(void *arg) 1341151576Sdavidxu{ 1342151576Sdavidxu struct timeval tv, tv2; 1343151576Sdavidxu struct itimer *it; 1344151576Sdavidxu struct proc *p; 1345151576Sdavidxu 1346151576Sdavidxu it = (struct itimer *)arg; 1347151576Sdavidxu p = it->it_proc; 1348151576Sdavidxu 1349151576Sdavidxu realtimer_clocktime(it->it_clockid, &tv); 1350151576Sdavidxu /* Only fire if time is reached. */ 1351151576Sdavidxu if (timevalcmp(&it->it_time.it_value, &tv, <=)) { 1352151576Sdavidxu if (timevalisset(&it->it_time.it_interval)) { 1353151576Sdavidxu timevaladd(&it->it_time.it_value, 1354151576Sdavidxu &it->it_time.it_interval); 1355151576Sdavidxu while (timevalcmp(&it->it_time.it_value, &tv, <=)) { 1356151576Sdavidxu it->it_overrun++; 1357151576Sdavidxu timevaladd(&it->it_time.it_value, 1358151576Sdavidxu &it->it_time.it_interval); 1359151576Sdavidxu } 1360151576Sdavidxu } else { 1361151576Sdavidxu /* single shot timer ? */ 1362151576Sdavidxu timevalclear(&it->it_time.it_value); 1363151576Sdavidxu } 1364151576Sdavidxu if (timevalisset(&it->it_time.it_value)) { 1365151576Sdavidxu tv2 = it->it_time.it_value; 1366151576Sdavidxu timevalsub(&tv2, &tv); 1367151576Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv2), 1368151576Sdavidxu realtimer_expire, it); 1369151576Sdavidxu } 1370151576Sdavidxu ITIMER_UNLOCK(it); 1371151576Sdavidxu itimer_fire(it); 1372151576Sdavidxu ITIMER_LOCK(it); 1373151576Sdavidxu } else if (timevalisset(&it->it_time.it_value)) { 1374151576Sdavidxu tv2 = it->it_time.it_value; 1375151576Sdavidxu timevalsub(&tv2, &tv); 1376151576Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv2), realtimer_expire, 1377151576Sdavidxu it); 1378151576Sdavidxu } 1379151576Sdavidxu} 1380151576Sdavidxu 1381151576Sdavidxuvoid 1382151576Sdavidxuitimer_fire(struct itimer *it) 1383151576Sdavidxu{ 1384151576Sdavidxu struct proc *p = it->it_proc; 1385151576Sdavidxu 1386151576Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL) { 1387151576Sdavidxu PROC_LOCK(p); 1388151576Sdavidxu ITIMER_LOCK(it); 1389151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) { 1390151576Sdavidxu it->it_overrun++; 1391151576Sdavidxu it->it_ksi.ksi_overrun = it->it_overrun; 1392151576Sdavidxu it->it_overrun_last = it->it_overrun; 1393151576Sdavidxu } else { 1394151576Sdavidxu it->it_ksi.ksi_overrun = it->it_overrun; 1395151576Sdavidxu it->it_overrun_last = it->it_overrun; 1396151576Sdavidxu it->it_overrun = 0; 1397151576Sdavidxu psignal_info(p, &it->it_ksi); 1398151576Sdavidxu } 1399151576Sdavidxu PROC_UNLOCK(p); 1400151576Sdavidxu ITIMER_UNLOCK(it); 1401151576Sdavidxu } 1402151576Sdavidxu} 1403151576Sdavidxu 1404151576Sdavidxustatic void 1405151576Sdavidxuitimers_alloc(struct proc *p) 1406151576Sdavidxu{ 1407151585Sdavidxu struct itimers *its; 1408151585Sdavidxu int i; 1409151576Sdavidxu 1410151585Sdavidxu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 1411151585Sdavidxu LIST_INIT(&its->its_virtual); 1412151585Sdavidxu LIST_INIT(&its->its_prof); 1413151585Sdavidxu TAILQ_INIT(&its->its_worklist); 1414151585Sdavidxu for (i = 0; i < TIMER_MAX; i++) 1415151585Sdavidxu its->its_timers[i] = NULL; 1416151576Sdavidxu PROC_LOCK(p); 1417151585Sdavidxu if (p->p_itimers == NULL) { 1418151585Sdavidxu p->p_itimers = its; 1419151576Sdavidxu PROC_UNLOCK(p); 1420151585Sdavidxu } 1421151585Sdavidxu else { 1422151576Sdavidxu PROC_UNLOCK(p); 1423151585Sdavidxu free(its, M_SUBPROC); 1424151576Sdavidxu } 1425151576Sdavidxu} 1426151576Sdavidxu 1427151576Sdavidxu/* Clean up timers when some process events are being triggered. */ 1428151576Sdavidxuvoid 1429151576Sdavidxuitimers_event_hook(struct proc *p, int event) 1430151576Sdavidxu{ 1431151576Sdavidxu struct itimers *its; 1432151576Sdavidxu struct itimer *it; 1433151576Sdavidxu int i; 1434151576Sdavidxu 1435151585Sdavidxu if (p->p_itimers != NULL) { 1436151585Sdavidxu its = p->p_itimers; 1437151576Sdavidxu for (i = 0; i < MAX_CLOCKS; ++i) { 1438151576Sdavidxu if (posix_clocks[i].event_hook != NULL) 1439151576Sdavidxu CLOCK_CALL(i, event_hook, (p, i, event)); 1440151576Sdavidxu } 1441151576Sdavidxu /* 1442151576Sdavidxu * According to susv3, XSI interval timers should be inherited 1443151576Sdavidxu * by new image. 1444151576Sdavidxu */ 1445151576Sdavidxu if (event == ITIMER_EV_EXEC) 1446151576Sdavidxu i = 3; 1447151576Sdavidxu else if (event == ITIMER_EV_EXIT) 1448151576Sdavidxu i = 0; 1449151576Sdavidxu else 1450151576Sdavidxu panic("unhandled event"); 1451151576Sdavidxu for (; i < TIMER_MAX; ++i) { 1452151576Sdavidxu if ((it = its->its_timers[i]) != NULL) { 1453151576Sdavidxu PROC_LOCK(p); 1454151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) 1455151576Sdavidxu sigqueue_take(&it->it_ksi); 1456151576Sdavidxu PROC_UNLOCK(p); 1457151576Sdavidxu uma_zfree(itimer_zone, its->its_timers[i]); 1458151576Sdavidxu its->its_timers[i] = NULL; 1459151576Sdavidxu } 1460151576Sdavidxu } 1461151576Sdavidxu if (its->its_timers[0] == NULL && 1462151576Sdavidxu its->its_timers[1] == NULL && 1463151576Sdavidxu its->its_timers[2] == NULL) { 1464151585Sdavidxu free(its, M_SUBPROC); 1465151585Sdavidxu p->p_itimers = NULL; 1466151576Sdavidxu } 1467151576Sdavidxu } 1468151576Sdavidxu} 1469