kern_time.c revision 170472
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 170472 2007-06-09 21:48:44Z attilio $"); 34116182Sobrien 351541Srgrimes#include <sys/param.h> 3648274Speter#include <sys/systm.h> 37160910Sdavidxu#include <sys/limits.h> 38162954Sphk#include <sys/clock.h> 3976166Smarkm#include <sys/lock.h> 4076166Smarkm#include <sys/mutex.h> 4112221Sbde#include <sys/sysproto.h> 42153259Sdavidxu#include <sys/eventhandler.h> 431541Srgrimes#include <sys/resourcevar.h> 443308Sphk#include <sys/signalvar.h> 451541Srgrimes#include <sys/kernel.h> 46140483Sps#include <sys/syscallsubr.h> 47152983Sdavidxu#include <sys/sysctl.h> 4825583Speter#include <sys/sysent.h> 49164033Srwatson#include <sys/priv.h> 501541Srgrimes#include <sys/proc.h> 51164184Strhodes#include <sys/posix4.h> 5225656Speter#include <sys/time.h> 53151576Sdavidxu#include <sys/timers.h> 5458377Sphk#include <sys/timetc.h> 551541Srgrimes#include <sys/vnode.h> 5676166Smarkm 5726335Speter#include <vm/vm.h> 5826335Speter#include <vm/vm_extern.h> 591541Srgrimes 60151576Sdavidxu#define MAX_CLOCKS (CLOCK_MONOTONIC+1) 61151576Sdavidxu 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 *); 84164713Sdavidxustatic struct itimer *itimer_find(struct proc *, int); 85151576Sdavidxustatic void itimers_alloc(struct proc *); 86161302Snetchildstatic void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp); 87161302Snetchildstatic void itimers_event_hook_exit(void *arg, struct proc *p); 88151576Sdavidxustatic int realtimer_create(struct itimer *); 89151576Sdavidxustatic int realtimer_gettime(struct itimer *, struct itimerspec *); 90151576Sdavidxustatic int realtimer_settime(struct itimer *, int, 91151576Sdavidxu struct itimerspec *, struct itimerspec *); 92151576Sdavidxustatic int realtimer_delete(struct itimer *); 93151869Sdavidxustatic void realtimer_clocktime(clockid_t, struct timespec *); 94151576Sdavidxustatic void realtimer_expire(void *); 95151576Sdavidxustatic int kern_timer_create(struct thread *, clockid_t, 96156134Sdavidxu struct sigevent *, int *, int); 97156134Sdavidxustatic int kern_timer_delete(struct thread *, int); 98151576Sdavidxu 99151576Sdavidxuint register_posix_clock(int, struct kclock *); 100151576Sdavidxuvoid itimer_fire(struct itimer *it); 101151869Sdavidxuint itimespecfix(struct timespec *ts); 102151576Sdavidxu 103151576Sdavidxu#define CLOCK_CALL(clock, call, arglist) \ 104151576Sdavidxu ((*posix_clocks[clock].call) arglist) 105151576Sdavidxu 106151576SdavidxuSYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 107151576Sdavidxu 108151576Sdavidxu 10930739Sphkstatic void 11030739Sphkno_lease_updatetime(deltat) 11130739Sphk int deltat; 11230739Sphk{ 11330739Sphk} 11430739Sphk 11592723Salfredvoid (*lease_updatetime)(int) = no_lease_updatetime; 11630739Sphk 11725583Speterstatic int 118102074Sphksettime(struct thread *td, struct timeval *tv) 11925583Speter{ 12045433Snsayer struct timeval delta, tv1, tv2; 12145438Snsayer static struct timeval maxtime, laststep; 12233690Sphk struct timespec ts; 12325583Speter int s; 12425583Speter 12525656Speter s = splclock(); 12633818Sbde microtime(&tv1); 12735029Sphk delta = *tv; 12835029Sphk timevalsub(&delta, &tv1); 12925583Speter 13025583Speter /* 13133818Sbde * If the system is secure, we do not allow the time to be 13245433Snsayer * set to a value earlier than 1 second less than the highest 13345433Snsayer * time we have yet seen. The worst a miscreant can do in 13445433Snsayer * this circumstance is "freeze" time. He couldn't go 13545433Snsayer * back to the past. 13645438Snsayer * 13745438Snsayer * We similarly do not allow the clock to be stepped more 13845438Snsayer * than one second, nor more than once per second. This allows 13945438Snsayer * a miscreant to make the clock march double-time, but no worse. 14025583Speter */ 14194343Sjhb if (securelevel_gt(td->td_ucred, 1) != 0) { 14245433Snsayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 14345437Smjacob /* 14445438Snsayer * Update maxtime to latest time we've seen. 14545437Smjacob */ 14645437Smjacob if (tv1.tv_sec > maxtime.tv_sec) 14745437Smjacob maxtime = tv1; 14845437Smjacob tv2 = *tv; 14945437Smjacob timevalsub(&tv2, &maxtime); 15045437Smjacob if (tv2.tv_sec < -1) { 15145437Smjacob tv->tv_sec = maxtime.tv_sec - 1; 15245433Snsayer printf("Time adjustment clamped to -1 second\n"); 15345433Snsayer } 15445437Smjacob } else { 15545438Snsayer if (tv1.tv_sec == laststep.tv_sec) { 15645438Snsayer splx(s); 15745438Snsayer return (EPERM); 15845438Snsayer } 15945438Snsayer if (delta.tv_sec > 1) { 16045438Snsayer tv->tv_sec = tv1.tv_sec + 1; 16145438Snsayer printf("Time adjustment clamped to +1 second\n"); 16245438Snsayer } 16345438Snsayer laststep = *tv; 16445433Snsayer } 16533818Sbde } 16633818Sbde 16733690Sphk ts.tv_sec = tv->tv_sec; 16833690Sphk ts.tv_nsec = tv->tv_usec * 1000; 16994343Sjhb mtx_lock(&Giant); 17058377Sphk tc_setclock(&ts); 17125583Speter (void) splsoftclock(); 17225583Speter lease_updatetime(delta.tv_sec); 17325583Speter splx(s); 17425583Speter resettodr(); 17594343Sjhb mtx_unlock(&Giant); 17625583Speter return (0); 17725583Speter} 17825583Speter 17912221Sbde#ifndef _SYS_SYSPROTO_H_ 18025583Speterstruct clock_gettime_args { 18125583Speter clockid_t clock_id; 18225583Speter struct timespec *tp; 18325583Speter}; 18425583Speter#endif 18525583Speter/* ARGSUSED */ 18625583Speterint 187102074Sphkclock_gettime(struct thread *td, struct clock_gettime_args *uap) 18825583Speter{ 18925583Speter struct timespec ats; 190151357Sps int error; 191151357Sps 192151357Sps error = kern_clock_gettime(td, uap->clock_id, &ats); 193151357Sps if (error == 0) 194151357Sps error = copyout(&ats, uap->tp, sizeof(ats)); 195151357Sps 196151357Sps return (error); 197151357Sps} 198151357Sps 199151357Spsint 200151357Spskern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 201151357Sps{ 202130884Skbyanc struct timeval sys, user; 203136152Sjhb struct proc *p; 20425583Speter 205136152Sjhb p = td->td_proc; 206151357Sps switch (clock_id) { 207152844Srwatson case CLOCK_REALTIME: /* Default to precise. */ 208152844Srwatson case CLOCK_REALTIME_PRECISE: 209151357Sps nanotime(ats); 210130654Skbyanc break; 211152844Srwatson case CLOCK_REALTIME_FAST: 212152844Srwatson getnanotime(ats); 213152844Srwatson break; 214130654Skbyanc case CLOCK_VIRTUAL: 215136152Sjhb PROC_LOCK(p); 216170472Sattilio PROC_SLOCK(p); 217136152Sjhb calcru(p, &user, &sys); 218170472Sattilio PROC_SUNLOCK(p); 219136152Sjhb PROC_UNLOCK(p); 220151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 221130654Skbyanc break; 222130654Skbyanc case CLOCK_PROF: 223136152Sjhb PROC_LOCK(p); 224170472Sattilio PROC_SLOCK(p); 225136152Sjhb calcru(p, &user, &sys); 226170472Sattilio PROC_SUNLOCK(p); 227136152Sjhb PROC_UNLOCK(p); 228130884Skbyanc timevaladd(&user, &sys); 229151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 230130654Skbyanc break; 231152844Srwatson case CLOCK_MONOTONIC: /* Default to precise. */ 232152844Srwatson case CLOCK_MONOTONIC_PRECISE: 233152585Sandre case CLOCK_UPTIME: 234152844Srwatson case CLOCK_UPTIME_PRECISE: 235151357Sps nanouptime(ats); 236130884Skbyanc break; 237152844Srwatson case CLOCK_UPTIME_FAST: 238152844Srwatson case CLOCK_MONOTONIC_FAST: 239152844Srwatson getnanouptime(ats); 240152844Srwatson break; 241152844Srwatson case CLOCK_SECOND: 242152844Srwatson ats->tv_sec = time_second; 243152844Srwatson ats->tv_nsec = 0; 244152844Srwatson break; 245130654Skbyanc default: 246111315Sphk return (EINVAL); 247130654Skbyanc } 248151357Sps return (0); 24925583Speter} 25025583Speter 25125583Speter#ifndef _SYS_SYSPROTO_H_ 25225583Speterstruct clock_settime_args { 25325583Speter clockid_t clock_id; 25425583Speter const struct timespec *tp; 25525583Speter}; 25625583Speter#endif 25725583Speter/* ARGSUSED */ 25825583Speterint 259102074Sphkclock_settime(struct thread *td, struct clock_settime_args *uap) 26025583Speter{ 26125583Speter struct timespec ats; 26225583Speter int error; 26325583Speter 264151357Sps if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 265151357Sps return (error); 266151357Sps return (kern_clock_settime(td, uap->clock_id, &ats)); 267151357Sps} 268151357Sps 269151357Spsint 270151357Spskern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 271151357Sps{ 272151357Sps struct timeval atv; 273151357Sps int error; 274151357Sps 275164033Srwatson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 27694343Sjhb return (error); 277151357Sps if (clock_id != CLOCK_REALTIME) 27894343Sjhb return (EINVAL); 279151357Sps if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 28094343Sjhb return (EINVAL); 28134901Sphk /* XXX Don't convert nsec->usec and back */ 282151357Sps TIMESPEC_TO_TIMEVAL(&atv, ats); 28394343Sjhb error = settime(td, &atv); 28482746Sdillon return (error); 28525583Speter} 28625583Speter 28725583Speter#ifndef _SYS_SYSPROTO_H_ 28825583Speterstruct clock_getres_args { 28925583Speter clockid_t clock_id; 29025583Speter struct timespec *tp; 29125583Speter}; 29225583Speter#endif 29325583Speterint 294102074Sphkclock_getres(struct thread *td, struct clock_getres_args *uap) 29525583Speter{ 29625583Speter struct timespec ts; 297151357Sps int error; 29825583Speter 299151357Sps if (uap->tp == NULL) 300151357Sps return (0); 301151357Sps 302151357Sps error = kern_clock_getres(td, uap->clock_id, &ts); 303151357Sps if (error == 0) 304151357Sps error = copyout(&ts, uap->tp, sizeof(ts)); 305151357Sps return (error); 306151357Sps} 307151357Sps 308151357Spsint 309151357Spskern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 310151357Sps{ 311151357Sps 312151357Sps ts->tv_sec = 0; 313151357Sps switch (clock_id) { 314130654Skbyanc case CLOCK_REALTIME: 315152844Srwatson case CLOCK_REALTIME_FAST: 316152844Srwatson case CLOCK_REALTIME_PRECISE: 317130654Skbyanc case CLOCK_MONOTONIC: 318152844Srwatson case CLOCK_MONOTONIC_FAST: 319152844Srwatson case CLOCK_MONOTONIC_PRECISE: 320152585Sandre case CLOCK_UPTIME: 321152844Srwatson case CLOCK_UPTIME_FAST: 322152844Srwatson case CLOCK_UPTIME_PRECISE: 323103964Sbde /* 324103964Sbde * Round up the result of the division cheaply by adding 1. 325103964Sbde * Rounding up is especially important if rounding down 326103964Sbde * would give 0. Perfect rounding is unimportant. 327103964Sbde */ 328151357Sps ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 329130654Skbyanc break; 330130654Skbyanc case CLOCK_VIRTUAL: 331130654Skbyanc case CLOCK_PROF: 332130654Skbyanc /* Accurately round up here because we can do so cheaply. */ 333151357Sps ts->tv_nsec = (1000000000 + hz - 1) / hz; 334130654Skbyanc break; 335152844Srwatson case CLOCK_SECOND: 336152844Srwatson ts->tv_sec = 1; 337152844Srwatson ts->tv_nsec = 0; 338152844Srwatson break; 339130654Skbyanc default: 340130654Skbyanc return (EINVAL); 341130654Skbyanc } 342151357Sps return (0); 34325583Speter} 34425583Speter 34526335Speterstatic int nanowait; 34625656Speter 347140481Spsint 348140481Spskern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 34925583Speter{ 35035045Sphk struct timespec ts, ts2, ts3; 35135042Sphk struct timeval tv; 35235042Sphk int error; 35325583Speter 35428773Sbde if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 35525656Speter return (EINVAL); 35643301Sdillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 35728773Sbde return (0); 35836119Sphk getnanouptime(&ts); 35935029Sphk timespecadd(&ts, rqt); 36035042Sphk TIMESPEC_TO_TIMEVAL(&tv, rqt); 36135042Sphk for (;;) { 36235042Sphk error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 36335042Sphk tvtohz(&tv)); 36436119Sphk getnanouptime(&ts2); 36535042Sphk if (error != EWOULDBLOCK) { 36635042Sphk if (error == ERESTART) 36735042Sphk error = EINTR; 36835042Sphk if (rmt != NULL) { 36935042Sphk timespecsub(&ts, &ts2); 37035042Sphk if (ts.tv_sec < 0) 37135042Sphk timespecclear(&ts); 37235042Sphk *rmt = ts; 37335042Sphk } 37435042Sphk return (error); 37535042Sphk } 37635029Sphk if (timespeccmp(&ts2, &ts, >=)) 37735042Sphk return (0); 37835045Sphk ts3 = ts; 37935045Sphk timespecsub(&ts3, &ts2); 38035045Sphk TIMESPEC_TO_TIMEVAL(&tv, &ts3); 38126335Speter } 38226335Speter} 38325583Speter 38426335Speter#ifndef _SYS_SYSPROTO_H_ 38526335Speterstruct nanosleep_args { 38626335Speter struct timespec *rqtp; 38726335Speter struct timespec *rmtp; 38826335Speter}; 38926335Speter#endif 39026335Speter/* ARGSUSED */ 39126335Speterint 392102074Sphknanosleep(struct thread *td, struct nanosleep_args *uap) 39326335Speter{ 39426335Speter struct timespec rmt, rqt; 39582746Sdillon int error; 39626335Speter 397107849Salfred error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 39826335Speter if (error) 39926335Speter return (error); 40082746Sdillon 401109521Salfred if (uap->rmtp && 402109521Salfred !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 403109521Salfred return (EFAULT); 404140481Sps error = kern_nanosleep(td, &rqt, &rmt); 405107849Salfred if (error && uap->rmtp) { 40682746Sdillon int error2; 40782746Sdillon 408107849Salfred error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 409109521Salfred if (error2) 41082746Sdillon error = error2; 41125583Speter } 41225656Speter return (error); 41325583Speter} 41425583Speter 41526335Speter#ifndef _SYS_SYSPROTO_H_ 4161541Srgrimesstruct gettimeofday_args { 4171541Srgrimes struct timeval *tp; 4181541Srgrimes struct timezone *tzp; 4191541Srgrimes}; 42012221Sbde#endif 4211541Srgrimes/* ARGSUSED */ 4221549Srgrimesint 423102074Sphkgettimeofday(struct thread *td, struct gettimeofday_args *uap) 4241541Srgrimes{ 4251541Srgrimes struct timeval atv; 426110286Stjr struct timezone rtz; 4271541Srgrimes int error = 0; 4281541Srgrimes 4291541Srgrimes if (uap->tp) { 4301541Srgrimes microtime(&atv); 43199012Salfred error = copyout(&atv, uap->tp, sizeof (atv)); 4321541Srgrimes } 43390836Sphk if (error == 0 && uap->tzp != NULL) { 434110299Sphk rtz.tz_minuteswest = tz_minuteswest; 435110299Sphk rtz.tz_dsttime = tz_dsttime; 436110286Stjr error = copyout(&rtz, uap->tzp, sizeof (rtz)); 43782746Sdillon } 4381541Srgrimes return (error); 4391541Srgrimes} 4401541Srgrimes 44112221Sbde#ifndef _SYS_SYSPROTO_H_ 4421541Srgrimesstruct settimeofday_args { 4431541Srgrimes struct timeval *tv; 4441541Srgrimes struct timezone *tzp; 4451541Srgrimes}; 44612221Sbde#endif 4471541Srgrimes/* ARGSUSED */ 4481549Srgrimesint 449102074Sphksettimeofday(struct thread *td, struct settimeofday_args *uap) 4501541Srgrimes{ 451144445Sjhb struct timeval atv, *tvp; 452144445Sjhb struct timezone atz, *tzp; 453144445Sjhb int error; 4541541Srgrimes 455144445Sjhb if (uap->tv) { 456144445Sjhb error = copyin(uap->tv, &atv, sizeof(atv)); 457144445Sjhb if (error) 458144445Sjhb return (error); 459144445Sjhb tvp = &atv; 460144445Sjhb } else 461144445Sjhb tvp = NULL; 462144445Sjhb if (uap->tzp) { 463144445Sjhb error = copyin(uap->tzp, &atz, sizeof(atz)); 464144445Sjhb if (error) 465144445Sjhb return (error); 466144445Sjhb tzp = &atz; 467144445Sjhb } else 468144445Sjhb tzp = NULL; 469144445Sjhb return (kern_settimeofday(td, tvp, tzp)); 470144445Sjhb} 471144445Sjhb 472144445Sjhbint 473144445Sjhbkern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 474144445Sjhb{ 475144445Sjhb int error; 476144445Sjhb 477164033Srwatson error = priv_check(td, PRIV_SETTIMEOFDAY); 478144445Sjhb if (error) 47994343Sjhb return (error); 4801541Srgrimes /* Verify all parameters before changing time. */ 481144445Sjhb if (tv) { 482144445Sjhb if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 48394343Sjhb return (EINVAL); 484144445Sjhb error = settime(td, tv); 48525656Speter } 486144445Sjhb if (tzp && error == 0) { 487144445Sjhb tz_minuteswest = tzp->tz_minuteswest; 488144445Sjhb tz_dsttime = tzp->tz_dsttime; 48982746Sdillon } 49082746Sdillon return (error); 4911541Srgrimes} 492144445Sjhb 49382746Sdillon/* 494167232Srwatson * Get value of an interval timer. The process virtual and profiling virtual 495167232Srwatson * time timers are kept in the p_stats area, since they can be swapped out. 496167232Srwatson * These are kept internally in the way they are specified externally: in 497167232Srwatson * time until they expire. 4981541Srgrimes * 499167232Srwatson * The real time interval timer is kept in the process table slot for the 500167232Srwatson * process, and its value (it_value) is kept as an absolute time rather than 501167232Srwatson * as a delta, so that it is easy to keep periodic real-time signals from 502167232Srwatson * drifting. 5031541Srgrimes * 5041541Srgrimes * Virtual time timers are processed in the hardclock() routine of 505167232Srwatson * kern_clock.c. The real time timer is processed by a timeout routine, 506167232Srwatson * called from the softclock() routine. Since a callout may be delayed in 507167232Srwatson * real time due to interrupt processing in the system, it is possible for 508167232Srwatson * the real time timeout routine (realitexpire, given below), to be delayed 509167232Srwatson * in real time past when it is supposed to occur. It does not suffice, 510167232Srwatson * therefore, to reload the real timer .it_value from the real time timers 511167232Srwatson * .it_interval. Rather, we compute the next time in absolute time the timer 512167232Srwatson * should go off. 5131541Srgrimes */ 51412221Sbde#ifndef _SYS_SYSPROTO_H_ 5151541Srgrimesstruct getitimer_args { 5161541Srgrimes u_int which; 5171541Srgrimes struct itimerval *itv; 5181541Srgrimes}; 51912221Sbde#endif 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 { 559170307Sjeff PROC_SLOCK(p); 560140832Ssobomax *aitv = p->p_stats->p_timer[which]; 561170307Sjeff PROC_SUNLOCK(p); 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 5721549Srgrimesint 573102074Sphksetitimer(struct thread *td, struct setitimer_args *uap) 5741541Srgrimes{ 575141470Sjhb struct itimerval aitv, oitv; 576140832Ssobomax int error; 5771541Srgrimes 578111034Stjr if (uap->itv == NULL) { 579111034Stjr uap->itv = uap->oitv; 580111034Stjr return (getitimer(td, (struct getitimer_args *)uap)); 581111034Stjr } 582111034Stjr 583111034Stjr if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 5841541Srgrimes return (error); 585140832Ssobomax error = kern_setitimer(td, uap->which, &aitv, &oitv); 586140832Ssobomax if (error != 0 || uap->oitv == NULL) 587140832Ssobomax return (error); 588140832Ssobomax return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 589140832Ssobomax} 590140832Ssobomax 591140832Ssobomaxint 592141470Sjhbkern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 593141470Sjhb struct itimerval *oitv) 594140832Ssobomax{ 595140832Ssobomax struct proc *p = td->td_proc; 596140832Ssobomax struct timeval ctv; 597140832Ssobomax 598141483Sjhb if (aitv == NULL) 599141483Sjhb return (kern_getitimer(td, which, oitv)); 600141483Sjhb 601140832Ssobomax if (which > ITIMER_PROF) 602111034Stjr return (EINVAL); 603140832Ssobomax if (itimerfix(&aitv->it_value)) 604111034Stjr return (EINVAL); 605140832Ssobomax if (!timevalisset(&aitv->it_value)) 606140832Ssobomax timevalclear(&aitv->it_interval); 607140832Ssobomax else if (itimerfix(&aitv->it_interval)) 608140832Ssobomax return (EINVAL); 60982746Sdillon 610140832Ssobomax if (which == ITIMER_REAL) { 611111034Stjr PROC_LOCK(p); 61235058Sphk if (timevalisset(&p->p_realtimer.it_value)) 61369286Sjake callout_stop(&p->p_itcallout); 614114980Sjhb getmicrouptime(&ctv); 615140832Ssobomax if (timevalisset(&aitv->it_value)) { 616140832Ssobomax callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 61769286Sjake realitexpire, p); 618140832Ssobomax timevaladd(&aitv->it_value, &ctv); 619114980Sjhb } 620140832Ssobomax *oitv = p->p_realtimer; 621140832Ssobomax p->p_realtimer = *aitv; 622111034Stjr PROC_UNLOCK(p); 623140832Ssobomax if (timevalisset(&oitv->it_value)) { 624140832Ssobomax if (timevalcmp(&oitv->it_value, &ctv, <)) 625140832Ssobomax timevalclear(&oitv->it_value); 626111034Stjr else 627140832Ssobomax timevalsub(&oitv->it_value, &ctv); 628111034Stjr } 62982746Sdillon } else { 630170307Sjeff PROC_SLOCK(p); 631140832Ssobomax *oitv = p->p_stats->p_timer[which]; 632140832Ssobomax p->p_stats->p_timer[which] = *aitv; 633170307Sjeff PROC_SUNLOCK(p); 63482746Sdillon } 635140832Ssobomax return (0); 6361541Srgrimes} 6371541Srgrimes 6381541Srgrimes/* 6391541Srgrimes * Real interval timer expired: 6401541Srgrimes * send process whose timer expired an alarm signal. 6411541Srgrimes * If time is not set up to reload, then just return. 6421541Srgrimes * Else compute next time timer should go off which is > current time. 6431541Srgrimes * This is where delay in processing this timeout causes multiple 6441541Srgrimes * SIGALRM calls to be compressed into one. 64536127Sbde * tvtohz() always adds 1 to allow for the time until the next clock 6469327Sbde * interrupt being strictly less than 1 clock tick, but we don't want 6479327Sbde * that here since we want to appear to be in sync with the clock 6489327Sbde * interrupt even when we're delayed. 6491541Srgrimes */ 6501541Srgrimesvoid 651102074Sphkrealitexpire(void *arg) 6521541Srgrimes{ 653102074Sphk struct proc *p; 65435044Sphk struct timeval ctv, ntv; 6551541Srgrimes 6561541Srgrimes p = (struct proc *)arg; 65773916Sjhb PROC_LOCK(p); 6581541Srgrimes psignal(p, SIGALRM); 65935058Sphk if (!timevalisset(&p->p_realtimer.it_interval)) { 66035058Sphk timevalclear(&p->p_realtimer.it_value); 661116123Sjhb if (p->p_flag & P_WEXIT) 662116123Sjhb wakeup(&p->p_itcallout); 66373916Sjhb PROC_UNLOCK(p); 6641541Srgrimes return; 6651541Srgrimes } 6661541Srgrimes for (;;) { 6671541Srgrimes timevaladd(&p->p_realtimer.it_value, 6681541Srgrimes &p->p_realtimer.it_interval); 66936119Sphk getmicrouptime(&ctv); 67035058Sphk if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 67135044Sphk ntv = p->p_realtimer.it_value; 67235044Sphk timevalsub(&ntv, &ctv); 67369286Sjake callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 67469286Sjake realitexpire, p); 67573916Sjhb PROC_UNLOCK(p); 6761541Srgrimes return; 6771541Srgrimes } 6781541Srgrimes } 67973916Sjhb /*NOTREACHED*/ 6801541Srgrimes} 6811541Srgrimes 6821541Srgrimes/* 6831541Srgrimes * Check that a proposed value to load into the .it_value or 6841541Srgrimes * .it_interval part of an interval timer is acceptable, and 6851541Srgrimes * fix it to have at least minimal value (i.e. if it is less 6861541Srgrimes * than the resolution of the clock, round it up.) 6871541Srgrimes */ 6881549Srgrimesint 689102074Sphkitimerfix(struct timeval *tv) 6901541Srgrimes{ 6911541Srgrimes 692151576Sdavidxu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 6931541Srgrimes return (EINVAL); 6941541Srgrimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 6951541Srgrimes tv->tv_usec = tick; 6961541Srgrimes return (0); 6971541Srgrimes} 6981541Srgrimes 6991541Srgrimes/* 7001541Srgrimes * Decrement an interval timer by a specified number 7011541Srgrimes * of microseconds, which must be less than a second, 7021541Srgrimes * i.e. < 1000000. If the timer expires, then reload 7031541Srgrimes * it. In this case, carry over (usec - old value) to 7041541Srgrimes * reduce the value reloaded into the timer so that 7051541Srgrimes * the timer does not drift. This routine assumes 7061541Srgrimes * that it is called in a context where the timers 7071541Srgrimes * on which it is operating cannot change in value. 7081541Srgrimes */ 7091549Srgrimesint 710102074Sphkitimerdecr(struct itimerval *itp, int usec) 7111541Srgrimes{ 7121541Srgrimes 7131541Srgrimes if (itp->it_value.tv_usec < usec) { 7141541Srgrimes if (itp->it_value.tv_sec == 0) { 7151541Srgrimes /* expired, and already in next interval */ 7161541Srgrimes usec -= itp->it_value.tv_usec; 7171541Srgrimes goto expire; 7181541Srgrimes } 7191541Srgrimes itp->it_value.tv_usec += 1000000; 7201541Srgrimes itp->it_value.tv_sec--; 7211541Srgrimes } 7221541Srgrimes itp->it_value.tv_usec -= usec; 7231541Srgrimes usec = 0; 72435058Sphk if (timevalisset(&itp->it_value)) 7251541Srgrimes return (1); 7261541Srgrimes /* expired, exactly at end of interval */ 7271541Srgrimesexpire: 72835058Sphk if (timevalisset(&itp->it_interval)) { 7291541Srgrimes itp->it_value = itp->it_interval; 7301541Srgrimes itp->it_value.tv_usec -= usec; 7311541Srgrimes if (itp->it_value.tv_usec < 0) { 7321541Srgrimes itp->it_value.tv_usec += 1000000; 7331541Srgrimes itp->it_value.tv_sec--; 7341541Srgrimes } 7351541Srgrimes } else 7361541Srgrimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 7371541Srgrimes return (0); 7381541Srgrimes} 7391541Srgrimes 7401541Srgrimes/* 7411541Srgrimes * Add and subtract routines for timevals. 7421541Srgrimes * N.B.: subtract routine doesn't deal with 7431541Srgrimes * results which are before the beginning, 7441541Srgrimes * it just gets very confused in this case. 7451541Srgrimes * Caveat emptor. 7461541Srgrimes */ 7471549Srgrimesvoid 748121523Salfredtimevaladd(struct timeval *t1, const struct timeval *t2) 7491541Srgrimes{ 7501541Srgrimes 7511541Srgrimes t1->tv_sec += t2->tv_sec; 7521541Srgrimes t1->tv_usec += t2->tv_usec; 7531541Srgrimes timevalfix(t1); 7541541Srgrimes} 7551541Srgrimes 7561549Srgrimesvoid 757121523Salfredtimevalsub(struct timeval *t1, const struct timeval *t2) 7581541Srgrimes{ 7591541Srgrimes 7601541Srgrimes t1->tv_sec -= t2->tv_sec; 7611541Srgrimes t1->tv_usec -= t2->tv_usec; 7621541Srgrimes timevalfix(t1); 7631541Srgrimes} 7641541Srgrimes 76512819Sphkstatic void 766102074Sphktimevalfix(struct timeval *t1) 7671541Srgrimes{ 7681541Srgrimes 7691541Srgrimes if (t1->tv_usec < 0) { 7701541Srgrimes t1->tv_sec--; 7711541Srgrimes t1->tv_usec += 1000000; 7721541Srgrimes } 7731541Srgrimes if (t1->tv_usec >= 1000000) { 7741541Srgrimes t1->tv_sec++; 7751541Srgrimes t1->tv_usec -= 1000000; 7761541Srgrimes } 7771541Srgrimes} 778108142Ssam 779108142Ssam/* 780108511Ssam * ratecheck(): simple time-based rate-limit checking. 781108142Ssam */ 782108142Ssamint 783108142Ssamratecheck(struct timeval *lasttime, const struct timeval *mininterval) 784108142Ssam{ 785108142Ssam struct timeval tv, delta; 786108142Ssam int rv = 0; 787108142Ssam 788108511Ssam getmicrouptime(&tv); /* NB: 10ms precision */ 789108511Ssam delta = tv; 790108511Ssam timevalsub(&delta, lasttime); 791108142Ssam 792108142Ssam /* 793108142Ssam * check for 0,0 is so that the message will be seen at least once, 794108142Ssam * even if interval is huge. 795108142Ssam */ 796108142Ssam if (timevalcmp(&delta, mininterval, >=) || 797108142Ssam (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 798108142Ssam *lasttime = tv; 799108142Ssam rv = 1; 800108142Ssam } 801108142Ssam 802108142Ssam return (rv); 803108142Ssam} 804108142Ssam 805108142Ssam/* 806108142Ssam * ppsratecheck(): packets (or events) per second limitation. 807108511Ssam * 808108511Ssam * Return 0 if the limit is to be enforced (e.g. the caller 809108511Ssam * should drop a packet because of the rate limitation). 810108511Ssam * 811111558Ssam * maxpps of 0 always causes zero to be returned. maxpps of -1 812111558Ssam * always causes 1 to be returned; this effectively defeats rate 813111558Ssam * limiting. 814111558Ssam * 815108511Ssam * Note that we maintain the struct timeval for compatibility 816108511Ssam * with other bsd systems. We reuse the storage and just monitor 817108511Ssam * clock ticks for minimal overhead. 818108142Ssam */ 819108142Ssamint 820108142Ssamppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 821108142Ssam{ 822108511Ssam int now; 823108142Ssam 824108142Ssam /* 825108511Ssam * Reset the last time and counter if this is the first call 826108511Ssam * or more than a second has passed since the last update of 827108511Ssam * lasttime. 828108142Ssam */ 829108511Ssam now = ticks; 830108511Ssam if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 831108511Ssam lasttime->tv_sec = now; 832108511Ssam *curpps = 1; 833111558Ssam return (maxpps != 0); 834108511Ssam } else { 835108511Ssam (*curpps)++; /* NB: ignore potential overflow */ 836108511Ssam return (maxpps < 0 || *curpps < maxpps); 837108511Ssam } 838108142Ssam} 839151576Sdavidxu 840151576Sdavidxustatic void 841151576Sdavidxuitimer_start(void) 842151576Sdavidxu{ 843151576Sdavidxu struct kclock rt_clock = { 844151576Sdavidxu .timer_create = realtimer_create, 845151576Sdavidxu .timer_delete = realtimer_delete, 846151576Sdavidxu .timer_settime = realtimer_settime, 847151576Sdavidxu .timer_gettime = realtimer_gettime, 848164713Sdavidxu .event_hook = NULL 849151576Sdavidxu }; 850151576Sdavidxu 851151576Sdavidxu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 852151576Sdavidxu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 853151576Sdavidxu register_posix_clock(CLOCK_REALTIME, &rt_clock); 854151576Sdavidxu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 855152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 856152983Sdavidxu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 857152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 858161302Snetchild EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 859153259Sdavidxu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 860161302Snetchild EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 861153259Sdavidxu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 862151576Sdavidxu} 863151576Sdavidxu 864151576Sdavidxuint 865151576Sdavidxuregister_posix_clock(int clockid, struct kclock *clk) 866151576Sdavidxu{ 867151576Sdavidxu if ((unsigned)clockid >= MAX_CLOCKS) { 868151576Sdavidxu printf("%s: invalid clockid\n", __func__); 869151576Sdavidxu return (0); 870151576Sdavidxu } 871151576Sdavidxu posix_clocks[clockid] = *clk; 872151576Sdavidxu return (1); 873151576Sdavidxu} 874151576Sdavidxu 875151576Sdavidxustatic int 876151576Sdavidxuitimer_init(void *mem, int size, int flags) 877151576Sdavidxu{ 878151576Sdavidxu struct itimer *it; 879151576Sdavidxu 880151576Sdavidxu it = (struct itimer *)mem; 881151576Sdavidxu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 882151576Sdavidxu return (0); 883151576Sdavidxu} 884151576Sdavidxu 885151576Sdavidxustatic void 886151576Sdavidxuitimer_fini(void *mem, int size) 887151576Sdavidxu{ 888151576Sdavidxu struct itimer *it; 889151576Sdavidxu 890151576Sdavidxu it = (struct itimer *)mem; 891151576Sdavidxu mtx_destroy(&it->it_mtx); 892151576Sdavidxu} 893151576Sdavidxu 894151576Sdavidxustatic void 895151576Sdavidxuitimer_enter(struct itimer *it) 896151576Sdavidxu{ 897151576Sdavidxu 898151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 899151576Sdavidxu it->it_usecount++; 900151576Sdavidxu} 901151576Sdavidxu 902151576Sdavidxustatic void 903151576Sdavidxuitimer_leave(struct itimer *it) 904151576Sdavidxu{ 905151576Sdavidxu 906151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 907151576Sdavidxu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 908151576Sdavidxu 909151576Sdavidxu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 910151576Sdavidxu wakeup(it); 911151576Sdavidxu} 912151576Sdavidxu 913151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 914156134Sdavidxustruct ktimer_create_args { 915151576Sdavidxu clockid_t clock_id; 916151576Sdavidxu struct sigevent * evp; 917156134Sdavidxu int * timerid; 918151576Sdavidxu}; 919151576Sdavidxu#endif 920151576Sdavidxuint 921156134Sdavidxuktimer_create(struct thread *td, struct ktimer_create_args *uap) 922151576Sdavidxu{ 923151576Sdavidxu struct sigevent *evp1, ev; 924156134Sdavidxu int id; 925151576Sdavidxu int error; 926151576Sdavidxu 927151576Sdavidxu if (uap->evp != NULL) { 928151576Sdavidxu error = copyin(uap->evp, &ev, sizeof(ev)); 929151576Sdavidxu if (error != 0) 930151576Sdavidxu return (error); 931151576Sdavidxu evp1 = &ev; 932151576Sdavidxu } else 933151576Sdavidxu evp1 = NULL; 934151576Sdavidxu 935151576Sdavidxu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 936151576Sdavidxu 937151576Sdavidxu if (error == 0) { 938156134Sdavidxu error = copyout(&id, uap->timerid, sizeof(int)); 939151576Sdavidxu if (error != 0) 940151576Sdavidxu kern_timer_delete(td, id); 941151576Sdavidxu } 942151576Sdavidxu return (error); 943151576Sdavidxu} 944151576Sdavidxu 945151576Sdavidxustatic int 946151576Sdavidxukern_timer_create(struct thread *td, clockid_t clock_id, 947156134Sdavidxu struct sigevent *evp, int *timerid, int preset_id) 948151576Sdavidxu{ 949151576Sdavidxu struct proc *p = td->td_proc; 950151576Sdavidxu struct itimer *it; 951151576Sdavidxu int id; 952151576Sdavidxu int error; 953151576Sdavidxu 954151576Sdavidxu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 955151576Sdavidxu return (EINVAL); 956151576Sdavidxu 957151576Sdavidxu if (posix_clocks[clock_id].timer_create == NULL) 958151576Sdavidxu return (EINVAL); 959151576Sdavidxu 960151576Sdavidxu if (evp != NULL) { 961151576Sdavidxu if (evp->sigev_notify != SIGEV_NONE && 962151869Sdavidxu evp->sigev_notify != SIGEV_SIGNAL && 963151869Sdavidxu evp->sigev_notify != SIGEV_THREAD_ID) 964151576Sdavidxu return (EINVAL); 965151869Sdavidxu if ((evp->sigev_notify == SIGEV_SIGNAL || 966151869Sdavidxu evp->sigev_notify == SIGEV_THREAD_ID) && 967151576Sdavidxu !_SIG_VALID(evp->sigev_signo)) 968151576Sdavidxu return (EINVAL); 969151576Sdavidxu } 970151576Sdavidxu 971151585Sdavidxu if (p->p_itimers == NULL) 972151576Sdavidxu itimers_alloc(p); 973151576Sdavidxu 974151576Sdavidxu it = uma_zalloc(itimer_zone, M_WAITOK); 975151576Sdavidxu it->it_flags = 0; 976151576Sdavidxu it->it_usecount = 0; 977151576Sdavidxu it->it_active = 0; 978151869Sdavidxu timespecclear(&it->it_time.it_value); 979151869Sdavidxu timespecclear(&it->it_time.it_interval); 980151576Sdavidxu it->it_overrun = 0; 981151576Sdavidxu it->it_overrun_last = 0; 982151576Sdavidxu it->it_clockid = clock_id; 983151576Sdavidxu it->it_timerid = -1; 984151576Sdavidxu it->it_proc = p; 985151576Sdavidxu ksiginfo_init(&it->it_ksi); 986151576Sdavidxu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 987151576Sdavidxu error = CLOCK_CALL(clock_id, timer_create, (it)); 988151576Sdavidxu if (error != 0) 989151576Sdavidxu goto out; 990151576Sdavidxu 991151576Sdavidxu PROC_LOCK(p); 992151576Sdavidxu if (preset_id != -1) { 993151576Sdavidxu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 994151576Sdavidxu id = preset_id; 995151585Sdavidxu if (p->p_itimers->its_timers[id] != NULL) { 996151576Sdavidxu PROC_UNLOCK(p); 997151576Sdavidxu error = 0; 998151576Sdavidxu goto out; 999151576Sdavidxu } 1000151576Sdavidxu } else { 1001151576Sdavidxu /* 1002151576Sdavidxu * Find a free timer slot, skipping those reserved 1003151576Sdavidxu * for setitimer(). 1004151576Sdavidxu */ 1005151576Sdavidxu for (id = 3; id < TIMER_MAX; id++) 1006151585Sdavidxu if (p->p_itimers->its_timers[id] == NULL) 1007151576Sdavidxu break; 1008151576Sdavidxu if (id == TIMER_MAX) { 1009151576Sdavidxu PROC_UNLOCK(p); 1010151576Sdavidxu error = EAGAIN; 1011151576Sdavidxu goto out; 1012151576Sdavidxu } 1013151576Sdavidxu } 1014151576Sdavidxu it->it_timerid = id; 1015151585Sdavidxu p->p_itimers->its_timers[id] = it; 1016151576Sdavidxu if (evp != NULL) 1017151576Sdavidxu it->it_sigev = *evp; 1018151576Sdavidxu else { 1019151576Sdavidxu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 1020151576Sdavidxu switch (clock_id) { 1021151576Sdavidxu default: 1022151576Sdavidxu case CLOCK_REALTIME: 1023151576Sdavidxu it->it_sigev.sigev_signo = SIGALRM; 1024151576Sdavidxu break; 1025151576Sdavidxu case CLOCK_VIRTUAL: 1026151576Sdavidxu it->it_sigev.sigev_signo = SIGVTALRM; 1027151576Sdavidxu break; 1028151576Sdavidxu case CLOCK_PROF: 1029151576Sdavidxu it->it_sigev.sigev_signo = SIGPROF; 1030151576Sdavidxu break; 1031151576Sdavidxu } 1032152029Sdavidxu it->it_sigev.sigev_value.sival_int = id; 1033151576Sdavidxu } 1034151576Sdavidxu 1035151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1036151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1037151576Sdavidxu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 1038151576Sdavidxu it->it_ksi.ksi_code = SI_TIMER; 1039151576Sdavidxu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 1040151576Sdavidxu it->it_ksi.ksi_timerid = id; 1041151576Sdavidxu } 1042151576Sdavidxu PROC_UNLOCK(p); 1043151576Sdavidxu *timerid = id; 1044151576Sdavidxu return (0); 1045151576Sdavidxu 1046151576Sdavidxuout: 1047151576Sdavidxu ITIMER_LOCK(it); 1048151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1049151576Sdavidxu ITIMER_UNLOCK(it); 1050151576Sdavidxu uma_zfree(itimer_zone, it); 1051151576Sdavidxu return (error); 1052151576Sdavidxu} 1053151576Sdavidxu 1054151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1055156134Sdavidxustruct ktimer_delete_args { 1056156134Sdavidxu int timerid; 1057151576Sdavidxu}; 1058151576Sdavidxu#endif 1059151576Sdavidxuint 1060156134Sdavidxuktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 1061151576Sdavidxu{ 1062151576Sdavidxu return (kern_timer_delete(td, uap->timerid)); 1063151576Sdavidxu} 1064151576Sdavidxu 1065151576Sdavidxustatic struct itimer * 1066164713Sdavidxuitimer_find(struct proc *p, int timerid) 1067151576Sdavidxu{ 1068151576Sdavidxu struct itimer *it; 1069151576Sdavidxu 1070151576Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1071151585Sdavidxu if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) || 1072151585Sdavidxu (it = p->p_itimers->its_timers[timerid]) == NULL) { 1073151576Sdavidxu return (NULL); 1074151576Sdavidxu } 1075151576Sdavidxu ITIMER_LOCK(it); 1076164713Sdavidxu if ((it->it_flags & ITF_DELETING) != 0) { 1077151576Sdavidxu ITIMER_UNLOCK(it); 1078151576Sdavidxu it = NULL; 1079151576Sdavidxu } 1080151576Sdavidxu return (it); 1081151576Sdavidxu} 1082151576Sdavidxu 1083151576Sdavidxustatic int 1084156134Sdavidxukern_timer_delete(struct thread *td, int timerid) 1085151576Sdavidxu{ 1086151576Sdavidxu struct proc *p = td->td_proc; 1087151576Sdavidxu struct itimer *it; 1088151576Sdavidxu 1089151576Sdavidxu PROC_LOCK(p); 1090164713Sdavidxu it = itimer_find(p, timerid); 1091151576Sdavidxu if (it == NULL) { 1092151576Sdavidxu PROC_UNLOCK(p); 1093151576Sdavidxu return (EINVAL); 1094151576Sdavidxu } 1095151576Sdavidxu PROC_UNLOCK(p); 1096151576Sdavidxu 1097151576Sdavidxu it->it_flags |= ITF_DELETING; 1098151576Sdavidxu while (it->it_usecount > 0) { 1099151576Sdavidxu it->it_flags |= ITF_WANTED; 1100151576Sdavidxu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 1101151576Sdavidxu } 1102151576Sdavidxu it->it_flags &= ~ITF_WANTED; 1103151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1104151576Sdavidxu ITIMER_UNLOCK(it); 1105151576Sdavidxu 1106151576Sdavidxu PROC_LOCK(p); 1107151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) 1108151576Sdavidxu sigqueue_take(&it->it_ksi); 1109151585Sdavidxu p->p_itimers->its_timers[timerid] = NULL; 1110151576Sdavidxu PROC_UNLOCK(p); 1111151576Sdavidxu uma_zfree(itimer_zone, it); 1112151576Sdavidxu return (0); 1113151576Sdavidxu} 1114151576Sdavidxu 1115151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1116156134Sdavidxustruct ktimer_settime_args { 1117156134Sdavidxu int timerid; 1118151576Sdavidxu int flags; 1119151576Sdavidxu const struct itimerspec * value; 1120151576Sdavidxu struct itimerspec * ovalue; 1121151576Sdavidxu}; 1122151576Sdavidxu#endif 1123151576Sdavidxuint 1124156134Sdavidxuktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 1125151576Sdavidxu{ 1126151576Sdavidxu struct proc *p = td->td_proc; 1127151576Sdavidxu struct itimer *it; 1128151576Sdavidxu struct itimerspec val, oval, *ovalp; 1129151576Sdavidxu int error; 1130151576Sdavidxu 1131151576Sdavidxu error = copyin(uap->value, &val, sizeof(val)); 1132151576Sdavidxu if (error != 0) 1133151576Sdavidxu return (error); 1134151576Sdavidxu 1135151576Sdavidxu if (uap->ovalue != NULL) 1136151576Sdavidxu ovalp = &oval; 1137151576Sdavidxu else 1138151576Sdavidxu ovalp = NULL; 1139151576Sdavidxu 1140151576Sdavidxu PROC_LOCK(p); 1141151576Sdavidxu if (uap->timerid < 3 || 1142164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1143151576Sdavidxu PROC_UNLOCK(p); 1144151576Sdavidxu error = EINVAL; 1145151576Sdavidxu } else { 1146151576Sdavidxu PROC_UNLOCK(p); 1147151576Sdavidxu itimer_enter(it); 1148151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_settime, 1149151576Sdavidxu (it, uap->flags, &val, ovalp)); 1150151576Sdavidxu itimer_leave(it); 1151151576Sdavidxu ITIMER_UNLOCK(it); 1152151576Sdavidxu } 1153151576Sdavidxu if (error == 0 && uap->ovalue != NULL) 1154151576Sdavidxu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1155151576Sdavidxu return (error); 1156151576Sdavidxu} 1157151576Sdavidxu 1158151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1159156134Sdavidxustruct ktimer_gettime_args { 1160156134Sdavidxu int timerid; 1161151576Sdavidxu struct itimerspec * value; 1162151576Sdavidxu}; 1163151576Sdavidxu#endif 1164151576Sdavidxuint 1165156134Sdavidxuktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 1166151576Sdavidxu{ 1167151576Sdavidxu struct proc *p = td->td_proc; 1168151576Sdavidxu struct itimer *it; 1169151576Sdavidxu struct itimerspec val; 1170151576Sdavidxu int error; 1171151576Sdavidxu 1172151576Sdavidxu PROC_LOCK(p); 1173151576Sdavidxu if (uap->timerid < 3 || 1174164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1175151576Sdavidxu PROC_UNLOCK(p); 1176151576Sdavidxu error = EINVAL; 1177151576Sdavidxu } else { 1178151576Sdavidxu PROC_UNLOCK(p); 1179151576Sdavidxu itimer_enter(it); 1180151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_gettime, 1181151576Sdavidxu (it, &val)); 1182151576Sdavidxu itimer_leave(it); 1183151576Sdavidxu ITIMER_UNLOCK(it); 1184151576Sdavidxu } 1185151576Sdavidxu if (error == 0) 1186151576Sdavidxu error = copyout(&val, uap->value, sizeof(val)); 1187151576Sdavidxu return (error); 1188151576Sdavidxu} 1189151576Sdavidxu 1190151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1191151576Sdavidxustruct timer_getoverrun_args { 1192156134Sdavidxu int timerid; 1193151576Sdavidxu}; 1194151576Sdavidxu#endif 1195151576Sdavidxuint 1196156134Sdavidxuktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 1197151576Sdavidxu{ 1198151576Sdavidxu struct proc *p = td->td_proc; 1199151576Sdavidxu struct itimer *it; 1200151576Sdavidxu int error ; 1201151576Sdavidxu 1202151576Sdavidxu PROC_LOCK(p); 1203151576Sdavidxu if (uap->timerid < 3 || 1204164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1205151576Sdavidxu PROC_UNLOCK(p); 1206151576Sdavidxu error = EINVAL; 1207151576Sdavidxu } else { 1208151576Sdavidxu td->td_retval[0] = it->it_overrun_last; 1209151576Sdavidxu ITIMER_UNLOCK(it); 1210151869Sdavidxu PROC_UNLOCK(p); 1211151576Sdavidxu error = 0; 1212151576Sdavidxu } 1213151576Sdavidxu return (error); 1214151576Sdavidxu} 1215151576Sdavidxu 1216151576Sdavidxustatic int 1217151576Sdavidxurealtimer_create(struct itimer *it) 1218151576Sdavidxu{ 1219151576Sdavidxu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 1220151576Sdavidxu return (0); 1221151576Sdavidxu} 1222151576Sdavidxu 1223151576Sdavidxustatic int 1224151576Sdavidxurealtimer_delete(struct itimer *it) 1225151576Sdavidxu{ 1226151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1227164713Sdavidxu 1228164713Sdavidxu ITIMER_UNLOCK(it); 1229164713Sdavidxu callout_drain(&it->it_callout); 1230164713Sdavidxu ITIMER_LOCK(it); 1231151576Sdavidxu return (0); 1232151576Sdavidxu} 1233151576Sdavidxu 1234151576Sdavidxustatic int 1235151576Sdavidxurealtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 1236151576Sdavidxu{ 1237151869Sdavidxu struct timespec cts; 1238151576Sdavidxu 1239151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1240151576Sdavidxu 1241151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1242151869Sdavidxu *ovalue = it->it_time; 1243151576Sdavidxu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 1244151869Sdavidxu timespecsub(&ovalue->it_value, &cts); 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{ 1259151869Sdavidxu struct timespec cts, ts; 1260151869Sdavidxu struct timeval tv; 1261151869Sdavidxu struct itimerspec val; 1262151576Sdavidxu 1263151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1264151576Sdavidxu 1265151869Sdavidxu val = *value; 1266151869Sdavidxu if (itimespecfix(&val.it_value)) 1267151576Sdavidxu return (EINVAL); 1268151576Sdavidxu 1269151869Sdavidxu if (timespecisset(&val.it_value)) { 1270151869Sdavidxu if (itimespecfix(&val.it_interval)) 1271151576Sdavidxu return (EINVAL); 1272151576Sdavidxu } else { 1273151869Sdavidxu timespecclear(&val.it_interval); 1274151576Sdavidxu } 1275151576Sdavidxu 1276151576Sdavidxu if (ovalue != NULL) 1277151576Sdavidxu realtimer_gettime(it, ovalue); 1278151576Sdavidxu 1279151576Sdavidxu it->it_time = val; 1280151869Sdavidxu if (timespecisset(&val.it_value)) { 1281151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1282151869Sdavidxu ts = val.it_value; 1283151576Sdavidxu if ((flags & TIMER_ABSTIME) == 0) { 1284151576Sdavidxu /* Convert to absolute time. */ 1285151869Sdavidxu timespecadd(&it->it_time.it_value, &cts); 1286151576Sdavidxu } else { 1287151869Sdavidxu timespecsub(&ts, &cts); 1288151576Sdavidxu /* 1289151869Sdavidxu * We don't care if ts is negative, tztohz will 1290151576Sdavidxu * fix it. 1291151576Sdavidxu */ 1292151576Sdavidxu } 1293151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1294151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1295151576Sdavidxu realtimer_expire, it); 1296151576Sdavidxu } else { 1297151576Sdavidxu callout_stop(&it->it_callout); 1298151576Sdavidxu } 1299151576Sdavidxu 1300151576Sdavidxu return (0); 1301151576Sdavidxu} 1302151576Sdavidxu 1303151576Sdavidxustatic void 1304151869Sdavidxurealtimer_clocktime(clockid_t id, struct timespec *ts) 1305151576Sdavidxu{ 1306151576Sdavidxu if (id == CLOCK_REALTIME) 1307151869Sdavidxu getnanotime(ts); 1308151576Sdavidxu else /* CLOCK_MONOTONIC */ 1309151869Sdavidxu getnanouptime(ts); 1310151576Sdavidxu} 1311151576Sdavidxu 1312151869Sdavidxuint 1313156134Sdavidxuitimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 1314151869Sdavidxu{ 1315151869Sdavidxu struct itimer *it; 1316151869Sdavidxu 1317151869Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1318164713Sdavidxu it = itimer_find(p, timerid); 1319151869Sdavidxu if (it != NULL) { 1320151869Sdavidxu ksi->ksi_overrun = it->it_overrun; 1321151869Sdavidxu it->it_overrun_last = it->it_overrun; 1322151869Sdavidxu it->it_overrun = 0; 1323151869Sdavidxu ITIMER_UNLOCK(it); 1324151869Sdavidxu return (0); 1325151869Sdavidxu } 1326151869Sdavidxu return (EINVAL); 1327151869Sdavidxu} 1328151869Sdavidxu 1329151869Sdavidxuint 1330151869Sdavidxuitimespecfix(struct timespec *ts) 1331151869Sdavidxu{ 1332151869Sdavidxu 1333151869Sdavidxu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1334151869Sdavidxu return (EINVAL); 1335151869Sdavidxu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 1336151869Sdavidxu ts->tv_nsec = tick * 1000; 1337151869Sdavidxu return (0); 1338151869Sdavidxu} 1339151869Sdavidxu 1340151576Sdavidxu/* Timeout callback for realtime timer */ 1341151576Sdavidxustatic void 1342151576Sdavidxurealtimer_expire(void *arg) 1343151576Sdavidxu{ 1344151869Sdavidxu struct timespec cts, ts; 1345151869Sdavidxu struct timeval tv; 1346151576Sdavidxu struct itimer *it; 1347151576Sdavidxu struct proc *p; 1348151576Sdavidxu 1349151576Sdavidxu it = (struct itimer *)arg; 1350151576Sdavidxu p = it->it_proc; 1351151576Sdavidxu 1352151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1353151576Sdavidxu /* Only fire if time is reached. */ 1354151869Sdavidxu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1355151869Sdavidxu if (timespecisset(&it->it_time.it_interval)) { 1356151869Sdavidxu timespecadd(&it->it_time.it_value, 1357151869Sdavidxu &it->it_time.it_interval); 1358151869Sdavidxu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1359152983Sdavidxu if (it->it_overrun < INT_MAX) 1360152983Sdavidxu it->it_overrun++; 1361152983Sdavidxu else 1362152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1363151869Sdavidxu timespecadd(&it->it_time.it_value, 1364151869Sdavidxu &it->it_time.it_interval); 1365151576Sdavidxu } 1366151576Sdavidxu } else { 1367151576Sdavidxu /* single shot timer ? */ 1368151869Sdavidxu timespecclear(&it->it_time.it_value); 1369151576Sdavidxu } 1370151869Sdavidxu if (timespecisset(&it->it_time.it_value)) { 1371151869Sdavidxu ts = it->it_time.it_value; 1372151869Sdavidxu timespecsub(&ts, &cts); 1373151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1374151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1375151576Sdavidxu realtimer_expire, it); 1376151576Sdavidxu } 1377151576Sdavidxu ITIMER_UNLOCK(it); 1378151576Sdavidxu itimer_fire(it); 1379151576Sdavidxu ITIMER_LOCK(it); 1380151869Sdavidxu } else if (timespecisset(&it->it_time.it_value)) { 1381151869Sdavidxu ts = it->it_time.it_value; 1382151869Sdavidxu timespecsub(&ts, &cts); 1383151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1384151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 1385151576Sdavidxu it); 1386151576Sdavidxu } 1387151576Sdavidxu} 1388151576Sdavidxu 1389151576Sdavidxuvoid 1390151576Sdavidxuitimer_fire(struct itimer *it) 1391151576Sdavidxu{ 1392151576Sdavidxu struct proc *p = it->it_proc; 1393151993Sdavidxu int ret; 1394151576Sdavidxu 1395151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1396151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1397151576Sdavidxu PROC_LOCK(p); 1398151993Sdavidxu if (!KSI_ONQ(&it->it_ksi)) { 1399152983Sdavidxu it->it_ksi.ksi_errno = 0; 1400151993Sdavidxu ret = psignal_event(p, &it->it_sigev, &it->it_ksi); 1401151993Sdavidxu if (__predict_false(ret != 0)) { 1402151993Sdavidxu it->it_overrun++; 1403151993Sdavidxu /* 1404151993Sdavidxu * Broken userland code, thread went 1405151993Sdavidxu * away, disarm the timer. 1406151869Sdavidxu */ 1407151993Sdavidxu if (ret == ESRCH) { 1408151869Sdavidxu ITIMER_LOCK(it); 1409151869Sdavidxu timespecclear(&it->it_time.it_value); 1410151869Sdavidxu timespecclear(&it->it_time.it_interval); 1411151869Sdavidxu callout_stop(&it->it_callout); 1412151869Sdavidxu ITIMER_UNLOCK(it); 1413151869Sdavidxu } 1414151869Sdavidxu } 1415151993Sdavidxu } else { 1416152983Sdavidxu if (it->it_overrun < INT_MAX) 1417152983Sdavidxu it->it_overrun++; 1418152983Sdavidxu else 1419152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1420151576Sdavidxu } 1421151576Sdavidxu PROC_UNLOCK(p); 1422151576Sdavidxu } 1423151576Sdavidxu} 1424151576Sdavidxu 1425151576Sdavidxustatic void 1426151576Sdavidxuitimers_alloc(struct proc *p) 1427151576Sdavidxu{ 1428151585Sdavidxu struct itimers *its; 1429151585Sdavidxu int i; 1430151576Sdavidxu 1431151585Sdavidxu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 1432151585Sdavidxu LIST_INIT(&its->its_virtual); 1433151585Sdavidxu LIST_INIT(&its->its_prof); 1434151585Sdavidxu TAILQ_INIT(&its->its_worklist); 1435151585Sdavidxu for (i = 0; i < TIMER_MAX; i++) 1436151585Sdavidxu its->its_timers[i] = NULL; 1437151576Sdavidxu PROC_LOCK(p); 1438151585Sdavidxu if (p->p_itimers == NULL) { 1439151585Sdavidxu p->p_itimers = its; 1440151576Sdavidxu PROC_UNLOCK(p); 1441151585Sdavidxu } 1442151585Sdavidxu else { 1443151576Sdavidxu PROC_UNLOCK(p); 1444151585Sdavidxu free(its, M_SUBPROC); 1445151576Sdavidxu } 1446151576Sdavidxu} 1447151576Sdavidxu 1448161302Snetchildstatic void 1449161302Snetchilditimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1450161302Snetchild{ 1451164713Sdavidxu itimers_event_hook_exit(arg, p); 1452161302Snetchild} 1453161302Snetchild 1454151576Sdavidxu/* Clean up timers when some process events are being triggered. */ 1455153259Sdavidxustatic void 1456161302Snetchilditimers_event_hook_exit(void *arg, struct proc *p) 1457151576Sdavidxu{ 1458151576Sdavidxu struct itimers *its; 1459151576Sdavidxu struct itimer *it; 1460153267Sdavidxu int event = (int)(intptr_t)arg; 1461151576Sdavidxu int i; 1462151576Sdavidxu 1463151585Sdavidxu if (p->p_itimers != NULL) { 1464151585Sdavidxu its = p->p_itimers; 1465151576Sdavidxu for (i = 0; i < MAX_CLOCKS; ++i) { 1466151576Sdavidxu if (posix_clocks[i].event_hook != NULL) 1467151576Sdavidxu CLOCK_CALL(i, event_hook, (p, i, event)); 1468151576Sdavidxu } 1469151576Sdavidxu /* 1470151576Sdavidxu * According to susv3, XSI interval timers should be inherited 1471151576Sdavidxu * by new image. 1472151576Sdavidxu */ 1473151576Sdavidxu if (event == ITIMER_EV_EXEC) 1474151576Sdavidxu i = 3; 1475151576Sdavidxu else if (event == ITIMER_EV_EXIT) 1476151576Sdavidxu i = 0; 1477151576Sdavidxu else 1478151576Sdavidxu panic("unhandled event"); 1479151576Sdavidxu for (; i < TIMER_MAX; ++i) { 1480164713Sdavidxu if ((it = its->its_timers[i]) != NULL) 1481164713Sdavidxu kern_timer_delete(curthread, i); 1482151576Sdavidxu } 1483151576Sdavidxu if (its->its_timers[0] == NULL && 1484151576Sdavidxu its->its_timers[1] == NULL && 1485151576Sdavidxu its->its_timers[2] == NULL) { 1486151585Sdavidxu free(its, M_SUBPROC); 1487151585Sdavidxu p->p_itimers = NULL; 1488151576Sdavidxu } 1489151576Sdavidxu } 1490151576Sdavidxu} 1491