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