blob: 0ff3e294d0e569b5b6b917720c3935888119689e [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Gerd Hoffmann7af192c2008-06-03 16:17:29 +02002/* paravirtual clock -- common code used by kvm/xen
3
Gerd Hoffmann7af192c2008-06-03 16:17:29 +02004*/
5
6#include <linux/kernel.h>
7#include <linux/percpu.h>
Marcelo Tosatti71056ae2012-11-27 23:28:55 -02008#include <linux/notifier.h>
9#include <linux/sched.h>
10#include <linux/gfp.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070011#include <linux/memblock.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010012#include <linux/nmi.h>
13
Marcelo Tosatti71056ae2012-11-27 23:28:55 -020014#include <asm/fixmap.h>
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020015#include <asm/pvclock.h>
Joao Martins9f08890a2017-11-08 17:19:55 +000016#include <asm/vgtod.h>
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020017
Glauber Costa424c32f2010-05-11 12:17:39 -040018static u8 valid_flags __read_mostly = 0;
Joao Martins9f08890a2017-11-08 17:19:55 +000019static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly;
Glauber Costa424c32f2010-05-11 12:17:39 -040020
21void pvclock_set_flags(u8 flags)
22{
23 valid_flags = flags;
24}
25
Glauber Costa3807f342008-07-28 11:47:52 -030026unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
27{
Harvey Harrisona0854602008-09-23 11:01:45 -070028 u64 pv_tsc_khz = 1000000ULL << 32;
Glauber Costa3807f342008-07-28 11:47:52 -030029
Harvey Harrisona0854602008-09-23 11:01:45 -070030 do_div(pv_tsc_khz, src->tsc_to_system_mul);
Glauber Costa3807f342008-07-28 11:47:52 -030031 if (src->tsc_shift < 0)
Harvey Harrisona0854602008-09-23 11:01:45 -070032 pv_tsc_khz <<= -src->tsc_shift;
Glauber Costa3807f342008-07-28 11:47:52 -030033 else
Harvey Harrisona0854602008-09-23 11:01:45 -070034 pv_tsc_khz >>= src->tsc_shift;
35 return pv_tsc_khz;
Glauber Costa3807f342008-07-28 11:47:52 -030036}
37
Marcelo Tosattid63285e2013-10-11 21:39:25 -030038void pvclock_touch_watchdogs(void)
39{
40 touch_softlockup_watchdog_sync();
41 clocksource_touch_watchdog();
42 rcu_cpu_stall_reset();
Marcelo Tosatti8b414522013-10-11 21:39:26 -030043 reset_hung_task_detector();
Marcelo Tosattid63285e2013-10-11 21:39:25 -030044}
45
Glauber Costa489fb492010-05-11 12:17:40 -040046static atomic64_t last_value = ATOMIC64_INIT(0);
47
Jeremy Fitzhardingee7a3481c2010-10-25 16:53:46 -070048void pvclock_resume(void)
49{
50 atomic64_set(&last_value, 0);
51}
52
Marcelo Tosatti26979022012-11-27 23:28:52 -020053u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
54{
55 unsigned version;
Marcelo Tosatti26979022012-11-27 23:28:52 -020056 u8 flags;
57
58 do {
Paolo Bonzini3aed64f2016-06-09 13:06:08 +020059 version = pvclock_read_begin(src);
Minfei Huanged911b42016-05-28 20:27:43 +080060 flags = src->flags;
Paolo Bonzini3aed64f2016-06-09 13:06:08 +020061 } while (pvclock_read_retry(src, version));
Marcelo Tosatti26979022012-11-27 23:28:52 -020062
63 return flags & valid_flags;
64}
65
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010066u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020067{
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020068 unsigned version;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010069 u64 ret;
Glauber Costa489fb492010-05-11 12:17:40 -040070 u64 last;
Marcelo Tosatti42b56372012-11-27 23:28:50 -020071 u8 flags;
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020072
73 do {
Paolo Bonzini3aed64f2016-06-09 13:06:08 +020074 version = pvclock_read_begin(src);
Paolo Bonzini108b2492016-09-01 14:21:03 +020075 ret = __pvclock_read_cycles(src, rdtsc_ordered());
Paolo Bonzini3aed64f2016-06-09 13:06:08 +020076 flags = src->flags;
77 } while (pvclock_read_retry(src, version));
Gerd Hoffmann7af192c2008-06-03 16:17:29 +020078
Marcelo Tosattid63285e2013-10-11 21:39:25 -030079 if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) {
80 src->flags &= ~PVCLOCK_GUEST_STOPPED;
81 pvclock_touch_watchdogs();
82 }
83
Glauber Costa3a0d7252010-05-11 12:17:45 -040084 if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
Marcelo Tosatti42b56372012-11-27 23:28:50 -020085 (flags & PVCLOCK_TSC_STABLE_BIT))
Glauber Costa3a0d7252010-05-11 12:17:45 -040086 return ret;
87
Glauber Costa489fb492010-05-11 12:17:40 -040088 /*
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 Hoffmann7af192c2008-06-03 16:17:29 +0200109 return ret;
110}
111
112void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
113 struct pvclock_vcpu_time_info *vcpu_time,
Arnd Bergmanne27c49292018-04-27 22:13:23 +0200114 struct timespec64 *ts)
Gerd Hoffmann7af192c2008-06-03 16:17:29 +0200115{
116 u32 version;
117 u64 delta;
Arnd Bergmanne27c49292018-04-27 22:13:23 +0200118 struct timespec64 now;
Gerd Hoffmann7af192c2008-06-03 16:17:29 +0200119
120 /* get wallclock at system boot */
121 do {
122 version = wall_clock->version;
123 rmb(); /* fetch version before time */
Arnd Bergmanne27c49292018-04-27 22:13:23 +0200124 /*
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 Hoffmann7af192c2008-06-03 16:17:29 +0200131 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 Bergmanne27c49292018-04-27 22:13:23 +0200137 delta += now.tv_sec * NSEC_PER_SEC + now.tv_nsec;
Gerd Hoffmann7af192c2008-06-03 16:17:29 +0200138
139 now.tv_nsec = do_div(delta, NSEC_PER_SEC);
140 now.tv_sec = delta;
141
Arnd Bergmanne27c49292018-04-27 22:13:23 +0200142 set_normalized_timespec64(ts, now.tv_sec, now.tv_nsec);
Gerd Hoffmann7af192c2008-06-03 16:17:29 +0200143}
Joao Martins9f08890a2017-11-08 17:19:55 +0000144
145void 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
151struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
152{
153 return pvti_cpu0_va;
154}
155EXPORT_SYMBOL_GPL(pvclock_get_pvti_cpu0_va);