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