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