vgettimeofday.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define VDSO_BUILD /* avoid some shift warnings for -m32 in <asm/page.h> */
  15. #include <linux/time.h>
  16. #include <asm/timex.h>
  17. #include <asm/unistd.h>
  18. #include <asm/vdso.h>
  19. #if CHIP_HAS_SPLIT_CYCLE()
  20. static inline cycles_t get_cycles_inline(void)
  21. {
  22. unsigned int high = __insn_mfspr(SPR_CYCLE_HIGH);
  23. unsigned int low = __insn_mfspr(SPR_CYCLE_LOW);
  24. unsigned int high2 = __insn_mfspr(SPR_CYCLE_HIGH);
  25. while (unlikely(high != high2)) {
  26. low = __insn_mfspr(SPR_CYCLE_LOW);
  27. high = high2;
  28. high2 = __insn_mfspr(SPR_CYCLE_HIGH);
  29. }
  30. return (((cycles_t)high) << 32) | low;
  31. }
  32. #define get_cycles get_cycles_inline
  33. #endif
  34. struct syscall_return_value {
  35. long value;
  36. long error;
  37. };
  38. /*
  39. * Find out the vDSO data page address in the process address space.
  40. */
  41. inline unsigned long get_datapage(void)
  42. {
  43. unsigned long ret;
  44. /* vdso data page located in the 2nd vDSO page. */
  45. asm volatile ("lnk %0" : "=r"(ret));
  46. ret &= ~(PAGE_SIZE - 1);
  47. ret += PAGE_SIZE;
  48. return ret;
  49. }
  50. static inline u64 vgetsns(struct vdso_data *vdso)
  51. {
  52. return ((get_cycles() - vdso->cycle_last) & vdso->mask) * vdso->mult;
  53. }
  54. static inline int do_realtime(struct vdso_data *vdso, struct timespec *ts)
  55. {
  56. unsigned count;
  57. u64 ns;
  58. do {
  59. count = raw_read_seqcount_begin(&vdso->tb_seq);
  60. ts->tv_sec = vdso->wall_time_sec;
  61. ns = vdso->wall_time_snsec;
  62. ns += vgetsns(vdso);
  63. ns >>= vdso->shift;
  64. } while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
  65. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  66. ts->tv_nsec = ns;
  67. return 0;
  68. }
  69. static inline int do_monotonic(struct vdso_data *vdso, struct timespec *ts)
  70. {
  71. unsigned count;
  72. u64 ns;
  73. do {
  74. count = raw_read_seqcount_begin(&vdso->tb_seq);
  75. ts->tv_sec = vdso->monotonic_time_sec;
  76. ns = vdso->monotonic_time_snsec;
  77. ns += vgetsns(vdso);
  78. ns >>= vdso->shift;
  79. } while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
  80. ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  81. ts->tv_nsec = ns;
  82. return 0;
  83. }
  84. static inline int do_realtime_coarse(struct vdso_data *vdso,
  85. struct timespec *ts)
  86. {
  87. unsigned count;
  88. do {
  89. count = raw_read_seqcount_begin(&vdso->tb_seq);
  90. ts->tv_sec = vdso->wall_time_coarse_sec;
  91. ts->tv_nsec = vdso->wall_time_coarse_nsec;
  92. } while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
  93. return 0;
  94. }
  95. static inline int do_monotonic_coarse(struct vdso_data *vdso,
  96. struct timespec *ts)
  97. {
  98. unsigned count;
  99. do {
  100. count = raw_read_seqcount_begin(&vdso->tb_seq);
  101. ts->tv_sec = vdso->monotonic_time_coarse_sec;
  102. ts->tv_nsec = vdso->monotonic_time_coarse_nsec;
  103. } while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
  104. return 0;
  105. }
  106. struct syscall_return_value __vdso_gettimeofday(struct timeval *tv,
  107. struct timezone *tz)
  108. {
  109. struct syscall_return_value ret = { 0, 0 };
  110. unsigned count;
  111. struct vdso_data *vdso = (struct vdso_data *)get_datapage();
  112. /* The use of the timezone is obsolete, normally tz is NULL. */
  113. if (unlikely(tz != NULL)) {
  114. do {
  115. count = raw_read_seqcount_begin(&vdso->tz_seq);
  116. tz->tz_minuteswest = vdso->tz_minuteswest;
  117. tz->tz_dsttime = vdso->tz_dsttime;
  118. } while (unlikely(read_seqcount_retry(&vdso->tz_seq, count)));
  119. }
  120. if (unlikely(tv == NULL))
  121. return ret;
  122. do_realtime(vdso, (struct timespec *)tv);
  123. tv->tv_usec /= 1000;
  124. return ret;
  125. }
  126. int gettimeofday(struct timeval *tv, struct timezone *tz)
  127. __attribute__((weak, alias("__vdso_gettimeofday")));
  128. static struct syscall_return_value vdso_fallback_gettime(long clock,
  129. struct timespec *ts)
  130. {
  131. struct syscall_return_value ret;
  132. __asm__ __volatile__ (
  133. "swint1"
  134. : "=R00" (ret.value), "=R01" (ret.error)
  135. : "R10" (__NR_clock_gettime), "R00" (clock), "R01" (ts)
  136. : "r2", "r3", "r4", "r5", "r6", "r7",
  137. "r8", "r9", "r11", "r12", "r13", "r14", "r15",
  138. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  139. "r24", "r25", "r26", "r27", "r28", "r29", "memory");
  140. return ret;
  141. }
  142. struct syscall_return_value __vdso_clock_gettime(clockid_t clock,
  143. struct timespec *ts)
  144. {
  145. struct vdso_data *vdso = (struct vdso_data *)get_datapage();
  146. struct syscall_return_value ret = { 0, 0 };
  147. switch (clock) {
  148. case CLOCK_REALTIME:
  149. do_realtime(vdso, ts);
  150. return ret;
  151. case CLOCK_MONOTONIC:
  152. do_monotonic(vdso, ts);
  153. return ret;
  154. case CLOCK_REALTIME_COARSE:
  155. do_realtime_coarse(vdso, ts);
  156. return ret;
  157. case CLOCK_MONOTONIC_COARSE:
  158. do_monotonic_coarse(vdso, ts);
  159. return ret;
  160. default:
  161. return vdso_fallback_gettime(clock, ts);
  162. }
  163. }
  164. int clock_gettime(clockid_t clock, struct timespec *ts)
  165. __attribute__((weak, alias("__vdso_clock_gettime")));