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> |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 30 | #include <asm/timer.h> |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 31 | #include <asm/cpu.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 32 | #include <asm/traps.h> |
| 33 | #include <asm/desc.h> |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 34 | #include <asm/tlbflush.h> |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 35 | #include <asm/apic.h> |
| 36 | #include <asm/apicdef.h> |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 37 | #include <asm/hypervisor.h> |
Peter Zijlstra | 48a8b97 | 2018-08-22 17:30:16 +0200 | [diff] [blame] | 38 | #include <asm/tlb.h> |
Yi Wang | 19308a4 | 2019-10-10 14:37:25 +0800 | [diff] [blame] | 39 | #include <asm/cpuidle_haltpoll.h> |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 40 | #include <asm/ptrace.h> |
| 41 | #include <asm/svm.h> |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 42 | |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 43 | DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled); |
| 44 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 45 | static int kvmapf = 1; |
| 46 | |
Dou Liyang | afdc3f58 | 2018-01-17 11:46:54 +0800 | [diff] [blame] | 47 | static int __init parse_no_kvmapf(char *arg) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 48 | { |
| 49 | kvmapf = 0; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | early_param("no-kvmapf", parse_no_kvmapf); |
| 54 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 55 | static int steal_acc = 1; |
Dou Liyang | afdc3f58 | 2018-01-17 11:46:54 +0800 | [diff] [blame] | 56 | static int __init parse_no_stealacc(char *arg) |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 57 | { |
| 58 | steal_acc = 0; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | early_param("no-steal-acc", parse_no_stealacc); |
| 63 | |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 64 | 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] | 65 | 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] | 66 | static int has_steal_clock = 0; |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 67 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 68 | /* |
| 69 | * No need for any "IO delay" on KVM |
| 70 | */ |
| 71 | static void kvm_io_delay(void) |
| 72 | { |
| 73 | } |
| 74 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 75 | #define KVM_TASK_SLEEP_HASHBITS 8 |
| 76 | #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS) |
| 77 | |
| 78 | struct kvm_task_sleep_node { |
| 79 | struct hlist_node link; |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 80 | struct swait_queue_head wq; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 81 | u32 token; |
| 82 | int cpu; |
| 83 | }; |
| 84 | |
| 85 | static struct kvm_task_sleep_head { |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 86 | raw_spinlock_t lock; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 87 | struct hlist_head list; |
| 88 | } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE]; |
| 89 | |
| 90 | static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b, |
| 91 | u32 token) |
| 92 | { |
| 93 | struct hlist_node *p; |
| 94 | |
| 95 | hlist_for_each(p, &b->list) { |
| 96 | struct kvm_task_sleep_node *n = |
| 97 | hlist_entry(p, typeof(*n), link); |
| 98 | if (n->token == token) |
| 99 | return n; |
| 100 | } |
| 101 | |
| 102 | return NULL; |
| 103 | } |
| 104 | |
Thomas Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
| 107 | u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS); |
| 108 | struct kvm_task_sleep_head *b = &async_pf_sleepers[key]; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 109 | struct kvm_task_sleep_node *e; |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame] | 110 | |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 111 | raw_spin_lock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 112 | e = _find_apf_task(b, token); |
| 113 | if (e) { |
| 114 | /* dummy entry exist -> wake up was delivered ahead of PF */ |
| 115 | hlist_del(&e->link); |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 116 | raw_spin_unlock(&b->lock); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 117 | kfree(e); |
| 118 | return false; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 121 | n->token = token; |
| 122 | n->cpu = smp_processor_id(); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 123 | init_swait_queue_head(&n->wq); |
| 124 | hlist_add_head(&n->link, &b->list); |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 125 | raw_spin_unlock(&b->lock); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * kvm_async_pf_task_wait_schedule - Wait for pagefault to be handled |
| 131 | * @token: Token to identify the sleep node entry |
| 132 | * |
| 133 | * Invoked from the async pagefault handling code or from the VM exit page |
| 134 | * fault handler. In both cases RCU is watching. |
| 135 | */ |
| 136 | void kvm_async_pf_task_wait_schedule(u32 token) |
| 137 | { |
| 138 | struct kvm_task_sleep_node n; |
| 139 | DECLARE_SWAITQUEUE(wait); |
| 140 | |
| 141 | lockdep_assert_irqs_disabled(); |
| 142 | |
Thomas Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 143 | if (!kvm_async_pf_queue_task(token, &n)) |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 144 | return; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 145 | |
| 146 | for (;;) { |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 147 | prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 148 | if (hlist_unhashed(&n.link)) |
| 149 | break; |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 150 | |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 151 | local_irq_enable(); |
| 152 | schedule(); |
| 153 | local_irq_disable(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 154 | } |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 155 | finish_swait(&n.wq, &wait); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 156 | } |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 157 | EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule); |
| 158 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 159 | static void apf_task_wake_one(struct kvm_task_sleep_node *n) |
| 160 | { |
| 161 | hlist_del_init(&n->link); |
Thomas Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 162 | if (swq_has_sleeper(&n->wq)) |
Peter Zijlstra | b3dae10 | 2018-06-12 10:34:52 +0200 | [diff] [blame] | 163 | swake_up_one(&n->wq); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void apf_task_wake_all(void) |
| 167 | { |
| 168 | int i; |
| 169 | |
| 170 | for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 171 | struct kvm_task_sleep_head *b = &async_pf_sleepers[i]; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 172 | struct kvm_task_sleep_node *n; |
| 173 | struct hlist_node *p, *next; |
| 174 | |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 175 | raw_spin_lock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 176 | hlist_for_each_safe(p, next, &b->list) { |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 177 | n = hlist_entry(p, typeof(*n), link); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 178 | if (n->cpu == smp_processor_id()) |
| 179 | apf_task_wake_one(n); |
| 180 | } |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 181 | raw_spin_unlock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | void kvm_async_pf_task_wake(u32 token) |
| 186 | { |
| 187 | u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS); |
| 188 | struct kvm_task_sleep_head *b = &async_pf_sleepers[key]; |
| 189 | struct kvm_task_sleep_node *n; |
| 190 | |
| 191 | if (token == ~0) { |
| 192 | apf_task_wake_all(); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | again: |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 197 | raw_spin_lock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 198 | n = _find_apf_task(b, token); |
| 199 | if (!n) { |
| 200 | /* |
| 201 | * async PF was not yet handled. |
| 202 | * Add dummy entry for the token. |
| 203 | */ |
Gleb Natapov | 62c49cc | 2012-05-02 15:04:02 +0300 | [diff] [blame] | 204 | n = kzalloc(sizeof(*n), GFP_ATOMIC); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 205 | if (!n) { |
| 206 | /* |
| 207 | * Allocation failed! Busy wait while other cpu |
| 208 | * handles async PF. |
| 209 | */ |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 210 | raw_spin_unlock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 211 | cpu_relax(); |
| 212 | goto again; |
| 213 | } |
| 214 | n->token = token; |
| 215 | n->cpu = smp_processor_id(); |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 216 | init_swait_queue_head(&n->wq); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 217 | hlist_add_head(&n->link, &b->list); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 218 | } else { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 219 | apf_task_wake_one(n); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 220 | } |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 221 | raw_spin_unlock(&b->lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake); |
| 225 | |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 226 | noinstr u32 kvm_read_and_reset_apf_flags(void) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 227 | { |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 228 | u32 flags = 0; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 229 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 230 | if (__this_cpu_read(apf_reason.enabled)) { |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 231 | flags = __this_cpu_read(apf_reason.flags); |
| 232 | __this_cpu_write(apf_reason.flags, 0); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 235 | return flags; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 236 | } |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 238 | |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 239 | noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 240 | { |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 241 | u32 flags = kvm_read_and_reset_apf_flags(); |
Thomas Gleixner | a27a0a5 | 2020-07-23 00:00:08 +0200 | [diff] [blame] | 242 | irqentry_state_t state; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 243 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 244 | if (!flags) |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 245 | return false; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 246 | |
Thomas Gleixner | a27a0a5 | 2020-07-23 00:00:08 +0200 | [diff] [blame] | 247 | state = irqentry_enter(regs); |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 248 | instrumentation_begin(); |
| 249 | |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 250 | /* |
| 251 | * If the host managed to inject an async #PF into an interrupt |
| 252 | * disabled region, then die hard as this is not going to end well |
| 253 | * and the host side is seriously broken. |
| 254 | */ |
| 255 | if (unlikely(!(regs->flags & X86_EFLAGS_IF))) |
| 256 | panic("Host injected async #PF in interrupt disabled region\n"); |
| 257 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 258 | if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) { |
Thomas Gleixner | 3a7c8fa | 2020-04-24 09:57:56 +0200 | [diff] [blame] | 259 | if (unlikely(!(user_mode(regs)))) |
| 260 | panic("Host injected async #PF in kernel mode\n"); |
| 261 | /* Page is swapped out by the host. */ |
| 262 | kvm_async_pf_task_wait_schedule(token); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 263 | } else { |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 264 | WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 265 | } |
Thomas Gleixner | 91eeafe | 2020-05-21 22:05:28 +0200 | [diff] [blame] | 266 | |
| 267 | instrumentation_end(); |
Thomas Gleixner | a27a0a5 | 2020-07-23 00:00:08 +0200 | [diff] [blame] | 268 | irqentry_exit(regs, state); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 269 | return true; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 272 | DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt) |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 273 | { |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 274 | struct pt_regs *old_regs = set_irq_regs(regs); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 275 | u32 token; |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 276 | |
Vitaly Kuznetsov | cc17b22 | 2020-09-08 15:53:50 +0200 | [diff] [blame] | 277 | ack_APIC_irq(); |
| 278 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 279 | inc_irq_stat(irq_hv_callback_count); |
| 280 | |
| 281 | if (__this_cpu_read(apf_reason.enabled)) { |
| 282 | token = __this_cpu_read(apf_reason.token); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 283 | kvm_async_pf_task_wake(token); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 284 | __this_cpu_write(apf_reason.token, 0); |
| 285 | wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1); |
| 286 | } |
| 287 | |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 288 | set_irq_regs(old_regs); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Rakib Mullick | d3ac881 | 2009-07-02 11:40:36 +0600 | [diff] [blame] | 291 | static void __init paravirt_ops_setup(void) |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 292 | { |
| 293 | pv_info.name = "KVM"; |
Andy Lutomirski | 29fa682 | 2014-12-05 19:03:28 -0800 | [diff] [blame] | 294 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 295 | if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY)) |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 296 | pv_ops.cpu.io_delay = kvm_io_delay; |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 297 | |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 298 | #ifdef CONFIG_X86_IO_APIC |
| 299 | no_timer_check = 1; |
| 300 | #endif |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 303 | static void kvm_register_steal_time(void) |
| 304 | { |
| 305 | int cpu = smp_processor_id(); |
| 306 | struct kvm_steal_time *st = &per_cpu(steal_time, cpu); |
| 307 | |
| 308 | if (!has_steal_clock) |
| 309 | return; |
| 310 | |
Dave Hansen | 5dfd486 | 2013-01-22 13:24:35 -0800 | [diff] [blame] | 311 | 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] | 312 | pr_info("stealtime: cpu %d, msr %llx\n", cpu, |
| 313 | (unsigned long long) slow_virt_to_phys(st)); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 316 | 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] | 317 | |
Wanpeng Li | 8ca2255 | 2016-11-07 11:13:40 +0800 | [diff] [blame] | 318 | 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] | 319 | { |
| 320 | /** |
| 321 | * This relies on __test_and_clear_bit to modify the memory |
| 322 | * in a way that is atomic with respect to the local CPU. |
| 323 | * The hypervisor only accesses this memory from the local CPU so |
| 324 | * there's no need for lock or memory barriers. |
| 325 | * An optimization barrier is implied in apic write. |
| 326 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 327 | 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] | 328 | return; |
Wanpeng Li | 8ca2255 | 2016-11-07 11:13:40 +0800 | [diff] [blame] | 329 | apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 330 | } |
| 331 | |
Nicholas Krause | ed3cf15 | 2015-05-20 00:24:10 -0400 | [diff] [blame] | 332 | static void kvm_guest_cpu_init(void) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 333 | { |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 334 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) { |
| 335 | u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason)); |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 336 | |
| 337 | WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled)); |
| 338 | |
| 339 | pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason)); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 340 | pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 341 | |
Radim Krčmář | fe2a302 | 2018-02-01 22:16:21 +0100 | [diff] [blame] | 342 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT)) |
| 343 | pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
| 344 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 345 | wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR); |
| 346 | |
Radim Krčmář | fe2a302 | 2018-02-01 22:16:21 +0100 | [diff] [blame] | 347 | wrmsrl(MSR_KVM_ASYNC_PF_EN, pa); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 348 | __this_cpu_write(apf_reason.enabled, 1); |
Vitaly Kuznetsov | 0a269a0 | 2021-04-14 14:35:40 +0200 | [diff] [blame] | 349 | pr_info("setup async PF for cpu %d\n", smp_processor_id()); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 350 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 351 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 352 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) { |
| 353 | unsigned long pa; |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 354 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 355 | /* Size alignment is implied but just to make it explicit. */ |
| 356 | BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 357 | __this_cpu_write(kvm_apic_eoi, 0); |
| 358 | pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi)) |
Dave Hansen | 5dfd486 | 2013-01-22 13:24:35 -0800 | [diff] [blame] | 359 | | KVM_MSR_ENABLED; |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 360 | wrmsrl(MSR_KVM_PV_EOI_EN, pa); |
| 361 | } |
| 362 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 363 | if (has_steal_clock) |
| 364 | kvm_register_steal_time(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 367 | static void kvm_pv_disable_apf(void) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 368 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 369 | if (!__this_cpu_read(apf_reason.enabled)) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 370 | return; |
| 371 | |
| 372 | wrmsrl(MSR_KVM_ASYNC_PF_EN, 0); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 373 | __this_cpu_write(apf_reason.enabled, 0); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 374 | |
Vitaly Kuznetsov | 0a269a0 | 2021-04-14 14:35:40 +0200 | [diff] [blame] | 375 | pr_info("disable async PF for cpu %d\n", smp_processor_id()); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 378 | static void kvm_pv_guest_cpu_reboot(void *unused) |
| 379 | { |
| 380 | /* |
| 381 | * We disable PV EOI before we load a new kernel by kexec, |
| 382 | * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory. |
| 383 | * New kernel can re-enable when it boots. |
| 384 | */ |
| 385 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 386 | wrmsrl(MSR_KVM_PV_EOI_EN, 0); |
| 387 | kvm_pv_disable_apf(); |
Florian Westphal | 8fbe6a5 | 2012-08-15 16:00:40 +0200 | [diff] [blame] | 388 | kvm_disable_steal_time(); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 389 | } |
| 390 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 391 | static int kvm_pv_reboot_notify(struct notifier_block *nb, |
| 392 | unsigned long code, void *unused) |
| 393 | { |
| 394 | if (code == SYS_RESTART) |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 395 | on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 396 | return NOTIFY_DONE; |
| 397 | } |
| 398 | |
| 399 | static struct notifier_block kvm_pv_reboot_nb = { |
| 400 | .notifier_call = kvm_pv_reboot_notify, |
| 401 | }; |
| 402 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 403 | static u64 kvm_steal_clock(int cpu) |
| 404 | { |
| 405 | u64 steal; |
| 406 | struct kvm_steal_time *src; |
| 407 | int version; |
| 408 | |
| 409 | src = &per_cpu(steal_time, cpu); |
| 410 | do { |
| 411 | version = src->version; |
Wanpeng Li | 5a48a62 | 2017-04-11 02:49:21 -0700 | [diff] [blame] | 412 | virt_rmb(); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 413 | steal = src->steal; |
Wanpeng Li | 5a48a62 | 2017-04-11 02:49:21 -0700 | [diff] [blame] | 414 | virt_rmb(); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 415 | } while ((version & 1) || (version != src->version)); |
| 416 | |
| 417 | return steal; |
| 418 | } |
| 419 | |
| 420 | void kvm_disable_steal_time(void) |
| 421 | { |
| 422 | if (!has_steal_clock) |
| 423 | return; |
| 424 | |
| 425 | wrmsr(MSR_KVM_STEAL_TIME, 0, 0); |
| 426 | } |
| 427 | |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 428 | static inline void __set_percpu_decrypted(void *ptr, unsigned long size) |
| 429 | { |
| 430 | early_set_memory_decrypted((unsigned long) ptr, size); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Iterate through all possible CPUs and map the memory region pointed |
| 435 | * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once. |
| 436 | * |
| 437 | * Note: we iterate through all possible CPUs to ensure that CPUs |
| 438 | * hotplugged will have their per-cpu variable already mapped as |
| 439 | * decrypted. |
| 440 | */ |
| 441 | static void __init sev_map_percpu_data(void) |
| 442 | { |
| 443 | int cpu; |
| 444 | |
| 445 | if (!sev_active()) |
| 446 | return; |
| 447 | |
| 448 | for_each_possible_cpu(cpu) { |
| 449 | __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason)); |
| 450 | __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time)); |
| 451 | __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi)); |
| 452 | } |
| 453 | } |
| 454 | |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame^] | 455 | static void kvm_guest_cpu_offline(void) |
| 456 | { |
| 457 | kvm_disable_steal_time(); |
| 458 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 459 | wrmsrl(MSR_KVM_PV_EOI_EN, 0); |
| 460 | kvm_pv_disable_apf(); |
| 461 | apf_task_wake_all(); |
| 462 | } |
| 463 | |
| 464 | static int kvm_cpu_online(unsigned int cpu) |
| 465 | { |
| 466 | unsigned long flags; |
| 467 | |
| 468 | local_irq_save(flags); |
| 469 | kvm_guest_cpu_init(); |
| 470 | local_irq_restore(flags); |
| 471 | return 0; |
| 472 | } |
| 473 | |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 474 | #ifdef CONFIG_SMP |
| 475 | |
| 476 | static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask); |
| 477 | |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 478 | static bool pv_tlb_flush_supported(void) |
| 479 | { |
| 480 | return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && |
| 481 | !kvm_para_has_hint(KVM_HINTS_REALTIME) && |
| 482 | kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)); |
| 483 | } |
| 484 | |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 485 | static bool pv_ipi_supported(void) |
| 486 | { |
| 487 | return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI); |
| 488 | } |
| 489 | |
| 490 | static bool pv_sched_yield_supported(void) |
| 491 | { |
| 492 | return (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) && |
| 493 | !kvm_para_has_hint(KVM_HINTS_REALTIME) && |
| 494 | kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)); |
| 495 | } |
| 496 | |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 497 | #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG) |
| 498 | |
| 499 | static void __send_ipi_mask(const struct cpumask *mask, int vector) |
| 500 | { |
| 501 | unsigned long flags; |
| 502 | int cpu, apic_id, icr; |
| 503 | int min = 0, max = 0; |
| 504 | #ifdef CONFIG_X86_64 |
| 505 | __uint128_t ipi_bitmap = 0; |
| 506 | #else |
| 507 | u64 ipi_bitmap = 0; |
| 508 | #endif |
Sean Christopherson | de81c2f | 2019-01-23 09:22:40 -0800 | [diff] [blame] | 509 | long ret; |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 510 | |
| 511 | if (cpumask_empty(mask)) |
| 512 | return; |
| 513 | |
| 514 | local_irq_save(flags); |
| 515 | |
| 516 | switch (vector) { |
| 517 | default: |
| 518 | icr = APIC_DM_FIXED | vector; |
| 519 | break; |
| 520 | case NMI_VECTOR: |
| 521 | icr = APIC_DM_NMI; |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | for_each_cpu(cpu, mask) { |
| 526 | apic_id = per_cpu(x86_cpu_to_apicid, cpu); |
| 527 | if (!ipi_bitmap) { |
| 528 | min = max = apic_id; |
| 529 | } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) { |
| 530 | ipi_bitmap <<= min - apic_id; |
| 531 | min = apic_id; |
| 532 | } else if (apic_id < min + KVM_IPI_CLUSTER_SIZE) { |
| 533 | max = apic_id < max ? max : apic_id; |
| 534 | } else { |
Sean Christopherson | de81c2f | 2019-01-23 09:22:40 -0800 | [diff] [blame] | 535 | ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap, |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 536 | (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr); |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 537 | WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld", |
| 538 | ret); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 539 | min = max = apic_id; |
| 540 | ipi_bitmap = 0; |
| 541 | } |
| 542 | __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap); |
| 543 | } |
| 544 | |
| 545 | if (ipi_bitmap) { |
Sean Christopherson | de81c2f | 2019-01-23 09:22:40 -0800 | [diff] [blame] | 546 | ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap, |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 547 | (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr); |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 548 | WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld", |
| 549 | ret); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | local_irq_restore(flags); |
| 553 | } |
| 554 | |
| 555 | static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) |
| 556 | { |
| 557 | __send_ipi_mask(mask, vector); |
| 558 | } |
| 559 | |
| 560 | static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector) |
| 561 | { |
| 562 | unsigned int this_cpu = smp_processor_id(); |
Wanpeng Li | 8a9442f | 2020-02-18 09:08:24 +0800 | [diff] [blame] | 563 | struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 564 | const struct cpumask *local_mask; |
| 565 | |
Wanpeng Li | 8a9442f | 2020-02-18 09:08:24 +0800 | [diff] [blame] | 566 | cpumask_copy(new_mask, mask); |
| 567 | cpumask_clear_cpu(this_cpu, new_mask); |
| 568 | local_mask = new_mask; |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 569 | __send_ipi_mask(local_mask, vector); |
| 570 | } |
| 571 | |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 572 | /* |
| 573 | * Set the IPI entry points |
| 574 | */ |
| 575 | static void kvm_setup_pv_ipi(void) |
| 576 | { |
| 577 | apic->send_IPI_mask = kvm_send_ipi_mask; |
| 578 | apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself; |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 579 | pr_info("setup PV IPIs\n"); |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 580 | } |
| 581 | |
Wanpeng Li | f85f6e7 | 2019-06-11 20:23:48 +0800 | [diff] [blame] | 582 | static void kvm_smp_send_call_func_ipi(const struct cpumask *mask) |
| 583 | { |
| 584 | int cpu; |
| 585 | |
| 586 | native_send_call_func_ipi(mask); |
| 587 | |
| 588 | /* Make sure other vCPUs get a chance to run if they need to. */ |
| 589 | for_each_cpu(cpu, mask) { |
| 590 | if (vcpu_is_preempted(cpu)) { |
| 591 | kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu)); |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
Linus Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 597 | static void kvm_flush_tlb_multi(const struct cpumask *cpumask, |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 598 | const struct flush_tlb_info *info) |
| 599 | { |
| 600 | u8 state; |
| 601 | int cpu; |
| 602 | struct kvm_steal_time *src; |
| 603 | struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); |
| 604 | |
| 605 | cpumask_copy(flushmask, cpumask); |
| 606 | /* |
| 607 | * We have to call flush only on online vCPUs. And |
| 608 | * queue flush_on_enter for pre-empted vCPUs |
| 609 | */ |
| 610 | for_each_cpu(cpu, flushmask) { |
Linus Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 611 | /* |
| 612 | * The local vCPU is never preempted, so we do not explicitly |
| 613 | * skip check for local vCPU - it will never be cleared from |
| 614 | * flushmask. |
| 615 | */ |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 616 | src = &per_cpu(steal_time, cpu); |
| 617 | state = READ_ONCE(src->preempted); |
| 618 | if ((state & KVM_VCPU_PREEMPTED)) { |
| 619 | if (try_cmpxchg(&src->preempted, &state, |
| 620 | state | KVM_VCPU_FLUSH_TLB)) |
| 621 | __cpumask_clear_cpu(cpu, flushmask); |
| 622 | } |
| 623 | } |
| 624 | |
Linus Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 625 | native_flush_tlb_multi(flushmask, info); |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | static __init int kvm_alloc_cpumask(void) |
| 629 | { |
| 630 | int cpu; |
| 631 | |
| 632 | if (!kvm_para_available() || nopv) |
| 633 | return 0; |
| 634 | |
| 635 | if (pv_tlb_flush_supported() || pv_ipi_supported()) |
| 636 | for_each_possible_cpu(cpu) { |
| 637 | zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu), |
| 638 | GFP_KERNEL, cpu_to_node(cpu)); |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | arch_initcall(kvm_alloc_cpumask); |
| 644 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 645 | static void __init kvm_smp_prepare_boot_cpu(void) |
| 646 | { |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 647 | /* |
| 648 | * Map the per-cpu variables as decrypted before kvm_guest_cpu_init() |
| 649 | * shares the guest physical address with the hypervisor. |
| 650 | */ |
| 651 | sev_map_percpu_data(); |
| 652 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 653 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 654 | native_smp_prepare_boot_cpu(); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 655 | kvm_spinlock_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 656 | } |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 657 | |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 658 | static int kvm_cpu_down_prepare(unsigned int cpu) |
| 659 | { |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame^] | 660 | unsigned long flags; |
| 661 | |
| 662 | local_irq_save(flags); |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 663 | kvm_guest_cpu_offline(); |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame^] | 664 | local_irq_restore(flags); |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 665 | return 0; |
| 666 | } |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 667 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 668 | #endif |
| 669 | |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame^] | 670 | static int kvm_suspend(void) |
| 671 | { |
| 672 | kvm_guest_cpu_offline(); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static void kvm_resume(void) |
| 678 | { |
| 679 | kvm_cpu_online(raw_smp_processor_id()); |
| 680 | } |
| 681 | |
| 682 | static struct syscore_ops kvm_syscore_ops = { |
| 683 | .suspend = kvm_suspend, |
| 684 | .resume = kvm_resume, |
| 685 | }; |
| 686 | |
Juergen Gross | f361464 | 2017-11-09 14:27:38 +0100 | [diff] [blame] | 687 | static void __init kvm_guest_init(void) |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 688 | { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 689 | int i; |
| 690 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 691 | paravirt_ops_setup(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 692 | register_reboot_notifier(&kvm_pv_reboot_nb); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 693 | for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) |
Rik van Riel | 9db284f | 2016-03-21 15:13:27 +0100 | [diff] [blame] | 694 | raw_spin_lock_init(&async_pf_sleepers[i].lock); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 695 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 696 | if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { |
| 697 | has_steal_clock = 1; |
Juergen Gross | a0e2bf7 | 2021-03-11 15:23:09 +0100 | [diff] [blame] | 698 | static_call_update(pv_steal_clock, kvm_steal_clock); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 699 | } |
| 700 | |
Michael S. Tsirkin | 9053666 | 2012-07-15 15:56:52 +0300 | [diff] [blame] | 701 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 702 | apic_set_eoi_write(kvm_guest_apic_eoi_write); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 703 | |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 704 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) { |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 705 | static_branch_enable(&kvm_async_pf_enabled); |
Paolo Bonzini | 26d05b3 | 2020-06-15 07:53:05 -0400 | [diff] [blame] | 706 | alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt); |
Vitaly Kuznetsov | b1d4057 | 2020-05-25 16:41:23 +0200 | [diff] [blame] | 707 | } |
Andy Lutomirski | ef68017 | 2020-02-28 10:42:48 -0800 | [diff] [blame] | 708 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 709 | #ifdef CONFIG_SMP |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 710 | if (pv_tlb_flush_supported()) { |
Linus Torvalds | 152d32a | 2021-05-01 10:14:08 -0700 | [diff] [blame] | 711 | pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi; |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 712 | pv_ops.mmu.tlb_remove_table = tlb_remove_table; |
| 713 | pr_info("KVM setup pv remote TLB flush\n"); |
| 714 | } |
| 715 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 716 | smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 717 | if (pv_sched_yield_supported()) { |
Wanpeng Li | f85f6e7 | 2019-06-11 20:23:48 +0800 | [diff] [blame] | 718 | smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi; |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 719 | pr_info("setup PV sched yield\n"); |
Wanpeng Li | f85f6e7 | 2019-06-11 20:23:48 +0800 | [diff] [blame] | 720 | } |
Sebastian Andrzej Siewior | 9a20ea4 | 2016-08-18 14:57:29 +0200 | [diff] [blame] | 721 | if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online", |
| 722 | kvm_cpu_online, kvm_cpu_down_prepare) < 0) |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 723 | pr_err("failed to install cpu hotplug callbacks\n"); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 724 | #else |
Brijesh Singh | 4716276 | 2017-10-20 09:30:58 -0500 | [diff] [blame] | 725 | sev_map_percpu_data(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 726 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 727 | #endif |
Ulrich Obergfell | 9919e39 | 2014-10-13 15:55:37 -0700 | [diff] [blame] | 728 | |
Vitaly Kuznetsov | 8b79fef | 2021-04-14 14:35:41 +0200 | [diff] [blame^] | 729 | register_syscore_ops(&kvm_syscore_ops); |
| 730 | |
Ulrich Obergfell | 9919e39 | 2014-10-13 15:55:37 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Hard lockup detection is enabled by default. Disable it, as guests |
| 733 | * can get false positives too easily, for example if the host is |
| 734 | * overcommitted. |
| 735 | */ |
Ulrich Obergfell | 692297d | 2015-04-14 15:44:19 -0700 | [diff] [blame] | 736 | hardlockup_detector_disable(); |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 737 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 738 | |
Paolo Bonzini | 1c300a4 | 2014-01-27 14:49:40 +0100 | [diff] [blame] | 739 | static noinline uint32_t __kvm_cpuid_base(void) |
| 740 | { |
| 741 | if (boot_cpu_data.cpuid_level < 0) |
| 742 | return 0; /* So we don't blow up on old processors */ |
| 743 | |
Borislav Petkov | 0c9f3536 | 2016-03-29 17:41:55 +0200 | [diff] [blame] | 744 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) |
Paolo Bonzini | 1c300a4 | 2014-01-27 14:49:40 +0100 | [diff] [blame] | 745 | return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static inline uint32_t kvm_cpuid_base(void) |
| 751 | { |
| 752 | static int kvm_cpuid_base = -1; |
| 753 | |
| 754 | if (kvm_cpuid_base == -1) |
| 755 | kvm_cpuid_base = __kvm_cpuid_base(); |
| 756 | |
| 757 | return kvm_cpuid_base; |
| 758 | } |
| 759 | |
| 760 | bool kvm_para_available(void) |
| 761 | { |
| 762 | return kvm_cpuid_base() != 0; |
| 763 | } |
| 764 | EXPORT_SYMBOL_GPL(kvm_para_available); |
| 765 | |
Paolo Bonzini | 77f01bd | 2014-01-27 14:51:44 +0100 | [diff] [blame] | 766 | unsigned int kvm_arch_para_features(void) |
| 767 | { |
| 768 | return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES); |
| 769 | } |
| 770 | |
Wanpeng Li | a4429e5 | 2018-02-13 09:05:40 +0800 | [diff] [blame] | 771 | unsigned int kvm_arch_para_hints(void) |
| 772 | { |
| 773 | return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES); |
| 774 | } |
Wanpeng Li | 1328edc | 2019-08-29 16:49:57 +0800 | [diff] [blame] | 775 | EXPORT_SYMBOL_GPL(kvm_arch_para_hints); |
Wanpeng Li | a4429e5 | 2018-02-13 09:05:40 +0800 | [diff] [blame] | 776 | |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 777 | static uint32_t __init kvm_detect(void) |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 778 | { |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 779 | return kvm_cpuid_base(); |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 780 | } |
| 781 | |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 782 | static void __init kvm_apic_init(void) |
| 783 | { |
Wanpeng Li | 2b519b57 | 2021-04-09 12:18:29 +0800 | [diff] [blame] | 784 | #ifdef CONFIG_SMP |
Wanpeng Li | a262bca | 2020-02-18 09:08:23 +0800 | [diff] [blame] | 785 | if (pv_ipi_supported()) |
Wanpeng Li | aaffcfd | 2018-07-23 14:39:52 +0800 | [diff] [blame] | 786 | kvm_setup_pv_ipi(); |
| 787 | #endif |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 788 | } |
| 789 | |
David Woodhouse | 2e008ff | 2020-10-24 22:35:35 +0100 | [diff] [blame] | 790 | static bool __init kvm_msi_ext_dest_id(void) |
| 791 | { |
| 792 | return kvm_para_has_feature(KVM_FEATURE_MSI_EXT_DEST_ID); |
| 793 | } |
| 794 | |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 795 | static void __init kvm_init_platform(void) |
| 796 | { |
Linus Torvalds | e61cf2e | 2018-08-19 10:38:36 -0700 | [diff] [blame] | 797 | kvmclock_init(); |
Wanpeng Li | d63bae0 | 2018-07-23 14:39:51 +0800 | [diff] [blame] | 798 | x86_platform.apic_post_init = kvm_apic_init; |
| 799 | } |
| 800 | |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 801 | #if defined(CONFIG_AMD_MEM_ENCRYPT) |
| 802 | static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs) |
| 803 | { |
| 804 | /* RAX and CPL are already in the GHCB */ |
| 805 | ghcb_set_rbx(ghcb, regs->bx); |
| 806 | ghcb_set_rcx(ghcb, regs->cx); |
| 807 | ghcb_set_rdx(ghcb, regs->dx); |
| 808 | ghcb_set_rsi(ghcb, regs->si); |
| 809 | } |
| 810 | |
| 811 | static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs) |
| 812 | { |
| 813 | /* No checking of the return state needed */ |
| 814 | return true; |
| 815 | } |
| 816 | #endif |
| 817 | |
Juergen Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 818 | const __initconst struct hypervisor_x86 x86_hyper_kvm = { |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 819 | .name = "KVM", |
| 820 | .detect = kvm_detect, |
| 821 | .type = X86_HYPER_KVM, |
| 822 | .init.guest_late_init = kvm_guest_init, |
| 823 | .init.x2apic_available = kvm_para_available, |
David Woodhouse | 2e008ff | 2020-10-24 22:35:35 +0100 | [diff] [blame] | 824 | .init.msi_ext_dest_id = kvm_msi_ext_dest_id, |
Tom Lendacky | 99419b2 | 2020-09-07 15:16:04 +0200 | [diff] [blame] | 825 | .init.init_platform = kvm_init_platform, |
| 826 | #if defined(CONFIG_AMD_MEM_ENCRYPT) |
| 827 | .runtime.sev_es_hcall_prepare = kvm_sev_es_hcall_prepare, |
| 828 | .runtime.sev_es_hcall_finish = kvm_sev_es_hcall_finish, |
| 829 | #endif |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 830 | }; |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 831 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 832 | static __init int activate_jump_labels(void) |
| 833 | { |
| 834 | if (has_steal_clock) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 835 | static_key_slow_inc(¶virt_steal_enabled); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 836 | if (steal_acc) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 837 | static_key_slow_inc(¶virt_steal_rq_enabled); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | arch_initcall(activate_jump_labels); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 843 | |
| 844 | #ifdef CONFIG_PARAVIRT_SPINLOCKS |
| 845 | |
| 846 | /* 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] | 847 | static void kvm_kick_cpu(int cpu) |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 848 | { |
| 849 | int apicid; |
| 850 | unsigned long flags = 0; |
| 851 | |
| 852 | apicid = per_cpu(x86_cpu_to_apicid, cpu); |
| 853 | kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid); |
| 854 | } |
| 855 | |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 856 | #include <asm/qspinlock.h> |
| 857 | |
| 858 | static void kvm_wait(u8 *ptr, u8 val) |
| 859 | { |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 860 | if (in_nmi()) |
| 861 | return; |
| 862 | |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 863 | /* |
| 864 | * halt until it's our turn and kicked. Note that we do safe halt |
| 865 | * for irq enabled case to avoid hang when lock info is overwritten |
| 866 | * in irq spinlock slowpath and no spurious interrupt occur to save us. |
| 867 | */ |
Wanpeng Li | f4e61f0 | 2021-03-15 14:55:28 +0800 | [diff] [blame] | 868 | if (irqs_disabled()) { |
| 869 | if (READ_ONCE(*ptr) == val) |
| 870 | halt(); |
| 871 | } else { |
| 872 | local_irq_disable(); |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 873 | |
Wanpeng Li | f4e61f0 | 2021-03-15 14:55:28 +0800 | [diff] [blame] | 874 | if (READ_ONCE(*ptr) == val) |
| 875 | safe_halt(); |
| 876 | |
| 877 | local_irq_enable(); |
| 878 | } |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Waiman Long | dd0fd8b | 2017-02-20 13:36:04 -0500 | [diff] [blame] | 881 | #ifdef CONFIG_X86_32 |
Waiman Long | 6c62985 | 2017-02-20 13:36:03 -0500 | [diff] [blame] | 882 | __visible bool __kvm_vcpu_is_preempted(long cpu) |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 883 | { |
| 884 | struct kvm_steal_time *src = &per_cpu(steal_time, cpu); |
| 885 | |
Wanpeng Li | fa55eed | 2017-12-12 17:33:01 -0800 | [diff] [blame] | 886 | return !!(src->preempted & KVM_VCPU_PREEMPTED); |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 887 | } |
| 888 | PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted); |
| 889 | |
Waiman Long | dd0fd8b | 2017-02-20 13:36:04 -0500 | [diff] [blame] | 890 | #else |
| 891 | |
| 892 | #include <asm/asm-offsets.h> |
| 893 | |
| 894 | extern bool __raw_callee_save___kvm_vcpu_is_preempted(long); |
| 895 | |
| 896 | /* |
| 897 | * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and |
| 898 | * restoring to/from the stack. |
| 899 | */ |
| 900 | asm( |
| 901 | ".pushsection .text;" |
| 902 | ".global __raw_callee_save___kvm_vcpu_is_preempted;" |
| 903 | ".type __raw_callee_save___kvm_vcpu_is_preempted, @function;" |
| 904 | "__raw_callee_save___kvm_vcpu_is_preempted:" |
| 905 | "movq __per_cpu_offset(,%rdi,8), %rax;" |
| 906 | "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);" |
| 907 | "setne %al;" |
| 908 | "ret;" |
Josh Poimboeuf | 083db67 | 2019-07-17 20:36:36 -0500 | [diff] [blame] | 909 | ".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] | 910 | ".popsection"); |
| 911 | |
| 912 | #endif |
| 913 | |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 914 | /* |
| 915 | * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present. |
| 916 | */ |
| 917 | void __init kvm_spinlock_init(void) |
| 918 | { |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 919 | /* |
| 920 | * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an |
| 921 | * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is |
| 922 | * preferred over native qspinlock when vCPU is preempted. |
| 923 | */ |
| 924 | if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) { |
| 925 | pr_info("PV spinlocks disabled, no host support\n"); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 926 | return; |
Zhenzhong Duan | de58502 | 2019-10-23 19:16:20 +0800 | [diff] [blame] | 927 | } |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 928 | |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 929 | /* |
| 930 | * Disable PV spinlocks and use native qspinlock when dedicated pCPUs |
| 931 | * are available. |
| 932 | */ |
| 933 | if (kvm_para_has_hint(KVM_HINTS_REALTIME)) { |
| 934 | pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n"); |
| 935 | goto out; |
| 936 | } |
Wanpeng Li | b2798ba | 2018-02-13 09:05:41 +0800 | [diff] [blame] | 937 | |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 938 | if (num_possible_cpus() == 1) { |
| 939 | pr_info("PV spinlocks disabled, single CPU\n"); |
| 940 | goto out; |
| 941 | } |
| 942 | |
| 943 | if (nopvspin) { |
| 944 | pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n"); |
| 945 | goto out; |
| 946 | } |
| 947 | |
| 948 | pr_info("PV spinlocks enabled\n"); |
Waiman Long | 3553ae5 | 2018-07-17 17:59:27 -0400 | [diff] [blame] | 949 | |
Waiman Long | bf0c7c3 | 2015-04-24 14:56:39 -0400 | [diff] [blame] | 950 | __pv_init_lock_hash(); |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 951 | pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath; |
| 952 | pv_ops.lock.queued_spin_unlock = |
| 953 | PV_CALLEE_SAVE(__pv_queued_spin_unlock); |
| 954 | pv_ops.lock.wait = kvm_wait; |
| 955 | pv_ops.lock.kick = kvm_kick_cpu; |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 956 | |
| 957 | if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 958 | pv_ops.lock.vcpu_is_preempted = |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 959 | PV_CALLEE_SAVE(__kvm_vcpu_is_preempted); |
| 960 | } |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 961 | /* |
| 962 | * When PV spinlock is enabled which is preferred over |
| 963 | * virt_spin_lock(), virt_spin_lock_key's value is meaningless. |
| 964 | * Just disable it anyway. |
| 965 | */ |
| 966 | out: |
| 967 | static_branch_disable(&virt_spin_lock_key); |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 968 | } |
Raghavendra K T | 3dbef3e | 2013-10-09 14:33:21 +0530 | [diff] [blame] | 969 | |
Srivatsa Vaddagiri | 92b7520 | 2013-08-06 14:55:41 +0530 | [diff] [blame] | 970 | #endif /* CONFIG_PARAVIRT_SPINLOCKS */ |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 971 | |
| 972 | #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL |
| 973 | |
| 974 | static void kvm_disable_host_haltpoll(void *i) |
| 975 | { |
| 976 | wrmsrl(MSR_KVM_POLL_CONTROL, 0); |
| 977 | } |
| 978 | |
| 979 | static void kvm_enable_host_haltpoll(void *i) |
| 980 | { |
| 981 | wrmsrl(MSR_KVM_POLL_CONTROL, 1); |
| 982 | } |
| 983 | |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 984 | void arch_haltpoll_enable(unsigned int cpu) |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 985 | { |
| 986 | if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) { |
Zhenzhong Duan | 5aefd78 | 2019-10-23 19:16:21 +0800 | [diff] [blame] | 987 | pr_err_once("host does not support poll control\n"); |
| 988 | pr_err_once("host upgrade recommended\n"); |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 989 | return; |
| 990 | } |
| 991 | |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 992 | /* Enable guest halt poll disables host halt poll */ |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 993 | smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1); |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 994 | } |
| 995 | EXPORT_SYMBOL_GPL(arch_haltpoll_enable); |
| 996 | |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 997 | void arch_haltpoll_disable(unsigned int cpu) |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 998 | { |
| 999 | if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) |
| 1000 | return; |
| 1001 | |
Li Qiang | b785a44 | 2020-09-24 08:58:00 -0700 | [diff] [blame] | 1002 | /* Disable guest halt poll enables host halt poll */ |
Joao Martins | 97d3eb9 | 2019-09-02 11:40:31 +0100 | [diff] [blame] | 1003 | smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1); |
Marcelo Tosatti | a1c4423 | 2019-07-03 20:51:29 -0300 | [diff] [blame] | 1004 | } |
| 1005 | EXPORT_SYMBOL_GPL(arch_haltpoll_disable); |
| 1006 | #endif |