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> |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 32 | #include <asm/timer.h> |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 33 | #include <asm/cpu.h> |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 34 | |
| 35 | #define MMU_QUEUE_SIZE 1024 |
| 36 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 37 | static int kvmapf = 1; |
| 38 | |
| 39 | static int parse_no_kvmapf(char *arg) |
| 40 | { |
| 41 | kvmapf = 0; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | early_param("no-kvmapf", parse_no_kvmapf); |
| 46 | |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 47 | struct kvm_para_state { |
| 48 | u8 mmu_queue[MMU_QUEUE_SIZE]; |
| 49 | int mmu_queue_len; |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static DEFINE_PER_CPU(struct kvm_para_state, para_state); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 53 | static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64); |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 54 | |
| 55 | static struct kvm_para_state *kvm_para_state(void) |
| 56 | { |
| 57 | return &per_cpu(para_state, raw_smp_processor_id()); |
| 58 | } |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * No need for any "IO delay" on KVM |
| 62 | */ |
| 63 | static void kvm_io_delay(void) |
| 64 | { |
| 65 | } |
| 66 | |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 67 | static 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 Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 81 | static 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 | |
| 89 | static void kvm_deferred_mmu_op(void *buffer, int len) |
| 90 | { |
| 91 | struct kvm_para_state *state = kvm_para_state(); |
| 92 | |
Marcelo Tosatti | 6ba6617 | 2009-08-25 01:13:10 -0300 | [diff] [blame] | 93 | if (paravirt_get_lazy_mode() != PARAVIRT_LAZY_MMU) { |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 94 | 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 Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 103 | static 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 Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 123 | kvm_deferred_mmu_op(&wpte, sizeof wpte); |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 124 | } |
| 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 | */ |
| 132 | static void kvm_set_pte(pte_t *ptep, pte_t pte) |
| 133 | { |
| 134 | kvm_mmu_write(ptep, pte_val(pte)); |
| 135 | } |
| 136 | |
| 137 | static 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 | |
| 143 | static 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 |
| 150 | static void kvm_set_pte_atomic(pte_t *ptep, pte_t pte) |
| 151 | { |
| 152 | kvm_mmu_write(ptep, pte_val(pte)); |
| 153 | } |
| 154 | |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 155 | static void kvm_pte_clear(struct mm_struct *mm, |
| 156 | unsigned long addr, pte_t *ptep) |
| 157 | { |
| 158 | kvm_mmu_write(ptep, 0); |
| 159 | } |
| 160 | |
| 161 | static void kvm_pmd_clear(pmd_t *pmdp) |
| 162 | { |
| 163 | kvm_mmu_write(pmdp, 0); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | static 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 |
| 173 | static 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 | |
| 180 | static 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 Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 186 | kvm_deferred_mmu_op(&ftlb, sizeof ftlb); |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Eduardo Habkost | f863993 | 2008-07-30 18:32:27 -0300 | [diff] [blame] | 189 | static void kvm_release_pt(unsigned long pfn) |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 190 | { |
| 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 Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 199 | static void kvm_enter_lazy_mmu(void) |
| 200 | { |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 201 | paravirt_enter_lazy_mmu(); |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void kvm_leave_lazy_mmu(void) |
| 205 | { |
| 206 | struct kvm_para_state *state = kvm_para_state(); |
| 207 | |
| 208 | mmu_queue_flush(state); |
Jeremy Fitzhardinge | b407fc5 | 2009-02-17 23:46:21 -0800 | [diff] [blame] | 209 | paravirt_leave_lazy_mmu(); |
Marcelo Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Rakib Mullick | d3ac881 | 2009-07-02 11:40:36 +0600 | [diff] [blame] | 212 | static void __init paravirt_ops_setup(void) |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 213 | { |
| 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 Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 220 | 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 Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 227 | 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 Tosatti | 096d14a | 2008-02-22 12:21:38 -0500 | [diff] [blame] | 239 | |
| 240 | pv_mmu_ops.lazy_mode.enter = kvm_enter_lazy_mmu; |
| 241 | pv_mmu_ops.lazy_mode.leave = kvm_leave_lazy_mmu; |
Marcelo Tosatti | 1da8a77 | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 242 | } |
Marcelo Tosatti | a90ede7 | 2009-02-11 22:45:42 -0200 | [diff] [blame] | 243 | #ifdef CONFIG_X86_IO_APIC |
| 244 | no_timer_check = 1; |
| 245 | #endif |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 248 | void __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 | |
| 263 | static 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 | |
| 275 | static 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 | |
| 283 | static struct notifier_block kvm_pv_reboot_nb = { |
| 284 | .notifier_call = kvm_pv_reboot_notify, |
| 285 | }; |
| 286 | |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 287 | #ifdef CONFIG_SMP |
| 288 | static void __init kvm_smp_prepare_boot_cpu(void) |
| 289 | { |
| 290 | WARN_ON(kvm_register_clock("primary cpu clock")); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 291 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 292 | native_smp_prepare_boot_cpu(); |
| 293 | } |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 294 | |
| 295 | static void kvm_guest_cpu_online(void *dummy) |
| 296 | { |
| 297 | kvm_guest_cpu_init(); |
| 298 | } |
| 299 | |
| 300 | static void kvm_guest_cpu_offline(void *dummy) |
| 301 | { |
| 302 | kvm_pv_disable_apf(NULL); |
| 303 | } |
| 304 | |
| 305 | static 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 | |
| 325 | static struct notifier_block __cpuinitdata kvm_cpu_notifier = { |
| 326 | .notifier_call = kvm_cpu_notify, |
| 327 | }; |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 328 | #endif |
| 329 | |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 330 | void __init kvm_guest_init(void) |
| 331 | { |
| 332 | if (!kvm_para_available()) |
| 333 | return; |
| 334 | |
| 335 | paravirt_ops_setup(); |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 336 | register_reboot_notifier(&kvm_pv_reboot_nb); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 337 | #ifdef CONFIG_SMP |
| 338 | smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame^] | 339 | register_cpu_notifier(&kvm_cpu_notifier); |
| 340 | #else |
| 341 | kvm_guest_cpu_init(); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 342 | #endif |
Marcelo Tosatti | 0cf1bfd | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 343 | } |