blob: d307c22e5c188ce6d9f9f2dffc1c75b1aaf43a88 [file] [log] [blame]
Thomas Gleixnerf6ce7f22019-05-19 15:51:49 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -05002/*
3 * KVM paravirt_ops implementation
4 *
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -05005 * Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 * Copyright IBM Corporation, 2007
7 * Authors: Anthony Liguori <aliguori@us.ibm.com>
8 */
9
Zhenzhong Duan5aefd782019-10-23 19:16:21 +080010#define pr_fmt(fmt) "kvm-guest: " fmt
11
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010012#include <linux/context_tracking.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040013#include <linux/init.h>
Paolo Bonzini26d05b32020-06-15 07:53:05 -040014#include <linux/irq.h>
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -050015#include <linux/kernel.h>
16#include <linux/kvm_para.h>
17#include <linux/cpu.h>
18#include <linux/mm.h>
Marcelo Tosatti1da8a772008-02-22 12:21:37 -050019#include <linux/highmem.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050020#include <linux/hardirq.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020021#include <linux/notifier.h>
22#include <linux/reboot.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020023#include <linux/hash.h>
24#include <linux/sched.h>
25#include <linux/slab.h>
26#include <linux/kprobes.h>
Ulrich Obergfell9919e392014-10-13 15:55:37 -070027#include <linux/nmi.h>
Rik van Riel9db284f2016-03-21 15:13:27 +010028#include <linux/swait.h>
Marcelo Tosattia90ede72009-02-11 22:45:42 -020029#include <asm/timer.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020030#include <asm/cpu.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020031#include <asm/traps.h>
32#include <asm/desc.h>
Gleb Natapov6c047cd2010-10-14 11:22:54 +020033#include <asm/tlbflush.h>
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030034#include <asm/apic.h>
35#include <asm/apicdef.h>
Prarit Bhargavafc733732012-07-06 13:47:39 -040036#include <asm/hypervisor.h>
Peter Zijlstra48a8b972018-08-22 17:30:16 +020037#include <asm/tlb.h>
Yi Wang19308a42019-10-10 14:37:25 +080038#include <asm/cpuidle_haltpoll.h>
Tom Lendacky99419b22020-09-07 15:16:04 +020039#include <asm/ptrace.h>
40#include <asm/svm.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050041
Andy Lutomirskief680172020-02-28 10:42:48 -080042DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
43
Gleb Natapovfd10cde2010-10-14 11:22:51 +020044static int kvmapf = 1;
45
Dou Liyangafdc3f582018-01-17 11:46:54 +080046static int __init parse_no_kvmapf(char *arg)
Gleb Natapovfd10cde2010-10-14 11:22:51 +020047{
48 kvmapf = 0;
49 return 0;
50}
51
52early_param("no-kvmapf", parse_no_kvmapf);
53
Glauber Costad910f5c2011-07-11 15:28:19 -040054static int steal_acc = 1;
Dou Liyangafdc3f582018-01-17 11:46:54 +080055static int __init parse_no_stealacc(char *arg)
Glauber Costad910f5c2011-07-11 15:28:19 -040056{
57 steal_acc = 0;
58 return 0;
59}
60
61early_param("no-steal-acc", parse_no_stealacc);
62
Brijesh Singh47162762017-10-20 09:30:58 -050063static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
Andi Kleen14e581c2019-03-29 17:47:42 -070064DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible;
Glauber Costad910f5c2011-07-11 15:28:19 -040065static int has_steal_clock = 0;
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050066
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -050067/*
68 * No need for any "IO delay" on KVM
69 */
70static void kvm_io_delay(void)
71{
72}
73
Gleb Natapov631bc482010-10-14 11:22:52 +020074#define KVM_TASK_SLEEP_HASHBITS 8
75#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
76
77struct kvm_task_sleep_node {
78 struct hlist_node link;
Rik van Riel9db284f2016-03-21 15:13:27 +010079 struct swait_queue_head wq;
Gleb Natapov631bc482010-10-14 11:22:52 +020080 u32 token;
81 int cpu;
82};
83
84static struct kvm_task_sleep_head {
Rik van Riel9db284f2016-03-21 15:13:27 +010085 raw_spinlock_t lock;
Gleb Natapov631bc482010-10-14 11:22:52 +020086 struct hlist_head list;
87} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
88
89static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
90 u32 token)
91{
92 struct hlist_node *p;
93
94 hlist_for_each(p, &b->list) {
95 struct kvm_task_sleep_node *n =
96 hlist_entry(p, typeof(*n), link);
97 if (n->token == token)
98 return n;
99 }
100
101 return NULL;
102}
103
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200104static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)
Gleb Natapov631bc482010-10-14 11:22:52 +0200105{
106 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
107 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100108 struct kvm_task_sleep_node *e;
Li Zhong9b132fb2012-12-04 10:35:13 +0800109
Rik van Riel9db284f2016-03-21 15:13:27 +0100110 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200111 e = _find_apf_task(b, token);
112 if (e) {
113 /* dummy entry exist -> wake up was delivered ahead of PF */
114 hlist_del(&e->link);
Rik van Riel9db284f2016-03-21 15:13:27 +0100115 raw_spin_unlock(&b->lock);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100116 kfree(e);
117 return false;
Gleb Natapov631bc482010-10-14 11:22:52 +0200118 }
119
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100120 n->token = token;
121 n->cpu = smp_processor_id();
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100122 init_swait_queue_head(&n->wq);
123 hlist_add_head(&n->link, &b->list);
Rik van Riel9db284f2016-03-21 15:13:27 +0100124 raw_spin_unlock(&b->lock);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100125 return true;
126}
127
128/*
129 * kvm_async_pf_task_wait_schedule - Wait for pagefault to be handled
130 * @token: Token to identify the sleep node entry
131 *
132 * Invoked from the async pagefault handling code or from the VM exit page
133 * fault handler. In both cases RCU is watching.
134 */
135void kvm_async_pf_task_wait_schedule(u32 token)
136{
137 struct kvm_task_sleep_node n;
138 DECLARE_SWAITQUEUE(wait);
139
140 lockdep_assert_irqs_disabled();
141
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200142 if (!kvm_async_pf_queue_task(token, &n))
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100143 return;
Gleb Natapov631bc482010-10-14 11:22:52 +0200144
145 for (;;) {
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100146 prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
Gleb Natapov631bc482010-10-14 11:22:52 +0200147 if (hlist_unhashed(&n.link))
148 break;
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200149
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100150 local_irq_enable();
151 schedule();
152 local_irq_disable();
Gleb Natapov631bc482010-10-14 11:22:52 +0200153 }
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100154 finish_swait(&n.wq, &wait);
Gleb Natapov631bc482010-10-14 11:22:52 +0200155}
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100156EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule);
157
Gleb Natapov631bc482010-10-14 11:22:52 +0200158static void apf_task_wake_one(struct kvm_task_sleep_node *n)
159{
160 hlist_del_init(&n->link);
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200161 if (swq_has_sleeper(&n->wq))
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200162 swake_up_one(&n->wq);
Gleb Natapov631bc482010-10-14 11:22:52 +0200163}
164
165static void apf_task_wake_all(void)
166{
167 int i;
168
169 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
Gleb Natapov631bc482010-10-14 11:22:52 +0200170 struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100171 struct kvm_task_sleep_node *n;
172 struct hlist_node *p, *next;
173
Rik van Riel9db284f2016-03-21 15:13:27 +0100174 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200175 hlist_for_each_safe(p, next, &b->list) {
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100176 n = hlist_entry(p, typeof(*n), link);
Gleb Natapov631bc482010-10-14 11:22:52 +0200177 if (n->cpu == smp_processor_id())
178 apf_task_wake_one(n);
179 }
Rik van Riel9db284f2016-03-21 15:13:27 +0100180 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200181 }
182}
183
184void kvm_async_pf_task_wake(u32 token)
185{
186 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
187 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
188 struct kvm_task_sleep_node *n;
189
190 if (token == ~0) {
191 apf_task_wake_all();
192 return;
193 }
194
195again:
Rik van Riel9db284f2016-03-21 15:13:27 +0100196 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200197 n = _find_apf_task(b, token);
198 if (!n) {
199 /*
200 * async PF was not yet handled.
201 * Add dummy entry for the token.
202 */
Gleb Natapov62c49cc2012-05-02 15:04:02 +0300203 n = kzalloc(sizeof(*n), GFP_ATOMIC);
Gleb Natapov631bc482010-10-14 11:22:52 +0200204 if (!n) {
205 /*
206 * Allocation failed! Busy wait while other cpu
207 * handles async PF.
208 */
Rik van Riel9db284f2016-03-21 15:13:27 +0100209 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200210 cpu_relax();
211 goto again;
212 }
213 n->token = token;
214 n->cpu = smp_processor_id();
Rik van Riel9db284f2016-03-21 15:13:27 +0100215 init_swait_queue_head(&n->wq);
Gleb Natapov631bc482010-10-14 11:22:52 +0200216 hlist_add_head(&n->link, &b->list);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100217 } else {
Gleb Natapov631bc482010-10-14 11:22:52 +0200218 apf_task_wake_one(n);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100219 }
Rik van Riel9db284f2016-03-21 15:13:27 +0100220 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200221 return;
222}
223EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
224
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200225noinstr u32 kvm_read_and_reset_apf_flags(void)
Gleb Natapov631bc482010-10-14 11:22:52 +0200226{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200227 u32 flags = 0;
Gleb Natapov631bc482010-10-14 11:22:52 +0200228
Christoph Lameter89cbc762014-08-17 12:30:40 -0500229 if (__this_cpu_read(apf_reason.enabled)) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200230 flags = __this_cpu_read(apf_reason.flags);
231 __this_cpu_write(apf_reason.flags, 0);
Gleb Natapov631bc482010-10-14 11:22:52 +0200232 }
233
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200234 return flags;
Gleb Natapov631bc482010-10-14 11:22:52 +0200235}
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200236EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags);
Gleb Natapov631bc482010-10-14 11:22:52 +0200237
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200238noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token)
Gleb Natapov631bc482010-10-14 11:22:52 +0200239{
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200240 u32 flags = kvm_read_and_reset_apf_flags();
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200241 irqentry_state_t state;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100242
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200243 if (!flags)
Andy Lutomirskief680172020-02-28 10:42:48 -0800244 return false;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100245
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200246 state = irqentry_enter(regs);
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200247 instrumentation_begin();
248
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100249 /*
250 * If the host managed to inject an async #PF into an interrupt
251 * disabled region, then die hard as this is not going to end well
252 * and the host side is seriously broken.
253 */
254 if (unlikely(!(regs->flags & X86_EFLAGS_IF)))
255 panic("Host injected async #PF in interrupt disabled region\n");
256
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200257 if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200258 if (unlikely(!(user_mode(regs))))
259 panic("Host injected async #PF in kernel mode\n");
260 /* Page is swapped out by the host. */
261 kvm_async_pf_task_wait_schedule(token);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100262 } else {
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400263 WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags);
Gleb Natapov631bc482010-10-14 11:22:52 +0200264 }
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200265
266 instrumentation_end();
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200267 irqentry_exit(regs, state);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100268 return true;
Gleb Natapov631bc482010-10-14 11:22:52 +0200269}
270
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400271DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt)
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200272{
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400273 struct pt_regs *old_regs = set_irq_regs(regs);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200274 u32 token;
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200275
Vitaly Kuznetsovcc17b222020-09-08 15:53:50 +0200276 ack_APIC_irq();
277
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200278 inc_irq_stat(irq_hv_callback_count);
279
280 if (__this_cpu_read(apf_reason.enabled)) {
281 token = __this_cpu_read(apf_reason.token);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200282 kvm_async_pf_task_wake(token);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200283 __this_cpu_write(apf_reason.token, 0);
284 wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1);
285 }
286
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400287 set_irq_regs(old_regs);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200288}
289
Rakib Mullickd3ac8812009-07-02 11:40:36 +0600290static void __init paravirt_ops_setup(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500291{
292 pv_info.name = "KVM";
Andy Lutomirski29fa6822014-12-05 19:03:28 -0800293
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500294 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
Juergen Gross5c835112018-08-28 09:40:19 +0200295 pv_ops.cpu.io_delay = kvm_io_delay;
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500296
Marcelo Tosattia90ede72009-02-11 22:45:42 -0200297#ifdef CONFIG_X86_IO_APIC
298 no_timer_check = 1;
299#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500300}
301
Glauber Costad910f5c2011-07-11 15:28:19 -0400302static void kvm_register_steal_time(void)
303{
304 int cpu = smp_processor_id();
305 struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
306
307 if (!has_steal_clock)
308 return;
309
Dave Hansen5dfd4862013-01-22 13:24:35 -0800310 wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800311 pr_info("stealtime: cpu %d, msr %llx\n", cpu,
312 (unsigned long long) slow_virt_to_phys(st));
Glauber Costad910f5c2011-07-11 15:28:19 -0400313}
314
Brijesh Singh47162762017-10-20 09:30:58 -0500315static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300316
Wanpeng Li8ca22552016-11-07 11:13:40 +0800317static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300318{
319 /**
320 * This relies on __test_and_clear_bit to modify the memory
321 * in a way that is atomic with respect to the local CPU.
322 * The hypervisor only accesses this memory from the local CPU so
323 * there's no need for lock or memory barriers.
324 * An optimization barrier is implied in apic write.
325 */
Christoph Lameter89cbc762014-08-17 12:30:40 -0500326 if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300327 return;
Wanpeng Li8ca22552016-11-07 11:13:40 +0800328 apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300329}
330
Nicholas Krauseed3cf152015-05-20 00:24:10 -0400331static void kvm_guest_cpu_init(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200332{
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200333 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
334 u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
Andy Lutomirskief680172020-02-28 10:42:48 -0800335
336 WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled));
337
338 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200339 pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
Wanpeng Li52a5c152017-07-13 18:30:42 -0700340
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100341 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
342 pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
343
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200344 wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR);
345
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100346 wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500347 __this_cpu_write(apf_reason.enabled, 1);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100348 pr_info("KVM setup async PF for cpu %d\n", smp_processor_id());
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200349 }
Glauber Costad910f5c2011-07-11 15:28:19 -0400350
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300351 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
352 unsigned long pa;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100353
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300354 /* Size alignment is implied but just to make it explicit. */
355 BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500356 __this_cpu_write(kvm_apic_eoi, 0);
357 pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
Dave Hansen5dfd4862013-01-22 13:24:35 -0800358 | KVM_MSR_ENABLED;
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300359 wrmsrl(MSR_KVM_PV_EOI_EN, pa);
360 }
361
Glauber Costad910f5c2011-07-11 15:28:19 -0400362 if (has_steal_clock)
363 kvm_register_steal_time();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200364}
365
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300366static void kvm_pv_disable_apf(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200367{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500368 if (!__this_cpu_read(apf_reason.enabled))
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200369 return;
370
371 wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500372 __this_cpu_write(apf_reason.enabled, 0);
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200373
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100374 pr_info("Unregister pv shared memory for cpu %d\n", smp_processor_id());
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200375}
376
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300377static void kvm_pv_guest_cpu_reboot(void *unused)
378{
379 /*
380 * We disable PV EOI before we load a new kernel by kexec,
381 * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
382 * New kernel can re-enable when it boots.
383 */
384 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
385 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
386 kvm_pv_disable_apf();
Florian Westphal8fbe6a52012-08-15 16:00:40 +0200387 kvm_disable_steal_time();
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300388}
389
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200390static int kvm_pv_reboot_notify(struct notifier_block *nb,
391 unsigned long code, void *unused)
392{
393 if (code == SYS_RESTART)
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300394 on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200395 return NOTIFY_DONE;
396}
397
398static struct notifier_block kvm_pv_reboot_nb = {
399 .notifier_call = kvm_pv_reboot_notify,
400};
401
Glauber Costad910f5c2011-07-11 15:28:19 -0400402static u64 kvm_steal_clock(int cpu)
403{
404 u64 steal;
405 struct kvm_steal_time *src;
406 int version;
407
408 src = &per_cpu(steal_time, cpu);
409 do {
410 version = src->version;
Wanpeng Li5a48a622017-04-11 02:49:21 -0700411 virt_rmb();
Glauber Costad910f5c2011-07-11 15:28:19 -0400412 steal = src->steal;
Wanpeng Li5a48a622017-04-11 02:49:21 -0700413 virt_rmb();
Glauber Costad910f5c2011-07-11 15:28:19 -0400414 } while ((version & 1) || (version != src->version));
415
416 return steal;
417}
418
419void kvm_disable_steal_time(void)
420{
421 if (!has_steal_clock)
422 return;
423
424 wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
425}
426
Brijesh Singh47162762017-10-20 09:30:58 -0500427static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
428{
429 early_set_memory_decrypted((unsigned long) ptr, size);
430}
431
432/*
433 * Iterate through all possible CPUs and map the memory region pointed
434 * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
435 *
436 * Note: we iterate through all possible CPUs to ensure that CPUs
437 * hotplugged will have their per-cpu variable already mapped as
438 * decrypted.
439 */
440static void __init sev_map_percpu_data(void)
441{
442 int cpu;
443
444 if (!sev_active())
445 return;
446
447 for_each_possible_cpu(cpu) {
448 __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
449 __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
450 __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
451 }
452}
453
Wanpeng Li2b519b572021-04-09 12:18:29 +0800454#ifdef CONFIG_SMP
455
456static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask);
457
Wanpeng Lia262bca2020-02-18 09:08:23 +0800458static bool pv_tlb_flush_supported(void)
459{
460 return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
461 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
462 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
463}
464
Wanpeng Lia262bca2020-02-18 09:08:23 +0800465static bool pv_ipi_supported(void)
466{
467 return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI);
468}
469
470static bool pv_sched_yield_supported(void)
471{
472 return (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) &&
473 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
474 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
475}
476
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800477#define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
478
479static void __send_ipi_mask(const struct cpumask *mask, int vector)
480{
481 unsigned long flags;
482 int cpu, apic_id, icr;
483 int min = 0, max = 0;
484#ifdef CONFIG_X86_64
485 __uint128_t ipi_bitmap = 0;
486#else
487 u64 ipi_bitmap = 0;
488#endif
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800489 long ret;
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800490
491 if (cpumask_empty(mask))
492 return;
493
494 local_irq_save(flags);
495
496 switch (vector) {
497 default:
498 icr = APIC_DM_FIXED | vector;
499 break;
500 case NMI_VECTOR:
501 icr = APIC_DM_NMI;
502 break;
503 }
504
505 for_each_cpu(cpu, mask) {
506 apic_id = per_cpu(x86_cpu_to_apicid, cpu);
507 if (!ipi_bitmap) {
508 min = max = apic_id;
509 } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
510 ipi_bitmap <<= min - apic_id;
511 min = apic_id;
512 } else if (apic_id < min + KVM_IPI_CLUSTER_SIZE) {
513 max = apic_id < max ? max : apic_id;
514 } else {
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800515 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800516 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800517 WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
518 ret);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800519 min = max = apic_id;
520 ipi_bitmap = 0;
521 }
522 __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
523 }
524
525 if (ipi_bitmap) {
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800526 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800527 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800528 WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
529 ret);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800530 }
531
532 local_irq_restore(flags);
533}
534
535static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
536{
537 __send_ipi_mask(mask, vector);
538}
539
540static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
541{
542 unsigned int this_cpu = smp_processor_id();
Wanpeng Li8a9442f2020-02-18 09:08:24 +0800543 struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800544 const struct cpumask *local_mask;
545
Wanpeng Li8a9442f2020-02-18 09:08:24 +0800546 cpumask_copy(new_mask, mask);
547 cpumask_clear_cpu(this_cpu, new_mask);
548 local_mask = new_mask;
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800549 __send_ipi_mask(local_mask, vector);
550}
551
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800552/*
553 * Set the IPI entry points
554 */
555static void kvm_setup_pv_ipi(void)
556{
557 apic->send_IPI_mask = kvm_send_ipi_mask;
558 apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800559 pr_info("setup PV IPIs\n");
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800560}
561
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800562static void kvm_smp_send_call_func_ipi(const struct cpumask *mask)
563{
564 int cpu;
565
566 native_send_call_func_ipi(mask);
567
568 /* Make sure other vCPUs get a chance to run if they need to. */
569 for_each_cpu(cpu, mask) {
570 if (vcpu_is_preempted(cpu)) {
571 kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu));
572 break;
573 }
574 }
575}
576
Linus Torvalds152d32a2021-05-01 10:14:08 -0700577static void kvm_flush_tlb_multi(const struct cpumask *cpumask,
Wanpeng Li2b519b572021-04-09 12:18:29 +0800578 const struct flush_tlb_info *info)
579{
580 u8 state;
581 int cpu;
582 struct kvm_steal_time *src;
583 struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
584
585 cpumask_copy(flushmask, cpumask);
586 /*
587 * We have to call flush only on online vCPUs. And
588 * queue flush_on_enter for pre-empted vCPUs
589 */
590 for_each_cpu(cpu, flushmask) {
Linus Torvalds152d32a2021-05-01 10:14:08 -0700591 /*
592 * The local vCPU is never preempted, so we do not explicitly
593 * skip check for local vCPU - it will never be cleared from
594 * flushmask.
595 */
Wanpeng Li2b519b572021-04-09 12:18:29 +0800596 src = &per_cpu(steal_time, cpu);
597 state = READ_ONCE(src->preempted);
598 if ((state & KVM_VCPU_PREEMPTED)) {
599 if (try_cmpxchg(&src->preempted, &state,
600 state | KVM_VCPU_FLUSH_TLB))
601 __cpumask_clear_cpu(cpu, flushmask);
602 }
603 }
604
Linus Torvalds152d32a2021-05-01 10:14:08 -0700605 native_flush_tlb_multi(flushmask, info);
Wanpeng Li2b519b572021-04-09 12:18:29 +0800606}
607
608static __init int kvm_alloc_cpumask(void)
609{
610 int cpu;
611
612 if (!kvm_para_available() || nopv)
613 return 0;
614
615 if (pv_tlb_flush_supported() || pv_ipi_supported())
616 for_each_possible_cpu(cpu) {
617 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu),
618 GFP_KERNEL, cpu_to_node(cpu));
619 }
620
621 return 0;
622}
623arch_initcall(kvm_alloc_cpumask);
624
Gleb Natapovca3f1012010-10-14 11:22:49 +0200625static void __init kvm_smp_prepare_boot_cpu(void)
626{
Brijesh Singh47162762017-10-20 09:30:58 -0500627 /*
628 * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
629 * shares the guest physical address with the hypervisor.
630 */
631 sev_map_percpu_data();
632
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200633 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200634 native_smp_prepare_boot_cpu();
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530635 kvm_spinlock_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200636}
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200637
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200638static void kvm_guest_cpu_offline(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200639{
Glauber Costad910f5c2011-07-11 15:28:19 -0400640 kvm_disable_steal_time();
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300641 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
642 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
643 kvm_pv_disable_apf();
Gleb Natapov631bc482010-10-14 11:22:52 +0200644 apf_task_wake_all();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200645}
646
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200647static int kvm_cpu_online(unsigned int cpu)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200648{
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200649 local_irq_disable();
650 kvm_guest_cpu_init();
651 local_irq_enable();
652 return 0;
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200653}
654
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200655static int kvm_cpu_down_prepare(unsigned int cpu)
656{
657 local_irq_disable();
658 kvm_guest_cpu_offline();
659 local_irq_enable();
660 return 0;
661}
Wanpeng Li2b519b572021-04-09 12:18:29 +0800662
Gleb Natapovca3f1012010-10-14 11:22:49 +0200663#endif
664
Juergen Grossf3614642017-11-09 14:27:38 +0100665static void __init kvm_guest_init(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500666{
Gleb Natapov631bc482010-10-14 11:22:52 +0200667 int i;
668
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500669 paravirt_ops_setup();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200670 register_reboot_notifier(&kvm_pv_reboot_nb);
Gleb Natapov631bc482010-10-14 11:22:52 +0200671 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
Rik van Riel9db284f2016-03-21 15:13:27 +0100672 raw_spin_lock_init(&async_pf_sleepers[i].lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200673
Glauber Costad910f5c2011-07-11 15:28:19 -0400674 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
675 has_steal_clock = 1;
Juergen Grossa0e2bf72021-03-11 15:23:09 +0100676 static_call_update(pv_steal_clock, kvm_steal_clock);
Glauber Costad910f5c2011-07-11 15:28:19 -0400677 }
678
Michael S. Tsirkin90536662012-07-15 15:56:52 +0300679 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
680 apic_set_eoi_write(kvm_guest_apic_eoi_write);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300681
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200682 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
Andy Lutomirskief680172020-02-28 10:42:48 -0800683 static_branch_enable(&kvm_async_pf_enabled);
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400684 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200685 }
Andy Lutomirskief680172020-02-28 10:42:48 -0800686
Gleb Natapovca3f1012010-10-14 11:22:49 +0200687#ifdef CONFIG_SMP
Wanpeng Li2b519b572021-04-09 12:18:29 +0800688 if (pv_tlb_flush_supported()) {
Linus Torvalds152d32a2021-05-01 10:14:08 -0700689 pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi;
Wanpeng Li2b519b572021-04-09 12:18:29 +0800690 pv_ops.mmu.tlb_remove_table = tlb_remove_table;
691 pr_info("KVM setup pv remote TLB flush\n");
692 }
693
Gleb Natapovca3f1012010-10-14 11:22:49 +0200694 smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
Wanpeng Lia262bca2020-02-18 09:08:23 +0800695 if (pv_sched_yield_supported()) {
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800696 smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi;
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800697 pr_info("setup PV sched yield\n");
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800698 }
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200699 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
700 kvm_cpu_online, kvm_cpu_down_prepare) < 0)
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800701 pr_err("failed to install cpu hotplug callbacks\n");
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200702#else
Brijesh Singh47162762017-10-20 09:30:58 -0500703 sev_map_percpu_data();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200704 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200705#endif
Ulrich Obergfell9919e392014-10-13 15:55:37 -0700706
707 /*
708 * Hard lockup detection is enabled by default. Disable it, as guests
709 * can get false positives too easily, for example if the host is
710 * overcommitted.
711 */
Ulrich Obergfell692297d2015-04-14 15:44:19 -0700712 hardlockup_detector_disable();
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500713}
Glauber Costad910f5c2011-07-11 15:28:19 -0400714
Paolo Bonzini1c300a42014-01-27 14:49:40 +0100715static noinline uint32_t __kvm_cpuid_base(void)
716{
717 if (boot_cpu_data.cpuid_level < 0)
718 return 0; /* So we don't blow up on old processors */
719
Borislav Petkov0c9f35362016-03-29 17:41:55 +0200720 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
Paolo Bonzini1c300a42014-01-27 14:49:40 +0100721 return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
722
723 return 0;
724}
725
726static inline uint32_t kvm_cpuid_base(void)
727{
728 static int kvm_cpuid_base = -1;
729
730 if (kvm_cpuid_base == -1)
731 kvm_cpuid_base = __kvm_cpuid_base();
732
733 return kvm_cpuid_base;
734}
735
736bool kvm_para_available(void)
737{
738 return kvm_cpuid_base() != 0;
739}
740EXPORT_SYMBOL_GPL(kvm_para_available);
741
Paolo Bonzini77f01bd2014-01-27 14:51:44 +0100742unsigned int kvm_arch_para_features(void)
743{
744 return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
745}
746
Wanpeng Lia4429e52018-02-13 09:05:40 +0800747unsigned int kvm_arch_para_hints(void)
748{
749 return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
750}
Wanpeng Li1328edc2019-08-29 16:49:57 +0800751EXPORT_SYMBOL_GPL(kvm_arch_para_hints);
Wanpeng Lia4429e52018-02-13 09:05:40 +0800752
Jason Wang9df56f12013-07-25 16:54:35 +0800753static uint32_t __init kvm_detect(void)
Prarit Bhargavafc733732012-07-06 13:47:39 -0400754{
Jason Wang9df56f12013-07-25 16:54:35 +0800755 return kvm_cpuid_base();
Prarit Bhargavafc733732012-07-06 13:47:39 -0400756}
757
Wanpeng Lid63bae02018-07-23 14:39:51 +0800758static void __init kvm_apic_init(void)
759{
Wanpeng Li2b519b572021-04-09 12:18:29 +0800760#ifdef CONFIG_SMP
Wanpeng Lia262bca2020-02-18 09:08:23 +0800761 if (pv_ipi_supported())
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800762 kvm_setup_pv_ipi();
763#endif
Wanpeng Lid63bae02018-07-23 14:39:51 +0800764}
765
David Woodhouse2e008ff2020-10-24 22:35:35 +0100766static bool __init kvm_msi_ext_dest_id(void)
767{
768 return kvm_para_has_feature(KVM_FEATURE_MSI_EXT_DEST_ID);
769}
770
Wanpeng Lid63bae02018-07-23 14:39:51 +0800771static void __init kvm_init_platform(void)
772{
Linus Torvaldse61cf2e2018-08-19 10:38:36 -0700773 kvmclock_init();
Wanpeng Lid63bae02018-07-23 14:39:51 +0800774 x86_platform.apic_post_init = kvm_apic_init;
775}
776
Tom Lendacky99419b22020-09-07 15:16:04 +0200777#if defined(CONFIG_AMD_MEM_ENCRYPT)
778static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
779{
780 /* RAX and CPL are already in the GHCB */
781 ghcb_set_rbx(ghcb, regs->bx);
782 ghcb_set_rcx(ghcb, regs->cx);
783 ghcb_set_rdx(ghcb, regs->dx);
784 ghcb_set_rsi(ghcb, regs->si);
785}
786
787static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
788{
789 /* No checking of the return state needed */
790 return true;
791}
792#endif
793
Juergen Gross03b2a322017-11-09 14:27:36 +0100794const __initconst struct hypervisor_x86 x86_hyper_kvm = {
Tom Lendacky99419b22020-09-07 15:16:04 +0200795 .name = "KVM",
796 .detect = kvm_detect,
797 .type = X86_HYPER_KVM,
798 .init.guest_late_init = kvm_guest_init,
799 .init.x2apic_available = kvm_para_available,
David Woodhouse2e008ff2020-10-24 22:35:35 +0100800 .init.msi_ext_dest_id = kvm_msi_ext_dest_id,
Tom Lendacky99419b22020-09-07 15:16:04 +0200801 .init.init_platform = kvm_init_platform,
802#if defined(CONFIG_AMD_MEM_ENCRYPT)
803 .runtime.sev_es_hcall_prepare = kvm_sev_es_hcall_prepare,
804 .runtime.sev_es_hcall_finish = kvm_sev_es_hcall_finish,
805#endif
Prarit Bhargavafc733732012-07-06 13:47:39 -0400806};
Prarit Bhargavafc733732012-07-06 13:47:39 -0400807
Glauber Costad910f5c2011-07-11 15:28:19 -0400808static __init int activate_jump_labels(void)
809{
810 if (has_steal_clock) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100811 static_key_slow_inc(&paravirt_steal_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400812 if (steal_acc)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100813 static_key_slow_inc(&paravirt_steal_rq_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400814 }
815
816 return 0;
817}
818arch_initcall(activate_jump_labels);
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530819
820#ifdef CONFIG_PARAVIRT_SPINLOCKS
821
822/* Kick a cpu by its apicid. Used to wake up a halted vcpu */
Raghavendra K T36bd6212013-08-16 15:08:41 +0530823static void kvm_kick_cpu(int cpu)
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530824{
825 int apicid;
826 unsigned long flags = 0;
827
828 apicid = per_cpu(x86_cpu_to_apicid, cpu);
829 kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
830}
831
Waiman Longbf0c7c32015-04-24 14:56:39 -0400832#include <asm/qspinlock.h>
833
834static void kvm_wait(u8 *ptr, u8 val)
835{
Waiman Longbf0c7c32015-04-24 14:56:39 -0400836 if (in_nmi())
837 return;
838
Waiman Longbf0c7c32015-04-24 14:56:39 -0400839 /*
840 * halt until it's our turn and kicked. Note that we do safe halt
841 * for irq enabled case to avoid hang when lock info is overwritten
842 * in irq spinlock slowpath and no spurious interrupt occur to save us.
843 */
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800844 if (irqs_disabled()) {
845 if (READ_ONCE(*ptr) == val)
846 halt();
847 } else {
848 local_irq_disable();
Waiman Longbf0c7c32015-04-24 14:56:39 -0400849
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800850 if (READ_ONCE(*ptr) == val)
851 safe_halt();
852
853 local_irq_enable();
854 }
Waiman Longbf0c7c32015-04-24 14:56:39 -0400855}
856
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500857#ifdef CONFIG_X86_32
Waiman Long6c629852017-02-20 13:36:03 -0500858__visible bool __kvm_vcpu_is_preempted(long cpu)
Peter Zijlstra3cded412016-11-15 16:47:06 +0100859{
860 struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
861
Wanpeng Lifa55eed2017-12-12 17:33:01 -0800862 return !!(src->preempted & KVM_VCPU_PREEMPTED);
Peter Zijlstra3cded412016-11-15 16:47:06 +0100863}
864PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
865
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500866#else
867
868#include <asm/asm-offsets.h>
869
870extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
871
872/*
873 * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
874 * restoring to/from the stack.
875 */
876asm(
877".pushsection .text;"
878".global __raw_callee_save___kvm_vcpu_is_preempted;"
879".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
880"__raw_callee_save___kvm_vcpu_is_preempted:"
881"movq __per_cpu_offset(,%rdi,8), %rax;"
882"cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
883"setne %al;"
884"ret;"
Josh Poimboeuf083db672019-07-17 20:36:36 -0500885".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;"
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500886".popsection");
887
888#endif
889
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530890/*
891 * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
892 */
893void __init kvm_spinlock_init(void)
894{
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800895 /*
896 * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
897 * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
898 * preferred over native qspinlock when vCPU is preempted.
899 */
900 if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
901 pr_info("PV spinlocks disabled, no host support\n");
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530902 return;
Zhenzhong Duande585022019-10-23 19:16:20 +0800903 }
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530904
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800905 /*
906 * Disable PV spinlocks and use native qspinlock when dedicated pCPUs
907 * are available.
908 */
909 if (kvm_para_has_hint(KVM_HINTS_REALTIME)) {
910 pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n");
911 goto out;
912 }
Wanpeng Lib2798ba2018-02-13 09:05:41 +0800913
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800914 if (num_possible_cpus() == 1) {
915 pr_info("PV spinlocks disabled, single CPU\n");
916 goto out;
917 }
918
919 if (nopvspin) {
920 pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n");
921 goto out;
922 }
923
924 pr_info("PV spinlocks enabled\n");
Waiman Long3553ae52018-07-17 17:59:27 -0400925
Waiman Longbf0c7c32015-04-24 14:56:39 -0400926 __pv_init_lock_hash();
Juergen Gross5c835112018-08-28 09:40:19 +0200927 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
928 pv_ops.lock.queued_spin_unlock =
929 PV_CALLEE_SAVE(__pv_queued_spin_unlock);
930 pv_ops.lock.wait = kvm_wait;
931 pv_ops.lock.kick = kvm_kick_cpu;
Peter Zijlstra3cded412016-11-15 16:47:06 +0100932
933 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
Juergen Gross5c835112018-08-28 09:40:19 +0200934 pv_ops.lock.vcpu_is_preempted =
Peter Zijlstra3cded412016-11-15 16:47:06 +0100935 PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
936 }
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800937 /*
938 * When PV spinlock is enabled which is preferred over
939 * virt_spin_lock(), virt_spin_lock_key's value is meaningless.
940 * Just disable it anyway.
941 */
942out:
943 static_branch_disable(&virt_spin_lock_key);
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530944}
Raghavendra K T3dbef3e2013-10-09 14:33:21 +0530945
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530946#endif /* CONFIG_PARAVIRT_SPINLOCKS */
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300947
948#ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
949
950static void kvm_disable_host_haltpoll(void *i)
951{
952 wrmsrl(MSR_KVM_POLL_CONTROL, 0);
953}
954
955static void kvm_enable_host_haltpoll(void *i)
956{
957 wrmsrl(MSR_KVM_POLL_CONTROL, 1);
958}
959
Joao Martins97d3eb92019-09-02 11:40:31 +0100960void arch_haltpoll_enable(unsigned int cpu)
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300961{
962 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800963 pr_err_once("host does not support poll control\n");
964 pr_err_once("host upgrade recommended\n");
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300965 return;
966 }
967
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300968 /* Enable guest halt poll disables host halt poll */
Joao Martins97d3eb92019-09-02 11:40:31 +0100969 smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300970}
971EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
972
Joao Martins97d3eb92019-09-02 11:40:31 +0100973void arch_haltpoll_disable(unsigned int cpu)
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300974{
975 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
976 return;
977
Li Qiangb785a442020-09-24 08:58:00 -0700978 /* Disable guest halt poll enables host halt poll */
Joao Martins97d3eb92019-09-02 11:40:31 +0100979 smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300980}
981EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
982#endif