blob: a91c6b482b48bc556c9e1c7ee1f9733cc6755ab9 [file] [log] [blame]
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -05001/*
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 Tosatti1da8a772008-02-22 12:21:37 -050028#include <linux/highmem.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050029#include <linux/hardirq.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020030#include <linux/notifier.h>
31#include <linux/reboot.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020032#include <linux/hash.h>
33#include <linux/sched.h>
34#include <linux/slab.h>
35#include <linux/kprobes.h>
Marcelo Tosattia90ede72009-02-11 22:45:42 -020036#include <asm/timer.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020037#include <asm/cpu.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020038#include <asm/traps.h>
39#include <asm/desc.h>
Gleb Natapov6c047cd2010-10-14 11:22:54 +020040#include <asm/tlbflush.h>
Gleb Natapove0875922012-04-04 15:30:33 +030041#include <asm/idle.h>
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030042#include <asm/apic.h>
43#include <asm/apicdef.h>
Prarit Bhargavafc733732012-07-06 13:47:39 -040044#include <asm/hypervisor.h>
Marcelo Tosatti3dc4f7c2012-11-27 23:28:56 -020045#include <asm/kvm_guest.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050046
Gleb Natapovfd10cde2010-10-14 11:22:51 +020047static int kvmapf = 1;
48
49static int parse_no_kvmapf(char *arg)
50{
51 kvmapf = 0;
52 return 0;
53}
54
55early_param("no-kvmapf", parse_no_kvmapf);
56
Glauber Costad910f5c2011-07-11 15:28:19 -040057static int steal_acc = 1;
58static int parse_no_stealacc(char *arg)
59{
60 steal_acc = 0;
61 return 0;
62}
63
64early_param("no-steal-acc", parse_no_stealacc);
65
Marcelo Tosatti3dc4f7c2012-11-27 23:28:56 -020066static int kvmclock_vsyscall = 1;
67static int parse_no_kvmclock_vsyscall(char *arg)
68{
69 kvmclock_vsyscall = 0;
70 return 0;
71}
72
73early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
74
Gleb Natapovfd10cde2010-10-14 11:22:51 +020075static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
Glauber Costad910f5c2011-07-11 15:28:19 -040076static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
77static int has_steal_clock = 0;
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050078
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -050079/*
80 * No need for any "IO delay" on KVM
81 */
82static void kvm_io_delay(void)
83{
84}
85
Gleb Natapov631bc482010-10-14 11:22:52 +020086#define KVM_TASK_SLEEP_HASHBITS 8
87#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
88
89struct kvm_task_sleep_node {
90 struct hlist_node link;
91 wait_queue_head_t wq;
92 u32 token;
93 int cpu;
Gleb Natapov6c047cd2010-10-14 11:22:54 +020094 bool halted;
Gleb Natapov631bc482010-10-14 11:22:52 +020095};
96
97static struct kvm_task_sleep_head {
98 spinlock_t lock;
99 struct hlist_head list;
100} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
101
102static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
103 u32 token)
104{
105 struct hlist_node *p;
106
107 hlist_for_each(p, &b->list) {
108 struct kvm_task_sleep_node *n =
109 hlist_entry(p, typeof(*n), link);
110 if (n->token == token)
111 return n;
112 }
113
114 return NULL;
115}
116
117void kvm_async_pf_task_wait(u32 token)
118{
119 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
120 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
121 struct kvm_task_sleep_node n, *e;
122 DEFINE_WAIT(wait);
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200123 int cpu, idle;
124
125 cpu = get_cpu();
126 idle = idle_cpu(cpu);
127 put_cpu();
Gleb Natapov631bc482010-10-14 11:22:52 +0200128
129 spin_lock(&b->lock);
130 e = _find_apf_task(b, token);
131 if (e) {
132 /* dummy entry exist -> wake up was delivered ahead of PF */
133 hlist_del(&e->link);
134 kfree(e);
135 spin_unlock(&b->lock);
136 return;
137 }
138
139 n.token = token;
140 n.cpu = smp_processor_id();
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200141 n.halted = idle || preempt_count() > 1;
Gleb Natapov631bc482010-10-14 11:22:52 +0200142 init_waitqueue_head(&n.wq);
143 hlist_add_head(&n.link, &b->list);
144 spin_unlock(&b->lock);
145
146 for (;;) {
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200147 if (!n.halted)
148 prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
Gleb Natapov631bc482010-10-14 11:22:52 +0200149 if (hlist_unhashed(&n.link))
150 break;
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200151
152 if (!n.halted) {
153 local_irq_enable();
154 schedule();
155 local_irq_disable();
156 } else {
157 /*
158 * We cannot reschedule. So halt.
159 */
160 native_safe_halt();
161 local_irq_disable();
162 }
Gleb Natapov631bc482010-10-14 11:22:52 +0200163 }
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200164 if (!n.halted)
165 finish_wait(&n.wq, &wait);
Gleb Natapov631bc482010-10-14 11:22:52 +0200166
167 return;
168}
169EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
170
171static void apf_task_wake_one(struct kvm_task_sleep_node *n)
172{
173 hlist_del_init(&n->link);
Gleb Natapov6c047cd2010-10-14 11:22:54 +0200174 if (n->halted)
175 smp_send_reschedule(n->cpu);
176 else if (waitqueue_active(&n->wq))
Gleb Natapov631bc482010-10-14 11:22:52 +0200177 wake_up(&n->wq);
178}
179
180static void apf_task_wake_all(void)
181{
182 int i;
183
184 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
185 struct hlist_node *p, *next;
186 struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
187 spin_lock(&b->lock);
188 hlist_for_each_safe(p, next, &b->list) {
189 struct kvm_task_sleep_node *n =
190 hlist_entry(p, typeof(*n), link);
191 if (n->cpu == smp_processor_id())
192 apf_task_wake_one(n);
193 }
194 spin_unlock(&b->lock);
195 }
196}
197
198void kvm_async_pf_task_wake(u32 token)
199{
200 u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
201 struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
202 struct kvm_task_sleep_node *n;
203
204 if (token == ~0) {
205 apf_task_wake_all();
206 return;
207 }
208
209again:
210 spin_lock(&b->lock);
211 n = _find_apf_task(b, token);
212 if (!n) {
213 /*
214 * async PF was not yet handled.
215 * Add dummy entry for the token.
216 */
Gleb Natapov62c49cc2012-05-02 15:04:02 +0300217 n = kzalloc(sizeof(*n), GFP_ATOMIC);
Gleb Natapov631bc482010-10-14 11:22:52 +0200218 if (!n) {
219 /*
220 * Allocation failed! Busy wait while other cpu
221 * handles async PF.
222 */
223 spin_unlock(&b->lock);
224 cpu_relax();
225 goto again;
226 }
227 n->token = token;
228 n->cpu = smp_processor_id();
229 init_waitqueue_head(&n->wq);
230 hlist_add_head(&n->link, &b->list);
231 } else
232 apf_task_wake_one(n);
233 spin_unlock(&b->lock);
234 return;
235}
236EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
237
238u32 kvm_read_and_reset_pf_reason(void)
239{
240 u32 reason = 0;
241
242 if (__get_cpu_var(apf_reason).enabled) {
243 reason = __get_cpu_var(apf_reason).reason;
244 __get_cpu_var(apf_reason).reason = 0;
245 }
246
247 return reason;
248}
249EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
250
251dotraplinkage void __kprobes
252do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
253{
254 switch (kvm_read_and_reset_pf_reason()) {
255 default:
256 do_page_fault(regs, error_code);
257 break;
258 case KVM_PV_REASON_PAGE_NOT_PRESENT:
259 /* page is swapped out by the host. */
Sasha Levinc5e015d2012-10-19 12:11:55 -0400260 rcu_irq_enter();
261 exit_idle();
Gleb Natapov631bc482010-10-14 11:22:52 +0200262 kvm_async_pf_task_wait((u32)read_cr2());
Sasha Levinc5e015d2012-10-19 12:11:55 -0400263 rcu_irq_exit();
Gleb Natapov631bc482010-10-14 11:22:52 +0200264 break;
265 case KVM_PV_REASON_PAGE_READY:
Gleb Natapove0875922012-04-04 15:30:33 +0300266 rcu_irq_enter();
267 exit_idle();
Gleb Natapov631bc482010-10-14 11:22:52 +0200268 kvm_async_pf_task_wake((u32)read_cr2());
Gleb Natapove0875922012-04-04 15:30:33 +0300269 rcu_irq_exit();
Gleb Natapov631bc482010-10-14 11:22:52 +0200270 break;
271 }
272}
273
Rakib Mullickd3ac8812009-07-02 11:40:36 +0600274static void __init paravirt_ops_setup(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500275{
276 pv_info.name = "KVM";
277 pv_info.paravirt_enabled = 1;
278
279 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
280 pv_cpu_ops.io_delay = kvm_io_delay;
281
Marcelo Tosattia90ede72009-02-11 22:45:42 -0200282#ifdef CONFIG_X86_IO_APIC
283 no_timer_check = 1;
284#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500285}
286
Glauber Costad910f5c2011-07-11 15:28:19 -0400287static void kvm_register_steal_time(void)
288{
289 int cpu = smp_processor_id();
290 struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
291
292 if (!has_steal_clock)
293 return;
294
295 memset(st, 0, sizeof(*st));
296
297 wrmsrl(MSR_KVM_STEAL_TIME, (__pa(st) | KVM_MSR_ENABLED));
298 printk(KERN_INFO "kvm-stealtime: cpu %d, msr %lx\n",
299 cpu, __pa(st));
300}
301
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300302static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
303
304static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
305{
306 /**
307 * This relies on __test_and_clear_bit to modify the memory
308 * in a way that is atomic with respect to the local CPU.
309 * The hypervisor only accesses this memory from the local CPU so
310 * there's no need for lock or memory barriers.
311 * An optimization barrier is implied in apic write.
312 */
313 if (__test_and_clear_bit(KVM_PV_EOI_BIT, &__get_cpu_var(kvm_apic_eoi)))
314 return;
Michael S. Tsirkin90536662012-07-15 15:56:52 +0300315 apic_write(APIC_EOI, APIC_EOI_ACK);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300316}
317
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200318void __cpuinit kvm_guest_cpu_init(void)
319{
320 if (!kvm_para_available())
321 return;
322
323 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
324 u64 pa = __pa(&__get_cpu_var(apf_reason));
325
Gleb Natapov6adba522010-10-14 11:22:55 +0200326#ifdef CONFIG_PREEMPT
327 pa |= KVM_ASYNC_PF_SEND_ALWAYS;
328#endif
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200329 wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
330 __get_cpu_var(apf_reason).enabled = 1;
331 printk(KERN_INFO"KVM setup async PF for cpu %d\n",
332 smp_processor_id());
333 }
Glauber Costad910f5c2011-07-11 15:28:19 -0400334
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300335 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
336 unsigned long pa;
337 /* Size alignment is implied but just to make it explicit. */
338 BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
339 __get_cpu_var(kvm_apic_eoi) = 0;
340 pa = __pa(&__get_cpu_var(kvm_apic_eoi)) | KVM_MSR_ENABLED;
341 wrmsrl(MSR_KVM_PV_EOI_EN, pa);
342 }
343
Glauber Costad910f5c2011-07-11 15:28:19 -0400344 if (has_steal_clock)
345 kvm_register_steal_time();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200346}
347
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300348static void kvm_pv_disable_apf(void)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200349{
350 if (!__get_cpu_var(apf_reason).enabled)
351 return;
352
353 wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
354 __get_cpu_var(apf_reason).enabled = 0;
355
356 printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
357 smp_processor_id());
358}
359
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300360static void kvm_pv_guest_cpu_reboot(void *unused)
361{
362 /*
363 * We disable PV EOI before we load a new kernel by kexec,
364 * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
365 * New kernel can re-enable when it boots.
366 */
367 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
368 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
369 kvm_pv_disable_apf();
Florian Westphal8fbe6a52012-08-15 16:00:40 +0200370 kvm_disable_steal_time();
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300371}
372
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200373static int kvm_pv_reboot_notify(struct notifier_block *nb,
374 unsigned long code, void *unused)
375{
376 if (code == SYS_RESTART)
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300377 on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200378 return NOTIFY_DONE;
379}
380
381static struct notifier_block kvm_pv_reboot_nb = {
382 .notifier_call = kvm_pv_reboot_notify,
383};
384
Glauber Costad910f5c2011-07-11 15:28:19 -0400385static u64 kvm_steal_clock(int cpu)
386{
387 u64 steal;
388 struct kvm_steal_time *src;
389 int version;
390
391 src = &per_cpu(steal_time, cpu);
392 do {
393 version = src->version;
394 rmb();
395 steal = src->steal;
396 rmb();
397 } while ((version & 1) || (version != src->version));
398
399 return steal;
400}
401
402void kvm_disable_steal_time(void)
403{
404 if (!has_steal_clock)
405 return;
406
407 wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
408}
409
Gleb Natapovca3f1012010-10-14 11:22:49 +0200410#ifdef CONFIG_SMP
411static void __init kvm_smp_prepare_boot_cpu(void)
412{
413 WARN_ON(kvm_register_clock("primary cpu clock"));
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200414 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200415 native_smp_prepare_boot_cpu();
416}
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200417
Sedat Dilek775077a2011-01-03 00:01:29 +0100418static void __cpuinit kvm_guest_cpu_online(void *dummy)
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200419{
420 kvm_guest_cpu_init();
421}
422
423static void kvm_guest_cpu_offline(void *dummy)
424{
Glauber Costad910f5c2011-07-11 15:28:19 -0400425 kvm_disable_steal_time();
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300426 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
427 wrmsrl(MSR_KVM_PV_EOI_EN, 0);
428 kvm_pv_disable_apf();
Gleb Natapov631bc482010-10-14 11:22:52 +0200429 apf_task_wake_all();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200430}
431
432static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
433 unsigned long action, void *hcpu)
434{
435 int cpu = (unsigned long)hcpu;
436 switch (action) {
437 case CPU_ONLINE:
438 case CPU_DOWN_FAILED:
439 case CPU_ONLINE_FROZEN:
440 smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
441 break;
442 case CPU_DOWN_PREPARE:
443 case CPU_DOWN_PREPARE_FROZEN:
444 smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
445 break;
446 default:
447 break;
448 }
449 return NOTIFY_OK;
450}
451
452static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
453 .notifier_call = kvm_cpu_notify,
454};
Gleb Natapovca3f1012010-10-14 11:22:49 +0200455#endif
456
Gleb Natapov631bc482010-10-14 11:22:52 +0200457static void __init kvm_apf_trap_init(void)
458{
459 set_intr_gate(14, &async_page_fault);
460}
461
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500462void __init kvm_guest_init(void)
463{
Gleb Natapov631bc482010-10-14 11:22:52 +0200464 int i;
465
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500466 if (!kvm_para_available())
467 return;
468
469 paravirt_ops_setup();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200470 register_reboot_notifier(&kvm_pv_reboot_nb);
Gleb Natapov631bc482010-10-14 11:22:52 +0200471 for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
472 spin_lock_init(&async_pf_sleepers[i].lock);
473 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
474 x86_init.irqs.trap_init = kvm_apf_trap_init;
475
Glauber Costad910f5c2011-07-11 15:28:19 -0400476 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
477 has_steal_clock = 1;
478 pv_time_ops.steal_clock = kvm_steal_clock;
479 }
480
Michael S. Tsirkin90536662012-07-15 15:56:52 +0300481 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
482 apic_set_eoi_write(kvm_guest_apic_eoi_write);
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +0300483
Marcelo Tosatti3dc4f7c2012-11-27 23:28:56 -0200484 if (kvmclock_vsyscall)
485 kvm_setup_vsyscall_timeinfo();
486
Gleb Natapovca3f1012010-10-14 11:22:49 +0200487#ifdef CONFIG_SMP
488 smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200489 register_cpu_notifier(&kvm_cpu_notifier);
490#else
491 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200492#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500493}
Glauber Costad910f5c2011-07-11 15:28:19 -0400494
Prarit Bhargavafc733732012-07-06 13:47:39 -0400495static bool __init kvm_detect(void)
496{
497 if (!kvm_para_available())
498 return false;
499 return true;
500}
501
502const struct hypervisor_x86 x86_hyper_kvm __refconst = {
503 .name = "KVM",
504 .detect = kvm_detect,
505};
506EXPORT_SYMBOL_GPL(x86_hyper_kvm);
507
Glauber Costad910f5c2011-07-11 15:28:19 -0400508static __init int activate_jump_labels(void)
509{
510 if (has_steal_clock) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100511 static_key_slow_inc(&paravirt_steal_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400512 if (steal_acc)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100513 static_key_slow_inc(&paravirt_steal_rq_enabled);
Glauber Costad910f5c2011-07-11 15:28:19 -0400514 }
515
516 return 0;
517}
518arch_initcall(activate_jump_labels);