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/module.h>
24 #include <linux/kernel.h>
25 #include <linux/kvm_para.h>
26 #include <linux/cpu.h>
28 #include <linux/highmem.h>
29 #include <linux/hardirq.h>
30 #include <linux/notifier.h>
31 #include <linux/reboot.h>
32 #include <linux/hash.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/kprobes.h>
36 #include <asm/timer.h>
38 #include <asm/traps.h>
40 #include <asm/tlbflush.h>
43 static int kvmapf
= 1;
45 static int parse_no_kvmapf(char *arg
)
51 early_param("no-kvmapf", parse_no_kvmapf
);
53 static int steal_acc
= 1;
54 static int parse_no_stealacc(char *arg
)
60 early_param("no-steal-acc", parse_no_stealacc
);
62 static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data
, apf_reason
) __aligned(64);
63 static DEFINE_PER_CPU(struct kvm_steal_time
, steal_time
) __aligned(64);
64 static int has_steal_clock
= 0;
67 * No need for any "IO delay" on KVM
69 static void kvm_io_delay(void)
73 #define KVM_TASK_SLEEP_HASHBITS 8
74 #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
76 struct kvm_task_sleep_node
{
77 struct hlist_node link
;
84 static struct kvm_task_sleep_head
{
86 struct hlist_head list
;
87 } async_pf_sleepers
[KVM_TASK_SLEEP_HASHSIZE
];
89 static struct kvm_task_sleep_node
*_find_apf_task(struct kvm_task_sleep_head
*b
,
94 hlist_for_each(p
, &b
->list
) {
95 struct kvm_task_sleep_node
*n
=
96 hlist_entry(p
, typeof(*n
), link
);
97 if (n
->token
== token
)
104 void kvm_async_pf_task_wait(u32 token
)
106 u32 key
= hash_32(token
, KVM_TASK_SLEEP_HASHBITS
);
107 struct kvm_task_sleep_head
*b
= &async_pf_sleepers
[key
];
108 struct kvm_task_sleep_node n
, *e
;
113 idle
= idle_cpu(cpu
);
117 e
= _find_apf_task(b
, token
);
119 /* dummy entry exist -> wake up was delivered ahead of PF */
122 spin_unlock(&b
->lock
);
127 n
.cpu
= smp_processor_id();
128 n
.halted
= idle
|| preempt_count() > 1;
129 init_waitqueue_head(&n
.wq
);
130 hlist_add_head(&n
.link
, &b
->list
);
131 spin_unlock(&b
->lock
);
135 prepare_to_wait(&n
.wq
, &wait
, TASK_UNINTERRUPTIBLE
);
136 if (hlist_unhashed(&n
.link
))
145 * We cannot reschedule. So halt.
152 finish_wait(&n
.wq
, &wait
);
156 EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait
);
158 static void apf_task_wake_one(struct kvm_task_sleep_node
*n
)
160 hlist_del_init(&n
->link
);
162 smp_send_reschedule(n
->cpu
);
163 else if (waitqueue_active(&n
->wq
))
167 static void apf_task_wake_all(void)
171 for (i
= 0; i
< KVM_TASK_SLEEP_HASHSIZE
; i
++) {
172 struct hlist_node
*p
, *next
;
173 struct kvm_task_sleep_head
*b
= &async_pf_sleepers
[i
];
175 hlist_for_each_safe(p
, next
, &b
->list
) {
176 struct kvm_task_sleep_node
*n
=
177 hlist_entry(p
, typeof(*n
), link
);
178 if (n
->cpu
== smp_processor_id())
179 apf_task_wake_one(n
);
181 spin_unlock(&b
->lock
);
185 void kvm_async_pf_task_wake(u32 token
)
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
;
198 n
= _find_apf_task(b
, token
);
201 * async PF was not yet handled.
202 * Add dummy entry for the token.
204 n
= kzalloc(sizeof(*n
), GFP_ATOMIC
);
207 * Allocation failed! Busy wait while other cpu
210 spin_unlock(&b
->lock
);
215 n
->cpu
= smp_processor_id();
216 init_waitqueue_head(&n
->wq
);
217 hlist_add_head(&n
->link
, &b
->list
);
219 apf_task_wake_one(n
);
220 spin_unlock(&b
->lock
);
223 EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake
);
225 u32
kvm_read_and_reset_pf_reason(void)
229 if (__get_cpu_var(apf_reason
).enabled
) {
230 reason
= __get_cpu_var(apf_reason
).reason
;
231 __get_cpu_var(apf_reason
).reason
= 0;
236 EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason
);
238 dotraplinkage
void __kprobes
239 do_async_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
241 switch (kvm_read_and_reset_pf_reason()) {
243 do_page_fault(regs
, error_code
);
245 case KVM_PV_REASON_PAGE_NOT_PRESENT
:
246 /* page is swapped out by the host. */
247 kvm_async_pf_task_wait((u32
)read_cr2());
249 case KVM_PV_REASON_PAGE_READY
:
252 kvm_async_pf_task_wake((u32
)read_cr2());
258 static void __init
paravirt_ops_setup(void)
260 pv_info
.name
= "KVM";
261 pv_info
.paravirt_enabled
= 1;
263 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY
))
264 pv_cpu_ops
.io_delay
= kvm_io_delay
;
266 #ifdef CONFIG_X86_IO_APIC
271 static void kvm_register_steal_time(void)
273 int cpu
= smp_processor_id();
274 struct kvm_steal_time
*st
= &per_cpu(steal_time
, cpu
);
276 if (!has_steal_clock
)
279 memset(st
, 0, sizeof(*st
));
281 wrmsrl(MSR_KVM_STEAL_TIME
, (__pa(st
) | KVM_MSR_ENABLED
));
282 printk(KERN_INFO
"kvm-stealtime: cpu %d, msr %lx\n",
286 void __cpuinit
kvm_guest_cpu_init(void)
288 if (!kvm_para_available())
291 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF
) && kvmapf
) {
292 u64 pa
= __pa(&__get_cpu_var(apf_reason
));
294 #ifdef CONFIG_PREEMPT
295 pa
|= KVM_ASYNC_PF_SEND_ALWAYS
;
297 wrmsrl(MSR_KVM_ASYNC_PF_EN
, pa
| KVM_ASYNC_PF_ENABLED
);
298 __get_cpu_var(apf_reason
).enabled
= 1;
299 printk(KERN_INFO
"KVM setup async PF for cpu %d\n",
304 kvm_register_steal_time();
307 static void kvm_pv_disable_apf(void *unused
)
309 if (!__get_cpu_var(apf_reason
).enabled
)
312 wrmsrl(MSR_KVM_ASYNC_PF_EN
, 0);
313 __get_cpu_var(apf_reason
).enabled
= 0;
315 printk(KERN_INFO
"Unregister pv shared memory for cpu %d\n",
319 static int kvm_pv_reboot_notify(struct notifier_block
*nb
,
320 unsigned long code
, void *unused
)
322 if (code
== SYS_RESTART
)
323 on_each_cpu(kvm_pv_disable_apf
, NULL
, 1);
327 static struct notifier_block kvm_pv_reboot_nb
= {
328 .notifier_call
= kvm_pv_reboot_notify
,
331 static u64
kvm_steal_clock(int cpu
)
334 struct kvm_steal_time
*src
;
337 src
= &per_cpu(steal_time
, cpu
);
339 version
= src
->version
;
343 } while ((version
& 1) || (version
!= src
->version
));
348 void kvm_disable_steal_time(void)
350 if (!has_steal_clock
)
353 wrmsr(MSR_KVM_STEAL_TIME
, 0, 0);
357 static void __init
kvm_smp_prepare_boot_cpu(void)
359 #ifdef CONFIG_KVM_CLOCK
360 WARN_ON(kvm_register_clock("primary cpu clock"));
362 kvm_guest_cpu_init();
363 native_smp_prepare_boot_cpu();
366 static void __cpuinit
kvm_guest_cpu_online(void *dummy
)
368 kvm_guest_cpu_init();
371 static void kvm_guest_cpu_offline(void *dummy
)
373 kvm_disable_steal_time();
374 kvm_pv_disable_apf(NULL
);
378 static int __cpuinit
kvm_cpu_notify(struct notifier_block
*self
,
379 unsigned long action
, void *hcpu
)
381 int cpu
= (unsigned long)hcpu
;
384 case CPU_DOWN_FAILED
:
385 case CPU_ONLINE_FROZEN
:
386 smp_call_function_single(cpu
, kvm_guest_cpu_online
, NULL
, 0);
388 case CPU_DOWN_PREPARE
:
389 case CPU_DOWN_PREPARE_FROZEN
:
390 smp_call_function_single(cpu
, kvm_guest_cpu_offline
, NULL
, 1);
398 static struct notifier_block __cpuinitdata kvm_cpu_notifier
= {
399 .notifier_call
= kvm_cpu_notify
,
403 static void __init
kvm_apf_trap_init(void)
405 set_intr_gate(14, &async_page_fault
);
408 void __init
kvm_guest_init(void)
412 if (!kvm_para_available())
415 paravirt_ops_setup();
416 register_reboot_notifier(&kvm_pv_reboot_nb
);
417 for (i
= 0; i
< KVM_TASK_SLEEP_HASHSIZE
; i
++)
418 spin_lock_init(&async_pf_sleepers
[i
].lock
);
419 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF
))
420 x86_init
.irqs
.trap_init
= kvm_apf_trap_init
;
422 if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME
)) {
424 pv_time_ops
.steal_clock
= kvm_steal_clock
;
428 smp_ops
.smp_prepare_boot_cpu
= kvm_smp_prepare_boot_cpu
;
429 register_cpu_notifier(&kvm_cpu_notifier
);
431 kvm_guest_cpu_init();
435 static __init
int activate_jump_labels(void)
437 if (has_steal_clock
) {
438 static_key_slow_inc(¶virt_steal_enabled
);
440 static_key_slow_inc(¶virt_steal_rq_enabled
);
445 arch_initcall(activate_jump_labels
);