pvclock.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /*
  24. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  25. * yielding a 64-bit result.
  26. */
  27. static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
  28. {
  29. u64 product;
  30. #ifdef __i386__
  31. u32 tmp1, tmp2;
  32. #else
  33. ulong tmp;
  34. #endif
  35. if (shift < 0)
  36. delta >>= -shift;
  37. else
  38. delta <<= shift;
  39. #ifdef __i386__
  40. __asm__ (
  41. "mul %5 ; "
  42. "mov %4,%%eax ; "
  43. "mov %%edx,%4 ; "
  44. "mul %5 ; "
  45. "xor %5,%5 ; "
  46. "add %4,%%eax ; "
  47. "adc %5,%%edx ; "
  48. : "=A" (product), "=r" (tmp1), "=r" (tmp2)
  49. : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
  50. #elif defined(__x86_64__)
  51. __asm__ (
  52. "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
  53. : [lo]"=a"(product),
  54. [hi]"=d"(tmp)
  55. : "0"(delta),
  56. [mul_frac]"rm"((u64)mul_frac));
  57. #else
  58. #error implement me!
  59. #endif
  60. return product;
  61. }
  62. static __always_inline
  63. unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
  64. cycle_t *cycles, u8 *flags)
  65. {
  66. unsigned version;
  67. cycle_t offset;
  68. u64 delta;
  69. version = src->version;
  70. /* Make the latest version visible */
  71. smp_rmb();
  72. delta = rdtsc_ordered() - src->tsc_timestamp;
  73. offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
  74. src->tsc_shift);
  75. *cycles = src->system_time + offset;
  76. *flags = src->flags;
  77. return version;
  78. }
  79. struct pvclock_vsyscall_time_info {
  80. struct pvclock_vcpu_time_info pvti;
  81. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  82. #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
  83. #endif /* _ASM_X86_PVCLOCK_H */