make it pretty
[unix-history] / usr / src / sys / kern / kern_time.c
CommitLineData
da7c5cc6 1/*
9cb30eb6
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
dbf0c423 5 * %sccs.include.redist.c%
9cb30eb6 6 *
f66fdda5 7 * @(#)kern_time.c 7.16 (Berkeley) %G%
da7c5cc6 8 */
961945a8 9
94368568 10#include "param.h"
8429d022 11#include "resourcevar.h"
94368568 12#include "kernel.h"
94368568 13#include "proc.h"
f66fdda5 14#include "vnode.h"
b6f30e0a 15
fb1db32c 16
1edb1cf8
BJ
17/*
18 * Time of day and interval timer support.
aa261505
BJ
19 *
20 * These routines provide the kernel entry points to get and set
21 * the time-of-day and per-process interval timers. Subroutines
22 * here provide support for adding and subtracting timeval structures
23 * and decrementing interval timers, optionally reloading the interval
24 * timers when they expire.
1edb1cf8
BJ
25 */
26
fc4a5eac
MK
27/* ARGSUSED */
28gettimeofday(p, uap, retval)
29 struct proc *p;
30 register struct args {
b6f30e0a
BJ
31 struct timeval *tp;
32 struct timezone *tzp;
fc4a5eac
MK
33 } *uap;
34 int *retval;
35{
b6f30e0a 36 struct timeval atv;
fc4a5eac 37 int error = 0;
4147b3f6 38
2b6a7e0f
KB
39 if (uap->tp) {
40 microtime(&atv);
fc4a5eac
MK
41 if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
42 sizeof (atv)))
d9c2f47f 43 return (error);
2b6a7e0f
KB
44 }
45 if (uap->tzp)
fc4a5eac
MK
46 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
47 sizeof (tz));
d9c2f47f 48 return (error);
4147b3f6
BJ
49}
50
cd23cb29 51/* ARGSUSED */
fc4a5eac
MK
52settimeofday(p, uap, retval)
53 struct proc *p;
54 struct args {
1edb1cf8
BJ
55 struct timeval *tv;
56 struct timezone *tzp;
fc4a5eac
MK
57 } *uap;
58 int *retval;
59{
b6f30e0a
BJ
60 struct timeval atv;
61 struct timezone atz;
fc4a5eac 62 int error, s;
4147b3f6 63
8429d022 64 if (error = suser(p->p_ucred, &p->p_acflag))
d9c2f47f 65 return (error);
2b6a7e0f 66 if (uap->tv) {
fc4a5eac
MK
67 if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
68 sizeof (struct timeval)))
d9c2f47f 69 return (error);
9cb30eb6
KM
70 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
71 boottime.tv_sec += atv.tv_sec - time.tv_sec;
f66fdda5 72 LEASE_UPDATETIME(atv.tv_sec - time.tv_sec);
9cb30eb6
KM
73 s = splhigh(); time = atv; splx(s);
74 resettodr();
2b6a7e0f 75 }
fc4a5eac
MK
76 if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
77 sizeof (atz))) == 0)
849cbd39 78 tz = atz;
d9c2f47f 79 return (error);
4147b3f6
BJ
80}
81
4ca0d0d6
MK
82extern int tickadj; /* "standard" clock skew, us./tick */
83int tickdelta; /* current clock skew, us. per tick */
84long timedelta; /* unapplied time correction, us. */
85long bigadj = 1000000; /* use 10x skew above bigadj us. */
99e47f6b 86
fc4a5eac
MK
87/* ARGSUSED */
88adjtime(p, uap, retval)
89 struct proc *p;
90 register struct args {
99e47f6b
MK
91 struct timeval *delta;
92 struct timeval *olddelta;
fc4a5eac
MK
93 } *uap;
94 int *retval;
95{
99e47f6b 96 struct timeval atv, oatv;
4ca0d0d6 97 register long ndelta;
fc4a5eac 98 int s, error;
99e47f6b 99
8429d022 100 if (error = suser(p->p_ucred, &p->p_acflag))
d9c2f47f 101 return (error);
fc4a5eac
MK
102 if (error =
103 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
d9c2f47f 104 return (error);
4ca0d0d6
MK
105 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
106 if (timedelta == 0)
107 if (ndelta > bigadj)
108 tickdelta = 10 * tickadj;
109 else
110 tickdelta = tickadj;
111 if (ndelta % tickdelta)
112 ndelta = ndelta / tickadj * tickadj;
113
8efc019f 114 s = splclock();
99e47f6b 115 if (uap->olddelta) {
4ca0d0d6
MK
116 oatv.tv_sec = timedelta / 1000000;
117 oatv.tv_usec = timedelta % 1000000;
99e47f6b 118 }
4ca0d0d6 119 timedelta = ndelta;
8efc019f 120 splx(s);
4ca0d0d6
MK
121
122 if (uap->olddelta)
123 (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
124 sizeof (struct timeval));
d9c2f47f 125 return (0);
99e47f6b
MK
126}
127
aa261505
BJ
128/*
129 * Get value of an interval timer. The process virtual and
8429d022 130 * profiling virtual time timers are kept in the p_stats area, since
aa261505
BJ
131 * they can be swapped out. These are kept internally in the
132 * way they are specified externally: in time until they expire.
133 *
134 * The real time interval timer is kept in the process table slot
135 * for the process, and its value (it_value) is kept as an
136 * absolute time rather than as a delta, so that it is easy to keep
137 * periodic real-time signals from drifting.
138 *
139 * Virtual time timers are processed in the hardclock() routine of
140 * kern_clock.c. The real time timer is processed by a timeout
141 * routine, called from the softclock() routine. Since a callout
142 * may be delayed in real time due to interrupt processing in the system,
143 * it is possible for the real time timeout routine (realitexpire, given below),
144 * to be delayed in real time past when it is supposed to occur. It
145 * does not suffice, therefore, to reload the real timer .it_value from the
146 * real time timers .it_interval. Rather, we compute the next time in
147 * absolute time the timer should go off.
148 */
fc4a5eac
MK
149/* ARGSUSED */
150getitimer(p, uap, retval)
151 struct proc *p;
152 register struct args {
b6f30e0a
BJ
153 u_int which;
154 struct itimerval *itv;
fc4a5eac
MK
155 } *uap;
156 int *retval;
157{
d01b68d6 158 struct itimerval aitv;
b6f30e0a 159 int s;
aac7ea5b 160
fc4a5eac 161 if (uap->which > ITIMER_PROF)
d9c2f47f 162 return (EINVAL);
fa5e5ab4 163 s = splclock();
d01b68d6 164 if (uap->which == ITIMER_REAL) {
aa261505
BJ
165 /*
166 * Convert from absoulte to relative time in .it_value
167 * part of real time timer. If time for real time timer
168 * has passed return 0, else return difference between
169 * current time and time for the timer to go off.
170 */
fc4a5eac 171 aitv = p->p_realtimer;
d01b68d6
BJ
172 if (timerisset(&aitv.it_value))
173 if (timercmp(&aitv.it_value, &time, <))
174 timerclear(&aitv.it_value);
175 else
176 timevalsub(&aitv.it_value, &time);
177 } else
8429d022 178 aitv = p->p_stats->p_timer[uap->which];
d01b68d6 179 splx(s);
d9c2f47f 180 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
fc4a5eac 181 sizeof (struct itimerval)));
aac7ea5b
BJ
182}
183
fc4a5eac
MK
184/* ARGSUSED */
185setitimer(p, uap, retval)
186 struct proc *p;
187 register struct args {
b6f30e0a 188 u_int which;
1edb1cf8 189 struct itimerval *itv, *oitv;
fc4a5eac
MK
190 } *uap;
191 int *retval;
192{
c4bbb24f
KB
193 struct itimerval aitv;
194 register struct itimerval *itvp;
fc4a5eac 195 int s, error;
aac7ea5b 196
fc4a5eac 197 if (uap->which > ITIMER_PROF)
d9c2f47f 198 return (EINVAL);
c4bbb24f 199 itvp = uap->itv;
fc4a5eac 200 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
c4bbb24f 201 sizeof(struct itimerval))))
d9c2f47f 202 return (error);
fc4a5eac 203 if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
d9c2f47f 204 return (error);
c4bbb24f 205 if (itvp == 0)
fc4a5eac
MK
206 return (0);
207 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
d9c2f47f 208 return (EINVAL);
fa5e5ab4 209 s = splclock();
d01b68d6 210 if (uap->which == ITIMER_REAL) {
b32450f4 211 untimeout(realitexpire, (caddr_t)p);
d01b68d6
BJ
212 if (timerisset(&aitv.it_value)) {
213 timevaladd(&aitv.it_value, &time);
b32450f4 214 timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
d01b68d6
BJ
215 }
216 p->p_realtimer = aitv;
217 } else
8429d022 218 p->p_stats->p_timer[uap->which] = aitv;
b6f30e0a 219 splx(s);
d9c2f47f 220 return (0);
b6f30e0a
BJ
221}
222
aa261505
BJ
223/*
224 * Real interval timer expired:
225 * send process whose timer expired an alarm signal.
226 * If time is not set up to reload, then just return.
227 * Else compute next time timer should go off which is > current time.
228 * This is where delay in processing this timeout causes multiple
229 * SIGALRM calls to be compressed into one.
230 */
231realitexpire(p)
d01b68d6
BJ
232 register struct proc *p;
233{
234 int s;
235
236 psignal(p, SIGALRM);
237 if (!timerisset(&p->p_realtimer.it_interval)) {
238 timerclear(&p->p_realtimer.it_value);
239 return;
240 }
241 for (;;) {
fa5e5ab4 242 s = splclock();
d01b68d6
BJ
243 timevaladd(&p->p_realtimer.it_value,
244 &p->p_realtimer.it_interval);
245 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
b32450f4
BJ
246 timeout(realitexpire, (caddr_t)p,
247 hzto(&p->p_realtimer.it_value));
d01b68d6
BJ
248 splx(s);
249 return;
250 }
251 splx(s);
252 }
253}
254
aa261505
BJ
255/*
256 * Check that a proposed value to load into the .it_value or
257 * .it_interval part of an interval timer is acceptable, and
258 * fix it to have at least minimal value (i.e. if it is less
259 * than the resolution of the clock, round it up.)
260 */
1edb1cf8
BJ
261itimerfix(tv)
262 struct timeval *tv;
b6f30e0a 263{
b6f30e0a 264
d01b68d6
BJ
265 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
266 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1edb1cf8 267 return (EINVAL);
c45fcba6 268 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1edb1cf8
BJ
269 tv->tv_usec = tick;
270 return (0);
b6f30e0a
BJ
271}
272
aa261505
BJ
273/*
274 * Decrement an interval timer by a specified number
275 * of microseconds, which must be less than a second,
276 * i.e. < 1000000. If the timer expires, then reload
277 * it. In this case, carry over (usec - old value) to
278 * reducint the value reloaded into the timer so that
279 * the timer does not drift. This routine assumes
280 * that it is called in a context where the timers
281 * on which it is operating cannot change in value.
282 */
b6f30e0a
BJ
283itimerdecr(itp, usec)
284 register struct itimerval *itp;
285 int usec;
286{
287
1edb1cf8
BJ
288 if (itp->it_value.tv_usec < usec) {
289 if (itp->it_value.tv_sec == 0) {
aa261505 290 /* expired, and already in next interval */
1edb1cf8 291 usec -= itp->it_value.tv_usec;
b6f30e0a 292 goto expire;
1edb1cf8
BJ
293 }
294 itp->it_value.tv_usec += 1000000;
295 itp->it_value.tv_sec--;
aac7ea5b 296 }
1edb1cf8
BJ
297 itp->it_value.tv_usec -= usec;
298 usec = 0;
299 if (timerisset(&itp->it_value))
b6f30e0a 300 return (1);
aa261505 301 /* expired, exactly at end of interval */
b6f30e0a 302expire:
1edb1cf8
BJ
303 if (timerisset(&itp->it_interval)) {
304 itp->it_value = itp->it_interval;
305 itp->it_value.tv_usec -= usec;
306 if (itp->it_value.tv_usec < 0) {
307 itp->it_value.tv_usec += 1000000;
308 itp->it_value.tv_sec--;
309 }
310 } else
aa261505 311 itp->it_value.tv_usec = 0; /* sec is already 0 */
b6f30e0a 312 return (0);
aac7ea5b
BJ
313}
314
aa261505
BJ
315/*
316 * Add and subtract routines for timevals.
317 * N.B.: subtract routine doesn't deal with
318 * results which are before the beginning,
319 * it just gets very confused in this case.
320 * Caveat emptor.
321 */
322timevaladd(t1, t2)
323 struct timeval *t1, *t2;
324{
325
326 t1->tv_sec += t2->tv_sec;
327 t1->tv_usec += t2->tv_usec;
328 timevalfix(t1);
329}
330
331timevalsub(t1, t2)
332 struct timeval *t1, *t2;
333{
334
335 t1->tv_sec -= t2->tv_sec;
336 t1->tv_usec -= t2->tv_usec;
337 timevalfix(t1);
338}
339
340timevalfix(t1)
341 struct timeval *t1;
342{
343
344 if (t1->tv_usec < 0) {
345 t1->tv_sec--;
346 t1->tv_usec += 1000000;
347 }
348 if (t1->tv_usec >= 1000000) {
349 t1->tv_sec++;
350 t1->tv_usec -= 1000000;
351 }
352}