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