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