kern_time.c revision 99012
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
3450477Speter * $FreeBSD: head/sys/kern/kern_time.c 99012 2002-06-29 02:00:02Z alfred $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
3848274Speter#include <sys/systm.h>
3976166Smarkm#include <sys/lock.h>
4076166Smarkm#include <sys/mutex.h>
4112221Sbde#include <sys/sysproto.h>
421541Srgrimes#include <sys/resourcevar.h>
433308Sphk#include <sys/signalvar.h>
441541Srgrimes#include <sys/kernel.h>
451541Srgrimes#include <sys/systm.h>
4625583Speter#include <sys/sysent.h>
471541Srgrimes#include <sys/proc.h>
4825656Speter#include <sys/time.h>
4958377Sphk#include <sys/timetc.h>
501541Srgrimes#include <sys/vnode.h>
5176166Smarkm
5226335Speter#include <vm/vm.h>
5326335Speter#include <vm/vm_extern.h>
541541Srgrimes
559369Sdgstruct timezone tz;
569369Sdg
578876Srgrimes/*
581541Srgrimes * Time of day and interval timer support.
591541Srgrimes *
601541Srgrimes * These routines provide the kernel entry points to get and set
611541Srgrimes * the time-of-day and per-process interval timers.  Subroutines
621541Srgrimes * here provide support for adding and subtracting timeval structures
631541Srgrimes * and decrementing interval timers, optionally reloading the interval
641541Srgrimes * timers when they expire.
651541Srgrimes */
661541Srgrimes
6792723Salfredstatic int	nanosleep1(struct thread *td, struct timespec *rqt,
6892723Salfred		    struct timespec *rmt);
6994343Sjhbstatic int	settime(struct thread *, struct timeval *);
7092723Salfredstatic void	timevalfix(struct timeval *);
7192723Salfredstatic void	no_lease_updatetime(int);
7213016Sbde
7330739Sphkstatic void
7430739Sphkno_lease_updatetime(deltat)
7530739Sphk	int deltat;
7630739Sphk{
7730739Sphk}
7830739Sphk
7992723Salfredvoid (*lease_updatetime)(int)  = no_lease_updatetime;
8030739Sphk
8125583Speterstatic int
8294343Sjhbsettime(td, tv)
8394343Sjhb	struct thread *td;
8425583Speter	struct timeval *tv;
8525583Speter{
8645433Snsayer	struct timeval delta, tv1, tv2;
8745438Snsayer	static struct timeval maxtime, laststep;
8833690Sphk	struct timespec ts;
8925583Speter	int s;
9025583Speter
9125656Speter	s = splclock();
9233818Sbde	microtime(&tv1);
9335029Sphk	delta = *tv;
9435029Sphk	timevalsub(&delta, &tv1);
9525583Speter
9625583Speter	/*
9733818Sbde	 * If the system is secure, we do not allow the time to be
9845433Snsayer	 * set to a value earlier than 1 second less than the highest
9945433Snsayer	 * time we have yet seen. The worst a miscreant can do in
10045433Snsayer	 * this circumstance is "freeze" time. He couldn't go
10145433Snsayer	 * back to the past.
10245438Snsayer	 *
10345438Snsayer	 * We similarly do not allow the clock to be stepped more
10445438Snsayer	 * than one second, nor more than once per second. This allows
10545438Snsayer	 * a miscreant to make the clock march double-time, but no worse.
10625583Speter	 */
10794343Sjhb	if (securelevel_gt(td->td_ucred, 1) != 0) {
10845433Snsayer		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
10945437Smjacob			/*
11045438Snsayer			 * Update maxtime to latest time we've seen.
11145437Smjacob			 */
11245437Smjacob			if (tv1.tv_sec > maxtime.tv_sec)
11345437Smjacob				maxtime = tv1;
11445437Smjacob			tv2 = *tv;
11545437Smjacob			timevalsub(&tv2, &maxtime);
11645437Smjacob			if (tv2.tv_sec < -1) {
11745437Smjacob				tv->tv_sec = maxtime.tv_sec - 1;
11845433Snsayer				printf("Time adjustment clamped to -1 second\n");
11945433Snsayer			}
12045437Smjacob		} else {
12145438Snsayer			if (tv1.tv_sec == laststep.tv_sec) {
12245438Snsayer				splx(s);
12345438Snsayer				return (EPERM);
12445438Snsayer			}
12545438Snsayer			if (delta.tv_sec > 1) {
12645438Snsayer				tv->tv_sec = tv1.tv_sec + 1;
12745438Snsayer				printf("Time adjustment clamped to +1 second\n");
12845438Snsayer			}
12945438Snsayer			laststep = *tv;
13045433Snsayer		}
13133818Sbde	}
13233818Sbde
13333690Sphk	ts.tv_sec = tv->tv_sec;
13433690Sphk	ts.tv_nsec = tv->tv_usec * 1000;
13594343Sjhb	mtx_lock(&Giant);
13658377Sphk	tc_setclock(&ts);
13725583Speter	(void) splsoftclock();
13825583Speter	lease_updatetime(delta.tv_sec);
13925583Speter	splx(s);
14025583Speter	resettodr();
14194343Sjhb	mtx_unlock(&Giant);
14225583Speter	return (0);
14325583Speter}
14425583Speter
14512221Sbde#ifndef _SYS_SYSPROTO_H_
14625583Speterstruct clock_gettime_args {
14725583Speter	clockid_t clock_id;
14825583Speter	struct	timespec *tp;
14925583Speter};
15025583Speter#endif
15125656Speter
15282746Sdillon/*
15382746Sdillon * MPSAFE
15482746Sdillon */
15525583Speter/* ARGSUSED */
15625583Speterint
15783366Sjulianclock_gettime(td, uap)
15883366Sjulian	struct thread *td;
15925583Speter	struct clock_gettime_args *uap;
16025583Speter{
16125583Speter	struct timespec ats;
16225583Speter
16325656Speter	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
16425583Speter		return (EINVAL);
16582746Sdillon	mtx_lock(&Giant);
16633690Sphk	nanotime(&ats);
16782746Sdillon	mtx_unlock(&Giant);
16825656Speter	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
16925583Speter}
17025583Speter
17125583Speter#ifndef _SYS_SYSPROTO_H_
17225583Speterstruct clock_settime_args {
17325583Speter	clockid_t clock_id;
17425583Speter	const struct	timespec *tp;
17525583Speter};
17625583Speter#endif
17725656Speter
17882746Sdillon/*
17982746Sdillon * MPSAFE
18082746Sdillon */
18125583Speter/* ARGSUSED */
18225583Speterint
18383366Sjulianclock_settime(td, uap)
18483366Sjulian	struct thread *td;
18525583Speter	struct clock_settime_args *uap;
18625583Speter{
18725583Speter	struct timeval atv;
18825583Speter	struct timespec ats;
18925583Speter	int error;
19025583Speter
19193593Sjhb	if ((error = suser(td)) != 0)
19294343Sjhb		return (error);
19394343Sjhb	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
19494343Sjhb		return (EINVAL);
19525583Speter	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
19694343Sjhb		return (error);
19794343Sjhb	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
19894343Sjhb		return (EINVAL);
19934901Sphk	/* XXX Don't convert nsec->usec and back */
20025583Speter	TIMESPEC_TO_TIMEVAL(&atv, &ats);
20194343Sjhb	error = settime(td, &atv);
20282746Sdillon	return (error);
20325583Speter}
20425583Speter
20525583Speter#ifndef _SYS_SYSPROTO_H_
20625583Speterstruct clock_getres_args {
20725583Speter	clockid_t clock_id;
20825583Speter	struct	timespec *tp;
20925583Speter};
21025583Speter#endif
21125656Speter
21225583Speterint
21383366Sjulianclock_getres(td, uap)
21483366Sjulian	struct thread *td;
21525583Speter	struct clock_getres_args *uap;
21625583Speter{
21725583Speter	struct timespec ts;
21825656Speter	int error;
21925583Speter
22025656Speter	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
22125583Speter		return (EINVAL);
22225656Speter	error = 0;
22325583Speter	if (SCARG(uap, tp)) {
22425583Speter		ts.tv_sec = 0;
22595529Sphk		ts.tv_nsec = 1000000000 / tc_getfrequency();
22625656Speter		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
22725583Speter	}
22825656Speter	return (error);
22925583Speter}
23025583Speter
23126335Speterstatic int nanowait;
23225656Speter
23326335Speterstatic int
23483366Sjuliannanosleep1(td, rqt, rmt)
23583366Sjulian	struct thread *td;
23626335Speter	struct timespec *rqt, *rmt;
23725583Speter{
23835045Sphk	struct timespec ts, ts2, ts3;
23935042Sphk	struct timeval tv;
24035042Sphk	int error;
24125583Speter
24228773Sbde	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
24325656Speter		return (EINVAL);
24443301Sdillon	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
24528773Sbde		return (0);
24636119Sphk	getnanouptime(&ts);
24735029Sphk	timespecadd(&ts, rqt);
24835042Sphk	TIMESPEC_TO_TIMEVAL(&tv, rqt);
24935042Sphk	for (;;) {
25035042Sphk		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
25135042Sphk		    tvtohz(&tv));
25236119Sphk		getnanouptime(&ts2);
25335042Sphk		if (error != EWOULDBLOCK) {
25435042Sphk			if (error == ERESTART)
25535042Sphk				error = EINTR;
25635042Sphk			if (rmt != NULL) {
25735042Sphk				timespecsub(&ts, &ts2);
25835042Sphk				if (ts.tv_sec < 0)
25935042Sphk					timespecclear(&ts);
26035042Sphk				*rmt = ts;
26135042Sphk			}
26235042Sphk			return (error);
26335042Sphk		}
26435029Sphk		if (timespeccmp(&ts2, &ts, >=))
26535042Sphk			return (0);
26635045Sphk		ts3 = ts;
26735045Sphk		timespecsub(&ts3, &ts2);
26835045Sphk		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
26926335Speter	}
27026335Speter}
27125583Speter
27226335Speter#ifndef _SYS_SYSPROTO_H_
27326335Speterstruct nanosleep_args {
27426335Speter	struct	timespec *rqtp;
27526335Speter	struct	timespec *rmtp;
27626335Speter};
27726335Speter#endif
27826335Speter
27982746Sdillon/*
28082746Sdillon * MPSAFE
28182746Sdillon */
28226335Speter/* ARGSUSED */
28326335Speterint
28483366Sjuliannanosleep(td, uap)
28583366Sjulian	struct thread *td;
28626335Speter	struct nanosleep_args *uap;
28726335Speter{
28826335Speter	struct timespec rmt, rqt;
28982746Sdillon	int error;
29026335Speter
29126335Speter	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
29226335Speter	if (error)
29326335Speter		return (error);
29482746Sdillon
29582746Sdillon	mtx_lock(&Giant);
29682746Sdillon	if (SCARG(uap, rmtp)) {
29752644Sphk		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
29882746Sdillon		    VM_PROT_WRITE)) {
29982746Sdillon			error = EFAULT;
30082746Sdillon			goto done2;
30182746Sdillon		}
30282746Sdillon	}
30383366Sjulian	error = nanosleep1(td, &rqt, &rmt);
30435043Speter	if (error && SCARG(uap, rmtp)) {
30582746Sdillon		int error2;
30682746Sdillon
30725656Speter		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
30826335Speter		if (error2)	/* XXX shouldn't happen, did useracc() above */
30982746Sdillon			error = error2;
31025583Speter	}
31182746Sdillondone2:
31282746Sdillon	mtx_unlock(&Giant);
31325656Speter	return (error);
31425583Speter}
31525583Speter
31626335Speter#ifndef _SYS_SYSPROTO_H_
3171541Srgrimesstruct gettimeofday_args {
3181541Srgrimes	struct	timeval *tp;
3191541Srgrimes	struct	timezone *tzp;
3201541Srgrimes};
32112221Sbde#endif
32282746Sdillon/*
32382746Sdillon * MPSAFE
32482746Sdillon */
3251541Srgrimes/* ARGSUSED */
3261549Srgrimesint
32783366Sjuliangettimeofday(td, uap)
32883366Sjulian	struct thread *td;
3291541Srgrimes	register struct gettimeofday_args *uap;
3301541Srgrimes{
3311541Srgrimes	struct timeval atv;
3321541Srgrimes	int error = 0;
3331541Srgrimes
3341541Srgrimes	if (uap->tp) {
3351541Srgrimes		microtime(&atv);
33699012Salfred		error = copyout(&atv, uap->tp, sizeof (atv));
3371541Srgrimes	}
33890836Sphk	if (error == 0 && uap->tzp != NULL) {
33990836Sphk		mtx_lock(&Giant);
34099012Salfred		error = copyout(&tz, uap->tzp, sizeof (tz));
34190836Sphk		mtx_unlock(&Giant);
34282746Sdillon	}
3431541Srgrimes	return (error);
3441541Srgrimes}
3451541Srgrimes
34612221Sbde#ifndef _SYS_SYSPROTO_H_
3471541Srgrimesstruct settimeofday_args {
3481541Srgrimes	struct	timeval *tv;
3491541Srgrimes	struct	timezone *tzp;
3501541Srgrimes};
35112221Sbde#endif
35282746Sdillon/*
35382746Sdillon * MPSAFE
35482746Sdillon */
3551541Srgrimes/* ARGSUSED */
3561549Srgrimesint
35783366Sjuliansettimeofday(td, uap)
35883366Sjulian	struct thread *td;
3591541Srgrimes	struct settimeofday_args *uap;
3601541Srgrimes{
36125656Speter	struct timeval atv;
3621541Srgrimes	struct timezone atz;
36382746Sdillon	int error = 0;
3641541Srgrimes
36593593Sjhb	if ((error = suser(td)))
36694343Sjhb		return (error);
3671541Srgrimes	/* Verify all parameters before changing time. */
36825656Speter	if (uap->tv) {
36999012Salfred		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
37094343Sjhb			return (error);
37194343Sjhb		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
37294343Sjhb			return (EINVAL);
37325656Speter	}
3741541Srgrimes	if (uap->tzp &&
37599012Salfred	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
37694343Sjhb		return (error);
37794343Sjhb
37894343Sjhb	if (uap->tv && (error = settime(td, &atv)))
37994343Sjhb		return (error);
38094343Sjhb	if (uap->tzp) {
38194343Sjhb		mtx_lock(&Giant);
38294343Sjhb		tz = atz;
38394343Sjhb		mtx_unlock(&Giant);
38482746Sdillon	}
38582746Sdillon	return (error);
3861541Srgrimes}
38782746Sdillon/*
3881541Srgrimes * Get value of an interval timer.  The process virtual and
3891541Srgrimes * profiling virtual time timers are kept in the p_stats area, since
3901541Srgrimes * they can be swapped out.  These are kept internally in the
3911541Srgrimes * way they are specified externally: in time until they expire.
3921541Srgrimes *
3931541Srgrimes * The real time interval timer is kept in the process table slot
3941541Srgrimes * for the process, and its value (it_value) is kept as an
3951541Srgrimes * absolute time rather than as a delta, so that it is easy to keep
3961541Srgrimes * periodic real-time signals from drifting.
3971541Srgrimes *
3981541Srgrimes * Virtual time timers are processed in the hardclock() routine of
3991541Srgrimes * kern_clock.c.  The real time timer is processed by a timeout
4001541Srgrimes * routine, called from the softclock() routine.  Since a callout
4011541Srgrimes * may be delayed in real time due to interrupt processing in the system,
4021541Srgrimes * it is possible for the real time timeout routine (realitexpire, given below),
4031541Srgrimes * to be delayed in real time past when it is supposed to occur.  It
4041541Srgrimes * does not suffice, therefore, to reload the real timer .it_value from the
4051541Srgrimes * real time timers .it_interval.  Rather, we compute the next time in
4061541Srgrimes * absolute time the timer should go off.
4071541Srgrimes */
40812221Sbde#ifndef _SYS_SYSPROTO_H_
4091541Srgrimesstruct getitimer_args {
4101541Srgrimes	u_int	which;
4111541Srgrimes	struct	itimerval *itv;
4121541Srgrimes};
41312221Sbde#endif
41482746Sdillon/*
41582746Sdillon * MPSAFE
41682746Sdillon */
4171541Srgrimes/* ARGSUSED */
4181549Srgrimesint
41983366Sjuliangetitimer(td, uap)
42083366Sjulian	struct thread *td;
4211541Srgrimes	register struct getitimer_args *uap;
4221541Srgrimes{
42383366Sjulian	struct proc *p = td->td_proc;
42434961Sphk	struct timeval ctv;
4251541Srgrimes	struct itimerval aitv;
4261541Srgrimes	int s;
42782746Sdillon	int error;
4281541Srgrimes
4291541Srgrimes	if (uap->which > ITIMER_PROF)
4301541Srgrimes		return (EINVAL);
43182746Sdillon
43282746Sdillon	mtx_lock(&Giant);
43382746Sdillon
43434961Sphk	s = splclock(); /* XXX still needed ? */
4351541Srgrimes	if (uap->which == ITIMER_REAL) {
4361541Srgrimes		/*
43736128Sbde		 * Convert from absolute to relative time in .it_value
4381541Srgrimes		 * part of real time timer.  If time for real time timer
4391541Srgrimes		 * has passed return 0, else return difference between
4401541Srgrimes		 * current time and time for the timer to go off.
4411541Srgrimes		 */
4421541Srgrimes		aitv = p->p_realtimer;
44335058Sphk		if (timevalisset(&aitv.it_value)) {
44436119Sphk			getmicrouptime(&ctv);
44535058Sphk			if (timevalcmp(&aitv.it_value, &ctv, <))
44635058Sphk				timevalclear(&aitv.it_value);
4471541Srgrimes			else
44834961Sphk				timevalsub(&aitv.it_value, &ctv);
44934961Sphk		}
45082746Sdillon	} else {
4511541Srgrimes		aitv = p->p_stats->p_timer[uap->which];
45282746Sdillon	}
4531541Srgrimes	splx(s);
45499012Salfred	error = copyout(&aitv, uap->itv, sizeof (struct itimerval));
45582746Sdillon	mtx_unlock(&Giant);
45682746Sdillon	return(error);
4571541Srgrimes}
4581541Srgrimes
45912221Sbde#ifndef _SYS_SYSPROTO_H_
4601541Srgrimesstruct setitimer_args {
4611541Srgrimes	u_int	which;
4621541Srgrimes	struct	itimerval *itv, *oitv;
4631541Srgrimes};
46412221Sbde#endif
46582746Sdillon/*
46682746Sdillon * MPSAFE
46782746Sdillon */
4681541Srgrimes/* ARGSUSED */
4691549Srgrimesint
47083366Sjuliansetitimer(td, uap)
47183366Sjulian	struct thread *td;
4721541Srgrimes	register struct setitimer_args *uap;
4731541Srgrimes{
47483366Sjulian	struct proc *p = td->td_proc;
4751541Srgrimes	struct itimerval aitv;
47634961Sphk	struct timeval ctv;
4771541Srgrimes	register struct itimerval *itvp;
47882746Sdillon	int s, error = 0;
4791541Srgrimes
4801541Srgrimes	if (uap->which > ITIMER_PROF)
4811541Srgrimes		return (EINVAL);
4821541Srgrimes	itvp = uap->itv;
48399012Salfred	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
4841541Srgrimes		return (error);
48582746Sdillon
48682746Sdillon	mtx_lock(&Giant);
48782746Sdillon
48812381Sbde	if ((uap->itv = uap->oitv) &&
48983366Sjulian	    (error = getitimer(td, (struct getitimer_args *)uap))) {
49082746Sdillon		goto done2;
49182746Sdillon	}
49282746Sdillon	if (itvp == 0) {
49382746Sdillon		error = 0;
49482746Sdillon		goto done2;
49582746Sdillon	}
49682746Sdillon	if (itimerfix(&aitv.it_value)) {
49782746Sdillon		error = EINVAL;
49882746Sdillon		goto done2;
49982746Sdillon	}
50082746Sdillon	if (!timevalisset(&aitv.it_value)) {
50135058Sphk		timevalclear(&aitv.it_interval);
50282746Sdillon	} else if (itimerfix(&aitv.it_interval)) {
50382746Sdillon		error = EINVAL;
50482746Sdillon		goto done2;
50582746Sdillon	}
50634961Sphk	s = splclock(); /* XXX: still needed ? */
5071541Srgrimes	if (uap->which == ITIMER_REAL) {
50835058Sphk		if (timevalisset(&p->p_realtimer.it_value))
50969286Sjake			callout_stop(&p->p_itcallout);
51035058Sphk		if (timevalisset(&aitv.it_value))
51169286Sjake			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
51269286Sjake			    realitexpire, p);
51336119Sphk		getmicrouptime(&ctv);
51435044Sphk		timevaladd(&aitv.it_value, &ctv);
5151541Srgrimes		p->p_realtimer = aitv;
51682746Sdillon	} else {
5171541Srgrimes		p->p_stats->p_timer[uap->which] = aitv;
51882746Sdillon	}
5191541Srgrimes	splx(s);
52082746Sdillondone2:
52182746Sdillon	mtx_unlock(&Giant);
52282746Sdillon	return (error);
5231541Srgrimes}
5241541Srgrimes
5251541Srgrimes/*
5261541Srgrimes * Real interval timer expired:
5271541Srgrimes * send process whose timer expired an alarm signal.
5281541Srgrimes * If time is not set up to reload, then just return.
5291541Srgrimes * Else compute next time timer should go off which is > current time.
5301541Srgrimes * This is where delay in processing this timeout causes multiple
5311541Srgrimes * SIGALRM calls to be compressed into one.
53236127Sbde * tvtohz() always adds 1 to allow for the time until the next clock
5339327Sbde * interrupt being strictly less than 1 clock tick, but we don't want
5349327Sbde * that here since we want to appear to be in sync with the clock
5359327Sbde * interrupt even when we're delayed.
5361541Srgrimes */
5371541Srgrimesvoid
5381541Srgrimesrealitexpire(arg)
5391541Srgrimes	void *arg;
5401541Srgrimes{
5411541Srgrimes	register struct proc *p;
54235044Sphk	struct timeval ctv, ntv;
5431541Srgrimes	int s;
5441541Srgrimes
5451541Srgrimes	p = (struct proc *)arg;
54673916Sjhb	PROC_LOCK(p);
5471541Srgrimes	psignal(p, SIGALRM);
54835058Sphk	if (!timevalisset(&p->p_realtimer.it_interval)) {
54935058Sphk		timevalclear(&p->p_realtimer.it_value);
55073916Sjhb		PROC_UNLOCK(p);
5511541Srgrimes		return;
5521541Srgrimes	}
5531541Srgrimes	for (;;) {
55434961Sphk		s = splclock(); /* XXX: still neeeded ? */
5551541Srgrimes		timevaladd(&p->p_realtimer.it_value,
5561541Srgrimes		    &p->p_realtimer.it_interval);
55736119Sphk		getmicrouptime(&ctv);
55835058Sphk		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
55935044Sphk			ntv = p->p_realtimer.it_value;
56035044Sphk			timevalsub(&ntv, &ctv);
56169286Sjake			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
56269286Sjake			    realitexpire, p);
5631541Srgrimes			splx(s);
56473916Sjhb			PROC_UNLOCK(p);
5651541Srgrimes			return;
5661541Srgrimes		}
5671541Srgrimes		splx(s);
5681541Srgrimes	}
56973916Sjhb	/*NOTREACHED*/
5701541Srgrimes}
5711541Srgrimes
5721541Srgrimes/*
5731541Srgrimes * Check that a proposed value to load into the .it_value or
5741541Srgrimes * .it_interval part of an interval timer is acceptable, and
5751541Srgrimes * fix it to have at least minimal value (i.e. if it is less
5761541Srgrimes * than the resolution of the clock, round it up.)
5771541Srgrimes */
5781549Srgrimesint
5791541Srgrimesitimerfix(tv)
5801541Srgrimes	struct timeval *tv;
5811541Srgrimes{
5821541Srgrimes
5831541Srgrimes	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
5841541Srgrimes	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
5851541Srgrimes		return (EINVAL);
5861541Srgrimes	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
5871541Srgrimes		tv->tv_usec = tick;
5881541Srgrimes	return (0);
5891541Srgrimes}
5901541Srgrimes
5911541Srgrimes/*
5921541Srgrimes * Decrement an interval timer by a specified number
5931541Srgrimes * of microseconds, which must be less than a second,
5941541Srgrimes * i.e. < 1000000.  If the timer expires, then reload
5951541Srgrimes * it.  In this case, carry over (usec - old value) to
5961541Srgrimes * reduce the value reloaded into the timer so that
5971541Srgrimes * the timer does not drift.  This routine assumes
5981541Srgrimes * that it is called in a context where the timers
5991541Srgrimes * on which it is operating cannot change in value.
6001541Srgrimes */
6011549Srgrimesint
6021541Srgrimesitimerdecr(itp, usec)
6031541Srgrimes	register struct itimerval *itp;
6041541Srgrimes	int usec;
6051541Srgrimes{
6061541Srgrimes
6071541Srgrimes	if (itp->it_value.tv_usec < usec) {
6081541Srgrimes		if (itp->it_value.tv_sec == 0) {
6091541Srgrimes			/* expired, and already in next interval */
6101541Srgrimes			usec -= itp->it_value.tv_usec;
6111541Srgrimes			goto expire;
6121541Srgrimes		}
6131541Srgrimes		itp->it_value.tv_usec += 1000000;
6141541Srgrimes		itp->it_value.tv_sec--;
6151541Srgrimes	}
6161541Srgrimes	itp->it_value.tv_usec -= usec;
6171541Srgrimes	usec = 0;
61835058Sphk	if (timevalisset(&itp->it_value))
6191541Srgrimes		return (1);
6201541Srgrimes	/* expired, exactly at end of interval */
6211541Srgrimesexpire:
62235058Sphk	if (timevalisset(&itp->it_interval)) {
6231541Srgrimes		itp->it_value = itp->it_interval;
6241541Srgrimes		itp->it_value.tv_usec -= usec;
6251541Srgrimes		if (itp->it_value.tv_usec < 0) {
6261541Srgrimes			itp->it_value.tv_usec += 1000000;
6271541Srgrimes			itp->it_value.tv_sec--;
6281541Srgrimes		}
6291541Srgrimes	} else
6301541Srgrimes		itp->it_value.tv_usec = 0;		/* sec is already 0 */
6311541Srgrimes	return (0);
6321541Srgrimes}
6331541Srgrimes
6341541Srgrimes/*
6351541Srgrimes * Add and subtract routines for timevals.
6361541Srgrimes * N.B.: subtract routine doesn't deal with
6371541Srgrimes * results which are before the beginning,
6381541Srgrimes * it just gets very confused in this case.
6391541Srgrimes * Caveat emptor.
6401541Srgrimes */
6411549Srgrimesvoid
6421541Srgrimestimevaladd(t1, t2)
6431541Srgrimes	struct timeval *t1, *t2;
6441541Srgrimes{
6451541Srgrimes
6461541Srgrimes	t1->tv_sec += t2->tv_sec;
6471541Srgrimes	t1->tv_usec += t2->tv_usec;
6481541Srgrimes	timevalfix(t1);
6491541Srgrimes}
6501541Srgrimes
6511549Srgrimesvoid
6521541Srgrimestimevalsub(t1, t2)
6531541Srgrimes	struct timeval *t1, *t2;
6541541Srgrimes{
6551541Srgrimes
6561541Srgrimes	t1->tv_sec -= t2->tv_sec;
6571541Srgrimes	t1->tv_usec -= t2->tv_usec;
6581541Srgrimes	timevalfix(t1);
6591541Srgrimes}
6601541Srgrimes
66112819Sphkstatic void
6621541Srgrimestimevalfix(t1)
6631541Srgrimes	struct timeval *t1;
6641541Srgrimes{
6651541Srgrimes
6661541Srgrimes	if (t1->tv_usec < 0) {
6671541Srgrimes		t1->tv_sec--;
6681541Srgrimes		t1->tv_usec += 1000000;
6691541Srgrimes	}
6701541Srgrimes	if (t1->tv_usec >= 1000000) {
6711541Srgrimes		t1->tv_sec++;
6721541Srgrimes		t1->tv_usec -= 1000000;
6731541Srgrimes	}
6741541Srgrimes}
675