Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
422 | giacomo | 1 | #ifndef _LINUX_TIME_H |
2 | #define _LINUX_TIME_H |
||
3 | |||
4 | #include <asm/param.h> |
||
5 | #include <linux/types.h> |
||
6 | |||
7 | #ifndef _STRUCT_TIMESPEC |
||
8 | #define _STRUCT_TIMESPEC |
||
9 | struct timespec { |
||
10 | time_t tv_sec; /* seconds */ |
||
11 | long tv_nsec; /* nanoseconds */ |
||
12 | }; |
||
13 | #endif /* _STRUCT_TIMESPEC */ |
||
14 | |||
15 | struct timeval { |
||
16 | time_t tv_sec; /* seconds */ |
||
17 | suseconds_t tv_usec; /* microseconds */ |
||
18 | }; |
||
19 | |||
20 | struct timezone { |
||
21 | int tz_minuteswest; /* minutes west of Greenwich */ |
||
22 | int tz_dsttime; /* type of dst correction */ |
||
23 | }; |
||
24 | |||
25 | #ifdef __KERNEL__ |
||
26 | |||
27 | #include <linux/spinlock.h> |
||
28 | #include <linux/seqlock.h> |
||
29 | #include <linux/timex.h> |
||
30 | #include <asm/div64.h> |
||
31 | #ifndef div_long_long_rem |
||
32 | |||
33 | #define div_long_long_rem(dividend,divisor,remainder) ({ \ |
||
34 | u64 result = dividend; \ |
||
35 | *remainder = do_div(result,divisor); \ |
||
36 | result; }) |
||
37 | |||
38 | #endif |
||
39 | |||
40 | /* |
||
41 | * Have the 32 bit jiffies value wrap 5 minutes after boot |
||
42 | * so jiffies wrap bugs show up earlier. |
||
43 | */ |
||
44 | #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ)) |
||
45 | |||
46 | /* |
||
47 | * Change timeval to jiffies, trying to avoid the |
||
48 | * most obvious overflows.. |
||
49 | * |
||
50 | * And some not so obvious. |
||
51 | * |
||
52 | * Note that we don't want to return MAX_LONG, because |
||
53 | * for various timeout reasons we often end up having |
||
54 | * to wait "jiffies+1" in order to guarantee that we wait |
||
55 | * at _least_ "jiffies" - so "jiffies+1" had better still |
||
56 | * be positive. |
||
57 | */ |
||
58 | #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1) |
||
59 | |||
60 | /* Parameters used to convert the timespec values */ |
||
61 | #ifndef USEC_PER_SEC |
||
62 | #define USEC_PER_SEC (1000000L) |
||
63 | #endif |
||
64 | |||
65 | #ifndef NSEC_PER_SEC |
||
66 | #define NSEC_PER_SEC (1000000000L) |
||
67 | #endif |
||
68 | |||
69 | #ifndef NSEC_PER_USEC |
||
70 | #define NSEC_PER_USEC (1000L) |
||
71 | #endif |
||
72 | |||
73 | /* |
||
74 | * We want to do realistic conversions of time so we need to use the same |
||
75 | * values the update wall clock code uses as the jiffies size. This value |
||
76 | * is: TICK_NSEC (which is defined in timex.h). This |
||
77 | * is a constant and is in nanoseconds. We will used scaled math |
||
78 | * with a set of scales defined here as SEC_JIFFIE_SC, USEC_JIFFIE_SC and |
||
79 | * NSEC_JIFFIE_SC. Note that these defines contain nothing but |
||
80 | * constants and so are computed at compile time. SHIFT_HZ (computed in |
||
81 | * timex.h) adjusts the scaling for different HZ values. |
||
82 | |||
83 | * Scaled math??? What is that? |
||
84 | * |
||
85 | * Scaled math is a way to do integer math on values that would, |
||
86 | * otherwise, either overflow, underflow, or cause undesired div |
||
87 | * instructions to appear in the execution path. In short, we "scale" |
||
88 | * up the operands so they take more bits (more precision, less |
||
89 | * underflow), do the desired operation and then "scale" the result back |
||
90 | * by the same amount. If we do the scaling by shifting we avoid the |
||
91 | * costly mpy and the dastardly div instructions. |
||
92 | |||
93 | * Suppose, for example, we want to convert from seconds to jiffies |
||
94 | * where jiffies is defined in nanoseconds as NSEC_PER_JIFFIE. The |
||
95 | * simple math is: jiff = (sec * NSEC_PER_SEC) / NSEC_PER_JIFFIE; We |
||
96 | * observe that (NSEC_PER_SEC / NSEC_PER_JIFFIE) is a constant which we |
||
97 | * might calculate at compile time, however, the result will only have |
||
98 | * about 3-4 bits of precision (less for smaller values of HZ). |
||
99 | * |
||
100 | * So, we scale as follows: |
||
101 | * jiff = (sec) * (NSEC_PER_SEC / NSEC_PER_JIFFIE); |
||
102 | * jiff = ((sec) * ((NSEC_PER_SEC * SCALE)/ NSEC_PER_JIFFIE)) / SCALE; |
||
103 | * Then we make SCALE a power of two so: |
||
104 | * jiff = ((sec) * ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) >> SCALE; |
||
105 | * Now we define: |
||
106 | * #define SEC_CONV = ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) |
||
107 | * jiff = (sec * SEC_CONV) >> SCALE; |
||
108 | * |
||
109 | * Often the math we use will expand beyond 32-bits so we tell C how to |
||
110 | * do this and pass the 64-bit result of the mpy through the ">> SCALE" |
||
111 | * which should take the result back to 32-bits. We want this expansion |
||
112 | * to capture as much precision as possible. At the same time we don't |
||
113 | * want to overflow so we pick the SCALE to avoid this. In this file, |
||
114 | * that means using a different scale for each range of HZ values (as |
||
115 | * defined in timex.h). |
||
116 | * |
||
117 | * For those who want to know, gcc will give a 64-bit result from a "*" |
||
118 | * operator if the result is a long long AND at least one of the |
||
119 | * operands is cast to long long (usually just prior to the "*" so as |
||
120 | * not to confuse it into thinking it really has a 64-bit operand, |
||
121 | * which, buy the way, it can do, but it take more code and at least 2 |
||
122 | * mpys). |
||
123 | |||
124 | * We also need to be aware that one second in nanoseconds is only a |
||
125 | * couple of bits away from overflowing a 32-bit word, so we MUST use |
||
126 | * 64-bits to get the full range time in nanoseconds. |
||
127 | |||
128 | */ |
||
129 | |||
130 | /* |
||
131 | * Here are the scales we will use. One for seconds, nanoseconds and |
||
132 | * microseconds. |
||
133 | * |
||
134 | * Within the limits of cpp we do a rough cut at the SEC_JIFFIE_SC and |
||
135 | * check if the sign bit is set. If not, we bump the shift count by 1. |
||
136 | * (Gets an extra bit of precision where we can use it.) |
||
137 | * We know it is set for HZ = 1024 and HZ = 100 not for 1000. |
||
138 | * Haven't tested others. |
||
139 | |||
140 | * Limits of cpp (for #if expressions) only long (no long long), but |
||
141 | * then we only need the most signicant bit. |
||
142 | */ |
||
143 | |||
144 | #define SEC_JIFFIE_SC (31 - SHIFT_HZ) |
||
145 | #if !((((NSEC_PER_SEC << 2) / TICK_NSEC) << (SEC_JIFFIE_SC - 2)) & 0x80000000) |
||
146 | #undef SEC_JIFFIE_SC |
||
147 | #define SEC_JIFFIE_SC (32 - SHIFT_HZ) |
||
148 | #endif |
||
149 | #define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29) |
||
150 | #define USEC_JIFFIE_SC (SEC_JIFFIE_SC + 19) |
||
151 | #define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC))\ |
||
152 | / (u64)TICK_NSEC)) |
||
153 | |||
154 | #define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC))\ |
||
155 | / (u64)TICK_NSEC)) |
||
156 | #define USEC_CONVERSION \ |
||
157 | ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC)) \ |
||
158 | / (u64)TICK_NSEC)) |
||
159 | /* |
||
160 | * USEC_ROUND is used in the timeval to jiffie conversion. See there |
||
161 | * for more details. It is the scaled resolution rounding value. Note |
||
162 | * that it is a 64-bit value. Since, when it is applied, we are already |
||
163 | * in jiffies (albit scaled), it is nothing but the bits we will shift |
||
164 | * off. |
||
165 | */ |
||
166 | #define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1) |
||
167 | /* |
||
168 | * The maximum jiffie value is (MAX_INT >> 1). Here we translate that |
||
169 | * into seconds. The 64-bit case will overflow if we are not careful, |
||
170 | * so use the messy SH_DIV macro to do it. Still all constants. |
||
171 | */ |
||
172 | #if BITS_PER_LONG < 64 |
||
173 | # define MAX_SEC_IN_JIFFIES \ |
||
174 | (long)((u64)((u64)MAX_JIFFY_OFFSET * TICK_NSEC) / NSEC_PER_SEC) |
||
175 | #else /* take care of overflow on 64 bits machines */ |
||
176 | # define MAX_SEC_IN_JIFFIES \ |
||
177 | (SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1) |
||
178 | |||
179 | #endif |
||
180 | /* |
||
181 | * The TICK_NSEC - 1 rounds up the value to the next resolution. Note |
||
182 | * that a remainder subtract here would not do the right thing as the |
||
183 | * resolution values don't fall on second boundries. I.e. the line: |
||
184 | * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. |
||
185 | * |
||
186 | * Rather, we just shift the bits off the right. |
||
187 | * |
||
188 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec |
||
189 | * value to a scaled second value. |
||
190 | */ |
||
191 | static __inline__ unsigned long |
||
192 | timespec_to_jiffies(struct timespec *value) |
||
193 | { |
||
194 | unsigned long sec = value->tv_sec; |
||
195 | long nsec = value->tv_nsec + TICK_NSEC - 1; |
||
196 | |||
197 | if (sec >= MAX_SEC_IN_JIFFIES){ |
||
198 | sec = MAX_SEC_IN_JIFFIES; |
||
199 | nsec = 0; |
||
200 | } |
||
201 | return (((u64)sec * SEC_CONVERSION) + |
||
202 | (((u64)nsec * NSEC_CONVERSION) >> |
||
203 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; |
||
204 | |||
205 | } |
||
206 | |||
207 | static __inline__ void |
||
208 | jiffies_to_timespec(unsigned long jiffies, struct timespec *value) |
||
209 | { |
||
210 | /* |
||
211 | * Convert jiffies to nanoseconds and separate with |
||
212 | * one divide. |
||
213 | */ |
||
214 | u64 nsec = (u64)jiffies * TICK_NSEC; |
||
215 | value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec); |
||
216 | } |
||
217 | |||
218 | /* Same for "timeval" |
||
219 | * |
||
220 | * Well, almost. The problem here is that the real system resolution is |
||
221 | * in nanoseconds and the value being converted is in micro seconds. |
||
222 | * Also for some machines (those that use HZ = 1024, in-particular), |
||
223 | * there is a LARGE error in the tick size in microseconds. |
||
224 | |||
225 | * The solution we use is to do the rounding AFTER we convert the |
||
226 | * microsecond part. Thus the USEC_ROUND, the bits to be shifted off. |
||
227 | * Instruction wise, this should cost only an additional add with carry |
||
228 | * instruction above the way it was done above. |
||
229 | */ |
||
230 | static __inline__ unsigned long |
||
231 | timeval_to_jiffies(struct timeval *value) |
||
232 | { |
||
233 | unsigned long sec = value->tv_sec; |
||
234 | long usec = value->tv_usec; |
||
235 | |||
236 | if (sec >= MAX_SEC_IN_JIFFIES){ |
||
237 | sec = MAX_SEC_IN_JIFFIES; |
||
238 | usec = 0; |
||
239 | } |
||
240 | return (((u64)sec * SEC_CONVERSION) + |
||
241 | (((u64)usec * USEC_CONVERSION + USEC_ROUND) >> |
||
242 | (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; |
||
243 | } |
||
244 | |||
245 | static __inline__ void |
||
246 | jiffies_to_timeval(unsigned long jiffies, struct timeval *value) |
||
247 | { |
||
248 | /* |
||
249 | * Convert jiffies to nanoseconds and separate with |
||
250 | * one divide. |
||
251 | */ |
||
252 | u64 nsec = (u64)jiffies * TICK_NSEC; |
||
253 | value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_usec); |
||
254 | value->tv_usec /= NSEC_PER_USEC; |
||
255 | } |
||
256 | |||
257 | static __inline__ int timespec_equal(struct timespec *a, struct timespec *b) |
||
258 | { |
||
259 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
||
260 | } |
||
261 | |||
262 | /* Converts Gregorian date to seconds since 1970-01-01 00:00:00. |
||
263 | * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 |
||
264 | * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. |
||
265 | * |
||
266 | * [For the Julian calendar (which was used in Russia before 1917, |
||
267 | * Britain & colonies before 1752, anywhere else before 1582, |
||
268 | * and is still in use by some communities) leave out the |
||
269 | * -year/100+year/400 terms, and add 10.] |
||
270 | * |
||
271 | * This algorithm was first published by Gauss (I think). |
||
272 | * |
||
273 | * WARNING: this function will overflow on 2106-02-07 06:28:16 on |
||
274 | * machines were long is 32-bit! (However, as time_t is signed, we |
||
275 | * will already get problems at other places on 2038-01-19 03:14:08) |
||
276 | */ |
||
277 | static inline unsigned long |
||
278 | mktime (unsigned int year, unsigned int mon, |
||
279 | unsigned int day, unsigned int hour, |
||
280 | unsigned int min, unsigned int sec) |
||
281 | { |
||
282 | if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */ |
||
283 | mon += 12; /* Puts Feb last since it has leap day */ |
||
284 | year -= 1; |
||
285 | } |
||
286 | |||
287 | return ((( |
||
288 | (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) + |
||
289 | year*365 - 719499 |
||
290 | )*24 + hour /* now have hours */ |
||
291 | )*60 + min /* now have minutes */ |
||
292 | )*60 + sec; /* finally seconds */ |
||
293 | } |
||
294 | |||
295 | extern struct timespec xtime; |
||
296 | extern struct timespec wall_to_monotonic; |
||
297 | extern seqlock_t xtime_lock; |
||
298 | |||
299 | static inline unsigned long get_seconds(void) |
||
300 | { |
||
301 | return xtime.tv_sec; |
||
302 | } |
||
303 | |||
304 | struct timespec current_kernel_time(void); |
||
305 | |||
306 | #define CURRENT_TIME (current_kernel_time()) |
||
307 | |||
308 | #endif /* __KERNEL__ */ |
||
309 | |||
310 | #define NFDBITS __NFDBITS |
||
311 | |||
312 | #ifdef __KERNEL__ |
||
313 | extern void do_gettimeofday(struct timeval *tv); |
||
314 | extern int do_settimeofday(struct timespec *tv); |
||
315 | extern int do_sys_settimeofday(struct timespec *tv, struct timezone *tz); |
||
316 | extern void clock_was_set(void); // call when ever the clock is set |
||
317 | extern int do_posix_clock_monotonic_gettime(struct timespec *tp); |
||
318 | extern long do_nanosleep(struct timespec *t); |
||
319 | extern long do_utimes(char __user * filename, struct timeval * times); |
||
320 | struct itimerval; |
||
321 | extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue); |
||
322 | extern int do_getitimer(int which, struct itimerval *value); |
||
323 | |||
324 | static inline void |
||
325 | set_normalized_timespec (struct timespec *ts, time_t sec, long nsec) |
||
326 | { |
||
327 | while (nsec > NSEC_PER_SEC) { |
||
328 | nsec -= NSEC_PER_SEC; |
||
329 | ++sec; |
||
330 | } |
||
331 | while (nsec < 0) { |
||
332 | nsec += NSEC_PER_SEC; |
||
333 | --sec; |
||
334 | } |
||
335 | ts->tv_sec = sec; |
||
336 | ts->tv_nsec = nsec; |
||
337 | } |
||
338 | #endif |
||
339 | |||
340 | #define FD_SETSIZE __FD_SETSIZE |
||
341 | #define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp) |
||
342 | #define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp) |
||
343 | #define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp) |
||
344 | #define FD_ZERO(fdsetp) __FD_ZERO(fdsetp) |
||
345 | |||
346 | /* |
||
347 | * Names of the interval timers, and structure |
||
348 | * defining a timer setting. |
||
349 | */ |
||
350 | #define ITIMER_REAL 0 |
||
351 | #define ITIMER_VIRTUAL 1 |
||
352 | #define ITIMER_PROF 2 |
||
353 | |||
354 | struct itimerspec { |
||
355 | struct timespec it_interval; /* timer period */ |
||
356 | struct timespec it_value; /* timer expiration */ |
||
357 | }; |
||
358 | |||
359 | struct itimerval { |
||
360 | struct timeval it_interval; /* timer interval */ |
||
361 | struct timeval it_value; /* current value */ |
||
362 | }; |
||
363 | |||
364 | |||
365 | /* |
||
366 | * The IDs of the various system clocks (for POSIX.1b interval timers). |
||
367 | */ |
||
368 | #define CLOCK_REALTIME 0 |
||
369 | #define CLOCK_MONOTONIC 1 |
||
370 | #define CLOCK_PROCESS_CPUTIME_ID 2 |
||
371 | #define CLOCK_THREAD_CPUTIME_ID 3 |
||
372 | #define CLOCK_REALTIME_HR 4 |
||
373 | #define CLOCK_MONOTONIC_HR 5 |
||
374 | |||
375 | #define MAX_CLOCKS 6 |
||
376 | #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC | \ |
||
377 | CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR) |
||
378 | #define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR) |
||
379 | |||
380 | /* |
||
381 | * The various flags for setting POSIX.1b interval timers. |
||
382 | */ |
||
383 | |||
384 | #define TIMER_ABSTIME 0x01 |
||
385 | |||
386 | |||
387 | #endif |