Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.54.2.4
      1 /*	$NetBSD: kern_time.c,v 1.54.2.4 2001/11/14 19:16:39 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 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. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.54.2.4 2001/11/14 19:16:39 nathanw Exp $");
     76 
     77 #include "fs_nfs.h"
     78 #include "opt_nfs.h"
     79 #include "opt_nfsserver.h"
     80 
     81 #include <sys/param.h>
     82 #include <sys/resourcevar.h>
     83 #include <sys/kernel.h>
     84 #include <sys/systm.h>
     85 #include <sys/lwp.h>
     86 #include <sys/proc.h>
     87 #include <sys/vnode.h>
     88 #include <sys/signalvar.h>
     89 #include <sys/syslog.h>
     90 
     91 #include <sys/mount.h>
     92 #include <sys/syscallargs.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #if defined(NFS) || defined(NFSSERVER)
     97 #include <nfs/rpcv2.h>
     98 #include <nfs/nfsproto.h>
     99 #include <nfs/nfs_var.h>
    100 #endif
    101 
    102 #include <machine/cpu.h>
    103 
    104 /*
    105  * Time of day and interval timer support.
    106  *
    107  * These routines provide the kernel entry points to get and set
    108  * the time-of-day and per-process interval timers.  Subroutines
    109  * here provide support for adding and subtracting timeval structures
    110  * and decrementing interval timers, optionally reloading the interval
    111  * timers when they expire.
    112  */
    113 
    114 /* This function is used by clock_settime and settimeofday */
    115 int
    116 settime(tv)
    117 	struct timeval *tv;
    118 {
    119 	struct timeval delta;
    120 	struct cpu_info *ci;
    121 	int s;
    122 
    123 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    124 	s = splclock();
    125 	timersub(tv, &time, &delta);
    126 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
    127 		splx(s);
    128 		return (EPERM);
    129 	}
    130 #ifdef notyet
    131 	if ((delta.tv_sec < 86400) && securelevel > 0) {
    132 		splx(s);
    133 		return (EPERM);
    134 	}
    135 #endif
    136 	time = *tv;
    137 	(void) spllowersoftclock();
    138 	timeradd(&boottime, &delta, &boottime);
    139 	/*
    140 	 * XXXSMP
    141 	 * This is wrong.  We should traverse a list of all
    142 	 * CPUs and add the delta to the runtime of those
    143 	 * CPUs which have a process on them.
    144 	 */
    145 	ci = curcpu();
    146 	timeradd(&ci->ci_schedstate.spc_runtime, &delta,
    147 	    &ci->ci_schedstate.spc_runtime);
    148 #	if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
    149 		nqnfs_lease_updatetime(delta.tv_sec);
    150 #	endif
    151 	splx(s);
    152 	resettodr();
    153 	return (0);
    154 }
    155 
    156 /* ARGSUSED */
    157 int
    158 sys_clock_gettime(l, v, retval)
    159 	struct lwp *l;
    160 	void *v;
    161 	register_t *retval;
    162 {
    163 	struct sys_clock_gettime_args /* {
    164 		syscallarg(clockid_t) clock_id;
    165 		syscallarg(struct timespec *) tp;
    166 	} */ *uap = v;
    167 	clockid_t clock_id;
    168 	struct timeval atv;
    169 	struct timespec ats;
    170 
    171 	clock_id = SCARG(uap, clock_id);
    172 	if (clock_id != CLOCK_REALTIME)
    173 		return (EINVAL);
    174 
    175 	microtime(&atv);
    176 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
    177 
    178 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    179 }
    180 
    181 /* ARGSUSED */
    182 int
    183 sys_clock_settime(l, v, retval)
    184 	struct lwp *l;
    185 	void *v;
    186 	register_t *retval;
    187 {
    188 	struct sys_clock_settime_args /* {
    189 		syscallarg(clockid_t) clock_id;
    190 		syscallarg(const struct timespec *) tp;
    191 	} */ *uap = v;
    192 	struct proc *p = l->l_proc;
    193 	clockid_t clock_id;
    194 	struct timespec ats;
    195 	int error;
    196 
    197 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    198 		return (error);
    199 
    200 	clock_id = SCARG(uap, clock_id);
    201 
    202 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
    203 		return (error);
    204 
    205 	return (clock_settime1(clock_id, &ats));
    206 }
    207 
    208 
    209 int
    210 clock_settime1(clock_id, ats)
    211 	clockid_t clock_id;
    212 	struct timespec *ats;
    213 {
    214 	struct timeval atv;
    215 	int error;
    216 
    217 	if (clock_id != CLOCK_REALTIME)
    218 		return (EINVAL);
    219 
    220 	TIMESPEC_TO_TIMEVAL(&atv, ats);
    221 	if ((error = settime(&atv)) != 0)
    222 		return (error);
    223 
    224 	return 0;
    225 }
    226 
    227 int
    228 sys_clock_getres(l, v, retval)
    229 	struct lwp *l;
    230 	void *v;
    231 	register_t *retval;
    232 {
    233 	struct sys_clock_getres_args /* {
    234 		syscallarg(clockid_t) clock_id;
    235 		syscallarg(struct timespec *) tp;
    236 	} */ *uap = v;
    237 	clockid_t clock_id;
    238 	struct timespec ts;
    239 	int error = 0;
    240 
    241 	clock_id = SCARG(uap, clock_id);
    242 	if (clock_id != CLOCK_REALTIME)
    243 		return (EINVAL);
    244 
    245 	if (SCARG(uap, tp)) {
    246 		ts.tv_sec = 0;
    247 		ts.tv_nsec = 1000000000 / hz;
    248 
    249 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    250 	}
    251 
    252 	return error;
    253 }
    254 
    255 /* ARGSUSED */
    256 int
    257 sys_nanosleep(l, v, retval)
    258 	struct lwp *l;
    259 	void *v;
    260 	register_t *retval;
    261 {
    262 	static int nanowait;
    263 	struct sys_nanosleep_args/* {
    264 		syscallarg(struct timespec *) rqtp;
    265 		syscallarg(struct timespec *) rmtp;
    266 	} */ *uap = v;
    267 	struct timespec rqt;
    268 	struct timespec rmt;
    269 	struct timeval atv, utv;
    270 	int error, s, timo;
    271 
    272 	error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
    273 		       sizeof(struct timespec));
    274 	if (error)
    275 		return (error);
    276 
    277 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
    278 	if (itimerfix(&atv) || atv.tv_sec > 1000000000)
    279 		return (EINVAL);
    280 
    281 	s = splclock();
    282 	timeradd(&atv,&time,&atv);
    283 	timo = hzto(&atv);
    284 	/*
    285 	 * Avoid inadvertantly sleeping forever
    286 	 */
    287 	if (timo == 0)
    288 		timo = 1;
    289 	splx(s);
    290 
    291 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
    292 	if (error == ERESTART)
    293 		error = EINTR;
    294 	if (error == EWOULDBLOCK)
    295 		error = 0;
    296 
    297 	if (SCARG(uap, rmtp)) {
    298 		int error;
    299 
    300 		s = splclock();
    301 		utv = time;
    302 		splx(s);
    303 
    304 		timersub(&atv, &utv, &utv);
    305 		if (utv.tv_sec < 0)
    306 			timerclear(&utv);
    307 
    308 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
    309 		error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
    310 			sizeof(rmt));
    311 		if (error)
    312 			return (error);
    313 	}
    314 
    315 	return error;
    316 }
    317 
    318 /* ARGSUSED */
    319 int
    320 sys_gettimeofday(l, v, retval)
    321 	struct lwp *l;
    322 	void *v;
    323 	register_t *retval;
    324 {
    325 	struct sys_gettimeofday_args /* {
    326 		syscallarg(struct timeval *) tp;
    327 		syscallarg(struct timezone *) tzp;
    328 	} */ *uap = v;
    329 	struct timeval atv;
    330 	int error = 0;
    331 	struct timezone tzfake;
    332 
    333 	if (SCARG(uap, tp)) {
    334 		microtime(&atv);
    335 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    336 		if (error)
    337 			return (error);
    338 	}
    339 	if (SCARG(uap, tzp)) {
    340 		/*
    341 		 * NetBSD has no kernel notion of time zone, so we just
    342 		 * fake up a timezone struct and return it if demanded.
    343 		 */
    344 		tzfake.tz_minuteswest = 0;
    345 		tzfake.tz_dsttime = 0;
    346 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    347 	}
    348 	return (error);
    349 }
    350 
    351 /* ARGSUSED */
    352 int
    353 sys_settimeofday(l, v, retval)
    354 	struct lwp *l;
    355 	void *v;
    356 	register_t *retval;
    357 {
    358 	struct sys_settimeofday_args /* {
    359 		syscallarg(const struct timeval *) tv;
    360 		syscallarg(const struct timezone *) tzp;
    361 	} */ *uap = v;
    362 	struct proc *p = l->l_proc;
    363 	struct timeval atv;
    364 	struct timezone atz;
    365 	struct timeval *tv = NULL;
    366 	struct timezone *tzp = NULL;
    367 	int error;
    368 
    369 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    370 		return (error);
    371 
    372 	/* Verify all parameters before changing time. */
    373 	if (SCARG(uap, tv)) {
    374 		if ((error = copyin(SCARG(uap, tv), &atv, sizeof(atv))) != 0)
    375 			return (error);
    376 		tv = &atv;
    377 	}
    378 	/* XXX since we don't use tz, probably no point in doing copyin. */
    379 	if (SCARG(uap, tzp)) {
    380 		if ((error = copyin(SCARG(uap, tzp), &atz, sizeof(atz))) != 0)
    381 			return (error);
    382 		tzp = &atz;
    383 	}
    384 
    385 	return settimeofday1(tv, tzp, p);
    386 }
    387 
    388 int
    389 settimeofday1(tv, tzp, p)
    390 	struct timeval *tv;
    391 	struct timezone *tzp;
    392 	struct proc *p;
    393 {
    394 	int error;
    395 
    396 	if (tv)
    397 		if ((error = settime(tv)) != 0)
    398 			return (error);
    399 	/*
    400 	 * NetBSD has no kernel notion of time zone, and only an
    401 	 * obsolete program would try to set it, so we log a warning.
    402 	 */
    403 	if (tzp)
    404 		log(LOG_WARNING, "pid %d attempted to set the "
    405 		    "(obsolete) kernel time zone\n", p->p_pid);
    406 	return (0);
    407 }
    408 
    409 int	tickdelta;			/* current clock skew, us. per tick */
    410 long	timedelta;			/* unapplied time correction, us. */
    411 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    412 
    413 /* ARGSUSED */
    414 int
    415 sys_adjtime(l, v, retval)
    416 	struct lwp *l;
    417 	void *v;
    418 	register_t *retval;
    419 {
    420 	struct sys_adjtime_args /* {
    421 		syscallarg(const struct timeval *) delta;
    422 		syscallarg(struct timeval *) olddelta;
    423 	} */ *uap = v;
    424 	struct proc *p = l->l_proc;
    425 	struct timeval atv;
    426 	struct timeval *oatv = NULL;
    427 	int error;
    428 
    429 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    430 		return (error);
    431 
    432 	error = copyin(SCARG(uap, delta), &atv, sizeof(struct timeval));
    433 	if (error)
    434 		return (error);
    435 
    436 	if (SCARG(uap, olddelta) != NULL) {
    437 		if (uvm_useracc((caddr_t)SCARG(uap, olddelta),
    438 		    sizeof(struct timeval), B_WRITE) == FALSE)
    439 			return (EFAULT);
    440 		oatv = SCARG(uap, olddelta);
    441 	}
    442 
    443 	return adjtime1(&atv, oatv, p);
    444 }
    445 
    446 int
    447 adjtime1(delta, olddelta, p)
    448 	struct timeval *delta;
    449 	struct timeval *olddelta;
    450 	struct proc *p;
    451 {
    452 	long ndelta, ntickdelta, odelta;
    453 	int s;
    454 
    455 	/*
    456 	 * Compute the total correction and the rate at which to apply it.
    457 	 * Round the adjustment down to a whole multiple of the per-tick
    458 	 * delta, so that after some number of incremental changes in
    459 	 * hardclock(), tickdelta will become zero, lest the correction
    460 	 * overshoot and start taking us away from the desired final time.
    461 	 */
    462 	ndelta = delta->tv_sec * 1000000 + delta->tv_usec;
    463 	if (ndelta > bigadj || ndelta < -bigadj)
    464 		ntickdelta = 10 * tickadj;
    465 	else
    466 		ntickdelta = tickadj;
    467 	if (ndelta % ntickdelta)
    468 		ndelta = ndelta / ntickdelta * ntickdelta;
    469 
    470 	/*
    471 	 * To make hardclock()'s job easier, make the per-tick delta negative
    472 	 * if we want time to run slower; then hardclock can simply compute
    473 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    474 	 */
    475 	if (ndelta < 0)
    476 		ntickdelta = -ntickdelta;
    477 	s = splclock();
    478 	odelta = timedelta;
    479 	timedelta = ndelta;
    480 	tickdelta = ntickdelta;
    481 	splx(s);
    482 
    483 	if (olddelta) {
    484 		delta->tv_sec = odelta / 1000000;
    485 		delta->tv_usec = odelta % 1000000;
    486 		(void) copyout(delta, olddelta, sizeof(struct timeval));
    487 	}
    488 	return (0);
    489 }
    490 
    491 /*
    492  * Get value of an interval timer.  The process virtual and
    493  * profiling virtual time timers are kept in the p_stats area, since
    494  * they can be swapped out.  These are kept internally in the
    495  * way they are specified externally: in time until they expire.
    496  *
    497  * The real time interval timer is kept in the process table slot
    498  * for the process, and its value (it_value) is kept as an
    499  * absolute time rather than as a delta, so that it is easy to keep
    500  * periodic real-time signals from drifting.
    501  *
    502  * Virtual time timers are processed in the hardclock() routine of
    503  * kern_clock.c.  The real time timer is processed by a timeout
    504  * routine, called from the softclock() routine.  Since a callout
    505  * may be delayed in real time due to interrupt processing in the system,
    506  * it is possible for the real time timeout routine (realitexpire, given below),
    507  * to be delayed in real time past when it is supposed to occur.  It
    508  * does not suffice, therefore, to reload the real timer .it_value from the
    509  * real time timers .it_interval.  Rather, we compute the next time in
    510  * absolute time the timer should go off.
    511  */
    512 /* ARGSUSED */
    513 int
    514 sys_getitimer(l, v, retval)
    515 	struct lwp *l;
    516 	void *v;
    517 	register_t *retval;
    518 {
    519 	struct sys_getitimer_args /* {
    520 		syscallarg(int) which;
    521 		syscallarg(struct itimerval *) itv;
    522 	} */ *uap = v;
    523 	struct proc *p = l->l_proc;
    524 	int which = SCARG(uap, which);
    525 	struct itimerval aitv;
    526 	int s;
    527 
    528 	if ((u_int)which > ITIMER_PROF)
    529 		return (EINVAL);
    530 	s = splclock();
    531 	if (which == ITIMER_REAL) {
    532 		/*
    533 		 * Convert from absolute to relative time in .it_value
    534 		 * part of real time timer.  If time for real time timer
    535 		 * has passed return 0, else return difference between
    536 		 * current time and time for the timer to go off.
    537 		 */
    538 		aitv = p->p_realtimer;
    539 		if (timerisset(&aitv.it_value)) {
    540 			if (timercmp(&aitv.it_value, &time, <))
    541 				timerclear(&aitv.it_value);
    542 			else
    543 				timersub(&aitv.it_value, &time, &aitv.it_value);
    544 		}
    545 	} else
    546 		aitv = p->p_stats->p_timer[which];
    547 	splx(s);
    548 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
    549 }
    550 
    551 /* ARGSUSED */
    552 int
    553 sys_setitimer(l, v, retval)
    554 	struct lwp *l;
    555 	void *v;
    556 	register_t *retval;
    557 {
    558 	struct sys_setitimer_args /* {
    559 		syscallarg(int) which;
    560 		syscallarg(const struct itimerval *) itv;
    561 		syscallarg(struct itimerval *) oitv;
    562 	} */ *uap = v;
    563 	struct proc *p = l->l_proc;
    564 	int which = SCARG(uap, which);
    565 	struct sys_getitimer_args getargs;
    566 	struct itimerval aitv;
    567 	const struct itimerval *itvp;
    568 	int s, error;
    569 
    570 	if ((u_int)which > ITIMER_PROF)
    571 		return (EINVAL);
    572 	itvp = SCARG(uap, itv);
    573 	if (itvp &&
    574 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
    575 		return (error);
    576 	if (SCARG(uap, oitv) != NULL) {
    577 		SCARG(&getargs, which) = which;
    578 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    579 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
    580 			return (error);
    581 	}
    582 	if (itvp == 0)
    583 		return (0);
    584 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    585 		return (EINVAL);
    586 	s = splclock();
    587 	if (which == ITIMER_REAL) {
    588 		callout_stop(&p->p_realit_ch);
    589 		if (timerisset(&aitv.it_value)) {
    590 			/*
    591 			 * Don't need to check hzto() return value, here.
    592 			 * callout_reset() does it for us.
    593 			 */
    594 			timeradd(&aitv.it_value, &time, &aitv.it_value);
    595 			callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
    596 			    realitexpire, p);
    597 		}
    598 		p->p_realtimer = aitv;
    599 	} else
    600 		p->p_stats->p_timer[which] = aitv;
    601 	splx(s);
    602 	return (0);
    603 }
    604 
    605 /*
    606  * Real interval timer expired:
    607  * send process whose timer expired an alarm signal.
    608  * If time is not set up to reload, then just return.
    609  * Else compute next time timer should go off which is > current time.
    610  * This is where delay in processing this timeout causes multiple
    611  * SIGALRM calls to be compressed into one.
    612  */
    613 void
    614 realitexpire(arg)
    615 	void *arg;
    616 {
    617 	struct proc *p;
    618 	int s;
    619 
    620 	p = (struct proc *)arg;
    621 	psignal(p, SIGALRM);
    622 	if (!timerisset(&p->p_realtimer.it_interval)) {
    623 		timerclear(&p->p_realtimer.it_value);
    624 		return;
    625 	}
    626 	for (;;) {
    627 		s = splclock();
    628 		timeradd(&p->p_realtimer.it_value,
    629 		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
    630 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    631 			/*
    632 			 * Don't need to check hzto() return value, here.
    633 			 * callout_reset() does it for us.
    634 			 */
    635 			callout_reset(&p->p_realit_ch,
    636 			    hzto(&p->p_realtimer.it_value), realitexpire, p);
    637 			splx(s);
    638 			return;
    639 		}
    640 		splx(s);
    641 	}
    642 }
    643 
    644 /*
    645  * Check that a proposed value to load into the .it_value or
    646  * .it_interval part of an interval timer is acceptable, and
    647  * fix it to have at least minimal value (i.e. if it is less
    648  * than the resolution of the clock, round it up.)
    649  */
    650 int
    651 itimerfix(tv)
    652 	struct timeval *tv;
    653 {
    654 
    655 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    656 		return (EINVAL);
    657 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    658 		tv->tv_usec = tick;
    659 	return (0);
    660 }
    661 
    662 /*
    663  * Decrement an interval timer by a specified number
    664  * of microseconds, which must be less than a second,
    665  * i.e. < 1000000.  If the timer expires, then reload
    666  * it.  In this case, carry over (usec - old value) to
    667  * reduce the value reloaded into the timer so that
    668  * the timer does not drift.  This routine assumes
    669  * that it is called in a context where the timers
    670  * on which it is operating cannot change in value.
    671  */
    672 int
    673 itimerdecr(itp, usec)
    674 	struct itimerval *itp;
    675 	int usec;
    676 {
    677 
    678 	if (itp->it_value.tv_usec < usec) {
    679 		if (itp->it_value.tv_sec == 0) {
    680 			/* expired, and already in next interval */
    681 			usec -= itp->it_value.tv_usec;
    682 			goto expire;
    683 		}
    684 		itp->it_value.tv_usec += 1000000;
    685 		itp->it_value.tv_sec--;
    686 	}
    687 	itp->it_value.tv_usec -= usec;
    688 	usec = 0;
    689 	if (timerisset(&itp->it_value))
    690 		return (1);
    691 	/* expired, exactly at end of interval */
    692 expire:
    693 	if (timerisset(&itp->it_interval)) {
    694 		itp->it_value = itp->it_interval;
    695 		itp->it_value.tv_usec -= usec;
    696 		if (itp->it_value.tv_usec < 0) {
    697 			itp->it_value.tv_usec += 1000000;
    698 			itp->it_value.tv_sec--;
    699 		}
    700 	} else
    701 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    702 	return (0);
    703 }
    704 
    705 /*
    706  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
    707  * for usage and rationale.
    708  */
    709 int
    710 ratecheck(lasttime, mininterval)
    711 	struct timeval *lasttime;
    712 	const struct timeval *mininterval;
    713 {
    714 	struct timeval tv, delta;
    715 	int s, rv = 0;
    716 
    717 	s = splclock();
    718 	tv = mono_time;
    719 	splx(s);
    720 
    721 	timersub(&tv, lasttime, &delta);
    722 
    723 	/*
    724 	 * check for 0,0 is so that the message will be seen at least once,
    725 	 * even if interval is huge.
    726 	 */
    727 	if (timercmp(&delta, mininterval, >=) ||
    728 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
    729 		*lasttime = tv;
    730 		rv = 1;
    731 	}
    732 
    733 	return (rv);
    734 }
    735 
    736 /*
    737  * ppsratecheck(): packets (or events) per second limitation.
    738  */
    739 int
    740 ppsratecheck(lasttime, curpps, maxpps)
    741 	struct timeval *lasttime;
    742 	int *curpps;
    743 	int maxpps;	/* maximum pps allowed */
    744 {
    745 	struct timeval tv, delta;
    746 	int s, rv;
    747 
    748 	s = splclock();
    749 	tv = mono_time;
    750 	splx(s);
    751 
    752 	timersub(&tv, lasttime, &delta);
    753 
    754 	/*
    755 	 * check for 0,0 is so that the message will be seen at least once.
    756 	 * if more than one second have passed since the last update of
    757 	 * lasttime, reset the counter.
    758 	 *
    759 	 * we do increment *curpps even in *curpps < maxpps case, as some may
    760 	 * try to use *curpps for stat purposes as well.
    761 	 */
    762 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
    763 	    delta.tv_sec >= 1) {
    764 		*lasttime = tv;
    765 		*curpps = 0;
    766 		rv = 1;
    767 	} else if (maxpps < 0)
    768 		rv = 1;
    769 	else if (*curpps < maxpps)
    770 		rv = 1;
    771 	else
    772 		rv = 0;
    773 
    774 #if 1 /*DIAGNOSTIC?*/
    775 	/* be careful about wrap-around */
    776 	if (*curpps + 1 > *curpps)
    777 		*curpps = *curpps + 1;
    778 #else
    779 	/*
    780 	 * assume that there's not too many calls to this function.
    781 	 * not sure if the assumption holds, as it depends on *caller's*
    782 	 * behavior, not the behavior of this function.
    783 	 * IMHO it is wrong to make assumption on the caller's behavior,
    784 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
    785 	 */
    786 	*curpps = *curpps + 1;
    787 #endif
    788 
    789 	return (rv);
    790 }
    791