blob: a672a2ef65b34ac8fd9c374a783a2c512a0b0271 [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>
Ashish Kalraf4495612021-08-24 11:07:07 +000030#include <linux/efi.h>
Marcelo Tosattia90ede72009-02-11 22:45:42 -020031#include <asm/timer.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020032#include <asm/cpu.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020033#include <asm/traps.h>
34#include <asm/desc.h>
Gleb Natapov6c047cd2010-10-14 11:22:54 +020035#include <asm/tlbflush.h>
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030036#include <asm/apic.h>
37#include <asm/apicdef.h>
Prarit Bhargavafc733732012-07-06 13:47:39 -040038#include <asm/hypervisor.h>
Peter Zijlstra48a8b972018-08-22 17:30:16 +020039#include <asm/tlb.h>
Yi Wang19308a42019-10-10 14:37:25 +080040#include <asm/cpuidle_haltpoll.h>
Tom Lendacky99419b22020-09-07 15:16:04 +020041#include <asm/ptrace.h>
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +020042#include <asm/reboot.h>
Tom Lendacky99419b22020-09-07 15:16:04 +020043#include <asm/svm.h>
Ashish Kalraf4495612021-08-24 11:07:07 +000044#include <asm/e820/api.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050045
Andy Lutomirskief680172020-02-28 10:42:48 -080046DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
47
Gleb Natapovfd10cde2010-10-14 11:22:51 +020048static int kvmapf = 1;
49
Dou Liyangafdc3f582018-01-17 11:46:54 +080050static int __init parse_no_kvmapf(char *arg)
Gleb Natapovfd10cde2010-10-14 11:22:51 +020051{
52 kvmapf = 0;
53 return 0;
54}
55
56early_param("no-kvmapf", parse_no_kvmapf);
57
Glauber Costad910f5c2011-07-11 15:28:19 -040058static int steal_acc = 1;
Dou Liyangafdc3f582018-01-17 11:46:54 +080059static int __init parse_no_stealacc(char *arg)
Glauber Costad910f5c2011-07-11 15:28:19 -040060{
61 steal_acc = 0;
62 return 0;
63}
64
65early_param("no-steal-acc", parse_no_stealacc);
66
Brijesh Singh47162762017-10-20 09:30:58 -050067static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
Andi Kleen14e581c2019-03-29 17:47:42 -070068DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible;
Glauber Costad910f5c2011-07-11 15:28:19 -040069static int has_steal_clock = 0;
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050070
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -050071/*
72 * No need for any "IO delay" on KVM
73 */
74static void kvm_io_delay(void)
75{
76}
77
Gleb Natapov631bc482010-10-14 11:22:52 +020078#define KVM_TASK_SLEEP_HASHBITS 8
79#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
80
81struct kvm_task_sleep_node {
82 struct hlist_node link;
Rik van Riel9db284f2016-03-21 15:13:27 +010083 struct swait_queue_head wq;
Gleb Natapov631bc482010-10-14 11:22:52 +020084 u32 token;
85 int cpu;
86};
87
88static struct kvm_task_sleep_head {
Rik van Riel9db284f2016-03-21 15:13:27 +010089 raw_spinlock_t lock;
Gleb Natapov631bc482010-10-14 11:22:52 +020090 struct hlist_head list;
91} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
92
93static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
94 u32 token)
95{
96 struct hlist_node *p;
97
98 hlist_for_each(p, &b->list) {
99 struct kvm_task_sleep_node *n =
100 hlist_entry(p, typeof(*n), link);
101 if (n->token == token)
102 return n;
103 }
104
105 return NULL;
106}
107
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200108static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)
Gleb Natapov631bc482010-10-14 11:22:52 +0200109{
110 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
111 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100112 struct kvm_task_sleep_node *e;
Li Zhong9b132fb2012-12-04 10:35:13 +0800113
Rik van Riel9db284f2016-03-21 15:13:27 +0100114 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200115 e = _find_apf_task(b, token);
116 if (e) {
117 /* dummy entry exist -> wake up was delivered ahead of PF */
118 hlist_del(&e->link);
Rik van Riel9db284f2016-03-21 15:13:27 +0100119 raw_spin_unlock(&b->lock);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100120 kfree(e);
121 return false;
Gleb Natapov631bc482010-10-14 11:22:52 +0200122 }
123
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100124 n->token = token;
125 n->cpu = smp_processor_id();
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100126 init_swait_queue_head(&n->wq);
127 hlist_add_head(&n->link, &b->list);
Rik van Riel9db284f2016-03-21 15:13:27 +0100128 raw_spin_unlock(&b->lock);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100129 return true;
130}
131
132/*
133 * kvm_async_pf_task_wait_schedule - Wait for pagefault to be handled
134 * @token: Token to identify the sleep node entry
135 *
136 * Invoked from the async pagefault handling code or from the VM exit page
137 * fault handler. In both cases RCU is watching.
138 */
139void kvm_async_pf_task_wait_schedule(u32 token)
140{
141 struct kvm_task_sleep_node n;
142 DECLARE_SWAITQUEUE(wait);
143
144 lockdep_assert_irqs_disabled();
145
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200146 if (!kvm_async_pf_queue_task(token, &n))
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100147 return;
Gleb Natapov631bc482010-10-14 11:22:52 +0200148
149 for (;;) {
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100150 prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
Gleb Natapov631bc482010-10-14 11:22:52 +0200151 if (hlist_unhashed(&n.link))
152 break;
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200153
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100154 local_irq_enable();
155 schedule();
156 local_irq_disable();
Gleb Natapov631bc482010-10-14 11:22:52 +0200157 }
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100158 finish_swait(&n.wq, &wait);
Gleb Natapov631bc482010-10-14 11:22:52 +0200159}
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100160EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule);
161
Gleb Natapov631bc482010-10-14 11:22:52 +0200162static void apf_task_wake_one(struct kvm_task_sleep_node *n)
163{
164 hlist_del_init(&n->link);
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200165 if (swq_has_sleeper(&n->wq))
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200166 swake_up_one(&n->wq);
Gleb Natapov631bc482010-10-14 11:22:52 +0200167}
168
169static void apf_task_wake_all(void)
170{
171 int i;
172
173 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
Gleb Natapov631bc482010-10-14 11:22:52 +0200174 struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100175 struct kvm_task_sleep_node *n;
176 struct hlist_node *p, *next;
177
Rik van Riel9db284f2016-03-21 15:13:27 +0100178 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200179 hlist_for_each_safe(p, next, &b->list) {
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100180 n = hlist_entry(p, typeof(*n), link);
Gleb Natapov631bc482010-10-14 11:22:52 +0200181 if (n->cpu == smp_processor_id())
182 apf_task_wake_one(n);
183 }
Rik van Riel9db284f2016-03-21 15:13:27 +0100184 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200185 }
186}
187
188void kvm_async_pf_task_wake(u32 token)
189{
190 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
191 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
192 struct kvm_task_sleep_node *n;
193
194 if (token == ~0) {
195 apf_task_wake_all();
196 return;
197 }
198
199again:
Rik van Riel9db284f2016-03-21 15:13:27 +0100200 raw_spin_lock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200201 n = _find_apf_task(b, token);
202 if (!n) {
203 /*
204 * async PF was not yet handled.
205 * Add dummy entry for the token.
206 */
Gleb Natapov62c49cc2012-05-02 15:04:02 +0300207 n = kzalloc(sizeof(*n), GFP_ATOMIC);
Gleb Natapov631bc482010-10-14 11:22:52 +0200208 if (!n) {
209 /*
210 * Allocation failed! Busy wait while other cpu
211 * handles async PF.
212 */
Rik van Riel9db284f2016-03-21 15:13:27 +0100213 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200214 cpu_relax();
215 goto again;
216 }
217 n->token = token;
218 n->cpu = smp_processor_id();
Rik van Riel9db284f2016-03-21 15:13:27 +0100219 init_swait_queue_head(&n->wq);
Gleb Natapov631bc482010-10-14 11:22:52 +0200220 hlist_add_head(&n->link, &b->list);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100221 } else {
Gleb Natapov631bc482010-10-14 11:22:52 +0200222 apf_task_wake_one(n);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100223 }
Rik van Riel9db284f2016-03-21 15:13:27 +0100224 raw_spin_unlock(&b->lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200225 return;
226}
227EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
228
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200229noinstr u32 kvm_read_and_reset_apf_flags(void)
Gleb Natapov631bc482010-10-14 11:22:52 +0200230{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200231 u32 flags = 0;
Gleb Natapov631bc482010-10-14 11:22:52 +0200232
Christoph Lameter89cbc762014-08-17 12:30:40 -0500233 if (__this_cpu_read(apf_reason.enabled)) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200234 flags = __this_cpu_read(apf_reason.flags);
235 __this_cpu_write(apf_reason.flags, 0);
Gleb Natapov631bc482010-10-14 11:22:52 +0200236 }
237
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200238 return flags;
Gleb Natapov631bc482010-10-14 11:22:52 +0200239}
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200240EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags);
Gleb Natapov631bc482010-10-14 11:22:52 +0200241
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200242noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token)
Gleb Natapov631bc482010-10-14 11:22:52 +0200243{
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200244 u32 flags = kvm_read_and_reset_apf_flags();
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200245 irqentry_state_t state;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100246
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200247 if (!flags)
Andy Lutomirskief680172020-02-28 10:42:48 -0800248 return false;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100249
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200250 state = irqentry_enter(regs);
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200251 instrumentation_begin();
252
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100253 /*
254 * If the host managed to inject an async #PF into an interrupt
255 * disabled region, then die hard as this is not going to end well
256 * and the host side is seriously broken.
257 */
258 if (unlikely(!(regs->flags & X86_EFLAGS_IF)))
259 panic("Host injected async #PF in interrupt disabled region\n");
260
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200261 if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
Thomas Gleixner3a7c8fa2020-04-24 09:57:56 +0200262 if (unlikely(!(user_mode(regs))))
263 panic("Host injected async #PF in kernel mode\n");
264 /* Page is swapped out by the host. */
265 kvm_async_pf_task_wait_schedule(token);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100266 } else {
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400267 WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags);
Gleb Natapov631bc482010-10-14 11:22:52 +0200268 }
Thomas Gleixner91eeafe2020-05-21 22:05:28 +0200269
270 instrumentation_end();
Thomas Gleixnera27a0a52020-07-23 00:00:08 +0200271 irqentry_exit(regs, state);
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100272 return true;
Gleb Natapov631bc482010-10-14 11:22:52 +0200273}
274
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400275DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt)
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200276{
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400277 struct pt_regs *old_regs = set_irq_regs(regs);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200278 u32 token;
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200279
Vitaly Kuznetsovcc17b222020-09-08 15:53:50 +0200280 ack_APIC_irq();
281
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200282 inc_irq_stat(irq_hv_callback_count);
283
284 if (__this_cpu_read(apf_reason.enabled)) {
285 token = __this_cpu_read(apf_reason.token);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200286 kvm_async_pf_task_wake(token);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200287 __this_cpu_write(apf_reason.token, 0);
288 wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1);
289 }
290
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400291 set_irq_regs(old_regs);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200292}
293
Rakib Mullickd3ac8812009-07-02 11:40:36 +0600294static void __init paravirt_ops_setup(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500295{
296 pv_info.name = "KVM";
Andy Lutomirski29fa6822014-12-05 19:03:28 -0800297
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500298 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
Juergen Gross5c835112018-08-28 09:40:19 +0200299 pv_ops.cpu.io_delay = kvm_io_delay;
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500300
Marcelo Tosattia90ede72009-02-11 22:45:42 -0200301#ifdef CONFIG_X86_IO_APIC
302 no_timer_check = 1;
303#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500304}
305
Glauber Costad910f5c2011-07-11 15:28:19 -0400306static void kvm_register_steal_time(void)
307{
308 int cpu = smp_processor_id();
309 struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
310
311 if (!has_steal_clock)
312 return;
313
Dave Hansen5dfd4862013-01-22 13:24:35 -0800314 wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800315 pr_info("stealtime: cpu %d, msr %llx\n", cpu,
316 (unsigned long long) slow_virt_to_phys(st));
Glauber Costad910f5c2011-07-11 15:28:19 -0400317}
318
Brijesh Singh47162762017-10-20 09:30:58 -0500319static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300320
Wanpeng Li8ca22552016-11-07 11:13:40 +0800321static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300322{
323 /**
324 * This relies on __test_and_clear_bit to modify the memory
325 * in a way that is atomic with respect to the local CPU.
326 * The hypervisor only accesses this memory from the local CPU so
327 * there's no need for lock or memory barriers.
328 * An optimization barrier is implied in apic write.
329 */
Christoph Lameter89cbc762014-08-17 12:30:40 -0500330 if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300331 return;
Wanpeng Li8ca22552016-11-07 11:13:40 +0800332 apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300333}
334
Nicholas Krauseed3cf152015-05-20 00:24:10 -0400335static void kvm_guest_cpu_init(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200336{
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200337 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
338 u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
Andy Lutomirskief680172020-02-28 10:42:48 -0800339
340 WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled));
341
342 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200343 pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
Wanpeng Li52a5c152017-07-13 18:30:42 -0700344
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100345 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
346 pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
347
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200348 wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR);
349
Radim Krčmářfe2a3022018-02-01 22:16:21 +0100350 wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500351 __this_cpu_write(apf_reason.enabled, 1);
Vitaly Kuznetsov0a269a02021-04-14 14:35:40 +0200352 pr_info("setup async PF for cpu %d\n", smp_processor_id());
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200353 }
Glauber Costad910f5c2011-07-11 15:28:19 -0400354
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300355 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
356 unsigned long pa;
Thomas Gleixner6bca69a2020-03-07 00:42:06 +0100357
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300358 /* Size alignment is implied but just to make it explicit. */
359 BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500360 __this_cpu_write(kvm_apic_eoi, 0);
361 pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
Dave Hansen5dfd4862013-01-22 13:24:35 -0800362 | KVM_MSR_ENABLED;
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300363 wrmsrl(MSR_KVM_PV_EOI_EN, pa);
364 }
365
Glauber Costad910f5c2011-07-11 15:28:19 -0400366 if (has_steal_clock)
367 kvm_register_steal_time();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200368}
369
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300370static void kvm_pv_disable_apf(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200371{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500372 if (!__this_cpu_read(apf_reason.enabled))
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200373 return;
374
375 wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500376 __this_cpu_write(apf_reason.enabled, 0);
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200377
Vitaly Kuznetsov0a269a02021-04-14 14:35:40 +0200378 pr_info("disable async PF for cpu %d\n", smp_processor_id());
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200379}
380
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200381static void kvm_disable_steal_time(void)
382{
383 if (!has_steal_clock)
384 return;
385
386 wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
387}
388
Glauber Costad910f5c2011-07-11 15:28:19 -0400389static u64 kvm_steal_clock(int cpu)
390{
391 u64 steal;
392 struct kvm_steal_time *src;
393 int version;
394
395 src = &per_cpu(steal_time, cpu);
396 do {
397 version = src->version;
Wanpeng Li5a48a622017-04-11 02:49:21 -0700398 virt_rmb();
Glauber Costad910f5c2011-07-11 15:28:19 -0400399 steal = src->steal;
Wanpeng Li5a48a622017-04-11 02:49:21 -0700400 virt_rmb();
Glauber Costad910f5c2011-07-11 15:28:19 -0400401 } while ((version & 1) || (version != src->version));
402
403 return steal;
404}
405
Brijesh Singh47162762017-10-20 09:30:58 -0500406static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
407{
408 early_set_memory_decrypted((unsigned long) ptr, size);
409}
410
411/*
412 * Iterate through all possible CPUs and map the memory region pointed
413 * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
414 *
415 * Note: we iterate through all possible CPUs to ensure that CPUs
416 * hotplugged will have their per-cpu variable already mapped as
417 * decrypted.
418 */
419static void __init sev_map_percpu_data(void)
420{
421 int cpu;
422
423 if (!sev_active())
424 return;
425
426 for_each_possible_cpu(cpu) {
427 __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
428 __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
429 __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
430 }
431}
432
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200433static void kvm_guest_cpu_offline(bool shutdown)
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200434{
435 kvm_disable_steal_time();
436 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
437 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
Ashish Kalraf4495612021-08-24 11:07:07 +0000438 if (kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL))
439 wrmsrl(MSR_KVM_MIGRATION_CONTROL, 0);
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200440 kvm_pv_disable_apf();
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200441 if (!shutdown)
442 apf_task_wake_all();
Vitaly Kuznetsovc02027b2021-04-14 14:35:42 +0200443 kvmclock_disable();
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200444}
445
446static int kvm_cpu_online(unsigned int cpu)
447{
448 unsigned long flags;
449
450 local_irq_save(flags);
451 kvm_guest_cpu_init();
452 local_irq_restore(flags);
453 return 0;
454}
455
Wanpeng Li2b519b572021-04-09 12:18:29 +0800456#ifdef CONFIG_SMP
457
458static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask);
459
Wanpeng Lia262bca2020-02-18 09:08:23 +0800460static bool pv_tlb_flush_supported(void)
461{
462 return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
463 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
464 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
465}
466
Wanpeng Lia262bca2020-02-18 09:08:23 +0800467static bool pv_ipi_supported(void)
468{
469 return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI);
470}
471
472static bool pv_sched_yield_supported(void)
473{
474 return (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) &&
475 !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
476 kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
477}
478
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800479#define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
480
481static void __send_ipi_mask(const struct cpumask *mask, int vector)
482{
483 unsigned long flags;
484 int cpu, apic_id, icr;
485 int min = 0, max = 0;
486#ifdef CONFIG_X86_64
487 __uint128_t ipi_bitmap = 0;
488#else
489 u64 ipi_bitmap = 0;
490#endif
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800491 long ret;
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800492
493 if (cpumask_empty(mask))
494 return;
495
496 local_irq_save(flags);
497
498 switch (vector) {
499 default:
500 icr = APIC_DM_FIXED | vector;
501 break;
502 case NMI_VECTOR:
503 icr = APIC_DM_NMI;
504 break;
505 }
506
507 for_each_cpu(cpu, mask) {
508 apic_id = per_cpu(x86_cpu_to_apicid, cpu);
509 if (!ipi_bitmap) {
510 min = max = apic_id;
511 } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
512 ipi_bitmap <<= min - apic_id;
513 min = apic_id;
514 } else if (apic_id < min + KVM_IPI_CLUSTER_SIZE) {
515 max = apic_id < max ? max : apic_id;
516 } else {
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800517 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800518 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800519 WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
520 ret);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800521 min = max = apic_id;
522 ipi_bitmap = 0;
523 }
524 __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
525 }
526
527 if (ipi_bitmap) {
Sean Christophersonde81c2f2019-01-23 09:22:40 -0800528 ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800529 (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800530 WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
531 ret);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800532 }
533
534 local_irq_restore(flags);
535}
536
537static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
538{
539 __send_ipi_mask(mask, vector);
540}
541
542static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
543{
544 unsigned int this_cpu = smp_processor_id();
Wanpeng Li8a9442f2020-02-18 09:08:24 +0800545 struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800546 const struct cpumask *local_mask;
547
Wanpeng Li8a9442f2020-02-18 09:08:24 +0800548 cpumask_copy(new_mask, mask);
549 cpumask_clear_cpu(this_cpu, new_mask);
550 local_mask = new_mask;
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800551 __send_ipi_mask(local_mask, vector);
552}
553
Ashish Kalraf4495612021-08-24 11:07:07 +0000554static int __init setup_efi_kvm_sev_migration(void)
555{
556 efi_char16_t efi_sev_live_migration_enabled[] = L"SevLiveMigrationEnabled";
557 efi_guid_t efi_variable_guid = AMD_SEV_MEM_ENCRYPT_GUID;
558 efi_status_t status;
559 unsigned long size;
560 bool enabled;
561
562 if (!sev_active() ||
563 !kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL))
564 return 0;
565
566 if (!efi_enabled(EFI_BOOT))
567 return 0;
568
569 if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
570 pr_info("%s : EFI runtime services are not enabled\n", __func__);
571 return 0;
572 }
573
574 size = sizeof(enabled);
575
576 /* Get variable contents into buffer */
577 status = efi.get_variable(efi_sev_live_migration_enabled,
578 &efi_variable_guid, NULL, &size, &enabled);
579
580 if (status == EFI_NOT_FOUND) {
581 pr_info("%s : EFI live migration variable not found\n", __func__);
582 return 0;
583 }
584
585 if (status != EFI_SUCCESS) {
586 pr_info("%s : EFI variable retrieval failed\n", __func__);
587 return 0;
588 }
589
590 if (enabled == 0) {
591 pr_info("%s: live migration disabled in EFI\n", __func__);
592 return 0;
593 }
594
595 pr_info("%s : live migration enabled in EFI\n", __func__);
596 wrmsrl(MSR_KVM_MIGRATION_CONTROL, KVM_MIGRATION_READY);
597
598 return 1;
599}
600
601late_initcall(setup_efi_kvm_sev_migration);
602
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800603/*
604 * Set the IPI entry points
605 */
606static void kvm_setup_pv_ipi(void)
607{
608 apic->send_IPI_mask = kvm_send_ipi_mask;
609 apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800610 pr_info("setup PV IPIs\n");
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800611}
612
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800613static void kvm_smp_send_call_func_ipi(const struct cpumask *mask)
614{
615 int cpu;
616
617 native_send_call_func_ipi(mask);
618
619 /* Make sure other vCPUs get a chance to run if they need to. */
620 for_each_cpu(cpu, mask) {
621 if (vcpu_is_preempted(cpu)) {
622 kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu));
623 break;
624 }
625 }
626}
627
Linus Torvalds152d32a2021-05-01 10:14:08 -0700628static void kvm_flush_tlb_multi(const struct cpumask *cpumask,
Wanpeng Li2b519b572021-04-09 12:18:29 +0800629 const struct flush_tlb_info *info)
630{
631 u8 state;
632 int cpu;
633 struct kvm_steal_time *src;
634 struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
635
636 cpumask_copy(flushmask, cpumask);
637 /*
638 * We have to call flush only on online vCPUs. And
639 * queue flush_on_enter for pre-empted vCPUs
640 */
641 for_each_cpu(cpu, flushmask) {
Linus Torvalds152d32a2021-05-01 10:14:08 -0700642 /*
643 * The local vCPU is never preempted, so we do not explicitly
644 * skip check for local vCPU - it will never be cleared from
645 * flushmask.
646 */
Wanpeng Li2b519b572021-04-09 12:18:29 +0800647 src = &per_cpu(steal_time, cpu);
648 state = READ_ONCE(src->preempted);
649 if ((state & KVM_VCPU_PREEMPTED)) {
650 if (try_cmpxchg(&src->preempted, &state,
651 state | KVM_VCPU_FLUSH_TLB))
652 __cpumask_clear_cpu(cpu, flushmask);
653 }
654 }
655
Linus Torvalds152d32a2021-05-01 10:14:08 -0700656 native_flush_tlb_multi(flushmask, info);
Wanpeng Li2b519b572021-04-09 12:18:29 +0800657}
658
659static __init int kvm_alloc_cpumask(void)
660{
661 int cpu;
662
663 if (!kvm_para_available() || nopv)
664 return 0;
665
666 if (pv_tlb_flush_supported() || pv_ipi_supported())
667 for_each_possible_cpu(cpu) {
668 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu),
669 GFP_KERNEL, cpu_to_node(cpu));
670 }
671
672 return 0;
673}
674arch_initcall(kvm_alloc_cpumask);
675
Gleb Natapovca3f1012010-10-14 11:22:49 +0200676static void __init kvm_smp_prepare_boot_cpu(void)
677{
Brijesh Singh47162762017-10-20 09:30:58 -0500678 /*
679 * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
680 * shares the guest physical address with the hypervisor.
681 */
682 sev_map_percpu_data();
683
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200684 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200685 native_smp_prepare_boot_cpu();
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530686 kvm_spinlock_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200687}
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200688
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200689static int kvm_cpu_down_prepare(unsigned int cpu)
690{
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200691 unsigned long flags;
692
693 local_irq_save(flags);
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200694 kvm_guest_cpu_offline(false);
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200695 local_irq_restore(flags);
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200696 return 0;
697}
Wanpeng Li2b519b572021-04-09 12:18:29 +0800698
Gleb Natapovca3f1012010-10-14 11:22:49 +0200699#endif
700
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200701static int kvm_suspend(void)
702{
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200703 kvm_guest_cpu_offline(false);
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200704
705 return 0;
706}
707
708static void kvm_resume(void)
709{
710 kvm_cpu_online(raw_smp_processor_id());
711}
712
713static struct syscore_ops kvm_syscore_ops = {
714 .suspend = kvm_suspend,
715 .resume = kvm_resume,
716};
717
Vitaly Kuznetsov384fc672021-04-14 14:35:44 +0200718static void kvm_pv_guest_cpu_reboot(void *unused)
719{
720 kvm_guest_cpu_offline(true);
721}
722
723static int kvm_pv_reboot_notify(struct notifier_block *nb,
724 unsigned long code, void *unused)
725{
726 if (code == SYS_RESTART)
727 on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
728 return NOTIFY_DONE;
729}
730
731static struct notifier_block kvm_pv_reboot_nb = {
732 .notifier_call = kvm_pv_reboot_notify,
733};
734
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200735/*
736 * After a PV feature is registered, the host will keep writing to the
737 * registered memory location. If the guest happens to shutdown, this memory
738 * won't be valid. In cases like kexec, in which you install a new kernel, this
739 * means a random memory location will be kept being written.
740 */
741#ifdef CONFIG_KEXEC_CORE
742static void kvm_crash_shutdown(struct pt_regs *regs)
743{
744 kvm_guest_cpu_offline(true);
745 native_machine_crash_shutdown(regs);
746}
747#endif
748
Juergen Grossf3614642017-11-09 14:27:38 +0100749static void __init kvm_guest_init(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500750{
Gleb Natapov631bc482010-10-14 11:22:52 +0200751 int i;
752
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500753 paravirt_ops_setup();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200754 register_reboot_notifier(&kvm_pv_reboot_nb);
Gleb Natapov631bc482010-10-14 11:22:52 +0200755 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
Rik van Riel9db284f2016-03-21 15:13:27 +0100756 raw_spin_lock_init(&async_pf_sleepers[i].lock);
Gleb Natapov631bc482010-10-14 11:22:52 +0200757
Glauber Costad910f5c2011-07-11 15:28:19 -0400758 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
759 has_steal_clock = 1;
Juergen Grossa0e2bf72021-03-11 15:23:09 +0100760 static_call_update(pv_steal_clock, kvm_steal_clock);
Glauber Costad910f5c2011-07-11 15:28:19 -0400761 }
762
Michael S. Tsirkin90536662012-07-15 15:56:52 +0300763 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
764 apic_set_eoi_write(kvm_guest_apic_eoi_write);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300765
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200766 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
Andy Lutomirskief680172020-02-28 10:42:48 -0800767 static_branch_enable(&kvm_async_pf_enabled);
Paolo Bonzini26d05b32020-06-15 07:53:05 -0400768 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt);
Vitaly Kuznetsovb1d40572020-05-25 16:41:23 +0200769 }
Andy Lutomirskief680172020-02-28 10:42:48 -0800770
Gleb Natapovca3f1012010-10-14 11:22:49 +0200771#ifdef CONFIG_SMP
Wanpeng Li2b519b572021-04-09 12:18:29 +0800772 if (pv_tlb_flush_supported()) {
Linus Torvalds152d32a2021-05-01 10:14:08 -0700773 pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi;
Wanpeng Li2b519b572021-04-09 12:18:29 +0800774 pv_ops.mmu.tlb_remove_table = tlb_remove_table;
775 pr_info("KVM setup pv remote TLB flush\n");
776 }
777
Gleb Natapovca3f1012010-10-14 11:22:49 +0200778 smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
Wanpeng Lia262bca2020-02-18 09:08:23 +0800779 if (pv_sched_yield_supported()) {
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800780 smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi;
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800781 pr_info("setup PV sched yield\n");
Wanpeng Lif85f6e72019-06-11 20:23:48 +0800782 }
Sebastian Andrzej Siewior9a20ea42016-08-18 14:57:29 +0200783 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
784 kvm_cpu_online, kvm_cpu_down_prepare) < 0)
Zhenzhong Duan5aefd782019-10-23 19:16:21 +0800785 pr_err("failed to install cpu hotplug callbacks\n");
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200786#else
Brijesh Singh47162762017-10-20 09:30:58 -0500787 sev_map_percpu_data();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200788 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200789#endif
Ulrich Obergfell9919e392014-10-13 15:55:37 -0700790
Vitaly Kuznetsov3d6b8412021-04-14 14:35:43 +0200791#ifdef CONFIG_KEXEC_CORE
792 machine_ops.crash_shutdown = kvm_crash_shutdown;
793#endif
794
Vitaly Kuznetsov8b79fef2021-04-14 14:35:41 +0200795 register_syscore_ops(&kvm_syscore_ops);
796
Ulrich Obergfell9919e392014-10-13 15:55:37 -0700797 /*
798 * Hard lockup detection is enabled by default. Disable it, as guests
799 * can get false positives too easily, for example if the host is
800 * overcommitted.
801 */
Ulrich Obergfell692297d2015-04-14 15:44:19 -0700802 hardlockup_detector_disable();
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500803}
Glauber Costad910f5c2011-07-11 15:28:19 -0400804
Paolo Bonzini1c300a42014-01-27 14:49:40 +0100805static noinline uint32_t __kvm_cpuid_base(void)
806{
807 if (boot_cpu_data.cpuid_level < 0)
808 return 0; /* So we don't blow up on old processors */
809
Borislav Petkov0c9f35362016-03-29 17:41:55 +0200810 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
Paolo Bonzini1c300a42014-01-27 14:49:40 +0100811 return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
812
813 return 0;
814}
815
816static inline uint32_t kvm_cpuid_base(void)
817{
818 static int kvm_cpuid_base = -1;
819
820 if (kvm_cpuid_base == -1)
821 kvm_cpuid_base = __kvm_cpuid_base();
822
823 return kvm_cpuid_base;
824}
825
826bool kvm_para_available(void)
827{
828 return kvm_cpuid_base() != 0;
829}
830EXPORT_SYMBOL_GPL(kvm_para_available);
831
Paolo Bonzini77f01bd2014-01-27 14:51:44 +0100832unsigned int kvm_arch_para_features(void)
833{
834 return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
835}
836
Wanpeng Lia4429e52018-02-13 09:05:40 +0800837unsigned int kvm_arch_para_hints(void)
838{
839 return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
840}
Wanpeng Li1328edc2019-08-29 16:49:57 +0800841EXPORT_SYMBOL_GPL(kvm_arch_para_hints);
Wanpeng Lia4429e52018-02-13 09:05:40 +0800842
Jason Wang9df56f12013-07-25 16:54:35 +0800843static uint32_t __init kvm_detect(void)
Prarit Bhargavafc733732012-07-06 13:47:39 -0400844{
Jason Wang9df56f12013-07-25 16:54:35 +0800845 return kvm_cpuid_base();
Prarit Bhargavafc733732012-07-06 13:47:39 -0400846}
847
Wanpeng Lid63bae02018-07-23 14:39:51 +0800848static void __init kvm_apic_init(void)
849{
Wanpeng Li2b519b572021-04-09 12:18:29 +0800850#ifdef CONFIG_SMP
Wanpeng Lia262bca2020-02-18 09:08:23 +0800851 if (pv_ipi_supported())
Wanpeng Liaaffcfd2018-07-23 14:39:52 +0800852 kvm_setup_pv_ipi();
853#endif
Wanpeng Lid63bae02018-07-23 14:39:51 +0800854}
855
David Woodhouse2e008ff2020-10-24 22:35:35 +0100856static bool __init kvm_msi_ext_dest_id(void)
857{
858 return kvm_para_has_feature(KVM_FEATURE_MSI_EXT_DEST_ID);
859}
860
Ashish Kalraf4495612021-08-24 11:07:07 +0000861static void kvm_sev_hc_page_enc_status(unsigned long pfn, int npages, bool enc)
862{
863 kvm_sev_hypercall3(KVM_HC_MAP_GPA_RANGE, pfn << PAGE_SHIFT, npages,
864 KVM_MAP_GPA_RANGE_ENC_STAT(enc) | KVM_MAP_GPA_RANGE_PAGE_SZ_4K);
865}
866
Wanpeng Lid63bae02018-07-23 14:39:51 +0800867static void __init kvm_init_platform(void)
868{
Ashish Kalraf4495612021-08-24 11:07:07 +0000869 if (sev_active() &&
870 kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) {
871 unsigned long nr_pages;
872
873 pv_ops.mmu.notify_page_enc_status_changed =
874 kvm_sev_hc_page_enc_status;
875
876 /*
877 * Ensure that _bss_decrypted section is marked as decrypted in the
878 * shared pages list.
879 */
880 nr_pages = DIV_ROUND_UP(__end_bss_decrypted - __start_bss_decrypted,
881 PAGE_SIZE);
882 early_set_mem_enc_dec_hypercall((unsigned long)__start_bss_decrypted,
883 nr_pages, 0);
884
885 /*
886 * If not booted using EFI, enable Live migration support.
887 */
888 if (!efi_enabled(EFI_BOOT))
889 wrmsrl(MSR_KVM_MIGRATION_CONTROL,
890 KVM_MIGRATION_READY);
891 }
Linus Torvaldse61cf2e2018-08-19 10:38:36 -0700892 kvmclock_init();
Wanpeng Lid63bae02018-07-23 14:39:51 +0800893 x86_platform.apic_post_init = kvm_apic_init;
894}
895
Tom Lendacky99419b22020-09-07 15:16:04 +0200896#if defined(CONFIG_AMD_MEM_ENCRYPT)
897static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
898{
899 /* RAX and CPL are already in the GHCB */
900 ghcb_set_rbx(ghcb, regs->bx);
901 ghcb_set_rcx(ghcb, regs->cx);
902 ghcb_set_rdx(ghcb, regs->dx);
903 ghcb_set_rsi(ghcb, regs->si);
904}
905
906static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
907{
908 /* No checking of the return state needed */
909 return true;
910}
911#endif
912
Juergen Gross03b2a322017-11-09 14:27:36 +0100913const __initconst struct hypervisor_x86 x86_hyper_kvm = {
Tom Lendacky99419b22020-09-07 15:16:04 +0200914 .name = "KVM",
915 .detect = kvm_detect,
916 .type = X86_HYPER_KVM,
917 .init.guest_late_init = kvm_guest_init,
918 .init.x2apic_available = kvm_para_available,
David Woodhouse2e008ff2020-10-24 22:35:35 +0100919 .init.msi_ext_dest_id = kvm_msi_ext_dest_id,
Tom Lendacky99419b22020-09-07 15:16:04 +0200920 .init.init_platform = kvm_init_platform,
921#if defined(CONFIG_AMD_MEM_ENCRYPT)
922 .runtime.sev_es_hcall_prepare = kvm_sev_es_hcall_prepare,
923 .runtime.sev_es_hcall_finish = kvm_sev_es_hcall_finish,
924#endif
Prarit Bhargavafc733732012-07-06 13:47:39 -0400925};
Prarit Bhargavafc733732012-07-06 13:47:39 -0400926
Glauber Costad910f5c2011-07-11 15:28:19 -0400927static __init int activate_jump_labels(void)
928{
929 if (has_steal_clock) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100930 static_key_slow_inc(&paravirt_steal_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400931 if (steal_acc)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100932 static_key_slow_inc(&paravirt_steal_rq_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400933 }
934
935 return 0;
936}
937arch_initcall(activate_jump_labels);
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530938
939#ifdef CONFIG_PARAVIRT_SPINLOCKS
940
941/* Kick a cpu by its apicid. Used to wake up a halted vcpu */
Raghavendra K T36bd6212013-08-16 15:08:41 +0530942static void kvm_kick_cpu(int cpu)
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +0530943{
944 int apicid;
945 unsigned long flags = 0;
946
947 apicid = per_cpu(x86_cpu_to_apicid, cpu);
948 kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
949}
950
Waiman Longbf0c7c32015-04-24 14:56:39 -0400951#include <asm/qspinlock.h>
952
953static void kvm_wait(u8 *ptr, u8 val)
954{
Waiman Longbf0c7c32015-04-24 14:56:39 -0400955 if (in_nmi())
956 return;
957
Waiman Longbf0c7c32015-04-24 14:56:39 -0400958 /*
959 * halt until it's our turn and kicked. Note that we do safe halt
960 * for irq enabled case to avoid hang when lock info is overwritten
961 * in irq spinlock slowpath and no spurious interrupt occur to save us.
962 */
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800963 if (irqs_disabled()) {
964 if (READ_ONCE(*ptr) == val)
965 halt();
966 } else {
967 local_irq_disable();
Waiman Longbf0c7c32015-04-24 14:56:39 -0400968
Lai Jiangshana40b2fd2021-08-14 11:51:29 +0800969 /* safe_halt() will enable IRQ */
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800970 if (READ_ONCE(*ptr) == val)
971 safe_halt();
Lai Jiangshana40b2fd2021-08-14 11:51:29 +0800972 else
973 local_irq_enable();
Wanpeng Lif4e61f02021-03-15 14:55:28 +0800974 }
Waiman Longbf0c7c32015-04-24 14:56:39 -0400975}
976
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500977#ifdef CONFIG_X86_32
Waiman Long6c629852017-02-20 13:36:03 -0500978__visible bool __kvm_vcpu_is_preempted(long cpu)
Peter Zijlstra3cded412016-11-15 16:47:06 +0100979{
980 struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
981
Wanpeng Lifa55eed2017-12-12 17:33:01 -0800982 return !!(src->preempted & KVM_VCPU_PREEMPTED);
Peter Zijlstra3cded412016-11-15 16:47:06 +0100983}
984PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
985
Waiman Longdd0fd8b2017-02-20 13:36:04 -0500986#else
987
988#include <asm/asm-offsets.h>
989
990extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
991
992/*
993 * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
994 * restoring to/from the stack.
995 */
996asm(
997".pushsection .text;"
998".global __raw_callee_save___kvm_vcpu_is_preempted;"
999".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
1000"__raw_callee_save___kvm_vcpu_is_preempted:"
1001"movq __per_cpu_offset(,%rdi,8), %rax;"
1002"cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
1003"setne %al;"
1004"ret;"
Josh Poimboeuf083db672019-07-17 20:36:36 -05001005".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;"
Waiman Longdd0fd8b2017-02-20 13:36:04 -05001006".popsection");
1007
1008#endif
1009
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +05301010/*
1011 * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
1012 */
1013void __init kvm_spinlock_init(void)
1014{
Zhenzhong Duan05eee612019-10-23 19:16:22 +08001015 /*
1016 * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
1017 * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
1018 * preferred over native qspinlock when vCPU is preempted.
1019 */
1020 if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
1021 pr_info("PV spinlocks disabled, no host support\n");
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +05301022 return;
Zhenzhong Duande585022019-10-23 19:16:20 +08001023 }
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +05301024
Zhenzhong Duan05eee612019-10-23 19:16:22 +08001025 /*
1026 * Disable PV spinlocks and use native qspinlock when dedicated pCPUs
1027 * are available.
1028 */
1029 if (kvm_para_has_hint(KVM_HINTS_REALTIME)) {
1030 pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n");
1031 goto out;
1032 }
Wanpeng Lib2798ba2018-02-13 09:05:41 +08001033
Zhenzhong Duan05eee612019-10-23 19:16:22 +08001034 if (num_possible_cpus() == 1) {
1035 pr_info("PV spinlocks disabled, single CPU\n");
1036 goto out;
1037 }
1038
1039 if (nopvspin) {
1040 pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n");
1041 goto out;
1042 }
1043
1044 pr_info("PV spinlocks enabled\n");
Waiman Long3553ae52018-07-17 17:59:27 -04001045
Waiman Longbf0c7c32015-04-24 14:56:39 -04001046 __pv_init_lock_hash();
Juergen Gross5c835112018-08-28 09:40:19 +02001047 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
1048 pv_ops.lock.queued_spin_unlock =
1049 PV_CALLEE_SAVE(__pv_queued_spin_unlock);
1050 pv_ops.lock.wait = kvm_wait;
1051 pv_ops.lock.kick = kvm_kick_cpu;
Peter Zijlstra3cded412016-11-15 16:47:06 +01001052
1053 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
Juergen Gross5c835112018-08-28 09:40:19 +02001054 pv_ops.lock.vcpu_is_preempted =
Peter Zijlstra3cded412016-11-15 16:47:06 +01001055 PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
1056 }
Zhenzhong Duan05eee612019-10-23 19:16:22 +08001057 /*
1058 * When PV spinlock is enabled which is preferred over
1059 * virt_spin_lock(), virt_spin_lock_key's value is meaningless.
1060 * Just disable it anyway.
1061 */
1062out:
1063 static_branch_disable(&virt_spin_lock_key);
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +05301064}
Raghavendra K T3dbef3e2013-10-09 14:33:21 +05301065
Srivatsa Vaddagiri92b75202013-08-06 14:55:41 +05301066#endif /* CONFIG_PARAVIRT_SPINLOCKS */
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001067
1068#ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
1069
1070static void kvm_disable_host_haltpoll(void *i)
1071{
1072 wrmsrl(MSR_KVM_POLL_CONTROL, 0);
1073}
1074
1075static void kvm_enable_host_haltpoll(void *i)
1076{
1077 wrmsrl(MSR_KVM_POLL_CONTROL, 1);
1078}
1079
Joao Martins97d3eb92019-09-02 11:40:31 +01001080void arch_haltpoll_enable(unsigned int cpu)
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001081{
1082 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
Zhenzhong Duan5aefd782019-10-23 19:16:21 +08001083 pr_err_once("host does not support poll control\n");
1084 pr_err_once("host upgrade recommended\n");
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001085 return;
1086 }
1087
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001088 /* Enable guest halt poll disables host halt poll */
Joao Martins97d3eb92019-09-02 11:40:31 +01001089 smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001090}
1091EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
1092
Joao Martins97d3eb92019-09-02 11:40:31 +01001093void arch_haltpoll_disable(unsigned int cpu)
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001094{
1095 if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
1096 return;
1097
Li Qiangb785a442020-09-24 08:58:00 -07001098 /* Disable guest halt poll enables host halt poll */
Joao Martins97d3eb92019-09-02 11:40:31 +01001099 smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
Marcelo Tosattia1c44232019-07-03 20:51:29 -03001100}
1101EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
1102#endif