kern_time.c revision 103964
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 103964 2002-09-25 12:00:38Z bde $
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
82102074Sphksettime(struct thread *td, struct timeval *tv)
8325583Speter{
8445433Snsayer	struct timeval delta, tv1, tv2;
8545438Snsayer	static struct timeval maxtime, laststep;
8633690Sphk	struct timespec ts;
8725583Speter	int s;
8825583Speter
8925656Speter	s = splclock();
9033818Sbde	microtime(&tv1);
9135029Sphk	delta = *tv;
9235029Sphk	timevalsub(&delta, &tv1);
9325583Speter
9425583Speter	/*
9533818Sbde	 * If the system is secure, we do not allow the time to be
9645433Snsayer	 * set to a value earlier than 1 second less than the highest
9745433Snsayer	 * time we have yet seen. The worst a miscreant can do in
9845433Snsayer	 * this circumstance is "freeze" time. He couldn't go
9945433Snsayer	 * back to the past.
10045438Snsayer	 *
10145438Snsayer	 * We similarly do not allow the clock to be stepped more
10245438Snsayer	 * than one second, nor more than once per second. This allows
10345438Snsayer	 * a miscreant to make the clock march double-time, but no worse.
10425583Speter	 */
10594343Sjhb	if (securelevel_gt(td->td_ucred, 1) != 0) {
10645433Snsayer		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
10745437Smjacob			/*
10845438Snsayer			 * Update maxtime to latest time we've seen.
10945437Smjacob			 */
11045437Smjacob			if (tv1.tv_sec > maxtime.tv_sec)
11145437Smjacob				maxtime = tv1;
11245437Smjacob			tv2 = *tv;
11345437Smjacob			timevalsub(&tv2, &maxtime);
11445437Smjacob			if (tv2.tv_sec < -1) {
11545437Smjacob				tv->tv_sec = maxtime.tv_sec - 1;
11645433Snsayer				printf("Time adjustment clamped to -1 second\n");
11745433Snsayer			}
11845437Smjacob		} else {
11945438Snsayer			if (tv1.tv_sec == laststep.tv_sec) {
12045438Snsayer				splx(s);
12145438Snsayer				return (EPERM);
12245438Snsayer			}
12345438Snsayer			if (delta.tv_sec > 1) {
12445438Snsayer				tv->tv_sec = tv1.tv_sec + 1;
12545438Snsayer				printf("Time adjustment clamped to +1 second\n");
12645438Snsayer			}
12745438Snsayer			laststep = *tv;
12845433Snsayer		}
12933818Sbde	}
13033818Sbde
13133690Sphk	ts.tv_sec = tv->tv_sec;
13233690Sphk	ts.tv_nsec = tv->tv_usec * 1000;
13394343Sjhb	mtx_lock(&Giant);
13458377Sphk	tc_setclock(&ts);
13525583Speter	(void) splsoftclock();
13625583Speter	lease_updatetime(delta.tv_sec);
13725583Speter	splx(s);
13825583Speter	resettodr();
13994343Sjhb	mtx_unlock(&Giant);
14025583Speter	return (0);
14125583Speter}
14225583Speter
14312221Sbde#ifndef _SYS_SYSPROTO_H_
14425583Speterstruct clock_gettime_args {
14525583Speter	clockid_t clock_id;
14625583Speter	struct	timespec *tp;
14725583Speter};
14825583Speter#endif
14925656Speter
15082746Sdillon/*
15182746Sdillon * MPSAFE
15282746Sdillon */
15325583Speter/* ARGSUSED */
15425583Speterint
155102074Sphkclock_gettime(struct thread *td, struct clock_gettime_args *uap)
15625583Speter{
15725583Speter	struct timespec ats;
15825583Speter
15925656Speter	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
16025583Speter		return (EINVAL);
16182746Sdillon	mtx_lock(&Giant);
16233690Sphk	nanotime(&ats);
16382746Sdillon	mtx_unlock(&Giant);
16425656Speter	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
16525583Speter}
16625583Speter
16725583Speter#ifndef _SYS_SYSPROTO_H_
16825583Speterstruct clock_settime_args {
16925583Speter	clockid_t clock_id;
17025583Speter	const struct	timespec *tp;
17125583Speter};
17225583Speter#endif
17325656Speter
17482746Sdillon/*
17582746Sdillon * MPSAFE
17682746Sdillon */
17725583Speter/* ARGSUSED */
17825583Speterint
179102074Sphkclock_settime(struct thread *td, struct clock_settime_args *uap)
18025583Speter{
18125583Speter	struct timeval atv;
18225583Speter	struct timespec ats;
18325583Speter	int error;
18425583Speter
18593593Sjhb	if ((error = suser(td)) != 0)
18694343Sjhb		return (error);
18794343Sjhb	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
18894343Sjhb		return (EINVAL);
18925583Speter	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
19094343Sjhb		return (error);
19194343Sjhb	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
19294343Sjhb		return (EINVAL);
19334901Sphk	/* XXX Don't convert nsec->usec and back */
19425583Speter	TIMESPEC_TO_TIMEVAL(&atv, &ats);
19594343Sjhb	error = settime(td, &atv);
19682746Sdillon	return (error);
19725583Speter}
19825583Speter
19925583Speter#ifndef _SYS_SYSPROTO_H_
20025583Speterstruct clock_getres_args {
20125583Speter	clockid_t clock_id;
20225583Speter	struct	timespec *tp;
20325583Speter};
20425583Speter#endif
20525656Speter
20625583Speterint
207102074Sphkclock_getres(struct thread *td, struct clock_getres_args *uap)
20825583Speter{
20925583Speter	struct timespec ts;
21025656Speter	int error;
21125583Speter
21225656Speter	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
21325583Speter		return (EINVAL);
21425656Speter	error = 0;
21525583Speter	if (SCARG(uap, tp)) {
21625583Speter		ts.tv_sec = 0;
217103964Sbde		/*
218103964Sbde		 * Round up the result of the division cheaply by adding 1.
219103964Sbde		 * Rounding up is especially important if rounding down
220103964Sbde		 * would give 0.  Perfect rounding is unimportant.
221103964Sbde		 */
222103964Sbde		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
22325656Speter		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
22425583Speter	}
22525656Speter	return (error);
22625583Speter}
22725583Speter
22826335Speterstatic int nanowait;
22925656Speter
23026335Speterstatic int
231102074Sphknanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
23225583Speter{
23335045Sphk	struct timespec ts, ts2, ts3;
23435042Sphk	struct timeval tv;
23535042Sphk	int error;
23625583Speter
23728773Sbde	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
23825656Speter		return (EINVAL);
23943301Sdillon	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
24028773Sbde		return (0);
24136119Sphk	getnanouptime(&ts);
24235029Sphk	timespecadd(&ts, rqt);
24335042Sphk	TIMESPEC_TO_TIMEVAL(&tv, rqt);
24435042Sphk	for (;;) {
24535042Sphk		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
24635042Sphk		    tvtohz(&tv));
24736119Sphk		getnanouptime(&ts2);
24835042Sphk		if (error != EWOULDBLOCK) {
24935042Sphk			if (error == ERESTART)
25035042Sphk				error = EINTR;
25135042Sphk			if (rmt != NULL) {
25235042Sphk				timespecsub(&ts, &ts2);
25335042Sphk				if (ts.tv_sec < 0)
25435042Sphk					timespecclear(&ts);
25535042Sphk				*rmt = ts;
25635042Sphk			}
25735042Sphk			return (error);
25835042Sphk		}
25935029Sphk		if (timespeccmp(&ts2, &ts, >=))
26035042Sphk			return (0);
26135045Sphk		ts3 = ts;
26235045Sphk		timespecsub(&ts3, &ts2);
26335045Sphk		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
26426335Speter	}
26526335Speter}
26625583Speter
26726335Speter#ifndef _SYS_SYSPROTO_H_
26826335Speterstruct nanosleep_args {
26926335Speter	struct	timespec *rqtp;
27026335Speter	struct	timespec *rmtp;
27126335Speter};
27226335Speter#endif
27326335Speter
27482746Sdillon/*
27582746Sdillon * MPSAFE
27682746Sdillon */
27726335Speter/* ARGSUSED */
27826335Speterint
279102074Sphknanosleep(struct thread *td, struct nanosleep_args *uap)
28026335Speter{
28126335Speter	struct timespec rmt, rqt;
28282746Sdillon	int error;
28326335Speter
28426335Speter	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
28526335Speter	if (error)
28626335Speter		return (error);
28782746Sdillon
28882746Sdillon	mtx_lock(&Giant);
28982746Sdillon	if (SCARG(uap, rmtp)) {
29052644Sphk		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
29182746Sdillon		    VM_PROT_WRITE)) {
29282746Sdillon			error = EFAULT;
29382746Sdillon			goto done2;
29482746Sdillon		}
29582746Sdillon	}
29683366Sjulian	error = nanosleep1(td, &rqt, &rmt);
29735043Speter	if (error && SCARG(uap, rmtp)) {
29882746Sdillon		int error2;
29982746Sdillon
30025656Speter		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
30126335Speter		if (error2)	/* XXX shouldn't happen, did useracc() above */
30282746Sdillon			error = error2;
30325583Speter	}
30482746Sdillondone2:
30582746Sdillon	mtx_unlock(&Giant);
30625656Speter	return (error);
30725583Speter}
30825583Speter
30926335Speter#ifndef _SYS_SYSPROTO_H_
3101541Srgrimesstruct gettimeofday_args {
3111541Srgrimes	struct	timeval *tp;
3121541Srgrimes	struct	timezone *tzp;
3131541Srgrimes};
31412221Sbde#endif
31582746Sdillon/*
31682746Sdillon * MPSAFE
31782746Sdillon */
3181541Srgrimes/* ARGSUSED */
3191549Srgrimesint
320102074Sphkgettimeofday(struct thread *td, struct gettimeofday_args *uap)
3211541Srgrimes{
3221541Srgrimes	struct timeval atv;
3231541Srgrimes	int error = 0;
3241541Srgrimes
3251541Srgrimes	if (uap->tp) {
3261541Srgrimes		microtime(&atv);
32799012Salfred		error = copyout(&atv, uap->tp, sizeof (atv));
3281541Srgrimes	}
32990836Sphk	if (error == 0 && uap->tzp != NULL) {
33090836Sphk		mtx_lock(&Giant);
33199012Salfred		error = copyout(&tz, uap->tzp, sizeof (tz));
33290836Sphk		mtx_unlock(&Giant);
33382746Sdillon	}
3341541Srgrimes	return (error);
3351541Srgrimes}
3361541Srgrimes
33712221Sbde#ifndef _SYS_SYSPROTO_H_
3381541Srgrimesstruct settimeofday_args {
3391541Srgrimes	struct	timeval *tv;
3401541Srgrimes	struct	timezone *tzp;
3411541Srgrimes};
34212221Sbde#endif
34382746Sdillon/*
34482746Sdillon * MPSAFE
34582746Sdillon */
3461541Srgrimes/* ARGSUSED */
3471549Srgrimesint
348102074Sphksettimeofday(struct thread *td, struct settimeofday_args *uap)
3491541Srgrimes{
35025656Speter	struct timeval atv;
3511541Srgrimes	struct timezone atz;
35282746Sdillon	int error = 0;
3531541Srgrimes
35493593Sjhb	if ((error = suser(td)))
35594343Sjhb		return (error);
3561541Srgrimes	/* Verify all parameters before changing time. */
35725656Speter	if (uap->tv) {
35899012Salfred		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
35994343Sjhb			return (error);
36094343Sjhb		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
36194343Sjhb			return (EINVAL);
36225656Speter	}
3631541Srgrimes	if (uap->tzp &&
36499012Salfred	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
36594343Sjhb		return (error);
36694343Sjhb
36794343Sjhb	if (uap->tv && (error = settime(td, &atv)))
36894343Sjhb		return (error);
36994343Sjhb	if (uap->tzp) {
37094343Sjhb		mtx_lock(&Giant);
37194343Sjhb		tz = atz;
37294343Sjhb		mtx_unlock(&Giant);
37382746Sdillon	}
37482746Sdillon	return (error);
3751541Srgrimes}
37682746Sdillon/*
3771541Srgrimes * Get value of an interval timer.  The process virtual and
3781541Srgrimes * profiling virtual time timers are kept in the p_stats area, since
3791541Srgrimes * they can be swapped out.  These are kept internally in the
3801541Srgrimes * way they are specified externally: in time until they expire.
3811541Srgrimes *
3821541Srgrimes * The real time interval timer is kept in the process table slot
3831541Srgrimes * for the process, and its value (it_value) is kept as an
3841541Srgrimes * absolute time rather than as a delta, so that it is easy to keep
3851541Srgrimes * periodic real-time signals from drifting.
3861541Srgrimes *
3871541Srgrimes * Virtual time timers are processed in the hardclock() routine of
3881541Srgrimes * kern_clock.c.  The real time timer is processed by a timeout
3891541Srgrimes * routine, called from the softclock() routine.  Since a callout
3901541Srgrimes * may be delayed in real time due to interrupt processing in the system,
3911541Srgrimes * it is possible for the real time timeout routine (realitexpire, given below),
3921541Srgrimes * to be delayed in real time past when it is supposed to occur.  It
3931541Srgrimes * does not suffice, therefore, to reload the real timer .it_value from the
3941541Srgrimes * real time timers .it_interval.  Rather, we compute the next time in
3951541Srgrimes * absolute time the timer should go off.
3961541Srgrimes */
39712221Sbde#ifndef _SYS_SYSPROTO_H_
3981541Srgrimesstruct getitimer_args {
3991541Srgrimes	u_int	which;
4001541Srgrimes	struct	itimerval *itv;
4011541Srgrimes};
40212221Sbde#endif
40382746Sdillon/*
40482746Sdillon * MPSAFE
40582746Sdillon */
4061541Srgrimes/* ARGSUSED */
4071549Srgrimesint
408102074Sphkgetitimer(struct thread *td, struct getitimer_args *uap)
4091541Srgrimes{
41083366Sjulian	struct proc *p = td->td_proc;
41134961Sphk	struct timeval ctv;
4121541Srgrimes	struct itimerval aitv;
4131541Srgrimes	int s;
41482746Sdillon	int error;
4151541Srgrimes
4161541Srgrimes	if (uap->which > ITIMER_PROF)
4171541Srgrimes		return (EINVAL);
41882746Sdillon
41982746Sdillon	mtx_lock(&Giant);
42082746Sdillon
42134961Sphk	s = splclock(); /* XXX still needed ? */
4221541Srgrimes	if (uap->which == ITIMER_REAL) {
4231541Srgrimes		/*
42436128Sbde		 * Convert from absolute to relative time in .it_value
4251541Srgrimes		 * part of real time timer.  If time for real time timer
4261541Srgrimes		 * has passed return 0, else return difference between
4271541Srgrimes		 * current time and time for the timer to go off.
4281541Srgrimes		 */
4291541Srgrimes		aitv = p->p_realtimer;
43035058Sphk		if (timevalisset(&aitv.it_value)) {
43136119Sphk			getmicrouptime(&ctv);
43235058Sphk			if (timevalcmp(&aitv.it_value, &ctv, <))
43335058Sphk				timevalclear(&aitv.it_value);
4341541Srgrimes			else
43534961Sphk				timevalsub(&aitv.it_value, &ctv);
43634961Sphk		}
43782746Sdillon	} else {
4381541Srgrimes		aitv = p->p_stats->p_timer[uap->which];
43982746Sdillon	}
4401541Srgrimes	splx(s);
44199012Salfred	error = copyout(&aitv, uap->itv, sizeof (struct itimerval));
44282746Sdillon	mtx_unlock(&Giant);
44382746Sdillon	return(error);
4441541Srgrimes}
4451541Srgrimes
44612221Sbde#ifndef _SYS_SYSPROTO_H_
4471541Srgrimesstruct setitimer_args {
4481541Srgrimes	u_int	which;
4491541Srgrimes	struct	itimerval *itv, *oitv;
4501541Srgrimes};
45112221Sbde#endif
45282746Sdillon/*
45382746Sdillon * MPSAFE
45482746Sdillon */
4551541Srgrimes/* ARGSUSED */
4561549Srgrimesint
457102074Sphksetitimer(struct thread *td, struct setitimer_args *uap)
4581541Srgrimes{
45983366Sjulian	struct proc *p = td->td_proc;
4601541Srgrimes	struct itimerval aitv;
46134961Sphk	struct timeval ctv;
462102074Sphk	struct itimerval *itvp;
46382746Sdillon	int s, error = 0;
4641541Srgrimes
4651541Srgrimes	if (uap->which > ITIMER_PROF)
4661541Srgrimes		return (EINVAL);
4671541Srgrimes	itvp = uap->itv;
46899012Salfred	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
4691541Srgrimes		return (error);
47082746Sdillon
47182746Sdillon	mtx_lock(&Giant);
47282746Sdillon
47312381Sbde	if ((uap->itv = uap->oitv) &&
47483366Sjulian	    (error = getitimer(td, (struct getitimer_args *)uap))) {
47582746Sdillon		goto done2;
47682746Sdillon	}
47782746Sdillon	if (itvp == 0) {
47882746Sdillon		error = 0;
47982746Sdillon		goto done2;
48082746Sdillon	}
48182746Sdillon	if (itimerfix(&aitv.it_value)) {
48282746Sdillon		error = EINVAL;
48382746Sdillon		goto done2;
48482746Sdillon	}
48582746Sdillon	if (!timevalisset(&aitv.it_value)) {
48635058Sphk		timevalclear(&aitv.it_interval);
48782746Sdillon	} else if (itimerfix(&aitv.it_interval)) {
48882746Sdillon		error = EINVAL;
48982746Sdillon		goto done2;
49082746Sdillon	}
49134961Sphk	s = splclock(); /* XXX: still needed ? */
4921541Srgrimes	if (uap->which == ITIMER_REAL) {
49335058Sphk		if (timevalisset(&p->p_realtimer.it_value))
49469286Sjake			callout_stop(&p->p_itcallout);
49535058Sphk		if (timevalisset(&aitv.it_value))
49669286Sjake			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
49769286Sjake			    realitexpire, p);
49836119Sphk		getmicrouptime(&ctv);
49935044Sphk		timevaladd(&aitv.it_value, &ctv);
5001541Srgrimes		p->p_realtimer = aitv;
50182746Sdillon	} else {
5021541Srgrimes		p->p_stats->p_timer[uap->which] = aitv;
50382746Sdillon	}
5041541Srgrimes	splx(s);
50582746Sdillondone2:
50682746Sdillon	mtx_unlock(&Giant);
50782746Sdillon	return (error);
5081541Srgrimes}
5091541Srgrimes
5101541Srgrimes/*
5111541Srgrimes * Real interval timer expired:
5121541Srgrimes * send process whose timer expired an alarm signal.
5131541Srgrimes * If time is not set up to reload, then just return.
5141541Srgrimes * Else compute next time timer should go off which is > current time.
5151541Srgrimes * This is where delay in processing this timeout causes multiple
5161541Srgrimes * SIGALRM calls to be compressed into one.
51736127Sbde * tvtohz() always adds 1 to allow for the time until the next clock
5189327Sbde * interrupt being strictly less than 1 clock tick, but we don't want
5199327Sbde * that here since we want to appear to be in sync with the clock
5209327Sbde * interrupt even when we're delayed.
5211541Srgrimes */
5221541Srgrimesvoid
523102074Sphkrealitexpire(void *arg)
5241541Srgrimes{
525102074Sphk	struct proc *p;
52635044Sphk	struct timeval ctv, ntv;
5271541Srgrimes	int s;
5281541Srgrimes
5291541Srgrimes	p = (struct proc *)arg;
53073916Sjhb	PROC_LOCK(p);
5311541Srgrimes	psignal(p, SIGALRM);
53235058Sphk	if (!timevalisset(&p->p_realtimer.it_interval)) {
53335058Sphk		timevalclear(&p->p_realtimer.it_value);
53473916Sjhb		PROC_UNLOCK(p);
5351541Srgrimes		return;
5361541Srgrimes	}
5371541Srgrimes	for (;;) {
53834961Sphk		s = splclock(); /* XXX: still neeeded ? */
5391541Srgrimes		timevaladd(&p->p_realtimer.it_value,
5401541Srgrimes		    &p->p_realtimer.it_interval);
54136119Sphk		getmicrouptime(&ctv);
54235058Sphk		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
54335044Sphk			ntv = p->p_realtimer.it_value;
54435044Sphk			timevalsub(&ntv, &ctv);
54569286Sjake			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
54669286Sjake			    realitexpire, p);
5471541Srgrimes			splx(s);
54873916Sjhb			PROC_UNLOCK(p);
5491541Srgrimes			return;
5501541Srgrimes		}
5511541Srgrimes		splx(s);
5521541Srgrimes	}
55373916Sjhb	/*NOTREACHED*/
5541541Srgrimes}
5551541Srgrimes
5561541Srgrimes/*
5571541Srgrimes * Check that a proposed value to load into the .it_value or
5581541Srgrimes * .it_interval part of an interval timer is acceptable, and
5591541Srgrimes * fix it to have at least minimal value (i.e. if it is less
5601541Srgrimes * than the resolution of the clock, round it up.)
5611541Srgrimes */
5621549Srgrimesint
563102074Sphkitimerfix(struct timeval *tv)
5641541Srgrimes{
5651541Srgrimes
5661541Srgrimes	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
5671541Srgrimes	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
5681541Srgrimes		return (EINVAL);
5691541Srgrimes	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
5701541Srgrimes		tv->tv_usec = tick;
5711541Srgrimes	return (0);
5721541Srgrimes}
5731541Srgrimes
5741541Srgrimes/*
5751541Srgrimes * Decrement an interval timer by a specified number
5761541Srgrimes * of microseconds, which must be less than a second,
5771541Srgrimes * i.e. < 1000000.  If the timer expires, then reload
5781541Srgrimes * it.  In this case, carry over (usec - old value) to
5791541Srgrimes * reduce the value reloaded into the timer so that
5801541Srgrimes * the timer does not drift.  This routine assumes
5811541Srgrimes * that it is called in a context where the timers
5821541Srgrimes * on which it is operating cannot change in value.
5831541Srgrimes */
5841549Srgrimesint
585102074Sphkitimerdecr(struct itimerval *itp, int usec)
5861541Srgrimes{
5871541Srgrimes
5881541Srgrimes	if (itp->it_value.tv_usec < usec) {
5891541Srgrimes		if (itp->it_value.tv_sec == 0) {
5901541Srgrimes			/* expired, and already in next interval */
5911541Srgrimes			usec -= itp->it_value.tv_usec;
5921541Srgrimes			goto expire;
5931541Srgrimes		}
5941541Srgrimes		itp->it_value.tv_usec += 1000000;
5951541Srgrimes		itp->it_value.tv_sec--;
5961541Srgrimes	}
5971541Srgrimes	itp->it_value.tv_usec -= usec;
5981541Srgrimes	usec = 0;
59935058Sphk	if (timevalisset(&itp->it_value))
6001541Srgrimes		return (1);
6011541Srgrimes	/* expired, exactly at end of interval */
6021541Srgrimesexpire:
60335058Sphk	if (timevalisset(&itp->it_interval)) {
6041541Srgrimes		itp->it_value = itp->it_interval;
6051541Srgrimes		itp->it_value.tv_usec -= usec;
6061541Srgrimes		if (itp->it_value.tv_usec < 0) {
6071541Srgrimes			itp->it_value.tv_usec += 1000000;
6081541Srgrimes			itp->it_value.tv_sec--;
6091541Srgrimes		}
6101541Srgrimes	} else
6111541Srgrimes		itp->it_value.tv_usec = 0;		/* sec is already 0 */
6121541Srgrimes	return (0);
6131541Srgrimes}
6141541Srgrimes
6151541Srgrimes/*
6161541Srgrimes * Add and subtract routines for timevals.
6171541Srgrimes * N.B.: subtract routine doesn't deal with
6181541Srgrimes * results which are before the beginning,
6191541Srgrimes * it just gets very confused in this case.
6201541Srgrimes * Caveat emptor.
6211541Srgrimes */
6221549Srgrimesvoid
623102074Sphktimevaladd(struct timeval *t1, struct timeval *t2)
6241541Srgrimes{
6251541Srgrimes
6261541Srgrimes	t1->tv_sec += t2->tv_sec;
6271541Srgrimes	t1->tv_usec += t2->tv_usec;
6281541Srgrimes	timevalfix(t1);
6291541Srgrimes}
6301541Srgrimes
6311549Srgrimesvoid
632102074Sphktimevalsub(struct timeval *t1, struct timeval *t2)
6331541Srgrimes{
6341541Srgrimes
6351541Srgrimes	t1->tv_sec -= t2->tv_sec;
6361541Srgrimes	t1->tv_usec -= t2->tv_usec;
6371541Srgrimes	timevalfix(t1);
6381541Srgrimes}
6391541Srgrimes
64012819Sphkstatic void
641102074Sphktimevalfix(struct timeval *t1)
6421541Srgrimes{
6431541Srgrimes
6441541Srgrimes	if (t1->tv_usec < 0) {
6451541Srgrimes		t1->tv_sec--;
6461541Srgrimes		t1->tv_usec += 1000000;
6471541Srgrimes	}
6481541Srgrimes	if (t1->tv_usec >= 1000000) {
6491541Srgrimes		t1->tv_sec++;
6501541Srgrimes		t1->tv_usec -= 1000000;
6511541Srgrimes	}
6521541Srgrimes}
653