1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Fast user context implementation of clock_gettime, gettimeofday, and time. 4 * 5 * Copyright (C) 2019 ARM Limited. 6 * Copyright 2006 Andi Kleen, SUSE Labs. 7 * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net> 8 * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany 9 */ 10 #ifndef __ASM_VDSO_GETTIMEOFDAY_H 11 #define __ASM_VDSO_GETTIMEOFDAY_H 12 13 #ifndef __ASSEMBLER__ 14 15 #include <uapi/linux/time.h> 16 #include <asm/vgtod.h> 17 #include <asm/unistd.h> 18 #include <asm/msr.h> 19 #include <asm/pvclock.h> 20 #include <clocksource/hyperv_timer.h> 21 #include <asm/vdso/sys_call.h> 22 23 #define VDSO_HAS_TIME 1 24 25 #define VDSO_HAS_CLOCK_GETRES 1 26 27 /* 28 * Declare the memory-mapped vclock data pages. These come from hypervisors. 29 * If we ever reintroduce something like direct access to an MMIO clock like 30 * the HPET again, it will go here as well. 31 * 32 * A load from any of these pages will segfault if the clock in question is 33 * disabled, so appropriate compiler barriers and checks need to be used 34 * to prevent stray loads. 35 * 36 * These declarations MUST NOT be const. The compiler will assume that 37 * an extern const variable has genuinely constant contents, and the 38 * resulting code won't work, since the whole point is that these pages 39 * change over time, possibly while we're accessing them. 40 */ 41 42 #ifdef CONFIG_PARAVIRT_CLOCK 43 /* 44 * This is the vCPU 0 pvclock page. We only use pvclock from the vDSO 45 * if the hypervisor tells us that all vCPUs can get valid data from the 46 * vCPU 0 page. 47 */ 48 extern struct pvclock_vsyscall_time_info pvclock_page 49 __attribute__((visibility("hidden"))); 50 #endif 51 52 #ifdef CONFIG_HYPERV_TIMER 53 extern struct ms_hyperv_tsc_page hvclock_page 54 __attribute__((visibility("hidden"))); 55 #endif 56 57 static __always_inline 58 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts) 59 { 60 return VDSO_SYSCALL2(clock_gettime,64,_clkid,_ts); 61 } 62 63 static __always_inline 64 long gettimeofday_fallback(struct __kernel_old_timeval *_tv, 65 struct timezone *_tz) 66 { 67 return VDSO_SYSCALL2(gettimeofday,,_tv,_tz); 68 } 69 70 static __always_inline 71 long clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts) 72 { 73 return VDSO_SYSCALL2(clock_getres,_time64,_clkid,_ts); 74 } 75 76 #ifndef CONFIG_X86_64 77 78 static __always_inline 79 long clock_gettime32_fallback(clockid_t _clkid, struct old_timespec32 *_ts) 80 { 81 return VDSO_SYSCALL2(clock_gettime,,_clkid,_ts); 82 } 83 84 static __always_inline long 85 clock_getres32_fallback(clockid_t _clkid, struct old_timespec32 *_ts) 86 { 87 return VDSO_SYSCALL2(clock_getres,,_clkid,_ts); 88 } 89 90 #endif 91 92 #ifdef CONFIG_PARAVIRT_CLOCK 93 static u64 vread_pvclock(void) 94 { 95 const struct pvclock_vcpu_time_info *pvti = &pvclock_page.pvti; 96 u32 version; 97 u64 ret; 98 99 /* 100 * Note: The kernel and hypervisor must guarantee that cpu ID 101 * number maps 1:1 to per-CPU pvclock time info. 102 * 103 * Because the hypervisor is entirely unaware of guest userspace 104 * preemption, it cannot guarantee that per-CPU pvclock time 105 * info is updated if the underlying CPU changes or that that 106 * version is increased whenever underlying CPU changes. 107 * 108 * On KVM, we are guaranteed that pvti updates for any vCPU are 109 * atomic as seen by *all* vCPUs. This is an even stronger 110 * guarantee than we get with a normal seqlock. 111 * 112 * On Xen, we don't appear to have that guarantee, but Xen still 113 * supplies a valid seqlock using the version field. 114 * 115 * We only do pvclock vdso timing at all if 116 * PVCLOCK_TSC_STABLE_BIT is set, and we interpret that bit to 117 * mean that all vCPUs have matching pvti and that the TSC is 118 * synced, so we can just look at vCPU 0's pvti. 119 */ 120 121 do { 122 version = pvclock_read_begin(pvti); 123 124 if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT))) 125 return U64_MAX; 126 127 ret = __pvclock_read_cycles(pvti, rdtsc_ordered()); 128 } while (pvclock_read_retry(pvti, version)); 129 130 return ret & S64_MAX; 131 } 132 #endif 133 134 #ifdef CONFIG_HYPERV_TIMER 135 static u64 vread_hvclock(void) 136 { 137 u64 tsc, time; 138 139 if (hv_read_tsc_page_tsc(&hvclock_page, &tsc, &time)) 140 return time & S64_MAX; 141 142 return U64_MAX; 143 } 144 #endif 145 146 static inline u64 __arch_get_hw_counter(s32 clock_mode, 147 const struct vdso_time_data *vd) 148 { 149 if (likely(clock_mode == VDSO_CLOCKMODE_TSC)) 150 return (u64)rdtsc_ordered() & S64_MAX; 151 /* 152 * For any memory-mapped vclock type, we need to make sure that gcc 153 * doesn't cleverly hoist a load before the mode check. Otherwise we 154 * might end up touching the memory-mapped page even if the vclock in 155 * question isn't enabled, which will segfault. Hence the barriers. 156 */ 157 #ifdef CONFIG_PARAVIRT_CLOCK 158 if (clock_mode == VDSO_CLOCKMODE_PVCLOCK) { 159 barrier(); 160 return vread_pvclock(); 161 } 162 #endif 163 #ifdef CONFIG_HYPERV_TIMER 164 if (clock_mode == VDSO_CLOCKMODE_HVCLOCK) { 165 barrier(); 166 return vread_hvclock(); 167 } 168 #endif 169 return U64_MAX; 170 } 171 172 static inline bool arch_vdso_clocksource_ok(const struct vdso_clock *vc) 173 { 174 return true; 175 } 176 #define vdso_clocksource_ok arch_vdso_clocksource_ok 177 178 /* 179 * Clocksource read value validation to handle PV and HyperV clocksources 180 * which can be invalidated asynchronously and indicate invalidation by 181 * returning U64_MAX, which can be effectively tested by checking for a 182 * negative value after casting it to s64. 183 * 184 * This effectively forces a S64_MAX mask on the calculations, unlike the 185 * U64_MAX mask normally used by x86 clocksources. 186 */ 187 static inline bool arch_vdso_cycles_ok(u64 cycles) 188 { 189 return (s64)cycles >= 0; 190 } 191 #define vdso_cycles_ok arch_vdso_cycles_ok 192 193 /* 194 * x86 specific calculation of nanoseconds for the current cycle count 195 * 196 * The regular implementation assumes that clocksource reads are globally 197 * monotonic. The TSC can be slightly off across sockets which can cause 198 * the regular delta calculation (@cycles - @last) to return a huge time 199 * jump. 200 * 201 * Therefore it needs to be verified that @cycles are greater than 202 * @vd->cycles_last. If not then use @vd->cycles_last, which is the base 203 * time of the current conversion period. 204 * 205 * This variant also uses a custom mask because while the clocksource mask of 206 * all the VDSO capable clocksources on x86 is U64_MAX, the above code uses 207 * U64_MASK as an exception value, additionally arch_vdso_cycles_ok() above 208 * declares everything with the MSB/Sign-bit set as invalid. Therefore the 209 * effective mask is S64_MAX. 210 */ 211 static __always_inline u64 vdso_calc_ns(const struct vdso_clock *vc, u64 cycles, u64 base) 212 { 213 u64 delta = cycles - vc->cycle_last; 214 215 /* 216 * Negative motion and deltas which can cause multiplication 217 * overflow require special treatment. This check covers both as 218 * negative motion is guaranteed to be greater than @vc::max_cycles 219 * due to unsigned comparison. 220 * 221 * Due to the MSB/Sign-bit being used as invalid marker (see 222 * arch_vdso_cycles_ok() above), the effective mask is S64_MAX, but that 223 * case is also unlikely and will also take the unlikely path here. 224 */ 225 if (unlikely(delta > vc->max_cycles)) { 226 /* 227 * Due to the above mentioned TSC wobbles, filter out 228 * negative motion. Per the above masking, the effective 229 * sign bit is now bit 62. 230 */ 231 if (delta & (1ULL << 62)) 232 return base >> vc->shift; 233 234 /* Handle multiplication overflow gracefully */ 235 return mul_u64_u32_add_u64_shr(delta & S64_MAX, vc->mult, base, vc->shift); 236 } 237 238 return ((delta * vc->mult) + base) >> vc->shift; 239 } 240 #define vdso_calc_ns vdso_calc_ns 241 242 #endif /* !__ASSEMBLER__ */ 243 244 #endif /* __ASM_VDSO_GETTIMEOFDAY_H */ 245