xref: /netbsd-src/sys/kern/kern_time.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: kern_time.c,v 1.14 1995/03/21 13:33:41 mycroft 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 
45 #include <sys/mount.h>
46 #include <sys/syscallargs.h>
47 
48 #include <machine/cpu.h>
49 
50 /*
51  * Time of day and interval timer support.
52  *
53  * These routines provide the kernel entry points to get and set
54  * the time-of-day and per-process interval timers.  Subroutines
55  * here provide support for adding and subtracting timeval structures
56  * and decrementing interval timers, optionally reloading the interval
57  * timers when they expire.
58  */
59 
60 /* ARGSUSED */
61 int
62 gettimeofday(p, uap, retval)
63 	struct proc *p;
64 	register struct gettimeofday_args /* {
65 		syscallarg(struct timeval *) tp;
66 		syscallarg(struct timezone *) tzp;
67 	} */ *uap;
68 	register_t *retval;
69 {
70 	struct timeval atv;
71 	int error = 0;
72 
73 	if (SCARG(uap, tp)) {
74 		microtime(&atv);
75 		if (error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
76 		    sizeof (atv)))
77 			return (error);
78 	}
79 	if (SCARG(uap, tzp))
80 		error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
81 		    sizeof (tz));
82 	return (error);
83 }
84 
85 /* ARGSUSED */
86 int
87 settimeofday(p, uap, retval)
88 	struct proc *p;
89 	struct settimeofday_args /* {
90 		syscallarg(struct timeval *) tv;
91 		syscallarg(struct timezone *) tzp;
92 	} */ *uap;
93 	register_t *retval;
94 {
95 	struct timeval atv, delta;
96 	struct timezone atz;
97 	int error, s;
98 
99 	if (error = suser(p->p_ucred, &p->p_acflag))
100 		return (error);
101 	/* Verify all parameters before changing time. */
102 	if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
103 	    (caddr_t)&atv, sizeof(atv))))
104 		return (error);
105 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
106 	    (caddr_t)&atz, sizeof(atz))))
107 		return (error);
108 	if (SCARG(uap, tv)) {
109 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
110 		s = splclock();
111 		timersub(&atv, &time, &delta);
112 		time = atv;
113 		(void) splsoftclock();
114 		timeradd(&boottime, &delta, &boottime);
115 		timeradd(&runtime, &delta, &runtime);
116 # 		if defined(NFSCLIENT) || defined(NFSSERVER)
117 			lease_updatetime(delta.tv_sec);
118 #		endif
119 		splx(s);
120 		resettodr();
121 	}
122 	if (SCARG(uap, tzp))
123 		tz = atz;
124 	return (0);
125 }
126 
127 int	tickdelta;			/* current clock skew, us. per tick */
128 long	timedelta;			/* unapplied time correction, us. */
129 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
130 
131 /* ARGSUSED */
132 int
133 adjtime(p, uap, retval)
134 	struct proc *p;
135 	register struct adjtime_args /* {
136 		syscallarg(struct timeval *) delta;
137 		syscallarg(struct timeval *) olddelta;
138 	} */ *uap;
139 	register_t *retval;
140 {
141 	struct timeval atv;
142 	register long ndelta, ntickdelta, odelta;
143 	int s, error;
144 
145 	if (error = suser(p->p_ucred, &p->p_acflag))
146 		return (error);
147 	if (error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
148 	    sizeof(struct timeval)))
149 		return (error);
150 
151 	/*
152 	 * Compute the total correction and the rate at which to apply it.
153 	 * Round the adjustment down to a whole multiple of the per-tick
154 	 * delta, so that after some number of incremental changes in
155 	 * hardclock(), tickdelta will become zero, lest the correction
156 	 * overshoot and start taking us away from the desired final time.
157 	 */
158 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
159 	if (ndelta > bigadj)
160 		ntickdelta = 10 * tickadj;
161 	else
162 		ntickdelta = tickadj;
163 	if (ndelta % ntickdelta)
164 		ndelta = ndelta / ntickdelta * ntickdelta;
165 
166 	/*
167 	 * To make hardclock()'s job easier, make the per-tick delta negative
168 	 * if we want time to run slower; then hardclock can simply compute
169 	 * tick + tickdelta, and subtract tickdelta from timedelta.
170 	 */
171 	if (ndelta < 0)
172 		ntickdelta = -ntickdelta;
173 	s = splclock();
174 	odelta = timedelta;
175 	timedelta = ndelta;
176 	tickdelta = ntickdelta;
177 	splx(s);
178 
179 	if (SCARG(uap, olddelta)) {
180 		atv.tv_sec = odelta / 1000000;
181 		atv.tv_usec = odelta % 1000000;
182 		(void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
183 		    sizeof(struct timeval));
184 	}
185 	return (0);
186 }
187 
188 /*
189  * Get value of an interval timer.  The process virtual and
190  * profiling virtual time timers are kept in the p_stats area, since
191  * they can be swapped out.  These are kept internally in the
192  * way they are specified externally: in time until they expire.
193  *
194  * The real time interval timer is kept in the process table slot
195  * for the process, and its value (it_value) is kept as an
196  * absolute time rather than as a delta, so that it is easy to keep
197  * periodic real-time signals from drifting.
198  *
199  * Virtual time timers are processed in the hardclock() routine of
200  * kern_clock.c.  The real time timer is processed by a timeout
201  * routine, called from the softclock() routine.  Since a callout
202  * may be delayed in real time due to interrupt processing in the system,
203  * it is possible for the real time timeout routine (realitexpire, given below),
204  * to be delayed in real time past when it is supposed to occur.  It
205  * does not suffice, therefore, to reload the real timer .it_value from the
206  * real time timers .it_interval.  Rather, we compute the next time in
207  * absolute time the timer should go off.
208  */
209 /* ARGSUSED */
210 int
211 getitimer(p, uap, retval)
212 	struct proc *p;
213 	register struct getitimer_args /* {
214 		syscallarg(u_int) which;
215 		syscallarg(struct itimerval *) itv;
216 	} */ *uap;
217 	register_t *retval;
218 {
219 	struct itimerval aitv;
220 	int s;
221 
222 	if (SCARG(uap, which) > ITIMER_PROF)
223 		return (EINVAL);
224 	s = splclock();
225 	if (SCARG(uap, which) == ITIMER_REAL) {
226 		/*
227 		 * Convert from absolute to relative time in .it_value
228 		 * part of real time timer.  If time for real time timer
229 		 * has passed return 0, else return difference between
230 		 * current time and time for the timer to go off.
231 		 */
232 		aitv = p->p_realtimer;
233 		if (timerisset(&aitv.it_value))
234 			if (timercmp(&aitv.it_value, &time, <))
235 				timerclear(&aitv.it_value);
236 			else
237 				timersub(&aitv.it_value, &time, &aitv.it_value);
238 	} else
239 		aitv = p->p_stats->p_timer[SCARG(uap, which)];
240 	splx(s);
241 	return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
242 	    sizeof (struct itimerval)));
243 }
244 
245 /* ARGSUSED */
246 int
247 setitimer(p, uap, retval)
248 	struct proc *p;
249 	register struct setitimer_args /* {
250 		syscallarg(u_int) which;
251 		syscallarg(struct itimerval *) itv;
252 		syscallarg(struct itimerval *) oitv;
253 	} */ *uap;
254 	register_t *retval;
255 {
256 	struct itimerval aitv;
257 	register struct itimerval *itvp;
258 	int s, error;
259 
260 	if (SCARG(uap, which) > ITIMER_PROF)
261 		return (EINVAL);
262 	itvp = SCARG(uap, itv);
263 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
264 	    sizeof(struct itimerval))))
265 		return (error);
266 	if ((SCARG(uap, itv) = SCARG(uap, oitv)) &&
267 	    (error = getitimer(p, uap, retval)))
268 		return (error);
269 	if (itvp == 0)
270 		return (0);
271 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
272 		return (EINVAL);
273 	s = splclock();
274 	if (SCARG(uap, which) == ITIMER_REAL) {
275 		untimeout(realitexpire, p);
276 		if (timerisset(&aitv.it_value)) {
277 			timeradd(&aitv.it_value, &time, &aitv.it_value);
278 			timeout(realitexpire, p, hzto(&aitv.it_value));
279 		}
280 		p->p_realtimer = aitv;
281 	} else
282 		p->p_stats->p_timer[SCARG(uap, which)] = aitv;
283 	splx(s);
284 	return (0);
285 }
286 
287 /*
288  * Real interval timer expired:
289  * send process whose timer expired an alarm signal.
290  * If time is not set up to reload, then just return.
291  * Else compute next time timer should go off which is > current time.
292  * This is where delay in processing this timeout causes multiple
293  * SIGALRM calls to be compressed into one.
294  */
295 void
296 realitexpire(arg)
297 	void *arg;
298 {
299 	register struct proc *p;
300 	int s;
301 
302 	p = (struct proc *)arg;
303 	psignal(p, SIGALRM);
304 	if (!timerisset(&p->p_realtimer.it_interval)) {
305 		timerclear(&p->p_realtimer.it_value);
306 		return;
307 	}
308 	for (;;) {
309 		s = splclock();
310 		timeradd(&p->p_realtimer.it_value,
311 		    &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
312 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
313 			timeout(realitexpire, p,
314 			    hzto(&p->p_realtimer.it_value));
315 			splx(s);
316 			return;
317 		}
318 		splx(s);
319 	}
320 }
321 
322 /*
323  * Check that a proposed value to load into the .it_value or
324  * .it_interval part of an interval timer is acceptable, and
325  * fix it to have at least minimal value (i.e. if it is less
326  * than the resolution of the clock, round it up.)
327  */
328 int
329 itimerfix(tv)
330 	struct timeval *tv;
331 {
332 
333 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
334 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
335 		return (EINVAL);
336 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
337 		tv->tv_usec = tick;
338 	return (0);
339 }
340 
341 /*
342  * Decrement an interval timer by a specified number
343  * of microseconds, which must be less than a second,
344  * i.e. < 1000000.  If the timer expires, then reload
345  * it.  In this case, carry over (usec - old value) to
346  * reduce the value reloaded into the timer so that
347  * the timer does not drift.  This routine assumes
348  * that it is called in a context where the timers
349  * on which it is operating cannot change in value.
350  */
351 int
352 itimerdecr(itp, usec)
353 	register struct itimerval *itp;
354 	int usec;
355 {
356 
357 	if (itp->it_value.tv_usec < usec) {
358 		if (itp->it_value.tv_sec == 0) {
359 			/* expired, and already in next interval */
360 			usec -= itp->it_value.tv_usec;
361 			goto expire;
362 		}
363 		itp->it_value.tv_usec += 1000000;
364 		itp->it_value.tv_sec--;
365 	}
366 	itp->it_value.tv_usec -= usec;
367 	usec = 0;
368 	if (timerisset(&itp->it_value))
369 		return (1);
370 	/* expired, exactly at end of interval */
371 expire:
372 	if (timerisset(&itp->it_interval)) {
373 		itp->it_value = itp->it_interval;
374 		itp->it_value.tv_usec -= usec;
375 		if (itp->it_value.tv_usec < 0) {
376 			itp->it_value.tv_usec += 1000000;
377 			itp->it_value.tv_sec--;
378 		}
379 	} else
380 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
381 	return (0);
382 }
383