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