KVM: VMX: move CR3/PDPTR update to vmx_set_cr3
[deliverable/linux.git] / arch / x86 / kvm / x86.c
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 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
9 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
20
21 #include <linux/kvm_host.h>
22 #include "irq.h"
23 #include "mmu.h"
24 #include "i8254.h"
25 #include "tss.h"
26 #include "kvm_cache_regs.h"
27 #include "x86.h"
28
29 #include <linux/clocksource.h>
30 #include <linux/interrupt.h>
31 #include <linux/kvm.h>
32 #include <linux/fs.h>
33 #include <linux/vmalloc.h>
34 #include <linux/module.h>
35 #include <linux/mman.h>
36 #include <linux/highmem.h>
37 #include <linux/iommu.h>
38 #include <linux/intel-iommu.h>
39 #include <linux/cpufreq.h>
40 #include <linux/user-return-notifier.h>
41 #include <trace/events/kvm.h>
42 #undef TRACE_INCLUDE_FILE
43 #define CREATE_TRACE_POINTS
44 #include "trace.h"
45
46 #include <asm/uaccess.h>
47 #include <asm/msr.h>
48 #include <asm/desc.h>
49 #include <asm/mtrr.h>
50 #include <asm/mce.h>
51
52 #define MAX_IO_MSRS 256
53 #define CR0_RESERVED_BITS \
54 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
55 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
56 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
57 #define CR4_RESERVED_BITS \
58 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
59 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
60 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
61 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
62
63 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
64
65 #define KVM_MAX_MCE_BANKS 32
66 #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
67
68 /* EFER defaults:
69 * - enable syscall per default because its emulated by KVM
70 * - enable LME and LMA per default on 64 bit KVM
71 */
72 #ifdef CONFIG_X86_64
73 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
74 #else
75 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
76 #endif
77
78 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
79 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
80
81 static void update_cr8_intercept(struct kvm_vcpu *vcpu);
82 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
83 struct kvm_cpuid_entry2 __user *entries);
84
85 struct kvm_x86_ops *kvm_x86_ops;
86 EXPORT_SYMBOL_GPL(kvm_x86_ops);
87
88 int ignore_msrs = 0;
89 module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
90
91 #define KVM_NR_SHARED_MSRS 16
92
93 struct kvm_shared_msrs_global {
94 int nr;
95 struct kvm_shared_msr {
96 u32 msr;
97 u64 value;
98 } msrs[KVM_NR_SHARED_MSRS];
99 };
100
101 struct kvm_shared_msrs {
102 struct user_return_notifier urn;
103 bool registered;
104 u64 current_value[KVM_NR_SHARED_MSRS];
105 };
106
107 static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
108 static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
109
110 struct kvm_stats_debugfs_item debugfs_entries[] = {
111 { "pf_fixed", VCPU_STAT(pf_fixed) },
112 { "pf_guest", VCPU_STAT(pf_guest) },
113 { "tlb_flush", VCPU_STAT(tlb_flush) },
114 { "invlpg", VCPU_STAT(invlpg) },
115 { "exits", VCPU_STAT(exits) },
116 { "io_exits", VCPU_STAT(io_exits) },
117 { "mmio_exits", VCPU_STAT(mmio_exits) },
118 { "signal_exits", VCPU_STAT(signal_exits) },
119 { "irq_window", VCPU_STAT(irq_window_exits) },
120 { "nmi_window", VCPU_STAT(nmi_window_exits) },
121 { "halt_exits", VCPU_STAT(halt_exits) },
122 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
123 { "hypercalls", VCPU_STAT(hypercalls) },
124 { "request_irq", VCPU_STAT(request_irq_exits) },
125 { "irq_exits", VCPU_STAT(irq_exits) },
126 { "host_state_reload", VCPU_STAT(host_state_reload) },
127 { "efer_reload", VCPU_STAT(efer_reload) },
128 { "fpu_reload", VCPU_STAT(fpu_reload) },
129 { "insn_emulation", VCPU_STAT(insn_emulation) },
130 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
131 { "irq_injections", VCPU_STAT(irq_injections) },
132 { "nmi_injections", VCPU_STAT(nmi_injections) },
133 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
134 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
135 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
136 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
137 { "mmu_flooded", VM_STAT(mmu_flooded) },
138 { "mmu_recycled", VM_STAT(mmu_recycled) },
139 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
140 { "mmu_unsync", VM_STAT(mmu_unsync) },
141 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
142 { "largepages", VM_STAT(lpages) },
143 { NULL }
144 };
145
146 static void kvm_on_user_return(struct user_return_notifier *urn)
147 {
148 unsigned slot;
149 struct kvm_shared_msr *global;
150 struct kvm_shared_msrs *locals
151 = container_of(urn, struct kvm_shared_msrs, urn);
152
153 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
154 global = &shared_msrs_global.msrs[slot];
155 if (global->value != locals->current_value[slot]) {
156 wrmsrl(global->msr, global->value);
157 locals->current_value[slot] = global->value;
158 }
159 }
160 locals->registered = false;
161 user_return_notifier_unregister(urn);
162 }
163
164 void kvm_define_shared_msr(unsigned slot, u32 msr)
165 {
166 int cpu;
167 u64 value;
168
169 if (slot >= shared_msrs_global.nr)
170 shared_msrs_global.nr = slot + 1;
171 shared_msrs_global.msrs[slot].msr = msr;
172 rdmsrl_safe(msr, &value);
173 shared_msrs_global.msrs[slot].value = value;
174 for_each_online_cpu(cpu)
175 per_cpu(shared_msrs, cpu).current_value[slot] = value;
176 }
177 EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
178
179 static void kvm_shared_msr_cpu_online(void)
180 {
181 unsigned i;
182 struct kvm_shared_msrs *locals = &__get_cpu_var(shared_msrs);
183
184 for (i = 0; i < shared_msrs_global.nr; ++i)
185 locals->current_value[i] = shared_msrs_global.msrs[i].value;
186 }
187
188 void kvm_set_shared_msr(unsigned slot, u64 value)
189 {
190 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
191
192 if (value == smsr->current_value[slot])
193 return;
194 smsr->current_value[slot] = value;
195 wrmsrl(shared_msrs_global.msrs[slot].msr, value);
196 if (!smsr->registered) {
197 smsr->urn.on_user_return = kvm_on_user_return;
198 user_return_notifier_register(&smsr->urn);
199 smsr->registered = true;
200 }
201 }
202 EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
203
204 unsigned long segment_base(u16 selector)
205 {
206 struct descriptor_table gdt;
207 struct desc_struct *d;
208 unsigned long table_base;
209 unsigned long v;
210
211 if (selector == 0)
212 return 0;
213
214 kvm_get_gdt(&gdt);
215 table_base = gdt.base;
216
217 if (selector & 4) { /* from ldt */
218 u16 ldt_selector = kvm_read_ldt();
219
220 table_base = segment_base(ldt_selector);
221 }
222 d = (struct desc_struct *)(table_base + (selector & ~7));
223 v = get_desc_base(d);
224 #ifdef CONFIG_X86_64
225 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
226 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
227 #endif
228 return v;
229 }
230 EXPORT_SYMBOL_GPL(segment_base);
231
232 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
233 {
234 if (irqchip_in_kernel(vcpu->kvm))
235 return vcpu->arch.apic_base;
236 else
237 return vcpu->arch.apic_base;
238 }
239 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
240
241 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
242 {
243 /* TODO: reserve bits check */
244 if (irqchip_in_kernel(vcpu->kvm))
245 kvm_lapic_set_base(vcpu, data);
246 else
247 vcpu->arch.apic_base = data;
248 }
249 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
250
251 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
252 {
253 WARN_ON(vcpu->arch.exception.pending);
254 vcpu->arch.exception.pending = true;
255 vcpu->arch.exception.has_error_code = false;
256 vcpu->arch.exception.nr = nr;
257 }
258 EXPORT_SYMBOL_GPL(kvm_queue_exception);
259
260 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
261 u32 error_code)
262 {
263 ++vcpu->stat.pf_guest;
264
265 if (vcpu->arch.exception.pending) {
266 switch(vcpu->arch.exception.nr) {
267 case DF_VECTOR:
268 /* triple fault -> shutdown */
269 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
270 return;
271 case PF_VECTOR:
272 vcpu->arch.exception.nr = DF_VECTOR;
273 vcpu->arch.exception.error_code = 0;
274 return;
275 default:
276 /* replace previous exception with a new one in a hope
277 that instruction re-execution will regenerate lost
278 exception */
279 vcpu->arch.exception.pending = false;
280 break;
281 }
282 }
283 vcpu->arch.cr2 = addr;
284 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
285 }
286
287 void kvm_inject_nmi(struct kvm_vcpu *vcpu)
288 {
289 vcpu->arch.nmi_pending = 1;
290 }
291 EXPORT_SYMBOL_GPL(kvm_inject_nmi);
292
293 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
294 {
295 WARN_ON(vcpu->arch.exception.pending);
296 vcpu->arch.exception.pending = true;
297 vcpu->arch.exception.has_error_code = true;
298 vcpu->arch.exception.nr = nr;
299 vcpu->arch.exception.error_code = error_code;
300 }
301 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
302
303 /*
304 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
305 * a #GP and return false.
306 */
307 bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
308 {
309 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
310 return true;
311 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
312 return false;
313 }
314 EXPORT_SYMBOL_GPL(kvm_require_cpl);
315
316 /*
317 * Load the pae pdptrs. Return true is they are all valid.
318 */
319 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
320 {
321 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
322 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
323 int i;
324 int ret;
325 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
326
327 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
328 offset * sizeof(u64), sizeof(pdpte));
329 if (ret < 0) {
330 ret = 0;
331 goto out;
332 }
333 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
334 if (is_present_gpte(pdpte[i]) &&
335 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
336 ret = 0;
337 goto out;
338 }
339 }
340 ret = 1;
341
342 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
343 __set_bit(VCPU_EXREG_PDPTR,
344 (unsigned long *)&vcpu->arch.regs_avail);
345 __set_bit(VCPU_EXREG_PDPTR,
346 (unsigned long *)&vcpu->arch.regs_dirty);
347 out:
348
349 return ret;
350 }
351 EXPORT_SYMBOL_GPL(load_pdptrs);
352
353 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
354 {
355 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
356 bool changed = true;
357 int r;
358
359 if (is_long_mode(vcpu) || !is_pae(vcpu))
360 return false;
361
362 if (!test_bit(VCPU_EXREG_PDPTR,
363 (unsigned long *)&vcpu->arch.regs_avail))
364 return true;
365
366 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
367 if (r < 0)
368 goto out;
369 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
370 out:
371
372 return changed;
373 }
374
375 void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
376 {
377 if (cr0 & CR0_RESERVED_BITS) {
378 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
379 cr0, vcpu->arch.cr0);
380 kvm_inject_gp(vcpu, 0);
381 return;
382 }
383
384 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
385 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
386 kvm_inject_gp(vcpu, 0);
387 return;
388 }
389
390 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
391 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
392 "and a clear PE flag\n");
393 kvm_inject_gp(vcpu, 0);
394 return;
395 }
396
397 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
398 #ifdef CONFIG_X86_64
399 if ((vcpu->arch.shadow_efer & EFER_LME)) {
400 int cs_db, cs_l;
401
402 if (!is_pae(vcpu)) {
403 printk(KERN_DEBUG "set_cr0: #GP, start paging "
404 "in long mode while PAE is disabled\n");
405 kvm_inject_gp(vcpu, 0);
406 return;
407 }
408 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
409 if (cs_l) {
410 printk(KERN_DEBUG "set_cr0: #GP, start paging "
411 "in long mode while CS.L == 1\n");
412 kvm_inject_gp(vcpu, 0);
413 return;
414
415 }
416 } else
417 #endif
418 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
419 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
420 "reserved bits\n");
421 kvm_inject_gp(vcpu, 0);
422 return;
423 }
424
425 }
426
427 kvm_x86_ops->set_cr0(vcpu, cr0);
428 vcpu->arch.cr0 = cr0;
429
430 kvm_mmu_reset_context(vcpu);
431 return;
432 }
433 EXPORT_SYMBOL_GPL(kvm_set_cr0);
434
435 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
436 {
437 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
438 }
439 EXPORT_SYMBOL_GPL(kvm_lmsw);
440
441 void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
442 {
443 unsigned long old_cr4 = vcpu->arch.cr4;
444 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
445
446 if (cr4 & CR4_RESERVED_BITS) {
447 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
448 kvm_inject_gp(vcpu, 0);
449 return;
450 }
451
452 if (is_long_mode(vcpu)) {
453 if (!(cr4 & X86_CR4_PAE)) {
454 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
455 "in long mode\n");
456 kvm_inject_gp(vcpu, 0);
457 return;
458 }
459 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
460 && ((cr4 ^ old_cr4) & pdptr_bits)
461 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
462 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
463 kvm_inject_gp(vcpu, 0);
464 return;
465 }
466
467 if (cr4 & X86_CR4_VMXE) {
468 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
469 kvm_inject_gp(vcpu, 0);
470 return;
471 }
472 kvm_x86_ops->set_cr4(vcpu, cr4);
473 vcpu->arch.cr4 = cr4;
474 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
475 kvm_mmu_reset_context(vcpu);
476 }
477 EXPORT_SYMBOL_GPL(kvm_set_cr4);
478
479 void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
480 {
481 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
482 kvm_mmu_sync_roots(vcpu);
483 kvm_mmu_flush_tlb(vcpu);
484 return;
485 }
486
487 if (is_long_mode(vcpu)) {
488 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
489 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
490 kvm_inject_gp(vcpu, 0);
491 return;
492 }
493 } else {
494 if (is_pae(vcpu)) {
495 if (cr3 & CR3_PAE_RESERVED_BITS) {
496 printk(KERN_DEBUG
497 "set_cr3: #GP, reserved bits\n");
498 kvm_inject_gp(vcpu, 0);
499 return;
500 }
501 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
502 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
503 "reserved bits\n");
504 kvm_inject_gp(vcpu, 0);
505 return;
506 }
507 }
508 /*
509 * We don't check reserved bits in nonpae mode, because
510 * this isn't enforced, and VMware depends on this.
511 */
512 }
513
514 /*
515 * Does the new cr3 value map to physical memory? (Note, we
516 * catch an invalid cr3 even in real-mode, because it would
517 * cause trouble later on when we turn on paging anyway.)
518 *
519 * A real CPU would silently accept an invalid cr3 and would
520 * attempt to use it - with largely undefined (and often hard
521 * to debug) behavior on the guest side.
522 */
523 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
524 kvm_inject_gp(vcpu, 0);
525 else {
526 vcpu->arch.cr3 = cr3;
527 vcpu->arch.mmu.new_cr3(vcpu);
528 }
529 }
530 EXPORT_SYMBOL_GPL(kvm_set_cr3);
531
532 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
533 {
534 if (cr8 & CR8_RESERVED_BITS) {
535 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
536 kvm_inject_gp(vcpu, 0);
537 return;
538 }
539 if (irqchip_in_kernel(vcpu->kvm))
540 kvm_lapic_set_tpr(vcpu, cr8);
541 else
542 vcpu->arch.cr8 = cr8;
543 }
544 EXPORT_SYMBOL_GPL(kvm_set_cr8);
545
546 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
547 {
548 if (irqchip_in_kernel(vcpu->kvm))
549 return kvm_lapic_get_cr8(vcpu);
550 else
551 return vcpu->arch.cr8;
552 }
553 EXPORT_SYMBOL_GPL(kvm_get_cr8);
554
555 static inline u32 bit(int bitno)
556 {
557 return 1 << (bitno & 31);
558 }
559
560 /*
561 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
562 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
563 *
564 * This list is modified at module load time to reflect the
565 * capabilities of the host cpu. This capabilities test skips MSRs that are
566 * kvm-specific. Those are put in the beginning of the list.
567 */
568
569 #define KVM_SAVE_MSRS_BEGIN 2
570 static u32 msrs_to_save[] = {
571 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
572 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
573 MSR_K6_STAR,
574 #ifdef CONFIG_X86_64
575 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
576 #endif
577 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
578 };
579
580 static unsigned num_msrs_to_save;
581
582 static u32 emulated_msrs[] = {
583 MSR_IA32_MISC_ENABLE,
584 };
585
586 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
587 {
588 if (efer & efer_reserved_bits) {
589 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
590 efer);
591 kvm_inject_gp(vcpu, 0);
592 return;
593 }
594
595 if (is_paging(vcpu)
596 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
597 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
598 kvm_inject_gp(vcpu, 0);
599 return;
600 }
601
602 if (efer & EFER_FFXSR) {
603 struct kvm_cpuid_entry2 *feat;
604
605 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
606 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
607 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
608 kvm_inject_gp(vcpu, 0);
609 return;
610 }
611 }
612
613 if (efer & EFER_SVME) {
614 struct kvm_cpuid_entry2 *feat;
615
616 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
617 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
618 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
619 kvm_inject_gp(vcpu, 0);
620 return;
621 }
622 }
623
624 kvm_x86_ops->set_efer(vcpu, efer);
625
626 efer &= ~EFER_LMA;
627 efer |= vcpu->arch.shadow_efer & EFER_LMA;
628
629 vcpu->arch.shadow_efer = efer;
630
631 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
632 kvm_mmu_reset_context(vcpu);
633 }
634
635 void kvm_enable_efer_bits(u64 mask)
636 {
637 efer_reserved_bits &= ~mask;
638 }
639 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
640
641
642 /*
643 * Writes msr value into into the appropriate "register".
644 * Returns 0 on success, non-0 otherwise.
645 * Assumes vcpu_load() was already called.
646 */
647 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
648 {
649 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
650 }
651
652 /*
653 * Adapt set_msr() to msr_io()'s calling convention
654 */
655 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
656 {
657 return kvm_set_msr(vcpu, index, *data);
658 }
659
660 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
661 {
662 static int version;
663 struct pvclock_wall_clock wc;
664 struct timespec now, sys, boot;
665
666 if (!wall_clock)
667 return;
668
669 version++;
670
671 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
672
673 /*
674 * The guest calculates current wall clock time by adding
675 * system time (updated by kvm_write_guest_time below) to the
676 * wall clock specified here. guest system time equals host
677 * system time for us, thus we must fill in host boot time here.
678 */
679 now = current_kernel_time();
680 ktime_get_ts(&sys);
681 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
682
683 wc.sec = boot.tv_sec;
684 wc.nsec = boot.tv_nsec;
685 wc.version = version;
686
687 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
688
689 version++;
690 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
691 }
692
693 static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
694 {
695 uint32_t quotient, remainder;
696
697 /* Don't try to replace with do_div(), this one calculates
698 * "(dividend << 32) / divisor" */
699 __asm__ ( "divl %4"
700 : "=a" (quotient), "=d" (remainder)
701 : "0" (0), "1" (dividend), "r" (divisor) );
702 return quotient;
703 }
704
705 static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
706 {
707 uint64_t nsecs = 1000000000LL;
708 int32_t shift = 0;
709 uint64_t tps64;
710 uint32_t tps32;
711
712 tps64 = tsc_khz * 1000LL;
713 while (tps64 > nsecs*2) {
714 tps64 >>= 1;
715 shift--;
716 }
717
718 tps32 = (uint32_t)tps64;
719 while (tps32 <= (uint32_t)nsecs) {
720 tps32 <<= 1;
721 shift++;
722 }
723
724 hv_clock->tsc_shift = shift;
725 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
726
727 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
728 __func__, tsc_khz, hv_clock->tsc_shift,
729 hv_clock->tsc_to_system_mul);
730 }
731
732 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
733
734 static void kvm_write_guest_time(struct kvm_vcpu *v)
735 {
736 struct timespec ts;
737 unsigned long flags;
738 struct kvm_vcpu_arch *vcpu = &v->arch;
739 void *shared_kaddr;
740 unsigned long this_tsc_khz;
741
742 if ((!vcpu->time_page))
743 return;
744
745 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
746 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
747 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
748 vcpu->hv_clock_tsc_khz = this_tsc_khz;
749 }
750 put_cpu_var(cpu_tsc_khz);
751
752 /* Keep irq disabled to prevent changes to the clock */
753 local_irq_save(flags);
754 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
755 ktime_get_ts(&ts);
756 local_irq_restore(flags);
757
758 /* With all the info we got, fill in the values */
759
760 vcpu->hv_clock.system_time = ts.tv_nsec +
761 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
762
763 /*
764 * The interface expects us to write an even number signaling that the
765 * update is finished. Since the guest won't see the intermediate
766 * state, we just increase by 2 at the end.
767 */
768 vcpu->hv_clock.version += 2;
769
770 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
771
772 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
773 sizeof(vcpu->hv_clock));
774
775 kunmap_atomic(shared_kaddr, KM_USER0);
776
777 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
778 }
779
780 static int kvm_request_guest_time_update(struct kvm_vcpu *v)
781 {
782 struct kvm_vcpu_arch *vcpu = &v->arch;
783
784 if (!vcpu->time_page)
785 return 0;
786 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
787 return 1;
788 }
789
790 static bool msr_mtrr_valid(unsigned msr)
791 {
792 switch (msr) {
793 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
794 case MSR_MTRRfix64K_00000:
795 case MSR_MTRRfix16K_80000:
796 case MSR_MTRRfix16K_A0000:
797 case MSR_MTRRfix4K_C0000:
798 case MSR_MTRRfix4K_C8000:
799 case MSR_MTRRfix4K_D0000:
800 case MSR_MTRRfix4K_D8000:
801 case MSR_MTRRfix4K_E0000:
802 case MSR_MTRRfix4K_E8000:
803 case MSR_MTRRfix4K_F0000:
804 case MSR_MTRRfix4K_F8000:
805 case MSR_MTRRdefType:
806 case MSR_IA32_CR_PAT:
807 return true;
808 case 0x2f8:
809 return true;
810 }
811 return false;
812 }
813
814 static bool valid_pat_type(unsigned t)
815 {
816 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
817 }
818
819 static bool valid_mtrr_type(unsigned t)
820 {
821 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
822 }
823
824 static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
825 {
826 int i;
827
828 if (!msr_mtrr_valid(msr))
829 return false;
830
831 if (msr == MSR_IA32_CR_PAT) {
832 for (i = 0; i < 8; i++)
833 if (!valid_pat_type((data >> (i * 8)) & 0xff))
834 return false;
835 return true;
836 } else if (msr == MSR_MTRRdefType) {
837 if (data & ~0xcff)
838 return false;
839 return valid_mtrr_type(data & 0xff);
840 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
841 for (i = 0; i < 8 ; i++)
842 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
843 return false;
844 return true;
845 }
846
847 /* variable MTRRs */
848 return valid_mtrr_type(data & 0xff);
849 }
850
851 static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
852 {
853 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
854
855 if (!mtrr_valid(vcpu, msr, data))
856 return 1;
857
858 if (msr == MSR_MTRRdefType) {
859 vcpu->arch.mtrr_state.def_type = data;
860 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
861 } else if (msr == MSR_MTRRfix64K_00000)
862 p[0] = data;
863 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
864 p[1 + msr - MSR_MTRRfix16K_80000] = data;
865 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
866 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
867 else if (msr == MSR_IA32_CR_PAT)
868 vcpu->arch.pat = data;
869 else { /* Variable MTRRs */
870 int idx, is_mtrr_mask;
871 u64 *pt;
872
873 idx = (msr - 0x200) / 2;
874 is_mtrr_mask = msr - 0x200 - 2 * idx;
875 if (!is_mtrr_mask)
876 pt =
877 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
878 else
879 pt =
880 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
881 *pt = data;
882 }
883
884 kvm_mmu_reset_context(vcpu);
885 return 0;
886 }
887
888 static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
889 {
890 u64 mcg_cap = vcpu->arch.mcg_cap;
891 unsigned bank_num = mcg_cap & 0xff;
892
893 switch (msr) {
894 case MSR_IA32_MCG_STATUS:
895 vcpu->arch.mcg_status = data;
896 break;
897 case MSR_IA32_MCG_CTL:
898 if (!(mcg_cap & MCG_CTL_P))
899 return 1;
900 if (data != 0 && data != ~(u64)0)
901 return -1;
902 vcpu->arch.mcg_ctl = data;
903 break;
904 default:
905 if (msr >= MSR_IA32_MC0_CTL &&
906 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
907 u32 offset = msr - MSR_IA32_MC0_CTL;
908 /* only 0 or all 1s can be written to IA32_MCi_CTL */
909 if ((offset & 0x3) == 0 &&
910 data != 0 && data != ~(u64)0)
911 return -1;
912 vcpu->arch.mce_banks[offset] = data;
913 break;
914 }
915 return 1;
916 }
917 return 0;
918 }
919
920 static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
921 {
922 struct kvm *kvm = vcpu->kvm;
923 int lm = is_long_mode(vcpu);
924 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
925 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
926 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
927 : kvm->arch.xen_hvm_config.blob_size_32;
928 u32 page_num = data & ~PAGE_MASK;
929 u64 page_addr = data & PAGE_MASK;
930 u8 *page;
931 int r;
932
933 r = -E2BIG;
934 if (page_num >= blob_size)
935 goto out;
936 r = -ENOMEM;
937 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
938 if (!page)
939 goto out;
940 r = -EFAULT;
941 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
942 goto out_free;
943 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
944 goto out_free;
945 r = 0;
946 out_free:
947 kfree(page);
948 out:
949 return r;
950 }
951
952 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
953 {
954 switch (msr) {
955 case MSR_EFER:
956 set_efer(vcpu, data);
957 break;
958 case MSR_K7_HWCR:
959 data &= ~(u64)0x40; /* ignore flush filter disable */
960 if (data != 0) {
961 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
962 data);
963 return 1;
964 }
965 break;
966 case MSR_FAM10H_MMIO_CONF_BASE:
967 if (data != 0) {
968 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
969 "0x%llx\n", data);
970 return 1;
971 }
972 break;
973 case MSR_AMD64_NB_CFG:
974 break;
975 case MSR_IA32_DEBUGCTLMSR:
976 if (!data) {
977 /* We support the non-activated case already */
978 break;
979 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
980 /* Values other than LBR and BTF are vendor-specific,
981 thus reserved and should throw a #GP */
982 return 1;
983 }
984 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
985 __func__, data);
986 break;
987 case MSR_IA32_UCODE_REV:
988 case MSR_IA32_UCODE_WRITE:
989 case MSR_VM_HSAVE_PA:
990 case MSR_AMD64_PATCH_LOADER:
991 break;
992 case 0x200 ... 0x2ff:
993 return set_msr_mtrr(vcpu, msr, data);
994 case MSR_IA32_APICBASE:
995 kvm_set_apic_base(vcpu, data);
996 break;
997 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
998 return kvm_x2apic_msr_write(vcpu, msr, data);
999 case MSR_IA32_MISC_ENABLE:
1000 vcpu->arch.ia32_misc_enable_msr = data;
1001 break;
1002 case MSR_KVM_WALL_CLOCK:
1003 vcpu->kvm->arch.wall_clock = data;
1004 kvm_write_wall_clock(vcpu->kvm, data);
1005 break;
1006 case MSR_KVM_SYSTEM_TIME: {
1007 if (vcpu->arch.time_page) {
1008 kvm_release_page_dirty(vcpu->arch.time_page);
1009 vcpu->arch.time_page = NULL;
1010 }
1011
1012 vcpu->arch.time = data;
1013
1014 /* we verify if the enable bit is set... */
1015 if (!(data & 1))
1016 break;
1017
1018 /* ...but clean it before doing the actual write */
1019 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1020
1021 vcpu->arch.time_page =
1022 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
1023
1024 if (is_error_page(vcpu->arch.time_page)) {
1025 kvm_release_page_clean(vcpu->arch.time_page);
1026 vcpu->arch.time_page = NULL;
1027 }
1028
1029 kvm_request_guest_time_update(vcpu);
1030 break;
1031 }
1032 case MSR_IA32_MCG_CTL:
1033 case MSR_IA32_MCG_STATUS:
1034 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1035 return set_msr_mce(vcpu, msr, data);
1036
1037 /* Performance counters are not protected by a CPUID bit,
1038 * so we should check all of them in the generic path for the sake of
1039 * cross vendor migration.
1040 * Writing a zero into the event select MSRs disables them,
1041 * which we perfectly emulate ;-). Any other value should be at least
1042 * reported, some guests depend on them.
1043 */
1044 case MSR_P6_EVNTSEL0:
1045 case MSR_P6_EVNTSEL1:
1046 case MSR_K7_EVNTSEL0:
1047 case MSR_K7_EVNTSEL1:
1048 case MSR_K7_EVNTSEL2:
1049 case MSR_K7_EVNTSEL3:
1050 if (data != 0)
1051 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1052 "0x%x data 0x%llx\n", msr, data);
1053 break;
1054 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1055 * so we ignore writes to make it happy.
1056 */
1057 case MSR_P6_PERFCTR0:
1058 case MSR_P6_PERFCTR1:
1059 case MSR_K7_PERFCTR0:
1060 case MSR_K7_PERFCTR1:
1061 case MSR_K7_PERFCTR2:
1062 case MSR_K7_PERFCTR3:
1063 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1064 "0x%x data 0x%llx\n", msr, data);
1065 break;
1066 default:
1067 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1068 return xen_hvm_config(vcpu, data);
1069 if (!ignore_msrs) {
1070 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1071 msr, data);
1072 return 1;
1073 } else {
1074 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1075 msr, data);
1076 break;
1077 }
1078 }
1079 return 0;
1080 }
1081 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1082
1083
1084 /*
1085 * Reads an msr value (of 'msr_index') into 'pdata'.
1086 * Returns 0 on success, non-0 otherwise.
1087 * Assumes vcpu_load() was already called.
1088 */
1089 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1090 {
1091 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1092 }
1093
1094 static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1095 {
1096 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1097
1098 if (!msr_mtrr_valid(msr))
1099 return 1;
1100
1101 if (msr == MSR_MTRRdefType)
1102 *pdata = vcpu->arch.mtrr_state.def_type +
1103 (vcpu->arch.mtrr_state.enabled << 10);
1104 else if (msr == MSR_MTRRfix64K_00000)
1105 *pdata = p[0];
1106 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1107 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1108 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1109 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1110 else if (msr == MSR_IA32_CR_PAT)
1111 *pdata = vcpu->arch.pat;
1112 else { /* Variable MTRRs */
1113 int idx, is_mtrr_mask;
1114 u64 *pt;
1115
1116 idx = (msr - 0x200) / 2;
1117 is_mtrr_mask = msr - 0x200 - 2 * idx;
1118 if (!is_mtrr_mask)
1119 pt =
1120 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1121 else
1122 pt =
1123 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1124 *pdata = *pt;
1125 }
1126
1127 return 0;
1128 }
1129
1130 static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1131 {
1132 u64 data;
1133 u64 mcg_cap = vcpu->arch.mcg_cap;
1134 unsigned bank_num = mcg_cap & 0xff;
1135
1136 switch (msr) {
1137 case MSR_IA32_P5_MC_ADDR:
1138 case MSR_IA32_P5_MC_TYPE:
1139 data = 0;
1140 break;
1141 case MSR_IA32_MCG_CAP:
1142 data = vcpu->arch.mcg_cap;
1143 break;
1144 case MSR_IA32_MCG_CTL:
1145 if (!(mcg_cap & MCG_CTL_P))
1146 return 1;
1147 data = vcpu->arch.mcg_ctl;
1148 break;
1149 case MSR_IA32_MCG_STATUS:
1150 data = vcpu->arch.mcg_status;
1151 break;
1152 default:
1153 if (msr >= MSR_IA32_MC0_CTL &&
1154 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1155 u32 offset = msr - MSR_IA32_MC0_CTL;
1156 data = vcpu->arch.mce_banks[offset];
1157 break;
1158 }
1159 return 1;
1160 }
1161 *pdata = data;
1162 return 0;
1163 }
1164
1165 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1166 {
1167 u64 data;
1168
1169 switch (msr) {
1170 case MSR_IA32_PLATFORM_ID:
1171 case MSR_IA32_UCODE_REV:
1172 case MSR_IA32_EBL_CR_POWERON:
1173 case MSR_IA32_DEBUGCTLMSR:
1174 case MSR_IA32_LASTBRANCHFROMIP:
1175 case MSR_IA32_LASTBRANCHTOIP:
1176 case MSR_IA32_LASTINTFROMIP:
1177 case MSR_IA32_LASTINTTOIP:
1178 case MSR_K8_SYSCFG:
1179 case MSR_K7_HWCR:
1180 case MSR_VM_HSAVE_PA:
1181 case MSR_P6_PERFCTR0:
1182 case MSR_P6_PERFCTR1:
1183 case MSR_P6_EVNTSEL0:
1184 case MSR_P6_EVNTSEL1:
1185 case MSR_K7_EVNTSEL0:
1186 case MSR_K7_PERFCTR0:
1187 case MSR_K8_INT_PENDING_MSG:
1188 case MSR_AMD64_NB_CFG:
1189 case MSR_FAM10H_MMIO_CONF_BASE:
1190 data = 0;
1191 break;
1192 case MSR_MTRRcap:
1193 data = 0x500 | KVM_NR_VAR_MTRR;
1194 break;
1195 case 0x200 ... 0x2ff:
1196 return get_msr_mtrr(vcpu, msr, pdata);
1197 case 0xcd: /* fsb frequency */
1198 data = 3;
1199 break;
1200 case MSR_IA32_APICBASE:
1201 data = kvm_get_apic_base(vcpu);
1202 break;
1203 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1204 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1205 break;
1206 case MSR_IA32_MISC_ENABLE:
1207 data = vcpu->arch.ia32_misc_enable_msr;
1208 break;
1209 case MSR_IA32_PERF_STATUS:
1210 /* TSC increment by tick */
1211 data = 1000ULL;
1212 /* CPU multiplier */
1213 data |= (((uint64_t)4ULL) << 40);
1214 break;
1215 case MSR_EFER:
1216 data = vcpu->arch.shadow_efer;
1217 break;
1218 case MSR_KVM_WALL_CLOCK:
1219 data = vcpu->kvm->arch.wall_clock;
1220 break;
1221 case MSR_KVM_SYSTEM_TIME:
1222 data = vcpu->arch.time;
1223 break;
1224 case MSR_IA32_P5_MC_ADDR:
1225 case MSR_IA32_P5_MC_TYPE:
1226 case MSR_IA32_MCG_CAP:
1227 case MSR_IA32_MCG_CTL:
1228 case MSR_IA32_MCG_STATUS:
1229 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1230 return get_msr_mce(vcpu, msr, pdata);
1231 default:
1232 if (!ignore_msrs) {
1233 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1234 return 1;
1235 } else {
1236 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1237 data = 0;
1238 }
1239 break;
1240 }
1241 *pdata = data;
1242 return 0;
1243 }
1244 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1245
1246 /*
1247 * Read or write a bunch of msrs. All parameters are kernel addresses.
1248 *
1249 * @return number of msrs set successfully.
1250 */
1251 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1252 struct kvm_msr_entry *entries,
1253 int (*do_msr)(struct kvm_vcpu *vcpu,
1254 unsigned index, u64 *data))
1255 {
1256 int i;
1257
1258 vcpu_load(vcpu);
1259
1260 down_read(&vcpu->kvm->slots_lock);
1261 for (i = 0; i < msrs->nmsrs; ++i)
1262 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1263 break;
1264 up_read(&vcpu->kvm->slots_lock);
1265
1266 vcpu_put(vcpu);
1267
1268 return i;
1269 }
1270
1271 /*
1272 * Read or write a bunch of msrs. Parameters are user addresses.
1273 *
1274 * @return number of msrs set successfully.
1275 */
1276 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1277 int (*do_msr)(struct kvm_vcpu *vcpu,
1278 unsigned index, u64 *data),
1279 int writeback)
1280 {
1281 struct kvm_msrs msrs;
1282 struct kvm_msr_entry *entries;
1283 int r, n;
1284 unsigned size;
1285
1286 r = -EFAULT;
1287 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1288 goto out;
1289
1290 r = -E2BIG;
1291 if (msrs.nmsrs >= MAX_IO_MSRS)
1292 goto out;
1293
1294 r = -ENOMEM;
1295 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1296 entries = vmalloc(size);
1297 if (!entries)
1298 goto out;
1299
1300 r = -EFAULT;
1301 if (copy_from_user(entries, user_msrs->entries, size))
1302 goto out_free;
1303
1304 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1305 if (r < 0)
1306 goto out_free;
1307
1308 r = -EFAULT;
1309 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1310 goto out_free;
1311
1312 r = n;
1313
1314 out_free:
1315 vfree(entries);
1316 out:
1317 return r;
1318 }
1319
1320 int kvm_dev_ioctl_check_extension(long ext)
1321 {
1322 int r;
1323
1324 switch (ext) {
1325 case KVM_CAP_IRQCHIP:
1326 case KVM_CAP_HLT:
1327 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
1328 case KVM_CAP_SET_TSS_ADDR:
1329 case KVM_CAP_EXT_CPUID:
1330 case KVM_CAP_CLOCKSOURCE:
1331 case KVM_CAP_PIT:
1332 case KVM_CAP_NOP_IO_DELAY:
1333 case KVM_CAP_MP_STATE:
1334 case KVM_CAP_SYNC_MMU:
1335 case KVM_CAP_REINJECT_CONTROL:
1336 case KVM_CAP_IRQ_INJECT_STATUS:
1337 case KVM_CAP_ASSIGN_DEV_IRQ:
1338 case KVM_CAP_IRQFD:
1339 case KVM_CAP_IOEVENTFD:
1340 case KVM_CAP_PIT2:
1341 case KVM_CAP_PIT_STATE2:
1342 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
1343 case KVM_CAP_XEN_HVM:
1344 case KVM_CAP_ADJUST_CLOCK:
1345 r = 1;
1346 break;
1347 case KVM_CAP_COALESCED_MMIO:
1348 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1349 break;
1350 case KVM_CAP_VAPIC:
1351 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1352 break;
1353 case KVM_CAP_NR_VCPUS:
1354 r = KVM_MAX_VCPUS;
1355 break;
1356 case KVM_CAP_NR_MEMSLOTS:
1357 r = KVM_MEMORY_SLOTS;
1358 break;
1359 case KVM_CAP_PV_MMU: /* obsolete */
1360 r = 0;
1361 break;
1362 case KVM_CAP_IOMMU:
1363 r = iommu_found();
1364 break;
1365 case KVM_CAP_MCE:
1366 r = KVM_MAX_MCE_BANKS;
1367 break;
1368 default:
1369 r = 0;
1370 break;
1371 }
1372 return r;
1373
1374 }
1375
1376 long kvm_arch_dev_ioctl(struct file *filp,
1377 unsigned int ioctl, unsigned long arg)
1378 {
1379 void __user *argp = (void __user *)arg;
1380 long r;
1381
1382 switch (ioctl) {
1383 case KVM_GET_MSR_INDEX_LIST: {
1384 struct kvm_msr_list __user *user_msr_list = argp;
1385 struct kvm_msr_list msr_list;
1386 unsigned n;
1387
1388 r = -EFAULT;
1389 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1390 goto out;
1391 n = msr_list.nmsrs;
1392 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1393 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1394 goto out;
1395 r = -E2BIG;
1396 if (n < msr_list.nmsrs)
1397 goto out;
1398 r = -EFAULT;
1399 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1400 num_msrs_to_save * sizeof(u32)))
1401 goto out;
1402 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
1403 &emulated_msrs,
1404 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1405 goto out;
1406 r = 0;
1407 break;
1408 }
1409 case KVM_GET_SUPPORTED_CPUID: {
1410 struct kvm_cpuid2 __user *cpuid_arg = argp;
1411 struct kvm_cpuid2 cpuid;
1412
1413 r = -EFAULT;
1414 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1415 goto out;
1416 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
1417 cpuid_arg->entries);
1418 if (r)
1419 goto out;
1420
1421 r = -EFAULT;
1422 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1423 goto out;
1424 r = 0;
1425 break;
1426 }
1427 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1428 u64 mce_cap;
1429
1430 mce_cap = KVM_MCE_CAP_SUPPORTED;
1431 r = -EFAULT;
1432 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1433 goto out;
1434 r = 0;
1435 break;
1436 }
1437 default:
1438 r = -EINVAL;
1439 }
1440 out:
1441 return r;
1442 }
1443
1444 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1445 {
1446 kvm_x86_ops->vcpu_load(vcpu, cpu);
1447 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1448 unsigned long khz = cpufreq_quick_get(cpu);
1449 if (!khz)
1450 khz = tsc_khz;
1451 per_cpu(cpu_tsc_khz, cpu) = khz;
1452 }
1453 kvm_request_guest_time_update(vcpu);
1454 }
1455
1456 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1457 {
1458 kvm_x86_ops->vcpu_put(vcpu);
1459 kvm_put_guest_fpu(vcpu);
1460 }
1461
1462 static int is_efer_nx(void)
1463 {
1464 unsigned long long efer = 0;
1465
1466 rdmsrl_safe(MSR_EFER, &efer);
1467 return efer & EFER_NX;
1468 }
1469
1470 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1471 {
1472 int i;
1473 struct kvm_cpuid_entry2 *e, *entry;
1474
1475 entry = NULL;
1476 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1477 e = &vcpu->arch.cpuid_entries[i];
1478 if (e->function == 0x80000001) {
1479 entry = e;
1480 break;
1481 }
1482 }
1483 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
1484 entry->edx &= ~(1 << 20);
1485 printk(KERN_INFO "kvm: guest NX capability removed\n");
1486 }
1487 }
1488
1489 /* when an old userspace process fills a new kernel module */
1490 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1491 struct kvm_cpuid *cpuid,
1492 struct kvm_cpuid_entry __user *entries)
1493 {
1494 int r, i;
1495 struct kvm_cpuid_entry *cpuid_entries;
1496
1497 r = -E2BIG;
1498 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1499 goto out;
1500 r = -ENOMEM;
1501 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1502 if (!cpuid_entries)
1503 goto out;
1504 r = -EFAULT;
1505 if (copy_from_user(cpuid_entries, entries,
1506 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1507 goto out_free;
1508 for (i = 0; i < cpuid->nent; i++) {
1509 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1510 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1511 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1512 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1513 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1514 vcpu->arch.cpuid_entries[i].index = 0;
1515 vcpu->arch.cpuid_entries[i].flags = 0;
1516 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1517 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1518 vcpu->arch.cpuid_entries[i].padding[2] = 0;
1519 }
1520 vcpu->arch.cpuid_nent = cpuid->nent;
1521 cpuid_fix_nx_cap(vcpu);
1522 r = 0;
1523 kvm_apic_set_version(vcpu);
1524
1525 out_free:
1526 vfree(cpuid_entries);
1527 out:
1528 return r;
1529 }
1530
1531 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1532 struct kvm_cpuid2 *cpuid,
1533 struct kvm_cpuid_entry2 __user *entries)
1534 {
1535 int r;
1536
1537 r = -E2BIG;
1538 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1539 goto out;
1540 r = -EFAULT;
1541 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
1542 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
1543 goto out;
1544 vcpu->arch.cpuid_nent = cpuid->nent;
1545 kvm_apic_set_version(vcpu);
1546 return 0;
1547
1548 out:
1549 return r;
1550 }
1551
1552 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1553 struct kvm_cpuid2 *cpuid,
1554 struct kvm_cpuid_entry2 __user *entries)
1555 {
1556 int r;
1557
1558 r = -E2BIG;
1559 if (cpuid->nent < vcpu->arch.cpuid_nent)
1560 goto out;
1561 r = -EFAULT;
1562 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1563 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1564 goto out;
1565 return 0;
1566
1567 out:
1568 cpuid->nent = vcpu->arch.cpuid_nent;
1569 return r;
1570 }
1571
1572 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1573 u32 index)
1574 {
1575 entry->function = function;
1576 entry->index = index;
1577 cpuid_count(entry->function, entry->index,
1578 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1579 entry->flags = 0;
1580 }
1581
1582 #define F(x) bit(X86_FEATURE_##x)
1583
1584 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1585 u32 index, int *nent, int maxnent)
1586 {
1587 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
1588 unsigned f_gbpages = kvm_x86_ops->gb_page_enable() ? F(GBPAGES) : 0;
1589 #ifdef CONFIG_X86_64
1590 unsigned f_lm = F(LM);
1591 #else
1592 unsigned f_lm = 0;
1593 #endif
1594
1595 /* cpuid 1.edx */
1596 const u32 kvm_supported_word0_x86_features =
1597 F(FPU) | F(VME) | F(DE) | F(PSE) |
1598 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1599 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1600 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1601 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1602 0 /* Reserved, DS, ACPI */ | F(MMX) |
1603 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1604 0 /* HTT, TM, Reserved, PBE */;
1605 /* cpuid 0x80000001.edx */
1606 const u32 kvm_supported_word1_x86_features =
1607 F(FPU) | F(VME) | F(DE) | F(PSE) |
1608 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1609 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1610 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1611 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1612 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
1613 F(FXSR) | F(FXSR_OPT) | f_gbpages | 0 /* RDTSCP */ |
1614 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1615 /* cpuid 1.ecx */
1616 const u32 kvm_supported_word4_x86_features =
1617 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1618 0 /* DS-CPL, VMX, SMX, EST */ |
1619 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1620 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1621 0 /* Reserved, DCA */ | F(XMM4_1) |
1622 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
1623 0 /* Reserved, XSAVE, OSXSAVE */;
1624 /* cpuid 0x80000001.ecx */
1625 const u32 kvm_supported_word6_x86_features =
1626 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1627 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1628 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1629 0 /* SKINIT */ | 0 /* WDT */;
1630
1631 /* all calls to cpuid_count() should be made on the same cpu */
1632 get_cpu();
1633 do_cpuid_1_ent(entry, function, index);
1634 ++*nent;
1635
1636 switch (function) {
1637 case 0:
1638 entry->eax = min(entry->eax, (u32)0xb);
1639 break;
1640 case 1:
1641 entry->edx &= kvm_supported_word0_x86_features;
1642 entry->ecx &= kvm_supported_word4_x86_features;
1643 /* we support x2apic emulation even if host does not support
1644 * it since we emulate x2apic in software */
1645 entry->ecx |= F(X2APIC);
1646 break;
1647 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1648 * may return different values. This forces us to get_cpu() before
1649 * issuing the first command, and also to emulate this annoying behavior
1650 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1651 case 2: {
1652 int t, times = entry->eax & 0xff;
1653
1654 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1655 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
1656 for (t = 1; t < times && *nent < maxnent; ++t) {
1657 do_cpuid_1_ent(&entry[t], function, 0);
1658 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1659 ++*nent;
1660 }
1661 break;
1662 }
1663 /* function 4 and 0xb have additional index. */
1664 case 4: {
1665 int i, cache_type;
1666
1667 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1668 /* read more entries until cache_type is zero */
1669 for (i = 1; *nent < maxnent; ++i) {
1670 cache_type = entry[i - 1].eax & 0x1f;
1671 if (!cache_type)
1672 break;
1673 do_cpuid_1_ent(&entry[i], function, i);
1674 entry[i].flags |=
1675 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1676 ++*nent;
1677 }
1678 break;
1679 }
1680 case 0xb: {
1681 int i, level_type;
1682
1683 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1684 /* read more entries until level_type is zero */
1685 for (i = 1; *nent < maxnent; ++i) {
1686 level_type = entry[i - 1].ecx & 0xff00;
1687 if (!level_type)
1688 break;
1689 do_cpuid_1_ent(&entry[i], function, i);
1690 entry[i].flags |=
1691 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1692 ++*nent;
1693 }
1694 break;
1695 }
1696 case 0x80000000:
1697 entry->eax = min(entry->eax, 0x8000001a);
1698 break;
1699 case 0x80000001:
1700 entry->edx &= kvm_supported_word1_x86_features;
1701 entry->ecx &= kvm_supported_word6_x86_features;
1702 break;
1703 }
1704 put_cpu();
1705 }
1706
1707 #undef F
1708
1709 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1710 struct kvm_cpuid_entry2 __user *entries)
1711 {
1712 struct kvm_cpuid_entry2 *cpuid_entries;
1713 int limit, nent = 0, r = -E2BIG;
1714 u32 func;
1715
1716 if (cpuid->nent < 1)
1717 goto out;
1718 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1719 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
1720 r = -ENOMEM;
1721 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1722 if (!cpuid_entries)
1723 goto out;
1724
1725 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1726 limit = cpuid_entries[0].eax;
1727 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1728 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1729 &nent, cpuid->nent);
1730 r = -E2BIG;
1731 if (nent >= cpuid->nent)
1732 goto out_free;
1733
1734 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1735 limit = cpuid_entries[nent - 1].eax;
1736 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1737 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1738 &nent, cpuid->nent);
1739 r = -E2BIG;
1740 if (nent >= cpuid->nent)
1741 goto out_free;
1742
1743 r = -EFAULT;
1744 if (copy_to_user(entries, cpuid_entries,
1745 nent * sizeof(struct kvm_cpuid_entry2)))
1746 goto out_free;
1747 cpuid->nent = nent;
1748 r = 0;
1749
1750 out_free:
1751 vfree(cpuid_entries);
1752 out:
1753 return r;
1754 }
1755
1756 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1757 struct kvm_lapic_state *s)
1758 {
1759 vcpu_load(vcpu);
1760 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1761 vcpu_put(vcpu);
1762
1763 return 0;
1764 }
1765
1766 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1767 struct kvm_lapic_state *s)
1768 {
1769 vcpu_load(vcpu);
1770 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1771 kvm_apic_post_state_restore(vcpu);
1772 update_cr8_intercept(vcpu);
1773 vcpu_put(vcpu);
1774
1775 return 0;
1776 }
1777
1778 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1779 struct kvm_interrupt *irq)
1780 {
1781 if (irq->irq < 0 || irq->irq >= 256)
1782 return -EINVAL;
1783 if (irqchip_in_kernel(vcpu->kvm))
1784 return -ENXIO;
1785 vcpu_load(vcpu);
1786
1787 kvm_queue_interrupt(vcpu, irq->irq, false);
1788
1789 vcpu_put(vcpu);
1790
1791 return 0;
1792 }
1793
1794 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1795 {
1796 vcpu_load(vcpu);
1797 kvm_inject_nmi(vcpu);
1798 vcpu_put(vcpu);
1799
1800 return 0;
1801 }
1802
1803 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1804 struct kvm_tpr_access_ctl *tac)
1805 {
1806 if (tac->flags)
1807 return -EINVAL;
1808 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1809 return 0;
1810 }
1811
1812 static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1813 u64 mcg_cap)
1814 {
1815 int r;
1816 unsigned bank_num = mcg_cap & 0xff, bank;
1817
1818 r = -EINVAL;
1819 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
1820 goto out;
1821 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
1822 goto out;
1823 r = 0;
1824 vcpu->arch.mcg_cap = mcg_cap;
1825 /* Init IA32_MCG_CTL to all 1s */
1826 if (mcg_cap & MCG_CTL_P)
1827 vcpu->arch.mcg_ctl = ~(u64)0;
1828 /* Init IA32_MCi_CTL to all 1s */
1829 for (bank = 0; bank < bank_num; bank++)
1830 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
1831 out:
1832 return r;
1833 }
1834
1835 static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
1836 struct kvm_x86_mce *mce)
1837 {
1838 u64 mcg_cap = vcpu->arch.mcg_cap;
1839 unsigned bank_num = mcg_cap & 0xff;
1840 u64 *banks = vcpu->arch.mce_banks;
1841
1842 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
1843 return -EINVAL;
1844 /*
1845 * if IA32_MCG_CTL is not all 1s, the uncorrected error
1846 * reporting is disabled
1847 */
1848 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1849 vcpu->arch.mcg_ctl != ~(u64)0)
1850 return 0;
1851 banks += 4 * mce->bank;
1852 /*
1853 * if IA32_MCi_CTL is not all 1s, the uncorrected error
1854 * reporting is disabled for the bank
1855 */
1856 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
1857 return 0;
1858 if (mce->status & MCI_STATUS_UC) {
1859 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
1860 !(vcpu->arch.cr4 & X86_CR4_MCE)) {
1861 printk(KERN_DEBUG "kvm: set_mce: "
1862 "injects mce exception while "
1863 "previous one is in progress!\n");
1864 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1865 return 0;
1866 }
1867 if (banks[1] & MCI_STATUS_VAL)
1868 mce->status |= MCI_STATUS_OVER;
1869 banks[2] = mce->addr;
1870 banks[3] = mce->misc;
1871 vcpu->arch.mcg_status = mce->mcg_status;
1872 banks[1] = mce->status;
1873 kvm_queue_exception(vcpu, MC_VECTOR);
1874 } else if (!(banks[1] & MCI_STATUS_VAL)
1875 || !(banks[1] & MCI_STATUS_UC)) {
1876 if (banks[1] & MCI_STATUS_VAL)
1877 mce->status |= MCI_STATUS_OVER;
1878 banks[2] = mce->addr;
1879 banks[3] = mce->misc;
1880 banks[1] = mce->status;
1881 } else
1882 banks[1] |= MCI_STATUS_OVER;
1883 return 0;
1884 }
1885
1886 long kvm_arch_vcpu_ioctl(struct file *filp,
1887 unsigned int ioctl, unsigned long arg)
1888 {
1889 struct kvm_vcpu *vcpu = filp->private_data;
1890 void __user *argp = (void __user *)arg;
1891 int r;
1892 struct kvm_lapic_state *lapic = NULL;
1893
1894 switch (ioctl) {
1895 case KVM_GET_LAPIC: {
1896 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1897
1898 r = -ENOMEM;
1899 if (!lapic)
1900 goto out;
1901 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
1902 if (r)
1903 goto out;
1904 r = -EFAULT;
1905 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
1906 goto out;
1907 r = 0;
1908 break;
1909 }
1910 case KVM_SET_LAPIC: {
1911 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1912 r = -ENOMEM;
1913 if (!lapic)
1914 goto out;
1915 r = -EFAULT;
1916 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1917 goto out;
1918 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
1919 if (r)
1920 goto out;
1921 r = 0;
1922 break;
1923 }
1924 case KVM_INTERRUPT: {
1925 struct kvm_interrupt irq;
1926
1927 r = -EFAULT;
1928 if (copy_from_user(&irq, argp, sizeof irq))
1929 goto out;
1930 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1931 if (r)
1932 goto out;
1933 r = 0;
1934 break;
1935 }
1936 case KVM_NMI: {
1937 r = kvm_vcpu_ioctl_nmi(vcpu);
1938 if (r)
1939 goto out;
1940 r = 0;
1941 break;
1942 }
1943 case KVM_SET_CPUID: {
1944 struct kvm_cpuid __user *cpuid_arg = argp;
1945 struct kvm_cpuid cpuid;
1946
1947 r = -EFAULT;
1948 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1949 goto out;
1950 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1951 if (r)
1952 goto out;
1953 break;
1954 }
1955 case KVM_SET_CPUID2: {
1956 struct kvm_cpuid2 __user *cpuid_arg = argp;
1957 struct kvm_cpuid2 cpuid;
1958
1959 r = -EFAULT;
1960 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1961 goto out;
1962 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1963 cpuid_arg->entries);
1964 if (r)
1965 goto out;
1966 break;
1967 }
1968 case KVM_GET_CPUID2: {
1969 struct kvm_cpuid2 __user *cpuid_arg = argp;
1970 struct kvm_cpuid2 cpuid;
1971
1972 r = -EFAULT;
1973 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1974 goto out;
1975 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1976 cpuid_arg->entries);
1977 if (r)
1978 goto out;
1979 r = -EFAULT;
1980 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1981 goto out;
1982 r = 0;
1983 break;
1984 }
1985 case KVM_GET_MSRS:
1986 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1987 break;
1988 case KVM_SET_MSRS:
1989 r = msr_io(vcpu, argp, do_set_msr, 0);
1990 break;
1991 case KVM_TPR_ACCESS_REPORTING: {
1992 struct kvm_tpr_access_ctl tac;
1993
1994 r = -EFAULT;
1995 if (copy_from_user(&tac, argp, sizeof tac))
1996 goto out;
1997 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1998 if (r)
1999 goto out;
2000 r = -EFAULT;
2001 if (copy_to_user(argp, &tac, sizeof tac))
2002 goto out;
2003 r = 0;
2004 break;
2005 };
2006 case KVM_SET_VAPIC_ADDR: {
2007 struct kvm_vapic_addr va;
2008
2009 r = -EINVAL;
2010 if (!irqchip_in_kernel(vcpu->kvm))
2011 goto out;
2012 r = -EFAULT;
2013 if (copy_from_user(&va, argp, sizeof va))
2014 goto out;
2015 r = 0;
2016 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2017 break;
2018 }
2019 case KVM_X86_SETUP_MCE: {
2020 u64 mcg_cap;
2021
2022 r = -EFAULT;
2023 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2024 goto out;
2025 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2026 break;
2027 }
2028 case KVM_X86_SET_MCE: {
2029 struct kvm_x86_mce mce;
2030
2031 r = -EFAULT;
2032 if (copy_from_user(&mce, argp, sizeof mce))
2033 goto out;
2034 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2035 break;
2036 }
2037 default:
2038 r = -EINVAL;
2039 }
2040 out:
2041 kfree(lapic);
2042 return r;
2043 }
2044
2045 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2046 {
2047 int ret;
2048
2049 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2050 return -1;
2051 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2052 return ret;
2053 }
2054
2055 static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2056 u64 ident_addr)
2057 {
2058 kvm->arch.ept_identity_map_addr = ident_addr;
2059 return 0;
2060 }
2061
2062 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2063 u32 kvm_nr_mmu_pages)
2064 {
2065 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2066 return -EINVAL;
2067
2068 down_write(&kvm->slots_lock);
2069 spin_lock(&kvm->mmu_lock);
2070
2071 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
2072 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
2073
2074 spin_unlock(&kvm->mmu_lock);
2075 up_write(&kvm->slots_lock);
2076 return 0;
2077 }
2078
2079 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2080 {
2081 return kvm->arch.n_alloc_mmu_pages;
2082 }
2083
2084 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2085 {
2086 int i;
2087 struct kvm_mem_alias *alias;
2088
2089 for (i = 0; i < kvm->arch.naliases; ++i) {
2090 alias = &kvm->arch.aliases[i];
2091 if (gfn >= alias->base_gfn
2092 && gfn < alias->base_gfn + alias->npages)
2093 return alias->target_gfn + gfn - alias->base_gfn;
2094 }
2095 return gfn;
2096 }
2097
2098 /*
2099 * Set a new alias region. Aliases map a portion of physical memory into
2100 * another portion. This is useful for memory windows, for example the PC
2101 * VGA region.
2102 */
2103 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2104 struct kvm_memory_alias *alias)
2105 {
2106 int r, n;
2107 struct kvm_mem_alias *p;
2108
2109 r = -EINVAL;
2110 /* General sanity checks */
2111 if (alias->memory_size & (PAGE_SIZE - 1))
2112 goto out;
2113 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2114 goto out;
2115 if (alias->slot >= KVM_ALIAS_SLOTS)
2116 goto out;
2117 if (alias->guest_phys_addr + alias->memory_size
2118 < alias->guest_phys_addr)
2119 goto out;
2120 if (alias->target_phys_addr + alias->memory_size
2121 < alias->target_phys_addr)
2122 goto out;
2123
2124 down_write(&kvm->slots_lock);
2125 spin_lock(&kvm->mmu_lock);
2126
2127 p = &kvm->arch.aliases[alias->slot];
2128 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2129 p->npages = alias->memory_size >> PAGE_SHIFT;
2130 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
2131
2132 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
2133 if (kvm->arch.aliases[n - 1].npages)
2134 break;
2135 kvm->arch.naliases = n;
2136
2137 spin_unlock(&kvm->mmu_lock);
2138 kvm_mmu_zap_all(kvm);
2139
2140 up_write(&kvm->slots_lock);
2141
2142 return 0;
2143
2144 out:
2145 return r;
2146 }
2147
2148 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2149 {
2150 int r;
2151
2152 r = 0;
2153 switch (chip->chip_id) {
2154 case KVM_IRQCHIP_PIC_MASTER:
2155 memcpy(&chip->chip.pic,
2156 &pic_irqchip(kvm)->pics[0],
2157 sizeof(struct kvm_pic_state));
2158 break;
2159 case KVM_IRQCHIP_PIC_SLAVE:
2160 memcpy(&chip->chip.pic,
2161 &pic_irqchip(kvm)->pics[1],
2162 sizeof(struct kvm_pic_state));
2163 break;
2164 case KVM_IRQCHIP_IOAPIC:
2165 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
2166 break;
2167 default:
2168 r = -EINVAL;
2169 break;
2170 }
2171 return r;
2172 }
2173
2174 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2175 {
2176 int r;
2177
2178 r = 0;
2179 switch (chip->chip_id) {
2180 case KVM_IRQCHIP_PIC_MASTER:
2181 spin_lock(&pic_irqchip(kvm)->lock);
2182 memcpy(&pic_irqchip(kvm)->pics[0],
2183 &chip->chip.pic,
2184 sizeof(struct kvm_pic_state));
2185 spin_unlock(&pic_irqchip(kvm)->lock);
2186 break;
2187 case KVM_IRQCHIP_PIC_SLAVE:
2188 spin_lock(&pic_irqchip(kvm)->lock);
2189 memcpy(&pic_irqchip(kvm)->pics[1],
2190 &chip->chip.pic,
2191 sizeof(struct kvm_pic_state));
2192 spin_unlock(&pic_irqchip(kvm)->lock);
2193 break;
2194 case KVM_IRQCHIP_IOAPIC:
2195 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
2196 break;
2197 default:
2198 r = -EINVAL;
2199 break;
2200 }
2201 kvm_pic_update_irq(pic_irqchip(kvm));
2202 return r;
2203 }
2204
2205 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2206 {
2207 int r = 0;
2208
2209 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2210 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
2211 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2212 return r;
2213 }
2214
2215 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2216 {
2217 int r = 0;
2218
2219 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2220 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
2221 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2222 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2223 return r;
2224 }
2225
2226 static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2227 {
2228 int r = 0;
2229
2230 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2231 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2232 sizeof(ps->channels));
2233 ps->flags = kvm->arch.vpit->pit_state.flags;
2234 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2235 return r;
2236 }
2237
2238 static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2239 {
2240 int r = 0, start = 0;
2241 u32 prev_legacy, cur_legacy;
2242 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2243 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2244 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2245 if (!prev_legacy && cur_legacy)
2246 start = 1;
2247 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2248 sizeof(kvm->arch.vpit->pit_state.channels));
2249 kvm->arch.vpit->pit_state.flags = ps->flags;
2250 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
2251 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2252 return r;
2253 }
2254
2255 static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2256 struct kvm_reinject_control *control)
2257 {
2258 if (!kvm->arch.vpit)
2259 return -ENXIO;
2260 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2261 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
2262 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2263 return 0;
2264 }
2265
2266 /*
2267 * Get (and clear) the dirty memory log for a memory slot.
2268 */
2269 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2270 struct kvm_dirty_log *log)
2271 {
2272 int r;
2273 int n;
2274 struct kvm_memory_slot *memslot;
2275 int is_dirty = 0;
2276
2277 down_write(&kvm->slots_lock);
2278
2279 r = kvm_get_dirty_log(kvm, log, &is_dirty);
2280 if (r)
2281 goto out;
2282
2283 /* If nothing is dirty, don't bother messing with page tables. */
2284 if (is_dirty) {
2285 spin_lock(&kvm->mmu_lock);
2286 kvm_mmu_slot_remove_write_access(kvm, log->slot);
2287 spin_unlock(&kvm->mmu_lock);
2288 memslot = &kvm->memslots[log->slot];
2289 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2290 memset(memslot->dirty_bitmap, 0, n);
2291 }
2292 r = 0;
2293 out:
2294 up_write(&kvm->slots_lock);
2295 return r;
2296 }
2297
2298 long kvm_arch_vm_ioctl(struct file *filp,
2299 unsigned int ioctl, unsigned long arg)
2300 {
2301 struct kvm *kvm = filp->private_data;
2302 void __user *argp = (void __user *)arg;
2303 int r = -ENOTTY;
2304 /*
2305 * This union makes it completely explicit to gcc-3.x
2306 * that these two variables' stack usage should be
2307 * combined, not added together.
2308 */
2309 union {
2310 struct kvm_pit_state ps;
2311 struct kvm_pit_state2 ps2;
2312 struct kvm_memory_alias alias;
2313 struct kvm_pit_config pit_config;
2314 } u;
2315
2316 switch (ioctl) {
2317 case KVM_SET_TSS_ADDR:
2318 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2319 if (r < 0)
2320 goto out;
2321 break;
2322 case KVM_SET_IDENTITY_MAP_ADDR: {
2323 u64 ident_addr;
2324
2325 r = -EFAULT;
2326 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2327 goto out;
2328 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2329 if (r < 0)
2330 goto out;
2331 break;
2332 }
2333 case KVM_SET_MEMORY_REGION: {
2334 struct kvm_memory_region kvm_mem;
2335 struct kvm_userspace_memory_region kvm_userspace_mem;
2336
2337 r = -EFAULT;
2338 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2339 goto out;
2340 kvm_userspace_mem.slot = kvm_mem.slot;
2341 kvm_userspace_mem.flags = kvm_mem.flags;
2342 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2343 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2344 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2345 if (r)
2346 goto out;
2347 break;
2348 }
2349 case KVM_SET_NR_MMU_PAGES:
2350 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2351 if (r)
2352 goto out;
2353 break;
2354 case KVM_GET_NR_MMU_PAGES:
2355 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2356 break;
2357 case KVM_SET_MEMORY_ALIAS:
2358 r = -EFAULT;
2359 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
2360 goto out;
2361 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
2362 if (r)
2363 goto out;
2364 break;
2365 case KVM_CREATE_IRQCHIP:
2366 r = -ENOMEM;
2367 kvm->arch.vpic = kvm_create_pic(kvm);
2368 if (kvm->arch.vpic) {
2369 r = kvm_ioapic_init(kvm);
2370 if (r) {
2371 kfree(kvm->arch.vpic);
2372 kvm->arch.vpic = NULL;
2373 goto out;
2374 }
2375 } else
2376 goto out;
2377 r = kvm_setup_default_irq_routing(kvm);
2378 if (r) {
2379 kfree(kvm->arch.vpic);
2380 kfree(kvm->arch.vioapic);
2381 goto out;
2382 }
2383 break;
2384 case KVM_CREATE_PIT:
2385 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2386 goto create_pit;
2387 case KVM_CREATE_PIT2:
2388 r = -EFAULT;
2389 if (copy_from_user(&u.pit_config, argp,
2390 sizeof(struct kvm_pit_config)))
2391 goto out;
2392 create_pit:
2393 down_write(&kvm->slots_lock);
2394 r = -EEXIST;
2395 if (kvm->arch.vpit)
2396 goto create_pit_unlock;
2397 r = -ENOMEM;
2398 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
2399 if (kvm->arch.vpit)
2400 r = 0;
2401 create_pit_unlock:
2402 up_write(&kvm->slots_lock);
2403 break;
2404 case KVM_IRQ_LINE_STATUS:
2405 case KVM_IRQ_LINE: {
2406 struct kvm_irq_level irq_event;
2407
2408 r = -EFAULT;
2409 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2410 goto out;
2411 if (irqchip_in_kernel(kvm)) {
2412 __s32 status;
2413 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2414 irq_event.irq, irq_event.level);
2415 if (ioctl == KVM_IRQ_LINE_STATUS) {
2416 irq_event.status = status;
2417 if (copy_to_user(argp, &irq_event,
2418 sizeof irq_event))
2419 goto out;
2420 }
2421 r = 0;
2422 }
2423 break;
2424 }
2425 case KVM_GET_IRQCHIP: {
2426 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2427 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
2428
2429 r = -ENOMEM;
2430 if (!chip)
2431 goto out;
2432 r = -EFAULT;
2433 if (copy_from_user(chip, argp, sizeof *chip))
2434 goto get_irqchip_out;
2435 r = -ENXIO;
2436 if (!irqchip_in_kernel(kvm))
2437 goto get_irqchip_out;
2438 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2439 if (r)
2440 goto get_irqchip_out;
2441 r = -EFAULT;
2442 if (copy_to_user(argp, chip, sizeof *chip))
2443 goto get_irqchip_out;
2444 r = 0;
2445 get_irqchip_out:
2446 kfree(chip);
2447 if (r)
2448 goto out;
2449 break;
2450 }
2451 case KVM_SET_IRQCHIP: {
2452 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2453 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
2454
2455 r = -ENOMEM;
2456 if (!chip)
2457 goto out;
2458 r = -EFAULT;
2459 if (copy_from_user(chip, argp, sizeof *chip))
2460 goto set_irqchip_out;
2461 r = -ENXIO;
2462 if (!irqchip_in_kernel(kvm))
2463 goto set_irqchip_out;
2464 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2465 if (r)
2466 goto set_irqchip_out;
2467 r = 0;
2468 set_irqchip_out:
2469 kfree(chip);
2470 if (r)
2471 goto out;
2472 break;
2473 }
2474 case KVM_GET_PIT: {
2475 r = -EFAULT;
2476 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
2477 goto out;
2478 r = -ENXIO;
2479 if (!kvm->arch.vpit)
2480 goto out;
2481 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
2482 if (r)
2483 goto out;
2484 r = -EFAULT;
2485 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
2486 goto out;
2487 r = 0;
2488 break;
2489 }
2490 case KVM_SET_PIT: {
2491 r = -EFAULT;
2492 if (copy_from_user(&u.ps, argp, sizeof u.ps))
2493 goto out;
2494 r = -ENXIO;
2495 if (!kvm->arch.vpit)
2496 goto out;
2497 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
2498 if (r)
2499 goto out;
2500 r = 0;
2501 break;
2502 }
2503 case KVM_GET_PIT2: {
2504 r = -ENXIO;
2505 if (!kvm->arch.vpit)
2506 goto out;
2507 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2508 if (r)
2509 goto out;
2510 r = -EFAULT;
2511 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2512 goto out;
2513 r = 0;
2514 break;
2515 }
2516 case KVM_SET_PIT2: {
2517 r = -EFAULT;
2518 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2519 goto out;
2520 r = -ENXIO;
2521 if (!kvm->arch.vpit)
2522 goto out;
2523 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2524 if (r)
2525 goto out;
2526 r = 0;
2527 break;
2528 }
2529 case KVM_REINJECT_CONTROL: {
2530 struct kvm_reinject_control control;
2531 r = -EFAULT;
2532 if (copy_from_user(&control, argp, sizeof(control)))
2533 goto out;
2534 r = kvm_vm_ioctl_reinject(kvm, &control);
2535 if (r)
2536 goto out;
2537 r = 0;
2538 break;
2539 }
2540 case KVM_XEN_HVM_CONFIG: {
2541 r = -EFAULT;
2542 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2543 sizeof(struct kvm_xen_hvm_config)))
2544 goto out;
2545 r = -EINVAL;
2546 if (kvm->arch.xen_hvm_config.flags)
2547 goto out;
2548 r = 0;
2549 break;
2550 }
2551 case KVM_SET_CLOCK: {
2552 struct timespec now;
2553 struct kvm_clock_data user_ns;
2554 u64 now_ns;
2555 s64 delta;
2556
2557 r = -EFAULT;
2558 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2559 goto out;
2560
2561 r = -EINVAL;
2562 if (user_ns.flags)
2563 goto out;
2564
2565 r = 0;
2566 ktime_get_ts(&now);
2567 now_ns = timespec_to_ns(&now);
2568 delta = user_ns.clock - now_ns;
2569 kvm->arch.kvmclock_offset = delta;
2570 break;
2571 }
2572 case KVM_GET_CLOCK: {
2573 struct timespec now;
2574 struct kvm_clock_data user_ns;
2575 u64 now_ns;
2576
2577 ktime_get_ts(&now);
2578 now_ns = timespec_to_ns(&now);
2579 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2580 user_ns.flags = 0;
2581
2582 r = -EFAULT;
2583 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2584 goto out;
2585 r = 0;
2586 break;
2587 }
2588
2589 default:
2590 ;
2591 }
2592 out:
2593 return r;
2594 }
2595
2596 static void kvm_init_msr_list(void)
2597 {
2598 u32 dummy[2];
2599 unsigned i, j;
2600
2601 /* skip the first msrs in the list. KVM-specific */
2602 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
2603 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2604 continue;
2605 if (j < i)
2606 msrs_to_save[j] = msrs_to_save[i];
2607 j++;
2608 }
2609 num_msrs_to_save = j;
2610 }
2611
2612 static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
2613 const void *v)
2614 {
2615 if (vcpu->arch.apic &&
2616 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
2617 return 0;
2618
2619 return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
2620 }
2621
2622 static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
2623 {
2624 if (vcpu->arch.apic &&
2625 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
2626 return 0;
2627
2628 return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
2629 }
2630
2631 static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2632 struct kvm_vcpu *vcpu)
2633 {
2634 void *data = val;
2635 int r = X86EMUL_CONTINUE;
2636
2637 while (bytes) {
2638 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2639 unsigned offset = addr & (PAGE_SIZE-1);
2640 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
2641 int ret;
2642
2643 if (gpa == UNMAPPED_GVA) {
2644 r = X86EMUL_PROPAGATE_FAULT;
2645 goto out;
2646 }
2647 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
2648 if (ret < 0) {
2649 r = X86EMUL_UNHANDLEABLE;
2650 goto out;
2651 }
2652
2653 bytes -= toread;
2654 data += toread;
2655 addr += toread;
2656 }
2657 out:
2658 return r;
2659 }
2660
2661 static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2662 struct kvm_vcpu *vcpu)
2663 {
2664 void *data = val;
2665 int r = X86EMUL_CONTINUE;
2666
2667 while (bytes) {
2668 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2669 unsigned offset = addr & (PAGE_SIZE-1);
2670 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2671 int ret;
2672
2673 if (gpa == UNMAPPED_GVA) {
2674 r = X86EMUL_PROPAGATE_FAULT;
2675 goto out;
2676 }
2677 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2678 if (ret < 0) {
2679 r = X86EMUL_UNHANDLEABLE;
2680 goto out;
2681 }
2682
2683 bytes -= towrite;
2684 data += towrite;
2685 addr += towrite;
2686 }
2687 out:
2688 return r;
2689 }
2690
2691
2692 static int emulator_read_emulated(unsigned long addr,
2693 void *val,
2694 unsigned int bytes,
2695 struct kvm_vcpu *vcpu)
2696 {
2697 gpa_t gpa;
2698
2699 if (vcpu->mmio_read_completed) {
2700 memcpy(val, vcpu->mmio_data, bytes);
2701 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
2702 vcpu->mmio_phys_addr, *(u64 *)val);
2703 vcpu->mmio_read_completed = 0;
2704 return X86EMUL_CONTINUE;
2705 }
2706
2707 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2708
2709 /* For APIC access vmexit */
2710 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2711 goto mmio;
2712
2713 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2714 == X86EMUL_CONTINUE)
2715 return X86EMUL_CONTINUE;
2716 if (gpa == UNMAPPED_GVA)
2717 return X86EMUL_PROPAGATE_FAULT;
2718
2719 mmio:
2720 /*
2721 * Is this MMIO handled locally?
2722 */
2723 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
2724 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
2725 return X86EMUL_CONTINUE;
2726 }
2727
2728 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
2729
2730 vcpu->mmio_needed = 1;
2731 vcpu->mmio_phys_addr = gpa;
2732 vcpu->mmio_size = bytes;
2733 vcpu->mmio_is_write = 0;
2734
2735 return X86EMUL_UNHANDLEABLE;
2736 }
2737
2738 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
2739 const void *val, int bytes)
2740 {
2741 int ret;
2742
2743 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2744 if (ret < 0)
2745 return 0;
2746 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
2747 return 1;
2748 }
2749
2750 static int emulator_write_emulated_onepage(unsigned long addr,
2751 const void *val,
2752 unsigned int bytes,
2753 struct kvm_vcpu *vcpu)
2754 {
2755 gpa_t gpa;
2756
2757 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2758
2759 if (gpa == UNMAPPED_GVA) {
2760 kvm_inject_page_fault(vcpu, addr, 2);
2761 return X86EMUL_PROPAGATE_FAULT;
2762 }
2763
2764 /* For APIC access vmexit */
2765 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2766 goto mmio;
2767
2768 if (emulator_write_phys(vcpu, gpa, val, bytes))
2769 return X86EMUL_CONTINUE;
2770
2771 mmio:
2772 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
2773 /*
2774 * Is this MMIO handled locally?
2775 */
2776 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
2777 return X86EMUL_CONTINUE;
2778
2779 vcpu->mmio_needed = 1;
2780 vcpu->mmio_phys_addr = gpa;
2781 vcpu->mmio_size = bytes;
2782 vcpu->mmio_is_write = 1;
2783 memcpy(vcpu->mmio_data, val, bytes);
2784
2785 return X86EMUL_CONTINUE;
2786 }
2787
2788 int emulator_write_emulated(unsigned long addr,
2789 const void *val,
2790 unsigned int bytes,
2791 struct kvm_vcpu *vcpu)
2792 {
2793 /* Crossing a page boundary? */
2794 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2795 int rc, now;
2796
2797 now = -addr & ~PAGE_MASK;
2798 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2799 if (rc != X86EMUL_CONTINUE)
2800 return rc;
2801 addr += now;
2802 val += now;
2803 bytes -= now;
2804 }
2805 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2806 }
2807 EXPORT_SYMBOL_GPL(emulator_write_emulated);
2808
2809 static int emulator_cmpxchg_emulated(unsigned long addr,
2810 const void *old,
2811 const void *new,
2812 unsigned int bytes,
2813 struct kvm_vcpu *vcpu)
2814 {
2815 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
2816 #ifndef CONFIG_X86_64
2817 /* guests cmpxchg8b have to be emulated atomically */
2818 if (bytes == 8) {
2819 gpa_t gpa;
2820 struct page *page;
2821 char *kaddr;
2822 u64 val;
2823
2824 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2825
2826 if (gpa == UNMAPPED_GVA ||
2827 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2828 goto emul_write;
2829
2830 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2831 goto emul_write;
2832
2833 val = *(u64 *)new;
2834
2835 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2836
2837 kaddr = kmap_atomic(page, KM_USER0);
2838 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2839 kunmap_atomic(kaddr, KM_USER0);
2840 kvm_release_page_dirty(page);
2841 }
2842 emul_write:
2843 #endif
2844
2845 return emulator_write_emulated(addr, new, bytes, vcpu);
2846 }
2847
2848 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2849 {
2850 return kvm_x86_ops->get_segment_base(vcpu, seg);
2851 }
2852
2853 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2854 {
2855 kvm_mmu_invlpg(vcpu, address);
2856 return X86EMUL_CONTINUE;
2857 }
2858
2859 int emulate_clts(struct kvm_vcpu *vcpu)
2860 {
2861 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
2862 return X86EMUL_CONTINUE;
2863 }
2864
2865 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2866 {
2867 struct kvm_vcpu *vcpu = ctxt->vcpu;
2868
2869 switch (dr) {
2870 case 0 ... 3:
2871 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2872 return X86EMUL_CONTINUE;
2873 default:
2874 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
2875 return X86EMUL_UNHANDLEABLE;
2876 }
2877 }
2878
2879 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2880 {
2881 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2882 int exception;
2883
2884 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2885 if (exception) {
2886 /* FIXME: better handling */
2887 return X86EMUL_UNHANDLEABLE;
2888 }
2889 return X86EMUL_CONTINUE;
2890 }
2891
2892 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2893 {
2894 u8 opcodes[4];
2895 unsigned long rip = kvm_rip_read(vcpu);
2896 unsigned long rip_linear;
2897
2898 if (!printk_ratelimit())
2899 return;
2900
2901 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2902
2903 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
2904
2905 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2906 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
2907 }
2908 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2909
2910 static struct x86_emulate_ops emulate_ops = {
2911 .read_std = kvm_read_guest_virt,
2912 .read_emulated = emulator_read_emulated,
2913 .write_emulated = emulator_write_emulated,
2914 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2915 };
2916
2917 static void cache_all_regs(struct kvm_vcpu *vcpu)
2918 {
2919 kvm_register_read(vcpu, VCPU_REGS_RAX);
2920 kvm_register_read(vcpu, VCPU_REGS_RSP);
2921 kvm_register_read(vcpu, VCPU_REGS_RIP);
2922 vcpu->arch.regs_dirty = ~0;
2923 }
2924
2925 int emulate_instruction(struct kvm_vcpu *vcpu,
2926 unsigned long cr2,
2927 u16 error_code,
2928 int emulation_type)
2929 {
2930 int r, shadow_mask;
2931 struct decode_cache *c;
2932 struct kvm_run *run = vcpu->run;
2933
2934 kvm_clear_exception_queue(vcpu);
2935 vcpu->arch.mmio_fault_cr2 = cr2;
2936 /*
2937 * TODO: fix emulate.c to use guest_read/write_register
2938 * instead of direct ->regs accesses, can save hundred cycles
2939 * on Intel for instructions that don't read/change RSP, for
2940 * for example.
2941 */
2942 cache_all_regs(vcpu);
2943
2944 vcpu->mmio_is_write = 0;
2945 vcpu->arch.pio.string = 0;
2946
2947 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
2948 int cs_db, cs_l;
2949 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2950
2951 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2952 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
2953 vcpu->arch.emulate_ctxt.mode =
2954 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
2955 ? X86EMUL_MODE_REAL : cs_l
2956 ? X86EMUL_MODE_PROT64 : cs_db
2957 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2958
2959 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2960
2961 /* Only allow emulation of specific instructions on #UD
2962 * (namely VMMCALL, sysenter, sysexit, syscall)*/
2963 c = &vcpu->arch.emulate_ctxt.decode;
2964 if (emulation_type & EMULTYPE_TRAP_UD) {
2965 if (!c->twobyte)
2966 return EMULATE_FAIL;
2967 switch (c->b) {
2968 case 0x01: /* VMMCALL */
2969 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2970 return EMULATE_FAIL;
2971 break;
2972 case 0x34: /* sysenter */
2973 case 0x35: /* sysexit */
2974 if (c->modrm_mod != 0 || c->modrm_rm != 0)
2975 return EMULATE_FAIL;
2976 break;
2977 case 0x05: /* syscall */
2978 if (c->modrm_mod != 0 || c->modrm_rm != 0)
2979 return EMULATE_FAIL;
2980 break;
2981 default:
2982 return EMULATE_FAIL;
2983 }
2984
2985 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
2986 return EMULATE_FAIL;
2987 }
2988
2989 ++vcpu->stat.insn_emulation;
2990 if (r) {
2991 ++vcpu->stat.insn_emulation_fail;
2992 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2993 return EMULATE_DONE;
2994 return EMULATE_FAIL;
2995 }
2996 }
2997
2998 if (emulation_type & EMULTYPE_SKIP) {
2999 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3000 return EMULATE_DONE;
3001 }
3002
3003 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
3004 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3005
3006 if (r == 0)
3007 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
3008
3009 if (vcpu->arch.pio.string)
3010 return EMULATE_DO_MMIO;
3011
3012 if ((r || vcpu->mmio_is_write) && run) {
3013 run->exit_reason = KVM_EXIT_MMIO;
3014 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3015 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3016 run->mmio.len = vcpu->mmio_size;
3017 run->mmio.is_write = vcpu->mmio_is_write;
3018 }
3019
3020 if (r) {
3021 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3022 return EMULATE_DONE;
3023 if (!vcpu->mmio_needed) {
3024 kvm_report_emulation_failure(vcpu, "mmio");
3025 return EMULATE_FAIL;
3026 }
3027 return EMULATE_DO_MMIO;
3028 }
3029
3030 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
3031
3032 if (vcpu->mmio_is_write) {
3033 vcpu->mmio_needed = 0;
3034 return EMULATE_DO_MMIO;
3035 }
3036
3037 return EMULATE_DONE;
3038 }
3039 EXPORT_SYMBOL_GPL(emulate_instruction);
3040
3041 static int pio_copy_data(struct kvm_vcpu *vcpu)
3042 {
3043 void *p = vcpu->arch.pio_data;
3044 gva_t q = vcpu->arch.pio.guest_gva;
3045 unsigned bytes;
3046 int ret;
3047
3048 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3049 if (vcpu->arch.pio.in)
3050 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
3051 else
3052 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
3053 return ret;
3054 }
3055
3056 int complete_pio(struct kvm_vcpu *vcpu)
3057 {
3058 struct kvm_pio_request *io = &vcpu->arch.pio;
3059 long delta;
3060 int r;
3061 unsigned long val;
3062
3063 if (!io->string) {
3064 if (io->in) {
3065 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3066 memcpy(&val, vcpu->arch.pio_data, io->size);
3067 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3068 }
3069 } else {
3070 if (io->in) {
3071 r = pio_copy_data(vcpu);
3072 if (r)
3073 return r;
3074 }
3075
3076 delta = 1;
3077 if (io->rep) {
3078 delta *= io->cur_count;
3079 /*
3080 * The size of the register should really depend on
3081 * current address size.
3082 */
3083 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3084 val -= delta;
3085 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
3086 }
3087 if (io->down)
3088 delta = -delta;
3089 delta *= io->size;
3090 if (io->in) {
3091 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3092 val += delta;
3093 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3094 } else {
3095 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3096 val += delta;
3097 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3098 }
3099 }
3100
3101 io->count -= io->cur_count;
3102 io->cur_count = 0;
3103
3104 return 0;
3105 }
3106
3107 static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
3108 {
3109 /* TODO: String I/O for in kernel device */
3110 int r;
3111
3112 if (vcpu->arch.pio.in)
3113 r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3114 vcpu->arch.pio.size, pd);
3115 else
3116 r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3117 vcpu->arch.pio.size, pd);
3118 return r;
3119 }
3120
3121 static int pio_string_write(struct kvm_vcpu *vcpu)
3122 {
3123 struct kvm_pio_request *io = &vcpu->arch.pio;
3124 void *pd = vcpu->arch.pio_data;
3125 int i, r = 0;
3126
3127 for (i = 0; i < io->cur_count; i++) {
3128 if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
3129 io->port, io->size, pd)) {
3130 r = -EOPNOTSUPP;
3131 break;
3132 }
3133 pd += io->size;
3134 }
3135 return r;
3136 }
3137
3138 int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
3139 {
3140 unsigned long val;
3141
3142 vcpu->run->exit_reason = KVM_EXIT_IO;
3143 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
3144 vcpu->run->io.size = vcpu->arch.pio.size = size;
3145 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3146 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3147 vcpu->run->io.port = vcpu->arch.pio.port = port;
3148 vcpu->arch.pio.in = in;
3149 vcpu->arch.pio.string = 0;
3150 vcpu->arch.pio.down = 0;
3151 vcpu->arch.pio.rep = 0;
3152
3153 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3154 size, 1);
3155
3156 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3157 memcpy(vcpu->arch.pio_data, &val, 4);
3158
3159 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3160 complete_pio(vcpu);
3161 return 1;
3162 }
3163 return 0;
3164 }
3165 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3166
3167 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
3168 int size, unsigned long count, int down,
3169 gva_t address, int rep, unsigned port)
3170 {
3171 unsigned now, in_page;
3172 int ret = 0;
3173
3174 vcpu->run->exit_reason = KVM_EXIT_IO;
3175 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
3176 vcpu->run->io.size = vcpu->arch.pio.size = size;
3177 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3178 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3179 vcpu->run->io.port = vcpu->arch.pio.port = port;
3180 vcpu->arch.pio.in = in;
3181 vcpu->arch.pio.string = 1;
3182 vcpu->arch.pio.down = down;
3183 vcpu->arch.pio.rep = rep;
3184
3185 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3186 size, count);
3187
3188 if (!count) {
3189 kvm_x86_ops->skip_emulated_instruction(vcpu);
3190 return 1;
3191 }
3192
3193 if (!down)
3194 in_page = PAGE_SIZE - offset_in_page(address);
3195 else
3196 in_page = offset_in_page(address) + size;
3197 now = min(count, (unsigned long)in_page / size);
3198 if (!now)
3199 now = 1;
3200 if (down) {
3201 /*
3202 * String I/O in reverse. Yuck. Kill the guest, fix later.
3203 */
3204 pr_unimpl(vcpu, "guest string pio down\n");
3205 kvm_inject_gp(vcpu, 0);
3206 return 1;
3207 }
3208 vcpu->run->io.count = now;
3209 vcpu->arch.pio.cur_count = now;
3210
3211 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
3212 kvm_x86_ops->skip_emulated_instruction(vcpu);
3213
3214 vcpu->arch.pio.guest_gva = address;
3215
3216 if (!vcpu->arch.pio.in) {
3217 /* string PIO write */
3218 ret = pio_copy_data(vcpu);
3219 if (ret == X86EMUL_PROPAGATE_FAULT) {
3220 kvm_inject_gp(vcpu, 0);
3221 return 1;
3222 }
3223 if (ret == 0 && !pio_string_write(vcpu)) {
3224 complete_pio(vcpu);
3225 if (vcpu->arch.pio.count == 0)
3226 ret = 1;
3227 }
3228 }
3229 /* no string PIO read support yet */
3230
3231 return ret;
3232 }
3233 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3234
3235 static void bounce_off(void *info)
3236 {
3237 /* nothing */
3238 }
3239
3240 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3241 void *data)
3242 {
3243 struct cpufreq_freqs *freq = data;
3244 struct kvm *kvm;
3245 struct kvm_vcpu *vcpu;
3246 int i, send_ipi = 0;
3247
3248 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3249 return 0;
3250 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3251 return 0;
3252 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
3253
3254 spin_lock(&kvm_lock);
3255 list_for_each_entry(kvm, &vm_list, vm_list) {
3256 kvm_for_each_vcpu(i, vcpu, kvm) {
3257 if (vcpu->cpu != freq->cpu)
3258 continue;
3259 if (!kvm_request_guest_time_update(vcpu))
3260 continue;
3261 if (vcpu->cpu != smp_processor_id())
3262 send_ipi++;
3263 }
3264 }
3265 spin_unlock(&kvm_lock);
3266
3267 if (freq->old < freq->new && send_ipi) {
3268 /*
3269 * We upscale the frequency. Must make the guest
3270 * doesn't see old kvmclock values while running with
3271 * the new frequency, otherwise we risk the guest sees
3272 * time go backwards.
3273 *
3274 * In case we update the frequency for another cpu
3275 * (which might be in guest context) send an interrupt
3276 * to kick the cpu out of guest context. Next time
3277 * guest context is entered kvmclock will be updated,
3278 * so the guest will not see stale values.
3279 */
3280 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3281 }
3282 return 0;
3283 }
3284
3285 static struct notifier_block kvmclock_cpufreq_notifier_block = {
3286 .notifier_call = kvmclock_cpufreq_notifier
3287 };
3288
3289 static void kvm_timer_init(void)
3290 {
3291 int cpu;
3292
3293 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
3294 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3295 CPUFREQ_TRANSITION_NOTIFIER);
3296 for_each_online_cpu(cpu) {
3297 unsigned long khz = cpufreq_get(cpu);
3298 if (!khz)
3299 khz = tsc_khz;
3300 per_cpu(cpu_tsc_khz, cpu) = khz;
3301 }
3302 } else {
3303 for_each_possible_cpu(cpu)
3304 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
3305 }
3306 }
3307
3308 int kvm_arch_init(void *opaque)
3309 {
3310 int r;
3311 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3312
3313 if (kvm_x86_ops) {
3314 printk(KERN_ERR "kvm: already loaded the other module\n");
3315 r = -EEXIST;
3316 goto out;
3317 }
3318
3319 if (!ops->cpu_has_kvm_support()) {
3320 printk(KERN_ERR "kvm: no hardware support\n");
3321 r = -EOPNOTSUPP;
3322 goto out;
3323 }
3324 if (ops->disabled_by_bios()) {
3325 printk(KERN_ERR "kvm: disabled by bios\n");
3326 r = -EOPNOTSUPP;
3327 goto out;
3328 }
3329
3330 r = kvm_mmu_module_init();
3331 if (r)
3332 goto out;
3333
3334 kvm_init_msr_list();
3335
3336 kvm_x86_ops = ops;
3337 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3338 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3339 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
3340 PT_DIRTY_MASK, PT64_NX_MASK, 0);
3341
3342 kvm_timer_init();
3343
3344 return 0;
3345
3346 out:
3347 return r;
3348 }
3349
3350 void kvm_arch_exit(void)
3351 {
3352 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3353 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3354 CPUFREQ_TRANSITION_NOTIFIER);
3355 kvm_x86_ops = NULL;
3356 kvm_mmu_module_exit();
3357 }
3358
3359 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3360 {
3361 ++vcpu->stat.halt_exits;
3362 if (irqchip_in_kernel(vcpu->kvm)) {
3363 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
3364 return 1;
3365 } else {
3366 vcpu->run->exit_reason = KVM_EXIT_HLT;
3367 return 0;
3368 }
3369 }
3370 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3371
3372 static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3373 unsigned long a1)
3374 {
3375 if (is_long_mode(vcpu))
3376 return a0;
3377 else
3378 return a0 | ((gpa_t)a1 << 32);
3379 }
3380
3381 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3382 {
3383 unsigned long nr, a0, a1, a2, a3, ret;
3384 int r = 1;
3385
3386 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3387 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3388 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3389 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3390 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
3391
3392 trace_kvm_hypercall(nr, a0, a1, a2, a3);
3393
3394 if (!is_long_mode(vcpu)) {
3395 nr &= 0xFFFFFFFF;
3396 a0 &= 0xFFFFFFFF;
3397 a1 &= 0xFFFFFFFF;
3398 a2 &= 0xFFFFFFFF;
3399 a3 &= 0xFFFFFFFF;
3400 }
3401
3402 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3403 ret = -KVM_EPERM;
3404 goto out;
3405 }
3406
3407 switch (nr) {
3408 case KVM_HC_VAPIC_POLL_IRQ:
3409 ret = 0;
3410 break;
3411 case KVM_HC_MMU_OP:
3412 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3413 break;
3414 default:
3415 ret = -KVM_ENOSYS;
3416 break;
3417 }
3418 out:
3419 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3420 ++vcpu->stat.hypercalls;
3421 return r;
3422 }
3423 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3424
3425 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3426 {
3427 char instruction[3];
3428 int ret = 0;
3429 unsigned long rip = kvm_rip_read(vcpu);
3430
3431
3432 /*
3433 * Blow out the MMU to ensure that no other VCPU has an active mapping
3434 * to ensure that the updated hypercall appears atomically across all
3435 * VCPUs.
3436 */
3437 kvm_mmu_zap_all(vcpu->kvm);
3438
3439 kvm_x86_ops->patch_hypercall(vcpu, instruction);
3440 if (emulator_write_emulated(rip, instruction, 3, vcpu)
3441 != X86EMUL_CONTINUE)
3442 ret = -EFAULT;
3443
3444 return ret;
3445 }
3446
3447 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3448 {
3449 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3450 }
3451
3452 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3453 {
3454 struct descriptor_table dt = { limit, base };
3455
3456 kvm_x86_ops->set_gdt(vcpu, &dt);
3457 }
3458
3459 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3460 {
3461 struct descriptor_table dt = { limit, base };
3462
3463 kvm_x86_ops->set_idt(vcpu, &dt);
3464 }
3465
3466 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3467 unsigned long *rflags)
3468 {
3469 kvm_lmsw(vcpu, msw);
3470 *rflags = kvm_get_rflags(vcpu);
3471 }
3472
3473 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3474 {
3475 unsigned long value;
3476
3477 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3478 switch (cr) {
3479 case 0:
3480 value = vcpu->arch.cr0;
3481 break;
3482 case 2:
3483 value = vcpu->arch.cr2;
3484 break;
3485 case 3:
3486 value = vcpu->arch.cr3;
3487 break;
3488 case 4:
3489 value = vcpu->arch.cr4;
3490 break;
3491 case 8:
3492 value = kvm_get_cr8(vcpu);
3493 break;
3494 default:
3495 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3496 return 0;
3497 }
3498
3499 return value;
3500 }
3501
3502 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3503 unsigned long *rflags)
3504 {
3505 switch (cr) {
3506 case 0:
3507 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
3508 *rflags = kvm_get_rflags(vcpu);
3509 break;
3510 case 2:
3511 vcpu->arch.cr2 = val;
3512 break;
3513 case 3:
3514 kvm_set_cr3(vcpu, val);
3515 break;
3516 case 4:
3517 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
3518 break;
3519 case 8:
3520 kvm_set_cr8(vcpu, val & 0xfUL);
3521 break;
3522 default:
3523 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3524 }
3525 }
3526
3527 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
3528 {
3529 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
3530 int j, nent = vcpu->arch.cpuid_nent;
3531
3532 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
3533 /* when no next entry is found, the current entry[i] is reselected */
3534 for (j = i + 1; ; j = (j + 1) % nent) {
3535 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
3536 if (ej->function == e->function) {
3537 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
3538 return j;
3539 }
3540 }
3541 return 0; /* silence gcc, even though control never reaches here */
3542 }
3543
3544 /* find an entry with matching function, matching index (if needed), and that
3545 * should be read next (if it's stateful) */
3546 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3547 u32 function, u32 index)
3548 {
3549 if (e->function != function)
3550 return 0;
3551 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3552 return 0;
3553 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
3554 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
3555 return 0;
3556 return 1;
3557 }
3558
3559 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3560 u32 function, u32 index)
3561 {
3562 int i;
3563 struct kvm_cpuid_entry2 *best = NULL;
3564
3565 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
3566 struct kvm_cpuid_entry2 *e;
3567
3568 e = &vcpu->arch.cpuid_entries[i];
3569 if (is_matching_cpuid_entry(e, function, index)) {
3570 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3571 move_to_next_stateful_cpuid_entry(vcpu, i);
3572 best = e;
3573 break;
3574 }
3575 /*
3576 * Both basic or both extended?
3577 */
3578 if (((e->function ^ function) & 0x80000000) == 0)
3579 if (!best || e->function > best->function)
3580 best = e;
3581 }
3582 return best;
3583 }
3584
3585 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3586 {
3587 struct kvm_cpuid_entry2 *best;
3588
3589 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3590 if (best)
3591 return best->eax & 0xff;
3592 return 36;
3593 }
3594
3595 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3596 {
3597 u32 function, index;
3598 struct kvm_cpuid_entry2 *best;
3599
3600 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3601 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3602 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3603 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3604 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3605 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3606 best = kvm_find_cpuid_entry(vcpu, function, index);
3607 if (best) {
3608 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3609 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3610 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3611 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
3612 }
3613 kvm_x86_ops->skip_emulated_instruction(vcpu);
3614 trace_kvm_cpuid(function,
3615 kvm_register_read(vcpu, VCPU_REGS_RAX),
3616 kvm_register_read(vcpu, VCPU_REGS_RBX),
3617 kvm_register_read(vcpu, VCPU_REGS_RCX),
3618 kvm_register_read(vcpu, VCPU_REGS_RDX));
3619 }
3620 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
3621
3622 /*
3623 * Check if userspace requested an interrupt window, and that the
3624 * interrupt window is open.
3625 *
3626 * No need to exit to userspace if we already have an interrupt queued.
3627 */
3628 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
3629 {
3630 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
3631 vcpu->run->request_interrupt_window &&
3632 kvm_arch_interrupt_allowed(vcpu));
3633 }
3634
3635 static void post_kvm_run_save(struct kvm_vcpu *vcpu)
3636 {
3637 struct kvm_run *kvm_run = vcpu->run;
3638
3639 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
3640 kvm_run->cr8 = kvm_get_cr8(vcpu);
3641 kvm_run->apic_base = kvm_get_apic_base(vcpu);
3642 if (irqchip_in_kernel(vcpu->kvm))
3643 kvm_run->ready_for_interrupt_injection = 1;
3644 else
3645 kvm_run->ready_for_interrupt_injection =
3646 kvm_arch_interrupt_allowed(vcpu) &&
3647 !kvm_cpu_has_interrupt(vcpu) &&
3648 !kvm_event_needs_reinjection(vcpu);
3649 }
3650
3651 static void vapic_enter(struct kvm_vcpu *vcpu)
3652 {
3653 struct kvm_lapic *apic = vcpu->arch.apic;
3654 struct page *page;
3655
3656 if (!apic || !apic->vapic_addr)
3657 return;
3658
3659 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
3660
3661 vcpu->arch.apic->vapic_page = page;
3662 }
3663
3664 static void vapic_exit(struct kvm_vcpu *vcpu)
3665 {
3666 struct kvm_lapic *apic = vcpu->arch.apic;
3667
3668 if (!apic || !apic->vapic_addr)
3669 return;
3670
3671 down_read(&vcpu->kvm->slots_lock);
3672 kvm_release_page_dirty(apic->vapic_page);
3673 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
3674 up_read(&vcpu->kvm->slots_lock);
3675 }
3676
3677 static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3678 {
3679 int max_irr, tpr;
3680
3681 if (!kvm_x86_ops->update_cr8_intercept)
3682 return;
3683
3684 if (!vcpu->arch.apic)
3685 return;
3686
3687 if (!vcpu->arch.apic->vapic_addr)
3688 max_irr = kvm_lapic_find_highest_irr(vcpu);
3689 else
3690 max_irr = -1;
3691
3692 if (max_irr != -1)
3693 max_irr >>= 4;
3694
3695 tpr = kvm_lapic_get_cr8(vcpu);
3696
3697 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3698 }
3699
3700 static void inject_pending_event(struct kvm_vcpu *vcpu)
3701 {
3702 /* try to reinject previous events if any */
3703 if (vcpu->arch.exception.pending) {
3704 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
3705 vcpu->arch.exception.has_error_code,
3706 vcpu->arch.exception.error_code);
3707 return;
3708 }
3709
3710 if (vcpu->arch.nmi_injected) {
3711 kvm_x86_ops->set_nmi(vcpu);
3712 return;
3713 }
3714
3715 if (vcpu->arch.interrupt.pending) {
3716 kvm_x86_ops->set_irq(vcpu);
3717 return;
3718 }
3719
3720 /* try to inject new event if pending */
3721 if (vcpu->arch.nmi_pending) {
3722 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3723 vcpu->arch.nmi_pending = false;
3724 vcpu->arch.nmi_injected = true;
3725 kvm_x86_ops->set_nmi(vcpu);
3726 }
3727 } else if (kvm_cpu_has_interrupt(vcpu)) {
3728 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
3729 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3730 false);
3731 kvm_x86_ops->set_irq(vcpu);
3732 }
3733 }
3734 }
3735
3736 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
3737 {
3738 int r;
3739 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
3740 vcpu->run->request_interrupt_window;
3741
3742 if (vcpu->requests)
3743 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3744 kvm_mmu_unload(vcpu);
3745
3746 r = kvm_mmu_reload(vcpu);
3747 if (unlikely(r))
3748 goto out;
3749
3750 if (vcpu->requests) {
3751 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
3752 __kvm_migrate_timers(vcpu);
3753 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3754 kvm_write_guest_time(vcpu);
3755 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3756 kvm_mmu_sync_roots(vcpu);
3757 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3758 kvm_x86_ops->tlb_flush(vcpu);
3759 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3760 &vcpu->requests)) {
3761 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
3762 r = 0;
3763 goto out;
3764 }
3765 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
3766 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
3767 r = 0;
3768 goto out;
3769 }
3770 }
3771
3772 preempt_disable();
3773
3774 kvm_x86_ops->prepare_guest_switch(vcpu);
3775 kvm_load_guest_fpu(vcpu);
3776
3777 local_irq_disable();
3778
3779 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3780 smp_mb__after_clear_bit();
3781
3782 if (vcpu->requests || need_resched() || signal_pending(current)) {
3783 set_bit(KVM_REQ_KICK, &vcpu->requests);
3784 local_irq_enable();
3785 preempt_enable();
3786 r = 1;
3787 goto out;
3788 }
3789
3790 inject_pending_event(vcpu);
3791
3792 /* enable NMI/IRQ window open exits if needed */
3793 if (vcpu->arch.nmi_pending)
3794 kvm_x86_ops->enable_nmi_window(vcpu);
3795 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3796 kvm_x86_ops->enable_irq_window(vcpu);
3797
3798 if (kvm_lapic_enabled(vcpu)) {
3799 update_cr8_intercept(vcpu);
3800 kvm_lapic_sync_to_vapic(vcpu);
3801 }
3802
3803 up_read(&vcpu->kvm->slots_lock);
3804
3805 kvm_guest_enter();
3806
3807 if (unlikely(vcpu->arch.switch_db_regs)) {
3808 set_debugreg(0, 7);
3809 set_debugreg(vcpu->arch.eff_db[0], 0);
3810 set_debugreg(vcpu->arch.eff_db[1], 1);
3811 set_debugreg(vcpu->arch.eff_db[2], 2);
3812 set_debugreg(vcpu->arch.eff_db[3], 3);
3813 }
3814
3815 trace_kvm_entry(vcpu->vcpu_id);
3816 kvm_x86_ops->run(vcpu);
3817
3818 if (unlikely(vcpu->arch.switch_db_regs || test_thread_flag(TIF_DEBUG))) {
3819 set_debugreg(current->thread.debugreg0, 0);
3820 set_debugreg(current->thread.debugreg1, 1);
3821 set_debugreg(current->thread.debugreg2, 2);
3822 set_debugreg(current->thread.debugreg3, 3);
3823 set_debugreg(current->thread.debugreg6, 6);
3824 set_debugreg(current->thread.debugreg7, 7);
3825 }
3826
3827 set_bit(KVM_REQ_KICK, &vcpu->requests);
3828 local_irq_enable();
3829
3830 ++vcpu->stat.exits;
3831
3832 /*
3833 * We must have an instruction between local_irq_enable() and
3834 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3835 * the interrupt shadow. The stat.exits increment will do nicely.
3836 * But we need to prevent reordering, hence this barrier():
3837 */
3838 barrier();
3839
3840 kvm_guest_exit();
3841
3842 preempt_enable();
3843
3844 down_read(&vcpu->kvm->slots_lock);
3845
3846 /*
3847 * Profile KVM exit RIPs:
3848 */
3849 if (unlikely(prof_on == KVM_PROFILING)) {
3850 unsigned long rip = kvm_rip_read(vcpu);
3851 profile_hit(KVM_PROFILING, (void *)rip);
3852 }
3853
3854
3855 kvm_lapic_sync_from_vapic(vcpu);
3856
3857 r = kvm_x86_ops->handle_exit(vcpu);
3858 out:
3859 return r;
3860 }
3861
3862
3863 static int __vcpu_run(struct kvm_vcpu *vcpu)
3864 {
3865 int r;
3866
3867 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
3868 pr_debug("vcpu %d received sipi with vector # %x\n",
3869 vcpu->vcpu_id, vcpu->arch.sipi_vector);
3870 kvm_lapic_reset(vcpu);
3871 r = kvm_arch_vcpu_reset(vcpu);
3872 if (r)
3873 return r;
3874 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3875 }
3876
3877 down_read(&vcpu->kvm->slots_lock);
3878 vapic_enter(vcpu);
3879
3880 r = 1;
3881 while (r > 0) {
3882 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
3883 r = vcpu_enter_guest(vcpu);
3884 else {
3885 up_read(&vcpu->kvm->slots_lock);
3886 kvm_vcpu_block(vcpu);
3887 down_read(&vcpu->kvm->slots_lock);
3888 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
3889 {
3890 switch(vcpu->arch.mp_state) {
3891 case KVM_MP_STATE_HALTED:
3892 vcpu->arch.mp_state =
3893 KVM_MP_STATE_RUNNABLE;
3894 case KVM_MP_STATE_RUNNABLE:
3895 break;
3896 case KVM_MP_STATE_SIPI_RECEIVED:
3897 default:
3898 r = -EINTR;
3899 break;
3900 }
3901 }
3902 }
3903
3904 if (r <= 0)
3905 break;
3906
3907 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
3908 if (kvm_cpu_has_pending_timer(vcpu))
3909 kvm_inject_pending_timer_irqs(vcpu);
3910
3911 if (dm_request_for_irq_injection(vcpu)) {
3912 r = -EINTR;
3913 vcpu->run->exit_reason = KVM_EXIT_INTR;
3914 ++vcpu->stat.request_irq_exits;
3915 }
3916 if (signal_pending(current)) {
3917 r = -EINTR;
3918 vcpu->run->exit_reason = KVM_EXIT_INTR;
3919 ++vcpu->stat.signal_exits;
3920 }
3921 if (need_resched()) {
3922 up_read(&vcpu->kvm->slots_lock);
3923 kvm_resched(vcpu);
3924 down_read(&vcpu->kvm->slots_lock);
3925 }
3926 }
3927
3928 up_read(&vcpu->kvm->slots_lock);
3929 post_kvm_run_save(vcpu);
3930
3931 vapic_exit(vcpu);
3932
3933 return r;
3934 }
3935
3936 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3937 {
3938 int r;
3939 sigset_t sigsaved;
3940
3941 vcpu_load(vcpu);
3942
3943 if (vcpu->sigset_active)
3944 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3945
3946 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3947 kvm_vcpu_block(vcpu);
3948 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
3949 r = -EAGAIN;
3950 goto out;
3951 }
3952
3953 /* re-sync apic's tpr */
3954 if (!irqchip_in_kernel(vcpu->kvm))
3955 kvm_set_cr8(vcpu, kvm_run->cr8);
3956
3957 if (vcpu->arch.pio.cur_count) {
3958 r = complete_pio(vcpu);
3959 if (r)
3960 goto out;
3961 }
3962 if (vcpu->mmio_needed) {
3963 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3964 vcpu->mmio_read_completed = 1;
3965 vcpu->mmio_needed = 0;
3966
3967 down_read(&vcpu->kvm->slots_lock);
3968 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
3969 EMULTYPE_NO_DECODE);
3970 up_read(&vcpu->kvm->slots_lock);
3971 if (r == EMULATE_DO_MMIO) {
3972 /*
3973 * Read-modify-write. Back to userspace.
3974 */
3975 r = 0;
3976 goto out;
3977 }
3978 }
3979 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3980 kvm_register_write(vcpu, VCPU_REGS_RAX,
3981 kvm_run->hypercall.ret);
3982
3983 r = __vcpu_run(vcpu);
3984
3985 out:
3986 if (vcpu->sigset_active)
3987 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3988
3989 vcpu_put(vcpu);
3990 return r;
3991 }
3992
3993 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3994 {
3995 vcpu_load(vcpu);
3996
3997 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3998 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3999 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4000 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4001 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4002 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4003 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4004 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4005 #ifdef CONFIG_X86_64
4006 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4007 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4008 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4009 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4010 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4011 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4012 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4013 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
4014 #endif
4015
4016 regs->rip = kvm_rip_read(vcpu);
4017 regs->rflags = kvm_get_rflags(vcpu);
4018
4019 vcpu_put(vcpu);
4020
4021 return 0;
4022 }
4023
4024 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4025 {
4026 vcpu_load(vcpu);
4027
4028 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4029 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4030 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4031 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4032 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4033 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4034 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4035 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
4036 #ifdef CONFIG_X86_64
4037 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4038 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4039 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4040 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4041 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4042 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4043 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4044 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
4045 #endif
4046
4047 kvm_rip_write(vcpu, regs->rip);
4048 kvm_set_rflags(vcpu, regs->rflags);
4049
4050 vcpu->arch.exception.pending = false;
4051
4052 vcpu_put(vcpu);
4053
4054 return 0;
4055 }
4056
4057 void kvm_get_segment(struct kvm_vcpu *vcpu,
4058 struct kvm_segment *var, int seg)
4059 {
4060 kvm_x86_ops->get_segment(vcpu, var, seg);
4061 }
4062
4063 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4064 {
4065 struct kvm_segment cs;
4066
4067 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
4068 *db = cs.db;
4069 *l = cs.l;
4070 }
4071 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4072
4073 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4074 struct kvm_sregs *sregs)
4075 {
4076 struct descriptor_table dt;
4077
4078 vcpu_load(vcpu);
4079
4080 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4081 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4082 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4083 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4084 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4085 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
4086
4087 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4088 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4089
4090 kvm_x86_ops->get_idt(vcpu, &dt);
4091 sregs->idt.limit = dt.limit;
4092 sregs->idt.base = dt.base;
4093 kvm_x86_ops->get_gdt(vcpu, &dt);
4094 sregs->gdt.limit = dt.limit;
4095 sregs->gdt.base = dt.base;
4096
4097 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4098 sregs->cr0 = vcpu->arch.cr0;
4099 sregs->cr2 = vcpu->arch.cr2;
4100 sregs->cr3 = vcpu->arch.cr3;
4101 sregs->cr4 = vcpu->arch.cr4;
4102 sregs->cr8 = kvm_get_cr8(vcpu);
4103 sregs->efer = vcpu->arch.shadow_efer;
4104 sregs->apic_base = kvm_get_apic_base(vcpu);
4105
4106 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
4107
4108 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
4109 set_bit(vcpu->arch.interrupt.nr,
4110 (unsigned long *)sregs->interrupt_bitmap);
4111
4112 vcpu_put(vcpu);
4113
4114 return 0;
4115 }
4116
4117 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4118 struct kvm_mp_state *mp_state)
4119 {
4120 vcpu_load(vcpu);
4121 mp_state->mp_state = vcpu->arch.mp_state;
4122 vcpu_put(vcpu);
4123 return 0;
4124 }
4125
4126 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4127 struct kvm_mp_state *mp_state)
4128 {
4129 vcpu_load(vcpu);
4130 vcpu->arch.mp_state = mp_state->mp_state;
4131 vcpu_put(vcpu);
4132 return 0;
4133 }
4134
4135 static void kvm_set_segment(struct kvm_vcpu *vcpu,
4136 struct kvm_segment *var, int seg)
4137 {
4138 kvm_x86_ops->set_segment(vcpu, var, seg);
4139 }
4140
4141 static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4142 struct kvm_segment *kvm_desct)
4143 {
4144 kvm_desct->base = get_desc_base(seg_desc);
4145 kvm_desct->limit = get_desc_limit(seg_desc);
4146 if (seg_desc->g) {
4147 kvm_desct->limit <<= 12;
4148 kvm_desct->limit |= 0xfff;
4149 }
4150 kvm_desct->selector = selector;
4151 kvm_desct->type = seg_desc->type;
4152 kvm_desct->present = seg_desc->p;
4153 kvm_desct->dpl = seg_desc->dpl;
4154 kvm_desct->db = seg_desc->d;
4155 kvm_desct->s = seg_desc->s;
4156 kvm_desct->l = seg_desc->l;
4157 kvm_desct->g = seg_desc->g;
4158 kvm_desct->avl = seg_desc->avl;
4159 if (!selector)
4160 kvm_desct->unusable = 1;
4161 else
4162 kvm_desct->unusable = 0;
4163 kvm_desct->padding = 0;
4164 }
4165
4166 static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4167 u16 selector,
4168 struct descriptor_table *dtable)
4169 {
4170 if (selector & 1 << 2) {
4171 struct kvm_segment kvm_seg;
4172
4173 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
4174
4175 if (kvm_seg.unusable)
4176 dtable->limit = 0;
4177 else
4178 dtable->limit = kvm_seg.limit;
4179 dtable->base = kvm_seg.base;
4180 }
4181 else
4182 kvm_x86_ops->get_gdt(vcpu, dtable);
4183 }
4184
4185 /* allowed just for 8 bytes segments */
4186 static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4187 struct desc_struct *seg_desc)
4188 {
4189 struct descriptor_table dtable;
4190 u16 index = selector >> 3;
4191
4192 get_segment_descriptor_dtable(vcpu, selector, &dtable);
4193
4194 if (dtable.limit < index * 8 + 7) {
4195 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
4196 return 1;
4197 }
4198 return kvm_read_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
4199 }
4200
4201 /* allowed just for 8 bytes segments */
4202 static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4203 struct desc_struct *seg_desc)
4204 {
4205 struct descriptor_table dtable;
4206 u16 index = selector >> 3;
4207
4208 get_segment_descriptor_dtable(vcpu, selector, &dtable);
4209
4210 if (dtable.limit < index * 8 + 7)
4211 return 1;
4212 return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
4213 }
4214
4215 static gpa_t get_tss_base_addr(struct kvm_vcpu *vcpu,
4216 struct desc_struct *seg_desc)
4217 {
4218 u32 base_addr = get_desc_base(seg_desc);
4219
4220 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
4221 }
4222
4223 static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4224 {
4225 struct kvm_segment kvm_seg;
4226
4227 kvm_get_segment(vcpu, &kvm_seg, seg);
4228 return kvm_seg.selector;
4229 }
4230
4231 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
4232 u16 selector,
4233 struct kvm_segment *kvm_seg)
4234 {
4235 struct desc_struct seg_desc;
4236
4237 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
4238 return 1;
4239 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
4240 return 0;
4241 }
4242
4243 static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
4244 {
4245 struct kvm_segment segvar = {
4246 .base = selector << 4,
4247 .limit = 0xffff,
4248 .selector = selector,
4249 .type = 3,
4250 .present = 1,
4251 .dpl = 3,
4252 .db = 0,
4253 .s = 1,
4254 .l = 0,
4255 .g = 0,
4256 .avl = 0,
4257 .unusable = 0,
4258 };
4259 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
4260 return 0;
4261 }
4262
4263 static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4264 {
4265 return (seg != VCPU_SREG_LDTR) &&
4266 (seg != VCPU_SREG_TR) &&
4267 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
4268 }
4269
4270 int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4271 int type_bits, int seg)
4272 {
4273 struct kvm_segment kvm_seg;
4274
4275 if (is_vm86_segment(vcpu, seg) || !(vcpu->arch.cr0 & X86_CR0_PE))
4276 return kvm_load_realmode_segment(vcpu, selector, seg);
4277 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
4278 return 1;
4279 kvm_seg.type |= type_bits;
4280
4281 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
4282 seg != VCPU_SREG_LDTR)
4283 if (!kvm_seg.s)
4284 kvm_seg.unusable = 1;
4285
4286 kvm_set_segment(vcpu, &kvm_seg, seg);
4287 return 0;
4288 }
4289
4290 static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4291 struct tss_segment_32 *tss)
4292 {
4293 tss->cr3 = vcpu->arch.cr3;
4294 tss->eip = kvm_rip_read(vcpu);
4295 tss->eflags = kvm_get_rflags(vcpu);
4296 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4297 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4298 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4299 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4300 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4301 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4302 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4303 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4304 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4305 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4306 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4307 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4308 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4309 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4310 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
4311 }
4312
4313 static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4314 struct tss_segment_32 *tss)
4315 {
4316 kvm_set_cr3(vcpu, tss->cr3);
4317
4318 kvm_rip_write(vcpu, tss->eip);
4319 kvm_set_rflags(vcpu, tss->eflags | 2);
4320
4321 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4322 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4323 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4324 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4325 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4326 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4327 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4328 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
4329
4330 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
4331 return 1;
4332
4333 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
4334 return 1;
4335
4336 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
4337 return 1;
4338
4339 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
4340 return 1;
4341
4342 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
4343 return 1;
4344
4345 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
4346 return 1;
4347
4348 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
4349 return 1;
4350 return 0;
4351 }
4352
4353 static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4354 struct tss_segment_16 *tss)
4355 {
4356 tss->ip = kvm_rip_read(vcpu);
4357 tss->flag = kvm_get_rflags(vcpu);
4358 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4359 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4360 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4361 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4362 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4363 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4364 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4365 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
4366
4367 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4368 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4369 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4370 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4371 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
4372 }
4373
4374 static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4375 struct tss_segment_16 *tss)
4376 {
4377 kvm_rip_write(vcpu, tss->ip);
4378 kvm_set_rflags(vcpu, tss->flag | 2);
4379 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4380 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4381 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4382 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4383 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4384 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4385 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4386 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
4387
4388 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
4389 return 1;
4390
4391 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
4392 return 1;
4393
4394 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
4395 return 1;
4396
4397 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
4398 return 1;
4399
4400 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
4401 return 1;
4402 return 0;
4403 }
4404
4405 static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
4406 u16 old_tss_sel, u32 old_tss_base,
4407 struct desc_struct *nseg_desc)
4408 {
4409 struct tss_segment_16 tss_segment_16;
4410 int ret = 0;
4411
4412 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4413 sizeof tss_segment_16))
4414 goto out;
4415
4416 save_state_to_tss16(vcpu, &tss_segment_16);
4417
4418 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4419 sizeof tss_segment_16))
4420 goto out;
4421
4422 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4423 &tss_segment_16, sizeof tss_segment_16))
4424 goto out;
4425
4426 if (old_tss_sel != 0xffff) {
4427 tss_segment_16.prev_task_link = old_tss_sel;
4428
4429 if (kvm_write_guest(vcpu->kvm,
4430 get_tss_base_addr(vcpu, nseg_desc),
4431 &tss_segment_16.prev_task_link,
4432 sizeof tss_segment_16.prev_task_link))
4433 goto out;
4434 }
4435
4436 if (load_state_from_tss16(vcpu, &tss_segment_16))
4437 goto out;
4438
4439 ret = 1;
4440 out:
4441 return ret;
4442 }
4443
4444 static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
4445 u16 old_tss_sel, u32 old_tss_base,
4446 struct desc_struct *nseg_desc)
4447 {
4448 struct tss_segment_32 tss_segment_32;
4449 int ret = 0;
4450
4451 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4452 sizeof tss_segment_32))
4453 goto out;
4454
4455 save_state_to_tss32(vcpu, &tss_segment_32);
4456
4457 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4458 sizeof tss_segment_32))
4459 goto out;
4460
4461 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4462 &tss_segment_32, sizeof tss_segment_32))
4463 goto out;
4464
4465 if (old_tss_sel != 0xffff) {
4466 tss_segment_32.prev_task_link = old_tss_sel;
4467
4468 if (kvm_write_guest(vcpu->kvm,
4469 get_tss_base_addr(vcpu, nseg_desc),
4470 &tss_segment_32.prev_task_link,
4471 sizeof tss_segment_32.prev_task_link))
4472 goto out;
4473 }
4474
4475 if (load_state_from_tss32(vcpu, &tss_segment_32))
4476 goto out;
4477
4478 ret = 1;
4479 out:
4480 return ret;
4481 }
4482
4483 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4484 {
4485 struct kvm_segment tr_seg;
4486 struct desc_struct cseg_desc;
4487 struct desc_struct nseg_desc;
4488 int ret = 0;
4489 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4490 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
4491
4492 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
4493
4494 /* FIXME: Handle errors. Failure to read either TSS or their
4495 * descriptors should generate a pagefault.
4496 */
4497 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4498 goto out;
4499
4500 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
4501 goto out;
4502
4503 if (reason != TASK_SWITCH_IRET) {
4504 int cpl;
4505
4506 cpl = kvm_x86_ops->get_cpl(vcpu);
4507 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4508 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4509 return 1;
4510 }
4511 }
4512
4513 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
4514 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
4515 return 1;
4516 }
4517
4518 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
4519 cseg_desc.type &= ~(1 << 1); //clear the B flag
4520 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
4521 }
4522
4523 if (reason == TASK_SWITCH_IRET) {
4524 u32 eflags = kvm_get_rflags(vcpu);
4525 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
4526 }
4527
4528 /* set back link to prev task only if NT bit is set in eflags
4529 note that old_tss_sel is not used afetr this point */
4530 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4531 old_tss_sel = 0xffff;
4532
4533 if (nseg_desc.type & 8)
4534 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4535 old_tss_base, &nseg_desc);
4536 else
4537 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4538 old_tss_base, &nseg_desc);
4539
4540 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
4541 u32 eflags = kvm_get_rflags(vcpu);
4542 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
4543 }
4544
4545 if (reason != TASK_SWITCH_IRET) {
4546 nseg_desc.type |= (1 << 1);
4547 save_guest_segment_descriptor(vcpu, tss_selector,
4548 &nseg_desc);
4549 }
4550
4551 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4552 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4553 tr_seg.type = 11;
4554 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
4555 out:
4556 return ret;
4557 }
4558 EXPORT_SYMBOL_GPL(kvm_task_switch);
4559
4560 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4561 struct kvm_sregs *sregs)
4562 {
4563 int mmu_reset_needed = 0;
4564 int pending_vec, max_bits;
4565 struct descriptor_table dt;
4566
4567 vcpu_load(vcpu);
4568
4569 dt.limit = sregs->idt.limit;
4570 dt.base = sregs->idt.base;
4571 kvm_x86_ops->set_idt(vcpu, &dt);
4572 dt.limit = sregs->gdt.limit;
4573 dt.base = sregs->gdt.base;
4574 kvm_x86_ops->set_gdt(vcpu, &dt);
4575
4576 vcpu->arch.cr2 = sregs->cr2;
4577 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
4578 vcpu->arch.cr3 = sregs->cr3;
4579
4580 kvm_set_cr8(vcpu, sregs->cr8);
4581
4582 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
4583 kvm_x86_ops->set_efer(vcpu, sregs->efer);
4584 kvm_set_apic_base(vcpu, sregs->apic_base);
4585
4586 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4587
4588 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
4589 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
4590 vcpu->arch.cr0 = sregs->cr0;
4591
4592 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
4593 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
4594 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
4595 load_pdptrs(vcpu, vcpu->arch.cr3);
4596 mmu_reset_needed = 1;
4597 }
4598
4599 if (mmu_reset_needed)
4600 kvm_mmu_reset_context(vcpu);
4601
4602 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4603 pending_vec = find_first_bit(
4604 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4605 if (pending_vec < max_bits) {
4606 kvm_queue_interrupt(vcpu, pending_vec, false);
4607 pr_debug("Set back pending irq %d\n", pending_vec);
4608 if (irqchip_in_kernel(vcpu->kvm))
4609 kvm_pic_clear_isr_ack(vcpu->kvm);
4610 }
4611
4612 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4613 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4614 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4615 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4616 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4617 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
4618
4619 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4620 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4621
4622 update_cr8_intercept(vcpu);
4623
4624 /* Older userspace won't unhalt the vcpu on reset. */
4625 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
4626 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4627 !(vcpu->arch.cr0 & X86_CR0_PE))
4628 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4629
4630 vcpu_put(vcpu);
4631
4632 return 0;
4633 }
4634
4635 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4636 struct kvm_guest_debug *dbg)
4637 {
4638 unsigned long rflags;
4639 int i;
4640
4641 vcpu_load(vcpu);
4642
4643 /*
4644 * Read rflags as long as potentially injected trace flags are still
4645 * filtered out.
4646 */
4647 rflags = kvm_get_rflags(vcpu);
4648
4649 vcpu->guest_debug = dbg->control;
4650 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
4651 vcpu->guest_debug = 0;
4652
4653 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
4654 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4655 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4656 vcpu->arch.switch_db_regs =
4657 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4658 } else {
4659 for (i = 0; i < KVM_NR_DB_REGS; i++)
4660 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4661 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4662 }
4663
4664 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
4665 vcpu->arch.singlestep_cs =
4666 get_segment_selector(vcpu, VCPU_SREG_CS);
4667 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
4668 }
4669
4670 /*
4671 * Trigger an rflags update that will inject or remove the trace
4672 * flags.
4673 */
4674 kvm_set_rflags(vcpu, rflags);
4675
4676 kvm_x86_ops->set_guest_debug(vcpu, dbg);
4677
4678 if (vcpu->guest_debug & KVM_GUESTDBG_INJECT_DB)
4679 kvm_queue_exception(vcpu, DB_VECTOR);
4680 else if (vcpu->guest_debug & KVM_GUESTDBG_INJECT_BP)
4681 kvm_queue_exception(vcpu, BP_VECTOR);
4682
4683 vcpu_put(vcpu);
4684
4685 return 0;
4686 }
4687
4688 /*
4689 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4690 * we have asm/x86/processor.h
4691 */
4692 struct fxsave {
4693 u16 cwd;
4694 u16 swd;
4695 u16 twd;
4696 u16 fop;
4697 u64 rip;
4698 u64 rdp;
4699 u32 mxcsr;
4700 u32 mxcsr_mask;
4701 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4702 #ifdef CONFIG_X86_64
4703 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4704 #else
4705 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4706 #endif
4707 };
4708
4709 /*
4710 * Translate a guest virtual address to a guest physical address.
4711 */
4712 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4713 struct kvm_translation *tr)
4714 {
4715 unsigned long vaddr = tr->linear_address;
4716 gpa_t gpa;
4717
4718 vcpu_load(vcpu);
4719 down_read(&vcpu->kvm->slots_lock);
4720 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
4721 up_read(&vcpu->kvm->slots_lock);
4722 tr->physical_address = gpa;
4723 tr->valid = gpa != UNMAPPED_GVA;
4724 tr->writeable = 1;
4725 tr->usermode = 0;
4726 vcpu_put(vcpu);
4727
4728 return 0;
4729 }
4730
4731 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4732 {
4733 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
4734
4735 vcpu_load(vcpu);
4736
4737 memcpy(fpu->fpr, fxsave->st_space, 128);
4738 fpu->fcw = fxsave->cwd;
4739 fpu->fsw = fxsave->swd;
4740 fpu->ftwx = fxsave->twd;
4741 fpu->last_opcode = fxsave->fop;
4742 fpu->last_ip = fxsave->rip;
4743 fpu->last_dp = fxsave->rdp;
4744 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4745
4746 vcpu_put(vcpu);
4747
4748 return 0;
4749 }
4750
4751 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4752 {
4753 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
4754
4755 vcpu_load(vcpu);
4756
4757 memcpy(fxsave->st_space, fpu->fpr, 128);
4758 fxsave->cwd = fpu->fcw;
4759 fxsave->swd = fpu->fsw;
4760 fxsave->twd = fpu->ftwx;
4761 fxsave->fop = fpu->last_opcode;
4762 fxsave->rip = fpu->last_ip;
4763 fxsave->rdp = fpu->last_dp;
4764 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4765
4766 vcpu_put(vcpu);
4767
4768 return 0;
4769 }
4770
4771 void fx_init(struct kvm_vcpu *vcpu)
4772 {
4773 unsigned after_mxcsr_mask;
4774
4775 /*
4776 * Touch the fpu the first time in non atomic context as if
4777 * this is the first fpu instruction the exception handler
4778 * will fire before the instruction returns and it'll have to
4779 * allocate ram with GFP_KERNEL.
4780 */
4781 if (!used_math())
4782 kvm_fx_save(&vcpu->arch.host_fx_image);
4783
4784 /* Initialize guest FPU by resetting ours and saving into guest's */
4785 preempt_disable();
4786 kvm_fx_save(&vcpu->arch.host_fx_image);
4787 kvm_fx_finit();
4788 kvm_fx_save(&vcpu->arch.guest_fx_image);
4789 kvm_fx_restore(&vcpu->arch.host_fx_image);
4790 preempt_enable();
4791
4792 vcpu->arch.cr0 |= X86_CR0_ET;
4793 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
4794 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4795 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
4796 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4797 }
4798 EXPORT_SYMBOL_GPL(fx_init);
4799
4800 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4801 {
4802 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4803 return;
4804
4805 vcpu->guest_fpu_loaded = 1;
4806 kvm_fx_save(&vcpu->arch.host_fx_image);
4807 kvm_fx_restore(&vcpu->arch.guest_fx_image);
4808 }
4809 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4810
4811 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4812 {
4813 if (!vcpu->guest_fpu_loaded)
4814 return;
4815
4816 vcpu->guest_fpu_loaded = 0;
4817 kvm_fx_save(&vcpu->arch.guest_fx_image);
4818 kvm_fx_restore(&vcpu->arch.host_fx_image);
4819 ++vcpu->stat.fpu_reload;
4820 }
4821 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
4822
4823 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4824 {
4825 if (vcpu->arch.time_page) {
4826 kvm_release_page_dirty(vcpu->arch.time_page);
4827 vcpu->arch.time_page = NULL;
4828 }
4829
4830 kvm_x86_ops->vcpu_free(vcpu);
4831 }
4832
4833 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4834 unsigned int id)
4835 {
4836 return kvm_x86_ops->vcpu_create(kvm, id);
4837 }
4838
4839 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4840 {
4841 int r;
4842
4843 /* We do fxsave: this must be aligned. */
4844 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
4845
4846 vcpu->arch.mtrr_state.have_fixed = 1;
4847 vcpu_load(vcpu);
4848 r = kvm_arch_vcpu_reset(vcpu);
4849 if (r == 0)
4850 r = kvm_mmu_setup(vcpu);
4851 vcpu_put(vcpu);
4852 if (r < 0)
4853 goto free_vcpu;
4854
4855 return 0;
4856 free_vcpu:
4857 kvm_x86_ops->vcpu_free(vcpu);
4858 return r;
4859 }
4860
4861 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
4862 {
4863 vcpu_load(vcpu);
4864 kvm_mmu_unload(vcpu);
4865 vcpu_put(vcpu);
4866
4867 kvm_x86_ops->vcpu_free(vcpu);
4868 }
4869
4870 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4871 {
4872 vcpu->arch.nmi_pending = false;
4873 vcpu->arch.nmi_injected = false;
4874
4875 vcpu->arch.switch_db_regs = 0;
4876 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
4877 vcpu->arch.dr6 = DR6_FIXED_1;
4878 vcpu->arch.dr7 = DR7_FIXED_1;
4879
4880 return kvm_x86_ops->vcpu_reset(vcpu);
4881 }
4882
4883 int kvm_arch_hardware_enable(void *garbage)
4884 {
4885 /*
4886 * Since this may be called from a hotplug notifcation,
4887 * we can't get the CPU frequency directly.
4888 */
4889 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
4890 int cpu = raw_smp_processor_id();
4891 per_cpu(cpu_tsc_khz, cpu) = 0;
4892 }
4893
4894 kvm_shared_msr_cpu_online();
4895
4896 return kvm_x86_ops->hardware_enable(garbage);
4897 }
4898
4899 void kvm_arch_hardware_disable(void *garbage)
4900 {
4901 kvm_x86_ops->hardware_disable(garbage);
4902 }
4903
4904 int kvm_arch_hardware_setup(void)
4905 {
4906 return kvm_x86_ops->hardware_setup();
4907 }
4908
4909 void kvm_arch_hardware_unsetup(void)
4910 {
4911 kvm_x86_ops->hardware_unsetup();
4912 }
4913
4914 void kvm_arch_check_processor_compat(void *rtn)
4915 {
4916 kvm_x86_ops->check_processor_compatibility(rtn);
4917 }
4918
4919 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4920 {
4921 struct page *page;
4922 struct kvm *kvm;
4923 int r;
4924
4925 BUG_ON(vcpu->kvm == NULL);
4926 kvm = vcpu->kvm;
4927
4928 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
4929 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
4930 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4931 else
4932 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
4933
4934 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4935 if (!page) {
4936 r = -ENOMEM;
4937 goto fail;
4938 }
4939 vcpu->arch.pio_data = page_address(page);
4940
4941 r = kvm_mmu_create(vcpu);
4942 if (r < 0)
4943 goto fail_free_pio_data;
4944
4945 if (irqchip_in_kernel(kvm)) {
4946 r = kvm_create_lapic(vcpu);
4947 if (r < 0)
4948 goto fail_mmu_destroy;
4949 }
4950
4951 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
4952 GFP_KERNEL);
4953 if (!vcpu->arch.mce_banks) {
4954 r = -ENOMEM;
4955 goto fail_mmu_destroy;
4956 }
4957 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
4958
4959 return 0;
4960
4961 fail_mmu_destroy:
4962 kvm_mmu_destroy(vcpu);
4963 fail_free_pio_data:
4964 free_page((unsigned long)vcpu->arch.pio_data);
4965 fail:
4966 return r;
4967 }
4968
4969 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4970 {
4971 kvm_free_lapic(vcpu);
4972 down_read(&vcpu->kvm->slots_lock);
4973 kvm_mmu_destroy(vcpu);
4974 up_read(&vcpu->kvm->slots_lock);
4975 free_page((unsigned long)vcpu->arch.pio_data);
4976 }
4977
4978 struct kvm *kvm_arch_create_vm(void)
4979 {
4980 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4981
4982 if (!kvm)
4983 return ERR_PTR(-ENOMEM);
4984
4985 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
4986 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
4987
4988 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4989 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
4990
4991 rdtscll(kvm->arch.vm_init_tsc);
4992
4993 return kvm;
4994 }
4995
4996 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
4997 {
4998 vcpu_load(vcpu);
4999 kvm_mmu_unload(vcpu);
5000 vcpu_put(vcpu);
5001 }
5002
5003 static void kvm_free_vcpus(struct kvm *kvm)
5004 {
5005 unsigned int i;
5006 struct kvm_vcpu *vcpu;
5007
5008 /*
5009 * Unpin any mmu pages first.
5010 */
5011 kvm_for_each_vcpu(i, vcpu, kvm)
5012 kvm_unload_vcpu_mmu(vcpu);
5013 kvm_for_each_vcpu(i, vcpu, kvm)
5014 kvm_arch_vcpu_free(vcpu);
5015
5016 mutex_lock(&kvm->lock);
5017 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5018 kvm->vcpus[i] = NULL;
5019
5020 atomic_set(&kvm->online_vcpus, 0);
5021 mutex_unlock(&kvm->lock);
5022 }
5023
5024 void kvm_arch_sync_events(struct kvm *kvm)
5025 {
5026 kvm_free_all_assigned_devices(kvm);
5027 }
5028
5029 void kvm_arch_destroy_vm(struct kvm *kvm)
5030 {
5031 kvm_iommu_unmap_guest(kvm);
5032 kvm_free_pit(kvm);
5033 kfree(kvm->arch.vpic);
5034 kfree(kvm->arch.vioapic);
5035 kvm_free_vcpus(kvm);
5036 kvm_free_physmem(kvm);
5037 if (kvm->arch.apic_access_page)
5038 put_page(kvm->arch.apic_access_page);
5039 if (kvm->arch.ept_identity_pagetable)
5040 put_page(kvm->arch.ept_identity_pagetable);
5041 kfree(kvm);
5042 }
5043
5044 int kvm_arch_set_memory_region(struct kvm *kvm,
5045 struct kvm_userspace_memory_region *mem,
5046 struct kvm_memory_slot old,
5047 int user_alloc)
5048 {
5049 int npages = mem->memory_size >> PAGE_SHIFT;
5050 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
5051
5052 /*To keep backward compatibility with older userspace,
5053 *x86 needs to hanlde !user_alloc case.
5054 */
5055 if (!user_alloc) {
5056 if (npages && !old.rmap) {
5057 unsigned long userspace_addr;
5058
5059 down_write(&current->mm->mmap_sem);
5060 userspace_addr = do_mmap(NULL, 0,
5061 npages * PAGE_SIZE,
5062 PROT_READ | PROT_WRITE,
5063 MAP_PRIVATE | MAP_ANONYMOUS,
5064 0);
5065 up_write(&current->mm->mmap_sem);
5066
5067 if (IS_ERR((void *)userspace_addr))
5068 return PTR_ERR((void *)userspace_addr);
5069
5070 /* set userspace_addr atomically for kvm_hva_to_rmapp */
5071 spin_lock(&kvm->mmu_lock);
5072 memslot->userspace_addr = userspace_addr;
5073 spin_unlock(&kvm->mmu_lock);
5074 } else {
5075 if (!old.user_alloc && old.rmap) {
5076 int ret;
5077
5078 down_write(&current->mm->mmap_sem);
5079 ret = do_munmap(current->mm, old.userspace_addr,
5080 old.npages * PAGE_SIZE);
5081 up_write(&current->mm->mmap_sem);
5082 if (ret < 0)
5083 printk(KERN_WARNING
5084 "kvm_vm_ioctl_set_memory_region: "
5085 "failed to munmap memory\n");
5086 }
5087 }
5088 }
5089
5090 spin_lock(&kvm->mmu_lock);
5091 if (!kvm->arch.n_requested_mmu_pages) {
5092 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5093 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5094 }
5095
5096 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
5097 spin_unlock(&kvm->mmu_lock);
5098
5099 return 0;
5100 }
5101
5102 void kvm_arch_flush_shadow(struct kvm *kvm)
5103 {
5104 kvm_mmu_zap_all(kvm);
5105 kvm_reload_remote_mmus(kvm);
5106 }
5107
5108 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5109 {
5110 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
5111 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5112 || vcpu->arch.nmi_pending ||
5113 (kvm_arch_interrupt_allowed(vcpu) &&
5114 kvm_cpu_has_interrupt(vcpu));
5115 }
5116
5117 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5118 {
5119 int me;
5120 int cpu = vcpu->cpu;
5121
5122 if (waitqueue_active(&vcpu->wq)) {
5123 wake_up_interruptible(&vcpu->wq);
5124 ++vcpu->stat.halt_wakeup;
5125 }
5126
5127 me = get_cpu();
5128 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5129 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5130 smp_send_reschedule(cpu);
5131 put_cpu();
5132 }
5133
5134 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5135 {
5136 return kvm_x86_ops->interrupt_allowed(vcpu);
5137 }
5138
5139 unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5140 {
5141 unsigned long rflags;
5142
5143 rflags = kvm_x86_ops->get_rflags(vcpu);
5144 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5145 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5146 return rflags;
5147 }
5148 EXPORT_SYMBOL_GPL(kvm_get_rflags);
5149
5150 void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5151 {
5152 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5153 vcpu->arch.singlestep_cs ==
5154 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5155 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5156 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5157 kvm_x86_ops->set_rflags(vcpu, rflags);
5158 }
5159 EXPORT_SYMBOL_GPL(kvm_set_rflags);
5160
5161 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5162 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5163 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5164 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5165 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
5166 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
5167 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
5168 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
5169 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
5170 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
5171 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
This page took 0.232726 seconds and 6 git commands to generate.