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