2 * KVM paravirt_ops implementation
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.
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.
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.
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>
23 #include <linux/context_tracking.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/kvm_para.h>
27 #include <linux/cpu.h>
29 #include <linux/highmem.h>
30 #include <linux/hardirq.h>
31 #include <linux/notifier.h>
32 #include <linux/reboot.h>
33 #include <linux/hash.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/kprobes.h>
37 #include <asm/timer.h>
39 #include <asm/traps.h>
41 #include <asm/tlbflush.h>
44 #include <asm/apicdef.h>
45 #include <asm/hypervisor.h>
46 #include <asm/kvm_guest.h>
48 static int kvmapf
= 1;
50 static int parse_no_kvmapf(char *arg
)
56 early_param("no-kvmapf", parse_no_kvmapf
);
58 static int steal_acc
= 1;
59 static int parse_no_stealacc(char *arg
)
65 early_param("no-steal-acc", parse_no_stealacc
);
67 static int kvmclock_vsyscall
= 1;
68 static int parse_no_kvmclock_vsyscall(char *arg
)
70 kvmclock_vsyscall
= 0;
74 early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall
);
76 static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data
, apf_reason
) __aligned(64);
77 static DEFINE_PER_CPU(struct kvm_steal_time
, steal_time
) __aligned(64);
78 static int has_steal_clock
= 0;
81 * No need for any "IO delay" on KVM
83 static void kvm_io_delay(void)
87 #define KVM_TASK_SLEEP_HASHBITS 8
88 #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
90 struct kvm_task_sleep_node
{
91 struct hlist_node link
;
98 static struct kvm_task_sleep_head
{
100 struct hlist_head list
;
101 } async_pf_sleepers
[KVM_TASK_SLEEP_HASHSIZE
];
103 static struct kvm_task_sleep_node
*_find_apf_task(struct kvm_task_sleep_head
*b
,
106 struct hlist_node
*p
;
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
)
118 void kvm_async_pf_task_wait(u32 token
)
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
;
128 e
= _find_apf_task(b
, token
);
130 /* dummy entry exist -> wake up was delivered ahead of PF */
133 spin_unlock(&b
->lock
);
140 n
.cpu
= smp_processor_id();
141 n
.halted
= is_idle_task(current
) || preempt_count() > 1;
142 init_waitqueue_head(&n
.wq
);
143 hlist_add_head(&n
.link
, &b
->list
);
144 spin_unlock(&b
->lock
);
148 prepare_to_wait(&n
.wq
, &wait
, TASK_UNINTERRUPTIBLE
);
149 if (hlist_unhashed(&n
.link
))
158 * We cannot reschedule. So halt.
167 finish_wait(&n
.wq
, &wait
);
172 EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait
);
174 static void apf_task_wake_one(struct kvm_task_sleep_node
*n
)
176 hlist_del_init(&n
->link
);
178 smp_send_reschedule(n
->cpu
);
179 else if (waitqueue_active(&n
->wq
))
183 static void apf_task_wake_all(void)
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
];
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
);
197 spin_unlock(&b
->lock
);
201 void kvm_async_pf_task_wake(u32 token
)
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
;
214 n
= _find_apf_task(b
, token
);
217 * async PF was not yet handled.
218 * Add dummy entry for the token.
220 n
= kzalloc(sizeof(*n
), GFP_ATOMIC
);
223 * Allocation failed! Busy wait while other cpu
226 spin_unlock(&b
->lock
);
231 n
->cpu
= smp_processor_id();
232 init_waitqueue_head(&n
->wq
);
233 hlist_add_head(&n
->link
, &b
->list
);
235 apf_task_wake_one(n
);
236 spin_unlock(&b
->lock
);
239 EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake
);
241 u32
kvm_read_and_reset_pf_reason(void)
245 if (__get_cpu_var(apf_reason
).enabled
) {
246 reason
= __get_cpu_var(apf_reason
).reason
;
247 __get_cpu_var(apf_reason
).reason
= 0;
252 EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason
);
254 dotraplinkage
void __kprobes
255 do_async_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
257 enum ctx_state prev_state
;
259 switch (kvm_read_and_reset_pf_reason()) {
261 do_page_fault(regs
, error_code
);
263 case KVM_PV_REASON_PAGE_NOT_PRESENT
:
264 /* page is swapped out by the host. */
265 prev_state
= exception_enter();
267 kvm_async_pf_task_wait((u32
)read_cr2());
268 exception_exit(prev_state
);
270 case KVM_PV_REASON_PAGE_READY
:
273 kvm_async_pf_task_wake((u32
)read_cr2());
279 static void __init
paravirt_ops_setup(void)
281 pv_info
.name
= "KVM";
284 * KVM isn't paravirt in the sense of paravirt_enabled. A KVM
285 * guest kernel works like a bare metal kernel with additional
286 * features, and paravirt_enabled is about features that are
289 pv_info
.paravirt_enabled
= 0;
291 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY
))
292 pv_cpu_ops
.io_delay
= kvm_io_delay
;
294 #ifdef CONFIG_X86_IO_APIC
299 static void kvm_register_steal_time(void)
301 int cpu
= smp_processor_id();
302 struct kvm_steal_time
*st
= &per_cpu(steal_time
, cpu
);
304 if (!has_steal_clock
)
307 memset(st
, 0, sizeof(*st
));
309 wrmsrl(MSR_KVM_STEAL_TIME
, (slow_virt_to_phys(st
) | KVM_MSR_ENABLED
));
310 pr_info("kvm-stealtime: cpu %d, msr %llx\n",
311 cpu
, (unsigned long long) slow_virt_to_phys(st
));
314 static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi
) = KVM_PV_EOI_DISABLED
;
316 static void kvm_guest_apic_eoi_write(u32 reg
, u32 val
)
319 * This relies on __test_and_clear_bit to modify the memory
320 * in a way that is atomic with respect to the local CPU.
321 * The hypervisor only accesses this memory from the local CPU so
322 * there's no need for lock or memory barriers.
323 * An optimization barrier is implied in apic write.
325 if (__test_and_clear_bit(KVM_PV_EOI_BIT
, &__get_cpu_var(kvm_apic_eoi
)))
327 apic_write(APIC_EOI
, APIC_EOI_ACK
);
330 void __cpuinit
kvm_guest_cpu_init(void)
332 if (!kvm_para_available())
335 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF
) && kvmapf
) {
336 u64 pa
= slow_virt_to_phys(&__get_cpu_var(apf_reason
));
338 #ifdef CONFIG_PREEMPT
339 pa
|= KVM_ASYNC_PF_SEND_ALWAYS
;
341 wrmsrl(MSR_KVM_ASYNC_PF_EN
, pa
| KVM_ASYNC_PF_ENABLED
);
342 __get_cpu_var(apf_reason
).enabled
= 1;
343 printk(KERN_INFO
"KVM setup async PF for cpu %d\n",
347 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI
)) {
349 /* Size alignment is implied but just to make it explicit. */
350 BUILD_BUG_ON(__alignof__(kvm_apic_eoi
) < 4);
351 __get_cpu_var(kvm_apic_eoi
) = 0;
352 pa
= slow_virt_to_phys(&__get_cpu_var(kvm_apic_eoi
))
354 wrmsrl(MSR_KVM_PV_EOI_EN
, pa
);
358 kvm_register_steal_time();
361 static void kvm_pv_disable_apf(void)
363 if (!__get_cpu_var(apf_reason
).enabled
)
366 wrmsrl(MSR_KVM_ASYNC_PF_EN
, 0);
367 __get_cpu_var(apf_reason
).enabled
= 0;
369 printk(KERN_INFO
"Unregister pv shared memory for cpu %d\n",
373 static void kvm_pv_guest_cpu_reboot(void *unused
)
376 * We disable PV EOI before we load a new kernel by kexec,
377 * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
378 * New kernel can re-enable when it boots.
380 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI
))
381 wrmsrl(MSR_KVM_PV_EOI_EN
, 0);
382 kvm_pv_disable_apf();
383 kvm_disable_steal_time();
386 static int kvm_pv_reboot_notify(struct notifier_block
*nb
,
387 unsigned long code
, void *unused
)
389 if (code
== SYS_RESTART
)
390 on_each_cpu(kvm_pv_guest_cpu_reboot
, NULL
, 1);
394 static struct notifier_block kvm_pv_reboot_nb
= {
395 .notifier_call
= kvm_pv_reboot_notify
,
398 static u64
kvm_steal_clock(int cpu
)
401 struct kvm_steal_time
*src
;
404 src
= &per_cpu(steal_time
, cpu
);
406 version
= src
->version
;
410 } while ((version
& 1) || (version
!= src
->version
));
415 void kvm_disable_steal_time(void)
417 if (!has_steal_clock
)
420 wrmsr(MSR_KVM_STEAL_TIME
, 0, 0);
424 static void __init
kvm_smp_prepare_boot_cpu(void)
426 WARN_ON(kvm_register_clock("primary cpu clock"));
427 kvm_guest_cpu_init();
428 native_smp_prepare_boot_cpu();
431 static void __cpuinit
kvm_guest_cpu_online(void *dummy
)
433 kvm_guest_cpu_init();
436 static void kvm_guest_cpu_offline(void *dummy
)
438 kvm_disable_steal_time();
439 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI
))
440 wrmsrl(MSR_KVM_PV_EOI_EN
, 0);
441 kvm_pv_disable_apf();
445 static int __cpuinit
kvm_cpu_notify(struct notifier_block
*self
,
446 unsigned long action
, void *hcpu
)
448 int cpu
= (unsigned long)hcpu
;
451 case CPU_DOWN_FAILED
:
452 case CPU_ONLINE_FROZEN
:
453 smp_call_function_single(cpu
, kvm_guest_cpu_online
, NULL
, 0);
455 case CPU_DOWN_PREPARE
:
456 case CPU_DOWN_PREPARE_FROZEN
:
457 smp_call_function_single(cpu
, kvm_guest_cpu_offline
, NULL
, 1);
465 static struct notifier_block __cpuinitdata kvm_cpu_notifier
= {
466 .notifier_call
= kvm_cpu_notify
,
470 static void __init
kvm_apf_trap_init(void)
472 set_intr_gate(14, &async_page_fault
);
475 void __init
kvm_guest_init(void)
479 if (!kvm_para_available())
482 paravirt_ops_setup();
483 register_reboot_notifier(&kvm_pv_reboot_nb
);
484 for (i
= 0; i
< KVM_TASK_SLEEP_HASHSIZE
; i
++)
485 spin_lock_init(&async_pf_sleepers
[i
].lock
);
486 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF
))
487 x86_init
.irqs
.trap_init
= kvm_apf_trap_init
;
489 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME
)) {
491 pv_time_ops
.steal_clock
= kvm_steal_clock
;
494 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI
))
495 apic_set_eoi_write(kvm_guest_apic_eoi_write
);
497 if (kvmclock_vsyscall
)
498 kvm_setup_vsyscall_timeinfo();
501 smp_ops
.smp_prepare_boot_cpu
= kvm_smp_prepare_boot_cpu
;
502 register_cpu_notifier(&kvm_cpu_notifier
);
504 kvm_guest_cpu_init();
508 static bool __init
kvm_detect(void)
510 if (!kvm_para_available())
515 const struct hypervisor_x86 x86_hyper_kvm __refconst
= {
517 .detect
= kvm_detect
,
518 .x2apic_available
= kvm_para_available
,
520 EXPORT_SYMBOL_GPL(x86_hyper_kvm
);
522 static __init
int activate_jump_labels(void)
524 if (has_steal_clock
) {
525 static_key_slow_inc(¶virt_steal_enabled
);
527 static_key_slow_inc(¶virt_steal_rq_enabled
);
532 arch_initcall(activate_jump_labels
);