direct-io.hg

view linux-2.4.30-xen-sparse/kernel/time.c @ 5647:6f462a11a08e

Register the portio handler only once.
Signed-off-by: Arun Sharma <arun.sharma@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jul 01 21:25:45 2005 +0000 (2005-07-01)
parents 85fcf3b1b7a5
children 56a63f9f378f
line source
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 */
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.
36 */
37 struct timezone sys_tz;
39 /* The xtime_lock is not only serializing the xtime read/writes but it's also
40 serializing all accesses to the global NTP variables now. */
41 extern rwlock_t xtime_lock;
43 #if !defined(__alpha__) && !defined(__ia64__)
45 /*
46 * sys_time() can be implemented in user-level using
47 * sys_gettimeofday(). Is this for backwards compatibility? If so,
48 * why not move it into the appropriate arch directory (for those
49 * architectures that need it).
50 *
51 * XXX This function is NOT 64-bit clean!
52 */
53 asmlinkage long sys_time(int * tloc)
54 {
55 struct timeval now;
56 int i;
58 do_gettimeofday(&now);
59 i = now.tv_sec;
60 if (tloc) {
61 if (put_user(i,tloc))
62 i = -EFAULT;
63 }
64 return i;
65 }
67 #if !defined(CONFIG_XEN)
69 /*
70 * sys_stime() can be implemented in user-level using
71 * sys_settimeofday(). Is this for backwards compatibility? If so,
72 * why not move it into the appropriate arch directory (for those
73 * architectures that need it).
74 */
76 asmlinkage long sys_stime(int * tptr)
77 {
78 int value;
80 if (!capable(CAP_SYS_TIME))
81 return -EPERM;
82 if (get_user(value, tptr))
83 return -EFAULT;
84 write_lock_irq(&xtime_lock);
85 vxtime_lock();
86 xtime.tv_sec = value;
87 xtime.tv_usec = 0;
88 vxtime_unlock();
89 time_adjust = 0; /* stop active adjtime() */
90 time_status |= STA_UNSYNC;
91 time_maxerror = NTP_PHASE_LIMIT;
92 time_esterror = NTP_PHASE_LIMIT;
93 write_unlock_irq(&xtime_lock);
94 return 0;
95 }
97 #endif
99 #endif
101 asmlinkage long sys_gettimeofday(struct timeval *tv, struct timezone *tz)
102 {
103 if (tv) {
104 struct timeval ktv;
105 do_gettimeofday(&ktv);
106 if (copy_to_user(tv, &ktv, sizeof(ktv)))
107 return -EFAULT;
108 }
109 if (tz) {
110 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
111 return -EFAULT;
112 }
113 return 0;
114 }
116 /*
117 * Adjust the time obtained from the CMOS to be UTC time instead of
118 * local time.
119 *
120 * This is ugly, but preferable to the alternatives. Otherwise we
121 * would either need to write a program to do it in /etc/rc (and risk
122 * confusion if the program gets run more than once; it would also be
123 * hard to make the program warp the clock precisely n hours) or
124 * compile in the timezone information into the kernel. Bad, bad....
125 *
126 * - TYT, 1992-01-01
127 *
128 * The best thing to do is to keep the CMOS clock in universal time (UTC)
129 * as real UNIX machines always do it. This avoids all headaches about
130 * daylight saving times and warping kernel clocks.
131 */
132 inline static void warp_clock(void)
133 {
134 write_lock_irq(&xtime_lock);
135 vxtime_lock();
136 xtime.tv_sec += sys_tz.tz_minuteswest * 60;
137 vxtime_unlock();
138 write_unlock_irq(&xtime_lock);
139 }
141 /*
142 * In case for some reason the CMOS clock has not already been running
143 * in UTC, but in some local time: The first time we set the timezone,
144 * we will warp the clock so that it is ticking UTC time instead of
145 * local time. Presumably, if someone is setting the timezone then we
146 * are running in an environment where the programs understand about
147 * timezones. This should be done at boot time in the /etc/rc script,
148 * as soon as possible, so that the clock can be set right. Otherwise,
149 * various programs will get confused when the clock gets warped.
150 */
152 int do_sys_settimeofday(struct timeval *tv, struct timezone *tz)
153 {
154 static int firsttime = 1;
156 if (!capable(CAP_SYS_TIME))
157 return -EPERM;
159 if (tz) {
160 /* SMP safe, global irq locking makes it work. */
161 sys_tz = *tz;
162 if (firsttime) {
163 firsttime = 0;
164 if (!tv)
165 warp_clock();
166 }
167 }
168 if (tv)
169 {
170 /* SMP safe, again the code in arch/foo/time.c should
171 * globally block out interrupts when it runs.
172 */
173 do_settimeofday(tv);
174 }
175 return 0;
176 }
178 asmlinkage long sys_settimeofday(struct timeval *tv, struct timezone *tz)
179 {
180 struct timeval new_tv;
181 struct timezone new_tz;
183 if (tv) {
184 if (copy_from_user(&new_tv, tv, sizeof(*tv)))
185 return -EFAULT;
186 }
187 if (tz) {
188 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
189 return -EFAULT;
190 }
192 return do_sys_settimeofday(tv ? &new_tv : NULL, tz ? &new_tz : NULL);
193 }
195 long pps_offset; /* pps time offset (us) */
196 long pps_jitter = MAXTIME; /* time dispersion (jitter) (us) */
198 long pps_freq; /* frequency offset (scaled ppm) */
199 long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */
201 long pps_valid = PPS_VALID; /* pps signal watchdog counter */
203 int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */
205 long pps_jitcnt; /* jitter limit exceeded */
206 long pps_calcnt; /* calibration intervals */
207 long pps_errcnt; /* calibration errors */
208 long pps_stbcnt; /* stability limit exceeded */
210 /* hook for a loadable hardpps kernel module */
211 void (*hardpps_ptr)(struct timeval *);
213 /* adjtimex mainly allows reading (and writing, if superuser) of
214 * kernel time-keeping variables. used by xntpd.
215 */
216 int do_adjtimex(struct timex *txc)
217 {
218 long ltemp, mtemp, save_adjust;
219 int result;
221 /* In order to modify anything, you gotta be super-user! */
222 if (txc->modes && !capable(CAP_SYS_TIME))
223 return -EPERM;
225 /* Now we validate the data before disabling interrupts */
227 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
228 /* singleshot must not be used with any other mode bits */
229 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
230 return -EINVAL;
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 write_lock_irq(&xtime_lock);
243 result = time_state; /* mostly `TIME_OK' */
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)
253 {
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;
262 }
263 time_freq = txc->freq - pps_freq;
264 }
266 if (txc->modes & ADJ_MAXERROR) {
267 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
268 result = -EINVAL;
269 goto leave;
270 }
271 time_maxerror = txc->maxerror;
272 }
274 if (txc->modes & ADJ_ESTERROR) {
275 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
276 result = -EINVAL;
277 goto leave;
278 }
279 time_esterror = txc->esterror;
280 }
282 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
283 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
284 result = -EINVAL;
285 goto leave;
286 }
287 time_constant = txc->constant;
288 }
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;
294 }
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;
300 /*
301 * Scale the phase adjustment and
302 * clamp to the operating range.
303 */
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;
311 /*
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.
315 */
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;
344 }
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;
357 }
358 tick = txc->tick;
359 }
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;
380 }
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 txc->tick = tick;
389 txc->ppsfreq = pps_freq;
390 txc->jitter = pps_jitter >> PPS_AVG;
391 txc->shift = pps_shift;
392 txc->stabil = pps_stabil;
393 txc->jitcnt = pps_jitcnt;
394 txc->calcnt = pps_calcnt;
395 txc->errcnt = pps_errcnt;
396 txc->stbcnt = pps_stbcnt;
397 write_unlock_irq(&xtime_lock);
398 do_gettimeofday(&txc->time);
399 return(result);
400 }
402 asmlinkage long sys_adjtimex(struct timex *txc_p)
403 {
404 struct timex txc; /* Local copy of parameter */
405 int ret;
407 /* Copy the user data space into the kernel copy
408 * structure. But bear in mind that the structures
409 * may change
410 */
411 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
412 return -EFAULT;
413 ret = do_adjtimex(&txc);
414 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
415 }