Import 2.3.9
[davej-history.git] / kernel / time.c
blob911442dad460c4c0a032b2f835288a4c41cfa1ca
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 #if !defined(__alpha__) && !defined(__ia64__)
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 * XXX This function is NOT 64-bit clean!
67 asmlinkage int sys_time(int * tloc)
69 int i;
71 /* SMP: This is fairly trivial. We grab CURRENT_TIME and
72 stuff it to user space. No side effects */
73 i = CURRENT_TIME;
74 if (tloc) {
75 if (put_user(i,tloc))
76 i = -EFAULT;
78 return i;
82 * sys_stime() can be implemented in user-level using
83 * sys_settimeofday(). Is this for backwards compatibility? If so,
84 * why not move it into the appropriate arch directory (for those
85 * architectures that need it).
88 asmlinkage int sys_stime(int * tptr)
90 int value;
92 if (!capable(CAP_SYS_TIME))
93 return -EPERM;
94 if (get_user(value, tptr))
95 return -EFAULT;
96 cli();
97 xtime.tv_sec = value;
98 xtime.tv_usec = 0;
99 time_adjust = 0; /* stop active adjtime() */
100 time_status |= STA_UNSYNC;
101 time_maxerror = NTP_PHASE_LIMIT;
102 time_esterror = NTP_PHASE_LIMIT;
103 sti();
104 return 0;
107 #endif
109 asmlinkage int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
111 if (tv) {
112 struct timeval ktv;
113 do_gettimeofday(&ktv);
114 if (copy_to_user(tv, &ktv, sizeof(ktv)))
115 return -EFAULT;
117 if (tz) {
118 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
119 return -EFAULT;
121 return 0;
125 * Adjust the time obtained from the CMOS to be UTC time instead of
126 * local time.
128 * This is ugly, but preferable to the alternatives. Otherwise we
129 * would either need to write a program to do it in /etc/rc (and risk
130 * confusion if the program gets run more than once; it would also be
131 * hard to make the program warp the clock precisely n hours) or
132 * compile in the timezone information into the kernel. Bad, bad....
134 * - TYT, 1992-01-01
136 * The best thing to do is to keep the CMOS clock in universal time (UTC)
137 * as real UNIX machines always do it. This avoids all headaches about
138 * daylight saving times and warping kernel clocks.
140 inline static void warp_clock(void)
142 cli();
143 xtime.tv_sec += sys_tz.tz_minuteswest * 60;
144 sti();
148 * In case for some reason the CMOS clock has not already been running
149 * in UTC, but in some local time: The first time we set the timezone,
150 * we will warp the clock so that it is ticking UTC time instead of
151 * local time. Presumably, if someone is setting the timezone then we
152 * are running in an environment where the programs understand about
153 * timezones. This should be done at boot time in the /etc/rc script,
154 * as soon as possible, so that the clock can be set right. Otherwise,
155 * various programs will get confused when the clock gets warped.
158 int do_sys_settimeofday(struct timeval *tv, struct timezone *tz)
160 static int firsttime = 1;
162 if (!capable(CAP_SYS_TIME))
163 return -EPERM;
165 if (tz) {
166 /* SMP safe, global irq locking makes it work. */
167 sys_tz = *tz;
168 if (firsttime) {
169 firsttime = 0;
170 if (!tv)
171 warp_clock();
174 if (tv)
176 /* SMP safe, again the code in arch/foo/time.c should
177 * globally block out interrupts when it runs.
179 do_settimeofday(tv);
181 return 0;
184 asmlinkage int sys_settimeofday(struct timeval *tv, struct timezone *tz)
186 struct timeval new_tv;
187 struct timezone new_tz;
189 if (tv) {
190 if (copy_from_user(&new_tv, tv, sizeof(*tv)))
191 return -EFAULT;
193 if (tz) {
194 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
195 return -EFAULT;
198 return do_sys_settimeofday(tv ? &new_tv : NULL, tz ? &new_tz : NULL);
201 long pps_offset = 0; /* pps time offset (us) */
202 long pps_jitter = MAXTIME; /* time dispersion (jitter) (us) */
204 long pps_freq = 0; /* frequency offset (scaled ppm) */
205 long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */
207 long pps_valid = PPS_VALID; /* pps signal watchdog counter */
209 int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */
211 long pps_jitcnt = 0; /* jitter limit exceeded */
212 long pps_calcnt = 0; /* calibration intervals */
213 long pps_errcnt = 0; /* calibration errors */
214 long pps_stbcnt = 0; /* stability limit exceeded */
216 /* hook for a loadable hardpps kernel module */
217 void (*hardpps_ptr)(struct timeval *) = (void (*)(struct timeval *))0;
219 /* adjtimex mainly allows reading (and writing, if superuser) of
220 * kernel time-keeping variables. used by xntpd.
222 int do_adjtimex(struct timex *txc)
224 long ltemp, mtemp, save_adjust;
225 int result = time_state; /* mostly `TIME_OK' */
227 /* In order to modify anything, you gotta be super-user! */
228 if (txc->modes && !capable(CAP_SYS_TIME))
229 return -EPERM;
231 /* Now we validate the data before disabling interrupts */
233 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
234 /* adjustment Offset limited to +- .512 seconds */
235 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
236 return -EINVAL;
238 /* if the quartz is off by more than 10% something is VERY wrong ! */
239 if (txc->modes & ADJ_TICK)
240 if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ)
241 return -EINVAL;
243 cli(); /* SMP: global cli() is enough protection. */
245 /* Save for later - semantics of adjtime is to return old value */
246 save_adjust = time_adjust;
248 #if 0 /* STA_CLOCKERR is never set yet */
249 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
250 #endif
251 /* If there are input parameters, then process them */
252 if (txc->modes)
254 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
255 time_status = (txc->status & ~STA_RONLY) |
256 (time_status & STA_RONLY);
258 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
259 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
260 result = -EINVAL;
261 goto leave;
263 time_freq = txc->freq - pps_freq;
266 if (txc->modes & ADJ_MAXERROR) {
267 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
268 result = -EINVAL;
269 goto leave;
271 time_maxerror = txc->maxerror;
274 if (txc->modes & ADJ_ESTERROR) {
275 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
276 result = -EINVAL;
277 goto leave;
279 time_esterror = txc->esterror;
282 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
283 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
284 result = -EINVAL;
285 goto leave;
287 time_constant = txc->constant;
290 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
291 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
292 /* adjtime() is independent from ntp_adjtime() */
293 time_adjust = txc->offset;
295 else if ( time_status & (STA_PLL | STA_PPSTIME) ) {
296 ltemp = (time_status & (STA_PPSTIME | STA_PPSSIGNAL)) ==
297 (STA_PPSTIME | STA_PPSSIGNAL) ?
298 pps_offset : txc->offset;
301 * Scale the phase adjustment and
302 * clamp to the operating range.
304 if (ltemp > MAXPHASE)
305 time_offset = MAXPHASE << SHIFT_UPDATE;
306 else if (ltemp < -MAXPHASE)
307 time_offset = -(MAXPHASE << SHIFT_UPDATE);
308 else
309 time_offset = ltemp << SHIFT_UPDATE;
312 * Select whether the frequency is to be controlled
313 * and in which mode (PLL or FLL). Clamp to the operating
314 * range. Ugly multiply/divide should be replaced someday.
317 if (time_status & STA_FREQHOLD || time_reftime == 0)
318 time_reftime = xtime.tv_sec;
319 mtemp = xtime.tv_sec - time_reftime;
320 time_reftime = xtime.tv_sec;
321 if (time_status & STA_FLL) {
322 if (mtemp >= MINSEC) {
323 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
324 SHIFT_UPDATE);
325 if (ltemp < 0)
326 time_freq -= -ltemp >> SHIFT_KH;
327 else
328 time_freq += ltemp >> SHIFT_KH;
329 } else /* calibration interval too short (p. 12) */
330 result = TIME_ERROR;
331 } else { /* PLL mode */
332 if (mtemp < MAXSEC) {
333 ltemp *= mtemp;
334 if (ltemp < 0)
335 time_freq -= -ltemp >> (time_constant +
336 time_constant +
337 SHIFT_KF - SHIFT_USEC);
338 else
339 time_freq += ltemp >> (time_constant +
340 time_constant +
341 SHIFT_KF - SHIFT_USEC);
342 } else /* calibration interval too long (p. 12) */
343 result = TIME_ERROR;
345 if (time_freq > time_tolerance)
346 time_freq = time_tolerance;
347 else if (time_freq < -time_tolerance)
348 time_freq = -time_tolerance;
349 } /* STA_PLL || STA_PPSTIME */
350 } /* txc->modes & ADJ_OFFSET */
351 if (txc->modes & ADJ_TICK) {
352 /* if the quartz is off by more than 10% something is
353 VERY wrong ! */
354 if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ) {
355 result = -EINVAL;
356 goto leave;
358 tick = txc->tick;
360 } /* txc->modes */
361 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
362 || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
363 && (time_status & STA_PPSSIGNAL) == 0)
364 /* p. 24, (b) */
365 || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
366 == (STA_PPSTIME|STA_PPSJITTER))
367 /* p. 24, (c) */
368 || ((time_status & STA_PPSFREQ) != 0
369 && (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
370 /* p. 24, (d) */
371 result = TIME_ERROR;
373 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
374 txc->offset = save_adjust;
375 else {
376 if (time_offset < 0)
377 txc->offset = -(-time_offset >> SHIFT_UPDATE);
378 else
379 txc->offset = time_offset >> SHIFT_UPDATE;
381 txc->freq = time_freq + pps_freq;
382 txc->maxerror = time_maxerror;
383 txc->esterror = time_esterror;
384 txc->status = time_status;
385 txc->constant = time_constant;
386 txc->precision = time_precision;
387 txc->tolerance = time_tolerance;
388 do_gettimeofday(&txc->time);
389 txc->tick = tick;
390 txc->ppsfreq = pps_freq;
391 txc->jitter = pps_jitter >> PPS_AVG;
392 txc->shift = pps_shift;
393 txc->stabil = pps_stabil;
394 txc->jitcnt = pps_jitcnt;
395 txc->calcnt = pps_calcnt;
396 txc->errcnt = pps_errcnt;
397 txc->stbcnt = pps_stbcnt;
399 sti();
400 return(result);
403 asmlinkage int sys_adjtimex(struct timex *txc_p)
405 struct timex txc; /* Local copy of parameter */
406 int ret;
408 /* Copy the user data space into the kernel copy
409 * structure. But bear in mind that the structures
410 * may change
412 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
413 return -EFAULT;
414 ret = do_adjtimex(&txc);
415 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;