vclock_gettime.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <linux/kernel.h>
  12. #include <linux/posix-timers.h>
  13. #include <linux/time.h>
  14. #include <linux/string.h>
  15. #include <asm/vsyscall.h>
  16. #include <asm/vgtod.h>
  17. #include <asm/timex.h>
  18. #include <asm/hpet.h>
  19. #include <asm/unistd.h>
  20. #include <asm/io.h>
  21. #include <asm/vgtod.h>
  22. #include "vextern.h"
  23. #define gtod vdso_vsyscall_gtod_data
  24. 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. 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. 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. /* Copy of the version in kernel/time.c which we cannot directly access */
  52. static void vset_normalized_timespec(struct timespec *ts, long sec, long nsec)
  53. {
  54. while (nsec >= NSEC_PER_SEC) {
  55. nsec -= NSEC_PER_SEC;
  56. ++sec;
  57. }
  58. while (nsec < 0) {
  59. nsec += NSEC_PER_SEC;
  60. --sec;
  61. }
  62. ts->tv_sec = sec;
  63. ts->tv_nsec = nsec;
  64. }
  65. static noinline int do_monotonic(struct timespec *ts)
  66. {
  67. unsigned long seq, ns, secs;
  68. do {
  69. seq = read_seqbegin(&gtod->lock);
  70. secs = gtod->wall_time_sec;
  71. ns = gtod->wall_time_nsec + vgetns();
  72. secs += gtod->wall_to_monotonic.tv_sec;
  73. ns += gtod->wall_to_monotonic.tv_nsec;
  74. } while (unlikely(read_seqretry(&gtod->lock, seq)));
  75. vset_normalized_timespec(ts, secs, ns);
  76. return 0;
  77. }
  78. int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
  79. {
  80. if (likely(gtod->sysctl_enabled && gtod->clock.vread))
  81. switch (clock) {
  82. case CLOCK_REALTIME:
  83. return do_realtime(ts);
  84. case CLOCK_MONOTONIC:
  85. return do_monotonic(ts);
  86. }
  87. return vdso_fallback_gettime(clock, ts);
  88. }
  89. int clock_gettime(clockid_t, struct timespec *)
  90. __attribute__((weak, alias("__vdso_clock_gettime")));
  91. int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  92. {
  93. long ret;
  94. if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
  95. BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
  96. offsetof(struct timespec, tv_nsec) ||
  97. sizeof(*tv) != sizeof(struct timespec));
  98. do_realtime((struct timespec *)tv);
  99. tv->tv_usec /= 1000;
  100. if (unlikely(tz != NULL)) {
  101. /* This relies on gcc inlining the memcpy. We'll notice
  102. if it ever fails to do so. */
  103. memcpy(tz, &gtod->sys_tz, sizeof(struct timezone));
  104. }
  105. return 0;
  106. }
  107. asm("syscall" : "=a" (ret) :
  108. "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
  109. return ret;
  110. }
  111. int gettimeofday(struct timeval *, struct timezone *)
  112. __attribute__((weak, alias("__vdso_gettimeofday")));