]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/kvm/kvm_main.c
KVM: Add general accessors to read and write guest memory
[mirror_ubuntu-artful-kernel.git] / drivers / kvm / kvm_main.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
e495606d
AK
19#include "x86_emulate.h"
20#include "segment_descriptor.h"
85f455f7 21#include "irq.h"
6aa8b732
AK
22
23#include <linux/kvm.h>
24#include <linux/module.h>
25#include <linux/errno.h>
6aa8b732
AK
26#include <linux/percpu.h>
27#include <linux/gfp.h>
6aa8b732
AK
28#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
6aa8b732 31#include <linux/reboot.h>
6aa8b732
AK
32#include <linux/debugfs.h>
33#include <linux/highmem.h>
34#include <linux/file.h>
59ae6c6b 35#include <linux/sysdev.h>
774c47f1 36#include <linux/cpu.h>
e8edc6e0 37#include <linux/sched.h>
d9e368d6
AK
38#include <linux/cpumask.h>
39#include <linux/smp.h>
d6d28168 40#include <linux/anon_inodes.h>
04d2cc77 41#include <linux/profile.h>
7aa81cc0 42#include <linux/kvm_para.h>
6aa8b732 43
e495606d
AK
44#include <asm/processor.h>
45#include <asm/msr.h>
46#include <asm/io.h>
47#include <asm/uaccess.h>
48#include <asm/desc.h>
6aa8b732
AK
49
50MODULE_AUTHOR("Qumranet");
51MODULE_LICENSE("GPL");
52
133de902
AK
53static DEFINE_SPINLOCK(kvm_lock);
54static LIST_HEAD(vm_list);
55
1b6c0168
AK
56static cpumask_t cpus_hardware_enabled;
57
cbdd1bea 58struct kvm_x86_ops *kvm_x86_ops;
c16f862d
RR
59struct kmem_cache *kvm_vcpu_cache;
60EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
1165f5fe 61
15ad7146
AK
62static __read_mostly struct preempt_ops kvm_preempt_ops;
63
1165f5fe 64#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
6aa8b732
AK
65
66static struct kvm_stats_debugfs_item {
67 const char *name;
1165f5fe 68 int offset;
6aa8b732
AK
69 struct dentry *dentry;
70} debugfs_entries[] = {
1165f5fe
AK
71 { "pf_fixed", STAT_OFFSET(pf_fixed) },
72 { "pf_guest", STAT_OFFSET(pf_guest) },
73 { "tlb_flush", STAT_OFFSET(tlb_flush) },
74 { "invlpg", STAT_OFFSET(invlpg) },
75 { "exits", STAT_OFFSET(exits) },
76 { "io_exits", STAT_OFFSET(io_exits) },
77 { "mmio_exits", STAT_OFFSET(mmio_exits) },
78 { "signal_exits", STAT_OFFSET(signal_exits) },
79 { "irq_window", STAT_OFFSET(irq_window_exits) },
80 { "halt_exits", STAT_OFFSET(halt_exits) },
b6958ce4 81 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
1165f5fe
AK
82 { "request_irq", STAT_OFFSET(request_irq_exits) },
83 { "irq_exits", STAT_OFFSET(irq_exits) },
e6adf283 84 { "light_exits", STAT_OFFSET(light_exits) },
2cc51560 85 { "efer_reload", STAT_OFFSET(efer_reload) },
1165f5fe 86 { NULL }
6aa8b732
AK
87};
88
89static struct dentry *debugfs_dir;
90
91#define MAX_IO_MSRS 256
92
707d92fa
RR
93#define CR0_RESERVED_BITS \
94 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
95 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
96 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
66aee91a
RR
97#define CR4_RESERVED_BITS \
98 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
99 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
100 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
101 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
102
7075bc81 103#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
6aa8b732
AK
104#define EFER_RESERVED_BITS 0xfffffffffffff2fe
105
05b3e0c2 106#ifdef CONFIG_X86_64
6aa8b732
AK
107// LDT or TSS descriptor in the GDT. 16 bytes.
108struct segment_descriptor_64 {
109 struct segment_descriptor s;
110 u32 base_higher;
111 u32 pad_zero;
112};
113
114#endif
115
bccf2150
AK
116static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
117 unsigned long arg);
118
6aa8b732
AK
119unsigned long segment_base(u16 selector)
120{
121 struct descriptor_table gdt;
122 struct segment_descriptor *d;
123 unsigned long table_base;
124 typedef unsigned long ul;
125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
130 asm ("sgdt %0" : "=m"(gdt));
131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
136 asm ("sldt %0" : "=g"(ldt_selector));
137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
140 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
05b3e0c2 141#ifdef CONFIG_X86_64
6aa8b732
AK
142 if (d->system == 0
143 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
145#endif
146 return v;
147}
148EXPORT_SYMBOL_GPL(segment_base);
149
5aacf0ca
JM
150static inline int valid_vcpu(int n)
151{
152 return likely(n >= 0 && n < KVM_MAX_VCPUS);
153}
154
7702fd1f
AK
155void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
156{
157 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
158 return;
159
160 vcpu->guest_fpu_loaded = 1;
b114b080
RR
161 fx_save(&vcpu->host_fx_image);
162 fx_restore(&vcpu->guest_fx_image);
7702fd1f
AK
163}
164EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
165
166void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
167{
168 if (!vcpu->guest_fpu_loaded)
169 return;
170
171 vcpu->guest_fpu_loaded = 0;
b114b080
RR
172 fx_save(&vcpu->guest_fx_image);
173 fx_restore(&vcpu->host_fx_image);
7702fd1f
AK
174}
175EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
176
bccf2150
AK
177/*
178 * Switches to specified vcpu, until a matching vcpu_put()
179 */
180static void vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 181{
15ad7146
AK
182 int cpu;
183
bccf2150 184 mutex_lock(&vcpu->mutex);
15ad7146
AK
185 cpu = get_cpu();
186 preempt_notifier_register(&vcpu->preempt_notifier);
cbdd1bea 187 kvm_x86_ops->vcpu_load(vcpu, cpu);
15ad7146 188 put_cpu();
6aa8b732
AK
189}
190
6aa8b732
AK
191static void vcpu_put(struct kvm_vcpu *vcpu)
192{
15ad7146 193 preempt_disable();
cbdd1bea 194 kvm_x86_ops->vcpu_put(vcpu);
15ad7146
AK
195 preempt_notifier_unregister(&vcpu->preempt_notifier);
196 preempt_enable();
6aa8b732
AK
197 mutex_unlock(&vcpu->mutex);
198}
199
d9e368d6
AK
200static void ack_flush(void *_completed)
201{
d9e368d6
AK
202}
203
204void kvm_flush_remote_tlbs(struct kvm *kvm)
205{
49d3bd7e 206 int i, cpu;
d9e368d6
AK
207 cpumask_t cpus;
208 struct kvm_vcpu *vcpu;
d9e368d6 209
d9e368d6 210 cpus_clear(cpus);
fb3f0f51
RR
211 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
212 vcpu = kvm->vcpus[i];
213 if (!vcpu)
214 continue;
d9e368d6
AK
215 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
216 continue;
217 cpu = vcpu->cpu;
218 if (cpu != -1 && cpu != raw_smp_processor_id())
49d3bd7e 219 cpu_set(cpu, cpus);
d9e368d6 220 }
49d3bd7e 221 smp_call_function_mask(cpus, ack_flush, NULL, 1);
d9e368d6
AK
222}
223
fb3f0f51
RR
224int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
225{
226 struct page *page;
227 int r;
228
229 mutex_init(&vcpu->mutex);
230 vcpu->cpu = -1;
231 vcpu->mmu.root_hpa = INVALID_PAGE;
232 vcpu->kvm = kvm;
233 vcpu->vcpu_id = id;
c5ec1534
HQ
234 if (!irqchip_in_kernel(kvm) || id == 0)
235 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
236 else
237 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
b6958ce4 238 init_waitqueue_head(&vcpu->wq);
fb3f0f51
RR
239
240 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
241 if (!page) {
242 r = -ENOMEM;
243 goto fail;
244 }
245 vcpu->run = page_address(page);
246
247 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
248 if (!page) {
249 r = -ENOMEM;
250 goto fail_free_run;
251 }
252 vcpu->pio_data = page_address(page);
253
fb3f0f51
RR
254 r = kvm_mmu_create(vcpu);
255 if (r < 0)
256 goto fail_free_pio_data;
257
258 return 0;
259
260fail_free_pio_data:
261 free_page((unsigned long)vcpu->pio_data);
262fail_free_run:
263 free_page((unsigned long)vcpu->run);
264fail:
265 return -ENOMEM;
266}
267EXPORT_SYMBOL_GPL(kvm_vcpu_init);
268
269void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
270{
271 kvm_mmu_destroy(vcpu);
1b9778da
ED
272 if (vcpu->apic)
273 hrtimer_cancel(&vcpu->apic->timer.dev);
97222cc8 274 kvm_free_apic(vcpu->apic);
fb3f0f51
RR
275 free_page((unsigned long)vcpu->pio_data);
276 free_page((unsigned long)vcpu->run);
277}
278EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
279
f17abe9a 280static struct kvm *kvm_create_vm(void)
6aa8b732
AK
281{
282 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
6aa8b732
AK
283
284 if (!kvm)
f17abe9a 285 return ERR_PTR(-ENOMEM);
6aa8b732 286
74906345 287 kvm_io_bus_init(&kvm->pio_bus);
11ec2804 288 mutex_init(&kvm->lock);
6aa8b732 289 INIT_LIST_HEAD(&kvm->active_mmu_pages);
2eeb2e94 290 kvm_io_bus_init(&kvm->mmio_bus);
5e58cfe4
RR
291 spin_lock(&kvm_lock);
292 list_add(&kvm->vm_list, &vm_list);
293 spin_unlock(&kvm_lock);
f17abe9a
AK
294 return kvm;
295}
296
6aa8b732
AK
297/*
298 * Free any memory in @free but not in @dont.
299 */
300static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
301 struct kvm_memory_slot *dont)
302{
303 int i;
304
305 if (!dont || free->phys_mem != dont->phys_mem)
306 if (free->phys_mem) {
307 for (i = 0; i < free->npages; ++i)
55a54f79
AK
308 if (free->phys_mem[i])
309 __free_page(free->phys_mem[i]);
6aa8b732
AK
310 vfree(free->phys_mem);
311 }
290fc38d
IE
312 if (!dont || free->rmap != dont->rmap)
313 vfree(free->rmap);
6aa8b732
AK
314
315 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
316 vfree(free->dirty_bitmap);
317
8b6d44c7 318 free->phys_mem = NULL;
6aa8b732 319 free->npages = 0;
8b6d44c7 320 free->dirty_bitmap = NULL;
6aa8b732
AK
321}
322
323static void kvm_free_physmem(struct kvm *kvm)
324{
325 int i;
326
327 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 328 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
329}
330
039576c0
AK
331static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
332{
333 int i;
334
3077c451 335 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
039576c0
AK
336 if (vcpu->pio.guest_pages[i]) {
337 __free_page(vcpu->pio.guest_pages[i]);
338 vcpu->pio.guest_pages[i] = NULL;
339 }
340}
341
7b53aa56
AK
342static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
343{
7b53aa56
AK
344 vcpu_load(vcpu);
345 kvm_mmu_unload(vcpu);
346 vcpu_put(vcpu);
347}
348
6aa8b732
AK
349static void kvm_free_vcpus(struct kvm *kvm)
350{
351 unsigned int i;
352
7b53aa56
AK
353 /*
354 * Unpin any mmu pages first.
355 */
356 for (i = 0; i < KVM_MAX_VCPUS; ++i)
fb3f0f51
RR
357 if (kvm->vcpus[i])
358 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
359 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
360 if (kvm->vcpus[i]) {
cbdd1bea 361 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
fb3f0f51
RR
362 kvm->vcpus[i] = NULL;
363 }
364 }
365
6aa8b732
AK
366}
367
f17abe9a
AK
368static void kvm_destroy_vm(struct kvm *kvm)
369{
133de902
AK
370 spin_lock(&kvm_lock);
371 list_del(&kvm->vm_list);
372 spin_unlock(&kvm_lock);
74906345 373 kvm_io_bus_destroy(&kvm->pio_bus);
2eeb2e94 374 kvm_io_bus_destroy(&kvm->mmio_bus);
85f455f7 375 kfree(kvm->vpic);
1fd4f2a5 376 kfree(kvm->vioapic);
6aa8b732
AK
377 kvm_free_vcpus(kvm);
378 kvm_free_physmem(kvm);
379 kfree(kvm);
f17abe9a
AK
380}
381
382static int kvm_vm_release(struct inode *inode, struct file *filp)
383{
384 struct kvm *kvm = filp->private_data;
385
386 kvm_destroy_vm(kvm);
6aa8b732
AK
387 return 0;
388}
389
390static void inject_gp(struct kvm_vcpu *vcpu)
391{
cbdd1bea 392 kvm_x86_ops->inject_gp(vcpu, 0);
6aa8b732
AK
393}
394
1342d353
AK
395/*
396 * Load the pae pdptrs. Return true is they are all valid.
397 */
398static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
6aa8b732
AK
399{
400 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
1342d353 401 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
6aa8b732 402 int i;
1342d353 403 int ret;
c820c2aa 404 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
6aa8b732 405
11ec2804 406 mutex_lock(&vcpu->kvm->lock);
195aefde
IE
407 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
408 offset * sizeof(u64), sizeof(pdpte));
409 if (ret < 0) {
c820c2aa
RR
410 ret = 0;
411 goto out;
412 }
c820c2aa
RR
413 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
414 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
1342d353
AK
415 ret = 0;
416 goto out;
417 }
6aa8b732 418 }
c820c2aa 419 ret = 1;
6aa8b732 420
c820c2aa 421 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
1342d353 422out:
11ec2804 423 mutex_unlock(&vcpu->kvm->lock);
6aa8b732 424
1342d353 425 return ret;
6aa8b732
AK
426}
427
428void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
429{
707d92fa 430 if (cr0 & CR0_RESERVED_BITS) {
6aa8b732
AK
431 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
432 cr0, vcpu->cr0);
433 inject_gp(vcpu);
434 return;
435 }
436
707d92fa 437 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
6aa8b732
AK
438 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
439 inject_gp(vcpu);
440 return;
441 }
442
707d92fa 443 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
6aa8b732
AK
444 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
445 "and a clear PE flag\n");
446 inject_gp(vcpu);
447 return;
448 }
449
707d92fa 450 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
05b3e0c2 451#ifdef CONFIG_X86_64
6aa8b732
AK
452 if ((vcpu->shadow_efer & EFER_LME)) {
453 int cs_db, cs_l;
454
455 if (!is_pae(vcpu)) {
456 printk(KERN_DEBUG "set_cr0: #GP, start paging "
457 "in long mode while PAE is disabled\n");
458 inject_gp(vcpu);
459 return;
460 }
cbdd1bea 461 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6aa8b732
AK
462 if (cs_l) {
463 printk(KERN_DEBUG "set_cr0: #GP, start paging "
464 "in long mode while CS.L == 1\n");
465 inject_gp(vcpu);
466 return;
467
468 }
469 } else
470#endif
1342d353 471 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
472 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
473 "reserved bits\n");
474 inject_gp(vcpu);
475 return;
476 }
477
478 }
479
cbdd1bea 480 kvm_x86_ops->set_cr0(vcpu, cr0);
6aa8b732
AK
481 vcpu->cr0 = cr0;
482
11ec2804 483 mutex_lock(&vcpu->kvm->lock);
6aa8b732 484 kvm_mmu_reset_context(vcpu);
11ec2804 485 mutex_unlock(&vcpu->kvm->lock);
6aa8b732
AK
486 return;
487}
488EXPORT_SYMBOL_GPL(set_cr0);
489
490void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
491{
492 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
493}
494EXPORT_SYMBOL_GPL(lmsw);
495
496void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
497{
66aee91a 498 if (cr4 & CR4_RESERVED_BITS) {
6aa8b732
AK
499 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
500 inject_gp(vcpu);
501 return;
502 }
503
a9058ecd 504 if (is_long_mode(vcpu)) {
66aee91a 505 if (!(cr4 & X86_CR4_PAE)) {
6aa8b732
AK
506 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
507 "in long mode\n");
508 inject_gp(vcpu);
509 return;
510 }
66aee91a 511 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
1342d353 512 && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
513 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
514 inject_gp(vcpu);
310bc76c 515 return;
6aa8b732
AK
516 }
517
66aee91a 518 if (cr4 & X86_CR4_VMXE) {
6aa8b732
AK
519 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
520 inject_gp(vcpu);
521 return;
522 }
cbdd1bea 523 kvm_x86_ops->set_cr4(vcpu, cr4);
81f50e3b 524 vcpu->cr4 = cr4;
11ec2804 525 mutex_lock(&vcpu->kvm->lock);
6aa8b732 526 kvm_mmu_reset_context(vcpu);
11ec2804 527 mutex_unlock(&vcpu->kvm->lock);
6aa8b732
AK
528}
529EXPORT_SYMBOL_GPL(set_cr4);
530
531void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
532{
a9058ecd 533 if (is_long_mode(vcpu)) {
f802a307 534 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
6aa8b732
AK
535 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
536 inject_gp(vcpu);
537 return;
538 }
539 } else {
f802a307
RR
540 if (is_pae(vcpu)) {
541 if (cr3 & CR3_PAE_RESERVED_BITS) {
542 printk(KERN_DEBUG
543 "set_cr3: #GP, reserved bits\n");
544 inject_gp(vcpu);
545 return;
546 }
547 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
548 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
549 "reserved bits\n");
550 inject_gp(vcpu);
551 return;
552 }
6aa8b732 553 }
21764863
RH
554 /*
555 * We don't check reserved bits in nonpae mode, because
556 * this isn't enforced, and VMware depends on this.
557 */
6aa8b732
AK
558 }
559
11ec2804 560 mutex_lock(&vcpu->kvm->lock);
d21225ee
IM
561 /*
562 * Does the new cr3 value map to physical memory? (Note, we
563 * catch an invalid cr3 even in real-mode, because it would
564 * cause trouble later on when we turn on paging anyway.)
565 *
566 * A real CPU would silently accept an invalid cr3 and would
567 * attempt to use it - with largely undefined (and often hard
568 * to debug) behavior on the guest side.
569 */
570 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
571 inject_gp(vcpu);
fb764416
RR
572 else {
573 vcpu->cr3 = cr3;
d21225ee 574 vcpu->mmu.new_cr3(vcpu);
fb764416 575 }
11ec2804 576 mutex_unlock(&vcpu->kvm->lock);
6aa8b732
AK
577}
578EXPORT_SYMBOL_GPL(set_cr3);
579
580void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
581{
7075bc81 582 if (cr8 & CR8_RESERVED_BITS) {
6aa8b732
AK
583 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
584 inject_gp(vcpu);
585 return;
586 }
97222cc8
ED
587 if (irqchip_in_kernel(vcpu->kvm))
588 kvm_lapic_set_tpr(vcpu, cr8);
589 else
590 vcpu->cr8 = cr8;
6aa8b732
AK
591}
592EXPORT_SYMBOL_GPL(set_cr8);
593
7017fc3d
ED
594unsigned long get_cr8(struct kvm_vcpu *vcpu)
595{
97222cc8
ED
596 if (irqchip_in_kernel(vcpu->kvm))
597 return kvm_lapic_get_cr8(vcpu);
598 else
599 return vcpu->cr8;
7017fc3d
ED
600}
601EXPORT_SYMBOL_GPL(get_cr8);
602
603u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
604{
97222cc8
ED
605 if (irqchip_in_kernel(vcpu->kvm))
606 return vcpu->apic_base;
607 else
608 return vcpu->apic_base;
7017fc3d
ED
609}
610EXPORT_SYMBOL_GPL(kvm_get_apic_base);
611
612void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
613{
97222cc8
ED
614 /* TODO: reserve bits check */
615 if (irqchip_in_kernel(vcpu->kvm))
616 kvm_lapic_set_base(vcpu, data);
617 else
618 vcpu->apic_base = data;
7017fc3d
ED
619}
620EXPORT_SYMBOL_GPL(kvm_set_apic_base);
621
6aa8b732
AK
622void fx_init(struct kvm_vcpu *vcpu)
623{
b114b080 624 unsigned after_mxcsr_mask;
6aa8b732 625
9bd01506
RR
626 /* Initialize guest FPU by resetting ours and saving into guest's */
627 preempt_disable();
b114b080 628 fx_save(&vcpu->host_fx_image);
6aa8b732 629 fpu_init();
b114b080
RR
630 fx_save(&vcpu->guest_fx_image);
631 fx_restore(&vcpu->host_fx_image);
9bd01506 632 preempt_enable();
6aa8b732 633
380102c8 634 vcpu->cr0 |= X86_CR0_ET;
b114b080
RR
635 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
636 vcpu->guest_fx_image.mxcsr = 0x1f80;
637 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
638 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
6aa8b732
AK
639}
640EXPORT_SYMBOL_GPL(fx_init);
641
6aa8b732
AK
642/*
643 * Allocate some memory and give it an address in the guest physical address
644 * space.
645 *
646 * Discontiguous memory is allowed, mostly for framebuffers.
647 */
2c6f5df9
AK
648static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
649 struct kvm_memory_region *mem)
6aa8b732
AK
650{
651 int r;
652 gfn_t base_gfn;
653 unsigned long npages;
654 unsigned long i;
655 struct kvm_memory_slot *memslot;
656 struct kvm_memory_slot old, new;
6aa8b732
AK
657
658 r = -EINVAL;
659 /* General sanity checks */
660 if (mem->memory_size & (PAGE_SIZE - 1))
661 goto out;
662 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
663 goto out;
664 if (mem->slot >= KVM_MEMORY_SLOTS)
665 goto out;
666 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
667 goto out;
668
669 memslot = &kvm->memslots[mem->slot];
670 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
671 npages = mem->memory_size >> PAGE_SHIFT;
672
673 if (!npages)
674 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
675
11ec2804 676 mutex_lock(&kvm->lock);
6aa8b732 677
6aa8b732
AK
678 new = old = *memslot;
679
680 new.base_gfn = base_gfn;
681 new.npages = npages;
682 new.flags = mem->flags;
683
684 /* Disallow changing a memory slot's size. */
685 r = -EINVAL;
686 if (npages && old.npages && npages != old.npages)
687 goto out_unlock;
688
689 /* Check for overlaps */
690 r = -EEXIST;
691 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
692 struct kvm_memory_slot *s = &kvm->memslots[i];
693
694 if (s == memslot)
695 continue;
696 if (!((base_gfn + npages <= s->base_gfn) ||
697 (base_gfn >= s->base_gfn + s->npages)))
698 goto out_unlock;
699 }
6aa8b732
AK
700
701 /* Deallocate if slot is being removed */
702 if (!npages)
8b6d44c7 703 new.phys_mem = NULL;
6aa8b732
AK
704
705 /* Free page dirty bitmap if unneeded */
706 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 707 new.dirty_bitmap = NULL;
6aa8b732
AK
708
709 r = -ENOMEM;
710
711 /* Allocate if a slot is being created */
712 if (npages && !new.phys_mem) {
713 new.phys_mem = vmalloc(npages * sizeof(struct page *));
714
715 if (!new.phys_mem)
0d8d2bd4 716 goto out_unlock;
6aa8b732 717
290fc38d
IE
718 new.rmap = vmalloc(npages * sizeof(struct page*));
719
720 if (!new.rmap)
721 goto out_unlock;
722
6aa8b732 723 memset(new.phys_mem, 0, npages * sizeof(struct page *));
290fc38d 724 memset(new.rmap, 0, npages * sizeof(*new.rmap));
6aa8b732
AK
725 for (i = 0; i < npages; ++i) {
726 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
727 | __GFP_ZERO);
728 if (!new.phys_mem[i])
0d8d2bd4 729 goto out_unlock;
6aa8b732
AK
730 }
731 }
732
733 /* Allocate page dirty bitmap if needed */
734 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
735 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
736
737 new.dirty_bitmap = vmalloc(dirty_bytes);
738 if (!new.dirty_bitmap)
0d8d2bd4 739 goto out_unlock;
6aa8b732
AK
740 memset(new.dirty_bitmap, 0, dirty_bytes);
741 }
742
6aa8b732
AK
743 if (mem->slot >= kvm->nmemslots)
744 kvm->nmemslots = mem->slot + 1;
745
746 *memslot = new;
6aa8b732 747
90cb0529
AK
748 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
749 kvm_flush_remote_tlbs(kvm);
6aa8b732 750
11ec2804 751 mutex_unlock(&kvm->lock);
6aa8b732
AK
752
753 kvm_free_physmem_slot(&old, &new);
754 return 0;
755
756out_unlock:
11ec2804 757 mutex_unlock(&kvm->lock);
6aa8b732
AK
758 kvm_free_physmem_slot(&new, &old);
759out:
760 return r;
761}
762
763/*
764 * Get (and clear) the dirty memory log for a memory slot.
765 */
2c6f5df9
AK
766static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
767 struct kvm_dirty_log *log)
6aa8b732
AK
768{
769 struct kvm_memory_slot *memslot;
770 int r, i;
771 int n;
772 unsigned long any = 0;
773
11ec2804 774 mutex_lock(&kvm->lock);
6aa8b732 775
6aa8b732
AK
776 r = -EINVAL;
777 if (log->slot >= KVM_MEMORY_SLOTS)
778 goto out;
779
780 memslot = &kvm->memslots[log->slot];
781 r = -ENOENT;
782 if (!memslot->dirty_bitmap)
783 goto out;
784
cd1a4a98 785 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 786
cd1a4a98 787 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
788 any = memslot->dirty_bitmap[i];
789
790 r = -EFAULT;
791 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
792 goto out;
793
39214915
RR
794 /* If nothing is dirty, don't bother messing with page tables. */
795 if (any) {
39214915
RR
796 kvm_mmu_slot_remove_write_access(kvm, log->slot);
797 kvm_flush_remote_tlbs(kvm);
798 memset(memslot->dirty_bitmap, 0, n);
39214915 799 }
6aa8b732
AK
800
801 r = 0;
802
803out:
11ec2804 804 mutex_unlock(&kvm->lock);
6aa8b732
AK
805 return r;
806}
807
e8207547
AK
808/*
809 * Set a new alias region. Aliases map a portion of physical memory into
810 * another portion. This is useful for memory windows, for example the PC
811 * VGA region.
812 */
813static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
814 struct kvm_memory_alias *alias)
815{
816 int r, n;
817 struct kvm_mem_alias *p;
818
819 r = -EINVAL;
820 /* General sanity checks */
821 if (alias->memory_size & (PAGE_SIZE - 1))
822 goto out;
823 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
824 goto out;
825 if (alias->slot >= KVM_ALIAS_SLOTS)
826 goto out;
827 if (alias->guest_phys_addr + alias->memory_size
828 < alias->guest_phys_addr)
829 goto out;
830 if (alias->target_phys_addr + alias->memory_size
831 < alias->target_phys_addr)
832 goto out;
833
11ec2804 834 mutex_lock(&kvm->lock);
e8207547
AK
835
836 p = &kvm->aliases[alias->slot];
837 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
838 p->npages = alias->memory_size >> PAGE_SHIFT;
839 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
840
841 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
842 if (kvm->aliases[n - 1].npages)
843 break;
844 kvm->naliases = n;
845
90cb0529 846 kvm_mmu_zap_all(kvm);
e8207547 847
11ec2804 848 mutex_unlock(&kvm->lock);
e8207547
AK
849
850 return 0;
851
852out:
853 return r;
854}
855
6ceb9d79
HQ
856static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
857{
858 int r;
859
860 r = 0;
861 switch (chip->chip_id) {
862 case KVM_IRQCHIP_PIC_MASTER:
863 memcpy (&chip->chip.pic,
864 &pic_irqchip(kvm)->pics[0],
865 sizeof(struct kvm_pic_state));
866 break;
867 case KVM_IRQCHIP_PIC_SLAVE:
868 memcpy (&chip->chip.pic,
869 &pic_irqchip(kvm)->pics[1],
870 sizeof(struct kvm_pic_state));
871 break;
6bf9e962
HQ
872 case KVM_IRQCHIP_IOAPIC:
873 memcpy (&chip->chip.ioapic,
874 ioapic_irqchip(kvm),
875 sizeof(struct kvm_ioapic_state));
876 break;
6ceb9d79
HQ
877 default:
878 r = -EINVAL;
879 break;
880 }
881 return r;
882}
883
884static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
885{
886 int r;
887
888 r = 0;
889 switch (chip->chip_id) {
890 case KVM_IRQCHIP_PIC_MASTER:
891 memcpy (&pic_irqchip(kvm)->pics[0],
892 &chip->chip.pic,
893 sizeof(struct kvm_pic_state));
894 break;
895 case KVM_IRQCHIP_PIC_SLAVE:
896 memcpy (&pic_irqchip(kvm)->pics[1],
897 &chip->chip.pic,
898 sizeof(struct kvm_pic_state));
899 break;
6bf9e962
HQ
900 case KVM_IRQCHIP_IOAPIC:
901 memcpy (ioapic_irqchip(kvm),
902 &chip->chip.ioapic,
903 sizeof(struct kvm_ioapic_state));
904 break;
6ceb9d79
HQ
905 default:
906 r = -EINVAL;
907 break;
908 }
909 kvm_pic_update_irq(pic_irqchip(kvm));
910 return r;
911}
912
290fc38d 913gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
e8207547
AK
914{
915 int i;
916 struct kvm_mem_alias *alias;
917
918 for (i = 0; i < kvm->naliases; ++i) {
919 alias = &kvm->aliases[i];
920 if (gfn >= alias->base_gfn
921 && gfn < alias->base_gfn + alias->npages)
922 return alias->target_gfn + gfn - alias->base_gfn;
923 }
924 return gfn;
925}
926
927static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
6aa8b732
AK
928{
929 int i;
930
931 for (i = 0; i < kvm->nmemslots; ++i) {
932 struct kvm_memory_slot *memslot = &kvm->memslots[i];
933
934 if (gfn >= memslot->base_gfn
935 && gfn < memslot->base_gfn + memslot->npages)
936 return memslot;
937 }
8b6d44c7 938 return NULL;
6aa8b732 939}
e8207547
AK
940
941struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
942{
943 gfn = unalias_gfn(kvm, gfn);
944 return __gfn_to_memslot(kvm, gfn);
945}
6aa8b732 946
954bbbc2
AK
947struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
948{
949 struct kvm_memory_slot *slot;
950
e8207547
AK
951 gfn = unalias_gfn(kvm, gfn);
952 slot = __gfn_to_memslot(kvm, gfn);
954bbbc2
AK
953 if (!slot)
954 return NULL;
955 return slot->phys_mem[gfn - slot->base_gfn];
956}
957EXPORT_SYMBOL_GPL(gfn_to_page);
958
195aefde
IE
959static int next_segment(unsigned long len, int offset)
960{
961 if (len > PAGE_SIZE - offset)
962 return PAGE_SIZE - offset;
963 else
964 return len;
965}
966
967int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
968 int len)
969{
970 void *page_virt;
971 struct page *page;
972
973 page = gfn_to_page(kvm, gfn);
974 if (!page)
975 return -EFAULT;
976 page_virt = kmap_atomic(page, KM_USER0);
977
978 memcpy(data, page_virt + offset, len);
979
980 kunmap_atomic(page_virt, KM_USER0);
981 return 0;
982}
983EXPORT_SYMBOL_GPL(kvm_read_guest_page);
984
985int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
986{
987 gfn_t gfn = gpa >> PAGE_SHIFT;
988 int seg;
989 int offset = offset_in_page(gpa);
990 int ret;
991
992 while ((seg = next_segment(len, offset)) != 0) {
993 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
994 if (ret < 0)
995 return ret;
996 offset = 0;
997 len -= seg;
998 data += seg;
999 ++gfn;
1000 }
1001 return 0;
1002}
1003EXPORT_SYMBOL_GPL(kvm_read_guest);
1004
1005int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1006 int offset, int len)
1007{
1008 void *page_virt;
1009 struct page *page;
1010
1011 page = gfn_to_page(kvm, gfn);
1012 if (!page)
1013 return -EFAULT;
1014 page_virt = kmap_atomic(page, KM_USER0);
1015
1016 memcpy(page_virt + offset, data, len);
1017
1018 kunmap_atomic(page_virt, KM_USER0);
1019 mark_page_dirty(kvm, gfn);
1020 return 0;
1021}
1022EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1023
1024int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1025 unsigned long len)
1026{
1027 gfn_t gfn = gpa >> PAGE_SHIFT;
1028 int seg;
1029 int offset = offset_in_page(gpa);
1030 int ret;
1031
1032 while ((seg = next_segment(len, offset)) != 0) {
1033 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1034 if (ret < 0)
1035 return ret;
1036 offset = 0;
1037 len -= seg;
1038 data += seg;
1039 ++gfn;
1040 }
1041 return 0;
1042}
1043
1044int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1045{
1046 void *page_virt;
1047 struct page *page;
1048
1049 page = gfn_to_page(kvm, gfn);
1050 if (!page)
1051 return -EFAULT;
1052 page_virt = kmap_atomic(page, KM_USER0);
1053
1054 memset(page_virt + offset, 0, len);
1055
1056 kunmap_atomic(page_virt, KM_USER0);
1057 return 0;
1058}
1059EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1060
1061int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1062{
1063 gfn_t gfn = gpa >> PAGE_SHIFT;
1064 int seg;
1065 int offset = offset_in_page(gpa);
1066 int ret;
1067
1068 while ((seg = next_segment(len, offset)) != 0) {
1069 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1070 if (ret < 0)
1071 return ret;
1072 offset = 0;
1073 len -= seg;
1074 ++gfn;
1075 }
1076 return 0;
1077}
1078EXPORT_SYMBOL_GPL(kvm_clear_guest);
1079
7e9d619d 1080/* WARNING: Does not work on aliased pages. */
6aa8b732
AK
1081void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1082{
31389947 1083 struct kvm_memory_slot *memslot;
6aa8b732 1084
7e9d619d
RR
1085 memslot = __gfn_to_memslot(kvm, gfn);
1086 if (memslot && memslot->dirty_bitmap) {
1087 unsigned long rel_gfn = gfn - memslot->base_gfn;
6aa8b732 1088
7e9d619d
RR
1089 /* avoid RMW */
1090 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1091 set_bit(rel_gfn, memslot->dirty_bitmap);
6aa8b732
AK
1092 }
1093}
1094
e7d5d76c 1095int emulator_read_std(unsigned long addr,
4c690a1e 1096 void *val,
6aa8b732 1097 unsigned int bytes,
cebff02b 1098 struct kvm_vcpu *vcpu)
6aa8b732 1099{
6aa8b732
AK
1100 void *data = val;
1101
1102 while (bytes) {
1103 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1104 unsigned offset = addr & (PAGE_SIZE-1);
1105 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
195aefde 1106 int ret;
6aa8b732
AK
1107
1108 if (gpa == UNMAPPED_GVA)
1109 return X86EMUL_PROPAGATE_FAULT;
195aefde
IE
1110 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1111 if (ret < 0)
6aa8b732 1112 return X86EMUL_UNHANDLEABLE;
6aa8b732
AK
1113
1114 bytes -= tocopy;
1115 data += tocopy;
1116 addr += tocopy;
1117 }
1118
1119 return X86EMUL_CONTINUE;
1120}
e7d5d76c 1121EXPORT_SYMBOL_GPL(emulator_read_std);
6aa8b732
AK
1122
1123static int emulator_write_std(unsigned long addr,
4c690a1e 1124 const void *val,
6aa8b732 1125 unsigned int bytes,
cebff02b 1126 struct kvm_vcpu *vcpu)
6aa8b732 1127{
f0242478 1128 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
6aa8b732
AK
1129 return X86EMUL_UNHANDLEABLE;
1130}
1131
97222cc8
ED
1132/*
1133 * Only apic need an MMIO device hook, so shortcut now..
1134 */
1135static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1136 gpa_t addr)
1137{
1138 struct kvm_io_device *dev;
1139
1140 if (vcpu->apic) {
1141 dev = &vcpu->apic->dev;
1142 if (dev->in_range(dev, addr))
1143 return dev;
1144 }
1145 return NULL;
1146}
1147
2eeb2e94
GH
1148static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1149 gpa_t addr)
1150{
97222cc8
ED
1151 struct kvm_io_device *dev;
1152
1153 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1154 if (dev == NULL)
1155 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1156 return dev;
2eeb2e94
GH
1157}
1158
74906345
ED
1159static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1160 gpa_t addr)
1161{
1162 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1163}
1164
6aa8b732 1165static int emulator_read_emulated(unsigned long addr,
4c690a1e 1166 void *val,
6aa8b732 1167 unsigned int bytes,
cebff02b 1168 struct kvm_vcpu *vcpu)
6aa8b732 1169{
2eeb2e94
GH
1170 struct kvm_io_device *mmio_dev;
1171 gpa_t gpa;
6aa8b732
AK
1172
1173 if (vcpu->mmio_read_completed) {
1174 memcpy(val, vcpu->mmio_data, bytes);
1175 vcpu->mmio_read_completed = 0;
1176 return X86EMUL_CONTINUE;
cebff02b 1177 } else if (emulator_read_std(addr, val, bytes, vcpu)
6aa8b732
AK
1178 == X86EMUL_CONTINUE)
1179 return X86EMUL_CONTINUE;
d27d4aca 1180
2eeb2e94
GH
1181 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1182 if (gpa == UNMAPPED_GVA)
1183 return X86EMUL_PROPAGATE_FAULT;
6aa8b732 1184
2eeb2e94
GH
1185 /*
1186 * Is this MMIO handled locally?
1187 */
1188 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1189 if (mmio_dev) {
1190 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1191 return X86EMUL_CONTINUE;
6aa8b732 1192 }
2eeb2e94
GH
1193
1194 vcpu->mmio_needed = 1;
1195 vcpu->mmio_phys_addr = gpa;
1196 vcpu->mmio_size = bytes;
1197 vcpu->mmio_is_write = 0;
1198
1199 return X86EMUL_UNHANDLEABLE;
6aa8b732
AK
1200}
1201
da4a00f0 1202static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
4c690a1e 1203 const void *val, int bytes)
da4a00f0 1204{
195aefde 1205 int ret;
da4a00f0 1206
195aefde
IE
1207 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1208 if (ret < 0)
da4a00f0 1209 return 0;
fe551881 1210 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
da4a00f0
AK
1211 return 1;
1212}
1213
b0fcd903
AK
1214static int emulator_write_emulated_onepage(unsigned long addr,
1215 const void *val,
1216 unsigned int bytes,
cebff02b 1217 struct kvm_vcpu *vcpu)
6aa8b732 1218{
2eeb2e94
GH
1219 struct kvm_io_device *mmio_dev;
1220 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
6aa8b732 1221
c9047f53 1222 if (gpa == UNMAPPED_GVA) {
cbdd1bea 1223 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
6aa8b732 1224 return X86EMUL_PROPAGATE_FAULT;
c9047f53 1225 }
6aa8b732 1226
da4a00f0
AK
1227 if (emulator_write_phys(vcpu, gpa, val, bytes))
1228 return X86EMUL_CONTINUE;
1229
2eeb2e94
GH
1230 /*
1231 * Is this MMIO handled locally?
1232 */
1233 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1234 if (mmio_dev) {
1235 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1236 return X86EMUL_CONTINUE;
1237 }
1238
6aa8b732
AK
1239 vcpu->mmio_needed = 1;
1240 vcpu->mmio_phys_addr = gpa;
1241 vcpu->mmio_size = bytes;
1242 vcpu->mmio_is_write = 1;
4c690a1e 1243 memcpy(vcpu->mmio_data, val, bytes);
6aa8b732
AK
1244
1245 return X86EMUL_CONTINUE;
1246}
1247
e7d5d76c 1248int emulator_write_emulated(unsigned long addr,
b0fcd903
AK
1249 const void *val,
1250 unsigned int bytes,
cebff02b 1251 struct kvm_vcpu *vcpu)
b0fcd903
AK
1252{
1253 /* Crossing a page boundary? */
1254 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1255 int rc, now;
1256
1257 now = -addr & ~PAGE_MASK;
cebff02b 1258 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
b0fcd903
AK
1259 if (rc != X86EMUL_CONTINUE)
1260 return rc;
1261 addr += now;
1262 val += now;
1263 bytes -= now;
1264 }
cebff02b 1265 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
b0fcd903 1266}
e7d5d76c 1267EXPORT_SYMBOL_GPL(emulator_write_emulated);
b0fcd903 1268
6aa8b732 1269static int emulator_cmpxchg_emulated(unsigned long addr,
4c690a1e
AK
1270 const void *old,
1271 const void *new,
6aa8b732 1272 unsigned int bytes,
cebff02b 1273 struct kvm_vcpu *vcpu)
6aa8b732
AK
1274{
1275 static int reported;
1276
1277 if (!reported) {
1278 reported = 1;
1279 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1280 }
cebff02b 1281 return emulator_write_emulated(addr, new, bytes, vcpu);
6aa8b732
AK
1282}
1283
1284static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1285{
cbdd1bea 1286 return kvm_x86_ops->get_segment_base(vcpu, seg);
6aa8b732
AK
1287}
1288
1289int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1290{
6aa8b732
AK
1291 return X86EMUL_CONTINUE;
1292}
1293
1294int emulate_clts(struct kvm_vcpu *vcpu)
1295{
404fb881 1296 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
6aa8b732
AK
1297 return X86EMUL_CONTINUE;
1298}
1299
1300int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1301{
1302 struct kvm_vcpu *vcpu = ctxt->vcpu;
1303
1304 switch (dr) {
1305 case 0 ... 3:
cbdd1bea 1306 *dest = kvm_x86_ops->get_dr(vcpu, dr);
6aa8b732
AK
1307 return X86EMUL_CONTINUE;
1308 default:
f0242478 1309 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
6aa8b732
AK
1310 return X86EMUL_UNHANDLEABLE;
1311 }
1312}
1313
1314int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1315{
1316 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1317 int exception;
1318
cbdd1bea 1319 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
6aa8b732
AK
1320 if (exception) {
1321 /* FIXME: better handling */
1322 return X86EMUL_UNHANDLEABLE;
1323 }
1324 return X86EMUL_CONTINUE;
1325}
1326
054b1369 1327void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
6aa8b732
AK
1328{
1329 static int reported;
1330 u8 opcodes[4];
054b1369 1331 unsigned long rip = vcpu->rip;
6aa8b732
AK
1332 unsigned long rip_linear;
1333
054b1369 1334 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
6aa8b732
AK
1335
1336 if (reported)
1337 return;
1338
054b1369 1339 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
6aa8b732 1340
054b1369
AK
1341 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1342 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
6aa8b732
AK
1343 reported = 1;
1344}
054b1369 1345EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
6aa8b732
AK
1346
1347struct x86_emulate_ops emulate_ops = {
1348 .read_std = emulator_read_std,
1349 .write_std = emulator_write_std,
1350 .read_emulated = emulator_read_emulated,
1351 .write_emulated = emulator_write_emulated,
1352 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1353};
1354
1355int emulate_instruction(struct kvm_vcpu *vcpu,
1356 struct kvm_run *run,
1357 unsigned long cr2,
3427318f
LV
1358 u16 error_code,
1359 int no_decode)
6aa8b732 1360{
a22436b7 1361 int r;
6aa8b732 1362
e7df56e4 1363 vcpu->mmio_fault_cr2 = cr2;
cbdd1bea 1364 kvm_x86_ops->cache_regs(vcpu);
6aa8b732 1365
6aa8b732 1366 vcpu->mmio_is_write = 0;
e70669ab 1367 vcpu->pio.string = 0;
3427318f
LV
1368
1369 if (!no_decode) {
1370 int cs_db, cs_l;
1371 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1372
1373 vcpu->emulate_ctxt.vcpu = vcpu;
1374 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1375 vcpu->emulate_ctxt.cr2 = cr2;
1376 vcpu->emulate_ctxt.mode =
1377 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1378 ? X86EMUL_MODE_REAL : cs_l
1379 ? X86EMUL_MODE_PROT64 : cs_db
1380 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1381
1382 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1383 vcpu->emulate_ctxt.cs_base = 0;
1384 vcpu->emulate_ctxt.ds_base = 0;
1385 vcpu->emulate_ctxt.es_base = 0;
1386 vcpu->emulate_ctxt.ss_base = 0;
1387 } else {
1388 vcpu->emulate_ctxt.cs_base =
1389 get_segment_base(vcpu, VCPU_SREG_CS);
1390 vcpu->emulate_ctxt.ds_base =
1391 get_segment_base(vcpu, VCPU_SREG_DS);
1392 vcpu->emulate_ctxt.es_base =
1393 get_segment_base(vcpu, VCPU_SREG_ES);
1394 vcpu->emulate_ctxt.ss_base =
1395 get_segment_base(vcpu, VCPU_SREG_SS);
1396 }
1397
1398 vcpu->emulate_ctxt.gs_base =
1399 get_segment_base(vcpu, VCPU_SREG_GS);
1400 vcpu->emulate_ctxt.fs_base =
1401 get_segment_base(vcpu, VCPU_SREG_FS);
1402
1403 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
a22436b7
LV
1404 if (r) {
1405 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1406 return EMULATE_DONE;
1407 return EMULATE_FAIL;
1408 }
3427318f
LV
1409 }
1410
a22436b7 1411 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
1be3aa47 1412
e70669ab
LV
1413 if (vcpu->pio.string)
1414 return EMULATE_DO_MMIO;
6aa8b732
AK
1415
1416 if ((r || vcpu->mmio_is_write) && run) {
8fc0d085 1417 run->exit_reason = KVM_EXIT_MMIO;
6aa8b732
AK
1418 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1419 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1420 run->mmio.len = vcpu->mmio_size;
1421 run->mmio.is_write = vcpu->mmio_is_write;
1422 }
1423
1424 if (r) {
a436036b
AK
1425 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1426 return EMULATE_DONE;
6aa8b732 1427 if (!vcpu->mmio_needed) {
054b1369 1428 kvm_report_emulation_failure(vcpu, "mmio");
6aa8b732
AK
1429 return EMULATE_FAIL;
1430 }
1431 return EMULATE_DO_MMIO;
1432 }
1433
cbdd1bea 1434 kvm_x86_ops->decache_regs(vcpu);
3427318f 1435 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
6aa8b732 1436
02c83209
AK
1437 if (vcpu->mmio_is_write) {
1438 vcpu->mmio_needed = 0;
6aa8b732 1439 return EMULATE_DO_MMIO;
02c83209 1440 }
6aa8b732
AK
1441
1442 return EMULATE_DONE;
1443}
1444EXPORT_SYMBOL_GPL(emulate_instruction);
1445
b6958ce4
ED
1446/*
1447 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1448 */
c5ec1534 1449static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
d3bef15f 1450{
b6958ce4
ED
1451 DECLARE_WAITQUEUE(wait, current);
1452
1453 add_wait_queue(&vcpu->wq, &wait);
1454
1455 /*
1456 * We will block until either an interrupt or a signal wakes us up
1457 */
c5ec1534
HQ
1458 while (!kvm_cpu_has_interrupt(vcpu)
1459 && !signal_pending(current)
1460 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1461 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
b6958ce4
ED
1462 set_current_state(TASK_INTERRUPTIBLE);
1463 vcpu_put(vcpu);
1464 schedule();
1465 vcpu_load(vcpu);
1466 }
d3bef15f 1467
c5ec1534 1468 __set_current_state(TASK_RUNNING);
b6958ce4 1469 remove_wait_queue(&vcpu->wq, &wait);
b6958ce4
ED
1470}
1471
1472int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1473{
d3bef15f 1474 ++vcpu->stat.halt_exits;
b6958ce4 1475 if (irqchip_in_kernel(vcpu->kvm)) {
c5ec1534
HQ
1476 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1477 kvm_vcpu_block(vcpu);
1478 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1479 return -EINTR;
b6958ce4
ED
1480 return 1;
1481 } else {
1482 vcpu->run->exit_reason = KVM_EXIT_HLT;
1483 return 0;
1484 }
d3bef15f
AK
1485}
1486EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1487
7aa81cc0 1488int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
270fd9b9 1489{
7aa81cc0 1490 unsigned long nr, a0, a1, a2, a3, ret;
270fd9b9 1491
cbdd1bea 1492 kvm_x86_ops->cache_regs(vcpu);
7aa81cc0
AL
1493
1494 nr = vcpu->regs[VCPU_REGS_RAX];
1495 a0 = vcpu->regs[VCPU_REGS_RBX];
1496 a1 = vcpu->regs[VCPU_REGS_RCX];
1497 a2 = vcpu->regs[VCPU_REGS_RDX];
1498 a3 = vcpu->regs[VCPU_REGS_RSI];
1499
1500 if (!is_long_mode(vcpu)) {
1501 nr &= 0xFFFFFFFF;
1502 a0 &= 0xFFFFFFFF;
1503 a1 &= 0xFFFFFFFF;
1504 a2 &= 0xFFFFFFFF;
1505 a3 &= 0xFFFFFFFF;
270fd9b9 1506 }
7aa81cc0 1507
270fd9b9
AK
1508 switch (nr) {
1509 default:
7aa81cc0
AL
1510 ret = -KVM_ENOSYS;
1511 break;
270fd9b9
AK
1512 }
1513 vcpu->regs[VCPU_REGS_RAX] = ret;
cbdd1bea 1514 kvm_x86_ops->decache_regs(vcpu);
7aa81cc0
AL
1515 return 0;
1516}
1517EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1518
1519int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1520{
1521 char instruction[3];
1522 int ret = 0;
1523
1524 mutex_lock(&vcpu->kvm->lock);
1525
1526 /*
1527 * Blow out the MMU to ensure that no other VCPU has an active mapping
1528 * to ensure that the updated hypercall appears atomically across all
1529 * VCPUs.
1530 */
1531 kvm_mmu_zap_all(vcpu->kvm);
1532
1533 kvm_x86_ops->cache_regs(vcpu);
1534 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1535 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1536 != X86EMUL_CONTINUE)
1537 ret = -EFAULT;
1538
1539 mutex_unlock(&vcpu->kvm->lock);
1540
1541 return ret;
270fd9b9 1542}
270fd9b9 1543
6aa8b732
AK
1544static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1545{
1546 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1547}
1548
1549void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1550{
1551 struct descriptor_table dt = { limit, base };
1552
cbdd1bea 1553 kvm_x86_ops->set_gdt(vcpu, &dt);
6aa8b732
AK
1554}
1555
1556void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1557{
1558 struct descriptor_table dt = { limit, base };
1559
cbdd1bea 1560 kvm_x86_ops->set_idt(vcpu, &dt);
6aa8b732
AK
1561}
1562
1563void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1564 unsigned long *rflags)
1565{
1566 lmsw(vcpu, msw);
cbdd1bea 1567 *rflags = kvm_x86_ops->get_rflags(vcpu);
6aa8b732
AK
1568}
1569
1570unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1571{
cbdd1bea 1572 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
6aa8b732
AK
1573 switch (cr) {
1574 case 0:
1575 return vcpu->cr0;
1576 case 2:
1577 return vcpu->cr2;
1578 case 3:
1579 return vcpu->cr3;
1580 case 4:
1581 return vcpu->cr4;
1582 default:
1583 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1584 return 0;
1585 }
1586}
1587
1588void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1589 unsigned long *rflags)
1590{
1591 switch (cr) {
1592 case 0:
1593 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
cbdd1bea 1594 *rflags = kvm_x86_ops->get_rflags(vcpu);
6aa8b732
AK
1595 break;
1596 case 2:
1597 vcpu->cr2 = val;
1598 break;
1599 case 3:
1600 set_cr3(vcpu, val);
1601 break;
1602 case 4:
1603 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1604 break;
1605 default:
1606 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1607 }
1608}
1609
3bab1f5d
AK
1610int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1611{
1612 u64 data;
1613
1614 switch (msr) {
1615 case 0xc0010010: /* SYSCFG */
1616 case 0xc0010015: /* HWCR */
1617 case MSR_IA32_PLATFORM_ID:
1618 case MSR_IA32_P5_MC_ADDR:
1619 case MSR_IA32_P5_MC_TYPE:
1620 case MSR_IA32_MC0_CTL:
1621 case MSR_IA32_MCG_STATUS:
1622 case MSR_IA32_MCG_CAP:
1623 case MSR_IA32_MC0_MISC:
1624 case MSR_IA32_MC0_MISC+4:
1625 case MSR_IA32_MC0_MISC+8:
1626 case MSR_IA32_MC0_MISC+12:
1627 case MSR_IA32_MC0_MISC+16:
1628 case MSR_IA32_UCODE_REV:
a8d13ea2 1629 case MSR_IA32_PERF_STATUS:
2dc7094b 1630 case MSR_IA32_EBL_CR_POWERON:
3bab1f5d
AK
1631 /* MTRR registers */
1632 case 0xfe:
1633 case 0x200 ... 0x2ff:
1634 data = 0;
1635 break;
a8d13ea2
AK
1636 case 0xcd: /* fsb frequency */
1637 data = 3;
1638 break;
3bab1f5d 1639 case MSR_IA32_APICBASE:
7017fc3d 1640 data = kvm_get_apic_base(vcpu);
3bab1f5d 1641 break;
6f00e68f
AK
1642 case MSR_IA32_MISC_ENABLE:
1643 data = vcpu->ia32_misc_enable_msr;
1644 break;
3bab1f5d
AK
1645#ifdef CONFIG_X86_64
1646 case MSR_EFER:
1647 data = vcpu->shadow_efer;
1648 break;
1649#endif
1650 default:
f0242478 1651 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
3bab1f5d
AK
1652 return 1;
1653 }
1654 *pdata = data;
1655 return 0;
1656}
1657EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1658
6aa8b732
AK
1659/*
1660 * Reads an msr value (of 'msr_index') into 'pdata'.
1661 * Returns 0 on success, non-0 otherwise.
1662 * Assumes vcpu_load() was already called.
1663 */
35f3f286 1664int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
6aa8b732 1665{
cbdd1bea 1666 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
6aa8b732
AK
1667}
1668
05b3e0c2 1669#ifdef CONFIG_X86_64
6aa8b732 1670
3bab1f5d 1671static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
6aa8b732 1672{
6aa8b732
AK
1673 if (efer & EFER_RESERVED_BITS) {
1674 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1675 efer);
1676 inject_gp(vcpu);
1677 return;
1678 }
1679
1680 if (is_paging(vcpu)
1681 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1682 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1683 inject_gp(vcpu);
1684 return;
1685 }
1686
cbdd1bea 1687 kvm_x86_ops->set_efer(vcpu, efer);
7725f0ba 1688
6aa8b732
AK
1689 efer &= ~EFER_LMA;
1690 efer |= vcpu->shadow_efer & EFER_LMA;
1691
1692 vcpu->shadow_efer = efer;
6aa8b732 1693}
6aa8b732
AK
1694
1695#endif
1696
3bab1f5d
AK
1697int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1698{
1699 switch (msr) {
1700#ifdef CONFIG_X86_64
1701 case MSR_EFER:
1702 set_efer(vcpu, data);
1703 break;
1704#endif
1705 case MSR_IA32_MC0_STATUS:
f0242478 1706 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
3bab1f5d
AK
1707 __FUNCTION__, data);
1708 break;
0e5bf0d0 1709 case MSR_IA32_MCG_STATUS:
f0242478 1710 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
0e5bf0d0
SK
1711 __FUNCTION__, data);
1712 break;
3bab1f5d
AK
1713 case MSR_IA32_UCODE_REV:
1714 case MSR_IA32_UCODE_WRITE:
1715 case 0x200 ... 0x2ff: /* MTRRs */
1716 break;
1717 case MSR_IA32_APICBASE:
7017fc3d 1718 kvm_set_apic_base(vcpu, data);
3bab1f5d 1719 break;
6f00e68f
AK
1720 case MSR_IA32_MISC_ENABLE:
1721 vcpu->ia32_misc_enable_msr = data;
1722 break;
3bab1f5d 1723 default:
f0242478 1724 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
3bab1f5d
AK
1725 return 1;
1726 }
1727 return 0;
1728}
1729EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1730
6aa8b732
AK
1731/*
1732 * Writes msr value into into the appropriate "register".
1733 * Returns 0 on success, non-0 otherwise.
1734 * Assumes vcpu_load() was already called.
1735 */
35f3f286 1736int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
6aa8b732 1737{
cbdd1bea 1738 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
6aa8b732
AK
1739}
1740
1741void kvm_resched(struct kvm_vcpu *vcpu)
1742{
3fca0365
YD
1743 if (!need_resched())
1744 return;
6aa8b732 1745 cond_resched();
6aa8b732
AK
1746}
1747EXPORT_SYMBOL_GPL(kvm_resched);
1748
06465c5a
AK
1749void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1750{
1751 int i;
1752 u32 function;
1753 struct kvm_cpuid_entry *e, *best;
1754
cbdd1bea 1755 kvm_x86_ops->cache_regs(vcpu);
06465c5a
AK
1756 function = vcpu->regs[VCPU_REGS_RAX];
1757 vcpu->regs[VCPU_REGS_RAX] = 0;
1758 vcpu->regs[VCPU_REGS_RBX] = 0;
1759 vcpu->regs[VCPU_REGS_RCX] = 0;
1760 vcpu->regs[VCPU_REGS_RDX] = 0;
1761 best = NULL;
1762 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1763 e = &vcpu->cpuid_entries[i];
1764 if (e->function == function) {
1765 best = e;
1766 break;
1767 }
1768 /*
1769 * Both basic or both extended?
1770 */
1771 if (((e->function ^ function) & 0x80000000) == 0)
1772 if (!best || e->function > best->function)
1773 best = e;
1774 }
1775 if (best) {
1776 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1777 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1778 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1779 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1780 }
cbdd1bea
CE
1781 kvm_x86_ops->decache_regs(vcpu);
1782 kvm_x86_ops->skip_emulated_instruction(vcpu);
06465c5a
AK
1783}
1784EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1785
039576c0 1786static int pio_copy_data(struct kvm_vcpu *vcpu)
46fc1477 1787{
039576c0
AK
1788 void *p = vcpu->pio_data;
1789 void *q;
1790 unsigned bytes;
1791 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1792
039576c0
AK
1793 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1794 PAGE_KERNEL);
1795 if (!q) {
039576c0
AK
1796 free_pio_guest_pages(vcpu);
1797 return -ENOMEM;
1798 }
1799 q += vcpu->pio.guest_page_offset;
1800 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1801 if (vcpu->pio.in)
1802 memcpy(q, p, bytes);
1803 else
1804 memcpy(p, q, bytes);
1805 q -= vcpu->pio.guest_page_offset;
1806 vunmap(q);
039576c0
AK
1807 free_pio_guest_pages(vcpu);
1808 return 0;
1809}
1810
1811static int complete_pio(struct kvm_vcpu *vcpu)
1812{
1813 struct kvm_pio_request *io = &vcpu->pio;
46fc1477 1814 long delta;
039576c0 1815 int r;
46fc1477 1816
cbdd1bea 1817 kvm_x86_ops->cache_regs(vcpu);
46fc1477
AK
1818
1819 if (!io->string) {
039576c0
AK
1820 if (io->in)
1821 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
46fc1477
AK
1822 io->size);
1823 } else {
039576c0
AK
1824 if (io->in) {
1825 r = pio_copy_data(vcpu);
1826 if (r) {
cbdd1bea 1827 kvm_x86_ops->cache_regs(vcpu);
039576c0
AK
1828 return r;
1829 }
1830 }
1831
46fc1477
AK
1832 delta = 1;
1833 if (io->rep) {
039576c0 1834 delta *= io->cur_count;
46fc1477
AK
1835 /*
1836 * The size of the register should really depend on
1837 * current address size.
1838 */
1839 vcpu->regs[VCPU_REGS_RCX] -= delta;
1840 }
039576c0 1841 if (io->down)
46fc1477
AK
1842 delta = -delta;
1843 delta *= io->size;
039576c0 1844 if (io->in)
46fc1477
AK
1845 vcpu->regs[VCPU_REGS_RDI] += delta;
1846 else
1847 vcpu->regs[VCPU_REGS_RSI] += delta;
1848 }
1849
cbdd1bea 1850 kvm_x86_ops->decache_regs(vcpu);
46fc1477 1851
039576c0
AK
1852 io->count -= io->cur_count;
1853 io->cur_count = 0;
1854
039576c0 1855 return 0;
46fc1477
AK
1856}
1857
65619eb5
ED
1858static void kernel_pio(struct kvm_io_device *pio_dev,
1859 struct kvm_vcpu *vcpu,
1860 void *pd)
74906345
ED
1861{
1862 /* TODO: String I/O for in kernel device */
1863
9cf98828 1864 mutex_lock(&vcpu->kvm->lock);
74906345
ED
1865 if (vcpu->pio.in)
1866 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1867 vcpu->pio.size,
65619eb5 1868 pd);
74906345
ED
1869 else
1870 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1871 vcpu->pio.size,
65619eb5 1872 pd);
9cf98828 1873 mutex_unlock(&vcpu->kvm->lock);
65619eb5
ED
1874}
1875
1876static void pio_string_write(struct kvm_io_device *pio_dev,
1877 struct kvm_vcpu *vcpu)
1878{
1879 struct kvm_pio_request *io = &vcpu->pio;
1880 void *pd = vcpu->pio_data;
1881 int i;
1882
9cf98828 1883 mutex_lock(&vcpu->kvm->lock);
65619eb5
ED
1884 for (i = 0; i < io->cur_count; i++) {
1885 kvm_iodevice_write(pio_dev, io->port,
1886 io->size,
1887 pd);
1888 pd += io->size;
1889 }
9cf98828 1890 mutex_unlock(&vcpu->kvm->lock);
74906345
ED
1891}
1892
3090dd73
LV
1893int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1894 int size, unsigned port)
1895{
1896 struct kvm_io_device *pio_dev;
1897
1898 vcpu->run->exit_reason = KVM_EXIT_IO;
1899 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1900 vcpu->run->io.size = vcpu->pio.size = size;
1901 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1902 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1903 vcpu->run->io.port = vcpu->pio.port = port;
1904 vcpu->pio.in = in;
1905 vcpu->pio.string = 0;
1906 vcpu->pio.down = 0;
1907 vcpu->pio.guest_page_offset = 0;
1908 vcpu->pio.rep = 0;
1909
cbdd1bea 1910 kvm_x86_ops->cache_regs(vcpu);
3090dd73 1911 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
cbdd1bea 1912 kvm_x86_ops->decache_regs(vcpu);
3090dd73 1913
0967b7bf
AK
1914 kvm_x86_ops->skip_emulated_instruction(vcpu);
1915
3090dd73
LV
1916 pio_dev = vcpu_find_pio_dev(vcpu, port);
1917 if (pio_dev) {
1918 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1919 complete_pio(vcpu);
1920 return 1;
1921 }
1922 return 0;
1923}
1924EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1925
1926int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1927 int size, unsigned long count, int down,
039576c0
AK
1928 gva_t address, int rep, unsigned port)
1929{
1930 unsigned now, in_page;
65619eb5 1931 int i, ret = 0;
039576c0
AK
1932 int nr_pages = 1;
1933 struct page *page;
74906345 1934 struct kvm_io_device *pio_dev;
039576c0
AK
1935
1936 vcpu->run->exit_reason = KVM_EXIT_IO;
1937 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
3090dd73 1938 vcpu->run->io.size = vcpu->pio.size = size;
039576c0 1939 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3090dd73
LV
1940 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1941 vcpu->run->io.port = vcpu->pio.port = port;
039576c0 1942 vcpu->pio.in = in;
3090dd73 1943 vcpu->pio.string = 1;
039576c0
AK
1944 vcpu->pio.down = down;
1945 vcpu->pio.guest_page_offset = offset_in_page(address);
1946 vcpu->pio.rep = rep;
1947
039576c0 1948 if (!count) {
cbdd1bea 1949 kvm_x86_ops->skip_emulated_instruction(vcpu);
039576c0
AK
1950 return 1;
1951 }
1952
039576c0
AK
1953 if (!down)
1954 in_page = PAGE_SIZE - offset_in_page(address);
1955 else
1956 in_page = offset_in_page(address) + size;
1957 now = min(count, (unsigned long)in_page / size);
1958 if (!now) {
1959 /*
1960 * String I/O straddles page boundary. Pin two guest pages
1961 * so that we satisfy atomicity constraints. Do just one
1962 * transaction to avoid complexity.
1963 */
1964 nr_pages = 2;
1965 now = 1;
1966 }
1967 if (down) {
1968 /*
1969 * String I/O in reverse. Yuck. Kill the guest, fix later.
1970 */
f0242478 1971 pr_unimpl(vcpu, "guest string pio down\n");
039576c0
AK
1972 inject_gp(vcpu);
1973 return 1;
1974 }
1975 vcpu->run->io.count = now;
1976 vcpu->pio.cur_count = now;
1977
0967b7bf
AK
1978 if (vcpu->pio.cur_count == vcpu->pio.count)
1979 kvm_x86_ops->skip_emulated_instruction(vcpu);
1980
039576c0 1981 for (i = 0; i < nr_pages; ++i) {
11ec2804 1982 mutex_lock(&vcpu->kvm->lock);
039576c0
AK
1983 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1984 if (page)
1985 get_page(page);
1986 vcpu->pio.guest_pages[i] = page;
11ec2804 1987 mutex_unlock(&vcpu->kvm->lock);
039576c0
AK
1988 if (!page) {
1989 inject_gp(vcpu);
1990 free_pio_guest_pages(vcpu);
1991 return 1;
1992 }
1993 }
1994
3090dd73 1995 pio_dev = vcpu_find_pio_dev(vcpu, port);
65619eb5
ED
1996 if (!vcpu->pio.in) {
1997 /* string PIO write */
1998 ret = pio_copy_data(vcpu);
1999 if (ret >= 0 && pio_dev) {
2000 pio_string_write(pio_dev, vcpu);
2001 complete_pio(vcpu);
2002 if (vcpu->pio.count == 0)
2003 ret = 1;
2004 }
2005 } else if (pio_dev)
f0242478 2006 pr_unimpl(vcpu, "no string pio read support yet, "
65619eb5
ED
2007 "port %x size %d count %ld\n",
2008 port, size, count);
2009
2010 return ret;
039576c0 2011}
3090dd73 2012EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
039576c0 2013
04d2cc77
AK
2014/*
2015 * Check if userspace requested an interrupt window, and that the
2016 * interrupt window is open.
2017 *
2018 * No need to exit to userspace if we already have an interrupt queued.
2019 */
2020static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2021 struct kvm_run *kvm_run)
2022{
2023 return (!vcpu->irq_summary &&
2024 kvm_run->request_interrupt_window &&
2025 vcpu->interrupt_window_open &&
2026 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2027}
2028
2029static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2030 struct kvm_run *kvm_run)
2031{
2032 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2033 kvm_run->cr8 = get_cr8(vcpu);
2034 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2035 if (irqchip_in_kernel(vcpu->kvm))
2036 kvm_run->ready_for_interrupt_injection = 1;
2037 else
2038 kvm_run->ready_for_interrupt_injection =
2039 (vcpu->interrupt_window_open &&
2040 vcpu->irq_summary == 0);
2041}
2042
2043static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2044{
2045 int r;
2046
2047 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2048 printk("vcpu %d received sipi with vector # %x\n",
2049 vcpu->vcpu_id, vcpu->sipi_vector);
2050 kvm_lapic_reset(vcpu);
2051 kvm_x86_ops->vcpu_reset(vcpu);
2052 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2053 }
2054
2055preempted:
2056 if (vcpu->guest_debug.enabled)
2057 kvm_x86_ops->guest_debug_pre(vcpu);
2058
2059again:
2060 r = kvm_mmu_reload(vcpu);
2061 if (unlikely(r))
2062 goto out;
2063
2064 preempt_disable();
2065
2066 kvm_x86_ops->prepare_guest_switch(vcpu);
2067 kvm_load_guest_fpu(vcpu);
2068
2069 local_irq_disable();
2070
2071 if (signal_pending(current)) {
2072 local_irq_enable();
2073 preempt_enable();
2074 r = -EINTR;
2075 kvm_run->exit_reason = KVM_EXIT_INTR;
2076 ++vcpu->stat.signal_exits;
2077 goto out;
2078 }
2079
2080 if (irqchip_in_kernel(vcpu->kvm))
2081 kvm_x86_ops->inject_pending_irq(vcpu);
2082 else if (!vcpu->mmio_read_completed)
2083 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2084
2085 vcpu->guest_mode = 1;
d172fcd3 2086 kvm_guest_enter();
04d2cc77
AK
2087
2088 if (vcpu->requests)
2089 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2090 kvm_x86_ops->tlb_flush(vcpu);
2091
2092 kvm_x86_ops->run(vcpu, kvm_run);
2093
2094 vcpu->guest_mode = 0;
2095 local_irq_enable();
2096
2097 ++vcpu->stat.exits;
2098
0552f73b
LV
2099 /*
2100 * We must have an instruction between local_irq_enable() and
2101 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2102 * the interrupt shadow. The stat.exits increment will do nicely.
2103 * But we need to prevent reordering, hence this barrier():
2104 */
2105 barrier();
2106
2107 kvm_guest_exit();
2108
04d2cc77
AK
2109 preempt_enable();
2110
2111 /*
2112 * Profile KVM exit RIPs:
2113 */
2114 if (unlikely(prof_on == KVM_PROFILING)) {
2115 kvm_x86_ops->cache_regs(vcpu);
2116 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2117 }
2118
2119 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2120
2121 if (r > 0) {
2122 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2123 r = -EINTR;
2124 kvm_run->exit_reason = KVM_EXIT_INTR;
2125 ++vcpu->stat.request_irq_exits;
2126 goto out;
2127 }
2128 if (!need_resched()) {
2129 ++vcpu->stat.light_exits;
2130 goto again;
2131 }
2132 }
2133
2134out:
2135 if (r > 0) {
2136 kvm_resched(vcpu);
2137 goto preempted;
2138 }
2139
2140 post_kvm_run_save(vcpu, kvm_run);
2141
2142 return r;
2143}
2144
2145
bccf2150 2146static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6aa8b732 2147{
6aa8b732 2148 int r;
1961d276 2149 sigset_t sigsaved;
6aa8b732 2150
bccf2150 2151 vcpu_load(vcpu);
6aa8b732 2152
c5ec1534
HQ
2153 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2154 kvm_vcpu_block(vcpu);
2155 vcpu_put(vcpu);
2156 return -EAGAIN;
2157 }
2158
1961d276
AK
2159 if (vcpu->sigset_active)
2160 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2161
54810342 2162 /* re-sync apic's tpr */
5cd4f6fd
HQ
2163 if (!irqchip_in_kernel(vcpu->kvm))
2164 set_cr8(vcpu, kvm_run->cr8);
54810342 2165
02c83209
AK
2166 if (vcpu->pio.cur_count) {
2167 r = complete_pio(vcpu);
2168 if (r)
2169 goto out;
2170 }
2171
2172 if (vcpu->mmio_needed) {
2173 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2174 vcpu->mmio_read_completed = 1;
2175 vcpu->mmio_needed = 0;
2176 r = emulate_instruction(vcpu, kvm_run,
3427318f 2177 vcpu->mmio_fault_cr2, 0, 1);
02c83209
AK
2178 if (r == EMULATE_DO_MMIO) {
2179 /*
2180 * Read-modify-write. Back to userspace.
2181 */
02c83209
AK
2182 r = 0;
2183 goto out;
46fc1477 2184 }
6aa8b732
AK
2185 }
2186
8eb7d334 2187 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
cbdd1bea 2188 kvm_x86_ops->cache_regs(vcpu);
b4e63f56 2189 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
cbdd1bea 2190 kvm_x86_ops->decache_regs(vcpu);
b4e63f56
AK
2191 }
2192
04d2cc77 2193 r = __vcpu_run(vcpu, kvm_run);
6aa8b732 2194
039576c0 2195out:
1961d276
AK
2196 if (vcpu->sigset_active)
2197 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2198
6aa8b732
AK
2199 vcpu_put(vcpu);
2200 return r;
2201}
2202
bccf2150
AK
2203static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2204 struct kvm_regs *regs)
6aa8b732 2205{
bccf2150 2206 vcpu_load(vcpu);
6aa8b732 2207
cbdd1bea 2208 kvm_x86_ops->cache_regs(vcpu);
6aa8b732
AK
2209
2210 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2211 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2212 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2213 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2214 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2215 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2216 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2217 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
05b3e0c2 2218#ifdef CONFIG_X86_64
6aa8b732
AK
2219 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2220 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2221 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2222 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2223 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2224 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2225 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2226 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2227#endif
2228
2229 regs->rip = vcpu->rip;
cbdd1bea 2230 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
6aa8b732
AK
2231
2232 /*
2233 * Don't leak debug flags in case they were set for guest debugging
2234 */
2235 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2236 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2237
2238 vcpu_put(vcpu);
2239
2240 return 0;
2241}
2242
bccf2150
AK
2243static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2244 struct kvm_regs *regs)
6aa8b732 2245{
bccf2150 2246 vcpu_load(vcpu);
6aa8b732
AK
2247
2248 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2249 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2250 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2251 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2252 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2253 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2254 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2255 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
05b3e0c2 2256#ifdef CONFIG_X86_64
6aa8b732
AK
2257 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2258 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2259 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2260 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2261 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2262 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2263 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2264 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2265#endif
2266
2267 vcpu->rip = regs->rip;
cbdd1bea 2268 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
6aa8b732 2269
cbdd1bea 2270 kvm_x86_ops->decache_regs(vcpu);
6aa8b732
AK
2271
2272 vcpu_put(vcpu);
2273
2274 return 0;
2275}
2276
2277static void get_segment(struct kvm_vcpu *vcpu,
2278 struct kvm_segment *var, int seg)
2279{
cbdd1bea 2280 return kvm_x86_ops->get_segment(vcpu, var, seg);
6aa8b732
AK
2281}
2282
bccf2150
AK
2283static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2284 struct kvm_sregs *sregs)
6aa8b732 2285{
6aa8b732 2286 struct descriptor_table dt;
2a8067f1 2287 int pending_vec;
6aa8b732 2288
bccf2150 2289 vcpu_load(vcpu);
6aa8b732
AK
2290
2291 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2292 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2293 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2294 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2295 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2296 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2297
2298 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2299 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2300
cbdd1bea 2301 kvm_x86_ops->get_idt(vcpu, &dt);
6aa8b732
AK
2302 sregs->idt.limit = dt.limit;
2303 sregs->idt.base = dt.base;
cbdd1bea 2304 kvm_x86_ops->get_gdt(vcpu, &dt);
6aa8b732
AK
2305 sregs->gdt.limit = dt.limit;
2306 sregs->gdt.base = dt.base;
2307
cbdd1bea 2308 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
6aa8b732
AK
2309 sregs->cr0 = vcpu->cr0;
2310 sregs->cr2 = vcpu->cr2;
2311 sregs->cr3 = vcpu->cr3;
2312 sregs->cr4 = vcpu->cr4;
7017fc3d 2313 sregs->cr8 = get_cr8(vcpu);
6aa8b732 2314 sregs->efer = vcpu->shadow_efer;
7017fc3d 2315 sregs->apic_base = kvm_get_apic_base(vcpu);
6aa8b732 2316
2a8067f1 2317 if (irqchip_in_kernel(vcpu->kvm)) {
c52fb35a
HQ
2318 memset(sregs->interrupt_bitmap, 0,
2319 sizeof sregs->interrupt_bitmap);
cbdd1bea 2320 pending_vec = kvm_x86_ops->get_irq(vcpu);
2a8067f1
ED
2321 if (pending_vec >= 0)
2322 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2323 } else
c52fb35a
HQ
2324 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2325 sizeof sregs->interrupt_bitmap);
6aa8b732
AK
2326
2327 vcpu_put(vcpu);
2328
2329 return 0;
2330}
2331
2332static void set_segment(struct kvm_vcpu *vcpu,
2333 struct kvm_segment *var, int seg)
2334{
cbdd1bea 2335 return kvm_x86_ops->set_segment(vcpu, var, seg);
6aa8b732
AK
2336}
2337
bccf2150
AK
2338static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2339 struct kvm_sregs *sregs)
6aa8b732 2340{
6aa8b732 2341 int mmu_reset_needed = 0;
2a8067f1 2342 int i, pending_vec, max_bits;
6aa8b732
AK
2343 struct descriptor_table dt;
2344
bccf2150 2345 vcpu_load(vcpu);
6aa8b732 2346
6aa8b732
AK
2347 dt.limit = sregs->idt.limit;
2348 dt.base = sregs->idt.base;
cbdd1bea 2349 kvm_x86_ops->set_idt(vcpu, &dt);
6aa8b732
AK
2350 dt.limit = sregs->gdt.limit;
2351 dt.base = sregs->gdt.base;
cbdd1bea 2352 kvm_x86_ops->set_gdt(vcpu, &dt);
6aa8b732
AK
2353
2354 vcpu->cr2 = sregs->cr2;
2355 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2356 vcpu->cr3 = sregs->cr3;
2357
7017fc3d 2358 set_cr8(vcpu, sregs->cr8);
6aa8b732
AK
2359
2360 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
05b3e0c2 2361#ifdef CONFIG_X86_64
cbdd1bea 2362 kvm_x86_ops->set_efer(vcpu, sregs->efer);
6aa8b732 2363#endif
7017fc3d 2364 kvm_set_apic_base(vcpu, sregs->apic_base);
6aa8b732 2365
cbdd1bea 2366 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
399badf3 2367
6aa8b732 2368 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
81f50e3b 2369 vcpu->cr0 = sregs->cr0;
cbdd1bea 2370 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
6aa8b732
AK
2371
2372 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
cbdd1bea 2373 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
1b0973bd
AK
2374 if (!is_long_mode(vcpu) && is_pae(vcpu))
2375 load_pdptrs(vcpu, vcpu->cr3);
6aa8b732
AK
2376
2377 if (mmu_reset_needed)
2378 kvm_mmu_reset_context(vcpu);
2379
c52fb35a
HQ
2380 if (!irqchip_in_kernel(vcpu->kvm)) {
2381 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2382 sizeof vcpu->irq_pending);
2383 vcpu->irq_summary = 0;
2384 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2385 if (vcpu->irq_pending[i])
2386 __set_bit(i, &vcpu->irq_summary);
2a8067f1
ED
2387 } else {
2388 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2389 pending_vec = find_first_bit(
2390 (const unsigned long *)sregs->interrupt_bitmap,
2391 max_bits);
2392 /* Only pending external irq is handled here */
2393 if (pending_vec < max_bits) {
cbdd1bea 2394 kvm_x86_ops->set_irq(vcpu, pending_vec);
2a8067f1
ED
2395 printk("Set back pending irq %d\n", pending_vec);
2396 }
c52fb35a 2397 }
6aa8b732 2398
024aa1c0
AK
2399 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2400 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2401 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2402 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2403 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2404 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2405
2406 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2407 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2408
6aa8b732
AK
2409 vcpu_put(vcpu);
2410
2411 return 0;
2412}
2413
1747fb71
RR
2414void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2415{
2416 struct kvm_segment cs;
2417
2418 get_segment(vcpu, &cs, VCPU_SREG_CS);
2419 *db = cs.db;
2420 *l = cs.l;
2421}
2422EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2423
6aa8b732
AK
2424/*
2425 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2426 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
bf591b24
MR
2427 *
2428 * This list is modified at module load time to reflect the
2429 * capabilities of the host cpu.
6aa8b732
AK
2430 */
2431static u32 msrs_to_save[] = {
2432 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2433 MSR_K6_STAR,
05b3e0c2 2434#ifdef CONFIG_X86_64
6aa8b732
AK
2435 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2436#endif
2437 MSR_IA32_TIME_STAMP_COUNTER,
2438};
2439
bf591b24
MR
2440static unsigned num_msrs_to_save;
2441
6f00e68f
AK
2442static u32 emulated_msrs[] = {
2443 MSR_IA32_MISC_ENABLE,
2444};
2445
bf591b24
MR
2446static __init void kvm_init_msr_list(void)
2447{
2448 u32 dummy[2];
2449 unsigned i, j;
2450
2451 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2452 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2453 continue;
2454 if (j < i)
2455 msrs_to_save[j] = msrs_to_save[i];
2456 j++;
2457 }
2458 num_msrs_to_save = j;
2459}
6aa8b732
AK
2460
2461/*
2462 * Adapt set_msr() to msr_io()'s calling convention
2463 */
2464static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2465{
35f3f286 2466 return kvm_set_msr(vcpu, index, *data);
6aa8b732
AK
2467}
2468
2469/*
2470 * Read or write a bunch of msrs. All parameters are kernel addresses.
2471 *
2472 * @return number of msrs set successfully.
2473 */
bccf2150 2474static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
6aa8b732
AK
2475 struct kvm_msr_entry *entries,
2476 int (*do_msr)(struct kvm_vcpu *vcpu,
2477 unsigned index, u64 *data))
2478{
6aa8b732
AK
2479 int i;
2480
bccf2150 2481 vcpu_load(vcpu);
6aa8b732
AK
2482
2483 for (i = 0; i < msrs->nmsrs; ++i)
2484 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2485 break;
2486
2487 vcpu_put(vcpu);
2488
2489 return i;
2490}
2491
2492/*
2493 * Read or write a bunch of msrs. Parameters are user addresses.
2494 *
2495 * @return number of msrs set successfully.
2496 */
bccf2150 2497static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
6aa8b732
AK
2498 int (*do_msr)(struct kvm_vcpu *vcpu,
2499 unsigned index, u64 *data),
2500 int writeback)
2501{
2502 struct kvm_msrs msrs;
2503 struct kvm_msr_entry *entries;
2504 int r, n;
2505 unsigned size;
2506
2507 r = -EFAULT;
2508 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2509 goto out;
2510
2511 r = -E2BIG;
2512 if (msrs.nmsrs >= MAX_IO_MSRS)
2513 goto out;
2514
2515 r = -ENOMEM;
2516 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2517 entries = vmalloc(size);
2518 if (!entries)
2519 goto out;
2520
2521 r = -EFAULT;
2522 if (copy_from_user(entries, user_msrs->entries, size))
2523 goto out_free;
2524
bccf2150 2525 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
6aa8b732
AK
2526 if (r < 0)
2527 goto out_free;
2528
2529 r = -EFAULT;
2530 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2531 goto out_free;
2532
2533 r = n;
2534
2535out_free:
2536 vfree(entries);
2537out:
2538 return r;
2539}
2540
2541/*
2542 * Translate a guest virtual address to a guest physical address.
2543 */
bccf2150
AK
2544static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2545 struct kvm_translation *tr)
6aa8b732
AK
2546{
2547 unsigned long vaddr = tr->linear_address;
6aa8b732
AK
2548 gpa_t gpa;
2549
bccf2150 2550 vcpu_load(vcpu);
11ec2804 2551 mutex_lock(&vcpu->kvm->lock);
6aa8b732
AK
2552 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2553 tr->physical_address = gpa;
2554 tr->valid = gpa != UNMAPPED_GVA;
2555 tr->writeable = 1;
2556 tr->usermode = 0;
11ec2804 2557 mutex_unlock(&vcpu->kvm->lock);
6aa8b732
AK
2558 vcpu_put(vcpu);
2559
2560 return 0;
2561}
2562
bccf2150
AK
2563static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2564 struct kvm_interrupt *irq)
6aa8b732 2565{
6aa8b732
AK
2566 if (irq->irq < 0 || irq->irq >= 256)
2567 return -EINVAL;
97222cc8
ED
2568 if (irqchip_in_kernel(vcpu->kvm))
2569 return -ENXIO;
bccf2150 2570 vcpu_load(vcpu);
6aa8b732
AK
2571
2572 set_bit(irq->irq, vcpu->irq_pending);
2573 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2574
2575 vcpu_put(vcpu);
2576
2577 return 0;
2578}
2579
bccf2150
AK
2580static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2581 struct kvm_debug_guest *dbg)
6aa8b732 2582{
6aa8b732
AK
2583 int r;
2584
bccf2150 2585 vcpu_load(vcpu);
6aa8b732 2586
cbdd1bea 2587 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
6aa8b732
AK
2588
2589 vcpu_put(vcpu);
2590
2591 return r;
2592}
2593
9a2bb7f4
AK
2594static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2595 unsigned long address,
2596 int *type)
2597{
2598 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2599 unsigned long pgoff;
2600 struct page *page;
2601
9a2bb7f4 2602 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
039576c0
AK
2603 if (pgoff == 0)
2604 page = virt_to_page(vcpu->run);
2605 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2606 page = virt_to_page(vcpu->pio_data);
2607 else
9a2bb7f4 2608 return NOPAGE_SIGBUS;
9a2bb7f4 2609 get_page(page);
cd0d9137
NAQ
2610 if (type != NULL)
2611 *type = VM_FAULT_MINOR;
2612
9a2bb7f4
AK
2613 return page;
2614}
2615
2616static struct vm_operations_struct kvm_vcpu_vm_ops = {
2617 .nopage = kvm_vcpu_nopage,
2618};
2619
2620static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2621{
2622 vma->vm_ops = &kvm_vcpu_vm_ops;
2623 return 0;
2624}
2625
bccf2150
AK
2626static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2627{
2628 struct kvm_vcpu *vcpu = filp->private_data;
2629
2630 fput(vcpu->kvm->filp);
2631 return 0;
2632}
2633
2634static struct file_operations kvm_vcpu_fops = {
2635 .release = kvm_vcpu_release,
2636 .unlocked_ioctl = kvm_vcpu_ioctl,
2637 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 2638 .mmap = kvm_vcpu_mmap,
bccf2150
AK
2639};
2640
2641/*
2642 * Allocates an inode for the vcpu.
2643 */
2644static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2645{
2646 int fd, r;
2647 struct inode *inode;
2648 struct file *file;
2649
d6d28168
AK
2650 r = anon_inode_getfd(&fd, &inode, &file,
2651 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2652 if (r)
2653 return r;
bccf2150 2654 atomic_inc(&vcpu->kvm->filp->f_count);
bccf2150 2655 return fd;
bccf2150
AK
2656}
2657
c5ea7660
AK
2658/*
2659 * Creates some virtual cpus. Good luck creating more than one.
2660 */
2661static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2662{
2663 int r;
2664 struct kvm_vcpu *vcpu;
2665
c5ea7660 2666 if (!valid_vcpu(n))
fb3f0f51 2667 return -EINVAL;
c5ea7660 2668
cbdd1bea 2669 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
fb3f0f51
RR
2670 if (IS_ERR(vcpu))
2671 return PTR_ERR(vcpu);
c5ea7660 2672
15ad7146
AK
2673 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2674
b114b080
RR
2675 /* We do fxsave: this must be aligned. */
2676 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2677
fb3f0f51 2678 vcpu_load(vcpu);
c5ea7660 2679 r = kvm_mmu_setup(vcpu);
c5ea7660 2680 vcpu_put(vcpu);
c5ea7660 2681 if (r < 0)
fb3f0f51
RR
2682 goto free_vcpu;
2683
11ec2804 2684 mutex_lock(&kvm->lock);
fb3f0f51
RR
2685 if (kvm->vcpus[n]) {
2686 r = -EEXIST;
11ec2804 2687 mutex_unlock(&kvm->lock);
fb3f0f51
RR
2688 goto mmu_unload;
2689 }
2690 kvm->vcpus[n] = vcpu;
11ec2804 2691 mutex_unlock(&kvm->lock);
c5ea7660 2692
fb3f0f51 2693 /* Now it's all set up, let userspace reach it */
bccf2150
AK
2694 r = create_vcpu_fd(vcpu);
2695 if (r < 0)
fb3f0f51
RR
2696 goto unlink;
2697 return r;
39c3b86e 2698
fb3f0f51 2699unlink:
11ec2804 2700 mutex_lock(&kvm->lock);
fb3f0f51 2701 kvm->vcpus[n] = NULL;
11ec2804 2702 mutex_unlock(&kvm->lock);
a2fa3e9f 2703
fb3f0f51
RR
2704mmu_unload:
2705 vcpu_load(vcpu);
2706 kvm_mmu_unload(vcpu);
2707 vcpu_put(vcpu);
c5ea7660 2708
fb3f0f51 2709free_vcpu:
cbdd1bea 2710 kvm_x86_ops->vcpu_free(vcpu);
c5ea7660
AK
2711 return r;
2712}
2713
2cc51560
ED
2714static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2715{
2716 u64 efer;
2717 int i;
2718 struct kvm_cpuid_entry *e, *entry;
2719
2720 rdmsrl(MSR_EFER, efer);
2721 entry = NULL;
2722 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2723 e = &vcpu->cpuid_entries[i];
2724 if (e->function == 0x80000001) {
2725 entry = e;
2726 break;
2727 }
2728 }
4c981b43 2729 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2cc51560 2730 entry->edx &= ~(1 << 20);
4c981b43 2731 printk(KERN_INFO "kvm: guest NX capability removed\n");
2cc51560
ED
2732 }
2733}
2734
06465c5a
AK
2735static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2736 struct kvm_cpuid *cpuid,
2737 struct kvm_cpuid_entry __user *entries)
2738{
2739 int r;
2740
2741 r = -E2BIG;
2742 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2743 goto out;
2744 r = -EFAULT;
2745 if (copy_from_user(&vcpu->cpuid_entries, entries,
2746 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2747 goto out;
2748 vcpu->cpuid_nent = cpuid->nent;
2cc51560 2749 cpuid_fix_nx_cap(vcpu);
06465c5a
AK
2750 return 0;
2751
2752out:
2753 return r;
2754}
2755
1961d276
AK
2756static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2757{
2758 if (sigset) {
2759 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2760 vcpu->sigset_active = 1;
2761 vcpu->sigset = *sigset;
2762 } else
2763 vcpu->sigset_active = 0;
2764 return 0;
2765}
2766
b8836737
AK
2767/*
2768 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2769 * we have asm/x86/processor.h
2770 */
2771struct fxsave {
2772 u16 cwd;
2773 u16 swd;
2774 u16 twd;
2775 u16 fop;
2776 u64 rip;
2777 u64 rdp;
2778 u32 mxcsr;
2779 u32 mxcsr_mask;
2780 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2781#ifdef CONFIG_X86_64
2782 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2783#else
2784 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2785#endif
2786};
2787
2788static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2789{
b114b080 2790 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
b8836737
AK
2791
2792 vcpu_load(vcpu);
2793
2794 memcpy(fpu->fpr, fxsave->st_space, 128);
2795 fpu->fcw = fxsave->cwd;
2796 fpu->fsw = fxsave->swd;
2797 fpu->ftwx = fxsave->twd;
2798 fpu->last_opcode = fxsave->fop;
2799 fpu->last_ip = fxsave->rip;
2800 fpu->last_dp = fxsave->rdp;
2801 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2802
2803 vcpu_put(vcpu);
2804
2805 return 0;
2806}
2807
2808static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2809{
b114b080 2810 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
b8836737
AK
2811
2812 vcpu_load(vcpu);
2813
2814 memcpy(fxsave->st_space, fpu->fpr, 128);
2815 fxsave->cwd = fpu->fcw;
2816 fxsave->swd = fpu->fsw;
2817 fxsave->twd = fpu->ftwx;
2818 fxsave->fop = fpu->last_opcode;
2819 fxsave->rip = fpu->last_ip;
2820 fxsave->rdp = fpu->last_dp;
2821 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2822
2823 vcpu_put(vcpu);
2824
2825 return 0;
2826}
2827
96ad2cc6
ED
2828static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2829 struct kvm_lapic_state *s)
2830{
2831 vcpu_load(vcpu);
2832 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2833 vcpu_put(vcpu);
2834
2835 return 0;
2836}
2837
2838static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2839 struct kvm_lapic_state *s)
2840{
2841 vcpu_load(vcpu);
2842 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2843 kvm_apic_post_state_restore(vcpu);
2844 vcpu_put(vcpu);
2845
2846 return 0;
2847}
2848
bccf2150
AK
2849static long kvm_vcpu_ioctl(struct file *filp,
2850 unsigned int ioctl, unsigned long arg)
6aa8b732 2851{
bccf2150 2852 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 2853 void __user *argp = (void __user *)arg;
6aa8b732
AK
2854 int r = -EINVAL;
2855
2856 switch (ioctl) {
9a2bb7f4 2857 case KVM_RUN:
f0fe5108
AK
2858 r = -EINVAL;
2859 if (arg)
2860 goto out;
9a2bb7f4 2861 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 2862 break;
6aa8b732
AK
2863 case KVM_GET_REGS: {
2864 struct kvm_regs kvm_regs;
2865
bccf2150
AK
2866 memset(&kvm_regs, 0, sizeof kvm_regs);
2867 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
6aa8b732
AK
2868 if (r)
2869 goto out;
2870 r = -EFAULT;
2f366987 2871 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
6aa8b732
AK
2872 goto out;
2873 r = 0;
2874 break;
2875 }
2876 case KVM_SET_REGS: {
2877 struct kvm_regs kvm_regs;
2878
2879 r = -EFAULT;
2f366987 2880 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732 2881 goto out;
bccf2150 2882 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
6aa8b732
AK
2883 if (r)
2884 goto out;
2885 r = 0;
2886 break;
2887 }
2888 case KVM_GET_SREGS: {
2889 struct kvm_sregs kvm_sregs;
2890
bccf2150
AK
2891 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2892 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2893 if (r)
2894 goto out;
2895 r = -EFAULT;
2f366987 2896 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
6aa8b732
AK
2897 goto out;
2898 r = 0;
2899 break;
2900 }
2901 case KVM_SET_SREGS: {
2902 struct kvm_sregs kvm_sregs;
2903
2904 r = -EFAULT;
2f366987 2905 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732 2906 goto out;
bccf2150 2907 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2908 if (r)
2909 goto out;
2910 r = 0;
2911 break;
2912 }
2913 case KVM_TRANSLATE: {
2914 struct kvm_translation tr;
2915
2916 r = -EFAULT;
2f366987 2917 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 2918 goto out;
bccf2150 2919 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
2920 if (r)
2921 goto out;
2922 r = -EFAULT;
2f366987 2923 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
2924 goto out;
2925 r = 0;
2926 break;
2927 }
2928 case KVM_INTERRUPT: {
2929 struct kvm_interrupt irq;
2930
2931 r = -EFAULT;
2f366987 2932 if (copy_from_user(&irq, argp, sizeof irq))
6aa8b732 2933 goto out;
bccf2150 2934 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
6aa8b732
AK
2935 if (r)
2936 goto out;
2937 r = 0;
2938 break;
2939 }
2940 case KVM_DEBUG_GUEST: {
2941 struct kvm_debug_guest dbg;
2942
2943 r = -EFAULT;
2f366987 2944 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 2945 goto out;
bccf2150 2946 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
2947 if (r)
2948 goto out;
2949 r = 0;
2950 break;
2951 }
bccf2150 2952 case KVM_GET_MSRS:
35f3f286 2953 r = msr_io(vcpu, argp, kvm_get_msr, 1);
bccf2150
AK
2954 break;
2955 case KVM_SET_MSRS:
2956 r = msr_io(vcpu, argp, do_set_msr, 0);
2957 break;
06465c5a
AK
2958 case KVM_SET_CPUID: {
2959 struct kvm_cpuid __user *cpuid_arg = argp;
2960 struct kvm_cpuid cpuid;
2961
2962 r = -EFAULT;
2963 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2964 goto out;
2965 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2966 if (r)
2967 goto out;
2968 break;
2969 }
1961d276
AK
2970 case KVM_SET_SIGNAL_MASK: {
2971 struct kvm_signal_mask __user *sigmask_arg = argp;
2972 struct kvm_signal_mask kvm_sigmask;
2973 sigset_t sigset, *p;
2974
2975 p = NULL;
2976 if (argp) {
2977 r = -EFAULT;
2978 if (copy_from_user(&kvm_sigmask, argp,
2979 sizeof kvm_sigmask))
2980 goto out;
2981 r = -EINVAL;
2982 if (kvm_sigmask.len != sizeof sigset)
2983 goto out;
2984 r = -EFAULT;
2985 if (copy_from_user(&sigset, sigmask_arg->sigset,
2986 sizeof sigset))
2987 goto out;
2988 p = &sigset;
2989 }
2990 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2991 break;
2992 }
b8836737
AK
2993 case KVM_GET_FPU: {
2994 struct kvm_fpu fpu;
2995
2996 memset(&fpu, 0, sizeof fpu);
2997 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2998 if (r)
2999 goto out;
3000 r = -EFAULT;
3001 if (copy_to_user(argp, &fpu, sizeof fpu))
3002 goto out;
3003 r = 0;
3004 break;
3005 }
3006 case KVM_SET_FPU: {
3007 struct kvm_fpu fpu;
3008
3009 r = -EFAULT;
3010 if (copy_from_user(&fpu, argp, sizeof fpu))
3011 goto out;
3012 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
3013 if (r)
3014 goto out;
3015 r = 0;
3016 break;
3017 }
96ad2cc6
ED
3018 case KVM_GET_LAPIC: {
3019 struct kvm_lapic_state lapic;
3020
3021 memset(&lapic, 0, sizeof lapic);
3022 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
3023 if (r)
3024 goto out;
3025 r = -EFAULT;
3026 if (copy_to_user(argp, &lapic, sizeof lapic))
3027 goto out;
3028 r = 0;
3029 break;
3030 }
3031 case KVM_SET_LAPIC: {
3032 struct kvm_lapic_state lapic;
3033
3034 r = -EFAULT;
3035 if (copy_from_user(&lapic, argp, sizeof lapic))
3036 goto out;
3037 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3038 if (r)
3039 goto out;
3040 r = 0;
3041 break;
3042 }
bccf2150
AK
3043 default:
3044 ;
3045 }
3046out:
3047 return r;
3048}
3049
3050static long kvm_vm_ioctl(struct file *filp,
3051 unsigned int ioctl, unsigned long arg)
3052{
3053 struct kvm *kvm = filp->private_data;
3054 void __user *argp = (void __user *)arg;
3055 int r = -EINVAL;
3056
3057 switch (ioctl) {
3058 case KVM_CREATE_VCPU:
3059 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3060 if (r < 0)
3061 goto out;
3062 break;
6aa8b732
AK
3063 case KVM_SET_MEMORY_REGION: {
3064 struct kvm_memory_region kvm_mem;
3065
3066 r = -EFAULT;
2f366987 3067 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
6aa8b732 3068 goto out;
2c6f5df9 3069 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
6aa8b732
AK
3070 if (r)
3071 goto out;
3072 break;
3073 }
3074 case KVM_GET_DIRTY_LOG: {
3075 struct kvm_dirty_log log;
3076
3077 r = -EFAULT;
2f366987 3078 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 3079 goto out;
2c6f5df9 3080 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
3081 if (r)
3082 goto out;
3083 break;
3084 }
e8207547
AK
3085 case KVM_SET_MEMORY_ALIAS: {
3086 struct kvm_memory_alias alias;
3087
3088 r = -EFAULT;
3089 if (copy_from_user(&alias, argp, sizeof alias))
3090 goto out;
3091 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3092 if (r)
3093 goto out;
3094 break;
3095 }
85f455f7
ED
3096 case KVM_CREATE_IRQCHIP:
3097 r = -ENOMEM;
3098 kvm->vpic = kvm_create_pic(kvm);
1fd4f2a5
ED
3099 if (kvm->vpic) {
3100 r = kvm_ioapic_init(kvm);
3101 if (r) {
3102 kfree(kvm->vpic);
3103 kvm->vpic = NULL;
3104 goto out;
3105 }
3106 }
85f455f7
ED
3107 else
3108 goto out;
3109 break;
3110 case KVM_IRQ_LINE: {
3111 struct kvm_irq_level irq_event;
3112
3113 r = -EFAULT;
3114 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3115 goto out;
3116 if (irqchip_in_kernel(kvm)) {
9cf98828 3117 mutex_lock(&kvm->lock);
85f455f7
ED
3118 if (irq_event.irq < 16)
3119 kvm_pic_set_irq(pic_irqchip(kvm),
3120 irq_event.irq,
3121 irq_event.level);
1fd4f2a5
ED
3122 kvm_ioapic_set_irq(kvm->vioapic,
3123 irq_event.irq,
3124 irq_event.level);
9cf98828 3125 mutex_unlock(&kvm->lock);
85f455f7
ED
3126 r = 0;
3127 }
3128 break;
3129 }
6ceb9d79
HQ
3130 case KVM_GET_IRQCHIP: {
3131 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3132 struct kvm_irqchip chip;
3133
3134 r = -EFAULT;
3135 if (copy_from_user(&chip, argp, sizeof chip))
3136 goto out;
3137 r = -ENXIO;
3138 if (!irqchip_in_kernel(kvm))
3139 goto out;
3140 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3141 if (r)
3142 goto out;
3143 r = -EFAULT;
3144 if (copy_to_user(argp, &chip, sizeof chip))
3145 goto out;
3146 r = 0;
3147 break;
3148 }
3149 case KVM_SET_IRQCHIP: {
3150 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3151 struct kvm_irqchip chip;
3152
3153 r = -EFAULT;
3154 if (copy_from_user(&chip, argp, sizeof chip))
3155 goto out;
3156 r = -ENXIO;
3157 if (!irqchip_in_kernel(kvm))
3158 goto out;
3159 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3160 if (r)
3161 goto out;
3162 r = 0;
3163 break;
3164 }
f17abe9a
AK
3165 default:
3166 ;
3167 }
3168out:
3169 return r;
3170}
3171
3172static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3173 unsigned long address,
3174 int *type)
3175{
3176 struct kvm *kvm = vma->vm_file->private_data;
3177 unsigned long pgoff;
f17abe9a
AK
3178 struct page *page;
3179
f17abe9a 3180 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
954bbbc2 3181 page = gfn_to_page(kvm, pgoff);
f17abe9a
AK
3182 if (!page)
3183 return NOPAGE_SIGBUS;
3184 get_page(page);
cd0d9137
NAQ
3185 if (type != NULL)
3186 *type = VM_FAULT_MINOR;
3187
f17abe9a
AK
3188 return page;
3189}
3190
3191static struct vm_operations_struct kvm_vm_vm_ops = {
3192 .nopage = kvm_vm_nopage,
3193};
3194
3195static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3196{
3197 vma->vm_ops = &kvm_vm_vm_ops;
3198 return 0;
3199}
3200
3201static struct file_operations kvm_vm_fops = {
3202 .release = kvm_vm_release,
3203 .unlocked_ioctl = kvm_vm_ioctl,
3204 .compat_ioctl = kvm_vm_ioctl,
3205 .mmap = kvm_vm_mmap,
3206};
3207
3208static int kvm_dev_ioctl_create_vm(void)
3209{
3210 int fd, r;
3211 struct inode *inode;
3212 struct file *file;
3213 struct kvm *kvm;
3214
f17abe9a 3215 kvm = kvm_create_vm();
d6d28168
AK
3216 if (IS_ERR(kvm))
3217 return PTR_ERR(kvm);
3218 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3219 if (r) {
3220 kvm_destroy_vm(kvm);
3221 return r;
f17abe9a
AK
3222 }
3223
bccf2150 3224 kvm->filp = file;
f17abe9a 3225
f17abe9a 3226 return fd;
f17abe9a
AK
3227}
3228
3229static long kvm_dev_ioctl(struct file *filp,
3230 unsigned int ioctl, unsigned long arg)
3231{
3232 void __user *argp = (void __user *)arg;
07c45a36 3233 long r = -EINVAL;
f17abe9a
AK
3234
3235 switch (ioctl) {
3236 case KVM_GET_API_VERSION:
f0fe5108
AK
3237 r = -EINVAL;
3238 if (arg)
3239 goto out;
f17abe9a
AK
3240 r = KVM_API_VERSION;
3241 break;
3242 case KVM_CREATE_VM:
f0fe5108
AK
3243 r = -EINVAL;
3244 if (arg)
3245 goto out;
f17abe9a
AK
3246 r = kvm_dev_ioctl_create_vm();
3247 break;
6aa8b732 3248 case KVM_GET_MSR_INDEX_LIST: {
2f366987 3249 struct kvm_msr_list __user *user_msr_list = argp;
6aa8b732
AK
3250 struct kvm_msr_list msr_list;
3251 unsigned n;
3252
3253 r = -EFAULT;
3254 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3255 goto out;
3256 n = msr_list.nmsrs;
6f00e68f 3257 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
6aa8b732
AK
3258 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3259 goto out;
3260 r = -E2BIG;
bf591b24 3261 if (n < num_msrs_to_save)
6aa8b732
AK
3262 goto out;
3263 r = -EFAULT;
3264 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
bf591b24 3265 num_msrs_to_save * sizeof(u32)))
6aa8b732 3266 goto out;
6f00e68f
AK
3267 if (copy_to_user(user_msr_list->indices
3268 + num_msrs_to_save * sizeof(u32),
3269 &emulated_msrs,
3270 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3271 goto out;
6aa8b732 3272 r = 0;
cc1d8955 3273 break;
6aa8b732 3274 }
85f455f7
ED
3275 case KVM_CHECK_EXTENSION: {
3276 int ext = (long)argp;
3277
3278 switch (ext) {
3279 case KVM_CAP_IRQCHIP:
b6958ce4 3280 case KVM_CAP_HLT:
85f455f7
ED
3281 r = 1;
3282 break;
3283 default:
3284 r = 0;
3285 break;
3286 }
5d308f45 3287 break;
85f455f7 3288 }
07c45a36
AK
3289 case KVM_GET_VCPU_MMAP_SIZE:
3290 r = -EINVAL;
3291 if (arg)
3292 goto out;
039576c0 3293 r = 2 * PAGE_SIZE;
07c45a36 3294 break;
6aa8b732
AK
3295 default:
3296 ;
3297 }
3298out:
3299 return r;
3300}
3301
6aa8b732 3302static struct file_operations kvm_chardev_ops = {
6aa8b732
AK
3303 .unlocked_ioctl = kvm_dev_ioctl,
3304 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
3305};
3306
3307static struct miscdevice kvm_dev = {
bbe4432e 3308 KVM_MINOR,
6aa8b732
AK
3309 "kvm",
3310 &kvm_chardev_ops,
3311};
3312
774c47f1
AK
3313/*
3314 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3315 * cached on it.
3316 */
3317static void decache_vcpus_on_cpu(int cpu)
3318{
3319 struct kvm *vm;
3320 struct kvm_vcpu *vcpu;
3321 int i;
3322
3323 spin_lock(&kvm_lock);
11ec2804 3324 list_for_each_entry(vm, &vm_list, vm_list)
774c47f1 3325 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
fb3f0f51
RR
3326 vcpu = vm->vcpus[i];
3327 if (!vcpu)
3328 continue;
774c47f1
AK
3329 /*
3330 * If the vcpu is locked, then it is running on some
3331 * other cpu and therefore it is not cached on the
3332 * cpu in question.
3333 *
3334 * If it's not locked, check the last cpu it executed
3335 * on.
3336 */
3337 if (mutex_trylock(&vcpu->mutex)) {
3338 if (vcpu->cpu == cpu) {
cbdd1bea 3339 kvm_x86_ops->vcpu_decache(vcpu);
774c47f1
AK
3340 vcpu->cpu = -1;
3341 }
3342 mutex_unlock(&vcpu->mutex);
3343 }
3344 }
3345 spin_unlock(&kvm_lock);
3346}
3347
1b6c0168
AK
3348static void hardware_enable(void *junk)
3349{
3350 int cpu = raw_smp_processor_id();
3351
3352 if (cpu_isset(cpu, cpus_hardware_enabled))
3353 return;
3354 cpu_set(cpu, cpus_hardware_enabled);
cbdd1bea 3355 kvm_x86_ops->hardware_enable(NULL);
1b6c0168
AK
3356}
3357
3358static void hardware_disable(void *junk)
3359{
3360 int cpu = raw_smp_processor_id();
3361
3362 if (!cpu_isset(cpu, cpus_hardware_enabled))
3363 return;
3364 cpu_clear(cpu, cpus_hardware_enabled);
3365 decache_vcpus_on_cpu(cpu);
cbdd1bea 3366 kvm_x86_ops->hardware_disable(NULL);
1b6c0168
AK
3367}
3368
774c47f1
AK
3369static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3370 void *v)
3371{
3372 int cpu = (long)v;
3373
3374 switch (val) {
cec9ad27
AK
3375 case CPU_DYING:
3376 case CPU_DYING_FROZEN:
6ec8a856
AK
3377 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3378 cpu);
3379 hardware_disable(NULL);
3380 break;
774c47f1 3381 case CPU_UP_CANCELED:
8bb78442 3382 case CPU_UP_CANCELED_FROZEN:
43934a38
JK
3383 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3384 cpu);
1b6c0168 3385 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
774c47f1 3386 break;
43934a38 3387 case CPU_ONLINE:
8bb78442 3388 case CPU_ONLINE_FROZEN:
43934a38
JK
3389 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3390 cpu);
1b6c0168 3391 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
774c47f1
AK
3392 break;
3393 }
3394 return NOTIFY_OK;
3395}
3396
9a2b85c6
RR
3397static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3398 void *v)
3399{
3400 if (val == SYS_RESTART) {
3401 /*
3402 * Some (well, at least mine) BIOSes hang on reboot if
3403 * in vmx root mode.
3404 */
3405 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3406 on_each_cpu(hardware_disable, NULL, 0, 1);
3407 }
3408 return NOTIFY_OK;
3409}
3410
3411static struct notifier_block kvm_reboot_notifier = {
3412 .notifier_call = kvm_reboot,
3413 .priority = 0,
3414};
3415
2eeb2e94
GH
3416void kvm_io_bus_init(struct kvm_io_bus *bus)
3417{
3418 memset(bus, 0, sizeof(*bus));
3419}
3420
3421void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3422{
3423 int i;
3424
3425 for (i = 0; i < bus->dev_count; i++) {
3426 struct kvm_io_device *pos = bus->devs[i];
3427
3428 kvm_iodevice_destructor(pos);
3429 }
3430}
3431
3432struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3433{
3434 int i;
3435
3436 for (i = 0; i < bus->dev_count; i++) {
3437 struct kvm_io_device *pos = bus->devs[i];
3438
3439 if (pos->in_range(pos, addr))
3440 return pos;
3441 }
3442
3443 return NULL;
3444}
3445
3446void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3447{
3448 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3449
3450 bus->devs[bus->dev_count++] = dev;
3451}
3452
774c47f1
AK
3453static struct notifier_block kvm_cpu_notifier = {
3454 .notifier_call = kvm_cpu_hotplug,
3455 .priority = 20, /* must be > scheduler priority */
3456};
3457
1165f5fe
AK
3458static u64 stat_get(void *_offset)
3459{
3460 unsigned offset = (long)_offset;
3461 u64 total = 0;
3462 struct kvm *kvm;
3463 struct kvm_vcpu *vcpu;
3464 int i;
3465
3466 spin_lock(&kvm_lock);
3467 list_for_each_entry(kvm, &vm_list, vm_list)
3468 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
fb3f0f51
RR
3469 vcpu = kvm->vcpus[i];
3470 if (vcpu)
3471 total += *(u32 *)((void *)vcpu + offset);
1165f5fe
AK
3472 }
3473 spin_unlock(&kvm_lock);
3474 return total;
3475}
3476
3dea7ca7 3477DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
1165f5fe 3478
6aa8b732
AK
3479static __init void kvm_init_debug(void)
3480{
3481 struct kvm_stats_debugfs_item *p;
3482
8b6d44c7 3483 debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732 3484 for (p = debugfs_entries; p->name; ++p)
1165f5fe
AK
3485 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3486 (void *)(long)p->offset,
3487 &stat_fops);
6aa8b732
AK
3488}
3489
3490static void kvm_exit_debug(void)
3491{
3492 struct kvm_stats_debugfs_item *p;
3493
3494 for (p = debugfs_entries; p->name; ++p)
3495 debugfs_remove(p->dentry);
3496 debugfs_remove(debugfs_dir);
3497}
3498
59ae6c6b
AK
3499static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3500{
4267c41a 3501 hardware_disable(NULL);
59ae6c6b
AK
3502 return 0;
3503}
3504
3505static int kvm_resume(struct sys_device *dev)
3506{
4267c41a 3507 hardware_enable(NULL);
59ae6c6b
AK
3508 return 0;
3509}
3510
3511static struct sysdev_class kvm_sysdev_class = {
af5ca3f4 3512 .name = "kvm",
59ae6c6b
AK
3513 .suspend = kvm_suspend,
3514 .resume = kvm_resume,
3515};
3516
3517static struct sys_device kvm_sysdev = {
3518 .id = 0,
3519 .cls = &kvm_sysdev_class,
3520};
3521
6aa8b732
AK
3522hpa_t bad_page_address;
3523
15ad7146
AK
3524static inline
3525struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3526{
3527 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3528}
3529
3530static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3531{
3532 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3533
cbdd1bea 3534 kvm_x86_ops->vcpu_load(vcpu, cpu);
15ad7146
AK
3535}
3536
3537static void kvm_sched_out(struct preempt_notifier *pn,
3538 struct task_struct *next)
3539{
3540 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3541
cbdd1bea 3542 kvm_x86_ops->vcpu_put(vcpu);
15ad7146
AK
3543}
3544
cbdd1bea 3545int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
c16f862d 3546 struct module *module)
6aa8b732
AK
3547{
3548 int r;
002c7f7c 3549 int cpu;
6aa8b732 3550
cbdd1bea 3551 if (kvm_x86_ops) {
09db28b8
YI
3552 printk(KERN_ERR "kvm: already loaded the other module\n");
3553 return -EEXIST;
3554 }
3555
e097f35c 3556 if (!ops->cpu_has_kvm_support()) {
6aa8b732
AK
3557 printk(KERN_ERR "kvm: no hardware support\n");
3558 return -EOPNOTSUPP;
3559 }
e097f35c 3560 if (ops->disabled_by_bios()) {
6aa8b732
AK
3561 printk(KERN_ERR "kvm: disabled by bios\n");
3562 return -EOPNOTSUPP;
3563 }
3564
cbdd1bea 3565 kvm_x86_ops = ops;
e097f35c 3566
cbdd1bea 3567 r = kvm_x86_ops->hardware_setup();
6aa8b732 3568 if (r < 0)
ca45aaae 3569 goto out;
6aa8b732 3570
002c7f7c
YS
3571 for_each_online_cpu(cpu) {
3572 smp_call_function_single(cpu,
cbdd1bea 3573 kvm_x86_ops->check_processor_compatibility,
002c7f7c
YS
3574 &r, 0, 1);
3575 if (r < 0)
3576 goto out_free_0;
3577 }
3578
1b6c0168 3579 on_each_cpu(hardware_enable, NULL, 0, 1);
774c47f1
AK
3580 r = register_cpu_notifier(&kvm_cpu_notifier);
3581 if (r)
3582 goto out_free_1;
6aa8b732
AK
3583 register_reboot_notifier(&kvm_reboot_notifier);
3584
59ae6c6b
AK
3585 r = sysdev_class_register(&kvm_sysdev_class);
3586 if (r)
3587 goto out_free_2;
3588
3589 r = sysdev_register(&kvm_sysdev);
3590 if (r)
3591 goto out_free_3;
3592
c16f862d
RR
3593 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3594 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3595 __alignof__(struct kvm_vcpu), 0, 0);
3596 if (!kvm_vcpu_cache) {
3597 r = -ENOMEM;
3598 goto out_free_4;
3599 }
3600
6aa8b732
AK
3601 kvm_chardev_ops.owner = module;
3602
3603 r = misc_register(&kvm_dev);
3604 if (r) {
3605 printk (KERN_ERR "kvm: misc device register failed\n");
3606 goto out_free;
3607 }
3608
15ad7146
AK
3609 kvm_preempt_ops.sched_in = kvm_sched_in;
3610 kvm_preempt_ops.sched_out = kvm_sched_out;
3611
c7addb90
AK
3612 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3613
3614 return 0;
6aa8b732
AK
3615
3616out_free:
c16f862d
RR
3617 kmem_cache_destroy(kvm_vcpu_cache);
3618out_free_4:
59ae6c6b
AK
3619 sysdev_unregister(&kvm_sysdev);
3620out_free_3:
3621 sysdev_class_unregister(&kvm_sysdev_class);
3622out_free_2:
6aa8b732 3623 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1
AK
3624 unregister_cpu_notifier(&kvm_cpu_notifier);
3625out_free_1:
1b6c0168 3626 on_each_cpu(hardware_disable, NULL, 0, 1);
002c7f7c 3627out_free_0:
cbdd1bea 3628 kvm_x86_ops->hardware_unsetup();
ca45aaae 3629out:
cbdd1bea 3630 kvm_x86_ops = NULL;
6aa8b732
AK
3631 return r;
3632}
3633
cbdd1bea 3634void kvm_exit_x86(void)
6aa8b732
AK
3635{
3636 misc_deregister(&kvm_dev);
c16f862d 3637 kmem_cache_destroy(kvm_vcpu_cache);
59ae6c6b
AK
3638 sysdev_unregister(&kvm_sysdev);
3639 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 3640 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 3641 unregister_cpu_notifier(&kvm_cpu_notifier);
1b6c0168 3642 on_each_cpu(hardware_disable, NULL, 0, 1);
cbdd1bea
CE
3643 kvm_x86_ops->hardware_unsetup();
3644 kvm_x86_ops = NULL;
6aa8b732
AK
3645}
3646
3647static __init int kvm_init(void)
3648{
3649 static struct page *bad_page;
37e29d90
AK
3650 int r;
3651
b5a33a75
AK
3652 r = kvm_mmu_module_init();
3653 if (r)
3654 goto out4;
3655
6aa8b732
AK
3656 kvm_init_debug();
3657
bf591b24
MR
3658 kvm_init_msr_list();
3659
6aa8b732
AK
3660 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3661 r = -ENOMEM;
3662 goto out;
3663 }
3664
3665 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3666 memset(__va(bad_page_address), 0, PAGE_SIZE);
3667
58e690e6 3668 return 0;
6aa8b732
AK
3669
3670out:
3671 kvm_exit_debug();
b5a33a75
AK
3672 kvm_mmu_module_exit();
3673out4:
6aa8b732
AK
3674 return r;
3675}
3676
3677static __exit void kvm_exit(void)
3678{
3679 kvm_exit_debug();
3680 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
b5a33a75 3681 kvm_mmu_module_exit();
6aa8b732
AK
3682}
3683
3684module_init(kvm_init)
3685module_exit(kvm_exit)
3686
cbdd1bea
CE
3687EXPORT_SYMBOL_GPL(kvm_init_x86);
3688EXPORT_SYMBOL_GPL(kvm_exit_x86);