Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * KVM paravirt_ops implementation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 19 | * Copyright IBM Corporation, 2007 |
| 20 | * Authors: Anthony Liguori <aliguori@us.ibm.com> |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/kvm_para.h> |
| 26 | #include <linux/cpu.h> |
| 27 | #include <linux/mm.h> |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 28 | #include <linux/highmem.h> |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 29 | #include <linux/hardirq.h> |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 30 | #include <linux/notifier.h> |
| 31 | #include <linux/reboot.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 32 | #include <linux/hash.h> |
| 33 | #include <linux/sched.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/kprobes.h> |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 36 | #include <asm/timer.h> |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 37 | #include <asm/cpu.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 38 | #include <asm/traps.h> |
| 39 | #include <asm/desc.h> |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 40 | #include <asm/tlbflush.h> |
Gleb Natapov | e087592 | 2012-04-04 15:30:33 +0300 | [diff] [blame] | 41 | #include <asm/idle.h> |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 42 | #include <asm/apic.h> |
| 43 | #include <asm/apicdef.h> |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 44 | #include <asm/hypervisor.h> |
Marcelo Tosatti | 3dc4f7c | 2012-11-27 23:28:56 -0200 | [diff] [blame] | 45 | #include <asm/kvm_guest.h> |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 46 | #include <asm/context_tracking.h> |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 47 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 48 | static int kvmapf = 1; |
| 49 | |
| 50 | static int parse_no_kvmapf(char *arg) |
| 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; |
| 59 | static int parse_no_stealacc(char *arg) |
| 60 | { |
| 61 | steal_acc = 0; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | early_param("no-steal-acc", parse_no_stealacc); |
| 66 | |
Marcelo Tosatti | 3dc4f7c | 2012-11-27 23:28:56 -0200 | [diff] [blame] | 67 | static int kvmclock_vsyscall = 1; |
| 68 | static int parse_no_kvmclock_vsyscall(char *arg) |
| 69 | { |
| 70 | kvmclock_vsyscall = 0; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall); |
| 75 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 76 | static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 77 | static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); |
| 78 | static int has_steal_clock = 0; |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 79 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 80 | /* |
| 81 | * No need for any "IO delay" on KVM |
| 82 | */ |
| 83 | static void kvm_io_delay(void) |
| 84 | { |
| 85 | } |
| 86 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 87 | #define KVM_TASK_SLEEP_HASHBITS 8 |
| 88 | #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS) |
| 89 | |
| 90 | struct kvm_task_sleep_node { |
| 91 | struct hlist_node link; |
| 92 | wait_queue_head_t wq; |
| 93 | u32 token; |
| 94 | int cpu; |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 95 | bool halted; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static struct kvm_task_sleep_head { |
| 99 | spinlock_t lock; |
| 100 | struct hlist_head list; |
| 101 | } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE]; |
| 102 | |
| 103 | static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b, |
| 104 | u32 token) |
| 105 | { |
| 106 | struct hlist_node *p; |
| 107 | |
| 108 | hlist_for_each(p, &b->list) { |
| 109 | struct kvm_task_sleep_node *n = |
| 110 | hlist_entry(p, typeof(*n), link); |
| 111 | if (n->token == token) |
| 112 | return n; |
| 113 | } |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | void kvm_async_pf_task_wait(u32 token) |
| 119 | { |
| 120 | u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS); |
| 121 | struct kvm_task_sleep_head *b = &async_pf_sleepers[key]; |
| 122 | struct kvm_task_sleep_node n, *e; |
| 123 | DEFINE_WAIT(wait); |
| 124 | |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 125 | rcu_irq_enter(); |
| 126 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 127 | spin_lock(&b->lock); |
| 128 | e = _find_apf_task(b, token); |
| 129 | if (e) { |
| 130 | /* dummy entry exist -> wake up was delivered ahead of PF */ |
| 131 | hlist_del(&e->link); |
| 132 | kfree(e); |
| 133 | spin_unlock(&b->lock); |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 134 | |
| 135 | rcu_irq_exit(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
| 139 | n.token = token; |
| 140 | n.cpu = smp_processor_id(); |
Gleb Natapov | 859f845 | 2012-11-28 15:19:08 +0200 | [diff] [blame] | 141 | n.halted = is_idle_task(current) || preempt_count() > 1; |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 142 | init_waitqueue_head(&n.wq); |
| 143 | hlist_add_head(&n.link, &b->list); |
| 144 | spin_unlock(&b->lock); |
| 145 | |
| 146 | for (;;) { |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 147 | if (!n.halted) |
| 148 | prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 149 | if (hlist_unhashed(&n.link)) |
| 150 | break; |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 151 | |
| 152 | if (!n.halted) { |
| 153 | local_irq_enable(); |
| 154 | schedule(); |
| 155 | local_irq_disable(); |
| 156 | } else { |
| 157 | /* |
| 158 | * We cannot reschedule. So halt. |
| 159 | */ |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 160 | rcu_irq_exit(); |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 161 | native_safe_halt(); |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 162 | rcu_irq_enter(); |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 163 | local_irq_disable(); |
| 164 | } |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 165 | } |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 166 | if (!n.halted) |
| 167 | finish_wait(&n.wq, &wait); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 168 | |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 169 | rcu_irq_exit(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait); |
| 173 | |
| 174 | static void apf_task_wake_one(struct kvm_task_sleep_node *n) |
| 175 | { |
| 176 | hlist_del_init(&n->link); |
Gleb Natapov | 6c047cd | 2010-10-14 11:22:54 +0200 | [diff] [blame] | 177 | if (n->halted) |
| 178 | smp_send_reschedule(n->cpu); |
| 179 | else if (waitqueue_active(&n->wq)) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 180 | wake_up(&n->wq); |
| 181 | } |
| 182 | |
| 183 | static void apf_task_wake_all(void) |
| 184 | { |
| 185 | int i; |
| 186 | |
| 187 | for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) { |
| 188 | struct hlist_node *p, *next; |
| 189 | struct kvm_task_sleep_head *b = &async_pf_sleepers[i]; |
| 190 | spin_lock(&b->lock); |
| 191 | hlist_for_each_safe(p, next, &b->list) { |
| 192 | struct kvm_task_sleep_node *n = |
| 193 | hlist_entry(p, typeof(*n), link); |
| 194 | if (n->cpu == smp_processor_id()) |
| 195 | apf_task_wake_one(n); |
| 196 | } |
| 197 | spin_unlock(&b->lock); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void kvm_async_pf_task_wake(u32 token) |
| 202 | { |
| 203 | u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS); |
| 204 | struct kvm_task_sleep_head *b = &async_pf_sleepers[key]; |
| 205 | struct kvm_task_sleep_node *n; |
| 206 | |
| 207 | if (token == ~0) { |
| 208 | apf_task_wake_all(); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | again: |
| 213 | spin_lock(&b->lock); |
| 214 | n = _find_apf_task(b, token); |
| 215 | if (!n) { |
| 216 | /* |
| 217 | * async PF was not yet handled. |
| 218 | * Add dummy entry for the token. |
| 219 | */ |
Gleb Natapov | 62c49cc | 2012-05-02 15:04:02 +0300 | [diff] [blame] | 220 | n = kzalloc(sizeof(*n), GFP_ATOMIC); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 221 | if (!n) { |
| 222 | /* |
| 223 | * Allocation failed! Busy wait while other cpu |
| 224 | * handles async PF. |
| 225 | */ |
| 226 | spin_unlock(&b->lock); |
| 227 | cpu_relax(); |
| 228 | goto again; |
| 229 | } |
| 230 | n->token = token; |
| 231 | n->cpu = smp_processor_id(); |
| 232 | init_waitqueue_head(&n->wq); |
| 233 | hlist_add_head(&n->link, &b->list); |
| 234 | } else |
| 235 | apf_task_wake_one(n); |
| 236 | spin_unlock(&b->lock); |
| 237 | return; |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake); |
| 240 | |
| 241 | u32 kvm_read_and_reset_pf_reason(void) |
| 242 | { |
| 243 | u32 reason = 0; |
| 244 | |
| 245 | if (__get_cpu_var(apf_reason).enabled) { |
| 246 | reason = __get_cpu_var(apf_reason).reason; |
| 247 | __get_cpu_var(apf_reason).reason = 0; |
| 248 | } |
| 249 | |
| 250 | return reason; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason); |
| 253 | |
| 254 | dotraplinkage void __kprobes |
| 255 | do_async_page_fault(struct pt_regs *regs, unsigned long error_code) |
| 256 | { |
| 257 | switch (kvm_read_and_reset_pf_reason()) { |
| 258 | default: |
| 259 | do_page_fault(regs, error_code); |
| 260 | break; |
| 261 | case KVM_PV_REASON_PAGE_NOT_PRESENT: |
| 262 | /* page is swapped out by the host. */ |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 263 | exception_enter(regs); |
Sasha Levin | c5e015d | 2012-10-19 12:11:55 -0400 | [diff] [blame] | 264 | exit_idle(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 265 | kvm_async_pf_task_wait((u32)read_cr2()); |
Li Zhong | 9b132fb | 2012-12-04 10:35:13 +0800 | [diff] [blame^] | 266 | exception_exit(regs); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 267 | break; |
| 268 | case KVM_PV_REASON_PAGE_READY: |
Gleb Natapov | e087592 | 2012-04-04 15:30:33 +0300 | [diff] [blame] | 269 | rcu_irq_enter(); |
| 270 | exit_idle(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 271 | kvm_async_pf_task_wake((u32)read_cr2()); |
Gleb Natapov | e087592 | 2012-04-04 15:30:33 +0300 | [diff] [blame] | 272 | rcu_irq_exit(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
Rakib Mullick | d3ac881 | 2009-07-02 11:40:36 +0600 | [diff] [blame] | 277 | static void __init paravirt_ops_setup(void) |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 278 | { |
| 279 | pv_info.name = "KVM"; |
| 280 | pv_info.paravirt_enabled = 1; |
| 281 | |
| 282 | if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY)) |
| 283 | pv_cpu_ops.io_delay = kvm_io_delay; |
| 284 | |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 285 | #ifdef CONFIG_X86_IO_APIC |
| 286 | no_timer_check = 1; |
| 287 | #endif |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 288 | } |
| 289 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 290 | static void kvm_register_steal_time(void) |
| 291 | { |
| 292 | int cpu = smp_processor_id(); |
| 293 | struct kvm_steal_time *st = &per_cpu(steal_time, cpu); |
| 294 | |
| 295 | if (!has_steal_clock) |
| 296 | return; |
| 297 | |
| 298 | memset(st, 0, sizeof(*st)); |
| 299 | |
| 300 | wrmsrl(MSR_KVM_STEAL_TIME, (__pa(st) | KVM_MSR_ENABLED)); |
| 301 | printk(KERN_INFO "kvm-stealtime: cpu %d, msr %lx\n", |
| 302 | cpu, __pa(st)); |
| 303 | } |
| 304 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 305 | static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED; |
| 306 | |
| 307 | static void kvm_guest_apic_eoi_write(u32 reg, u32 val) |
| 308 | { |
| 309 | /** |
| 310 | * This relies on __test_and_clear_bit to modify the memory |
| 311 | * in a way that is atomic with respect to the local CPU. |
| 312 | * The hypervisor only accesses this memory from the local CPU so |
| 313 | * there's no need for lock or memory barriers. |
| 314 | * An optimization barrier is implied in apic write. |
| 315 | */ |
| 316 | if (__test_and_clear_bit(KVM_PV_EOI_BIT, &__get_cpu_var(kvm_apic_eoi))) |
| 317 | return; |
Michael S. Tsirkin | 9053666 | 2012-07-15 15:56:52 +0300 | [diff] [blame] | 318 | apic_write(APIC_EOI, APIC_EOI_ACK); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 319 | } |
| 320 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 321 | void __cpuinit kvm_guest_cpu_init(void) |
| 322 | { |
| 323 | if (!kvm_para_available()) |
| 324 | return; |
| 325 | |
| 326 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) { |
| 327 | u64 pa = __pa(&__get_cpu_var(apf_reason)); |
| 328 | |
Gleb Natapov | 6adba52 | 2010-10-14 11:22:55 +0200 | [diff] [blame] | 329 | #ifdef CONFIG_PREEMPT |
| 330 | pa |= KVM_ASYNC_PF_SEND_ALWAYS; |
| 331 | #endif |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 332 | wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED); |
| 333 | __get_cpu_var(apf_reason).enabled = 1; |
| 334 | printk(KERN_INFO"KVM setup async PF for cpu %d\n", |
| 335 | smp_processor_id()); |
| 336 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 337 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 338 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) { |
| 339 | unsigned long pa; |
| 340 | /* Size alignment is implied but just to make it explicit. */ |
| 341 | BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4); |
| 342 | __get_cpu_var(kvm_apic_eoi) = 0; |
| 343 | pa = __pa(&__get_cpu_var(kvm_apic_eoi)) | KVM_MSR_ENABLED; |
| 344 | wrmsrl(MSR_KVM_PV_EOI_EN, pa); |
| 345 | } |
| 346 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 347 | if (has_steal_clock) |
| 348 | kvm_register_steal_time(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 351 | static void kvm_pv_disable_apf(void) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 352 | { |
| 353 | if (!__get_cpu_var(apf_reason).enabled) |
| 354 | return; |
| 355 | |
| 356 | wrmsrl(MSR_KVM_ASYNC_PF_EN, 0); |
| 357 | __get_cpu_var(apf_reason).enabled = 0; |
| 358 | |
| 359 | printk(KERN_INFO"Unregister pv shared memory for cpu %d\n", |
| 360 | smp_processor_id()); |
| 361 | } |
| 362 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 363 | static void kvm_pv_guest_cpu_reboot(void *unused) |
| 364 | { |
| 365 | /* |
| 366 | * We disable PV EOI before we load a new kernel by kexec, |
| 367 | * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory. |
| 368 | * New kernel can re-enable when it boots. |
| 369 | */ |
| 370 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 371 | wrmsrl(MSR_KVM_PV_EOI_EN, 0); |
| 372 | kvm_pv_disable_apf(); |
Florian Westphal | 8fbe6a5 | 2012-08-15 16:00:40 +0200 | [diff] [blame] | 373 | kvm_disable_steal_time(); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 374 | } |
| 375 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 376 | static int kvm_pv_reboot_notify(struct notifier_block *nb, |
| 377 | unsigned long code, void *unused) |
| 378 | { |
| 379 | if (code == SYS_RESTART) |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 380 | on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 381 | return NOTIFY_DONE; |
| 382 | } |
| 383 | |
| 384 | static struct notifier_block kvm_pv_reboot_nb = { |
| 385 | .notifier_call = kvm_pv_reboot_notify, |
| 386 | }; |
| 387 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 388 | static u64 kvm_steal_clock(int cpu) |
| 389 | { |
| 390 | u64 steal; |
| 391 | struct kvm_steal_time *src; |
| 392 | int version; |
| 393 | |
| 394 | src = &per_cpu(steal_time, cpu); |
| 395 | do { |
| 396 | version = src->version; |
| 397 | rmb(); |
| 398 | steal = src->steal; |
| 399 | rmb(); |
| 400 | } while ((version & 1) || (version != src->version)); |
| 401 | |
| 402 | return steal; |
| 403 | } |
| 404 | |
| 405 | void kvm_disable_steal_time(void) |
| 406 | { |
| 407 | if (!has_steal_clock) |
| 408 | return; |
| 409 | |
| 410 | wrmsr(MSR_KVM_STEAL_TIME, 0, 0); |
| 411 | } |
| 412 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 413 | #ifdef CONFIG_SMP |
| 414 | static void __init kvm_smp_prepare_boot_cpu(void) |
| 415 | { |
| 416 | WARN_ON(kvm_register_clock("primary cpu clock")); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 417 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 418 | native_smp_prepare_boot_cpu(); |
| 419 | } |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 420 | |
Sedat Dilek | 775077a | 2011-01-03 00:01:29 +0100 | [diff] [blame] | 421 | static void __cpuinit kvm_guest_cpu_online(void *dummy) |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 422 | { |
| 423 | kvm_guest_cpu_init(); |
| 424 | } |
| 425 | |
| 426 | static void kvm_guest_cpu_offline(void *dummy) |
| 427 | { |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 428 | kvm_disable_steal_time(); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 429 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 430 | wrmsrl(MSR_KVM_PV_EOI_EN, 0); |
| 431 | kvm_pv_disable_apf(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 432 | apf_task_wake_all(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static int __cpuinit kvm_cpu_notify(struct notifier_block *self, |
| 436 | unsigned long action, void *hcpu) |
| 437 | { |
| 438 | int cpu = (unsigned long)hcpu; |
| 439 | switch (action) { |
| 440 | case CPU_ONLINE: |
| 441 | case CPU_DOWN_FAILED: |
| 442 | case CPU_ONLINE_FROZEN: |
| 443 | smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0); |
| 444 | break; |
| 445 | case CPU_DOWN_PREPARE: |
| 446 | case CPU_DOWN_PREPARE_FROZEN: |
| 447 | smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1); |
| 448 | break; |
| 449 | default: |
| 450 | break; |
| 451 | } |
| 452 | return NOTIFY_OK; |
| 453 | } |
| 454 | |
| 455 | static struct notifier_block __cpuinitdata kvm_cpu_notifier = { |
| 456 | .notifier_call = kvm_cpu_notify, |
| 457 | }; |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 458 | #endif |
| 459 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 460 | static void __init kvm_apf_trap_init(void) |
| 461 | { |
| 462 | set_intr_gate(14, &async_page_fault); |
| 463 | } |
| 464 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 465 | void __init kvm_guest_init(void) |
| 466 | { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 467 | int i; |
| 468 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 469 | if (!kvm_para_available()) |
| 470 | return; |
| 471 | |
| 472 | paravirt_ops_setup(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 473 | register_reboot_notifier(&kvm_pv_reboot_nb); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 474 | for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) |
| 475 | spin_lock_init(&async_pf_sleepers[i].lock); |
| 476 | if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF)) |
| 477 | x86_init.irqs.trap_init = kvm_apf_trap_init; |
| 478 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 479 | if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { |
| 480 | has_steal_clock = 1; |
| 481 | pv_time_ops.steal_clock = kvm_steal_clock; |
| 482 | } |
| 483 | |
Michael S. Tsirkin | 9053666 | 2012-07-15 15:56:52 +0300 | [diff] [blame] | 484 | if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) |
| 485 | apic_set_eoi_write(kvm_guest_apic_eoi_write); |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 486 | |
Marcelo Tosatti | 3dc4f7c | 2012-11-27 23:28:56 -0200 | [diff] [blame] | 487 | if (kvmclock_vsyscall) |
| 488 | kvm_setup_vsyscall_timeinfo(); |
| 489 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 490 | #ifdef CONFIG_SMP |
| 491 | smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 492 | register_cpu_notifier(&kvm_cpu_notifier); |
| 493 | #else |
| 494 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 495 | #endif |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 496 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 497 | |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 498 | static bool __init kvm_detect(void) |
| 499 | { |
| 500 | if (!kvm_para_available()) |
| 501 | return false; |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | const struct hypervisor_x86 x86_hyper_kvm __refconst = { |
| 506 | .name = "KVM", |
| 507 | .detect = kvm_detect, |
| 508 | }; |
| 509 | EXPORT_SYMBOL_GPL(x86_hyper_kvm); |
| 510 | |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 511 | static __init int activate_jump_labels(void) |
| 512 | { |
| 513 | if (has_steal_clock) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 514 | static_key_slow_inc(¶virt_steal_enabled); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 515 | if (steal_acc) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 516 | static_key_slow_inc(¶virt_steal_rq_enabled); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | arch_initcall(activate_jump_labels); |