blob: 032d03b6b54aaa01b0858a6d115c73ede759148a [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>
Marcelo Tosattia90ede72009-02-11 22:45:42 -020032#include <asm/timer.h>
Gleb Natapovfd10cde2010-10-14 11:22:51 +020033#include <asm/cpu.h>
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050034
35#define MMU_QUEUE_SIZE 1024
36
Gleb Natapovfd10cde2010-10-14 11:22:51 +020037static int kvmapf = 1;
38
39static int parse_no_kvmapf(char *arg)
40{
41 kvmapf = 0;
42 return 0;
43}
44
45early_param("no-kvmapf", parse_no_kvmapf);
46
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050047struct kvm_para_state {
48 u8 mmu_queue[MMU_QUEUE_SIZE];
49 int mmu_queue_len;
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050050};
51
52static DEFINE_PER_CPU(struct kvm_para_state, para_state);
Gleb Natapovfd10cde2010-10-14 11:22:51 +020053static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050054
55static struct kvm_para_state *kvm_para_state(void)
56{
57 return &per_cpu(para_state, raw_smp_processor_id());
58}
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -050059
60/*
61 * No need for any "IO delay" on KVM
62 */
63static void kvm_io_delay(void)
64{
65}
66
Marcelo Tosatti1da8a772008-02-22 12:21:37 -050067static void kvm_mmu_op(void *buffer, unsigned len)
68{
69 int r;
70 unsigned long a1, a2;
71
72 do {
73 a1 = __pa(buffer);
74 a2 = 0; /* on i386 __pa() always returns <4G */
75 r = kvm_hypercall3(KVM_HC_MMU_OP, len, a1, a2);
76 buffer += r;
77 len -= r;
78 } while (len);
79}
80
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050081static void mmu_queue_flush(struct kvm_para_state *state)
82{
83 if (state->mmu_queue_len) {
84 kvm_mmu_op(state->mmu_queue, state->mmu_queue_len);
85 state->mmu_queue_len = 0;
86 }
87}
88
89static void kvm_deferred_mmu_op(void *buffer, int len)
90{
91 struct kvm_para_state *state = kvm_para_state();
92
Marcelo Tosatti6ba66172009-08-25 01:13:10 -030093 if (paravirt_get_lazy_mode() != PARAVIRT_LAZY_MMU) {
Marcelo Tosatti096d14a2008-02-22 12:21:38 -050094 kvm_mmu_op(buffer, len);
95 return;
96 }
97 if (state->mmu_queue_len + len > sizeof state->mmu_queue)
98 mmu_queue_flush(state);
99 memcpy(state->mmu_queue + state->mmu_queue_len, buffer, len);
100 state->mmu_queue_len += len;
101}
102
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500103static void kvm_mmu_write(void *dest, u64 val)
104{
105 __u64 pte_phys;
106 struct kvm_mmu_op_write_pte wpte;
107
108#ifdef CONFIG_HIGHPTE
109 struct page *page;
110 unsigned long dst = (unsigned long) dest;
111
112 page = kmap_atomic_to_page(dest);
113 pte_phys = page_to_pfn(page);
114 pte_phys <<= PAGE_SHIFT;
115 pte_phys += (dst & ~(PAGE_MASK));
116#else
117 pte_phys = (unsigned long)__pa(dest);
118#endif
119 wpte.header.op = KVM_MMU_OP_WRITE_PTE;
120 wpte.pte_val = val;
121 wpte.pte_phys = pte_phys;
122
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500123 kvm_deferred_mmu_op(&wpte, sizeof wpte);
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500124}
125
126/*
127 * We only need to hook operations that are MMU writes. We hook these so that
128 * we can use lazy MMU mode to batch these operations. We could probably
129 * improve the performance of the host code if we used some of the information
130 * here to simplify processing of batched writes.
131 */
132static void kvm_set_pte(pte_t *ptep, pte_t pte)
133{
134 kvm_mmu_write(ptep, pte_val(pte));
135}
136
137static void kvm_set_pte_at(struct mm_struct *mm, unsigned long addr,
138 pte_t *ptep, pte_t pte)
139{
140 kvm_mmu_write(ptep, pte_val(pte));
141}
142
143static void kvm_set_pmd(pmd_t *pmdp, pmd_t pmd)
144{
145 kvm_mmu_write(pmdp, pmd_val(pmd));
146}
147
148#if PAGETABLE_LEVELS >= 3
149#ifdef CONFIG_X86_PAE
150static void kvm_set_pte_atomic(pte_t *ptep, pte_t pte)
151{
152 kvm_mmu_write(ptep, pte_val(pte));
153}
154
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500155static void kvm_pte_clear(struct mm_struct *mm,
156 unsigned long addr, pte_t *ptep)
157{
158 kvm_mmu_write(ptep, 0);
159}
160
161static void kvm_pmd_clear(pmd_t *pmdp)
162{
163 kvm_mmu_write(pmdp, 0);
164}
165#endif
166
167static void kvm_set_pud(pud_t *pudp, pud_t pud)
168{
169 kvm_mmu_write(pudp, pud_val(pud));
170}
171
172#if PAGETABLE_LEVELS == 4
173static void kvm_set_pgd(pgd_t *pgdp, pgd_t pgd)
174{
175 kvm_mmu_write(pgdp, pgd_val(pgd));
176}
177#endif
178#endif /* PAGETABLE_LEVELS >= 3 */
179
180static void kvm_flush_tlb(void)
181{
182 struct kvm_mmu_op_flush_tlb ftlb = {
183 .header.op = KVM_MMU_OP_FLUSH_TLB,
184 };
185
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500186 kvm_deferred_mmu_op(&ftlb, sizeof ftlb);
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500187}
188
Eduardo Habkostf8639932008-07-30 18:32:27 -0300189static void kvm_release_pt(unsigned long pfn)
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500190{
191 struct kvm_mmu_op_release_pt rpt = {
192 .header.op = KVM_MMU_OP_RELEASE_PT,
193 .pt_phys = (u64)pfn << PAGE_SHIFT,
194 };
195
196 kvm_mmu_op(&rpt, sizeof rpt);
197}
198
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500199static void kvm_enter_lazy_mmu(void)
200{
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500201 paravirt_enter_lazy_mmu();
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500202}
203
204static void kvm_leave_lazy_mmu(void)
205{
206 struct kvm_para_state *state = kvm_para_state();
207
208 mmu_queue_flush(state);
Jeremy Fitzhardingeb407fc52009-02-17 23:46:21 -0800209 paravirt_leave_lazy_mmu();
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500210}
211
Rakib Mullickd3ac8812009-07-02 11:40:36 +0600212static void __init paravirt_ops_setup(void)
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500213{
214 pv_info.name = "KVM";
215 pv_info.paravirt_enabled = 1;
216
217 if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
218 pv_cpu_ops.io_delay = kvm_io_delay;
219
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500220 if (kvm_para_has_feature(KVM_FEATURE_MMU_OP)) {
221 pv_mmu_ops.set_pte = kvm_set_pte;
222 pv_mmu_ops.set_pte_at = kvm_set_pte_at;
223 pv_mmu_ops.set_pmd = kvm_set_pmd;
224#if PAGETABLE_LEVELS >= 3
225#ifdef CONFIG_X86_PAE
226 pv_mmu_ops.set_pte_atomic = kvm_set_pte_atomic;
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500227 pv_mmu_ops.pte_clear = kvm_pte_clear;
228 pv_mmu_ops.pmd_clear = kvm_pmd_clear;
229#endif
230 pv_mmu_ops.set_pud = kvm_set_pud;
231#if PAGETABLE_LEVELS == 4
232 pv_mmu_ops.set_pgd = kvm_set_pgd;
233#endif
234#endif
235 pv_mmu_ops.flush_tlb_user = kvm_flush_tlb;
236 pv_mmu_ops.release_pte = kvm_release_pt;
237 pv_mmu_ops.release_pmd = kvm_release_pt;
238 pv_mmu_ops.release_pud = kvm_release_pt;
Marcelo Tosatti096d14a2008-02-22 12:21:38 -0500239
240 pv_mmu_ops.lazy_mode.enter = kvm_enter_lazy_mmu;
241 pv_mmu_ops.lazy_mode.leave = kvm_leave_lazy_mmu;
Marcelo Tosatti1da8a772008-02-22 12:21:37 -0500242 }
Marcelo Tosattia90ede72009-02-11 22:45:42 -0200243#ifdef CONFIG_X86_IO_APIC
244 no_timer_check = 1;
245#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500246}
247
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200248void __cpuinit kvm_guest_cpu_init(void)
249{
250 if (!kvm_para_available())
251 return;
252
253 if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
254 u64 pa = __pa(&__get_cpu_var(apf_reason));
255
256 wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
257 __get_cpu_var(apf_reason).enabled = 1;
258 printk(KERN_INFO"KVM setup async PF for cpu %d\n",
259 smp_processor_id());
260 }
261}
262
263static void kvm_pv_disable_apf(void *unused)
264{
265 if (!__get_cpu_var(apf_reason).enabled)
266 return;
267
268 wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
269 __get_cpu_var(apf_reason).enabled = 0;
270
271 printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
272 smp_processor_id());
273}
274
275static int kvm_pv_reboot_notify(struct notifier_block *nb,
276 unsigned long code, void *unused)
277{
278 if (code == SYS_RESTART)
279 on_each_cpu(kvm_pv_disable_apf, NULL, 1);
280 return NOTIFY_DONE;
281}
282
283static struct notifier_block kvm_pv_reboot_nb = {
284 .notifier_call = kvm_pv_reboot_notify,
285};
286
Gleb Natapovca3f1012010-10-14 11:22:49 +0200287#ifdef CONFIG_SMP
288static void __init kvm_smp_prepare_boot_cpu(void)
289{
290 WARN_ON(kvm_register_clock("primary cpu clock"));
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200291 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200292 native_smp_prepare_boot_cpu();
293}
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200294
295static void kvm_guest_cpu_online(void *dummy)
296{
297 kvm_guest_cpu_init();
298}
299
300static void kvm_guest_cpu_offline(void *dummy)
301{
302 kvm_pv_disable_apf(NULL);
303}
304
305static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
306 unsigned long action, void *hcpu)
307{
308 int cpu = (unsigned long)hcpu;
309 switch (action) {
310 case CPU_ONLINE:
311 case CPU_DOWN_FAILED:
312 case CPU_ONLINE_FROZEN:
313 smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
314 break;
315 case CPU_DOWN_PREPARE:
316 case CPU_DOWN_PREPARE_FROZEN:
317 smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
318 break;
319 default:
320 break;
321 }
322 return NOTIFY_OK;
323}
324
325static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
326 .notifier_call = kvm_cpu_notify,
327};
Gleb Natapovca3f1012010-10-14 11:22:49 +0200328#endif
329
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500330void __init kvm_guest_init(void)
331{
332 if (!kvm_para_available())
333 return;
334
335 paravirt_ops_setup();
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200336 register_reboot_notifier(&kvm_pv_reboot_nb);
Gleb Natapovca3f1012010-10-14 11:22:49 +0200337#ifdef CONFIG_SMP
338 smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
Gleb Natapovfd10cde2010-10-14 11:22:51 +0200339 register_cpu_notifier(&kvm_cpu_notifier);
340#else
341 kvm_guest_cpu_init();
Gleb Natapovca3f1012010-10-14 11:22:49 +0200342#endif
Marcelo Tosatti0cf1bfd2008-02-22 12:21:36 -0500343}