vclock_gettime.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, gettimeofday, and time.
  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. #define gtod (&VVAR(vsyscall_gtod_data))
  24. notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
  25. {
  26. long ret;
  27. asm("syscall" : "=a" (ret) :
  28. "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
  29. return ret;
  30. }
  31. notrace static inline long vgetns(void)
  32. {
  33. long v;
  34. cycles_t (*vread)(void);
  35. vread = gtod->clock.vread;
  36. v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
  37. return (v * gtod->clock.mult) >> gtod->clock.shift;
  38. }
  39. notrace static noinline int do_realtime(struct timespec *ts)
  40. {
  41. unsigned long seq, ns;
  42. do {
  43. seq = read_seqbegin(&gtod->lock);
  44. ts->tv_sec = gtod->wall_time_sec;
  45. ts->tv_nsec = gtod->wall_time_nsec;
  46. ns = vgetns();
  47. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  48. timespec_add_ns(ts, ns);
  49. return 0;
  50. }
  51. notrace static noinline int do_monotonic(struct timespec *ts)
  52. {
  53. unsigned long seq, ns, secs;
  54. do {
  55. seq = read_seqbegin(&gtod->lock);
  56. secs = gtod->wall_time_sec;
  57. ns = gtod->wall_time_nsec + vgetns();
  58. secs += gtod->wall_to_monotonic.tv_sec;
  59. ns += gtod->wall_to_monotonic.tv_nsec;
  60. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  61. /* wall_time_nsec, vgetns(), and wall_to_monotonic.tv_nsec
  62. * are all guaranteed to be nonnegative.
  63. */
  64. while (ns >= NSEC_PER_SEC) {
  65. ns -= NSEC_PER_SEC;
  66. ++secs;
  67. }
  68. ts->tv_sec = secs;
  69. ts->tv_nsec = ns;
  70. return 0;
  71. }
  72. notrace static noinline int do_realtime_coarse(struct timespec *ts)
  73. {
  74. unsigned long seq;
  75. do {
  76. seq = read_seqbegin(&gtod->lock);
  77. ts->tv_sec = gtod->wall_time_coarse.tv_sec;
  78. ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
  79. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  80. return 0;
  81. }
  82. notrace static noinline int do_monotonic_coarse(struct timespec *ts)
  83. {
  84. unsigned long seq, ns, secs;
  85. do {
  86. seq = read_seqbegin(&gtod->lock);
  87. secs = gtod->wall_time_coarse.tv_sec;
  88. ns = gtod->wall_time_coarse.tv_nsec;
  89. secs += gtod->wall_to_monotonic.tv_sec;
  90. ns += gtod->wall_to_monotonic.tv_nsec;
  91. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  92. /* wall_time_nsec and wall_to_monotonic.tv_nsec are
  93. * guaranteed to be between 0 and NSEC_PER_SEC.
  94. */
  95. if (ns >= NSEC_PER_SEC) {
  96. ns -= NSEC_PER_SEC;
  97. ++secs;
  98. }
  99. ts->tv_sec = secs;
  100. ts->tv_nsec = 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")));
  148. /* This will break when the xtime seconds get inaccurate, but that is
  149. * unlikely */
  150. static __always_inline long time_syscall(long *t)
  151. {
  152. long secs;
  153. asm volatile("syscall"
  154. : "=a" (secs)
  155. : "0" (__NR_time), "D" (t) : "cc", "r11", "cx", "memory");
  156. return secs;
  157. }
  158. notrace time_t __vdso_time(time_t *t)
  159. {
  160. time_t result;
  161. if (unlikely(!VVAR(vsyscall_gtod_data).sysctl_enabled))
  162. return time_syscall(t);
  163. /* This is atomic on x86_64 so we don't need any locks. */
  164. result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
  165. if (t)
  166. *t = result;
  167. return result;
  168. }
  169. int time(time_t *t)
  170. __attribute__((weak, alias("__vdso_time")));