vclock_gettime.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 static noinline int do_realtime_coarse(struct timespec *ts)
  81. {
  82. unsigned long seq;
  83. do {
  84. seq = read_seqbegin(&gtod->lock);
  85. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  86. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  87. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  88. return 0;
  89. }
  90. notrace static noinline int do_monotonic_coarse(struct timespec *ts)
  91. {
  92. unsigned long seq, ns, secs;
  93. do {
  94. seq = read_seqbegin(&gtod->lock);
  95. secs = gtod->wall_time_coarse.tv_sec;
  96. ns = gtod->wall_time_coarse.tv_nsec;
  97. secs += gtod->wall_to_monotonic.tv_sec;
  98. ns += gtod->wall_to_monotonic.tv_nsec;
  99. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  100. vset_normalized_timespec(ts, secs, ns);
  101. return 0;
  102. }
  103. notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  104. {
  105. if (likely(gtod->sysctl_enabled))
  106. switch (clock) {
  107. case CLOCK_REALTIME:
  108. if (likely(gtod->clock.vread))
  109. return do_realtime(ts);
  110. break;
  111. case CLOCK_MONOTONIC:
  112. if (likely(gtod->clock.vread))
  113. return do_monotonic(ts);
  114. break;
  115. case CLOCK_REALTIME_COARSE:
  116. return do_realtime_coarse(ts);
  117. case CLOCK_MONOTONIC_COARSE:
  118. return do_monotonic_coarse(ts);
  119. }
  120. return vdso_fallback_gettime(clock, ts);
  121. }
  122. int clock_gettime(clockid_t, struct timespec *)
  123. __attribute__((weak, alias("__vdso_clock_gettime")));
  124. notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  125. {
  126. long ret;
  127. if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
  128. if (likely(tv != NULL)) {
  129. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  130. offsetof(struct timespec, tv_nsec) ||
  131. sizeof(*tv) != sizeof(struct timespec));
  132. do_realtime((struct timespec *)tv);
  133. tv->tv_usec /= 1000;
  134. }
  135. if (unlikely(tz != NULL)) {
  136. /* Avoid memcpy. Some old compilers fail to inline it */
  137. tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
  138. tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
  139. }
  140. return 0;
  141. }
  142. asm("syscall" : "=a" (ret) :
  143. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  144. return ret;
  145. }
  146. int gettimeofday(struct timeval *, struct timezone *)
  147. __attribute__((weak, alias("__vdso_gettimeofday")));