kern_time.c revision 248186
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 248186 2013-03-12 06:58:49Z mav $"); 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> 46247797Sdavide#include <sys/sleepqueue.h> 47140483Sps#include <sys/syscallsubr.h> 48152983Sdavidxu#include <sys/sysctl.h> 4925583Speter#include <sys/sysent.h> 50164033Srwatson#include <sys/priv.h> 511541Srgrimes#include <sys/proc.h> 52164184Strhodes#include <sys/posix4.h> 5325656Speter#include <sys/time.h> 54151576Sdavidxu#include <sys/timers.h> 5558377Sphk#include <sys/timetc.h> 561541Srgrimes#include <sys/vnode.h> 5776166Smarkm 5826335Speter#include <vm/vm.h> 5926335Speter#include <vm/vm_extern.h> 601541Srgrimes 61151576Sdavidxu#define MAX_CLOCKS (CLOCK_MONOTONIC+1) 62239347Sdavidxu#define CPUCLOCK_BIT 0x80000000 63239347Sdavidxu#define CPUCLOCK_PROCESS_BIT 0x40000000 64239347Sdavidxu#define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT)) 65239347Sdavidxu#define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid)) 66239347Sdavidxu#define MAKE_PROCESS_CPUCLOCK(pid) \ 67239347Sdavidxu (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid)) 68151576Sdavidxu 69151576Sdavidxustatic struct kclock posix_clocks[MAX_CLOCKS]; 70151576Sdavidxustatic uma_zone_t itimer_zone = NULL; 71151576Sdavidxu 728876Srgrimes/* 731541Srgrimes * Time of day and interval timer support. 741541Srgrimes * 751541Srgrimes * These routines provide the kernel entry points to get and set 761541Srgrimes * the time-of-day and per-process interval timers. Subroutines 771541Srgrimes * here provide support for adding and subtracting timeval structures 781541Srgrimes * and decrementing interval timers, optionally reloading the interval 791541Srgrimes * timers when they expire. 801541Srgrimes */ 811541Srgrimes 8294343Sjhbstatic int settime(struct thread *, struct timeval *); 8392723Salfredstatic void timevalfix(struct timeval *); 8413016Sbde 85151576Sdavidxustatic void itimer_start(void); 86151576Sdavidxustatic int itimer_init(void *, int, int); 87151576Sdavidxustatic void itimer_fini(void *, int); 88151576Sdavidxustatic void itimer_enter(struct itimer *); 89151576Sdavidxustatic void itimer_leave(struct itimer *); 90164713Sdavidxustatic struct itimer *itimer_find(struct proc *, int); 91151576Sdavidxustatic void itimers_alloc(struct proc *); 92161302Snetchildstatic void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp); 93161302Snetchildstatic void itimers_event_hook_exit(void *arg, struct proc *p); 94151576Sdavidxustatic int realtimer_create(struct itimer *); 95151576Sdavidxustatic int realtimer_gettime(struct itimer *, struct itimerspec *); 96151576Sdavidxustatic int realtimer_settime(struct itimer *, int, 97151576Sdavidxu struct itimerspec *, struct itimerspec *); 98151576Sdavidxustatic int realtimer_delete(struct itimer *); 99151869Sdavidxustatic void realtimer_clocktime(clockid_t, struct timespec *); 100151576Sdavidxustatic void realtimer_expire(void *); 101151576Sdavidxustatic int kern_timer_create(struct thread *, clockid_t, 102156134Sdavidxu struct sigevent *, int *, int); 103156134Sdavidxustatic int kern_timer_delete(struct thread *, int); 104151576Sdavidxu 105151576Sdavidxuint register_posix_clock(int, struct kclock *); 106151576Sdavidxuvoid itimer_fire(struct itimer *it); 107151869Sdavidxuint itimespecfix(struct timespec *ts); 108151576Sdavidxu 109151576Sdavidxu#define CLOCK_CALL(clock, call, arglist) \ 110151576Sdavidxu ((*posix_clocks[clock].call) arglist) 111151576Sdavidxu 112151576SdavidxuSYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL); 113151576Sdavidxu 114151576Sdavidxu 11525583Speterstatic int 116102074Sphksettime(struct thread *td, struct timeval *tv) 11725583Speter{ 11845433Snsayer struct timeval delta, tv1, tv2; 11945438Snsayer static struct timeval maxtime, laststep; 12033690Sphk struct timespec ts; 12125583Speter int s; 12225583Speter 12325656Speter s = splclock(); 12433818Sbde microtime(&tv1); 12535029Sphk delta = *tv; 12635029Sphk timevalsub(&delta, &tv1); 12725583Speter 12825583Speter /* 12933818Sbde * If the system is secure, we do not allow the time to be 13045433Snsayer * set to a value earlier than 1 second less than the highest 13145433Snsayer * time we have yet seen. The worst a miscreant can do in 13245433Snsayer * this circumstance is "freeze" time. He couldn't go 13345433Snsayer * back to the past. 13445438Snsayer * 13545438Snsayer * We similarly do not allow the clock to be stepped more 13645438Snsayer * than one second, nor more than once per second. This allows 13745438Snsayer * a miscreant to make the clock march double-time, but no worse. 13825583Speter */ 13994343Sjhb if (securelevel_gt(td->td_ucred, 1) != 0) { 14045433Snsayer if (delta.tv_sec < 0 || delta.tv_usec < 0) { 14145437Smjacob /* 14245438Snsayer * Update maxtime to latest time we've seen. 14345437Smjacob */ 14445437Smjacob if (tv1.tv_sec > maxtime.tv_sec) 14545437Smjacob maxtime = tv1; 14645437Smjacob tv2 = *tv; 14745437Smjacob timevalsub(&tv2, &maxtime); 14845437Smjacob if (tv2.tv_sec < -1) { 14945437Smjacob tv->tv_sec = maxtime.tv_sec - 1; 15045433Snsayer printf("Time adjustment clamped to -1 second\n"); 15145433Snsayer } 15245437Smjacob } else { 15345438Snsayer if (tv1.tv_sec == laststep.tv_sec) { 15445438Snsayer splx(s); 15545438Snsayer return (EPERM); 15645438Snsayer } 15745438Snsayer if (delta.tv_sec > 1) { 15845438Snsayer tv->tv_sec = tv1.tv_sec + 1; 15945438Snsayer printf("Time adjustment clamped to +1 second\n"); 16045438Snsayer } 16145438Snsayer laststep = *tv; 16245433Snsayer } 16333818Sbde } 16433818Sbde 16533690Sphk ts.tv_sec = tv->tv_sec; 16633690Sphk ts.tv_nsec = tv->tv_usec * 1000; 16794343Sjhb mtx_lock(&Giant); 16858377Sphk tc_setclock(&ts); 16925583Speter resettodr(); 17094343Sjhb mtx_unlock(&Giant); 17125583Speter return (0); 17225583Speter} 17325583Speter 17412221Sbde#ifndef _SYS_SYSPROTO_H_ 175239347Sdavidxustruct clock_getcpuclockid2_args { 176239347Sdavidxu id_t id; 177239347Sdavidxu int which, 178239347Sdavidxu clockid_t *clock_id; 179239347Sdavidxu}; 180239347Sdavidxu#endif 181239347Sdavidxu/* ARGSUSED */ 182239347Sdavidxuint 183239347Sdavidxusys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap) 184239347Sdavidxu{ 185239347Sdavidxu clockid_t clk_id; 186239347Sdavidxu struct proc *p; 187239347Sdavidxu pid_t pid; 188239347Sdavidxu lwpid_t tid; 189239347Sdavidxu int error; 190239347Sdavidxu 191239347Sdavidxu switch(uap->which) { 192239347Sdavidxu case CPUCLOCK_WHICH_PID: 193239347Sdavidxu if (uap->id != 0) { 194239347Sdavidxu p = pfind(uap->id); 195239347Sdavidxu if (p == NULL) 196239347Sdavidxu return (ESRCH); 197239347Sdavidxu error = p_cansee(td, p); 198239347Sdavidxu PROC_UNLOCK(p); 199239347Sdavidxu if (error) 200239347Sdavidxu return (error); 201239347Sdavidxu pid = uap->id; 202239347Sdavidxu } else { 203239347Sdavidxu pid = td->td_proc->p_pid; 204239347Sdavidxu } 205239347Sdavidxu clk_id = MAKE_PROCESS_CPUCLOCK(pid); 206239347Sdavidxu break; 207239347Sdavidxu case CPUCLOCK_WHICH_TID: 208239347Sdavidxu if (uap->id == 0) 209239347Sdavidxu tid = td->td_tid; 210239347Sdavidxu else 211239347Sdavidxu tid = uap->id; 212239347Sdavidxu clk_id = MAKE_THREAD_CPUCLOCK(tid); 213239347Sdavidxu break; 214239347Sdavidxu default: 215239347Sdavidxu return (EINVAL); 216239347Sdavidxu } 217239347Sdavidxu return (copyout(&clk_id, uap->clock_id, sizeof(clockid_t))); 218239347Sdavidxu} 219239347Sdavidxu 220239347Sdavidxu#ifndef _SYS_SYSPROTO_H_ 22125583Speterstruct clock_gettime_args { 22225583Speter clockid_t clock_id; 22325583Speter struct timespec *tp; 22425583Speter}; 22525583Speter#endif 22625583Speter/* ARGSUSED */ 22725583Speterint 228225617Skmacysys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) 22925583Speter{ 23025583Speter struct timespec ats; 231151357Sps int error; 232151357Sps 233151357Sps error = kern_clock_gettime(td, uap->clock_id, &ats); 234151357Sps if (error == 0) 235151357Sps error = copyout(&ats, uap->tp, sizeof(ats)); 236151357Sps 237151357Sps return (error); 238151357Sps} 239151357Sps 240239347Sdavidxustatic inline void 241239347Sdavidxucputick2timespec(uint64_t runtime, struct timespec *ats) 242239347Sdavidxu{ 243239347Sdavidxu runtime = cputick2usec(runtime); 244239347Sdavidxu ats->tv_sec = runtime / 1000000; 245239347Sdavidxu ats->tv_nsec = runtime % 1000000 * 1000; 246239347Sdavidxu} 247239347Sdavidxu 248239347Sdavidxustatic void 249239347Sdavidxuget_thread_cputime(struct thread *targettd, struct timespec *ats) 250239347Sdavidxu{ 251239347Sdavidxu uint64_t runtime, curtime, switchtime; 252239347Sdavidxu 253239347Sdavidxu if (targettd == NULL) { /* current thread */ 254239347Sdavidxu critical_enter(); 255239347Sdavidxu switchtime = PCPU_GET(switchtime); 256239347Sdavidxu curtime = cpu_ticks(); 257239347Sdavidxu runtime = curthread->td_runtime; 258239347Sdavidxu critical_exit(); 259239347Sdavidxu runtime += curtime - switchtime; 260239347Sdavidxu } else { 261239347Sdavidxu thread_lock(targettd); 262239347Sdavidxu runtime = targettd->td_runtime; 263239347Sdavidxu thread_unlock(targettd); 264239347Sdavidxu } 265239347Sdavidxu cputick2timespec(runtime, ats); 266239347Sdavidxu} 267239347Sdavidxu 268239347Sdavidxustatic void 269239347Sdavidxuget_process_cputime(struct proc *targetp, struct timespec *ats) 270239347Sdavidxu{ 271239347Sdavidxu uint64_t runtime; 272239347Sdavidxu struct rusage ru; 273239347Sdavidxu 274239347Sdavidxu PROC_SLOCK(targetp); 275239347Sdavidxu rufetch(targetp, &ru); 276239347Sdavidxu runtime = targetp->p_rux.rux_runtime; 277239347Sdavidxu PROC_SUNLOCK(targetp); 278239347Sdavidxu cputick2timespec(runtime, ats); 279239347Sdavidxu} 280239347Sdavidxu 281239347Sdavidxustatic int 282239347Sdavidxuget_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) 283239347Sdavidxu{ 284239347Sdavidxu struct proc *p, *p2; 285239347Sdavidxu struct thread *td2; 286239347Sdavidxu lwpid_t tid; 287239347Sdavidxu pid_t pid; 288239347Sdavidxu int error; 289239347Sdavidxu 290239347Sdavidxu p = td->td_proc; 291239347Sdavidxu if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { 292239347Sdavidxu tid = clock_id & CPUCLOCK_ID_MASK; 293239347Sdavidxu td2 = tdfind(tid, p->p_pid); 294239347Sdavidxu if (td2 == NULL) 295239347Sdavidxu return (EINVAL); 296239347Sdavidxu get_thread_cputime(td2, ats); 297239347Sdavidxu PROC_UNLOCK(td2->td_proc); 298239347Sdavidxu } else { 299239347Sdavidxu pid = clock_id & CPUCLOCK_ID_MASK; 300239347Sdavidxu p2 = pfind(pid); 301239347Sdavidxu if (p2 == NULL) 302239347Sdavidxu return (EINVAL); 303239347Sdavidxu error = p_cansee(td, p2); 304239347Sdavidxu if (error) { 305239347Sdavidxu PROC_UNLOCK(p2); 306239347Sdavidxu return (EINVAL); 307239347Sdavidxu } 308239347Sdavidxu get_process_cputime(p2, ats); 309239347Sdavidxu PROC_UNLOCK(p2); 310239347Sdavidxu } 311239347Sdavidxu return (0); 312239347Sdavidxu} 313239347Sdavidxu 314151357Spsint 315151357Spskern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) 316151357Sps{ 317130884Skbyanc struct timeval sys, user; 318136152Sjhb struct proc *p; 31925583Speter 320136152Sjhb p = td->td_proc; 321151357Sps switch (clock_id) { 322152844Srwatson case CLOCK_REALTIME: /* Default to precise. */ 323152844Srwatson case CLOCK_REALTIME_PRECISE: 324151357Sps nanotime(ats); 325130654Skbyanc break; 326152844Srwatson case CLOCK_REALTIME_FAST: 327152844Srwatson getnanotime(ats); 328152844Srwatson break; 329130654Skbyanc case CLOCK_VIRTUAL: 330136152Sjhb PROC_LOCK(p); 331170472Sattilio PROC_SLOCK(p); 332136152Sjhb calcru(p, &user, &sys); 333170472Sattilio PROC_SUNLOCK(p); 334136152Sjhb PROC_UNLOCK(p); 335151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 336130654Skbyanc break; 337130654Skbyanc case CLOCK_PROF: 338136152Sjhb PROC_LOCK(p); 339170472Sattilio PROC_SLOCK(p); 340136152Sjhb calcru(p, &user, &sys); 341170472Sattilio PROC_SUNLOCK(p); 342136152Sjhb PROC_UNLOCK(p); 343130884Skbyanc timevaladd(&user, &sys); 344151357Sps TIMEVAL_TO_TIMESPEC(&user, ats); 345130654Skbyanc break; 346152844Srwatson case CLOCK_MONOTONIC: /* Default to precise. */ 347152844Srwatson case CLOCK_MONOTONIC_PRECISE: 348152585Sandre case CLOCK_UPTIME: 349152844Srwatson case CLOCK_UPTIME_PRECISE: 350151357Sps nanouptime(ats); 351130884Skbyanc break; 352152844Srwatson case CLOCK_UPTIME_FAST: 353152844Srwatson case CLOCK_MONOTONIC_FAST: 354152844Srwatson getnanouptime(ats); 355152844Srwatson break; 356152844Srwatson case CLOCK_SECOND: 357152844Srwatson ats->tv_sec = time_second; 358152844Srwatson ats->tv_nsec = 0; 359152844Srwatson break; 360175429Sdavidxu case CLOCK_THREAD_CPUTIME_ID: 361239347Sdavidxu get_thread_cputime(NULL, ats); 362175429Sdavidxu break; 363239347Sdavidxu case CLOCK_PROCESS_CPUTIME_ID: 364239347Sdavidxu PROC_LOCK(p); 365239347Sdavidxu get_process_cputime(p, ats); 366239347Sdavidxu PROC_UNLOCK(p); 367239347Sdavidxu break; 368130654Skbyanc default: 369239347Sdavidxu if ((int)clock_id >= 0) 370239347Sdavidxu return (EINVAL); 371239347Sdavidxu return (get_cputime(td, clock_id, ats)); 372130654Skbyanc } 373151357Sps return (0); 37425583Speter} 37525583Speter 37625583Speter#ifndef _SYS_SYSPROTO_H_ 37725583Speterstruct clock_settime_args { 37825583Speter clockid_t clock_id; 37925583Speter const struct timespec *tp; 38025583Speter}; 38125583Speter#endif 38225583Speter/* ARGSUSED */ 38325583Speterint 384225617Skmacysys_clock_settime(struct thread *td, struct clock_settime_args *uap) 38525583Speter{ 38625583Speter struct timespec ats; 38725583Speter int error; 38825583Speter 389151357Sps if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 390151357Sps return (error); 391151357Sps return (kern_clock_settime(td, uap->clock_id, &ats)); 392151357Sps} 393151357Sps 394151357Spsint 395151357Spskern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats) 396151357Sps{ 397151357Sps struct timeval atv; 398151357Sps int error; 399151357Sps 400164033Srwatson if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 40194343Sjhb return (error); 402151357Sps if (clock_id != CLOCK_REALTIME) 40394343Sjhb return (EINVAL); 404151357Sps if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000) 40594343Sjhb return (EINVAL); 40634901Sphk /* XXX Don't convert nsec->usec and back */ 407151357Sps TIMESPEC_TO_TIMEVAL(&atv, ats); 40894343Sjhb error = settime(td, &atv); 40982746Sdillon return (error); 41025583Speter} 41125583Speter 41225583Speter#ifndef _SYS_SYSPROTO_H_ 41325583Speterstruct clock_getres_args { 41425583Speter clockid_t clock_id; 41525583Speter struct timespec *tp; 41625583Speter}; 41725583Speter#endif 41825583Speterint 419225617Skmacysys_clock_getres(struct thread *td, struct clock_getres_args *uap) 42025583Speter{ 42125583Speter struct timespec ts; 422151357Sps int error; 42325583Speter 424151357Sps if (uap->tp == NULL) 425151357Sps return (0); 426151357Sps 427151357Sps error = kern_clock_getres(td, uap->clock_id, &ts); 428151357Sps if (error == 0) 429151357Sps error = copyout(&ts, uap->tp, sizeof(ts)); 430151357Sps return (error); 431151357Sps} 432151357Sps 433151357Spsint 434151357Spskern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) 435151357Sps{ 436151357Sps 437151357Sps ts->tv_sec = 0; 438151357Sps switch (clock_id) { 439130654Skbyanc case CLOCK_REALTIME: 440152844Srwatson case CLOCK_REALTIME_FAST: 441152844Srwatson case CLOCK_REALTIME_PRECISE: 442130654Skbyanc case CLOCK_MONOTONIC: 443152844Srwatson case CLOCK_MONOTONIC_FAST: 444152844Srwatson case CLOCK_MONOTONIC_PRECISE: 445152585Sandre case CLOCK_UPTIME: 446152844Srwatson case CLOCK_UPTIME_FAST: 447152844Srwatson case CLOCK_UPTIME_PRECISE: 448103964Sbde /* 449103964Sbde * Round up the result of the division cheaply by adding 1. 450103964Sbde * Rounding up is especially important if rounding down 451103964Sbde * would give 0. Perfect rounding is unimportant. 452103964Sbde */ 453151357Sps ts->tv_nsec = 1000000000 / tc_getfrequency() + 1; 454130654Skbyanc break; 455130654Skbyanc case CLOCK_VIRTUAL: 456130654Skbyanc case CLOCK_PROF: 457130654Skbyanc /* Accurately round up here because we can do so cheaply. */ 458151357Sps ts->tv_nsec = (1000000000 + hz - 1) / hz; 459130654Skbyanc break; 460152844Srwatson case CLOCK_SECOND: 461152844Srwatson ts->tv_sec = 1; 462152844Srwatson ts->tv_nsec = 0; 463152844Srwatson break; 464175429Sdavidxu case CLOCK_THREAD_CPUTIME_ID: 465239347Sdavidxu case CLOCK_PROCESS_CPUTIME_ID: 466239347Sdavidxu cputime: 467175429Sdavidxu /* sync with cputick2usec */ 468175429Sdavidxu ts->tv_nsec = 1000000 / cpu_tickrate(); 469175429Sdavidxu if (ts->tv_nsec == 0) 470175429Sdavidxu ts->tv_nsec = 1000; 471175429Sdavidxu break; 472130654Skbyanc default: 473239347Sdavidxu if ((int)clock_id < 0) 474239347Sdavidxu goto cputime; 475130654Skbyanc return (EINVAL); 476130654Skbyanc } 477151357Sps return (0); 47825583Speter} 47925583Speter 480248186Smavstatic uint8_t nanowait[MAXCPU]; 48125656Speter 482140481Spsint 483140481Spskern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 48425583Speter{ 485247797Sdavide struct timespec ts; 486247797Sdavide sbintime_t sbt, sbtt, prec, tmp; 487247898Smav time_t over; 48835042Sphk int error; 48925583Speter 49028773Sbde if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 49125656Speter return (EINVAL); 49243301Sdillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 49328773Sbde return (0); 494247898Smav ts = *rqt; 495247898Smav if (ts.tv_sec > INT32_MAX / 2) { 496247898Smav over = ts.tv_sec - INT32_MAX / 2; 497247898Smav ts.tv_sec -= over; 498247898Smav } else 499247898Smav over = 0; 500247898Smav tmp = tstosbt(ts); 501247797Sdavide prec = tmp; 502247797Sdavide prec >>= tc_precexp; 503247797Sdavide if (TIMESEL(&sbt, tmp)) 504247797Sdavide sbt += tc_tick_sbt; 505247797Sdavide sbt += tmp; 506248186Smav error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp", 507248186Smav sbt, prec, C_ABSOLUTE); 508247797Sdavide if (error != EWOULDBLOCK) { 509247797Sdavide if (error == ERESTART) 510247797Sdavide error = EINTR; 511247797Sdavide TIMESEL(&sbtt, tmp); 512247797Sdavide if (rmt != NULL) { 513247797Sdavide ts = sbttots(sbt - sbtt); 514247898Smav ts.tv_sec += over; 515247797Sdavide if (ts.tv_sec < 0) 516247797Sdavide timespecclear(&ts); 517247797Sdavide *rmt = ts; 51835042Sphk } 519247797Sdavide if (sbtt >= sbt) 52035042Sphk return (0); 521247797Sdavide return (error); 52226335Speter } 523247797Sdavide return (0); 52426335Speter} 52525583Speter 52626335Speter#ifndef _SYS_SYSPROTO_H_ 52726335Speterstruct nanosleep_args { 52826335Speter struct timespec *rqtp; 52926335Speter struct timespec *rmtp; 53026335Speter}; 53126335Speter#endif 53226335Speter/* ARGSUSED */ 53326335Speterint 534225617Skmacysys_nanosleep(struct thread *td, struct nanosleep_args *uap) 53526335Speter{ 53626335Speter struct timespec rmt, rqt; 53782746Sdillon int error; 53826335Speter 539107849Salfred error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 54026335Speter if (error) 54126335Speter return (error); 54282746Sdillon 543109521Salfred if (uap->rmtp && 544109521Salfred !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 545109521Salfred return (EFAULT); 546140481Sps error = kern_nanosleep(td, &rqt, &rmt); 547107849Salfred if (error && uap->rmtp) { 54882746Sdillon int error2; 54982746Sdillon 550107849Salfred error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 551109521Salfred if (error2) 55282746Sdillon error = error2; 55325583Speter } 55425656Speter return (error); 55525583Speter} 55625583Speter 55726335Speter#ifndef _SYS_SYSPROTO_H_ 5581541Srgrimesstruct gettimeofday_args { 5591541Srgrimes struct timeval *tp; 5601541Srgrimes struct timezone *tzp; 5611541Srgrimes}; 56212221Sbde#endif 5631541Srgrimes/* ARGSUSED */ 5641549Srgrimesint 565225617Skmacysys_gettimeofday(struct thread *td, struct gettimeofday_args *uap) 5661541Srgrimes{ 5671541Srgrimes struct timeval atv; 568110286Stjr struct timezone rtz; 5691541Srgrimes int error = 0; 5701541Srgrimes 5711541Srgrimes if (uap->tp) { 5721541Srgrimes microtime(&atv); 57399012Salfred error = copyout(&atv, uap->tp, sizeof (atv)); 5741541Srgrimes } 57590836Sphk if (error == 0 && uap->tzp != NULL) { 576110299Sphk rtz.tz_minuteswest = tz_minuteswest; 577110299Sphk rtz.tz_dsttime = tz_dsttime; 578110286Stjr error = copyout(&rtz, uap->tzp, sizeof (rtz)); 57982746Sdillon } 5801541Srgrimes return (error); 5811541Srgrimes} 5821541Srgrimes 58312221Sbde#ifndef _SYS_SYSPROTO_H_ 5841541Srgrimesstruct settimeofday_args { 5851541Srgrimes struct timeval *tv; 5861541Srgrimes struct timezone *tzp; 5871541Srgrimes}; 58812221Sbde#endif 5891541Srgrimes/* ARGSUSED */ 5901549Srgrimesint 591225617Skmacysys_settimeofday(struct thread *td, struct settimeofday_args *uap) 5921541Srgrimes{ 593144445Sjhb struct timeval atv, *tvp; 594144445Sjhb struct timezone atz, *tzp; 595144445Sjhb int error; 5961541Srgrimes 597144445Sjhb if (uap->tv) { 598144445Sjhb error = copyin(uap->tv, &atv, sizeof(atv)); 599144445Sjhb if (error) 600144445Sjhb return (error); 601144445Sjhb tvp = &atv; 602144445Sjhb } else 603144445Sjhb tvp = NULL; 604144445Sjhb if (uap->tzp) { 605144445Sjhb error = copyin(uap->tzp, &atz, sizeof(atz)); 606144445Sjhb if (error) 607144445Sjhb return (error); 608144445Sjhb tzp = &atz; 609144445Sjhb } else 610144445Sjhb tzp = NULL; 611144445Sjhb return (kern_settimeofday(td, tvp, tzp)); 612144445Sjhb} 613144445Sjhb 614144445Sjhbint 615144445Sjhbkern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 616144445Sjhb{ 617144445Sjhb int error; 618144445Sjhb 619164033Srwatson error = priv_check(td, PRIV_SETTIMEOFDAY); 620144445Sjhb if (error) 62194343Sjhb return (error); 6221541Srgrimes /* Verify all parameters before changing time. */ 623144445Sjhb if (tv) { 624144445Sjhb if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 62594343Sjhb return (EINVAL); 626144445Sjhb error = settime(td, tv); 62725656Speter } 628144445Sjhb if (tzp && error == 0) { 629144445Sjhb tz_minuteswest = tzp->tz_minuteswest; 630144445Sjhb tz_dsttime = tzp->tz_dsttime; 63182746Sdillon } 63282746Sdillon return (error); 6331541Srgrimes} 634144445Sjhb 63582746Sdillon/* 636167232Srwatson * Get value of an interval timer. The process virtual and profiling virtual 637167232Srwatson * time timers are kept in the p_stats area, since they can be swapped out. 638167232Srwatson * These are kept internally in the way they are specified externally: in 639167232Srwatson * time until they expire. 6401541Srgrimes * 641167232Srwatson * The real time interval timer is kept in the process table slot for the 642167232Srwatson * process, and its value (it_value) is kept as an absolute time rather than 643167232Srwatson * as a delta, so that it is easy to keep periodic real-time signals from 644167232Srwatson * drifting. 6451541Srgrimes * 6461541Srgrimes * Virtual time timers are processed in the hardclock() routine of 647167232Srwatson * kern_clock.c. The real time timer is processed by a timeout routine, 648167232Srwatson * called from the softclock() routine. Since a callout may be delayed in 649167232Srwatson * real time due to interrupt processing in the system, it is possible for 650167232Srwatson * the real time timeout routine (realitexpire, given below), to be delayed 651167232Srwatson * in real time past when it is supposed to occur. It does not suffice, 652167232Srwatson * therefore, to reload the real timer .it_value from the real time timers 653167232Srwatson * .it_interval. Rather, we compute the next time in absolute time the timer 654167232Srwatson * should go off. 6551541Srgrimes */ 65612221Sbde#ifndef _SYS_SYSPROTO_H_ 6571541Srgrimesstruct getitimer_args { 6581541Srgrimes u_int which; 6591541Srgrimes struct itimerval *itv; 6601541Srgrimes}; 66112221Sbde#endif 6621549Srgrimesint 663225617Skmacysys_getitimer(struct thread *td, struct getitimer_args *uap) 6641541Srgrimes{ 665141470Sjhb struct itimerval aitv; 666140832Ssobomax int error; 667140832Ssobomax 668140832Ssobomax error = kern_getitimer(td, uap->which, &aitv); 669140832Ssobomax if (error != 0) 670140832Ssobomax return (error); 671140832Ssobomax return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 672140832Ssobomax} 673140832Ssobomax 674140832Ssobomaxint 675140832Ssobomaxkern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 676140832Ssobomax{ 67783366Sjulian struct proc *p = td->td_proc; 67834961Sphk struct timeval ctv; 6791541Srgrimes 680140832Ssobomax if (which > ITIMER_PROF) 6811541Srgrimes return (EINVAL); 68282746Sdillon 683140832Ssobomax if (which == ITIMER_REAL) { 6841541Srgrimes /* 68536128Sbde * Convert from absolute to relative time in .it_value 6861541Srgrimes * part of real time timer. If time for real time timer 6871541Srgrimes * has passed return 0, else return difference between 6881541Srgrimes * current time and time for the timer to go off. 6891541Srgrimes */ 690111034Stjr PROC_LOCK(p); 691140832Ssobomax *aitv = p->p_realtimer; 692111034Stjr PROC_UNLOCK(p); 693140832Ssobomax if (timevalisset(&aitv->it_value)) { 694247903Smav microuptime(&ctv); 695140832Ssobomax if (timevalcmp(&aitv->it_value, &ctv, <)) 696140832Ssobomax timevalclear(&aitv->it_value); 6971541Srgrimes else 698140832Ssobomax timevalsub(&aitv->it_value, &ctv); 69934961Sphk } 70082746Sdillon } else { 701170307Sjeff PROC_SLOCK(p); 702140832Ssobomax *aitv = p->p_stats->p_timer[which]; 703170307Sjeff PROC_SUNLOCK(p); 70482746Sdillon } 705140832Ssobomax return (0); 7061541Srgrimes} 7071541Srgrimes 70812221Sbde#ifndef _SYS_SYSPROTO_H_ 7091541Srgrimesstruct setitimer_args { 7101541Srgrimes u_int which; 7111541Srgrimes struct itimerval *itv, *oitv; 7121541Srgrimes}; 71312221Sbde#endif 7141549Srgrimesint 715225617Skmacysys_setitimer(struct thread *td, struct setitimer_args *uap) 7161541Srgrimes{ 717141470Sjhb struct itimerval aitv, oitv; 718140832Ssobomax int error; 7191541Srgrimes 720111034Stjr if (uap->itv == NULL) { 721111034Stjr uap->itv = uap->oitv; 722225617Skmacy return (sys_getitimer(td, (struct getitimer_args *)uap)); 723111034Stjr } 724111034Stjr 725111034Stjr if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 7261541Srgrimes return (error); 727140832Ssobomax error = kern_setitimer(td, uap->which, &aitv, &oitv); 728140832Ssobomax if (error != 0 || uap->oitv == NULL) 729140832Ssobomax return (error); 730140832Ssobomax return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 731140832Ssobomax} 732140832Ssobomax 733140832Ssobomaxint 734141470Sjhbkern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 735141470Sjhb struct itimerval *oitv) 736140832Ssobomax{ 737140832Ssobomax struct proc *p = td->td_proc; 738140832Ssobomax struct timeval ctv; 739247903Smav sbintime_t sbt, pr; 740140832Ssobomax 741141483Sjhb if (aitv == NULL) 742141483Sjhb return (kern_getitimer(td, which, oitv)); 743141483Sjhb 744140832Ssobomax if (which > ITIMER_PROF) 745111034Stjr return (EINVAL); 746247903Smav if (itimerfix(&aitv->it_value) || 747247903Smav aitv->it_value.tv_sec > INT32_MAX / 2) 748111034Stjr return (EINVAL); 749140832Ssobomax if (!timevalisset(&aitv->it_value)) 750140832Ssobomax timevalclear(&aitv->it_interval); 751247903Smav else if (itimerfix(&aitv->it_interval) || 752247903Smav aitv->it_interval.tv_sec > INT32_MAX / 2) 753140832Ssobomax return (EINVAL); 75482746Sdillon 755140832Ssobomax if (which == ITIMER_REAL) { 756111034Stjr PROC_LOCK(p); 75735058Sphk if (timevalisset(&p->p_realtimer.it_value)) 75869286Sjake callout_stop(&p->p_itcallout); 759247903Smav microuptime(&ctv); 760140832Ssobomax if (timevalisset(&aitv->it_value)) { 761247903Smav pr = tvtosbt(aitv->it_value) >> tc_precexp; 762140832Ssobomax timevaladd(&aitv->it_value, &ctv); 763247903Smav sbt = tvtosbt(aitv->it_value); 764247903Smav callout_reset_sbt(&p->p_itcallout, sbt, pr, 765247903Smav realitexpire, p, C_ABSOLUTE); 766114980Sjhb } 767140832Ssobomax *oitv = p->p_realtimer; 768140832Ssobomax p->p_realtimer = *aitv; 769111034Stjr PROC_UNLOCK(p); 770140832Ssobomax if (timevalisset(&oitv->it_value)) { 771140832Ssobomax if (timevalcmp(&oitv->it_value, &ctv, <)) 772140832Ssobomax timevalclear(&oitv->it_value); 773111034Stjr else 774140832Ssobomax timevalsub(&oitv->it_value, &ctv); 775111034Stjr } 77682746Sdillon } else { 777170307Sjeff PROC_SLOCK(p); 778140832Ssobomax *oitv = p->p_stats->p_timer[which]; 779140832Ssobomax p->p_stats->p_timer[which] = *aitv; 780170307Sjeff PROC_SUNLOCK(p); 78182746Sdillon } 782140832Ssobomax return (0); 7831541Srgrimes} 7841541Srgrimes 7851541Srgrimes/* 7861541Srgrimes * Real interval timer expired: 7871541Srgrimes * send process whose timer expired an alarm signal. 7881541Srgrimes * If time is not set up to reload, then just return. 7891541Srgrimes * Else compute next time timer should go off which is > current time. 7901541Srgrimes * This is where delay in processing this timeout causes multiple 7911541Srgrimes * SIGALRM calls to be compressed into one. 79236127Sbde * tvtohz() always adds 1 to allow for the time until the next clock 7939327Sbde * interrupt being strictly less than 1 clock tick, but we don't want 7949327Sbde * that here since we want to appear to be in sync with the clock 7959327Sbde * interrupt even when we're delayed. 7961541Srgrimes */ 7971541Srgrimesvoid 798102074Sphkrealitexpire(void *arg) 7991541Srgrimes{ 800102074Sphk struct proc *p; 801247903Smav struct timeval ctv; 802247903Smav sbintime_t isbt; 8031541Srgrimes 8041541Srgrimes p = (struct proc *)arg; 805225617Skmacy kern_psignal(p, SIGALRM); 80635058Sphk if (!timevalisset(&p->p_realtimer.it_interval)) { 80735058Sphk timevalclear(&p->p_realtimer.it_value); 808116123Sjhb if (p->p_flag & P_WEXIT) 809116123Sjhb wakeup(&p->p_itcallout); 8101541Srgrimes return; 8111541Srgrimes } 812247903Smav isbt = tvtosbt(p->p_realtimer.it_interval); 813247903Smav if (isbt >= sbt_timethreshold) 814247903Smav getmicrouptime(&ctv); 815247903Smav else 816247903Smav microuptime(&ctv); 817247903Smav do { 8181541Srgrimes timevaladd(&p->p_realtimer.it_value, 8191541Srgrimes &p->p_realtimer.it_interval); 820247903Smav } while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=)); 821247903Smav callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value), 822247903Smav isbt >> tc_precexp, realitexpire, p, C_ABSOLUTE); 8231541Srgrimes} 8241541Srgrimes 8251541Srgrimes/* 8261541Srgrimes * Check that a proposed value to load into the .it_value or 8271541Srgrimes * .it_interval part of an interval timer is acceptable, and 8281541Srgrimes * fix it to have at least minimal value (i.e. if it is less 8291541Srgrimes * than the resolution of the clock, round it up.) 8301541Srgrimes */ 8311549Srgrimesint 832102074Sphkitimerfix(struct timeval *tv) 8331541Srgrimes{ 8341541Srgrimes 835151576Sdavidxu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 8361541Srgrimes return (EINVAL); 837247903Smav if (tv->tv_sec == 0 && tv->tv_usec != 0 && 838247903Smav tv->tv_usec < (u_int)tick / 16) 839247903Smav tv->tv_usec = (u_int)tick / 16; 8401541Srgrimes return (0); 8411541Srgrimes} 8421541Srgrimes 8431541Srgrimes/* 8441541Srgrimes * Decrement an interval timer by a specified number 8451541Srgrimes * of microseconds, which must be less than a second, 8461541Srgrimes * i.e. < 1000000. If the timer expires, then reload 8471541Srgrimes * it. In this case, carry over (usec - old value) to 8481541Srgrimes * reduce the value reloaded into the timer so that 8491541Srgrimes * the timer does not drift. This routine assumes 8501541Srgrimes * that it is called in a context where the timers 8511541Srgrimes * on which it is operating cannot change in value. 8521541Srgrimes */ 8531549Srgrimesint 854102074Sphkitimerdecr(struct itimerval *itp, int usec) 8551541Srgrimes{ 8561541Srgrimes 8571541Srgrimes if (itp->it_value.tv_usec < usec) { 8581541Srgrimes if (itp->it_value.tv_sec == 0) { 8591541Srgrimes /* expired, and already in next interval */ 8601541Srgrimes usec -= itp->it_value.tv_usec; 8611541Srgrimes goto expire; 8621541Srgrimes } 8631541Srgrimes itp->it_value.tv_usec += 1000000; 8641541Srgrimes itp->it_value.tv_sec--; 8651541Srgrimes } 8661541Srgrimes itp->it_value.tv_usec -= usec; 8671541Srgrimes usec = 0; 86835058Sphk if (timevalisset(&itp->it_value)) 8691541Srgrimes return (1); 8701541Srgrimes /* expired, exactly at end of interval */ 8711541Srgrimesexpire: 87235058Sphk if (timevalisset(&itp->it_interval)) { 8731541Srgrimes itp->it_value = itp->it_interval; 8741541Srgrimes itp->it_value.tv_usec -= usec; 8751541Srgrimes if (itp->it_value.tv_usec < 0) { 8761541Srgrimes itp->it_value.tv_usec += 1000000; 8771541Srgrimes itp->it_value.tv_sec--; 8781541Srgrimes } 8791541Srgrimes } else 8801541Srgrimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 8811541Srgrimes return (0); 8821541Srgrimes} 8831541Srgrimes 8841541Srgrimes/* 8851541Srgrimes * Add and subtract routines for timevals. 8861541Srgrimes * N.B.: subtract routine doesn't deal with 8871541Srgrimes * results which are before the beginning, 8881541Srgrimes * it just gets very confused in this case. 8891541Srgrimes * Caveat emptor. 8901541Srgrimes */ 8911549Srgrimesvoid 892121523Salfredtimevaladd(struct timeval *t1, const struct timeval *t2) 8931541Srgrimes{ 8941541Srgrimes 8951541Srgrimes t1->tv_sec += t2->tv_sec; 8961541Srgrimes t1->tv_usec += t2->tv_usec; 8971541Srgrimes timevalfix(t1); 8981541Srgrimes} 8991541Srgrimes 9001549Srgrimesvoid 901121523Salfredtimevalsub(struct timeval *t1, const struct timeval *t2) 9021541Srgrimes{ 9031541Srgrimes 9041541Srgrimes t1->tv_sec -= t2->tv_sec; 9051541Srgrimes t1->tv_usec -= t2->tv_usec; 9061541Srgrimes timevalfix(t1); 9071541Srgrimes} 9081541Srgrimes 90912819Sphkstatic void 910102074Sphktimevalfix(struct timeval *t1) 9111541Srgrimes{ 9121541Srgrimes 9131541Srgrimes if (t1->tv_usec < 0) { 9141541Srgrimes t1->tv_sec--; 9151541Srgrimes t1->tv_usec += 1000000; 9161541Srgrimes } 9171541Srgrimes if (t1->tv_usec >= 1000000) { 9181541Srgrimes t1->tv_sec++; 9191541Srgrimes t1->tv_usec -= 1000000; 9201541Srgrimes } 9211541Srgrimes} 922108142Ssam 923108142Ssam/* 924108511Ssam * ratecheck(): simple time-based rate-limit checking. 925108142Ssam */ 926108142Ssamint 927108142Ssamratecheck(struct timeval *lasttime, const struct timeval *mininterval) 928108142Ssam{ 929108142Ssam struct timeval tv, delta; 930108142Ssam int rv = 0; 931108142Ssam 932108511Ssam getmicrouptime(&tv); /* NB: 10ms precision */ 933108511Ssam delta = tv; 934108511Ssam timevalsub(&delta, lasttime); 935108142Ssam 936108142Ssam /* 937108142Ssam * check for 0,0 is so that the message will be seen at least once, 938108142Ssam * even if interval is huge. 939108142Ssam */ 940108142Ssam if (timevalcmp(&delta, mininterval, >=) || 941108142Ssam (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 942108142Ssam *lasttime = tv; 943108142Ssam rv = 1; 944108142Ssam } 945108142Ssam 946108142Ssam return (rv); 947108142Ssam} 948108142Ssam 949108142Ssam/* 950108142Ssam * ppsratecheck(): packets (or events) per second limitation. 951108511Ssam * 952108511Ssam * Return 0 if the limit is to be enforced (e.g. the caller 953108511Ssam * should drop a packet because of the rate limitation). 954108511Ssam * 955111558Ssam * maxpps of 0 always causes zero to be returned. maxpps of -1 956111558Ssam * always causes 1 to be returned; this effectively defeats rate 957111558Ssam * limiting. 958111558Ssam * 959108511Ssam * Note that we maintain the struct timeval for compatibility 960108511Ssam * with other bsd systems. We reuse the storage and just monitor 961108511Ssam * clock ticks for minimal overhead. 962108142Ssam */ 963108142Ssamint 964108142Ssamppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 965108142Ssam{ 966108511Ssam int now; 967108142Ssam 968108142Ssam /* 969108511Ssam * Reset the last time and counter if this is the first call 970108511Ssam * or more than a second has passed since the last update of 971108511Ssam * lasttime. 972108142Ssam */ 973108511Ssam now = ticks; 974108511Ssam if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 975108511Ssam lasttime->tv_sec = now; 976108511Ssam *curpps = 1; 977111558Ssam return (maxpps != 0); 978108511Ssam } else { 979108511Ssam (*curpps)++; /* NB: ignore potential overflow */ 980108511Ssam return (maxpps < 0 || *curpps < maxpps); 981108511Ssam } 982108142Ssam} 983151576Sdavidxu 984151576Sdavidxustatic void 985151576Sdavidxuitimer_start(void) 986151576Sdavidxu{ 987151576Sdavidxu struct kclock rt_clock = { 988151576Sdavidxu .timer_create = realtimer_create, 989151576Sdavidxu .timer_delete = realtimer_delete, 990151576Sdavidxu .timer_settime = realtimer_settime, 991151576Sdavidxu .timer_gettime = realtimer_gettime, 992164713Sdavidxu .event_hook = NULL 993151576Sdavidxu }; 994151576Sdavidxu 995151576Sdavidxu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 996151576Sdavidxu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 997151576Sdavidxu register_posix_clock(CLOCK_REALTIME, &rt_clock); 998151576Sdavidxu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 999152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 1000152983Sdavidxu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 1001152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 1002161302Snetchild EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 1003153259Sdavidxu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 1004161302Snetchild EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 1005153259Sdavidxu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 1006151576Sdavidxu} 1007151576Sdavidxu 1008151576Sdavidxuint 1009151576Sdavidxuregister_posix_clock(int clockid, struct kclock *clk) 1010151576Sdavidxu{ 1011151576Sdavidxu if ((unsigned)clockid >= MAX_CLOCKS) { 1012151576Sdavidxu printf("%s: invalid clockid\n", __func__); 1013151576Sdavidxu return (0); 1014151576Sdavidxu } 1015151576Sdavidxu posix_clocks[clockid] = *clk; 1016151576Sdavidxu return (1); 1017151576Sdavidxu} 1018151576Sdavidxu 1019151576Sdavidxustatic int 1020151576Sdavidxuitimer_init(void *mem, int size, int flags) 1021151576Sdavidxu{ 1022151576Sdavidxu struct itimer *it; 1023151576Sdavidxu 1024151576Sdavidxu it = (struct itimer *)mem; 1025151576Sdavidxu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 1026151576Sdavidxu return (0); 1027151576Sdavidxu} 1028151576Sdavidxu 1029151576Sdavidxustatic void 1030151576Sdavidxuitimer_fini(void *mem, int size) 1031151576Sdavidxu{ 1032151576Sdavidxu struct itimer *it; 1033151576Sdavidxu 1034151576Sdavidxu it = (struct itimer *)mem; 1035151576Sdavidxu mtx_destroy(&it->it_mtx); 1036151576Sdavidxu} 1037151576Sdavidxu 1038151576Sdavidxustatic void 1039151576Sdavidxuitimer_enter(struct itimer *it) 1040151576Sdavidxu{ 1041151576Sdavidxu 1042151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1043151576Sdavidxu it->it_usecount++; 1044151576Sdavidxu} 1045151576Sdavidxu 1046151576Sdavidxustatic void 1047151576Sdavidxuitimer_leave(struct itimer *it) 1048151576Sdavidxu{ 1049151576Sdavidxu 1050151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1051151576Sdavidxu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 1052151576Sdavidxu 1053151576Sdavidxu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 1054151576Sdavidxu wakeup(it); 1055151576Sdavidxu} 1056151576Sdavidxu 1057151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1058156134Sdavidxustruct ktimer_create_args { 1059151576Sdavidxu clockid_t clock_id; 1060151576Sdavidxu struct sigevent * evp; 1061156134Sdavidxu int * timerid; 1062151576Sdavidxu}; 1063151576Sdavidxu#endif 1064151576Sdavidxuint 1065225617Skmacysys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 1066151576Sdavidxu{ 1067151576Sdavidxu struct sigevent *evp1, ev; 1068156134Sdavidxu int id; 1069151576Sdavidxu int error; 1070151576Sdavidxu 1071151576Sdavidxu if (uap->evp != NULL) { 1072151576Sdavidxu error = copyin(uap->evp, &ev, sizeof(ev)); 1073151576Sdavidxu if (error != 0) 1074151576Sdavidxu return (error); 1075151576Sdavidxu evp1 = &ev; 1076151576Sdavidxu } else 1077151576Sdavidxu evp1 = NULL; 1078151576Sdavidxu 1079151576Sdavidxu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 1080151576Sdavidxu 1081151576Sdavidxu if (error == 0) { 1082156134Sdavidxu error = copyout(&id, uap->timerid, sizeof(int)); 1083151576Sdavidxu if (error != 0) 1084151576Sdavidxu kern_timer_delete(td, id); 1085151576Sdavidxu } 1086151576Sdavidxu return (error); 1087151576Sdavidxu} 1088151576Sdavidxu 1089151576Sdavidxustatic int 1090151576Sdavidxukern_timer_create(struct thread *td, clockid_t clock_id, 1091156134Sdavidxu struct sigevent *evp, int *timerid, int preset_id) 1092151576Sdavidxu{ 1093151576Sdavidxu struct proc *p = td->td_proc; 1094151576Sdavidxu struct itimer *it; 1095151576Sdavidxu int id; 1096151576Sdavidxu int error; 1097151576Sdavidxu 1098151576Sdavidxu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 1099151576Sdavidxu return (EINVAL); 1100151576Sdavidxu 1101151576Sdavidxu if (posix_clocks[clock_id].timer_create == NULL) 1102151576Sdavidxu return (EINVAL); 1103151576Sdavidxu 1104151576Sdavidxu if (evp != NULL) { 1105151576Sdavidxu if (evp->sigev_notify != SIGEV_NONE && 1106151869Sdavidxu evp->sigev_notify != SIGEV_SIGNAL && 1107151869Sdavidxu evp->sigev_notify != SIGEV_THREAD_ID) 1108151576Sdavidxu return (EINVAL); 1109151869Sdavidxu if ((evp->sigev_notify == SIGEV_SIGNAL || 1110151869Sdavidxu evp->sigev_notify == SIGEV_THREAD_ID) && 1111151576Sdavidxu !_SIG_VALID(evp->sigev_signo)) 1112151576Sdavidxu return (EINVAL); 1113151576Sdavidxu } 1114151576Sdavidxu 1115151585Sdavidxu if (p->p_itimers == NULL) 1116151576Sdavidxu itimers_alloc(p); 1117151576Sdavidxu 1118151576Sdavidxu it = uma_zalloc(itimer_zone, M_WAITOK); 1119151576Sdavidxu it->it_flags = 0; 1120151576Sdavidxu it->it_usecount = 0; 1121151576Sdavidxu it->it_active = 0; 1122151869Sdavidxu timespecclear(&it->it_time.it_value); 1123151869Sdavidxu timespecclear(&it->it_time.it_interval); 1124151576Sdavidxu it->it_overrun = 0; 1125151576Sdavidxu it->it_overrun_last = 0; 1126151576Sdavidxu it->it_clockid = clock_id; 1127151576Sdavidxu it->it_timerid = -1; 1128151576Sdavidxu it->it_proc = p; 1129151576Sdavidxu ksiginfo_init(&it->it_ksi); 1130151576Sdavidxu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 1131151576Sdavidxu error = CLOCK_CALL(clock_id, timer_create, (it)); 1132151576Sdavidxu if (error != 0) 1133151576Sdavidxu goto out; 1134151576Sdavidxu 1135151576Sdavidxu PROC_LOCK(p); 1136151576Sdavidxu if (preset_id != -1) { 1137151576Sdavidxu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 1138151576Sdavidxu id = preset_id; 1139151585Sdavidxu if (p->p_itimers->its_timers[id] != NULL) { 1140151576Sdavidxu PROC_UNLOCK(p); 1141151576Sdavidxu error = 0; 1142151576Sdavidxu goto out; 1143151576Sdavidxu } 1144151576Sdavidxu } else { 1145151576Sdavidxu /* 1146151576Sdavidxu * Find a free timer slot, skipping those reserved 1147151576Sdavidxu * for setitimer(). 1148151576Sdavidxu */ 1149151576Sdavidxu for (id = 3; id < TIMER_MAX; id++) 1150151585Sdavidxu if (p->p_itimers->its_timers[id] == NULL) 1151151576Sdavidxu break; 1152151576Sdavidxu if (id == TIMER_MAX) { 1153151576Sdavidxu PROC_UNLOCK(p); 1154151576Sdavidxu error = EAGAIN; 1155151576Sdavidxu goto out; 1156151576Sdavidxu } 1157151576Sdavidxu } 1158151576Sdavidxu it->it_timerid = id; 1159151585Sdavidxu p->p_itimers->its_timers[id] = it; 1160151576Sdavidxu if (evp != NULL) 1161151576Sdavidxu it->it_sigev = *evp; 1162151576Sdavidxu else { 1163151576Sdavidxu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 1164151576Sdavidxu switch (clock_id) { 1165151576Sdavidxu default: 1166151576Sdavidxu case CLOCK_REALTIME: 1167151576Sdavidxu it->it_sigev.sigev_signo = SIGALRM; 1168151576Sdavidxu break; 1169151576Sdavidxu case CLOCK_VIRTUAL: 1170151576Sdavidxu it->it_sigev.sigev_signo = SIGVTALRM; 1171151576Sdavidxu break; 1172151576Sdavidxu case CLOCK_PROF: 1173151576Sdavidxu it->it_sigev.sigev_signo = SIGPROF; 1174151576Sdavidxu break; 1175151576Sdavidxu } 1176152029Sdavidxu it->it_sigev.sigev_value.sival_int = id; 1177151576Sdavidxu } 1178151576Sdavidxu 1179151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1180151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1181151576Sdavidxu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 1182151576Sdavidxu it->it_ksi.ksi_code = SI_TIMER; 1183151576Sdavidxu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 1184151576Sdavidxu it->it_ksi.ksi_timerid = id; 1185151576Sdavidxu } 1186151576Sdavidxu PROC_UNLOCK(p); 1187151576Sdavidxu *timerid = id; 1188151576Sdavidxu return (0); 1189151576Sdavidxu 1190151576Sdavidxuout: 1191151576Sdavidxu ITIMER_LOCK(it); 1192151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1193151576Sdavidxu ITIMER_UNLOCK(it); 1194151576Sdavidxu uma_zfree(itimer_zone, it); 1195151576Sdavidxu return (error); 1196151576Sdavidxu} 1197151576Sdavidxu 1198151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1199156134Sdavidxustruct ktimer_delete_args { 1200156134Sdavidxu int timerid; 1201151576Sdavidxu}; 1202151576Sdavidxu#endif 1203151576Sdavidxuint 1204225617Skmacysys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 1205151576Sdavidxu{ 1206151576Sdavidxu return (kern_timer_delete(td, uap->timerid)); 1207151576Sdavidxu} 1208151576Sdavidxu 1209151576Sdavidxustatic struct itimer * 1210164713Sdavidxuitimer_find(struct proc *p, int timerid) 1211151576Sdavidxu{ 1212151576Sdavidxu struct itimer *it; 1213151576Sdavidxu 1214151576Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1215190301Scperciva if ((p->p_itimers == NULL) || 1216190301Scperciva (timerid < 0) || (timerid >= TIMER_MAX) || 1217151585Sdavidxu (it = p->p_itimers->its_timers[timerid]) == NULL) { 1218151576Sdavidxu return (NULL); 1219151576Sdavidxu } 1220151576Sdavidxu ITIMER_LOCK(it); 1221164713Sdavidxu if ((it->it_flags & ITF_DELETING) != 0) { 1222151576Sdavidxu ITIMER_UNLOCK(it); 1223151576Sdavidxu it = NULL; 1224151576Sdavidxu } 1225151576Sdavidxu return (it); 1226151576Sdavidxu} 1227151576Sdavidxu 1228151576Sdavidxustatic int 1229156134Sdavidxukern_timer_delete(struct thread *td, int timerid) 1230151576Sdavidxu{ 1231151576Sdavidxu struct proc *p = td->td_proc; 1232151576Sdavidxu struct itimer *it; 1233151576Sdavidxu 1234151576Sdavidxu PROC_LOCK(p); 1235164713Sdavidxu it = itimer_find(p, timerid); 1236151576Sdavidxu if (it == NULL) { 1237151576Sdavidxu PROC_UNLOCK(p); 1238151576Sdavidxu return (EINVAL); 1239151576Sdavidxu } 1240151576Sdavidxu PROC_UNLOCK(p); 1241151576Sdavidxu 1242151576Sdavidxu it->it_flags |= ITF_DELETING; 1243151576Sdavidxu while (it->it_usecount > 0) { 1244151576Sdavidxu it->it_flags |= ITF_WANTED; 1245151576Sdavidxu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 1246151576Sdavidxu } 1247151576Sdavidxu it->it_flags &= ~ITF_WANTED; 1248151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1249151576Sdavidxu ITIMER_UNLOCK(it); 1250151576Sdavidxu 1251151576Sdavidxu PROC_LOCK(p); 1252151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) 1253151576Sdavidxu sigqueue_take(&it->it_ksi); 1254151585Sdavidxu p->p_itimers->its_timers[timerid] = NULL; 1255151576Sdavidxu PROC_UNLOCK(p); 1256151576Sdavidxu uma_zfree(itimer_zone, it); 1257151576Sdavidxu return (0); 1258151576Sdavidxu} 1259151576Sdavidxu 1260151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1261156134Sdavidxustruct ktimer_settime_args { 1262156134Sdavidxu int timerid; 1263151576Sdavidxu int flags; 1264151576Sdavidxu const struct itimerspec * value; 1265151576Sdavidxu struct itimerspec * ovalue; 1266151576Sdavidxu}; 1267151576Sdavidxu#endif 1268151576Sdavidxuint 1269225617Skmacysys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 1270151576Sdavidxu{ 1271151576Sdavidxu struct proc *p = td->td_proc; 1272151576Sdavidxu struct itimer *it; 1273151576Sdavidxu struct itimerspec val, oval, *ovalp; 1274151576Sdavidxu int error; 1275151576Sdavidxu 1276151576Sdavidxu error = copyin(uap->value, &val, sizeof(val)); 1277151576Sdavidxu if (error != 0) 1278151576Sdavidxu return (error); 1279151576Sdavidxu 1280151576Sdavidxu if (uap->ovalue != NULL) 1281151576Sdavidxu ovalp = &oval; 1282151576Sdavidxu else 1283151576Sdavidxu ovalp = NULL; 1284151576Sdavidxu 1285151576Sdavidxu PROC_LOCK(p); 1286151576Sdavidxu if (uap->timerid < 3 || 1287164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1288151576Sdavidxu PROC_UNLOCK(p); 1289151576Sdavidxu error = EINVAL; 1290151576Sdavidxu } else { 1291151576Sdavidxu PROC_UNLOCK(p); 1292151576Sdavidxu itimer_enter(it); 1293151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_settime, 1294151576Sdavidxu (it, uap->flags, &val, ovalp)); 1295151576Sdavidxu itimer_leave(it); 1296151576Sdavidxu ITIMER_UNLOCK(it); 1297151576Sdavidxu } 1298151576Sdavidxu if (error == 0 && uap->ovalue != NULL) 1299151576Sdavidxu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1300151576Sdavidxu return (error); 1301151576Sdavidxu} 1302151576Sdavidxu 1303151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1304156134Sdavidxustruct ktimer_gettime_args { 1305156134Sdavidxu int timerid; 1306151576Sdavidxu struct itimerspec * value; 1307151576Sdavidxu}; 1308151576Sdavidxu#endif 1309151576Sdavidxuint 1310225617Skmacysys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 1311151576Sdavidxu{ 1312151576Sdavidxu struct proc *p = td->td_proc; 1313151576Sdavidxu struct itimer *it; 1314151576Sdavidxu struct itimerspec val; 1315151576Sdavidxu int error; 1316151576Sdavidxu 1317151576Sdavidxu PROC_LOCK(p); 1318151576Sdavidxu if (uap->timerid < 3 || 1319164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1320151576Sdavidxu PROC_UNLOCK(p); 1321151576Sdavidxu error = EINVAL; 1322151576Sdavidxu } else { 1323151576Sdavidxu PROC_UNLOCK(p); 1324151576Sdavidxu itimer_enter(it); 1325151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_gettime, 1326151576Sdavidxu (it, &val)); 1327151576Sdavidxu itimer_leave(it); 1328151576Sdavidxu ITIMER_UNLOCK(it); 1329151576Sdavidxu } 1330151576Sdavidxu if (error == 0) 1331151576Sdavidxu error = copyout(&val, uap->value, sizeof(val)); 1332151576Sdavidxu return (error); 1333151576Sdavidxu} 1334151576Sdavidxu 1335151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1336151576Sdavidxustruct timer_getoverrun_args { 1337156134Sdavidxu int timerid; 1338151576Sdavidxu}; 1339151576Sdavidxu#endif 1340151576Sdavidxuint 1341225617Skmacysys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 1342151576Sdavidxu{ 1343151576Sdavidxu struct proc *p = td->td_proc; 1344151576Sdavidxu struct itimer *it; 1345151576Sdavidxu int error ; 1346151576Sdavidxu 1347151576Sdavidxu PROC_LOCK(p); 1348151576Sdavidxu if (uap->timerid < 3 || 1349164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1350151576Sdavidxu PROC_UNLOCK(p); 1351151576Sdavidxu error = EINVAL; 1352151576Sdavidxu } else { 1353151576Sdavidxu td->td_retval[0] = it->it_overrun_last; 1354151576Sdavidxu ITIMER_UNLOCK(it); 1355151869Sdavidxu PROC_UNLOCK(p); 1356151576Sdavidxu error = 0; 1357151576Sdavidxu } 1358151576Sdavidxu return (error); 1359151576Sdavidxu} 1360151576Sdavidxu 1361151576Sdavidxustatic int 1362151576Sdavidxurealtimer_create(struct itimer *it) 1363151576Sdavidxu{ 1364151576Sdavidxu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 1365151576Sdavidxu return (0); 1366151576Sdavidxu} 1367151576Sdavidxu 1368151576Sdavidxustatic int 1369151576Sdavidxurealtimer_delete(struct itimer *it) 1370151576Sdavidxu{ 1371151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1372164713Sdavidxu 1373184067Sdavidxu /* 1374184067Sdavidxu * clear timer's value and interval to tell realtimer_expire 1375184067Sdavidxu * to not rearm the timer. 1376184067Sdavidxu */ 1377184067Sdavidxu timespecclear(&it->it_time.it_value); 1378184067Sdavidxu timespecclear(&it->it_time.it_interval); 1379164713Sdavidxu ITIMER_UNLOCK(it); 1380164713Sdavidxu callout_drain(&it->it_callout); 1381164713Sdavidxu ITIMER_LOCK(it); 1382151576Sdavidxu return (0); 1383151576Sdavidxu} 1384151576Sdavidxu 1385151576Sdavidxustatic int 1386151576Sdavidxurealtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 1387151576Sdavidxu{ 1388151869Sdavidxu struct timespec cts; 1389151576Sdavidxu 1390151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1391151576Sdavidxu 1392151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1393151869Sdavidxu *ovalue = it->it_time; 1394151576Sdavidxu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 1395151869Sdavidxu timespecsub(&ovalue->it_value, &cts); 1396151576Sdavidxu if (ovalue->it_value.tv_sec < 0 || 1397151576Sdavidxu (ovalue->it_value.tv_sec == 0 && 1398151576Sdavidxu ovalue->it_value.tv_nsec == 0)) { 1399151576Sdavidxu ovalue->it_value.tv_sec = 0; 1400151576Sdavidxu ovalue->it_value.tv_nsec = 1; 1401151576Sdavidxu } 1402151576Sdavidxu } 1403151576Sdavidxu return (0); 1404151576Sdavidxu} 1405151576Sdavidxu 1406151576Sdavidxustatic int 1407151576Sdavidxurealtimer_settime(struct itimer *it, int flags, 1408151576Sdavidxu struct itimerspec *value, struct itimerspec *ovalue) 1409151576Sdavidxu{ 1410151869Sdavidxu struct timespec cts, ts; 1411151869Sdavidxu struct timeval tv; 1412151869Sdavidxu struct itimerspec val; 1413151576Sdavidxu 1414151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1415151576Sdavidxu 1416151869Sdavidxu val = *value; 1417151869Sdavidxu if (itimespecfix(&val.it_value)) 1418151576Sdavidxu return (EINVAL); 1419151576Sdavidxu 1420151869Sdavidxu if (timespecisset(&val.it_value)) { 1421151869Sdavidxu if (itimespecfix(&val.it_interval)) 1422151576Sdavidxu return (EINVAL); 1423151576Sdavidxu } else { 1424151869Sdavidxu timespecclear(&val.it_interval); 1425151576Sdavidxu } 1426151576Sdavidxu 1427151576Sdavidxu if (ovalue != NULL) 1428151576Sdavidxu realtimer_gettime(it, ovalue); 1429151576Sdavidxu 1430151576Sdavidxu it->it_time = val; 1431151869Sdavidxu if (timespecisset(&val.it_value)) { 1432151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1433151869Sdavidxu ts = val.it_value; 1434151576Sdavidxu if ((flags & TIMER_ABSTIME) == 0) { 1435151576Sdavidxu /* Convert to absolute time. */ 1436151869Sdavidxu timespecadd(&it->it_time.it_value, &cts); 1437151576Sdavidxu } else { 1438151869Sdavidxu timespecsub(&ts, &cts); 1439151576Sdavidxu /* 1440151869Sdavidxu * We don't care if ts is negative, tztohz will 1441151576Sdavidxu * fix it. 1442151576Sdavidxu */ 1443151576Sdavidxu } 1444151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1445151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1446151576Sdavidxu realtimer_expire, it); 1447151576Sdavidxu } else { 1448151576Sdavidxu callout_stop(&it->it_callout); 1449151576Sdavidxu } 1450151576Sdavidxu 1451151576Sdavidxu return (0); 1452151576Sdavidxu} 1453151576Sdavidxu 1454151576Sdavidxustatic void 1455151869Sdavidxurealtimer_clocktime(clockid_t id, struct timespec *ts) 1456151576Sdavidxu{ 1457151576Sdavidxu if (id == CLOCK_REALTIME) 1458151869Sdavidxu getnanotime(ts); 1459151576Sdavidxu else /* CLOCK_MONOTONIC */ 1460151869Sdavidxu getnanouptime(ts); 1461151576Sdavidxu} 1462151576Sdavidxu 1463151869Sdavidxuint 1464156134Sdavidxuitimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 1465151869Sdavidxu{ 1466151869Sdavidxu struct itimer *it; 1467151869Sdavidxu 1468151869Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1469164713Sdavidxu it = itimer_find(p, timerid); 1470151869Sdavidxu if (it != NULL) { 1471151869Sdavidxu ksi->ksi_overrun = it->it_overrun; 1472151869Sdavidxu it->it_overrun_last = it->it_overrun; 1473151869Sdavidxu it->it_overrun = 0; 1474151869Sdavidxu ITIMER_UNLOCK(it); 1475151869Sdavidxu return (0); 1476151869Sdavidxu } 1477151869Sdavidxu return (EINVAL); 1478151869Sdavidxu} 1479151869Sdavidxu 1480151869Sdavidxuint 1481151869Sdavidxuitimespecfix(struct timespec *ts) 1482151869Sdavidxu{ 1483151869Sdavidxu 1484151869Sdavidxu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1485151869Sdavidxu return (EINVAL); 1486151869Sdavidxu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 1487151869Sdavidxu ts->tv_nsec = tick * 1000; 1488151869Sdavidxu return (0); 1489151869Sdavidxu} 1490151869Sdavidxu 1491151576Sdavidxu/* Timeout callback for realtime timer */ 1492151576Sdavidxustatic void 1493151576Sdavidxurealtimer_expire(void *arg) 1494151576Sdavidxu{ 1495151869Sdavidxu struct timespec cts, ts; 1496151869Sdavidxu struct timeval tv; 1497151576Sdavidxu struct itimer *it; 1498151576Sdavidxu 1499151576Sdavidxu it = (struct itimer *)arg; 1500151576Sdavidxu 1501151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1502151576Sdavidxu /* Only fire if time is reached. */ 1503151869Sdavidxu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1504151869Sdavidxu if (timespecisset(&it->it_time.it_interval)) { 1505151869Sdavidxu timespecadd(&it->it_time.it_value, 1506151869Sdavidxu &it->it_time.it_interval); 1507151869Sdavidxu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1508152983Sdavidxu if (it->it_overrun < INT_MAX) 1509152983Sdavidxu it->it_overrun++; 1510152983Sdavidxu else 1511152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1512151869Sdavidxu timespecadd(&it->it_time.it_value, 1513151869Sdavidxu &it->it_time.it_interval); 1514151576Sdavidxu } 1515151576Sdavidxu } else { 1516151576Sdavidxu /* single shot timer ? */ 1517151869Sdavidxu timespecclear(&it->it_time.it_value); 1518151576Sdavidxu } 1519151869Sdavidxu if (timespecisset(&it->it_time.it_value)) { 1520151869Sdavidxu ts = it->it_time.it_value; 1521151869Sdavidxu timespecsub(&ts, &cts); 1522151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1523151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1524151576Sdavidxu realtimer_expire, it); 1525151576Sdavidxu } 1526184067Sdavidxu itimer_enter(it); 1527151576Sdavidxu ITIMER_UNLOCK(it); 1528151576Sdavidxu itimer_fire(it); 1529151576Sdavidxu ITIMER_LOCK(it); 1530184067Sdavidxu itimer_leave(it); 1531151869Sdavidxu } else if (timespecisset(&it->it_time.it_value)) { 1532151869Sdavidxu ts = it->it_time.it_value; 1533151869Sdavidxu timespecsub(&ts, &cts); 1534151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1535151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 1536151576Sdavidxu it); 1537151576Sdavidxu } 1538151576Sdavidxu} 1539151576Sdavidxu 1540151576Sdavidxuvoid 1541151576Sdavidxuitimer_fire(struct itimer *it) 1542151576Sdavidxu{ 1543151576Sdavidxu struct proc *p = it->it_proc; 1544213642Sdavidxu struct thread *td; 1545151576Sdavidxu 1546151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1547151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1548213642Sdavidxu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 1549213642Sdavidxu ITIMER_LOCK(it); 1550213642Sdavidxu timespecclear(&it->it_time.it_value); 1551213642Sdavidxu timespecclear(&it->it_time.it_interval); 1552213642Sdavidxu callout_stop(&it->it_callout); 1553213642Sdavidxu ITIMER_UNLOCK(it); 1554213642Sdavidxu return; 1555213642Sdavidxu } 1556151993Sdavidxu if (!KSI_ONQ(&it->it_ksi)) { 1557152983Sdavidxu it->it_ksi.ksi_errno = 0; 1558213642Sdavidxu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1559213642Sdavidxu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 1560151993Sdavidxu } else { 1561152983Sdavidxu if (it->it_overrun < INT_MAX) 1562152983Sdavidxu it->it_overrun++; 1563152983Sdavidxu else 1564152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1565151576Sdavidxu } 1566151576Sdavidxu PROC_UNLOCK(p); 1567151576Sdavidxu } 1568151576Sdavidxu} 1569151576Sdavidxu 1570151576Sdavidxustatic void 1571151576Sdavidxuitimers_alloc(struct proc *p) 1572151576Sdavidxu{ 1573151585Sdavidxu struct itimers *its; 1574151585Sdavidxu int i; 1575151576Sdavidxu 1576151585Sdavidxu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 1577151585Sdavidxu LIST_INIT(&its->its_virtual); 1578151585Sdavidxu LIST_INIT(&its->its_prof); 1579151585Sdavidxu TAILQ_INIT(&its->its_worklist); 1580151585Sdavidxu for (i = 0; i < TIMER_MAX; i++) 1581151585Sdavidxu its->its_timers[i] = NULL; 1582151576Sdavidxu PROC_LOCK(p); 1583151585Sdavidxu if (p->p_itimers == NULL) { 1584151585Sdavidxu p->p_itimers = its; 1585151576Sdavidxu PROC_UNLOCK(p); 1586151585Sdavidxu } 1587151585Sdavidxu else { 1588151576Sdavidxu PROC_UNLOCK(p); 1589151585Sdavidxu free(its, M_SUBPROC); 1590151576Sdavidxu } 1591151576Sdavidxu} 1592151576Sdavidxu 1593161302Snetchildstatic void 1594161302Snetchilditimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1595161302Snetchild{ 1596164713Sdavidxu itimers_event_hook_exit(arg, p); 1597161302Snetchild} 1598161302Snetchild 1599151576Sdavidxu/* Clean up timers when some process events are being triggered. */ 1600153259Sdavidxustatic void 1601161302Snetchilditimers_event_hook_exit(void *arg, struct proc *p) 1602151576Sdavidxu{ 1603151576Sdavidxu struct itimers *its; 1604151576Sdavidxu struct itimer *it; 1605153267Sdavidxu int event = (int)(intptr_t)arg; 1606151576Sdavidxu int i; 1607151576Sdavidxu 1608151585Sdavidxu if (p->p_itimers != NULL) { 1609151585Sdavidxu its = p->p_itimers; 1610151576Sdavidxu for (i = 0; i < MAX_CLOCKS; ++i) { 1611151576Sdavidxu if (posix_clocks[i].event_hook != NULL) 1612151576Sdavidxu CLOCK_CALL(i, event_hook, (p, i, event)); 1613151576Sdavidxu } 1614151576Sdavidxu /* 1615151576Sdavidxu * According to susv3, XSI interval timers should be inherited 1616151576Sdavidxu * by new image. 1617151576Sdavidxu */ 1618151576Sdavidxu if (event == ITIMER_EV_EXEC) 1619151576Sdavidxu i = 3; 1620151576Sdavidxu else if (event == ITIMER_EV_EXIT) 1621151576Sdavidxu i = 0; 1622151576Sdavidxu else 1623151576Sdavidxu panic("unhandled event"); 1624151576Sdavidxu for (; i < TIMER_MAX; ++i) { 1625164713Sdavidxu if ((it = its->its_timers[i]) != NULL) 1626164713Sdavidxu kern_timer_delete(curthread, i); 1627151576Sdavidxu } 1628151576Sdavidxu if (its->its_timers[0] == NULL && 1629151576Sdavidxu its->its_timers[1] == NULL && 1630151576Sdavidxu its->its_timers[2] == NULL) { 1631151585Sdavidxu free(its, M_SUBPROC); 1632151585Sdavidxu p->p_itimers = NULL; 1633151576Sdavidxu } 1634151576Sdavidxu } 1635151576Sdavidxu} 1636