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