]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/kvm/x86.c
KVM: VMX: Avoid exit when setting cr8 if the local apic is in the kernel
[mirror_ubuntu-bionic-kernel.git] / drivers / kvm / x86.c
CommitLineData
043405e1
CO
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 *
8 * Authors:
9 * Avi Kivity <avi@qumranet.com>
10 * Yaniv Kamay <yaniv@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
16
313a3dc7 17#include "kvm.h"
043405e1 18#include "x86.h"
d825ed0a 19#include "x86_emulate.h"
5fb76f9b 20#include "segment_descriptor.h"
313a3dc7
CO
21#include "irq.h"
22
23#include <linux/kvm.h>
24#include <linux/fs.h>
25#include <linux/vmalloc.h>
5fb76f9b 26#include <linux/module.h>
0de10343 27#include <linux/mman.h>
043405e1
CO
28
29#include <asm/uaccess.h>
d825ed0a 30#include <asm/msr.h>
043405e1 31
313a3dc7 32#define MAX_IO_MSRS 256
a03490ed
CO
33#define CR0_RESERVED_BITS \
34 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
35 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
36 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
37#define CR4_RESERVED_BITS \
38 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
39 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
40 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
41 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
42
43#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
15c4a640 44#define EFER_RESERVED_BITS 0xfffffffffffff2fe
313a3dc7 45
ba1389b7
AK
46#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
417bc304 48
97896d04
ZX
49struct kvm_x86_ops *kvm_x86_ops;
50
417bc304 51struct kvm_stats_debugfs_item debugfs_entries[] = {
ba1389b7
AK
52 { "pf_fixed", VCPU_STAT(pf_fixed) },
53 { "pf_guest", VCPU_STAT(pf_guest) },
54 { "tlb_flush", VCPU_STAT(tlb_flush) },
55 { "invlpg", VCPU_STAT(invlpg) },
56 { "exits", VCPU_STAT(exits) },
57 { "io_exits", VCPU_STAT(io_exits) },
58 { "mmio_exits", VCPU_STAT(mmio_exits) },
59 { "signal_exits", VCPU_STAT(signal_exits) },
60 { "irq_window", VCPU_STAT(irq_window_exits) },
61 { "halt_exits", VCPU_STAT(halt_exits) },
62 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
63 { "request_irq", VCPU_STAT(request_irq_exits) },
64 { "irq_exits", VCPU_STAT(irq_exits) },
65 { "host_state_reload", VCPU_STAT(host_state_reload) },
66 { "efer_reload", VCPU_STAT(efer_reload) },
67 { "fpu_reload", VCPU_STAT(fpu_reload) },
68 { "insn_emulation", VCPU_STAT(insn_emulation) },
69 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
4cee5764
AK
70 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
71 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
72 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
73 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
74 { "mmu_flooded", VM_STAT(mmu_flooded) },
75 { "mmu_recycled", VM_STAT(mmu_recycled) },
0f74a24c 76 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
417bc304
HB
77 { NULL }
78};
79
80
5fb76f9b
CO
81unsigned long segment_base(u16 selector)
82{
83 struct descriptor_table gdt;
84 struct segment_descriptor *d;
85 unsigned long table_base;
86 unsigned long v;
87
88 if (selector == 0)
89 return 0;
90
91 asm("sgdt %0" : "=m"(gdt));
92 table_base = gdt.base;
93
94 if (selector & 4) { /* from ldt */
95 u16 ldt_selector;
96
97 asm("sldt %0" : "=g"(ldt_selector));
98 table_base = segment_base(ldt_selector);
99 }
100 d = (struct segment_descriptor *)(table_base + (selector & ~7));
101 v = d->base_low | ((unsigned long)d->base_mid << 16) |
102 ((unsigned long)d->base_high << 24);
103#ifdef CONFIG_X86_64
104 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
105 v |= ((unsigned long) \
106 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
107#endif
108 return v;
109}
110EXPORT_SYMBOL_GPL(segment_base);
111
6866b83e
CO
112u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
113{
114 if (irqchip_in_kernel(vcpu->kvm))
115 return vcpu->apic_base;
116 else
117 return vcpu->apic_base;
118}
119EXPORT_SYMBOL_GPL(kvm_get_apic_base);
120
121void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
122{
123 /* TODO: reserve bits check */
124 if (irqchip_in_kernel(vcpu->kvm))
125 kvm_lapic_set_base(vcpu, data);
126 else
127 vcpu->apic_base = data;
128}
129EXPORT_SYMBOL_GPL(kvm_set_apic_base);
130
298101da
AK
131void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
132{
133 WARN_ON(vcpu->exception.pending);
134 vcpu->exception.pending = true;
135 vcpu->exception.has_error_code = false;
136 vcpu->exception.nr = nr;
137}
138EXPORT_SYMBOL_GPL(kvm_queue_exception);
139
c3c91fee
AK
140void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
141 u32 error_code)
142{
143 ++vcpu->stat.pf_guest;
144 if (vcpu->exception.pending && vcpu->exception.nr == PF_VECTOR) {
145 printk(KERN_DEBUG "kvm: inject_page_fault:"
146 " double fault 0x%lx\n", addr);
147 vcpu->exception.nr = DF_VECTOR;
148 vcpu->exception.error_code = 0;
149 return;
150 }
151 vcpu->cr2 = addr;
152 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
153}
154
298101da
AK
155void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
156{
157 WARN_ON(vcpu->exception.pending);
158 vcpu->exception.pending = true;
159 vcpu->exception.has_error_code = true;
160 vcpu->exception.nr = nr;
161 vcpu->exception.error_code = error_code;
162}
163EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
164
165static void __queue_exception(struct kvm_vcpu *vcpu)
166{
167 kvm_x86_ops->queue_exception(vcpu, vcpu->exception.nr,
168 vcpu->exception.has_error_code,
169 vcpu->exception.error_code);
170}
171
a03490ed
CO
172/*
173 * Load the pae pdptrs. Return true is they are all valid.
174 */
175int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
176{
177 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
178 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
179 int i;
180 int ret;
181 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
182
183 mutex_lock(&vcpu->kvm->lock);
184 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
185 offset * sizeof(u64), sizeof(pdpte));
186 if (ret < 0) {
187 ret = 0;
188 goto out;
189 }
190 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
191 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
192 ret = 0;
193 goto out;
194 }
195 }
196 ret = 1;
197
198 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
199out:
200 mutex_unlock(&vcpu->kvm->lock);
201
202 return ret;
203}
204
d835dfec
AK
205static bool pdptrs_changed(struct kvm_vcpu *vcpu)
206{
207 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
208 bool changed = true;
209 int r;
210
211 if (is_long_mode(vcpu) || !is_pae(vcpu))
212 return false;
213
214 mutex_lock(&vcpu->kvm->lock);
215 r = kvm_read_guest(vcpu->kvm, vcpu->cr3 & ~31u, pdpte, sizeof(pdpte));
216 if (r < 0)
217 goto out;
218 changed = memcmp(pdpte, vcpu->pdptrs, sizeof(pdpte)) != 0;
219out:
220 mutex_unlock(&vcpu->kvm->lock);
221
222 return changed;
223}
224
a03490ed
CO
225void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
226{
227 if (cr0 & CR0_RESERVED_BITS) {
228 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
229 cr0, vcpu->cr0);
c1a5d4f9 230 kvm_inject_gp(vcpu, 0);
a03490ed
CO
231 return;
232 }
233
234 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
235 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
c1a5d4f9 236 kvm_inject_gp(vcpu, 0);
a03490ed
CO
237 return;
238 }
239
240 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
241 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
242 "and a clear PE flag\n");
c1a5d4f9 243 kvm_inject_gp(vcpu, 0);
a03490ed
CO
244 return;
245 }
246
247 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
248#ifdef CONFIG_X86_64
249 if ((vcpu->shadow_efer & EFER_LME)) {
250 int cs_db, cs_l;
251
252 if (!is_pae(vcpu)) {
253 printk(KERN_DEBUG "set_cr0: #GP, start paging "
254 "in long mode while PAE is disabled\n");
c1a5d4f9 255 kvm_inject_gp(vcpu, 0);
a03490ed
CO
256 return;
257 }
258 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
259 if (cs_l) {
260 printk(KERN_DEBUG "set_cr0: #GP, start paging "
261 "in long mode while CS.L == 1\n");
c1a5d4f9 262 kvm_inject_gp(vcpu, 0);
a03490ed
CO
263 return;
264
265 }
266 } else
267#endif
268 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
269 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
270 "reserved bits\n");
c1a5d4f9 271 kvm_inject_gp(vcpu, 0);
a03490ed
CO
272 return;
273 }
274
275 }
276
277 kvm_x86_ops->set_cr0(vcpu, cr0);
278 vcpu->cr0 = cr0;
279
280 mutex_lock(&vcpu->kvm->lock);
281 kvm_mmu_reset_context(vcpu);
282 mutex_unlock(&vcpu->kvm->lock);
283 return;
284}
285EXPORT_SYMBOL_GPL(set_cr0);
286
287void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
288{
289 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
290}
291EXPORT_SYMBOL_GPL(lmsw);
292
293void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
294{
295 if (cr4 & CR4_RESERVED_BITS) {
296 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
c1a5d4f9 297 kvm_inject_gp(vcpu, 0);
a03490ed
CO
298 return;
299 }
300
301 if (is_long_mode(vcpu)) {
302 if (!(cr4 & X86_CR4_PAE)) {
303 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
304 "in long mode\n");
c1a5d4f9 305 kvm_inject_gp(vcpu, 0);
a03490ed
CO
306 return;
307 }
308 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
309 && !load_pdptrs(vcpu, vcpu->cr3)) {
310 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
c1a5d4f9 311 kvm_inject_gp(vcpu, 0);
a03490ed
CO
312 return;
313 }
314
315 if (cr4 & X86_CR4_VMXE) {
316 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
c1a5d4f9 317 kvm_inject_gp(vcpu, 0);
a03490ed
CO
318 return;
319 }
320 kvm_x86_ops->set_cr4(vcpu, cr4);
321 vcpu->cr4 = cr4;
322 mutex_lock(&vcpu->kvm->lock);
323 kvm_mmu_reset_context(vcpu);
324 mutex_unlock(&vcpu->kvm->lock);
325}
326EXPORT_SYMBOL_GPL(set_cr4);
327
328void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
329{
d835dfec
AK
330 if (cr3 == vcpu->cr3 && !pdptrs_changed(vcpu)) {
331 kvm_mmu_flush_tlb(vcpu);
332 return;
333 }
334
a03490ed
CO
335 if (is_long_mode(vcpu)) {
336 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
337 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
c1a5d4f9 338 kvm_inject_gp(vcpu, 0);
a03490ed
CO
339 return;
340 }
341 } else {
342 if (is_pae(vcpu)) {
343 if (cr3 & CR3_PAE_RESERVED_BITS) {
344 printk(KERN_DEBUG
345 "set_cr3: #GP, reserved bits\n");
c1a5d4f9 346 kvm_inject_gp(vcpu, 0);
a03490ed
CO
347 return;
348 }
349 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
350 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
351 "reserved bits\n");
c1a5d4f9 352 kvm_inject_gp(vcpu, 0);
a03490ed
CO
353 return;
354 }
355 }
356 /*
357 * We don't check reserved bits in nonpae mode, because
358 * this isn't enforced, and VMware depends on this.
359 */
360 }
361
362 mutex_lock(&vcpu->kvm->lock);
363 /*
364 * Does the new cr3 value map to physical memory? (Note, we
365 * catch an invalid cr3 even in real-mode, because it would
366 * cause trouble later on when we turn on paging anyway.)
367 *
368 * A real CPU would silently accept an invalid cr3 and would
369 * attempt to use it - with largely undefined (and often hard
370 * to debug) behavior on the guest side.
371 */
372 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
c1a5d4f9 373 kvm_inject_gp(vcpu, 0);
a03490ed
CO
374 else {
375 vcpu->cr3 = cr3;
376 vcpu->mmu.new_cr3(vcpu);
377 }
378 mutex_unlock(&vcpu->kvm->lock);
379}
380EXPORT_SYMBOL_GPL(set_cr3);
381
382void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
383{
384 if (cr8 & CR8_RESERVED_BITS) {
385 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
c1a5d4f9 386 kvm_inject_gp(vcpu, 0);
a03490ed
CO
387 return;
388 }
389 if (irqchip_in_kernel(vcpu->kvm))
390 kvm_lapic_set_tpr(vcpu, cr8);
391 else
392 vcpu->cr8 = cr8;
393}
394EXPORT_SYMBOL_GPL(set_cr8);
395
396unsigned long get_cr8(struct kvm_vcpu *vcpu)
397{
398 if (irqchip_in_kernel(vcpu->kvm))
399 return kvm_lapic_get_cr8(vcpu);
400 else
401 return vcpu->cr8;
402}
403EXPORT_SYMBOL_GPL(get_cr8);
404
043405e1
CO
405/*
406 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
407 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
408 *
409 * This list is modified at module load time to reflect the
410 * capabilities of the host cpu.
411 */
412static u32 msrs_to_save[] = {
413 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
414 MSR_K6_STAR,
415#ifdef CONFIG_X86_64
416 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
417#endif
418 MSR_IA32_TIME_STAMP_COUNTER,
419};
420
421static unsigned num_msrs_to_save;
422
423static u32 emulated_msrs[] = {
424 MSR_IA32_MISC_ENABLE,
425};
426
15c4a640
CO
427#ifdef CONFIG_X86_64
428
429static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
430{
431 if (efer & EFER_RESERVED_BITS) {
432 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
433 efer);
c1a5d4f9 434 kvm_inject_gp(vcpu, 0);
15c4a640
CO
435 return;
436 }
437
438 if (is_paging(vcpu)
439 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
440 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
c1a5d4f9 441 kvm_inject_gp(vcpu, 0);
15c4a640
CO
442 return;
443 }
444
445 kvm_x86_ops->set_efer(vcpu, efer);
446
447 efer &= ~EFER_LMA;
448 efer |= vcpu->shadow_efer & EFER_LMA;
449
450 vcpu->shadow_efer = efer;
451}
452
453#endif
454
455/*
456 * Writes msr value into into the appropriate "register".
457 * Returns 0 on success, non-0 otherwise.
458 * Assumes vcpu_load() was already called.
459 */
460int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
461{
462 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
463}
464
313a3dc7
CO
465/*
466 * Adapt set_msr() to msr_io()'s calling convention
467 */
468static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
469{
470 return kvm_set_msr(vcpu, index, *data);
471}
472
15c4a640
CO
473
474int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
475{
476 switch (msr) {
477#ifdef CONFIG_X86_64
478 case MSR_EFER:
479 set_efer(vcpu, data);
480 break;
481#endif
482 case MSR_IA32_MC0_STATUS:
483 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
484 __FUNCTION__, data);
485 break;
486 case MSR_IA32_MCG_STATUS:
487 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
488 __FUNCTION__, data);
489 break;
490 case MSR_IA32_UCODE_REV:
491 case MSR_IA32_UCODE_WRITE:
492 case 0x200 ... 0x2ff: /* MTRRs */
493 break;
494 case MSR_IA32_APICBASE:
495 kvm_set_apic_base(vcpu, data);
496 break;
497 case MSR_IA32_MISC_ENABLE:
498 vcpu->ia32_misc_enable_msr = data;
499 break;
500 default:
501 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
502 return 1;
503 }
504 return 0;
505}
506EXPORT_SYMBOL_GPL(kvm_set_msr_common);
507
508
509/*
510 * Reads an msr value (of 'msr_index') into 'pdata'.
511 * Returns 0 on success, non-0 otherwise.
512 * Assumes vcpu_load() was already called.
513 */
514int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
515{
516 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
517}
518
519int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
520{
521 u64 data;
522
523 switch (msr) {
524 case 0xc0010010: /* SYSCFG */
525 case 0xc0010015: /* HWCR */
526 case MSR_IA32_PLATFORM_ID:
527 case MSR_IA32_P5_MC_ADDR:
528 case MSR_IA32_P5_MC_TYPE:
529 case MSR_IA32_MC0_CTL:
530 case MSR_IA32_MCG_STATUS:
531 case MSR_IA32_MCG_CAP:
532 case MSR_IA32_MC0_MISC:
533 case MSR_IA32_MC0_MISC+4:
534 case MSR_IA32_MC0_MISC+8:
535 case MSR_IA32_MC0_MISC+12:
536 case MSR_IA32_MC0_MISC+16:
537 case MSR_IA32_UCODE_REV:
538 case MSR_IA32_PERF_STATUS:
539 case MSR_IA32_EBL_CR_POWERON:
540 /* MTRR registers */
541 case 0xfe:
542 case 0x200 ... 0x2ff:
543 data = 0;
544 break;
545 case 0xcd: /* fsb frequency */
546 data = 3;
547 break;
548 case MSR_IA32_APICBASE:
549 data = kvm_get_apic_base(vcpu);
550 break;
551 case MSR_IA32_MISC_ENABLE:
552 data = vcpu->ia32_misc_enable_msr;
553 break;
554#ifdef CONFIG_X86_64
555 case MSR_EFER:
556 data = vcpu->shadow_efer;
557 break;
558#endif
559 default:
560 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
561 return 1;
562 }
563 *pdata = data;
564 return 0;
565}
566EXPORT_SYMBOL_GPL(kvm_get_msr_common);
567
313a3dc7
CO
568/*
569 * Read or write a bunch of msrs. All parameters are kernel addresses.
570 *
571 * @return number of msrs set successfully.
572 */
573static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
574 struct kvm_msr_entry *entries,
575 int (*do_msr)(struct kvm_vcpu *vcpu,
576 unsigned index, u64 *data))
577{
578 int i;
579
580 vcpu_load(vcpu);
581
582 for (i = 0; i < msrs->nmsrs; ++i)
583 if (do_msr(vcpu, entries[i].index, &entries[i].data))
584 break;
585
586 vcpu_put(vcpu);
587
588 return i;
589}
590
591/*
592 * Read or write a bunch of msrs. Parameters are user addresses.
593 *
594 * @return number of msrs set successfully.
595 */
596static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
597 int (*do_msr)(struct kvm_vcpu *vcpu,
598 unsigned index, u64 *data),
599 int writeback)
600{
601 struct kvm_msrs msrs;
602 struct kvm_msr_entry *entries;
603 int r, n;
604 unsigned size;
605
606 r = -EFAULT;
607 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
608 goto out;
609
610 r = -E2BIG;
611 if (msrs.nmsrs >= MAX_IO_MSRS)
612 goto out;
613
614 r = -ENOMEM;
615 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
616 entries = vmalloc(size);
617 if (!entries)
618 goto out;
619
620 r = -EFAULT;
621 if (copy_from_user(entries, user_msrs->entries, size))
622 goto out_free;
623
624 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
625 if (r < 0)
626 goto out_free;
627
628 r = -EFAULT;
629 if (writeback && copy_to_user(user_msrs->entries, entries, size))
630 goto out_free;
631
632 r = n;
633
634out_free:
635 vfree(entries);
636out:
637 return r;
638}
639
e9b11c17
ZX
640/*
641 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
642 * cached on it.
643 */
644void decache_vcpus_on_cpu(int cpu)
645{
646 struct kvm *vm;
647 struct kvm_vcpu *vcpu;
648 int i;
649
650 spin_lock(&kvm_lock);
651 list_for_each_entry(vm, &vm_list, vm_list)
652 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
653 vcpu = vm->vcpus[i];
654 if (!vcpu)
655 continue;
656 /*
657 * If the vcpu is locked, then it is running on some
658 * other cpu and therefore it is not cached on the
659 * cpu in question.
660 *
661 * If it's not locked, check the last cpu it executed
662 * on.
663 */
664 if (mutex_trylock(&vcpu->mutex)) {
665 if (vcpu->cpu == cpu) {
666 kvm_x86_ops->vcpu_decache(vcpu);
667 vcpu->cpu = -1;
668 }
669 mutex_unlock(&vcpu->mutex);
670 }
671 }
672 spin_unlock(&kvm_lock);
673}
674
018d00d2
ZX
675int kvm_dev_ioctl_check_extension(long ext)
676{
677 int r;
678
679 switch (ext) {
680 case KVM_CAP_IRQCHIP:
681 case KVM_CAP_HLT:
682 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
683 case KVM_CAP_USER_MEMORY:
684 case KVM_CAP_SET_TSS_ADDR:
07716717 685 case KVM_CAP_EXT_CPUID:
018d00d2
ZX
686 r = 1;
687 break;
688 default:
689 r = 0;
690 break;
691 }
692 return r;
693
694}
695
043405e1
CO
696long kvm_arch_dev_ioctl(struct file *filp,
697 unsigned int ioctl, unsigned long arg)
698{
699 void __user *argp = (void __user *)arg;
700 long r;
701
702 switch (ioctl) {
703 case KVM_GET_MSR_INDEX_LIST: {
704 struct kvm_msr_list __user *user_msr_list = argp;
705 struct kvm_msr_list msr_list;
706 unsigned n;
707
708 r = -EFAULT;
709 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
710 goto out;
711 n = msr_list.nmsrs;
712 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
713 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
714 goto out;
715 r = -E2BIG;
716 if (n < num_msrs_to_save)
717 goto out;
718 r = -EFAULT;
719 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
720 num_msrs_to_save * sizeof(u32)))
721 goto out;
722 if (copy_to_user(user_msr_list->indices
723 + num_msrs_to_save * sizeof(u32),
724 &emulated_msrs,
725 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
726 goto out;
727 r = 0;
728 break;
729 }
730 default:
731 r = -EINVAL;
732 }
733out:
734 return r;
735}
736
313a3dc7
CO
737void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
738{
739 kvm_x86_ops->vcpu_load(vcpu, cpu);
740}
741
742void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
743{
744 kvm_x86_ops->vcpu_put(vcpu);
9327fd11 745 kvm_put_guest_fpu(vcpu);
313a3dc7
CO
746}
747
07716717 748static int is_efer_nx(void)
313a3dc7
CO
749{
750 u64 efer;
313a3dc7
CO
751
752 rdmsrl(MSR_EFER, efer);
07716717
DK
753 return efer & EFER_NX;
754}
755
756static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
757{
758 int i;
759 struct kvm_cpuid_entry2 *e, *entry;
760
313a3dc7
CO
761 entry = NULL;
762 for (i = 0; i < vcpu->cpuid_nent; ++i) {
763 e = &vcpu->cpuid_entries[i];
764 if (e->function == 0x80000001) {
765 entry = e;
766 break;
767 }
768 }
07716717 769 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
313a3dc7
CO
770 entry->edx &= ~(1 << 20);
771 printk(KERN_INFO "kvm: guest NX capability removed\n");
772 }
773}
774
07716717 775/* when an old userspace process fills a new kernel module */
313a3dc7
CO
776static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
777 struct kvm_cpuid *cpuid,
778 struct kvm_cpuid_entry __user *entries)
07716717
DK
779{
780 int r, i;
781 struct kvm_cpuid_entry *cpuid_entries;
782
783 r = -E2BIG;
784 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
785 goto out;
786 r = -ENOMEM;
787 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
788 if (!cpuid_entries)
789 goto out;
790 r = -EFAULT;
791 if (copy_from_user(cpuid_entries, entries,
792 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
793 goto out_free;
794 for (i = 0; i < cpuid->nent; i++) {
795 vcpu->cpuid_entries[i].function = cpuid_entries[i].function;
796 vcpu->cpuid_entries[i].eax = cpuid_entries[i].eax;
797 vcpu->cpuid_entries[i].ebx = cpuid_entries[i].ebx;
798 vcpu->cpuid_entries[i].ecx = cpuid_entries[i].ecx;
799 vcpu->cpuid_entries[i].edx = cpuid_entries[i].edx;
800 vcpu->cpuid_entries[i].index = 0;
801 vcpu->cpuid_entries[i].flags = 0;
802 vcpu->cpuid_entries[i].padding[0] = 0;
803 vcpu->cpuid_entries[i].padding[1] = 0;
804 vcpu->cpuid_entries[i].padding[2] = 0;
805 }
806 vcpu->cpuid_nent = cpuid->nent;
807 cpuid_fix_nx_cap(vcpu);
808 r = 0;
809
810out_free:
811 vfree(cpuid_entries);
812out:
813 return r;
814}
815
816static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
817 struct kvm_cpuid2 *cpuid,
818 struct kvm_cpuid_entry2 __user *entries)
313a3dc7
CO
819{
820 int r;
821
822 r = -E2BIG;
823 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
824 goto out;
825 r = -EFAULT;
826 if (copy_from_user(&vcpu->cpuid_entries, entries,
07716717 827 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
313a3dc7
CO
828 goto out;
829 vcpu->cpuid_nent = cpuid->nent;
313a3dc7
CO
830 return 0;
831
832out:
833 return r;
834}
835
07716717
DK
836static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
837 struct kvm_cpuid2 *cpuid,
838 struct kvm_cpuid_entry2 __user *entries)
839{
840 int r;
841
842 r = -E2BIG;
843 if (cpuid->nent < vcpu->cpuid_nent)
844 goto out;
845 r = -EFAULT;
846 if (copy_to_user(entries, &vcpu->cpuid_entries,
847 vcpu->cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
848 goto out;
849 return 0;
850
851out:
852 cpuid->nent = vcpu->cpuid_nent;
853 return r;
854}
855
856static inline u32 bit(int bitno)
857{
858 return 1 << (bitno & 31);
859}
860
861static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
862 u32 index)
863{
864 entry->function = function;
865 entry->index = index;
866 cpuid_count(entry->function, entry->index,
867 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
868 entry->flags = 0;
869}
870
871static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
872 u32 index, int *nent, int maxnent)
873{
874 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
875 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
876 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
877 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
878 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
879 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
880 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
881 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
882 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
883 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
884 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
885 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
886 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
887 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
888 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
889 bit(X86_FEATURE_PGE) |
890 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
891 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
892 bit(X86_FEATURE_SYSCALL) |
893 (bit(X86_FEATURE_NX) && is_efer_nx()) |
894#ifdef CONFIG_X86_64
895 bit(X86_FEATURE_LM) |
896#endif
897 bit(X86_FEATURE_MMXEXT) |
898 bit(X86_FEATURE_3DNOWEXT) |
899 bit(X86_FEATURE_3DNOW);
900 const u32 kvm_supported_word3_x86_features =
901 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
902 const u32 kvm_supported_word6_x86_features =
903 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
904
905 /* all func 2 cpuid_count() should be called on the same cpu */
906 get_cpu();
907 do_cpuid_1_ent(entry, function, index);
908 ++*nent;
909
910 switch (function) {
911 case 0:
912 entry->eax = min(entry->eax, (u32)0xb);
913 break;
914 case 1:
915 entry->edx &= kvm_supported_word0_x86_features;
916 entry->ecx &= kvm_supported_word3_x86_features;
917 break;
918 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
919 * may return different values. This forces us to get_cpu() before
920 * issuing the first command, and also to emulate this annoying behavior
921 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
922 case 2: {
923 int t, times = entry->eax & 0xff;
924
925 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
926 for (t = 1; t < times && *nent < maxnent; ++t) {
927 do_cpuid_1_ent(&entry[t], function, 0);
928 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
929 ++*nent;
930 }
931 break;
932 }
933 /* function 4 and 0xb have additional index. */
934 case 4: {
935 int index, cache_type;
936
937 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
938 /* read more entries until cache_type is zero */
939 for (index = 1; *nent < maxnent; ++index) {
940 cache_type = entry[index - 1].eax & 0x1f;
941 if (!cache_type)
942 break;
943 do_cpuid_1_ent(&entry[index], function, index);
944 entry[index].flags |=
945 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
946 ++*nent;
947 }
948 break;
949 }
950 case 0xb: {
951 int index, level_type;
952
953 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
954 /* read more entries until level_type is zero */
955 for (index = 1; *nent < maxnent; ++index) {
956 level_type = entry[index - 1].ecx & 0xff;
957 if (!level_type)
958 break;
959 do_cpuid_1_ent(&entry[index], function, index);
960 entry[index].flags |=
961 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
962 ++*nent;
963 }
964 break;
965 }
966 case 0x80000000:
967 entry->eax = min(entry->eax, 0x8000001a);
968 break;
969 case 0x80000001:
970 entry->edx &= kvm_supported_word1_x86_features;
971 entry->ecx &= kvm_supported_word6_x86_features;
972 break;
973 }
974 put_cpu();
975}
976
977static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm,
978 struct kvm_cpuid2 *cpuid,
979 struct kvm_cpuid_entry2 __user *entries)
980{
981 struct kvm_cpuid_entry2 *cpuid_entries;
982 int limit, nent = 0, r = -E2BIG;
983 u32 func;
984
985 if (cpuid->nent < 1)
986 goto out;
987 r = -ENOMEM;
988 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
989 if (!cpuid_entries)
990 goto out;
991
992 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
993 limit = cpuid_entries[0].eax;
994 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
995 do_cpuid_ent(&cpuid_entries[nent], func, 0,
996 &nent, cpuid->nent);
997 r = -E2BIG;
998 if (nent >= cpuid->nent)
999 goto out_free;
1000
1001 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1002 limit = cpuid_entries[nent - 1].eax;
1003 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1004 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1005 &nent, cpuid->nent);
1006 r = -EFAULT;
1007 if (copy_to_user(entries, cpuid_entries,
1008 nent * sizeof(struct kvm_cpuid_entry2)))
1009 goto out_free;
1010 cpuid->nent = nent;
1011 r = 0;
1012
1013out_free:
1014 vfree(cpuid_entries);
1015out:
1016 return r;
1017}
1018
313a3dc7
CO
1019static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1020 struct kvm_lapic_state *s)
1021{
1022 vcpu_load(vcpu);
1023 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
1024 vcpu_put(vcpu);
1025
1026 return 0;
1027}
1028
1029static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1030 struct kvm_lapic_state *s)
1031{
1032 vcpu_load(vcpu);
1033 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
1034 kvm_apic_post_state_restore(vcpu);
1035 vcpu_put(vcpu);
1036
1037 return 0;
1038}
1039
f77bc6a4
ZX
1040static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1041 struct kvm_interrupt *irq)
1042{
1043 if (irq->irq < 0 || irq->irq >= 256)
1044 return -EINVAL;
1045 if (irqchip_in_kernel(vcpu->kvm))
1046 return -ENXIO;
1047 vcpu_load(vcpu);
1048
1049 set_bit(irq->irq, vcpu->irq_pending);
1050 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1051
1052 vcpu_put(vcpu);
1053
1054 return 0;
1055}
1056
313a3dc7
CO
1057long kvm_arch_vcpu_ioctl(struct file *filp,
1058 unsigned int ioctl, unsigned long arg)
1059{
1060 struct kvm_vcpu *vcpu = filp->private_data;
1061 void __user *argp = (void __user *)arg;
1062 int r;
1063
1064 switch (ioctl) {
1065 case KVM_GET_LAPIC: {
1066 struct kvm_lapic_state lapic;
1067
1068 memset(&lapic, 0, sizeof lapic);
1069 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1070 if (r)
1071 goto out;
1072 r = -EFAULT;
1073 if (copy_to_user(argp, &lapic, sizeof lapic))
1074 goto out;
1075 r = 0;
1076 break;
1077 }
1078 case KVM_SET_LAPIC: {
1079 struct kvm_lapic_state lapic;
1080
1081 r = -EFAULT;
1082 if (copy_from_user(&lapic, argp, sizeof lapic))
1083 goto out;
1084 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1085 if (r)
1086 goto out;
1087 r = 0;
1088 break;
1089 }
f77bc6a4
ZX
1090 case KVM_INTERRUPT: {
1091 struct kvm_interrupt irq;
1092
1093 r = -EFAULT;
1094 if (copy_from_user(&irq, argp, sizeof irq))
1095 goto out;
1096 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1097 if (r)
1098 goto out;
1099 r = 0;
1100 break;
1101 }
313a3dc7
CO
1102 case KVM_SET_CPUID: {
1103 struct kvm_cpuid __user *cpuid_arg = argp;
1104 struct kvm_cpuid cpuid;
1105
1106 r = -EFAULT;
1107 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1108 goto out;
1109 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1110 if (r)
1111 goto out;
1112 break;
1113 }
07716717
DK
1114 case KVM_SET_CPUID2: {
1115 struct kvm_cpuid2 __user *cpuid_arg = argp;
1116 struct kvm_cpuid2 cpuid;
1117
1118 r = -EFAULT;
1119 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1120 goto out;
1121 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1122 cpuid_arg->entries);
1123 if (r)
1124 goto out;
1125 break;
1126 }
1127 case KVM_GET_CPUID2: {
1128 struct kvm_cpuid2 __user *cpuid_arg = argp;
1129 struct kvm_cpuid2 cpuid;
1130
1131 r = -EFAULT;
1132 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1133 goto out;
1134 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1135 cpuid_arg->entries);
1136 if (r)
1137 goto out;
1138 r = -EFAULT;
1139 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1140 goto out;
1141 r = 0;
1142 break;
1143 }
313a3dc7
CO
1144 case KVM_GET_MSRS:
1145 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1146 break;
1147 case KVM_SET_MSRS:
1148 r = msr_io(vcpu, argp, do_set_msr, 0);
1149 break;
1150 default:
1151 r = -EINVAL;
1152 }
1153out:
1154 return r;
1155}
1156
1fe779f8
CO
1157static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1158{
1159 int ret;
1160
1161 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1162 return -1;
1163 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1164 return ret;
1165}
1166
1167static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1168 u32 kvm_nr_mmu_pages)
1169{
1170 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1171 return -EINVAL;
1172
1173 mutex_lock(&kvm->lock);
1174
1175 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1176 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
1177
1178 mutex_unlock(&kvm->lock);
1179 return 0;
1180}
1181
1182static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1183{
1184 return kvm->n_alloc_mmu_pages;
1185}
1186
e9f85cde
ZX
1187gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1188{
1189 int i;
1190 struct kvm_mem_alias *alias;
1191
1192 for (i = 0; i < kvm->naliases; ++i) {
1193 alias = &kvm->aliases[i];
1194 if (gfn >= alias->base_gfn
1195 && gfn < alias->base_gfn + alias->npages)
1196 return alias->target_gfn + gfn - alias->base_gfn;
1197 }
1198 return gfn;
1199}
1200
1fe779f8
CO
1201/*
1202 * Set a new alias region. Aliases map a portion of physical memory into
1203 * another portion. This is useful for memory windows, for example the PC
1204 * VGA region.
1205 */
1206static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1207 struct kvm_memory_alias *alias)
1208{
1209 int r, n;
1210 struct kvm_mem_alias *p;
1211
1212 r = -EINVAL;
1213 /* General sanity checks */
1214 if (alias->memory_size & (PAGE_SIZE - 1))
1215 goto out;
1216 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1217 goto out;
1218 if (alias->slot >= KVM_ALIAS_SLOTS)
1219 goto out;
1220 if (alias->guest_phys_addr + alias->memory_size
1221 < alias->guest_phys_addr)
1222 goto out;
1223 if (alias->target_phys_addr + alias->memory_size
1224 < alias->target_phys_addr)
1225 goto out;
1226
1227 mutex_lock(&kvm->lock);
1228
1229 p = &kvm->aliases[alias->slot];
1230 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1231 p->npages = alias->memory_size >> PAGE_SHIFT;
1232 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1233
1234 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1235 if (kvm->aliases[n - 1].npages)
1236 break;
1237 kvm->naliases = n;
1238
1239 kvm_mmu_zap_all(kvm);
1240
1241 mutex_unlock(&kvm->lock);
1242
1243 return 0;
1244
1245out:
1246 return r;
1247}
1248
1249static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1250{
1251 int r;
1252
1253 r = 0;
1254 switch (chip->chip_id) {
1255 case KVM_IRQCHIP_PIC_MASTER:
1256 memcpy(&chip->chip.pic,
1257 &pic_irqchip(kvm)->pics[0],
1258 sizeof(struct kvm_pic_state));
1259 break;
1260 case KVM_IRQCHIP_PIC_SLAVE:
1261 memcpy(&chip->chip.pic,
1262 &pic_irqchip(kvm)->pics[1],
1263 sizeof(struct kvm_pic_state));
1264 break;
1265 case KVM_IRQCHIP_IOAPIC:
1266 memcpy(&chip->chip.ioapic,
1267 ioapic_irqchip(kvm),
1268 sizeof(struct kvm_ioapic_state));
1269 break;
1270 default:
1271 r = -EINVAL;
1272 break;
1273 }
1274 return r;
1275}
1276
1277static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1278{
1279 int r;
1280
1281 r = 0;
1282 switch (chip->chip_id) {
1283 case KVM_IRQCHIP_PIC_MASTER:
1284 memcpy(&pic_irqchip(kvm)->pics[0],
1285 &chip->chip.pic,
1286 sizeof(struct kvm_pic_state));
1287 break;
1288 case KVM_IRQCHIP_PIC_SLAVE:
1289 memcpy(&pic_irqchip(kvm)->pics[1],
1290 &chip->chip.pic,
1291 sizeof(struct kvm_pic_state));
1292 break;
1293 case KVM_IRQCHIP_IOAPIC:
1294 memcpy(ioapic_irqchip(kvm),
1295 &chip->chip.ioapic,
1296 sizeof(struct kvm_ioapic_state));
1297 break;
1298 default:
1299 r = -EINVAL;
1300 break;
1301 }
1302 kvm_pic_update_irq(pic_irqchip(kvm));
1303 return r;
1304}
1305
5bb064dc
ZX
1306/*
1307 * Get (and clear) the dirty memory log for a memory slot.
1308 */
1309int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1310 struct kvm_dirty_log *log)
1311{
1312 int r;
1313 int n;
1314 struct kvm_memory_slot *memslot;
1315 int is_dirty = 0;
1316
1317 mutex_lock(&kvm->lock);
1318
1319 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1320 if (r)
1321 goto out;
1322
1323 /* If nothing is dirty, don't bother messing with page tables. */
1324 if (is_dirty) {
1325 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1326 kvm_flush_remote_tlbs(kvm);
1327 memslot = &kvm->memslots[log->slot];
1328 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1329 memset(memslot->dirty_bitmap, 0, n);
1330 }
1331 r = 0;
1332out:
1333 mutex_unlock(&kvm->lock);
1334 return r;
1335}
1336
1fe779f8
CO
1337long kvm_arch_vm_ioctl(struct file *filp,
1338 unsigned int ioctl, unsigned long arg)
1339{
1340 struct kvm *kvm = filp->private_data;
1341 void __user *argp = (void __user *)arg;
1342 int r = -EINVAL;
1343
1344 switch (ioctl) {
1345 case KVM_SET_TSS_ADDR:
1346 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1347 if (r < 0)
1348 goto out;
1349 break;
1350 case KVM_SET_MEMORY_REGION: {
1351 struct kvm_memory_region kvm_mem;
1352 struct kvm_userspace_memory_region kvm_userspace_mem;
1353
1354 r = -EFAULT;
1355 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1356 goto out;
1357 kvm_userspace_mem.slot = kvm_mem.slot;
1358 kvm_userspace_mem.flags = kvm_mem.flags;
1359 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1360 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1361 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1362 if (r)
1363 goto out;
1364 break;
1365 }
1366 case KVM_SET_NR_MMU_PAGES:
1367 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1368 if (r)
1369 goto out;
1370 break;
1371 case KVM_GET_NR_MMU_PAGES:
1372 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1373 break;
1374 case KVM_SET_MEMORY_ALIAS: {
1375 struct kvm_memory_alias alias;
1376
1377 r = -EFAULT;
1378 if (copy_from_user(&alias, argp, sizeof alias))
1379 goto out;
1380 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1381 if (r)
1382 goto out;
1383 break;
1384 }
1385 case KVM_CREATE_IRQCHIP:
1386 r = -ENOMEM;
1387 kvm->vpic = kvm_create_pic(kvm);
1388 if (kvm->vpic) {
1389 r = kvm_ioapic_init(kvm);
1390 if (r) {
1391 kfree(kvm->vpic);
1392 kvm->vpic = NULL;
1393 goto out;
1394 }
1395 } else
1396 goto out;
1397 break;
1398 case KVM_IRQ_LINE: {
1399 struct kvm_irq_level irq_event;
1400
1401 r = -EFAULT;
1402 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1403 goto out;
1404 if (irqchip_in_kernel(kvm)) {
1405 mutex_lock(&kvm->lock);
1406 if (irq_event.irq < 16)
1407 kvm_pic_set_irq(pic_irqchip(kvm),
1408 irq_event.irq,
1409 irq_event.level);
1410 kvm_ioapic_set_irq(kvm->vioapic,
1411 irq_event.irq,
1412 irq_event.level);
1413 mutex_unlock(&kvm->lock);
1414 r = 0;
1415 }
1416 break;
1417 }
1418 case KVM_GET_IRQCHIP: {
1419 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1420 struct kvm_irqchip chip;
1421
1422 r = -EFAULT;
1423 if (copy_from_user(&chip, argp, sizeof chip))
1424 goto out;
1425 r = -ENXIO;
1426 if (!irqchip_in_kernel(kvm))
1427 goto out;
1428 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1429 if (r)
1430 goto out;
1431 r = -EFAULT;
1432 if (copy_to_user(argp, &chip, sizeof chip))
1433 goto out;
1434 r = 0;
1435 break;
1436 }
1437 case KVM_SET_IRQCHIP: {
1438 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1439 struct kvm_irqchip chip;
1440
1441 r = -EFAULT;
1442 if (copy_from_user(&chip, argp, sizeof chip))
1443 goto out;
1444 r = -ENXIO;
1445 if (!irqchip_in_kernel(kvm))
1446 goto out;
1447 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1448 if (r)
1449 goto out;
1450 r = 0;
1451 break;
1452 }
07716717
DK
1453 case KVM_GET_SUPPORTED_CPUID: {
1454 struct kvm_cpuid2 __user *cpuid_arg = argp;
1455 struct kvm_cpuid2 cpuid;
1456
1457 r = -EFAULT;
1458 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1459 goto out;
1460 r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid,
1461 cpuid_arg->entries);
1462 if (r)
1463 goto out;
1464
1465 r = -EFAULT;
1466 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1467 goto out;
1468 r = 0;
1469 break;
1470 }
1fe779f8
CO
1471 default:
1472 ;
1473 }
1474out:
1475 return r;
1476}
1477
a16b043c 1478static void kvm_init_msr_list(void)
043405e1
CO
1479{
1480 u32 dummy[2];
1481 unsigned i, j;
1482
1483 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1484 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1485 continue;
1486 if (j < i)
1487 msrs_to_save[j] = msrs_to_save[i];
1488 j++;
1489 }
1490 num_msrs_to_save = j;
1491}
1492
bbd9b64e
CO
1493/*
1494 * Only apic need an MMIO device hook, so shortcut now..
1495 */
1496static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1497 gpa_t addr)
1498{
1499 struct kvm_io_device *dev;
1500
1501 if (vcpu->apic) {
1502 dev = &vcpu->apic->dev;
1503 if (dev->in_range(dev, addr))
1504 return dev;
1505 }
1506 return NULL;
1507}
1508
1509
1510static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1511 gpa_t addr)
1512{
1513 struct kvm_io_device *dev;
1514
1515 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1516 if (dev == NULL)
1517 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1518 return dev;
1519}
1520
1521int emulator_read_std(unsigned long addr,
1522 void *val,
1523 unsigned int bytes,
1524 struct kvm_vcpu *vcpu)
1525{
1526 void *data = val;
1527
1528 while (bytes) {
1529 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1530 unsigned offset = addr & (PAGE_SIZE-1);
1531 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1532 int ret;
1533
1534 if (gpa == UNMAPPED_GVA)
1535 return X86EMUL_PROPAGATE_FAULT;
1536 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1537 if (ret < 0)
1538 return X86EMUL_UNHANDLEABLE;
1539
1540 bytes -= tocopy;
1541 data += tocopy;
1542 addr += tocopy;
1543 }
1544
1545 return X86EMUL_CONTINUE;
1546}
1547EXPORT_SYMBOL_GPL(emulator_read_std);
1548
bbd9b64e
CO
1549static int emulator_read_emulated(unsigned long addr,
1550 void *val,
1551 unsigned int bytes,
1552 struct kvm_vcpu *vcpu)
1553{
1554 struct kvm_io_device *mmio_dev;
1555 gpa_t gpa;
1556
1557 if (vcpu->mmio_read_completed) {
1558 memcpy(val, vcpu->mmio_data, bytes);
1559 vcpu->mmio_read_completed = 0;
1560 return X86EMUL_CONTINUE;
1561 }
1562
1563 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1564
1565 /* For APIC access vmexit */
1566 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1567 goto mmio;
1568
1569 if (emulator_read_std(addr, val, bytes, vcpu)
1570 == X86EMUL_CONTINUE)
1571 return X86EMUL_CONTINUE;
1572 if (gpa == UNMAPPED_GVA)
1573 return X86EMUL_PROPAGATE_FAULT;
1574
1575mmio:
1576 /*
1577 * Is this MMIO handled locally?
1578 */
1579 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1580 if (mmio_dev) {
1581 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1582 return X86EMUL_CONTINUE;
1583 }
1584
1585 vcpu->mmio_needed = 1;
1586 vcpu->mmio_phys_addr = gpa;
1587 vcpu->mmio_size = bytes;
1588 vcpu->mmio_is_write = 0;
1589
1590 return X86EMUL_UNHANDLEABLE;
1591}
1592
1593static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1594 const void *val, int bytes)
1595{
1596 int ret;
1597
1598 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1599 if (ret < 0)
1600 return 0;
1601 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1602 return 1;
1603}
1604
1605static int emulator_write_emulated_onepage(unsigned long addr,
1606 const void *val,
1607 unsigned int bytes,
1608 struct kvm_vcpu *vcpu)
1609{
1610 struct kvm_io_device *mmio_dev;
1611 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1612
1613 if (gpa == UNMAPPED_GVA) {
c3c91fee 1614 kvm_inject_page_fault(vcpu, addr, 2);
bbd9b64e
CO
1615 return X86EMUL_PROPAGATE_FAULT;
1616 }
1617
1618 /* For APIC access vmexit */
1619 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1620 goto mmio;
1621
1622 if (emulator_write_phys(vcpu, gpa, val, bytes))
1623 return X86EMUL_CONTINUE;
1624
1625mmio:
1626 /*
1627 * Is this MMIO handled locally?
1628 */
1629 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1630 if (mmio_dev) {
1631 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1632 return X86EMUL_CONTINUE;
1633 }
1634
1635 vcpu->mmio_needed = 1;
1636 vcpu->mmio_phys_addr = gpa;
1637 vcpu->mmio_size = bytes;
1638 vcpu->mmio_is_write = 1;
1639 memcpy(vcpu->mmio_data, val, bytes);
1640
1641 return X86EMUL_CONTINUE;
1642}
1643
1644int emulator_write_emulated(unsigned long addr,
1645 const void *val,
1646 unsigned int bytes,
1647 struct kvm_vcpu *vcpu)
1648{
1649 /* Crossing a page boundary? */
1650 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1651 int rc, now;
1652
1653 now = -addr & ~PAGE_MASK;
1654 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1655 if (rc != X86EMUL_CONTINUE)
1656 return rc;
1657 addr += now;
1658 val += now;
1659 bytes -= now;
1660 }
1661 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1662}
1663EXPORT_SYMBOL_GPL(emulator_write_emulated);
1664
1665static int emulator_cmpxchg_emulated(unsigned long addr,
1666 const void *old,
1667 const void *new,
1668 unsigned int bytes,
1669 struct kvm_vcpu *vcpu)
1670{
1671 static int reported;
1672
1673 if (!reported) {
1674 reported = 1;
1675 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1676 }
1677 return emulator_write_emulated(addr, new, bytes, vcpu);
1678}
1679
1680static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1681{
1682 return kvm_x86_ops->get_segment_base(vcpu, seg);
1683}
1684
1685int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1686{
1687 return X86EMUL_CONTINUE;
1688}
1689
1690int emulate_clts(struct kvm_vcpu *vcpu)
1691{
1692 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
1693 return X86EMUL_CONTINUE;
1694}
1695
1696int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1697{
1698 struct kvm_vcpu *vcpu = ctxt->vcpu;
1699
1700 switch (dr) {
1701 case 0 ... 3:
1702 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1703 return X86EMUL_CONTINUE;
1704 default:
1705 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1706 return X86EMUL_UNHANDLEABLE;
1707 }
1708}
1709
1710int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1711{
1712 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1713 int exception;
1714
1715 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1716 if (exception) {
1717 /* FIXME: better handling */
1718 return X86EMUL_UNHANDLEABLE;
1719 }
1720 return X86EMUL_CONTINUE;
1721}
1722
1723void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1724{
1725 static int reported;
1726 u8 opcodes[4];
1727 unsigned long rip = vcpu->rip;
1728 unsigned long rip_linear;
1729
1730 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1731
1732 if (reported)
1733 return;
1734
1735 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1736
1737 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1738 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1739 reported = 1;
1740}
1741EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1742
1743struct x86_emulate_ops emulate_ops = {
1744 .read_std = emulator_read_std,
bbd9b64e
CO
1745 .read_emulated = emulator_read_emulated,
1746 .write_emulated = emulator_write_emulated,
1747 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1748};
1749
1750int emulate_instruction(struct kvm_vcpu *vcpu,
1751 struct kvm_run *run,
1752 unsigned long cr2,
1753 u16 error_code,
1754 int no_decode)
1755{
1756 int r;
1757
1758 vcpu->mmio_fault_cr2 = cr2;
1759 kvm_x86_ops->cache_regs(vcpu);
1760
1761 vcpu->mmio_is_write = 0;
1762 vcpu->pio.string = 0;
1763
1764 if (!no_decode) {
1765 int cs_db, cs_l;
1766 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1767
1768 vcpu->emulate_ctxt.vcpu = vcpu;
1769 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
bbd9b64e
CO
1770 vcpu->emulate_ctxt.mode =
1771 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1772 ? X86EMUL_MODE_REAL : cs_l
1773 ? X86EMUL_MODE_PROT64 : cs_db
1774 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1775
1776 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1777 vcpu->emulate_ctxt.cs_base = 0;
1778 vcpu->emulate_ctxt.ds_base = 0;
1779 vcpu->emulate_ctxt.es_base = 0;
1780 vcpu->emulate_ctxt.ss_base = 0;
1781 } else {
1782 vcpu->emulate_ctxt.cs_base =
1783 get_segment_base(vcpu, VCPU_SREG_CS);
1784 vcpu->emulate_ctxt.ds_base =
1785 get_segment_base(vcpu, VCPU_SREG_DS);
1786 vcpu->emulate_ctxt.es_base =
1787 get_segment_base(vcpu, VCPU_SREG_ES);
1788 vcpu->emulate_ctxt.ss_base =
1789 get_segment_base(vcpu, VCPU_SREG_SS);
1790 }
1791
1792 vcpu->emulate_ctxt.gs_base =
1793 get_segment_base(vcpu, VCPU_SREG_GS);
1794 vcpu->emulate_ctxt.fs_base =
1795 get_segment_base(vcpu, VCPU_SREG_FS);
1796
1797 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
f2b5756b 1798 ++vcpu->stat.insn_emulation;
bbd9b64e 1799 if (r) {
f2b5756b 1800 ++vcpu->stat.insn_emulation_fail;
bbd9b64e
CO
1801 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1802 return EMULATE_DONE;
1803 return EMULATE_FAIL;
1804 }
1805 }
1806
1807 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
1808
1809 if (vcpu->pio.string)
1810 return EMULATE_DO_MMIO;
1811
1812 if ((r || vcpu->mmio_is_write) && run) {
1813 run->exit_reason = KVM_EXIT_MMIO;
1814 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1815 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1816 run->mmio.len = vcpu->mmio_size;
1817 run->mmio.is_write = vcpu->mmio_is_write;
1818 }
1819
1820 if (r) {
1821 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1822 return EMULATE_DONE;
1823 if (!vcpu->mmio_needed) {
1824 kvm_report_emulation_failure(vcpu, "mmio");
1825 return EMULATE_FAIL;
1826 }
1827 return EMULATE_DO_MMIO;
1828 }
1829
1830 kvm_x86_ops->decache_regs(vcpu);
1831 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
1832
1833 if (vcpu->mmio_is_write) {
1834 vcpu->mmio_needed = 0;
1835 return EMULATE_DO_MMIO;
1836 }
1837
1838 return EMULATE_DONE;
1839}
1840EXPORT_SYMBOL_GPL(emulate_instruction);
1841
de7d789a
CO
1842static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
1843{
1844 int i;
1845
1846 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
1847 if (vcpu->pio.guest_pages[i]) {
b4231d61 1848 kvm_release_page_dirty(vcpu->pio.guest_pages[i]);
de7d789a
CO
1849 vcpu->pio.guest_pages[i] = NULL;
1850 }
1851}
1852
1853static int pio_copy_data(struct kvm_vcpu *vcpu)
1854{
1855 void *p = vcpu->pio_data;
1856 void *q;
1857 unsigned bytes;
1858 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1859
1860 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1861 PAGE_KERNEL);
1862 if (!q) {
1863 free_pio_guest_pages(vcpu);
1864 return -ENOMEM;
1865 }
1866 q += vcpu->pio.guest_page_offset;
1867 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1868 if (vcpu->pio.in)
1869 memcpy(q, p, bytes);
1870 else
1871 memcpy(p, q, bytes);
1872 q -= vcpu->pio.guest_page_offset;
1873 vunmap(q);
1874 free_pio_guest_pages(vcpu);
1875 return 0;
1876}
1877
1878int complete_pio(struct kvm_vcpu *vcpu)
1879{
1880 struct kvm_pio_request *io = &vcpu->pio;
1881 long delta;
1882 int r;
1883
1884 kvm_x86_ops->cache_regs(vcpu);
1885
1886 if (!io->string) {
1887 if (io->in)
1888 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1889 io->size);
1890 } else {
1891 if (io->in) {
1892 r = pio_copy_data(vcpu);
1893 if (r) {
1894 kvm_x86_ops->cache_regs(vcpu);
1895 return r;
1896 }
1897 }
1898
1899 delta = 1;
1900 if (io->rep) {
1901 delta *= io->cur_count;
1902 /*
1903 * The size of the register should really depend on
1904 * current address size.
1905 */
1906 vcpu->regs[VCPU_REGS_RCX] -= delta;
1907 }
1908 if (io->down)
1909 delta = -delta;
1910 delta *= io->size;
1911 if (io->in)
1912 vcpu->regs[VCPU_REGS_RDI] += delta;
1913 else
1914 vcpu->regs[VCPU_REGS_RSI] += delta;
1915 }
1916
1917 kvm_x86_ops->decache_regs(vcpu);
1918
1919 io->count -= io->cur_count;
1920 io->cur_count = 0;
1921
1922 return 0;
1923}
1924
1925static void kernel_pio(struct kvm_io_device *pio_dev,
1926 struct kvm_vcpu *vcpu,
1927 void *pd)
1928{
1929 /* TODO: String I/O for in kernel device */
1930
1931 mutex_lock(&vcpu->kvm->lock);
1932 if (vcpu->pio.in)
1933 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1934 vcpu->pio.size,
1935 pd);
1936 else
1937 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1938 vcpu->pio.size,
1939 pd);
1940 mutex_unlock(&vcpu->kvm->lock);
1941}
1942
1943static void pio_string_write(struct kvm_io_device *pio_dev,
1944 struct kvm_vcpu *vcpu)
1945{
1946 struct kvm_pio_request *io = &vcpu->pio;
1947 void *pd = vcpu->pio_data;
1948 int i;
1949
1950 mutex_lock(&vcpu->kvm->lock);
1951 for (i = 0; i < io->cur_count; i++) {
1952 kvm_iodevice_write(pio_dev, io->port,
1953 io->size,
1954 pd);
1955 pd += io->size;
1956 }
1957 mutex_unlock(&vcpu->kvm->lock);
1958}
1959
1960static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1961 gpa_t addr)
1962{
1963 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1964}
1965
1966int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1967 int size, unsigned port)
1968{
1969 struct kvm_io_device *pio_dev;
1970
1971 vcpu->run->exit_reason = KVM_EXIT_IO;
1972 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1973 vcpu->run->io.size = vcpu->pio.size = size;
1974 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1975 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1976 vcpu->run->io.port = vcpu->pio.port = port;
1977 vcpu->pio.in = in;
1978 vcpu->pio.string = 0;
1979 vcpu->pio.down = 0;
1980 vcpu->pio.guest_page_offset = 0;
1981 vcpu->pio.rep = 0;
1982
1983 kvm_x86_ops->cache_regs(vcpu);
1984 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1985 kvm_x86_ops->decache_regs(vcpu);
1986
1987 kvm_x86_ops->skip_emulated_instruction(vcpu);
1988
1989 pio_dev = vcpu_find_pio_dev(vcpu, port);
1990 if (pio_dev) {
1991 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1992 complete_pio(vcpu);
1993 return 1;
1994 }
1995 return 0;
1996}
1997EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1998
1999int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2000 int size, unsigned long count, int down,
2001 gva_t address, int rep, unsigned port)
2002{
2003 unsigned now, in_page;
2004 int i, ret = 0;
2005 int nr_pages = 1;
2006 struct page *page;
2007 struct kvm_io_device *pio_dev;
2008
2009 vcpu->run->exit_reason = KVM_EXIT_IO;
2010 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2011 vcpu->run->io.size = vcpu->pio.size = size;
2012 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2013 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2014 vcpu->run->io.port = vcpu->pio.port = port;
2015 vcpu->pio.in = in;
2016 vcpu->pio.string = 1;
2017 vcpu->pio.down = down;
2018 vcpu->pio.guest_page_offset = offset_in_page(address);
2019 vcpu->pio.rep = rep;
2020
2021 if (!count) {
2022 kvm_x86_ops->skip_emulated_instruction(vcpu);
2023 return 1;
2024 }
2025
2026 if (!down)
2027 in_page = PAGE_SIZE - offset_in_page(address);
2028 else
2029 in_page = offset_in_page(address) + size;
2030 now = min(count, (unsigned long)in_page / size);
2031 if (!now) {
2032 /*
2033 * String I/O straddles page boundary. Pin two guest pages
2034 * so that we satisfy atomicity constraints. Do just one
2035 * transaction to avoid complexity.
2036 */
2037 nr_pages = 2;
2038 now = 1;
2039 }
2040 if (down) {
2041 /*
2042 * String I/O in reverse. Yuck. Kill the guest, fix later.
2043 */
2044 pr_unimpl(vcpu, "guest string pio down\n");
c1a5d4f9 2045 kvm_inject_gp(vcpu, 0);
de7d789a
CO
2046 return 1;
2047 }
2048 vcpu->run->io.count = now;
2049 vcpu->pio.cur_count = now;
2050
2051 if (vcpu->pio.cur_count == vcpu->pio.count)
2052 kvm_x86_ops->skip_emulated_instruction(vcpu);
2053
2054 for (i = 0; i < nr_pages; ++i) {
2055 mutex_lock(&vcpu->kvm->lock);
2056 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2057 vcpu->pio.guest_pages[i] = page;
2058 mutex_unlock(&vcpu->kvm->lock);
2059 if (!page) {
c1a5d4f9 2060 kvm_inject_gp(vcpu, 0);
de7d789a
CO
2061 free_pio_guest_pages(vcpu);
2062 return 1;
2063 }
2064 }
2065
2066 pio_dev = vcpu_find_pio_dev(vcpu, port);
2067 if (!vcpu->pio.in) {
2068 /* string PIO write */
2069 ret = pio_copy_data(vcpu);
2070 if (ret >= 0 && pio_dev) {
2071 pio_string_write(pio_dev, vcpu);
2072 complete_pio(vcpu);
2073 if (vcpu->pio.count == 0)
2074 ret = 1;
2075 }
2076 } else if (pio_dev)
2077 pr_unimpl(vcpu, "no string pio read support yet, "
2078 "port %x size %d count %ld\n",
2079 port, size, count);
2080
2081 return ret;
2082}
2083EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2084
f8c16bba 2085int kvm_arch_init(void *opaque)
043405e1 2086{
56c6d28a 2087 int r;
f8c16bba
ZX
2088 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2089
56c6d28a
ZX
2090 r = kvm_mmu_module_init();
2091 if (r)
2092 goto out_fail;
2093
043405e1 2094 kvm_init_msr_list();
f8c16bba
ZX
2095
2096 if (kvm_x86_ops) {
2097 printk(KERN_ERR "kvm: already loaded the other module\n");
56c6d28a
ZX
2098 r = -EEXIST;
2099 goto out;
f8c16bba
ZX
2100 }
2101
2102 if (!ops->cpu_has_kvm_support()) {
2103 printk(KERN_ERR "kvm: no hardware support\n");
56c6d28a
ZX
2104 r = -EOPNOTSUPP;
2105 goto out;
f8c16bba
ZX
2106 }
2107 if (ops->disabled_by_bios()) {
2108 printk(KERN_ERR "kvm: disabled by bios\n");
56c6d28a
ZX
2109 r = -EOPNOTSUPP;
2110 goto out;
f8c16bba
ZX
2111 }
2112
2113 kvm_x86_ops = ops;
56c6d28a 2114 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
f8c16bba 2115 return 0;
56c6d28a
ZX
2116
2117out:
2118 kvm_mmu_module_exit();
2119out_fail:
2120 return r;
043405e1 2121}
8776e519 2122
f8c16bba
ZX
2123void kvm_arch_exit(void)
2124{
2125 kvm_x86_ops = NULL;
56c6d28a
ZX
2126 kvm_mmu_module_exit();
2127}
f8c16bba 2128
8776e519
HB
2129int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2130{
2131 ++vcpu->stat.halt_exits;
2132 if (irqchip_in_kernel(vcpu->kvm)) {
2133 vcpu->mp_state = VCPU_MP_STATE_HALTED;
2134 kvm_vcpu_block(vcpu);
2135 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
2136 return -EINTR;
2137 return 1;
2138 } else {
2139 vcpu->run->exit_reason = KVM_EXIT_HLT;
2140 return 0;
2141 }
2142}
2143EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2144
2145int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2146{
2147 unsigned long nr, a0, a1, a2, a3, ret;
2148
2149 kvm_x86_ops->cache_regs(vcpu);
2150
2151 nr = vcpu->regs[VCPU_REGS_RAX];
2152 a0 = vcpu->regs[VCPU_REGS_RBX];
2153 a1 = vcpu->regs[VCPU_REGS_RCX];
2154 a2 = vcpu->regs[VCPU_REGS_RDX];
2155 a3 = vcpu->regs[VCPU_REGS_RSI];
2156
2157 if (!is_long_mode(vcpu)) {
2158 nr &= 0xFFFFFFFF;
2159 a0 &= 0xFFFFFFFF;
2160 a1 &= 0xFFFFFFFF;
2161 a2 &= 0xFFFFFFFF;
2162 a3 &= 0xFFFFFFFF;
2163 }
2164
2165 switch (nr) {
2166 default:
2167 ret = -KVM_ENOSYS;
2168 break;
2169 }
2170 vcpu->regs[VCPU_REGS_RAX] = ret;
2171 kvm_x86_ops->decache_regs(vcpu);
2172 return 0;
2173}
2174EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2175
2176int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2177{
2178 char instruction[3];
2179 int ret = 0;
2180
2181 mutex_lock(&vcpu->kvm->lock);
2182
2183 /*
2184 * Blow out the MMU to ensure that no other VCPU has an active mapping
2185 * to ensure that the updated hypercall appears atomically across all
2186 * VCPUs.
2187 */
2188 kvm_mmu_zap_all(vcpu->kvm);
2189
2190 kvm_x86_ops->cache_regs(vcpu);
2191 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2192 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
2193 != X86EMUL_CONTINUE)
2194 ret = -EFAULT;
2195
2196 mutex_unlock(&vcpu->kvm->lock);
2197
2198 return ret;
2199}
2200
2201static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2202{
2203 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2204}
2205
2206void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2207{
2208 struct descriptor_table dt = { limit, base };
2209
2210 kvm_x86_ops->set_gdt(vcpu, &dt);
2211}
2212
2213void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2214{
2215 struct descriptor_table dt = { limit, base };
2216
2217 kvm_x86_ops->set_idt(vcpu, &dt);
2218}
2219
2220void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2221 unsigned long *rflags)
2222{
2223 lmsw(vcpu, msw);
2224 *rflags = kvm_x86_ops->get_rflags(vcpu);
2225}
2226
2227unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2228{
2229 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2230 switch (cr) {
2231 case 0:
2232 return vcpu->cr0;
2233 case 2:
2234 return vcpu->cr2;
2235 case 3:
2236 return vcpu->cr3;
2237 case 4:
2238 return vcpu->cr4;
2239 default:
2240 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2241 return 0;
2242 }
2243}
2244
2245void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2246 unsigned long *rflags)
2247{
2248 switch (cr) {
2249 case 0:
2250 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
2251 *rflags = kvm_x86_ops->get_rflags(vcpu);
2252 break;
2253 case 2:
2254 vcpu->cr2 = val;
2255 break;
2256 case 3:
2257 set_cr3(vcpu, val);
2258 break;
2259 case 4:
2260 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
2261 break;
2262 default:
2263 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2264 }
2265}
2266
07716717
DK
2267static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2268{
2269 struct kvm_cpuid_entry2 *e = &vcpu->cpuid_entries[i];
2270 int j, nent = vcpu->cpuid_nent;
2271
2272 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2273 /* when no next entry is found, the current entry[i] is reselected */
2274 for (j = i + 1; j == i; j = (j + 1) % nent) {
2275 struct kvm_cpuid_entry2 *ej = &vcpu->cpuid_entries[j];
2276 if (ej->function == e->function) {
2277 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2278 return j;
2279 }
2280 }
2281 return 0; /* silence gcc, even though control never reaches here */
2282}
2283
2284/* find an entry with matching function, matching index (if needed), and that
2285 * should be read next (if it's stateful) */
2286static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2287 u32 function, u32 index)
2288{
2289 if (e->function != function)
2290 return 0;
2291 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2292 return 0;
2293 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2294 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2295 return 0;
2296 return 1;
2297}
2298
8776e519
HB
2299void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2300{
2301 int i;
07716717
DK
2302 u32 function, index;
2303 struct kvm_cpuid_entry2 *e, *best;
8776e519
HB
2304
2305 kvm_x86_ops->cache_regs(vcpu);
2306 function = vcpu->regs[VCPU_REGS_RAX];
07716717 2307 index = vcpu->regs[VCPU_REGS_RCX];
8776e519
HB
2308 vcpu->regs[VCPU_REGS_RAX] = 0;
2309 vcpu->regs[VCPU_REGS_RBX] = 0;
2310 vcpu->regs[VCPU_REGS_RCX] = 0;
2311 vcpu->regs[VCPU_REGS_RDX] = 0;
2312 best = NULL;
2313 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2314 e = &vcpu->cpuid_entries[i];
07716717
DK
2315 if (is_matching_cpuid_entry(e, function, index)) {
2316 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2317 move_to_next_stateful_cpuid_entry(vcpu, i);
8776e519
HB
2318 best = e;
2319 break;
2320 }
2321 /*
2322 * Both basic or both extended?
2323 */
2324 if (((e->function ^ function) & 0x80000000) == 0)
2325 if (!best || e->function > best->function)
2326 best = e;
2327 }
2328 if (best) {
2329 vcpu->regs[VCPU_REGS_RAX] = best->eax;
2330 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
2331 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
2332 vcpu->regs[VCPU_REGS_RDX] = best->edx;
2333 }
2334 kvm_x86_ops->decache_regs(vcpu);
2335 kvm_x86_ops->skip_emulated_instruction(vcpu);
2336}
2337EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
d0752060 2338
b6c7a5dc
HB
2339/*
2340 * Check if userspace requested an interrupt window, and that the
2341 * interrupt window is open.
2342 *
2343 * No need to exit to userspace if we already have an interrupt queued.
2344 */
2345static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2346 struct kvm_run *kvm_run)
2347{
2348 return (!vcpu->irq_summary &&
2349 kvm_run->request_interrupt_window &&
2350 vcpu->interrupt_window_open &&
2351 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2352}
2353
2354static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2355 struct kvm_run *kvm_run)
2356{
2357 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2358 kvm_run->cr8 = get_cr8(vcpu);
2359 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2360 if (irqchip_in_kernel(vcpu->kvm))
2361 kvm_run->ready_for_interrupt_injection = 1;
2362 else
2363 kvm_run->ready_for_interrupt_injection =
2364 (vcpu->interrupt_window_open &&
2365 vcpu->irq_summary == 0);
2366}
2367
2368static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2369{
2370 int r;
2371
2372 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2373 pr_debug("vcpu %d received sipi with vector # %x\n",
2374 vcpu->vcpu_id, vcpu->sipi_vector);
2375 kvm_lapic_reset(vcpu);
2376 r = kvm_x86_ops->vcpu_reset(vcpu);
2377 if (r)
2378 return r;
2379 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2380 }
2381
2382preempted:
2383 if (vcpu->guest_debug.enabled)
2384 kvm_x86_ops->guest_debug_pre(vcpu);
2385
2386again:
2387 r = kvm_mmu_reload(vcpu);
2388 if (unlikely(r))
2389 goto out;
2390
2391 kvm_inject_pending_timer_irqs(vcpu);
2392
2393 preempt_disable();
2394
2395 kvm_x86_ops->prepare_guest_switch(vcpu);
2396 kvm_load_guest_fpu(vcpu);
2397
2398 local_irq_disable();
2399
2400 if (signal_pending(current)) {
2401 local_irq_enable();
2402 preempt_enable();
2403 r = -EINTR;
2404 kvm_run->exit_reason = KVM_EXIT_INTR;
2405 ++vcpu->stat.signal_exits;
2406 goto out;
2407 }
2408
298101da
AK
2409 if (vcpu->exception.pending)
2410 __queue_exception(vcpu);
2411 else if (irqchip_in_kernel(vcpu->kvm))
b6c7a5dc 2412 kvm_x86_ops->inject_pending_irq(vcpu);
eb9774f0 2413 else
b6c7a5dc
HB
2414 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2415
2416 vcpu->guest_mode = 1;
2417 kvm_guest_enter();
2418
2419 if (vcpu->requests)
2420 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2421 kvm_x86_ops->tlb_flush(vcpu);
2422
2423 kvm_x86_ops->run(vcpu, kvm_run);
2424
2425 vcpu->guest_mode = 0;
2426 local_irq_enable();
2427
2428 ++vcpu->stat.exits;
2429
2430 /*
2431 * We must have an instruction between local_irq_enable() and
2432 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2433 * the interrupt shadow. The stat.exits increment will do nicely.
2434 * But we need to prevent reordering, hence this barrier():
2435 */
2436 barrier();
2437
2438 kvm_guest_exit();
2439
2440 preempt_enable();
2441
2442 /*
2443 * Profile KVM exit RIPs:
2444 */
2445 if (unlikely(prof_on == KVM_PROFILING)) {
2446 kvm_x86_ops->cache_regs(vcpu);
2447 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2448 }
2449
298101da
AK
2450 if (vcpu->exception.pending && kvm_x86_ops->exception_injected(vcpu))
2451 vcpu->exception.pending = false;
2452
b6c7a5dc
HB
2453 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2454
2455 if (r > 0) {
2456 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2457 r = -EINTR;
2458 kvm_run->exit_reason = KVM_EXIT_INTR;
2459 ++vcpu->stat.request_irq_exits;
2460 goto out;
2461 }
e1beb1d3 2462 if (!need_resched())
b6c7a5dc 2463 goto again;
b6c7a5dc
HB
2464 }
2465
2466out:
2467 if (r > 0) {
2468 kvm_resched(vcpu);
2469 goto preempted;
2470 }
2471
2472 post_kvm_run_save(vcpu, kvm_run);
2473
2474 return r;
2475}
2476
2477int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2478{
2479 int r;
2480 sigset_t sigsaved;
2481
2482 vcpu_load(vcpu);
2483
2484 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2485 kvm_vcpu_block(vcpu);
2486 vcpu_put(vcpu);
2487 return -EAGAIN;
2488 }
2489
2490 if (vcpu->sigset_active)
2491 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2492
2493 /* re-sync apic's tpr */
2494 if (!irqchip_in_kernel(vcpu->kvm))
2495 set_cr8(vcpu, kvm_run->cr8);
2496
2497 if (vcpu->pio.cur_count) {
2498 r = complete_pio(vcpu);
2499 if (r)
2500 goto out;
2501 }
2502#if CONFIG_HAS_IOMEM
2503 if (vcpu->mmio_needed) {
2504 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2505 vcpu->mmio_read_completed = 1;
2506 vcpu->mmio_needed = 0;
2507 r = emulate_instruction(vcpu, kvm_run,
2508 vcpu->mmio_fault_cr2, 0, 1);
2509 if (r == EMULATE_DO_MMIO) {
2510 /*
2511 * Read-modify-write. Back to userspace.
2512 */
2513 r = 0;
2514 goto out;
2515 }
2516 }
2517#endif
2518 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2519 kvm_x86_ops->cache_regs(vcpu);
2520 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2521 kvm_x86_ops->decache_regs(vcpu);
2522 }
2523
2524 r = __vcpu_run(vcpu, kvm_run);
2525
2526out:
2527 if (vcpu->sigset_active)
2528 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2529
2530 vcpu_put(vcpu);
2531 return r;
2532}
2533
2534int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2535{
2536 vcpu_load(vcpu);
2537
2538 kvm_x86_ops->cache_regs(vcpu);
2539
2540 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2541 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2542 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2543 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2544 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2545 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2546 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2547 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
2548#ifdef CONFIG_X86_64
2549 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2550 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2551 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2552 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2553 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2554 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2555 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2556 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2557#endif
2558
2559 regs->rip = vcpu->rip;
2560 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2561
2562 /*
2563 * Don't leak debug flags in case they were set for guest debugging
2564 */
2565 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2566 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2567
2568 vcpu_put(vcpu);
2569
2570 return 0;
2571}
2572
2573int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2574{
2575 vcpu_load(vcpu);
2576
2577 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2578 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2579 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2580 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2581 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2582 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2583 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2584 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
2585#ifdef CONFIG_X86_64
2586 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2587 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2588 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2589 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2590 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2591 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2592 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2593 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2594#endif
2595
2596 vcpu->rip = regs->rip;
2597 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2598
2599 kvm_x86_ops->decache_regs(vcpu);
2600
2601 vcpu_put(vcpu);
2602
2603 return 0;
2604}
2605
2606static void get_segment(struct kvm_vcpu *vcpu,
2607 struct kvm_segment *var, int seg)
2608{
2609 return kvm_x86_ops->get_segment(vcpu, var, seg);
2610}
2611
2612void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2613{
2614 struct kvm_segment cs;
2615
2616 get_segment(vcpu, &cs, VCPU_SREG_CS);
2617 *db = cs.db;
2618 *l = cs.l;
2619}
2620EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2621
2622int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2623 struct kvm_sregs *sregs)
2624{
2625 struct descriptor_table dt;
2626 int pending_vec;
2627
2628 vcpu_load(vcpu);
2629
2630 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2631 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2632 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2633 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2634 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2635 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2636
2637 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2638 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2639
2640 kvm_x86_ops->get_idt(vcpu, &dt);
2641 sregs->idt.limit = dt.limit;
2642 sregs->idt.base = dt.base;
2643 kvm_x86_ops->get_gdt(vcpu, &dt);
2644 sregs->gdt.limit = dt.limit;
2645 sregs->gdt.base = dt.base;
2646
2647 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2648 sregs->cr0 = vcpu->cr0;
2649 sregs->cr2 = vcpu->cr2;
2650 sregs->cr3 = vcpu->cr3;
2651 sregs->cr4 = vcpu->cr4;
2652 sregs->cr8 = get_cr8(vcpu);
2653 sregs->efer = vcpu->shadow_efer;
2654 sregs->apic_base = kvm_get_apic_base(vcpu);
2655
2656 if (irqchip_in_kernel(vcpu->kvm)) {
2657 memset(sregs->interrupt_bitmap, 0,
2658 sizeof sregs->interrupt_bitmap);
2659 pending_vec = kvm_x86_ops->get_irq(vcpu);
2660 if (pending_vec >= 0)
2661 set_bit(pending_vec,
2662 (unsigned long *)sregs->interrupt_bitmap);
2663 } else
2664 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2665 sizeof sregs->interrupt_bitmap);
2666
2667 vcpu_put(vcpu);
2668
2669 return 0;
2670}
2671
2672static void set_segment(struct kvm_vcpu *vcpu,
2673 struct kvm_segment *var, int seg)
2674{
2675 return kvm_x86_ops->set_segment(vcpu, var, seg);
2676}
2677
2678int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2679 struct kvm_sregs *sregs)
2680{
2681 int mmu_reset_needed = 0;
2682 int i, pending_vec, max_bits;
2683 struct descriptor_table dt;
2684
2685 vcpu_load(vcpu);
2686
2687 dt.limit = sregs->idt.limit;
2688 dt.base = sregs->idt.base;
2689 kvm_x86_ops->set_idt(vcpu, &dt);
2690 dt.limit = sregs->gdt.limit;
2691 dt.base = sregs->gdt.base;
2692 kvm_x86_ops->set_gdt(vcpu, &dt);
2693
2694 vcpu->cr2 = sregs->cr2;
2695 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2696 vcpu->cr3 = sregs->cr3;
2697
2698 set_cr8(vcpu, sregs->cr8);
2699
2700 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2701#ifdef CONFIG_X86_64
2702 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2703#endif
2704 kvm_set_apic_base(vcpu, sregs->apic_base);
2705
2706 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2707
2708 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2709 vcpu->cr0 = sregs->cr0;
2710 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2711
2712 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2713 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2714 if (!is_long_mode(vcpu) && is_pae(vcpu))
2715 load_pdptrs(vcpu, vcpu->cr3);
2716
2717 if (mmu_reset_needed)
2718 kvm_mmu_reset_context(vcpu);
2719
2720 if (!irqchip_in_kernel(vcpu->kvm)) {
2721 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2722 sizeof vcpu->irq_pending);
2723 vcpu->irq_summary = 0;
2724 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2725 if (vcpu->irq_pending[i])
2726 __set_bit(i, &vcpu->irq_summary);
2727 } else {
2728 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2729 pending_vec = find_first_bit(
2730 (const unsigned long *)sregs->interrupt_bitmap,
2731 max_bits);
2732 /* Only pending external irq is handled here */
2733 if (pending_vec < max_bits) {
2734 kvm_x86_ops->set_irq(vcpu, pending_vec);
2735 pr_debug("Set back pending irq %d\n",
2736 pending_vec);
2737 }
2738 }
2739
2740 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2741 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2742 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2743 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2744 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2745 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2746
2747 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2748 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2749
2750 vcpu_put(vcpu);
2751
2752 return 0;
2753}
2754
2755int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2756 struct kvm_debug_guest *dbg)
2757{
2758 int r;
2759
2760 vcpu_load(vcpu);
2761
2762 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2763
2764 vcpu_put(vcpu);
2765
2766 return r;
2767}
2768
d0752060
HB
2769/*
2770 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2771 * we have asm/x86/processor.h
2772 */
2773struct fxsave {
2774 u16 cwd;
2775 u16 swd;
2776 u16 twd;
2777 u16 fop;
2778 u64 rip;
2779 u64 rdp;
2780 u32 mxcsr;
2781 u32 mxcsr_mask;
2782 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2783#ifdef CONFIG_X86_64
2784 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2785#else
2786 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2787#endif
2788};
2789
8b006791
ZX
2790/*
2791 * Translate a guest virtual address to a guest physical address.
2792 */
2793int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2794 struct kvm_translation *tr)
2795{
2796 unsigned long vaddr = tr->linear_address;
2797 gpa_t gpa;
2798
2799 vcpu_load(vcpu);
2800 mutex_lock(&vcpu->kvm->lock);
2801 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2802 tr->physical_address = gpa;
2803 tr->valid = gpa != UNMAPPED_GVA;
2804 tr->writeable = 1;
2805 tr->usermode = 0;
2806 mutex_unlock(&vcpu->kvm->lock);
2807 vcpu_put(vcpu);
2808
2809 return 0;
2810}
2811
d0752060
HB
2812int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2813{
2814 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2815
2816 vcpu_load(vcpu);
2817
2818 memcpy(fpu->fpr, fxsave->st_space, 128);
2819 fpu->fcw = fxsave->cwd;
2820 fpu->fsw = fxsave->swd;
2821 fpu->ftwx = fxsave->twd;
2822 fpu->last_opcode = fxsave->fop;
2823 fpu->last_ip = fxsave->rip;
2824 fpu->last_dp = fxsave->rdp;
2825 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2826
2827 vcpu_put(vcpu);
2828
2829 return 0;
2830}
2831
2832int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2833{
2834 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
2835
2836 vcpu_load(vcpu);
2837
2838 memcpy(fxsave->st_space, fpu->fpr, 128);
2839 fxsave->cwd = fpu->fcw;
2840 fxsave->swd = fpu->fsw;
2841 fxsave->twd = fpu->ftwx;
2842 fxsave->fop = fpu->last_opcode;
2843 fxsave->rip = fpu->last_ip;
2844 fxsave->rdp = fpu->last_dp;
2845 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2846
2847 vcpu_put(vcpu);
2848
2849 return 0;
2850}
2851
2852void fx_init(struct kvm_vcpu *vcpu)
2853{
2854 unsigned after_mxcsr_mask;
2855
2856 /* Initialize guest FPU by resetting ours and saving into guest's */
2857 preempt_disable();
2858 fx_save(&vcpu->host_fx_image);
2859 fpu_init();
2860 fx_save(&vcpu->guest_fx_image);
2861 fx_restore(&vcpu->host_fx_image);
2862 preempt_enable();
2863
2864 vcpu->cr0 |= X86_CR0_ET;
2865 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
2866 vcpu->guest_fx_image.mxcsr = 0x1f80;
2867 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
2868 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
2869}
2870EXPORT_SYMBOL_GPL(fx_init);
2871
2872void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
2873{
2874 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
2875 return;
2876
2877 vcpu->guest_fpu_loaded = 1;
2878 fx_save(&vcpu->host_fx_image);
2879 fx_restore(&vcpu->guest_fx_image);
2880}
2881EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
2882
2883void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
2884{
2885 if (!vcpu->guest_fpu_loaded)
2886 return;
2887
2888 vcpu->guest_fpu_loaded = 0;
2889 fx_save(&vcpu->guest_fx_image);
2890 fx_restore(&vcpu->host_fx_image);
f096ed85 2891 ++vcpu->stat.fpu_reload;
d0752060
HB
2892}
2893EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
e9b11c17
ZX
2894
2895void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
2896{
2897 kvm_x86_ops->vcpu_free(vcpu);
2898}
2899
2900struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2901 unsigned int id)
2902{
26e5215f
AK
2903 return kvm_x86_ops->vcpu_create(kvm, id);
2904}
e9b11c17 2905
26e5215f
AK
2906int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2907{
2908 int r;
e9b11c17
ZX
2909
2910 /* We do fxsave: this must be aligned. */
2911 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2912
2913 vcpu_load(vcpu);
2914 r = kvm_arch_vcpu_reset(vcpu);
2915 if (r == 0)
2916 r = kvm_mmu_setup(vcpu);
2917 vcpu_put(vcpu);
2918 if (r < 0)
2919 goto free_vcpu;
2920
26e5215f 2921 return 0;
e9b11c17
ZX
2922free_vcpu:
2923 kvm_x86_ops->vcpu_free(vcpu);
26e5215f 2924 return r;
e9b11c17
ZX
2925}
2926
d40ccc62 2927void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
e9b11c17
ZX
2928{
2929 vcpu_load(vcpu);
2930 kvm_mmu_unload(vcpu);
2931 vcpu_put(vcpu);
2932
2933 kvm_x86_ops->vcpu_free(vcpu);
2934}
2935
2936int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
2937{
2938 return kvm_x86_ops->vcpu_reset(vcpu);
2939}
2940
2941void kvm_arch_hardware_enable(void *garbage)
2942{
2943 kvm_x86_ops->hardware_enable(garbage);
2944}
2945
2946void kvm_arch_hardware_disable(void *garbage)
2947{
2948 kvm_x86_ops->hardware_disable(garbage);
2949}
2950
2951int kvm_arch_hardware_setup(void)
2952{
2953 return kvm_x86_ops->hardware_setup();
2954}
2955
2956void kvm_arch_hardware_unsetup(void)
2957{
2958 kvm_x86_ops->hardware_unsetup();
2959}
2960
2961void kvm_arch_check_processor_compat(void *rtn)
2962{
2963 kvm_x86_ops->check_processor_compatibility(rtn);
2964}
2965
2966int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
2967{
2968 struct page *page;
2969 struct kvm *kvm;
2970 int r;
2971
2972 BUG_ON(vcpu->kvm == NULL);
2973 kvm = vcpu->kvm;
2974
2975 vcpu->mmu.root_hpa = INVALID_PAGE;
2976 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
2977 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2978 else
2979 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
2980
2981 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2982 if (!page) {
2983 r = -ENOMEM;
2984 goto fail;
2985 }
2986 vcpu->pio_data = page_address(page);
2987
2988 r = kvm_mmu_create(vcpu);
2989 if (r < 0)
2990 goto fail_free_pio_data;
2991
2992 if (irqchip_in_kernel(kvm)) {
2993 r = kvm_create_lapic(vcpu);
2994 if (r < 0)
2995 goto fail_mmu_destroy;
2996 }
2997
2998 return 0;
2999
3000fail_mmu_destroy:
3001 kvm_mmu_destroy(vcpu);
3002fail_free_pio_data:
3003 free_page((unsigned long)vcpu->pio_data);
3004fail:
3005 return r;
3006}
3007
3008void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3009{
3010 kvm_free_lapic(vcpu);
3011 kvm_mmu_destroy(vcpu);
3012 free_page((unsigned long)vcpu->pio_data);
3013}
d19a9cd2
ZX
3014
3015struct kvm *kvm_arch_create_vm(void)
3016{
3017 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3018
3019 if (!kvm)
3020 return ERR_PTR(-ENOMEM);
3021
3022 INIT_LIST_HEAD(&kvm->active_mmu_pages);
3023
3024 return kvm;
3025}
3026
3027static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3028{
3029 vcpu_load(vcpu);
3030 kvm_mmu_unload(vcpu);
3031 vcpu_put(vcpu);
3032}
3033
3034static void kvm_free_vcpus(struct kvm *kvm)
3035{
3036 unsigned int i;
3037
3038 /*
3039 * Unpin any mmu pages first.
3040 */
3041 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3042 if (kvm->vcpus[i])
3043 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3044 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3045 if (kvm->vcpus[i]) {
3046 kvm_arch_vcpu_free(kvm->vcpus[i]);
3047 kvm->vcpus[i] = NULL;
3048 }
3049 }
3050
3051}
3052
3053void kvm_arch_destroy_vm(struct kvm *kvm)
3054{
3055 kfree(kvm->vpic);
3056 kfree(kvm->vioapic);
3057 kvm_free_vcpus(kvm);
3058 kvm_free_physmem(kvm);
3059 kfree(kvm);
3060}
0de10343
ZX
3061
3062int kvm_arch_set_memory_region(struct kvm *kvm,
3063 struct kvm_userspace_memory_region *mem,
3064 struct kvm_memory_slot old,
3065 int user_alloc)
3066{
3067 int npages = mem->memory_size >> PAGE_SHIFT;
3068 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3069
3070 /*To keep backward compatibility with older userspace,
3071 *x86 needs to hanlde !user_alloc case.
3072 */
3073 if (!user_alloc) {
3074 if (npages && !old.rmap) {
3075 down_write(&current->mm->mmap_sem);
3076 memslot->userspace_addr = do_mmap(NULL, 0,
3077 npages * PAGE_SIZE,
3078 PROT_READ | PROT_WRITE,
3079 MAP_SHARED | MAP_ANONYMOUS,
3080 0);
3081 up_write(&current->mm->mmap_sem);
3082
3083 if (IS_ERR((void *)memslot->userspace_addr))
3084 return PTR_ERR((void *)memslot->userspace_addr);
3085 } else {
3086 if (!old.user_alloc && old.rmap) {
3087 int ret;
3088
3089 down_write(&current->mm->mmap_sem);
3090 ret = do_munmap(current->mm, old.userspace_addr,
3091 old.npages * PAGE_SIZE);
3092 up_write(&current->mm->mmap_sem);
3093 if (ret < 0)
3094 printk(KERN_WARNING
3095 "kvm_vm_ioctl_set_memory_region: "
3096 "failed to munmap memory\n");
3097 }
3098 }
3099 }
3100
3101 if (!kvm->n_requested_mmu_pages) {
3102 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3103 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3104 }
3105
3106 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3107 kvm_flush_remote_tlbs(kvm);
3108
3109 return 0;
3110}