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