[PATCH] v4l: videobuf update
[linux-2.6/history.git] / kernel / time.c
blobfd23f6774b56fe389ae2e5ca5e4904a14a9a3f85
1 /*
2 * linux/kernel/time.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * This file contains the interface functions for the various
7 * time related system calls: time, stime, gettimeofday, settimeofday,
8 * adjtime
9 */
11 * Modification history kernel/time.c
13 * 1993-09-02 Philip Gladstone
14 * Created file with time related functions from sched.c and adjtimex()
15 * 1993-10-08 Torsten Duwe
16 * adjtime interface update and CMOS clock write code
17 * 1995-08-13 Torsten Duwe
18 * kernel PLL updated to 1994-12-13 specs (rfc-1589)
19 * 1999-01-16 Ulrich Windl
20 * Introduced error checking for many cases in adjtimex().
21 * Updated NTP code according to technical memorandum Jan '96
22 * "A Kernel Model for Precision Timekeeping" by Dave Mills
23 * Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
24 * (Even though the technical memorandum forbids it)
27 #include <linux/timex.h>
28 #include <linux/errno.h>
29 #include <linux/smp_lock.h>
30 #include <asm/uaccess.h>
32 /*
33 * The timezone where the local system is located. Used as a default by some
34 * programs who obtain this value by using gettimeofday.
36 struct timezone sys_tz;
38 #if !defined(__alpha__) && !defined(__ia64__)
41 * sys_time() can be implemented in user-level using
42 * sys_gettimeofday(). Is this for backwards compatibility? If so,
43 * why not move it into the appropriate arch directory (for those
44 * architectures that need it).
46 * XXX This function is NOT 64-bit clean!
48 asmlinkage long sys_time(int * tloc)
50 int i;
52 /* SMP: This is fairly trivial. We grab CURRENT_TIME and
53 stuff it to user space. No side effects */
54 i = get_seconds();
55 if (tloc) {
56 if (put_user(i,tloc))
57 i = -EFAULT;
59 return i;
63 * sys_stime() can be implemented in user-level using
64 * sys_settimeofday(). Is this for backwards compatibility? If so,
65 * why not move it into the appropriate arch directory (for those
66 * architectures that need it).
69 asmlinkage long sys_stime(time_t *tptr)
71 struct timespec tv;
73 if (!capable(CAP_SYS_TIME))
74 return -EPERM;
75 if (get_user(tv.tv_sec, tptr))
76 return -EFAULT;
78 tv.tv_nsec = 0;
79 do_settimeofday(&tv);
80 return 0;
83 #endif
85 asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz)
87 if (likely(tv != NULL)) {
88 struct timeval ktv;
89 do_gettimeofday(&ktv);
90 if (copy_to_user(tv, &ktv, sizeof(ktv)))
91 return -EFAULT;
93 if (unlikely(tz != NULL)) {
94 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
95 return -EFAULT;
97 return 0;
101 * Adjust the time obtained from the CMOS to be UTC time instead of
102 * local time.
104 * This is ugly, but preferable to the alternatives. Otherwise we
105 * would either need to write a program to do it in /etc/rc (and risk
106 * confusion if the program gets run more than once; it would also be
107 * hard to make the program warp the clock precisely n hours) or
108 * compile in the timezone information into the kernel. Bad, bad....
110 * - TYT, 1992-01-01
112 * The best thing to do is to keep the CMOS clock in universal time (UTC)
113 * as real UNIX machines always do it. This avoids all headaches about
114 * daylight saving times and warping kernel clocks.
116 inline static void warp_clock(void)
118 write_seqlock_irq(&xtime_lock);
119 wall_to_monotonic.tv_sec -= sys_tz.tz_minuteswest * 60;
120 xtime.tv_sec += sys_tz.tz_minuteswest * 60;
121 time_interpolator_update(sys_tz.tz_minuteswest * 60 * NSEC_PER_SEC);
122 write_sequnlock_irq(&xtime_lock);
123 clock_was_set();
127 * In case for some reason the CMOS clock has not already been running
128 * in UTC, but in some local time: The first time we set the timezone,
129 * we will warp the clock so that it is ticking UTC time instead of
130 * local time. Presumably, if someone is setting the timezone then we
131 * are running in an environment where the programs understand about
132 * timezones. This should be done at boot time in the /etc/rc script,
133 * as soon as possible, so that the clock can be set right. Otherwise,
134 * various programs will get confused when the clock gets warped.
137 int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)
139 static int firsttime = 1;
141 if (!capable(CAP_SYS_TIME))
142 return -EPERM;
144 if (tz) {
145 /* SMP safe, global irq locking makes it work. */
146 sys_tz = *tz;
147 if (firsttime) {
148 firsttime = 0;
149 if (!tv)
150 warp_clock();
153 if (tv)
155 /* SMP safe, again the code in arch/foo/time.c should
156 * globally block out interrupts when it runs.
158 return do_settimeofday(tv);
160 return 0;
163 asmlinkage long sys_settimeofday(struct timeval __user *tv,
164 struct timezone __user *tz)
166 struct timeval user_tv;
167 struct timespec new_ts;
168 struct timezone new_tz;
170 if (tv) {
171 if (copy_from_user(&user_tv, tv, sizeof(*tv)))
172 return -EFAULT;
173 new_ts.tv_sec = user_tv.tv_sec;
174 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
176 if (tz) {
177 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
178 return -EFAULT;
181 return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
184 long pps_offset; /* pps time offset (us) */
185 long pps_jitter = MAXTIME; /* time dispersion (jitter) (us) */
187 long pps_freq; /* frequency offset (scaled ppm) */
188 long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */
190 long pps_valid = PPS_VALID; /* pps signal watchdog counter */
192 int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */
194 long pps_jitcnt; /* jitter limit exceeded */
195 long pps_calcnt; /* calibration intervals */
196 long pps_errcnt; /* calibration errors */
197 long pps_stbcnt; /* stability limit exceeded */
199 /* hook for a loadable hardpps kernel module */
200 void (*hardpps_ptr)(struct timeval *);
202 /* adjtimex mainly allows reading (and writing, if superuser) of
203 * kernel time-keeping variables. used by xntpd.
205 int do_adjtimex(struct timex *txc)
207 long ltemp, mtemp, save_adjust;
208 int result;
210 /* In order to modify anything, you gotta be super-user! */
211 if (txc->modes && !capable(CAP_SYS_TIME))
212 return -EPERM;
214 /* Now we validate the data before disabling interrupts */
216 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
217 /* singleshot must not be used with any other mode bits */
218 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
219 return -EINVAL;
221 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
222 /* adjustment Offset limited to +- .512 seconds */
223 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
224 return -EINVAL;
226 /* if the quartz is off by more than 10% something is VERY wrong ! */
227 if (txc->modes & ADJ_TICK)
228 if (txc->tick < 900000/USER_HZ ||
229 txc->tick > 1100000/USER_HZ)
230 return -EINVAL;
232 write_seqlock_irq(&xtime_lock);
233 result = time_state; /* mostly `TIME_OK' */
235 /* Save for later - semantics of adjtime is to return old value */
236 save_adjust = time_adjust;
238 #if 0 /* STA_CLOCKERR is never set yet */
239 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
240 #endif
241 /* If there are input parameters, then process them */
242 if (txc->modes)
244 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
245 time_status = (txc->status & ~STA_RONLY) |
246 (time_status & STA_RONLY);
248 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
249 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
250 result = -EINVAL;
251 goto leave;
253 time_freq = txc->freq - pps_freq;
256 if (txc->modes & ADJ_MAXERROR) {
257 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
258 result = -EINVAL;
259 goto leave;
261 time_maxerror = txc->maxerror;
264 if (txc->modes & ADJ_ESTERROR) {
265 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
266 result = -EINVAL;
267 goto leave;
269 time_esterror = txc->esterror;
272 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
273 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
274 result = -EINVAL;
275 goto leave;
277 time_constant = txc->constant;
280 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
281 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
282 /* adjtime() is independent from ntp_adjtime() */
283 time_adjust = txc->offset;
285 else if ( time_status & (STA_PLL | STA_PPSTIME) ) {
286 ltemp = (time_status & (STA_PPSTIME | STA_PPSSIGNAL)) ==
287 (STA_PPSTIME | STA_PPSSIGNAL) ?
288 pps_offset : txc->offset;
291 * Scale the phase adjustment and
292 * clamp to the operating range.
294 if (ltemp > MAXPHASE)
295 time_offset = MAXPHASE << SHIFT_UPDATE;
296 else if (ltemp < -MAXPHASE)
297 time_offset = -(MAXPHASE << SHIFT_UPDATE);
298 else
299 time_offset = ltemp << SHIFT_UPDATE;
302 * Select whether the frequency is to be controlled
303 * and in which mode (PLL or FLL). Clamp to the operating
304 * range. Ugly multiply/divide should be replaced someday.
307 if (time_status & STA_FREQHOLD || time_reftime == 0)
308 time_reftime = xtime.tv_sec;
309 mtemp = xtime.tv_sec - time_reftime;
310 time_reftime = xtime.tv_sec;
311 if (time_status & STA_FLL) {
312 if (mtemp >= MINSEC) {
313 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
314 SHIFT_UPDATE);
315 if (ltemp < 0)
316 time_freq -= -ltemp >> SHIFT_KH;
317 else
318 time_freq += ltemp >> SHIFT_KH;
319 } else /* calibration interval too short (p. 12) */
320 result = TIME_ERROR;
321 } else { /* PLL mode */
322 if (mtemp < MAXSEC) {
323 ltemp *= mtemp;
324 if (ltemp < 0)
325 time_freq -= -ltemp >> (time_constant +
326 time_constant +
327 SHIFT_KF - SHIFT_USEC);
328 else
329 time_freq += ltemp >> (time_constant +
330 time_constant +
331 SHIFT_KF - SHIFT_USEC);
332 } else /* calibration interval too long (p. 12) */
333 result = TIME_ERROR;
335 if (time_freq > time_tolerance)
336 time_freq = time_tolerance;
337 else if (time_freq < -time_tolerance)
338 time_freq = -time_tolerance;
339 } /* STA_PLL || STA_PPSTIME */
340 } /* txc->modes & ADJ_OFFSET */
341 if (txc->modes & ADJ_TICK) {
342 tick_usec = txc->tick;
343 tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
345 } /* txc->modes */
346 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
347 || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
348 && (time_status & STA_PPSSIGNAL) == 0)
349 /* p. 24, (b) */
350 || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
351 == (STA_PPSTIME|STA_PPSJITTER))
352 /* p. 24, (c) */
353 || ((time_status & STA_PPSFREQ) != 0
354 && (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
355 /* p. 24, (d) */
356 result = TIME_ERROR;
358 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
359 txc->offset = save_adjust;
360 else {
361 if (time_offset < 0)
362 txc->offset = -(-time_offset >> SHIFT_UPDATE);
363 else
364 txc->offset = time_offset >> SHIFT_UPDATE;
366 txc->freq = time_freq + pps_freq;
367 txc->maxerror = time_maxerror;
368 txc->esterror = time_esterror;
369 txc->status = time_status;
370 txc->constant = time_constant;
371 txc->precision = time_precision;
372 txc->tolerance = time_tolerance;
373 txc->tick = tick_usec;
374 txc->ppsfreq = pps_freq;
375 txc->jitter = pps_jitter >> PPS_AVG;
376 txc->shift = pps_shift;
377 txc->stabil = pps_stabil;
378 txc->jitcnt = pps_jitcnt;
379 txc->calcnt = pps_calcnt;
380 txc->errcnt = pps_errcnt;
381 txc->stbcnt = pps_stbcnt;
382 write_sequnlock_irq(&xtime_lock);
383 do_gettimeofday(&txc->time);
384 return(result);
387 asmlinkage long sys_adjtimex(struct timex __user *txc_p)
389 struct timex txc; /* Local copy of parameter */
390 int ret;
392 /* Copy the user data space into the kernel copy
393 * structure. But bear in mind that the structures
394 * may change
396 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
397 return -EFAULT;
398 ret = do_adjtimex(&txc);
399 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
402 struct timespec current_kernel_time(void)
404 struct timespec now;
405 unsigned long seq;
407 do {
408 seq = read_seqbegin(&xtime_lock);
410 now = xtime;
411 } while (read_seqretry(&xtime_lock, seq));
413 return now;
416 #if (BITS_PER_LONG < 64)
417 u64 get_jiffies_64(void)
419 unsigned long seq;
420 u64 ret;
422 do {
423 seq = read_seqbegin(&xtime_lock);
424 ret = jiffies_64;
425 } while (read_seqretry(&xtime_lock, seq));
426 return ret;
428 #endif