Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_PVCLOCK_H
3#define _ASM_X86_PVCLOCK_H
4
5#include <asm/clocksource.h>
6#include <asm/pvclock-abi.h>
7
8/* some helper functions for xen and kvm pv clock sources */
9u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
10u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
11void pvclock_set_flags(u8 flags);
12unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
13void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
14 struct pvclock_vcpu_time_info *vcpu,
15 struct timespec64 *ts);
16void pvclock_resume(void);
17
18void pvclock_touch_watchdogs(void);
19
20static __always_inline
21unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
22{
23 unsigned version = src->version & ~1;
24 /* Make sure that the version is read before the data. */
25 virt_rmb();
26 return version;
27}
28
29static __always_inline
30bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
31 unsigned version)
32{
33 /* Make sure that the version is re-read after the data. */
34 virt_rmb();
35 return unlikely(version != src->version);
36}
37
38/*
39 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
40 * yielding a 64-bit result.
41 */
42static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
43{
44 u64 product;
45#ifdef __i386__
46 u32 tmp1, tmp2;
47#else
48 ulong tmp;
49#endif
50
51 if (shift < 0)
52 delta >>= -shift;
53 else
54 delta <<= shift;
55
56#ifdef __i386__
57 __asm__ (
58 "mul %5 ; "
59 "mov %4,%%eax ; "
60 "mov %%edx,%4 ; "
61 "mul %5 ; "
62 "xor %5,%5 ; "
63 "add %4,%%eax ; "
64 "adc %5,%%edx ; "
65 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
66 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
67#elif defined(__x86_64__)
68 __asm__ (
69 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
70 : [lo]"=a"(product),
71 [hi]"=d"(tmp)
72 : "0"(delta),
73 [mul_frac]"rm"((u64)mul_frac));
74#else
75#error implement me!
76#endif
77
78 return product;
79}
80
81static __always_inline
82u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc)
83{
84 u64 delta = tsc - src->tsc_timestamp;
85 u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
86 src->tsc_shift);
87 return src->system_time + offset;
88}
89
90struct pvclock_vsyscall_time_info {
91 struct pvclock_vcpu_time_info pvti;
92} __attribute__((__aligned__(SMP_CACHE_BYTES)));
93
94#define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
95
96#ifdef CONFIG_PARAVIRT_CLOCK
97void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti);
98struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void);
99#else
100static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
101{
102 return NULL;
103}
104#endif
105
106#endif /* _ASM_X86_PVCLOCK_H */
1#ifndef _ASM_X86_PVCLOCK_H
2#define _ASM_X86_PVCLOCK_H
3
4#include <linux/clocksource.h>
5#include <asm/pvclock-abi.h>
6
7/* some helper functions for xen and kvm pv clock sources */
8cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
9u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
10void pvclock_set_flags(u8 flags);
11unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
12void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
13 struct pvclock_vcpu_time_info *vcpu,
14 struct timespec *ts);
15void pvclock_resume(void);
16
17void pvclock_touch_watchdogs(void);
18
19/*
20 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
21 * yielding a 64-bit result.
22 */
23static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
24{
25 u64 product;
26#ifdef __i386__
27 u32 tmp1, tmp2;
28#else
29 ulong tmp;
30#endif
31
32 if (shift < 0)
33 delta >>= -shift;
34 else
35 delta <<= shift;
36
37#ifdef __i386__
38 __asm__ (
39 "mul %5 ; "
40 "mov %4,%%eax ; "
41 "mov %%edx,%4 ; "
42 "mul %5 ; "
43 "xor %5,%5 ; "
44 "add %4,%%eax ; "
45 "adc %5,%%edx ; "
46 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
47 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
48#elif defined(__x86_64__)
49 __asm__ (
50 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
51 : [lo]"=a"(product),
52 [hi]"=d"(tmp)
53 : "0"(delta),
54 [mul_frac]"rm"((u64)mul_frac));
55#else
56#error implement me!
57#endif
58
59 return product;
60}
61
62static __always_inline
63u64 pvclock_get_nsec_offset(const struct pvclock_vcpu_time_info *src)
64{
65 u64 delta = __native_read_tsc() - src->tsc_timestamp;
66 return pvclock_scale_delta(delta, src->tsc_to_system_mul,
67 src->tsc_shift);
68}
69
70static __always_inline
71unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
72 cycle_t *cycles, u8 *flags)
73{
74 unsigned version;
75 cycle_t ret, offset;
76 u8 ret_flags;
77
78 version = src->version;
79 /* Note: emulated platforms which do not advertise SSE2 support
80 * result in kvmclock not using the necessary RDTSC barriers.
81 * Without barriers, it is possible that RDTSC instruction reads from
82 * the time stamp counter outside rdtsc_barrier protected section
83 * below, resulting in violation of monotonicity.
84 */
85 rdtsc_barrier();
86 offset = pvclock_get_nsec_offset(src);
87 ret = src->system_time + offset;
88 ret_flags = src->flags;
89 rdtsc_barrier();
90
91 *cycles = ret;
92 *flags = ret_flags;
93 return version;
94}
95
96struct pvclock_vsyscall_time_info {
97 struct pvclock_vcpu_time_info pvti;
98} __attribute__((__aligned__(SMP_CACHE_BYTES)));
99
100#define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
101#define PVCLOCK_VSYSCALL_NR_PAGES (((NR_CPUS-1)/(PAGE_SIZE/PVTI_SIZE))+1)
102
103int __init pvclock_init_vsyscall(struct pvclock_vsyscall_time_info *i,
104 int size);
105struct pvclock_vcpu_time_info *pvclock_get_vsyscall_time_info(int cpu);
106
107#endif /* _ASM_X86_PVCLOCK_H */