5 #include <linux/types.h>
7 #ifndef _STRUCT_TIMESPEC
8 #define _STRUCT_TIMESPEC
10 time_t tv_sec; /* seconds */
11 long tv_nsec; /* nanoseconds */
13 #endif /* _STRUCT_TIMESPEC */
16 time_t tv_sec; /* seconds */
17 suseconds_t tv_usec; /* microseconds */
21 int tz_minuteswest; /* minutes west of Greenwich */
22 int tz_dsttime; /* type of dst correction */
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
33 #define div_long_long_rem(dividend,divisor,remainder) ({ \
34 u64 result = dividend; \
35 *remainder = do_div(result,divisor); \
41 * Have the 32 bit jiffies value wrap 5 minutes after boot
42 * so jiffies wrap bugs show up earlier.
44 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
47 * Change timeval to jiffies, trying to avoid the
48 * most obvious overflows..
50 * And some not so obvious.
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
58 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
60 /* Parameters used to convert the timespec values */
62 #define USEC_PER_SEC (1000000L)
66 #define NSEC_PER_SEC (1000000000L)
70 #define NSEC_PER_USEC (1000L)
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.
83 * Scaled math??? What is that?
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.
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).
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;
106 * #define SEC_CONV = ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE))
107 * jiff = (sec * SEC_CONV) >> SCALE;
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).
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
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.
131 * Here are the scales we will use. One for seconds, nanoseconds and
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.
140 * Limits of cpp (for #if expressions) only long (no long long), but
141 * then we only need the most signicant bit.
144 #define SEC_JIFFIE_SC (31 - SHIFT_HZ)
145 #if !((((NSEC_PER_SEC << 2) / TICK_NSEC) << (SEC_JIFFIE_SC - 2)) & 0x80000000)
147 #define SEC_JIFFIE_SC (32 - SHIFT_HZ)
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 TICK_NSEC -1) / (u64)TICK_NSEC))
154 #define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\
155 TICK_NSEC -1) / (u64)TICK_NSEC))
156 #define USEC_CONVERSION \
157 ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC) +\
158 TICK_NSEC -1) / (u64)TICK_NSEC))
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
166 #define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1)
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.
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)
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.
186 * Rather, we just shift the bits off the right.
188 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
189 * value to a scaled second value.
191 static __inline__ unsigned long
192 timespec_to_jiffies(struct timespec *value)
194 unsigned long sec = value->tv_sec;
195 long nsec = value->tv_nsec + TICK_NSEC - 1;
197 if (sec >= MAX_SEC_IN_JIFFIES){
198 sec = MAX_SEC_IN_JIFFIES;
201 return (((u64)sec * SEC_CONVERSION) +
202 (((u64)nsec * NSEC_CONVERSION) >>
203 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
207 static __inline__ void
208 jiffies_to_timespec(unsigned long jiffies, struct timespec *value)
211 * Convert jiffies to nanoseconds and separate with
214 u64 nsec = (u64)jiffies * TICK_NSEC;
215 value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec);
218 /* Same for "timeval"
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.
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.
230 static __inline__ unsigned long
231 timeval_to_jiffies(struct timeval *value)
233 unsigned long sec = value->tv_sec;
234 long usec = value->tv_usec;
236 if (sec >= MAX_SEC_IN_JIFFIES){
237 sec = MAX_SEC_IN_JIFFIES;
240 return (((u64)sec * SEC_CONVERSION) +
241 (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
242 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
245 static __inline__ void
246 jiffies_to_timeval(unsigned long jiffies, struct timeval *value)
249 * Convert jiffies to nanoseconds and separate with
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;
257 static __inline__ int timespec_equal(struct timespec *a, struct timespec *b)
259 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
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.
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.]
271 * This algorithm was first published by Gauss (I think).
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)
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)
282 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
283 mon += 12; /* Puts Feb last since it has leap day */
288 (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
290 )*24 + hour /* now have hours */
291 )*60 + min /* now have minutes */
292 )*60 + sec; /* finally seconds */
295 extern struct timespec xtime;
296 extern struct timespec wall_to_monotonic;
297 extern seqlock_t xtime_lock;
299 static inline unsigned long get_seconds(void)
304 struct timespec current_kernel_time(void);
306 #define CURRENT_TIME (current_kernel_time())
308 #endif /* __KERNEL__ */
310 #define NFDBITS __NFDBITS
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);
321 extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);
322 extern int do_getitimer(int which, struct itimerval *value);
325 set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
327 while (nsec > NSEC_PER_SEC) {
328 nsec -= NSEC_PER_SEC;
332 nsec += NSEC_PER_SEC;
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)
347 * Names of the interval timers, and structure
348 * defining a timer setting.
350 #define ITIMER_REAL 0
351 #define ITIMER_VIRTUAL 1
352 #define ITIMER_PROF 2
355 struct timespec it_interval; /* timer period */
356 struct timespec it_value; /* timer expiration */
360 struct timeval it_interval; /* timer interval */
361 struct timeval it_value; /* current value */
366 * The IDs of the various system clocks (for POSIX.1b interval timers).
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
376 #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC | \
377 CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR)
378 #define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR)
381 * The various flags for setting POSIX.1b interval timers.
384 #define TIMER_ABSTIME 0x01