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