Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.47.2.1
      1  1.47.2.1   thorpej /*	$NetBSD: kern_time.c,v 1.47.2.1 2000/07/13 20:18:12 thorpej Exp $	*/
      2      1.42       cgd 
      3      1.42       cgd /*-
      4      1.42       cgd  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5      1.42       cgd  * All rights reserved.
      6      1.42       cgd  *
      7      1.42       cgd  * This code is derived from software contributed to The NetBSD Foundation
      8      1.42       cgd  * by Christopher G. Demetriou.
      9      1.42       cgd  *
     10      1.42       cgd  * Redistribution and use in source and binary forms, with or without
     11      1.42       cgd  * modification, are permitted provided that the following conditions
     12      1.42       cgd  * are met:
     13      1.42       cgd  * 1. Redistributions of source code must retain the above copyright
     14      1.42       cgd  *    notice, this list of conditions and the following disclaimer.
     15      1.42       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.42       cgd  *    notice, this list of conditions and the following disclaimer in the
     17      1.42       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.42       cgd  * 3. All advertising materials mentioning features or use of this software
     19      1.42       cgd  *    must display the following acknowledgement:
     20      1.42       cgd  *	This product includes software developed by the NetBSD
     21      1.42       cgd  *	Foundation, Inc. and its contributors.
     22      1.42       cgd  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.42       cgd  *    contributors may be used to endorse or promote products derived
     24      1.42       cgd  *    from this software without specific prior written permission.
     25      1.42       cgd  *
     26      1.42       cgd  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.42       cgd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.42       cgd  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.42       cgd  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.42       cgd  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.42       cgd  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.42       cgd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.42       cgd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.42       cgd  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.42       cgd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.42       cgd  * POSSIBILITY OF SUCH DAMAGE.
     37      1.42       cgd  */
     38       1.9       cgd 
     39       1.1       cgd /*
     40       1.8       cgd  * Copyright (c) 1982, 1986, 1989, 1993
     41       1.8       cgd  *	The Regents of the University of California.  All rights reserved.
     42       1.1       cgd  *
     43       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44       1.1       cgd  * modification, are permitted provided that the following conditions
     45       1.1       cgd  * are met:
     46       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52       1.1       cgd  *    must display the following acknowledgement:
     53       1.1       cgd  *	This product includes software developed by the University of
     54       1.1       cgd  *	California, Berkeley and its contributors.
     55       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56       1.1       cgd  *    may be used to endorse or promote products derived from this software
     57       1.1       cgd  *    without specific prior written permission.
     58       1.1       cgd  *
     59       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69       1.1       cgd  * SUCH DAMAGE.
     70       1.1       cgd  *
     71      1.33      fvdl  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     72       1.1       cgd  */
     73      1.31   thorpej 
     74      1.31   thorpej #include "fs_nfs.h"
     75      1.34   thorpej #include "opt_nfsserver.h"
     76       1.1       cgd 
     77       1.5   mycroft #include <sys/param.h>
     78       1.5   mycroft #include <sys/resourcevar.h>
     79       1.5   mycroft #include <sys/kernel.h>
     80       1.8       cgd #include <sys/systm.h>
     81       1.5   mycroft #include <sys/proc.h>
     82       1.8       cgd #include <sys/vnode.h>
     83      1.17  christos #include <sys/signalvar.h>
     84      1.25     perry #include <sys/syslog.h>
     85       1.1       cgd 
     86      1.11       cgd #include <sys/mount.h>
     87      1.11       cgd #include <sys/syscallargs.h>
     88      1.19  christos 
     89      1.37   thorpej #include <vm/vm.h>
     90      1.37   thorpej #include <uvm/uvm_extern.h>
     91      1.37   thorpej 
     92      1.26   thorpej #if defined(NFS) || defined(NFSSERVER)
     93      1.20      fvdl #include <nfs/rpcv2.h>
     94      1.20      fvdl #include <nfs/nfsproto.h>
     95      1.19  christos #include <nfs/nfs_var.h>
     96      1.19  christos #endif
     97      1.17  christos 
     98       1.5   mycroft #include <machine/cpu.h>
     99      1.23       cgd 
    100      1.23       cgd /*
    101       1.1       cgd  * Time of day and interval timer support.
    102       1.1       cgd  *
    103       1.1       cgd  * These routines provide the kernel entry points to get and set
    104       1.1       cgd  * the time-of-day and per-process interval timers.  Subroutines
    105       1.1       cgd  * here provide support for adding and subtracting timeval structures
    106       1.1       cgd  * and decrementing interval timers, optionally reloading the interval
    107       1.1       cgd  * timers when they expire.
    108       1.1       cgd  */
    109       1.1       cgd 
    110      1.22       jtc /* This function is used by clock_settime and settimeofday */
    111      1.39      tron int
    112      1.22       jtc settime(tv)
    113      1.22       jtc 	struct timeval *tv;
    114      1.22       jtc {
    115      1.22       jtc 	struct timeval delta;
    116      1.47   thorpej 	struct cpu_info *ci;
    117      1.22       jtc 	int s;
    118      1.22       jtc 
    119      1.22       jtc 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    120      1.22       jtc 	s = splclock();
    121      1.22       jtc 	timersub(tv, &time, &delta);
    122      1.29       tls 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1)
    123      1.29       tls 		return (EPERM);
    124      1.29       tls #ifdef notyet
    125      1.29       tls 	if ((delta.tv_sec < 86400) && securelevel > 0)
    126      1.29       tls 		return (EPERM);
    127      1.29       tls #endif
    128      1.22       jtc 	time = *tv;
    129      1.38   thorpej 	(void) spllowersoftclock();
    130      1.22       jtc 	timeradd(&boottime, &delta, &boottime);
    131      1.47   thorpej 	/*
    132      1.47   thorpej 	 * XXXSMP
    133      1.47   thorpej 	 * This is wrong.  We should traverse a list of all
    134      1.47   thorpej 	 * CPUs and add the delta to the runtime of those
    135      1.47   thorpej 	 * CPUs which have a process on them.
    136      1.47   thorpej 	 */
    137      1.47   thorpej 	ci = curcpu();
    138      1.47   thorpej 	timeradd(&ci->ci_schedstate.spc_runtime, &delta,
    139      1.47   thorpej 	    &ci->ci_schedstate.spc_runtime);
    140      1.26   thorpej #	if defined(NFS) || defined(NFSSERVER)
    141      1.22       jtc 		nqnfs_lease_updatetime(delta.tv_sec);
    142      1.22       jtc #	endif
    143      1.22       jtc 	splx(s);
    144      1.22       jtc 	resettodr();
    145      1.29       tls 	return (0);
    146      1.22       jtc }
    147      1.22       jtc 
    148      1.22       jtc /* ARGSUSED */
    149      1.22       jtc int
    150      1.22       jtc sys_clock_gettime(p, v, retval)
    151      1.22       jtc 	struct proc *p;
    152      1.22       jtc 	void *v;
    153      1.22       jtc 	register_t *retval;
    154      1.22       jtc {
    155      1.45  augustss 	struct sys_clock_gettime_args /* {
    156      1.22       jtc 		syscallarg(clockid_t) clock_id;
    157      1.23       cgd 		syscallarg(struct timespec *) tp;
    158      1.23       cgd 	} */ *uap = v;
    159      1.22       jtc 	clockid_t clock_id;
    160      1.22       jtc 	struct timeval atv;
    161      1.22       jtc 	struct timespec ats;
    162      1.22       jtc 
    163      1.22       jtc 	clock_id = SCARG(uap, clock_id);
    164      1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    165      1.22       jtc 		return (EINVAL);
    166      1.22       jtc 
    167      1.22       jtc 	microtime(&atv);
    168      1.22       jtc 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
    169      1.22       jtc 
    170      1.24       cgd 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    171      1.22       jtc }
    172      1.22       jtc 
    173      1.22       jtc /* ARGSUSED */
    174      1.22       jtc int
    175      1.22       jtc sys_clock_settime(p, v, retval)
    176      1.22       jtc 	struct proc *p;
    177      1.22       jtc 	void *v;
    178      1.22       jtc 	register_t *retval;
    179      1.22       jtc {
    180      1.45  augustss 	struct sys_clock_settime_args /* {
    181      1.22       jtc 		syscallarg(clockid_t) clock_id;
    182      1.23       cgd 		syscallarg(const struct timespec *) tp;
    183      1.23       cgd 	} */ *uap = v;
    184      1.22       jtc 	clockid_t clock_id;
    185      1.22       jtc 	struct timeval atv;
    186      1.22       jtc 	struct timespec ats;
    187      1.22       jtc 	int error;
    188      1.22       jtc 
    189      1.22       jtc 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    190      1.22       jtc 		return (error);
    191      1.22       jtc 
    192      1.22       jtc 	clock_id = SCARG(uap, clock_id);
    193      1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    194      1.22       jtc 		return (EINVAL);
    195      1.22       jtc 
    196      1.24       cgd 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
    197      1.23       cgd 		return (error);
    198      1.22       jtc 
    199      1.22       jtc 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
    200      1.29       tls 	if ((error = settime(&atv)))
    201      1.29       tls 		return (error);
    202      1.22       jtc 
    203      1.22       jtc 	return 0;
    204      1.22       jtc }
    205      1.22       jtc 
    206      1.22       jtc int
    207      1.22       jtc sys_clock_getres(p, v, retval)
    208      1.22       jtc 	struct proc *p;
    209      1.22       jtc 	void *v;
    210      1.22       jtc 	register_t *retval;
    211      1.22       jtc {
    212      1.45  augustss 	struct sys_clock_getres_args /* {
    213      1.22       jtc 		syscallarg(clockid_t) clock_id;
    214      1.23       cgd 		syscallarg(struct timespec *) tp;
    215      1.23       cgd 	} */ *uap = v;
    216      1.22       jtc 	clockid_t clock_id;
    217      1.22       jtc 	struct timespec ts;
    218      1.22       jtc 	int error = 0;
    219      1.22       jtc 
    220      1.22       jtc 	clock_id = SCARG(uap, clock_id);
    221      1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    222      1.22       jtc 		return (EINVAL);
    223      1.22       jtc 
    224      1.22       jtc 	if (SCARG(uap, tp)) {
    225      1.22       jtc 		ts.tv_sec = 0;
    226      1.22       jtc 		ts.tv_nsec = 1000000000 / hz;
    227      1.22       jtc 
    228      1.35     perry 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    229      1.22       jtc 	}
    230      1.22       jtc 
    231      1.22       jtc 	return error;
    232      1.22       jtc }
    233      1.22       jtc 
    234      1.27       jtc /* ARGSUSED */
    235      1.27       jtc int
    236      1.27       jtc sys_nanosleep(p, v, retval)
    237      1.27       jtc 	struct proc *p;
    238      1.27       jtc 	void *v;
    239      1.27       jtc 	register_t *retval;
    240      1.27       jtc {
    241      1.27       jtc 	static int nanowait;
    242      1.45  augustss 	struct sys_nanosleep_args/* {
    243      1.27       jtc 		syscallarg(struct timespec *) rqtp;
    244      1.27       jtc 		syscallarg(struct timespec *) rmtp;
    245      1.27       jtc 	} */ *uap = v;
    246      1.27       jtc 	struct timespec rqt;
    247      1.27       jtc 	struct timespec rmt;
    248      1.27       jtc 	struct timeval atv, utv;
    249      1.27       jtc 	int error, s, timo;
    250      1.27       jtc 
    251      1.27       jtc 	error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
    252      1.27       jtc 		       sizeof(struct timespec));
    253      1.27       jtc 	if (error)
    254      1.27       jtc 		return (error);
    255      1.27       jtc 
    256      1.27       jtc 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
    257      1.27       jtc 	if (itimerfix(&atv))
    258      1.27       jtc 		return (EINVAL);
    259      1.27       jtc 
    260      1.27       jtc 	s = splclock();
    261      1.27       jtc 	timeradd(&atv,&time,&atv);
    262      1.27       jtc 	timo = hzto(&atv);
    263      1.27       jtc 	/*
    264      1.27       jtc 	 * Avoid inadvertantly sleeping forever
    265      1.27       jtc 	 */
    266      1.27       jtc 	if (timo == 0)
    267      1.27       jtc 		timo = 1;
    268      1.27       jtc 	splx(s);
    269      1.27       jtc 
    270      1.27       jtc 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
    271      1.27       jtc 	if (error == ERESTART)
    272      1.27       jtc 		error = EINTR;
    273      1.27       jtc 	if (error == EWOULDBLOCK)
    274      1.27       jtc 		error = 0;
    275      1.27       jtc 
    276      1.27       jtc 	if (SCARG(uap, rmtp)) {
    277      1.28       jtc 		int error;
    278      1.28       jtc 
    279      1.27       jtc 		s = splclock();
    280      1.27       jtc 		utv = time;
    281      1.27       jtc 		splx(s);
    282      1.27       jtc 
    283      1.27       jtc 		timersub(&atv, &utv, &utv);
    284      1.27       jtc 		if (utv.tv_sec < 0)
    285      1.27       jtc 			timerclear(&utv);
    286      1.27       jtc 
    287      1.27       jtc 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
    288      1.27       jtc 		error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
    289      1.28       jtc 			sizeof(rmt));
    290      1.28       jtc 		if (error)
    291      1.28       jtc 			return (error);
    292      1.27       jtc 	}
    293      1.27       jtc 
    294      1.27       jtc 	return error;
    295      1.27       jtc }
    296      1.22       jtc 
    297       1.1       cgd /* ARGSUSED */
    298       1.3    andrew int
    299      1.16   mycroft sys_gettimeofday(p, v, retval)
    300       1.1       cgd 	struct proc *p;
    301      1.15   thorpej 	void *v;
    302      1.15   thorpej 	register_t *retval;
    303      1.15   thorpej {
    304      1.45  augustss 	struct sys_gettimeofday_args /* {
    305      1.11       cgd 		syscallarg(struct timeval *) tp;
    306      1.11       cgd 		syscallarg(struct timezone *) tzp;
    307      1.15   thorpej 	} */ *uap = v;
    308       1.1       cgd 	struct timeval atv;
    309       1.1       cgd 	int error = 0;
    310      1.25     perry 	struct timezone tzfake;
    311       1.1       cgd 
    312      1.11       cgd 	if (SCARG(uap, tp)) {
    313       1.1       cgd 		microtime(&atv);
    314      1.35     perry 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    315      1.17  christos 		if (error)
    316       1.1       cgd 			return (error);
    317       1.1       cgd 	}
    318      1.25     perry 	if (SCARG(uap, tzp)) {
    319      1.25     perry 		/*
    320      1.32   mycroft 		 * NetBSD has no kernel notion of time zone, so we just
    321      1.25     perry 		 * fake up a timezone struct and return it if demanded.
    322      1.25     perry 		 */
    323      1.25     perry 		tzfake.tz_minuteswest = 0;
    324      1.25     perry 		tzfake.tz_dsttime = 0;
    325      1.35     perry 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    326      1.25     perry 	}
    327       1.1       cgd 	return (error);
    328       1.1       cgd }
    329       1.1       cgd 
    330       1.1       cgd /* ARGSUSED */
    331       1.3    andrew int
    332      1.16   mycroft sys_settimeofday(p, v, retval)
    333       1.1       cgd 	struct proc *p;
    334      1.15   thorpej 	void *v;
    335      1.15   thorpej 	register_t *retval;
    336      1.15   thorpej {
    337      1.16   mycroft 	struct sys_settimeofday_args /* {
    338      1.24       cgd 		syscallarg(const struct timeval *) tv;
    339      1.24       cgd 		syscallarg(const struct timezone *) tzp;
    340      1.15   thorpej 	} */ *uap = v;
    341      1.22       jtc 	struct timeval atv;
    342       1.1       cgd 	struct timezone atz;
    343      1.22       jtc 	int error;
    344       1.1       cgd 
    345      1.17  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    346       1.1       cgd 		return (error);
    347       1.8       cgd 	/* Verify all parameters before changing time. */
    348      1.24       cgd 	if (SCARG(uap, tv) && (error = copyin(SCARG(uap, tv),
    349      1.24       cgd 	    &atv, sizeof(atv))))
    350       1.8       cgd 		return (error);
    351      1.25     perry 	/* XXX since we don't use tz, probably no point in doing copyin. */
    352      1.24       cgd 	if (SCARG(uap, tzp) && (error = copyin(SCARG(uap, tzp),
    353      1.24       cgd 	    &atz, sizeof(atz))))
    354       1.8       cgd 		return (error);
    355      1.22       jtc 	if (SCARG(uap, tv))
    356      1.29       tls 		if ((error = settime(&atv)))
    357      1.29       tls 			return (error);
    358      1.25     perry 	/*
    359      1.32   mycroft 	 * NetBSD has no kernel notion of time zone, and only an
    360      1.25     perry 	 * obsolete program would try to set it, so we log a warning.
    361      1.25     perry 	 */
    362      1.11       cgd 	if (SCARG(uap, tzp))
    363      1.25     perry 		log(LOG_WARNING, "pid %d attempted to set the "
    364      1.32   mycroft 		    "(obsolete) kernel time zone\n", p->p_pid);
    365       1.8       cgd 	return (0);
    366       1.1       cgd }
    367       1.1       cgd 
    368       1.1       cgd int	tickdelta;			/* current clock skew, us. per tick */
    369       1.1       cgd long	timedelta;			/* unapplied time correction, us. */
    370       1.1       cgd long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    371       1.1       cgd 
    372       1.1       cgd /* ARGSUSED */
    373       1.3    andrew int
    374      1.16   mycroft sys_adjtime(p, v, retval)
    375       1.1       cgd 	struct proc *p;
    376      1.15   thorpej 	void *v;
    377      1.15   thorpej 	register_t *retval;
    378      1.15   thorpej {
    379      1.45  augustss 	struct sys_adjtime_args /* {
    380      1.24       cgd 		syscallarg(const struct timeval *) delta;
    381      1.11       cgd 		syscallarg(struct timeval *) olddelta;
    382      1.15   thorpej 	} */ *uap = v;
    383       1.8       cgd 	struct timeval atv;
    384      1.45  augustss 	long ndelta, ntickdelta, odelta;
    385       1.1       cgd 	int s, error;
    386       1.1       cgd 
    387      1.17  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    388       1.1       cgd 		return (error);
    389      1.17  christos 
    390      1.24       cgd 	error = copyin(SCARG(uap, delta), &atv, sizeof(struct timeval));
    391      1.17  christos 	if (error)
    392       1.1       cgd 		return (error);
    393      1.37   thorpej 	if (SCARG(uap, olddelta) != NULL &&
    394      1.37   thorpej 	    uvm_useracc((caddr_t)SCARG(uap, olddelta), sizeof(struct timeval),
    395      1.37   thorpej 	     B_WRITE) == FALSE)
    396      1.37   thorpej 		return (EFAULT);
    397       1.8       cgd 
    398       1.8       cgd 	/*
    399       1.8       cgd 	 * Compute the total correction and the rate at which to apply it.
    400       1.8       cgd 	 * Round the adjustment down to a whole multiple of the per-tick
    401       1.8       cgd 	 * delta, so that after some number of incremental changes in
    402       1.8       cgd 	 * hardclock(), tickdelta will become zero, lest the correction
    403       1.8       cgd 	 * overshoot and start taking us away from the desired final time.
    404       1.8       cgd 	 */
    405       1.1       cgd 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    406      1.41       hwr 	if (ndelta > bigadj || ndelta < -bigadj)
    407       1.8       cgd 		ntickdelta = 10 * tickadj;
    408       1.8       cgd 	else
    409       1.8       cgd 		ntickdelta = tickadj;
    410       1.8       cgd 	if (ndelta % ntickdelta)
    411       1.8       cgd 		ndelta = ndelta / ntickdelta * ntickdelta;
    412       1.8       cgd 
    413       1.8       cgd 	/*
    414       1.8       cgd 	 * To make hardclock()'s job easier, make the per-tick delta negative
    415       1.8       cgd 	 * if we want time to run slower; then hardclock can simply compute
    416       1.8       cgd 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    417       1.8       cgd 	 */
    418       1.8       cgd 	if (ndelta < 0)
    419       1.8       cgd 		ntickdelta = -ntickdelta;
    420       1.1       cgd 	s = splclock();
    421       1.8       cgd 	odelta = timedelta;
    422       1.1       cgd 	timedelta = ndelta;
    423       1.8       cgd 	tickdelta = ntickdelta;
    424       1.1       cgd 	splx(s);
    425       1.1       cgd 
    426      1.11       cgd 	if (SCARG(uap, olddelta)) {
    427       1.8       cgd 		atv.tv_sec = odelta / 1000000;
    428       1.8       cgd 		atv.tv_usec = odelta % 1000000;
    429      1.24       cgd 		(void) copyout(&atv, SCARG(uap, olddelta),
    430       1.8       cgd 		    sizeof(struct timeval));
    431       1.8       cgd 	}
    432       1.1       cgd 	return (0);
    433       1.1       cgd }
    434       1.1       cgd 
    435       1.1       cgd /*
    436       1.1       cgd  * Get value of an interval timer.  The process virtual and
    437       1.1       cgd  * profiling virtual time timers are kept in the p_stats area, since
    438       1.1       cgd  * they can be swapped out.  These are kept internally in the
    439       1.1       cgd  * way they are specified externally: in time until they expire.
    440       1.1       cgd  *
    441       1.1       cgd  * The real time interval timer is kept in the process table slot
    442       1.1       cgd  * for the process, and its value (it_value) is kept as an
    443       1.1       cgd  * absolute time rather than as a delta, so that it is easy to keep
    444       1.1       cgd  * periodic real-time signals from drifting.
    445       1.1       cgd  *
    446       1.1       cgd  * Virtual time timers are processed in the hardclock() routine of
    447       1.1       cgd  * kern_clock.c.  The real time timer is processed by a timeout
    448       1.1       cgd  * routine, called from the softclock() routine.  Since a callout
    449       1.1       cgd  * may be delayed in real time due to interrupt processing in the system,
    450       1.1       cgd  * it is possible for the real time timeout routine (realitexpire, given below),
    451       1.1       cgd  * to be delayed in real time past when it is supposed to occur.  It
    452       1.1       cgd  * does not suffice, therefore, to reload the real timer .it_value from the
    453       1.1       cgd  * real time timers .it_interval.  Rather, we compute the next time in
    454       1.1       cgd  * absolute time the timer should go off.
    455       1.1       cgd  */
    456       1.1       cgd /* ARGSUSED */
    457       1.3    andrew int
    458      1.16   mycroft sys_getitimer(p, v, retval)
    459       1.1       cgd 	struct proc *p;
    460      1.15   thorpej 	void *v;
    461      1.15   thorpej 	register_t *retval;
    462      1.15   thorpej {
    463      1.45  augustss 	struct sys_getitimer_args /* {
    464      1.30   mycroft 		syscallarg(int) which;
    465      1.11       cgd 		syscallarg(struct itimerval *) itv;
    466      1.15   thorpej 	} */ *uap = v;
    467      1.30   mycroft 	int which = SCARG(uap, which);
    468       1.1       cgd 	struct itimerval aitv;
    469       1.1       cgd 	int s;
    470       1.1       cgd 
    471      1.30   mycroft 	if ((u_int)which > ITIMER_PROF)
    472       1.1       cgd 		return (EINVAL);
    473       1.1       cgd 	s = splclock();
    474      1.30   mycroft 	if (which == ITIMER_REAL) {
    475       1.1       cgd 		/*
    476      1.12   mycroft 		 * Convert from absolute to relative time in .it_value
    477       1.1       cgd 		 * part of real time timer.  If time for real time timer
    478       1.1       cgd 		 * has passed return 0, else return difference between
    479       1.1       cgd 		 * current time and time for the timer to go off.
    480       1.1       cgd 		 */
    481       1.1       cgd 		aitv = p->p_realtimer;
    482      1.36   thorpej 		if (timerisset(&aitv.it_value)) {
    483       1.1       cgd 			if (timercmp(&aitv.it_value, &time, <))
    484       1.1       cgd 				timerclear(&aitv.it_value);
    485       1.1       cgd 			else
    486      1.14   mycroft 				timersub(&aitv.it_value, &time, &aitv.it_value);
    487      1.36   thorpej 		}
    488       1.1       cgd 	} else
    489      1.30   mycroft 		aitv = p->p_stats->p_timer[which];
    490       1.1       cgd 	splx(s);
    491      1.35     perry 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
    492       1.1       cgd }
    493       1.1       cgd 
    494       1.1       cgd /* ARGSUSED */
    495       1.3    andrew int
    496      1.16   mycroft sys_setitimer(p, v, retval)
    497       1.1       cgd 	struct proc *p;
    498      1.45  augustss 	void *v;
    499      1.15   thorpej 	register_t *retval;
    500      1.15   thorpej {
    501      1.45  augustss 	struct sys_setitimer_args /* {
    502      1.30   mycroft 		syscallarg(int) which;
    503      1.24       cgd 		syscallarg(const struct itimerval *) itv;
    504      1.11       cgd 		syscallarg(struct itimerval *) oitv;
    505      1.15   thorpej 	} */ *uap = v;
    506      1.30   mycroft 	int which = SCARG(uap, which);
    507      1.21       cgd 	struct sys_getitimer_args getargs;
    508       1.1       cgd 	struct itimerval aitv;
    509      1.45  augustss 	const struct itimerval *itvp;
    510       1.1       cgd 	int s, error;
    511       1.1       cgd 
    512      1.30   mycroft 	if ((u_int)which > ITIMER_PROF)
    513       1.1       cgd 		return (EINVAL);
    514      1.11       cgd 	itvp = SCARG(uap, itv);
    515      1.24       cgd 	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
    516       1.1       cgd 		return (error);
    517      1.21       cgd 	if (SCARG(uap, oitv) != NULL) {
    518      1.30   mycroft 		SCARG(&getargs, which) = which;
    519      1.21       cgd 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    520      1.23       cgd 		if ((error = sys_getitimer(p, &getargs, retval)) != 0)
    521      1.21       cgd 			return (error);
    522      1.21       cgd 	}
    523       1.1       cgd 	if (itvp == 0)
    524       1.1       cgd 		return (0);
    525       1.1       cgd 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    526       1.1       cgd 		return (EINVAL);
    527       1.1       cgd 	s = splclock();
    528      1.30   mycroft 	if (which == ITIMER_REAL) {
    529      1.44   thorpej 		callout_stop(&p->p_realit_ch);
    530       1.1       cgd 		if (timerisset(&aitv.it_value)) {
    531  1.47.2.1   thorpej 			/*
    532  1.47.2.1   thorpej 			 * Don't need to check hzto() return value, here.
    533  1.47.2.1   thorpej 			 * callout_reset() does it for us.
    534  1.47.2.1   thorpej 			 */
    535      1.14   mycroft 			timeradd(&aitv.it_value, &time, &aitv.it_value);
    536      1.44   thorpej 			callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
    537      1.44   thorpej 			    realitexpire, p);
    538       1.1       cgd 		}
    539       1.1       cgd 		p->p_realtimer = aitv;
    540       1.1       cgd 	} else
    541      1.30   mycroft 		p->p_stats->p_timer[which] = aitv;
    542       1.1       cgd 	splx(s);
    543       1.1       cgd 	return (0);
    544       1.1       cgd }
    545       1.1       cgd 
    546       1.1       cgd /*
    547       1.1       cgd  * Real interval timer expired:
    548       1.1       cgd  * send process whose timer expired an alarm signal.
    549       1.1       cgd  * If time is not set up to reload, then just return.
    550       1.1       cgd  * Else compute next time timer should go off which is > current time.
    551       1.1       cgd  * This is where delay in processing this timeout causes multiple
    552       1.1       cgd  * SIGALRM calls to be compressed into one.
    553       1.1       cgd  */
    554       1.3    andrew void
    555       1.6       cgd realitexpire(arg)
    556       1.6       cgd 	void *arg;
    557       1.6       cgd {
    558      1.45  augustss 	struct proc *p;
    559       1.1       cgd 	int s;
    560       1.1       cgd 
    561       1.6       cgd 	p = (struct proc *)arg;
    562       1.1       cgd 	psignal(p, SIGALRM);
    563       1.1       cgd 	if (!timerisset(&p->p_realtimer.it_interval)) {
    564       1.1       cgd 		timerclear(&p->p_realtimer.it_value);
    565       1.1       cgd 		return;
    566       1.1       cgd 	}
    567       1.1       cgd 	for (;;) {
    568       1.1       cgd 		s = splclock();
    569      1.14   mycroft 		timeradd(&p->p_realtimer.it_value,
    570      1.14   mycroft 		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
    571       1.1       cgd 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    572  1.47.2.1   thorpej 			/*
    573  1.47.2.1   thorpej 			 * Don't need to check hzto() return value, here.
    574  1.47.2.1   thorpej 			 * callout_reset() does it for us.
    575  1.47.2.1   thorpej 			 */
    576      1.44   thorpej 			callout_reset(&p->p_realit_ch,
    577      1.44   thorpej 			    hzto(&p->p_realtimer.it_value), realitexpire, p);
    578       1.1       cgd 			splx(s);
    579       1.1       cgd 			return;
    580       1.1       cgd 		}
    581       1.1       cgd 		splx(s);
    582       1.1       cgd 	}
    583       1.1       cgd }
    584       1.1       cgd 
    585       1.1       cgd /*
    586       1.1       cgd  * Check that a proposed value to load into the .it_value or
    587       1.1       cgd  * .it_interval part of an interval timer is acceptable, and
    588       1.1       cgd  * fix it to have at least minimal value (i.e. if it is less
    589       1.1       cgd  * than the resolution of the clock, round it up.)
    590       1.1       cgd  */
    591       1.3    andrew int
    592       1.1       cgd itimerfix(tv)
    593       1.1       cgd 	struct timeval *tv;
    594       1.1       cgd {
    595       1.1       cgd 
    596       1.1       cgd 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
    597       1.1       cgd 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    598       1.1       cgd 		return (EINVAL);
    599       1.1       cgd 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    600       1.1       cgd 		tv->tv_usec = tick;
    601       1.1       cgd 	return (0);
    602       1.1       cgd }
    603       1.1       cgd 
    604       1.1       cgd /*
    605       1.1       cgd  * Decrement an interval timer by a specified number
    606       1.1       cgd  * of microseconds, which must be less than a second,
    607       1.1       cgd  * i.e. < 1000000.  If the timer expires, then reload
    608       1.1       cgd  * it.  In this case, carry over (usec - old value) to
    609       1.8       cgd  * reduce the value reloaded into the timer so that
    610       1.1       cgd  * the timer does not drift.  This routine assumes
    611       1.1       cgd  * that it is called in a context where the timers
    612       1.1       cgd  * on which it is operating cannot change in value.
    613       1.1       cgd  */
    614       1.3    andrew int
    615       1.1       cgd itimerdecr(itp, usec)
    616      1.45  augustss 	struct itimerval *itp;
    617       1.1       cgd 	int usec;
    618       1.1       cgd {
    619       1.1       cgd 
    620       1.1       cgd 	if (itp->it_value.tv_usec < usec) {
    621       1.1       cgd 		if (itp->it_value.tv_sec == 0) {
    622       1.1       cgd 			/* expired, and already in next interval */
    623       1.1       cgd 			usec -= itp->it_value.tv_usec;
    624       1.1       cgd 			goto expire;
    625       1.1       cgd 		}
    626       1.1       cgd 		itp->it_value.tv_usec += 1000000;
    627       1.1       cgd 		itp->it_value.tv_sec--;
    628       1.1       cgd 	}
    629       1.1       cgd 	itp->it_value.tv_usec -= usec;
    630       1.1       cgd 	usec = 0;
    631       1.1       cgd 	if (timerisset(&itp->it_value))
    632       1.1       cgd 		return (1);
    633       1.1       cgd 	/* expired, exactly at end of interval */
    634       1.1       cgd expire:
    635       1.1       cgd 	if (timerisset(&itp->it_interval)) {
    636       1.1       cgd 		itp->it_value = itp->it_interval;
    637       1.1       cgd 		itp->it_value.tv_usec -= usec;
    638       1.1       cgd 		if (itp->it_value.tv_usec < 0) {
    639       1.1       cgd 			itp->it_value.tv_usec += 1000000;
    640       1.1       cgd 			itp->it_value.tv_sec--;
    641       1.1       cgd 		}
    642       1.1       cgd 	} else
    643       1.1       cgd 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    644       1.1       cgd 	return (0);
    645      1.42       cgd }
    646      1.42       cgd 
    647      1.42       cgd /*
    648      1.42       cgd  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
    649      1.42       cgd  * for usage and rationale.
    650      1.42       cgd  */
    651      1.42       cgd int
    652      1.42       cgd ratecheck(lasttime, mininterval)
    653      1.42       cgd 	struct timeval *lasttime;
    654      1.42       cgd 	const struct timeval *mininterval;
    655      1.42       cgd {
    656      1.42       cgd 	struct timeval delta;
    657      1.42       cgd 	int s, rv = 0;
    658      1.42       cgd 
    659      1.42       cgd 	s = splclock();
    660      1.43    itojun 	timersub(&mono_time, lasttime, &delta);
    661      1.42       cgd 
    662      1.42       cgd 	/*
    663      1.42       cgd 	 * check for 0,0 is so that the message will be seen at least once,
    664      1.42       cgd 	 * even if interval is huge.
    665      1.42       cgd 	 */
    666      1.42       cgd 	if (timercmp(&delta, mininterval, >=) ||
    667      1.42       cgd 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
    668      1.42       cgd 		*lasttime = mono_time;
    669      1.42       cgd 		rv = 1;
    670      1.42       cgd 	}
    671      1.42       cgd 	splx(s);
    672      1.42       cgd 
    673      1.42       cgd 	return (rv);
    674       1.1       cgd }
    675