Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.19
      1 /*	$NetBSD: kern_time.c,v 1.19 1996/02/13 21:10:43 christos 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/nfs_var.h>
     51 #endif
     52 
     53 #include <machine/cpu.h>
     54 
     55 /*
     56  * Time of day and interval timer support.
     57  *
     58  * These routines provide the kernel entry points to get and set
     59  * the time-of-day and per-process interval timers.  Subroutines
     60  * here provide support for adding and subtracting timeval structures
     61  * and decrementing interval timers, optionally reloading the interval
     62  * timers when they expire.
     63  */
     64 
     65 /* ARGSUSED */
     66 int
     67 sys_gettimeofday(p, v, retval)
     68 	struct proc *p;
     69 	void *v;
     70 	register_t *retval;
     71 {
     72 	register struct sys_gettimeofday_args /* {
     73 		syscallarg(struct timeval *) tp;
     74 		syscallarg(struct timezone *) tzp;
     75 	} */ *uap = v;
     76 	struct timeval atv;
     77 	int error = 0;
     78 
     79 	if (SCARG(uap, tp)) {
     80 		microtime(&atv);
     81 		error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
     82 				sizeof (atv));
     83 		if (error)
     84 			return (error);
     85 	}
     86 	if (SCARG(uap, tzp))
     87 		error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
     88 		    sizeof (tz));
     89 	return (error);
     90 }
     91 
     92 /* ARGSUSED */
     93 int
     94 sys_settimeofday(p, v, retval)
     95 	struct proc *p;
     96 	void *v;
     97 	register_t *retval;
     98 {
     99 	struct sys_settimeofday_args /* {
    100 		syscallarg(struct timeval *) tv;
    101 		syscallarg(struct timezone *) tzp;
    102 	} */ *uap = v;
    103 	struct timeval atv, delta;
    104 	struct timezone atz;
    105 	int error, s;
    106 
    107 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    108 		return (error);
    109 	/* Verify all parameters before changing time. */
    110 	if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
    111 	    (caddr_t)&atv, sizeof(atv))))
    112 		return (error);
    113 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
    114 	    (caddr_t)&atz, sizeof(atz))))
    115 		return (error);
    116 	if (SCARG(uap, tv)) {
    117 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    118 		s = splclock();
    119 		timersub(&atv, &time, &delta);
    120 		time = atv;
    121 		(void) splsoftclock();
    122 		timeradd(&boottime, &delta, &boottime);
    123 		timeradd(&runtime, &delta, &runtime);
    124 # 		if defined(NFSCLIENT) || defined(NFSSERVER)
    125 			lease_updatetime(delta.tv_sec);
    126 #		endif
    127 		splx(s);
    128 		resettodr();
    129 	}
    130 	if (SCARG(uap, tzp))
    131 		tz = atz;
    132 	return (0);
    133 }
    134 
    135 int	tickdelta;			/* current clock skew, us. per tick */
    136 long	timedelta;			/* unapplied time correction, us. */
    137 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    138 
    139 /* ARGSUSED */
    140 int
    141 sys_adjtime(p, v, retval)
    142 	struct proc *p;
    143 	void *v;
    144 	register_t *retval;
    145 {
    146 	register struct sys_adjtime_args /* {
    147 		syscallarg(struct timeval *) delta;
    148 		syscallarg(struct timeval *) olddelta;
    149 	} */ *uap = v;
    150 	struct timeval atv;
    151 	register long ndelta, ntickdelta, odelta;
    152 	int s, error;
    153 
    154 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    155 		return (error);
    156 
    157 	error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
    158 		       sizeof(struct timeval));
    159 	if (error)
    160 		return (error);
    161 
    162 	/*
    163 	 * Compute the total correction and the rate at which to apply it.
    164 	 * Round the adjustment down to a whole multiple of the per-tick
    165 	 * delta, so that after some number of incremental changes in
    166 	 * hardclock(), tickdelta will become zero, lest the correction
    167 	 * overshoot and start taking us away from the desired final time.
    168 	 */
    169 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    170 	if (ndelta > bigadj)
    171 		ntickdelta = 10 * tickadj;
    172 	else
    173 		ntickdelta = tickadj;
    174 	if (ndelta % ntickdelta)
    175 		ndelta = ndelta / ntickdelta * ntickdelta;
    176 
    177 	/*
    178 	 * To make hardclock()'s job easier, make the per-tick delta negative
    179 	 * if we want time to run slower; then hardclock can simply compute
    180 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    181 	 */
    182 	if (ndelta < 0)
    183 		ntickdelta = -ntickdelta;
    184 	s = splclock();
    185 	odelta = timedelta;
    186 	timedelta = ndelta;
    187 	tickdelta = ntickdelta;
    188 	splx(s);
    189 
    190 	if (SCARG(uap, olddelta)) {
    191 		atv.tv_sec = odelta / 1000000;
    192 		atv.tv_usec = odelta % 1000000;
    193 		(void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
    194 		    sizeof(struct timeval));
    195 	}
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Get value of an interval timer.  The process virtual and
    201  * profiling virtual time timers are kept in the p_stats area, since
    202  * they can be swapped out.  These are kept internally in the
    203  * way they are specified externally: in time until they expire.
    204  *
    205  * The real time interval timer is kept in the process table slot
    206  * for the process, and its value (it_value) is kept as an
    207  * absolute time rather than as a delta, so that it is easy to keep
    208  * periodic real-time signals from drifting.
    209  *
    210  * Virtual time timers are processed in the hardclock() routine of
    211  * kern_clock.c.  The real time timer is processed by a timeout
    212  * routine, called from the softclock() routine.  Since a callout
    213  * may be delayed in real time due to interrupt processing in the system,
    214  * it is possible for the real time timeout routine (realitexpire, given below),
    215  * to be delayed in real time past when it is supposed to occur.  It
    216  * does not suffice, therefore, to reload the real timer .it_value from the
    217  * real time timers .it_interval.  Rather, we compute the next time in
    218  * absolute time the timer should go off.
    219  */
    220 /* ARGSUSED */
    221 int
    222 sys_getitimer(p, v, retval)
    223 	struct proc *p;
    224 	void *v;
    225 	register_t *retval;
    226 {
    227 	register struct sys_getitimer_args /* {
    228 		syscallarg(u_int) which;
    229 		syscallarg(struct itimerval *) itv;
    230 	} */ *uap = v;
    231 	struct itimerval aitv;
    232 	int s;
    233 
    234 	if (SCARG(uap, which) > ITIMER_PROF)
    235 		return (EINVAL);
    236 	s = splclock();
    237 	if (SCARG(uap, which) == ITIMER_REAL) {
    238 		/*
    239 		 * Convert from absolute to relative time in .it_value
    240 		 * part of real time timer.  If time for real time timer
    241 		 * has passed return 0, else return difference between
    242 		 * current time and time for the timer to go off.
    243 		 */
    244 		aitv = p->p_realtimer;
    245 		if (timerisset(&aitv.it_value))
    246 			if (timercmp(&aitv.it_value, &time, <))
    247 				timerclear(&aitv.it_value);
    248 			else
    249 				timersub(&aitv.it_value, &time, &aitv.it_value);
    250 	} else
    251 		aitv = p->p_stats->p_timer[SCARG(uap, which)];
    252 	splx(s);
    253 	return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
    254 	    sizeof (struct itimerval)));
    255 }
    256 
    257 /* ARGSUSED */
    258 int
    259 sys_setitimer(p, v, retval)
    260 	struct proc *p;
    261 	register void *v;
    262 	register_t *retval;
    263 {
    264 	register struct sys_setitimer_args /* {
    265 		syscallarg(u_int) which;
    266 		syscallarg(struct itimerval *) itv;
    267 		syscallarg(struct itimerval *) oitv;
    268 	} */ *uap = v;
    269 	struct itimerval aitv;
    270 	register struct itimerval *itvp;
    271 	int s, error;
    272 
    273 	if (SCARG(uap, which) > ITIMER_PROF)
    274 		return (EINVAL);
    275 	itvp = SCARG(uap, itv);
    276 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
    277 	    sizeof(struct itimerval))))
    278 		return (error);
    279 	if ((SCARG(uap, itv) = SCARG(uap, oitv)) &&
    280 	    (error = sys_getitimer(p, uap, retval)))
    281 		return (error);
    282 	if (itvp == 0)
    283 		return (0);
    284 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    285 		return (EINVAL);
    286 	s = splclock();
    287 	if (SCARG(uap, which) == ITIMER_REAL) {
    288 		untimeout(realitexpire, p);
    289 		if (timerisset(&aitv.it_value)) {
    290 			timeradd(&aitv.it_value, &time, &aitv.it_value);
    291 			timeout(realitexpire, p, hzto(&aitv.it_value));
    292 		}
    293 		p->p_realtimer = aitv;
    294 	} else
    295 		p->p_stats->p_timer[SCARG(uap, which)] = aitv;
    296 	splx(s);
    297 	return (0);
    298 }
    299 
    300 /*
    301  * Real interval timer expired:
    302  * send process whose timer expired an alarm signal.
    303  * If time is not set up to reload, then just return.
    304  * Else compute next time timer should go off which is > current time.
    305  * This is where delay in processing this timeout causes multiple
    306  * SIGALRM calls to be compressed into one.
    307  */
    308 void
    309 realitexpire(arg)
    310 	void *arg;
    311 {
    312 	register struct proc *p;
    313 	int s;
    314 
    315 	p = (struct proc *)arg;
    316 	psignal(p, SIGALRM);
    317 	if (!timerisset(&p->p_realtimer.it_interval)) {
    318 		timerclear(&p->p_realtimer.it_value);
    319 		return;
    320 	}
    321 	for (;;) {
    322 		s = splclock();
    323 		timeradd(&p->p_realtimer.it_value,
    324 		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
    325 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    326 			timeout(realitexpire, p,
    327 			    hzto(&p->p_realtimer.it_value));
    328 			splx(s);
    329 			return;
    330 		}
    331 		splx(s);
    332 	}
    333 }
    334 
    335 /*
    336  * Check that a proposed value to load into the .it_value or
    337  * .it_interval part of an interval timer is acceptable, and
    338  * fix it to have at least minimal value (i.e. if it is less
    339  * than the resolution of the clock, round it up.)
    340  */
    341 int
    342 itimerfix(tv)
    343 	struct timeval *tv;
    344 {
    345 
    346 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
    347 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    348 		return (EINVAL);
    349 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    350 		tv->tv_usec = tick;
    351 	return (0);
    352 }
    353 
    354 /*
    355  * Decrement an interval timer by a specified number
    356  * of microseconds, which must be less than a second,
    357  * i.e. < 1000000.  If the timer expires, then reload
    358  * it.  In this case, carry over (usec - old value) to
    359  * reduce the value reloaded into the timer so that
    360  * the timer does not drift.  This routine assumes
    361  * that it is called in a context where the timers
    362  * on which it is operating cannot change in value.
    363  */
    364 int
    365 itimerdecr(itp, usec)
    366 	register struct itimerval *itp;
    367 	int usec;
    368 {
    369 
    370 	if (itp->it_value.tv_usec < usec) {
    371 		if (itp->it_value.tv_sec == 0) {
    372 			/* expired, and already in next interval */
    373 			usec -= itp->it_value.tv_usec;
    374 			goto expire;
    375 		}
    376 		itp->it_value.tv_usec += 1000000;
    377 		itp->it_value.tv_sec--;
    378 	}
    379 	itp->it_value.tv_usec -= usec;
    380 	usec = 0;
    381 	if (timerisset(&itp->it_value))
    382 		return (1);
    383 	/* expired, exactly at end of interval */
    384 expire:
    385 	if (timerisset(&itp->it_interval)) {
    386 		itp->it_value = itp->it_interval;
    387 		itp->it_value.tv_usec -= usec;
    388 		if (itp->it_value.tv_usec < 0) {
    389 			itp->it_value.tv_usec += 1000000;
    390 			itp->it_value.tv_sec--;
    391 		}
    392 	} else
    393 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    394 	return (0);
    395 }
    396