Thomas Gleixner | fd534e9 | 2019-05-23 11:14:39 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 2 | /* paravirtual clock -- common code used by kvm/xen |
| 3 | |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/percpu.h> |
Marcelo Tosatti | 71056ae | 2012-11-27 23:28:55 -0200 | [diff] [blame] | 8 | #include <linux/notifier.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/gfp.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 11 | #include <linux/memblock.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 12 | #include <linux/nmi.h> |
| 13 | |
Marcelo Tosatti | 71056ae | 2012-11-27 23:28:55 -0200 | [diff] [blame] | 14 | #include <asm/fixmap.h> |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 15 | #include <asm/pvclock.h> |
Joao Martins | 9f08890a | 2017-11-08 17:19:55 +0000 | [diff] [blame] | 16 | #include <asm/vgtod.h> |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 17 | |
Glauber Costa | 424c32f | 2010-05-11 12:17:39 -0400 | [diff] [blame] | 18 | static u8 valid_flags __read_mostly = 0; |
Joao Martins | 9f08890a | 2017-11-08 17:19:55 +0000 | [diff] [blame] | 19 | static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly; |
Glauber Costa | 424c32f | 2010-05-11 12:17:39 -0400 | [diff] [blame] | 20 | |
| 21 | void pvclock_set_flags(u8 flags) |
| 22 | { |
| 23 | valid_flags = flags; |
| 24 | } |
| 25 | |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 26 | unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src) |
| 27 | { |
Harvey Harrison | a085460 | 2008-09-23 11:01:45 -0700 | [diff] [blame] | 28 | u64 pv_tsc_khz = 1000000ULL << 32; |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 29 | |
Harvey Harrison | a085460 | 2008-09-23 11:01:45 -0700 | [diff] [blame] | 30 | do_div(pv_tsc_khz, src->tsc_to_system_mul); |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 31 | if (src->tsc_shift < 0) |
Harvey Harrison | a085460 | 2008-09-23 11:01:45 -0700 | [diff] [blame] | 32 | pv_tsc_khz <<= -src->tsc_shift; |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 33 | else |
Harvey Harrison | a085460 | 2008-09-23 11:01:45 -0700 | [diff] [blame] | 34 | pv_tsc_khz >>= src->tsc_shift; |
| 35 | return pv_tsc_khz; |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 36 | } |
| 37 | |
Marcelo Tosatti | d63285e | 2013-10-11 21:39:25 -0300 | [diff] [blame] | 38 | void pvclock_touch_watchdogs(void) |
| 39 | { |
| 40 | touch_softlockup_watchdog_sync(); |
| 41 | clocksource_touch_watchdog(); |
| 42 | rcu_cpu_stall_reset(); |
Marcelo Tosatti | 8b41452 | 2013-10-11 21:39:26 -0300 | [diff] [blame] | 43 | reset_hung_task_detector(); |
Marcelo Tosatti | d63285e | 2013-10-11 21:39:25 -0300 | [diff] [blame] | 44 | } |
| 45 | |
Glauber Costa | 489fb49 | 2010-05-11 12:17:40 -0400 | [diff] [blame] | 46 | static atomic64_t last_value = ATOMIC64_INIT(0); |
| 47 | |
Jeremy Fitzhardinge | e7a3481c | 2010-10-25 16:53:46 -0700 | [diff] [blame] | 48 | void pvclock_resume(void) |
| 49 | { |
| 50 | atomic64_set(&last_value, 0); |
| 51 | } |
| 52 | |
Marcelo Tosatti | 2697902 | 2012-11-27 23:28:52 -0200 | [diff] [blame] | 53 | u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src) |
| 54 | { |
| 55 | unsigned version; |
Marcelo Tosatti | 2697902 | 2012-11-27 23:28:52 -0200 | [diff] [blame] | 56 | u8 flags; |
| 57 | |
| 58 | do { |
Paolo Bonzini | 3aed64f | 2016-06-09 13:06:08 +0200 | [diff] [blame] | 59 | version = pvclock_read_begin(src); |
Minfei Huang | ed911b4 | 2016-05-28 20:27:43 +0800 | [diff] [blame] | 60 | flags = src->flags; |
Paolo Bonzini | 3aed64f | 2016-06-09 13:06:08 +0200 | [diff] [blame] | 61 | } while (pvclock_read_retry(src, version)); |
Marcelo Tosatti | 2697902 | 2012-11-27 23:28:52 -0200 | [diff] [blame] | 62 | |
| 63 | return flags & valid_flags; |
| 64 | } |
| 65 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 66 | u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 67 | { |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 68 | unsigned version; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 69 | u64 ret; |
Glauber Costa | 489fb49 | 2010-05-11 12:17:40 -0400 | [diff] [blame] | 70 | u64 last; |
Marcelo Tosatti | 42b5637 | 2012-11-27 23:28:50 -0200 | [diff] [blame] | 71 | u8 flags; |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 72 | |
| 73 | do { |
Paolo Bonzini | 3aed64f | 2016-06-09 13:06:08 +0200 | [diff] [blame] | 74 | version = pvclock_read_begin(src); |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 75 | ret = __pvclock_read_cycles(src, rdtsc_ordered()); |
Paolo Bonzini | 3aed64f | 2016-06-09 13:06:08 +0200 | [diff] [blame] | 76 | flags = src->flags; |
| 77 | } while (pvclock_read_retry(src, version)); |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 78 | |
Marcelo Tosatti | d63285e | 2013-10-11 21:39:25 -0300 | [diff] [blame] | 79 | if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) { |
| 80 | src->flags &= ~PVCLOCK_GUEST_STOPPED; |
| 81 | pvclock_touch_watchdogs(); |
| 82 | } |
| 83 | |
Glauber Costa | 3a0d725 | 2010-05-11 12:17:45 -0400 | [diff] [blame] | 84 | if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) && |
Marcelo Tosatti | 42b5637 | 2012-11-27 23:28:50 -0200 | [diff] [blame] | 85 | (flags & PVCLOCK_TSC_STABLE_BIT)) |
Glauber Costa | 3a0d725 | 2010-05-11 12:17:45 -0400 | [diff] [blame] | 86 | return ret; |
| 87 | |
Glauber Costa | 489fb49 | 2010-05-11 12:17:40 -0400 | [diff] [blame] | 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 | |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock, |
| 113 | struct pvclock_vcpu_time_info *vcpu_time, |
Arnd Bergmann | e27c4929 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 114 | struct timespec64 *ts) |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 115 | { |
| 116 | u32 version; |
| 117 | u64 delta; |
Arnd Bergmann | e27c4929 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 118 | struct timespec64 now; |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 119 | |
| 120 | /* get wallclock at system boot */ |
| 121 | do { |
| 122 | version = wall_clock->version; |
| 123 | rmb(); /* fetch version before time */ |
Arnd Bergmann | e27c4929 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 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 | */ |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 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 */ |
Arnd Bergmann | e27c4929 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 137 | delta += now.tv_sec * NSEC_PER_SEC + now.tv_nsec; |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 138 | |
| 139 | now.tv_nsec = do_div(delta, NSEC_PER_SEC); |
| 140 | now.tv_sec = delta; |
| 141 | |
Arnd Bergmann | e27c4929 | 2018-04-27 22:13:23 +0200 | [diff] [blame] | 142 | set_normalized_timespec64(ts, now.tv_sec, now.tv_nsec); |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 143 | } |
Joao Martins | 9f08890a | 2017-11-08 17:19:55 +0000 | [diff] [blame] | 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); |