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