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,
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)
28 #include <linux/timex.h>
29 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
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
)
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!)
52 void get_fast_time(struct timeval
* 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
)
71 /* SMP: This is fairly trivial. We grab CURRENT_TIME and
72 stuff it to user space. No side effects */
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
)
92 if (!capable(CAP_SYS_TIME
))
94 if (get_user(value
, tptr
))
99 time_adjust
= 0; /* stop active adjtime() */
100 time_status
|= STA_UNSYNC
;
101 time_maxerror
= NTP_PHASE_LIMIT
;
102 time_esterror
= NTP_PHASE_LIMIT
;
109 asmlinkage
int sys_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
113 do_gettimeofday(&ktv
);
114 if (copy_to_user(tv
, &ktv
, sizeof(ktv
)))
118 if (copy_to_user(tz
, &sys_tz
, sizeof(sys_tz
)))
125 * Adjust the time obtained from the CMOS to be UTC time instead of
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....
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)
143 xtime
.tv_sec
+= sys_tz
.tz_minuteswest
* 60;
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
))
166 /* SMP safe, global irq locking makes it work. */
176 /* SMP safe, again the code in arch/foo/time.c should
177 * globally block out interrupts when it runs.
184 asmlinkage
int sys_settimeofday(struct timeval
*tv
, struct timezone
*tz
)
186 struct timeval new_tv
;
187 struct timezone new_tz
;
190 if (copy_from_user(&new_tv
, tv
, sizeof(*tv
)))
194 if (copy_from_user(&new_tz
, tz
, sizeof(*tz
)))
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
))
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
)
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
)
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 */
251 /* If there are input parameters, then process them */
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
) {
263 time_freq
= txc
->freq
- pps_freq
;
266 if (txc
->modes
& ADJ_MAXERROR
) {
267 if (txc
->maxerror
< 0 || txc
->maxerror
>= NTP_PHASE_LIMIT
) {
271 time_maxerror
= txc
->maxerror
;
274 if (txc
->modes
& ADJ_ESTERROR
) {
275 if (txc
->esterror
< 0 || txc
->esterror
>= NTP_PHASE_LIMIT
) {
279 time_esterror
= txc
->esterror
;
282 if (txc
->modes
& ADJ_TIMECONST
) { /* p. 24 */
283 if (txc
->constant
< 0) { /* NTP v4 uses values > 6 */
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
);
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
-
326 time_freq
-= -ltemp
>> SHIFT_KH
;
328 time_freq
+= ltemp
>> SHIFT_KH
;
329 } else /* calibration interval too short (p. 12) */
331 } else { /* PLL mode */
332 if (mtemp
< MAXSEC
) {
335 time_freq
-= -ltemp
>> (time_constant
+
337 SHIFT_KF
- SHIFT_USEC
);
339 time_freq
+= ltemp
>> (time_constant
+
341 SHIFT_KF
- SHIFT_USEC
);
342 } else /* calibration interval too long (p. 12) */
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
354 if (txc
->tick
< 900000/HZ
|| txc
->tick
> 1100000/HZ
) {
361 leave
: if ((time_status
& (STA_UNSYNC
|STA_CLOCKERR
)) != 0
362 || ((time_status
& (STA_PPSFREQ
|STA_PPSTIME
)) != 0
363 && (time_status
& STA_PPSSIGNAL
) == 0)
365 || ((time_status
& (STA_PPSTIME
|STA_PPSJITTER
))
366 == (STA_PPSTIME
|STA_PPSJITTER
))
368 || ((time_status
& STA_PPSFREQ
) != 0
369 && (time_status
& (STA_PPSWANDER
|STA_PPSERROR
)) != 0))
373 if ((txc
->modes
& ADJ_OFFSET_SINGLESHOT
) == ADJ_OFFSET_SINGLESHOT
)
374 txc
->offset
= save_adjust
;
377 txc
->offset
= -(-time_offset
>> SHIFT_UPDATE
);
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
);
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
;
403 asmlinkage
int sys_adjtimex(struct timex
*txc_p
)
405 struct timex txc
; /* Local copy of parameter */
408 /* Copy the user data space into the kernel copy
409 * structure. But bear in mind that the structures
412 if(copy_from_user(&txc
, txc_p
, sizeof(struct timex
)))
414 ret
= do_adjtimex(&txc
);
415 return copy_to_user(txc_p
, &txc
, sizeof(struct timex
)) ? -EFAULT
: ret
;