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