kern_time.c revision 247797
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 247797 2013-03-04 15:57:41Z davide $"); 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 48026335Speterstatic int nanowait; 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; 48735042Sphk int error; 48825583Speter 48928773Sbde if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 49025656Speter return (EINVAL); 49143301Sdillon if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 49228773Sbde return (0); 493247797Sdavide tmp = tstosbt(*rqt); 494247797Sdavide prec = tmp; 495247797Sdavide prec >>= tc_precexp; 496247797Sdavide if (TIMESEL(&sbt, tmp)) 497247797Sdavide sbt += tc_tick_sbt; 498247797Sdavide sbt += tmp; 499247797Sdavide error = tsleep_sbt(&nanowait, PWAIT | PCATCH, "nanslp", sbt, prec, 500247797Sdavide C_ABSOLUTE); 501247797Sdavide if (error != EWOULDBLOCK) { 502247797Sdavide if (error == ERESTART) 503247797Sdavide error = EINTR; 504247797Sdavide TIMESEL(&sbtt, tmp); 505247797Sdavide if (rmt != NULL) { 506247797Sdavide ts = sbttots(sbt - sbtt); 507247797Sdavide if (ts.tv_sec < 0) 508247797Sdavide timespecclear(&ts); 509247797Sdavide *rmt = ts; 51035042Sphk } 511247797Sdavide if (sbtt >= sbt) 51235042Sphk return (0); 513247797Sdavide return (error); 51426335Speter } 515247797Sdavide return (0); 51626335Speter} 51725583Speter 51826335Speter#ifndef _SYS_SYSPROTO_H_ 51926335Speterstruct nanosleep_args { 52026335Speter struct timespec *rqtp; 52126335Speter struct timespec *rmtp; 52226335Speter}; 52326335Speter#endif 52426335Speter/* ARGSUSED */ 52526335Speterint 526225617Skmacysys_nanosleep(struct thread *td, struct nanosleep_args *uap) 52726335Speter{ 52826335Speter struct timespec rmt, rqt; 52982746Sdillon int error; 53026335Speter 531107849Salfred error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 53226335Speter if (error) 53326335Speter return (error); 53482746Sdillon 535109521Salfred if (uap->rmtp && 536109521Salfred !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 537109521Salfred return (EFAULT); 538140481Sps error = kern_nanosleep(td, &rqt, &rmt); 539107849Salfred if (error && uap->rmtp) { 54082746Sdillon int error2; 54182746Sdillon 542107849Salfred error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 543109521Salfred if (error2) 54482746Sdillon error = error2; 54525583Speter } 54625656Speter return (error); 54725583Speter} 54825583Speter 54926335Speter#ifndef _SYS_SYSPROTO_H_ 5501541Srgrimesstruct gettimeofday_args { 5511541Srgrimes struct timeval *tp; 5521541Srgrimes struct timezone *tzp; 5531541Srgrimes}; 55412221Sbde#endif 5551541Srgrimes/* ARGSUSED */ 5561549Srgrimesint 557225617Skmacysys_gettimeofday(struct thread *td, struct gettimeofday_args *uap) 5581541Srgrimes{ 5591541Srgrimes struct timeval atv; 560110286Stjr struct timezone rtz; 5611541Srgrimes int error = 0; 5621541Srgrimes 5631541Srgrimes if (uap->tp) { 5641541Srgrimes microtime(&atv); 56599012Salfred error = copyout(&atv, uap->tp, sizeof (atv)); 5661541Srgrimes } 56790836Sphk if (error == 0 && uap->tzp != NULL) { 568110299Sphk rtz.tz_minuteswest = tz_minuteswest; 569110299Sphk rtz.tz_dsttime = tz_dsttime; 570110286Stjr error = copyout(&rtz, uap->tzp, sizeof (rtz)); 57182746Sdillon } 5721541Srgrimes return (error); 5731541Srgrimes} 5741541Srgrimes 57512221Sbde#ifndef _SYS_SYSPROTO_H_ 5761541Srgrimesstruct settimeofday_args { 5771541Srgrimes struct timeval *tv; 5781541Srgrimes struct timezone *tzp; 5791541Srgrimes}; 58012221Sbde#endif 5811541Srgrimes/* ARGSUSED */ 5821549Srgrimesint 583225617Skmacysys_settimeofday(struct thread *td, struct settimeofday_args *uap) 5841541Srgrimes{ 585144445Sjhb struct timeval atv, *tvp; 586144445Sjhb struct timezone atz, *tzp; 587144445Sjhb int error; 5881541Srgrimes 589144445Sjhb if (uap->tv) { 590144445Sjhb error = copyin(uap->tv, &atv, sizeof(atv)); 591144445Sjhb if (error) 592144445Sjhb return (error); 593144445Sjhb tvp = &atv; 594144445Sjhb } else 595144445Sjhb tvp = NULL; 596144445Sjhb if (uap->tzp) { 597144445Sjhb error = copyin(uap->tzp, &atz, sizeof(atz)); 598144445Sjhb if (error) 599144445Sjhb return (error); 600144445Sjhb tzp = &atz; 601144445Sjhb } else 602144445Sjhb tzp = NULL; 603144445Sjhb return (kern_settimeofday(td, tvp, tzp)); 604144445Sjhb} 605144445Sjhb 606144445Sjhbint 607144445Sjhbkern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp) 608144445Sjhb{ 609144445Sjhb int error; 610144445Sjhb 611164033Srwatson error = priv_check(td, PRIV_SETTIMEOFDAY); 612144445Sjhb if (error) 61394343Sjhb return (error); 6141541Srgrimes /* Verify all parameters before changing time. */ 615144445Sjhb if (tv) { 616144445Sjhb if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 61794343Sjhb return (EINVAL); 618144445Sjhb error = settime(td, tv); 61925656Speter } 620144445Sjhb if (tzp && error == 0) { 621144445Sjhb tz_minuteswest = tzp->tz_minuteswest; 622144445Sjhb tz_dsttime = tzp->tz_dsttime; 62382746Sdillon } 62482746Sdillon return (error); 6251541Srgrimes} 626144445Sjhb 62782746Sdillon/* 628167232Srwatson * Get value of an interval timer. The process virtual and profiling virtual 629167232Srwatson * time timers are kept in the p_stats area, since they can be swapped out. 630167232Srwatson * These are kept internally in the way they are specified externally: in 631167232Srwatson * time until they expire. 6321541Srgrimes * 633167232Srwatson * The real time interval timer is kept in the process table slot for the 634167232Srwatson * process, and its value (it_value) is kept as an absolute time rather than 635167232Srwatson * as a delta, so that it is easy to keep periodic real-time signals from 636167232Srwatson * drifting. 6371541Srgrimes * 6381541Srgrimes * Virtual time timers are processed in the hardclock() routine of 639167232Srwatson * kern_clock.c. The real time timer is processed by a timeout routine, 640167232Srwatson * called from the softclock() routine. Since a callout may be delayed in 641167232Srwatson * real time due to interrupt processing in the system, it is possible for 642167232Srwatson * the real time timeout routine (realitexpire, given below), to be delayed 643167232Srwatson * in real time past when it is supposed to occur. It does not suffice, 644167232Srwatson * therefore, to reload the real timer .it_value from the real time timers 645167232Srwatson * .it_interval. Rather, we compute the next time in absolute time the timer 646167232Srwatson * should go off. 6471541Srgrimes */ 64812221Sbde#ifndef _SYS_SYSPROTO_H_ 6491541Srgrimesstruct getitimer_args { 6501541Srgrimes u_int which; 6511541Srgrimes struct itimerval *itv; 6521541Srgrimes}; 65312221Sbde#endif 6541549Srgrimesint 655225617Skmacysys_getitimer(struct thread *td, struct getitimer_args *uap) 6561541Srgrimes{ 657141470Sjhb struct itimerval aitv; 658140832Ssobomax int error; 659140832Ssobomax 660140832Ssobomax error = kern_getitimer(td, uap->which, &aitv); 661140832Ssobomax if (error != 0) 662140832Ssobomax return (error); 663140832Ssobomax return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 664140832Ssobomax} 665140832Ssobomax 666140832Ssobomaxint 667140832Ssobomaxkern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 668140832Ssobomax{ 66983366Sjulian struct proc *p = td->td_proc; 67034961Sphk struct timeval ctv; 6711541Srgrimes 672140832Ssobomax if (which > ITIMER_PROF) 6731541Srgrimes return (EINVAL); 67482746Sdillon 675140832Ssobomax if (which == ITIMER_REAL) { 6761541Srgrimes /* 67736128Sbde * Convert from absolute to relative time in .it_value 6781541Srgrimes * part of real time timer. If time for real time timer 6791541Srgrimes * has passed return 0, else return difference between 6801541Srgrimes * current time and time for the timer to go off. 6811541Srgrimes */ 682111034Stjr PROC_LOCK(p); 683140832Ssobomax *aitv = p->p_realtimer; 684111034Stjr PROC_UNLOCK(p); 685140832Ssobomax if (timevalisset(&aitv->it_value)) { 68636119Sphk getmicrouptime(&ctv); 687140832Ssobomax if (timevalcmp(&aitv->it_value, &ctv, <)) 688140832Ssobomax timevalclear(&aitv->it_value); 6891541Srgrimes else 690140832Ssobomax timevalsub(&aitv->it_value, &ctv); 69134961Sphk } 69282746Sdillon } else { 693170307Sjeff PROC_SLOCK(p); 694140832Ssobomax *aitv = p->p_stats->p_timer[which]; 695170307Sjeff PROC_SUNLOCK(p); 69682746Sdillon } 697140832Ssobomax return (0); 6981541Srgrimes} 6991541Srgrimes 70012221Sbde#ifndef _SYS_SYSPROTO_H_ 7011541Srgrimesstruct setitimer_args { 7021541Srgrimes u_int which; 7031541Srgrimes struct itimerval *itv, *oitv; 7041541Srgrimes}; 70512221Sbde#endif 7061549Srgrimesint 707225617Skmacysys_setitimer(struct thread *td, struct setitimer_args *uap) 7081541Srgrimes{ 709141470Sjhb struct itimerval aitv, oitv; 710140832Ssobomax int error; 7111541Srgrimes 712111034Stjr if (uap->itv == NULL) { 713111034Stjr uap->itv = uap->oitv; 714225617Skmacy return (sys_getitimer(td, (struct getitimer_args *)uap)); 715111034Stjr } 716111034Stjr 717111034Stjr if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 7181541Srgrimes return (error); 719140832Ssobomax error = kern_setitimer(td, uap->which, &aitv, &oitv); 720140832Ssobomax if (error != 0 || uap->oitv == NULL) 721140832Ssobomax return (error); 722140832Ssobomax return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 723140832Ssobomax} 724140832Ssobomax 725140832Ssobomaxint 726141470Sjhbkern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 727141470Sjhb struct itimerval *oitv) 728140832Ssobomax{ 729140832Ssobomax struct proc *p = td->td_proc; 730140832Ssobomax struct timeval ctv; 731140832Ssobomax 732141483Sjhb if (aitv == NULL) 733141483Sjhb return (kern_getitimer(td, which, oitv)); 734141483Sjhb 735140832Ssobomax if (which > ITIMER_PROF) 736111034Stjr return (EINVAL); 737140832Ssobomax if (itimerfix(&aitv->it_value)) 738111034Stjr return (EINVAL); 739140832Ssobomax if (!timevalisset(&aitv->it_value)) 740140832Ssobomax timevalclear(&aitv->it_interval); 741140832Ssobomax else if (itimerfix(&aitv->it_interval)) 742140832Ssobomax return (EINVAL); 74382746Sdillon 744140832Ssobomax if (which == ITIMER_REAL) { 745111034Stjr PROC_LOCK(p); 74635058Sphk if (timevalisset(&p->p_realtimer.it_value)) 74769286Sjake callout_stop(&p->p_itcallout); 748114980Sjhb getmicrouptime(&ctv); 749140832Ssobomax if (timevalisset(&aitv->it_value)) { 750140832Ssobomax callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 75169286Sjake realitexpire, p); 752140832Ssobomax timevaladd(&aitv->it_value, &ctv); 753114980Sjhb } 754140832Ssobomax *oitv = p->p_realtimer; 755140832Ssobomax p->p_realtimer = *aitv; 756111034Stjr PROC_UNLOCK(p); 757140832Ssobomax if (timevalisset(&oitv->it_value)) { 758140832Ssobomax if (timevalcmp(&oitv->it_value, &ctv, <)) 759140832Ssobomax timevalclear(&oitv->it_value); 760111034Stjr else 761140832Ssobomax timevalsub(&oitv->it_value, &ctv); 762111034Stjr } 76382746Sdillon } else { 764170307Sjeff PROC_SLOCK(p); 765140832Ssobomax *oitv = p->p_stats->p_timer[which]; 766140832Ssobomax p->p_stats->p_timer[which] = *aitv; 767170307Sjeff PROC_SUNLOCK(p); 76882746Sdillon } 769140832Ssobomax return (0); 7701541Srgrimes} 7711541Srgrimes 7721541Srgrimes/* 7731541Srgrimes * Real interval timer expired: 7741541Srgrimes * send process whose timer expired an alarm signal. 7751541Srgrimes * If time is not set up to reload, then just return. 7761541Srgrimes * Else compute next time timer should go off which is > current time. 7771541Srgrimes * This is where delay in processing this timeout causes multiple 7781541Srgrimes * SIGALRM calls to be compressed into one. 77936127Sbde * tvtohz() always adds 1 to allow for the time until the next clock 7809327Sbde * interrupt being strictly less than 1 clock tick, but we don't want 7819327Sbde * that here since we want to appear to be in sync with the clock 7829327Sbde * interrupt even when we're delayed. 7831541Srgrimes */ 7841541Srgrimesvoid 785102074Sphkrealitexpire(void *arg) 7861541Srgrimes{ 787102074Sphk struct proc *p; 78835044Sphk struct timeval ctv, ntv; 7891541Srgrimes 7901541Srgrimes p = (struct proc *)arg; 791225617Skmacy kern_psignal(p, SIGALRM); 79235058Sphk if (!timevalisset(&p->p_realtimer.it_interval)) { 79335058Sphk timevalclear(&p->p_realtimer.it_value); 794116123Sjhb if (p->p_flag & P_WEXIT) 795116123Sjhb wakeup(&p->p_itcallout); 7961541Srgrimes return; 7971541Srgrimes } 7981541Srgrimes for (;;) { 7991541Srgrimes timevaladd(&p->p_realtimer.it_value, 8001541Srgrimes &p->p_realtimer.it_interval); 80136119Sphk getmicrouptime(&ctv); 80235058Sphk if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 80335044Sphk ntv = p->p_realtimer.it_value; 80435044Sphk timevalsub(&ntv, &ctv); 80569286Sjake callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 80669286Sjake realitexpire, p); 8071541Srgrimes return; 8081541Srgrimes } 8091541Srgrimes } 81073916Sjhb /*NOTREACHED*/ 8111541Srgrimes} 8121541Srgrimes 8131541Srgrimes/* 8141541Srgrimes * Check that a proposed value to load into the .it_value or 8151541Srgrimes * .it_interval part of an interval timer is acceptable, and 8161541Srgrimes * fix it to have at least minimal value (i.e. if it is less 8171541Srgrimes * than the resolution of the clock, round it up.) 8181541Srgrimes */ 8191549Srgrimesint 820102074Sphkitimerfix(struct timeval *tv) 8211541Srgrimes{ 8221541Srgrimes 823151576Sdavidxu if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) 8241541Srgrimes return (EINVAL); 8251541Srgrimes if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 8261541Srgrimes tv->tv_usec = tick; 8271541Srgrimes return (0); 8281541Srgrimes} 8291541Srgrimes 8301541Srgrimes/* 8311541Srgrimes * Decrement an interval timer by a specified number 8321541Srgrimes * of microseconds, which must be less than a second, 8331541Srgrimes * i.e. < 1000000. If the timer expires, then reload 8341541Srgrimes * it. In this case, carry over (usec - old value) to 8351541Srgrimes * reduce the value reloaded into the timer so that 8361541Srgrimes * the timer does not drift. This routine assumes 8371541Srgrimes * that it is called in a context where the timers 8381541Srgrimes * on which it is operating cannot change in value. 8391541Srgrimes */ 8401549Srgrimesint 841102074Sphkitimerdecr(struct itimerval *itp, int usec) 8421541Srgrimes{ 8431541Srgrimes 8441541Srgrimes if (itp->it_value.tv_usec < usec) { 8451541Srgrimes if (itp->it_value.tv_sec == 0) { 8461541Srgrimes /* expired, and already in next interval */ 8471541Srgrimes usec -= itp->it_value.tv_usec; 8481541Srgrimes goto expire; 8491541Srgrimes } 8501541Srgrimes itp->it_value.tv_usec += 1000000; 8511541Srgrimes itp->it_value.tv_sec--; 8521541Srgrimes } 8531541Srgrimes itp->it_value.tv_usec -= usec; 8541541Srgrimes usec = 0; 85535058Sphk if (timevalisset(&itp->it_value)) 8561541Srgrimes return (1); 8571541Srgrimes /* expired, exactly at end of interval */ 8581541Srgrimesexpire: 85935058Sphk if (timevalisset(&itp->it_interval)) { 8601541Srgrimes itp->it_value = itp->it_interval; 8611541Srgrimes itp->it_value.tv_usec -= usec; 8621541Srgrimes if (itp->it_value.tv_usec < 0) { 8631541Srgrimes itp->it_value.tv_usec += 1000000; 8641541Srgrimes itp->it_value.tv_sec--; 8651541Srgrimes } 8661541Srgrimes } else 8671541Srgrimes itp->it_value.tv_usec = 0; /* sec is already 0 */ 8681541Srgrimes return (0); 8691541Srgrimes} 8701541Srgrimes 8711541Srgrimes/* 8721541Srgrimes * Add and subtract routines for timevals. 8731541Srgrimes * N.B.: subtract routine doesn't deal with 8741541Srgrimes * results which are before the beginning, 8751541Srgrimes * it just gets very confused in this case. 8761541Srgrimes * Caveat emptor. 8771541Srgrimes */ 8781549Srgrimesvoid 879121523Salfredtimevaladd(struct timeval *t1, const struct timeval *t2) 8801541Srgrimes{ 8811541Srgrimes 8821541Srgrimes t1->tv_sec += t2->tv_sec; 8831541Srgrimes t1->tv_usec += t2->tv_usec; 8841541Srgrimes timevalfix(t1); 8851541Srgrimes} 8861541Srgrimes 8871549Srgrimesvoid 888121523Salfredtimevalsub(struct timeval *t1, const struct timeval *t2) 8891541Srgrimes{ 8901541Srgrimes 8911541Srgrimes t1->tv_sec -= t2->tv_sec; 8921541Srgrimes t1->tv_usec -= t2->tv_usec; 8931541Srgrimes timevalfix(t1); 8941541Srgrimes} 8951541Srgrimes 89612819Sphkstatic void 897102074Sphktimevalfix(struct timeval *t1) 8981541Srgrimes{ 8991541Srgrimes 9001541Srgrimes if (t1->tv_usec < 0) { 9011541Srgrimes t1->tv_sec--; 9021541Srgrimes t1->tv_usec += 1000000; 9031541Srgrimes } 9041541Srgrimes if (t1->tv_usec >= 1000000) { 9051541Srgrimes t1->tv_sec++; 9061541Srgrimes t1->tv_usec -= 1000000; 9071541Srgrimes } 9081541Srgrimes} 909108142Ssam 910108142Ssam/* 911108511Ssam * ratecheck(): simple time-based rate-limit checking. 912108142Ssam */ 913108142Ssamint 914108142Ssamratecheck(struct timeval *lasttime, const struct timeval *mininterval) 915108142Ssam{ 916108142Ssam struct timeval tv, delta; 917108142Ssam int rv = 0; 918108142Ssam 919108511Ssam getmicrouptime(&tv); /* NB: 10ms precision */ 920108511Ssam delta = tv; 921108511Ssam timevalsub(&delta, lasttime); 922108142Ssam 923108142Ssam /* 924108142Ssam * check for 0,0 is so that the message will be seen at least once, 925108142Ssam * even if interval is huge. 926108142Ssam */ 927108142Ssam if (timevalcmp(&delta, mininterval, >=) || 928108142Ssam (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 929108142Ssam *lasttime = tv; 930108142Ssam rv = 1; 931108142Ssam } 932108142Ssam 933108142Ssam return (rv); 934108142Ssam} 935108142Ssam 936108142Ssam/* 937108142Ssam * ppsratecheck(): packets (or events) per second limitation. 938108511Ssam * 939108511Ssam * Return 0 if the limit is to be enforced (e.g. the caller 940108511Ssam * should drop a packet because of the rate limitation). 941108511Ssam * 942111558Ssam * maxpps of 0 always causes zero to be returned. maxpps of -1 943111558Ssam * always causes 1 to be returned; this effectively defeats rate 944111558Ssam * limiting. 945111558Ssam * 946108511Ssam * Note that we maintain the struct timeval for compatibility 947108511Ssam * with other bsd systems. We reuse the storage and just monitor 948108511Ssam * clock ticks for minimal overhead. 949108142Ssam */ 950108142Ssamint 951108142Ssamppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 952108142Ssam{ 953108511Ssam int now; 954108142Ssam 955108142Ssam /* 956108511Ssam * Reset the last time and counter if this is the first call 957108511Ssam * or more than a second has passed since the last update of 958108511Ssam * lasttime. 959108142Ssam */ 960108511Ssam now = ticks; 961108511Ssam if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 962108511Ssam lasttime->tv_sec = now; 963108511Ssam *curpps = 1; 964111558Ssam return (maxpps != 0); 965108511Ssam } else { 966108511Ssam (*curpps)++; /* NB: ignore potential overflow */ 967108511Ssam return (maxpps < 0 || *curpps < maxpps); 968108511Ssam } 969108142Ssam} 970151576Sdavidxu 971151576Sdavidxustatic void 972151576Sdavidxuitimer_start(void) 973151576Sdavidxu{ 974151576Sdavidxu struct kclock rt_clock = { 975151576Sdavidxu .timer_create = realtimer_create, 976151576Sdavidxu .timer_delete = realtimer_delete, 977151576Sdavidxu .timer_settime = realtimer_settime, 978151576Sdavidxu .timer_gettime = realtimer_gettime, 979164713Sdavidxu .event_hook = NULL 980151576Sdavidxu }; 981151576Sdavidxu 982151576Sdavidxu itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), 983151576Sdavidxu NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0); 984151576Sdavidxu register_posix_clock(CLOCK_REALTIME, &rt_clock); 985151576Sdavidxu register_posix_clock(CLOCK_MONOTONIC, &rt_clock); 986152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); 987152983Sdavidxu p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); 988152983Sdavidxu p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); 989161302Snetchild EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, 990153259Sdavidxu (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); 991161302Snetchild EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, 992153259Sdavidxu (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); 993151576Sdavidxu} 994151576Sdavidxu 995151576Sdavidxuint 996151576Sdavidxuregister_posix_clock(int clockid, struct kclock *clk) 997151576Sdavidxu{ 998151576Sdavidxu if ((unsigned)clockid >= MAX_CLOCKS) { 999151576Sdavidxu printf("%s: invalid clockid\n", __func__); 1000151576Sdavidxu return (0); 1001151576Sdavidxu } 1002151576Sdavidxu posix_clocks[clockid] = *clk; 1003151576Sdavidxu return (1); 1004151576Sdavidxu} 1005151576Sdavidxu 1006151576Sdavidxustatic int 1007151576Sdavidxuitimer_init(void *mem, int size, int flags) 1008151576Sdavidxu{ 1009151576Sdavidxu struct itimer *it; 1010151576Sdavidxu 1011151576Sdavidxu it = (struct itimer *)mem; 1012151576Sdavidxu mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF); 1013151576Sdavidxu return (0); 1014151576Sdavidxu} 1015151576Sdavidxu 1016151576Sdavidxustatic void 1017151576Sdavidxuitimer_fini(void *mem, int size) 1018151576Sdavidxu{ 1019151576Sdavidxu struct itimer *it; 1020151576Sdavidxu 1021151576Sdavidxu it = (struct itimer *)mem; 1022151576Sdavidxu mtx_destroy(&it->it_mtx); 1023151576Sdavidxu} 1024151576Sdavidxu 1025151576Sdavidxustatic void 1026151576Sdavidxuitimer_enter(struct itimer *it) 1027151576Sdavidxu{ 1028151576Sdavidxu 1029151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1030151576Sdavidxu it->it_usecount++; 1031151576Sdavidxu} 1032151576Sdavidxu 1033151576Sdavidxustatic void 1034151576Sdavidxuitimer_leave(struct itimer *it) 1035151576Sdavidxu{ 1036151576Sdavidxu 1037151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1038151576Sdavidxu KASSERT(it->it_usecount > 0, ("invalid it_usecount")); 1039151576Sdavidxu 1040151576Sdavidxu if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0) 1041151576Sdavidxu wakeup(it); 1042151576Sdavidxu} 1043151576Sdavidxu 1044151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1045156134Sdavidxustruct ktimer_create_args { 1046151576Sdavidxu clockid_t clock_id; 1047151576Sdavidxu struct sigevent * evp; 1048156134Sdavidxu int * timerid; 1049151576Sdavidxu}; 1050151576Sdavidxu#endif 1051151576Sdavidxuint 1052225617Skmacysys_ktimer_create(struct thread *td, struct ktimer_create_args *uap) 1053151576Sdavidxu{ 1054151576Sdavidxu struct sigevent *evp1, ev; 1055156134Sdavidxu int id; 1056151576Sdavidxu int error; 1057151576Sdavidxu 1058151576Sdavidxu if (uap->evp != NULL) { 1059151576Sdavidxu error = copyin(uap->evp, &ev, sizeof(ev)); 1060151576Sdavidxu if (error != 0) 1061151576Sdavidxu return (error); 1062151576Sdavidxu evp1 = &ev; 1063151576Sdavidxu } else 1064151576Sdavidxu evp1 = NULL; 1065151576Sdavidxu 1066151576Sdavidxu error = kern_timer_create(td, uap->clock_id, evp1, &id, -1); 1067151576Sdavidxu 1068151576Sdavidxu if (error == 0) { 1069156134Sdavidxu error = copyout(&id, uap->timerid, sizeof(int)); 1070151576Sdavidxu if (error != 0) 1071151576Sdavidxu kern_timer_delete(td, id); 1072151576Sdavidxu } 1073151576Sdavidxu return (error); 1074151576Sdavidxu} 1075151576Sdavidxu 1076151576Sdavidxustatic int 1077151576Sdavidxukern_timer_create(struct thread *td, clockid_t clock_id, 1078156134Sdavidxu struct sigevent *evp, int *timerid, int preset_id) 1079151576Sdavidxu{ 1080151576Sdavidxu struct proc *p = td->td_proc; 1081151576Sdavidxu struct itimer *it; 1082151576Sdavidxu int id; 1083151576Sdavidxu int error; 1084151576Sdavidxu 1085151576Sdavidxu if (clock_id < 0 || clock_id >= MAX_CLOCKS) 1086151576Sdavidxu return (EINVAL); 1087151576Sdavidxu 1088151576Sdavidxu if (posix_clocks[clock_id].timer_create == NULL) 1089151576Sdavidxu return (EINVAL); 1090151576Sdavidxu 1091151576Sdavidxu if (evp != NULL) { 1092151576Sdavidxu if (evp->sigev_notify != SIGEV_NONE && 1093151869Sdavidxu evp->sigev_notify != SIGEV_SIGNAL && 1094151869Sdavidxu evp->sigev_notify != SIGEV_THREAD_ID) 1095151576Sdavidxu return (EINVAL); 1096151869Sdavidxu if ((evp->sigev_notify == SIGEV_SIGNAL || 1097151869Sdavidxu evp->sigev_notify == SIGEV_THREAD_ID) && 1098151576Sdavidxu !_SIG_VALID(evp->sigev_signo)) 1099151576Sdavidxu return (EINVAL); 1100151576Sdavidxu } 1101151576Sdavidxu 1102151585Sdavidxu if (p->p_itimers == NULL) 1103151576Sdavidxu itimers_alloc(p); 1104151576Sdavidxu 1105151576Sdavidxu it = uma_zalloc(itimer_zone, M_WAITOK); 1106151576Sdavidxu it->it_flags = 0; 1107151576Sdavidxu it->it_usecount = 0; 1108151576Sdavidxu it->it_active = 0; 1109151869Sdavidxu timespecclear(&it->it_time.it_value); 1110151869Sdavidxu timespecclear(&it->it_time.it_interval); 1111151576Sdavidxu it->it_overrun = 0; 1112151576Sdavidxu it->it_overrun_last = 0; 1113151576Sdavidxu it->it_clockid = clock_id; 1114151576Sdavidxu it->it_timerid = -1; 1115151576Sdavidxu it->it_proc = p; 1116151576Sdavidxu ksiginfo_init(&it->it_ksi); 1117151576Sdavidxu it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT; 1118151576Sdavidxu error = CLOCK_CALL(clock_id, timer_create, (it)); 1119151576Sdavidxu if (error != 0) 1120151576Sdavidxu goto out; 1121151576Sdavidxu 1122151576Sdavidxu PROC_LOCK(p); 1123151576Sdavidxu if (preset_id != -1) { 1124151576Sdavidxu KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id")); 1125151576Sdavidxu id = preset_id; 1126151585Sdavidxu if (p->p_itimers->its_timers[id] != NULL) { 1127151576Sdavidxu PROC_UNLOCK(p); 1128151576Sdavidxu error = 0; 1129151576Sdavidxu goto out; 1130151576Sdavidxu } 1131151576Sdavidxu } else { 1132151576Sdavidxu /* 1133151576Sdavidxu * Find a free timer slot, skipping those reserved 1134151576Sdavidxu * for setitimer(). 1135151576Sdavidxu */ 1136151576Sdavidxu for (id = 3; id < TIMER_MAX; id++) 1137151585Sdavidxu if (p->p_itimers->its_timers[id] == NULL) 1138151576Sdavidxu break; 1139151576Sdavidxu if (id == TIMER_MAX) { 1140151576Sdavidxu PROC_UNLOCK(p); 1141151576Sdavidxu error = EAGAIN; 1142151576Sdavidxu goto out; 1143151576Sdavidxu } 1144151576Sdavidxu } 1145151576Sdavidxu it->it_timerid = id; 1146151585Sdavidxu p->p_itimers->its_timers[id] = it; 1147151576Sdavidxu if (evp != NULL) 1148151576Sdavidxu it->it_sigev = *evp; 1149151576Sdavidxu else { 1150151576Sdavidxu it->it_sigev.sigev_notify = SIGEV_SIGNAL; 1151151576Sdavidxu switch (clock_id) { 1152151576Sdavidxu default: 1153151576Sdavidxu case CLOCK_REALTIME: 1154151576Sdavidxu it->it_sigev.sigev_signo = SIGALRM; 1155151576Sdavidxu break; 1156151576Sdavidxu case CLOCK_VIRTUAL: 1157151576Sdavidxu it->it_sigev.sigev_signo = SIGVTALRM; 1158151576Sdavidxu break; 1159151576Sdavidxu case CLOCK_PROF: 1160151576Sdavidxu it->it_sigev.sigev_signo = SIGPROF; 1161151576Sdavidxu break; 1162151576Sdavidxu } 1163152029Sdavidxu it->it_sigev.sigev_value.sival_int = id; 1164151576Sdavidxu } 1165151576Sdavidxu 1166151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1167151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1168151576Sdavidxu it->it_ksi.ksi_signo = it->it_sigev.sigev_signo; 1169151576Sdavidxu it->it_ksi.ksi_code = SI_TIMER; 1170151576Sdavidxu it->it_ksi.ksi_value = it->it_sigev.sigev_value; 1171151576Sdavidxu it->it_ksi.ksi_timerid = id; 1172151576Sdavidxu } 1173151576Sdavidxu PROC_UNLOCK(p); 1174151576Sdavidxu *timerid = id; 1175151576Sdavidxu return (0); 1176151576Sdavidxu 1177151576Sdavidxuout: 1178151576Sdavidxu ITIMER_LOCK(it); 1179151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1180151576Sdavidxu ITIMER_UNLOCK(it); 1181151576Sdavidxu uma_zfree(itimer_zone, it); 1182151576Sdavidxu return (error); 1183151576Sdavidxu} 1184151576Sdavidxu 1185151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1186156134Sdavidxustruct ktimer_delete_args { 1187156134Sdavidxu int timerid; 1188151576Sdavidxu}; 1189151576Sdavidxu#endif 1190151576Sdavidxuint 1191225617Skmacysys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap) 1192151576Sdavidxu{ 1193151576Sdavidxu return (kern_timer_delete(td, uap->timerid)); 1194151576Sdavidxu} 1195151576Sdavidxu 1196151576Sdavidxustatic struct itimer * 1197164713Sdavidxuitimer_find(struct proc *p, int timerid) 1198151576Sdavidxu{ 1199151576Sdavidxu struct itimer *it; 1200151576Sdavidxu 1201151576Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1202190301Scperciva if ((p->p_itimers == NULL) || 1203190301Scperciva (timerid < 0) || (timerid >= TIMER_MAX) || 1204151585Sdavidxu (it = p->p_itimers->its_timers[timerid]) == NULL) { 1205151576Sdavidxu return (NULL); 1206151576Sdavidxu } 1207151576Sdavidxu ITIMER_LOCK(it); 1208164713Sdavidxu if ((it->it_flags & ITF_DELETING) != 0) { 1209151576Sdavidxu ITIMER_UNLOCK(it); 1210151576Sdavidxu it = NULL; 1211151576Sdavidxu } 1212151576Sdavidxu return (it); 1213151576Sdavidxu} 1214151576Sdavidxu 1215151576Sdavidxustatic int 1216156134Sdavidxukern_timer_delete(struct thread *td, int timerid) 1217151576Sdavidxu{ 1218151576Sdavidxu struct proc *p = td->td_proc; 1219151576Sdavidxu struct itimer *it; 1220151576Sdavidxu 1221151576Sdavidxu PROC_LOCK(p); 1222164713Sdavidxu it = itimer_find(p, timerid); 1223151576Sdavidxu if (it == NULL) { 1224151576Sdavidxu PROC_UNLOCK(p); 1225151576Sdavidxu return (EINVAL); 1226151576Sdavidxu } 1227151576Sdavidxu PROC_UNLOCK(p); 1228151576Sdavidxu 1229151576Sdavidxu it->it_flags |= ITF_DELETING; 1230151576Sdavidxu while (it->it_usecount > 0) { 1231151576Sdavidxu it->it_flags |= ITF_WANTED; 1232151576Sdavidxu msleep(it, &it->it_mtx, PPAUSE, "itimer", 0); 1233151576Sdavidxu } 1234151576Sdavidxu it->it_flags &= ~ITF_WANTED; 1235151576Sdavidxu CLOCK_CALL(it->it_clockid, timer_delete, (it)); 1236151576Sdavidxu ITIMER_UNLOCK(it); 1237151576Sdavidxu 1238151576Sdavidxu PROC_LOCK(p); 1239151576Sdavidxu if (KSI_ONQ(&it->it_ksi)) 1240151576Sdavidxu sigqueue_take(&it->it_ksi); 1241151585Sdavidxu p->p_itimers->its_timers[timerid] = NULL; 1242151576Sdavidxu PROC_UNLOCK(p); 1243151576Sdavidxu uma_zfree(itimer_zone, it); 1244151576Sdavidxu return (0); 1245151576Sdavidxu} 1246151576Sdavidxu 1247151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1248156134Sdavidxustruct ktimer_settime_args { 1249156134Sdavidxu int timerid; 1250151576Sdavidxu int flags; 1251151576Sdavidxu const struct itimerspec * value; 1252151576Sdavidxu struct itimerspec * ovalue; 1253151576Sdavidxu}; 1254151576Sdavidxu#endif 1255151576Sdavidxuint 1256225617Skmacysys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap) 1257151576Sdavidxu{ 1258151576Sdavidxu struct proc *p = td->td_proc; 1259151576Sdavidxu struct itimer *it; 1260151576Sdavidxu struct itimerspec val, oval, *ovalp; 1261151576Sdavidxu int error; 1262151576Sdavidxu 1263151576Sdavidxu error = copyin(uap->value, &val, sizeof(val)); 1264151576Sdavidxu if (error != 0) 1265151576Sdavidxu return (error); 1266151576Sdavidxu 1267151576Sdavidxu if (uap->ovalue != NULL) 1268151576Sdavidxu ovalp = &oval; 1269151576Sdavidxu else 1270151576Sdavidxu ovalp = NULL; 1271151576Sdavidxu 1272151576Sdavidxu PROC_LOCK(p); 1273151576Sdavidxu if (uap->timerid < 3 || 1274164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1275151576Sdavidxu PROC_UNLOCK(p); 1276151576Sdavidxu error = EINVAL; 1277151576Sdavidxu } else { 1278151576Sdavidxu PROC_UNLOCK(p); 1279151576Sdavidxu itimer_enter(it); 1280151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_settime, 1281151576Sdavidxu (it, uap->flags, &val, ovalp)); 1282151576Sdavidxu itimer_leave(it); 1283151576Sdavidxu ITIMER_UNLOCK(it); 1284151576Sdavidxu } 1285151576Sdavidxu if (error == 0 && uap->ovalue != NULL) 1286151576Sdavidxu error = copyout(ovalp, uap->ovalue, sizeof(*ovalp)); 1287151576Sdavidxu return (error); 1288151576Sdavidxu} 1289151576Sdavidxu 1290151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1291156134Sdavidxustruct ktimer_gettime_args { 1292156134Sdavidxu int timerid; 1293151576Sdavidxu struct itimerspec * value; 1294151576Sdavidxu}; 1295151576Sdavidxu#endif 1296151576Sdavidxuint 1297225617Skmacysys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap) 1298151576Sdavidxu{ 1299151576Sdavidxu struct proc *p = td->td_proc; 1300151576Sdavidxu struct itimer *it; 1301151576Sdavidxu struct itimerspec val; 1302151576Sdavidxu int error; 1303151576Sdavidxu 1304151576Sdavidxu PROC_LOCK(p); 1305151576Sdavidxu if (uap->timerid < 3 || 1306164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1307151576Sdavidxu PROC_UNLOCK(p); 1308151576Sdavidxu error = EINVAL; 1309151576Sdavidxu } else { 1310151576Sdavidxu PROC_UNLOCK(p); 1311151576Sdavidxu itimer_enter(it); 1312151576Sdavidxu error = CLOCK_CALL(it->it_clockid, timer_gettime, 1313151576Sdavidxu (it, &val)); 1314151576Sdavidxu itimer_leave(it); 1315151576Sdavidxu ITIMER_UNLOCK(it); 1316151576Sdavidxu } 1317151576Sdavidxu if (error == 0) 1318151576Sdavidxu error = copyout(&val, uap->value, sizeof(val)); 1319151576Sdavidxu return (error); 1320151576Sdavidxu} 1321151576Sdavidxu 1322151576Sdavidxu#ifndef _SYS_SYSPROTO_H_ 1323151576Sdavidxustruct timer_getoverrun_args { 1324156134Sdavidxu int timerid; 1325151576Sdavidxu}; 1326151576Sdavidxu#endif 1327151576Sdavidxuint 1328225617Skmacysys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap) 1329151576Sdavidxu{ 1330151576Sdavidxu struct proc *p = td->td_proc; 1331151576Sdavidxu struct itimer *it; 1332151576Sdavidxu int error ; 1333151576Sdavidxu 1334151576Sdavidxu PROC_LOCK(p); 1335151576Sdavidxu if (uap->timerid < 3 || 1336164713Sdavidxu (it = itimer_find(p, uap->timerid)) == NULL) { 1337151576Sdavidxu PROC_UNLOCK(p); 1338151576Sdavidxu error = EINVAL; 1339151576Sdavidxu } else { 1340151576Sdavidxu td->td_retval[0] = it->it_overrun_last; 1341151576Sdavidxu ITIMER_UNLOCK(it); 1342151869Sdavidxu PROC_UNLOCK(p); 1343151576Sdavidxu error = 0; 1344151576Sdavidxu } 1345151576Sdavidxu return (error); 1346151576Sdavidxu} 1347151576Sdavidxu 1348151576Sdavidxustatic int 1349151576Sdavidxurealtimer_create(struct itimer *it) 1350151576Sdavidxu{ 1351151576Sdavidxu callout_init_mtx(&it->it_callout, &it->it_mtx, 0); 1352151576Sdavidxu return (0); 1353151576Sdavidxu} 1354151576Sdavidxu 1355151576Sdavidxustatic int 1356151576Sdavidxurealtimer_delete(struct itimer *it) 1357151576Sdavidxu{ 1358151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1359164713Sdavidxu 1360184067Sdavidxu /* 1361184067Sdavidxu * clear timer's value and interval to tell realtimer_expire 1362184067Sdavidxu * to not rearm the timer. 1363184067Sdavidxu */ 1364184067Sdavidxu timespecclear(&it->it_time.it_value); 1365184067Sdavidxu timespecclear(&it->it_time.it_interval); 1366164713Sdavidxu ITIMER_UNLOCK(it); 1367164713Sdavidxu callout_drain(&it->it_callout); 1368164713Sdavidxu ITIMER_LOCK(it); 1369151576Sdavidxu return (0); 1370151576Sdavidxu} 1371151576Sdavidxu 1372151576Sdavidxustatic int 1373151576Sdavidxurealtimer_gettime(struct itimer *it, struct itimerspec *ovalue) 1374151576Sdavidxu{ 1375151869Sdavidxu struct timespec cts; 1376151576Sdavidxu 1377151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1378151576Sdavidxu 1379151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1380151869Sdavidxu *ovalue = it->it_time; 1381151576Sdavidxu if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) { 1382151869Sdavidxu timespecsub(&ovalue->it_value, &cts); 1383151576Sdavidxu if (ovalue->it_value.tv_sec < 0 || 1384151576Sdavidxu (ovalue->it_value.tv_sec == 0 && 1385151576Sdavidxu ovalue->it_value.tv_nsec == 0)) { 1386151576Sdavidxu ovalue->it_value.tv_sec = 0; 1387151576Sdavidxu ovalue->it_value.tv_nsec = 1; 1388151576Sdavidxu } 1389151576Sdavidxu } 1390151576Sdavidxu return (0); 1391151576Sdavidxu} 1392151576Sdavidxu 1393151576Sdavidxustatic int 1394151576Sdavidxurealtimer_settime(struct itimer *it, int flags, 1395151576Sdavidxu struct itimerspec *value, struct itimerspec *ovalue) 1396151576Sdavidxu{ 1397151869Sdavidxu struct timespec cts, ts; 1398151869Sdavidxu struct timeval tv; 1399151869Sdavidxu struct itimerspec val; 1400151576Sdavidxu 1401151576Sdavidxu mtx_assert(&it->it_mtx, MA_OWNED); 1402151576Sdavidxu 1403151869Sdavidxu val = *value; 1404151869Sdavidxu if (itimespecfix(&val.it_value)) 1405151576Sdavidxu return (EINVAL); 1406151576Sdavidxu 1407151869Sdavidxu if (timespecisset(&val.it_value)) { 1408151869Sdavidxu if (itimespecfix(&val.it_interval)) 1409151576Sdavidxu return (EINVAL); 1410151576Sdavidxu } else { 1411151869Sdavidxu timespecclear(&val.it_interval); 1412151576Sdavidxu } 1413151576Sdavidxu 1414151576Sdavidxu if (ovalue != NULL) 1415151576Sdavidxu realtimer_gettime(it, ovalue); 1416151576Sdavidxu 1417151576Sdavidxu it->it_time = val; 1418151869Sdavidxu if (timespecisset(&val.it_value)) { 1419151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1420151869Sdavidxu ts = val.it_value; 1421151576Sdavidxu if ((flags & TIMER_ABSTIME) == 0) { 1422151576Sdavidxu /* Convert to absolute time. */ 1423151869Sdavidxu timespecadd(&it->it_time.it_value, &cts); 1424151576Sdavidxu } else { 1425151869Sdavidxu timespecsub(&ts, &cts); 1426151576Sdavidxu /* 1427151869Sdavidxu * We don't care if ts is negative, tztohz will 1428151576Sdavidxu * fix it. 1429151576Sdavidxu */ 1430151576Sdavidxu } 1431151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1432151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1433151576Sdavidxu realtimer_expire, it); 1434151576Sdavidxu } else { 1435151576Sdavidxu callout_stop(&it->it_callout); 1436151576Sdavidxu } 1437151576Sdavidxu 1438151576Sdavidxu return (0); 1439151576Sdavidxu} 1440151576Sdavidxu 1441151576Sdavidxustatic void 1442151869Sdavidxurealtimer_clocktime(clockid_t id, struct timespec *ts) 1443151576Sdavidxu{ 1444151576Sdavidxu if (id == CLOCK_REALTIME) 1445151869Sdavidxu getnanotime(ts); 1446151576Sdavidxu else /* CLOCK_MONOTONIC */ 1447151869Sdavidxu getnanouptime(ts); 1448151576Sdavidxu} 1449151576Sdavidxu 1450151869Sdavidxuint 1451156134Sdavidxuitimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi) 1452151869Sdavidxu{ 1453151869Sdavidxu struct itimer *it; 1454151869Sdavidxu 1455151869Sdavidxu PROC_LOCK_ASSERT(p, MA_OWNED); 1456164713Sdavidxu it = itimer_find(p, timerid); 1457151869Sdavidxu if (it != NULL) { 1458151869Sdavidxu ksi->ksi_overrun = it->it_overrun; 1459151869Sdavidxu it->it_overrun_last = it->it_overrun; 1460151869Sdavidxu it->it_overrun = 0; 1461151869Sdavidxu ITIMER_UNLOCK(it); 1462151869Sdavidxu return (0); 1463151869Sdavidxu } 1464151869Sdavidxu return (EINVAL); 1465151869Sdavidxu} 1466151869Sdavidxu 1467151869Sdavidxuint 1468151869Sdavidxuitimespecfix(struct timespec *ts) 1469151869Sdavidxu{ 1470151869Sdavidxu 1471151869Sdavidxu if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1472151869Sdavidxu return (EINVAL); 1473151869Sdavidxu if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 1474151869Sdavidxu ts->tv_nsec = tick * 1000; 1475151869Sdavidxu return (0); 1476151869Sdavidxu} 1477151869Sdavidxu 1478151576Sdavidxu/* Timeout callback for realtime timer */ 1479151576Sdavidxustatic void 1480151576Sdavidxurealtimer_expire(void *arg) 1481151576Sdavidxu{ 1482151869Sdavidxu struct timespec cts, ts; 1483151869Sdavidxu struct timeval tv; 1484151576Sdavidxu struct itimer *it; 1485151576Sdavidxu 1486151576Sdavidxu it = (struct itimer *)arg; 1487151576Sdavidxu 1488151869Sdavidxu realtimer_clocktime(it->it_clockid, &cts); 1489151576Sdavidxu /* Only fire if time is reached. */ 1490151869Sdavidxu if (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1491151869Sdavidxu if (timespecisset(&it->it_time.it_interval)) { 1492151869Sdavidxu timespecadd(&it->it_time.it_value, 1493151869Sdavidxu &it->it_time.it_interval); 1494151869Sdavidxu while (timespeccmp(&cts, &it->it_time.it_value, >=)) { 1495152983Sdavidxu if (it->it_overrun < INT_MAX) 1496152983Sdavidxu it->it_overrun++; 1497152983Sdavidxu else 1498152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1499151869Sdavidxu timespecadd(&it->it_time.it_value, 1500151869Sdavidxu &it->it_time.it_interval); 1501151576Sdavidxu } 1502151576Sdavidxu } else { 1503151576Sdavidxu /* single shot timer ? */ 1504151869Sdavidxu timespecclear(&it->it_time.it_value); 1505151576Sdavidxu } 1506151869Sdavidxu if (timespecisset(&it->it_time.it_value)) { 1507151869Sdavidxu ts = it->it_time.it_value; 1508151869Sdavidxu timespecsub(&ts, &cts); 1509151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1510151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), 1511151576Sdavidxu realtimer_expire, it); 1512151576Sdavidxu } 1513184067Sdavidxu itimer_enter(it); 1514151576Sdavidxu ITIMER_UNLOCK(it); 1515151576Sdavidxu itimer_fire(it); 1516151576Sdavidxu ITIMER_LOCK(it); 1517184067Sdavidxu itimer_leave(it); 1518151869Sdavidxu } else if (timespecisset(&it->it_time.it_value)) { 1519151869Sdavidxu ts = it->it_time.it_value; 1520151869Sdavidxu timespecsub(&ts, &cts); 1521151869Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts); 1522151869Sdavidxu callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire, 1523151576Sdavidxu it); 1524151576Sdavidxu } 1525151576Sdavidxu} 1526151576Sdavidxu 1527151576Sdavidxuvoid 1528151576Sdavidxuitimer_fire(struct itimer *it) 1529151576Sdavidxu{ 1530151576Sdavidxu struct proc *p = it->it_proc; 1531213642Sdavidxu struct thread *td; 1532151576Sdavidxu 1533151869Sdavidxu if (it->it_sigev.sigev_notify == SIGEV_SIGNAL || 1534151869Sdavidxu it->it_sigev.sigev_notify == SIGEV_THREAD_ID) { 1535213642Sdavidxu if (sigev_findtd(p, &it->it_sigev, &td) != 0) { 1536213642Sdavidxu ITIMER_LOCK(it); 1537213642Sdavidxu timespecclear(&it->it_time.it_value); 1538213642Sdavidxu timespecclear(&it->it_time.it_interval); 1539213642Sdavidxu callout_stop(&it->it_callout); 1540213642Sdavidxu ITIMER_UNLOCK(it); 1541213642Sdavidxu return; 1542213642Sdavidxu } 1543151993Sdavidxu if (!KSI_ONQ(&it->it_ksi)) { 1544152983Sdavidxu it->it_ksi.ksi_errno = 0; 1545213642Sdavidxu ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev); 1546213642Sdavidxu tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi); 1547151993Sdavidxu } else { 1548152983Sdavidxu if (it->it_overrun < INT_MAX) 1549152983Sdavidxu it->it_overrun++; 1550152983Sdavidxu else 1551152983Sdavidxu it->it_ksi.ksi_errno = ERANGE; 1552151576Sdavidxu } 1553151576Sdavidxu PROC_UNLOCK(p); 1554151576Sdavidxu } 1555151576Sdavidxu} 1556151576Sdavidxu 1557151576Sdavidxustatic void 1558151576Sdavidxuitimers_alloc(struct proc *p) 1559151576Sdavidxu{ 1560151585Sdavidxu struct itimers *its; 1561151585Sdavidxu int i; 1562151576Sdavidxu 1563151585Sdavidxu its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO); 1564151585Sdavidxu LIST_INIT(&its->its_virtual); 1565151585Sdavidxu LIST_INIT(&its->its_prof); 1566151585Sdavidxu TAILQ_INIT(&its->its_worklist); 1567151585Sdavidxu for (i = 0; i < TIMER_MAX; i++) 1568151585Sdavidxu its->its_timers[i] = NULL; 1569151576Sdavidxu PROC_LOCK(p); 1570151585Sdavidxu if (p->p_itimers == NULL) { 1571151585Sdavidxu p->p_itimers = its; 1572151576Sdavidxu PROC_UNLOCK(p); 1573151585Sdavidxu } 1574151585Sdavidxu else { 1575151576Sdavidxu PROC_UNLOCK(p); 1576151585Sdavidxu free(its, M_SUBPROC); 1577151576Sdavidxu } 1578151576Sdavidxu} 1579151576Sdavidxu 1580161302Snetchildstatic void 1581161302Snetchilditimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 1582161302Snetchild{ 1583164713Sdavidxu itimers_event_hook_exit(arg, p); 1584161302Snetchild} 1585161302Snetchild 1586151576Sdavidxu/* Clean up timers when some process events are being triggered. */ 1587153259Sdavidxustatic void 1588161302Snetchilditimers_event_hook_exit(void *arg, struct proc *p) 1589151576Sdavidxu{ 1590151576Sdavidxu struct itimers *its; 1591151576Sdavidxu struct itimer *it; 1592153267Sdavidxu int event = (int)(intptr_t)arg; 1593151576Sdavidxu int i; 1594151576Sdavidxu 1595151585Sdavidxu if (p->p_itimers != NULL) { 1596151585Sdavidxu its = p->p_itimers; 1597151576Sdavidxu for (i = 0; i < MAX_CLOCKS; ++i) { 1598151576Sdavidxu if (posix_clocks[i].event_hook != NULL) 1599151576Sdavidxu CLOCK_CALL(i, event_hook, (p, i, event)); 1600151576Sdavidxu } 1601151576Sdavidxu /* 1602151576Sdavidxu * According to susv3, XSI interval timers should be inherited 1603151576Sdavidxu * by new image. 1604151576Sdavidxu */ 1605151576Sdavidxu if (event == ITIMER_EV_EXEC) 1606151576Sdavidxu i = 3; 1607151576Sdavidxu else if (event == ITIMER_EV_EXIT) 1608151576Sdavidxu i = 0; 1609151576Sdavidxu else 1610151576Sdavidxu panic("unhandled event"); 1611151576Sdavidxu for (; i < TIMER_MAX; ++i) { 1612164713Sdavidxu if ((it = its->its_timers[i]) != NULL) 1613164713Sdavidxu kern_timer_delete(curthread, i); 1614151576Sdavidxu } 1615151576Sdavidxu if (its->its_timers[0] == NULL && 1616151576Sdavidxu its->its_timers[1] == NULL && 1617151576Sdavidxu its->its_timers[2] == NULL) { 1618151585Sdavidxu free(its, M_SUBPROC); 1619151585Sdavidxu p->p_itimers = NULL; 1620151576Sdavidxu } 1621151576Sdavidxu } 1622151576Sdavidxu} 1623