1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. 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 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 * $Id: kern_time.c,v 1.12 1995/11/19 00:59:22 bde Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/sysproto.h>
39#include <sys/resourcevar.h>
40#include <sys/signalvar.h>
41#include <sys/kernel.h>
42#include <sys/systm.h>
43#include <sys/proc.h>
44#include <sys/vnode.h>
45
46#include <machine/cpu.h>
47
48struct timezone tz;
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#ifndef _SYS_SYSPROTO_H_
61struct gettimeofday_args {
62 struct timeval *tp;
63 struct timezone *tzp;
64};
65#endif
66/* ARGSUSED */
67int
68gettimeofday(p, uap, retval)
69 struct proc *p;
70 register struct gettimeofday_args *uap;
71 int *retval;
72{
73 struct timeval atv;
74 int error = 0;
75
76 if (uap->tp) {
77 microtime(&atv);
78 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
79 sizeof (atv))))
80 return (error);
81 }
82 if (uap->tzp)
83 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
84 sizeof (tz));
85 return (error);
86}
87
88#ifndef _SYS_SYSPROTO_H_
89struct settimeofday_args {
90 struct timeval *tv;
91 struct timezone *tzp;
92};
93#endif
94/* ARGSUSED */
95int
96settimeofday(p, uap, retval)
97 struct proc *p;
98 struct settimeofday_args *uap;
99 int *retval;
100{
101 struct timeval atv, delta;
102 struct timezone atz;
103 int error, s;
104
105 if ((error = suser(p->p_ucred, &p->p_acflag)))
106 return (error);
107 /* Verify all parameters before changing time. */
108 if (uap->tv &&
109 (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv))))
110 return (error);
111 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
112 return (EINVAL);
113 if (uap->tzp &&
114 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
115 return (error);
116 if (uap->tv) {
117 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
118 s = splclock();
119 /* nb. delta.tv_usec may be < 0, but this is OK here */
120 delta.tv_sec = atv.tv_sec - time.tv_sec;
121 delta.tv_usec = atv.tv_usec - time.tv_usec;
122 time = atv; /* XXX should avoid skew in tv_usec */
123 (void) splsoftclock();
124 timevalfix(&delta);
125 timevaladd(&boottime, &delta);
126 timevaladd(&runtime, &delta);
127 LEASE_UPDATETIME(delta.tv_sec);
128 splx(s);
129 resettodr();
130 }
131 if (uap->tzp)
132 tz = atz;
133 return (0);
134}
135
136extern int tickadj; /* "standard" clock skew, us./tick */
137int tickdelta; /* current clock skew, us. per tick */
138long timedelta; /* unapplied time correction, us. */
139static long bigadj = 1000000; /* use 10x skew above bigadj us. */
140
141#ifndef _SYS_SYSPROTO_H_
142struct adjtime_args {
143 struct timeval *delta;
144 struct timeval *olddelta;
145};
146#endif
147/* ARGSUSED */
148int
149adjtime(p, uap, retval)
150 struct proc *p;
151 register struct adjtime_args *uap;
152 int *retval;
153{
154 struct timeval atv;
155 register long ndelta, ntickdelta, odelta;
156 int s, error;
157
158 if ((error = suser(p->p_ucred, &p->p_acflag)))
159 return (error);
160 if ((error =
161 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
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 (uap->olddelta) {
193 atv.tv_sec = odelta / 1000000;
194 atv.tv_usec = odelta % 1000000;
195 (void) copyout((caddr_t)&atv, (caddr_t)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#ifndef _SYS_SYSPROTO_H_
223struct getitimer_args {
224 u_int which;
225 struct itimerval *itv;
226};
227#endif
228/* ARGSUSED */
229int
230getitimer(p, uap, retval)
231 struct proc *p;
232 register struct getitimer_args *uap;
233 int *retval;
234{
235 struct itimerval aitv;
236 int s;
237
238 if (uap->which > ITIMER_PROF)
239 return (EINVAL);
240 s = splclock();
241 if (uap->which == ITIMER_REAL) {
242 /*
243 * Convert from absoulte to relative time in .it_value
244 * part of real time timer. If time for real time timer
245 * has passed return 0, else return difference between
246 * current time and time for the timer to go off.
247 */
248 aitv = p->p_realtimer;
249 if (timerisset(&aitv.it_value))
250 if (timercmp(&aitv.it_value, &time, <))
251 timerclear(&aitv.it_value);
252 else
253 timevalsub(&aitv.it_value,
254 (struct timeval *)&time);
255 } else
256 aitv = p->p_stats->p_timer[uap->which];
257 splx(s);
258 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
259 sizeof (struct itimerval)));
260}
261
262#ifndef _SYS_SYSPROTO_H_
263struct setitimer_args {
264 u_int which;
265 struct itimerval *itv, *oitv;
266};
267#endif
268/* ARGSUSED */
269int
270setitimer(p, uap, retval)
271 struct proc *p;
272 register struct setitimer_args *uap;
273 int *retval;
274{
275 struct itimerval aitv;
276 register struct itimerval *itvp;
277 int s, error;
278
279 if (uap->which > ITIMER_PROF)
280 return (EINVAL);
281 itvp = uap->itv;
282 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
283 sizeof(struct itimerval))))
284 return (error);
285 if ((uap->itv = uap->oitv) &&
286 (error = getitimer(p, (struct getitimer_args *)uap, retval)))
287 return (error);
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 (uap->which == ITIMER_REAL) {
294 untimeout(realitexpire, (caddr_t)p);
295 if (timerisset(&aitv.it_value)) {
296 timevaladd(&aitv.it_value, (struct timeval *)&time);
297 timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
298 }
299 p->p_realtimer = aitv;
300 } else
301 p->p_stats->p_timer[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 * hzto() always adds 1 to allow for the time until the next clock
314 * interrupt being strictly less than 1 clock tick, but we don't want
315 * that here since we want to appear to be in sync with the clock
316 * interrupt even when we're delayed.
317 */
318void
319realitexpire(arg)
320 void *arg;
321{
322 register struct proc *p;
323 int s;
324
325 p = (struct proc *)arg;
326 psignal(p, SIGALRM);
327 if (!timerisset(&p->p_realtimer.it_interval)) {
328 timerclear(&p->p_realtimer.it_value);
329 return;
330 }
331 for (;;) {
332 s = splclock();
333 timevaladd(&p->p_realtimer.it_value,
334 &p->p_realtimer.it_interval);
335 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
336 timeout(realitexpire, (caddr_t)p,
337 hzto(&p->p_realtimer.it_value) - 1);
338 splx(s);
339 return;
340 }
341 splx(s);
342 }
343}
344
345/*
346 * Check that a proposed value to load into the .it_value or
347 * .it_interval part of an interval timer is acceptable, and
348 * fix it to have at least minimal value (i.e. if it is less
349 * than the resolution of the clock, round it up.)
350 */
351int
352itimerfix(tv)
353 struct timeval *tv;
354{
355
356 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
357 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
358 return (EINVAL);
359 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
360 tv->tv_usec = tick;
361 return (0);
362}
363
364/*
365 * Decrement an interval timer by a specified number
366 * of microseconds, which must be less than a second,
367 * i.e. < 1000000. If the timer expires, then reload
368 * it. In this case, carry over (usec - old value) to
369 * reduce the value reloaded into the timer so that
370 * the timer does not drift. This routine assumes
371 * that it is called in a context where the timers
372 * on which it is operating cannot change in value.
373 */
374int
375itimerdecr(itp, usec)
376 register struct itimerval *itp;
377 int usec;
378{
379
380 if (itp->it_value.tv_usec < usec) {
381 if (itp->it_value.tv_sec == 0) {
382 /* expired, and already in next interval */
383 usec -= itp->it_value.tv_usec;
384 goto expire;
385 }
386 itp->it_value.tv_usec += 1000000;
387 itp->it_value.tv_sec--;
388 }
389 itp->it_value.tv_usec -= usec;
390 usec = 0;
391 if (timerisset(&itp->it_value))
392 return (1);
393 /* expired, exactly at end of interval */
394expire:
395 if (timerisset(&itp->it_interval)) {
396 itp->it_value = itp->it_interval;
397 itp->it_value.tv_usec -= usec;
398 if (itp->it_value.tv_usec < 0) {
399 itp->it_value.tv_usec += 1000000;
400 itp->it_value.tv_sec--;
401 }
402 } else
403 itp->it_value.tv_usec = 0; /* sec is already 0 */
404 return (0);
405}
406
407/*
408 * Add and subtract routines for timevals.
409 * N.B.: subtract routine doesn't deal with
410 * results which are before the beginning,
411 * it just gets very confused in this case.
412 * Caveat emptor.
413 */
414void
415timevaladd(t1, t2)
416 struct timeval *t1, *t2;
417{
418
419 t1->tv_sec += t2->tv_sec;
420 t1->tv_usec += t2->tv_usec;
421 timevalfix(t1);
422}
423
424void
425timevalsub(t1, t2)
426 struct timeval *t1, *t2;
427{
428
429 t1->tv_sec -= t2->tv_sec;
430 t1->tv_usec -= t2->tv_usec;
431 timevalfix(t1);
432}
433
434static void
435timevalfix(t1)
436 struct timeval *t1;
437{
438
439 if (t1->tv_usec < 0) {
440 t1->tv_sec--;
441 t1->tv_usec += 1000000;
442 }
443 if (t1->tv_usec >= 1000000) {
444 t1->tv_sec++;
445 t1->tv_usec -= 1000000;
446 }
447}
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. 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 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 * $Id: kern_time.c,v 1.12 1995/11/19 00:59:22 bde Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/sysproto.h>
39#include <sys/resourcevar.h>
40#include <sys/signalvar.h>
41#include <sys/kernel.h>
42#include <sys/systm.h>
43#include <sys/proc.h>
44#include <sys/vnode.h>
45
46#include <machine/cpu.h>
47
48struct timezone tz;
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#ifndef _SYS_SYSPROTO_H_
61struct gettimeofday_args {
62 struct timeval *tp;
63 struct timezone *tzp;
64};
65#endif
66/* ARGSUSED */
67int
68gettimeofday(p, uap, retval)
69 struct proc *p;
70 register struct gettimeofday_args *uap;
71 int *retval;
72{
73 struct timeval atv;
74 int error = 0;
75
76 if (uap->tp) {
77 microtime(&atv);
78 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
79 sizeof (atv))))
80 return (error);
81 }
82 if (uap->tzp)
83 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
84 sizeof (tz));
85 return (error);
86}
87
88#ifndef _SYS_SYSPROTO_H_
89struct settimeofday_args {
90 struct timeval *tv;
91 struct timezone *tzp;
92};
93#endif
94/* ARGSUSED */
95int
96settimeofday(p, uap, retval)
97 struct proc *p;
98 struct settimeofday_args *uap;
99 int *retval;
100{
101 struct timeval atv, delta;
102 struct timezone atz;
103 int error, s;
104
105 if ((error = suser(p->p_ucred, &p->p_acflag)))
106 return (error);
107 /* Verify all parameters before changing time. */
108 if (uap->tv &&
109 (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv))))
110 return (error);
111 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
112 return (EINVAL);
113 if (uap->tzp &&
114 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
115 return (error);
116 if (uap->tv) {
117 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
118 s = splclock();
119 /* nb. delta.tv_usec may be < 0, but this is OK here */
120 delta.tv_sec = atv.tv_sec - time.tv_sec;
121 delta.tv_usec = atv.tv_usec - time.tv_usec;
122 time = atv; /* XXX should avoid skew in tv_usec */
123 (void) splsoftclock();
124 timevalfix(&delta);
125 timevaladd(&boottime, &delta);
126 timevaladd(&runtime, &delta);
127 LEASE_UPDATETIME(delta.tv_sec);
128 splx(s);
129 resettodr();
130 }
131 if (uap->tzp)
132 tz = atz;
133 return (0);
134}
135
136extern int tickadj; /* "standard" clock skew, us./tick */
137int tickdelta; /* current clock skew, us. per tick */
138long timedelta; /* unapplied time correction, us. */
139static long bigadj = 1000000; /* use 10x skew above bigadj us. */
140
141#ifndef _SYS_SYSPROTO_H_
142struct adjtime_args {
143 struct timeval *delta;
144 struct timeval *olddelta;
145};
146#endif
147/* ARGSUSED */
148int
149adjtime(p, uap, retval)
150 struct proc *p;
151 register struct adjtime_args *uap;
152 int *retval;
153{
154 struct timeval atv;
155 register long ndelta, ntickdelta, odelta;
156 int s, error;
157
158 if ((error = suser(p->p_ucred, &p->p_acflag)))
159 return (error);
160 if ((error =
161 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
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 (uap->olddelta) {
193 atv.tv_sec = odelta / 1000000;
194 atv.tv_usec = odelta % 1000000;
195 (void) copyout((caddr_t)&atv, (caddr_t)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#ifndef _SYS_SYSPROTO_H_
223struct getitimer_args {
224 u_int which;
225 struct itimerval *itv;
226};
227#endif
228/* ARGSUSED */
229int
230getitimer(p, uap, retval)
231 struct proc *p;
232 register struct getitimer_args *uap;
233 int *retval;
234{
235 struct itimerval aitv;
236 int s;
237
238 if (uap->which > ITIMER_PROF)
239 return (EINVAL);
240 s = splclock();
241 if (uap->which == ITIMER_REAL) {
242 /*
243 * Convert from absoulte to relative time in .it_value
244 * part of real time timer. If time for real time timer
245 * has passed return 0, else return difference between
246 * current time and time for the timer to go off.
247 */
248 aitv = p->p_realtimer;
249 if (timerisset(&aitv.it_value))
250 if (timercmp(&aitv.it_value, &time, <))
251 timerclear(&aitv.it_value);
252 else
253 timevalsub(&aitv.it_value,
254 (struct timeval *)&time);
255 } else
256 aitv = p->p_stats->p_timer[uap->which];
257 splx(s);
258 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
259 sizeof (struct itimerval)));
260}
261
262#ifndef _SYS_SYSPROTO_H_
263struct setitimer_args {
264 u_int which;
265 struct itimerval *itv, *oitv;
266};
267#endif
268/* ARGSUSED */
269int
270setitimer(p, uap, retval)
271 struct proc *p;
272 register struct setitimer_args *uap;
273 int *retval;
274{
275 struct itimerval aitv;
276 register struct itimerval *itvp;
277 int s, error;
278
279 if (uap->which > ITIMER_PROF)
280 return (EINVAL);
281 itvp = uap->itv;
282 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
283 sizeof(struct itimerval))))
284 return (error);
285 if ((uap->itv = uap->oitv) &&
286 (error = getitimer(p, (struct getitimer_args *)uap, retval)))
287 return (error);
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 (uap->which == ITIMER_REAL) {
294 untimeout(realitexpire, (caddr_t)p);
295 if (timerisset(&aitv.it_value)) {
296 timevaladd(&aitv.it_value, (struct timeval *)&time);
297 timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
298 }
299 p->p_realtimer = aitv;
300 } else
301 p->p_stats->p_timer[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 * hzto() always adds 1 to allow for the time until the next clock
314 * interrupt being strictly less than 1 clock tick, but we don't want
315 * that here since we want to appear to be in sync with the clock
316 * interrupt even when we're delayed.
317 */
318void
319realitexpire(arg)
320 void *arg;
321{
322 register struct proc *p;
323 int s;
324
325 p = (struct proc *)arg;
326 psignal(p, SIGALRM);
327 if (!timerisset(&p->p_realtimer.it_interval)) {
328 timerclear(&p->p_realtimer.it_value);
329 return;
330 }
331 for (;;) {
332 s = splclock();
333 timevaladd(&p->p_realtimer.it_value,
334 &p->p_realtimer.it_interval);
335 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
336 timeout(realitexpire, (caddr_t)p,
337 hzto(&p->p_realtimer.it_value) - 1);
338 splx(s);
339 return;
340 }
341 splx(s);
342 }
343}
344
345/*
346 * Check that a proposed value to load into the .it_value or
347 * .it_interval part of an interval timer is acceptable, and
348 * fix it to have at least minimal value (i.e. if it is less
349 * than the resolution of the clock, round it up.)
350 */
351int
352itimerfix(tv)
353 struct timeval *tv;
354{
355
356 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
357 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
358 return (EINVAL);
359 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
360 tv->tv_usec = tick;
361 return (0);
362}
363
364/*
365 * Decrement an interval timer by a specified number
366 * of microseconds, which must be less than a second,
367 * i.e. < 1000000. If the timer expires, then reload
368 * it. In this case, carry over (usec - old value) to
369 * reduce the value reloaded into the timer so that
370 * the timer does not drift. This routine assumes
371 * that it is called in a context where the timers
372 * on which it is operating cannot change in value.
373 */
374int
375itimerdecr(itp, usec)
376 register struct itimerval *itp;
377 int usec;
378{
379
380 if (itp->it_value.tv_usec < usec) {
381 if (itp->it_value.tv_sec == 0) {
382 /* expired, and already in next interval */
383 usec -= itp->it_value.tv_usec;
384 goto expire;
385 }
386 itp->it_value.tv_usec += 1000000;
387 itp->it_value.tv_sec--;
388 }
389 itp->it_value.tv_usec -= usec;
390 usec = 0;
391 if (timerisset(&itp->it_value))
392 return (1);
393 /* expired, exactly at end of interval */
394expire:
395 if (timerisset(&itp->it_interval)) {
396 itp->it_value = itp->it_interval;
397 itp->it_value.tv_usec -= usec;
398 if (itp->it_value.tv_usec < 0) {
399 itp->it_value.tv_usec += 1000000;
400 itp->it_value.tv_sec--;
401 }
402 } else
403 itp->it_value.tv_usec = 0; /* sec is already 0 */
404 return (0);
405}
406
407/*
408 * Add and subtract routines for timevals.
409 * N.B.: subtract routine doesn't deal with
410 * results which are before the beginning,
411 * it just gets very confused in this case.
412 * Caveat emptor.
413 */
414void
415timevaladd(t1, t2)
416 struct timeval *t1, *t2;
417{
418
419 t1->tv_sec += t2->tv_sec;
420 t1->tv_usec += t2->tv_usec;
421 timevalfix(t1);
422}
423
424void
425timevalsub(t1, t2)
426 struct timeval *t1, *t2;
427{
428
429 t1->tv_sec -= t2->tv_sec;
430 t1->tv_usec -= t2->tv_usec;
431 timevalfix(t1);
432}
433
434static void
435timevalfix(t1)
436 struct timeval *t1;
437{
438
439 if (t1->tv_usec < 0) {
440 t1->tv_sec--;
441 t1->tv_usec += 1000000;
442 }
443 if (t1->tv_usec >= 1000000) {
444 t1->tv_sec++;
445 t1->tv_usec -= 1000000;
446 }
447}