1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* paravirtual clock -- common code used by kvm/xen 3 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/percpu.h> 8 #include <linux/notifier.h> 9 #include <linux/sched.h> 10 #include <linux/gfp.h> 11 #include <linux/memblock.h> 12 #include <linux/nmi.h> 13 14 #include <asm/fixmap.h> 15 #include <asm/pvclock.h> 16 #include <asm/vgtod.h> 17 18 static u8 valid_flags __read_mostly = 0; 19 static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly; 20 21 void pvclock_set_flags(u8 flags) 22 { 23 valid_flags = flags; 24 } 25 26 unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src) 27 { 28 u64 pv_tsc_khz = 1000000ULL << 32; 29 30 do_div(pv_tsc_khz, src->tsc_to_system_mul); 31 if (src->tsc_shift < 0) 32 pv_tsc_khz <<= -src->tsc_shift; 33 else 34 pv_tsc_khz >>= src->tsc_shift; 35 return pv_tsc_khz; 36 } 37 38 void pvclock_touch_watchdogs(void) 39 { 40 touch_softlockup_watchdog_sync(); 41 clocksource_touch_watchdog(); 42 rcu_cpu_stall_reset(); 43 reset_hung_task_detector(); 44 } 45 46 static atomic64_t last_value = ATOMIC64_INIT(0); 47 48 void pvclock_resume(void) 49 { 50 atomic64_set(&last_value, 0); 51 } 52 53 u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src) 54 { 55 unsigned version; 56 u8 flags; 57 58 do { 59 version = pvclock_read_begin(src); 60 flags = src->flags; 61 } while (pvclock_read_retry(src, version)); 62 63 return flags & valid_flags; 64 } 65 66 u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) 67 { 68 unsigned version; 69 u64 ret; 70 u64 last; 71 u8 flags; 72 73 do { 74 version = pvclock_read_begin(src); 75 ret = __pvclock_read_cycles(src, rdtsc_ordered()); 76 flags = src->flags; 77 } while (pvclock_read_retry(src, version)); 78 79 if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) { 80 src->flags &= ~PVCLOCK_GUEST_STOPPED; 81 pvclock_touch_watchdogs(); 82 } 83 84 if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) && 85 (flags & PVCLOCK_TSC_STABLE_BIT)) 86 return ret; 87 88 /* 89 * Assumption here is that last_value, a global accumulator, always goes 90 * forward. If we are less than that, we should not be much smaller. 91 * We assume there is an error marging we're inside, and then the correction 92 * does not sacrifice accuracy. 93 * 94 * For reads: global may have changed between test and return, 95 * but this means someone else updated poked the clock at a later time. 96 * We just need to make sure we are not seeing a backwards event. 97 * 98 * For updates: last_value = ret is not enough, since two vcpus could be 99 * updating at the same time, and one of them could be slightly behind, 100 * making the assumption that last_value always go forward fail to hold. 101 */ 102 last = atomic64_read(&last_value); 103 do { 104 if (ret < last) 105 return last; 106 last = atomic64_cmpxchg(&last_value, last, ret); 107 } while (unlikely(last != ret)); 108 109 return ret; 110 } 111 112 void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock, 113 struct pvclock_vcpu_time_info *vcpu_time, 114 struct timespec64 *ts) 115 { 116 u32 version; 117 u64 delta; 118 struct timespec64 now; 119 120 /* get wallclock at system boot */ 121 do { 122 version = wall_clock->version; 123 rmb(); /* fetch version before time */ 124 /* 125 * Note: wall_clock->sec is a u32 value, so it can 126 * only store dates between 1970 and 2106. To allow 127 * times beyond that, we need to create a new hypercall 128 * interface with an extended pvclock_wall_clock structure 129 * like ARM has. 130 */ 131 now.tv_sec = wall_clock->sec; 132 now.tv_nsec = wall_clock->nsec; 133 rmb(); /* fetch time before checking version */ 134 } while ((wall_clock->version & 1) || (version != wall_clock->version)); 135 136 delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */ 137 delta += now.tv_sec * NSEC_PER_SEC + now.tv_nsec; 138 139 now.tv_nsec = do_div(delta, NSEC_PER_SEC); 140 now.tv_sec = delta; 141 142 set_normalized_timespec64(ts, now.tv_sec, now.tv_nsec); 143 } 144 145 void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti) 146 { 147 WARN_ON(vclock_was_used(VCLOCK_PVCLOCK)); 148 pvti_cpu0_va = pvti; 149 } 150 151 struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void) 152 { 153 return pvti_cpu0_va; 154 } 155 EXPORT_SYMBOL_GPL(pvclock_get_pvti_cpu0_va); 156