blob: caec54fbf07faed2814cc4312bfbf83ad8d74176 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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>
Andrew Mortone9cdb1e2007-03-01 11:28:13 +020023#include <linux/magic.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#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>
Avi Kivity59ae6c62007-02-12 00:54:48 -080038#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080039#include <linux/cpu.h>
Avi Kivityf17abe92007-02-21 19:28:04 +020040#include <linux/file.h>
Avi Kivity37e29d92007-02-20 14:07:37 +020041#include <linux/fs.h>
42#include <linux/mount.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
44#include "x86_emulate.h"
45#include "segment_descriptor.h"
46
47MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
Avi Kivity133de902007-02-12 00:54:44 -080050static DEFINE_SPINLOCK(kvm_lock);
51static LIST_HEAD(vm_list);
52
Avi Kivity6aa8b732006-12-10 02:21:36 -080053struct kvm_arch_ops *kvm_arch_ops;
54struct kvm_stat kvm_stat;
55EXPORT_SYMBOL_GPL(kvm_stat);
56
57static struct kvm_stats_debugfs_item {
58 const char *name;
59 u32 *data;
60 struct dentry *dentry;
61} debugfs_entries[] = {
62 { "pf_fixed", &kvm_stat.pf_fixed },
63 { "pf_guest", &kvm_stat.pf_guest },
64 { "tlb_flush", &kvm_stat.tlb_flush },
65 { "invlpg", &kvm_stat.invlpg },
66 { "exits", &kvm_stat.exits },
67 { "io_exits", &kvm_stat.io_exits },
68 { "mmio_exits", &kvm_stat.mmio_exits },
69 { "signal_exits", &kvm_stat.signal_exits },
Dor Laorc1150d82007-01-05 16:36:24 -080070 { "irq_window", &kvm_stat.irq_window_exits },
71 { "halt_exits", &kvm_stat.halt_exits },
72 { "request_irq", &kvm_stat.request_irq_exits },
Avi Kivity6aa8b732006-12-10 02:21:36 -080073 { "irq_exits", &kvm_stat.irq_exits },
Al Viro8b6d44c2007-02-09 16:38:40 +000074 { NULL, NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080075};
76
77static struct dentry *debugfs_dir;
78
Avi Kivity37e29d92007-02-20 14:07:37 +020079struct vfsmount *kvmfs_mnt;
80
Avi Kivity6aa8b732006-12-10 02:21:36 -080081#define MAX_IO_MSRS 256
82
83#define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
84#define LMSW_GUEST_MASK 0x0eULL
85#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
86#define CR8_RESEVED_BITS (~0x0fULL)
87#define EFER_RESERVED_BITS 0xfffffffffffff2fe
88
Avi Kivity05b3e0c2006-12-13 00:33:45 -080089#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080090// LDT or TSS descriptor in the GDT. 16 bytes.
91struct segment_descriptor_64 {
92 struct segment_descriptor s;
93 u32 base_higher;
94 u32 pad_zero;
95};
96
97#endif
98
Avi Kivitybccf2152007-02-21 18:04:26 +020099static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
100 unsigned long arg);
101
Avi Kivityf17abe92007-02-21 19:28:04 +0200102static struct inode *kvmfs_inode(struct file_operations *fops)
103{
104 int error = -ENOMEM;
105 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
106
107 if (!inode)
108 goto eexit_1;
109
110 inode->i_fop = fops;
111
112 /*
113 * Mark the inode dirty from the very beginning,
114 * that way it will never be moved to the dirty
115 * list because mark_inode_dirty() will think
116 * that it already _is_ on the dirty list.
117 */
118 inode->i_state = I_DIRTY;
119 inode->i_mode = S_IRUSR | S_IWUSR;
120 inode->i_uid = current->fsuid;
121 inode->i_gid = current->fsgid;
122 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
123 return inode;
124
125eexit_1:
126 return ERR_PTR(error);
127}
128
129static struct file *kvmfs_file(struct inode *inode, void *private_data)
130{
131 struct file *file = get_empty_filp();
132
133 if (!file)
134 return ERR_PTR(-ENFILE);
135
136 file->f_path.mnt = mntget(kvmfs_mnt);
137 file->f_path.dentry = d_alloc_anon(inode);
138 if (!file->f_path.dentry)
139 return ERR_PTR(-ENOMEM);
140 file->f_mapping = inode->i_mapping;
141
142 file->f_pos = 0;
143 file->f_flags = O_RDWR;
144 file->f_op = inode->i_fop;
145 file->f_mode = FMODE_READ | FMODE_WRITE;
146 file->f_version = 0;
147 file->private_data = private_data;
148 return file;
149}
150
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151unsigned long segment_base(u16 selector)
152{
153 struct descriptor_table gdt;
154 struct segment_descriptor *d;
155 unsigned long table_base;
156 typedef unsigned long ul;
157 unsigned long v;
158
159 if (selector == 0)
160 return 0;
161
162 asm ("sgdt %0" : "=m"(gdt));
163 table_base = gdt.base;
164
165 if (selector & 4) { /* from ldt */
166 u16 ldt_selector;
167
168 asm ("sldt %0" : "=g"(ldt_selector));
169 table_base = segment_base(ldt_selector);
170 }
171 d = (struct segment_descriptor *)(table_base + (selector & ~7));
172 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800173#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 if (d->system == 0
175 && (d->type == 2 || d->type == 9 || d->type == 11))
176 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
177#endif
178 return v;
179}
180EXPORT_SYMBOL_GPL(segment_base);
181
James Morris5aacf0c2006-12-22 01:04:55 -0800182static inline int valid_vcpu(int n)
183{
184 return likely(n >= 0 && n < KVM_MAX_VCPUS);
185}
186
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200187int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
188 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189{
190 unsigned char *host_buf = dest;
191 unsigned long req_size = size;
192
193 while (size) {
194 hpa_t paddr;
195 unsigned now;
196 unsigned offset;
197 hva_t guest_buf;
198
199 paddr = gva_to_hpa(vcpu, addr);
200
201 if (is_error_hpa(paddr))
202 break;
203
204 guest_buf = (hva_t)kmap_atomic(
205 pfn_to_page(paddr >> PAGE_SHIFT),
206 KM_USER0);
207 offset = addr & ~PAGE_MASK;
208 guest_buf |= offset;
209 now = min(size, PAGE_SIZE - offset);
210 memcpy(host_buf, (void*)guest_buf, now);
211 host_buf += now;
212 addr += now;
213 size -= now;
214 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
215 }
216 return req_size - size;
217}
218EXPORT_SYMBOL_GPL(kvm_read_guest);
219
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200220int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
221 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222{
223 unsigned char *host_buf = data;
224 unsigned long req_size = size;
225
226 while (size) {
227 hpa_t paddr;
228 unsigned now;
229 unsigned offset;
230 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200231 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232
233 paddr = gva_to_hpa(vcpu, addr);
234
235 if (is_error_hpa(paddr))
236 break;
237
Uri Lublinab51a432007-02-21 18:25:21 +0200238 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
239 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 guest_buf = (hva_t)kmap_atomic(
241 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
242 offset = addr & ~PAGE_MASK;
243 guest_buf |= offset;
244 now = min(size, PAGE_SIZE - offset);
245 memcpy((void*)guest_buf, host_buf, now);
246 host_buf += now;
247 addr += now;
248 size -= now;
249 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
250 }
251 return req_size - size;
252}
253EXPORT_SYMBOL_GPL(kvm_write_guest);
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255/*
256 * Switches to specified vcpu, until a matching vcpu_put()
257 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200258static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259{
Avi Kivitybccf2152007-02-21 18:04:26 +0200260 mutex_lock(&vcpu->mutex);
261 kvm_arch_ops->vcpu_load(vcpu);
262}
263
264/*
265 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
266 * if the slot is not populated.
267 */
268static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
269{
270 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271
272 mutex_lock(&vcpu->mutex);
Avi Kivitybccf2152007-02-21 18:04:26 +0200273 if (!vcpu->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 mutex_unlock(&vcpu->mutex);
Al Viro8b6d44c2007-02-09 16:38:40 +0000275 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200277 kvm_arch_ops->vcpu_load(vcpu);
278 return vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279}
280
281static void vcpu_put(struct kvm_vcpu *vcpu)
282{
283 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 mutex_unlock(&vcpu->mutex);
285}
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
290 int i;
291
292 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200293 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294
295 spin_lock_init(&kvm->lock);
296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
297 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
298 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
299
300 mutex_init(&vcpu->mutex);
Avi Kivity133de902007-02-12 00:54:44 -0800301 vcpu->cpu = -1;
Avi Kivity86a2b422007-01-05 16:36:57 -0800302 vcpu->kvm = kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 vcpu->mmu.root_hpa = INVALID_PAGE;
304 INIT_LIST_HEAD(&vcpu->free_pages);
Avi Kivity133de902007-02-12 00:54:44 -0800305 spin_lock(&kvm_lock);
306 list_add(&kvm->vm_list, &vm_list);
307 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200309 return kvm;
310}
311
312static int kvm_dev_open(struct inode *inode, struct file *filp)
313{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314 return 0;
315}
316
317/*
318 * Free any memory in @free but not in @dont.
319 */
320static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
321 struct kvm_memory_slot *dont)
322{
323 int i;
324
325 if (!dont || free->phys_mem != dont->phys_mem)
326 if (free->phys_mem) {
327 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800328 if (free->phys_mem[i])
329 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 vfree(free->phys_mem);
331 }
332
333 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
334 vfree(free->dirty_bitmap);
335
Al Viro8b6d44c2007-02-09 16:38:40 +0000336 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000338 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339}
340
341static void kvm_free_physmem(struct kvm *kvm)
342{
343 int i;
344
345 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000346 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347}
348
349static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
350{
Avi Kivitybccf2152007-02-21 18:04:26 +0200351 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800352 return;
353
Avi Kivitybccf2152007-02-21 18:04:26 +0200354 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800356 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800357 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200358 free_page((unsigned long)vcpu->run);
359 vcpu->run = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360}
361
362static void kvm_free_vcpus(struct kvm *kvm)
363{
364 unsigned int i;
365
366 for (i = 0; i < KVM_MAX_VCPUS; ++i)
367 kvm_free_vcpu(&kvm->vcpus[i]);
368}
369
370static int kvm_dev_release(struct inode *inode, struct file *filp)
371{
Avi Kivityf17abe92007-02-21 19:28:04 +0200372 return 0;
373}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374
Avi Kivityf17abe92007-02-21 19:28:04 +0200375static void kvm_destroy_vm(struct kvm *kvm)
376{
Avi Kivity133de902007-02-12 00:54:44 -0800377 spin_lock(&kvm_lock);
378 list_del(&kvm->vm_list);
379 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 kvm_free_vcpus(kvm);
381 kvm_free_physmem(kvm);
382 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200383}
384
385static int kvm_vm_release(struct inode *inode, struct file *filp)
386{
387 struct kvm *kvm = filp->private_data;
388
389 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390 return 0;
391}
392
393static void inject_gp(struct kvm_vcpu *vcpu)
394{
395 kvm_arch_ops->inject_gp(vcpu, 0);
396}
397
Avi Kivity1342d352007-01-05 16:36:39 -0800398/*
399 * Load the pae pdptrs. Return true is they are all valid.
400 */
401static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402{
403 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800404 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 int i;
406 u64 pdpte;
407 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800408 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 struct kvm_memory_slot *memslot;
410
411 spin_lock(&vcpu->kvm->lock);
412 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
413 /* FIXME: !memslot - emulate? 0xff? */
414 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
415
Avi Kivity1342d352007-01-05 16:36:39 -0800416 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417 for (i = 0; i < 4; ++i) {
418 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800419 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
420 ret = 0;
421 goto out;
422 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423 }
424
Avi Kivity1342d352007-01-05 16:36:39 -0800425 for (i = 0; i < 4; ++i)
426 vcpu->pdptrs[i] = pdpt[offset + i];
427
428out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 kunmap_atomic(pdpt, KM_USER0);
430 spin_unlock(&vcpu->kvm->lock);
431
Avi Kivity1342d352007-01-05 16:36:39 -0800432 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433}
434
435void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
436{
437 if (cr0 & CR0_RESEVED_BITS) {
438 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
439 cr0, vcpu->cr0);
440 inject_gp(vcpu);
441 return;
442 }
443
444 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
445 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
446 inject_gp(vcpu);
447 return;
448 }
449
450 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
451 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
452 "and a clear PE flag\n");
453 inject_gp(vcpu);
454 return;
455 }
456
457 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800458#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 if ((vcpu->shadow_efer & EFER_LME)) {
460 int cs_db, cs_l;
461
462 if (!is_pae(vcpu)) {
463 printk(KERN_DEBUG "set_cr0: #GP, start paging "
464 "in long mode while PAE is disabled\n");
465 inject_gp(vcpu);
466 return;
467 }
468 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
469 if (cs_l) {
470 printk(KERN_DEBUG "set_cr0: #GP, start paging "
471 "in long mode while CS.L == 1\n");
472 inject_gp(vcpu);
473 return;
474
475 }
476 } else
477#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800478 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
480 "reserved bits\n");
481 inject_gp(vcpu);
482 return;
483 }
484
485 }
486
487 kvm_arch_ops->set_cr0(vcpu, cr0);
488 vcpu->cr0 = cr0;
489
490 spin_lock(&vcpu->kvm->lock);
491 kvm_mmu_reset_context(vcpu);
492 spin_unlock(&vcpu->kvm->lock);
493 return;
494}
495EXPORT_SYMBOL_GPL(set_cr0);
496
497void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
498{
Avi Kivity399badf2007-01-05 16:36:38 -0800499 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
501}
502EXPORT_SYMBOL_GPL(lmsw);
503
504void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
505{
506 if (cr4 & CR4_RESEVED_BITS) {
507 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
508 inject_gp(vcpu);
509 return;
510 }
511
Avi Kivitya9058ec2006-12-29 16:49:37 -0800512 if (is_long_mode(vcpu)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 if (!(cr4 & CR4_PAE_MASK)) {
514 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
515 "in long mode\n");
516 inject_gp(vcpu);
517 return;
518 }
519 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
Avi Kivity1342d352007-01-05 16:36:39 -0800520 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
522 inject_gp(vcpu);
523 }
524
525 if (cr4 & CR4_VMXE_MASK) {
526 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
527 inject_gp(vcpu);
528 return;
529 }
530 kvm_arch_ops->set_cr4(vcpu, cr4);
531 spin_lock(&vcpu->kvm->lock);
532 kvm_mmu_reset_context(vcpu);
533 spin_unlock(&vcpu->kvm->lock);
534}
535EXPORT_SYMBOL_GPL(set_cr4);
536
537void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
538{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800539 if (is_long_mode(vcpu)) {
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200540 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 } else {
546 if (cr3 & CR3_RESEVED_BITS) {
547 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
551 if (is_paging(vcpu) && is_pae(vcpu) &&
Avi Kivity1342d352007-01-05 16:36:39 -0800552 !load_pdptrs(vcpu, cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
554 "reserved bits\n");
555 inject_gp(vcpu);
556 return;
557 }
558 }
559
560 vcpu->cr3 = cr3;
561 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800562 /*
563 * Does the new cr3 value map to physical memory? (Note, we
564 * catch an invalid cr3 even in real-mode, because it would
565 * cause trouble later on when we turn on paging anyway.)
566 *
567 * A real CPU would silently accept an invalid cr3 and would
568 * attempt to use it - with largely undefined (and often hard
569 * to debug) behavior on the guest side.
570 */
571 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
572 inject_gp(vcpu);
573 else
574 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 spin_unlock(&vcpu->kvm->lock);
576}
577EXPORT_SYMBOL_GPL(set_cr3);
578
579void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
580{
581 if ( cr8 & CR8_RESEVED_BITS) {
582 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
583 inject_gp(vcpu);
584 return;
585 }
586 vcpu->cr8 = cr8;
587}
588EXPORT_SYMBOL_GPL(set_cr8);
589
590void fx_init(struct kvm_vcpu *vcpu)
591{
592 struct __attribute__ ((__packed__)) fx_image_s {
593 u16 control; //fcw
594 u16 status; //fsw
595 u16 tag; // ftw
596 u16 opcode; //fop
597 u64 ip; // fpu ip
598 u64 operand;// fpu dp
599 u32 mxcsr;
600 u32 mxcsr_mask;
601
602 } *fx_image;
603
604 fx_save(vcpu->host_fx_image);
605 fpu_init();
606 fx_save(vcpu->guest_fx_image);
607 fx_restore(vcpu->host_fx_image);
608
609 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
610 fx_image->mxcsr = 0x1f80;
611 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
612 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
613}
614EXPORT_SYMBOL_GPL(fx_init);
615
Uri Lublin02b27c12007-02-22 17:15:33 +0200616static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
617{
618 spin_lock(&vcpu->kvm->lock);
619 kvm_mmu_slot_remove_write_access(vcpu, slot);
620 spin_unlock(&vcpu->kvm->lock);
621}
622
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 * Allocate some memory and give it an address in the guest physical address
625 * space.
626 *
627 * Discontiguous memory is allowed, mostly for framebuffers.
628 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200629static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
630 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631{
632 int r;
633 gfn_t base_gfn;
634 unsigned long npages;
635 unsigned long i;
636 struct kvm_memory_slot *memslot;
637 struct kvm_memory_slot old, new;
638 int memory_config_version;
639
640 r = -EINVAL;
641 /* General sanity checks */
642 if (mem->memory_size & (PAGE_SIZE - 1))
643 goto out;
644 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
645 goto out;
646 if (mem->slot >= KVM_MEMORY_SLOTS)
647 goto out;
648 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
649 goto out;
650
651 memslot = &kvm->memslots[mem->slot];
652 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
653 npages = mem->memory_size >> PAGE_SHIFT;
654
655 if (!npages)
656 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
657
658raced:
659 spin_lock(&kvm->lock);
660
661 memory_config_version = kvm->memory_config_version;
662 new = old = *memslot;
663
664 new.base_gfn = base_gfn;
665 new.npages = npages;
666 new.flags = mem->flags;
667
668 /* Disallow changing a memory slot's size. */
669 r = -EINVAL;
670 if (npages && old.npages && npages != old.npages)
671 goto out_unlock;
672
673 /* Check for overlaps */
674 r = -EEXIST;
675 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
676 struct kvm_memory_slot *s = &kvm->memslots[i];
677
678 if (s == memslot)
679 continue;
680 if (!((base_gfn + npages <= s->base_gfn) ||
681 (base_gfn >= s->base_gfn + s->npages)))
682 goto out_unlock;
683 }
684 /*
685 * Do memory allocations outside lock. memory_config_version will
686 * detect any races.
687 */
688 spin_unlock(&kvm->lock);
689
690 /* Deallocate if slot is being removed */
691 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000692 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693
694 /* Free page dirty bitmap if unneeded */
695 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000696 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697
698 r = -ENOMEM;
699
700 /* Allocate if a slot is being created */
701 if (npages && !new.phys_mem) {
702 new.phys_mem = vmalloc(npages * sizeof(struct page *));
703
704 if (!new.phys_mem)
705 goto out_free;
706
707 memset(new.phys_mem, 0, npages * sizeof(struct page *));
708 for (i = 0; i < npages; ++i) {
709 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
710 | __GFP_ZERO);
711 if (!new.phys_mem[i])
712 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200713 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 }
715 }
716
717 /* Allocate page dirty bitmap if needed */
718 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
719 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
720
721 new.dirty_bitmap = vmalloc(dirty_bytes);
722 if (!new.dirty_bitmap)
723 goto out_free;
724 memset(new.dirty_bitmap, 0, dirty_bytes);
725 }
726
727 spin_lock(&kvm->lock);
728
729 if (memory_config_version != kvm->memory_config_version) {
730 spin_unlock(&kvm->lock);
731 kvm_free_physmem_slot(&new, &old);
732 goto raced;
733 }
734
735 r = -EAGAIN;
736 if (kvm->busy)
737 goto out_unlock;
738
739 if (mem->slot >= kvm->nmemslots)
740 kvm->nmemslots = mem->slot + 1;
741
742 *memslot = new;
743 ++kvm->memory_config_version;
744
745 spin_unlock(&kvm->lock);
746
747 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
748 struct kvm_vcpu *vcpu;
749
Avi Kivitybccf2152007-02-21 18:04:26 +0200750 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 if (!vcpu)
752 continue;
Uri Lublinff990d52007-02-22 17:37:32 +0200753 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
754 do_remove_write_access(vcpu, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 kvm_mmu_reset_context(vcpu);
756 vcpu_put(vcpu);
757 }
758
759 kvm_free_physmem_slot(&old, &new);
760 return 0;
761
762out_unlock:
763 spin_unlock(&kvm->lock);
764out_free:
765 kvm_free_physmem_slot(&new, &old);
766out:
767 return r;
768}
769
770/*
771 * Get (and clear) the dirty memory log for a memory slot.
772 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200773static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
774 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775{
776 struct kvm_memory_slot *memslot;
777 int r, i;
778 int n;
Avi Kivity714b93d2007-01-05 16:36:53 -0800779 int cleared;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 unsigned long any = 0;
781
782 spin_lock(&kvm->lock);
783
784 /*
785 * Prevent changes to guest memory configuration even while the lock
786 * is not taken.
787 */
788 ++kvm->busy;
789 spin_unlock(&kvm->lock);
790 r = -EINVAL;
791 if (log->slot >= KVM_MEMORY_SLOTS)
792 goto out;
793
794 memslot = &kvm->memslots[log->slot];
795 r = -ENOENT;
796 if (!memslot->dirty_bitmap)
797 goto out;
798
Uri Lublincd1a4a92007-02-22 16:43:09 +0200799 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800
Uri Lublincd1a4a92007-02-22 16:43:09 +0200801 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 any = memslot->dirty_bitmap[i];
803
804 r = -EFAULT;
805 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
806 goto out;
807
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 if (any) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800809 cleared = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Avi Kivitybccf2152007-02-21 18:04:26 +0200811 struct kvm_vcpu *vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
Avi Kivitybccf2152007-02-21 18:04:26 +0200813 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 if (!vcpu)
815 continue;
Avi Kivity714b93d2007-01-05 16:36:53 -0800816 if (!cleared) {
817 do_remove_write_access(vcpu, log->slot);
818 memset(memslot->dirty_bitmap, 0, n);
819 cleared = 1;
820 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 kvm_arch_ops->tlb_flush(vcpu);
822 vcpu_put(vcpu);
823 }
824 }
825
826 r = 0;
827
828out:
829 spin_lock(&kvm->lock);
830 --kvm->busy;
831 spin_unlock(&kvm->lock);
832 return r;
833}
834
835struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
836{
837 int i;
838
839 for (i = 0; i < kvm->nmemslots; ++i) {
840 struct kvm_memory_slot *memslot = &kvm->memslots[i];
841
842 if (gfn >= memslot->base_gfn
843 && gfn < memslot->base_gfn + memslot->npages)
844 return memslot;
845 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000846 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847}
848EXPORT_SYMBOL_GPL(gfn_to_memslot);
849
850void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
851{
852 int i;
Al Viro8b6d44c2007-02-09 16:38:40 +0000853 struct kvm_memory_slot *memslot = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 unsigned long rel_gfn;
855
856 for (i = 0; i < kvm->nmemslots; ++i) {
857 memslot = &kvm->memslots[i];
858
859 if (gfn >= memslot->base_gfn
860 && gfn < memslot->base_gfn + memslot->npages) {
861
862 if (!memslot || !memslot->dirty_bitmap)
863 return;
864
865 rel_gfn = gfn - memslot->base_gfn;
866
867 /* avoid RMW */
868 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
869 set_bit(rel_gfn, memslot->dirty_bitmap);
870 return;
871 }
872 }
873}
874
875static int emulator_read_std(unsigned long addr,
876 unsigned long *val,
877 unsigned int bytes,
878 struct x86_emulate_ctxt *ctxt)
879{
880 struct kvm_vcpu *vcpu = ctxt->vcpu;
881 void *data = val;
882
883 while (bytes) {
884 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
885 unsigned offset = addr & (PAGE_SIZE-1);
886 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
887 unsigned long pfn;
888 struct kvm_memory_slot *memslot;
889 void *page;
890
891 if (gpa == UNMAPPED_GVA)
892 return X86EMUL_PROPAGATE_FAULT;
893 pfn = gpa >> PAGE_SHIFT;
894 memslot = gfn_to_memslot(vcpu->kvm, pfn);
895 if (!memslot)
896 return X86EMUL_UNHANDLEABLE;
897 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
898
899 memcpy(data, page + offset, tocopy);
900
901 kunmap_atomic(page, KM_USER0);
902
903 bytes -= tocopy;
904 data += tocopy;
905 addr += tocopy;
906 }
907
908 return X86EMUL_CONTINUE;
909}
910
911static int emulator_write_std(unsigned long addr,
912 unsigned long val,
913 unsigned int bytes,
914 struct x86_emulate_ctxt *ctxt)
915{
916 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
917 addr, bytes);
918 return X86EMUL_UNHANDLEABLE;
919}
920
921static int emulator_read_emulated(unsigned long addr,
922 unsigned long *val,
923 unsigned int bytes,
924 struct x86_emulate_ctxt *ctxt)
925{
926 struct kvm_vcpu *vcpu = ctxt->vcpu;
927
928 if (vcpu->mmio_read_completed) {
929 memcpy(val, vcpu->mmio_data, bytes);
930 vcpu->mmio_read_completed = 0;
931 return X86EMUL_CONTINUE;
932 } else if (emulator_read_std(addr, val, bytes, ctxt)
933 == X86EMUL_CONTINUE)
934 return X86EMUL_CONTINUE;
935 else {
936 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 if (gpa == UNMAPPED_GVA)
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200939 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 vcpu->mmio_needed = 1;
941 vcpu->mmio_phys_addr = gpa;
942 vcpu->mmio_size = bytes;
943 vcpu->mmio_is_write = 0;
944
945 return X86EMUL_UNHANDLEABLE;
946 }
947}
948
Avi Kivityda4a00f2007-01-05 16:36:44 -0800949static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
950 unsigned long val, int bytes)
951{
952 struct kvm_memory_slot *m;
953 struct page *page;
954 void *virt;
955
956 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
957 return 0;
958 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
959 if (!m)
960 return 0;
961 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
962 kvm_mmu_pre_write(vcpu, gpa, bytes);
Uri Lublinab51a432007-02-21 18:25:21 +0200963 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -0800964 virt = kmap_atomic(page, KM_USER0);
965 memcpy(virt + offset_in_page(gpa), &val, bytes);
966 kunmap_atomic(virt, KM_USER0);
967 kvm_mmu_post_write(vcpu, gpa, bytes);
968 return 1;
969}
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971static int emulator_write_emulated(unsigned long addr,
972 unsigned long val,
973 unsigned int bytes,
974 struct x86_emulate_ctxt *ctxt)
975{
976 struct kvm_vcpu *vcpu = ctxt->vcpu;
977 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
978
979 if (gpa == UNMAPPED_GVA)
980 return X86EMUL_PROPAGATE_FAULT;
981
Avi Kivityda4a00f2007-01-05 16:36:44 -0800982 if (emulator_write_phys(vcpu, gpa, val, bytes))
983 return X86EMUL_CONTINUE;
984
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 vcpu->mmio_needed = 1;
986 vcpu->mmio_phys_addr = gpa;
987 vcpu->mmio_size = bytes;
988 vcpu->mmio_is_write = 1;
989 memcpy(vcpu->mmio_data, &val, bytes);
990
991 return X86EMUL_CONTINUE;
992}
993
994static int emulator_cmpxchg_emulated(unsigned long addr,
995 unsigned long old,
996 unsigned long new,
997 unsigned int bytes,
998 struct x86_emulate_ctxt *ctxt)
999{
1000 static int reported;
1001
1002 if (!reported) {
1003 reported = 1;
1004 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1005 }
1006 return emulator_write_emulated(addr, new, bytes, ctxt);
1007}
1008
Avi Kivity32b35622007-01-05 16:36:51 -08001009#ifdef CONFIG_X86_32
1010
1011static int emulator_cmpxchg8b_emulated(unsigned long addr,
1012 unsigned long old_lo,
1013 unsigned long old_hi,
1014 unsigned long new_lo,
1015 unsigned long new_hi,
1016 struct x86_emulate_ctxt *ctxt)
1017{
1018 static int reported;
1019 int r;
1020
1021 if (!reported) {
1022 reported = 1;
1023 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1024 }
1025 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1026 if (r != X86EMUL_CONTINUE)
1027 return r;
1028 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1029}
1030
1031#endif
1032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1034{
1035 return kvm_arch_ops->get_segment_base(vcpu, seg);
1036}
1037
1038int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1039{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 return X86EMUL_CONTINUE;
1041}
1042
1043int emulate_clts(struct kvm_vcpu *vcpu)
1044{
Avi Kivity399badf2007-01-05 16:36:38 -08001045 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046
Avi Kivity399badf2007-01-05 16:36:38 -08001047 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1048 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 kvm_arch_ops->set_cr0(vcpu, cr0);
1050 return X86EMUL_CONTINUE;
1051}
1052
1053int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1054{
1055 struct kvm_vcpu *vcpu = ctxt->vcpu;
1056
1057 switch (dr) {
1058 case 0 ... 3:
1059 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1060 return X86EMUL_CONTINUE;
1061 default:
1062 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1063 __FUNCTION__, dr);
1064 return X86EMUL_UNHANDLEABLE;
1065 }
1066}
1067
1068int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1069{
1070 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1071 int exception;
1072
1073 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1074 if (exception) {
1075 /* FIXME: better handling */
1076 return X86EMUL_UNHANDLEABLE;
1077 }
1078 return X86EMUL_CONTINUE;
1079}
1080
1081static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1082{
1083 static int reported;
1084 u8 opcodes[4];
1085 unsigned long rip = ctxt->vcpu->rip;
1086 unsigned long rip_linear;
1087
1088 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1089
1090 if (reported)
1091 return;
1092
1093 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1094
1095 printk(KERN_ERR "emulation failed but !mmio_needed?"
1096 " rip %lx %02x %02x %02x %02x\n",
1097 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1098 reported = 1;
1099}
1100
1101struct x86_emulate_ops emulate_ops = {
1102 .read_std = emulator_read_std,
1103 .write_std = emulator_write_std,
1104 .read_emulated = emulator_read_emulated,
1105 .write_emulated = emulator_write_emulated,
1106 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity32b35622007-01-05 16:36:51 -08001107#ifdef CONFIG_X86_32
1108 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1109#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110};
1111
1112int emulate_instruction(struct kvm_vcpu *vcpu,
1113 struct kvm_run *run,
1114 unsigned long cr2,
1115 u16 error_code)
1116{
1117 struct x86_emulate_ctxt emulate_ctxt;
1118 int r;
1119 int cs_db, cs_l;
1120
1121 kvm_arch_ops->cache_regs(vcpu);
1122
1123 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1124
1125 emulate_ctxt.vcpu = vcpu;
1126 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1127 emulate_ctxt.cr2 = cr2;
1128 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1129 ? X86EMUL_MODE_REAL : cs_l
1130 ? X86EMUL_MODE_PROT64 : cs_db
1131 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1132
1133 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1134 emulate_ctxt.cs_base = 0;
1135 emulate_ctxt.ds_base = 0;
1136 emulate_ctxt.es_base = 0;
1137 emulate_ctxt.ss_base = 0;
1138 } else {
1139 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1140 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1141 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1142 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1143 }
1144
1145 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1146 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1147
1148 vcpu->mmio_is_write = 0;
1149 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1150
1151 if ((r || vcpu->mmio_is_write) && run) {
1152 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1153 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1154 run->mmio.len = vcpu->mmio_size;
1155 run->mmio.is_write = vcpu->mmio_is_write;
1156 }
1157
1158 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001159 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1160 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 if (!vcpu->mmio_needed) {
1162 report_emulation_failure(&emulate_ctxt);
1163 return EMULATE_FAIL;
1164 }
1165 return EMULATE_DO_MMIO;
1166 }
1167
1168 kvm_arch_ops->decache_regs(vcpu);
1169 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1170
1171 if (vcpu->mmio_is_write)
1172 return EMULATE_DO_MMIO;
1173
1174 return EMULATE_DONE;
1175}
1176EXPORT_SYMBOL_GPL(emulate_instruction);
1177
Avi Kivity270fd9b2007-02-19 14:37:47 +02001178int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1179{
1180 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1181
Dor Laor9b22bf52007-02-19 16:44:49 +02001182 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001183 ret = -KVM_EINVAL;
1184#ifdef CONFIG_X86_64
1185 if (is_long_mode(vcpu)) {
1186 nr = vcpu->regs[VCPU_REGS_RAX];
1187 a0 = vcpu->regs[VCPU_REGS_RDI];
1188 a1 = vcpu->regs[VCPU_REGS_RSI];
1189 a2 = vcpu->regs[VCPU_REGS_RDX];
1190 a3 = vcpu->regs[VCPU_REGS_RCX];
1191 a4 = vcpu->regs[VCPU_REGS_R8];
1192 a5 = vcpu->regs[VCPU_REGS_R9];
1193 } else
1194#endif
1195 {
1196 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1197 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1198 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1199 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1200 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1201 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1202 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1203 }
1204 switch (nr) {
1205 default:
1206 ;
1207 }
1208 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001209 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001210 return 1;
1211}
1212EXPORT_SYMBOL_GPL(kvm_hypercall);
1213
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1215{
1216 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1217}
1218
1219void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1220{
1221 struct descriptor_table dt = { limit, base };
1222
1223 kvm_arch_ops->set_gdt(vcpu, &dt);
1224}
1225
1226void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1227{
1228 struct descriptor_table dt = { limit, base };
1229
1230 kvm_arch_ops->set_idt(vcpu, &dt);
1231}
1232
1233void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1234 unsigned long *rflags)
1235{
1236 lmsw(vcpu, msw);
1237 *rflags = kvm_arch_ops->get_rflags(vcpu);
1238}
1239
1240unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1241{
Avi Kivity399badf2007-01-05 16:36:38 -08001242 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 switch (cr) {
1244 case 0:
1245 return vcpu->cr0;
1246 case 2:
1247 return vcpu->cr2;
1248 case 3:
1249 return vcpu->cr3;
1250 case 4:
1251 return vcpu->cr4;
1252 default:
1253 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1254 return 0;
1255 }
1256}
1257
1258void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1259 unsigned long *rflags)
1260{
1261 switch (cr) {
1262 case 0:
1263 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1264 *rflags = kvm_arch_ops->get_rflags(vcpu);
1265 break;
1266 case 2:
1267 vcpu->cr2 = val;
1268 break;
1269 case 3:
1270 set_cr3(vcpu, val);
1271 break;
1272 case 4:
1273 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1274 break;
1275 default:
1276 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1277 }
1278}
1279
Ingo Molnar102d8322007-02-19 14:37:47 +02001280/*
1281 * Register the para guest with the host:
1282 */
1283static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1284{
1285 struct kvm_vcpu_para_state *para_state;
1286 hpa_t para_state_hpa, hypercall_hpa;
1287 struct page *para_state_page;
1288 unsigned char *hypercall;
1289 gpa_t hypercall_gpa;
1290
1291 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1292 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1293
1294 /*
1295 * Needs to be page aligned:
1296 */
1297 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1298 goto err_gp;
1299
1300 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1301 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1302 if (is_error_hpa(para_state_hpa))
1303 goto err_gp;
1304
Uri Lublinab51a432007-02-21 18:25:21 +02001305 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001306 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1307 para_state = kmap_atomic(para_state_page, KM_USER0);
1308
1309 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1310 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1311
1312 para_state->host_version = KVM_PARA_API_VERSION;
1313 /*
1314 * We cannot support guests that try to register themselves
1315 * with a newer API version than the host supports:
1316 */
1317 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1318 para_state->ret = -KVM_EINVAL;
1319 goto err_kunmap_skip;
1320 }
1321
1322 hypercall_gpa = para_state->hypercall_gpa;
1323 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1324 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1325 if (is_error_hpa(hypercall_hpa)) {
1326 para_state->ret = -KVM_EINVAL;
1327 goto err_kunmap_skip;
1328 }
1329
1330 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1331 vcpu->para_state_page = para_state_page;
1332 vcpu->para_state_gpa = para_state_gpa;
1333 vcpu->hypercall_gpa = hypercall_gpa;
1334
Uri Lublinab51a432007-02-21 18:25:21 +02001335 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001336 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1337 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1338 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1339 kunmap_atomic(hypercall, KM_USER1);
1340
1341 para_state->ret = 0;
1342err_kunmap_skip:
1343 kunmap_atomic(para_state, KM_USER0);
1344 return 0;
1345err_gp:
1346 return 1;
1347}
1348
Avi Kivity3bab1f52006-12-29 16:49:48 -08001349int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1350{
1351 u64 data;
1352
1353 switch (msr) {
1354 case 0xc0010010: /* SYSCFG */
1355 case 0xc0010015: /* HWCR */
1356 case MSR_IA32_PLATFORM_ID:
1357 case MSR_IA32_P5_MC_ADDR:
1358 case MSR_IA32_P5_MC_TYPE:
1359 case MSR_IA32_MC0_CTL:
1360 case MSR_IA32_MCG_STATUS:
1361 case MSR_IA32_MCG_CAP:
1362 case MSR_IA32_MC0_MISC:
1363 case MSR_IA32_MC0_MISC+4:
1364 case MSR_IA32_MC0_MISC+8:
1365 case MSR_IA32_MC0_MISC+12:
1366 case MSR_IA32_MC0_MISC+16:
1367 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001368 case MSR_IA32_PERF_STATUS:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001369 /* MTRR registers */
1370 case 0xfe:
1371 case 0x200 ... 0x2ff:
1372 data = 0;
1373 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001374 case 0xcd: /* fsb frequency */
1375 data = 3;
1376 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001377 case MSR_IA32_APICBASE:
1378 data = vcpu->apic_base;
1379 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001380 case MSR_IA32_MISC_ENABLE:
1381 data = vcpu->ia32_misc_enable_msr;
1382 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001383#ifdef CONFIG_X86_64
1384 case MSR_EFER:
1385 data = vcpu->shadow_efer;
1386 break;
1387#endif
1388 default:
1389 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1390 return 1;
1391 }
1392 *pdata = data;
1393 return 0;
1394}
1395EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1396
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397/*
1398 * Reads an msr value (of 'msr_index') into 'pdata'.
1399 * Returns 0 on success, non-0 otherwise.
1400 * Assumes vcpu_load() was already called.
1401 */
1402static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1403{
1404 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1405}
1406
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001407#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408
Avi Kivity3bab1f52006-12-29 16:49:48 -08001409static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 if (efer & EFER_RESERVED_BITS) {
1412 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1413 efer);
1414 inject_gp(vcpu);
1415 return;
1416 }
1417
1418 if (is_paging(vcpu)
1419 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1420 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1421 inject_gp(vcpu);
1422 return;
1423 }
1424
Avi Kivity7725f0b2006-12-13 00:34:01 -08001425 kvm_arch_ops->set_efer(vcpu, efer);
1426
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427 efer &= ~EFER_LMA;
1428 efer |= vcpu->shadow_efer & EFER_LMA;
1429
1430 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
1433#endif
1434
Avi Kivity3bab1f52006-12-29 16:49:48 -08001435int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1436{
1437 switch (msr) {
1438#ifdef CONFIG_X86_64
1439 case MSR_EFER:
1440 set_efer(vcpu, data);
1441 break;
1442#endif
1443 case MSR_IA32_MC0_STATUS:
1444 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1445 __FUNCTION__, data);
1446 break;
1447 case MSR_IA32_UCODE_REV:
1448 case MSR_IA32_UCODE_WRITE:
1449 case 0x200 ... 0x2ff: /* MTRRs */
1450 break;
1451 case MSR_IA32_APICBASE:
1452 vcpu->apic_base = data;
1453 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001454 case MSR_IA32_MISC_ENABLE:
1455 vcpu->ia32_misc_enable_msr = data;
1456 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001457 /*
1458 * This is the 'probe whether the host is KVM' logic:
1459 */
1460 case MSR_KVM_API_MAGIC:
1461 return vcpu_register_para(vcpu, data);
1462
Avi Kivity3bab1f52006-12-29 16:49:48 -08001463 default:
1464 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1465 return 1;
1466 }
1467 return 0;
1468}
1469EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1470
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471/*
1472 * Writes msr value into into the appropriate "register".
1473 * Returns 0 on success, non-0 otherwise.
1474 * Assumes vcpu_load() was already called.
1475 */
1476static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1477{
1478 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1479}
1480
1481void kvm_resched(struct kvm_vcpu *vcpu)
1482{
1483 vcpu_put(vcpu);
1484 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001485 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486}
1487EXPORT_SYMBOL_GPL(kvm_resched);
1488
1489void load_msrs(struct vmx_msr_entry *e, int n)
1490{
1491 int i;
1492
1493 for (i = 0; i < n; ++i)
1494 wrmsrl(e[i].index, e[i].data);
1495}
1496EXPORT_SYMBOL_GPL(load_msrs);
1497
1498void save_msrs(struct vmx_msr_entry *e, int n)
1499{
1500 int i;
1501
1502 for (i = 0; i < n; ++i)
1503 rdmsrl(e[i].index, e[i].data);
1504}
1505EXPORT_SYMBOL_GPL(save_msrs);
1506
Avi Kivity06465c52007-02-28 20:46:53 +02001507void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1508{
1509 int i;
1510 u32 function;
1511 struct kvm_cpuid_entry *e, *best;
1512
1513 kvm_arch_ops->cache_regs(vcpu);
1514 function = vcpu->regs[VCPU_REGS_RAX];
1515 vcpu->regs[VCPU_REGS_RAX] = 0;
1516 vcpu->regs[VCPU_REGS_RBX] = 0;
1517 vcpu->regs[VCPU_REGS_RCX] = 0;
1518 vcpu->regs[VCPU_REGS_RDX] = 0;
1519 best = NULL;
1520 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1521 e = &vcpu->cpuid_entries[i];
1522 if (e->function == function) {
1523 best = e;
1524 break;
1525 }
1526 /*
1527 * Both basic or both extended?
1528 */
1529 if (((e->function ^ function) & 0x80000000) == 0)
1530 if (!best || e->function > best->function)
1531 best = e;
1532 }
1533 if (best) {
1534 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1535 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1536 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1537 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1538 }
1539 kvm_arch_ops->decache_regs(vcpu);
1540 kvm_arch_ops->skip_emulated_instruction(vcpu);
1541}
1542EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1543
Avi Kivity46fc1472007-02-22 19:39:30 +02001544static void complete_pio(struct kvm_vcpu *vcpu)
1545{
1546 struct kvm_io *io = &vcpu->run->io;
1547 long delta;
1548
1549 kvm_arch_ops->cache_regs(vcpu);
1550
1551 if (!io->string) {
1552 if (io->direction == KVM_EXIT_IO_IN)
1553 memcpy(&vcpu->regs[VCPU_REGS_RAX], &io->value,
1554 io->size);
1555 } else {
1556 delta = 1;
1557 if (io->rep) {
1558 delta *= io->count;
1559 /*
1560 * The size of the register should really depend on
1561 * current address size.
1562 */
1563 vcpu->regs[VCPU_REGS_RCX] -= delta;
1564 }
1565 if (io->string_down)
1566 delta = -delta;
1567 delta *= io->size;
1568 if (io->direction == KVM_EXIT_IO_IN)
1569 vcpu->regs[VCPU_REGS_RDI] += delta;
1570 else
1571 vcpu->regs[VCPU_REGS_RSI] += delta;
1572 }
1573
1574 vcpu->pio_pending = 0;
1575 vcpu->run->io_completed = 0;
1576
1577 kvm_arch_ops->decache_regs(vcpu);
1578
1579 kvm_arch_ops->skip_emulated_instruction(vcpu);
1580}
1581
Avi Kivitybccf2152007-02-21 18:04:26 +02001582static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 int r;
1585
Avi Kivitybccf2152007-02-21 18:04:26 +02001586 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587
Dor Laor54810342007-02-12 00:54:39 -08001588 /* re-sync apic's tpr */
1589 vcpu->cr8 = kvm_run->cr8;
1590
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591 if (kvm_run->emulated) {
1592 kvm_arch_ops->skip_emulated_instruction(vcpu);
1593 kvm_run->emulated = 0;
1594 }
1595
Avi Kivity46fc1472007-02-22 19:39:30 +02001596 if (kvm_run->io_completed) {
1597 if (vcpu->pio_pending)
1598 complete_pio(vcpu);
1599 else {
1600 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1601 vcpu->mmio_read_completed = 1;
1602 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 }
1604
1605 vcpu->mmio_needed = 0;
1606
1607 r = kvm_arch_ops->run(vcpu, kvm_run);
1608
1609 vcpu_put(vcpu);
1610 return r;
1611}
1612
Avi Kivitybccf2152007-02-21 18:04:26 +02001613static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1614 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615{
Avi Kivitybccf2152007-02-21 18:04:26 +02001616 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617
1618 kvm_arch_ops->cache_regs(vcpu);
1619
1620 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1621 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1622 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1623 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1624 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1625 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1626 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1627 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001628#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1630 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1631 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1632 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1633 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1634 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1635 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1636 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1637#endif
1638
1639 regs->rip = vcpu->rip;
1640 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1641
1642 /*
1643 * Don't leak debug flags in case they were set for guest debugging
1644 */
1645 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1646 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1647
1648 vcpu_put(vcpu);
1649
1650 return 0;
1651}
1652
Avi Kivitybccf2152007-02-21 18:04:26 +02001653static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1654 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655{
Avi Kivitybccf2152007-02-21 18:04:26 +02001656 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657
1658 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1659 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1660 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1661 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1662 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1663 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1664 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1665 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001666#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1668 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1669 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1670 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1671 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1672 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1673 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1674 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1675#endif
1676
1677 vcpu->rip = regs->rip;
1678 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1679
1680 kvm_arch_ops->decache_regs(vcpu);
1681
1682 vcpu_put(vcpu);
1683
1684 return 0;
1685}
1686
1687static void get_segment(struct kvm_vcpu *vcpu,
1688 struct kvm_segment *var, int seg)
1689{
1690 return kvm_arch_ops->get_segment(vcpu, var, seg);
1691}
1692
Avi Kivitybccf2152007-02-21 18:04:26 +02001693static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1694 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 struct descriptor_table dt;
1697
Avi Kivitybccf2152007-02-21 18:04:26 +02001698 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699
1700 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1701 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1702 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1703 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1704 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1705 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1706
1707 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1708 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1709
1710 kvm_arch_ops->get_idt(vcpu, &dt);
1711 sregs->idt.limit = dt.limit;
1712 sregs->idt.base = dt.base;
1713 kvm_arch_ops->get_gdt(vcpu, &dt);
1714 sregs->gdt.limit = dt.limit;
1715 sregs->gdt.base = dt.base;
1716
Avi Kivity399badf2007-01-05 16:36:38 -08001717 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 sregs->cr0 = vcpu->cr0;
1719 sregs->cr2 = vcpu->cr2;
1720 sregs->cr3 = vcpu->cr3;
1721 sregs->cr4 = vcpu->cr4;
1722 sregs->cr8 = vcpu->cr8;
1723 sregs->efer = vcpu->shadow_efer;
1724 sregs->apic_base = vcpu->apic_base;
1725
1726 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1727 sizeof sregs->interrupt_bitmap);
1728
1729 vcpu_put(vcpu);
1730
1731 return 0;
1732}
1733
1734static void set_segment(struct kvm_vcpu *vcpu,
1735 struct kvm_segment *var, int seg)
1736{
1737 return kvm_arch_ops->set_segment(vcpu, var, seg);
1738}
1739
Avi Kivitybccf2152007-02-21 18:04:26 +02001740static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1741 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 int mmu_reset_needed = 0;
1744 int i;
1745 struct descriptor_table dt;
1746
Avi Kivitybccf2152007-02-21 18:04:26 +02001747 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748
1749 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1750 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1751 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1752 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1753 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1754 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1755
1756 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1757 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1758
1759 dt.limit = sregs->idt.limit;
1760 dt.base = sregs->idt.base;
1761 kvm_arch_ops->set_idt(vcpu, &dt);
1762 dt.limit = sregs->gdt.limit;
1763 dt.base = sregs->gdt.base;
1764 kvm_arch_ops->set_gdt(vcpu, &dt);
1765
1766 vcpu->cr2 = sregs->cr2;
1767 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1768 vcpu->cr3 = sregs->cr3;
1769
1770 vcpu->cr8 = sregs->cr8;
1771
1772 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001773#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1775#endif
1776 vcpu->apic_base = sregs->apic_base;
1777
Avi Kivity399badf2007-01-05 16:36:38 -08001778 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1779
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1781 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1782
1783 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1784 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08001785 if (!is_long_mode(vcpu) && is_pae(vcpu))
1786 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787
1788 if (mmu_reset_needed)
1789 kvm_mmu_reset_context(vcpu);
1790
1791 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1792 sizeof vcpu->irq_pending);
1793 vcpu->irq_summary = 0;
1794 for (i = 0; i < NR_IRQ_WORDS; ++i)
1795 if (vcpu->irq_pending[i])
1796 __set_bit(i, &vcpu->irq_summary);
1797
1798 vcpu_put(vcpu);
1799
1800 return 0;
1801}
1802
1803/*
1804 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1805 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08001806 *
1807 * This list is modified at module load time to reflect the
1808 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 */
1810static u32 msrs_to_save[] = {
1811 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1812 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001813#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1815#endif
1816 MSR_IA32_TIME_STAMP_COUNTER,
1817};
1818
Michael Riepebf591b22006-12-22 01:05:36 -08001819static unsigned num_msrs_to_save;
1820
Avi Kivity6f00e682007-01-26 00:56:40 -08001821static u32 emulated_msrs[] = {
1822 MSR_IA32_MISC_ENABLE,
1823};
1824
Michael Riepebf591b22006-12-22 01:05:36 -08001825static __init void kvm_init_msr_list(void)
1826{
1827 u32 dummy[2];
1828 unsigned i, j;
1829
1830 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1831 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1832 continue;
1833 if (j < i)
1834 msrs_to_save[j] = msrs_to_save[i];
1835 j++;
1836 }
1837 num_msrs_to_save = j;
1838}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839
1840/*
1841 * Adapt set_msr() to msr_io()'s calling convention
1842 */
1843static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1844{
1845 return set_msr(vcpu, index, *data);
1846}
1847
1848/*
1849 * Read or write a bunch of msrs. All parameters are kernel addresses.
1850 *
1851 * @return number of msrs set successfully.
1852 */
Avi Kivitybccf2152007-02-21 18:04:26 +02001853static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854 struct kvm_msr_entry *entries,
1855 int (*do_msr)(struct kvm_vcpu *vcpu,
1856 unsigned index, u64 *data))
1857{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 int i;
1859
Avi Kivitybccf2152007-02-21 18:04:26 +02001860 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861
1862 for (i = 0; i < msrs->nmsrs; ++i)
1863 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1864 break;
1865
1866 vcpu_put(vcpu);
1867
1868 return i;
1869}
1870
1871/*
1872 * Read or write a bunch of msrs. Parameters are user addresses.
1873 *
1874 * @return number of msrs set successfully.
1875 */
Avi Kivitybccf2152007-02-21 18:04:26 +02001876static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 int (*do_msr)(struct kvm_vcpu *vcpu,
1878 unsigned index, u64 *data),
1879 int writeback)
1880{
1881 struct kvm_msrs msrs;
1882 struct kvm_msr_entry *entries;
1883 int r, n;
1884 unsigned size;
1885
1886 r = -EFAULT;
1887 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1888 goto out;
1889
1890 r = -E2BIG;
1891 if (msrs.nmsrs >= MAX_IO_MSRS)
1892 goto out;
1893
1894 r = -ENOMEM;
1895 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1896 entries = vmalloc(size);
1897 if (!entries)
1898 goto out;
1899
1900 r = -EFAULT;
1901 if (copy_from_user(entries, user_msrs->entries, size))
1902 goto out_free;
1903
Avi Kivitybccf2152007-02-21 18:04:26 +02001904 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 if (r < 0)
1906 goto out_free;
1907
1908 r = -EFAULT;
1909 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1910 goto out_free;
1911
1912 r = n;
1913
1914out_free:
1915 vfree(entries);
1916out:
1917 return r;
1918}
1919
1920/*
1921 * Translate a guest virtual address to a guest physical address.
1922 */
Avi Kivitybccf2152007-02-21 18:04:26 +02001923static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1924 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925{
1926 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 gpa_t gpa;
1928
Avi Kivitybccf2152007-02-21 18:04:26 +02001929 vcpu_load(vcpu);
1930 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1932 tr->physical_address = gpa;
1933 tr->valid = gpa != UNMAPPED_GVA;
1934 tr->writeable = 1;
1935 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02001936 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 vcpu_put(vcpu);
1938
1939 return 0;
1940}
1941
Avi Kivitybccf2152007-02-21 18:04:26 +02001942static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1943 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945 if (irq->irq < 0 || irq->irq >= 256)
1946 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02001947 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948
1949 set_bit(irq->irq, vcpu->irq_pending);
1950 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1951
1952 vcpu_put(vcpu);
1953
1954 return 0;
1955}
1956
Avi Kivitybccf2152007-02-21 18:04:26 +02001957static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1958 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 int r;
1961
Avi Kivitybccf2152007-02-21 18:04:26 +02001962 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963
1964 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1965
1966 vcpu_put(vcpu);
1967
1968 return r;
1969}
1970
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001971static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
1972 unsigned long address,
1973 int *type)
1974{
1975 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1976 unsigned long pgoff;
1977 struct page *page;
1978
1979 *type = VM_FAULT_MINOR;
1980 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1981 if (pgoff != 0)
1982 return NOPAGE_SIGBUS;
1983 page = virt_to_page(vcpu->run);
1984 get_page(page);
1985 return page;
1986}
1987
1988static struct vm_operations_struct kvm_vcpu_vm_ops = {
1989 .nopage = kvm_vcpu_nopage,
1990};
1991
1992static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1993{
1994 vma->vm_ops = &kvm_vcpu_vm_ops;
1995 return 0;
1996}
1997
Avi Kivitybccf2152007-02-21 18:04:26 +02001998static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1999{
2000 struct kvm_vcpu *vcpu = filp->private_data;
2001
2002 fput(vcpu->kvm->filp);
2003 return 0;
2004}
2005
2006static struct file_operations kvm_vcpu_fops = {
2007 .release = kvm_vcpu_release,
2008 .unlocked_ioctl = kvm_vcpu_ioctl,
2009 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002010 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002011};
2012
2013/*
2014 * Allocates an inode for the vcpu.
2015 */
2016static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2017{
2018 int fd, r;
2019 struct inode *inode;
2020 struct file *file;
2021
2022 atomic_inc(&vcpu->kvm->filp->f_count);
2023 inode = kvmfs_inode(&kvm_vcpu_fops);
2024 if (IS_ERR(inode)) {
2025 r = PTR_ERR(inode);
2026 goto out1;
2027 }
2028
2029 file = kvmfs_file(inode, vcpu);
2030 if (IS_ERR(file)) {
2031 r = PTR_ERR(file);
2032 goto out2;
2033 }
2034
2035 r = get_unused_fd();
2036 if (r < 0)
2037 goto out3;
2038 fd = r;
2039 fd_install(fd, file);
2040
2041 return fd;
2042
2043out3:
2044 fput(file);
2045out2:
2046 iput(inode);
2047out1:
2048 fput(vcpu->kvm->filp);
2049 return r;
2050}
2051
Avi Kivityc5ea7662007-02-20 18:41:05 +02002052/*
2053 * Creates some virtual cpus. Good luck creating more than one.
2054 */
2055static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2056{
2057 int r;
2058 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002059 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002060
2061 r = -EINVAL;
2062 if (!valid_vcpu(n))
2063 goto out;
2064
2065 vcpu = &kvm->vcpus[n];
2066
2067 mutex_lock(&vcpu->mutex);
2068
2069 if (vcpu->vmcs) {
2070 mutex_unlock(&vcpu->mutex);
2071 return -EEXIST;
2072 }
2073
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002074 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2075 r = -ENOMEM;
2076 if (!page)
2077 goto out_unlock;
2078 vcpu->run = page_address(page);
2079
Avi Kivityc5ea7662007-02-20 18:41:05 +02002080 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2081 FX_IMAGE_ALIGN);
2082 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2083
2084 r = kvm_arch_ops->vcpu_create(vcpu);
2085 if (r < 0)
2086 goto out_free_vcpus;
2087
2088 r = kvm_mmu_create(vcpu);
2089 if (r < 0)
2090 goto out_free_vcpus;
2091
2092 kvm_arch_ops->vcpu_load(vcpu);
2093 r = kvm_mmu_setup(vcpu);
2094 if (r >= 0)
2095 r = kvm_arch_ops->vcpu_setup(vcpu);
2096 vcpu_put(vcpu);
2097
2098 if (r < 0)
2099 goto out_free_vcpus;
2100
Avi Kivitybccf2152007-02-21 18:04:26 +02002101 r = create_vcpu_fd(vcpu);
2102 if (r < 0)
2103 goto out_free_vcpus;
2104
2105 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002106
2107out_free_vcpus:
2108 kvm_free_vcpu(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002109out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002110 mutex_unlock(&vcpu->mutex);
2111out:
2112 return r;
2113}
2114
Avi Kivity06465c52007-02-28 20:46:53 +02002115static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2116 struct kvm_cpuid *cpuid,
2117 struct kvm_cpuid_entry __user *entries)
2118{
2119 int r;
2120
2121 r = -E2BIG;
2122 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2123 goto out;
2124 r = -EFAULT;
2125 if (copy_from_user(&vcpu->cpuid_entries, entries,
2126 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2127 goto out;
2128 vcpu->cpuid_nent = cpuid->nent;
2129 return 0;
2130
2131out:
2132 return r;
2133}
2134
Avi Kivitybccf2152007-02-21 18:04:26 +02002135static long kvm_vcpu_ioctl(struct file *filp,
2136 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137{
Avi Kivitybccf2152007-02-21 18:04:26 +02002138 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002139 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 int r = -EINVAL;
2141
2142 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002143 case KVM_RUN:
2144 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 case KVM_GET_REGS: {
2147 struct kvm_regs kvm_regs;
2148
Avi Kivitybccf2152007-02-21 18:04:26 +02002149 memset(&kvm_regs, 0, sizeof kvm_regs);
2150 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 if (r)
2152 goto out;
2153 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002154 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 goto out;
2156 r = 0;
2157 break;
2158 }
2159 case KVM_SET_REGS: {
2160 struct kvm_regs kvm_regs;
2161
2162 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002163 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002165 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166 if (r)
2167 goto out;
2168 r = 0;
2169 break;
2170 }
2171 case KVM_GET_SREGS: {
2172 struct kvm_sregs kvm_sregs;
2173
Avi Kivitybccf2152007-02-21 18:04:26 +02002174 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2175 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176 if (r)
2177 goto out;
2178 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002179 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 goto out;
2181 r = 0;
2182 break;
2183 }
2184 case KVM_SET_SREGS: {
2185 struct kvm_sregs kvm_sregs;
2186
2187 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002188 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002190 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 if (r)
2192 goto out;
2193 r = 0;
2194 break;
2195 }
2196 case KVM_TRANSLATE: {
2197 struct kvm_translation tr;
2198
2199 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002200 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002202 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 if (r)
2204 goto out;
2205 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002206 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 goto out;
2208 r = 0;
2209 break;
2210 }
2211 case KVM_INTERRUPT: {
2212 struct kvm_interrupt irq;
2213
2214 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002215 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002217 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218 if (r)
2219 goto out;
2220 r = 0;
2221 break;
2222 }
2223 case KVM_DEBUG_GUEST: {
2224 struct kvm_debug_guest dbg;
2225
2226 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002227 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002229 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230 if (r)
2231 goto out;
2232 r = 0;
2233 break;
2234 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002235 case KVM_GET_MSRS:
2236 r = msr_io(vcpu, argp, get_msr, 1);
2237 break;
2238 case KVM_SET_MSRS:
2239 r = msr_io(vcpu, argp, do_set_msr, 0);
2240 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002241 case KVM_SET_CPUID: {
2242 struct kvm_cpuid __user *cpuid_arg = argp;
2243 struct kvm_cpuid cpuid;
2244
2245 r = -EFAULT;
2246 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2247 goto out;
2248 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2249 if (r)
2250 goto out;
2251 break;
2252 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002253 default:
2254 ;
2255 }
2256out:
2257 return r;
2258}
2259
2260static long kvm_vm_ioctl(struct file *filp,
2261 unsigned int ioctl, unsigned long arg)
2262{
2263 struct kvm *kvm = filp->private_data;
2264 void __user *argp = (void __user *)arg;
2265 int r = -EINVAL;
2266
2267 switch (ioctl) {
2268 case KVM_CREATE_VCPU:
2269 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2270 if (r < 0)
2271 goto out;
2272 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 case KVM_SET_MEMORY_REGION: {
2274 struct kvm_memory_region kvm_mem;
2275
2276 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002277 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002279 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 if (r)
2281 goto out;
2282 break;
2283 }
2284 case KVM_GET_DIRTY_LOG: {
2285 struct kvm_dirty_log log;
2286
2287 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002288 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002290 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291 if (r)
2292 goto out;
2293 break;
2294 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002295 default:
2296 ;
2297 }
2298out:
2299 return r;
2300}
2301
2302static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2303 unsigned long address,
2304 int *type)
2305{
2306 struct kvm *kvm = vma->vm_file->private_data;
2307 unsigned long pgoff;
2308 struct kvm_memory_slot *slot;
2309 struct page *page;
2310
2311 *type = VM_FAULT_MINOR;
2312 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2313 slot = gfn_to_memslot(kvm, pgoff);
2314 if (!slot)
2315 return NOPAGE_SIGBUS;
2316 page = gfn_to_page(slot, pgoff);
2317 if (!page)
2318 return NOPAGE_SIGBUS;
2319 get_page(page);
2320 return page;
2321}
2322
2323static struct vm_operations_struct kvm_vm_vm_ops = {
2324 .nopage = kvm_vm_nopage,
2325};
2326
2327static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2328{
2329 vma->vm_ops = &kvm_vm_vm_ops;
2330 return 0;
2331}
2332
2333static struct file_operations kvm_vm_fops = {
2334 .release = kvm_vm_release,
2335 .unlocked_ioctl = kvm_vm_ioctl,
2336 .compat_ioctl = kvm_vm_ioctl,
2337 .mmap = kvm_vm_mmap,
2338};
2339
2340static int kvm_dev_ioctl_create_vm(void)
2341{
2342 int fd, r;
2343 struct inode *inode;
2344 struct file *file;
2345 struct kvm *kvm;
2346
2347 inode = kvmfs_inode(&kvm_vm_fops);
2348 if (IS_ERR(inode)) {
2349 r = PTR_ERR(inode);
2350 goto out1;
2351 }
2352
2353 kvm = kvm_create_vm();
2354 if (IS_ERR(kvm)) {
2355 r = PTR_ERR(kvm);
2356 goto out2;
2357 }
2358
2359 file = kvmfs_file(inode, kvm);
2360 if (IS_ERR(file)) {
2361 r = PTR_ERR(file);
2362 goto out3;
2363 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002364 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002365
2366 r = get_unused_fd();
2367 if (r < 0)
2368 goto out4;
2369 fd = r;
2370 fd_install(fd, file);
2371
2372 return fd;
2373
2374out4:
2375 fput(file);
2376out3:
2377 kvm_destroy_vm(kvm);
2378out2:
2379 iput(inode);
2380out1:
2381 return r;
2382}
2383
2384static long kvm_dev_ioctl(struct file *filp,
2385 unsigned int ioctl, unsigned long arg)
2386{
2387 void __user *argp = (void __user *)arg;
2388 int r = -EINVAL;
2389
2390 switch (ioctl) {
2391 case KVM_GET_API_VERSION:
2392 r = KVM_API_VERSION;
2393 break;
2394 case KVM_CREATE_VM:
2395 r = kvm_dev_ioctl_create_vm();
2396 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002398 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 struct kvm_msr_list msr_list;
2400 unsigned n;
2401
2402 r = -EFAULT;
2403 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2404 goto out;
2405 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002406 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2408 goto out;
2409 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002410 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 goto out;
2412 r = -EFAULT;
2413 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002414 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002416 if (copy_to_user(user_msr_list->indices
2417 + num_msrs_to_save * sizeof(u32),
2418 &emulated_msrs,
2419 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2420 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002422 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 }
2424 default:
2425 ;
2426 }
2427out:
2428 return r;
2429}
2430
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431static struct file_operations kvm_chardev_ops = {
2432 .open = kvm_dev_open,
2433 .release = kvm_dev_release,
2434 .unlocked_ioctl = kvm_dev_ioctl,
2435 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436};
2437
2438static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002439 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 "kvm",
2441 &kvm_chardev_ops,
2442};
2443
2444static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2445 void *v)
2446{
2447 if (val == SYS_RESTART) {
2448 /*
2449 * Some (well, at least mine) BIOSes hang on reboot if
2450 * in vmx root mode.
2451 */
2452 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Al Viro8b6d44c2007-02-09 16:38:40 +00002453 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 }
2455 return NOTIFY_OK;
2456}
2457
2458static struct notifier_block kvm_reboot_notifier = {
2459 .notifier_call = kvm_reboot,
2460 .priority = 0,
2461};
2462
Avi Kivity774c47f2007-02-12 00:54:47 -08002463/*
2464 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2465 * cached on it.
2466 */
2467static void decache_vcpus_on_cpu(int cpu)
2468{
2469 struct kvm *vm;
2470 struct kvm_vcpu *vcpu;
2471 int i;
2472
2473 spin_lock(&kvm_lock);
2474 list_for_each_entry(vm, &vm_list, vm_list)
2475 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2476 vcpu = &vm->vcpus[i];
2477 /*
2478 * If the vcpu is locked, then it is running on some
2479 * other cpu and therefore it is not cached on the
2480 * cpu in question.
2481 *
2482 * If it's not locked, check the last cpu it executed
2483 * on.
2484 */
2485 if (mutex_trylock(&vcpu->mutex)) {
2486 if (vcpu->cpu == cpu) {
2487 kvm_arch_ops->vcpu_decache(vcpu);
2488 vcpu->cpu = -1;
2489 }
2490 mutex_unlock(&vcpu->mutex);
2491 }
2492 }
2493 spin_unlock(&kvm_lock);
2494}
2495
2496static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2497 void *v)
2498{
2499 int cpu = (long)v;
2500
2501 switch (val) {
Jeremy Katz43934a32007-02-19 14:37:46 +02002502 case CPU_DOWN_PREPARE:
Avi Kivity774c47f2007-02-12 00:54:47 -08002503 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002504 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2505 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002506 decache_vcpus_on_cpu(cpu);
2507 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2508 NULL, 0, 1);
2509 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002510 case CPU_ONLINE:
2511 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2512 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002513 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2514 NULL, 0, 1);
2515 break;
2516 }
2517 return NOTIFY_OK;
2518}
2519
2520static struct notifier_block kvm_cpu_notifier = {
2521 .notifier_call = kvm_cpu_hotplug,
2522 .priority = 20, /* must be > scheduler priority */
2523};
2524
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525static __init void kvm_init_debug(void)
2526{
2527 struct kvm_stats_debugfs_item *p;
2528
Al Viro8b6d44c2007-02-09 16:38:40 +00002529 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 for (p = debugfs_entries; p->name; ++p)
2531 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2532 p->data);
2533}
2534
2535static void kvm_exit_debug(void)
2536{
2537 struct kvm_stats_debugfs_item *p;
2538
2539 for (p = debugfs_entries; p->name; ++p)
2540 debugfs_remove(p->dentry);
2541 debugfs_remove(debugfs_dir);
2542}
2543
Avi Kivity59ae6c62007-02-12 00:54:48 -08002544static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2545{
2546 decache_vcpus_on_cpu(raw_smp_processor_id());
Avi Kivity19d14082007-02-19 14:37:48 +02002547 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002548 return 0;
2549}
2550
2551static int kvm_resume(struct sys_device *dev)
2552{
Avi Kivity19d14082007-02-19 14:37:48 +02002553 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002554 return 0;
2555}
2556
2557static struct sysdev_class kvm_sysdev_class = {
2558 set_kset_name("kvm"),
2559 .suspend = kvm_suspend,
2560 .resume = kvm_resume,
2561};
2562
2563static struct sys_device kvm_sysdev = {
2564 .id = 0,
2565 .cls = &kvm_sysdev_class,
2566};
2567
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568hpa_t bad_page_address;
2569
Avi Kivity37e29d92007-02-20 14:07:37 +02002570static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2571 const char *dev_name, void *data, struct vfsmount *mnt)
2572{
Andrew Mortone9cdb1e2007-03-01 11:28:13 +02002573 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
Avi Kivity37e29d92007-02-20 14:07:37 +02002574}
2575
2576static struct file_system_type kvm_fs_type = {
2577 .name = "kvmfs",
2578 .get_sb = kvmfs_get_sb,
2579 .kill_sb = kill_anon_super,
2580};
2581
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2583{
2584 int r;
2585
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002586 if (kvm_arch_ops) {
2587 printk(KERN_ERR "kvm: already loaded the other module\n");
2588 return -EEXIST;
2589 }
2590
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002591 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 printk(KERN_ERR "kvm: no hardware support\n");
2593 return -EOPNOTSUPP;
2594 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002595 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 printk(KERN_ERR "kvm: disabled by bios\n");
2597 return -EOPNOTSUPP;
2598 }
2599
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002600 kvm_arch_ops = ops;
2601
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 r = kvm_arch_ops->hardware_setup();
2603 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02002604 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605
Al Viro8b6d44c2007-02-09 16:38:40 +00002606 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002607 r = register_cpu_notifier(&kvm_cpu_notifier);
2608 if (r)
2609 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 register_reboot_notifier(&kvm_reboot_notifier);
2611
Avi Kivity59ae6c62007-02-12 00:54:48 -08002612 r = sysdev_class_register(&kvm_sysdev_class);
2613 if (r)
2614 goto out_free_2;
2615
2616 r = sysdev_register(&kvm_sysdev);
2617 if (r)
2618 goto out_free_3;
2619
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 kvm_chardev_ops.owner = module;
2621
2622 r = misc_register(&kvm_dev);
2623 if (r) {
2624 printk (KERN_ERR "kvm: misc device register failed\n");
2625 goto out_free;
2626 }
2627
2628 return r;
2629
2630out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002631 sysdev_unregister(&kvm_sysdev);
2632out_free_3:
2633 sysdev_class_unregister(&kvm_sysdev_class);
2634out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002636 unregister_cpu_notifier(&kvm_cpu_notifier);
2637out_free_1:
Al Viro8b6d44c2007-02-09 16:38:40 +00002638 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02002640out:
2641 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 return r;
2643}
2644
2645void kvm_exit_arch(void)
2646{
2647 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002648 sysdev_unregister(&kvm_sysdev);
2649 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002651 unregister_cpu_notifier(&kvm_cpu_notifier);
Al Viro8b6d44c2007-02-09 16:38:40 +00002652 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002654 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655}
2656
2657static __init int kvm_init(void)
2658{
2659 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02002660 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661
Avi Kivity37e29d92007-02-20 14:07:37 +02002662 r = register_filesystem(&kvm_fs_type);
2663 if (r)
2664 goto out3;
2665
2666 kvmfs_mnt = kern_mount(&kvm_fs_type);
2667 r = PTR_ERR(kvmfs_mnt);
2668 if (IS_ERR(kvmfs_mnt))
2669 goto out2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 kvm_init_debug();
2671
Michael Riepebf591b22006-12-22 01:05:36 -08002672 kvm_init_msr_list();
2673
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2675 r = -ENOMEM;
2676 goto out;
2677 }
2678
2679 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2680 memset(__va(bad_page_address), 0, PAGE_SIZE);
2681
Avi Kivity58e690e2007-02-26 16:29:43 +02002682 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683
2684out:
2685 kvm_exit_debug();
Avi Kivity37e29d92007-02-20 14:07:37 +02002686 mntput(kvmfs_mnt);
2687out2:
2688 unregister_filesystem(&kvm_fs_type);
2689out3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 return r;
2691}
2692
2693static __exit void kvm_exit(void)
2694{
2695 kvm_exit_debug();
2696 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivity37e29d92007-02-20 14:07:37 +02002697 mntput(kvmfs_mnt);
2698 unregister_filesystem(&kvm_fs_type);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699}
2700
2701module_init(kvm_init)
2702module_exit(kvm_exit)
2703
2704EXPORT_SYMBOL_GPL(kvm_init_arch);
2705EXPORT_SYMBOL_GPL(kvm_exit_arch);