pvclock.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef _ASM_X86_PVCLOCK_H
  2. #define _ASM_X86_PVCLOCK_H
  3. #include <linux/clocksource.h>
  4. #include <asm/pvclock-abi.h>
  5. #ifdef CONFIG_KVM_GUEST
  6. extern struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void);
  7. #else
  8. static inline struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void)
  9. {
  10. return NULL;
  11. }
  12. #endif
  13. /* some helper functions for xen and kvm pv clock sources */
  14. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
  15. u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
  16. void pvclock_set_flags(u8 flags);
  17. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
  18. void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
  19. struct pvclock_vcpu_time_info *vcpu,
  20. struct timespec *ts);
  21. void pvclock_resume(void);
  22. void pvclock_touch_watchdogs(void);
  23. static __always_inline
  24. unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
  25. {
  26. unsigned version = src->version & ~1;
  27. /* Make sure that the version is read before the data. */
  28. virt_rmb();
  29. return version;
  30. }
  31. static __always_inline
  32. bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
  33. unsigned version)
  34. {
  35. /* Make sure that the version is re-read after the data. */
  36. virt_rmb();
  37. return unlikely(version != src->version);
  38. }
  39. /*
  40. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  41. * yielding a 64-bit result.
  42. */
  43. static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
  44. {
  45. u64 product;
  46. #ifdef __i386__
  47. u32 tmp1, tmp2;
  48. #else
  49. ulong tmp;
  50. #endif
  51. if (shift < 0)
  52. delta >>= -shift;
  53. else
  54. delta <<= shift;
  55. #ifdef __i386__
  56. __asm__ (
  57. "mul %5 ; "
  58. "mov %4,%%eax ; "
  59. "mov %%edx,%4 ; "
  60. "mul %5 ; "
  61. "xor %5,%5 ; "
  62. "add %4,%%eax ; "
  63. "adc %5,%%edx ; "
  64. : "=A" (product), "=r" (tmp1), "=r" (tmp2)
  65. : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
  66. #elif defined(__x86_64__)
  67. __asm__ (
  68. "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
  69. : [lo]"=a"(product),
  70. [hi]"=d"(tmp)
  71. : "0"(delta),
  72. [mul_frac]"rm"((u64)mul_frac));
  73. #else
  74. #error implement me!
  75. #endif
  76. return product;
  77. }
  78. static __always_inline
  79. cycle_t __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
  80. u64 tsc)
  81. {
  82. u64 delta = tsc - src->tsc_timestamp;
  83. cycle_t offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
  84. src->tsc_shift);
  85. return src->system_time + offset;
  86. }
  87. struct pvclock_vsyscall_time_info {
  88. struct pvclock_vcpu_time_info pvti;
  89. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  90. #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
  91. #endif /* _ASM_X86_PVCLOCK_H */