- Uncomment some SYN handling code
[reactos.git] / reactos / lib / drivers / oskittcp / oskittcp / kern_clock.c
1 /*
2 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
3 * All rights reserved.
4 *
5 * This file is part of the Flux OSKit. The OSKit is free software, also known
6 * as "open source;" you can redistribute it and/or modify it under the terms
7 * of the GNU General Public License (GPL), version 2, as published by the Free
8 * Software Foundation (FSF). To explore alternate licensing terms, contact
9 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
10 *
11 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
14 * received a copy of the GPL along with the OSKit; see the file COPYING. If
15 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
16 */
17 /*
18 * I am having no mercy with this file.
19 * I ripped out everything I didn't need
20 */
21 /*-
22 * Copyright (c) 1982, 1986, 1991, 1993
23 * The Regents of the University of California. All rights reserved.
24 * (c) UNIX System Laboratories, Inc.
25 * All or some portions of this file are derived from material licensed
26 * to the University of California by American Telephone and Telegraph
27 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
28 * the permission of UNIX System Laboratories, Inc.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
59 * $\Id: kern_clock.c,v 1.26 1996/07/30 16:59:22 bde Exp $
60 */
61
62 /* Portions of this software are covered by the following: */
63 /******************************************************************************
64 * *
65 * Copyright (c) David L. Mills 1993, 1994 *
66 * *
67 * Permission to use, copy, modify, and distribute this software and its *
68 * documentation for any purpose and without fee is hereby granted, provided *
69 * that the above copyright notice appears in all copies and that both the *
70 * copyright notice and this permission notice appear in supporting *
71 * documentation, and that the name University of Delaware not be used in *
72 * advertising or publicity pertaining to distribution of the software *
73 * without specific, written prior permission. The University of Delaware *
74 * makes no representations about the suitability this software for any *
75 * purpose. It is provided "as is" without express or implied warranty. *
76 * *
77 *****************************************************************************/
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/callout.h>
82 #include <sys/kernel.h>
83 #include <sys/proc.h>
84
85 /* Exported to machdep.c. */
86 struct callout *callfree, *callout;
87
88 struct callout calltodo;
89
90 long tk_cancc;
91 long tk_nin;
92 long tk_nout;
93 long tk_rawcc;
94
95 /*
96 * Clock handling routines.
97 *
98 * This code is written to operate with two timers that run independently of
99 * each other. The main clock, running hz times per second, is used to keep
100 * track of real time. The second timer handles kernel and user profiling,
101 * and does resource use estimation. If the second timer is programmable,
102 * it is randomized to avoid aliasing between the two clocks. For example,
103 * the randomization prevents an adversary from always giving up the cpu
104 * just before its quantum expires. Otherwise, it would never accumulate
105 * cpu ticks. The mean frequency of the second timer is stathz.
106 *
107 * If no second timer exists, stathz will be zero; in this case we drive
108 * profiling and statistics off the main clock. This WILL NOT be accurate;
109 * do not do it unless absolutely necessary.
110 *
111 * The statistics clock may (or may not) be run at a higher rate while
112 * profiling. This profile clock runs at profhz. We require that profhz
113 * be an integral multiple of stathz.
114 *
115 * If the statistics clock is running fast, it must be divided by the ratio
116 * profhz/stathz for statistics. (For profiling, every tick counts.)
117 */
118
119 /*
120 * TODO:
121 * allocate more timeout table slots when table overflows.
122 */
123
124 /*
125 * Bump a timeval by a small number of usec's.
126 */
127 #define BUMPTIME(t, usec) { \
128 register volatile struct timeval *tp = (t); \
129 register long us; \
130 \
131 tp->tv_usec = us = tp->tv_usec + (usec); \
132 if (us >= 1000000) { \
133 tp->tv_usec = us - 1000000; \
134 tp->tv_sec++; \
135 } \
136 }
137
138 int ticks;
139 volatile struct timeval kern_time;
140 volatile struct timeval mono_time;
141
142 /*
143 * Software (low priority) clock interrupt.
144 * Run periodic events from timeout queue.
145 */
146 /*ARGSUSED*/
147 void
148 softclock()
149 {
150 register struct callout *c;
151 register void *arg;
152 register void (*func) __P((void *));
153 register int s;
154
155 s = splhigh();
156 while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
157 func = c->c_func;
158 arg = c->c_arg;
159 calltodo.c_next = c->c_next;
160 c->c_next = callfree;
161 callfree = c;
162 splx(s);
163 (*func)(arg);
164 (void) splhigh();
165 }
166 splx(s);
167 }
168
169 /*
170 * timeout --
171 * Execute a function after a specified length of time.
172 *
173 * untimeout --
174 * Cancel previous timeout function call.
175 *
176 * See AT&T BCI Driver Reference Manual for specification. This
177 * implementation differs from that one in that no identification
178 * value is returned from timeout, rather, the original arguments
179 * to timeout are used to identify entries for untimeout.
180 */
181 void
182 timeout(ftn, arg, ticks)
183 timeout_t ftn;
184 void *arg;
185 register int ticks;
186 {
187 register struct callout *new, *p, *t;
188 register int s;
189
190 if (ticks <= 0)
191 ticks = 1;
192
193 /* Lock out the clock. */
194 s = splhigh();
195
196 /* Fill in the next free callout structure. */
197 if (callfree == NULL)
198 panic("timeout table full");
199 new = callfree;
200 callfree = new->c_next;
201 new->c_arg = arg;
202 new->c_func = ftn;
203
204 /*
205 * The time for each event is stored as a difference from the time
206 * of the previous event on the queue. Walk the queue, correcting
207 * the ticks argument for queue entries passed. Correct the ticks
208 * value for the queue entry immediately after the insertion point
209 * as well. Watch out for negative c_time values; these represent
210 * overdue events.
211 */
212 for (p = &calltodo;
213 (t = p->c_next) != NULL && ticks > t->c_time; p = t)
214 if (t->c_time > 0)
215 ticks -= t->c_time;
216 new->c_time = ticks;
217 if (t != NULL)
218 t->c_time -= ticks;
219
220 /* Insert the new entry into the queue. */
221 p->c_next = new;
222 new->c_next = t;
223 splx(s);
224 }
225
226 void
227 untimeout(ftn, arg)
228 timeout_t ftn;
229 void *arg;
230 {
231 register struct callout *p, *t;
232 register int s;
233
234 s = splhigh();
235 for (p = &calltodo; (t = p->c_next) != NULL; p = t)
236 if (t->c_func == ftn && t->c_arg == arg) {
237 /* Increment next entry's tick count. */
238 if (t->c_next && t->c_time > 0)
239 t->c_next->c_time += t->c_time;
240
241 /* Move entry from callout queue to callfree queue. */
242 p->c_next = t->c_next;
243 t->c_next = callfree;
244 callfree = t;
245 break;
246 }
247 splx(s);
248 }
249
250 /*
251 * Compute number of hz until specified time. Used to
252 * compute third argument to timeout() from an absolute time.
253 */
254 int
255 hzto(tv)
256 struct timeval *tv;
257 {
258 register unsigned long ticks;
259 register long sec, usec;
260 int s;
261
262 /*
263 * If the number of usecs in the whole seconds part of the time
264 * difference fits in a long, then the total number of usecs will
265 * fit in an unsigned long. Compute the total and convert it to
266 * ticks, rounding up and adding 1 to allow for the current tick
267 * to expire. Rounding also depends on unsigned long arithmetic
268 * to avoid overflow.
269 *
270 * Otherwise, if the number of ticks in the whole seconds part of
271 * the time difference fits in a long, then convert the parts to
272 * ticks separately and add, using similar rounding methods and
273 * overflow avoidance. This method would work in the previous
274 * case but it is slightly slower and assumes that hz is integral.
275 *
276 * Otherwise, round the time difference down to the maximum
277 * representable value.
278 *
279 * If ints have 32 bits, then the maximum value for any timeout in
280 * 10ms ticks is 248 days.
281 */
282 s = splclock();
283 sec = tv->tv_sec - kern_time.tv_sec;
284 usec = tv->tv_usec - kern_time.tv_usec;
285 splx(s);
286 if (usec < 0) {
287 sec--;
288 usec += 1000000;
289 }
290 if (sec < 0) {
291 #ifdef DIAGNOSTIC
292 printf("hzto: negative time difference %ld sec %ld usec\n",
293 sec, usec);
294 #endif
295 ticks = 1;
296 } else if (sec <= LONG_MAX / 1000000)
297 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
298 / tick + 1;
299 else if (sec <= LONG_MAX / hz)
300 ticks = sec * hz
301 + ((unsigned long)usec + (tick - 1)) / tick + 1;
302 else
303 ticks = LONG_MAX;
304 if (ticks > INT_MAX)
305 ticks = INT_MAX;
306 return (ticks);
307 }
308