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