kern_time.c revision 184067
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 184067 2008-10-20 02:37:53Z davidxu $"); 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; 204175429Sdavidxu uint64_t runtime, curtime, switchtime; 20525583Speter 206136152Sjhb p = td->td_proc; 207151357Sps switch (clock_id) { 208152844Srwatson case CLOCK_REALTIME: /* Default to precise. */ 209152844Srwatson case CLOCK_REALTIME_PRECISE: 210151357Sps nanotime(ats); 211130654Skbyanc break; 212152844Srwatson case CLOCK_REALTIME_FAST: 213152844Srwatson getnanotime(ats); 214152844Srwatson break; 215130654Skbyanc case CLOCK_VIRTUAL: 216136152Sjhb PROC_LOCK(p); 217170472Sattilio PROC_SLOCK(p); 218136152Sjhb calcru(p, &user, &sys); 219170472Sattilio PROC_SUNLOCK(p); 220136152Sjhb PROC_UNLOCK(p); 221151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 222130654Skbyanc break; 223130654Skbyanc case CLOCK_PROF: 224136152Sjhb PROC_LOCK(p); 225170472Sattilio PROC_SLOCK(p); 226136152Sjhb calcru(p, &user, &sys); 227170472Sattilio PROC_SUNLOCK(p); 228136152Sjhb PROC_UNLOCK(p); 229130884Skbyanc timevaladd(&user, &sys); 230151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 231130654Skbyanc break; 232152844Srwatson case CLOCK_MONOTONIC: /* Default to precise. */ 233152844Srwatson case CLOCK_MONOTONIC_PRECISE: 234152585Sandre case CLOCK_UPTIME: 235152844Srwatson case CLOCK_UPTIME_PRECISE: 236151357Sps nanouptime(ats); 237130884Skbyanc break; 238152844Srwatson case CLOCK_UPTIME_FAST: 239152844Srwatson case CLOCK_MONOTONIC_FAST: 240152844Srwatson getnanouptime(ats); 241152844Srwatson break; 242152844Srwatson case CLOCK_SECOND: 243152844Srwatson ats->tv_sec = time_second; 244152844Srwatson ats->tv_nsec = 0; 245152844Srwatson break; 246175429Sdavidxu case CLOCK_THREAD_CPUTIME_ID: 247175429Sdavidxu critical_enter(); 248175429Sdavidxu switchtime = PCPU_GET(switchtime); 249175429Sdavidxu curtime = cpu_ticks(); 250175439Sdavidxu runtime = td->td_runtime; 251175429Sdavidxu critical_exit(); 252175439Sdavidxu runtime = cputick2usec(runtime + curtime - switchtime); 253175429Sdavidxu ats->tv_sec = runtime / 1000000; 254175429Sdavidxu ats->tv_nsec = runtime % 1000000 * 1000; 255175429Sdavidxu break; 256130654Skbyanc default: 257111315Sphk return (EINVAL); 258130654Skbyanc } 259151357Sps return (0); 26025583Speter} 26125583Speter 26225583Speter#ifndef _SYS_SYSPROTO_H_ 26325583Speterstruct clock_settime_args { 26425583Speter clockid_t clock_id; 26525583Speter const struct timespec *tp; 26625583Speter}; 26725583Speter#endif 26825583Speter/* ARGSUSED */ 26925583Speterint 270102074Sphkclock_settime(struct thread *td, struct clock_settime_args *uap) 27125583Speter{ 27225583Speter struct timespec ats; 27325583Speter int error; 27425583Speter 275151357Sps if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 276151357Sps return (error); 277151357Sps return (kern_clock_settime(td, uap->clock_id, &ats)); 278151357Sps} 279151357Sps 280151357Spsint 281151357Spskern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 282151357Sps{ 283151357Sps struct timeval atv; 284151357Sps int error; 285151357Sps 286164033Srwatson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 28794343Sjhb return (error); 288151357Sps if (clock_id != CLOCK_REALTIME) 28994343Sjhb return (EINVAL); 290151357Sps if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 29194343Sjhb return (EINVAL); 29234901Sphk /* XXX Don't convert nsec->usec and back */ 293151357Sps TIMESPEC_TO_TIMEVAL(&atv, ats); 29494343Sjhb error = settime(td, &atv); 29582746Sdillon return (error); 29625583Speter} 29725583Speter 29825583Speter#ifndef _SYS_SYSPROTO_H_ 29925583Speterstruct clock_getres_args { 30025583Speter clockid_t clock_id; 30125583Speter struct timespec *tp; 30225583Speter}; 30325583Speter#endif 30425583Speterint 305102074Sphkclock_getres(struct thread *td, struct clock_getres_args *uap) 30625583Speter{ 30725583Speter struct timespec ts; 308151357Sps int error; 30925583Speter 310151357Sps if (uap->tp == NULL) 311151357Sps return (0); 312151357Sps 313151357Sps error = kern_clock_getres(td, uap->clock_id, &ts); 314151357Sps if (error == 0) 315151357Sps error = copyout(&ts, uap->tp, sizeof(ts)); 316151357Sps return (error); 317151357Sps} 318151357Sps 319151357Spsint 320151357Spskern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 321151357Sps{ 322151357Sps 323151357Sps ts->tv_sec = 0; 324151357Sps switch (clock_id) { 325130654Skbyanc case CLOCK_REALTIME: 326152844Srwatson case CLOCK_REALTIME_FAST: 327152844Srwatson case CLOCK_REALTIME_PRECISE: 328130654Skbyanc case CLOCK_MONOTONIC: 329152844Srwatson case CLOCK_MONOTONIC_FAST: 330152844Srwatson case CLOCK_MONOTONIC_PRECISE: 331152585Sandre case CLOCK_UPTIME: 332152844Srwatson case CLOCK_UPTIME_FAST: 333152844Srwatson case CLOCK_UPTIME_PRECISE: 334103964Sbde /* 335103964Sbde * Round up the result of the division cheaply by adding 1. 336103964Sbde * Rounding up is especially important if rounding down 337103964Sbde * would give 0. Perfect rounding is unimportant. 338103964Sbde */ 339151357Sps ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 340130654Skbyanc break; 341130654Skbyanc case CLOCK_VIRTUAL: 342130654Skbyanc case CLOCK_PROF: 343130654Skbyanc /* Accurately round up here because we can do so cheaply. */ 344151357Sps ts->tv_nsec = (1000000000 + hz - 1) / hz; 345130654Skbyanc break; 346152844Srwatson case CLOCK_SECOND: 347152844Srwatson ts->tv_sec = 1; 348152844Srwatson ts->tv_nsec = 0; 349152844Srwatson break; 350175429Sdavidxu case CLOCK_THREAD_CPUTIME_ID: 351175429Sdavidxu /* sync with cputick2usec */ 352175429Sdavidxu ts->tv_nsec = 1000000 / cpu_tickrate(); 353175429Sdavidxu if (ts->tv_nsec == 0) 354175429Sdavidxu ts->tv_nsec = 1000; 355175429Sdavidxu break; 356130654Skbyanc default: 357130654Skbyanc return (EINVAL); 358130654Skbyanc } 359151357Sps return (0); 36025583Speter} 36125583Speter 36226335Speterstatic int nanowait; 36325656Speter 364140481Spsint 365140481Spskern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 36625583Speter{ 36735045Sphk struct timespec ts, ts2, ts3; 36835042Sphk struct timeval tv; 36935042Sphk int error; 37025583Speter 37128773Sbde if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 37225656Speter return (EINVAL); 37343301Sdillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 37428773Sbde return (0); 37536119Sphk getnanouptime(&ts); 37635029Sphk timespecadd(&ts, rqt); 37735042Sphk TIMESPEC_TO_TIMEVAL(&tv, rqt); 37835042Sphk for (;;) { 37935042Sphk error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 38035042Sphk tvtohz(&tv)); 38136119Sphk getnanouptime(&ts2); 38235042Sphk if (error != EWOULDBLOCK) { 38335042Sphk if (error == ERESTART) 38435042Sphk error = EINTR; 38535042Sphk if (rmt != NULL) { 38635042Sphk timespecsub(&ts, &ts2); 38735042Sphk if (ts.tv_sec < 0) 38835042Sphk timespecclear(&ts); 38935042Sphk *rmt = ts; 39035042Sphk } 39135042Sphk return (error); 39235042Sphk } 39335029Sphk if (timespeccmp(&ts2, &ts, >=)) 39435042Sphk return (0); 39535045Sphk ts3 = ts; 39635045Sphk timespecsub(&ts3, &ts2); 39735045Sphk TIMESPEC_TO_TIMEVAL(&tv, &ts3); 39826335Speter } 39926335Speter} 40025583Speter 40126335Speter#ifndef _SYS_SYSPROTO_H_ 40226335Speterstruct nanosleep_args { 40326335Speter struct timespec *rqtp; 40426335Speter struct timespec *rmtp; 40526335Speter}; 40626335Speter#endif 40726335Speter/* ARGSUSED */ 40826335Speterint 409102074Sphknanosleep(struct thread *td, struct nanosleep_args *uap) 41026335Speter{ 41126335Speter struct timespec rmt, rqt; 41282746Sdillon int error; 41326335Speter 414107849Salfred error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 41526335Speter if (error) 41626335Speter return (error); 41782746Sdillon 418109521Salfred if (uap->rmtp && 419109521Salfred !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 420109521Salfred return (EFAULT); 421140481Sps error = kern_nanosleep(td, &rqt, &rmt); 422107849Salfred if (error && uap->rmtp) { 42382746Sdillon int error2; 42482746Sdillon 425107849Salfred error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 426109521Salfred if (error2) 42782746Sdillon error = error2; 42825583Speter } 42925656Speter return (error); 43025583Speter} 43125583Speter 43226335Speter#ifndef _SYS_SYSPROTO_H_ 4331541Srgrimesstruct gettimeofday_args { 4341541Srgrimes struct timeval *tp; 4351541Srgrimes struct timezone *tzp; 4361541Srgrimes}; 43712221Sbde#endif 4381541Srgrimes/* ARGSUSED */ 4391549Srgrimesint 440102074Sphkgettimeofday(struct thread *td, struct gettimeofday_args *uap) 4411541Srgrimes{ 4421541Srgrimes struct timeval atv; 443110286Stjr struct timezone rtz; 4441541Srgrimes int error = 0; 4451541Srgrimes 4461541Srgrimes if (uap->tp) { 4471541Srgrimes microtime(&atv); 44899012Salfred error = copyout(&atv, uap->tp, sizeof (atv)); 4491541Srgrimes } 45090836Sphk if (error == 0 && uap->tzp != NULL) { 451110299Sphk rtz.tz_minuteswest = tz_minuteswest; 452110299Sphk rtz.tz_dsttime = tz_dsttime; 453110286Stjr error = copyout(&rtz, uap->tzp, sizeof (rtz)); 45482746Sdillon } 4551541Srgrimes return (error); 4561541Srgrimes} 4571541Srgrimes 45812221Sbde#ifndef _SYS_SYSPROTO_H_ 4591541Srgrimesstruct settimeofday_args { 4601541Srgrimes struct timeval *tv; 4611541Srgrimes struct timezone *tzp; 4621541Srgrimes}; 46312221Sbde#endif 4641541Srgrimes/* ARGSUSED */ 4651549Srgrimesint 466102074Sphksettimeofday(struct thread *td, struct settimeofday_args *uap) 4671541Srgrimes{ 468144445Sjhb struct timeval atv, *tvp; 469144445Sjhb struct timezone atz, *tzp; 470144445Sjhb int error; 4711541Srgrimes 472144445Sjhb if (uap->tv) { 473144445Sjhb error = copyin(uap->tv, &atv, sizeof(atv)); 474144445Sjhb if (error) 475144445Sjhb return (error); 476144445Sjhb tvp = &atv; 477144445Sjhb } else 478144445Sjhb tvp = NULL; 479144445Sjhb if (uap->tzp) { 480144445Sjhb error = copyin(uap->tzp, &atz, sizeof(atz)); 481144445Sjhb if (error) 482144445Sjhb return (error); 483144445Sjhb tzp = &atz; 484144445Sjhb } else 485144445Sjhb tzp = NULL; 486144445Sjhb return (kern_settimeofday(td, tvp, tzp)); 487144445Sjhb} 488144445Sjhb 489144445Sjhbint 490144445Sjhbkern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 491144445Sjhb{ 492144445Sjhb int error; 493144445Sjhb 494164033Srwatson error = priv_check(td, PRIV_SETTIMEOFDAY); 495144445Sjhb if (error) 49694343Sjhb return (error); 4971541Srgrimes /* Verify all parameters before changing time. */ 498144445Sjhb if (tv) { 499144445Sjhb if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 50094343Sjhb return (EINVAL); 501144445Sjhb error = settime(td, tv); 50225656Speter } 503144445Sjhb if (tzp && error == 0) { 504144445Sjhb tz_minuteswest = tzp->tz_minuteswest; 505144445Sjhb tz_dsttime = tzp->tz_dsttime; 50682746Sdillon } 50782746Sdillon return (error); 5081541Srgrimes} 509144445Sjhb 51082746Sdillon/* 511167232Srwatson * Get value of an interval timer. The process virtual and profiling virtual 512167232Srwatson * time timers are kept in the p_stats area, since they can be swapped out. 513167232Srwatson * These are kept internally in the way they are specified externally: in 514167232Srwatson * time until they expire. 5151541Srgrimes * 516167232Srwatson * The real time interval timer is kept in the process table slot for the 517167232Srwatson * process, and its value (it_value) is kept as an absolute time rather than 518167232Srwatson * as a delta, so that it is easy to keep periodic real-time signals from 519167232Srwatson * drifting. 5201541Srgrimes * 5211541Srgrimes * Virtual time timers are processed in the hardclock() routine of 522167232Srwatson * kern_clock.c. The real time timer is processed by a timeout routine, 523167232Srwatson * called from the softclock() routine. Since a callout may be delayed in 524167232Srwatson * real time due to interrupt processing in the system, it is possible for 525167232Srwatson * the real time timeout routine (realitexpire, given below), to be delayed 526167232Srwatson * in real time past when it is supposed to occur. It does not suffice, 527167232Srwatson * therefore, to reload the real timer .it_value from the real time timers 528167232Srwatson * .it_interval. Rather, we compute the next time in absolute time the timer 529167232Srwatson * should go off. 5301541Srgrimes */ 53112221Sbde#ifndef _SYS_SYSPROTO_H_ 5321541Srgrimesstruct getitimer_args { 5331541Srgrimes u_int which; 5341541Srgrimes struct itimerval *itv; 5351541Srgrimes}; 53612221Sbde#endif 5371549Srgrimesint 538102074Sphkgetitimer(struct thread *td, struct getitimer_args *uap) 5391541Srgrimes{ 540141470Sjhb struct itimerval aitv; 541140832Ssobomax int error; 542140832Ssobomax 543140832Ssobomax error = kern_getitimer(td, uap->which, &aitv); 544140832Ssobomax if (error != 0) 545140832Ssobomax return (error); 546140832Ssobomax return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 547140832Ssobomax} 548140832Ssobomax 549140832Ssobomaxint 550140832Ssobomaxkern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 551140832Ssobomax{ 55283366Sjulian struct proc *p = td->td_proc; 55334961Sphk struct timeval ctv; 5541541Srgrimes 555140832Ssobomax if (which > ITIMER_PROF) 5561541Srgrimes return (EINVAL); 55782746Sdillon 558140832Ssobomax if (which == ITIMER_REAL) { 5591541Srgrimes /* 56036128Sbde * Convert from absolute to relative time in .it_value 5611541Srgrimes * part of real time timer. If time for real time timer 5621541Srgrimes * has passed return 0, else return difference between 5631541Srgrimes * current time and time for the timer to go off. 5641541Srgrimes */ 565111034Stjr PROC_LOCK(p); 566140832Ssobomax *aitv = p->p_realtimer; 567111034Stjr PROC_UNLOCK(p); 568140832Ssobomax if (timevalisset(&aitv->it_value)) { 56936119Sphk getmicrouptime(&ctv); 570140832Ssobomax if (timevalcmp(&aitv->it_value, &ctv, <)) 571140832Ssobomax timevalclear(&aitv->it_value); 5721541Srgrimes else 573140832Ssobomax timevalsub(&aitv->it_value, &ctv); 57434961Sphk } 57582746Sdillon } else { 576170307Sjeff PROC_SLOCK(p); 577140832Ssobomax *aitv = p->p_stats->p_timer[which]; 578170307Sjeff PROC_SUNLOCK(p); 57982746Sdillon } 580140832Ssobomax return (0); 5811541Srgrimes} 5821541Srgrimes 58312221Sbde#ifndef _SYS_SYSPROTO_H_ 5841541Srgrimesstruct setitimer_args { 5851541Srgrimes u_int which; 5861541Srgrimes struct itimerval *itv, *oitv; 5871541Srgrimes}; 58812221Sbde#endif 5891549Srgrimesint 590102074Sphksetitimer(struct thread *td, struct setitimer_args *uap) 5911541Srgrimes{ 592141470Sjhb struct itimerval aitv, oitv; 593140832Ssobomax int error; 5941541Srgrimes 595111034Stjr if (uap->itv == NULL) { 596111034Stjr uap->itv = uap->oitv; 597111034Stjr return (getitimer(td, (struct getitimer_args *)uap)); 598111034Stjr } 599111034Stjr 600111034Stjr if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 6011541Srgrimes return (error); 602140832Ssobomax error = kern_setitimer(td, uap->which, &aitv, &oitv); 603140832Ssobomax if (error != 0 || uap->oitv == NULL) 604140832Ssobomax return (error); 605140832Ssobomax return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 606140832Ssobomax} 607140832Ssobomax 608140832Ssobomaxint 609141470Sjhbkern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 610141470Sjhb struct itimerval *oitv) 611140832Ssobomax{ 612140832Ssobomax struct proc *p = td->td_proc; 613140832Ssobomax struct timeval ctv; 614140832Ssobomax 615141483Sjhb if (aitv == NULL) 616141483Sjhb return (kern_getitimer(td, which, oitv)); 617141483Sjhb 618140832Ssobomax if (which > ITIMER_PROF) 619111034Stjr return (EINVAL); 620140832Ssobomax if (itimerfix(&aitv->it_value)) 621111034Stjr return (EINVAL); 622140832Ssobomax if (!timevalisset(&aitv->it_value)) 623140832Ssobomax timevalclear(&aitv->it_interval); 624140832Ssobomax else if (itimerfix(&aitv->it_interval)) 625140832Ssobomax return (EINVAL); 62682746Sdillon 627140832Ssobomax if (which == ITIMER_REAL) { 628111034Stjr PROC_LOCK(p); 62935058Sphk if (timevalisset(&p->p_realtimer.it_value)) 63069286Sjake callout_stop(&p->p_itcallout); 631114980Sjhb getmicrouptime(&ctv); 632140832Ssobomax if (timevalisset(&aitv->it_value)) { 633140832Ssobomax callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 63469286Sjake realitexpire, p); 635140832Ssobomax timevaladd(&aitv->it_value, &ctv); 636114980Sjhb } 637140832Ssobomax *oitv = p->p_realtimer; 638140832Ssobomax p->p_realtimer = *aitv; 639111034Stjr PROC_UNLOCK(p); 640140832Ssobomax if (timevalisset(&oitv->it_value)) { 641140832Ssobomax if (timevalcmp(&oitv->it_value, &ctv, <)) 642140832Ssobomax timevalclear(&oitv->it_value); 643111034Stjr else 644140832Ssobomax timevalsub(&oitv->it_value, &ctv); 645111034Stjr } 64682746Sdillon } else { 647170307Sjeff PROC_SLOCK(p); 648140832Ssobomax *oitv = p->p_stats->p_timer[which]; 649140832Ssobomax p->p_stats->p_timer[which] = *aitv; 650170307Sjeff PROC_SUNLOCK(p); 65182746Sdillon } 652140832Ssobomax return (0); 6531541Srgrimes} 6541541Srgrimes 6551541Srgrimes/* 6561541Srgrimes * Real interval timer expired: 6571541Srgrimes * send process whose timer expired an alarm signal. 6581541Srgrimes * If time is not set up to reload, then just return. 6591541Srgrimes * Else compute next time timer should go off which is > current time. 6601541Srgrimes * This is where delay in processing this timeout causes multiple 6611541Srgrimes * SIGALRM calls to be compressed into one. 66236127Sbde * tvtohz() always adds 1 to allow for the time until the next clock 6639327Sbde * interrupt being strictly less than 1 clock tick, but we don't want 6649327Sbde * that here since we want to appear to be in sync with the clock 6659327Sbde * interrupt even when we're delayed. 6661541Srgrimes */ 6671541Srgrimesvoid 668102074Sphkrealitexpire(void *arg) 6691541Srgrimes{ 670102074Sphk struct proc *p; 67135044Sphk struct timeval ctv, ntv; 6721541Srgrimes 6731541Srgrimes p = (struct proc *)arg; 67473916Sjhb PROC_LOCK(p); 6751541Srgrimes psignal(p, SIGALRM); 67635058Sphk if (!timevalisset(&p->p_realtimer.it_interval)) { 67735058Sphk timevalclear(&p->p_realtimer.it_value); 678116123Sjhb if (p->p_flag & P_WEXIT) 679116123Sjhb wakeup(&p->p_itcallout); 68073916Sjhb PROC_UNLOCK(p); 6811541Srgrimes return; 6821541Srgrimes } 6831541Srgrimes for (;;) { 6841541Srgrimes timevaladd(&p->p_realtimer.it_value, 6851541Srgrimes &p->p_realtimer.it_interval); 68636119Sphk getmicrouptime(&ctv); 68735058Sphk if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 68835044Sphk ntv = p->p_realtimer.it_value; 68935044Sphk timevalsub(&ntv, &ctv); 69069286Sjake callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 69169286Sjake realitexpire, p); 69273916Sjhb PROC_UNLOCK(p); 6931541Srgrimes return; 6941541Srgrimes } 6951541Srgrimes } 69673916Sjhb /*NOTREACHED*/ 6971541Srgrimes} 6981541Srgrimes 6991541Srgrimes/* 7001541Srgrimes * Check that a proposed value to load into the .it_value or 7011541Srgrimes * .it_interval part of an interval timer is acceptable, and 7021541Srgrimes * fix it to have at least minimal value (i.e. if it is less 7031541Srgrimes * than the resolution of the clock, round it up.) 7041541Srgrimes */ 7051549Srgrimesint 706102074Sphkitimerfix(struct timeval *tv) 7071541Srgrimes{ 7081541Srgrimes 709151576Sdavidxu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 7101541Srgrimes return (EINVAL); 7111541Srgrimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 7121541Srgrimes tv->tv_usec = tick; 7131541Srgrimes return (0); 7141541Srgrimes} 7151541Srgrimes 7161541Srgrimes/* 7171541Srgrimes * Decrement an interval timer by a specified number 7181541Srgrimes * of microseconds, which must be less than a second, 7191541Srgrimes * i.e. < 1000000. If the timer expires, then reload 7201541Srgrimes * it. In this case, carry over (usec - old value) to 7211541Srgrimes * reduce the value reloaded into the timer so that 7221541Srgrimes * the timer does not drift. This routine assumes 7231541Srgrimes * that it is called in a context where the timers 7241541Srgrimes * on which it is operating cannot change in value. 7251541Srgrimes */ 7261549Srgrimesint 727102074Sphkitimerdecr(struct itimerval *itp, int usec) 7281541Srgrimes{ 7291541Srgrimes 7301541Srgrimes if (itp->it_value.tv_usec < usec) { 7311541Srgrimes if (itp->it_value.tv_sec == 0) { 7321541Srgrimes /* expired, and already in next interval */ 7331541Srgrimes usec -= itp->it_value.tv_usec; 7341541Srgrimes goto expire; 7351541Srgrimes } 7361541Srgrimes itp->it_value.tv_usec += 1000000; 7371541Srgrimes itp->it_value.tv_sec--; 7381541Srgrimes } 7391541Srgrimes itp->it_value.tv_usec -= usec; 7401541Srgrimes usec = 0; 74135058Sphk if (timevalisset(&itp->it_value)) 7421541Srgrimes return (1); 7431541Srgrimes /* expired, exactly at end of interval */ 7441541Srgrimesexpire: 74535058Sphk if (timevalisset(&itp->it_interval)) { 7461541Srgrimes itp->it_value = itp->it_interval; 7471541Srgrimes itp->it_value.tv_usec -= usec; 7481541Srgrimes if (itp->it_value.tv_usec < 0) { 7491541Srgrimes itp->it_value.tv_usec += 1000000; 7501541Srgrimes itp->it_value.tv_sec--; 7511541Srgrimes } 7521541Srgrimes } else 7531541Srgrimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 7541541Srgrimes return (0); 7551541Srgrimes} 7561541Srgrimes 7571541Srgrimes/* 7581541Srgrimes * Add and subtract routines for timevals. 7591541Srgrimes * N.B.: subtract routine doesn't deal with 7601541Srgrimes * results which are before the beginning, 7611541Srgrimes * it just gets very confused in this case. 7621541Srgrimes * Caveat emptor. 7631541Srgrimes */ 7641549Srgrimesvoid 765121523Salfredtimevaladd(struct timeval *t1, const struct timeval *t2) 7661541Srgrimes{ 7671541Srgrimes 7681541Srgrimes t1->tv_sec += t2->tv_sec; 7691541Srgrimes t1->tv_usec += t2->tv_usec; 7701541Srgrimes timevalfix(t1); 7711541Srgrimes} 7721541Srgrimes 7731549Srgrimesvoid 774121523Salfredtimevalsub(struct timeval *t1, const struct timeval *t2) 7751541Srgrimes{ 7761541Srgrimes 7771541Srgrimes t1->tv_sec -= t2->tv_sec; 7781541Srgrimes t1->tv_usec -= t2->tv_usec; 7791541Srgrimes timevalfix(t1); 7801541Srgrimes} 7811541Srgrimes 78212819Sphkstatic void 783102074Sphktimevalfix(struct timeval *t1) 7841541Srgrimes{ 7851541Srgrimes 7861541Srgrimes if (t1->tv_usec < 0) { 7871541Srgrimes t1->tv_sec--; 7881541Srgrimes t1->tv_usec += 1000000; 7891541Srgrimes } 7901541Srgrimes if (t1->tv_usec >= 1000000) { 7911541Srgrimes t1->tv_sec++; 7921541Srgrimes t1->tv_usec -= 1000000; 7931541Srgrimes } 7941541Srgrimes} 795108142Ssam 796108142Ssam/* 797108511Ssam * ratecheck(): simple time-based rate-limit checking. 798108142Ssam */ 799108142Ssamint 800108142Ssamratecheck(struct timeval *lasttime, const struct timeval *mininterval) 801108142Ssam{ 802108142Ssam struct timeval tv, delta; 803108142Ssam int rv = 0; 804108142Ssam 805108511Ssam getmicrouptime(&tv); /* NB: 10ms precision */ 806108511Ssam delta = tv; 807108511Ssam timevalsub(&delta, lasttime); 808108142Ssam 809108142Ssam /* 810108142Ssam * check for 0,0 is so that the message will be seen at least once, 811108142Ssam * even if interval is huge. 812108142Ssam */ 813108142Ssam if (timevalcmp(&delta, mininterval, >=) || 814108142Ssam (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 815108142Ssam *lasttime = tv; 816108142Ssam rv = 1; 817108142Ssam } 818108142Ssam 819108142Ssam return (rv); 820108142Ssam} 821108142Ssam 822108142Ssam/* 823108142Ssam * ppsratecheck(): packets (or events) per second limitation. 824108511Ssam * 825108511Ssam * Return 0 if the limit is to be enforced (e.g. the caller 826108511Ssam * should drop a packet because of the rate limitation). 827108511Ssam * 828111558Ssam * maxpps of 0 always causes zero to be returned. maxpps of -1 829111558Ssam * always causes 1 to be returned; this effectively defeats rate 830111558Ssam * limiting. 831111558Ssam * 832108511Ssam * Note that we maintain the struct timeval for compatibility 833108511Ssam * with other bsd systems. We reuse the storage and just monitor 834108511Ssam * clock ticks for minimal overhead. 835108142Ssam */ 836108142Ssamint 837108142Ssamppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 838108142Ssam{ 839108511Ssam int now; 840108142Ssam 841108142Ssam /* 842108511Ssam * Reset the last time and counter if this is the first call 843108511Ssam * or more than a second has passed since the last update of 844108511Ssam * lasttime. 845108142Ssam */ 846108511Ssam now = ticks; 847108511Ssam if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 848108511Ssam lasttime->tv_sec = now; 849108511Ssam *curpps = 1; 850111558Ssam return (maxpps != 0); 851108511Ssam } else { 852108511Ssam (*curpps)++; /* NB: ignore potential overflow */ 853108511Ssam return (maxpps < 0 || *curpps < maxpps); 854108511Ssam } 855108142Ssam} 856151576Sdavidxu 857151576Sdavidxustatic void 858151576Sdavidxuitimer_start(void) 859151576Sdavidxu{ 860151576Sdavidxu struct kclock rt_clock = { 861151576Sdavidxu .timer_create = realtimer_create, 862151576Sdavidxu .timer_delete = realtimer_delete, 863151576Sdavidxu .timer_settime = realtimer_settime, 864151576Sdavidxu .timer_gettime = realtimer_gettime, 865164713Sdavidxu .event_hook = NULL 866151576Sdavidxu }; 867151576Sdavidxu 868151576Sdavidxu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 869151576Sdavidxu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 870151576Sdavidxu register_posix_clock(CLOCK_REALTIME, &rt_clock); 871151576Sdavidxu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 872152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 873152983Sdavidxu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 874152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 875161302Snetchild EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 876153259Sdavidxu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 877161302Snetchild EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 878153259Sdavidxu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 879151576Sdavidxu} 880151576Sdavidxu 881151576Sdavidxuint 882151576Sdavidxuregister_posix_clock(int clockid, struct kclock *clk) 883151576Sdavidxu{ 884151576Sdavidxu if ((unsigned)clockid >= MAX_CLOCKS) { 885151576Sdavidxu printf("%s: invalid clockid\n", __func__); 886151576Sdavidxu return (0); 887151576Sdavidxu } 888151576Sdavidxu posix_clocks[clockid] = *clk; 889151576Sdavidxu return (1); 890151576Sdavidxu} 891151576Sdavidxu 892151576Sdavidxustatic int 893151576Sdavidxuitimer_init(void *mem, int size, int flags) 894151576Sdavidxu{ 895151576Sdavidxu struct itimer *it; 896151576Sdavidxu 897151576Sdavidxu it = (struct itimer *)mem; 898151576Sdavidxu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 899151576Sdavidxu return (0); 900151576Sdavidxu} 901151576Sdavidxu 902151576Sdavidxustatic void 903151576Sdavidxuitimer_fini(void *mem, int size) 904151576Sdavidxu{ 905151576Sdavidxu struct itimer *it; 906151576Sdavidxu 907151576Sdavidxu it = (struct itimer *)mem; 908151576Sdavidxu mtx_destroy(&it->it_mtx); 909151576Sdavidxu} 910151576Sdavidxu 911151576Sdavidxustatic void 912151576Sdavidxuitimer_enter(struct itimer *it) 913151576Sdavidxu{ 914151576Sdavidxu 915151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 916151576Sdavidxu it->it_usecount++; 917151576Sdavidxu} 918151576Sdavidxu 919151576Sdavidxustatic void 920151576Sdavidxuitimer_leave(struct itimer *it) 921151576Sdavidxu{ 922151576Sdavidxu 923151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 924151576Sdavidxu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 925151576Sdavidxu 926151576Sdavidxu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 927151576Sdavidxu wakeup(it); 928151576Sdavidxu} 929151576Sdavidxu 930151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 931156134Sdavidxustruct ktimer_create_args { 932151576Sdavidxu clockid_t clock_id; 933151576Sdavidxu struct sigevent * evp; 934156134Sdavidxu int * timerid; 935151576Sdavidxu}; 936151576Sdavidxu#endif 937151576Sdavidxuint 938156134Sdavidxuktimer_create(struct thread *td, struct ktimer_create_args *uap) 939151576Sdavidxu{ 940151576Sdavidxu struct sigevent *evp1, ev; 941156134Sdavidxu int id; 942151576Sdavidxu int error; 943151576Sdavidxu 944151576Sdavidxu if (uap->evp != NULL) { 945151576Sdavidxu error = copyin(uap->evp, &ev, sizeof(ev)); 946151576Sdavidxu if (error != 0) 947151576Sdavidxu return (error); 948151576Sdavidxu evp1 = &ev; 949151576Sdavidxu } else 950151576Sdavidxu evp1 = NULL; 951151576Sdavidxu 952151576Sdavidxu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 953151576Sdavidxu 954151576Sdavidxu if (error == 0) { 955156134Sdavidxu error = copyout(&id, uap->timerid, sizeof(int)); 956151576Sdavidxu if (error != 0) 957151576Sdavidxu kern_timer_delete(td, id); 958151576Sdavidxu } 959151576Sdavidxu return (error); 960151576Sdavidxu} 961151576Sdavidxu 962151576Sdavidxustatic int 963151576Sdavidxukern_timer_create(struct thread *td, clockid_t clock_id, 964156134Sdavidxu struct sigevent *evp, int *timerid, int preset_id) 965151576Sdavidxu{ 966151576Sdavidxu struct proc *p = td->td_proc; 967151576Sdavidxu struct itimer *it; 968151576Sdavidxu int id; 969151576Sdavidxu int error; 970151576Sdavidxu 971151576Sdavidxu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 972151576Sdavidxu return (EINVAL); 973151576Sdavidxu 974151576Sdavidxu if (posix_clocks[clock_id].timer_create == NULL) 975151576Sdavidxu return (EINVAL); 976151576Sdavidxu 977151576Sdavidxu if (evp != NULL) { 978151576Sdavidxu if (evp->sigev_notify != SIGEV_NONE && 979151869Sdavidxu evp->sigev_notify != SIGEV_SIGNAL && 980151869Sdavidxu evp->sigev_notify != SIGEV_THREAD_ID) 981151576Sdavidxu return (EINVAL); 982151869Sdavidxu if ((evp->sigev_notify == SIGEV_SIGNAL || 983151869Sdavidxu evp->sigev_notify == SIGEV_THREAD_ID) && 984151576Sdavidxu !_SIG_VALID(evp->sigev_signo)) 985151576Sdavidxu return (EINVAL); 986151576Sdavidxu } 987151576Sdavidxu 988151585Sdavidxu if (p->p_itimers == NULL) 989151576Sdavidxu itimers_alloc(p); 990151576Sdavidxu 991151576Sdavidxu it = uma_zalloc(itimer_zone, M_WAITOK); 992151576Sdavidxu it->it_flags = 0; 993151576Sdavidxu it->it_usecount = 0; 994151576Sdavidxu it->it_active = 0; 995151869Sdavidxu timespecclear(&it->it_time.it_value); 996151869Sdavidxu timespecclear(&it->it_time.it_interval); 997151576Sdavidxu it->it_overrun = 0; 998151576Sdavidxu it->it_overrun_last = 0; 999151576Sdavidxu it->it_clockid = clock_id; 1000151576Sdavidxu it->it_timerid = -1; 1001151576Sdavidxu it->it_proc = p; 1002151576Sdavidxu ksiginfo_init(&it->it_ksi); 1003151576Sdavidxu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 1004151576Sdavidxu error = CLOCK_CALL(clock_id, timer_create, (it)); 1005151576Sdavidxu if (error != 0) 1006151576Sdavidxu goto out; 1007151576Sdavidxu 1008151576Sdavidxu PROC_LOCK(p); 1009151576Sdavidxu if (preset_id != -1) { 1010151576Sdavidxu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 1011151576Sdavidxu id = preset_id; 1012151585Sdavidxu if (p->p_itimers->its_timers[id] != NULL) { 1013151576Sdavidxu PROC_UNLOCK(p); 1014151576Sdavidxu error = 0; 1015151576Sdavidxu goto out; 1016151576Sdavidxu } 1017151576Sdavidxu } else { 1018151576Sdavidxu /* 1019151576Sdavidxu * Find a free timer slot, skipping those reserved 1020151576Sdavidxu * for setitimer(). 1021151576Sdavidxu */ 1022151576Sdavidxu for (id = 3; id < TIMER_MAX; id++) 1023151585Sdavidxu if (p->p_itimers->its_timers[id] == NULL) 1024151576Sdavidxu break; 1025151576Sdavidxu if (id == TIMER_MAX) { 1026151576Sdavidxu PROC_UNLOCK(p); 1027151576Sdavidxu error = EAGAIN; 1028151576Sdavidxu goto out; 1029151576Sdavidxu } 1030151576Sdavidxu } 1031151576Sdavidxu it->it_timerid = id; 1032151585Sdavidxu p->p_itimers->its_timers[id] = it; 1033151576Sdavidxu if (evp != NULL) 1034151576Sdavidxu it->it_sigev = *evp; 1035151576Sdavidxu else { 1036151576Sdavidxu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 1037151576Sdavidxu switch (clock_id) { 1038151576Sdavidxu default: 1039151576Sdavidxu case CLOCK_REALTIME: 1040151576Sdavidxu it->it_sigev.sigev_signo = SIGALRM; 1041151576Sdavidxu break; 1042151576Sdavidxu case CLOCK_VIRTUAL: 1043151576Sdavidxu it->it_sigev.sigev_signo = SIGVTALRM; 1044151576Sdavidxu break; 1045151576Sdavidxu case CLOCK_PROF: 1046151576Sdavidxu it->it_sigev.sigev_signo = SIGPROF; 1047151576Sdavidxu break; 1048151576Sdavidxu } 1049152029Sdavidxu it->it_sigev.sigev_value.sival_int = id; 1050151576Sdavidxu } 1051151576Sdavidxu 1052151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1053151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1054151576Sdavidxu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 1055151576Sdavidxu it->it_ksi.ksi_code = SI_TIMER; 1056151576Sdavidxu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 1057151576Sdavidxu it->it_ksi.ksi_timerid = id; 1058151576Sdavidxu } 1059151576Sdavidxu PROC_UNLOCK(p); 1060151576Sdavidxu *timerid = id; 1061151576Sdavidxu return (0); 1062151576Sdavidxu 1063151576Sdavidxuout: 1064151576Sdavidxu ITIMER_LOCK(it); 1065151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1066151576Sdavidxu ITIMER_UNLOCK(it); 1067151576Sdavidxu uma_zfree(itimer_zone, it); 1068151576Sdavidxu return (error); 1069151576Sdavidxu} 1070151576Sdavidxu 1071151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1072156134Sdavidxustruct ktimer_delete_args { 1073156134Sdavidxu int timerid; 1074151576Sdavidxu}; 1075151576Sdavidxu#endif 1076151576Sdavidxuint 1077156134Sdavidxuktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 1078151576Sdavidxu{ 1079151576Sdavidxu return (kern_timer_delete(td, uap->timerid)); 1080151576Sdavidxu} 1081151576Sdavidxu 1082151576Sdavidxustatic struct itimer * 1083164713Sdavidxuitimer_find(struct proc *p, int timerid) 1084151576Sdavidxu{ 1085151576Sdavidxu struct itimer *it; 1086151576Sdavidxu 1087151576Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1088151585Sdavidxu if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) || 1089151585Sdavidxu (it = p->p_itimers->its_timers[timerid]) == NULL) { 1090151576Sdavidxu return (NULL); 1091151576Sdavidxu } 1092151576Sdavidxu ITIMER_LOCK(it); 1093164713Sdavidxu if ((it->it_flags & ITF_DELETING) != 0) { 1094151576Sdavidxu ITIMER_UNLOCK(it); 1095151576Sdavidxu it = NULL; 1096151576Sdavidxu } 1097151576Sdavidxu return (it); 1098151576Sdavidxu} 1099151576Sdavidxu 1100151576Sdavidxustatic int 1101156134Sdavidxukern_timer_delete(struct thread *td, int timerid) 1102151576Sdavidxu{ 1103151576Sdavidxu struct proc *p = td->td_proc; 1104151576Sdavidxu struct itimer *it; 1105151576Sdavidxu 1106151576Sdavidxu PROC_LOCK(p); 1107164713Sdavidxu it = itimer_find(p, timerid); 1108151576Sdavidxu if (it == NULL) { 1109151576Sdavidxu PROC_UNLOCK(p); 1110151576Sdavidxu return (EINVAL); 1111151576Sdavidxu } 1112151576Sdavidxu PROC_UNLOCK(p); 1113151576Sdavidxu 1114151576Sdavidxu it->it_flags |= ITF_DELETING; 1115151576Sdavidxu while (it->it_usecount > 0) { 1116151576Sdavidxu it->it_flags |= ITF_WANTED; 1117151576Sdavidxu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 1118151576Sdavidxu } 1119151576Sdavidxu it->it_flags &= ~ITF_WANTED; 1120151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1121151576Sdavidxu ITIMER_UNLOCK(it); 1122151576Sdavidxu 1123151576Sdavidxu PROC_LOCK(p); 1124151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) 1125151576Sdavidxu sigqueue_take(&it->it_ksi); 1126151585Sdavidxu p->p_itimers->its_timers[timerid] = NULL; 1127151576Sdavidxu PROC_UNLOCK(p); 1128151576Sdavidxu uma_zfree(itimer_zone, it); 1129151576Sdavidxu return (0); 1130151576Sdavidxu} 1131151576Sdavidxu 1132151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1133156134Sdavidxustruct ktimer_settime_args { 1134156134Sdavidxu int timerid; 1135151576Sdavidxu int flags; 1136151576Sdavidxu const struct itimerspec * value; 1137151576Sdavidxu struct itimerspec * ovalue; 1138151576Sdavidxu}; 1139151576Sdavidxu#endif 1140151576Sdavidxuint 1141156134Sdavidxuktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 1142151576Sdavidxu{ 1143151576Sdavidxu struct proc *p = td->td_proc; 1144151576Sdavidxu struct itimer *it; 1145151576Sdavidxu struct itimerspec val, oval, *ovalp; 1146151576Sdavidxu int error; 1147151576Sdavidxu 1148151576Sdavidxu error = copyin(uap->value, &val, sizeof(val)); 1149151576Sdavidxu if (error != 0) 1150151576Sdavidxu return (error); 1151151576Sdavidxu 1152151576Sdavidxu if (uap->ovalue != NULL) 1153151576Sdavidxu ovalp = &oval; 1154151576Sdavidxu else 1155151576Sdavidxu ovalp = NULL; 1156151576Sdavidxu 1157151576Sdavidxu PROC_LOCK(p); 1158151576Sdavidxu if (uap->timerid < 3 || 1159164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1160151576Sdavidxu PROC_UNLOCK(p); 1161151576Sdavidxu error = EINVAL; 1162151576Sdavidxu } else { 1163151576Sdavidxu PROC_UNLOCK(p); 1164151576Sdavidxu itimer_enter(it); 1165151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_settime, 1166151576Sdavidxu (it, uap->flags, &val, ovalp)); 1167151576Sdavidxu itimer_leave(it); 1168151576Sdavidxu ITIMER_UNLOCK(it); 1169151576Sdavidxu } 1170151576Sdavidxu if (error == 0 && uap->ovalue != NULL) 1171151576Sdavidxu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1172151576Sdavidxu return (error); 1173151576Sdavidxu} 1174151576Sdavidxu 1175151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1176156134Sdavidxustruct ktimer_gettime_args { 1177156134Sdavidxu int timerid; 1178151576Sdavidxu struct itimerspec * value; 1179151576Sdavidxu}; 1180151576Sdavidxu#endif 1181151576Sdavidxuint 1182156134Sdavidxuktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 1183151576Sdavidxu{ 1184151576Sdavidxu struct proc *p = td->td_proc; 1185151576Sdavidxu struct itimer *it; 1186151576Sdavidxu struct itimerspec val; 1187151576Sdavidxu int error; 1188151576Sdavidxu 1189151576Sdavidxu PROC_LOCK(p); 1190151576Sdavidxu if (uap->timerid < 3 || 1191164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1192151576Sdavidxu PROC_UNLOCK(p); 1193151576Sdavidxu error = EINVAL; 1194151576Sdavidxu } else { 1195151576Sdavidxu PROC_UNLOCK(p); 1196151576Sdavidxu itimer_enter(it); 1197151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_gettime, 1198151576Sdavidxu (it, &val)); 1199151576Sdavidxu itimer_leave(it); 1200151576Sdavidxu ITIMER_UNLOCK(it); 1201151576Sdavidxu } 1202151576Sdavidxu if (error == 0) 1203151576Sdavidxu error = copyout(&val, uap->value, sizeof(val)); 1204151576Sdavidxu return (error); 1205151576Sdavidxu} 1206151576Sdavidxu 1207151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1208151576Sdavidxustruct timer_getoverrun_args { 1209156134Sdavidxu int timerid; 1210151576Sdavidxu}; 1211151576Sdavidxu#endif 1212151576Sdavidxuint 1213156134Sdavidxuktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 1214151576Sdavidxu{ 1215151576Sdavidxu struct proc *p = td->td_proc; 1216151576Sdavidxu struct itimer *it; 1217151576Sdavidxu int error ; 1218151576Sdavidxu 1219151576Sdavidxu PROC_LOCK(p); 1220151576Sdavidxu if (uap->timerid < 3 || 1221164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1222151576Sdavidxu PROC_UNLOCK(p); 1223151576Sdavidxu error = EINVAL; 1224151576Sdavidxu } else { 1225151576Sdavidxu td->td_retval[0] = it->it_overrun_last; 1226151576Sdavidxu ITIMER_UNLOCK(it); 1227151869Sdavidxu PROC_UNLOCK(p); 1228151576Sdavidxu error = 0; 1229151576Sdavidxu } 1230151576Sdavidxu return (error); 1231151576Sdavidxu} 1232151576Sdavidxu 1233151576Sdavidxustatic int 1234151576Sdavidxurealtimer_create(struct itimer *it) 1235151576Sdavidxu{ 1236151576Sdavidxu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 1237151576Sdavidxu return (0); 1238151576Sdavidxu} 1239151576Sdavidxu 1240151576Sdavidxustatic int 1241151576Sdavidxurealtimer_delete(struct itimer *it) 1242151576Sdavidxu{ 1243151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1244164713Sdavidxu 1245184067Sdavidxu /* 1246184067Sdavidxu * clear timer's value and interval to tell realtimer_expire 1247184067Sdavidxu * to not rearm the timer. 1248184067Sdavidxu */ 1249184067Sdavidxu timespecclear(&it->it_time.it_value); 1250184067Sdavidxu timespecclear(&it->it_time.it_interval); 1251164713Sdavidxu ITIMER_UNLOCK(it); 1252164713Sdavidxu callout_drain(&it->it_callout); 1253164713Sdavidxu ITIMER_LOCK(it); 1254151576Sdavidxu return (0); 1255151576Sdavidxu} 1256151576Sdavidxu 1257151576Sdavidxustatic int 1258151576Sdavidxurealtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 1259151576Sdavidxu{ 1260151869Sdavidxu struct timespec cts; 1261151576Sdavidxu 1262151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1263151576Sdavidxu 1264151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1265151869Sdavidxu *ovalue = it->it_time; 1266151576Sdavidxu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 1267151869Sdavidxu timespecsub(&ovalue->it_value, &cts); 1268151576Sdavidxu if (ovalue->it_value.tv_sec < 0 || 1269151576Sdavidxu (ovalue->it_value.tv_sec == 0 && 1270151576Sdavidxu ovalue->it_value.tv_nsec == 0)) { 1271151576Sdavidxu ovalue->it_value.tv_sec = 0; 1272151576Sdavidxu ovalue->it_value.tv_nsec = 1; 1273151576Sdavidxu } 1274151576Sdavidxu } 1275151576Sdavidxu return (0); 1276151576Sdavidxu} 1277151576Sdavidxu 1278151576Sdavidxustatic int 1279151576Sdavidxurealtimer_settime(struct itimer *it, int flags, 1280151576Sdavidxu struct itimerspec *value, struct itimerspec *ovalue) 1281151576Sdavidxu{ 1282151869Sdavidxu struct timespec cts, ts; 1283151869Sdavidxu struct timeval tv; 1284151869Sdavidxu struct itimerspec val; 1285151576Sdavidxu 1286151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1287151576Sdavidxu 1288151869Sdavidxu val = *value; 1289151869Sdavidxu if (itimespecfix(&val.it_value)) 1290151576Sdavidxu return (EINVAL); 1291151576Sdavidxu 1292151869Sdavidxu if (timespecisset(&val.it_value)) { 1293151869Sdavidxu if (itimespecfix(&val.it_interval)) 1294151576Sdavidxu return (EINVAL); 1295151576Sdavidxu } else { 1296151869Sdavidxu timespecclear(&val.it_interval); 1297151576Sdavidxu } 1298151576Sdavidxu 1299151576Sdavidxu if (ovalue != NULL) 1300151576Sdavidxu realtimer_gettime(it, ovalue); 1301151576Sdavidxu 1302151576Sdavidxu it->it_time = val; 1303151869Sdavidxu if (timespecisset(&val.it_value)) { 1304151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1305151869Sdavidxu ts = val.it_value; 1306151576Sdavidxu if ((flags & TIMER_ABSTIME) == 0) { 1307151576Sdavidxu /* Convert to absolute time. */ 1308151869Sdavidxu timespecadd(&it->it_time.it_value, &cts); 1309151576Sdavidxu } else { 1310151869Sdavidxu timespecsub(&ts, &cts); 1311151576Sdavidxu /* 1312151869Sdavidxu * We don't care if ts is negative, tztohz will 1313151576Sdavidxu * fix it. 1314151576Sdavidxu */ 1315151576Sdavidxu } 1316151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1317151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1318151576Sdavidxu realtimer_expire, it); 1319151576Sdavidxu } else { 1320151576Sdavidxu callout_stop(&it->it_callout); 1321151576Sdavidxu } 1322151576Sdavidxu 1323151576Sdavidxu return (0); 1324151576Sdavidxu} 1325151576Sdavidxu 1326151576Sdavidxustatic void 1327151869Sdavidxurealtimer_clocktime(clockid_t id, struct timespec *ts) 1328151576Sdavidxu{ 1329151576Sdavidxu if (id == CLOCK_REALTIME) 1330151869Sdavidxu getnanotime(ts); 1331151576Sdavidxu else /* CLOCK_MONOTONIC */ 1332151869Sdavidxu getnanouptime(ts); 1333151576Sdavidxu} 1334151576Sdavidxu 1335151869Sdavidxuint 1336156134Sdavidxuitimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 1337151869Sdavidxu{ 1338151869Sdavidxu struct itimer *it; 1339151869Sdavidxu 1340151869Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1341164713Sdavidxu it = itimer_find(p, timerid); 1342151869Sdavidxu if (it != NULL) { 1343151869Sdavidxu ksi->ksi_overrun = it->it_overrun; 1344151869Sdavidxu it->it_overrun_last = it->it_overrun; 1345151869Sdavidxu it->it_overrun = 0; 1346151869Sdavidxu ITIMER_UNLOCK(it); 1347151869Sdavidxu return (0); 1348151869Sdavidxu } 1349151869Sdavidxu return (EINVAL); 1350151869Sdavidxu} 1351151869Sdavidxu 1352151869Sdavidxuint 1353151869Sdavidxuitimespecfix(struct timespec *ts) 1354151869Sdavidxu{ 1355151869Sdavidxu 1356151869Sdavidxu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1357151869Sdavidxu return (EINVAL); 1358151869Sdavidxu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 1359151869Sdavidxu ts->tv_nsec = tick * 1000; 1360151869Sdavidxu return (0); 1361151869Sdavidxu} 1362151869Sdavidxu 1363151576Sdavidxu/* Timeout callback for realtime timer */ 1364151576Sdavidxustatic void 1365151576Sdavidxurealtimer_expire(void *arg) 1366151576Sdavidxu{ 1367151869Sdavidxu struct timespec cts, ts; 1368151869Sdavidxu struct timeval tv; 1369151576Sdavidxu struct itimer *it; 1370151576Sdavidxu struct proc *p; 1371151576Sdavidxu 1372151576Sdavidxu it = (struct itimer *)arg; 1373151576Sdavidxu p = it->it_proc; 1374151576Sdavidxu 1375151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1376151576Sdavidxu /* Only fire if time is reached. */ 1377151869Sdavidxu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1378151869Sdavidxu if (timespecisset(&it->it_time.it_interval)) { 1379151869Sdavidxu timespecadd(&it->it_time.it_value, 1380151869Sdavidxu &it->it_time.it_interval); 1381151869Sdavidxu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1382152983Sdavidxu if (it->it_overrun < INT_MAX) 1383152983Sdavidxu it->it_overrun++; 1384152983Sdavidxu else 1385152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1386151869Sdavidxu timespecadd(&it->it_time.it_value, 1387151869Sdavidxu &it->it_time.it_interval); 1388151576Sdavidxu } 1389151576Sdavidxu } else { 1390151576Sdavidxu /* single shot timer ? */ 1391151869Sdavidxu timespecclear(&it->it_time.it_value); 1392151576Sdavidxu } 1393151869Sdavidxu if (timespecisset(&it->it_time.it_value)) { 1394151869Sdavidxu ts = it->it_time.it_value; 1395151869Sdavidxu timespecsub(&ts, &cts); 1396151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1397151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1398151576Sdavidxu realtimer_expire, it); 1399151576Sdavidxu } 1400184067Sdavidxu itimer_enter(it); 1401151576Sdavidxu ITIMER_UNLOCK(it); 1402151576Sdavidxu itimer_fire(it); 1403151576Sdavidxu ITIMER_LOCK(it); 1404184067Sdavidxu itimer_leave(it); 1405151869Sdavidxu } else if (timespecisset(&it->it_time.it_value)) { 1406151869Sdavidxu ts = it->it_time.it_value; 1407151869Sdavidxu timespecsub(&ts, &cts); 1408151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1409151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 1410151576Sdavidxu it); 1411151576Sdavidxu } 1412151576Sdavidxu} 1413151576Sdavidxu 1414151576Sdavidxuvoid 1415151576Sdavidxuitimer_fire(struct itimer *it) 1416151576Sdavidxu{ 1417151576Sdavidxu struct proc *p = it->it_proc; 1418151993Sdavidxu int ret; 1419151576Sdavidxu 1420151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1421151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1422151576Sdavidxu PROC_LOCK(p); 1423151993Sdavidxu if (!KSI_ONQ(&it->it_ksi)) { 1424152983Sdavidxu it->it_ksi.ksi_errno = 0; 1425151993Sdavidxu ret = psignal_event(p, &it->it_sigev, &it->it_ksi); 1426151993Sdavidxu if (__predict_false(ret != 0)) { 1427151993Sdavidxu it->it_overrun++; 1428151993Sdavidxu /* 1429151993Sdavidxu * Broken userland code, thread went 1430151993Sdavidxu * away, disarm the timer. 1431151869Sdavidxu */ 1432151993Sdavidxu if (ret == ESRCH) { 1433151869Sdavidxu ITIMER_LOCK(it); 1434151869Sdavidxu timespecclear(&it->it_time.it_value); 1435151869Sdavidxu timespecclear(&it->it_time.it_interval); 1436151869Sdavidxu callout_stop(&it->it_callout); 1437151869Sdavidxu ITIMER_UNLOCK(it); 1438151869Sdavidxu } 1439151869Sdavidxu } 1440151993Sdavidxu } else { 1441152983Sdavidxu if (it->it_overrun < INT_MAX) 1442152983Sdavidxu it->it_overrun++; 1443152983Sdavidxu else 1444152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1445151576Sdavidxu } 1446151576Sdavidxu PROC_UNLOCK(p); 1447151576Sdavidxu } 1448151576Sdavidxu} 1449151576Sdavidxu 1450151576Sdavidxustatic void 1451151576Sdavidxuitimers_alloc(struct proc *p) 1452151576Sdavidxu{ 1453151585Sdavidxu struct itimers *its; 1454151585Sdavidxu int i; 1455151576Sdavidxu 1456151585Sdavidxu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 1457151585Sdavidxu LIST_INIT(&its->its_virtual); 1458151585Sdavidxu LIST_INIT(&its->its_prof); 1459151585Sdavidxu TAILQ_INIT(&its->its_worklist); 1460151585Sdavidxu for (i = 0; i < TIMER_MAX; i++) 1461151585Sdavidxu its->its_timers[i] = NULL; 1462151576Sdavidxu PROC_LOCK(p); 1463151585Sdavidxu if (p->p_itimers == NULL) { 1464151585Sdavidxu p->p_itimers = its; 1465151576Sdavidxu PROC_UNLOCK(p); 1466151585Sdavidxu } 1467151585Sdavidxu else { 1468151576Sdavidxu PROC_UNLOCK(p); 1469151585Sdavidxu free(its, M_SUBPROC); 1470151576Sdavidxu } 1471151576Sdavidxu} 1472151576Sdavidxu 1473161302Snetchildstatic void 1474161302Snetchilditimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1475161302Snetchild{ 1476164713Sdavidxu itimers_event_hook_exit(arg, p); 1477161302Snetchild} 1478161302Snetchild 1479151576Sdavidxu/* Clean up timers when some process events are being triggered. */ 1480153259Sdavidxustatic void 1481161302Snetchilditimers_event_hook_exit(void *arg, struct proc *p) 1482151576Sdavidxu{ 1483151576Sdavidxu struct itimers *its; 1484151576Sdavidxu struct itimer *it; 1485153267Sdavidxu int event = (int)(intptr_t)arg; 1486151576Sdavidxu int i; 1487151576Sdavidxu 1488151585Sdavidxu if (p->p_itimers != NULL) { 1489151585Sdavidxu its = p->p_itimers; 1490151576Sdavidxu for (i = 0; i < MAX_CLOCKS; ++i) { 1491151576Sdavidxu if (posix_clocks[i].event_hook != NULL) 1492151576Sdavidxu CLOCK_CALL(i, event_hook, (p, i, event)); 1493151576Sdavidxu } 1494151576Sdavidxu /* 1495151576Sdavidxu * According to susv3, XSI interval timers should be inherited 1496151576Sdavidxu * by new image. 1497151576Sdavidxu */ 1498151576Sdavidxu if (event == ITIMER_EV_EXEC) 1499151576Sdavidxu i = 3; 1500151576Sdavidxu else if (event == ITIMER_EV_EXIT) 1501151576Sdavidxu i = 0; 1502151576Sdavidxu else 1503151576Sdavidxu panic("unhandled event"); 1504151576Sdavidxu for (; i < TIMER_MAX; ++i) { 1505164713Sdavidxu if ((it = its->its_timers[i]) != NULL) 1506164713Sdavidxu kern_timer_delete(curthread, i); 1507151576Sdavidxu } 1508151576Sdavidxu if (its->its_timers[0] == NULL && 1509151576Sdavidxu its->its_timers[1] == NULL && 1510151576Sdavidxu its->its_timers[2] == NULL) { 1511151585Sdavidxu free(its, M_SUBPROC); 1512151585Sdavidxu p->p_itimers = NULL; 1513151576Sdavidxu } 1514151576Sdavidxu } 1515151576Sdavidxu} 1516