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/export.h>
31#include <linux/timex.h>
32#include <linux/capability.h>
33#include <linux/timekeeper_internal.h>
34#include <linux/errno.h>
35#include <linux/syscalls.h>
36#include <linux/security.h>
37#include <linux/fs.h>
38#include <linux/math64.h>
39#include <linux/ptrace.h>
40
41#include <asm/uaccess.h>
42#include <asm/unistd.h>
43
44#include <generated/timeconst.h>
45#include "timekeeping.h"
46
47
48
49
50
51struct timezone sys_tz;
52
53EXPORT_SYMBOL(sys_tz);
54
55#ifdef __ARCH_WANT_SYS_TIME
56
57
58
59
60
61
62
63SYSCALL_DEFINE1(time, time_t __user *, tloc)
64{
65 time_t i = get_seconds();
66
67 if (tloc) {
68 if (put_user(i,tloc))
69 return -EFAULT;
70 }
71 force_successful_syscall_return();
72 return i;
73}
74
75
76
77
78
79
80
81
82SYSCALL_DEFINE1(stime, time_t __user *, tptr)
83{
84 struct timespec tv;
85 int err;
86
87 if (get_user(tv.tv_sec, tptr))
88 return -EFAULT;
89
90 tv.tv_nsec = 0;
91
92 err = security_settime(&tv, NULL);
93 if (err)
94 return err;
95
96 do_settimeofday(&tv);
97 return 0;
98}
99
100#endif
101
102SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv,
103 struct timezone __user *, tz)
104{
105 if (likely(tv != NULL)) {
106 struct timeval ktv;
107 do_gettimeofday(&ktv);
108 if (copy_to_user(tv, &ktv, sizeof(ktv)))
109 return -EFAULT;
110 }
111 if (unlikely(tz != NULL)) {
112 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
113 return -EFAULT;
114 }
115 return 0;
116}
117
118
119
120
121
122int persistent_clock_is_local;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140static inline void warp_clock(void)
141{
142 if (sys_tz.tz_minuteswest != 0) {
143 struct timespec adjust;
144
145 persistent_clock_is_local = 1;
146 adjust.tv_sec = sys_tz.tz_minuteswest * 60;
147 adjust.tv_nsec = 0;
148 timekeeping_inject_offset(&adjust);
149 }
150}
151
152
153
154
155
156
157
158
159
160
161
162
163int do_sys_settimeofday64(const struct timespec64 *tv, const struct timezone *tz)
164{
165 static int firsttime = 1;
166 int error = 0;
167
168 if (tv && !timespec64_valid(tv))
169 return -EINVAL;
170
171 error = security_settime64(tv, tz);
172 if (error)
173 return error;
174
175 if (tz) {
176
177 if (tz->tz_minuteswest > 15*60 || tz->tz_minuteswest < -15*60)
178 return -EINVAL;
179
180 sys_tz = *tz;
181 update_vsyscall_tz();
182 if (firsttime) {
183 firsttime = 0;
184 if (!tv)
185 warp_clock();
186 }
187 }
188 if (tv)
189 return do_settimeofday64(tv);
190 return 0;
191}
192
193SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
194 struct timezone __user *, tz)
195{
196 struct timeval user_tv;
197 struct timespec new_ts;
198 struct timezone new_tz;
199
200 if (tv) {
201 if (copy_from_user(&user_tv, tv, sizeof(*tv)))
202 return -EFAULT;
203
204 if (!timeval_valid(&user_tv))
205 return -EINVAL;
206
207 new_ts.tv_sec = user_tv.tv_sec;
208 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
209 }
210 if (tz) {
211 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
212 return -EFAULT;
213 }
214
215 return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
216}
217
218SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
219{
220 struct timex txc;
221 int ret;
222
223
224
225
226
227 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
228 return -EFAULT;
229 ret = do_adjtimex(&txc);
230 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
231}
232
233
234
235
236
237
238
239
240struct timespec current_fs_time(struct super_block *sb)
241{
242 struct timespec now = current_kernel_time();
243 return timespec_trunc(now, sb->s_time_gran);
244}
245EXPORT_SYMBOL(current_fs_time);
246
247
248
249
250
251
252
253unsigned int jiffies_to_msecs(const unsigned long j)
254{
255#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
256 return (MSEC_PER_SEC / HZ) * j;
257#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
258 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
259#else
260# if BITS_PER_LONG == 32
261 return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
262# else
263 return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
264# endif
265#endif
266}
267EXPORT_SYMBOL(jiffies_to_msecs);
268
269unsigned int jiffies_to_usecs(const unsigned long j)
270{
271
272
273
274
275 BUILD_BUG_ON(HZ > USEC_PER_SEC);
276
277#if !(USEC_PER_SEC % HZ)
278 return (USEC_PER_SEC / HZ) * j;
279#else
280# if BITS_PER_LONG == 32
281 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
282# else
283 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
284# endif
285#endif
286}
287EXPORT_SYMBOL(jiffies_to_usecs);
288
289
290
291
292
293
294
295
296
297struct timespec timespec_trunc(struct timespec t, unsigned gran)
298{
299
300 if (gran == 1) {
301
302 } else if (gran == NSEC_PER_SEC) {
303 t.tv_nsec = 0;
304 } else if (gran > 1 && gran < NSEC_PER_SEC) {
305 t.tv_nsec -= t.tv_nsec % gran;
306 } else {
307 WARN(1, "illegal file time granularity: %u", gran);
308 }
309 return t;
310}
311EXPORT_SYMBOL(timespec_trunc);
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333time64_t mktime64(const unsigned int year0, const unsigned int mon0,
334 const unsigned int day, const unsigned int hour,
335 const unsigned int min, const unsigned int sec)
336{
337 unsigned int mon = mon0, year = year0;
338
339
340 if (0 >= (int) (mon -= 2)) {
341 mon += 12;
342 year -= 1;
343 }
344
345 return ((((time64_t)
346 (year/4 - year/100 + year/400 + 367*mon/12 + day) +
347 year*365 - 719499
348 )*24 + hour
349 )*60 + min
350 )*60 + sec;
351}
352EXPORT_SYMBOL(mktime64);
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
369{
370 while (nsec >= NSEC_PER_SEC) {
371
372
373
374
375
376 asm("" : "+rm"(nsec));
377 nsec -= NSEC_PER_SEC;
378 ++sec;
379 }
380 while (nsec < 0) {
381 asm("" : "+rm"(nsec));
382 nsec += NSEC_PER_SEC;
383 --sec;
384 }
385 ts->tv_sec = sec;
386 ts->tv_nsec = nsec;
387}
388EXPORT_SYMBOL(set_normalized_timespec);
389
390
391
392
393
394
395
396struct timespec ns_to_timespec(const s64 nsec)
397{
398 struct timespec ts;
399 s32 rem;
400
401 if (!nsec)
402 return (struct timespec) {0, 0};
403
404 ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
405 if (unlikely(rem < 0)) {
406 ts.tv_sec--;
407 rem += NSEC_PER_SEC;
408 }
409 ts.tv_nsec = rem;
410
411 return ts;
412}
413EXPORT_SYMBOL(ns_to_timespec);
414
415
416
417
418
419
420
421struct timeval ns_to_timeval(const s64 nsec)
422{
423 struct timespec ts = ns_to_timespec(nsec);
424 struct timeval tv;
425
426 tv.tv_sec = ts.tv_sec;
427 tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
428
429 return tv;
430}
431EXPORT_SYMBOL(ns_to_timeval);
432
433#if BITS_PER_LONG == 32
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
449{
450 while (nsec >= NSEC_PER_SEC) {
451
452
453
454
455
456 asm("" : "+rm"(nsec));
457 nsec -= NSEC_PER_SEC;
458 ++sec;
459 }
460 while (nsec < 0) {
461 asm("" : "+rm"(nsec));
462 nsec += NSEC_PER_SEC;
463 --sec;
464 }
465 ts->tv_sec = sec;
466 ts->tv_nsec = nsec;
467}
468EXPORT_SYMBOL(set_normalized_timespec64);
469
470
471
472
473
474
475
476struct timespec64 ns_to_timespec64(const s64 nsec)
477{
478 struct timespec64 ts;
479 s32 rem;
480
481 if (!nsec)
482 return (struct timespec64) {0, 0};
483
484 ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
485 if (unlikely(rem < 0)) {
486 ts.tv_sec--;
487 rem += NSEC_PER_SEC;
488 }
489 ts.tv_nsec = rem;
490
491 return ts;
492}
493EXPORT_SYMBOL(ns_to_timespec64);
494#endif
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519unsigned long __msecs_to_jiffies(const unsigned int m)
520{
521
522
523
524 if ((int)m < 0)
525 return MAX_JIFFY_OFFSET;
526 return _msecs_to_jiffies(m);
527}
528EXPORT_SYMBOL(__msecs_to_jiffies);
529
530unsigned long __usecs_to_jiffies(const unsigned int u)
531{
532 if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
533 return MAX_JIFFY_OFFSET;
534 return _usecs_to_jiffies(u);
535}
536EXPORT_SYMBOL(__usecs_to_jiffies);
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553static unsigned long
554__timespec64_to_jiffies(u64 sec, long nsec)
555{
556 nsec = nsec + TICK_NSEC - 1;
557
558 if (sec >= MAX_SEC_IN_JIFFIES){
559 sec = MAX_SEC_IN_JIFFIES;
560 nsec = 0;
561 }
562 return ((sec * SEC_CONVERSION) +
563 (((u64)nsec * NSEC_CONVERSION) >>
564 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
565
566}
567
568static unsigned long
569__timespec_to_jiffies(unsigned long sec, long nsec)
570{
571 return __timespec64_to_jiffies((u64)sec, nsec);
572}
573
574unsigned long
575timespec64_to_jiffies(const struct timespec64 *value)
576{
577 return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec);
578}
579EXPORT_SYMBOL(timespec64_to_jiffies);
580
581void
582jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
583{
584
585
586
587
588 u32 rem;
589 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
590 NSEC_PER_SEC, &rem);
591 value->tv_nsec = rem;
592}
593EXPORT_SYMBOL(jiffies_to_timespec64);
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611unsigned long
612timeval_to_jiffies(const struct timeval *value)
613{
614 return __timespec_to_jiffies(value->tv_sec,
615 value->tv_usec * NSEC_PER_USEC);
616}
617EXPORT_SYMBOL(timeval_to_jiffies);
618
619void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
620{
621
622
623
624
625 u32 rem;
626
627 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
628 NSEC_PER_SEC, &rem);
629 value->tv_usec = rem / NSEC_PER_USEC;
630}
631EXPORT_SYMBOL(jiffies_to_timeval);
632
633
634
635
636clock_t jiffies_to_clock_t(unsigned long x)
637{
638#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
639# if HZ < USER_HZ
640 return x * (USER_HZ / HZ);
641# else
642 return x / (HZ / USER_HZ);
643# endif
644#else
645 return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
646#endif
647}
648EXPORT_SYMBOL(jiffies_to_clock_t);
649
650unsigned long clock_t_to_jiffies(unsigned long x)
651{
652#if (HZ % USER_HZ)==0
653 if (x >= ~0UL / (HZ / USER_HZ))
654 return ~0UL;
655 return x * (HZ / USER_HZ);
656#else
657
658 if (x >= ~0UL / HZ * USER_HZ)
659 return ~0UL;
660
661
662 return div_u64((u64)x * HZ, USER_HZ);
663#endif
664}
665EXPORT_SYMBOL(clock_t_to_jiffies);
666
667u64 jiffies_64_to_clock_t(u64 x)
668{
669#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
670# if HZ < USER_HZ
671 x = div_u64(x * USER_HZ, HZ);
672# elif HZ > USER_HZ
673 x = div_u64(x, HZ / USER_HZ);
674# else
675
676# endif
677#else
678
679
680
681
682
683 x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
684#endif
685 return x;
686}
687EXPORT_SYMBOL(jiffies_64_to_clock_t);
688
689u64 nsec_to_clock_t(u64 x)
690{
691#if (NSEC_PER_SEC % USER_HZ) == 0
692 return div_u64(x, NSEC_PER_SEC / USER_HZ);
693#elif (USER_HZ % 512) == 0
694 return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
695#else
696
697
698
699
700
701 return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
702#endif
703}
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718u64 nsecs_to_jiffies64(u64 n)
719{
720#if (NSEC_PER_SEC % HZ) == 0
721
722 return div_u64(n, NSEC_PER_SEC / HZ);
723#elif (HZ % 512) == 0
724
725 return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
726#else
727
728
729
730
731 return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
732#endif
733}
734EXPORT_SYMBOL(nsecs_to_jiffies64);
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749unsigned long nsecs_to_jiffies(u64 n)
750{
751 return (unsigned long)nsecs_to_jiffies64(n);
752}
753EXPORT_SYMBOL_GPL(nsecs_to_jiffies);
754
755
756
757
758
759struct timespec timespec_add_safe(const struct timespec lhs,
760 const struct timespec rhs)
761{
762 struct timespec res;
763
764 set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
765 lhs.tv_nsec + rhs.tv_nsec);
766
767 if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
768 res.tv_sec = TIME_T_MAX;
769
770 return res;
771}
772
773
774
775
776
777
778struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
779 const struct timespec64 rhs)
780{
781 struct timespec64 res;
782
783 set_normalized_timespec64(&res, lhs.tv_sec + rhs.tv_sec,
784 lhs.tv_nsec + rhs.tv_nsec);
785
786 if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) {
787 res.tv_sec = TIME64_MAX;
788 res.tv_nsec = 0;
789 }
790
791 return res;
792}
793