2.2.0-final
[davej-history.git] / kernel / time.c
blobb1347c32f8cdd50bddf4bc0866bb4a050765f49b
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/mm.h>
28 #include <linux/timex.h>
29 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
33 /*
34 * The timezone where the local system is located. Used as a default by some
35 * programs who obtain this value by using gettimeofday.
37 struct timezone sys_tz = { 0, 0};
39 static void do_normal_gettime(struct timeval * tm)
41 *tm=xtime;
44 void (*do_get_fast_time)(struct timeval *) = do_normal_gettime;
47 * Generic way to access 'xtime' (the current time of day).
48 * This can be changed if the platform provides a more accurate (and fast!)
49 * version.
52 void get_fast_time(struct timeval * t)
54 do_get_fast_time(t);
57 #ifndef __alpha__
60 * sys_time() can be implemented in user-level using
61 * sys_gettimeofday(). Is this for backwards compatibility? If so,
62 * why not move it into the appropriate arch directory (for those
63 * architectures that need it).
65 asmlinkage int sys_time(int * tloc)
67 int i;
69 /* SMP: This is fairly trivial. We grab CURRENT_TIME and
70 stuff it to user space. No side effects */
71 i = CURRENT_TIME;
72 if (tloc) {
73 if (put_user(i,tloc))
74 i = -EFAULT;
76 return i;
80 * sys_stime() can be implemented in user-level using
81 * sys_settimeofday(). Is this for backwards compatibility? If so,
82 * why not move it into the appropriate arch directory (for those
83 * architectures that need it).
86 asmlinkage int sys_stime(int * tptr)
88 int value;
90 if (!capable(CAP_SYS_TIME))
91 return -EPERM;
92 if (get_user(value, tptr))
93 return -EFAULT;
94 cli();
95 xtime.tv_sec = value;
96 xtime.tv_usec = 0;
97 time_adjust = 0; /* stop active adjtime() */
98 time_status |= STA_UNSYNC;
99 time_state = TIME_ERROR; /* p. 24, (a) */
100 time_maxerror = NTP_PHASE_LIMIT;
101 time_esterror = NTP_PHASE_LIMIT;
102 sti();
103 return 0;
106 #endif
108 asmlinkage int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
110 if (tv) {
111 struct timeval ktv;
112 do_gettimeofday(&ktv);
113 if (copy_to_user(tv, &ktv, sizeof(ktv)))
114 return -EFAULT;
116 if (tz) {
117 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
118 return -EFAULT;
120 return 0;
124 * Adjust the time obtained from the CMOS to be UTC time instead of
125 * local time.
127 * This is ugly, but preferable to the alternatives. Otherwise we
128 * would either need to write a program to do it in /etc/rc (and risk
129 * confusion if the program gets run more than once; it would also be
130 * hard to make the program warp the clock precisely n hours) or
131 * compile in the timezone information into the kernel. Bad, bad....
133 * - TYT, 1992-01-01
135 * The best thing to do is to keep the CMOS clock in universal time (UTC)
136 * as real UNIX machines always do it. This avoids all headaches about
137 * daylight saving times and warping kernel clocks.
139 inline static void warp_clock(void)
141 cli();
142 xtime.tv_sec += sys_tz.tz_minuteswest * 60;
143 sti();
147 * In case for some reason the CMOS clock has not already been running
148 * in UTC, but in some local time: The first time we set the timezone,
149 * we will warp the clock so that it is ticking UTC time instead of
150 * local time. Presumably, if someone is setting the timezone then we
151 * are running in an environment where the programs understand about
152 * timezones. This should be done at boot time in the /etc/rc script,
153 * as soon as possible, so that the clock can be set right. Otherwise,
154 * various programs will get confused when the clock gets warped.
157 int do_sys_settimeofday(struct timeval *tv, struct timezone *tz)
159 static int firsttime = 1;
161 if (!capable(CAP_SYS_TIME))
162 return -EPERM;
164 if (tz) {
165 /* SMP safe, global irq locking makes it work. */
166 sys_tz = *tz;
167 if (firsttime) {
168 firsttime = 0;
169 if (!tv)
170 warp_clock();
173 if (tv)
175 /* SMP safe, again the code in arch/foo/time.c should
176 * globally block out interrupts when it runs.
178 do_settimeofday(tv);
180 return 0;
183 asmlinkage int sys_settimeofday(struct timeval *tv, struct timezone *tz)
185 struct timeval new_tv;
186 struct timezone new_tz;
188 if (tv) {
189 if (copy_from_user(&new_tv, tv, sizeof(*tv)))
190 return -EFAULT;
192 if (tz) {
193 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
194 return -EFAULT;
197 return do_sys_settimeofday(tv ? &new_tv : NULL, tz ? &new_tz : NULL);
200 long pps_offset = 0; /* pps time offset (us) */
201 long pps_jitter = MAXTIME; /* time dispersion (jitter) (us) */
203 long pps_freq = 0; /* frequency offset (scaled ppm) */
204 long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */
206 long pps_valid = PPS_VALID; /* pps signal watchdog counter */
208 int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */
210 long pps_jitcnt = 0; /* jitter limit exceeded */
211 long pps_calcnt = 0; /* calibration intervals */
212 long pps_errcnt = 0; /* calibration errors */
213 long pps_stbcnt = 0; /* stability limit exceeded */
215 /* hook for a loadable hardpps kernel module */
216 void (*hardpps_ptr)(struct timeval *) = (void (*)(struct timeval *))0;
218 /* adjtimex mainly allows reading (and writing, if superuser) of
219 * kernel time-keeping variables. used by xntpd.
221 int do_adjtimex(struct timex *txc)
223 long ltemp, mtemp, save_adjust;
224 int error = 0;
226 /* In order to modify anything, you gotta be super-user! */
227 if (txc->modes && !capable(CAP_SYS_TIME))
228 return -EPERM;
230 /* Now we validate the data before disabling interrupts */
232 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
233 /* adjustment Offset limited to +- .512 seconds */
234 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
235 return -EINVAL;
237 /* if the quartz is off by more than 10% something is VERY wrong ! */
238 if (txc->modes & ADJ_TICK)
239 if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ)
240 return -EINVAL;
242 cli(); /* SMP: global cli() is enough protection. */
244 /* Save for later - semantics of adjtime is to return old value */
245 save_adjust = time_adjust;
247 #if 0 /* STA_CLOCKERR is never set yet */
248 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
249 #endif
250 /* If there are input parameters, then process them */
251 if (txc->modes)
253 if (time_state == TIME_ERROR)
254 time_state = TIME_OK; /* reset error -- why? */
256 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
257 time_status = (txc->status & ~STA_RONLY) |
258 (time_status & STA_RONLY);
260 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
261 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
262 error = -EINVAL;
263 goto leave;
265 time_freq = txc->freq - pps_freq;
268 if (txc->modes & ADJ_MAXERROR) {
269 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
270 error = -EINVAL;
271 goto leave;
273 time_maxerror = txc->maxerror;
276 if (txc->modes & ADJ_ESTERROR) {
277 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
278 error = -EINVAL;
279 goto leave;
281 time_esterror = txc->esterror;
284 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
285 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
286 error = -EINVAL;
287 goto leave;
289 time_constant = txc->constant;
292 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
293 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
294 /* adjtime() is independent from ntp_adjtime() */
295 time_adjust = txc->offset;
297 else if ( time_status & (STA_PLL | STA_PPSTIME) ) {
298 ltemp = (time_status & (STA_PPSTIME | STA_PPSSIGNAL)) ==
299 (STA_PPSTIME | STA_PPSSIGNAL) ?
300 pps_offset : txc->offset;
303 * Scale the phase adjustment and
304 * clamp to the operating range.
306 if (ltemp > MAXPHASE)
307 time_offset = MAXPHASE << SHIFT_UPDATE;
308 else if (ltemp < -MAXPHASE)
309 time_offset = -(MAXPHASE << SHIFT_UPDATE);
310 else
311 time_offset = ltemp << SHIFT_UPDATE;
314 * Select whether the frequency is to be controlled
315 * and in which mode (PLL or FLL). Clamp to the operating
316 * range. Ugly multiply/divide should be replaced someday.
319 if (time_status & STA_FREQHOLD || time_reftime == 0)
320 time_reftime = xtime.tv_sec;
321 mtemp = xtime.tv_sec - time_reftime;
322 time_reftime = xtime.tv_sec;
323 if (time_status & STA_FLL) {
324 if (mtemp >= MINSEC) {
325 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
326 SHIFT_UPDATE);
327 if (ltemp < 0)
328 time_freq -= -ltemp >> SHIFT_KH;
329 else
330 time_freq += ltemp >> SHIFT_KH;
331 } else /* calibration interval too short (p. 12) */
332 time_state = TIME_ERROR;
333 } else { /* PLL mode */
334 if (mtemp < MAXSEC) {
335 ltemp *= mtemp;
336 if (ltemp < 0)
337 time_freq -= -ltemp >> (time_constant +
338 time_constant +
339 SHIFT_KF - SHIFT_USEC);
340 else
341 time_freq += ltemp >> (time_constant +
342 time_constant +
343 SHIFT_KF - SHIFT_USEC);
344 } else /* calibration interval too long (p. 12) */
345 time_state = TIME_ERROR;
347 if (time_freq > time_tolerance)
348 time_freq = time_tolerance;
349 else if (time_freq < -time_tolerance)
350 time_freq = -time_tolerance;
351 } /* STA_PLL || STA_PPSTIME */
352 } /* txc->modes & ADJ_OFFSET */
353 if (txc->modes & ADJ_TICK) {
354 /* if the quartz is off by more than 10% something is
355 VERY wrong ! */
356 if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ) {
357 error = -EINVAL;
358 goto leave;
360 tick = txc->tick;
362 } /* txc->modes */
363 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
364 || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
365 && (time_status & STA_PPSSIGNAL) == 0)
366 /* p. 24, (b) */
367 || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
368 == (STA_PPSTIME|STA_PPSJITTER))
369 /* p. 24, (c) */
370 || ((time_status & STA_PPSFREQ) != 0
371 && (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
372 /* p. 24, (d) */
373 time_state = TIME_ERROR;
375 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
376 txc->offset = save_adjust;
377 else {
378 if (time_offset < 0)
379 txc->offset = -(-time_offset >> SHIFT_UPDATE);
380 else
381 txc->offset = time_offset >> SHIFT_UPDATE;
383 txc->freq = time_freq + pps_freq;
384 txc->maxerror = time_maxerror;
385 txc->esterror = time_esterror;
386 txc->status = time_status;
387 txc->constant = time_constant;
388 txc->precision = time_precision;
389 txc->tolerance = time_tolerance;
390 do_gettimeofday(&txc->time);
391 txc->tick = tick;
392 txc->ppsfreq = pps_freq;
393 txc->jitter = pps_jitter >> PPS_AVG;
394 txc->shift = pps_shift;
395 txc->stabil = pps_stabil;
396 txc->jitcnt = pps_jitcnt;
397 txc->calcnt = pps_calcnt;
398 txc->errcnt = pps_errcnt;
399 txc->stbcnt = pps_stbcnt;
401 sti();
402 return(error < 0 ? error : time_state);
405 asmlinkage int sys_adjtimex(struct timex *txc_p)
407 struct timex txc; /* Local copy of parameter */
408 int ret;
410 /* Copy the user data space into the kernel copy
411 * structure. But bear in mind that the structures
412 * may change
414 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
415 return -EFAULT;
416 ret = do_adjtimex(&txc);
417 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;