patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / kernel / time.c
1 /*
2  *  linux/kernel/time.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  This file contains the interface functions for the various
7  *  time related system calls: time, stime, gettimeofday, settimeofday,
8  *                             adjtime
9  */
10 /*
11  * Modification history kernel/time.c
12  * 
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)
25  */
26
27 #include <linux/module.h>
28 #include <linux/timex.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <asm/unistd.h>
33
34 /* 
35  * The timezone where the local system is located.  Used as a default by some
36  * programs who obtain this value by using gettimeofday.
37  */
38 struct timezone sys_tz;
39
40 EXPORT_SYMBOL(sys_tz);
41
42 #ifdef __ARCH_WANT_SYS_TIME
43
44 /*
45  * sys_time() can be implemented in user-level using
46  * sys_gettimeofday().  Is this for backwards compatibility?  If so,
47  * why not move it into the appropriate arch directory (for those
48  * architectures that need it).
49  *
50  * XXX This function is NOT 64-bit clean!
51  */
52 asmlinkage long sys_time(int __user * tloc)
53 {
54         int i;
55         struct timeval tv;
56
57         do_gettimeofday(&tv);
58         i = tv.tv_sec;
59
60         if (tloc) {
61                 if (put_user(i,tloc))
62                         i = -EFAULT;
63         }
64         return i;
65 }
66
67 /*
68  * sys_stime() can be implemented in user-level using
69  * sys_settimeofday().  Is this for backwards compatibility?  If so,
70  * why not move it into the appropriate arch directory (for those
71  * architectures that need it).
72  */
73  
74 asmlinkage long sys_stime(time_t __user *tptr)
75 {
76         struct timespec tv;
77
78         if (!capable(CAP_SYS_TIME))
79                 return -EPERM;
80         if (get_user(tv.tv_sec, tptr))
81                 return -EFAULT;
82
83         tv.tv_nsec = 0;
84         do_settimeofday(&tv);
85         return 0;
86 }
87
88 #endif /* __ARCH_WANT_SYS_TIME */
89
90 asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz)
91 {
92         if (likely(tv != NULL)) {
93                 struct timeval ktv;
94                 do_gettimeofday(&ktv);
95                 if (copy_to_user(tv, &ktv, sizeof(ktv)))
96                         return -EFAULT;
97         }
98         if (unlikely(tz != NULL)) {
99                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
100                         return -EFAULT;
101         }
102         return 0;
103 }
104
105 /*
106  * Adjust the time obtained from the CMOS to be UTC time instead of
107  * local time.
108  * 
109  * This is ugly, but preferable to the alternatives.  Otherwise we
110  * would either need to write a program to do it in /etc/rc (and risk
111  * confusion if the program gets run more than once; it would also be 
112  * hard to make the program warp the clock precisely n hours)  or
113  * compile in the timezone information into the kernel.  Bad, bad....
114  *
115  *                                              - TYT, 1992-01-01
116  *
117  * The best thing to do is to keep the CMOS clock in universal time (UTC)
118  * as real UNIX machines always do it. This avoids all headaches about
119  * daylight saving times and warping kernel clocks.
120  */
121 inline static void warp_clock(void)
122 {
123         write_seqlock_irq(&xtime_lock);
124         wall_to_monotonic.tv_sec -= sys_tz.tz_minuteswest * 60;
125         xtime.tv_sec += sys_tz.tz_minuteswest * 60;
126         time_interpolator_update(sys_tz.tz_minuteswest * 60 * NSEC_PER_SEC);
127         write_sequnlock_irq(&xtime_lock);
128         clock_was_set();
129 }
130
131 /*
132  * In case for some reason the CMOS clock has not already been running
133  * in UTC, but in some local time: The first time we set the timezone,
134  * we will warp the clock so that it is ticking UTC time instead of
135  * local time. Presumably, if someone is setting the timezone then we
136  * are running in an environment where the programs understand about
137  * timezones. This should be done at boot time in the /etc/rc script,
138  * as soon as possible, so that the clock can be set right. Otherwise,
139  * various programs will get confused when the clock gets warped.
140  */
141
142 int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)
143 {
144         static int firsttime = 1;
145
146         if (!capable(CAP_SYS_TIME))
147                 return -EPERM;
148                 
149         if (tz) {
150                 /* SMP safe, global irq locking makes it work. */
151                 sys_tz = *tz;
152                 if (firsttime) {
153                         firsttime = 0;
154                         if (!tv)
155                                 warp_clock();
156                 }
157         }
158         if (tv)
159         {
160                 /* SMP safe, again the code in arch/foo/time.c should
161                  * globally block out interrupts when it runs.
162                  */
163                 return do_settimeofday(tv);
164         }
165         return 0;
166 }
167
168 asmlinkage long sys_settimeofday(struct timeval __user *tv,
169                                 struct timezone __user *tz)
170 {
171         struct timeval user_tv;
172         struct timespec new_ts;
173         struct timezone new_tz;
174
175         if (tv) {
176                 if (copy_from_user(&user_tv, tv, sizeof(*tv)))
177                         return -EFAULT;
178                 new_ts.tv_sec = user_tv.tv_sec;
179                 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
180         }
181         if (tz) {
182                 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
183                         return -EFAULT;
184         }
185
186         return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
187 }
188
189 long pps_offset;                /* pps time offset (us) */
190 long pps_jitter = MAXTIME;      /* time dispersion (jitter) (us) */
191
192 long pps_freq;                  /* frequency offset (scaled ppm) */
193 long pps_stabil = MAXFREQ;      /* frequency dispersion (scaled ppm) */
194
195 long pps_valid = PPS_VALID;     /* pps signal watchdog counter */
196
197 int pps_shift = PPS_SHIFT;      /* interval duration (s) (shift) */
198
199 long pps_jitcnt;                /* jitter limit exceeded */
200 long pps_calcnt;                /* calibration intervals */
201 long pps_errcnt;                /* calibration errors */
202 long pps_stbcnt;                /* stability limit exceeded */
203
204 /* hook for a loadable hardpps kernel module */
205 void (*hardpps_ptr)(struct timeval *);
206
207 /* adjtimex mainly allows reading (and writing, if superuser) of
208  * kernel time-keeping variables. used by xntpd.
209  */
210 int do_adjtimex(struct timex *txc)
211 {
212         long ltemp, mtemp, save_adjust;
213         int result;
214
215         /* In order to modify anything, you gotta be super-user! */
216         if (txc->modes && !capable(CAP_SYS_TIME))
217                 return -EPERM;
218                 
219         /* Now we validate the data before disabling interrupts */
220
221         if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
222           /* singleshot must not be used with any other mode bits */
223                 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
224                         return -EINVAL;
225
226         if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
227           /* adjustment Offset limited to +- .512 seconds */
228                 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
229                         return -EINVAL; 
230
231         /* if the quartz is off by more than 10% something is VERY wrong ! */
232         if (txc->modes & ADJ_TICK)
233                 if (txc->tick <  900000/USER_HZ ||
234                     txc->tick > 1100000/USER_HZ)
235                         return -EINVAL;
236
237         write_seqlock_irq(&xtime_lock);
238         result = time_state;    /* mostly `TIME_OK' */
239
240         /* Save for later - semantics of adjtime is to return old value */
241         save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
242
243 #if 0   /* STA_CLOCKERR is never set yet */
244         time_status &= ~STA_CLOCKERR;           /* reset STA_CLOCKERR */
245 #endif
246         /* If there are input parameters, then process them */
247         if (txc->modes)
248         {
249             if (txc->modes & ADJ_STATUS)        /* only set allowed bits */
250                 time_status =  (txc->status & ~STA_RONLY) |
251                               (time_status & STA_RONLY);
252
253             if (txc->modes & ADJ_FREQUENCY) {   /* p. 22 */
254                 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
255                     result = -EINVAL;
256                     goto leave;
257                 }
258                 time_freq = txc->freq - pps_freq;
259             }
260
261             if (txc->modes & ADJ_MAXERROR) {
262                 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
263                     result = -EINVAL;
264                     goto leave;
265                 }
266                 time_maxerror = txc->maxerror;
267             }
268
269             if (txc->modes & ADJ_ESTERROR) {
270                 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
271                     result = -EINVAL;
272                     goto leave;
273                 }
274                 time_esterror = txc->esterror;
275             }
276
277             if (txc->modes & ADJ_TIMECONST) {   /* p. 24 */
278                 if (txc->constant < 0) {        /* NTP v4 uses values > 6 */
279                     result = -EINVAL;
280                     goto leave;
281                 }
282                 time_constant = txc->constant;
283             }
284
285             if (txc->modes & ADJ_OFFSET) {      /* values checked earlier */
286                 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
287                     /* adjtime() is independent from ntp_adjtime() */
288                     if ((time_next_adjust = txc->offset) == 0)
289                          time_adjust = 0;
290                 }
291                 else if ( time_status & (STA_PLL | STA_PPSTIME) ) {
292                     ltemp = (time_status & (STA_PPSTIME | STA_PPSSIGNAL)) ==
293                             (STA_PPSTIME | STA_PPSSIGNAL) ?
294                             pps_offset : txc->offset;
295
296                     /*
297                      * Scale the phase adjustment and
298                      * clamp to the operating range.
299                      */
300                     if (ltemp > MAXPHASE)
301                         time_offset = MAXPHASE << SHIFT_UPDATE;
302                     else if (ltemp < -MAXPHASE)
303                         time_offset = -(MAXPHASE << SHIFT_UPDATE);
304                     else
305                         time_offset = ltemp << SHIFT_UPDATE;
306
307                     /*
308                      * Select whether the frequency is to be controlled
309                      * and in which mode (PLL or FLL). Clamp to the operating
310                      * range. Ugly multiply/divide should be replaced someday.
311                      */
312
313                     if (time_status & STA_FREQHOLD || time_reftime == 0)
314                         time_reftime = xtime.tv_sec;
315                     mtemp = xtime.tv_sec - time_reftime;
316                     time_reftime = xtime.tv_sec;
317                     if (time_status & STA_FLL) {
318                         if (mtemp >= MINSEC) {
319                             ltemp = (time_offset / mtemp) << (SHIFT_USEC -
320                                                               SHIFT_UPDATE);
321                             if (ltemp < 0)
322                                 time_freq -= -ltemp >> SHIFT_KH;
323                             else
324                                 time_freq += ltemp >> SHIFT_KH;
325                         } else /* calibration interval too short (p. 12) */
326                                 result = TIME_ERROR;
327                     } else {    /* PLL mode */
328                         if (mtemp < MAXSEC) {
329                             ltemp *= mtemp;
330                             if (ltemp < 0)
331                                 time_freq -= -ltemp >> (time_constant +
332                                                         time_constant +
333                                                         SHIFT_KF - SHIFT_USEC);
334                             else
335                                 time_freq += ltemp >> (time_constant +
336                                                        time_constant +
337                                                        SHIFT_KF - SHIFT_USEC);
338                         } else /* calibration interval too long (p. 12) */
339                                 result = TIME_ERROR;
340                     }
341                     if (time_freq > time_tolerance)
342                         time_freq = time_tolerance;
343                     else if (time_freq < -time_tolerance)
344                         time_freq = -time_tolerance;
345                 } /* STA_PLL || STA_PPSTIME */
346             } /* txc->modes & ADJ_OFFSET */
347             if (txc->modes & ADJ_TICK) {
348                 tick_usec = txc->tick;
349                 tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
350             }
351         } /* txc->modes */
352 leave:  if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
353             || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
354                 && (time_status & STA_PPSSIGNAL) == 0)
355             /* p. 24, (b) */
356             || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
357                 == (STA_PPSTIME|STA_PPSJITTER))
358             /* p. 24, (c) */
359             || ((time_status & STA_PPSFREQ) != 0
360                 && (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
361             /* p. 24, (d) */
362                 result = TIME_ERROR;
363         
364         if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
365             txc->offset    = save_adjust;
366         else {
367             if (time_offset < 0)
368                 txc->offset = -(-time_offset >> SHIFT_UPDATE);
369             else
370                 txc->offset = time_offset >> SHIFT_UPDATE;
371         }
372         txc->freq          = time_freq + pps_freq;
373         txc->maxerror      = time_maxerror;
374         txc->esterror      = time_esterror;
375         txc->status        = time_status;
376         txc->constant      = time_constant;
377         txc->precision     = time_precision;
378         txc->tolerance     = time_tolerance;
379         txc->tick          = tick_usec;
380         txc->ppsfreq       = pps_freq;
381         txc->jitter        = pps_jitter >> PPS_AVG;
382         txc->shift         = pps_shift;
383         txc->stabil        = pps_stabil;
384         txc->jitcnt        = pps_jitcnt;
385         txc->calcnt        = pps_calcnt;
386         txc->errcnt        = pps_errcnt;
387         txc->stbcnt        = pps_stbcnt;
388         write_sequnlock_irq(&xtime_lock);
389         do_gettimeofday(&txc->time);
390         return(result);
391 }
392
393 asmlinkage long sys_adjtimex(struct timex __user *txc_p)
394 {
395         struct timex txc;               /* Local copy of parameter */
396         int ret;
397
398         /* Copy the user data space into the kernel copy
399          * structure. But bear in mind that the structures
400          * may change
401          */
402         if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
403                 return -EFAULT;
404         ret = do_adjtimex(&txc);
405         return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
406 }
407
408 struct timespec current_kernel_time(void)
409 {
410         struct timespec now;
411         unsigned long seq;
412
413         do {
414                 seq = read_seqbegin(&xtime_lock);
415                 
416                 now = xtime;
417         } while (read_seqretry(&xtime_lock, seq));
418
419         return now; 
420 }
421
422 EXPORT_SYMBOL(current_kernel_time);
423
424 #if (BITS_PER_LONG < 64)
425 u64 get_jiffies_64(void)
426 {
427         unsigned long seq;
428         u64 ret;
429
430         do {
431                 seq = read_seqbegin(&xtime_lock);
432                 ret = jiffies_64;
433         } while (read_seqretry(&xtime_lock, seq));
434         return ret;
435 }
436
437 EXPORT_SYMBOL(get_jiffies_64);
438 #endif
439
440 EXPORT_SYMBOL(jiffies);