Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.105.4.6
      1 /*	$NetBSD: kern_time.c,v 1.105.4.6 2007/01/11 22:23:00 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004, 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christopher G. Demetriou.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1982, 1986, 1989, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.105.4.6 2007/01/11 22:23:00 ad Exp $");
     72 
     73 #include "fs_nfs.h"
     74 #include "opt_nfs.h"
     75 #include "opt_nfsserver.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/resourcevar.h>
     79 #include <sys/kernel.h>
     80 #include <sys/systm.h>
     81 #include <sys/proc.h>
     82 #include <sys/sa.h>
     83 #include <sys/savar.h>
     84 #include <sys/vnode.h>
     85 #include <sys/signalvar.h>
     86 #include <sys/syslog.h>
     87 #ifdef __HAVE_TIMECOUNTER
     88 #include <sys/timetc.h>
     89 #else /* !__HAVE_TIMECOUNTER */
     90 #include <sys/timevar.h>
     91 #endif /* !__HAVE_TIMECOUNTER */
     92 #include <sys/kauth.h>
     93 
     94 #include <sys/mount.h>
     95 #include <sys/syscallargs.h>
     96 
     97 #include <uvm/uvm_extern.h>
     98 
     99 #if defined(NFS) || defined(NFSSERVER)
    100 #include <nfs/rpcv2.h>
    101 #include <nfs/nfsproto.h>
    102 #include <nfs/nfs.h>
    103 #include <nfs/nfs_var.h>
    104 #endif
    105 
    106 #include <machine/cpu.h>
    107 
    108 POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
    109     &pool_allocator_nointr);
    110 POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
    111     &pool_allocator_nointr);
    112 
    113 #ifdef __HAVE_TIMECOUNTER
    114 static int itimespecfix(struct timespec *);		/* XXX move itimerfix to timespecs */
    115 #endif /* __HAVE_TIMECOUNTER */
    116 
    117 /* Time of day and interval timer support.
    118  *
    119  * These routines provide the kernel entry points to get and set
    120  * the time-of-day and per-process interval timers.  Subroutines
    121  * here provide support for adding and subtracting timeval structures
    122  * and decrementing interval timers, optionally reloading the interval
    123  * timers when they expire.
    124  */
    125 
    126 /* This function is used by clock_settime and settimeofday */
    127 int
    128 settime(struct proc *p, struct timespec *ts)
    129 {
    130 	struct timeval delta, tv;
    131 #ifdef __HAVE_TIMECOUNTER
    132 	struct timeval now;
    133 	struct timespec ts1;
    134 #endif /* !__HAVE_TIMECOUNTER */
    135 	struct cpu_info *ci;
    136 	int s;
    137 
    138 	/*
    139 	 * Don't allow the time to be set forward so far it will wrap
    140 	 * and become negative, thus allowing an attacker to bypass
    141 	 * the next check below.  The cutoff is 1 year before rollover
    142 	 * occurs, so even if the attacker uses adjtime(2) to move
    143 	 * the time past the cutoff, it will take a very long time
    144 	 * to get to the wrap point.
    145 	 *
    146 	 * XXX: we check against INT_MAX since on 64-bit
    147 	 *	platforms, sizeof(int) != sizeof(long) and
    148 	 *	time_t is 32 bits even when atv.tv_sec is 64 bits.
    149 	 */
    150 	if (ts->tv_sec > INT_MAX - 365*24*60*60) {
    151 		struct proc *pp;
    152 
    153 		rw_enter(&proclist_lock, RW_READER);
    154 		pp = p->p_pptr;
    155 		mutex_enter(&pp->p_mutex);
    156 		log(LOG_WARNING, "pid %d (%s) "
    157 		    "invoked by uid %d ppid %d (%s) "
    158 		    "tried to set clock forward to %ld\n",
    159 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
    160 		    pp->p_pid, pp->p_comm, (long)ts->tv_sec);
    161 		mutex_exit(&pp->p_mutex);
    162 		rw_exit(&proclist_lock);
    163 		return (EPERM);
    164 	}
    165 	TIMESPEC_TO_TIMEVAL(&tv, ts);
    166 
    167 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    168 	s = splclock();
    169 #ifdef __HAVE_TIMECOUNTER
    170 	microtime(&now);
    171 	timersub(&tv, &now, &delta);
    172 #else /* !__HAVE_TIMECOUNTER */
    173 	timersub(&tv, &time, &delta);
    174 #endif /* !__HAVE_TIMECOUNTER */
    175 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) &&
    176 	    kauth_authorize_system(p->p_cred, KAUTH_SYSTEM_TIME,
    177 	    KAUTH_REQ_SYSTEM_TIME_BACKWARDS, NULL, NULL, NULL)) {
    178 		splx(s);
    179 		return (EPERM);
    180 	}
    181 #ifdef notyet
    182 	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
    183 		splx(s);
    184 		return (EPERM);
    185 	}
    186 #endif
    187 
    188 #ifdef __HAVE_TIMECOUNTER
    189 	TIMEVAL_TO_TIMESPEC(&tv, &ts1);
    190 	tc_setclock(&ts1);
    191 #else /* !__HAVE_TIMECOUNTER */
    192 	time = tv;
    193 #endif /* !__HAVE_TIMECOUNTER */
    194 
    195 	(void) spllowersoftclock();
    196 
    197 	timeradd(&boottime, &delta, &boottime);
    198 
    199 	/*
    200 	 * XXXSMP
    201 	 * This is wrong.  We should traverse a list of all
    202 	 * CPUs and add the delta to the runtime of those
    203 	 * CPUs which have a process on them.
    204 	 */
    205 	ci = curcpu();
    206 	timeradd(&ci->ci_schedstate.spc_runtime, &delta,
    207 	    &ci->ci_schedstate.spc_runtime);
    208 #if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
    209 	nqnfs_lease_updatetime(delta.tv_sec);
    210 #endif
    211 	splx(s);
    212 	resettodr();
    213 	return (0);
    214 }
    215 
    216 /* ARGSUSED */
    217 int
    218 sys_clock_gettime(struct lwp *l, void *v, register_t *retval)
    219 {
    220 	struct sys_clock_gettime_args /* {
    221 		syscallarg(clockid_t) clock_id;
    222 		syscallarg(struct timespec *) tp;
    223 	} */ *uap = v;
    224 	clockid_t clock_id;
    225 	struct timespec ats;
    226 
    227 	clock_id = SCARG(uap, clock_id);
    228 	switch (clock_id) {
    229 	case CLOCK_REALTIME:
    230 		nanotime(&ats);
    231 		break;
    232 	case CLOCK_MONOTONIC:
    233 #ifdef __HAVE_TIMECOUNTER
    234 		nanouptime(&ats);
    235 #else /* !__HAVE_TIMECOUNTER */
    236 		{
    237 		int s;
    238 
    239 		/* XXX "hz" granularity */
    240 		s = splclock();
    241 		TIMEVAL_TO_TIMESPEC(&mono_time,&ats);
    242 		splx(s);
    243 		}
    244 #endif /* !__HAVE_TIMECOUNTER */
    245 		break;
    246 	default:
    247 		return (EINVAL);
    248 	}
    249 
    250 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    251 }
    252 
    253 /* ARGSUSED */
    254 int
    255 sys_clock_settime(struct lwp *l, void *v, register_t *retval)
    256 {
    257 	struct sys_clock_settime_args /* {
    258 		syscallarg(clockid_t) clock_id;
    259 		syscallarg(const struct timespec *) tp;
    260 	} */ *uap = v;
    261 	int error;
    262 
    263 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    264 	    KAUTH_REQ_SYSTEM_TIME_SYSTEM, NULL, NULL, NULL)) != 0)
    265 		return (error);
    266 
    267 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), SCARG(uap, tp));
    268 }
    269 
    270 
    271 int
    272 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp)
    273 {
    274 	struct timespec ats;
    275 	int error;
    276 
    277 	if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
    278 		return (error);
    279 
    280 	switch (clock_id) {
    281 	case CLOCK_REALTIME:
    282 		if ((error = settime(p, &ats)) != 0)
    283 			return (error);
    284 		break;
    285 	case CLOCK_MONOTONIC:
    286 		return (EINVAL);	/* read-only clock */
    287 	default:
    288 		return (EINVAL);
    289 	}
    290 
    291 	return 0;
    292 }
    293 
    294 int
    295 sys_clock_getres(struct lwp *l, void *v, register_t *retval)
    296 {
    297 	struct sys_clock_getres_args /* {
    298 		syscallarg(clockid_t) clock_id;
    299 		syscallarg(struct timespec *) tp;
    300 	} */ *uap = v;
    301 	clockid_t clock_id;
    302 	struct timespec ts;
    303 	int error = 0;
    304 
    305 	clock_id = SCARG(uap, clock_id);
    306 	switch (clock_id) {
    307 	case CLOCK_REALTIME:
    308 	case CLOCK_MONOTONIC:
    309 		ts.tv_sec = 0;
    310 #ifdef __HAVE_TIMECOUNTER
    311 		if (tc_getfrequency() > 1000000000)
    312 			ts.tv_nsec = 1;
    313 		else
    314 			ts.tv_nsec = 1000000000 / tc_getfrequency();
    315 #else /* !__HAVE_TIMECOUNTER */
    316 		ts.tv_nsec = 1000000000 / hz;
    317 #endif /* !__HAVE_TIMECOUNTER */
    318 		break;
    319 	default:
    320 		return (EINVAL);
    321 	}
    322 
    323 	if (SCARG(uap, tp))
    324 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    325 
    326 	return error;
    327 }
    328 
    329 /* ARGSUSED */
    330 int
    331 sys_nanosleep(struct lwp *l, void *v, register_t *retval)
    332 {
    333 #ifdef __HAVE_TIMECOUNTER
    334 	struct sys_nanosleep_args/* {
    335 		syscallarg(struct timespec *) rqtp;
    336 		syscallarg(struct timespec *) rmtp;
    337 	} */ *uap = v;
    338 	struct timespec rmt, rqt;
    339 	int error, timo;
    340 
    341 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    342 	if (error)
    343 		return (error);
    344 
    345 	if (itimespecfix(&rqt))
    346 		return (EINVAL);
    347 
    348 	timo = tstohz(&rqt);
    349 	/*
    350 	 * Avoid inadvertantly sleeping forever
    351 	 */
    352 	if (timo == 0)
    353 		timo = 1;
    354 
    355 	getnanouptime(&rmt);
    356 
    357 	error = sched_pause("nanoslp", TRUE, timo);
    358 	if (error == ERESTART)
    359 		error = EINTR;
    360 	if (error == EWOULDBLOCK)
    361 		error = 0;
    362 
    363 	if (SCARG(uap, rmtp)) {
    364 		int error1;
    365 		struct timespec rmtend;
    366 
    367 		getnanouptime(&rmtend);
    368 
    369 		timespecsub(&rmtend, &rmt, &rmt);
    370 		timespecsub(&rqt, &rmt, &rmt);
    371 		if (rmt.tv_sec < 0)
    372 			timespecclear(&rmt);
    373 
    374 		error1 = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
    375 			sizeof(rmt));
    376 		if (error1)
    377 			return (error1);
    378 	}
    379 
    380 	return error;
    381 #else /* !__HAVE_TIMECOUNTER */
    382 	struct sys_nanosleep_args/* {
    383 		syscallarg(struct timespec *) rqtp;
    384 		syscallarg(struct timespec *) rmtp;
    385 	} */ *uap = v;
    386 	struct timespec rqt;
    387 	struct timespec rmt;
    388 	struct timeval atv, utv;
    389 	int error, s, timo;
    390 
    391 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    392 	if (error)
    393 		return (error);
    394 
    395 	TIMESPEC_TO_TIMEVAL(&atv,&rqt);
    396 	if (itimerfix(&atv))
    397 		return (EINVAL);
    398 
    399 	s = splclock();
    400 	timeradd(&atv,&time,&atv);
    401 	timo = hzto(&atv);
    402 	/*
    403 	 * Avoid inadvertantly sleeping forever
    404 	 */
    405 	if (timo == 0)
    406 		timo = 1;
    407 	splx(s);
    408 
    409 	error = sched_pause("nanoslp", TRUE, timo);
    410 	if (error == ERESTART)
    411 		error = EINTR;
    412 	if (error == EWOULDBLOCK)
    413 		error = 0;
    414 
    415 	if (SCARG(uap, rmtp)) {
    416 		int error1;
    417 
    418 		s = splclock();
    419 		utv = time;
    420 		splx(s);
    421 
    422 		timersub(&atv, &utv, &utv);
    423 		if (utv.tv_sec < 0)
    424 			timerclear(&utv);
    425 
    426 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
    427 		error1 = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
    428 			sizeof(rmt));
    429 		if (error1)
    430 			return (error1);
    431 	}
    432 
    433 	return error;
    434 #endif /* !__HAVE_TIMECOUNTER */
    435 }
    436 
    437 /* ARGSUSED */
    438 int
    439 sys_gettimeofday(struct lwp *l, void *v, register_t *retval)
    440 {
    441 	struct sys_gettimeofday_args /* {
    442 		syscallarg(struct timeval *) tp;
    443 		syscallarg(void *) tzp;		really "struct timezone *"
    444 	} */ *uap = v;
    445 	struct timeval atv;
    446 	int error = 0;
    447 	struct timezone tzfake;
    448 
    449 	if (SCARG(uap, tp)) {
    450 		microtime(&atv);
    451 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    452 		if (error)
    453 			return (error);
    454 	}
    455 	if (SCARG(uap, tzp)) {
    456 		/*
    457 		 * NetBSD has no kernel notion of time zone, so we just
    458 		 * fake up a timezone struct and return it if demanded.
    459 		 */
    460 		tzfake.tz_minuteswest = 0;
    461 		tzfake.tz_dsttime = 0;
    462 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    463 	}
    464 	return (error);
    465 }
    466 
    467 /* ARGSUSED */
    468 int
    469 sys_settimeofday(struct lwp *l, void *v, register_t *retval)
    470 {
    471 	struct sys_settimeofday_args /* {
    472 		syscallarg(const struct timeval *) tv;
    473 		syscallarg(const void *) tzp;	really "const struct timezone *"
    474 	} */ *uap = v;
    475 	int error;
    476 
    477 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    478 	    KAUTH_REQ_SYSTEM_TIME_SYSTEM, NULL, NULL, NULL)) != 0)
    479 		return (error);
    480 
    481 	return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), l->l_proc);
    482 }
    483 
    484 int
    485 settimeofday1(const struct timeval *utv, const struct timezone *utzp,
    486     struct proc *p)
    487 {
    488 	struct timeval atv;
    489 	struct timespec ts;
    490 	int error;
    491 
    492 	/* Verify all parameters before changing time. */
    493 	/*
    494 	 * NetBSD has no kernel notion of time zone, and only an
    495 	 * obsolete program would try to set it, so we log a warning.
    496 	 */
    497 	if (utzp)
    498 		log(LOG_WARNING, "pid %d attempted to set the "
    499 		    "(obsolete) kernel time zone\n", p->p_pid);
    500 
    501 	if (utv == NULL)
    502 		return 0;
    503 
    504 	if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
    505 		return error;
    506 	TIMEVAL_TO_TIMESPEC(&atv, &ts);
    507 	return settime(p, &ts);
    508 }
    509 
    510 #ifndef __HAVE_TIMECOUNTER
    511 int	tickdelta;			/* current clock skew, us. per tick */
    512 long	timedelta;			/* unapplied time correction, us. */
    513 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    514 #endif
    515 
    516 int	time_adjusted;			/* set if an adjustment is made */
    517 
    518 /* ARGSUSED */
    519 int
    520 sys_adjtime(struct lwp *l, void *v, register_t *retval)
    521 {
    522 	struct sys_adjtime_args /* {
    523 		syscallarg(const struct timeval *) delta;
    524 		syscallarg(struct timeval *) olddelta;
    525 	} */ *uap = v;
    526 	int error;
    527 
    528 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    529 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
    530 		return (error);
    531 
    532 	return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), l->l_proc);
    533 }
    534 
    535 int
    536 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
    537 {
    538 	struct timeval atv;
    539 	int error = 0;
    540 
    541 #ifdef __HAVE_TIMECOUNTER
    542 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    543 #else /* !__HAVE_TIMECOUNTER */
    544 	long ndelta, ntickdelta, odelta;
    545 	int s;
    546 #endif /* !__HAVE_TIMECOUNTER */
    547 
    548 #ifdef __HAVE_TIMECOUNTER
    549 	if (olddelta) {
    550 		atv.tv_sec = time_adjtime / 1000000;
    551 		atv.tv_usec = time_adjtime % 1000000;
    552 		if (atv.tv_usec < 0) {
    553 			atv.tv_usec += 1000000;
    554 			atv.tv_sec--;
    555 		}
    556 		error = copyout(&atv, olddelta, sizeof(struct timeval));
    557 		if (error)
    558 			return (error);
    559 	}
    560 
    561 	if (delta) {
    562 		error = copyin(delta, &atv, sizeof(struct timeval));
    563 		if (error)
    564 			return (error);
    565 
    566 		time_adjtime = (int64_t)atv.tv_sec * 1000000 +
    567 			atv.tv_usec;
    568 
    569 		if (time_adjtime)
    570 			/* We need to save the system time during shutdown */
    571 			time_adjusted |= 1;
    572 	}
    573 #else /* !__HAVE_TIMECOUNTER */
    574 	error = copyin(delta, &atv, sizeof(struct timeval));
    575 	if (error)
    576 		return (error);
    577 
    578 	/*
    579 	 * Compute the total correction and the rate at which to apply it.
    580 	 * Round the adjustment down to a whole multiple of the per-tick
    581 	 * delta, so that after some number of incremental changes in
    582 	 * hardclock(), tickdelta will become zero, lest the correction
    583 	 * overshoot and start taking us away from the desired final time.
    584 	 */
    585 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    586 	if (ndelta > bigadj || ndelta < -bigadj)
    587 		ntickdelta = 10 * tickadj;
    588 	else
    589 		ntickdelta = tickadj;
    590 	if (ndelta % ntickdelta)
    591 		ndelta = ndelta / ntickdelta * ntickdelta;
    592 
    593 	/*
    594 	 * To make hardclock()'s job easier, make the per-tick delta negative
    595 	 * if we want time to run slower; then hardclock can simply compute
    596 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    597 	 */
    598 	if (ndelta < 0)
    599 		ntickdelta = -ntickdelta;
    600 	if (ndelta != 0)
    601 		/* We need to save the system clock time during shutdown */
    602 		time_adjusted |= 1;
    603 	s = splclock();
    604 	odelta = timedelta;
    605 	timedelta = ndelta;
    606 	tickdelta = ntickdelta;
    607 	splx(s);
    608 
    609 	if (olddelta) {
    610 		atv.tv_sec = odelta / 1000000;
    611 		atv.tv_usec = odelta % 1000000;
    612 		error = copyout(&atv, olddelta, sizeof(struct timeval));
    613 	}
    614 #endif /* __HAVE_TIMECOUNTER */
    615 
    616 	return error;
    617 }
    618 
    619 /*
    620  * Interval timer support. Both the BSD getitimer() family and the POSIX
    621  * timer_*() family of routines are supported.
    622  *
    623  * All timers are kept in an array pointed to by p_timers, which is
    624  * allocated on demand - many processes don't use timers at all. The
    625  * first three elements in this array are reserved for the BSD timers:
    626  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
    627  * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
    628  * syscall.
    629  *
    630  * Realtime timers are kept in the ptimer structure as an absolute
    631  * time; virtual time timers are kept as a linked list of deltas.
    632  * Virtual time timers are processed in the hardclock() routine of
    633  * kern_clock.c.  The real time timer is processed by a callout
    634  * routine, called from the softclock() routine.  Since a callout may
    635  * be delayed in real time due to interrupt processing in the system,
    636  * it is possible for the real time timeout routine (realtimeexpire,
    637  * given below), to be delayed in real time past when it is supposed
    638  * to occur.  It does not suffice, therefore, to reload the real timer
    639  * .it_value from the real time timers .it_interval.  Rather, we
    640  * compute the next time in absolute time the timer should go off.  */
    641 
    642 /* Allocate a POSIX realtime timer. */
    643 int
    644 sys_timer_create(struct lwp *l, void *v, register_t *retval)
    645 {
    646 	struct sys_timer_create_args /* {
    647 		syscallarg(clockid_t) clock_id;
    648 		syscallarg(struct sigevent *) evp;
    649 		syscallarg(timer_t *) timerid;
    650 	} */ *uap = v;
    651 
    652 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
    653 	    SCARG(uap, evp), copyin, l);
    654 }
    655 
    656 int
    657 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
    658     copyin_t fetch_event, struct lwp *l)
    659 {
    660 	int error;
    661 	timer_t timerid;
    662 	struct ptimer *pt;
    663 	struct proc *p;
    664 
    665 	p = l->l_proc;
    666 
    667 	if (id < CLOCK_REALTIME ||
    668 	    id > CLOCK_PROF)
    669 		return (EINVAL);
    670 
    671 	if (p->p_timers == NULL)
    672 		timers_alloc(p);
    673 
    674 	/* Find a free timer slot, skipping those reserved for setitimer(). */
    675 	for (timerid = 3; timerid < TIMER_MAX; timerid++)
    676 		if (p->p_timers->pts_timers[timerid] == NULL)
    677 			break;
    678 
    679 	if (timerid == TIMER_MAX)
    680 		return EAGAIN;
    681 
    682 	pt = pool_get(&ptimer_pool, PR_WAITOK);
    683 	if (evp) {
    684 		if (((error =
    685 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
    686 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
    687 			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
    688 			pool_put(&ptimer_pool, pt);
    689 			return (error ? error : EINVAL);
    690 		}
    691 	} else {
    692 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
    693 		switch (id) {
    694 		case CLOCK_REALTIME:
    695 			pt->pt_ev.sigev_signo = SIGALRM;
    696 			break;
    697 		case CLOCK_VIRTUAL:
    698 			pt->pt_ev.sigev_signo = SIGVTALRM;
    699 			break;
    700 		case CLOCK_PROF:
    701 			pt->pt_ev.sigev_signo = SIGPROF;
    702 			break;
    703 		}
    704 		pt->pt_ev.sigev_value.sival_int = timerid;
    705 	}
    706 	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
    707 	pt->pt_info.ksi_errno = 0;
    708 	pt->pt_info.ksi_code = 0;
    709 	pt->pt_info.ksi_pid = p->p_pid;
    710 	pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
    711 	pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
    712 
    713 	pt->pt_type = id;
    714 	pt->pt_proc = p;
    715 	pt->pt_overruns = 0;
    716 	pt->pt_poverruns = 0;
    717 	pt->pt_entry = timerid;
    718 	timerclear(&pt->pt_time.it_value);
    719 	if (id == CLOCK_REALTIME)
    720 		callout_init(&pt->pt_ch);
    721 	else
    722 		pt->pt_active = 0;
    723 
    724 	p->p_timers->pts_timers[timerid] = pt;
    725 
    726 	return copyout(&timerid, tid, sizeof(timerid));
    727 }
    728 
    729 /* Delete a POSIX realtime timer */
    730 int
    731 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
    732 {
    733 	struct sys_timer_delete_args /*  {
    734 		syscallarg(timer_t) timerid;
    735 	} */ *uap = v;
    736 	struct proc *p = l->l_proc;
    737 	timer_t timerid;
    738 	struct ptimer *pt, *ptn;
    739 	int s;
    740 
    741 	timerid = SCARG(uap, timerid);
    742 
    743 	if ((p->p_timers == NULL) ||
    744 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    745 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    746 		return (EINVAL);
    747 
    748 	if (pt->pt_type == CLOCK_REALTIME)
    749 		callout_stop(&pt->pt_ch);
    750 	else if (pt->pt_active) {
    751 		s = splclock();
    752 		ptn = LIST_NEXT(pt, pt_list);
    753 		LIST_REMOVE(pt, pt_list);
    754 		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    755 			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
    756 			    &ptn->pt_time.it_value);
    757 		splx(s);
    758 	}
    759 
    760 	p->p_timers->pts_timers[timerid] = NULL;
    761 	pool_put(&ptimer_pool, pt);
    762 
    763 	return (0);
    764 }
    765 
    766 /*
    767  * Set up the given timer. The value in pt->pt_time.it_value is taken
    768  * to be an absolute time for CLOCK_REALTIME timers and a relative
    769  * time for virtual timers.
    770  * Must be called at splclock().
    771  */
    772 void
    773 timer_settime(struct ptimer *pt)
    774 {
    775 	struct ptimer *ptn, *pptn;
    776 	struct ptlist *ptl;
    777 
    778 	if (pt->pt_type == CLOCK_REALTIME) {
    779 		callout_stop(&pt->pt_ch);
    780 		if (timerisset(&pt->pt_time.it_value)) {
    781 			/*
    782 			 * Don't need to check hzto() return value, here.
    783 			 * callout_reset() does it for us.
    784 			 */
    785 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
    786 			    realtimerexpire, pt);
    787 		}
    788 	} else {
    789 		if (pt->pt_active) {
    790 			ptn = LIST_NEXT(pt, pt_list);
    791 			LIST_REMOVE(pt, pt_list);
    792 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    793 				timeradd(&pt->pt_time.it_value,
    794 				    &ptn->pt_time.it_value,
    795 				    &ptn->pt_time.it_value);
    796 		}
    797 		if (timerisset(&pt->pt_time.it_value)) {
    798 			if (pt->pt_type == CLOCK_VIRTUAL)
    799 				ptl = &pt->pt_proc->p_timers->pts_virtual;
    800 			else
    801 				ptl = &pt->pt_proc->p_timers->pts_prof;
    802 
    803 			for (ptn = LIST_FIRST(ptl), pptn = NULL;
    804 			     ptn && timercmp(&pt->pt_time.it_value,
    805 				 &ptn->pt_time.it_value, >);
    806 			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
    807 				timersub(&pt->pt_time.it_value,
    808 				    &ptn->pt_time.it_value,
    809 				    &pt->pt_time.it_value);
    810 
    811 			if (pptn)
    812 				LIST_INSERT_AFTER(pptn, pt, pt_list);
    813 			else
    814 				LIST_INSERT_HEAD(ptl, pt, pt_list);
    815 
    816 			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
    817 				timersub(&ptn->pt_time.it_value,
    818 				    &pt->pt_time.it_value,
    819 				    &ptn->pt_time.it_value);
    820 
    821 			pt->pt_active = 1;
    822 		} else
    823 			pt->pt_active = 0;
    824 	}
    825 }
    826 
    827 void
    828 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
    829 {
    830 #ifdef __HAVE_TIMECOUNTER
    831 	struct timeval now;
    832 #endif
    833 	struct ptimer *ptn;
    834 
    835 	*aitv = pt->pt_time;
    836 	if (pt->pt_type == CLOCK_REALTIME) {
    837 		/*
    838 		 * Convert from absolute to relative time in .it_value
    839 		 * part of real time timer.  If time for real time
    840 		 * timer has passed return 0, else return difference
    841 		 * between current time and time for the timer to go
    842 		 * off.
    843 		 */
    844 		if (timerisset(&aitv->it_value)) {
    845 #ifdef __HAVE_TIMECOUNTER
    846 			getmicrotime(&now);
    847 			if (timercmp(&aitv->it_value, &now, <))
    848 				timerclear(&aitv->it_value);
    849 			else
    850 				timersub(&aitv->it_value, &now,
    851 				    &aitv->it_value);
    852 #else /* !__HAVE_TIMECOUNTER */
    853 			if (timercmp(&aitv->it_value, &time, <))
    854 				timerclear(&aitv->it_value);
    855 			else
    856 				timersub(&aitv->it_value, &time,
    857 				    &aitv->it_value);
    858 #endif /* !__HAVE_TIMECOUNTER */
    859 		}
    860 	} else if (pt->pt_active) {
    861 		if (pt->pt_type == CLOCK_VIRTUAL)
    862 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
    863 		else
    864 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
    865 		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
    866 			timeradd(&aitv->it_value,
    867 			    &ptn->pt_time.it_value, &aitv->it_value);
    868 		KASSERT(ptn != NULL); /* pt should be findable on the list */
    869 	} else
    870 		timerclear(&aitv->it_value);
    871 }
    872 
    873 
    874 
    875 /* Set and arm a POSIX realtime timer */
    876 int
    877 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
    878 {
    879 	struct sys_timer_settime_args /* {
    880 		syscallarg(timer_t) timerid;
    881 		syscallarg(int) flags;
    882 		syscallarg(const struct itimerspec *) value;
    883 		syscallarg(struct itimerspec *) ovalue;
    884 	} */ *uap = v;
    885 	int error;
    886 	struct itimerspec value, ovalue, *ovp = NULL;
    887 
    888 	if ((error = copyin(SCARG(uap, value), &value,
    889 	    sizeof(struct itimerspec))) != 0)
    890 		return (error);
    891 
    892 	if (SCARG(uap, ovalue))
    893 		ovp = &ovalue;
    894 
    895 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    896 	    SCARG(uap, flags), l->l_proc)) != 0)
    897 		return error;
    898 
    899 	if (ovp)
    900 		return copyout(&ovalue, SCARG(uap, ovalue),
    901 		    sizeof(struct itimerspec));
    902 	return 0;
    903 }
    904 
    905 int
    906 dotimer_settime(int timerid, struct itimerspec *value,
    907     struct itimerspec *ovalue, int flags, struct proc *p)
    908 {
    909 #ifdef __HAVE_TIMECOUNTER
    910 	struct timeval now;
    911 #endif
    912 	struct itimerval val, oval;
    913 	struct ptimer *pt;
    914 	int s;
    915 
    916 	if ((p->p_timers == NULL) ||
    917 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    918 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    919 		return (EINVAL);
    920 
    921 	TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
    922 	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
    923 	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
    924 		return (EINVAL);
    925 
    926 	oval = pt->pt_time;
    927 	pt->pt_time = val;
    928 
    929 	s = splclock();
    930 	/*
    931 	 * If we've been passed a relative time for a realtime timer,
    932 	 * convert it to absolute; if an absolute time for a virtual
    933 	 * timer, convert it to relative and make sure we don't set it
    934 	 * to zero, which would cancel the timer, or let it go
    935 	 * negative, which would confuse the comparison tests.
    936 	 */
    937 	if (timerisset(&pt->pt_time.it_value)) {
    938 		if (pt->pt_type == CLOCK_REALTIME) {
    939 #ifdef __HAVE_TIMECOUNTER
    940 			if ((flags & TIMER_ABSTIME) == 0) {
    941 				getmicrotime(&now);
    942 				timeradd(&pt->pt_time.it_value, &now,
    943 				    &pt->pt_time.it_value);
    944 			}
    945 #else /* !__HAVE_TIMECOUNTER */
    946 			if ((flags & TIMER_ABSTIME) == 0)
    947 				timeradd(&pt->pt_time.it_value, &time,
    948 				    &pt->pt_time.it_value);
    949 #endif /* !__HAVE_TIMECOUNTER */
    950 		} else {
    951 			if ((flags & TIMER_ABSTIME) != 0) {
    952 #ifdef __HAVE_TIMECOUNTER
    953 				getmicrotime(&now);
    954 				timersub(&pt->pt_time.it_value, &now,
    955 				    &pt->pt_time.it_value);
    956 #else /* !__HAVE_TIMECOUNTER */
    957 				timersub(&pt->pt_time.it_value, &time,
    958 				    &pt->pt_time.it_value);
    959 #endif /* !__HAVE_TIMECOUNTER */
    960 				if (!timerisset(&pt->pt_time.it_value) ||
    961 				    pt->pt_time.it_value.tv_sec < 0) {
    962 					pt->pt_time.it_value.tv_sec = 0;
    963 					pt->pt_time.it_value.tv_usec = 1;
    964 				}
    965 			}
    966 		}
    967 	}
    968 
    969 	timer_settime(pt);
    970 	splx(s);
    971 
    972 	if (ovalue) {
    973 		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
    974 		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
    975 	}
    976 
    977 	return (0);
    978 }
    979 
    980 /* Return the time remaining until a POSIX timer fires. */
    981 int
    982 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
    983 {
    984 	struct sys_timer_gettime_args /* {
    985 		syscallarg(timer_t) timerid;
    986 		syscallarg(struct itimerspec *) value;
    987 	} */ *uap = v;
    988 	struct itimerspec its;
    989 	int error;
    990 
    991 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    992 	    &its)) != 0)
    993 		return error;
    994 
    995 	return copyout(&its, SCARG(uap, value), sizeof(its));
    996 }
    997 
    998 int
    999 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
   1000 {
   1001 	int s;
   1002 	struct ptimer *pt;
   1003 	struct itimerval aitv;
   1004 
   1005 	if ((p->p_timers == NULL) ||
   1006 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
   1007 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
   1008 		return (EINVAL);
   1009 
   1010 	s = splclock();
   1011 	timer_gettime(pt, &aitv);
   1012 	splx(s);
   1013 
   1014 	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
   1015 	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
   1016 
   1017 	return 0;
   1018 }
   1019 
   1020 /*
   1021  * Return the count of the number of times a periodic timer expired
   1022  * while a notification was already pending. The counter is reset when
   1023  * a timer expires and a notification can be posted.
   1024  */
   1025 int
   1026 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
   1027 {
   1028 	struct sys_timer_getoverrun_args /* {
   1029 		syscallarg(timer_t) timerid;
   1030 	} */ *uap = v;
   1031 	struct proc *p = l->l_proc;
   1032 	int timerid;
   1033 	struct ptimer *pt;
   1034 
   1035 	timerid = SCARG(uap, timerid);
   1036 
   1037 	if ((p->p_timers == NULL) ||
   1038 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
   1039 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
   1040 		return (EINVAL);
   1041 
   1042 	*retval = pt->pt_poverruns;
   1043 
   1044 	return (0);
   1045 }
   1046 
   1047 /* Glue function that triggers an upcall; called from userret(). */
   1048 void
   1049 timerupcall(struct lwp *l)
   1050 {
   1051 	struct ptimers *pt = l->l_proc->p_timers;
   1052 	unsigned int i, fired, done;
   1053 
   1054 	KDASSERT(l->l_proc->p_sa);
   1055 	/* Bail out if we do not own the virtual processor */
   1056 	if (l->l_savp->savp_lwp != l)
   1057 		return ;
   1058 
   1059 	KERNEL_LOCK(1, l);
   1060 
   1061 	fired = pt->pts_fired;
   1062 	done = 0;
   1063 	while ((i = ffs(fired)) != 0) {
   1064 		siginfo_t *si;
   1065 		int mask = 1 << --i;
   1066 		int f;
   1067 
   1068 		lwp_lock(l);
   1069 		f = l->l_flag & L_SA;
   1070 		l->l_flag &= ~L_SA;
   1071 		lwp_unlock(l);
   1072 		si = siginfo_alloc(PR_WAITOK);
   1073 		si->_info = pt->pts_timers[i]->pt_info.ksi_info;
   1074 		if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
   1075 		    sizeof(*si), si, siginfo_free) != 0) {
   1076 			siginfo_free(si);
   1077 			/* XXX What do we do here?? */
   1078 		} else
   1079 			done |= mask;
   1080 		fired &= ~mask;
   1081 		lwp_lock(l);
   1082 		l->l_flag |= f;
   1083 		lwp_unlock(l);
   1084 	}
   1085 	pt->pts_fired &= ~done;
   1086 	if (pt->pts_fired == 0)
   1087 		l->l_proc->p_timerpend = 0;
   1088 
   1089 	KERNEL_UNLOCK_LAST(l);
   1090 }
   1091 
   1092 /*
   1093  * Real interval timer expired:
   1094  * send process whose timer expired an alarm signal.
   1095  * If time is not set up to reload, then just return.
   1096  * Else compute next time timer should go off which is > current time.
   1097  * This is where delay in processing this timeout causes multiple
   1098  * SIGALRM calls to be compressed into one.
   1099  */
   1100 void
   1101 realtimerexpire(void *arg)
   1102 {
   1103 #ifdef __HAVE_TIMECOUNTER
   1104 	struct timeval now;
   1105 #endif
   1106 	struct ptimer *pt;
   1107 	int s;
   1108 
   1109 	pt = (struct ptimer *)arg;
   1110 
   1111 	itimerfire(pt);
   1112 
   1113 	if (!timerisset(&pt->pt_time.it_interval)) {
   1114 		timerclear(&pt->pt_time.it_value);
   1115 		return;
   1116 	}
   1117 #ifdef __HAVE_TIMECOUNTER
   1118 	for (;;) {
   1119 		s = splclock();	/* XXX need spl now? */
   1120 		timeradd(&pt->pt_time.it_value,
   1121 		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
   1122 		getmicrotime(&now);
   1123 		if (timercmp(&pt->pt_time.it_value, &now, >)) {
   1124 			/*
   1125 			 * Don't need to check hzto() return value, here.
   1126 			 * callout_reset() does it for us.
   1127 			 */
   1128 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
   1129 			    realtimerexpire, pt);
   1130 			splx(s);
   1131 			return;
   1132 		}
   1133 		splx(s);
   1134 		pt->pt_overruns++;
   1135 	}
   1136 #else /* !__HAVE_TIMECOUNTER */
   1137 	for (;;) {
   1138 		s = splclock();
   1139 		timeradd(&pt->pt_time.it_value,
   1140 		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
   1141 		if (timercmp(&pt->pt_time.it_value, &time, >)) {
   1142 			/*
   1143 			 * Don't need to check hzto() return value, here.
   1144 			 * callout_reset() does it for us.
   1145 			 */
   1146 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
   1147 			    realtimerexpire, pt);
   1148 			splx(s);
   1149 			return;
   1150 		}
   1151 		splx(s);
   1152 		pt->pt_overruns++;
   1153 	}
   1154 #endif /* !__HAVE_TIMECOUNTER */
   1155 }
   1156 
   1157 /* BSD routine to get the value of an interval timer. */
   1158 /* ARGSUSED */
   1159 int
   1160 sys_getitimer(struct lwp *l, void *v, register_t *retval)
   1161 {
   1162 	struct sys_getitimer_args /* {
   1163 		syscallarg(int) which;
   1164 		syscallarg(struct itimerval *) itv;
   1165 	} */ *uap = v;
   1166 	struct proc *p = l->l_proc;
   1167 	struct itimerval aitv;
   1168 	int error;
   1169 
   1170 	error = dogetitimer(p, SCARG(uap, which), &aitv);
   1171 	if (error)
   1172 		return error;
   1173 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
   1174 }
   1175 
   1176 int
   1177 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
   1178 {
   1179 	int s;
   1180 
   1181 	if ((u_int)which > ITIMER_PROF)
   1182 		return (EINVAL);
   1183 
   1184 	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
   1185 		timerclear(&itvp->it_value);
   1186 		timerclear(&itvp->it_interval);
   1187 	} else {
   1188 		s = splclock();
   1189 		timer_gettime(p->p_timers->pts_timers[which], itvp);
   1190 		splx(s);
   1191 	}
   1192 
   1193 	return 0;
   1194 }
   1195 
   1196 /* BSD routine to set/arm an interval timer. */
   1197 /* ARGSUSED */
   1198 int
   1199 sys_setitimer(struct lwp *l, void *v, register_t *retval)
   1200 {
   1201 	struct sys_setitimer_args /* {
   1202 		syscallarg(int) which;
   1203 		syscallarg(const struct itimerval *) itv;
   1204 		syscallarg(struct itimerval *) oitv;
   1205 	} */ *uap = v;
   1206 	struct proc *p = l->l_proc;
   1207 	int which = SCARG(uap, which);
   1208 	struct sys_getitimer_args getargs;
   1209 	const struct itimerval *itvp;
   1210 	struct itimerval aitv;
   1211 	int error;
   1212 
   1213 	if ((u_int)which > ITIMER_PROF)
   1214 		return (EINVAL);
   1215 	itvp = SCARG(uap, itv);
   1216 	if (itvp &&
   1217 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
   1218 		return (error);
   1219 	if (SCARG(uap, oitv) != NULL) {
   1220 		SCARG(&getargs, which) = which;
   1221 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   1222 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
   1223 			return (error);
   1224 	}
   1225 	if (itvp == 0)
   1226 		return (0);
   1227 
   1228 	return dosetitimer(p, which, &aitv);
   1229 }
   1230 
   1231 int
   1232 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
   1233 {
   1234 #ifdef __HAVE_TIMECOUNTER
   1235 	struct timeval now;
   1236 #endif
   1237 	struct ptimer *pt;
   1238 	int s;
   1239 
   1240 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
   1241 		return (EINVAL);
   1242 
   1243 	/*
   1244 	 * Don't bother allocating data structures if the process just
   1245 	 * wants to clear the timer.
   1246 	 */
   1247 	if (!timerisset(&itvp->it_value) &&
   1248 	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
   1249 		return (0);
   1250 
   1251 	if (p->p_timers == NULL)
   1252 		timers_alloc(p);
   1253 	if (p->p_timers->pts_timers[which] == NULL) {
   1254 		pt = pool_get(&ptimer_pool, PR_WAITOK);
   1255 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1256 		pt->pt_ev.sigev_value.sival_int = which;
   1257 		pt->pt_overruns = 0;
   1258 		pt->pt_proc = p;
   1259 		pt->pt_type = which;
   1260 		pt->pt_entry = which;
   1261 		switch (which) {
   1262 		case ITIMER_REAL:
   1263 			callout_init(&pt->pt_ch);
   1264 			pt->pt_ev.sigev_signo = SIGALRM;
   1265 			break;
   1266 		case ITIMER_VIRTUAL:
   1267 			pt->pt_active = 0;
   1268 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1269 			break;
   1270 		case ITIMER_PROF:
   1271 			pt->pt_active = 0;
   1272 			pt->pt_ev.sigev_signo = SIGPROF;
   1273 			break;
   1274 		}
   1275 	} else
   1276 		pt = p->p_timers->pts_timers[which];
   1277 
   1278 	pt->pt_time = *itvp;
   1279 	p->p_timers->pts_timers[which] = pt;
   1280 
   1281 	s = splclock();
   1282 	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
   1283 		/* Convert to absolute time */
   1284 #ifdef __HAVE_TIMECOUNTER
   1285 		/* XXX need to wrap in splclock for timecounters case? */
   1286 		getmicrotime(&now);
   1287 		timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
   1288 #else /* !__HAVE_TIMECOUNTER */
   1289 		timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
   1290 #endif /* !__HAVE_TIMECOUNTER */
   1291 	}
   1292 	timer_settime(pt);
   1293 	splx(s);
   1294 
   1295 	return (0);
   1296 }
   1297 
   1298 /* Utility routines to manage the array of pointers to timers. */
   1299 void
   1300 timers_alloc(struct proc *p)
   1301 {
   1302 	int i;
   1303 	struct ptimers *pts;
   1304 
   1305 	pts = pool_get(&ptimers_pool, PR_WAITOK);
   1306 	LIST_INIT(&pts->pts_virtual);
   1307 	LIST_INIT(&pts->pts_prof);
   1308 	for (i = 0; i < TIMER_MAX; i++)
   1309 		pts->pts_timers[i] = NULL;
   1310 	pts->pts_fired = 0;
   1311 	p->p_timers = pts;
   1312 }
   1313 
   1314 /*
   1315  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
   1316  * then clean up all timers and free all the data structures. If
   1317  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
   1318  * by timer_create(), not the BSD setitimer() timers, and only free the
   1319  * structure if none of those remain.
   1320  */
   1321 void
   1322 timers_free(struct proc *p, int which)
   1323 {
   1324 	int i, s;
   1325 	struct ptimers *pts;
   1326 	struct ptimer *pt, *ptn;
   1327 	struct timeval tv;
   1328 
   1329 	if (p->p_timers) {
   1330 		pts = p->p_timers;
   1331 		if (which == TIMERS_ALL)
   1332 			i = 0;
   1333 		else {
   1334 			s = splclock();
   1335 			timerclear(&tv);
   1336 			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
   1337 			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
   1338 			     ptn = LIST_NEXT(ptn, pt_list))
   1339 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
   1340 			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
   1341 			if (ptn) {
   1342 				timeradd(&tv, &ptn->pt_time.it_value,
   1343 				    &ptn->pt_time.it_value);
   1344 				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
   1345 				    ptn, pt_list);
   1346 			}
   1347 
   1348 			timerclear(&tv);
   1349 			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
   1350 			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
   1351 			     ptn = LIST_NEXT(ptn, pt_list))
   1352 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
   1353 			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
   1354 			if (ptn) {
   1355 				timeradd(&tv, &ptn->pt_time.it_value,
   1356 				    &ptn->pt_time.it_value);
   1357 				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
   1358 				    pt_list);
   1359 			}
   1360 			splx(s);
   1361 			i = 3;
   1362 		}
   1363 		for ( ; i < TIMER_MAX; i++)
   1364 			if ((pt = pts->pts_timers[i]) != NULL) {
   1365 				if (pt->pt_type == CLOCK_REALTIME)
   1366 					callout_stop(&pt->pt_ch);
   1367 				pts->pts_timers[i] = NULL;
   1368 				pool_put(&ptimer_pool, pt);
   1369 			}
   1370 		if ((pts->pts_timers[0] == NULL) &&
   1371 		    (pts->pts_timers[1] == NULL) &&
   1372 		    (pts->pts_timers[2] == NULL)) {
   1373 			p->p_timers = NULL;
   1374 			pool_put(&ptimers_pool, pts);
   1375 		}
   1376 	}
   1377 }
   1378 
   1379 /*
   1380  * Check that a proposed value to load into the .it_value or
   1381  * .it_interval part of an interval timer is acceptable, and
   1382  * fix it to have at least minimal value (i.e. if it is less
   1383  * than the resolution of the clock, round it up.)
   1384  */
   1385 int
   1386 itimerfix(struct timeval *tv)
   1387 {
   1388 
   1389 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
   1390 		return (EINVAL);
   1391 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
   1392 		tv->tv_usec = tick;
   1393 	return (0);
   1394 }
   1395 
   1396 #ifdef __HAVE_TIMECOUNTER
   1397 int
   1398 itimespecfix(struct timespec *ts)
   1399 {
   1400 
   1401 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
   1402 		return (EINVAL);
   1403 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
   1404 		ts->tv_nsec = tick * 1000;
   1405 	return (0);
   1406 }
   1407 #endif /* __HAVE_TIMECOUNTER */
   1408 
   1409 /*
   1410  * Decrement an interval timer by a specified number
   1411  * of microseconds, which must be less than a second,
   1412  * i.e. < 1000000.  If the timer expires, then reload
   1413  * it.  In this case, carry over (usec - old value) to
   1414  * reduce the value reloaded into the timer so that
   1415  * the timer does not drift.  This routine assumes
   1416  * that it is called in a context where the timers
   1417  * on which it is operating cannot change in value.
   1418  */
   1419 int
   1420 itimerdecr(struct ptimer *pt, int usec)
   1421 {
   1422 	struct itimerval *itp;
   1423 
   1424 	itp = &pt->pt_time;
   1425 	if (itp->it_value.tv_usec < usec) {
   1426 		if (itp->it_value.tv_sec == 0) {
   1427 			/* expired, and already in next interval */
   1428 			usec -= itp->it_value.tv_usec;
   1429 			goto expire;
   1430 		}
   1431 		itp->it_value.tv_usec += 1000000;
   1432 		itp->it_value.tv_sec--;
   1433 	}
   1434 	itp->it_value.tv_usec -= usec;
   1435 	usec = 0;
   1436 	if (timerisset(&itp->it_value))
   1437 		return (1);
   1438 	/* expired, exactly at end of interval */
   1439 expire:
   1440 	if (timerisset(&itp->it_interval)) {
   1441 		itp->it_value = itp->it_interval;
   1442 		itp->it_value.tv_usec -= usec;
   1443 		if (itp->it_value.tv_usec < 0) {
   1444 			itp->it_value.tv_usec += 1000000;
   1445 			itp->it_value.tv_sec--;
   1446 		}
   1447 		timer_settime(pt);
   1448 	} else
   1449 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
   1450 	return (0);
   1451 }
   1452 
   1453 void
   1454 itimerfire(struct ptimer *pt)
   1455 {
   1456 	struct proc *p = pt->pt_proc;
   1457 	struct sadata_vp *vp;
   1458 	unsigned int i;
   1459 
   1460 	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
   1461 		/*
   1462 		 * No RT signal infrastructure exists at this time;
   1463 		 * just post the signal number and throw away the
   1464 		 * value.
   1465 		 */
   1466 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo))
   1467 			pt->pt_overruns++;
   1468 		else {
   1469 			ksiginfo_t ksi;
   1470 			(void)memset(&ksi, 0, sizeof(ksi));
   1471 			ksi.ksi_signo = pt->pt_ev.sigev_signo;
   1472 			ksi.ksi_code = SI_TIMER;
   1473 			ksi.ksi_sigval = pt->pt_ev.sigev_value;
   1474 			pt->pt_poverruns = pt->pt_overruns;
   1475 			pt->pt_overruns = 0;
   1476 			mutex_enter(&proclist_mutex);
   1477 			kpsignal(p, &ksi, NULL);
   1478 			mutex_exit(&proclist_mutex);
   1479 		}
   1480 	} else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_sflag & PS_SA)) {
   1481 		/* Cause the process to generate an upcall when it returns. */
   1482 		if (!p->p_timerpend) {
   1483 			/*
   1484 			 * XXX stop signals can be processed inside tsleep,
   1485 			 * which can be inside sa_yield's inner loop, which
   1486 			 * makes testing for sa_idle alone insuffucent to
   1487 			 * determine if we really should call setrunnable.
   1488 			 */
   1489 			pt->pt_poverruns = pt->pt_overruns;
   1490 			pt->pt_overruns = 0;
   1491 			i = 1 << pt->pt_entry;
   1492 			p->p_timers->pts_fired = i;
   1493 			p->p_timerpend = 1;
   1494 
   1495 			mutex_enter(&p->p_smutex);
   1496 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1497 				lwp_need_userret(vp->savp_lwp);
   1498 				lwp_lock(vp->savp_lwp);
   1499 				if (vp->savp_lwp->l_flag & L_SA_IDLE) {
   1500 					vp->savp_lwp->l_flag &= ~L_SA_IDLE;
   1501 					lwp_unlock(vp->savp_lwp);
   1502 					wakeup(vp->savp_lwp);
   1503 					break;
   1504 				}
   1505 				lwp_unlock(vp->savp_lwp);
   1506 			}
   1507 			mutex_exit(&p->p_smutex);
   1508 		} else {
   1509 			i = 1 << pt->pt_entry;
   1510 			if ((p->p_timers->pts_fired & i) == 0) {
   1511 				pt->pt_poverruns = pt->pt_overruns;
   1512 				pt->pt_overruns = 0;
   1513 				p->p_timers->pts_fired |= i;
   1514 			} else
   1515 				pt->pt_overruns++;
   1516 		}
   1517 	}
   1518 }
   1519 
   1520 /*
   1521  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
   1522  * for usage and rationale.
   1523  */
   1524 int
   1525 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
   1526 {
   1527 	struct timeval tv, delta;
   1528 	int rv = 0;
   1529 #ifndef __HAVE_TIMECOUNTER
   1530 	int s;
   1531 #endif
   1532 
   1533 #ifdef __HAVE_TIMECOUNTER
   1534 	getmicrouptime(&tv);
   1535 #else /* !__HAVE_TIMECOUNTER */
   1536 	s = splclock();
   1537 	tv = mono_time;
   1538 	splx(s);
   1539 #endif /* !__HAVE_TIMECOUNTER */
   1540 	timersub(&tv, lasttime, &delta);
   1541 
   1542 	/*
   1543 	 * check for 0,0 is so that the message will be seen at least once,
   1544 	 * even if interval is huge.
   1545 	 */
   1546 	if (timercmp(&delta, mininterval, >=) ||
   1547 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
   1548 		*lasttime = tv;
   1549 		rv = 1;
   1550 	}
   1551 
   1552 	return (rv);
   1553 }
   1554 
   1555 /*
   1556  * ppsratecheck(): packets (or events) per second limitation.
   1557  */
   1558 int
   1559 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
   1560 {
   1561 	struct timeval tv, delta;
   1562 	int rv;
   1563 #ifndef __HAVE_TIMECOUNTER
   1564 	int s;
   1565 #endif
   1566 
   1567 #ifdef __HAVE_TIMECOUNTER
   1568 	getmicrouptime(&tv);
   1569 #else /* !__HAVE_TIMECOUNTER */
   1570 	s = splclock();
   1571 	tv = mono_time;
   1572 	splx(s);
   1573 #endif /* !__HAVE_TIMECOUNTER */
   1574 	timersub(&tv, lasttime, &delta);
   1575 
   1576 	/*
   1577 	 * check for 0,0 is so that the message will be seen at least once.
   1578 	 * if more than one second have passed since the last update of
   1579 	 * lasttime, reset the counter.
   1580 	 *
   1581 	 * we do increment *curpps even in *curpps < maxpps case, as some may
   1582 	 * try to use *curpps for stat purposes as well.
   1583 	 */
   1584 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
   1585 	    delta.tv_sec >= 1) {
   1586 		*lasttime = tv;
   1587 		*curpps = 0;
   1588 	}
   1589 	if (maxpps < 0)
   1590 		rv = 1;
   1591 	else if (*curpps < maxpps)
   1592 		rv = 1;
   1593 	else
   1594 		rv = 0;
   1595 
   1596 #if 1 /*DIAGNOSTIC?*/
   1597 	/* be careful about wrap-around */
   1598 	if (*curpps + 1 > *curpps)
   1599 		*curpps = *curpps + 1;
   1600 #else
   1601 	/*
   1602 	 * assume that there's not too many calls to this function.
   1603 	 * not sure if the assumption holds, as it depends on *caller's*
   1604 	 * behavior, not the behavior of this function.
   1605 	 * IMHO it is wrong to make assumption on the caller's behavior,
   1606 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
   1607 	 */
   1608 	*curpps = *curpps + 1;
   1609 #endif
   1610 
   1611 	return (rv);
   1612 }
   1613