pvclock.h 2.5 KB

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