pvclock.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_PVCLOCK_H
  3. #define _ASM_X86_PVCLOCK_H
  4. #include <asm/clocksource.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_read_wallclock(struct pvclock_wall_clock *wall,
  12. struct pvclock_vcpu_time_info *vcpu,
  13. struct timespec64 *ts);
  14. void pvclock_resume(void);
  15. void pvclock_touch_watchdogs(void);
  16. static __always_inline
  17. unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
  18. {
  19. unsigned version = src->version & ~1;
  20. /* Make sure that the version is read before the data. */
  21. virt_rmb();
  22. return version;
  23. }
  24. static __always_inline
  25. bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
  26. unsigned version)
  27. {
  28. /* Make sure that the version is re-read after the data. */
  29. virt_rmb();
  30. return unlikely(version != src->version);
  31. }
  32. /*
  33. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  34. * yielding a 64-bit result.
  35. */
  36. static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
  37. {
  38. u64 product;
  39. #ifdef __i386__
  40. u32 tmp1, tmp2;
  41. #else
  42. ulong tmp;
  43. #endif
  44. if (shift < 0)
  45. delta >>= -shift;
  46. else
  47. delta <<= shift;
  48. #ifdef __i386__
  49. __asm__ (
  50. "mul %5 ; "
  51. "mov %4,%%eax ; "
  52. "mov %%edx,%4 ; "
  53. "mul %5 ; "
  54. "xor %5,%5 ; "
  55. "add %4,%%eax ; "
  56. "adc %5,%%edx ; "
  57. : "=A" (product), "=r" (tmp1), "=r" (tmp2)
  58. : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
  59. #elif defined(__x86_64__)
  60. __asm__ (
  61. "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
  62. : [lo]"=a"(product),
  63. [hi]"=d"(tmp)
  64. : "0"(delta),
  65. [mul_frac]"rm"((u64)mul_frac));
  66. #else
  67. #error implement me!
  68. #endif
  69. return product;
  70. }
  71. static __always_inline
  72. u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc)
  73. {
  74. u64 delta = tsc - src->tsc_timestamp;
  75. u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
  76. src->tsc_shift);
  77. return src->system_time + offset;
  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. #ifdef CONFIG_PARAVIRT_CLOCK
  84. void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti);
  85. struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void);
  86. #else
  87. static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
  88. {
  89. return NULL;
  90. }
  91. #endif
  92. #endif /* _ASM_X86_PVCLOCK_H */