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