kern_time.c revision 1.21
1/*	$NetBSD: kern_time.c,v 1.21 1996/10/24 04:35:33 cgd Exp $	*/
2
3/*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
36 */
37
38#include <sys/param.h>
39#include <sys/resourcevar.h>
40#include <sys/kernel.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
43#include <sys/vnode.h>
44#include <sys/signalvar.h>
45
46#include <sys/mount.h>
47#include <sys/syscallargs.h>
48
49#if defined(NFSCLIENT) || defined(NFSSERVER)
50#include <nfs/rpcv2.h>
51#include <nfs/nfsproto.h>
52#include <nfs/nfs_var.h>
53#endif
54
55#include <machine/cpu.h>
56
57/*
58 * Time of day and interval timer support.
59 *
60 * These routines provide the kernel entry points to get and set
61 * the time-of-day and per-process interval timers.  Subroutines
62 * here provide support for adding and subtracting timeval structures
63 * and decrementing interval timers, optionally reloading the interval
64 * timers when they expire.
65 */
66
67/* ARGSUSED */
68int
69sys_gettimeofday(p, v, retval)
70	struct proc *p;
71	void *v;
72	register_t *retval;
73{
74	register struct sys_gettimeofday_args /* {
75		syscallarg(struct timeval *) tp;
76		syscallarg(struct timezone *) tzp;
77	} */ *uap = v;
78	struct timeval atv;
79	int error = 0;
80
81	if (SCARG(uap, tp)) {
82		microtime(&atv);
83		error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
84				sizeof (atv));
85		if (error)
86			return (error);
87	}
88	if (SCARG(uap, tzp))
89		error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
90		    sizeof (tz));
91	return (error);
92}
93
94/* ARGSUSED */
95int
96sys_settimeofday(p, v, retval)
97	struct proc *p;
98	void *v;
99	register_t *retval;
100{
101	struct sys_settimeofday_args /* {
102		syscallarg(struct timeval *) tv;
103		syscallarg(struct timezone *) tzp;
104	} */ *uap = v;
105	struct timeval atv, delta;
106	struct timezone atz;
107	int error, s;
108
109	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
110		return (error);
111	/* Verify all parameters before changing time. */
112	if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
113	    (caddr_t)&atv, sizeof(atv))))
114		return (error);
115	if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
116	    (caddr_t)&atz, sizeof(atz))))
117		return (error);
118	if (SCARG(uap, tv)) {
119		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
120		s = splclock();
121		timersub(&atv, &time, &delta);
122		time = atv;
123		(void) splsoftclock();
124		timeradd(&boottime, &delta, &boottime);
125		timeradd(&runtime, &delta, &runtime);
126# 		if defined(NFSCLIENT) || defined(NFSSERVER)
127			nqnfs_lease_updatetime(delta.tv_sec);
128#		endif
129		splx(s);
130		resettodr();
131	}
132	if (SCARG(uap, tzp))
133		tz = atz;
134	return (0);
135}
136
137int	tickdelta;			/* current clock skew, us. per tick */
138long	timedelta;			/* unapplied time correction, us. */
139long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
140
141/* ARGSUSED */
142int
143sys_adjtime(p, v, retval)
144	struct proc *p;
145	void *v;
146	register_t *retval;
147{
148	register struct sys_adjtime_args /* {
149		syscallarg(struct timeval *) delta;
150		syscallarg(struct timeval *) olddelta;
151	} */ *uap = v;
152	struct timeval atv;
153	register long ndelta, ntickdelta, odelta;
154	int s, error;
155
156	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
157		return (error);
158
159	error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
160		       sizeof(struct timeval));
161	if (error)
162		return (error);
163
164	/*
165	 * Compute the total correction and the rate at which to apply it.
166	 * Round the adjustment down to a whole multiple of the per-tick
167	 * delta, so that after some number of incremental changes in
168	 * hardclock(), tickdelta will become zero, lest the correction
169	 * overshoot and start taking us away from the desired final time.
170	 */
171	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
172	if (ndelta > bigadj)
173		ntickdelta = 10 * tickadj;
174	else
175		ntickdelta = tickadj;
176	if (ndelta % ntickdelta)
177		ndelta = ndelta / ntickdelta * ntickdelta;
178
179	/*
180	 * To make hardclock()'s job easier, make the per-tick delta negative
181	 * if we want time to run slower; then hardclock can simply compute
182	 * tick + tickdelta, and subtract tickdelta from timedelta.
183	 */
184	if (ndelta < 0)
185		ntickdelta = -ntickdelta;
186	s = splclock();
187	odelta = timedelta;
188	timedelta = ndelta;
189	tickdelta = ntickdelta;
190	splx(s);
191
192	if (SCARG(uap, olddelta)) {
193		atv.tv_sec = odelta / 1000000;
194		atv.tv_usec = odelta % 1000000;
195		(void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
196		    sizeof(struct timeval));
197	}
198	return (0);
199}
200
201/*
202 * Get value of an interval timer.  The process virtual and
203 * profiling virtual time timers are kept in the p_stats area, since
204 * they can be swapped out.  These are kept internally in the
205 * way they are specified externally: in time until they expire.
206 *
207 * The real time interval timer is kept in the process table slot
208 * for the process, and its value (it_value) is kept as an
209 * absolute time rather than as a delta, so that it is easy to keep
210 * periodic real-time signals from drifting.
211 *
212 * Virtual time timers are processed in the hardclock() routine of
213 * kern_clock.c.  The real time timer is processed by a timeout
214 * routine, called from the softclock() routine.  Since a callout
215 * may be delayed in real time due to interrupt processing in the system,
216 * it is possible for the real time timeout routine (realitexpire, given below),
217 * to be delayed in real time past when it is supposed to occur.  It
218 * does not suffice, therefore, to reload the real timer .it_value from the
219 * real time timers .it_interval.  Rather, we compute the next time in
220 * absolute time the timer should go off.
221 */
222/* ARGSUSED */
223int
224sys_getitimer(p, v, retval)
225	struct proc *p;
226	void *v;
227	register_t *retval;
228{
229	register struct sys_getitimer_args /* {
230		syscallarg(u_int) which;
231		syscallarg(struct itimerval *) itv;
232	} */ *uap = v;
233	struct itimerval aitv;
234	int s;
235
236	if (SCARG(uap, which) > ITIMER_PROF)
237		return (EINVAL);
238	s = splclock();
239	if (SCARG(uap, which) == ITIMER_REAL) {
240		/*
241		 * Convert from absolute to relative time in .it_value
242		 * part of real time timer.  If time for real time timer
243		 * has passed return 0, else return difference between
244		 * current time and time for the timer to go off.
245		 */
246		aitv = p->p_realtimer;
247		if (timerisset(&aitv.it_value))
248			if (timercmp(&aitv.it_value, &time, <))
249				timerclear(&aitv.it_value);
250			else
251				timersub(&aitv.it_value, &time, &aitv.it_value);
252	} else
253		aitv = p->p_stats->p_timer[SCARG(uap, which)];
254	splx(s);
255	return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
256	    sizeof (struct itimerval)));
257}
258
259/* ARGSUSED */
260int
261sys_setitimer(p, v, retval)
262	struct proc *p;
263	register void *v;
264	register_t *retval;
265{
266	register struct sys_setitimer_args /* {
267		syscallarg(u_int) which;
268		syscallarg(struct itimerval *) itv;
269		syscallarg(struct itimerval *) oitv;
270	} */ *uap = v;
271	struct sys_getitimer_args getargs;
272	struct itimerval aitv;
273	register struct itimerval *itvp;
274	int s, error;
275
276	if (SCARG(uap, which) > ITIMER_PROF)
277		return (EINVAL);
278	itvp = SCARG(uap, itv);
279	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
280	    sizeof(struct itimerval))))
281		return (error);
282	if (SCARG(uap, oitv) != NULL) {
283		SCARG(&getargs, which) = SCARG(uap, which);
284		SCARG(&getargs, itv) = SCARG(uap, oitv);
285	    	if ((error = sys_getitimer(p, &getargs, retval)) != 0)
286			return (error);
287	}
288	if (itvp == 0)
289		return (0);
290	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
291		return (EINVAL);
292	s = splclock();
293	if (SCARG(uap, which) == ITIMER_REAL) {
294		untimeout(realitexpire, p);
295		if (timerisset(&aitv.it_value)) {
296			timeradd(&aitv.it_value, &time, &aitv.it_value);
297			timeout(realitexpire, p, hzto(&aitv.it_value));
298		}
299		p->p_realtimer = aitv;
300	} else
301		p->p_stats->p_timer[SCARG(uap, which)] = aitv;
302	splx(s);
303	return (0);
304}
305
306/*
307 * Real interval timer expired:
308 * send process whose timer expired an alarm signal.
309 * If time is not set up to reload, then just return.
310 * Else compute next time timer should go off which is > current time.
311 * This is where delay in processing this timeout causes multiple
312 * SIGALRM calls to be compressed into one.
313 */
314void
315realitexpire(arg)
316	void *arg;
317{
318	register struct proc *p;
319	int s;
320
321	p = (struct proc *)arg;
322	psignal(p, SIGALRM);
323	if (!timerisset(&p->p_realtimer.it_interval)) {
324		timerclear(&p->p_realtimer.it_value);
325		return;
326	}
327	for (;;) {
328		s = splclock();
329		timeradd(&p->p_realtimer.it_value,
330		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
331		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
332			timeout(realitexpire, p,
333			    hzto(&p->p_realtimer.it_value));
334			splx(s);
335			return;
336		}
337		splx(s);
338	}
339}
340
341/*
342 * Check that a proposed value to load into the .it_value or
343 * .it_interval part of an interval timer is acceptable, and
344 * fix it to have at least minimal value (i.e. if it is less
345 * than the resolution of the clock, round it up.)
346 */
347int
348itimerfix(tv)
349	struct timeval *tv;
350{
351
352	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
353	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
354		return (EINVAL);
355	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
356		tv->tv_usec = tick;
357	return (0);
358}
359
360/*
361 * Decrement an interval timer by a specified number
362 * of microseconds, which must be less than a second,
363 * i.e. < 1000000.  If the timer expires, then reload
364 * it.  In this case, carry over (usec - old value) to
365 * reduce the value reloaded into the timer so that
366 * the timer does not drift.  This routine assumes
367 * that it is called in a context where the timers
368 * on which it is operating cannot change in value.
369 */
370int
371itimerdecr(itp, usec)
372	register struct itimerval *itp;
373	int usec;
374{
375
376	if (itp->it_value.tv_usec < usec) {
377		if (itp->it_value.tv_sec == 0) {
378			/* expired, and already in next interval */
379			usec -= itp->it_value.tv_usec;
380			goto expire;
381		}
382		itp->it_value.tv_usec += 1000000;
383		itp->it_value.tv_sec--;
384	}
385	itp->it_value.tv_usec -= usec;
386	usec = 0;
387	if (timerisset(&itp->it_value))
388		return (1);
389	/* expired, exactly at end of interval */
390expire:
391	if (timerisset(&itp->it_interval)) {
392		itp->it_value = itp->it_interval;
393		itp->it_value.tv_usec -= usec;
394		if (itp->it_value.tv_usec < 0) {
395			itp->it_value.tv_usec += 1000000;
396			itp->it_value.tv_sec--;
397		}
398	} else
399		itp->it_value.tv_usec = 0;		/* sec is already 0 */
400	return (0);
401}
402