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