blob: c97859170286cf801171b4a529183d6af2ff6ab5 [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>
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +020029#include <linux/syscore_ops.h>
Marcelo Tosattia90ede72009-02-11 22:45:42 -020030#include <asm/timer.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020031#include <asm/cpu.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020032#include <asm/traps.h>
33#include <asm/desc.h>
Gleb Natapov6c047cd2010-10-14 11:22:54 +020034#include <asm/tlbflush.h>
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030035#include <asm/apic.h>
36#include <asm/apicdef.h>
Prarit Bhargavafc733732012-07-06 13:47:39 -040037#include <asm/hypervisor.h>
Peter Zijlstra48a8b972018-08-22 17:30:16 +020038#include <asm/tlb.h>
Yi Wang19308a42019-10-10 14:37:25 +080039#include <asm/cpuidle_haltpoll.h>
Tom Lendacky99419b22020-09-07 15:16:04 +020040#include <asm/ptrace.h>
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +020041#include <asm/reboot.h>
Tom Lendacky99419b22020-09-07 15:16:04 +020042#include <asm/svm.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050043
Andy Lutomirskief680172020-02-28 10:42:48 -080044DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
45
Gleb Natapovfd10cde2010-10-14 11:22:51 +020046static int kvmapf = 1;
47
Dou Liyangafdc3f582018-01-17 11:46:54 +080048static int __init parse_no_kvmapf(char *arg)
Gleb Natapovfd10cde2010-10-14 11:22:51 +020049{
50 kvmapf = 0;
51 return 0;
52}
53
54early_param("no-kvmapf", parse_no_kvmapf);
55
Glauber Costad910f5c2011-07-11 15:28:19 -040056static int steal_acc = 1;
Dou Liyangafdc3f582018-01-17 11:46:54 +080057static int __init parse_no_stealacc(char *arg)
Glauber Costad910f5c2011-07-11 15:28:19 -040058{
59 steal_acc = 0;
60 return 0;
61}
62
63early_param("no-steal-acc", parse_no_stealacc);
64
Brijesh Singh47162762017-10-20 09:30:58 -050065static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
Andi Kleen14e581c2019-03-29 17:47:42 -070066DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible;
Glauber Costad910f5c2011-07-11 15:28:19 -040067static int has_steal_clock = 0;
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050068
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -050069/*
70 * No need for any "IO delay" on KVM
71 */
72static void kvm_io_delay(void)
73{
74}
75
Gleb Natapov631bc482010-10-14 11:22:52 +020076#define KVM_TASK_SLEEP_HASHBITS 8
77#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
78
79struct kvm_task_sleep_node {
80 struct hlist_node link;
Rik van Riel9db284f2016-03-21 15:13:27 +010081 struct swait_queue_head wq;
Gleb Natapov631bc482010-10-14 11:22:52 +020082 u32 token;
83 int cpu;
84};
85
86static struct kvm_task_sleep_head {
Rik van Riel9db284f2016-03-21 15:13:27 +010087 raw_spinlock_t lock;
Gleb Natapov631bc482010-10-14 11:22:52 +020088 struct hlist_head list;
89} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
90
91static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
92 u32 token)
93{
94 struct hlist_node *p;
95
96 hlist_for_each(p, &b->list) {
97 struct kvm_task_sleep_node *n =
98 hlist_entry(p, typeof(*n), link);
99 if (n->token == token)
100 return n;
101 }
102
103 return NULL;
104}
105
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200106static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)
Gleb Natapov631bc482010-10-14 11:22:52 +0200107{
108 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
109 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100110 struct kvm_task_sleep_node *e;
Li Zhong9b132fb2012-12-04 10:35:13 +0800111
Rik van Riel9db284f2016-03-21 15:13:27 +0100112 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200113 e = _find_apf_task(b, token);
114 if (e) {
115 /* dummy entry exist -> wake up was delivered ahead of PF */
116 hlist_del(&e->link);
Rik van Riel9db284f2016-03-21 15:13:27 +0100117 raw_spin_unlock(&b->lock);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100118 kfree(e);
119 return false;
Gleb Natapov631bc482010-10-14 11:22:52 +0200120 }
121
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100122 n->token = token;
123 n->cpu = smp_processor_id();
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100124 init_swait_queue_head(&n->wq);
125 hlist_add_head(&n->link, &b->list);
Rik van Riel9db284f2016-03-21 15:13:27 +0100126 raw_spin_unlock(&b->lock);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100127 return true;
128}
129
130/*
131 * kvm_async_pf_task_wait_schedule - Wait for pagefault to be handled
132 * @token: Token to identify the sleep node entry
133 *
134 * Invoked from the async pagefault handling code or from the VM exit page
135 * fault handler. In both cases RCU is watching.
136 */
137void kvm_async_pf_task_wait_schedule(u32 token)
138{
139 struct kvm_task_sleep_node n;
140 DECLARE_SWAITQUEUE(wait);
141
142 lockdep_assert_irqs_disabled();
143
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200144 if (!kvm_async_pf_queue_task(token, &n))
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100145 return;
Gleb Natapov631bc482010-10-14 11:22:52 +0200146
147 for (;;) {
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100148 prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
Gleb Natapov631bc482010-10-14 11:22:52 +0200149 if (hlist_unhashed(&n.link))
150 break;
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200151
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100152 local_irq_enable();
153 schedule();
154 local_irq_disable();
Gleb Natapov631bc482010-10-14 11:22:52 +0200155 }
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100156 finish_swait(&n.wq, &wait);
Gleb Natapov631bc482010-10-14 11:22:52 +0200157}
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100158EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule);
159
Gleb Natapov631bc482010-10-14 11:22:52 +0200160static void apf_task_wake_one(struct kvm_task_sleep_node *n)
161{
162 hlist_del_init(&n->link);
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200163 if (swq_has_sleeper(&n->wq))
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200164 swake_up_one(&n->wq);
Gleb Natapov631bc482010-10-14 11:22:52 +0200165}
166
167static void apf_task_wake_all(void)
168{
169 int i;
170
171 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
Gleb Natapov631bc482010-10-14 11:22:52 +0200172 struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100173 struct kvm_task_sleep_node *n;
174 struct hlist_node *p, *next;
175
Rik van Riel9db284f2016-03-21 15:13:27 +0100176 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200177 hlist_for_each_safe(p, next, &b->list) {
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100178 n = hlist_entry(p, typeof(*n), link);
Gleb Natapov631bc482010-10-14 11:22:52 +0200179 if (n->cpu == smp_processor_id())
180 apf_task_wake_one(n);
181 }
Rik van Riel9db284f2016-03-21 15:13:27 +0100182 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200183 }
184}
185
186void kvm_async_pf_task_wake(u32 token)
187{
188 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
189 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
190 struct kvm_task_sleep_node *n;
191
192 if (token == ~0) {
193 apf_task_wake_all();
194 return;
195 }
196
197again:
Rik van Riel9db284f2016-03-21 15:13:27 +0100198 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200199 n = _find_apf_task(b, token);
200 if (!n) {
201 /*
202 * async PF was not yet handled.
203 * Add dummy entry for the token.
204 */
Gleb Natapov62c49cc2012-05-02 15:04:02 +0300205 n = kzalloc(sizeof(*n), GFP_ATOMIC);
Gleb Natapov631bc482010-10-14 11:22:52 +0200206 if (!n) {
207 /*
208 * Allocation failed! Busy wait while other cpu
209 * handles async PF.
210 */
Rik van Riel9db284f2016-03-21 15:13:27 +0100211 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200212 cpu_relax();
213 goto again;
214 }
215 n->token = token;
216 n->cpu = smp_processor_id();
Rik van Riel9db284f2016-03-21 15:13:27 +0100217 init_swait_queue_head(&n->wq);
Gleb Natapov631bc482010-10-14 11:22:52 +0200218 hlist_add_head(&n->link, &b->list);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100219 } else {
Gleb Natapov631bc482010-10-14 11:22:52 +0200220 apf_task_wake_one(n);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100221 }
Rik van Riel9db284f2016-03-21 15:13:27 +0100222 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200223 return;
224}
225EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
226
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200227noinstr u32 kvm_read_and_reset_apf_flags(void)
Gleb Natapov631bc482010-10-14 11:22:52 +0200228{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200229 u32 flags = 0;
Gleb Natapov631bc482010-10-14 11:22:52 +0200230
Christoph Lameter89cbc762014-08-17 12:30:40 -0500231 if (__this_cpu_read(apf_reason.enabled)) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200232 flags = __this_cpu_read(apf_reason.flags);
233 __this_cpu_write(apf_reason.flags, 0);
Gleb Natapov631bc482010-10-14 11:22:52 +0200234 }
235
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200236 return flags;
Gleb Natapov631bc482010-10-14 11:22:52 +0200237}
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200238EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags);
Gleb Natapov631bc482010-10-14 11:22:52 +0200239
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200240noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token)
Gleb Natapov631bc482010-10-14 11:22:52 +0200241{
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200242 u32 flags = kvm_read_and_reset_apf_flags();
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200243 irqentry_state_t state;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100244
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200245 if (!flags)
Andy Lutomirskief680172020-02-28 10:42:48 -0800246 return false;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100247
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200248 state = irqentry_enter(regs);
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200249 instrumentation_begin();
250
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100251 /*
252 * If the host managed to inject an async #PF into an interrupt
253 * disabled region, then die hard as this is not going to end well
254 * and the host side is seriously broken.
255 */
256 if (unlikely(!(regs->flags & X86_EFLAGS_IF)))
257 panic("Host injected async #PF in interrupt disabled region\n");
258
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200259 if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200260 if (unlikely(!(user_mode(regs))))
261 panic("Host injected async #PF in kernel mode\n");
262 /* Page is swapped out by the host. */
263 kvm_async_pf_task_wait_schedule(token);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100264 } else {
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400265 WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags);
Gleb Natapov631bc482010-10-14 11:22:52 +0200266 }
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200267
268 instrumentation_end();
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200269 irqentry_exit(regs, state);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100270 return true;
Gleb Natapov631bc482010-10-14 11:22:52 +0200271}
272
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400273DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt)
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200274{
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400275 struct pt_regs *old_regs = set_irq_regs(regs);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200276 u32 token;
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200277
Vitaly Kuznetsovcc17b222020-09-08 15:53:50 +0200278 ack_APIC_irq();
279
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200280 inc_irq_stat(irq_hv_callback_count);
281
282 if (__this_cpu_read(apf_reason.enabled)) {
283 token = __this_cpu_read(apf_reason.token);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200284 kvm_async_pf_task_wake(token);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200285 __this_cpu_write(apf_reason.token, 0);
286 wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1);
287 }
288
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400289 set_irq_regs(old_regs);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200290}
291
Rakib Mullickd3ac8812009-07-02 11:40:36 +0600292static void __init paravirt_ops_setup(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500293{
294 pv_info.name = "KVM";
Andy Lutomirski29fa6822014-12-05 19:03:28 -0800295
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500296 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
Juergen Gross5c835112018-08-28 09:40:19 +0200297 pv_ops.cpu.io_delay = kvm_io_delay;
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500298
Marcelo Tosattia90ede72009-02-11 22:45:42 -0200299#ifdef CONFIG_X86_IO_APIC
300 no_timer_check = 1;
301#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500302}
303
Glauber Costad910f5c2011-07-11 15:28:19 -0400304static void kvm_register_steal_time(void)
305{
306 int cpu = smp_processor_id();
307 struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
308
309 if (!has_steal_clock)
310 return;
311
Dave Hansen5dfd4862013-01-22 13:24:35 -0800312 wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800313 pr_info("stealtime: cpu %d, msr %llx\n", cpu,
314 (unsigned long long) slow_virt_to_phys(st));
Glauber Costad910f5c2011-07-11 15:28:19 -0400315}
316
Brijesh Singh47162762017-10-20 09:30:58 -0500317static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300318
Wanpeng Li8ca22552016-11-07 11:13:40 +0800319static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300320{
321 /**
322 * This relies on __test_and_clear_bit to modify the memory
323 * in a way that is atomic with respect to the local CPU.
324 * The hypervisor only accesses this memory from the local CPU so
325 * there's no need for lock or memory barriers.
326 * An optimization barrier is implied in apic write.
327 */
Christoph Lameter89cbc762014-08-17 12:30:40 -0500328 if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300329 return;
Wanpeng Li8ca22552016-11-07 11:13:40 +0800330 apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300331}
332
Nicholas Krauseed3cf152015-05-20 00:24:10 -0400333static void kvm_guest_cpu_init(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200334{
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200335 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
336 u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
Andy Lutomirskief680172020-02-28 10:42:48 -0800337
338 WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled));
339
340 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200341 pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
Wanpeng Li52a5c152017-07-13 18:30:42 -0700342
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100343 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
344 pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
345
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200346 wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR);
347
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100348 wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500349 __this_cpu_write(apf_reason.enabled, 1);
Vitaly Kuznetsov0a269a02021-04-14 14:35:40 +0200350 pr_info("setup async PF for cpu %d\n", smp_processor_id());
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200351 }
Glauber Costad910f5c2011-07-11 15:28:19 -0400352
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300353 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
354 unsigned long pa;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100355
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300356 /* Size alignment is implied but just to make it explicit. */
357 BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500358 __this_cpu_write(kvm_apic_eoi, 0);
359 pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
Dave Hansen5dfd4862013-01-22 13:24:35 -0800360 | KVM_MSR_ENABLED;
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300361 wrmsrl(MSR_KVM_PV_EOI_EN, pa);
362 }
363
Glauber Costad910f5c2011-07-11 15:28:19 -0400364 if (has_steal_clock)
365 kvm_register_steal_time();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200366}
367
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300368static void kvm_pv_disable_apf(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200369{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500370 if (!__this_cpu_read(apf_reason.enabled))
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200371 return;
372
373 wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500374 __this_cpu_write(apf_reason.enabled, 0);
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200375
Vitaly Kuznetsov0a269a02021-04-14 14:35:40 +0200376 pr_info("disable async PF for cpu %d\n", smp_processor_id());
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200377}
378
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200379static void kvm_disable_steal_time(void)
380{
381 if (!has_steal_clock)
382 return;
383
384 wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
385}
386
Glauber Costad910f5c2011-07-11 15:28:19 -0400387static u64 kvm_steal_clock(int cpu)
388{
389 u64 steal;
390 struct kvm_steal_time *src;
391 int version;
392
393 src = &per_cpu(steal_time, cpu);
394 do {
395 version = src->version;
Wanpeng Li5a48a622017-04-11 02:49:21 -0700396 virt_rmb();
Glauber Costad910f5c2011-07-11 15:28:19 -0400397 steal = src->steal;
Wanpeng Li5a48a622017-04-11 02:49:21 -0700398 virt_rmb();
Glauber Costad910f5c2011-07-11 15:28:19 -0400399 } while ((version & 1) || (version != src->version));
400
401 return steal;
402}
403
Brijesh Singh47162762017-10-20 09:30:58 -0500404static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
405{
406 early_set_memory_decrypted((unsigned long) ptr, size);
407}
408
409/*
410 * Iterate through all possible CPUs and map the memory region pointed
411 * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
412 *
413 * Note: we iterate through all possible CPUs to ensure that CPUs
414 * hotplugged will have their per-cpu variable already mapped as
415 * decrypted.
416 */
417static void __init sev_map_percpu_data(void)
418{
419 int cpu;
420
421 if (!sev_active())
422 return;
423
424 for_each_possible_cpu(cpu) {
425 __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
426 __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
427 __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
428 }
429}
430
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200431static void kvm_guest_cpu_offline(bool shutdown)
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200432{
433 kvm_disable_steal_time();
434 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
435 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
436 kvm_pv_disable_apf();
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200437 if (!shutdown)
438 apf_task_wake_all();
Vitaly Kuznetsovc02027b2021-04-14 14:35:42 +0200439 kvmclock_disable();
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200440}
441
442static int kvm_cpu_online(unsigned int cpu)
443{
444 unsigned long flags;
445
446 local_irq_save(flags);
447 kvm_guest_cpu_init();
448 local_irq_restore(flags);
449 return 0;
450}
451
Wanpeng Li2b519b572021-04-09 12:18:29 +0800452#ifdef CONFIG_SMP
453
454static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask);
455
Wanpeng Lia262bca2020-02-18 09:08:23 +0800456static bool pv_tlb_flush_supported(void)
457{
458 return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
459 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
460 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
461}
462
Wanpeng Lia262bca2020-02-18 09:08:23 +0800463static bool pv_ipi_supported(void)
464{
465 return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI);
466}
467
468static bool pv_sched_yield_supported(void)
469{
470 return (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) &&
471 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
472 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
473}
474
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800475#define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
476
477static void __send_ipi_mask(const struct cpumask *mask, int vector)
478{
479 unsigned long flags;
480 int cpu, apic_id, icr;
481 int min = 0, max = 0;
482#ifdef CONFIG_X86_64
483 __uint128_t ipi_bitmap = 0;
484#else
485 u64 ipi_bitmap = 0;
486#endif
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800487 long ret;
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800488
489 if (cpumask_empty(mask))
490 return;
491
492 local_irq_save(flags);
493
494 switch (vector) {
495 default:
496 icr = APIC_DM_FIXED | vector;
497 break;
498 case NMI_VECTOR:
499 icr = APIC_DM_NMI;
500 break;
501 }
502
503 for_each_cpu(cpu, mask) {
504 apic_id = per_cpu(x86_cpu_to_apicid, cpu);
505 if (!ipi_bitmap) {
506 min = max = apic_id;
507 } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
508 ipi_bitmap <<= min - apic_id;
509 min = apic_id;
510 } else if (apic_id < min + KVM_IPI_CLUSTER_SIZE) {
511 max = apic_id < max ? max : apic_id;
512 } else {
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800513 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800514 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800515 WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
516 ret);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800517 min = max = apic_id;
518 ipi_bitmap = 0;
519 }
520 __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
521 }
522
523 if (ipi_bitmap) {
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800524 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800525 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800526 WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
527 ret);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800528 }
529
530 local_irq_restore(flags);
531}
532
533static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
534{
535 __send_ipi_mask(mask, vector);
536}
537
538static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
539{
540 unsigned int this_cpu = smp_processor_id();
Wanpeng Li8a9442f2020-02-18 09:08:24 +0800541 struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800542 const struct cpumask *local_mask;
543
Wanpeng Li8a9442f2020-02-18 09:08:24 +0800544 cpumask_copy(new_mask, mask);
545 cpumask_clear_cpu(this_cpu, new_mask);
546 local_mask = new_mask;
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800547 __send_ipi_mask(local_mask, vector);
548}
549
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800550/*
551 * Set the IPI entry points
552 */
553static void kvm_setup_pv_ipi(void)
554{
555 apic->send_IPI_mask = kvm_send_ipi_mask;
556 apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800557 pr_info("setup PV IPIs\n");
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800558}
559
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800560static void kvm_smp_send_call_func_ipi(const struct cpumask *mask)
561{
562 int cpu;
563
564 native_send_call_func_ipi(mask);
565
566 /* Make sure other vCPUs get a chance to run if they need to. */
567 for_each_cpu(cpu, mask) {
568 if (vcpu_is_preempted(cpu)) {
569 kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu));
570 break;
571 }
572 }
573}
574
Linus Torvalds152d32a2021-05-01 10:14:08 -0700575static void kvm_flush_tlb_multi(const struct cpumask *cpumask,
Wanpeng Li2b519b572021-04-09 12:18:29 +0800576 const struct flush_tlb_info *info)
577{
578 u8 state;
579 int cpu;
580 struct kvm_steal_time *src;
581 struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
582
583 cpumask_copy(flushmask, cpumask);
584 /*
585 * We have to call flush only on online vCPUs. And
586 * queue flush_on_enter for pre-empted vCPUs
587 */
588 for_each_cpu(cpu, flushmask) {
Linus Torvalds152d32a2021-05-01 10:14:08 -0700589 /*
590 * The local vCPU is never preempted, so we do not explicitly
591 * skip check for local vCPU - it will never be cleared from
592 * flushmask.
593 */
Wanpeng Li2b519b572021-04-09 12:18:29 +0800594 src = &per_cpu(steal_time, cpu);
595 state = READ_ONCE(src->preempted);
596 if ((state & KVM_VCPU_PREEMPTED)) {
597 if (try_cmpxchg(&src->preempted, &state,
598 state | KVM_VCPU_FLUSH_TLB))
599 __cpumask_clear_cpu(cpu, flushmask);
600 }
601 }
602
Linus Torvalds152d32a2021-05-01 10:14:08 -0700603 native_flush_tlb_multi(flushmask, info);
Wanpeng Li2b519b572021-04-09 12:18:29 +0800604}
605
606static __init int kvm_alloc_cpumask(void)
607{
608 int cpu;
609
610 if (!kvm_para_available() || nopv)
611 return 0;
612
613 if (pv_tlb_flush_supported() || pv_ipi_supported())
614 for_each_possible_cpu(cpu) {
615 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu),
616 GFP_KERNEL, cpu_to_node(cpu));
617 }
618
619 return 0;
620}
621arch_initcall(kvm_alloc_cpumask);
622
Gleb Natapovca3f1012010-10-14 11:22:49 +0200623static void __init kvm_smp_prepare_boot_cpu(void)
624{
Brijesh Singh47162762017-10-20 09:30:58 -0500625 /*
626 * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
627 * shares the guest physical address with the hypervisor.
628 */
629 sev_map_percpu_data();
630
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200631 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200632 native_smp_prepare_boot_cpu();
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530633 kvm_spinlock_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200634}
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200635
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200636static int kvm_cpu_down_prepare(unsigned int cpu)
637{
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200638 unsigned long flags;
639
640 local_irq_save(flags);
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200641 kvm_guest_cpu_offline(false);
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200642 local_irq_restore(flags);
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200643 return 0;
644}
Wanpeng Li2b519b572021-04-09 12:18:29 +0800645
Gleb Natapovca3f1012010-10-14 11:22:49 +0200646#endif
647
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200648static int kvm_suspend(void)
649{
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200650 kvm_guest_cpu_offline(false);
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200651
652 return 0;
653}
654
655static void kvm_resume(void)
656{
657 kvm_cpu_online(raw_smp_processor_id());
658}
659
660static struct syscore_ops kvm_syscore_ops = {
661 .suspend = kvm_suspend,
662 .resume = kvm_resume,
663};
664
Vitaly Kuznetsov384fc672021-04-14 14:35:44 +0200665static void kvm_pv_guest_cpu_reboot(void *unused)
666{
667 kvm_guest_cpu_offline(true);
668}
669
670static int kvm_pv_reboot_notify(struct notifier_block *nb,
671 unsigned long code, void *unused)
672{
673 if (code == SYS_RESTART)
674 on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
675 return NOTIFY_DONE;
676}
677
678static struct notifier_block kvm_pv_reboot_nb = {
679 .notifier_call = kvm_pv_reboot_notify,
680};
681
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200682/*
683 * After a PV feature is registered, the host will keep writing to the
684 * registered memory location. If the guest happens to shutdown, this memory
685 * won't be valid. In cases like kexec, in which you install a new kernel, this
686 * means a random memory location will be kept being written.
687 */
688#ifdef CONFIG_KEXEC_CORE
689static void kvm_crash_shutdown(struct pt_regs *regs)
690{
691 kvm_guest_cpu_offline(true);
692 native_machine_crash_shutdown(regs);
693}
694#endif
695
Juergen Grossf3614642017-11-09 14:27:38 +0100696static void __init kvm_guest_init(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500697{
Gleb Natapov631bc482010-10-14 11:22:52 +0200698 int i;
699
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500700 paravirt_ops_setup();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200701 register_reboot_notifier(&kvm_pv_reboot_nb);
Gleb Natapov631bc482010-10-14 11:22:52 +0200702 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
Rik van Riel9db284f2016-03-21 15:13:27 +0100703 raw_spin_lock_init(&async_pf_sleepers[i].lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200704
Glauber Costad910f5c2011-07-11 15:28:19 -0400705 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
706 has_steal_clock = 1;
Juergen Grossa0e2bf72021-03-11 15:23:09 +0100707 static_call_update(pv_steal_clock, kvm_steal_clock);
Glauber Costad910f5c2011-07-11 15:28:19 -0400708 }
709
Michael S. Tsirkin90536662012-07-15 15:56:52 +0300710 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
711 apic_set_eoi_write(kvm_guest_apic_eoi_write);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300712
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200713 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
Andy Lutomirskief680172020-02-28 10:42:48 -0800714 static_branch_enable(&kvm_async_pf_enabled);
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400715 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200716 }
Andy Lutomirskief680172020-02-28 10:42:48 -0800717
Gleb Natapovca3f1012010-10-14 11:22:49 +0200718#ifdef CONFIG_SMP
Wanpeng Li2b519b572021-04-09 12:18:29 +0800719 if (pv_tlb_flush_supported()) {
Linus Torvalds152d32a2021-05-01 10:14:08 -0700720 pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi;
Wanpeng Li2b519b572021-04-09 12:18:29 +0800721 pv_ops.mmu.tlb_remove_table = tlb_remove_table;
722 pr_info("KVM setup pv remote TLB flush\n");
723 }
724
Gleb Natapovca3f1012010-10-14 11:22:49 +0200725 smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
Wanpeng Lia262bca2020-02-18 09:08:23 +0800726 if (pv_sched_yield_supported()) {
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800727 smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi;
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800728 pr_info("setup PV sched yield\n");
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800729 }
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200730 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
731 kvm_cpu_online, kvm_cpu_down_prepare) < 0)
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800732 pr_err("failed to install cpu hotplug callbacks\n");
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200733#else
Brijesh Singh47162762017-10-20 09:30:58 -0500734 sev_map_percpu_data();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200735 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200736#endif
Ulrich Obergfell9919e392014-10-13 15:55:37 -0700737
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200738#ifdef CONFIG_KEXEC_CORE
739 machine_ops.crash_shutdown = kvm_crash_shutdown;
740#endif
741
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200742 register_syscore_ops(&kvm_syscore_ops);
743
Ulrich Obergfell9919e392014-10-13 15:55:37 -0700744 /*
745 * Hard lockup detection is enabled by default. Disable it, as guests
746 * can get false positives too easily, for example if the host is
747 * overcommitted.
748 */
Ulrich Obergfell692297d2015-04-14 15:44:19 -0700749 hardlockup_detector_disable();
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500750}
Glauber Costad910f5c2011-07-11 15:28:19 -0400751
Paolo Bonzini1c300a42014-01-27 14:49:40 +0100752static noinline uint32_t __kvm_cpuid_base(void)
753{
754 if (boot_cpu_data.cpuid_level < 0)
755 return 0; /* So we don't blow up on old processors */
756
Borislav Petkov0c9f35362016-03-29 17:41:55 +0200757 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
Paul Durrant760849b2021-11-05 09:51:01 +0000758 return hypervisor_cpuid_base(KVM_SIGNATURE, 0);
Paolo Bonzini1c300a42014-01-27 14:49:40 +0100759
760 return 0;
761}
762
763static inline uint32_t kvm_cpuid_base(void)
764{
765 static int kvm_cpuid_base = -1;
766
767 if (kvm_cpuid_base == -1)
768 kvm_cpuid_base = __kvm_cpuid_base();
769
770 return kvm_cpuid_base;
771}
772
773bool kvm_para_available(void)
774{
775 return kvm_cpuid_base() != 0;
776}
777EXPORT_SYMBOL_GPL(kvm_para_available);
778
Paolo Bonzini77f01bd2014-01-27 14:51:44 +0100779unsigned int kvm_arch_para_features(void)
780{
781 return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
782}
783
Wanpeng Lia4429e52018-02-13 09:05:40 +0800784unsigned int kvm_arch_para_hints(void)
785{
786 return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
787}
Wanpeng Li1328edc2019-08-29 16:49:57 +0800788EXPORT_SYMBOL_GPL(kvm_arch_para_hints);
Wanpeng Lia4429e52018-02-13 09:05:40 +0800789
Jason Wang9df56f12013-07-25 16:54:35 +0800790static uint32_t __init kvm_detect(void)
Prarit Bhargavafc733732012-07-06 13:47:39 -0400791{
Jason Wang9df56f12013-07-25 16:54:35 +0800792 return kvm_cpuid_base();
Prarit Bhargavafc733732012-07-06 13:47:39 -0400793}
794
Wanpeng Lid63bae02018-07-23 14:39:51 +0800795static void __init kvm_apic_init(void)
796{
Wanpeng Li2b519b572021-04-09 12:18:29 +0800797#ifdef CONFIG_SMP
Wanpeng Lia262bca2020-02-18 09:08:23 +0800798 if (pv_ipi_supported())
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800799 kvm_setup_pv_ipi();
800#endif
Wanpeng Lid63bae02018-07-23 14:39:51 +0800801}
802
David Woodhouse2e008ff2020-10-24 22:35:35 +0100803static bool __init kvm_msi_ext_dest_id(void)
804{
805 return kvm_para_has_feature(KVM_FEATURE_MSI_EXT_DEST_ID);
806}
807
Wanpeng Lid63bae02018-07-23 14:39:51 +0800808static void __init kvm_init_platform(void)
809{
Linus Torvaldse61cf2e2018-08-19 10:38:36 -0700810 kvmclock_init();
Wanpeng Lid63bae02018-07-23 14:39:51 +0800811 x86_platform.apic_post_init = kvm_apic_init;
812}
813
Tom Lendacky99419b22020-09-07 15:16:04 +0200814#if defined(CONFIG_AMD_MEM_ENCRYPT)
815static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
816{
817 /* RAX and CPL are already in the GHCB */
818 ghcb_set_rbx(ghcb, regs->bx);
819 ghcb_set_rcx(ghcb, regs->cx);
820 ghcb_set_rdx(ghcb, regs->dx);
821 ghcb_set_rsi(ghcb, regs->si);
822}
823
824static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
825{
826 /* No checking of the return state needed */
827 return true;
828}
829#endif
830
Juergen Gross03b2a322017-11-09 14:27:36 +0100831const __initconst struct hypervisor_x86 x86_hyper_kvm = {
Tom Lendacky99419b22020-09-07 15:16:04 +0200832 .name = "KVM",
833 .detect = kvm_detect,
834 .type = X86_HYPER_KVM,
835 .init.guest_late_init = kvm_guest_init,
836 .init.x2apic_available = kvm_para_available,
David Woodhouse2e008ff2020-10-24 22:35:35 +0100837 .init.msi_ext_dest_id = kvm_msi_ext_dest_id,
Tom Lendacky99419b22020-09-07 15:16:04 +0200838 .init.init_platform = kvm_init_platform,
839#if defined(CONFIG_AMD_MEM_ENCRYPT)
840 .runtime.sev_es_hcall_prepare = kvm_sev_es_hcall_prepare,
841 .runtime.sev_es_hcall_finish = kvm_sev_es_hcall_finish,
842#endif
Prarit Bhargavafc733732012-07-06 13:47:39 -0400843};
Prarit Bhargavafc733732012-07-06 13:47:39 -0400844
Glauber Costad910f5c2011-07-11 15:28:19 -0400845static __init int activate_jump_labels(void)
846{
847 if (has_steal_clock) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100848 static_key_slow_inc(&paravirt_steal_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400849 if (steal_acc)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100850 static_key_slow_inc(&paravirt_steal_rq_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400851 }
852
853 return 0;
854}
855arch_initcall(activate_jump_labels);
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530856
857#ifdef CONFIG_PARAVIRT_SPINLOCKS
858
859/* Kick a cpu by its apicid. Used to wake up a halted vcpu */
Raghavendra K T36bd6212013-08-16 15:08:41 +0530860static void kvm_kick_cpu(int cpu)
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530861{
862 int apicid;
863 unsigned long flags = 0;
864
865 apicid = per_cpu(x86_cpu_to_apicid, cpu);
866 kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
867}
868
Waiman Longbf0c7c32015-04-24 14:56:39 -0400869#include <asm/qspinlock.h>
870
871static void kvm_wait(u8 *ptr, u8 val)
872{
Waiman Longbf0c7c32015-04-24 14:56:39 -0400873 if (in_nmi())
874 return;
875
Waiman Longbf0c7c32015-04-24 14:56:39 -0400876 /*
877 * halt until it's our turn and kicked. Note that we do safe halt
878 * for irq enabled case to avoid hang when lock info is overwritten
879 * in irq spinlock slowpath and no spurious interrupt occur to save us.
880 */
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800881 if (irqs_disabled()) {
882 if (READ_ONCE(*ptr) == val)
883 halt();
884 } else {
885 local_irq_disable();
Waiman Longbf0c7c32015-04-24 14:56:39 -0400886
Lai Jiangshana40b2fd2021-08-14 11:51:29 +0800887 /* safe_halt() will enable IRQ */
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800888 if (READ_ONCE(*ptr) == val)
889 safe_halt();
Lai Jiangshana40b2fd2021-08-14 11:51:29 +0800890 else
891 local_irq_enable();
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800892 }
Waiman Longbf0c7c32015-04-24 14:56:39 -0400893}
894
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500895#ifdef CONFIG_X86_32
Waiman Long6c629852017-02-20 13:36:03 -0500896__visible bool __kvm_vcpu_is_preempted(long cpu)
Peter Zijlstra3cded412016-11-15 16:47:06 +0100897{
898 struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
899
Wanpeng Lifa55eed2017-12-12 17:33:01 -0800900 return !!(src->preempted & KVM_VCPU_PREEMPTED);
Peter Zijlstra3cded412016-11-15 16:47:06 +0100901}
902PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
903
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500904#else
905
906#include <asm/asm-offsets.h>
907
908extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
909
910/*
911 * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
912 * restoring to/from the stack.
913 */
914asm(
915".pushsection .text;"
916".global __raw_callee_save___kvm_vcpu_is_preempted;"
917".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
918"__raw_callee_save___kvm_vcpu_is_preempted:"
919"movq __per_cpu_offset(,%rdi,8), %rax;"
920"cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
921"setne %al;"
922"ret;"
Josh Poimboeuf083db672019-07-17 20:36:36 -0500923".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;"
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500924".popsection");
925
926#endif
927
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530928/*
929 * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
930 */
931void __init kvm_spinlock_init(void)
932{
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800933 /*
934 * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
935 * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
936 * preferred over native qspinlock when vCPU is preempted.
937 */
938 if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
939 pr_info("PV spinlocks disabled, no host support\n");
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530940 return;
Zhenzhong Duande585022019-10-23 19:16:20 +0800941 }
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530942
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800943 /*
944 * Disable PV spinlocks and use native qspinlock when dedicated pCPUs
945 * are available.
946 */
947 if (kvm_para_has_hint(KVM_HINTS_REALTIME)) {
948 pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n");
949 goto out;
950 }
Wanpeng Lib2798ba2018-02-13 09:05:41 +0800951
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800952 if (num_possible_cpus() == 1) {
953 pr_info("PV spinlocks disabled, single CPU\n");
954 goto out;
955 }
956
957 if (nopvspin) {
958 pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n");
959 goto out;
960 }
961
962 pr_info("PV spinlocks enabled\n");
Waiman Long3553ae52018-07-17 17:59:27 -0400963
Waiman Longbf0c7c32015-04-24 14:56:39 -0400964 __pv_init_lock_hash();
Juergen Gross5c835112018-08-28 09:40:19 +0200965 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
966 pv_ops.lock.queued_spin_unlock =
967 PV_CALLEE_SAVE(__pv_queued_spin_unlock);
968 pv_ops.lock.wait = kvm_wait;
969 pv_ops.lock.kick = kvm_kick_cpu;
Peter Zijlstra3cded412016-11-15 16:47:06 +0100970
971 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
Juergen Gross5c835112018-08-28 09:40:19 +0200972 pv_ops.lock.vcpu_is_preempted =
Peter Zijlstra3cded412016-11-15 16:47:06 +0100973 PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
974 }
Zhenzhong Duan05eee612019-10-23 19:16:22 +0800975 /*
976 * When PV spinlock is enabled which is preferred over
977 * virt_spin_lock(), virt_spin_lock_key's value is meaningless.
978 * Just disable it anyway.
979 */
980out:
981 static_branch_disable(&virt_spin_lock_key);
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530982}
Raghavendra K T3dbef3e2013-10-09 14:33:21 +0530983
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530984#endif /* CONFIG_PARAVIRT_SPINLOCKS */
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300985
986#ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
987
988static void kvm_disable_host_haltpoll(void *i)
989{
990 wrmsrl(MSR_KVM_POLL_CONTROL, 0);
991}
992
993static void kvm_enable_host_haltpoll(void *i)
994{
995 wrmsrl(MSR_KVM_POLL_CONTROL, 1);
996}
997
Joao Martins97d3eb92019-09-02 11:40:31 +0100998void arch_haltpoll_enable(unsigned int cpu)
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300999{
1000 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
Zhenzhong Duan5aefd782019-10-23 19:16:21 +08001001 pr_err_once("host does not support poll control\n");
1002 pr_err_once("host upgrade recommended\n");
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001003 return;
1004 }
1005
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001006 /* Enable guest halt poll disables host halt poll */
Joao Martins97d3eb92019-09-02 11:40:31 +01001007 smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001008}
1009EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
1010
Joao Martins97d3eb92019-09-02 11:40:31 +01001011void arch_haltpoll_disable(unsigned int cpu)
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001012{
1013 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
1014 return;
1015
Li Qiangb785a442020-09-24 08:58:00 -07001016 /* Disable guest halt poll enables host halt poll */
Joao Martins97d3eb92019-09-02 11:40:31 +01001017 smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001018}
1019EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
1020#endif