vclock_gettime.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2006 Andi Kleen, SUSE Labs.
  3. * Subject to the GNU Public License, v.2
  4. *
  5. * Fast user context implementation of clock_gettime and gettimeofday.
  6. *
  7. * The code should have no internal unresolved relocations.
  8. * Check with readelf after changing.
  9. * Also alternative() doesn't work.
  10. */
  11. /* Disable profiling for userspace code: */
  12. #define DISABLE_BRANCH_PROFILING
  13. #include <linux/kernel.h>
  14. #include <linux/posix-timers.h>
  15. #include <linux/time.h>
  16. #include <linux/string.h>
  17. #include <asm/vsyscall.h>
  18. #include <asm/vgtod.h>
  19. #include <asm/timex.h>
  20. #include <asm/hpet.h>
  21. #include <asm/unistd.h>
  22. #include <asm/io.h>
  23. #include "vextern.h"
  24. #define gtod vdso_vsyscall_gtod_data
  25. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  26. {
  27. long ret;
  28. asm("syscall" : "=a" (ret) :
  29. "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
  30. return ret;
  31. }
  32. notrace static inline long vgetns(void)
  33. {
  34. long v;
  35. cycles_t (*vread)(void);
  36. vread = gtod->clock.vread;
  37. v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
  38. return (v * gtod->clock.mult) >> gtod->clock.shift;
  39. }
  40. notrace static noinline int do_realtime(struct timespec *ts)
  41. {
  42. unsigned long seq, ns;
  43. do {
  44. seq = read_seqbegin(&gtod->lock);
  45. ts->tv_sec = gtod->wall_time_sec;
  46. ts->tv_nsec = gtod->wall_time_nsec;
  47. ns = vgetns();
  48. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  49. timespec_add_ns(ts, ns);
  50. return 0;
  51. }
  52. /* Copy of the version in kernel/time.c which we cannot directly access */
  53. notrace static void
  54. vset_normalized_timespec(struct timespec *ts, long sec, long nsec)
  55. {
  56. while (nsec >= NSEC_PER_SEC) {
  57. nsec -= NSEC_PER_SEC;
  58. ++sec;
  59. }
  60. while (nsec < 0) {
  61. nsec += NSEC_PER_SEC;
  62. --sec;
  63. }
  64. ts->tv_sec = sec;
  65. ts->tv_nsec = nsec;
  66. }
  67. notrace static noinline int do_monotonic(struct timespec *ts)
  68. {
  69. unsigned long seq, ns, secs;
  70. do {
  71. seq = read_seqbegin(&gtod->lock);
  72. secs = gtod->wall_time_sec;
  73. ns = gtod->wall_time_nsec + vgetns();
  74. secs += gtod->wall_to_monotonic.tv_sec;
  75. ns += gtod->wall_to_monotonic.tv_nsec;
  76. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  77. vset_normalized_timespec(ts, secs, ns);
  78. return 0;
  79. }
  80. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  81. {
  82. if (likely(gtod->sysctl_enabled && gtod->clock.vread))
  83. switch (clock) {
  84. case CLOCK_REALTIME:
  85. return do_realtime(ts);
  86. case CLOCK_MONOTONIC:
  87. return do_monotonic(ts);
  88. }
  89. return vdso_fallback_gettime(clock, ts);
  90. }
  91. int clock_gettime(clockid_t, struct timespec *)
  92. __attribute__((weak, alias("__vdso_clock_gettime")));
  93. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  94. {
  95. long ret;
  96. if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
  97. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  98. offsetof(struct timespec, tv_nsec) ||
  99. sizeof(*tv) != sizeof(struct timespec));
  100. do_realtime((struct timespec *)tv);
  101. tv->tv_usec /= 1000;
  102. if (unlikely(tz != NULL)) {
  103. /* Avoid memcpy. Some old compilers fail to inline it */
  104. tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
  105. tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
  106. }
  107. return 0;
  108. }
  109. asm("syscall" : "=a" (ret) :
  110. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  111. return ret;
  112. }
  113. int gettimeofday(struct timeval *, struct timezone *)
  114. __attribute__((weak, alias("__vdso_gettimeofday")));