pvclock.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* paravirtual clock -- common code used by kvm/xen
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/percpu.h>
  16. #include <asm/pvclock.h>
  17. /*
  18. * These are perodically updated
  19. * xen: magic shared_info page
  20. * kvm: gpa registered via msr
  21. * and then copied here.
  22. */
  23. struct pvclock_shadow_time {
  24. u64 tsc_timestamp; /* TSC at last update of time vals. */
  25. u64 system_timestamp; /* Time, in nanosecs, since boot. */
  26. u32 tsc_to_nsec_mul;
  27. int tsc_shift;
  28. u32 version;
  29. };
  30. /*
  31. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  32. * yielding a 64-bit result.
  33. */
  34. static inline u64 scale_delta(u64 delta, u32 mul_frac, int shift)
  35. {
  36. u64 product;
  37. #ifdef __i386__
  38. u32 tmp1, tmp2;
  39. #endif
  40. if (shift < 0)
  41. delta >>= -shift;
  42. else
  43. delta <<= shift;
  44. #ifdef __i386__
  45. __asm__ (
  46. "mul %5 ; "
  47. "mov %4,%%eax ; "
  48. "mov %%edx,%4 ; "
  49. "mul %5 ; "
  50. "xor %5,%5 ; "
  51. "add %4,%%eax ; "
  52. "adc %5,%%edx ; "
  53. : "=A" (product), "=r" (tmp1), "=r" (tmp2)
  54. : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
  55. #elif __x86_64__
  56. __asm__ (
  57. "mul %%rdx ; shrd $32,%%rdx,%%rax"
  58. : "=a" (product) : "0" (delta), "d" ((u64)mul_frac) );
  59. #else
  60. #error implement me!
  61. #endif
  62. return product;
  63. }
  64. static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
  65. {
  66. u64 delta = native_read_tsc() - shadow->tsc_timestamp;
  67. return scale_delta(delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift);
  68. }
  69. /*
  70. * Reads a consistent set of time-base values from hypervisor,
  71. * into a shadow data area.
  72. */
  73. static unsigned pvclock_get_time_values(struct pvclock_shadow_time *dst,
  74. struct pvclock_vcpu_time_info *src)
  75. {
  76. do {
  77. dst->version = src->version;
  78. rmb(); /* fetch version before data */
  79. dst->tsc_timestamp = src->tsc_timestamp;
  80. dst->system_timestamp = src->system_time;
  81. dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
  82. dst->tsc_shift = src->tsc_shift;
  83. rmb(); /* test version after fetching data */
  84. } while ((src->version & 1) || (dst->version != src->version));
  85. return dst->version;
  86. }
  87. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  88. {
  89. struct pvclock_shadow_time shadow;
  90. unsigned version;
  91. cycle_t ret, offset;
  92. do {
  93. version = pvclock_get_time_values(&shadow, src);
  94. barrier();
  95. offset = pvclock_get_nsec_offset(&shadow);
  96. ret = shadow.system_timestamp + offset;
  97. barrier();
  98. } while (version != src->version);
  99. return ret;
  100. }
  101. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  102. struct pvclock_vcpu_time_info *vcpu_time,
  103. struct timespec *ts)
  104. {
  105. u32 version;
  106. u64 delta;
  107. struct timespec now;
  108. /* get wallclock at system boot */
  109. do {
  110. version = wall_clock->version;
  111. rmb(); /* fetch version before time */
  112. now.tv_sec = wall_clock->sec;
  113. now.tv_nsec = wall_clock->nsec;
  114. rmb(); /* fetch time before checking version */
  115. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  116. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  117. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  118. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  119. now.tv_sec = delta;
  120. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  121. }