00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <linux/module.h>
00031 #include <linux/timex.h>
00032 #include <linux/capability.h>
00033 #include <linux/clocksource.h>
00034 #include <linux/errno.h>
00035 #include <linux/syscalls.h>
00036 #include <linux/security.h>
00037 #include <linux/fs.h>
00038 #include <linux/slab.h>
00039 #include <linux/math64.h>
00040 #include <linux/ptrace.h>
00041
00042 #include <asm/uaccess.h>
00043 #include <asm/unistd.h>
00044
00045 #include "timeconst.h"
00046
00047
00048
00049
00050
00051 struct timezone sys_tz;
00052
00053 EXPORT_SYMBOL(sys_tz);
00054
00055 #ifdef __ARCH_WANT_SYS_TIME
00056
00057
00058
00059
00060
00061
00062
00063 SYSCALL_DEFINE1(time, time_t __user *, tloc)
00064 {
00065 time_t i = get_seconds();
00066
00067 if (tloc) {
00068 if (put_user(i,tloc))
00069 return -EFAULT;
00070 }
00071 force_successful_syscall_return();
00072 return i;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082 SYSCALL_DEFINE1(stime, time_t __user *, tptr)
00083 {
00084 struct timespec tv;
00085 int err;
00086
00087 if (get_user(tv.tv_sec, tptr))
00088 return -EFAULT;
00089
00090 tv.tv_nsec = 0;
00091
00092 err = security_settime(&tv, NULL);
00093 if (err)
00094 return err;
00095
00096 do_settimeofday(&tv);
00097 return 0;
00098 }
00099
00100 #endif
00101
00102 SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv,
00103 struct timezone __user *, tz)
00104 {
00105 if (likely(tv != NULL)) {
00106 struct timeval ktv;
00107 do_gettimeofday(&ktv);
00108 if (copy_to_user(tv, &ktv, sizeof(ktv)))
00109 return -EFAULT;
00110 }
00111 if (unlikely(tz != NULL)) {
00112 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
00113 return -EFAULT;
00114 }
00115 return 0;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 static inline void warp_clock(void)
00135 {
00136 write_seqlock_irq(&xtime_lock);
00137 wall_to_monotonic.tv_sec -= sys_tz.tz_minuteswest * 60;
00138 xtime.tv_sec += sys_tz.tz_minuteswest * 60;
00139 update_xtime_cache(0);
00140 write_sequnlock_irq(&xtime_lock);
00141 clock_was_set();
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)
00156 {
00157 static int firsttime = 1;
00158 int error = 0;
00159
00160 if (tv && !timespec_valid(tv))
00161 return -EINVAL;
00162
00163 error = security_settime(tv, tz);
00164 if (error)
00165 return error;
00166
00167 if (tz) {
00168
00169 sys_tz = *tz;
00170 update_vsyscall_tz();
00171 if (firsttime) {
00172 firsttime = 0;
00173 if (!tv)
00174 warp_clock();
00175 }
00176 }
00177 if (tv)
00178 {
00179
00180
00181
00182 return do_settimeofday(tv);
00183 }
00184 return 0;
00185 }
00186
00187 SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
00188 struct timezone __user *, tz)
00189 {
00190 struct timeval user_tv;
00191 struct timespec new_ts;
00192 struct timezone new_tz;
00193
00194 if (tv) {
00195 if (copy_from_user(&user_tv, tv, sizeof(*tv)))
00196 return -EFAULT;
00197 new_ts.tv_sec = user_tv.tv_sec;
00198 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
00199 }
00200 if (tz) {
00201 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
00202 return -EFAULT;
00203 }
00204
00205 return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
00206 }
00207
00208 SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
00209 {
00210 struct timex txc;
00211 int ret;
00212
00213
00214
00215
00216
00217 if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
00218 return -EFAULT;
00219 ret = do_adjtimex(&txc);
00220 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
00221 }
00222
00223 #ifndef DDE_LINUX
00224
00225
00226
00227
00228
00229
00230
00231 struct timespec current_fs_time(struct super_block *sb)
00232 {
00233 struct timespec now = current_kernel_time();
00234 return timespec_trunc(now, sb->s_time_gran);
00235 }
00236 EXPORT_SYMBOL(current_fs_time);
00237
00238
00239
00240
00241
00242
00243
00244 unsigned int inline jiffies_to_msecs(const unsigned long j)
00245 {
00246 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
00247 return (MSEC_PER_SEC / HZ) * j;
00248 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
00249 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
00250 #else
00251 # if BITS_PER_LONG == 32
00252 return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
00253 # else
00254 return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
00255 # endif
00256 #endif
00257 }
00258 EXPORT_SYMBOL(jiffies_to_msecs);
00259
00260 unsigned int inline jiffies_to_usecs(const unsigned long j)
00261 {
00262 #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
00263 return (USEC_PER_SEC / HZ) * j;
00264 #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
00265 return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
00266 #else
00267 # if BITS_PER_LONG == 32
00268 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
00269 # else
00270 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
00271 # endif
00272 #endif
00273 }
00274 EXPORT_SYMBOL(jiffies_to_usecs);
00275 #endif
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 struct timespec timespec_trunc(struct timespec t, unsigned gran)
00290 {
00291
00292
00293
00294
00295
00296 if (gran <= jiffies_to_usecs(1) * 1000) {
00297
00298 } else if (gran == 1000000000) {
00299 t.tv_nsec = 0;
00300 } else {
00301 t.tv_nsec -= t.tv_nsec % gran;
00302 }
00303 return t;
00304 }
00305 EXPORT_SYMBOL(timespec_trunc);
00306
00307 #ifndef CONFIG_GENERIC_TIME
00308
00309
00310
00311
00312 void getnstimeofday(struct timespec *tv)
00313 {
00314 struct timeval x;
00315
00316 do_gettimeofday(&x);
00317 tv->tv_sec = x.tv_sec;
00318 tv->tv_nsec = x.tv_usec * NSEC_PER_USEC;
00319 }
00320 EXPORT_SYMBOL_GPL(getnstimeofday);
00321 #endif
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 unsigned long
00339 mktime(const unsigned int year0, const unsigned int mon0,
00340 const unsigned int day, const unsigned int hour,
00341 const unsigned int min, const unsigned int sec)
00342 {
00343 unsigned int mon = mon0, year = year0;
00344
00345
00346 if (0 >= (int) (mon -= 2)) {
00347 mon += 12;
00348 year -= 1;
00349 }
00350
00351 return ((((unsigned long)
00352 (year/4 - year/100 + year/400 + 367*mon/12 + day) +
00353 year*365 - 719499
00354 )*24 + hour
00355 )*60 + min
00356 )*60 + sec;
00357 }
00358
00359 EXPORT_SYMBOL(mktime);
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec)
00376 {
00377 while (nsec >= NSEC_PER_SEC) {
00378 nsec -= NSEC_PER_SEC;
00379 ++sec;
00380 }
00381 while (nsec < 0) {
00382 nsec += NSEC_PER_SEC;
00383 --sec;
00384 }
00385 ts->tv_sec = sec;
00386 ts->tv_nsec = nsec;
00387 }
00388 EXPORT_SYMBOL(set_normalized_timespec);
00389
00390
00391
00392
00393
00394
00395
00396 struct timespec ns_to_timespec(const s64 nsec)
00397 {
00398 struct timespec ts;
00399 s32 rem;
00400
00401 if (!nsec)
00402 return (struct timespec) {0, 0};
00403
00404 ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
00405 if (unlikely(rem < 0)) {
00406 ts.tv_sec--;
00407 rem += NSEC_PER_SEC;
00408 }
00409 ts.tv_nsec = rem;
00410
00411 return ts;
00412 }
00413 EXPORT_SYMBOL(ns_to_timespec);
00414
00415
00416
00417
00418
00419
00420
00421 struct timeval ns_to_timeval(const s64 nsec)
00422 {
00423 struct timespec ts = ns_to_timespec(nsec);
00424 struct timeval tv;
00425
00426 tv.tv_sec = ts.tv_sec;
00427 tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
00428
00429 return tv;
00430 }
00431 EXPORT_SYMBOL(ns_to_timeval);
00432
00433 #ifndef DDE_LINUX
00434
00435
00436
00437
00438
00439
00440 unsigned int jiffies_to_msecs(const unsigned long j)
00441 {
00442 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
00443 return (MSEC_PER_SEC / HZ) * j;
00444 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
00445 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
00446 #else
00447 return (j * MSEC_PER_SEC) / HZ;
00448 #endif
00449 }
00450 EXPORT_SYMBOL(jiffies_to_msecs);
00451
00452 unsigned int jiffies_to_usecs(const unsigned long j)
00453 {
00454 #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
00455 return (USEC_PER_SEC / HZ) * j;
00456 #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
00457 return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
00458 #else
00459 return (j * USEC_PER_SEC) / HZ;
00460 #endif
00461 }
00462 EXPORT_SYMBOL(jiffies_to_usecs);
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 unsigned long msecs_to_jiffies(const unsigned int m)
00479 {
00480
00481
00482
00483 if ((int)m < 0)
00484 return MAX_JIFFY_OFFSET;
00485
00486 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
00487
00488
00489
00490
00491
00492 return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
00493 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
00494
00495
00496
00497
00498
00499
00500
00501 if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
00502 return MAX_JIFFY_OFFSET;
00503
00504 return m * (HZ / MSEC_PER_SEC);
00505 #else
00506
00507
00508
00509
00510
00511 if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
00512 return MAX_JIFFY_OFFSET;
00513
00514 return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32)
00515 >> MSEC_TO_HZ_SHR32;
00516 #endif
00517 }
00518 EXPORT_SYMBOL(msecs_to_jiffies);
00519
00520 unsigned long usecs_to_jiffies(const unsigned int u)
00521 {
00522 if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
00523 return MAX_JIFFY_OFFSET;
00524 #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
00525 return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ);
00526 #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
00527 return u * (HZ / USEC_PER_SEC);
00528 #else
00529 return (USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32)
00530 >> USEC_TO_HZ_SHR32;
00531 #endif
00532 }
00533 EXPORT_SYMBOL(usecs_to_jiffies);
00534 #else
00535 unsigned int jiffies_to_msecs(const unsigned long j)
00536 {
00537 return (j*1000) / HZ;
00538 }
00539 EXPORT_SYMBOL(jiffies_to_msecs);
00540
00541 unsigned int jiffies_to_usecs(const unsigned long j)
00542 {
00543 return (j*1000000) / HZ;
00544 }
00545 EXPORT_SYMBOL(jiffies_to_usecs);
00546
00547 unsigned long msecs_to_jiffies(const unsigned int m)
00548 {
00549 if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
00550 return MAX_JIFFY_OFFSET;
00551 return (m * HZ + MSEC_PER_SEC) / MSEC_PER_SEC;
00552 }
00553 EXPORT_SYMBOL(msecs_to_jiffies);
00554
00555 unsigned long usecs_to_jiffies(const unsigned int u)
00556 {
00557 if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
00558 return MAX_JIFFY_OFFSET;
00559 return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC;
00560 }
00561 EXPORT_SYMBOL(usecs_to_jiffies);
00562 #endif
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 unsigned long
00576 timespec_to_jiffies(const struct timespec *value)
00577 {
00578 unsigned long sec = value->tv_sec;
00579 long nsec = value->tv_nsec + TICK_NSEC - 1;
00580
00581 if (sec >= MAX_SEC_IN_JIFFIES){
00582 sec = MAX_SEC_IN_JIFFIES;
00583 nsec = 0;
00584 }
00585 return (((u64)sec * SEC_CONVERSION) +
00586 (((u64)nsec * NSEC_CONVERSION) >>
00587 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
00588
00589 }
00590 EXPORT_SYMBOL(timespec_to_jiffies);
00591
00592 void
00593 jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
00594 {
00595
00596
00597
00598
00599 u32 rem;
00600 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
00601 NSEC_PER_SEC, &rem);
00602 value->tv_nsec = rem;
00603 }
00604 EXPORT_SYMBOL(jiffies_to_timespec);
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 unsigned long
00619 timeval_to_jiffies(const struct timeval *value)
00620 {
00621 unsigned long sec = value->tv_sec;
00622 long usec = value->tv_usec;
00623
00624 if (sec >= MAX_SEC_IN_JIFFIES){
00625 sec = MAX_SEC_IN_JIFFIES;
00626 usec = 0;
00627 }
00628 return (((u64)sec * SEC_CONVERSION) +
00629 (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
00630 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
00631 }
00632 EXPORT_SYMBOL(timeval_to_jiffies);
00633
00634 void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
00635 {
00636
00637
00638
00639
00640 u32 rem;
00641
00642 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
00643 NSEC_PER_SEC, &rem);
00644 value->tv_usec = rem / NSEC_PER_USEC;
00645 }
00646 EXPORT_SYMBOL(jiffies_to_timeval);
00647
00648
00649
00650
00651 clock_t jiffies_to_clock_t(long x)
00652 {
00653 #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
00654 # if HZ < USER_HZ
00655 return x * (USER_HZ / HZ);
00656 # else
00657 return x / (HZ / USER_HZ);
00658 # endif
00659 #else
00660 return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
00661 #endif
00662 }
00663 EXPORT_SYMBOL(jiffies_to_clock_t);
00664
00665 #ifndef DDE_LINUX
00666 unsigned long clock_t_to_jiffies(unsigned long x)
00667 {
00668 #if (HZ % USER_HZ)==0
00669 if (x >= ~0UL / (HZ / USER_HZ))
00670 return ~0UL;
00671 return x * (HZ / USER_HZ);
00672 #else
00673
00674 if (x >= ~0UL / HZ * USER_HZ)
00675 return ~0UL;
00676
00677
00678 return div_u64((u64)x * HZ, USER_HZ);
00679 #endif
00680 }
00681 #else
00682 unsigned long clock_t_to_jiffies(unsigned long x)
00683 {
00684 if (x >= ~0UL / (HZ / USER_HZ))
00685 return ~0UL;
00686 return x * (HZ / USER_HZ);
00687 }
00688 #endif
00689 EXPORT_SYMBOL(clock_t_to_jiffies);
00690
00691 u64 jiffies_64_to_clock_t(u64 x)
00692 {
00693 #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
00694 # if HZ < USER_HZ
00695 x = div_u64(x * USER_HZ, HZ);
00696 # elif HZ > USER_HZ
00697 x = div_u64(x, HZ / USER_HZ);
00698 # else
00699
00700 # endif
00701 #else
00702
00703
00704
00705
00706
00707 x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
00708 #endif
00709 return x;
00710 }
00711 EXPORT_SYMBOL(jiffies_64_to_clock_t);
00712
00713 u64 nsec_to_clock_t(u64 x)
00714 {
00715 #if (NSEC_PER_SEC % USER_HZ) == 0
00716 return div_u64(x, NSEC_PER_SEC / USER_HZ);
00717 #elif (USER_HZ % 512) == 0
00718 return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
00719 #else
00720
00721
00722
00723
00724
00725 return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
00726 #endif
00727 }
00728
00729 #if (BITS_PER_LONG < 64)
00730 u64 get_jiffies_64(void)
00731 {
00732 unsigned long seq;
00733 u64 ret;
00734
00735 do {
00736 seq = read_seqbegin(&xtime_lock);
00737 ret = jiffies_64;
00738 } while (read_seqretry(&xtime_lock, seq));
00739 return ret;
00740 }
00741 EXPORT_SYMBOL(get_jiffies_64);
00742 #endif
00743
00744 EXPORT_SYMBOL(jiffies);
00745
00746
00747
00748
00749
00750 struct timespec timespec_add_safe(const struct timespec lhs,
00751 const struct timespec rhs)
00752 {
00753 struct timespec res;
00754
00755 set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
00756 lhs.tv_nsec + rhs.tv_nsec);
00757
00758 if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
00759 res.tv_sec = TIME_T_MAX;
00760
00761 return res;
00762 }