Thomas Gleixner | f6ce7f2 | 2019-05-19 15:51:49 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 2 | /* |
| 3 | * KVM paravirt_ops implementation |
| 4 | * |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 5 | * 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 Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 10 | #define pr_fmt(fmt) "kvm-guest: " fmt |
| 11 | |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 12 | #include <linux/context_tracking.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 13 | #include <linux/init.h> |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 14 | #include <linux/irq.h> |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/kvm_para.h> |
| 17 | #include <linux/cpu.h> |
| 18 | #include <linux/mm.h> |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 19 | #include <linux/highmem.h> |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 20 | #include <linux/hardirq.h> |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 21 | #include <linux/notifier.h> |
| 22 | #include <linux/reboot.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 23 | #include <linux/hash.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/kprobes.h> |
Ulrich Obergfell | 9919e39 | 2014-10-13 15:55:37 -0700 | [diff] [blame] | 27 | #include <linux/nmi.h> |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 28 | #include <linux/swait.h> |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 29 | #include <linux/syscore_ops.h> |
Ashish Kalra | f449561 | 2021-08-24 11:07:07 +0000 | [diff] [blame^] | 30 | #include <linux/efi.h> |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 31 | #include <asm/timer.h> |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 32 | #include <asm/cpu.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 33 | #include <asm/traps.h> |
| 34 | #include <asm/desc.h> |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 35 | #include <asm/tlbflush.h> |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 36 | #include <asm/apic.h> |
| 37 | #include <asm/apicdef.h> |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 38 | #include <asm/hypervisor.h> |
Peter Zijlstra | 48a8b97 | 2018-08-22 17:30:16 +0200 | [diff] [blame] | 39 | #include <asm/tlb.h> |
Yi Wang | 19308a4 | 2019-10-10 14:37:25 +0800 | [diff] [blame] | 40 | #include <asm/cpuidle_haltpoll.h> |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 41 | #include <asm/ptrace.h> |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 42 | #include <asm/reboot.h> |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 43 | #include <asm/svm.h> |
Ashish Kalra | f449561 | 2021-08-24 11:07:07 +0000 | [diff] [blame^] | 44 | #include <asm/e820/api.h> |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 45 | |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 46 | DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled); |
| 47 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 48 | static int kvmapf = 1; |
| 49 | |
Dou Liyang | afdc3f58 | 2018-01-17 11:46:54 +0800 | [diff] [blame] | 50 | static int __init parse_no_kvmapf(char *arg) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 51 | { |
| 52 | kvmapf = 0; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | early_param("no-kvmapf", parse_no_kvmapf); |
| 57 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 58 | static int steal_acc = 1; |
Dou Liyang | afdc3f58 | 2018-01-17 11:46:54 +0800 | [diff] [blame] | 59 | static int __init parse_no_stealacc(char *arg) |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 60 | { |
| 61 | steal_acc = 0; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | early_param("no-steal-acc", parse_no_stealacc); |
| 66 | |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 67 | static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64); |
Andi Kleen | 14e581c | 2019-03-29 17:47:42 -0700 | [diff] [blame] | 68 | DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible; |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 69 | static int has_steal_clock = 0; |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 70 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 71 | /* |
| 72 | * No need for any "IO delay" on KVM |
| 73 | */ |
| 74 | static void kvm_io_delay(void) |
| 75 | { |
| 76 | } |
| 77 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 78 | #define KVM_TASK_SLEEP_HASHBITS 8 |
| 79 | #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS) |
| 80 | |
| 81 | struct kvm_task_sleep_node { |
| 82 | struct hlist_node link; |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 83 | struct swait_queue_head wq; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 84 | u32 token; |
| 85 | int cpu; |
| 86 | }; |
| 87 | |
| 88 | static struct kvm_task_sleep_head { |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 89 | raw_spinlock_t lock; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 90 | struct hlist_head list; |
| 91 | } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE]; |
| 92 | |
| 93 | static 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 Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 108 | static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 109 | { |
| 110 | u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS); |
| 111 | struct kvm_task_sleep_head *b = &async_pf_sleepers[key]; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 112 | struct kvm_task_sleep_node *e; |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame] | 113 | |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 114 | raw_spin_lock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 115 | 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 Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 119 | raw_spin_unlock(&b->lock); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 120 | kfree(e); |
| 121 | return false; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 124 | n->token = token; |
| 125 | n->cpu = smp_processor_id(); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 126 | init_swait_queue_head(&n->wq); |
| 127 | hlist_add_head(&n->link, &b->list); |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 128 | raw_spin_unlock(&b->lock); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 129 | 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 | */ |
| 139 | void 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 Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 146 | if (!kvm_async_pf_queue_task(token, &n)) |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 147 | return; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 148 | |
| 149 | for (;;) { |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 150 | prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 151 | if (hlist_unhashed(&n.link)) |
| 152 | break; |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 153 | |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 154 | local_irq_enable(); |
| 155 | schedule(); |
| 156 | local_irq_disable(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 157 | } |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 158 | finish_swait(&n.wq, &wait); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 159 | } |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 160 | EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule); |
| 161 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 162 | static void apf_task_wake_one(struct kvm_task_sleep_node *n) |
| 163 | { |
| 164 | hlist_del_init(&n->link); |
Thomas Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 165 | if (swq_has_sleeper(&n->wq)) |
Peter Zijlstra | b3dae10 | 2018-06-12 10:34:52 +0200 | [diff] [blame] | 166 | swake_up_one(&n->wq); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void apf_task_wake_all(void) |
| 170 | { |
| 171 | int i; |
| 172 | |
| 173 | for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 174 | struct kvm_task_sleep_head *b = &async_pf_sleepers[i]; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 175 | struct kvm_task_sleep_node *n; |
| 176 | struct hlist_node *p, *next; |
| 177 | |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 178 | raw_spin_lock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 179 | hlist_for_each_safe(p, next, &b->list) { |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 180 | n = hlist_entry(p, typeof(*n), link); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 181 | if (n->cpu == smp_processor_id()) |
| 182 | apf_task_wake_one(n); |
| 183 | } |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 184 | raw_spin_unlock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | void 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 | |
| 199 | again: |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 200 | raw_spin_lock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 201 | 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 Natapov | 62c49cc | 2012-05-02 15:04:02 +0300 | [diff] [blame] | 207 | n = kzalloc(sizeof(*n), GFP_ATOMIC); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 208 | if (!n) { |
| 209 | /* |
| 210 | * Allocation failed! Busy wait while other cpu |
| 211 | * handles async PF. |
| 212 | */ |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 213 | raw_spin_unlock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 214 | cpu_relax(); |
| 215 | goto again; |
| 216 | } |
| 217 | n->token = token; |
| 218 | n->cpu = smp_processor_id(); |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 219 | init_swait_queue_head(&n->wq); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 220 | hlist_add_head(&n->link, &b->list); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 221 | } else { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 222 | apf_task_wake_one(n); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 223 | } |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 224 | raw_spin_unlock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake); |
| 228 | |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 229 | noinstr u32 kvm_read_and_reset_apf_flags(void) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 230 | { |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 231 | u32 flags = 0; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 232 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 233 | if (__this_cpu_read(apf_reason.enabled)) { |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 234 | flags = __this_cpu_read(apf_reason.flags); |
| 235 | __this_cpu_write(apf_reason.flags, 0); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 238 | return flags; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 239 | } |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 241 | |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 242 | noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 243 | { |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 244 | u32 flags = kvm_read_and_reset_apf_flags(); |
Thomas Gleixner | a27a0a5 | 2020-07-23 00:00:08 +0200 | [diff] [blame] | 245 | irqentry_state_t state; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 246 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 247 | if (!flags) |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 248 | return false; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 249 | |
Thomas Gleixner | a27a0a5 | 2020-07-23 00:00:08 +0200 | [diff] [blame] | 250 | state = irqentry_enter(regs); |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 251 | instrumentation_begin(); |
| 252 | |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 253 | /* |
| 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 Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 261 | if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) { |
Thomas Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 262 | 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 Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 266 | } else { |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 267 | WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 268 | } |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 269 | |
| 270 | instrumentation_end(); |
Thomas Gleixner | a27a0a5 | 2020-07-23 00:00:08 +0200 | [diff] [blame] | 271 | irqentry_exit(regs, state); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 272 | return true; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 275 | DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt) |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 276 | { |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 277 | struct pt_regs *old_regs = set_irq_regs(regs); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 278 | u32 token; |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 279 | |
Vitaly Kuznetsov | cc17b22 | 2020-09-08 15:53:50 +0200 | [diff] [blame] | 280 | ack_APIC_irq(); |
| 281 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 282 | 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 Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 286 | kvm_async_pf_task_wake(token); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 287 | __this_cpu_write(apf_reason.token, 0); |
| 288 | wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1); |
| 289 | } |
| 290 | |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 291 | set_irq_regs(old_regs); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Rakib Mullick | d3ac881 | 2009-07-02 11:40:36 +0600 | [diff] [blame] | 294 | static void __init paravirt_ops_setup(void) |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 295 | { |
| 296 | pv_info.name = "KVM"; |
Andy Lutomirski | 29fa682 | 2014-12-05 19:03:28 -0800 | [diff] [blame] | 297 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 298 | if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY)) |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 299 | pv_ops.cpu.io_delay = kvm_io_delay; |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 300 | |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 301 | #ifdef CONFIG_X86_IO_APIC |
| 302 | no_timer_check = 1; |
| 303 | #endif |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 304 | } |
| 305 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 306 | static 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 Hansen | 5dfd486 | 2013-01-22 13:24:35 -0800 | [diff] [blame] | 314 | wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED)); |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 315 | pr_info("stealtime: cpu %d, msr %llx\n", cpu, |
| 316 | (unsigned long long) slow_virt_to_phys(st)); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 319 | static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED; |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 320 | |
Wanpeng Li | 8ca2255 | 2016-11-07 11:13:40 +0800 | [diff] [blame] | 321 | static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val) |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 322 | { |
| 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 Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 330 | if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi))) |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 331 | return; |
Wanpeng Li | 8ca2255 | 2016-11-07 11:13:40 +0800 | [diff] [blame] | 332 | apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 333 | } |
| 334 | |
Nicholas Krause | ed3cf15 | 2015-05-20 00:24:10 -0400 | [diff] [blame] | 335 | static void kvm_guest_cpu_init(void) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 336 | { |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 337 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) { |
| 338 | u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason)); |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 339 | |
| 340 | WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled)); |
| 341 | |
| 342 | pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason)); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 343 | pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 344 | |
Radim Krčmář | fe2a302 | 2018-02-01 22:16:21 +0100 | [diff] [blame] | 345 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT)) |
| 346 | pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
| 347 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 348 | wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR); |
| 349 | |
Radim Krčmář | fe2a302 | 2018-02-01 22:16:21 +0100 | [diff] [blame] | 350 | wrmsrl(MSR_KVM_ASYNC_PF_EN, pa); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 351 | __this_cpu_write(apf_reason.enabled, 1); |
Vitaly Kuznetsov | 0a269a0 | 2021-04-14 14:35:40 +0200 | [diff] [blame] | 352 | pr_info("setup async PF for cpu %d\n", smp_processor_id()); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 353 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 354 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 355 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) { |
| 356 | unsigned long pa; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 357 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 358 | /* Size alignment is implied but just to make it explicit. */ |
| 359 | BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 360 | __this_cpu_write(kvm_apic_eoi, 0); |
| 361 | pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi)) |
Dave Hansen | 5dfd486 | 2013-01-22 13:24:35 -0800 | [diff] [blame] | 362 | | KVM_MSR_ENABLED; |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 363 | wrmsrl(MSR_KVM_PV_EOI_EN, pa); |
| 364 | } |
| 365 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 366 | if (has_steal_clock) |
| 367 | kvm_register_steal_time(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 370 | static void kvm_pv_disable_apf(void) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 371 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 372 | if (!__this_cpu_read(apf_reason.enabled)) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 373 | return; |
| 374 | |
| 375 | wrmsrl(MSR_KVM_ASYNC_PF_EN, 0); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 376 | __this_cpu_write(apf_reason.enabled, 0); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 377 | |
Vitaly Kuznetsov | 0a269a0 | 2021-04-14 14:35:40 +0200 | [diff] [blame] | 378 | pr_info("disable async PF for cpu %d\n", smp_processor_id()); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 381 | static 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 Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 389 | static 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 Li | 5a48a62 | 2017-04-11 02:49:21 -0700 | [diff] [blame] | 398 | virt_rmb(); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 399 | steal = src->steal; |
Wanpeng Li | 5a48a62 | 2017-04-11 02:49:21 -0700 | [diff] [blame] | 400 | virt_rmb(); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 401 | } while ((version & 1) || (version != src->version)); |
| 402 | |
| 403 | return steal; |
| 404 | } |
| 405 | |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 406 | static 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 | */ |
| 419 | static 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 Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 433 | static void kvm_guest_cpu_offline(bool shutdown) |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 434 | { |
| 435 | kvm_disable_steal_time(); |
| 436 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 437 | wrmsrl(MSR_KVM_PV_EOI_EN, 0); |
Ashish Kalra | f449561 | 2021-08-24 11:07:07 +0000 | [diff] [blame^] | 438 | if (kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) |
| 439 | wrmsrl(MSR_KVM_MIGRATION_CONTROL, 0); |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 440 | kvm_pv_disable_apf(); |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 441 | if (!shutdown) |
| 442 | apf_task_wake_all(); |
Vitaly Kuznetsov | c02027b | 2021-04-14 14:35:42 +0200 | [diff] [blame] | 443 | kvmclock_disable(); |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static 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 Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 456 | #ifdef CONFIG_SMP |
| 457 | |
| 458 | static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask); |
| 459 | |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 460 | static 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 Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 467 | static bool pv_ipi_supported(void) |
| 468 | { |
| 469 | return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI); |
| 470 | } |
| 471 | |
| 472 | static 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 Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 479 | #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG) |
| 480 | |
| 481 | static 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 Christopherson | de81c2f | 2019-01-23 09:22:40 -0800 | [diff] [blame] | 491 | long ret; |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 492 | |
| 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 Christopherson | de81c2f | 2019-01-23 09:22:40 -0800 | [diff] [blame] | 517 | ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap, |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 518 | (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr); |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 519 | WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld", |
| 520 | ret); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 521 | 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 Christopherson | de81c2f | 2019-01-23 09:22:40 -0800 | [diff] [blame] | 528 | ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap, |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 529 | (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr); |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 530 | WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld", |
| 531 | ret); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | local_irq_restore(flags); |
| 535 | } |
| 536 | |
| 537 | static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) |
| 538 | { |
| 539 | __send_ipi_mask(mask, vector); |
| 540 | } |
| 541 | |
| 542 | static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector) |
| 543 | { |
| 544 | unsigned int this_cpu = smp_processor_id(); |
Wanpeng Li | 8a9442f | 2020-02-18 09:08:24 +0800 | [diff] [blame] | 545 | struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 546 | const struct cpumask *local_mask; |
| 547 | |
Wanpeng Li | 8a9442f | 2020-02-18 09:08:24 +0800 | [diff] [blame] | 548 | cpumask_copy(new_mask, mask); |
| 549 | cpumask_clear_cpu(this_cpu, new_mask); |
| 550 | local_mask = new_mask; |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 551 | __send_ipi_mask(local_mask, vector); |
| 552 | } |
| 553 | |
Ashish Kalra | f449561 | 2021-08-24 11:07:07 +0000 | [diff] [blame^] | 554 | static 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 | |
| 601 | late_initcall(setup_efi_kvm_sev_migration); |
| 602 | |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 603 | /* |
| 604 | * Set the IPI entry points |
| 605 | */ |
| 606 | static 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 Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 610 | pr_info("setup PV IPIs\n"); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 611 | } |
| 612 | |
Wanpeng Li | f85f6e7 | 2019-06-11 20:23:48 +0800 | [diff] [blame] | 613 | static 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 Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 628 | static void kvm_flush_tlb_multi(const struct cpumask *cpumask, |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 629 | 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 Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 642 | /* |
| 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 Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 647 | 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 Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 656 | native_flush_tlb_multi(flushmask, info); |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static __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 | } |
| 674 | arch_initcall(kvm_alloc_cpumask); |
| 675 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 676 | static void __init kvm_smp_prepare_boot_cpu(void) |
| 677 | { |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 678 | /* |
| 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 Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 684 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 685 | native_smp_prepare_boot_cpu(); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 686 | kvm_spinlock_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 687 | } |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 688 | |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 689 | static int kvm_cpu_down_prepare(unsigned int cpu) |
| 690 | { |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 691 | unsigned long flags; |
| 692 | |
| 693 | local_irq_save(flags); |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 694 | kvm_guest_cpu_offline(false); |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 695 | local_irq_restore(flags); |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 696 | return 0; |
| 697 | } |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 698 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 699 | #endif |
| 700 | |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 701 | static int kvm_suspend(void) |
| 702 | { |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 703 | kvm_guest_cpu_offline(false); |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static void kvm_resume(void) |
| 709 | { |
| 710 | kvm_cpu_online(raw_smp_processor_id()); |
| 711 | } |
| 712 | |
| 713 | static struct syscore_ops kvm_syscore_ops = { |
| 714 | .suspend = kvm_suspend, |
| 715 | .resume = kvm_resume, |
| 716 | }; |
| 717 | |
Vitaly Kuznetsov | 384fc67 | 2021-04-14 14:35:44 +0200 | [diff] [blame] | 718 | static void kvm_pv_guest_cpu_reboot(void *unused) |
| 719 | { |
| 720 | kvm_guest_cpu_offline(true); |
| 721 | } |
| 722 | |
| 723 | static 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 | |
| 731 | static struct notifier_block kvm_pv_reboot_nb = { |
| 732 | .notifier_call = kvm_pv_reboot_notify, |
| 733 | }; |
| 734 | |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 735 | /* |
| 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 |
| 742 | static 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 Gross | f361464 | 2017-11-09 14:27:38 +0100 | [diff] [blame] | 749 | static void __init kvm_guest_init(void) |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 750 | { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 751 | int i; |
| 752 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 753 | paravirt_ops_setup(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 754 | register_reboot_notifier(&kvm_pv_reboot_nb); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 755 | for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 756 | raw_spin_lock_init(&async_pf_sleepers[i].lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 757 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 758 | if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { |
| 759 | has_steal_clock = 1; |
Juergen Gross | a0e2bf7 | 2021-03-11 15:23:09 +0100 | [diff] [blame] | 760 | static_call_update(pv_steal_clock, kvm_steal_clock); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Michael S. Tsirkin | 9053666 | 2012-07-15 15:56:52 +0300 | [diff] [blame] | 763 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 764 | apic_set_eoi_write(kvm_guest_apic_eoi_write); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 765 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 766 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) { |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 767 | static_branch_enable(&kvm_async_pf_enabled); |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 768 | alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 769 | } |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 770 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 771 | #ifdef CONFIG_SMP |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 772 | if (pv_tlb_flush_supported()) { |
Linus Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 773 | pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi; |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 774 | pv_ops.mmu.tlb_remove_table = tlb_remove_table; |
| 775 | pr_info("KVM setup pv remote TLB flush\n"); |
| 776 | } |
| 777 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 778 | smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 779 | if (pv_sched_yield_supported()) { |
Wanpeng Li | f85f6e7 | 2019-06-11 20:23:48 +0800 | [diff] [blame] | 780 | smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi; |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 781 | pr_info("setup PV sched yield\n"); |
Wanpeng Li | f85f6e7 | 2019-06-11 20:23:48 +0800 | [diff] [blame] | 782 | } |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 783 | if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online", |
| 784 | kvm_cpu_online, kvm_cpu_down_prepare) < 0) |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 785 | pr_err("failed to install cpu hotplug callbacks\n"); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 786 | #else |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 787 | sev_map_percpu_data(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 788 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 789 | #endif |
Ulrich Obergfell | 9919e39 | 2014-10-13 15:55:37 -0700 | [diff] [blame] | 790 | |
Vitaly Kuznetsov | 3d6b841 | 2021-04-14 14:35:43 +0200 | [diff] [blame] | 791 | #ifdef CONFIG_KEXEC_CORE |
| 792 | machine_ops.crash_shutdown = kvm_crash_shutdown; |
| 793 | #endif |
| 794 | |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame] | 795 | register_syscore_ops(&kvm_syscore_ops); |
| 796 | |
Ulrich Obergfell | 9919e39 | 2014-10-13 15:55:37 -0700 | [diff] [blame] | 797 | /* |
| 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 Obergfell | 692297d | 2015-04-14 15:44:19 -0700 | [diff] [blame] | 802 | hardlockup_detector_disable(); |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 803 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 804 | |
Paolo Bonzini | 1c300a4 | 2014-01-27 14:49:40 +0100 | [diff] [blame] | 805 | static 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 Petkov | 0c9f3536 | 2016-03-29 17:41:55 +0200 | [diff] [blame] | 810 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) |
Paolo Bonzini | 1c300a4 | 2014-01-27 14:49:40 +0100 | [diff] [blame] | 811 | return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0); |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static 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 | |
| 826 | bool kvm_para_available(void) |
| 827 | { |
| 828 | return kvm_cpuid_base() != 0; |
| 829 | } |
| 830 | EXPORT_SYMBOL_GPL(kvm_para_available); |
| 831 | |
Paolo Bonzini | 77f01bd | 2014-01-27 14:51:44 +0100 | [diff] [blame] | 832 | unsigned int kvm_arch_para_features(void) |
| 833 | { |
| 834 | return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES); |
| 835 | } |
| 836 | |
Wanpeng Li | a4429e5 | 2018-02-13 09:05:40 +0800 | [diff] [blame] | 837 | unsigned int kvm_arch_para_hints(void) |
| 838 | { |
| 839 | return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES); |
| 840 | } |
Wanpeng Li | 1328edc | 2019-08-29 16:49:57 +0800 | [diff] [blame] | 841 | EXPORT_SYMBOL_GPL(kvm_arch_para_hints); |
Wanpeng Li | a4429e5 | 2018-02-13 09:05:40 +0800 | [diff] [blame] | 842 | |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 843 | static uint32_t __init kvm_detect(void) |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 844 | { |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 845 | return kvm_cpuid_base(); |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 846 | } |
| 847 | |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 848 | static void __init kvm_apic_init(void) |
| 849 | { |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 850 | #ifdef CONFIG_SMP |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 851 | if (pv_ipi_supported()) |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 852 | kvm_setup_pv_ipi(); |
| 853 | #endif |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 854 | } |
| 855 | |
David Woodhouse | 2e008ff | 2020-10-24 22:35:35 +0100 | [diff] [blame] | 856 | static bool __init kvm_msi_ext_dest_id(void) |
| 857 | { |
| 858 | return kvm_para_has_feature(KVM_FEATURE_MSI_EXT_DEST_ID); |
| 859 | } |
| 860 | |
Ashish Kalra | f449561 | 2021-08-24 11:07:07 +0000 | [diff] [blame^] | 861 | static 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 Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 867 | static void __init kvm_init_platform(void) |
| 868 | { |
Ashish Kalra | f449561 | 2021-08-24 11:07:07 +0000 | [diff] [blame^] | 869 | 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 Torvalds | e61cf2e | 2018-08-19 10:38:36 -0700 | [diff] [blame] | 892 | kvmclock_init(); |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 893 | x86_platform.apic_post_init = kvm_apic_init; |
| 894 | } |
| 895 | |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 896 | #if defined(CONFIG_AMD_MEM_ENCRYPT) |
| 897 | static 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 | |
| 906 | static 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 Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 913 | const __initconst struct hypervisor_x86 x86_hyper_kvm = { |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 914 | .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 Woodhouse | 2e008ff | 2020-10-24 22:35:35 +0100 | [diff] [blame] | 919 | .init.msi_ext_dest_id = kvm_msi_ext_dest_id, |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 920 | .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 Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 925 | }; |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 926 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 927 | static __init int activate_jump_labels(void) |
| 928 | { |
| 929 | if (has_steal_clock) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 930 | static_key_slow_inc(¶virt_steal_enabled); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 931 | if (steal_acc) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 932 | static_key_slow_inc(¶virt_steal_rq_enabled); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | arch_initcall(activate_jump_labels); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 938 | |
| 939 | #ifdef CONFIG_PARAVIRT_SPINLOCKS |
| 940 | |
| 941 | /* Kick a cpu by its apicid. Used to wake up a halted vcpu */ |
Raghavendra K T | 36bd621 | 2013-08-16 15:08:41 +0530 | [diff] [blame] | 942 | static void kvm_kick_cpu(int cpu) |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 943 | { |
| 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 Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 951 | #include <asm/qspinlock.h> |
| 952 | |
| 953 | static void kvm_wait(u8 *ptr, u8 val) |
| 954 | { |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 955 | if (in_nmi()) |
| 956 | return; |
| 957 | |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 958 | /* |
| 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 Li | f4e61f0 | 2021-03-15 14:55:28 +0800 | [diff] [blame] | 963 | if (irqs_disabled()) { |
| 964 | if (READ_ONCE(*ptr) == val) |
| 965 | halt(); |
| 966 | } else { |
| 967 | local_irq_disable(); |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 968 | |
Lai Jiangshan | a40b2fd | 2021-08-14 11:51:29 +0800 | [diff] [blame] | 969 | /* safe_halt() will enable IRQ */ |
Wanpeng Li | f4e61f0 | 2021-03-15 14:55:28 +0800 | [diff] [blame] | 970 | if (READ_ONCE(*ptr) == val) |
| 971 | safe_halt(); |
Lai Jiangshan | a40b2fd | 2021-08-14 11:51:29 +0800 | [diff] [blame] | 972 | else |
| 973 | local_irq_enable(); |
Wanpeng Li | f4e61f0 | 2021-03-15 14:55:28 +0800 | [diff] [blame] | 974 | } |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 975 | } |
| 976 | |
Waiman Long | dd0fd8b | 2017-02-20 13:36:04 -0500 | [diff] [blame] | 977 | #ifdef CONFIG_X86_32 |
Waiman Long | 6c62985 | 2017-02-20 13:36:03 -0500 | [diff] [blame] | 978 | __visible bool __kvm_vcpu_is_preempted(long cpu) |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 979 | { |
| 980 | struct kvm_steal_time *src = &per_cpu(steal_time, cpu); |
| 981 | |
Wanpeng Li | fa55eed | 2017-12-12 17:33:01 -0800 | [diff] [blame] | 982 | return !!(src->preempted & KVM_VCPU_PREEMPTED); |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 983 | } |
| 984 | PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted); |
| 985 | |
Waiman Long | dd0fd8b | 2017-02-20 13:36:04 -0500 | [diff] [blame] | 986 | #else |
| 987 | |
| 988 | #include <asm/asm-offsets.h> |
| 989 | |
| 990 | extern 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 | */ |
| 996 | asm( |
| 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 Poimboeuf | 083db67 | 2019-07-17 20:36:36 -0500 | [diff] [blame] | 1005 | ".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;" |
Waiman Long | dd0fd8b | 2017-02-20 13:36:04 -0500 | [diff] [blame] | 1006 | ".popsection"); |
| 1007 | |
| 1008 | #endif |
| 1009 | |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 1010 | /* |
| 1011 | * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present. |
| 1012 | */ |
| 1013 | void __init kvm_spinlock_init(void) |
| 1014 | { |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 1015 | /* |
| 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 Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 1022 | return; |
Zhenzhong Duan | de58502 | 2019-10-23 19:16:20 +0800 | [diff] [blame] | 1023 | } |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 1024 | |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 1025 | /* |
| 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 Li | b2798ba | 2018-02-13 09:05:41 +0800 | [diff] [blame] | 1033 | |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 1034 | 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 Long | 3553ae5 | 2018-07-17 17:59:27 -0400 | [diff] [blame] | 1045 | |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 1046 | __pv_init_lock_hash(); |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 1047 | 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 Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 1052 | |
| 1053 | if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 1054 | pv_ops.lock.vcpu_is_preempted = |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 1055 | PV_CALLEE_SAVE(__kvm_vcpu_is_preempted); |
| 1056 | } |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 1057 | /* |
| 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 | */ |
| 1062 | out: |
| 1063 | static_branch_disable(&virt_spin_lock_key); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 1064 | } |
Raghavendra K T | 3dbef3e | 2013-10-09 14:33:21 +0530 | [diff] [blame] | 1065 | |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 1066 | #endif /* CONFIG_PARAVIRT_SPINLOCKS */ |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1067 | |
| 1068 | #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL |
| 1069 | |
| 1070 | static void kvm_disable_host_haltpoll(void *i) |
| 1071 | { |
| 1072 | wrmsrl(MSR_KVM_POLL_CONTROL, 0); |
| 1073 | } |
| 1074 | |
| 1075 | static void kvm_enable_host_haltpoll(void *i) |
| 1076 | { |
| 1077 | wrmsrl(MSR_KVM_POLL_CONTROL, 1); |
| 1078 | } |
| 1079 | |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 1080 | void arch_haltpoll_enable(unsigned int cpu) |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1081 | { |
| 1082 | if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) { |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 1083 | pr_err_once("host does not support poll control\n"); |
| 1084 | pr_err_once("host upgrade recommended\n"); |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1085 | return; |
| 1086 | } |
| 1087 | |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1088 | /* Enable guest halt poll disables host halt poll */ |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 1089 | smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1); |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(arch_haltpoll_enable); |
| 1092 | |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 1093 | void arch_haltpoll_disable(unsigned int cpu) |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1094 | { |
| 1095 | if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) |
| 1096 | return; |
| 1097 | |
Li Qiang | b785a44 | 2020-09-24 08:58:00 -0700 | [diff] [blame] | 1098 | /* Disable guest halt poll enables host halt poll */ |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 1099 | smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1); |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1100 | } |
| 1101 | EXPORT_SYMBOL_GPL(arch_haltpoll_disable); |
| 1102 | #endif |