Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.22
      1  1.22       jtc /*	$NetBSD: kern_time.c,v 1.22 1996/11/15 22:44:26 jtc Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*
      4   1.8       cgd  * Copyright (c) 1982, 1986, 1989, 1993
      5   1.8       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35   1.9       cgd  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.5   mycroft #include <sys/param.h>
     39   1.5   mycroft #include <sys/resourcevar.h>
     40   1.5   mycroft #include <sys/kernel.h>
     41   1.8       cgd #include <sys/systm.h>
     42   1.5   mycroft #include <sys/proc.h>
     43   1.8       cgd #include <sys/vnode.h>
     44  1.17  christos #include <sys/signalvar.h>
     45   1.1       cgd 
     46  1.11       cgd #include <sys/mount.h>
     47  1.11       cgd #include <sys/syscallargs.h>
     48  1.19  christos 
     49  1.19  christos #if defined(NFSCLIENT) || defined(NFSSERVER)
     50  1.20      fvdl #include <nfs/rpcv2.h>
     51  1.20      fvdl #include <nfs/nfsproto.h>
     52  1.19  christos #include <nfs/nfs_var.h>
     53  1.19  christos #endif
     54  1.17  christos 
     55   1.5   mycroft #include <machine/cpu.h>
     56   1.1       cgd 
     57   1.1       cgd /*
     58   1.1       cgd  * Time of day and interval timer support.
     59   1.1       cgd  *
     60   1.1       cgd  * These routines provide the kernel entry points to get and set
     61   1.1       cgd  * the time-of-day and per-process interval timers.  Subroutines
     62   1.1       cgd  * here provide support for adding and subtracting timeval structures
     63   1.1       cgd  * and decrementing interval timers, optionally reloading the interval
     64   1.1       cgd  * timers when they expire.
     65   1.1       cgd  */
     66   1.1       cgd 
     67  1.22       jtc 
     68  1.22       jtc /* This function is used by clock_settime and settimeofday */
     69  1.22       jtc static void
     70  1.22       jtc settime(tv)
     71  1.22       jtc 	struct timeval *tv;
     72  1.22       jtc {
     73  1.22       jtc 	struct timeval delta;
     74  1.22       jtc 	int s;
     75  1.22       jtc 
     76  1.22       jtc 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
     77  1.22       jtc 	s = splclock();
     78  1.22       jtc 	timersub(tv, &time, &delta);
     79  1.22       jtc 	time = *tv;
     80  1.22       jtc 	(void) splsoftclock();
     81  1.22       jtc 	timeradd(&boottime, &delta, &boottime);
     82  1.22       jtc 	timeradd(&runtime, &delta, &runtime);
     83  1.22       jtc #	if defined(NFSCLIENT) || defined(NFSSERVER)
     84  1.22       jtc 		nqnfs_lease_updatetime(delta.tv_sec);
     85  1.22       jtc #	endif
     86  1.22       jtc 	splx(s);
     87  1.22       jtc 	resettodr();
     88  1.22       jtc }
     89  1.22       jtc 
     90  1.22       jtc /* ARGSUSED */
     91  1.22       jtc int
     92  1.22       jtc sys_clock_gettime(p, v, retval)
     93  1.22       jtc 	struct proc *p;
     94  1.22       jtc 	void *v;
     95  1.22       jtc 	register_t *retval;
     96  1.22       jtc {
     97  1.22       jtc 	register struct sys_clock_gettime_args /* {
     98  1.22       jtc 		syscallarg(clockid_t) clock_id;
     99  1.22       jtc                 syscallarg(struct timespec *) tp;
    100  1.22       jtc         } */ *uap = v;
    101  1.22       jtc 	clockid_t clock_id;
    102  1.22       jtc 	struct timeval atv;
    103  1.22       jtc 	struct timespec ats;
    104  1.22       jtc 
    105  1.22       jtc 	clock_id = SCARG(uap, clock_id);
    106  1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    107  1.22       jtc 		return (EINVAL);
    108  1.22       jtc 
    109  1.22       jtc 	microtime(&atv);
    110  1.22       jtc 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
    111  1.22       jtc 
    112  1.22       jtc 	return copyout((caddr_t)&ats, SCARG(uap, tp), sizeof(ats));
    113  1.22       jtc }
    114  1.22       jtc 
    115  1.22       jtc /* ARGSUSED */
    116  1.22       jtc int
    117  1.22       jtc sys_clock_settime(p, v, retval)
    118  1.22       jtc 	struct proc *p;
    119  1.22       jtc 	void *v;
    120  1.22       jtc 	register_t *retval;
    121  1.22       jtc {
    122  1.22       jtc 	register struct sys_clock_settime_args /* {
    123  1.22       jtc 		syscallarg(clockid_t) clock_id;
    124  1.22       jtc                 syscallarg(struct timespec *) tp;
    125  1.22       jtc         } */ *uap = v;
    126  1.22       jtc 	clockid_t clock_id;
    127  1.22       jtc 	struct timeval atv;
    128  1.22       jtc 	struct timespec ats;
    129  1.22       jtc 	int error;
    130  1.22       jtc 
    131  1.22       jtc 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    132  1.22       jtc 		return (error);
    133  1.22       jtc 
    134  1.22       jtc 	clock_id = SCARG(uap, clock_id);
    135  1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    136  1.22       jtc 		return (EINVAL);
    137  1.22       jtc 
    138  1.22       jtc 	if (error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ats, sizeof(ats)))
    139  1.22       jtc                 return (error);
    140  1.22       jtc 
    141  1.22       jtc 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
    142  1.22       jtc 	settime(&atv);
    143  1.22       jtc 
    144  1.22       jtc 	return 0;
    145  1.22       jtc }
    146  1.22       jtc 
    147  1.22       jtc int
    148  1.22       jtc sys_clock_getres(p, v, retval)
    149  1.22       jtc 	struct proc *p;
    150  1.22       jtc 	void *v;
    151  1.22       jtc 	register_t *retval;
    152  1.22       jtc {
    153  1.22       jtc 	register struct sys_clock_getres_args /* {
    154  1.22       jtc 		syscallarg(clockid_t) clock_id;
    155  1.22       jtc                 syscallarg(struct timespec *) tp;
    156  1.22       jtc         } */ *uap = v;
    157  1.22       jtc 	clockid_t clock_id;
    158  1.22       jtc 	struct timespec ts;
    159  1.22       jtc 	int error = 0;
    160  1.22       jtc 
    161  1.22       jtc 	clock_id = SCARG(uap, clock_id);
    162  1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    163  1.22       jtc 		return (EINVAL);
    164  1.22       jtc 
    165  1.22       jtc 	if (SCARG(uap, tp)) {
    166  1.22       jtc 		ts.tv_sec = 0;
    167  1.22       jtc 		ts.tv_nsec = 1000000000 / hz;
    168  1.22       jtc 
    169  1.22       jtc 		error = copyout((caddr_t)&ts, (caddr_t)SCARG(uap, tp),
    170  1.22       jtc 			sizeof (ts));
    171  1.22       jtc 	}
    172  1.22       jtc 
    173  1.22       jtc 	return error;
    174  1.22       jtc }
    175  1.22       jtc 
    176  1.22       jtc 
    177   1.1       cgd /* ARGSUSED */
    178   1.3    andrew int
    179  1.16   mycroft sys_gettimeofday(p, v, retval)
    180   1.1       cgd 	struct proc *p;
    181  1.15   thorpej 	void *v;
    182  1.15   thorpej 	register_t *retval;
    183  1.15   thorpej {
    184  1.16   mycroft 	register struct sys_gettimeofday_args /* {
    185  1.11       cgd 		syscallarg(struct timeval *) tp;
    186  1.11       cgd 		syscallarg(struct timezone *) tzp;
    187  1.15   thorpej 	} */ *uap = v;
    188   1.1       cgd 	struct timeval atv;
    189   1.1       cgd 	int error = 0;
    190   1.1       cgd 
    191  1.11       cgd 	if (SCARG(uap, tp)) {
    192   1.1       cgd 		microtime(&atv);
    193  1.17  christos 		error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
    194  1.17  christos 				sizeof (atv));
    195  1.17  christos 		if (error)
    196   1.1       cgd 			return (error);
    197   1.1       cgd 	}
    198  1.11       cgd 	if (SCARG(uap, tzp))
    199  1.11       cgd 		error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
    200   1.1       cgd 		    sizeof (tz));
    201   1.1       cgd 	return (error);
    202   1.1       cgd }
    203   1.1       cgd 
    204   1.1       cgd /* ARGSUSED */
    205   1.3    andrew int
    206  1.16   mycroft sys_settimeofday(p, v, retval)
    207   1.1       cgd 	struct proc *p;
    208  1.15   thorpej 	void *v;
    209  1.15   thorpej 	register_t *retval;
    210  1.15   thorpej {
    211  1.16   mycroft 	struct sys_settimeofday_args /* {
    212  1.11       cgd 		syscallarg(struct timeval *) tv;
    213  1.11       cgd 		syscallarg(struct timezone *) tzp;
    214  1.15   thorpej 	} */ *uap = v;
    215  1.22       jtc 	struct timeval atv;
    216   1.1       cgd 	struct timezone atz;
    217  1.22       jtc 	int error;
    218   1.1       cgd 
    219  1.17  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    220   1.1       cgd 		return (error);
    221   1.8       cgd 	/* Verify all parameters before changing time. */
    222  1.11       cgd 	if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
    223  1.11       cgd 	    (caddr_t)&atv, sizeof(atv))))
    224   1.8       cgd 		return (error);
    225  1.11       cgd 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
    226  1.11       cgd 	    (caddr_t)&atz, sizeof(atz))))
    227   1.8       cgd 		return (error);
    228  1.22       jtc 	if (SCARG(uap, tv))
    229  1.22       jtc 		settime(&atv);
    230  1.11       cgd 	if (SCARG(uap, tzp))
    231   1.1       cgd 		tz = atz;
    232   1.8       cgd 	return (0);
    233   1.1       cgd }
    234   1.1       cgd 
    235   1.1       cgd int	tickdelta;			/* current clock skew, us. per tick */
    236   1.1       cgd long	timedelta;			/* unapplied time correction, us. */
    237   1.1       cgd long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    238   1.1       cgd 
    239   1.1       cgd /* ARGSUSED */
    240   1.3    andrew int
    241  1.16   mycroft sys_adjtime(p, v, retval)
    242   1.1       cgd 	struct proc *p;
    243  1.15   thorpej 	void *v;
    244  1.15   thorpej 	register_t *retval;
    245  1.15   thorpej {
    246  1.16   mycroft 	register struct sys_adjtime_args /* {
    247  1.11       cgd 		syscallarg(struct timeval *) delta;
    248  1.11       cgd 		syscallarg(struct timeval *) olddelta;
    249  1.15   thorpej 	} */ *uap = v;
    250   1.8       cgd 	struct timeval atv;
    251   1.8       cgd 	register long ndelta, ntickdelta, odelta;
    252   1.1       cgd 	int s, error;
    253   1.1       cgd 
    254  1.17  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    255   1.1       cgd 		return (error);
    256  1.17  christos 
    257  1.17  christos 	error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
    258  1.17  christos 		       sizeof(struct timeval));
    259  1.17  christos 	if (error)
    260   1.1       cgd 		return (error);
    261   1.8       cgd 
    262   1.8       cgd 	/*
    263   1.8       cgd 	 * Compute the total correction and the rate at which to apply it.
    264   1.8       cgd 	 * Round the adjustment down to a whole multiple of the per-tick
    265   1.8       cgd 	 * delta, so that after some number of incremental changes in
    266   1.8       cgd 	 * hardclock(), tickdelta will become zero, lest the correction
    267   1.8       cgd 	 * overshoot and start taking us away from the desired final time.
    268   1.8       cgd 	 */
    269   1.1       cgd 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    270   1.8       cgd 	if (ndelta > bigadj)
    271   1.8       cgd 		ntickdelta = 10 * tickadj;
    272   1.8       cgd 	else
    273   1.8       cgd 		ntickdelta = tickadj;
    274   1.8       cgd 	if (ndelta % ntickdelta)
    275   1.8       cgd 		ndelta = ndelta / ntickdelta * ntickdelta;
    276   1.8       cgd 
    277   1.8       cgd 	/*
    278   1.8       cgd 	 * To make hardclock()'s job easier, make the per-tick delta negative
    279   1.8       cgd 	 * if we want time to run slower; then hardclock can simply compute
    280   1.8       cgd 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    281   1.8       cgd 	 */
    282   1.8       cgd 	if (ndelta < 0)
    283   1.8       cgd 		ntickdelta = -ntickdelta;
    284   1.1       cgd 	s = splclock();
    285   1.8       cgd 	odelta = timedelta;
    286   1.1       cgd 	timedelta = ndelta;
    287   1.8       cgd 	tickdelta = ntickdelta;
    288   1.1       cgd 	splx(s);
    289   1.1       cgd 
    290  1.11       cgd 	if (SCARG(uap, olddelta)) {
    291   1.8       cgd 		atv.tv_sec = odelta / 1000000;
    292   1.8       cgd 		atv.tv_usec = odelta % 1000000;
    293  1.11       cgd 		(void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
    294   1.8       cgd 		    sizeof(struct timeval));
    295   1.8       cgd 	}
    296   1.1       cgd 	return (0);
    297   1.1       cgd }
    298   1.1       cgd 
    299   1.1       cgd /*
    300   1.1       cgd  * Get value of an interval timer.  The process virtual and
    301   1.1       cgd  * profiling virtual time timers are kept in the p_stats area, since
    302   1.1       cgd  * they can be swapped out.  These are kept internally in the
    303   1.1       cgd  * way they are specified externally: in time until they expire.
    304   1.1       cgd  *
    305   1.1       cgd  * The real time interval timer is kept in the process table slot
    306   1.1       cgd  * for the process, and its value (it_value) is kept as an
    307   1.1       cgd  * absolute time rather than as a delta, so that it is easy to keep
    308   1.1       cgd  * periodic real-time signals from drifting.
    309   1.1       cgd  *
    310   1.1       cgd  * Virtual time timers are processed in the hardclock() routine of
    311   1.1       cgd  * kern_clock.c.  The real time timer is processed by a timeout
    312   1.1       cgd  * routine, called from the softclock() routine.  Since a callout
    313   1.1       cgd  * may be delayed in real time due to interrupt processing in the system,
    314   1.1       cgd  * it is possible for the real time timeout routine (realitexpire, given below),
    315   1.1       cgd  * to be delayed in real time past when it is supposed to occur.  It
    316   1.1       cgd  * does not suffice, therefore, to reload the real timer .it_value from the
    317   1.1       cgd  * real time timers .it_interval.  Rather, we compute the next time in
    318   1.1       cgd  * absolute time the timer should go off.
    319   1.1       cgd  */
    320   1.1       cgd /* ARGSUSED */
    321   1.3    andrew int
    322  1.16   mycroft sys_getitimer(p, v, retval)
    323   1.1       cgd 	struct proc *p;
    324  1.15   thorpej 	void *v;
    325  1.15   thorpej 	register_t *retval;
    326  1.15   thorpej {
    327  1.16   mycroft 	register struct sys_getitimer_args /* {
    328  1.11       cgd 		syscallarg(u_int) which;
    329  1.11       cgd 		syscallarg(struct itimerval *) itv;
    330  1.15   thorpej 	} */ *uap = v;
    331   1.1       cgd 	struct itimerval aitv;
    332   1.1       cgd 	int s;
    333   1.1       cgd 
    334  1.11       cgd 	if (SCARG(uap, which) > ITIMER_PROF)
    335   1.1       cgd 		return (EINVAL);
    336   1.1       cgd 	s = splclock();
    337  1.11       cgd 	if (SCARG(uap, which) == ITIMER_REAL) {
    338   1.1       cgd 		/*
    339  1.12   mycroft 		 * Convert from absolute to relative time in .it_value
    340   1.1       cgd 		 * part of real time timer.  If time for real time timer
    341   1.1       cgd 		 * has passed return 0, else return difference between
    342   1.1       cgd 		 * current time and time for the timer to go off.
    343   1.1       cgd 		 */
    344   1.1       cgd 		aitv = p->p_realtimer;
    345   1.1       cgd 		if (timerisset(&aitv.it_value))
    346   1.1       cgd 			if (timercmp(&aitv.it_value, &time, <))
    347   1.1       cgd 				timerclear(&aitv.it_value);
    348   1.1       cgd 			else
    349  1.14   mycroft 				timersub(&aitv.it_value, &time, &aitv.it_value);
    350   1.1       cgd 	} else
    351  1.11       cgd 		aitv = p->p_stats->p_timer[SCARG(uap, which)];
    352   1.1       cgd 	splx(s);
    353  1.11       cgd 	return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
    354   1.1       cgd 	    sizeof (struct itimerval)));
    355   1.1       cgd }
    356   1.1       cgd 
    357   1.1       cgd /* ARGSUSED */
    358   1.3    andrew int
    359  1.16   mycroft sys_setitimer(p, v, retval)
    360   1.1       cgd 	struct proc *p;
    361  1.17  christos 	register void *v;
    362  1.15   thorpej 	register_t *retval;
    363  1.15   thorpej {
    364  1.16   mycroft 	register struct sys_setitimer_args /* {
    365  1.11       cgd 		syscallarg(u_int) which;
    366  1.11       cgd 		syscallarg(struct itimerval *) itv;
    367  1.11       cgd 		syscallarg(struct itimerval *) oitv;
    368  1.15   thorpej 	} */ *uap = v;
    369  1.21       cgd 	struct sys_getitimer_args getargs;
    370   1.1       cgd 	struct itimerval aitv;
    371   1.1       cgd 	register struct itimerval *itvp;
    372   1.1       cgd 	int s, error;
    373   1.1       cgd 
    374  1.11       cgd 	if (SCARG(uap, which) > ITIMER_PROF)
    375   1.1       cgd 		return (EINVAL);
    376  1.11       cgd 	itvp = SCARG(uap, itv);
    377   1.1       cgd 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
    378   1.1       cgd 	    sizeof(struct itimerval))))
    379   1.1       cgd 		return (error);
    380  1.21       cgd 	if (SCARG(uap, oitv) != NULL) {
    381  1.21       cgd 		SCARG(&getargs, which) = SCARG(uap, which);
    382  1.21       cgd 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    383  1.21       cgd 	    	if ((error = sys_getitimer(p, &getargs, retval)) != 0)
    384  1.21       cgd 			return (error);
    385  1.21       cgd 	}
    386   1.1       cgd 	if (itvp == 0)
    387   1.1       cgd 		return (0);
    388   1.1       cgd 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    389   1.1       cgd 		return (EINVAL);
    390   1.1       cgd 	s = splclock();
    391  1.11       cgd 	if (SCARG(uap, which) == ITIMER_REAL) {
    392   1.7   mycroft 		untimeout(realitexpire, p);
    393   1.1       cgd 		if (timerisset(&aitv.it_value)) {
    394  1.14   mycroft 			timeradd(&aitv.it_value, &time, &aitv.it_value);
    395   1.7   mycroft 			timeout(realitexpire, p, hzto(&aitv.it_value));
    396   1.1       cgd 		}
    397   1.1       cgd 		p->p_realtimer = aitv;
    398   1.1       cgd 	} else
    399  1.11       cgd 		p->p_stats->p_timer[SCARG(uap, which)] = aitv;
    400   1.1       cgd 	splx(s);
    401   1.1       cgd 	return (0);
    402   1.1       cgd }
    403   1.1       cgd 
    404   1.1       cgd /*
    405   1.1       cgd  * Real interval timer expired:
    406   1.1       cgd  * send process whose timer expired an alarm signal.
    407   1.1       cgd  * If time is not set up to reload, then just return.
    408   1.1       cgd  * Else compute next time timer should go off which is > current time.
    409   1.1       cgd  * This is where delay in processing this timeout causes multiple
    410   1.1       cgd  * SIGALRM calls to be compressed into one.
    411   1.1       cgd  */
    412   1.3    andrew void
    413   1.6       cgd realitexpire(arg)
    414   1.6       cgd 	void *arg;
    415   1.6       cgd {
    416   1.1       cgd 	register struct proc *p;
    417   1.1       cgd 	int s;
    418   1.1       cgd 
    419   1.6       cgd 	p = (struct proc *)arg;
    420   1.1       cgd 	psignal(p, SIGALRM);
    421   1.1       cgd 	if (!timerisset(&p->p_realtimer.it_interval)) {
    422   1.1       cgd 		timerclear(&p->p_realtimer.it_value);
    423   1.1       cgd 		return;
    424   1.1       cgd 	}
    425   1.1       cgd 	for (;;) {
    426   1.1       cgd 		s = splclock();
    427  1.14   mycroft 		timeradd(&p->p_realtimer.it_value,
    428  1.14   mycroft 		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
    429   1.1       cgd 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    430   1.7   mycroft 			timeout(realitexpire, p,
    431   1.1       cgd 			    hzto(&p->p_realtimer.it_value));
    432   1.1       cgd 			splx(s);
    433   1.1       cgd 			return;
    434   1.1       cgd 		}
    435   1.1       cgd 		splx(s);
    436   1.1       cgd 	}
    437   1.1       cgd }
    438   1.1       cgd 
    439   1.1       cgd /*
    440   1.1       cgd  * Check that a proposed value to load into the .it_value or
    441   1.1       cgd  * .it_interval part of an interval timer is acceptable, and
    442   1.1       cgd  * fix it to have at least minimal value (i.e. if it is less
    443   1.1       cgd  * than the resolution of the clock, round it up.)
    444   1.1       cgd  */
    445   1.3    andrew int
    446   1.1       cgd itimerfix(tv)
    447   1.1       cgd 	struct timeval *tv;
    448   1.1       cgd {
    449   1.1       cgd 
    450   1.1       cgd 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
    451   1.1       cgd 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    452   1.1       cgd 		return (EINVAL);
    453   1.1       cgd 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    454   1.1       cgd 		tv->tv_usec = tick;
    455   1.1       cgd 	return (0);
    456   1.1       cgd }
    457   1.1       cgd 
    458   1.1       cgd /*
    459   1.1       cgd  * Decrement an interval timer by a specified number
    460   1.1       cgd  * of microseconds, which must be less than a second,
    461   1.1       cgd  * i.e. < 1000000.  If the timer expires, then reload
    462   1.1       cgd  * it.  In this case, carry over (usec - old value) to
    463   1.8       cgd  * reduce the value reloaded into the timer so that
    464   1.1       cgd  * the timer does not drift.  This routine assumes
    465   1.1       cgd  * that it is called in a context where the timers
    466   1.1       cgd  * on which it is operating cannot change in value.
    467   1.1       cgd  */
    468   1.3    andrew int
    469   1.1       cgd itimerdecr(itp, usec)
    470   1.1       cgd 	register struct itimerval *itp;
    471   1.1       cgd 	int usec;
    472   1.1       cgd {
    473   1.1       cgd 
    474   1.1       cgd 	if (itp->it_value.tv_usec < usec) {
    475   1.1       cgd 		if (itp->it_value.tv_sec == 0) {
    476   1.1       cgd 			/* expired, and already in next interval */
    477   1.1       cgd 			usec -= itp->it_value.tv_usec;
    478   1.1       cgd 			goto expire;
    479   1.1       cgd 		}
    480   1.1       cgd 		itp->it_value.tv_usec += 1000000;
    481   1.1       cgd 		itp->it_value.tv_sec--;
    482   1.1       cgd 	}
    483   1.1       cgd 	itp->it_value.tv_usec -= usec;
    484   1.1       cgd 	usec = 0;
    485   1.1       cgd 	if (timerisset(&itp->it_value))
    486   1.1       cgd 		return (1);
    487   1.1       cgd 	/* expired, exactly at end of interval */
    488   1.1       cgd expire:
    489   1.1       cgd 	if (timerisset(&itp->it_interval)) {
    490   1.1       cgd 		itp->it_value = itp->it_interval;
    491   1.1       cgd 		itp->it_value.tv_usec -= usec;
    492   1.1       cgd 		if (itp->it_value.tv_usec < 0) {
    493   1.1       cgd 			itp->it_value.tv_usec += 1000000;
    494   1.1       cgd 			itp->it_value.tv_sec--;
    495   1.1       cgd 		}
    496   1.1       cgd 	} else
    497   1.1       cgd 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    498   1.1       cgd 	return (0);
    499   1.1       cgd }
    500