1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/module.h>
31#include <linux/timex.h>
32#include <linux/errno.h>
33#include <linux/smp_lock.h>
34#include <asm/uaccess.h>
35#include <asm/unistd.h>
36#include <linux/fs.h>
37
38
39
40
41
42struct timezone sys_tz;
43
44EXPORT_SYMBOL(sys_tz);
45
46#ifdef __ARCH_WANT_SYS_TIME
47
48
49
50
51
52
53
54
55
56asmlinkage long sys_time(int __user * tloc)
57{
58 int i;
59 struct timeval tv;
60
61 do_gettimeofday(&tv);
62 i = tv.tv_sec;
63
64 if (tloc) {
65 if (put_user(i,tloc))
66 i = -EFAULT;
67 }
68 return i;
69}
70
71
72
73
74
75
76
77
78asmlinkage long sys_stime(time_t __user *tptr)
79{
80 struct timespec tv;
81
82 if (!capable(CAP_SYS_TIME))
83 return -EPERM;
84 if (get_user(tv.tv_sec, tptr))
85 return -EFAULT;
86
87 tv.tv_nsec = 0;
88 do_settimeofday(&tv);
89 return 0;
90}
91
92#endif
93
94asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz)
95{
96 if (likely(tv != NULL)) {
97 struct timeval ktv;
98 do_gettimeofday(&ktv);
99 if (copy_to_user(tv, &ktv, sizeof(ktv)))
100 return -EFAULT;
101 }
102 if (unlikely(tz != NULL)) {
103 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
104 return -EFAULT;
105 }
106 return 0;
107}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125inline static void warp_clock(void)
126{
127 write_seqlock_irq(&xtime_lock);
128 wall_to_monotonic.tv_sec -= sys_tz.tz_minuteswest * 60;
129 xtime.tv_sec += sys_tz.tz_minuteswest * 60;
130 time_interpolator_reset();
131 write_sequnlock_irq(&xtime_lock);
132 clock_was_set();
133}
134
135
136
137
138
139
140
141
142
143
144
145
146int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)
147{
148 static int firsttime = 1;
149
150 if (!capable(CAP_SYS_TIME))
151 return -EPERM;
152
153 if (tz) {
154
155 sys_tz = *tz;
156 if (firsttime) {
157 firsttime = 0;
158 if (!tv)
159 warp_clock();
160 }
161 }
162 if (tv)
163 {
164
165
166
167 return do_settimeofday(tv);
168 }
169 return 0;
170}
171
172asmlinkage long sys_settimeofday(struct timeval __user *tv,
173 struct timezone __user *tz)
174{
175 struct timeval user_tv;
176 struct timespec new_ts;
177 struct timezone new_tz;
178
179 if (tv) {
180 if (copy_from_user(&user_tv, tv, sizeof(*tv)))
181 return -EFAULT;
182 new_ts.tv_sec = user_tv.tv_sec;
183 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
184 }
185 if (tz) {
186 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
187 return -EFAULT;
188 }
189
190 return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
191}
192
193long pps_offset;
194long pps_jitter = MAXTIME;
195
196long pps_freq;
197long pps_stabil = MAXFREQ;
198
199long pps_valid = PPS_VALID;
200
201int pps_shift = PPS_SHIFT;
202
203long pps_jitcnt;
204long pps_calcnt;
205long pps_errcnt;
206long pps_stbcnt;
207
208
209void (*hardpps_ptr)(struct timeval *);
210
211
212
213
214int do_adjtimex(struct timex *txc)
215{
216 long ltemp, mtemp, save_adjust;
217 int result;
218
219
220 if (txc->modes && !capable(CAP_SYS_TIME))
221 return -EPERM;
222
223
224
225 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
226
227 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
228 return -EINVAL;
229
230 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
231
232 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
233 return -EINVAL;
234
235
236 if (txc->modes & ADJ_TICK)
237 if (txc->tick < 900000/USER_HZ ||
238 txc->tick > 1100000/USER_HZ)
239 return -EINVAL;
240
241 write_seqlock_irq(&xtime_lock);
242 result = time_state;
243
244
245 save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
246
247#if 0
248 time_status &= ~STA_CLOCKERR;
249#endif
250
251 if (txc->modes)
252 {
253 if (txc->modes & ADJ_STATUS)
254 time_status = (txc->status & ~STA_RONLY) |
255 (time_status & STA_RONLY);
256
257 if (txc->modes & ADJ_FREQUENCY) {
258 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
259 result = -EINVAL;
260 goto leave;
261 }
262 time_freq = txc->freq - pps_freq;
263 }
264
265 if (txc->modes & ADJ_MAXERROR) {
266 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
267 result = -EINVAL;
268 goto leave;
269 }
270 time_maxerror = txc->maxerror;
271 }
272
273 if (txc->modes & ADJ_ESTERROR) {
274 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
275 result = -EINVAL;
276 goto leave;
277 }
278 time_esterror = txc->esterror;
279 }
280
281 if (txc->modes & ADJ_TIMECONST) {
282 if (txc->constant < 0) {
283 result = -EINVAL;
284 goto leave;
285 }
286 time_constant = txc->constant;
287 }
288
289 if (txc->modes & ADJ_OFFSET) {
290 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
291
292 if ((time_next_adjust = txc->offset) == 0)
293 time_adjust = 0;
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;
299
300
301
302
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;
310
311
312
313
314
315
316
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
330 result = TIME_ERROR;
331 } else {
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
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 }
350 }
351 if (txc->modes & ADJ_TICK) {
352 tick_usec = txc->tick;
353 tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
354 }
355 }
356leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
357 || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
358 && (time_status & STA_PPSSIGNAL) == 0)
359
360 || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
361 == (STA_PPSTIME|STA_PPSJITTER))
362
363 || ((time_status & STA_PPSFREQ) != 0
364 && (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
365
366 result = TIME_ERROR;
367
368 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
369 txc->offset = save_adjust;
370 else {
371 if (time_offset < 0)
372 txc->offset = -(-time_offset >> SHIFT_UPDATE);
373 else
374 txc->offset = time_offset >> SHIFT_UPDATE;
375 }
376 txc->freq = time_freq + pps_freq;
377 txc->maxerror = time_maxerror;
378 txc->esterror = time_esterror;
379 txc->status = time_status;
380 txc->constant = time_constant;
381 txc->precision = time_precision;
382 txc->tolerance = time_tolerance;
383 txc->tick = tick_usec;
384 txc->ppsfreq = pps_freq;
385 txc->jitter = pps_jitter >> PPS_AVG;
386 txc->shift = pps_shift;
387 txc->stabil = pps_stabil;
388 txc->jitcnt = pps_jitcnt;
389 txc->calcnt = pps_calcnt;
390 txc->errcnt = pps_errcnt;
391 txc->stbcnt = pps_stbcnt;
392 write_sequnlock_irq(&xtime_lock);
393 do_gettimeofday(&txc->time);
394 return(result);
395}
396
397asmlinkage long sys_adjtimex(struct timex __user *txc_p)
398{
399 struct timex txc;
400 int ret;
401
402
403
404
405
406 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
407 return -EFAULT;
408 ret = do_adjtimex(&txc);
409 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
410}
411
412struct timespec current_kernel_time(void)
413{
414 struct timespec now;
415 unsigned long seq;
416
417 do {
418 seq = read_seqbegin(&xtime_lock);
419
420 now = xtime;
421 } while (read_seqretry(&xtime_lock, seq));
422
423 return now;
424}
425
426EXPORT_SYMBOL(current_kernel_time);
427
428
429
430
431
432
433
434
435struct timespec current_fs_time(struct super_block *sb)
436{
437 struct timespec now = current_kernel_time();
438 return timespec_trunc(now, get_sb_time_gran(sb));
439}
440EXPORT_SYMBOL(current_fs_time);
441
442
443
444
445
446
447
448
449
450
451
452
453
454struct timespec timespec_trunc(struct timespec t, unsigned gran)
455{
456
457
458
459
460
461 if (gran <= jiffies_to_usecs(1) * 1000) {
462
463 } else if (gran == 1000000000) {
464 t.tv_nsec = 0;
465 } else {
466 t.tv_nsec -= t.tv_nsec % gran;
467 }
468 return t;
469}
470EXPORT_SYMBOL(timespec_trunc);
471
472#ifdef CONFIG_TIME_INTERPOLATION
473void getnstimeofday (struct timespec *tv)
474{
475 unsigned long seq,sec,nsec;
476
477 do {
478 seq = read_seqbegin(&xtime_lock);
479 sec = xtime.tv_sec;
480 nsec = xtime.tv_nsec+time_interpolator_get_offset();
481 } while (unlikely(read_seqretry(&xtime_lock, seq)));
482
483 while (unlikely(nsec >= NSEC_PER_SEC)) {
484 nsec -= NSEC_PER_SEC;
485 ++sec;
486 }
487 tv->tv_sec = sec;
488 tv->tv_nsec = nsec;
489}
490
491int do_settimeofday (struct timespec *tv)
492{
493 time_t wtm_sec, sec = tv->tv_sec;
494 long wtm_nsec, nsec = tv->tv_nsec;
495
496 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
497 return -EINVAL;
498
499 write_seqlock_irq(&xtime_lock);
500 {
501
502
503
504
505
506
507 nsec -= time_interpolator_get_offset();
508
509 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
510 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
511
512 set_normalized_timespec(&xtime, sec, nsec);
513 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
514
515 time_adjust = 0;
516 time_status |= STA_UNSYNC;
517 time_maxerror = NTP_PHASE_LIMIT;
518 time_esterror = NTP_PHASE_LIMIT;
519 time_interpolator_reset();
520 }
521 write_sequnlock_irq(&xtime_lock);
522 clock_was_set();
523 return 0;
524}
525
526EXPORT_SYMBOL(do_settimeofday);
527
528void do_gettimeofday (struct timeval *tv)
529{
530 unsigned long seq, nsec, usec, sec, offset;
531 do {
532 seq = read_seqbegin(&xtime_lock);
533 offset = time_interpolator_get_offset();
534 sec = xtime.tv_sec;
535 nsec = xtime.tv_nsec;
536 } while (unlikely(read_seqretry(&xtime_lock, seq)));
537
538 usec = (nsec + offset) / 1000;
539
540 while (unlikely(usec >= USEC_PER_SEC)) {
541 usec -= USEC_PER_SEC;
542 ++sec;
543 }
544
545 tv->tv_sec = sec;
546 tv->tv_usec = usec;
547}
548
549EXPORT_SYMBOL(do_gettimeofday);
550
551
552#else
553
554
555
556
557void getnstimeofday(struct timespec *tv)
558{
559 struct timeval x;
560
561 do_gettimeofday(&x);
562 tv->tv_sec = x.tv_sec;
563 tv->tv_nsec = x.tv_usec * NSEC_PER_USEC;
564}
565#endif
566
567#if (BITS_PER_LONG < 64)
568u64 get_jiffies_64(void)
569{
570 unsigned long seq;
571 u64 ret;
572
573 do {
574 seq = read_seqbegin(&xtime_lock);
575 ret = jiffies_64;
576 } while (read_seqretry(&xtime_lock, seq));
577 return ret;
578}
579
580EXPORT_SYMBOL(get_jiffies_64);
581#endif
582
583EXPORT_SYMBOL(jiffies);
584