Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.54.2.2
      1  1.54.2.2   nathanw /*	$NetBSD: kern_time.c,v 1.54.2.2 2001/06/21 20:06:57 nathanw 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.54     bjh21 #include "opt_nfs.h"
     76      1.34   thorpej #include "opt_nfsserver.h"
     77       1.1       cgd 
     78       1.5   mycroft #include <sys/param.h>
     79       1.5   mycroft #include <sys/resourcevar.h>
     80       1.5   mycroft #include <sys/kernel.h>
     81       1.8       cgd #include <sys/systm.h>
     82  1.54.2.1   nathanw #include <sys/lwp.h>
     83       1.5   mycroft #include <sys/proc.h>
     84       1.8       cgd #include <sys/vnode.h>
     85      1.17  christos #include <sys/signalvar.h>
     86      1.25     perry #include <sys/syslog.h>
     87       1.1       cgd 
     88      1.11       cgd #include <sys/mount.h>
     89      1.11       cgd #include <sys/syscallargs.h>
     90      1.19  christos 
     91      1.37   thorpej #include <uvm/uvm_extern.h>
     92      1.37   thorpej 
     93      1.26   thorpej #if defined(NFS) || defined(NFSSERVER)
     94      1.20      fvdl #include <nfs/rpcv2.h>
     95      1.20      fvdl #include <nfs/nfsproto.h>
     96      1.19  christos #include <nfs/nfs_var.h>
     97      1.19  christos #endif
     98      1.17  christos 
     99       1.5   mycroft #include <machine/cpu.h>
    100      1.23       cgd 
    101      1.23       cgd /*
    102       1.1       cgd  * Time of day and interval timer support.
    103       1.1       cgd  *
    104       1.1       cgd  * These routines provide the kernel entry points to get and set
    105       1.1       cgd  * the time-of-day and per-process interval timers.  Subroutines
    106       1.1       cgd  * here provide support for adding and subtracting timeval structures
    107       1.1       cgd  * and decrementing interval timers, optionally reloading the interval
    108       1.1       cgd  * timers when they expire.
    109       1.1       cgd  */
    110       1.1       cgd 
    111      1.22       jtc /* This function is used by clock_settime and settimeofday */
    112      1.39      tron int
    113      1.22       jtc settime(tv)
    114      1.22       jtc 	struct timeval *tv;
    115      1.22       jtc {
    116      1.22       jtc 	struct timeval delta;
    117      1.47   thorpej 	struct cpu_info *ci;
    118      1.22       jtc 	int s;
    119      1.22       jtc 
    120      1.22       jtc 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    121      1.22       jtc 	s = splclock();
    122      1.22       jtc 	timersub(tv, &time, &delta);
    123  1.54.2.2   nathanw 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
    124  1.54.2.2   nathanw 		splx(s);
    125      1.29       tls 		return (EPERM);
    126  1.54.2.2   nathanw 	}
    127      1.29       tls #ifdef notyet
    128  1.54.2.2   nathanw 	if ((delta.tv_sec < 86400) && securelevel > 0) {
    129  1.54.2.2   nathanw 		splx(s);
    130      1.29       tls 		return (EPERM);
    131  1.54.2.2   nathanw 	}
    132      1.29       tls #endif
    133      1.22       jtc 	time = *tv;
    134      1.38   thorpej 	(void) spllowersoftclock();
    135      1.22       jtc 	timeradd(&boottime, &delta, &boottime);
    136      1.47   thorpej 	/*
    137      1.47   thorpej 	 * XXXSMP
    138      1.47   thorpej 	 * This is wrong.  We should traverse a list of all
    139      1.47   thorpej 	 * CPUs and add the delta to the runtime of those
    140      1.47   thorpej 	 * CPUs which have a process on them.
    141      1.47   thorpej 	 */
    142      1.47   thorpej 	ci = curcpu();
    143      1.47   thorpej 	timeradd(&ci->ci_schedstate.spc_runtime, &delta,
    144      1.47   thorpej 	    &ci->ci_schedstate.spc_runtime);
    145      1.54     bjh21 #	if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
    146      1.22       jtc 		nqnfs_lease_updatetime(delta.tv_sec);
    147      1.22       jtc #	endif
    148      1.22       jtc 	splx(s);
    149      1.22       jtc 	resettodr();
    150      1.29       tls 	return (0);
    151      1.22       jtc }
    152      1.22       jtc 
    153      1.22       jtc /* ARGSUSED */
    154      1.22       jtc int
    155  1.54.2.1   nathanw sys_clock_gettime(l, v, retval)
    156  1.54.2.1   nathanw 	struct lwp *l;
    157      1.22       jtc 	void *v;
    158      1.22       jtc 	register_t *retval;
    159      1.22       jtc {
    160      1.45  augustss 	struct sys_clock_gettime_args /* {
    161      1.22       jtc 		syscallarg(clockid_t) clock_id;
    162      1.23       cgd 		syscallarg(struct timespec *) tp;
    163      1.23       cgd 	} */ *uap = v;
    164      1.22       jtc 	clockid_t clock_id;
    165      1.22       jtc 	struct timeval atv;
    166      1.22       jtc 	struct timespec ats;
    167      1.22       jtc 
    168      1.22       jtc 	clock_id = SCARG(uap, clock_id);
    169      1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    170      1.22       jtc 		return (EINVAL);
    171      1.22       jtc 
    172      1.22       jtc 	microtime(&atv);
    173      1.22       jtc 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
    174      1.22       jtc 
    175      1.24       cgd 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    176      1.22       jtc }
    177      1.22       jtc 
    178      1.22       jtc /* ARGSUSED */
    179      1.22       jtc int
    180  1.54.2.1   nathanw sys_clock_settime(l, v, retval)
    181  1.54.2.1   nathanw 	struct lwp *l;
    182      1.22       jtc 	void *v;
    183      1.22       jtc 	register_t *retval;
    184      1.22       jtc {
    185      1.45  augustss 	struct sys_clock_settime_args /* {
    186      1.22       jtc 		syscallarg(clockid_t) clock_id;
    187      1.23       cgd 		syscallarg(const struct timespec *) tp;
    188      1.23       cgd 	} */ *uap = v;
    189  1.54.2.1   nathanw 	struct proc *p = l->l_proc;
    190      1.22       jtc 	clockid_t clock_id;
    191      1.22       jtc 	struct timeval atv;
    192      1.22       jtc 	struct timespec ats;
    193      1.22       jtc 	int error;
    194      1.22       jtc 
    195      1.22       jtc 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    196      1.22       jtc 		return (error);
    197      1.22       jtc 
    198      1.22       jtc 	clock_id = SCARG(uap, clock_id);
    199      1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    200      1.22       jtc 		return (EINVAL);
    201      1.22       jtc 
    202      1.24       cgd 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
    203      1.23       cgd 		return (error);
    204      1.22       jtc 
    205      1.22       jtc 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
    206      1.29       tls 	if ((error = settime(&atv)))
    207      1.29       tls 		return (error);
    208      1.22       jtc 
    209      1.22       jtc 	return 0;
    210      1.22       jtc }
    211      1.22       jtc 
    212      1.22       jtc int
    213  1.54.2.1   nathanw sys_clock_getres(l, v, retval)
    214  1.54.2.1   nathanw 	struct lwp *l;
    215      1.22       jtc 	void *v;
    216      1.22       jtc 	register_t *retval;
    217      1.22       jtc {
    218      1.45  augustss 	struct sys_clock_getres_args /* {
    219      1.22       jtc 		syscallarg(clockid_t) clock_id;
    220      1.23       cgd 		syscallarg(struct timespec *) tp;
    221      1.23       cgd 	} */ *uap = v;
    222      1.22       jtc 	clockid_t clock_id;
    223      1.22       jtc 	struct timespec ts;
    224      1.22       jtc 	int error = 0;
    225      1.22       jtc 
    226      1.22       jtc 	clock_id = SCARG(uap, clock_id);
    227      1.22       jtc 	if (clock_id != CLOCK_REALTIME)
    228      1.22       jtc 		return (EINVAL);
    229      1.22       jtc 
    230      1.22       jtc 	if (SCARG(uap, tp)) {
    231      1.22       jtc 		ts.tv_sec = 0;
    232      1.22       jtc 		ts.tv_nsec = 1000000000 / hz;
    233      1.22       jtc 
    234      1.35     perry 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    235      1.22       jtc 	}
    236      1.22       jtc 
    237      1.22       jtc 	return error;
    238      1.22       jtc }
    239      1.22       jtc 
    240      1.27       jtc /* ARGSUSED */
    241      1.27       jtc int
    242  1.54.2.1   nathanw sys_nanosleep(l, v, retval)
    243  1.54.2.1   nathanw 	struct lwp *l;
    244      1.27       jtc 	void *v;
    245      1.27       jtc 	register_t *retval;
    246      1.27       jtc {
    247      1.27       jtc 	static int nanowait;
    248      1.45  augustss 	struct sys_nanosleep_args/* {
    249      1.27       jtc 		syscallarg(struct timespec *) rqtp;
    250      1.27       jtc 		syscallarg(struct timespec *) rmtp;
    251      1.27       jtc 	} */ *uap = v;
    252      1.27       jtc 	struct timespec rqt;
    253      1.27       jtc 	struct timespec rmt;
    254      1.27       jtc 	struct timeval atv, utv;
    255      1.27       jtc 	int error, s, timo;
    256      1.27       jtc 
    257      1.27       jtc 	error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
    258      1.27       jtc 		       sizeof(struct timespec));
    259      1.27       jtc 	if (error)
    260      1.27       jtc 		return (error);
    261      1.27       jtc 
    262      1.27       jtc 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
    263      1.27       jtc 	if (itimerfix(&atv))
    264      1.27       jtc 		return (EINVAL);
    265      1.27       jtc 
    266      1.27       jtc 	s = splclock();
    267      1.27       jtc 	timeradd(&atv,&time,&atv);
    268      1.27       jtc 	timo = hzto(&atv);
    269      1.27       jtc 	/*
    270      1.27       jtc 	 * Avoid inadvertantly sleeping forever
    271      1.27       jtc 	 */
    272      1.27       jtc 	if (timo == 0)
    273      1.27       jtc 		timo = 1;
    274      1.27       jtc 	splx(s);
    275      1.27       jtc 
    276      1.27       jtc 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
    277      1.27       jtc 	if (error == ERESTART)
    278      1.27       jtc 		error = EINTR;
    279      1.27       jtc 	if (error == EWOULDBLOCK)
    280      1.27       jtc 		error = 0;
    281      1.27       jtc 
    282      1.27       jtc 	if (SCARG(uap, rmtp)) {
    283      1.28       jtc 		int error;
    284      1.28       jtc 
    285      1.27       jtc 		s = splclock();
    286      1.27       jtc 		utv = time;
    287      1.27       jtc 		splx(s);
    288      1.27       jtc 
    289      1.27       jtc 		timersub(&atv, &utv, &utv);
    290      1.27       jtc 		if (utv.tv_sec < 0)
    291      1.27       jtc 			timerclear(&utv);
    292      1.27       jtc 
    293      1.27       jtc 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
    294      1.27       jtc 		error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
    295      1.28       jtc 			sizeof(rmt));
    296      1.28       jtc 		if (error)
    297      1.28       jtc 			return (error);
    298      1.27       jtc 	}
    299      1.27       jtc 
    300      1.27       jtc 	return error;
    301      1.27       jtc }
    302      1.22       jtc 
    303       1.1       cgd /* ARGSUSED */
    304       1.3    andrew int
    305  1.54.2.1   nathanw sys_gettimeofday(l, v, retval)
    306  1.54.2.1   nathanw 	struct lwp *l;
    307      1.15   thorpej 	void *v;
    308      1.15   thorpej 	register_t *retval;
    309      1.15   thorpej {
    310      1.45  augustss 	struct sys_gettimeofday_args /* {
    311      1.11       cgd 		syscallarg(struct timeval *) tp;
    312      1.11       cgd 		syscallarg(struct timezone *) tzp;
    313      1.15   thorpej 	} */ *uap = v;
    314       1.1       cgd 	struct timeval atv;
    315       1.1       cgd 	int error = 0;
    316      1.25     perry 	struct timezone tzfake;
    317       1.1       cgd 
    318      1.11       cgd 	if (SCARG(uap, tp)) {
    319       1.1       cgd 		microtime(&atv);
    320      1.35     perry 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    321      1.17  christos 		if (error)
    322       1.1       cgd 			return (error);
    323       1.1       cgd 	}
    324      1.25     perry 	if (SCARG(uap, tzp)) {
    325      1.25     perry 		/*
    326      1.32   mycroft 		 * NetBSD has no kernel notion of time zone, so we just
    327      1.25     perry 		 * fake up a timezone struct and return it if demanded.
    328      1.25     perry 		 */
    329      1.25     perry 		tzfake.tz_minuteswest = 0;
    330      1.25     perry 		tzfake.tz_dsttime = 0;
    331      1.35     perry 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    332      1.25     perry 	}
    333       1.1       cgd 	return (error);
    334       1.1       cgd }
    335       1.1       cgd 
    336       1.1       cgd /* ARGSUSED */
    337       1.3    andrew int
    338  1.54.2.1   nathanw sys_settimeofday(l, v, retval)
    339  1.54.2.1   nathanw 	struct lwp *l;
    340      1.15   thorpej 	void *v;
    341      1.15   thorpej 	register_t *retval;
    342      1.15   thorpej {
    343      1.16   mycroft 	struct sys_settimeofday_args /* {
    344      1.24       cgd 		syscallarg(const struct timeval *) tv;
    345      1.24       cgd 		syscallarg(const struct timezone *) tzp;
    346      1.15   thorpej 	} */ *uap = v;
    347  1.54.2.1   nathanw 	struct proc *p = l->l_proc;
    348      1.22       jtc 	struct timeval atv;
    349       1.1       cgd 	struct timezone atz;
    350      1.22       jtc 	int error;
    351       1.1       cgd 
    352      1.17  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    353       1.1       cgd 		return (error);
    354       1.8       cgd 	/* Verify all parameters before changing time. */
    355      1.24       cgd 	if (SCARG(uap, tv) && (error = copyin(SCARG(uap, tv),
    356      1.24       cgd 	    &atv, sizeof(atv))))
    357       1.8       cgd 		return (error);
    358      1.25     perry 	/* XXX since we don't use tz, probably no point in doing copyin. */
    359      1.24       cgd 	if (SCARG(uap, tzp) && (error = copyin(SCARG(uap, tzp),
    360      1.24       cgd 	    &atz, sizeof(atz))))
    361       1.8       cgd 		return (error);
    362      1.22       jtc 	if (SCARG(uap, tv))
    363      1.29       tls 		if ((error = settime(&atv)))
    364      1.29       tls 			return (error);
    365      1.25     perry 	/*
    366      1.32   mycroft 	 * NetBSD has no kernel notion of time zone, and only an
    367      1.25     perry 	 * obsolete program would try to set it, so we log a warning.
    368      1.25     perry 	 */
    369      1.11       cgd 	if (SCARG(uap, tzp))
    370      1.25     perry 		log(LOG_WARNING, "pid %d attempted to set the "
    371      1.32   mycroft 		    "(obsolete) kernel time zone\n", p->p_pid);
    372       1.8       cgd 	return (0);
    373       1.1       cgd }
    374       1.1       cgd 
    375       1.1       cgd int	tickdelta;			/* current clock skew, us. per tick */
    376       1.1       cgd long	timedelta;			/* unapplied time correction, us. */
    377       1.1       cgd long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    378       1.1       cgd 
    379       1.1       cgd /* ARGSUSED */
    380       1.3    andrew int
    381  1.54.2.1   nathanw sys_adjtime(l, v, retval)
    382  1.54.2.1   nathanw 	struct lwp *l;
    383      1.15   thorpej 	void *v;
    384      1.15   thorpej 	register_t *retval;
    385      1.15   thorpej {
    386      1.45  augustss 	struct sys_adjtime_args /* {
    387      1.24       cgd 		syscallarg(const struct timeval *) delta;
    388      1.11       cgd 		syscallarg(struct timeval *) olddelta;
    389      1.15   thorpej 	} */ *uap = v;
    390  1.54.2.1   nathanw 	struct proc *p = l->l_proc;
    391       1.8       cgd 	struct timeval atv;
    392      1.45  augustss 	long ndelta, ntickdelta, odelta;
    393       1.1       cgd 	int s, error;
    394       1.1       cgd 
    395      1.17  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    396       1.1       cgd 		return (error);
    397      1.17  christos 
    398      1.24       cgd 	error = copyin(SCARG(uap, delta), &atv, sizeof(struct timeval));
    399      1.17  christos 	if (error)
    400       1.1       cgd 		return (error);
    401      1.37   thorpej 	if (SCARG(uap, olddelta) != NULL &&
    402      1.37   thorpej 	    uvm_useracc((caddr_t)SCARG(uap, olddelta), sizeof(struct timeval),
    403      1.37   thorpej 	     B_WRITE) == FALSE)
    404      1.37   thorpej 		return (EFAULT);
    405       1.8       cgd 
    406       1.8       cgd 	/*
    407       1.8       cgd 	 * Compute the total correction and the rate at which to apply it.
    408       1.8       cgd 	 * Round the adjustment down to a whole multiple of the per-tick
    409       1.8       cgd 	 * delta, so that after some number of incremental changes in
    410       1.8       cgd 	 * hardclock(), tickdelta will become zero, lest the correction
    411       1.8       cgd 	 * overshoot and start taking us away from the desired final time.
    412       1.8       cgd 	 */
    413       1.1       cgd 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    414      1.41       hwr 	if (ndelta > bigadj || ndelta < -bigadj)
    415       1.8       cgd 		ntickdelta = 10 * tickadj;
    416       1.8       cgd 	else
    417       1.8       cgd 		ntickdelta = tickadj;
    418       1.8       cgd 	if (ndelta % ntickdelta)
    419       1.8       cgd 		ndelta = ndelta / ntickdelta * ntickdelta;
    420       1.8       cgd 
    421       1.8       cgd 	/*
    422       1.8       cgd 	 * To make hardclock()'s job easier, make the per-tick delta negative
    423       1.8       cgd 	 * if we want time to run slower; then hardclock can simply compute
    424       1.8       cgd 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    425       1.8       cgd 	 */
    426       1.8       cgd 	if (ndelta < 0)
    427       1.8       cgd 		ntickdelta = -ntickdelta;
    428       1.1       cgd 	s = splclock();
    429       1.8       cgd 	odelta = timedelta;
    430       1.1       cgd 	timedelta = ndelta;
    431       1.8       cgd 	tickdelta = ntickdelta;
    432       1.1       cgd 	splx(s);
    433       1.1       cgd 
    434      1.11       cgd 	if (SCARG(uap, olddelta)) {
    435       1.8       cgd 		atv.tv_sec = odelta / 1000000;
    436       1.8       cgd 		atv.tv_usec = odelta % 1000000;
    437      1.24       cgd 		(void) copyout(&atv, SCARG(uap, olddelta),
    438       1.8       cgd 		    sizeof(struct timeval));
    439       1.8       cgd 	}
    440       1.1       cgd 	return (0);
    441       1.1       cgd }
    442       1.1       cgd 
    443       1.1       cgd /*
    444       1.1       cgd  * Get value of an interval timer.  The process virtual and
    445       1.1       cgd  * profiling virtual time timers are kept in the p_stats area, since
    446       1.1       cgd  * they can be swapped out.  These are kept internally in the
    447       1.1       cgd  * way they are specified externally: in time until they expire.
    448       1.1       cgd  *
    449       1.1       cgd  * The real time interval timer is kept in the process table slot
    450       1.1       cgd  * for the process, and its value (it_value) is kept as an
    451       1.1       cgd  * absolute time rather than as a delta, so that it is easy to keep
    452       1.1       cgd  * periodic real-time signals from drifting.
    453       1.1       cgd  *
    454       1.1       cgd  * Virtual time timers are processed in the hardclock() routine of
    455       1.1       cgd  * kern_clock.c.  The real time timer is processed by a timeout
    456       1.1       cgd  * routine, called from the softclock() routine.  Since a callout
    457       1.1       cgd  * may be delayed in real time due to interrupt processing in the system,
    458       1.1       cgd  * it is possible for the real time timeout routine (realitexpire, given below),
    459       1.1       cgd  * to be delayed in real time past when it is supposed to occur.  It
    460       1.1       cgd  * does not suffice, therefore, to reload the real timer .it_value from the
    461       1.1       cgd  * real time timers .it_interval.  Rather, we compute the next time in
    462       1.1       cgd  * absolute time the timer should go off.
    463       1.1       cgd  */
    464       1.1       cgd /* ARGSUSED */
    465       1.3    andrew int
    466  1.54.2.1   nathanw sys_getitimer(l, v, retval)
    467  1.54.2.1   nathanw 	struct lwp *l;
    468      1.15   thorpej 	void *v;
    469      1.15   thorpej 	register_t *retval;
    470      1.15   thorpej {
    471      1.45  augustss 	struct sys_getitimer_args /* {
    472      1.30   mycroft 		syscallarg(int) which;
    473      1.11       cgd 		syscallarg(struct itimerval *) itv;
    474      1.15   thorpej 	} */ *uap = v;
    475  1.54.2.1   nathanw 	struct proc *p = l->l_proc;
    476      1.30   mycroft 	int which = SCARG(uap, which);
    477       1.1       cgd 	struct itimerval aitv;
    478       1.1       cgd 	int s;
    479       1.1       cgd 
    480      1.30   mycroft 	if ((u_int)which > ITIMER_PROF)
    481       1.1       cgd 		return (EINVAL);
    482       1.1       cgd 	s = splclock();
    483      1.30   mycroft 	if (which == ITIMER_REAL) {
    484       1.1       cgd 		/*
    485      1.12   mycroft 		 * Convert from absolute to relative time in .it_value
    486       1.1       cgd 		 * part of real time timer.  If time for real time timer
    487       1.1       cgd 		 * has passed return 0, else return difference between
    488       1.1       cgd 		 * current time and time for the timer to go off.
    489       1.1       cgd 		 */
    490       1.1       cgd 		aitv = p->p_realtimer;
    491      1.36   thorpej 		if (timerisset(&aitv.it_value)) {
    492       1.1       cgd 			if (timercmp(&aitv.it_value, &time, <))
    493       1.1       cgd 				timerclear(&aitv.it_value);
    494       1.1       cgd 			else
    495      1.14   mycroft 				timersub(&aitv.it_value, &time, &aitv.it_value);
    496      1.36   thorpej 		}
    497       1.1       cgd 	} else
    498      1.30   mycroft 		aitv = p->p_stats->p_timer[which];
    499       1.1       cgd 	splx(s);
    500      1.35     perry 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
    501       1.1       cgd }
    502       1.1       cgd 
    503       1.1       cgd /* ARGSUSED */
    504       1.3    andrew int
    505  1.54.2.1   nathanw sys_setitimer(l, v, retval)
    506  1.54.2.1   nathanw 	struct lwp *l;
    507      1.45  augustss 	void *v;
    508      1.15   thorpej 	register_t *retval;
    509      1.15   thorpej {
    510      1.45  augustss 	struct sys_setitimer_args /* {
    511      1.30   mycroft 		syscallarg(int) which;
    512      1.24       cgd 		syscallarg(const struct itimerval *) itv;
    513      1.11       cgd 		syscallarg(struct itimerval *) oitv;
    514      1.15   thorpej 	} */ *uap = v;
    515  1.54.2.1   nathanw 	struct proc *p = l->l_proc;
    516      1.30   mycroft 	int which = SCARG(uap, which);
    517      1.21       cgd 	struct sys_getitimer_args getargs;
    518       1.1       cgd 	struct itimerval aitv;
    519      1.45  augustss 	const struct itimerval *itvp;
    520       1.1       cgd 	int s, error;
    521       1.1       cgd 
    522      1.30   mycroft 	if ((u_int)which > ITIMER_PROF)
    523       1.1       cgd 		return (EINVAL);
    524      1.11       cgd 	itvp = SCARG(uap, itv);
    525      1.24       cgd 	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
    526       1.1       cgd 		return (error);
    527      1.21       cgd 	if (SCARG(uap, oitv) != NULL) {
    528      1.30   mycroft 		SCARG(&getargs, which) = which;
    529      1.21       cgd 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    530  1.54.2.1   nathanw 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
    531      1.21       cgd 			return (error);
    532      1.21       cgd 	}
    533       1.1       cgd 	if (itvp == 0)
    534       1.1       cgd 		return (0);
    535       1.1       cgd 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    536       1.1       cgd 		return (EINVAL);
    537       1.1       cgd 	s = splclock();
    538      1.30   mycroft 	if (which == ITIMER_REAL) {
    539      1.44   thorpej 		callout_stop(&p->p_realit_ch);
    540       1.1       cgd 		if (timerisset(&aitv.it_value)) {
    541      1.52   thorpej 			/*
    542      1.52   thorpej 			 * Don't need to check hzto() return value, here.
    543      1.52   thorpej 			 * callout_reset() does it for us.
    544      1.52   thorpej 			 */
    545      1.14   mycroft 			timeradd(&aitv.it_value, &time, &aitv.it_value);
    546      1.44   thorpej 			callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
    547      1.44   thorpej 			    realitexpire, p);
    548       1.1       cgd 		}
    549       1.1       cgd 		p->p_realtimer = aitv;
    550       1.1       cgd 	} else
    551      1.30   mycroft 		p->p_stats->p_timer[which] = aitv;
    552       1.1       cgd 	splx(s);
    553       1.1       cgd 	return (0);
    554       1.1       cgd }
    555       1.1       cgd 
    556       1.1       cgd /*
    557       1.1       cgd  * Real interval timer expired:
    558       1.1       cgd  * send process whose timer expired an alarm signal.
    559       1.1       cgd  * If time is not set up to reload, then just return.
    560       1.1       cgd  * Else compute next time timer should go off which is > current time.
    561       1.1       cgd  * This is where delay in processing this timeout causes multiple
    562       1.1       cgd  * SIGALRM calls to be compressed into one.
    563       1.1       cgd  */
    564       1.3    andrew void
    565       1.6       cgd realitexpire(arg)
    566       1.6       cgd 	void *arg;
    567       1.6       cgd {
    568      1.45  augustss 	struct proc *p;
    569       1.1       cgd 	int s;
    570       1.1       cgd 
    571       1.6       cgd 	p = (struct proc *)arg;
    572       1.1       cgd 	psignal(p, SIGALRM);
    573       1.1       cgd 	if (!timerisset(&p->p_realtimer.it_interval)) {
    574       1.1       cgd 		timerclear(&p->p_realtimer.it_value);
    575       1.1       cgd 		return;
    576       1.1       cgd 	}
    577       1.1       cgd 	for (;;) {
    578       1.1       cgd 		s = splclock();
    579      1.14   mycroft 		timeradd(&p->p_realtimer.it_value,
    580      1.14   mycroft 		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
    581       1.1       cgd 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    582      1.52   thorpej 			/*
    583      1.52   thorpej 			 * Don't need to check hzto() return value, here.
    584      1.52   thorpej 			 * callout_reset() does it for us.
    585      1.52   thorpej 			 */
    586      1.44   thorpej 			callout_reset(&p->p_realit_ch,
    587      1.44   thorpej 			    hzto(&p->p_realtimer.it_value), realitexpire, p);
    588       1.1       cgd 			splx(s);
    589       1.1       cgd 			return;
    590       1.1       cgd 		}
    591       1.1       cgd 		splx(s);
    592       1.1       cgd 	}
    593       1.1       cgd }
    594       1.1       cgd 
    595       1.1       cgd /*
    596       1.1       cgd  * Check that a proposed value to load into the .it_value or
    597       1.1       cgd  * .it_interval part of an interval timer is acceptable, and
    598       1.1       cgd  * fix it to have at least minimal value (i.e. if it is less
    599       1.1       cgd  * than the resolution of the clock, round it up.)
    600       1.1       cgd  */
    601       1.3    andrew int
    602       1.1       cgd itimerfix(tv)
    603       1.1       cgd 	struct timeval *tv;
    604       1.1       cgd {
    605       1.1       cgd 
    606       1.1       cgd 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
    607       1.1       cgd 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    608       1.1       cgd 		return (EINVAL);
    609       1.1       cgd 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    610       1.1       cgd 		tv->tv_usec = tick;
    611       1.1       cgd 	return (0);
    612       1.1       cgd }
    613       1.1       cgd 
    614       1.1       cgd /*
    615       1.1       cgd  * Decrement an interval timer by a specified number
    616       1.1       cgd  * of microseconds, which must be less than a second,
    617       1.1       cgd  * i.e. < 1000000.  If the timer expires, then reload
    618       1.1       cgd  * it.  In this case, carry over (usec - old value) to
    619       1.8       cgd  * reduce the value reloaded into the timer so that
    620       1.1       cgd  * the timer does not drift.  This routine assumes
    621       1.1       cgd  * that it is called in a context where the timers
    622       1.1       cgd  * on which it is operating cannot change in value.
    623       1.1       cgd  */
    624       1.3    andrew int
    625       1.1       cgd itimerdecr(itp, usec)
    626      1.45  augustss 	struct itimerval *itp;
    627       1.1       cgd 	int usec;
    628       1.1       cgd {
    629       1.1       cgd 
    630       1.1       cgd 	if (itp->it_value.tv_usec < usec) {
    631       1.1       cgd 		if (itp->it_value.tv_sec == 0) {
    632       1.1       cgd 			/* expired, and already in next interval */
    633       1.1       cgd 			usec -= itp->it_value.tv_usec;
    634       1.1       cgd 			goto expire;
    635       1.1       cgd 		}
    636       1.1       cgd 		itp->it_value.tv_usec += 1000000;
    637       1.1       cgd 		itp->it_value.tv_sec--;
    638       1.1       cgd 	}
    639       1.1       cgd 	itp->it_value.tv_usec -= usec;
    640       1.1       cgd 	usec = 0;
    641       1.1       cgd 	if (timerisset(&itp->it_value))
    642       1.1       cgd 		return (1);
    643       1.1       cgd 	/* expired, exactly at end of interval */
    644       1.1       cgd expire:
    645       1.1       cgd 	if (timerisset(&itp->it_interval)) {
    646       1.1       cgd 		itp->it_value = itp->it_interval;
    647       1.1       cgd 		itp->it_value.tv_usec -= usec;
    648       1.1       cgd 		if (itp->it_value.tv_usec < 0) {
    649       1.1       cgd 			itp->it_value.tv_usec += 1000000;
    650       1.1       cgd 			itp->it_value.tv_sec--;
    651       1.1       cgd 		}
    652       1.1       cgd 	} else
    653       1.1       cgd 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    654       1.1       cgd 	return (0);
    655      1.42       cgd }
    656      1.42       cgd 
    657      1.42       cgd /*
    658      1.42       cgd  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
    659      1.42       cgd  * for usage and rationale.
    660      1.42       cgd  */
    661      1.42       cgd int
    662      1.42       cgd ratecheck(lasttime, mininterval)
    663      1.42       cgd 	struct timeval *lasttime;
    664      1.42       cgd 	const struct timeval *mininterval;
    665      1.42       cgd {
    666      1.49    itojun 	struct timeval tv, delta;
    667      1.42       cgd 	int s, rv = 0;
    668      1.42       cgd 
    669      1.42       cgd 	s = splclock();
    670      1.49    itojun 	tv = mono_time;
    671      1.49    itojun 	splx(s);
    672      1.49    itojun 
    673      1.49    itojun 	timersub(&tv, lasttime, &delta);
    674      1.42       cgd 
    675      1.42       cgd 	/*
    676      1.42       cgd 	 * check for 0,0 is so that the message will be seen at least once,
    677      1.42       cgd 	 * even if interval is huge.
    678      1.42       cgd 	 */
    679      1.42       cgd 	if (timercmp(&delta, mininterval, >=) ||
    680      1.42       cgd 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
    681      1.49    itojun 		*lasttime = tv;
    682      1.42       cgd 		rv = 1;
    683      1.42       cgd 	}
    684      1.50    itojun 
    685      1.50    itojun 	return (rv);
    686      1.50    itojun }
    687      1.50    itojun 
    688      1.50    itojun /*
    689      1.50    itojun  * ppsratecheck(): packets (or events) per second limitation.
    690      1.50    itojun  */
    691      1.50    itojun int
    692      1.50    itojun ppsratecheck(lasttime, curpps, maxpps)
    693      1.50    itojun 	struct timeval *lasttime;
    694      1.50    itojun 	int *curpps;
    695      1.50    itojun 	int maxpps;	/* maximum pps allowed */
    696      1.50    itojun {
    697      1.50    itojun 	struct timeval tv, delta;
    698      1.50    itojun 	int s, rv;
    699      1.50    itojun 
    700      1.50    itojun 	s = splclock();
    701      1.50    itojun 	tv = mono_time;
    702      1.50    itojun 	splx(s);
    703      1.50    itojun 
    704      1.50    itojun 	timersub(&tv, lasttime, &delta);
    705      1.50    itojun 
    706      1.50    itojun 	/*
    707      1.50    itojun 	 * check for 0,0 is so that the message will be seen at least once.
    708      1.50    itojun 	 * if more than one second have passed since the last update of
    709      1.50    itojun 	 * lasttime, reset the counter.
    710      1.50    itojun 	 *
    711      1.50    itojun 	 * we do increment *curpps even in *curpps < maxpps case, as some may
    712      1.50    itojun 	 * try to use *curpps for stat purposes as well.
    713      1.50    itojun 	 */
    714      1.50    itojun 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
    715      1.50    itojun 	    delta.tv_sec >= 1) {
    716      1.50    itojun 		*lasttime = tv;
    717      1.50    itojun 		*curpps = 0;
    718      1.50    itojun 		rv = 1;
    719      1.53    itojun 	} else if (maxpps < 0)
    720      1.53    itojun 		rv = 1;
    721      1.53    itojun 	else if (*curpps < maxpps)
    722      1.50    itojun 		rv = 1;
    723      1.50    itojun 	else
    724      1.50    itojun 		rv = 0;
    725      1.50    itojun 
    726      1.51     jhawk #if 1 /*DIAGNOSTIC?*/
    727      1.50    itojun 	/* be careful about wrap-around */
    728      1.50    itojun 	if (*curpps + 1 > *curpps)
    729      1.50    itojun 		*curpps = *curpps + 1;
    730      1.50    itojun #else
    731      1.50    itojun 	/*
    732      1.50    itojun 	 * assume that there's not too many calls to this function.
    733      1.50    itojun 	 * not sure if the assumption holds, as it depends on *caller's*
    734      1.50    itojun 	 * behavior, not the behavior of this function.
    735      1.50    itojun 	 * IMHO it is wrong to make assumption on the caller's behavior,
    736      1.51     jhawk 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
    737      1.50    itojun 	 */
    738      1.50    itojun 	*curpps = *curpps + 1;
    739      1.50    itojun #endif
    740      1.42       cgd 
    741      1.42       cgd 	return (rv);
    742       1.1       cgd }
    743